From 22a0618f9fd2f0ef079f7e35f0796a22583e68e5 Mon Sep 17 00:00:00 2001 From: Dor Date: Wed, 24 May 2017 23:41:40 +0300 Subject: [PATCH] 1.4: Added GetEventSource() and a property EventSource which calls the same function to retrieve an event's source, will return -1 for server, and a player's netID when triggered via a client. Removed RconPrint, no longer using reflection for Print, prints now show their call location from within ServerWrapper. Fixed calls to GetPlayer... functions for a player that doesn't exist resulting exceptions. Fixed delegate references not being removed. --- ServerScriptExample/ServerScriptExample.cs | 2 +- ServerWrapper/Properties/AssemblyInfo.cs | 2 +- ServerWrapper/ServerScript.cs | 89 ++++------ ServerWrapper/ServerWrapper.csproj | 6 +- ServerWrapper/Wrapper.cs | 179 ++++++++++++--------- 5 files changed, 144 insertions(+), 134 deletions(-) diff --git a/ServerScriptExample/ServerScriptExample.cs b/ServerScriptExample/ServerScriptExample.cs index 84cab0d..119efa5 100644 --- a/ServerScriptExample/ServerScriptExample.cs +++ b/ServerScriptExample/ServerScriptExample.cs @@ -82,7 +82,7 @@ public override void Load() { CancelEvent(); - RconPrint("Example rcon command."); + Print("Example rcon command."); } })); diff --git a/ServerWrapper/Properties/AssemblyInfo.cs b/ServerWrapper/Properties/AssemblyInfo.cs index 40c0aed..6888727 100644 --- a/ServerWrapper/Properties/AssemblyInfo.cs +++ b/ServerWrapper/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyVersion("1.4.0.0")] //[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ServerWrapper/ServerScript.cs b/ServerWrapper/ServerScript.cs index 01bcc78..05a432a 100644 --- a/ServerWrapper/ServerScript.cs +++ b/ServerWrapper/ServerScript.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Runtime.CompilerServices; namespace ServerWrapper { @@ -12,6 +13,8 @@ public abstract class ServerScript : MarshalByRefObject, IServerScript private List dependencies = new List(); public ReadOnlyCollection Dependencies { get; private set; } + public int EventSource { get { return GetEventSource(); } } + public ReadOnlyDictionary Players { get { return w.Players; } } public ReadOnlyDictionary PlayersByNetId { get { return w.PlayersByNetId; } } @@ -58,7 +61,15 @@ internal void CreateProxy(Wrapper wrapper, Queue scripts) { w = wrapper; - SetTimeout(0, (t) => { w.RconPrint("ServerWrapper: Proxy created for script \"" + Name + "\""); w.RconPrint(""); t.Loop = false; w.ETPhoneHome(this, scripts); }); // First ScriptTimer with Print and a call to Loop (usually) causes a small hiccup of 32~150ms for whatever reason, better have it done ahead of time. + SetTimeout(0, (t) => + { + w.Print("Proxy created for script \"" + Name + "\""); + w.Print(""); + + t.Loop = false; // First ScriptTimer with Print and a call to Loop (usually) causes a small hiccup of 32~150ms for whatever reason, better have it done ahead of time. + + w.ETPhoneHome(this, scripts); + }); } public abstract void Load(); @@ -74,66 +85,48 @@ public void AddDependency(Type type) public void Print(params object[] args) { - if (w != null) w.Print(PrintPrefix.Concat(args).ToArray()); + InternalPrint(args); } - /*public void RconPrint(string str) + private void InternalPrint(object[] args, + [CallerLineNumber] int sourceLineNumber = 0) { - if (w != null) w.RconPrint("ServerWrapper script \"" + Name + "\": " + str); - }*/ - - public void RconPrint(params object[] args) - { - if (w != null) w.RconPrint(PrintPrefix.Concat(args).ToArray()); + if (w != null) w.Print(this, "Print (" + Name + ")", "ServerWrapper\\ServerScript.cs", sourceLineNumber, PrintType.Info, args); } public ushort[] GetPlayers() { - if (w != null) return w.GetPlayers(); - - return null; + return w != null ? w.GetPlayers() : null; } public string GetPlayerName(int ID) { - if (w != null) return w.GetPlayerName(ID); - - return null; + return w != null ? w.GetPlayerName(ID) : null; } public IEnumerable GetPlayerIdentifiers(int ID) { - if (w != null) return w.GetPlayerIdentifiers(ID); - - return null; + return w != null ? w.GetPlayerIdentifiers(ID) : null; } public int GetPlayerPing(int ID) { - if (w != null) return w.GetPlayerPing(ID); - - return -1; + return w != null ? w.GetPlayerPing(ID) : -1; } public string GetPlayerEP(int ID) { - if (w != null) return w.GetPlayerEP(ID); - - return null; + return w != null ? w.GetPlayerEP(ID) : null; } public double GetPlayerLastMsg(int ID) { - if (w != null) return w.GetPlayerLastMsg(ID); - - return 99999999; + return w != null ? w.GetPlayerLastMsg(ID) : 99999999; } public int GetHostID() { - if (w != null) return w.GetHostID(); - - return -1; + return w != null ? w.GetHostID() : -1; } public void DropPlayer(int ID, string reason) @@ -148,10 +141,7 @@ public void TempBanPlayer(int ID, string reason) public Player GetPlayerFromID(int ID) { - if (w != null) return w.GetPlayerFromID(ID); - - //return default(Player); - return null; + return w != null ? w.GetPlayerFromID(ID) : /*default(Player)*/ null; } public void TriggerClientEvent(string eventname, int netID, params object[] args) @@ -168,11 +158,12 @@ public void RegisterServerEvent(string eventname) public bool TriggerEvent(string eventname, params object[] args) { - if (w != null) return w.TriggerEvent(eventname, ConvertArgsFromLocal(args)); + bool notcanceled = false; + if (w != null) notcanceled = w.TriggerEvent(eventname, ConvertArgsFromLocal(args)); lock (DelegateReferences) foreach (object arg in args) if (arg.GetType().IsSubclassOf(typeof(Delegate))) if (DelegateReferences.ContainsKey((Delegate)arg)) DelegateReferences.Remove((Delegate)arg); - return false; + return notcanceled; } public void CancelEvent() @@ -182,9 +173,7 @@ public void CancelEvent() public bool WasEventCanceled() { - if (w != null) return w.WasEventCanceled(); - - return false; + return w != null ? w.WasEventCanceled() : false; } internal void RemoveScriptTimerHandler(ScriptTimer timer) @@ -247,7 +236,7 @@ internal void TriggerLocalEvent(string eventname, params object[] args) } catch (Exception e) { - w.RconPrint("Error executing event handler for event " + eventname + " in resource ServerWrapper (" + Name + "): \n"); + w.Print(PrintType.Error, "Error executing event handler for event " + eventname + " in script " + Name + ": \n"); w.PrintException(e); //EventHandlers[eventname].Clear(); @@ -338,30 +327,22 @@ public void RemoveAllEventHandlers(string eventname) public int GetInstanceID() { - if (w != null) return w.GetInstanceID(); - - return -1; + return w != null ? w.GetInstanceID() : -1; } - /*public string GetInvokingResource() + public int GetEventSource() { - if (w != null) return w.GetInvokingResource(); - - return null; - }*/ + return w != null ? w.GetEventSource() : -1; + } public bool StopResource(string resourceName) { - if (w != null) return w.StopResource(resourceName); - - return false; + return w != null ? w.StopResource(resourceName) : false; } public bool StartResource(string resourceName) { - if (w != null) return w.StartResource(resourceName); - - return false; + return w != null ? w.StartResource(resourceName) : false; } public void SetGameType(string gameType) diff --git a/ServerWrapper/ServerWrapper.csproj b/ServerWrapper/ServerWrapper.csproj index ae827f3..b13304a 100644 --- a/ServerWrapper/ServerWrapper.csproj +++ b/ServerWrapper/ServerWrapper.csproj @@ -56,9 +56,13 @@ ..\..\..\..\..\Program Files\FiveM\cfx-server\CitizenMP.Server.exe - + + False ..\packages\NeoLua.1.2.20\lib\net45\Neo.Lua.dll + + ..\packages\NeoLua.1.2.20\lib\net45\Neo.Lua.Desktop.dll + False ..\..\..\..\Newtonsoft.Json.dll diff --git a/ServerWrapper/Wrapper.cs b/ServerWrapper/Wrapper.cs index 516018c..ff20a67 100644 --- a/ServerWrapper/Wrapper.cs +++ b/ServerWrapper/Wrapper.cs @@ -13,22 +13,30 @@ namespace ServerWrapper { + internal enum PrintType + { + Info, + Debug, + Warning, + Error, + Fatal + } + internal class Wrapper : MarshalByRefObject { private static bool initialized = false; private static Assembly serverasm = null; - private static Type LogScriptFunctions = null, RconScriptFunctions = null, PlayerScriptFunctions = null, EventScriptFunctions = null, ScriptEnvironment = null, ClientInstances = null, ResourceScriptFunctions = null; + private static Type PlayerScriptFunctions = null, EventScriptFunctions = null, ScriptEnvironment = null, ClientInstances = null, ResourceScriptFunctions = null; internal static Type SEScriptTimer = null; internal static PropertyInfo SEScriptTimerFunction = null, SEScriptTimerTickFrom = null; internal static IList SEScriptTimerList = null; - private static MethodInfo LSFPrint = null; - private static MethodInfo RconSFRconPrint = null; private static MethodInfo PSFDropPlayer = null, PSFTempBanPlayer = null, PSFGetHostId = null; private static MethodInfo ESFTriggerClientEvent = null, ESFRegisterServerEvent = null, ESFTriggerEvent = null, ESFCancelEvent = null, ESFWasEventCanceled = null; + private static object SECurrentEnvironment = null, SELuaEnvironment = null; private static MethodInfo SESetTimeout = null, SEAddEventHandler = null, SEGetInstanceId = null; - private static MethodInfo /*RSFGetInvokingResource = null,*/ RSFStopResource = null, RSFStartResource = null, RSFSetGameType = null, RSFSetMapName = null; + private static MethodInfo RSFStopResource = null, RSFStartResource = null, RSFSetGameType = null, RSFSetMapName = null; internal static ReadOnlyDictionary Clients { get; private set; } internal static ReadOnlyDictionary ClientsByNetId { get; private set; } @@ -66,20 +74,6 @@ public static void Initialize() if (serverasm != null) { - LogScriptFunctions = serverasm.GetType("CitizenMP.Server.Resources.LogScriptFunctions"); - - if (LogScriptFunctions != null) - { - LSFPrint = LogScriptFunctions.GetMethod("Print_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); - } - - RconScriptFunctions = serverasm.GetType("CitizenMP.Server.Resources.RconScriptFunctions"); - - if (RconScriptFunctions != null) - { - RconSFRconPrint = RconScriptFunctions.GetMethod("RconPrint_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); - } - PlayerScriptFunctions = serverasm.GetType("CitizenMP.Server.Resources.PlayerScriptFunctions"); if (PlayerScriptFunctions != null) @@ -108,14 +102,16 @@ public static void Initialize() SEAddEventHandler = ScriptEnvironment.GetMethod("AddEventHandler_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); SEGetInstanceId = ScriptEnvironment.GetMethod("GetInstanceId", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); - object CurrentEnvironmentInstance = ScriptEnvironment.GetProperty("CurrentEnvironment", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).GetValue(null); - EventHandlers = (Dictionary>)ScriptEnvironment.GetField("m_eventHandlers", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(CurrentEnvironmentInstance); + SECurrentEnvironment = ScriptEnvironment.GetProperty("CurrentEnvironment", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).GetValue(null); + EventHandlers = (Dictionary>)ScriptEnvironment.GetField("m_eventHandlers", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(SECurrentEnvironment); + + SELuaEnvironment = ScriptEnvironment.GetField("m_luaEnvironment", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(SECurrentEnvironment); SEScriptTimer = ScriptEnvironment.GetNestedType("ScriptTimer", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (SEScriptTimer != null) { - SEScriptTimerList = (IList)ScriptEnvironment.GetField("m_timers", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(CurrentEnvironmentInstance); + SEScriptTimerList = (IList)ScriptEnvironment.GetField("m_timers", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(SECurrentEnvironment); SEScriptTimerFunction = SEScriptTimer.GetProperty("Function", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); SEScriptTimerTickFrom = SEScriptTimer.GetProperty("TickFrom", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); @@ -134,7 +130,6 @@ public static void Initialize() if (ResourceScriptFunctions != null) { - //RSFGetInvokingResource = ScriptEnvironment.GetMethod("GetInvokingResource_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); RSFStopResource = ScriptEnvironment.GetMethod("StopResource_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); RSFStartResource = ScriptEnvironment.GetMethod("StartResource_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); RSFSetGameType = ScriptEnvironment.GetMethod("SetGameType_f", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); @@ -145,14 +140,14 @@ public static void Initialize() { foreach (MethodInfo mi in t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) { - foreach (object attr in mi.GetCustomAttributes()) - { - if (attr.GetType().ToString().Contains("LuaMember")) - { - instance.RconPrint(t + " (" + mi.ReturnType + ") " + mi.Name, string.Join(", ", mi.GetParameters().Select(pi => "(" + pi.ParameterType + ") " + pi.Name))); - } - } + foreach (object attr in mi.GetCustomAttributes()) if (attr.GetType().ToString().Contains("LuaMember")) instance.Print(t + " (" + mi.ReturnType + ") " + mi.Name, string.Join(", ", mi.GetParameters().Select(pi => "(" + pi.ParameterType + ") " + pi.Name))); + + //instance.Print(t + " (" + mi.ReturnType + ") " + mi.Name, string.Join(", ", mi.GetParameters().Select(pi => "(" + pi.ParameterType + ") " + pi.Name))); } + + //foreach (FieldInfo fi in t.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) instance.Print(t + " (" + fi.FieldType + ") " + fi.Name); + + //foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) instance.Print(t + " (" + pi.PropertyType + ") " + pi.Name); }*/ } @@ -259,7 +254,9 @@ public static void Initialize() if (currentver.Length < 3 || !int.TryParse(currentver[2], out CurrentBuild)) CurrentBuild = 0; if (currentver.Length < 4 || !int.TryParse(currentver[3], out CurrentRevision)) CurrentRevision = 0; string current = CurrentMajor + "." + CurrentMinor + "." + CurrentBuild, latest = ""; + //CurrentMajor = 0; + bool update = false; while (true) @@ -286,17 +283,17 @@ public static void Initialize() if (update) { - instance.RconPrint("ServerWrapper: ---------------------------------------------------------------------------------------------------"); - instance.RconPrint("ServerWrapper: An update to ServerWrapper is available!"); - instance.RconPrint("ServerWrapper: Current version: " + current + ", latest version: " + latest + "."); - instance.RconPrint("ServerWrapper: For more info, visit https://forum.fivem.net/t/release-c-net-wrapper-for-server-side-scripts/20325."); - instance.RconPrint("ServerWrapper: Enter the command \"swupdate\" to open the link above in your default browser."); - instance.RconPrint("ServerWrapper: ---------------------------------------------------------------------------------------------------"); + instance.Print("---------------------------------------------------------------------------------------------------"); + instance.Print("A ServerWrapper update is available!"); + instance.Print("Current version: " + current + ", latest version: " + latest + "."); + instance.Print("For more info, visit https://forum.fivem.net/t/release-c-net-wrapper-for-server-side-scripts/20325."); + instance.Print("Enter the command \"swupdate\" to open the link above in your default browser."); + instance.Print("---------------------------------------------------------------------------------------------------"); } } catch (Exception e) { - instance.RconPrint("ServerWrapper: Update check failed, will try again in 10 minutes."); + instance.Print(PrintType.Error, "Update check failed, will try again in 10 minutes."); instance.PrintException(e); } @@ -315,25 +312,25 @@ internal void PrintException(Exception e) e = e.InnerException; } - foreach (string msgs in msg.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)) RconPrint(msgs); + foreach (string m in msg.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)) Print(PrintType.Error, m); } private static void Load(string path) { - instance.RconPrint("ServerWrapper: Loading scripts in \"" + path + "\"..."); + instance.Print("Loading scripts in \"" + path + "\"..."); foreach (string file in Directory.GetFiles(path, "*.dll")) { if (AssemblyName.GetAssemblyName(file).Name == "ServerWrapper") { - instance.RconPrint("ServerWrapper: Found ServerWrapper assembly in Scripts folder. Removing..."); + instance.Print("Found ServerWrapper assembly in Scripts folder. Removing..."); try { File.Delete(file); } catch (Exception e) { - instance.RconPrint("ServerWrapper: Failed to remove ServerWrapper assembly from Scripts folder, this may cause issues."); + instance.Print(PrintType.Warning, "Failed to remove ServerWrapper assembly from Scripts folder, this may cause issues."); instance.PrintException(e); } } @@ -393,7 +390,7 @@ private static void Load(string path) mefLoader.Domain.UnhandledException += (sender, e) => { - instance.RconPrint("ServerWrapper: Unhandled exception occured in script."); + instance.Print(PrintType.Error, "Unhandled exception occured in script."); instance.PrintException((Exception)e.ExceptionObject); }; @@ -408,7 +405,7 @@ private static void Load(string path) { SeparateAppDomain.Delete(path); - instance.RconPrint("ServerWrapper: No scripts found in \"" + path + "\"."); + instance.Print("No scripts found in \"" + path + "\"."); return; } @@ -417,8 +414,8 @@ private static void Load(string path) _scripts.Add(path, scripts); - instance.RconPrint("ServerWrapper: " + scripts.Count + " script(s) found in \"" + path + "\"."); - instance.RconPrint(""); + instance.Print(scripts.Count + " script(s) found in \"" + path + "\"."); + instance.Print(""); // Sorting logic from ScriptHookVDotNet Dictionary> graph = new Dictionary>(); @@ -442,8 +439,8 @@ private static void Load(string path) if (s == null) { - instance.RconPrint("ServerWrapper: Detected a circular script dependency. Aborting..."); - instance.RconPrint(""); + instance.Print(PrintType.Fatal, "Detected a circular script dependency. Aborting..."); + instance.Print(""); return; } @@ -454,7 +451,7 @@ private static void Load(string path) foreach (var kv in graph) kv.Value.Remove(s.TypeName); } - LoadScripts(sorted); + if (graph.Count == 0) LoadScripts(sorted); } private static void LoadScripts(Queue scripts) @@ -473,12 +470,12 @@ private static void LoadScripts(Queue scripts) } catch (Exception e) { - instance.RconPrint("ServerWrapper: \"" + script.Name + "\"'s Tick() failed."); + instance.Print(PrintType.Error, "\"" + script.Name + "\"'s Tick() failed."); instance.PrintException(e); } }, true); - instance.RconPrint("ServerWrapper: Creating proxy for script \"" + script.Name + "\"..."); + instance.Print("Creating proxy for script \"" + script.Name + "\"..."); ss.CreateProxy(instance, scripts); } @@ -492,7 +489,7 @@ internal void ETPhoneHome(ServerScript script, Queue scripts) } catch (Exception e) { - RconPrint("ServerWrapper: \"" + script.Name + "\"'s Load() failed."); + Print(PrintType.Error, "\"" + script.Name + "\"'s Load() failed."); PrintException(e); } @@ -503,20 +500,20 @@ internal void ETPhoneHome(ServerScript script, Queue scripts) private static void Reload(string path) { - instance.RconPrint("ServerWrapper: Reloading scripts in \"" + path + "\"... (Unloading...)"); - instance.RconPrint(""); + instance.Print("Reloading scripts in \"" + path + "\"... (Unloading...)"); + instance.Print(""); Unload(path); - instance.RconPrint("ServerWrapper: Reloading scripts in \"" + path + "\"... (Loading...)"); - instance.RconPrint(""); + instance.Print("Reloading scripts in \"" + path + "\"... (Loading...)"); + instance.Print(""); Load(path); } private static void Unload(string path) { - instance.RconPrint("ServerWrapper: Unloading scripts in \"" + path + "\"..."); + instance.Print("Unloading scripts in \"" + path + "\"..."); AppDomain oldAppDomain = null; List oldscripts = new List(); @@ -530,8 +527,8 @@ private static void Unload(string path) oldAppDomain = SeparateAppDomain.Extract(path); } - instance.RconPrint("ServerWrapper: " + oldscripts.Count + " script(s) unloaded from \"" + path + "\"."); - instance.RconPrint(""); + instance.Print(oldscripts.Count + " script(s) unloaded from \"" + path + "\"."); + instance.Print(""); ScriptTimer[] timers; lock (ScriptTimers) timers = ScriptTimers.ToArray(); @@ -554,7 +551,7 @@ private static void Unload(string path) } catch (Exception e) { - instance.RconPrint("ServerWrapper: Failed to remove \"" + script.Name + "\"'s event handlers for event \"" + eventname + "\"."); + instance.Print(PrintType.Error, "Failed to remove \"" + script.Name + "\"'s event handlers for event \"" + eventname + "\"."); instance.PrintException(e); } } @@ -574,7 +571,7 @@ private static void Unload(string path) } catch (Exception e) { - instance.RconPrint("ServerWrapper: \"" + script.Name + "\"'s Unload() failed."); + instance.Print(PrintType.Error, "\"" + script.Name + "\"'s Unload() failed."); instance.PrintException(e); } } @@ -587,17 +584,47 @@ private static void Unload(string path) internal void Print(params object[] args) { - if (LSFPrint != null) LSFPrint.Invoke(null, new object[] { args }); + Print(PrintType.Info, args); } - /*internal void RconPrint(string str) + internal void Print(PrintType type, params object[] args) { - if (RSFRconPrint != null) RSFRconPrint.Invoke(null, new object[] { str }); - }*/ + Print(this.Log("Print", "ServerWrapper\\Wrapper.cs"), type, args); + } + + internal void Print(string prefix, string file, int line, PrintType type, params object[] args) + { + Print(this.Log(prefix, file, line), type, args); + } + + 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); + } - internal void RconPrint(params object[] args) + internal void Print(CitizenMP.Server.Logging.BaseLog log, PrintType type, params object[] args) { - if (RconSFRconPrint != null) RconSFRconPrint.Invoke(null, new object[] { string.Join(" ", args) }); + Func func = () => { return string.Join(" ", args.Select(a => a ?? "null")); }; + if (type == PrintType.Debug) + { + log.Debug(func); + } + else if (type == PrintType.Warning) + { + log.Warn(func); + } + else if (type == PrintType.Error) + { + log.Error(func); + } + else if (type == PrintType.Fatal) + { + log.Fatal(func); + } + else + { + log.Info(func); + } } internal ushort[] GetPlayers() @@ -609,35 +636,35 @@ internal string GetPlayerName(int ID) { Player p = GetPlayerFromID(ID); - return p.Name; + return p != null ? p.Name : null; } internal IEnumerable GetPlayerIdentifiers(int ID) { Player p = GetPlayerFromID(ID); - return p.Identifiers; + return p != null ? p.Identifiers : null; } internal int GetPlayerPing(int ID) { Player p = GetPlayerFromID(ID); - return p.Ping; + return p != null ? p.Ping : -1; } internal string GetPlayerEP(int ID) { Player p = GetPlayerFromID(ID); - return p.RemoteEP.ToString(); + return p != null ? p.RemoteEP.ToString() : null; } internal double GetPlayerLastMsg(int ID) { Player p = GetPlayerFromID(ID); - return Time.CurrentTime - p.LastSeen; + return p != null ? Time.CurrentTime - p.LastSeen : 99999999; } internal int GetHostID() @@ -683,9 +710,7 @@ internal void CancelEvent() internal bool WasEventCanceled() { - if (ESFWasEventCanceled != null) return (bool)ESFWasEventCanceled.Invoke(null, new object[] { }); - - return false; + return ESFWasEventCanceled != null ? (bool)ESFWasEventCanceled.Invoke(null, new object[] { }) : false; } internal ScriptTimer CreateTimer(ServerScript caller, int delay, bool loop = false) @@ -788,7 +813,7 @@ internal static object[] ConvertArgsFromNLua(params object[] args) continue; } - if (type.ToString().StartsWith("Neo.IronLua")) instance.Print("ServerWrapper: Conversation required for " + type + "!"); + if (type.ToString().StartsWith("Neo.IronLua")) instance.Print(PrintType.Warning, "Conversation required for " + type + "!"); Converted.Add(arg); } @@ -881,10 +906,10 @@ internal int GetInstanceID() return SEGetInstanceId != null ? (int)SEGetInstanceId.Invoke(null, new object[] { }) : -1; } - /*internal string GetInvokingResource() + internal int GetEventSource() { - return RSFGetInvokingResource != null ? (string)RSFGetInvokingResource.Invoke(null, new object[] { }) : null; - }*/ + return SELuaEnvironment != null ? (int)((Neo.IronLua.LuaGlobal)SELuaEnvironment)["source"] : -1; + } internal bool StopResource(string resourceName) {