❔ NLua csharp hooks

Hi. Im using NLua to embed Lua in my project. Rn i can replace default print method to use hooked console. How i can do same but for require method? in example it dosen't works
var script = """

require "chunk";

testPrint("hello world")
print('test');

""";

var chunk = """
function testPrint(arg)
print(arg .. "hook 2");
end
""";


var luaEngine = new NLua.Lua(true);
luaEngine.State.Encoding = Encoding.UTF8;

luaEngine.RegisterFunction("print", PrintHook<object>);
luaEngine.RegisterFunction("require", RequireHook);

try
{
var result = luaEngine.DoString(script, "Main.lua");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

static void PrintHook<T>(T text)
{
Console.WriteLine($"Hooked {text}");
}

LuaStatus RequireHook(string chunkName)
{
try
{
Console.WriteLine($"Hooked {chunkName}");
// not works
//return luaEngine.State.LoadString(chunk);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// fallback
return LuaStatus.OK;
}
var script = """

require "chunk";

testPrint("hello world")
print('test');

""";

var chunk = """
function testPrint(arg)
print(arg .. "hook 2");
end
""";


var luaEngine = new NLua.Lua(true);
luaEngine.State.Encoding = Encoding.UTF8;

luaEngine.RegisterFunction("print", PrintHook<object>);
luaEngine.RegisterFunction("require", RequireHook);

try
{
var result = luaEngine.DoString(script, "Main.lua");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

static void PrintHook<T>(T text)
{
Console.WriteLine($"Hooked {text}");
}

LuaStatus RequireHook(string chunkName)
{
try
{
Console.WriteLine($"Hooked {chunkName}");
// not works
//return luaEngine.State.LoadString(chunk);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// fallback
return LuaStatus.OK;
}
2 Replies
~FallenParadise~
~FallenParadise~OP2y ago
anyone?
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server