internal ScriptDebugSession(int port, MoonSharpVsCodeDebugServer server, AsyncDebugger debugger) : base(port, server, debugger)
 {
     if (debugger == null)
     {
         throw new ArgumentNullException(nameof(debugger));
     }
 }
Example #2
0
        private static bool AwaitDebuggerAttach(MoonSharpVsCodeDebugServer server)
        {
            // as soon as a client has attached, 'm_Client__' field of 'm_Current' isn't null anymore
            //
            // we wait for ~60 seconds for a client to attach

            BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic;
            FieldInfo    field     = server.GetType().GetField("m_Current", bindFlags);
            object       current   = field.GetValue(server);

            FieldInfo property = current.GetType().GetField("m_Client__", bindFlags);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            Console.WriteLine("Waiting for VS Code debugger to attach");
            while (property.GetValue(current) == null)
            {
                Thread.Sleep(500);
                if (stopwatch.Elapsed.TotalSeconds > 60)
                {
                    return(false);
                }
            }
            stopwatch.Stop();
            Console.WriteLine("VS Code debugger attached");
            return(true);
        }
Example #3
0
 /// <summary>
 /// Starts the remote debugger and opens the interface in the users browser
 /// </summary>
 public static void StartRemoteDebugger()
 {
     if (s_RemoteDebugger == null)
     {
         s_RemoteDebugger = new MoonSharpVsCodeDebugServer();
         s_RemoteDebugger.Start();
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Path to lua script file: ");
            string path = Console.ReadLine();

            if (!File.Exists(path))
            {
                Console.WriteLine("File not found.");
                Console.ReadKey();
                return;
            }

            var script = new Script();

            script.Globals["WriteToConsole"] = new Func <string, int>(text =>
            {
                Console.WriteLine(text);
                return(text.Length);
            });



            MoonSharpVsCodeDebugServer server = new MoonSharpVsCodeDebugServer();

            server.Start();
            server.AttachToScript(script, "DebugScript");
            //server.AttachToScript(LuaFileManager.Instance.script, "DebugScript");

            // read script
            string scriptData = File.ReadAllText(path);

            script.DoString(scriptData, null, path);
            //LuaFileManager.Instance.script.DoString(scriptData, null, path);

            // wait for debugger to attach
            bool attached = AwaitDebuggerAttach(server);

            if (!attached)
            {
                Console.WriteLine("VS Code debugger did not attach. Running the script.");
            }

            DynValue className = script.Globals.Get("LuaTest2");
            DynValue newFunc   = className.Table.Get("new");
            DynValue classInst = script.Call(newFunc, className);

            //LuaFileInfo info = LuaFileManager.Instance.SingleGetInfo("test", "LuaTest2");
            //DynValue function = LuaFileManager.Instance.script.Globals.Get("LuaTest2").Table.Get("OnClickDoLocalMove");
            //LuaFileManager.Instance.script.Call(function, info.classInst);


            // run script's main function
            //script.Call(script.Globals["main"]);
            //script.Call(script.Globals["OnClickDoLocalMove"]);

            Console.ReadKey();
        }
Example #5
0
        private void SetupDebugEnvironment()
        {
            debug = true;

            if (debug)
            {
                server = new MoonSharpVsCodeDebugServer();
                server.Start();
            }
        }
Example #6
0
        public static MoonSharpVsCodeDebugServer GetServer()
        {
            if (server == null)
            {
                server = new MoonSharpVsCodeDebugServer();
                server.Start();
            }

            return(server);
        }
Example #7
0
        protected virtual void StartVSCodeDebugger()
        {
            if (DebugServer == null)
            {
                // Create the debugger server
                DebugServer = new MoonSharpVsCodeDebugServer();

                // Start the debugger server
                DebugServer.Start();
            }
        }
Example #8
0
        public static void Main(string[] argv)
        {
            MoonSharpVsCodeDebugServer server = new MoonSharpVsCodeDebugServer();

            server.Logger = s => Console.WriteLine(s);
            server.Start();

            Script script1 = new Script();

            script1.DoFile(@"Z:/HDD/temp/lua/fact.lua");
            server.AttachToScript(script1, "Script #1");
            Closure func1 = script1.Globals.Get("run").Function;

            Script script2 = new Script();

            script2.DoFile(@"Z:/HDD/temp/lua/fact2.lua");
            server.AttachToScript(script2, "Script #2");
            Closure func2 = script2.Globals.Get("run").Function;

            Console.WriteLine("READY.");
            int i = 0;

            server.Current = null;

            while (true)            //(Console.ReadKey().Key != ConsoleKey.Escape)
            {
                if (Console.KeyAvailable)
                {
                    Console.ReadKey();
                    server.Detach(script2);
                    Console.WriteLine("Detached");
                }

                Closure func = ((++i) % 2) == 0 ? func1 : func2;

                try
                {
                    var val = func.Call(5);
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine(val.Number);
                    System.Threading.Thread.Sleep(1000);
                }
                catch (InterpreterException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(ex.DecoratedMessage);
                }
            }
        }
Example #9
0
        private static void DebuggerMain(string[] args)
        {
            MoonSharpVsCodeDebugServer server = new MoonSharpVsCodeDebugServer().Start();
            Script s = new Script();

            server.AttachToScript(s, "xxx");

            DynValue func = s.DoString("return function()\nprint 'x';\nend;");

            while (!Console.KeyAvailable)
            {
                func.Function.Call();
                System.Threading.Tasks.Task.Delay(100).Wait();
            }

            Console.ReadKey();
        }
Example #10
0
    // Use this for initialization
    void Start()
    {
        server = new MoonSharpVsCodeDebugServer().Start();
        script = new Script();

        Script script1 = new Script();

        script1.DoString(@"

	function run()
		for i = 1, 4 do
			print (i)
		end
	end
");

        server.AttachToScript(script1, "Script #1");
        func = script1.Globals.Get("run").Function;
    }
Example #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Path to lua script file: ");
            string path = Console.ReadLine();

            if (!File.Exists(path))
            {
                Console.WriteLine("File not found.");
                Console.ReadKey();
                return;
            }

            var script = new Script();

            script.Globals["WriteToConsole"] = new Func <string, int>(text =>
            {
                Console.WriteLine(text);
                return(text.Length);
            });

            MoonSharpVsCodeDebugServer server = new MoonSharpVsCodeDebugServer();

            server.Start();
            server.AttachToScript(script, "DebugScript");

            // read script
            string scriptData = File.ReadAllText(path);

            script.DoString(scriptData, null, path);

            // wait for debugger to attach
            bool attached = AwaitDebuggerAttach(server);

            if (!attached)
            {
                Console.WriteLine("VS Code debugger did not attach. Running the script.");
            }

            // run script's main function
            script.Call(script.Globals["main"]);

            Console.ReadKey();
        }
Example #12
0
        /// <summary>
        /// Initialise the Lua interpreter so we can start running Lua code.
        /// </summary>
        public virtual void InitEnvironment()
        {
            if (initialised)
            {
                return;
            }

            Script.DefaultOptions.DebugPrint = (s) => { UnityEngine.Debug.Log(s); };

            // In some use cases (e.g. downloadable Lua files) some Lua modules can pose a potential security risk.
            // You can restrict which core lua modules are available here if needed. See the MoonSharp documentation for details.
            interpreter = new Script(CoreModules.Preset_Complete);

            // Load all Lua scripts in the project
            InitLuaScriptFiles();

            // Initialize any attached initializer components (e.g. LuaUtils)
            LuaEnvironmentInitializer[] initializers = GetComponents <LuaEnvironmentInitializer>();
            foreach (LuaEnvironmentInitializer initializer in initializers)
            {
                initializer.Initialize();
            }

            //
            // Change this to #if UNITY_STANDALONE if you want to debug a standalone build.
            //
            #if UNITY_EDITOR
            if (startDebugServer &&
                DebugServer == null)
            {
                // Create the debugger server
                DebugServer = new MoonSharpVsCodeDebugServer(debugServerPort);

                // Start the debugger server
                DebugServer.Start();

                // Attach the MoonSharp script to the debugger
                DebugServer.AttachToScript(interpreter, gameObject.name);
            }
            #endif

            initialised = true;
        }
Example #13
0
        public static void TestVsDebugger()
        {
            var script = new Script();

            script.Globals["print"] = new Func <string, int>(text =>
            {
                Console.WriteLine(text);
                return(text.Length);
            });
            //debug
            MoonSharpVsCodeDebugServer server = new MoonSharpVsCodeDebugServer();

            server.Start();
            server.AttachToScript(script, "DebugScript");

            /*
             * string scriptCode = @"
             *                  function main()
             *      local chars = """"
             *      chars = print(""Hello world"")
             *  end
             * ";
             * /**/
            //Console.WriteLine(scriptCode);

            string scriptCode = File.ReadAllText(@"X:\gitdir\TestMoonSharp\TestMoonSharp\Scripts\debug.lua");

            //Console.WriteLine(scriptCode);

            script.DoString(scriptCode, null, @"X:\gitdir\TestMoonSharp\TestMoonSharp\Scripts\debug.lua");
            // wait for debugger to attach
            bool attached = AwaitDebuggerAttach(server);

            if (!attached)
            {
                Console.WriteLine("VS Code debugger did not attach. Running the script.");
            }


            script.Call(script.Globals["main"]);

            Console.ReadKey();
        }
Example #14
0
 internal EmptyDebugSession(MoonSharpVsCodeDebugServer server)
     : base(true, false)
 {
     m_Server = server;
 }
		internal MoonSharpDebugSession(MoonSharpVsCodeDebugServer server, AsyncDebugger debugger)
			: base(true, false)
		{
			m_Server = server;
			m_Debug = debugger;
		}
 internal MoonSharpDebugSession(int port, MoonSharpVsCodeDebugServer server, AsyncDebugger debugger)
 {
     Port     = port;
     Server   = server;
     Debugger = debugger;
 }
 internal MoonSharpDebugSession(MoonSharpVsCodeDebugServer server, AsyncDebugger debugger)
     : base(true, false)
 {
     m_Server = server;
     m_Debug  = debugger;
 }
		internal EmptyDebugSession(MoonSharpVsCodeDebugServer server)
			: base(true, false)
		{
			m_Server = server;
		}
Example #19
0
 internal DetachedDebugSession(int port, MoonSharpVsCodeDebugServer server) : base(port, server, null)
 {
 }