-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
473 lines (376 loc) · 16.7 KB
/
MainWindow.xaml.cs
File metadata and controls
473 lines (376 loc) · 16.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using static MHWRoommates.Room;
namespace MHWRoommates
{
public partial class MainWindow : Window
{
private NPCList npcList;
public bool MoveBothWindows;
private readonly string curDir = Directory.GetCurrentDirectory();
public MainWindow(NPCList list)
{
MoveBothWindows = true;
InitializeComponent();
PopulateRoomComboBox();
npcList = list;
PopulateNPCComboBox();
PopulateAnimationsComboBox();
}
private void PopulateRoomComboBox()
{
Room_Select.Items.Add(LIVING_QUARTERS);
Room_Select.Items.Add(PRIVATE_QUARTERS);
Room_Select.Items.Add(PRIVATE_SUITE);
Room_Select.Items.Add(SELIANA_SUITE);
Room_Select.Items.Add(ASTERA);
Room_Select.Items.Add(SELIANA);
Room_Select.Items.Add(SELIANA_PUB);
Room_Select.Items.Add(RESEARCH_BASE);
}
private void PopulateNPCComboBox()
{
for (int i = 0; i < npcList.NPCs.Count; i++)
{
ComboBoxItem item = new ComboBoxItem();
SetWarningColour(npcList.NPCs[i], item);
if (npcList.NPCs[i].Warning != "Ignore")
{
item.Content = npcList.NPCs[i];
NPC_Select.Items.Add(item);
}
}
}
private void SetWarningColour(NPC npc, ComboBoxItem item)
{
switch (npc.Warning)
{
case "Crash": item.Background = Brushes.Red; item.Foreground = Brushes.White; break;
case "Missing": item.Background = Brushes.Gray; item.Foreground = Brushes.White; break;
case "Missing-Rooms": item.Background = Brushes.DarkGray; item.Foreground = Brushes.White; break;
case "Missing-Research": item.Background = Brushes.LightGray; break;
case "NoLoop": item.Background = Brushes.Purple; item.Foreground = Brushes.White; break;
case "Cheat": item.Background = Brushes.DarkRed; item.Foreground = Brushes.White; item.IsEnabled = false; break;
case "Bounds": item.Background = Brushes.Yellow; break;
case "Story": item.Background = Brushes.Pink; break;
case "Placeholder": item.Background = Brushes.Green; item.Foreground = Brushes.White; break;
case "Animation": item.Background = Brushes.Blue; item.Foreground = Brushes.White; break;
case "Iceborne":
case "Iceborne-Anim": item.Background = Brushes.LightSkyBlue; break;
}
}
private void PopulateAnimationsComboBox()
{
for (int i = 0; i < npcList.NPCs.Count; i++)
{
switch (npcList.NPCs[i].Warning)
{
case "Crash":
case "Ignore":
case "Placeholder":
case "Animation":
case "Cheat":
case "Iceborne-Anim":
break;
default: AddNPCAnimations(npcList.NPCs[i]); break;
}
}
}
private void AddNPCAnimations(NPC npc)
{
bool hasMultipleAnimations = npc.Animations != null && npc.Animations.Count > 0;
if (hasMultipleAnimations)
{
for (int j = 0; j < npc.Animations.Count; j++)
{
ComboBoxItem item = new ComboBoxItem();
NPC npcToAdd = new NPC
{
Name = $"{npc.Name} [{npc.Animations[j]:D4}]",
Animation = npc.Animations[j]
};
item.Content = npcToAdd;
Animation_Select.Items.Add(item);
}
} else {
ComboBoxItem item = new ComboBoxItem{ Content = npc };
Animation_Select.Items.Add(item);
}
}
private void Select_Button_Click(object sender, RoutedEventArgs e)
{
if (NPC_Select.SelectedItem == null || Room_Select.SelectedItem == null || NPC_Instance.Value == null)
return;
EnableControls();
NPC selectedNPC = ((ComboBoxItem)NPC_Select.SelectedItem).Content as NPC;
string npcFilePath = GetNPCFilePath(GetNPCDirectory());
if (!File.Exists(npcFilePath))
{
Delete_Button.IsEnabled = false;
FillControlsWithDefault(selectedNPC);
} else
{
Delete_Button.IsEnabled = true;
LoadNPC(npcFilePath);
}
}
private void EnableControls()
{
X_Position.IsEnabled = true;
Y_Position.IsEnabled = true;
Z_Position.IsEnabled = true;
X_Rotation.IsEnabled = true;
Y_Rotation.IsEnabled = true;
Z_Rotation.IsEnabled = true;
Animation_Select.IsEnabled = true;
Save_Button.IsEnabled = true;
Select_Button.IsEnabled = false;
}
private void FillControlsWithDefault(NPC selectedNPC)
{
Room selectedRoom = (Room_Select.SelectedItem as Room);
bool defaultExists = selectedNPC.DefaultRooms != null && selectedNPC.DefaultRooms.Contains(selectedRoom.ID);
int defaultPosID = (defaultExists) ? selectedNPC.DefaultRooms.IndexOf(selectedRoom.ID) : 0;
Point3D defaultPosition = (defaultExists)?
selectedNPC.DefaultPositions[defaultPosID] : selectedRoom.DefaultPosition;
X_Position.Value = defaultPosition.x;
Y_Position.Value = defaultPosition.y;
Z_Position.Value = defaultPosition.z;
Y_Rotation.Value = defaultPosition.r;
LoadAnimation(selectedNPC.Animation);
}
private void LoadNPC(string npcFilePath)
{
using (BinaryReader reader = new BinaryReader(File.Open(npcFilePath, FileMode.Open)))
{
reader.BaseStream.Position = RMOffsets.TRANSFORM_START;
X_Position.Value = reader.ReadSingle();
Y_Position.Value = reader.ReadSingle();
Z_Position.Value = reader.ReadSingle();
X_Rotation.Value = reader.ReadSingle();
Y_Rotation.Value = reader.ReadSingle();
Z_Rotation.Value = reader.ReadSingle();
reader.BaseStream.Position = RMOffsets.ANIM;
uint animationToLoad = reader.ReadUInt32();
LoadAnimation(animationToLoad);
}
}
private void LoadAnimation(uint animationToLoad)
{
var temp = Animation_Select.Items.OfType<ComboBoxItem>().First(x => ((NPC)x.Content).Animation == animationToLoad);
Animation_Select.SelectedItem = temp ?? Animation_Select.Items.GetItemAt(1);
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
string npcDirectory = GetNPCDirectory();
Directory.CreateDirectory(npcDirectory);
string npcFilePath = GetNPCFilePath(npcDirectory, NPC_Instance.Value.Value);
CreateBaseNPCFile(npcFilePath);
byte[] fsm = GetNpcFSM();
using (BinaryWriter writer = new BinaryWriter(File.Open(npcFilePath, FileMode.Open)))
{
SaveNPC(fsm, writer);
}
Delete_Button.IsEnabled = true;
MessageBox.Show($"Saved to \"{npcFilePath}\".", "NPC Saved", MessageBoxButton.OK, MessageBoxImage.Information);
}
private byte[] GetNpcFSM()
{
const int HOUSEKEEPER = 14;
const int SELIANA_KEEPER = 152;
NPC npc = (((ComboBoxItem)NPC_Select.SelectedItem).Content as NPC);
if (npc.Index != HOUSEKEEPER && npc.Index != SELIANA_KEEPER)
return new byte[] { };
byte[] fsm;
using (BinaryReader reader = new BinaryReader(File.Open(RMFiles.FSM, FileMode.Open)))
{
fsm = reader.ReadBytes((int)reader.BaseStream.Length);
}
return fsm;
}
private void SaveNPC(byte[] fsm, BinaryWriter writer)
{
NPC selectedNPC = (((ComboBoxItem)NPC_Select.SelectedItem).Content as NPC);
uint selectedAnimation = (((ComboBoxItem)Animation_Select.SelectedItem).Content as NPC).Animation;
writer.BaseStream.Position = RMOffsets.TRANSFORM_START;
writer.Write(X_Position.Value.Value);
writer.Write(Y_Position.Value.Value);
writer.Write(Z_Position.Value.Value);
writer.Write(X_Rotation.Value.Value);
writer.Write(Y_Rotation.Value.Value);
writer.Write(Z_Rotation.Value.Value);
writer.BaseStream.Position = RMOffsets.ID;
writer.Write(selectedNPC.Index);
writer.Write(selectedAnimation);
// Unknown value, usually matches animation or npcID, doesn't seem to affect anything?
writer.Write(selectedAnimation % 1000);
WriteFSMLink(fsm, writer, selectedNPC);
}
private void WriteFSMLink(byte[] fsm, BinaryWriter writer, NPC selectedNPC)
{
// Only supporting Housekeeper's FSM
const int HOUSEKEEPER = 14;
const int SELIANA_KEEPER = 152;
if (selectedNPC.Index != HOUSEKEEPER && selectedNPC.Index != SELIANA_KEEPER)
return;
string roomID = (Room_Select.SelectedItem as Room).ID;
string housekeeperID = (roomID=="506")? "707" : "016";
writer.BaseStream.Position = RMOffsets.FSM;
writer.Write(fsm);
writer.BaseStream.Position = RMOffsets.FSM_ROOM;
writer.Write(roomID.ToCharArray());
writer.BaseStream.Position = RMOffsets.FSM_FOLDER;
writer.Write(housekeeperID.ToCharArray());
writer.BaseStream.Position = RMOffsets.FSM_FILE;
writer.Write(housekeeperID.ToCharArray());
if (roomID == "501") // 501 fsm file appended with _000
{
writer.Write("_000".ToCharArray());
writer.Write(fsm, RMOffsets.FSM_END, 7);
} else if (roomID == "506") // Seliana Keeper's fsm file appended with _010
{
writer.Write("_010".ToCharArray());
writer.Write(fsm, RMOffsets.FSM_END, 7);
}
}
private static void CreateBaseNPCFile(string npcFilePath)
{
if (File.Exists(npcFilePath))
{
MakeNewNPCBackup(npcFilePath);
}
else
{
File.Copy(RMFiles.SOBJ, npcFilePath);
}
}
private static void MakeNewNPCBackup(string npcFilePath)
{
if (File.Exists($"{npcFilePath}.bak")) File.Delete($"{npcFilePath}.bak");
File.Copy(npcFilePath, $"{npcFilePath}.bak");
}
private string GetNPCFilePath(string npcDirectory)
{
return GetNPCFilePath(npcDirectory, NPC_Instance.Value.Value);
}
private string GetNPCFilePath(string npcDirectory, int instance)
{
string npcFile = npcDirectory;
int index = (((ComboBoxItem)NPC_Select.SelectedItem).Content as NPC).Index; //.npcid
npcFile += $"n{index:D3}_{instance:D3}.sobj";
return npcFile;
}
private string GetNPCDirectory()
{
string npcDirectory = curDir;
npcDirectory = (Room_Select.SelectedItem as Room).Path;
int index = (((ComboBoxItem)NPC_Select.SelectedItem).Content as NPC).Index; //.npcid
npcDirectory += $"n{index:D3}\\";
return npcDirectory;
}
private void NPCRoom_Select_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DisableControls();
NPC_Instance.Value = 0;
}
private void NPC_Instance_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
DisableControls();
}
private void DisableControls()
{
X_Position.IsEnabled = false;
Y_Position.IsEnabled = false;
Z_Position.IsEnabled = false;
X_Rotation.IsEnabled = false;
Y_Rotation.IsEnabled = false;
Z_Rotation.IsEnabled = false;
X_Position.Value = 0;
Y_Position.Value = 0;
Z_Position.Value = 0;
X_Rotation.Value = 0;
Y_Rotation.Value = 0;
Z_Rotation.Value = 0;
Animation_Select.IsEnabled = false;
Animation_Select.SelectedItem = null;
Delete_Button.IsEnabled = false;
Save_Button.IsEnabled = false;
NPC_Instance.IsEnabled = NPC_Select.SelectedItem != null && Room_Select.SelectedItem != null;
Select_Button.IsEnabled = NPC_Instance.IsEnabled && NPC_Instance.Value != null;
}
private void Delete_Button_Click(object sender, RoutedEventArgs e)
{
string npcDirectory = GetNPCDirectory();
string npcPath = GetNPCFilePath(npcDirectory);
string message = $"Are you sure you want to delete \"{npcPath}\"?";
MessageBoxResult result = MessageBox.Show(message, "Delete NPC?", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result.Equals(MessageBoxResult.Yes))
{
DeleteNPC(npcPath, npcDirectory);
}
}
private void DeleteNPC(string npcPath, string npcDirectory)
{
Delete_Button.IsEnabled = false;
string npcBackup = $"{npcPath}.bak";
DeleteNPCFile(npcPath, npcBackup);
DeleteNPCDirectory(npcDirectory);
}
private static void DeleteNPCFile(string npcPath, string npcBackup)
{
if (!File.Exists(npcPath))
return;
File.Delete(npcPath);
if (File.Exists(npcBackup)) File.Delete(npcBackup);
MessageBox.Show($"Deleted \"{npcPath}\".", "NPC Deleted!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
private static void DeleteNPCDirectory(string npcDirectory)
{
if (Directory.GetFiles(npcDirectory).Length <= 0)
Directory.Delete(npcDirectory);
}
private void Colour_Help_Button_Click(object sender, RoutedEventArgs e)
{
string colourInfo =
"Red: Possible Crash\n\n" +
"Pink: NPC is likely important to story and might mess something up. Don't @ me\n\n" +
"Yellow: Interacting with NPC might push you out of bounds\n\n" +
"Gray: Possibly doesn't appear, but won't crash the game. Can use their animations on other NPCs\n" +
" Shades from darkest to lightest: missing in all > missing in rooms > missing in just research\n\n" +
"Purple: Animation does not loop\n\n" +
"Blue: Animation either unkown or doesn't exist, can use animation from a different NPC\n\n" +
"Green: Likely a dummy NPC that won't normally appear in game. Also fits Blue\n\n" +
"Dark Red: Can be used to cheat, disabled by default\n\n" +
"Light Blue: Something to do with Iceborne. Be careful.";
MessageBox.Show(colourInfo, "Colour Key", MessageBoxButton.OK, MessageBoxImage.Question);
}
private void Window_LocationChanged(object sender, System.EventArgs e)
{
if (OwnedWindows.Count <= 0) return;
if (!MoveBothWindows) return;
EditRoomsWindow roomsWindow = (EditRoomsWindow)OwnedWindows[0];
roomsWindow.MoveBothWindows = false;
roomsWindow.Top = this.Top + this.Height;
roomsWindow.Left = this.Left;
roomsWindow.MoveBothWindows = true;
}
private void Window_Follow_CheckBox_Checked(object sender, RoutedEventArgs e)
{
setWindowFollow(true);
}
private void Window_Follow_CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
setWindowFollow(false);
}
private void setWindowFollow(bool isFollowing)
{
this.MoveBothWindows = isFollowing;
if (OwnedWindows.Count <= 0) return;
EditRoomsWindow roomsWindow = (EditRoomsWindow)OwnedWindows[0];
roomsWindow.MoveBothWindows = this.MoveBothWindows;
}
}
}