Skip to content

Commit

Permalink
Portable Path saving
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSomeGuy1234 committed May 15, 2022
1 parent c3069a4 commit 4598ede
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions XeniaPatchUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,48 @@ public MainWindow()
{
throw new ArgumentException("Patch folder instance not found!");
}
UpdatePatchFiles();
PatchFolder.patchFolder.PortableFolder = @"C:\path\to\portableXeniaCanary\patches\"; // TODO: Create a config file to save portable path, and add game favouriting

LoadAppConfig();
patchfileListBox.DataContext = PatchFolder.patchFiles;
portableCheckbox.DataContext = PatchFolder.patchFolder.IsPortable;
//portableCheckbox.DataContext = PatchFolder.patchFolder.IsPortable; // wot

patchfileListBox.Items.SortDescriptions.Add(new SortDescription("GameName", ListSortDirection.Ascending));
}


// Config is split between MainWindow and PatchFolder. See PatchFolder.UpdateAppConfig
// Calling this function calls UpdatePatchFiles which ends up calling UpdateAppConfig, so basically on startup we load settings then save them again which we probably shouldn't do but meh.
void LoadAppConfig()
{
string isportable = "False";
string portablepath = @"C:\path\to\portableXeniaCanary\patches\";

bool isPortableBool = false;

if (!File.Exists(PatchFolder.ConfigPath))
{
File.WriteAllText(PatchFolder.ConfigPath,
$"IsPortable = \"{isportable}\"\n" +
$"PortablePath = \"{portablepath}\"\n"
);
}
else
{
string[] configLines = File.ReadAllLines(PatchFolder.ConfigPath);
foreach (string line in configLines)
{
if (line.Contains("IsPortable"))
{
isPortableBool = PatchFile.GetStringBetweenFirstQuotePair(line) == "True";
}
else if (line.Contains("PortablePath"))
{
portablepath = PatchFile.GetStringBetweenFirstQuotePair(line);
}
}
}
PatchFolder.patchFolder.IsPortable = isPortableBool;
PatchFolder.patchFolder.PortableFolder = portablepath;
UpdatePatchFiles();
}

private void Window_Drop(object sender, DragEventArgs e)
{
Expand Down Expand Up @@ -102,7 +134,8 @@ private void portablePathTextBox_KeyDown(object sender, KeyEventArgs e)
public void UpdatePatchFiles(object sender = null, RoutedEventArgs e = null)
{
PatchFolder.patchFolder.PopulatePatchFolder();
PatchFolder.patchFolder.PatchesEnabled = PatchFolder.patchFolder.GetIfPatchesEnabledInConfig();
PatchFolder.patchFolder.PatchesEnabled = PatchFolder.patchFolder.GetIfPatchesEnabledInXeniaConfig();
PatchFolder.UpdateAppConfig();

}

Expand All @@ -123,6 +156,7 @@ public class PatchFolder : INotifyPropertyChanged
public static ObservableCollection<PatchFile> patchFiles = new ObservableCollection<PatchFile>();
const string NonPortableFolder = @"C:\%homepath%\Documents\Xenia\patches\";
const string RelativeXeniaConfigPath = @"\..\xenia-canary.config.toml";
public static string ConfigPath = AppContext.BaseDirectory + @"\config.cfg";

bool patchesEnabled;
public bool PatchesEnabled {
Expand Down Expand Up @@ -166,6 +200,7 @@ public bool IsPortable
set
{
isPortable = value;
UpdateAppConfig();
OnPropertyChanged();
}
}
Expand Down Expand Up @@ -244,7 +279,7 @@ public bool UpdateXeniaConfig(bool enablePatches)
}

// Called when updating the patch folder
public bool GetIfPatchesEnabledInConfig()
public bool GetIfPatchesEnabledInXeniaConfig()
{
string finalConfigPath = System.IO.Path.GetFullPath(Environment.ExpandEnvironmentVariables(IsPortable ? PortableFolder + RelativeXeniaConfigPath : NonPortableFolder + RelativeXeniaConfigPath));
if (!File.Exists(finalConfigPath))
Expand All @@ -262,6 +297,16 @@ public bool GetIfPatchesEnabledInConfig()
return false;
}

// Other part of app config. I know reading and writing the config should really be together but whatever.
public static void UpdateAppConfig()
{
// In the future we should take a less rudimentary approach if we have different things in different classes to save.
File.WriteAllText(PatchFolder.ConfigPath,
$"IsPortable = \"{PatchFolder.patchFolder.IsPortable}\"\n" +
$"PortablePath = \"{PatchFolder.patchFolder.PortableFolder}\"\n"
);
}

protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
Expand Down

0 comments on commit 4598ede

Please sign in to comment.