Example #1
0
 public void Setup()
 {
     lua                 = new Lua();
     luaEnv              = lua.CreateEnvironment();
     options             = new LuaCompileOptions();
     options.DebugEngine = new LuaStackTraceDebugger();
 }
Example #2
0
        public void TestMember10()
        {
            LuaGlobalPortable g = new LuaGlobalPortable(new Lua());

            g["a"] = 1;
            g.Members.Clear();

            TestResult(new LuaResult(g["a"]), new object[] { null });
        }
Example #3
0
 public void Setup()
 {
     IOC.Reset();
     lua       = new Lua();
     luaEnv    = lua.CreateEnvironment();
     apiBridge = Mock.Interface <IApiBridge>();
     IOC.Register <IApiBridge>(() => apiBridge);
     IOC.Register <ApiAdapter>(() => new ApiAdapter());
     api        = IOC.Resolve <ApiAdapter>();
     api.LuaEnv = luaEnv;
 }
Example #4
0
        private static void TestDynamic()
        {
            var lua    = new Lua();
            var global = new LuaGlobalPortable(lua)
            {
                ["workspace"] = new DynData()
            };

            var r = global.DoChunk("return workspace.Part", "Test.lua");

            Console.WriteLine(r.ToString());
        }
Example #5
0
        void LoadScene(string id)
        {
            if (lua != null)
            {
                lua.Dispose();
            }
            Elements = new List <XmlUIElement>();
            lua      = new Lua();
            env      = lua.CreateEnvironment();
            env.DoChunk("events = {}", "$internal");
            env.Add(apiname, api);
            env.Add("dom", new LuaDom(this));
            env.Add("sound", new LuaSound(this));
            LuaStyleEnvironment.RegisterFuncs(env);
            _g = (dynamic)env;
            var scn = xml.Scenes.Where((x) => x.ID == id).First();

            if (scn.Scripts != null)
            {
                foreach (var script in scn.Scripts)
                {
                    env.DoChunk(script, "$xml");
                }
            }
            foreach (var item in scn.Items)
            {
                if (item is XInt.Button)
                {
                    var btn = (XInt.Button)item;
                    Elements.Add(new XmlUIButton(this, btn, styles.Where((x) => x.ID == btn.Style).First()));
                }
                else if (item is XInt.Image)
                {
                    Elements.Add(new XmlUIImage((XInt.Image)item, this));
                }
                else if (item is XInt.ServerList)
                {
                    var sl = (XInt.ServerList)item;
                    Elements.Add(new XmlUIServerList(sl, styles.Where((x) => x.ID == sl.Style).First(), this));
                }
                else if (item is XInt.Panel)
                {
                    var pnl = (XInt.Panel)item;
                    Elements.Add(new XmlUIPanel(pnl, styles.Where((x) => x.ID == pnl.Style).First(), this));
                }
                else if (item is XInt.ChatBox)
                {
                    var cb = (XInt.ChatBox)item;
                    Elements.Add(new XmlChatBox(cb, styles.Where((x) => x.ID == cb.Style).First(), this));
                }
            }
        }
 public void Reset(ref Lua context)
 {
     try
     {
         _global = context.CreateEnvironment();
         _chunks.Clear();
     }
     catch (Exception e)
     {
         DebugHelper.logError("Reset()", "Cannot reset LuaScriptEnvironment");
         throw e;
     }
 }
 public LuaScriptEnvironment(ref Lua context)
 {
     try
     {
         _global = context.CreateEnvironment();
         _chunks = new Dictionary <string, LuaChunk>();
     }
     catch (Exception e)
     {
         DebugHelper.logError("LuaScriptEnvironment()", e.Message);
         throw e;
     }
 }
Example #8
0
        public static void PublishObjectMethods(object instance, LuaGlobalPortable luaEnvironment)
        {
            var      dyn     = luaEnvironment as dynamic;
            var      methods = instance.GetType().GetMethods();
            Delegate theFunction;

            foreach (var m in methods)
            {
                if (m.ReturnType == typeof(void))
                {
                    theFunction = CreateAction(instance, m);
                }
                else
                {
                    theFunction = CreateFunc(instance, m);
                }
                dyn[m.Name] = theFunction;
            }
        }
