diff --git a/PulsarModLoader/CustomGUI/GUIMain.cs b/PulsarModLoader/CustomGUI/GUIMain.cs index dc1a147..b60dd3a 100644 --- a/PulsarModLoader/CustomGUI/GUIMain.cs +++ b/PulsarModLoader/CustomGUI/GUIMain.cs @@ -5,14 +5,20 @@ using System.Reflection; using HarmonyLib; using PulsarModLoader.Utilities; -using Steamworks; using UnityEngine; using static UnityEngine.GUILayout; +using System.Net.Http; +using System.Globalization; +using System.IO; +using System.IO.Compression; +using System.Threading; namespace PulsarModLoader.CustomGUI { internal class GUIMain : MonoBehaviour { + Dictionary Readme = new Dictionary(); + readonly CultureInfo ci; public static GUIMain Instance = null; public bool GUIActive = false; static float Height = .40f; @@ -73,7 +79,66 @@ void OnGUI() Window = GUI.Window(999910, Window, WindowFunction, "ModManager"); } } - + + private void GetReadme(string ModName, string ModURL) + { + bool ReadmeLocked = Readme.ContainsKey(ModName); + if (!ReadmeLocked) + { + Readme.Add(ModName, String.Empty); + if (ModURL.StartsWith("file://", false, ci)) + { + string ModZip = Path.Combine(Directory.GetCurrentDirectory(), "Mods", ModName + ".zip"); + if (PMLConfig.ZipModLoad && !PMLConfig.ZipModMode && File.Exists(ModZip)) + { + using (ZipArchive Archive = ZipFile.OpenRead(ModZip)) + { + foreach (ZipArchiveEntry Entry in Archive.Entries) + { + if (Entry.FullName.EndsWith(ModURL.Replace("file://", String.Empty).Trim('/'), StringComparison.OrdinalIgnoreCase)) + { + if (Entry.Length > PMLConfig.MaxLoadSizeBytes.Value) + { + StreamReader StreamReadme = new StreamReader(Entry.Open()); + Readme[ModName] = StreamReadme.ReadToEnd(); + } + else + { + Readme[ModName] = $"Error: Readme is too large."; + } + break; + } + } + } + } + else + { + Readme[ModName] = "Error: Readme not found."; + } + } + else + { + using (var Client = new HttpClient()) + { + Client.MaxResponseContentBufferSize = PMLConfig.MaxLoadSizeBytes.Value; + using (HttpResponseMessage Response = Client.GetAsync(ModURL).Result) + { + if (Response.IsSuccessStatusCode) + { + //Readme.Add(ModName, Response.Content.ReadAsStringAsync().Result); //Since we lock using string.empty, we must replace the value. + Readme[ModName] = Response.Content.ReadAsStringAsync().Result; + } + else + { + Readme[ModName] = $"Error: HTTP Code {Response.StatusCode}."; + } + + } + } + } + } + } + void WindowFunction(int WindowID) { @@ -140,6 +205,25 @@ void WindowFunction(int WindowID) else if(Button($"Update this mod to version {result.Data.Version}?")) ModUpdateCheck.UpdateMod(result); } + + //Get Readme from URL + if (!string.IsNullOrEmpty(mod.ReadmeURL)) + { + bool ReadmeLocked = Readme.TryGetValue(mod.Name, out string ReadmeValue); + bool ReadmeEmpty = string.IsNullOrEmpty(ReadmeValue); +// Logger.Info($"locked,empty:{ReadmeLocked},{ReadmeEmpty}"); + if (ReadmeEmpty && !ReadmeLocked) + { + if (PMLConfig.AutoPullReadme.Value || Button("Load Readme")) + { + new Thread(() => { GetReadme(mod.Name, mod.ReadmeURL); }).Start(); + } + } + else + { + Label($"Readme:\n\n{Readme[mod.Name]}"); + } + } } EndScrollView(); } diff --git a/PulsarModLoader/CustomGUI/PMLSettings.cs b/PulsarModLoader/CustomGUI/PMLSettings.cs index 72c487a..89bc145 100644 --- a/PulsarModLoader/CustomGUI/PMLSettings.cs +++ b/PulsarModLoader/CustomGUI/PMLSettings.cs @@ -1,7 +1,6 @@ using System; using System.IO; using System.Linq; -using System.Reflection; using UnityEngine; using static UnityEngine.GUILayout; @@ -10,7 +9,6 @@ namespace PulsarModLoader.CustomGUI { class PMLSettings : ModSettingsMenu { - public override string Name() => "PulsarModLoader"; public override void Draw() { @@ -40,7 +38,11 @@ public override void Draw() //Zip Mod Settings HorizontalSlider(0, 100, 100); + BeginHorizontal(); + FlexibleSpace(); Label("Zip Mods"); + FlexibleSpace(); + EndHorizontal(); BeginHorizontal(); Label($"Load Mods From Zips: {PMLConfig.ZipModLoad.Value}"); if (Button("Toggle Loading Of Zip Mods")) @@ -50,13 +52,47 @@ public override void Draw() EndHorizontal(); BeginHorizontal(); - Label($"Keep Zips After Load: {PMLConfig.ZipModMode}"); + Label($"Delete Zips After Load: {PMLConfig.ZipModMode}"); if (Button("Toggle Zip Mod Mode")) { PMLConfig.ZipModMode.Value = !PMLConfig.ZipModMode.Value; } EndHorizontal(); + + //Max Load Size Settings HorizontalSlider(0, 100, 100); + BeginHorizontal(); + FlexibleSpace(); + Label($"File Size Loading Limits\nCurrent Size: {PMLConfig.MaxLoadSizeBytes.Value / 1048576}MiB"); + FlexibleSpace(); + EndHorizontal(); + BeginHorizontal(); + if(Button("-10MiB") && PMLConfig.MaxLoadSizeBytes.Value > PMLConfig.DefaultMaxLoadSizeBytes) + { + PMLConfig.MaxLoadSizeBytes.Value = PMLConfig.MaxLoadSizeBytes.Value - PMLConfig.DefaultMaxLoadSizeBytes; + } + if(Button("Default")) + { + PMLConfig.MaxLoadSizeBytes.Value = PMLConfig.DefaultMaxLoadSizeBytes; + } + if (Button("+10MiB") && PMLConfig.MaxLoadSizeBytes.Value <= (uint.MaxValue - PMLConfig.DefaultMaxLoadSizeBytes)) + { + PMLConfig.MaxLoadSizeBytes.Value = PMLConfig.MaxLoadSizeBytes.Value + PMLConfig.DefaultMaxLoadSizeBytes; + } + EndHorizontal(); + Label("Dont Change This Unless You Understand What It Does"); + + //Readme Loading Settings + HorizontalSlider(0, 0, 0); + BeginHorizontal(); + FlexibleSpace(); + Label("Readme Settings"); + FlexibleSpace(); + EndHorizontal(); + if (Button($"Readmes Will be loaded: {(PMLConfig.AutoPullReadme.Value ? "Automatically" : "Manually")}")) + { + PMLConfig.AutoPullReadme.Value = !PMLConfig.AutoPullReadme.Value; + } } } } \ No newline at end of file diff --git a/PulsarModLoader/ModManager.cs b/PulsarModLoader/ModManager.cs index 4c77450..cee790c 100644 --- a/PulsarModLoader/ModManager.cs +++ b/PulsarModLoader/ModManager.cs @@ -214,7 +214,6 @@ public void LoadModsDirectory(string modsDir) { foreach (string ZipPath in Directory.GetFiles(modsDir, "*.zip")) { - //Get the full path from the mods dir path string ZipExtractPath = Path.GetFullPath(modsDir); //Ensure that the mods dir path has a path seperator else add it. @@ -231,24 +230,30 @@ public void LoadModsDirectory(string modsDir) { if (Entry.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { + if (Entry.Length < PMLConfig.MaxLoadSizeBytes.Value) + { + Logger.Info($"Error: Extraction of {Entry.Name} failed, Too Large!)"); + break; + } + string DestinationPath = Path.GetFullPath(Path.Combine(modsDir, Entry.Name)); - //If the mod exists, delete it and replace with this one. if (File.Exists(DestinationPath)) { File.Delete(DestinationPath); } - //Check the Destination is in the mods dir, then extract - if (DestinationPath.StartsWith(modsDir, StringComparison.Ordinal)) + if (!DestinationPath.StartsWith(modsDir, StringComparison.Ordinal)) { - Entry.ExtractToFile(DestinationPath); + Logger.Info($"Extraction Path for {Entry.Name} !modsDir"); + break; } + + Entry.ExtractToFile(DestinationPath); } } } - //Delete Zip archive once we are done as we have the DLL's now if (PMLConfig.ZipModMode) { File.Delete(ZipPath); diff --git a/PulsarModLoader/PMLConfig.cs b/PulsarModLoader/PMLConfig.cs index fbf1e81..486e9f9 100644 --- a/PulsarModLoader/PMLConfig.cs +++ b/PulsarModLoader/PMLConfig.cs @@ -5,14 +5,18 @@ namespace PulsarModLoader { public static class PMLConfig { - public static SaveValue ModInfoTextAnchor = - new SaveValue("ModInfoTextAnchor", TextAnchor.UpperLeft); + public static SaveValue ModInfoTextAnchor = new SaveValue("ModInfoTextAnchor", TextAnchor.UpperLeft); + + public static SaveValue AutoPullReadme = new SaveValue("AutoPullReadme", false); public static SaveValue DebugMode = new SaveValue("DebugMode", false); public static SaveValue ZipModLoad = new SaveValue("ZipModLoad", true); public static SaveValue ZipModMode = new SaveValue("ZipModMode", false); + public static uint DefaultMaxLoadSizeBytes = 10485760; + public static SaveValue MaxLoadSizeBytes = new SaveValue("MaxLoadSizeBytes", DefaultMaxLoadSizeBytes); + public static SaveValue LastPMLUpdateCheck = new SaveValue("LastPMLUpdateCheck", DateTime.Today.AddDays(-2)); public static void SetDefault() diff --git a/PulsarModLoader/PulsarMod.cs b/PulsarModLoader/PulsarMod.cs index 8a4a76a..993f91e 100644 --- a/PulsarModLoader/PulsarMod.cs +++ b/PulsarModLoader/PulsarMod.cs @@ -59,6 +59,17 @@ public virtual string License } } + /// + /// URL To the readme of the mod. + /// + public virtual string ReadmeURL + { + get + { + return string.Empty; + } + } + /// /// Version of mod. Displayed in mod list. /// diff --git a/PulsarModLoader/PulsarModLoader.csproj b/PulsarModLoader/PulsarModLoader.csproj index 66fd1f3..8cbba20 100644 --- a/PulsarModLoader/PulsarModLoader.csproj +++ b/PulsarModLoader/PulsarModLoader.csproj @@ -91,6 +91,7 @@ False + @@ -99,7 +100,42 @@ True True + + ..\packages\System.IO.4.3.0\lib\net462\System.IO.dll + True + True + + + ..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll + True + True + + + ..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + True + True + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + diff --git a/PulsarModLoader/packages.config b/PulsarModLoader/packages.config index d5325e9..2dfe47f 100644 --- a/PulsarModLoader/packages.config +++ b/PulsarModLoader/packages.config @@ -1,5 +1,12 @@  + + + + + + + \ No newline at end of file