Exemplo n.º 1
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();
                }
            }
        }
Exemplo n.º 2
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();
            }
        }