Example #9
0
 public static void RegisterFuncs(LuaGlobalPortable gp)
 {
     gp.Add("mixcolor", (Func <float, string, string, Color4>)MixColor);
     gp.Add("color", (Func <string, Color4>)Color);
 }
Example #10
0
 static LuaStyleEnvironment()
 {
     L = new Lua();
     G = L.CreateEnvironment();
     RegisterFuncs(G);
 }
Example #11
0
        public static void LuaThread()
        {
            if (watcher == null)
            {
                Watch();
            }


            App.c = Chroma.Instance;
            App.c.Initialize();
            App.c.DeviceAccess += C_DeviceAccess;
            App.NewScriptsContext();
            // WE NEED TO ENSURE CHROMA IS INITIALISED
            callbacks = new List <dynamic>();

            var ms_luaDebug          = new LuaStackTraceDebugger();
            var ms_luaCompileOptions = new LuaCompileOptions();

            ms_luaCompileOptions.DebugEngine = ms_luaDebug;
            scriptThreads = new Collection <Thread>();

            string path = @"%appdata%\ChromaSync";

            path = Environment.ExpandEnvironmentVariables(path);

            string scriptsPath  = Path.Combine(path, "scripts");
            string packagesPath = Path.Combine(path, "packages");

            if (!Directory.Exists(scriptsPath))
            {
                Directory.CreateDirectory(scriptsPath);
            }


            // Todo: Get all scripts including the packages
            var files = Directory.GetFiles(path, "*.lua", SearchOption.AllDirectories);

            foreach (string st in files)
            {
                var      v        = RegistryKeeper.GetValue(st);
                MenuItem menuItem = new MenuItem(Path.GetFileName(st));
                menuItem.Name   = Path.GetFileName(st);
                menuItem.Tag    = st;
                menuItem.Click += MenuItem_Click;
                if (!st.Contains("\\ChromaSync\\packages\\"))
                {
                    App.scriptsMenu.MenuItems.Add(menuItem);
                }
                if (v.Equals("True"))
                {
                    menuItem.Checked = true;
                    scriptThreads.Add(
                        new Thread(() =>
                    {
                        using (Lua l = new Lua())
                        {
                            LuaGlobalPortable g = l.CreateEnvironment();
                            dynamic dg          = g;
                            dg.DebugLua         = new Func <object, bool>(debug);
                            dg.ConvertInt       = new Func <object, int>(convertInt);
                            dg.NewCustom        = new Func <string, Color, object>(newCustom);
                            dg.IntToByte        = new Func <int, byte>(IntToByte);
                            dg.Headset          = Headset.Instance;
                            dg.Keyboard         = Keyboard.Instance;
                            dg.Mouse            = Mouse.Instance;
                            dg.Keypad           = Keypad.Instance;
                            dg.Mousepad         = Mousepad.Instance;

                            dg.RegisterForEvents = new Func <string, object, bool>(registerEvents);
                            debug("starting Lua script: " + st);
                            try
                            {
                                LuaChunk compiled = l.CompileChunk(st, ms_luaCompileOptions);
                                var d             = g.DoChunk(compiled);
                            }
                            catch (LuaException e)
                            {
                                App.Log.Error(e);
                            }
                            catch (Exception e)
                            {
                                App.Log.Info(e);
                                Thread.ResetAbort();
                            }
                        }
                    }));
                    scriptThreads.Last().Start();
                }
            }
        }
Example #12
0
 public void Teardown()
 {
     luaEnv = null;
     lua.Dispose();
 }
