diff --git a/SCPStats/EventHandler.cs b/SCPStats/EventHandler.cs index a96beaa..1af89ee 100644 --- a/SCPStats/EventHandler.cs +++ b/SCPStats/EventHandler.cs @@ -1,17 +1,13 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net.Http; -using System.Net.WebSockets; using System.Security.Cryptography; using System.Text; -using System.Threading; using System.Threading.Tasks; using Exiled.API.Features; using Exiled.Events.EventArgs; using Exiled.Loader; -using Grenades; +using WebSocketSharp; namespace SCPStats { @@ -23,22 +19,19 @@ internal class EventHandler private static bool Pinged = true; private static bool Exited = false; - private static Websocket ws = null; + private static WebSocket ws = null; private static Task Pinger = null; private static bool PingerActive = false; - - private static bool ConnectGrace = false; + private static List Queue = new List(); internal static void Reset() { - ws?.Close(false); + ws?.Close(); ws = null; Exited = false; Pinged = true; - Queue = new List(); - ConnectGrace = false; } internal static void Start() @@ -72,34 +65,37 @@ private static string HandleId(string id) private static async Task CreateConnection() { + if (ws != null && ws.IsAlive) return; + Pinged = false; if (Exited) { - ws?.Close(false); + ws?.Close(); SCPStats.Singleton.OnDisabled(); return; } try { - ws?.Close(false); + if(ws != null && ws.IsAlive) ws?.Close(); - ws = new Websocket("wss://scpstats.com/plugin"); + ws = new WebSocket("wss://scpstats.com/plugin"); - ws.OnMessage = msg => + ws.OnMessage += (sender, e) => { + if (!e.IsText) return; #if DEBUG - Log.Info(msg); + Log.Info(e.Data); #endif - switch (msg) + switch (e.Data) { case "i": Log.Warn("Authentication failed. Exiting."); Exited = true; - ws?.Close(false); + ws?.Close(); SCPStats.Singleton.OnDisabled(); return; @@ -117,12 +113,14 @@ private static async Task CreateConnection() } }; - ws.OnClose = () => + ws.OnClose += (sender, e) => { + if (Exited) return; + CreateConnection(); }; - await ws.Connect(); + ws.Connect(); } catch (Exception e) { @@ -148,11 +146,11 @@ private static async Task CreateConnection() private static async Task Ping() { - while (ws.ws.State == WebSocketState.Open) + while (ws.IsAlive) { if (Pinged) { - CreateConnection(); + ws?.Close(); return; } @@ -165,35 +163,17 @@ private static async Task Ping() PingerActive = false; } - private static async Task UnGrace() - { - await Task.Delay(10000); - ConnectGrace = false; - } - private static async Task SendRequest(string type, Dictionary data) { if (Exited) { - ws?.Close(false); + ws?.Close(); SCPStats.Singleton.OnDisabled(); return; } - if (ConnectGrace && (ws == null || ws.ws.State != WebSocketState.Open)) - { - var str1 = type+(data != null ? DictToString(data) : ""); - - var message1 = "p" + SCPStats.Singleton.Config.ServerId + str1.Length.ToString() + " " + str1 + HmacSha256Digest(SCPStats.Singleton.Config.Secret, str1); - - Queue.Add(message1); - return; - } - - if (ws == null || ws.ws.State != WebSocketState.Open) + if (ws == null || !ws.IsAlive) { - ConnectGrace = true; - UnGrace(); await CreateConnection(); } @@ -201,7 +181,7 @@ private static async Task SendRequest(string type, Dictionary da var message = "p" + SCPStats.Singleton.Config.ServerId + str.Length.ToString() + " " + str + HmacSha256Digest(SCPStats.Singleton.Config.Secret, str); - await ws.Send(message); + ws.Send(message); } private static bool IsPlayerValid(Player p, bool dnt = true, bool role = true) @@ -253,7 +233,7 @@ internal static void Waiting() internal static void OnKill(DyingEventArgs ev) { if (!ev.IsAllowed || !IsPlayerValid(ev.Target, false) || !IsPlayerValid(ev.Killer, false) || !RoundSummary.RoundInProgress()) return; - + var data = new Dictionary() { {"playerid", HandleId(ev.Target.RawUserId)}, @@ -262,10 +242,11 @@ internal static void OnKill(DyingEventArgs ev) {"damagetype", DamageTypes.ToIndex(ev.HitInformation.GetDamageType()).ToString()} }; + if(!ev.Target.DoNotTrack) SendRequest("02", data); if (ev.Killer.RawUserId == ev.Target.RawUserId || ev.Killer.DoNotTrack) return; - + data = new Dictionary() { {"playerid", HandleId(ev.Killer.RawUserId)}, diff --git a/SCPStats/FodyWeavers.xml b/SCPStats/FodyWeavers.xml new file mode 100644 index 0000000..e8f8931 --- /dev/null +++ b/SCPStats/FodyWeavers.xml @@ -0,0 +1,8 @@ + + + + + websocket-sharp + + + \ No newline at end of file diff --git a/SCPStats/SCPStats.csproj b/SCPStats/SCPStats.csproj index e2ae65c..1341a2e 100644 --- a/SCPStats/SCPStats.csproj +++ b/SCPStats/SCPStats.csproj @@ -1,5 +1,6 @@  + Debug @@ -35,6 +36,10 @@ lib\Assembly-CSharp-Publicized.dll + + ..\packages\Costura.Fody.5.0.0-alpha0281\lib\netstandard1.0\Costura.dll + True + ..\packages\EXILED.2.1.7\lib\net472\Exiled.API.dll True @@ -59,32 +64,169 @@ ..\packages\EXILED.2.1.7\lib\net472\Exiled.Updater.dll True + + ..\packages\Microsoft.Extensions.Logging.Abstractions.3.0.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + True + + + ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll + True + lib\Mirror.dll + + + ..\packages\System.AppContext.4.3.0\lib\net463\System.AppContext.dll + True + + + + ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + True + - + + ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + True + + + ..\packages\System.Diagnostics.Tracing.4.3.0\lib\net462\System.Diagnostics.Tracing.dll + True + + + ..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll + True + + + ..\packages\System.IO.4.3.0\lib\net462\System.IO.dll + True + + + ..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + True + + + + ..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll + True + + + ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + True + + + ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + True + + + ..\packages\System.Linq.4.3.0\lib\net463\System.Linq.dll + True + + + ..\packages\System.Linq.Expressions.4.3.0\lib\net463\System.Linq.Expressions.dll + True + + + ..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + True + + + ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + + + + ..\packages\System.Reactive.4.2.0\lib\net46\System.Reactive.dll + True + + + ..\packages\System.Reflection.4.3.0\lib\net462\System.Reflection.dll + True + + + ..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + True + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + True + + + ..\packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll + True + + + ..\packages\System.Runtime.InteropServices.4.3.0\lib\net463\System.Runtime.InteropServices.dll + True + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + True + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + + + ..\packages\System.Text.RegularExpressions.4.3.0\lib\net463\System.Text.RegularExpressions.dll + True + + + ..\packages\System.Threading.Tasks.Extensions.4.5.3\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + True + + + + ..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + True + lib\UnityEngine.dll lib\UnityEngine.CoreModule.dll + + lib\websocket-sharp.dll + - + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + + +