Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Auricrystal committed Aug 21, 2023
1 parent 43d2436 commit 4a81825
Show file tree
Hide file tree
Showing 815 changed files with 7,613 additions and 252 deletions.
Binary file modified .vs/RemnantWorldChanger/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/RemnantWorldChanger/v17/.suo
Binary file not shown.
44 changes: 37 additions & 7 deletions BulkSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;
using static RemnantWorldChanger.DataPackage;

namespace RemnantWorldChanger
Expand All @@ -28,7 +29,7 @@ public static T RandomEnumValue<T>()
}
public ObservableCollection<DataPackage> SaveInfo { get; set; }

private Dictionary<Guid, byte[]> GuidToBytes { get; set; }
public Dictionary<Guid, byte[]> GuidToBytes { get; set; }

public BulkSave(ObservableCollection<DataPackage> header, Dictionary<Guid, byte[]> data)
{
Expand All @@ -48,14 +49,14 @@ public void AddSave(byte[] savedata, SaveType type = SaveType.All, string world
GuidToBytes.Add(guid, savedata);
var dp = new DataPackage(guid) { Difficulty = diff, World = world, Name = name, Mods = mods, Type = type };
SaveInfo.Add(dp);
//Debug.WriteLine("Created: "+dp);
}
public void AddSave(byte[] savedata, DataPackage dp)
{
GuidToBytes.Add(dp.ID, savedata);
SaveInfo.Add(dp);
}


public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
{
var directory = new DirectoryInfo(
Expand All @@ -67,7 +68,6 @@ public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = nul
return directory;
}

[Conditional("DEBUG")]
public void SerializeData(string name)
{
if (!Directory.Exists(name))
Expand All @@ -86,16 +86,37 @@ public void SerializeData(string name)
}
public static BulkSave DeserializeData(string path)
{
var header = new ObservableCollection<DataPackage>(Directory.EnumerateFiles(path, "*.RIndex").Select(s => JsonSerializer.Deserialize<ObservableCollection<DataPackage>>(File.ReadAllText(s))).SelectMany(x => x));
var indexes = Directory.EnumerateFiles(path, "*.RIndex");
if (indexes.Count() > 1)
MessageBox.Show("Multiple Data Files detected!");

ObservableCollection<DataPackage> header = new ObservableCollection<DataPackage>();
Dictionary<Guid, byte[]> bulk = new Dictionary<Guid, byte[]>();

foreach (string index in indexes)
{
var data = Path.ChangeExtension(index, ".RData");
if (!File.Exists(data))
{
MessageBox.Show($"{Path.GetFileName(data)} does not exist!!");
continue;
}
header = new ObservableCollection<DataPackage>(header.Union(JsonSerializer.Deserialize<ObservableCollection<DataPackage>>(File.ReadAllText(index))));

JsonSerializer.Deserialize<Dictionary<Guid, byte[]>>(File.ReadAllText(data)).ToList().ForEach(pair => bulk[pair.Key] = pair.Value);

var data = Directory.EnumerateFiles(path, "*.RData").Select(s => JsonSerializer.Deserialize<Dictionary<Guid, byte[]>>(File.ReadAllText(s))).SelectMany(x => x).ToDictionary(x => x.Key, y => y.Value);
}

return new BulkSave(header, data);
//var header = new ObservableCollection<DataPackage>(Directory.EnumerateFiles(path, "*.RIndex").Select(s => JsonSerializer.Deserialize<ObservableCollection<DataPackage>>(File.ReadAllText(s))).SelectMany(x => x).GroupBy(x=>x.ID).Select(x=>x.First()));

// var data = Directory.EnumerateFiles(path, "*.RData").Select(s => JsonSerializer.Deserialize<Dictionary<Guid, byte[]>>(File.ReadAllText(s))).SelectMany(x => x).GroupBy(kvp=>kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);

return new BulkSave(header, bulk);
}

}

public class DataPackage
public class DataPackage : IEquatable<DataPackage>
{
public enum SaveDifficulty { Unset, Survivor, Veteran, Nightmare, Apocalypse }
public enum SaveType { All, MiniBoss, WorldBoss, SideD, OverworldPOI, Vendor }
Expand Down Expand Up @@ -123,6 +144,15 @@ public DataPackage(Guid guid, SaveType type = SaveType.All, SaveDifficulty diff
{
return $"Type: {Type.ToString()} World: {World} Name: {Name} Difficulty: {Difficulty.ToString()} Mods: {Mods} GUID: {ID}";
}

public bool Equals(DataPackage? other)
{
if (other is null)
return false;
return this.Difficulty == other.Difficulty && this.Name == other.Name && this.Mods == other.Mods;
}
public override bool Equals(object obj) => Equals(obj as DataPackage);
public override int GetHashCode() => (Difficulty, Name, Mods).GetHashCode();
}

}
70 changes: 70 additions & 0 deletions DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Runtime.Intrinsics.Arm;
using System.Diagnostics;

namespace RemnantWorldChanger
{
public static class DictionaryExtensions
{
public static bool ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict,
TKey oldKey, TKey newKey)
{
if (!dict.Remove(oldKey, out var value))
return false;

dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort
return true;
}
public static bool NewPackage(this BulkSave Saves, out DataPackage dp)
{
Guid guid = Guid.NewGuid();
return EditPackage(Saves, dp = new DataPackage(guid), false);
}
public static bool EditPackage(this BulkSave Saves, DataPackage dp, bool editmode = true)
{
SaveEditor editor = new SaveEditor(dp);

if (editor.ShowDialog() == false)
{
Debug.WriteLine("Dialog Result False");
return false;
}

if (editor.Save is null)
{
Debug.WriteLine("Editor Save Null");
return false;
}


if (editmode && dp.ID != editor.Save.ID)
{
var _ = MessageBox.Show($"Warning GUID Mismatch:\n\nOld: {dp.ID}\nNew: {editor.Save.ID}\n\nChange GUID?", "GUID Mismatch", MessageBoxButton.YesNoCancel);
if (_ == MessageBoxResult.Cancel)
return false;

if (_ == MessageBoxResult.No)
editor.Save.ID = dp.ID;
else
{
Saves.GuidToBytes.ChangeKey(dp.ID, editor.Save.ID);
}
}

if ((editmode == Saves.SaveInfo.Remove(dp)))
Saves.SaveInfo.Add(editor.Save);
else
return false;
return true;


}
}
}
8 changes: 6 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<DockPanel Background="#220000">
<Menu DockPanel.Dock="Top" Height="20" VerticalAlignment="Top">
<MenuItem Header="Options">
<MenuItem Header="Auto-Update" IsCheckable="True"/>
<MenuItem Header="Check for update"/>
<MenuItem Header="Auto-Update" IsCheckable="True" IsEnabled="False"/>
<MenuItem Header="Check for update" IsEnabled="False"/>
<MenuItem Header="Open Data Folder" Click="ViewDataFolder_Click"/>
<MenuItem Name="btnGenExample" Header="Generate Test Saves" Click="GenerateExamples_Click" Visibility="Collapsed" IsEnabled="false"/>
</MenuItem>
<CheckBox Name="cbKeepSave" Content="Keep Checkpoint" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/>
Expand All @@ -32,6 +33,7 @@
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete Save" Click="DeleteSave_Click"/>
<MenuItem Header="Edit Save" Click="SaveEdit_Click"/>
<MenuItem Header="Load Save" Click="LoadSave_Click"/>
</ContextMenu>
Expand All @@ -48,6 +50,7 @@
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete Save" Click="DeleteSave_Click"/>
<MenuItem Header="Edit Save" Click="SaveEdit_Click"/>
<MenuItem Header="Load Save" Click="LoadSave_Click"/>
</ContextMenu>
Expand All @@ -63,6 +66,7 @@
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete Save" Click="DeleteSave_Click"/>
<MenuItem Header="Edit Save" Click="SaveEdit_Click"/>
<MenuItem Header="Load Save" Click="LoadSave_Click"/>
</ContextMenu>
Expand Down
86 changes: 48 additions & 38 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
Expand Down Expand Up @@ -99,6 +100,8 @@ private void EnableDebugOptions()
btnGenExample.IsEnabled = true;
}



private void SaveCheckpoint_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Saving Checkpoint.....");
Expand All @@ -110,8 +113,12 @@ private void SaveCheckpoint_Click(object sender, RoutedEventArgs e)

DataPackage? dp;
if (ofd.ShowDialog() == true)
if ((dp = NewPackage()) is not null)
Saves.AddSave(File.ReadAllBytes(ofd.FileName), dp);
if (Saves.NewPackage(out dp))
Saves.GuidToBytes.Add(dp.ID, File.ReadAllBytes(ofd.FileName));
else
Debug.WriteLine("New Package Failed!");
else
Debug.WriteLine("Dialog False");

SaveList.SelectedIndex = 0;
ViewUpdate();
Expand Down Expand Up @@ -208,48 +215,15 @@ public bool Exists(string name, SaveDifficulty diff, string mod, out DataPackage
return true;
}

private DataPackage? NewPackage()
{
Guid guid = Guid.NewGuid();
return EditPackage(new DataPackage(guid));
}
private DataPackage? EditPackage(DataPackage dp)
{
SaveEditor editor = new SaveEditor() { };
var ser = new JsonSerializerOptions
{
WriteIndented = true
};
ser.Converters.Add(new JsonStringEnumConverter());
editor.EditorWindow.Text = JsonSerializer.Serialize<DataPackage>(dp, ser);


if (editor.ShowDialog() == false)
return null;

if (editor.Save is null)
return null;

return editor.Save;


}
private void SaveEdit_Click(object sender, RoutedEventArgs e)
{
var dp = FindSelected();
if (dp != null)
{
Debug.WriteLine($"FOUND: {dp} GUID:{dp.ID}");
DataPackage? dpo;
if ((dpo = EditPackage(dp)) is not null)
if (Saves.SaveInfo.Remove(dp))
Saves.SaveInfo.Add(dpo);
SaveList.ItemsSource = Saves.SaveInfo.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.First().World);
DifficultyList.ItemsSource = Saves.SaveInfo.Select(x => x.Difficulty).Distinct();
ModifierList.ItemsSource = Saves.SaveInfo.Select(x => x.Mods).Distinct();

ViewUpdate();

if (Saves.EditPackage(dp))
ViewUpdate();
}
else { Debug.WriteLine($"Editor: False"); }
}
Expand Down Expand Up @@ -284,8 +258,20 @@ private void Save_SelectionChanged(object sender, SelectionChangedEventArgs e)

