Exemplo n.º 1
0
 internal void CancelTimeout(ServerScript caller, string id)
 {
     lock (ScriptTimers) foreach (ScriptTimer st in ScriptTimers.ToArray().Where(st => st.caller == caller && st.ID == id))
         {
             st.Cancel();
         }
 }
Exemplo n.º 2
0
        private static void LoadScripts(Queue <IServerScript> scripts)
        {
            if (scripts.Count > 0)
            {
                IServerScript iscript = scripts.Dequeue();

                ServerScript script = (ServerScript)iscript;

                ((ILease)script.GetLifetimeService()).Register(instance);

                script.timer = new ScriptTimer(script, 100, (timer) =>
                {
                    try
                    {
                        iscript.Tick();
                    }
                    catch (Exception e)
                    {
                        instance.Print(PrintType.Error, "\"" + iscript.Name + "\"'s Tick() failed.");
                        instance.PrintException(e);
                    }
                }, true);

                ((ILease)script.timer.GetLifetimeService()).Register(instance);

                instance.Print("Creating proxy for script \"" + iscript.Name + "\"...");

                script.CreateProxy(instance, scripts);
            }
        }
Exemplo n.º 3
0
        internal void RemoveAllEventHandlers(ServerScript caller, string eventname)
        {
            IServerScript icaller = (IServerScript)caller;

            lock (scripteventhandlers)
            {
                if (!scripteventhandlers.ContainsKey(icaller))
                {
                    scripteventhandlers.Add(icaller, new Dictionary <string, List <Delegate> >());
                }
                if (!scripteventhandlers[icaller].ContainsKey(eventname))
                {
                    scripteventhandlers[icaller].Add(eventname, new List <Delegate>());
                }

                if (EventHandlers != null && EventHandlers.ContainsKey(eventname))
                {
                    foreach (Delegate handler in scripteventhandlers[icaller][eventname])
                    {
                        EventHandlers[eventname].RemoveAll(h => h == handler);
                    }
                }

                scripteventhandlers[icaller][eventname].Clear();
            }
        }
Exemplo n.º 4
0
        internal void AddEventHandler(ServerScript caller, string eventname, int args)
        {
            IServerScript icaller = (IServerScript)caller;

            lock (scripteventhandlers)
            {
                if (!scripteventhandlers.ContainsKey(icaller))
                {
                    scripteventhandlers.Add(icaller, new Dictionary <string, List <Delegate> >());
                }
                if (!scripteventhandlers[icaller].ContainsKey(eventname))
                {
                    scripteventhandlers[icaller].Add(eventname, new List <Delegate>());
                }

                if (scripteventhandlers[icaller][eventname].Any(h => h.Method.GetParameters().Length == args))
                {
                    return;
                }

                Delegate[] handlers =
                {
                    new Action(() =>            { caller.TriggerLocalEvent(eventname); }),
                    new Action <object>((a1) => { object[] cargs = ConvertArgsFromNLua(a1); caller.TriggerLocalEvent(eventname, cargs[0]);}),
                    new Action <object, object>((a1, a2) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1]); }),
                    new Action <object, object, object>((a1, a2, a3) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2]); }),
                    new Action <object, object, object, object>((a1, a2, a3, a4) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3]); }),
                    new Action <object, object, object, object, object>((a1, a2, a3, a4, a5) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4]); }),
                    new Action <object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5]); }),
                    new Action <object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6]); }),
                    new Action <object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7]); }),
                    new Action <object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10], cargs[11]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10], cargs[11], cargs[12]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10], cargs[11], cargs[12], cargs[13]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10], cargs[11], cargs[12], cargs[13], cargs[14]); }),
                    new Action <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) =>{ object[] cargs = ConvertArgsFromNLua(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16); caller.TriggerLocalEvent(eventname, cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5], cargs[6], cargs[7], cargs[8], cargs[9], cargs[10], cargs[11], cargs[12], cargs[13], cargs[14], cargs[15]); })
                };

                foreach (Delegate handler in handlers)
                {
                    if (handler.Method.GetParameters().Length != args)
                    {
                        continue;
                    }

                    scripteventhandlers[icaller][eventname].Add(handler);

                    if (eventname != "rconCommand")
                    {
                        AddEventHandler(eventname, handler);
                    }
                }
            }
        }
