public Game(Player player, Player enemy, IMessageLogger logger) { Player = player; Enemy = enemy; MessageLogger = logger; // Redefine import function to disable all lua imports, // practically sandboxing the lua scripts context.DoString("import = function () assert (false, \"illegal import\") end"); // Add some functions for the game logic context.RegisterFunction("addEventHandler", this, GetType().GetMethod(nameof(AddEventHandler))); context.RegisterFunction("triggerEvent", this, GetType().GetMethod(nameof(TriggerEvent))); context.RegisterFunction("print", MessageLogger, MessageLogger.GetType().GetMethod(nameof(IMessageLogger.Print))); context.RegisterFunction("debug", MessageLogger, MessageLogger.GetType().GetMethod(nameof(IMessageLogger.Debug))); // note: lua has an own assert function that throws an exception instead of continuing normally. //context.RegisterFunction("assert", MessageLogger, MessageLogger.GetType().GetMethod(nameof(IMessageLogger.Assert))); // Load the main game logic into the lua engine. context.DoFile(Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), "game.lua")); }