Customize module loading with ILuaModuleLoader
mainLua-CSharp provides the ILuaModuleLoader interface to control how require resolves modules. This mechanism runs before the standard Lua package.searchers.
LuaState.ModuleLoader: Set this property to provide a custom loader.FileModuleLoader: The default implementation that loads modules from Lua files.CompositeModuleLoader.Create(loader1, loader2, ...): Allows combining multiple loaders into a single search chain.LuaState.LoadedModules: Provides access to thepackage.loadedtable for cached modules.
public interface ILuaModuleLoader
{
bool Exists(string moduleName);
ValueTask<LuaModule> LoadAsync(string moduleName, CancellationToken cancellationToken = default);
}
// Usage example
state.ModuleLoader = CompositeModuleLoader.Create(
new CustomModuleLoader1(),
new CustomModuleLoader2()
);