private void LoadSave_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine($"Loading: {FindSelected()}");
DataPackage? _ = FindSelected();
Debug.WriteLine($"Loading: {_}");
if (_ is null)
return;

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "World Saves (.sav)|save_*.sav";
ofd.DefaultExt = ".sav";
ofd.Title = "Overwrite Save";

if (ofd.ShowDialog() == true)
File.WriteAllBytes(ofd.FileName, Saves.GuidToBytes[_.ID]);
else
Debug.WriteLine("Load Save Failed!");
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -351,5 +337,29 @@ private void LockedSave_Changed(object sender, FileSystemEventArgs e)
});
SaveWatcher.EnableRaisingEvents = true;
}

private void ViewDataFolder_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(Environment.GetEnvironmentVariable("WINDIR") + @"\explorer.exe", Packages);
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void DeleteSave_Click(object sender, RoutedEventArgs e)
{
var _ = FindSelected();
if (_ == null)
return;
var result = MessageBox.Show($"Are you sure you want to delete: \n\nDifficulty: {_.Difficulty}\nName: {_.Name}\nMods: {_.Mods} ", "WARNING", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
if (Saves.GuidToBytes.Remove(_.ID))
Saves.SaveInfo.Remove(_);
ViewUpdate();
}
}
}
17 changes: 17 additions & 0 deletions Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0-windows</TargetFramework>
<SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
10 changes: 10 additions & 0 deletions Properties/PublishProfiles/FolderProfile.pubxml.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>True|2023-08-21T18:59:25.2633679Z;True|2023-08-21T12:01:47.2938761-04:00;False|2023-08-21T12:01:22.4718346-04:00;True|2023-08-21T12:00:32.1813074-04:00;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>
Loading

0 comments on commit 4a81825

Please sign in to comment.