예제 #1
0
파일: Game.cs 프로젝트: amur-tiger/yu-gi-oh
        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"));
        }