Skip to content

Commit

Permalink
1.4:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DorCoMaNdO committed May 24, 2017
1 parent cffdb35 commit 22a0618
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 134 deletions.
2 changes: 1 addition & 1 deletion ServerScriptExample/ServerScriptExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override void Load()
{
CancelEvent();

RconPrint("Example rcon command.");
Print("Example rcon command.");
}
}));

Expand Down
2 changes: 1 addition & 1 deletion ServerWrapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
89 changes: 35 additions & 54 deletions ServerWrapper/ServerScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;

namespace ServerWrapper
{
Expand All @@ -12,6 +13,8 @@ public abstract class ServerScript : MarshalByRefObject, IServerScript
private List<string> dependencies = new List<string>();
public ReadOnlyCollection<string> Dependencies { get; private set; }

public int EventSource { get { return GetEventSource(); } }

public ReadOnlyDictionary<string, Player> Players { get { return w.Players; } }
public ReadOnlyDictionary<ushort, Player> PlayersByNetId { get { return w.PlayersByNetId; } }

Expand Down Expand Up @@ -58,7 +61,15 @@ internal void CreateProxy(Wrapper wrapper, Queue<IServerScript> 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();
Expand All @@ -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<string> 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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion ServerWrapper/ServerWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@
<HintPath>..\..\..\..\..\Program Files\FiveM\cfx-server\CitizenMP.Server.exe</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Neo.Lua">
<Reference Include="Neo.Lua, Version=5.3.0.0, Culture=neutral, PublicKeyToken=fdb0cd4fe8a6e3b2, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NeoLua.1.2.20\lib\net45\Neo.Lua.dll</HintPath>
</Reference>
<Reference Include="Neo.Lua.Desktop">
<HintPath>..\packages\NeoLua.1.2.20\lib\net45\Neo.Lua.Desktop.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\Newtonsoft.Json.dll</HintPath>
Expand Down
Loading

0 comments on commit 22a0618

Please sign in to comment.