//returns false if game was busy
        public bool SaveLoadedFiles()
        {
            bool wasRunning = this.DebugEngine.CurrentState == DebugState.Running;

            if (wasRunning)
            {
                if (!GameLoopHook.PauseGame())
                {
                    return(false);
                }
            }

            string data = CreateFileString();

            BBLua.lua_newtable(this.L);
            int i = 1;

            foreach (string substr in data.SplitBy(15000)) //the limit in shok seems to be at ~ 2^14, otherwise crashes savegame loading
            {
                BBLua.lua_pushstring(this.L, substr);
                BBLua.lua_rawseti(this.L, -2, i);
                i++;
            }
            BBLua.lua_setglobal(this.L, "_LuaDebugger_FileData");

            if (wasRunning)
            {
                GameLoopHook.ResumeGame();
            }

            return(true);
        }
Example #2
0
        static DebuggerDllExports()
        {
            if (Environment.GetEnvironmentVariable("ldbWaitForDebugger") == "yes")
            {
                while (!Debugger.IsAttached)
                {
                    Thread.Sleep(1);
                }

                GlobalState.IsInVisualStudio = true;
            }

            if (!Application.ExecutablePath.ToLower().Contains(GlobalState.SettlersExe))
            {
                MessageBox.Show("This DLL only works with " + GlobalState.SettlersExe);
                Environment.Exit(0);
            }

            GlobalState.SettlersThread       = Thread.CurrentThread;
            GlobalState.SettlersWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
            //WindowStyle settlersWndStyle = (WindowStyle)WinAPI.GetWindowLong(GlobalState.SettlersWindowHandle, WinAPI.GWL_STYLE);
            //settlersWndStyle |= WindowStyle.WS_MINIMIZEBOX | WindowStyle.WS_SIZEBOX | WindowStyle.WS_MAXIMIZEBOX;
            //WinAPI.SetWindowLong(GlobalState.SettlersWindowHandle, WinAPI.GWL_STYLE, (uint)settlersWndStyle);

            //TickHook.InstallHook();
            GameLoopHook.InstallHook();
            ErrorHook.InstallHook();
            Thread uiThread = new Thread(new ThreadStart(DbgThread.RunMessageLoop));

            uiThread.SetApartmentState(ApartmentState.STA);
            uiThread.Start();
            GlobalState.UIThread = uiThread;
        }
        //returns false if game was busy
        public bool RestoreLoadedFiles()
        {
            bool wasRunning = this.DebugEngine.CurrentState == DebugState.Running;

            if (wasRunning)
            {
                if (!GameLoopHook.PauseGame())
                {
                    return(false);
                }
            }

            BBLua.lua_getglobal(this.L, "_LuaDebugger_FileData");
            if (BBLua.lua_type(this.L, -1) != LuaType.Table)
            {
                if (wasRunning)
                {
                    GameLoopHook.ResumeGame();
                }
                return(true);
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 1; ; i++)
            {
                BBLua.lua_rawgeti(this.L, -1, i);
                if (BBLua.lua_type(this.L, -1) != LuaType.String)
                {
                    BBLua.lua_settop(this.L, -2);
                    break;
                }
                sb.Append(BBLua.lua_tostring(this.L, -1));
                BBLua.lua_settop(this.L, -2);
            }

            if (wasRunning)
            {
                GameLoopHook.ResumeGame();
            }

            this.LoadedFiles.Clear();

            RestoreFromFileString(sb.ToString());

            return(true);
        }
        public bool RunDelegateSafely(MethodInvoker dlg)
        {
            bool unfreeze = false;

            if (this.CurrentState == DebugState.Running)
            {
                unfreeze = true;
                if (!GameLoopHook.PauseGame())
                {
                    return(false);
                }
            }
            this.DebugEngine.RemoveHook();

            dlg();

            this.DebugEngine.SetHook();
            if (unfreeze)
            {
                GameLoopHook.ResumeGame();
            }
            return(true);
        }
        public string EvaluateLua(string expression)
        {
            bool unfreeze = false;

            if (this.CurrentState == DebugState.Running)
            {
                unfreeze = true;
                if (!GameLoopHook.PauseGame())
                {
                    return("Error: Game is busy!");
                }
            }
            this.DebugEngine.RemoveHook();

            string asStatement = expression;

            expression = "return " + expression;

            string    result = "";
            LuaResult err    = BBLua.luaL_loadbuffer(this.L, expression, expression.Length, "from console");

            if (err == LuaResult.OK)
            {
                int stackTop = BBLua.lua_gettop(this.L);
                err = BBLua.lua_pcall(this.L, 0, -1, 0);
                int nResults = 1 + BBLua.lua_gettop(this.L) - stackTop;

                if (nResults == 1)
                {
                    result = TosToString();
                }
                else if (nResults > 1)
                {
                    string[] results = new string[nResults];
                    do
                    {
                        nResults--;
                        results[nResults] = TosToString(true);
                    } while (nResults != 0);

                    result = "(" + string.Join(", ", results) + ")";
                }
            }
            else
            {
                string parseErrExpr = TosToString();

                err = BBLua.luaL_loadbuffer(this.L, asStatement, asStatement.Length, "from console");
                if (err == LuaResult.OK)
                {
                    err = BBLua.lua_pcall(this.L, 0, 0, 0); //statement -> no return values
                }
                if (err != LuaResult.OK)
                {
                    result = TosToString();
                }
            }

            this.DebugEngine.SetHook();
            if (unfreeze)
            {
                GameLoopHook.ResumeGame();
            }

            return(result);
        }