-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainWindow.xaml.cs
184 lines (152 loc) · 7.14 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Microsoft.Win32;
using Newtonsoft.Json;
using Save_Editor.Models;
namespace Save_Editor {
public partial class MainWindow {
private static readonly string TARGET_FOLDER = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%Low\Cradle Games\Hellpoint\");
private const string CMP_FILE = @"R:\Games\Hellpoint\Save Compare\S1.json";
public SaveFile saveFile { get; private set; }
public string targetFile;
public MainWindow() {
if (!LoadFile()) {
Application.Current.Shutdown();
return;
}
if (!Data.BREACHES.ContainsKey(saveFile.world.breach)) {
Data.BREACHES.Add(saveFile.world.breach, $"Unknown ({saveFile.world.breach})");
}
if (!Data.COVENANTS.ContainsKey(saveFile.player.covenant)) {
Data.COVENANTS.Add(saveFile.player.covenant, $"Unknown ({saveFile.player.covenant})");
}
InitializeComponent();
// Setup App-wide Ctrl+S.
var saveAll = new RoutedCommand();
var ib = new InputBinding(saveAll, new KeyGesture(Key.S, ModifierKeys.Control));
InputBindings.Add(ib);
// Bind handler.
var cb = new CommandBinding(saveAll);
cb.Executed += (sender, args) => SaveFile();
CommandBindings.Add(cb);
//var f = Global.ToHexString(new Guid("363d739b-9a36-4a9d-9dc1-2487b92eb475").ToByteArray());
//var s = "B8 27 8A 7A AA D3 BB 49 AE 39 96 30 46 51 EF 39".FromHexStringToGuidString();
ExtractData();
}
private void ExtractData() {
try {
var toWrite = $"Breach: {Data.GetNameOrId(saveFile.world.breach)},\r\n";
toWrite += $"Covenant: {Data.GetNameOrId(saveFile.player.covenant)},\r\n";
var index = -1;
foreach (var slot in saveFile.player.slots) {
index++;
if (!slot.used) continue;
toWrite += $"Slot {(Slot.Index) index}:\r\n";
foreach (var item in slot.items) {
if (item == -1) continue;
toWrite += $" {saveFile.player.items[item].nameOrId},\r\n";
}
}
toWrite += JsonConvert.SerializeObject(saveFile.world.sStates, Formatting.Indented) + "\r\n";
foreach (var item in saveFile.player.items) {
if (Data.ALL_IDS.ContainsKey(item.id)) continue;
toWrite += ("{\r\n" +
$" \"id\": \"{item.id}\",\r\n" +
$" \"count\": {item.count},\r\n" +
$" \"selection\": {item.selection},\r\n" +
$" \"values\": [{string.Join(", ", item.values)}],\r\n" +
$" \"sockets\": [{string.Join(", ", item.sockets)}]\r\n" +
"},\r\n");
}
File.WriteAllText(CMP_FILE, toWrite);
} catch (Exception) {
// ignored
}
}
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
private bool LoadFile() {
var target = GetOpenTarget();
if (string.IsNullOrEmpty(target)) {
Application.Current.Shutdown();
return false;
}
targetFile = target;
var json = File.ReadAllText(targetFile, Encoding.UTF8);
saveFile = JsonConvert.DeserializeObject<SaveFile>(json);
ReIndexItems();
saveFile.player.items.CollectionChanged += Items_CollectionChanged;
return true;
}
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
private void SaveFile() {
var target = GetSaveTarget();
if (string.IsNullOrEmpty(target)) return;
var json = JsonConvert.SerializeObject(saveFile, Formatting.None);
File.WriteAllText(target, json);
}
private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if (e.Action == NotifyCollectionChangedAction.Remove) {
var position = e.OldStartingIndex;
foreach (var slot in saveFile.player.slots) {
for (var i = 0; i < slot.items.Count; i++) {
var item = slot.items[i];
if (item == position) throw new InvalidOperationException("Cannot remove an item in use.");
if (item > position) slot.items[i] -= 1;
}
}
foreach (var item in saveFile.player.items) {
for (var i = 0; i < item.sockets.Length; i++) {
var socketItem = item.sockets[i];
if (socketItem == position) throw new InvalidOperationException("Cannot remove an item in use.");
if (socketItem > position) item.sockets[i] -= 1;
}
}
}
}
private string GetOpenTarget() {
var ofdResult = new OpenFileDialog {
Filter = "JSON(HP)|*.hp",
Multiselect = false,
InitialDirectory = targetFile == null ? TARGET_FOLDER : Path.GetDirectoryName(targetFile) ?? TARGET_FOLDER
};
ofdResult.ShowDialog();
return ofdResult.FileName;
}
private string GetSaveTarget() {
var sfdResult = new SaveFileDialog {
Filter = "JSON(HP)|*.hp",
FileName = $"{Path.GetFileNameWithoutExtension(targetFile)}",
InitialDirectory = targetFile == null ? TARGET_FOLDER : Path.GetDirectoryName(targetFile) ?? TARGET_FOLDER,
AddExtension = true
};
return sfdResult.ShowDialog() == true ? sfdResult.FileName : null;
}
private void AddItem_OnClick(object sender, RoutedEventArgs e) {
if (cb_item_to_add.SelectedValue == null) return;
var itemToAdd = (Guid) cb_item_to_add.SelectedValue;
saveFile.player.items.Add(new Item(itemToAdd));
ReIndexItems();
}
private void ReIndexItems() {
for (var i = 0; i < saveFile.player.items.Count; i++) {
saveFile.player.items[i].index = i;
}
CollectionViewSource.GetDefaultView(saveFile.player.items).Refresh();
}
private void DgItems_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
switch (e.PropertyName) {
case nameof(Item.nameOverride):
case nameof(Item.nameOrId):
e.Cancel = true;
break;
}
}
}
}