Skip to content

Commit

Permalink
Merge pull request #43 from X-Hax/master
Browse files Browse the repository at this point in the history
-
  • Loading branch information
TimTH98 authored Mar 24, 2024
2 parents f2ec83b + 4204219 commit 21c223c
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 211 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To use SA Mod Manager, you must have:
## How To Use
- Download the [latest version from GitHub](https://github.com/X-Hax/SA-Mod-Manager/releases/latest) then extract the zip anywhere.
- Run SAModManager.exe
- In the "Manager Config" tab, set your Game location and it will automatically install the Mod Loader.
- Your games should be detected automatically, but if they are not, you can set their location in the "Manager Config" tab and it will install the Mod Loader.
- Enjoy!

## Troubleshooting Guide
Expand Down
10 changes: 0 additions & 10 deletions SA-Mod-Manager/App.config
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SAModManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<SAModManager.Properties.Settings>
<setting name="GameValue" serializeAs="String">
<value>0</value>
</setting>
</SAModManager.Properties.Settings>
</userSettings>
</configuration>
4 changes: 2 additions & 2 deletions SA-Mod-Manager/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public partial class App : Application

public static string ManagerConfigFile = Path.Combine(ConfigFolder, "Manager.json");
public static ManagerSettings ManagerSettings { get; set; }
public static Profiles Profiles { get; set; }

private static readonly Mutex mutex = new(true, pipeName);
public static Updater.UriQueue UriQueue;
Expand Down Expand Up @@ -573,6 +574,7 @@ private static void HandleVanillaTransition(string[] args)
index++;
}
}

public static Uri GetResourceUri(string resourceName)
{
// Get the assembly where the resource is located
Expand Down Expand Up @@ -603,7 +605,6 @@ public static Uri GetResourceUri(string resourceName)
}
}


private static ManagerSettings LoadManagerConfig()
{
ManagerSettings settings = ManagerSettings.Deserialize(Path.Combine(ConfigFolder, ManagerConfigFile));
Expand All @@ -621,7 +622,6 @@ private static ManagerSettings LoadManagerConfig()
return settings;
}