Exemplo n.º 5
0
        internal ScriptTimer(ServerScript caller, int interval, ScriptTimerHandler callback, bool loop = false)
        {
            if (Wrapper.SEScriptTimer == null || Wrapper.SEScriptTimerFunction == null || Wrapper.SEScriptTimerTickFrom == null || Wrapper.SEScriptTimerList == null)
            {
                return;
            }

            this.caller = caller;

            ID = Guid.NewGuid().ToString();

            lock (Wrapper.ScriptTimers)
            {
                while (Wrapper.ScriptTimers.Any(st => st.ID == ID))
                {
                    ID = Guid.NewGuid().ToString();                                                 // Not taking any chances.
                }
                Wrapper.ScriptTimers.Add(this);
            }

            SEScriptTimer = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(Wrapper.SEScriptTimer.Assembly.FullName, Wrapper.SEScriptTimer.FullName);

            Interval = interval;

            this.loop = loop;

            Handler = callback;

            Function = new Action(() =>
            {
                if (Handler != null)
                {
                    Handler(this);
                }

                if (Loop)
                {
                    Start();
                }
                else
                {
                    Cancel();
                }
            });
        }
Exemplo n.º 6
0
        internal void ETPhoneHome(ServerScript script, Queue <IServerScript> scripts)
        {
            try
            {
                script.Load();
            }
            catch (Exception e)
            {
                Print(PrintType.Error, "\"" + script.Name + "\"'s Load() failed.");
                PrintException(e);
            }

            if (script.timer.Loop)
            {
                script.timer.Start();
            }

            LoadScripts(scripts);
        }
Exemplo n.º 7
0
 internal bool HasTimeoutFinished(ServerScript caller, string id)
 {
     lock (ScriptTimers) return(!ScriptTimers.Any(st => st.ID == id));
 }
Exemplo n.º 8
0
        internal ScriptTimer CreateTimer(ServerScript caller, int delay, bool loop = false)
        {
            ScriptTimer st = new ScriptTimer(caller, delay, (timer) => { caller.CallScriptTimerHandler(timer); }, loop);

            return(st.caller == caller ? st : null);
        }
Exemplo n.º 9
0
 internal void Print(ServerScript script, string prefix, string file, int line, PrintType type, params object[] args)
 {
     Print(LogExtensions.Log(script, prefix, file, line), type, args);
 }
Exemplo n.º 10
0
        private static void Unload(string path)
        {
            instance.Print("Unloading scripts in \"" + path + "\"...");

            AppDomain            oldAppDomain = null;
            List <IServerScript> oldscripts   = new List <IServerScript>();

            if (_scripts.ContainsKey(path))
            {
                oldscripts = _scripts[path];

                _scripts.Remove(path);

                oldAppDomain = SeparateAppDomain.Extract(path);
            }

            instance.Print(oldscripts.Count + " script(s) unloaded from \"" + path + "\".");
            instance.Print("");

            ScriptTimer[] timers;
            lock (ScriptTimers) timers = ScriptTimers.ToArray();

            foreach (IServerScript iscript in oldscripts)
            {
                foreach (ScriptTimer st in timers.Where(st => st.caller == iscript))
                {
                    st.Dispose();
                }

                lock (scripteventhandlers)
                {
                    if (scripteventhandlers.ContainsKey(iscript))
                    {
                        foreach (string eventname in scripteventhandlers[iscript].Keys)
                        {
                            try
                            {
                                instance.RemoveAllEventHandlers((ServerScript)iscript, eventname);

                                ((ServerScript)iscript).RemoveAllEventHandlers(eventname);
                            }
                            catch (Exception e)
                            {
                                instance.Print(PrintType.Error, "Failed to remove \"" + iscript.Name + "\"'s event handlers for event \"" + eventname + "\".");
                                instance.PrintException(e);
                            }
                        }

                        scripteventhandlers[iscript].Clear();

                        scripteventhandlers.Remove(iscript);
                    }
                }

                ServerScript script = ((ServerScript)iscript);

                if (script.timer != null)
                {
                    script.timer.Dispose();
                }

                try
                {
                    iscript.Unload();
                }
                catch (Exception e)
                {
                    instance.Print(PrintType.Error, "\"" + iscript.Name + "\"'s Unload() failed.");
                    instance.PrintException(e);
                }

                ((ILease)script.GetLifetimeService()).Unregister(instance);
            }

            lock (scripteventhandlers) scripteventhandlers.Clear();
            //assemblies.Clear();

            if (oldAppDomain != null)
            {
                AppDomain.Unload(oldAppDomain);
            }
        }