diff --git a/Client/Loader/LoaderContext.cs b/Client/Loader/LoaderContext.cs index 1438f01..f2d970e 100644 --- a/Client/Loader/LoaderContext.cs +++ b/Client/Loader/LoaderContext.cs @@ -1,14 +1,11 @@ -using System; +using SHVDN; +using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Console = GTA.Console; -using SHVDN; using System.Windows.Forms; +using Console = GTA.Console; namespace RageCoop.Client.Loader { @@ -17,7 +14,8 @@ namespace RageCoop.Client.Loader { #region PRIMARY-LOADING-LOGIC public static ConcurrentDictionary LoadedDomains => new ConcurrentDictionary(_loadedDomains); - static readonly ConcurrentDictionary _loadedDomains = new ConcurrentDictionary(); + + private static readonly ConcurrentDictionary _loadedDomains = new ConcurrentDictionary(); public bool UnloadRequested; public string BaseDirectory => AppDomain.CurrentDomain.BaseDirectory; @@ -129,7 +127,8 @@ namespace RageCoop.Client.Loader domain.Dispose(); ScriptDomain.Unload(domain.CurrentDomain); } - catch (Exception e) { + catch (Exception e) + { ex = e; GTA.UI.Notification.Show(ex.ToString()); } diff --git a/Client/Loader/Main.cs b/Client/Loader/Main.cs index f182e2e..1c27837 100644 --- a/Client/Loader/Main.cs +++ b/Client/Loader/Main.cs @@ -1,20 +1,17 @@ -using System; +using GTA; +using System; using System.Collections.Concurrent; using System.IO; using System.Threading; -using System.Windows.Forms; -using GTA; -using SHVDN; using Console = GTA.Console; namespace RageCoop.Client.Loader { public class Main : GTA.Script { - static readonly string GameDir = Directory.GetParent(typeof(SHVDN.ScriptDomain).Assembly.Location).FullName; - - static readonly string ScriptsLocation = Path.Combine(GameDir, "RageCoop", "Scripts"); + private static readonly string GameDir = Directory.GetParent(typeof(SHVDN.ScriptDomain).Assembly.Location).FullName; + private static readonly string ScriptsLocation = Path.Combine(GameDir, "RageCoop", "Scripts"); private static readonly ConcurrentQueue TaskQueue = new ConcurrentQueue(); - static int MainThreadID; + private static int MainThreadID; public Main() { if (LoaderContext.PrimaryDomain != null) { throw new InvalidOperationException("Improperly placed loader assembly, please re-install to fix this issue"); } @@ -55,7 +52,7 @@ namespace RageCoop.Client.Loader private static void DomainTick(object sender, EventArgs e) { - if (MainThreadID == default) { MainThreadID=Thread.CurrentThread.ManagedThreadId;} + if (MainThreadID == default) { MainThreadID = Thread.CurrentThread.ManagedThreadId; } while (TaskQueue.TryDequeue(out var task)) { try diff --git a/Client/Loader/Properties/AssemblyInfo.cs b/Client/Loader/Properties/AssemblyInfo.cs index b5ba820..cc6d007 100644 --- a/Client/Loader/Properties/AssemblyInfo.cs +++ b/Client/Loader/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/Client/Scripts/DevTools/DevTool.cs b/Client/Scripts/DevTools/DevTool.cs index f64eacd..638bdd5 100644 --- a/Client/Scripts/DevTools/DevTool.cs +++ b/Client/Scripts/DevTools/DevTool.cs @@ -1,6 +1,5 @@ using GTA; using GTA.Math; -using RageCoop.Client.Scripting; using System; using System.Drawing; using System.Threading; diff --git a/Client/Scripts/Main.cs b/Client/Scripts/Main.cs index f4e093c..966342e 100644 --- a/Client/Scripts/Main.cs +++ b/Client/Scripts/Main.cs @@ -5,17 +5,15 @@ using RageCoop.Client.Menus; using RageCoop.Client.Scripting; using RageCoop.Core; using SHVDN; -using Script = GTA.Script; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; -using System.Threading; -using System.Threading.Tasks; using System.Windows.Forms; using Console = GTA.Console; +using Script = GTA.Script; namespace RageCoop.Client { @@ -33,7 +31,6 @@ namespace RageCoop.Client internal static RelationshipGroup SyncedPedsGroup; internal static new Settings Settings = null; - internal static BaseScript BaseScript = new BaseScript(); #if !NON_INTERACTIVE #endif diff --git a/Client/Scripts/Menus/Sub/DebugMenu.cs b/Client/Scripts/Menus/Sub/DebugMenu.cs index 4c8e97c..14ad8a6 100644 --- a/Client/Scripts/Menus/Sub/DebugMenu.cs +++ b/Client/Scripts/Menus/Sub/DebugMenu.cs @@ -1,9 +1,7 @@ using GTA; using LemonUI.Menus; -using RageCoop.Client.Scripting; using System; using System.Drawing; -using System.IO; namespace RageCoop.Client { @@ -19,7 +17,7 @@ namespace RageCoop.Client UseMouse = false, Alignment = Main.Settings.FlipMenu ? GTA.UI.Alignment.Right : GTA.UI.Alignment.Left }; - public static NativeItem ReloadItem = new NativeItem("Reload","Reload RAGECOOP and associated scripts"); + public static NativeItem ReloadItem = new NativeItem("Reload", "Reload RAGECOOP and associated scripts"); public static NativeItem SimulatedLatencyItem = new NativeItem("Simulated network latency", "Simulated network latency in ms (one way)", "0"); public static NativeCheckboxItem ShowOwnerItem = new NativeCheckboxItem("Show entity owner", "Show the owner name of the entity you're aiming at", false); private static readonly NativeCheckboxItem ShowNetworkInfoItem = new NativeCheckboxItem("Show Network Info", Networking.ShowNetworkInfo); diff --git a/Client/Scripts/Networking/Send.cs b/Client/Scripts/Networking/Send.cs index b32bf07..1071792 100644 --- a/Client/Scripts/Networking/Send.cs +++ b/Client/Scripts/Networking/Send.cs @@ -2,8 +2,8 @@ using GTA.Math; using GTA.Native; using Lidgren.Network; -using RageCoop.Core; using RageCoop.Client.Scripting; +using RageCoop.Core; using System; using System.Collections.Generic; diff --git a/Client/Scripts/Properties/AssemblyInfo.cs b/Client/Scripts/Properties/AssemblyInfo.cs index 6a92e52..b44b645 100644 --- a/Client/Scripts/Properties/AssemblyInfo.cs +++ b/Client/Scripts/Properties/AssemblyInfo.cs @@ -16,7 +16,7 @@ using System.Resources; // Version informationr( -[assembly: AssemblyVersion("1.5.4.334")] -[assembly: AssemblyFileVersion("1.5.4.334")] +[assembly: AssemblyVersion("1.5.4.341")] +[assembly: AssemblyFileVersion("1.5.4.341")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )] diff --git a/Client/Scripts/Scripting/API.cs b/Client/Scripts/Scripting/API.cs index f807d05..c29de2f 100644 --- a/Client/Scripts/Scripting/API.cs +++ b/Client/Scripts/Scripting/API.cs @@ -6,7 +6,6 @@ using RageCoop.Core.Scripting; using SHVDN; using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Windows.Forms; diff --git a/Client/Scripts/Scripting/BaseScript.cs b/Client/Scripts/Scripting/BaseScript.cs index bcfa408..962ea8c 100644 --- a/Client/Scripts/Scripting/BaseScript.cs +++ b/Client/Scripts/Scripting/BaseScript.cs @@ -9,11 +9,10 @@ using System.Threading.Tasks; namespace RageCoop.Client.Scripting { - [ScriptAttributes(Author = "RageCoop", NoDefaultInstance = true, SupportURL = "https://github.com/RAGECOOP/RAGECOOP-V")] - internal class BaseScript : ClientScript + internal static class BaseScript { - private bool _isHost = false; - public override void OnStart() + private static bool _isHost = false; + public static void OnStart() { API.Events.OnPedDeleted += (s, p) => { API.SendCustomEvent(CustomEvents.OnPedDeleted, p.ID); }; API.Events.OnVehicleDeleted += (s, p) => { API.SendCustomEvent(CustomEvents.OnVehicleDeleted, p.ID); }; @@ -58,19 +57,19 @@ namespace RageCoop.Client.Scripting }); } - private void WeatherTimeSync(CustomEventReceivedArgs e) + private static void WeatherTimeSync(CustomEventReceivedArgs e) { World.CurrentTimeOfDay = new TimeSpan((int)e.Args[0], (int)e.Args[1], (int)e.Args[2]); Function.Call(Hash._SET_WEATHER_TYPE_TRANSITION, (int)e.Args[3], (int)e.Args[4], (float)e.Args[5]); } - private void SetDisplayNameTag(CustomEventReceivedArgs e) + private static void SetDisplayNameTag(CustomEventReceivedArgs e) { var p = PlayerList.GetPlayer((int)e.Args[0]); if (p != null) { p.DisplayNameTag = (bool)e.Args[1]; } } - private void UpdatePedBlip(CustomEventReceivedArgs e) + private static void UpdatePedBlip(CustomEventReceivedArgs e) { var p = Entity.FromHandle((int)e.Args[0]); if (p == null) { return; } @@ -90,7 +89,7 @@ namespace RageCoop.Client.Scripting } } - private void CreateVehicle(CustomEventReceivedArgs e) + private static void CreateVehicle(CustomEventReceivedArgs e) { var vehicleModel = (Model)e.Args[1]; vehicleModel.Request(1000); @@ -109,7 +108,7 @@ namespace RageCoop.Client.Scripting EntityPool.Add(v); } - private void DeleteServerBlip(CustomEventReceivedArgs e) + private static void DeleteServerBlip(CustomEventReceivedArgs e) { if (EntityPool.ServerBlips.TryGetValue((int)e.Args[0], out var blip)) { @@ -118,7 +117,7 @@ namespace RageCoop.Client.Scripting } } - private void ServerBlipSync(CustomEventReceivedArgs obj) + private static void ServerBlipSync(CustomEventReceivedArgs obj) { int id = (int)obj.Args[0]; var sprite = (BlipSprite)(ushort)obj.Args[1]; @@ -140,15 +139,12 @@ namespace RageCoop.Client.Scripting } - private void DeleteEntity(CustomEventReceivedArgs e) + private static void DeleteEntity(CustomEventReceivedArgs e) { Entity.FromHandle((int)e.Args[0])?.Delete(); } - public override void OnStop() - { - } - private void SetNameTag(CustomEventReceivedArgs e) + private static void SetNameTag(CustomEventReceivedArgs e) { var p = PlayerList.GetPlayer((int)e.Args[0]); if (p != null) @@ -156,11 +152,11 @@ namespace RageCoop.Client.Scripting p.DisplayNameTag = (bool)e.Args[1]; } } - private void SetAutoRespawn(CustomEventReceivedArgs args) + private static void SetAutoRespawn(CustomEventReceivedArgs args) { API.Config.EnableAutoRespawn = (bool)args.Args[0]; } - private void DeleteServerProp(CustomEventReceivedArgs e) + private static void DeleteServerProp(CustomEventReceivedArgs e) { var id = (int)e.Args[0]; if (EntityPool.ServerProps.TryGetValue(id, out var prop)) @@ -170,7 +166,7 @@ namespace RageCoop.Client.Scripting } } - private void ServerObjectSync(CustomEventReceivedArgs e) + private static void ServerObjectSync(CustomEventReceivedArgs e) { SyncedProp prop; var id = (int)e.Args[0]; @@ -188,7 +184,7 @@ namespace RageCoop.Client.Scripting prop.Model.Request(1000); prop.Update(); } - private void NativeCall(CustomEventReceivedArgs e) + private static void NativeCall(CustomEventReceivedArgs e) { List arguments = new List(); int i; @@ -272,7 +268,7 @@ namespace RageCoop.Client.Scripting } } - private InputArgument GetInputArgument(object obj) + private static InputArgument GetInputArgument(object obj) { // Implicit conversion switch (obj) diff --git a/Client/Scripts/Scripting/ClientScript.cs b/Client/Scripts/Scripting/ClientScript.cs index 7ae9508..a19bdc6 100644 --- a/Client/Scripts/Scripting/ClientScript.cs +++ b/Client/Scripts/Scripting/ClientScript.cs @@ -1,5 +1,4 @@ using RageCoop.Core.Scripting; -using System; namespace RageCoop.Client.Scripting { diff --git a/Client/Scripts/Scripting/Resources.cs b/Client/Scripts/Scripting/Resources.cs index aee29db..f219a43 100644 --- a/Client/Scripts/Scripting/Resources.cs +++ b/Client/Scripts/Scripting/Resources.cs @@ -8,7 +8,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Threading; namespace RageCoop.Client.Scripting { @@ -135,7 +134,7 @@ namespace RageCoop.Client.Scripting LoadedResources.TryAdd(r.Name, r); } - void SetupScripts() + private void SetupScripts() { foreach (var s in GetClientScripts()) { @@ -165,7 +164,8 @@ namespace RageCoop.Client.Scripting API.Logger.Debug("Started script: " + s.GetType().FullName); } } - void StopScripts() + + private void StopScripts() { foreach (var s in GetClientScripts()) { diff --git a/Client/Scripts/Util/Util.cs b/Client/Scripts/Util/Util.cs index 1107049..550497b 100644 --- a/Client/Scripts/Util/Util.cs +++ b/Client/Scripts/Util/Util.cs @@ -1,20 +1,13 @@ using GTA; -using Console = GTA.Console; using GTA.Math; using GTA.Native; +using Newtonsoft.Json; using RageCoop.Core; using System; -using System.Collections.Generic; using System.Drawing; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Windows.Forms; -using System.Xml.Serialization; -using Newtonsoft.Json; -using SHVDN; -using System.Runtime.InteropServices.ComTypes; [assembly: InternalsVisibleTo("RageCoop.Client.Installer")] namespace RageCoop.Client diff --git a/Client/Scripts/WorldThread.cs b/Client/Scripts/WorldThread.cs index 5f8fa09..2d44487 100644 --- a/Client/Scripts/WorldThread.cs +++ b/Client/Scripts/WorldThread.cs @@ -1,11 +1,9 @@ using GTA; using GTA.Native; -using RageCoop.Client.Scripting; -using RageCoop.Core; using System; using System.Collections.Generic; -using System.Threading.Tasks; using System.Threading; +using System.Threading.Tasks; namespace RageCoop.Client { @@ -23,7 +21,7 @@ namespace RageCoop.Client public WorldThread() { Util.StartUpCheck(); - Instance = this; + Instance = this; Tick += OnTick; Aborted += (sender, e) => { diff --git a/Core/CoreUtils.cs b/Core/CoreUtils.cs index 7925e31..55b020a 100644 --- a/Core/CoreUtils.cs +++ b/Core/CoreUtils.cs @@ -37,7 +37,7 @@ namespace RageCoop.Core public static Version GetLatestVersion(string branch = "dev-nightly") { var url = $"https://raw.githubusercontent.com/RAGECOOP/RAGECOOP-V/{branch}/RageCoop.Server/Properties/AssemblyInfo.cs"; - var versionLine = HttpHelper.DownloadString(url).Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("[assembly: AssemblyVersion(")).First(); + var versionLine = HttpHelper.DownloadString(url).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("[assembly: AssemblyVersion(")).First(); var start = versionLine.IndexOf('\"') + 1; var end = versionLine.LastIndexOf('\"'); return Version.Parse(versionLine.Substring(start, end - start)); @@ -427,7 +427,7 @@ namespace RageCoop.Core return output; } - public static bool IsScript(this Type type,Type scriptType) + public static bool IsScript(this Type type, Type scriptType) { return !type.IsAbstract && type.IsSubclassOf(scriptType); } diff --git a/Core/Logger.cs b/Core/Logger.cs index 03c7332..980940f 100644 --- a/Core/Logger.cs +++ b/Core/Logger.cs @@ -1,10 +1,8 @@ -using GTA.NaturalMotion; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Text; using System.Threading; namespace RageCoop.Core @@ -142,7 +140,8 @@ namespace RageCoop.Core Flush(); } } - string Format(LogLine line) + + private string Format(LogLine line) { return string.Format("[{0}][{2}] [{3}] {1}", line.TimeStamp.ToString(DateTimeFormat), line.Message, Name, line.LogLevel.ToString()); } diff --git a/Server/Properties/AssemblyInfo.cs b/Server/Properties/AssemblyInfo.cs index b3d4551..796fd25 100644 --- a/Server/Properties/AssemblyInfo.cs +++ b/Server/Properties/AssemblyInfo.cs @@ -15,7 +15,7 @@ using System.Resources; [assembly: AssemblyCulture("")] // Version information -[assembly: AssemblyVersion("1.5.4.106")] -[assembly: AssemblyFileVersion("1.5.4.106")] +[assembly: AssemblyVersion("1.5.4.118")] +[assembly: AssemblyFileVersion("1.5.4.118")] [assembly: NeutralResourcesLanguageAttribute( "en-US" )] diff --git a/Server/RageCoop.Server.csproj b/Server/RageCoop.Server.csproj index 36526b1..c973b03 100644 --- a/Server/RageCoop.Server.csproj +++ b/Server/RageCoop.Server.csproj @@ -55,6 +55,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Server/Scripting/Resources.cs b/Server/Scripting/Resources.cs index 5b237a9..ee7c864 100644 --- a/Server/Scripting/Resources.cs +++ b/Server/Scripting/Resources.cs @@ -1,8 +1,10 @@ -using ICSharpCode.SharpZipLib.Zip; +using DiscUtils.Iso9660; +using ICSharpCode.SharpZipLib.Zip; using RageCoop.Core; using System; using System.IO; using System.Threading.Tasks; + namespace RageCoop.Server.Scripting { internal class Resources @@ -16,6 +18,7 @@ namespace RageCoop.Server.Scripting Server = server; Logger = server.Logger; } + private readonly Dictionary Packages = new(); private readonly Dictionary ClientResources = new(); private readonly Dictionary ResourceStreams = new(); public void LoadAll() @@ -26,25 +29,37 @@ namespace RageCoop.Server.Scripting Directory.CreateDirectory(path); foreach (var pkg in Directory.GetFiles(path, "*.respkg", SearchOption.AllDirectories)) { - Logger?.Debug($"Adding resources from package \"{Path.GetFileNameWithoutExtension(pkg)}\""); - var pkgZip = new ZipFile(pkg); - foreach (ZipEntry e in pkgZip) + try { - if (!e.IsFile) { continue; } - if (e.Name.StartsWith("Client") && e.Name.EndsWith(".res")) + Logger?.Debug($"Adding resources from package \"{Path.GetFileNameWithoutExtension(pkg)}\""); + var pkgStream = File.Open(pkg, FileMode.Open, FileAccess.Read, FileShare.Read); + var reader = new CDReader(pkgStream, true); + foreach (var e in reader.Root.GetDirectories("Client")[0].GetFiles()) { - var stream = pkgZip.GetInputStream(e).ToMemStream(); - ClientResources.Add(Path.GetFileNameWithoutExtension(e.Name), stream); - Logger?.Debug("Resource added: " + Path.GetFileNameWithoutExtension(e.Name)); + var name = Path.GetFileNameWithoutExtension(e.Name); + if (!ClientResources.TryAdd(name, e.Open(FileMode.Open))) + { + Logger?.Warning($"Resource \"{name}\" already added, ignoring"); + continue; + } + Logger?.Debug("Resource added: " + name); } - else if (e.Name.StartsWith("Server") && e.Name.EndsWith(".res")) + foreach (var e in reader.Root.GetDirectories("Server")[0].GetFiles()) { - var stream = pkgZip.GetInputStream(e).ToMemStream(); - ResourceStreams.Add(Path.GetFileNameWithoutExtension(e.Name), stream); - Logger?.Debug("Resource added: " + Path.GetFileNameWithoutExtension(e.Name)); + var name = Path.GetFileNameWithoutExtension(e.Name); + if (!ResourceStreams.TryAdd(name, e.Open(FileMode.Open))) + { + Logger?.Warning($"Resource \"{name}\" already loaded, ignoring"); + continue; + } + Logger?.Debug("Resource added: " + name); } + Packages.Add(reader, pkgStream); + } + catch (Exception ex) + { + Logger?.Error("Failed to read resources from package: " + pkg, ex); } - pkgZip.Close(); } } @@ -64,9 +79,16 @@ namespace RageCoop.Server.Scripting { foreach (var resourceFolder in resourceFolders) { + var zipPath = Path.Combine(tmpDir, Path.GetFileName(resourceFolder)) + ".res"; + var name = Path.GetFileNameWithoutExtension(zipPath); + if (ClientResources.ContainsKey(name)) + { + Logger?.Warning($"Client resource \"{name}\" already loaded, ignoring"); + continue; + } + // Pack client side resource as a zip file Logger?.Info("Packing client-side resource: " + resourceFolder); - var zipPath = Path.Combine(tmpDir, Path.GetFileName(resourceFolder)) + ".res"; try { using ZipFile zip = ZipFile.Create(zipPath); @@ -82,7 +104,7 @@ namespace RageCoop.Server.Scripting } zip.CommitUpdate(); zip.Close(); - ClientResources.Add(Path.GetFileNameWithoutExtension(zipPath), File.OpenRead(zipPath)); + ClientResources.Add(name, File.OpenRead(zipPath)); } catch (Exception ex) { @@ -91,14 +113,6 @@ namespace RageCoop.Server.Scripting } } } - var packed = Directory.GetFiles(path, "*.res", SearchOption.TopDirectoryOnly); - if (packed.Length > 0) - { - foreach (var file in packed) - { - ClientResources.Add(Path.GetFileNameWithoutExtension(file), File.OpenRead(file)); - } - } } // Server @@ -127,14 +141,6 @@ namespace RageCoop.Server.Scripting Logger?.Error(ex); } } - foreach (var res in Directory.GetFiles(path, "*.res", SearchOption.TopDirectoryOnly)) - { - if (!ResourceStreams.TryAdd(Path.GetFileNameWithoutExtension(res), File.OpenRead(res))) - { - Logger?.Warning($"Resource \"{res}\" cannot be loaded, ignoring..."); - continue; - } - } foreach (var res in ResourceStreams) { try @@ -206,6 +212,18 @@ namespace RageCoop.Server.Scripting } LoadedResources.Clear(); } + foreach (var s in ClientResources.Values) + { + try + { + s.Close(); + s.Dispose(); + } + catch (Exception ex) + { + Logger?.Error("[Resources.CloseStream]", ex); + } + } foreach (var s in ResourceStreams.Values) { try @@ -218,17 +236,10 @@ namespace RageCoop.Server.Scripting Logger?.Error("[Resources.CloseStream]", ex); } } - foreach (var s in ClientResources.Values) + foreach (var r in Packages) { - try - { - s.Close(); - s.Dispose(); - } - catch (Exception ex) - { - Logger?.Error("[Resources.CloseStream]", ex); - } + r.Key.Dispose(); + r.Value.Dispose(); } } public void SendTo(Client client)