Beispiel #1
0
    public LuaWTBObject Duplicate()
    {
        List <WTBObject> dupeObjects = new List <WTBObject>()
        {
            WTBObject
        };

        dupeObjects.AddRange(WTBObject.GetChildrenAll());

        DuplicateTargetCommand cmd = new DuplicateTargetCommand(dupeObjects);

        // list also contains the children
        List <WTBObject> duplicatedWTBOs = cmd.LuaDuplicate();

        foreach (WTBObject wtbo in duplicatedWTBOs)
        {
            wtbo.Sync();
            LuaHandler scriptHandler = wtbo.GetComponent <LuaHandler>();
            if (scriptHandler != null)
            {
                scriptHandler.Run();
            }
        }

        duplicatedWTBOs[0].transform.position = WTBObject.transform.position;
        duplicatedWTBOs[0].transform.rotation = WTBObject.transform.rotation;

        duplicatedWTBOs[0].ComponentByName("Transform").PropertyByName("HasPhysics").Refresh();

        return(Task.GetOrMakeLuaPart(duplicatedWTBOs[0]));
    }
Beispiel #2
0
        internal Client(IDatabase db, Assembly assembly, TCursor debugInstance)
        {
            _csharpCompiler = new CSharpCompiler();
            _luaHandler     = new LuaHandler(db);
            _delegateReader = DelegateReader.CreateCachedWithDefaultAssemblyProvider();

            DebugInstance = debugInstance;
            Database      = db;
        }
        public void RegisterLuaHandler(string handlerName)
        {
            luaEnv.DoString("require '" + handlerName + "'");
            ILuaHandler handler = new LuaHandler();

            handler.NAME = luaEnv.Global.GetInPath <string>(handlerName + ".NAME");
            handler.ListNotificationInterests = luaEnv.Global.GetInPath <List <string> >(handlerName + ".ListNotificationInterests");
            handler.OnRegister = luaEnv.Global.GetInPath <Action>(handlerName + ".OnRegister");
            handler.OnRemove   = luaEnv.Global.GetInPath <Action>(handlerName + ".OnRemove");
            handler.Request    = luaEnv.Global.GetInPath <HandleNotification>(handlerName + ".HandleNotification");
            base.RegisterLuaHandler(handler);
        }
Beispiel #4
0
        public static bool ExecuteMethod(LuaScript luaScript, LuaScriptMethods scriptMethods)
        {
            var scriptResult = LuaHandler.Run(luaScript);

            if (scriptResult != null)
            {
                var func = GetMethodFromScript(luaScript, scriptMethods);
                if (func != null)
                {
                    var funcResult = luaScript.Script.Call(func);
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
 /// <summary>
 /// Initializes the bridge to DoW2, called by DebugManager.Init().
 /// </summary>
 static public void LuaInit()
 {
     TimeStampedTrace("CopeDebug - Lua Init");
     s_luaHandler = LuaCallHandler; // setup a handler for LUA calls by the injected CopeLua.dll
     try
     {
         SetLuaHandler(Marshal.GetFunctionPointerForDelegate(s_luaHandler));
     }
     catch (Exception ex)
     {
         TimeStampedTrace("CopeDebug - Lua Init Failed");
         TimeStampedTrace(ex.Message);
         return;
     }
     TimeStampedTrace("CopeDebug - Lua Init Finished");
 }
Beispiel #6
0
 /// <summary>
 /// Initializes the bridge to DoW2, called by DebugManager.Init().
 /// </summary>
 static public void LuaInit()
 {
     TimeStampedTrace("CopeDebug - Lua Init");
     s_luaHandler = LuaCallHandler; // setup a handler for LUA calls by the injected CopeLua.dll
     try
     {
         SetLuaHandler(Marshal.GetFunctionPointerForDelegate(s_luaHandler));
     }
     catch (Exception ex)
     {
         TimeStampedTrace("CopeDebug - Lua Init Failed");
         TimeStampedTrace(ex.Message);
         return;
     }
     TimeStampedTrace("CopeDebug - Lua Init Finished");
 }
Beispiel #7
0
        static Lua()
        {
            Ditto.Connected += () =>
            {
                // Load lua scripts from directory
                var _ = Ditto.Client.DoAsync(client =>
                {
                    client.MessageReceived    += DiscordMessageReceived;
                    client.UserJoined         += DiscordUserJoined;
                    client.GuildMemberUpdated += GuildMemberUpdated;
                });


                if (!Initialized)
                {
                    Initialized = true;

                    // Load all files to _script
                    Task.Run(async() =>
                    {
                        foreach (var filePath in Directory.GetFiles($"{Globals.AppDirectory}\\data\\lua", "*.*", SearchOption.AllDirectories))
                        {
                            var guildId = Convert.ToUInt64(Path.GetDirectoryName(filePath).Split("\\").Last());
                            LuaHandler.CreateLuaScript(out LuaScript luaScript, null, null, null);
                            luaScript.Guild    = await Ditto.Client.DoAsync(client => client.GetGuild(guildId)).ConfigureAwait(false);
                            luaScript.FileName = Path.GetFileName(filePath);
                            ExecuteMethod(luaScript, LuaScriptMethods.Initialise);
                            LuaHandler.ApplyScriptVariables(luaScript, true);
                            await LuaHandler.RunAsync(luaScript).ConfigureAwait(false);

                            var luaScripts = _scripts.GetOrAdd(luaScript.Guild, new ConcurrentList <LuaScript>());
                            luaScripts.Add(luaScript);
                        }
                    });

                    // Manual events
                    Task.Run(async() =>
                    {
                        var tasks     = new ConcurrentDictionary <int, Tuple <Task, DateTime> >();
                        var tickDelay = TimeSpan.FromSeconds(10);

                        var dateStart = DateTime.MinValue;
                        while (Ditto.Running)
                        {
                            dateStart = DateTime.Now;
                            foreach (var scriptPair in _scripts)
                            {
                                foreach (var script in scriptPair.Value)
                                {
                                    if (tasks.TryGetValue(script.Id, out Tuple <Task, DateTime> tuple))
                                    {
                                        if ((tuple.Item1.Status == TaskStatus.RanToCompletion || tuple.Item1.Status == TaskStatus.Canceled || tuple.Item1.Status == TaskStatus.Faulted) &&
                                            (dateStart - tuple.Item2) > tickDelay)
                                        {
                                            tasks.TryRemove(script.Id, out tuple);
                                        }
                                    }

                                    if (!tasks.TryGetValue(script.Id, out tuple))
                                    {
                                        tasks.TryAdd(script.Id, new Tuple <Task, DateTime>(Task.Run(() =>
                                        {
                                            ExecuteMethod(script, LuaScriptMethods.Tick);
                                        }), dateStart));
                                    }
                                }
                            }

                            await Task.Delay(250).ConfigureAwait(false);
                        }
                    });
                }
                return(Task.CompletedTask);
            };
            Ditto.Exit += () =>
            {
                // Load lua scripts from directory
                return(Ditto.Client.DoAsync(client =>
                {
                    if (client != null)
                    {
                        client.MessageReceived -= DiscordMessageReceived;
                        client.UserJoined -= DiscordUserJoined;
                        client.GuildMemberUpdated -= GuildMemberUpdated;
                    }
                }));
            };
        }
Beispiel #8
0
 public Client(IDatabase db)
 {
     _decompiler     = new ActionDecompiler();
     _csharpCompiler = new CSharpCompiler();
     _luaHandler     = new LuaHandler(db);
 }