public static async Task EnableOneClickInstall()
{
try
Expand Down
4 changes: 4 additions & 0 deletions SA-Mod-Manager/Configuration/ManagerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using SAModManager.UI;
using SAModManager.Configuration.SA2;
using SAModManager.Configuration.SADX;
using System.Collections.Generic;
using System.Security.RightsManagement;

namespace SAModManager.Configuration
{
Expand Down Expand Up @@ -153,6 +155,8 @@ public class ManagerSettings
/// </summary>
[IniAlwaysInclude]
public UpdateSettings UpdateSettings { get; set; } = new();
//store game id installed
public List<uint> gamesInstalled { get; set; } = new();

/// <summary>
/// Deserializes a Manager Settings CFG (JSON) file and returns a populated class.
Expand Down
204 changes: 204 additions & 0 deletions SA-Mod-Manager/Configuration/ProfileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using SAModManager.Configuration.SA2;
using SAModManager.Configuration.SADX;
using SAModManager.Ini;
using SAModManager.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SAModManager.Configuration
{
/// <summary>
/// Static Class to manage the App Profiles.
/// </summary>
static public class ProfileManager
{
private static string GetProfilePath()
{
return Path.Combine(App.CurrentGame.ProfilesDirectory, "Profiles.json");
}

/// <summary>
/// Checks to ensure an index is within the bounds of the App's Profile List.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private static bool CheckIndexBounds(int index)
{
if (!(index > App.Profiles.ProfilesList.Count) && !(index < 0))
return true;
else
return false;
}

/// <summary>
/// Converts Old Loader File to new GameSettings format.
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="destFile"></param>
private static void ConvertProfile(string sourceFile, string destFile)
{
sourceFile = Path.Combine(App.CurrentGame.gameDirectory, "mods", sourceFile);

try
{
switch (App.CurrentGame.id)
{
case SetGame.SADX:
SADX.GameSettings sadxSettings = new();
SADXLoaderInfo sadxLoader = IniSerializer.Deserialize<SADXLoaderInfo>(sourceFile);
sadxSettings.ConvertFromV0(sadxLoader);
sadxSettings.Serialize(App.CurrentGame.ProfilesDirectory, destFile);
break;
case SetGame.SA2:
SA2.GameSettings sa2Settings = new();
SA2LoaderInfo sa2Loader = IniSerializer.Deserialize<SA2LoaderInfo>(sourceFile);
sa2Settings.ConvertFromV0(sa2Loader);
sa2Settings.Serialize(App.CurrentGame.ProfilesDirectory, destFile);
break;
}
}
catch (Exception ex)
{
//new ExceptionHandler(ex);
return;
}
}

/// <summary>
/// Saves the App Profile's file.
/// </summary>
public static void SaveProfiles()
{
App.Profiles.Serialize(GetProfilePath());
}

/// <summary>
/// Add a profile to the App's Profiles List.
/// </summary>
/// <param name="name"></param>
public static void AddProfile(string name)
{
App.Profiles.ProfilesList.Add(new ProfileEntry(name, name + ".json"));
}

/// <summary>
/// Remove a profile from the App's Profile List by name.
/// </summary>
/// <param name="name"></param>
public static void RemoveProfile(string name)
{
foreach (ProfileEntry entry in App.Profiles.ProfilesList)
{
if (entry.Name == name)
App.Profiles.ProfilesList.Remove(entry);
}
}

/// <summary>
/// Remove a profile from the App's Profile List by index.
/// </summary>
/// <param name="index"></param>
public static void RemoveProfile(int index)
{
if (CheckIndexBounds(index))
App.Profiles.ProfilesList.RemoveAt(index);
}

/// <summary>
/// Sets the App's Profiles.
/// </summary>
public static void SetProfile()
{
App.Profiles = Profiles.Deserialize(GetProfilePath());
}

/// <summary>
/// Automatically migrates any profiles found in the Game Install>mods directory to the new GameSettings format.
/// Set deletesource to true if you want to delete the source file when conversion is completed.
/// </summary>
public static void MigrateProfiles(bool deletesource = false)
{
if (App.CurrentGame.gameDirectory != "")
{
if (Directory.Exists(App.CurrentGame.gameDirectory))
{
if (!Directory.Exists(App.CurrentGame.ProfilesDirectory))
Directory.CreateDirectory(App.CurrentGame.ProfilesDirectory);

foreach (var item in Directory.EnumerateFiles(Path.Combine(App.CurrentGame.gameDirectory, "mods"), "*.ini"))
{
string sourceFile = Path.GetFileName(item);
string destFile =
Path.GetFileNameWithoutExtension(sourceFile) == App.CurrentGame.loader.name ? "Default.json" : Path.GetFileNameWithoutExtension(sourceFile) + ".json";

File.Copy(Path.GetFullPath(item), Path.Combine(App.CurrentGame.ProfilesDirectory, destFile), true);
ConvertProfile(sourceFile, destFile);

if (deletesource)
File.Delete(Path.GetFullPath(sourceFile));
}
}
}
}

/// <summary>
/// This should only ever be run a single time on boot/initial install for a game.
/// The MigrateProfiles function is public and should be used in any other instance of mass migrating.
/// </summary>
public static void CreateProfiles()
{
MigrateProfiles(false);

Profiles profiles = new();

foreach (var item in Directory.EnumerateFiles(App.CurrentGame.ProfilesDirectory, "*.json"))
{
if (Path.GetFileName(item) != "Profiles.json")
profiles.ProfilesList.Add(new ProfileEntry(Path.GetFileNameWithoutExtension(item), Path.GetFileName(item)));
}

profiles.Serialize(GetProfilePath());

App.Profiles = profiles;
}

/// <summary>
/// Gets currently selected Profile as a ProfileEntry.
/// </summary>
/// <returns></returns>
public static ProfileEntry GetCurrentProfile()
{
return App.Profiles.ProfilesList[App.Profiles.ProfileIndex];
}

/// <summary>
/// Gets a profile from a specific index, returns null if index is out of bounds.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public static ProfileEntry GetProfile(int index)
{
if (CheckIndexBounds(index))
return App.Profiles.ProfilesList[index];
else
return null;
}

public static void ValidateProfileIndex()
{
if (App.Profiles.ProfileIndex < 0 || App.Profiles.ProfileIndex > App.Profiles.ProfilesList.Count)
App.Profiles.ProfileIndex = 0;
}

public static void ValidateProfiles()
{
ValidateProfileIndex();
App.Profiles.ValidateProfiles();
}
}
}
7 changes: 7 additions & 0 deletions SA-Mod-Manager/Configuration/Profiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public void Serialize(string path)
}
}