Example #13
0
        /// <summary>
        /// Inits the Lua Engine
        /// </summary>
        public static bool Start()
        {
            ConsoleUtils.Write(ConsoleMsgType.Status, "Initializing Lua Engine...\n");

            LuaEngine = new Lua();
            Global    = LuaEngine.CreateEnvironment();

            // Register Functions
            GetFunctions();

            List <string> scriptFiles = new List <string>();

            if (!File.Exists("npc/scripts_main.txt"))
            {
                ConsoleUtils.Write(ConsoleMsgType.FatalError, "Could not find file npc/scripts_main.txt. Terminating...\n");
                return(false);
            }
            scriptFiles.AddRange(File.ReadAllLines("npc/scripts_main.txt"));

            StringBuilder chunkText = new StringBuilder();

            for (int i = 0; i < scriptFiles.Count; i++)
            {
                if (scriptFiles[i].StartsWith("//"))
                {                 // Ignore comments
                    continue;
                }
                else if (scriptFiles[i].StartsWith("npc:"))
                {                 // Loads a Lua
                    string fname = scriptFiles[i].Split(new char[] { ':' }, 2)[1].TrimStart(' ');
                    if (!File.Exists(fname))
                    {
                        ConsoleUtils.Write(ConsoleMsgType.Error, "Could not find file '{0}'...\n", fname);
                        continue;
                    }

                    chunkText.Append(" ");
                    chunkText.Append(File.ReadAllText(fname));
                }
                else if (scriptFiles[i].StartsWith("import:"))
                {                 // imports another txt
                    string fname = scriptFiles[i].Split(new char[] { ':' }, 2)[1].TrimStart(' ');
                    if (!File.Exists(fname))
                    {
                        ConsoleUtils.Write(ConsoleMsgType.Error, "Could not find file '{0}'...\n", fname);
                        continue;
                    }

                    scriptFiles.AddRange(File.ReadAllLines(fname));
                }
            }

            Chunk = LuaEngine.CompileChunk(chunkText.ToString(), "main", null);

            Global.DoChunk(Chunk);

            /*
             * var luaFunc = LuaSysGlobal["GetStruct"] as Func<LuaResult>;
             * if (luaFunc == null)
             *      return null;
             * LuaResult r = luaFunc();
             */

            ConsoleUtils.Write(ConsoleMsgType.Status, "Lua Engine initialized...\n");
            return(true);
        }
Example #14
0
 public void Load(Stream scriptSource)
 {
     api.Init();
     luaEnv  = myLua.CreateEnvironment();
     myChunk = myLua.CompileChunk(new StreamReader(scriptSource), "__mutagenScript", null);
 }
Example #15
0
        public static void LuaThread()
        {
            if (watcher == null)
            {
                Watch();
            }

            // WE NEED TO ENSURE CHROMA IS INITIALISED
            var c = Chroma.Instance;

            callbacks = new List <dynamic>();
            var ms_luaDebug          = new LuaStackTraceDebugger();
            var ms_luaCompileOptions = new LuaCompileOptions();

            ms_luaCompileOptions.DebugEngine = ms_luaDebug;
            scriptThreads = new Collection <Thread>();
            EventHook.MouseHook.MouseAction += new EventHandler(Event);

            string path = @"%appdata%\ChromaSync";

            path = Environment.ExpandEnvironmentVariables(path);

            path = Path.Combine(path, "scripts");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (string st in Directory.GetFiles(path, "*_main.lua", SearchOption.AllDirectories))
            {
                scriptThreads.Add(
                    new Thread(() =>
                {
                    using (Lua l = new Lua())
                    {
                        LuaGlobalPortable g = l.CreateEnvironment();
                        dynamic dg          = g;
                        dg.DebugLua         = new Func <object, bool>(debug);
                        dg.ConvertInt       = new Func <JValue, int>(convertInt);
                        dg.NewCustom        = new Func <string, Color, object>(newCustom);
                        dg.IntToByte        = new Func <int, byte>(IntToByte);
                        dg.Headset          = Headset.Instance;
                        dg.Keyboard         = Keyboard.Instance;
                        dg.Mouse            = Mouse.Instance;
                        dg.Keypad           = Keypad.Instance;
                        dg.Mousepad         = Mousepad.Instance;

                        dg.RegisterForEvents = new Func <string, object, bool>(registerEvents);
                        debug("starting Lua script: " + st);
                        try
                        {
                            LuaChunk compiled = l.CompileChunk(st, ms_luaCompileOptions);
                            var d             = g.DoChunk(compiled);
                        }
                        catch (LuaException e)
                        {
                            debug(e.FileName + ": " + e.Line + ": " + e.Message);
                        } catch (Exception e)
                        {
                            debug(e.Message);
                        }
                    }
                }));
                scriptThreads.Last().Start();
            }
        }
Example #16
0
 internal void UpdateLuaRuntime(Lua lua)
 {
     this.luaRuntime = new LuaGlobalPortable(lua);
 }         // proc UpdateLuaRuntime