Skip to content

Commit

Permalink
Changes from PR57
Browse files Browse the repository at this point in the history
  • Loading branch information
FloppyDisk committed Jul 4, 2023
1 parent fa92888 commit ca43564
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 38 deletions.
68 changes: 42 additions & 26 deletions PulsarModLoader/CustomGUI/GUIMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@
using UnityEngine;
using static UnityEngine.GUILayout;
using System.Net.Http;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using Logger = PulsarModLoader.Utilities.Logger;
using System.Threading.Tasks;
using System.Threading;
using static UnityEngine.UI.GridLayoutGroup;

namespace PulsarModLoader.CustomGUI
{
internal class GUIMain : MonoBehaviour
{
NameValueCollection Readme = new NameValueCollection();
NameValueCollection ReadmeLock = new NameValueCollection(); //fuck you


Dictionary<string, string> Readme = new Dictionary<string, string>();
readonly CultureInfo ci;
public static GUIMain Instance = null;
public bool GUIActive = false;
static float Height = .40f;
Expand Down Expand Up @@ -86,14 +80,12 @@ void OnGUI()
}
}

readonly CultureInfo ci;
private void GetReadme(string ModName, string ModURL)
{
// \/ Fuck you in particular \/
if (Readme[ModName] == null && ReadmeLock[ModName] == null)
bool ReadmeLocked = Readme.ContainsKey(ModName);
if (!ReadmeLocked)
{
ReadmeLock.Add(ModName, String.Empty);

Readme.Add(ModName, String.Empty);
if (ModURL.StartsWith("file://", false, ci))
{
string ModZip = Path.Combine(Directory.GetCurrentDirectory(), "Mods", ModName + ".zip");
Expand All @@ -105,26 +97,48 @@ private void GetReadme(string ModName, string ModURL)
{
if (Entry.FullName.EndsWith(ModURL.Replace("file://", String.Empty).Trim('/'), StringComparison.OrdinalIgnoreCase))
{
StreamReader StreamReadme = new StreamReader(Entry.Open());
Readme.Add(ModName, StreamReadme.ReadToEnd());
if (Entry.Length > PMLConfig.MaxLoadSizeBytes.Value)
{
StreamReader StreamReadme = new StreamReader(Entry.Open());
Readme.Add(ModName, StreamReadme.ReadToEnd());
}
else
{
Readme.Add(ModName, $"Error: Readme is too large.");
}
break;
}
}
}
}
else
{
Readme.Add(ModName, "Readme not found.");
Readme.Add(ModName, "Error: Readme not found.");
}
}
else
{
var Client = new HttpClient();
HttpResponseMessage response = Client.GetAsync(ModURL).Result;
Readme.Add(ModName, response.Content.ReadAsStringAsync().Result);
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.Add(ModName, $"Error: HTTP Code {Response.StatusCode}.");
}

}
}
}
}
}

void WindowFunction(int WindowID)
{

Expand Down Expand Up @@ -193,20 +207,22 @@ void WindowFunction(int WindowID)
}

//Get Readme from URL
if (mod.ReadmeURL != string.Empty)
if (!string.IsNullOrEmpty(mod.ReadmeURL))
{
if (Readme[mod.Name] == null )
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"))
{
Label("Readme:\nPulling readme, Please wait...");
new Thread(() => { GetReadme(mod.Name, mod.ReadmeURL); }).Start();
}
}
else
{
Label($"Readme:\n{Readme[mod.Name]}");
}
else
{
Label($"Readme:\n\n{Readme[mod.Name]}");
}
}
}
EndScrollView();
Expand Down
44 changes: 38 additions & 6 deletions PulsarModLoader/CustomGUI/PMLSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using static UnityEngine.GUILayout;

Expand All @@ -10,14 +9,9 @@ namespace PulsarModLoader.CustomGUI
{
class PMLSettings : ModSettingsMenu
{

public override string Name() => "PulsarModLoader";
public override void Draw()
{
if (Button($"Auto Download Readme: {PMLConfig.AutoPullReadme}"))
{
PMLConfig.AutoPullReadme.Value = !PMLConfig.AutoPullReadme.Value;
}
if (Button("Debug Mode: " + (PMLConfig.DebugMode ? "Enabled" : "Disabled")))
{
PMLConfig.DebugMode.Value = !PMLConfig.DebugMode.Value;
Expand All @@ -44,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"))
Expand All @@ -60,7 +58,41 @@ public override void Draw()
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;
}
}
}
}
17 changes: 11 additions & 6 deletions PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions PulsarModLoader/PMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static class PMLConfig
public static SaveValue<bool> ZipModLoad = new SaveValue<bool>("ZipModLoad", true);
public static SaveValue<bool> ZipModMode = new SaveValue<bool>("ZipModMode", false);

public static uint DefaultMaxLoadSizeBytes = 10485760;
public static SaveValue<uint> MaxLoadSizeBytes = new SaveValue<uint>("MaxLoadSizeBytes", DefaultMaxLoadSizeBytes);

public static SaveValue<DateTime> LastPMLUpdateCheck = new SaveValue<DateTime>("LastPMLUpdateCheck", DateTime.Today.AddDays(-2));

public static void SetDefault()
Expand Down

0 comments on commit ca43564

Please sign in to comment.