/// <summary>
/// Runs a validation check on profiles to ensure that no files have been manually deleted or manually renamed. Removes any missing/renamed entries.
/// </summary>
public bool ValidateProfiles()
{
try
Expand Down Expand Up @@ -178,6 +181,10 @@ public bool ValidateProfiles()
return true;
}

/// <summary>
/// Creates a Default Profile file.
/// </summary>
/// <returns></returns>
public static Profiles MakeDefaultProfileFile()
{
return new()
Expand Down
5 changes: 3 additions & 2 deletions SA-Mod-Manager/Configuration/SA2/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ public static GameSettings Deserialize(string path)
/// <param name="path"></param>
public void Serialize(string path, string profileName)
{
try
// TODO: Fix this function.
path = Path.Combine(App.CurrentGame.ProfilesDirectory, profileName);
try
{
if (Directory.Exists(App.CurrentGame.ProfilesDirectory))
{
Expand All @@ -480,7 +482,6 @@ public void Serialize(string path, string profileName)
Directory.CreateDirectory(App.CurrentGame.ProfilesDirectory);
if (Directory.Exists(App.CurrentGame.ProfilesDirectory))
{
path = Path.Combine(App.CurrentGame.ProfilesDirectory, profileName);
string jsonContent = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, jsonContent);
}
Expand Down
8 changes: 1 addition & 7 deletions SA-Mod-Manager/Configuration/SA2/SA2GameConfig.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using SAModManager.Ini;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;

namespace SAModManager.Configuration.SA2
Expand Down
3 changes: 2 additions & 1 deletion SA-Mod-Manager/Configuration/SADX/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,8 @@ public static GameSettings Deserialize(string path)
/// <param name="path"></param>
public void Serialize(string path, string profileName)
{
// TODO: Fix this function
path = Path.Combine(path, profileName);
try
{
if (Directory.Exists(App.CurrentGame.ProfilesDirectory))
Expand All @@ -894,7 +896,6 @@ public void Serialize(string path, string profileName)
Directory.CreateDirectory(App.CurrentGame.ProfilesDirectory);
if (Directory.Exists(App.CurrentGame.ProfilesDirectory))
{
path = Path.Combine(App.CurrentGame.ProfilesDirectory, profileName);
string jsonContent = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, jsonContent);
}
Expand Down
18 changes: 15 additions & 3 deletions SA-Mod-Manager/FormBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,26 @@ public static UIElement CreateNumericBox(ConfigSchemaProperty property, CustomPr
};
panel.Children.Add(backing);

Decimal numVal = 0;
if (!Decimal.TryParse(storeInfo.GetConfigValue().ToString(), out numVal))
numVal = 0;
Decimal numMax = Decimal.MaxValue;
if (property.MaxValue.ToString() != "")
if (!Decimal.TryParse(property.MaxValue.ToString(), out numMax))
numMax = Decimal.MaxValue;
Decimal numMin = Decimal.MinValue;
if (property.MinValue.ToString() != "")
if (!Decimal.TryParse(property.MinValue.ToString(), out numMin))
numMin = Decimal.MinValue;

NumberBox element = new()
{
MinWidth = 100,
Height = 22,
Value = Decimal.Parse(storeInfo.GetConfigValue().ToString()),
Value = numVal,
HorizontalAlignment = HorizontalAlignment.Right,
MinValue = property.MinValue != "" ? Decimal.Parse(property.MinValue.ToString()) : Decimal.MinValue,
MaxValue = property.MaxValue != "" ? Decimal.Parse(property.MaxValue.ToString()) : Decimal.MaxValue,
MinValue = numMin,
MaxValue = numMax,
Tag = storeInfo
};

Expand Down
Loading

0 comments on commit 21c223c

Please sign in to comment.