Skip to content

Commit

Permalink
Add functionality to GUIMain to pull the readme from a specified URL …
Browse files Browse the repository at this point in the history
…in the mod. (#56)

* Add feature readme from URL

* Clean up and remove unused

* Add auto download option

* Revert "Add auto download option"

This reverts commit 0031d30.

* Dunno why it works now but it does

* I did not forget to merge the button from auto

* call readme from url or file://[name] in zip file

* Changes From PR 56, See PR

---------

Co-authored-by: FloppyDisk <--global>
  • Loading branch information
flpydsk authored Jul 4, 2023
1 parent b4678fc commit 9cd19b5
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 13 deletions.
88 changes: 86 additions & 2 deletions PulsarModLoader/CustomGUI/GUIMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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)
{

Expand Down Expand Up @@ -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();
}
Expand Down
42 changes: 39 additions & 3 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,7 +9,6 @@ namespace PulsarModLoader.CustomGUI
{
class PMLSettings : ModSettingsMenu
{

public override string Name() => "PulsarModLoader";
public override void Draw()
{
Expand Down Expand Up @@ -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"))
Expand All @@ -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;
}
}
}
}
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
8 changes: 6 additions & 2 deletions PulsarModLoader/PMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ namespace PulsarModLoader
{
public static class PMLConfig
{
public static SaveValue<UnityEngine.TextAnchor> ModInfoTextAnchor =
new SaveValue<TextAnchor>("ModInfoTextAnchor", TextAnchor.UpperLeft);
public static SaveValue<UnityEngine.TextAnchor> ModInfoTextAnchor = new SaveValue<TextAnchor>("ModInfoTextAnchor", TextAnchor.UpperLeft);

public static SaveValue<bool> AutoPullReadme = new SaveValue<bool>("AutoPullReadme", false);

public static SaveValue<bool> DebugMode = new SaveValue<bool>("DebugMode", false);

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
11 changes: 11 additions & 0 deletions PulsarModLoader/PulsarMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public virtual string License
}
}

/// <ReadmeURL>
/// URL To the readme of the mod.
/// </ReadmeURL>
public virtual string ReadmeURL
{
get
{
return string.Empty;
}
}

/// <summary>
/// Version of mod. Displayed in mod list.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions PulsarModLoader/PulsarModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
Expand All @@ -99,7 +100,42 @@
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
Expand Down
7 changes: 7 additions & 0 deletions PulsarModLoader/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Cecil" version="0.11.3" targetFramework="net472" />
<package id="System.IO" version="4.3.0" targetFramework="net472" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net472" />
<package id="System.Runtime" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net472" />
<package id="System.IO.Compression.ZipFile" version="4.3.0" targetFramework="net472" />
</packages>

0 comments on commit 9cd19b5

Please sign in to comment.