Skip to content

Commit

Permalink
Add config menu and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
FloppyDisk committed Jul 1, 2023
1 parent 397b32d commit 21821ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
24 changes: 23 additions & 1 deletion PulsarModLoader/CustomGUI/PMLSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace PulsarModLoader.CustomGUI
{
class PMLSettings : ModSettingsMenu
{

public override string Name() => "PulsarModLoader";
public override void Draw()
{
Expand All @@ -34,7 +35,28 @@ public override void Draw()
PMLConfig.ModInfoTextAnchor.Value = Enum.GetValues(typeof(TextAnchor)).Cast<TextAnchor>().SkipWhile(e => (int)e != (int)PMLConfig.ModInfoTextAnchor.Value).Skip(1).First();
}
EndHorizontal();

if (Button("Reset to default")) PMLConfig.SetDefault();

//Zip Mod Settings
HorizontalSlider(0, 100, 100);
Label("Zip Mods");
BeginHorizontal();
Label($"Load Mods From Zips: {PMLConfig.ZipModLoad.Value}");
if (Button("Toggle Loading Of Zip Mods"))
{
PMLConfig.ZipModLoad.Value = !PMLConfig.ZipModLoad.Value;
}

EndHorizontal();
BeginHorizontal();
Label($"Keep Zips After Load: {PMLConfig.ZipModMode}");
if (Button("Toggle Zip Mod Mode"))
{
PMLConfig.ZipModMode.Value = !PMLConfig.ZipModMode.Value;
}
EndHorizontal();
HorizontalSlider(0, 100, 100);
}
}
}
}
62 changes: 34 additions & 28 deletions PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,44 +210,50 @@ public void LoadModsDirectory(string modsDir)
modDirectories.Add(modsDir);

//Unzip mods
foreach (string ZipPath in Directory.GetFiles(modsDir, "*.zip"))
if (PMLConfig.ZipModLoad)
{
//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.
//This stops a path traversal attack from the zip file
if (!ZipExtractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
foreach (string ZipPath in Directory.GetFiles(modsDir, "*.zip"))
{
ZipExtractPath += Path.DirectorySeparatorChar;
}
//Get the full path from the mods dir path
string ZipExtractPath = Path.GetFullPath(modsDir);

//Open zip file index and extract only dll files
using (ZipArchive Archive = ZipFile.OpenRead(ZipPath))
{
foreach (ZipArchiveEntry Entry in Archive.Entries)
//Ensure that the mods dir path has a path seperator else add it.
//This stops a path traversal attack from the zip file
if (!ZipExtractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
{
if (Entry.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
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);
}
ZipExtractPath += Path.DirectorySeparatorChar;
}

//Check the Destination is in the mods dir, then extract
if (DestinationPath.StartsWith(modsDir, StringComparison.Ordinal))
//Open zip file index and extract only dll files
using (ZipArchive Archive = ZipFile.OpenRead(ZipPath))
{
foreach (ZipArchiveEntry Entry in Archive.Entries)
{
if (Entry.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
Entry.ExtractToFile(DestinationPath);
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))
{
Entry.ExtractToFile(DestinationPath);
}
}
}
}
}

//Delete Zip archive once we are done as we have the DLL's now
File.Delete(ZipPath);
//Delete Zip archive once we are done as we have the DLL's now
if (PMLConfig.ZipModMode)
{
File.Delete(ZipPath);
}
}
}

// Load mods
Expand Down
3 changes: 3 additions & 0 deletions PulsarModLoader/PMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class PMLConfig

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 SaveValue<DateTime> LastPMLUpdateCheck = new SaveValue<DateTime>("LastPMLUpdateCheck", DateTime.Today.AddDays(-2));

public static void SetDefault()
Expand Down

0 comments on commit 21821ee

Please sign in to comment.