-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCustomActorDatabase.cs
More file actions
636 lines (527 loc) · 25.4 KB
/
CustomActorDatabase.cs
File metadata and controls
636 lines (527 loc) · 25.4 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using OpenTK.Input;
using System.IO;
using System.Globalization;
using System.Net;
using System.Reflection;
using Ionic.Zip;
using Tommy;
namespace SharpOcarina
{
public partial class CustomActorDatabase : Form
{
string[] actor_categories = { "Puzzle", "Enemy", "Boss", "NPC", "Utility", "Other", "Dependency" };
//string path = "Z:\\Users\\Noka\\Documents\\GitHub\\CustomActorDatabase\\";
string website = "https://raw.githubusercontent.com/Nokaubure/CustomActorDatabase/";
public List<DatabaseCustomActor> Database;
public XmlNodeList nodes;
public string filter = "";
string tempw = "";
public MainForm mainform;
public WebClient client;
public List<CustomActorz64rom> z64romactors = new List<CustomActorz64rom>();
public List<CustomObjectz64rom> z64romobjects = new List<CustomObjectz64rom>();
public bool reload = false;
public List<CustomActorNode> CustomActorNodes = new List<CustomActorNode>();
public CustomActorDatabase(MainForm _mainform)
{
mainform = _mainform;
InitializeComponent();
Init();
UpdateWindow();
}
public void Init()
{
foreach (string category in actor_categories)
{
ToolStripMenuItem MenuItem = new System.Windows.Forms.ToolStripMenuItem() { Name = category, Text = category };
MenuItem.Click += new System.EventHandler(this.SearchCategory);
CategoriesButton.DropDownItems.Add(MenuItem);
}
tempw = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tempw\\");
client = Helpers.DownloadTemporalFile(website + "master/CustomActors.xml");
XmlDocument doc = new XmlDocument();
#if DEBUG
var fileName = "Z:\\Users\\Noka\\Documents\\GitHub\\CustomActorDatabase\\CustomActors.xml";
#else
var fileName = tempw + "CustomActors.xml";
#endif
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
doc.Load(fs);
#if DEBUG
XmlNodeList nodes = doc.SelectNodes("Table/Actor | Table/Actor2 | Table/Debug");
#else
XmlNodeList nodes = doc.SelectNodes("Table/Actor | Table/Actor2");
#endif
Database = new List<DatabaseCustomActor>();
// FilterTextBox.Text = filter;
//List<ushort> index_list = new List<ushort>();
if (rom64.isSet())
{
List<String> actors = rom64.getList("src\\actor\\");
foreach (String str in actors)
{
string basename = "";
ushort index = 0;
int version = -1;
if (!rom64.getNameAndIndex(str, ref basename, ref index))
continue;
TomlTable toml = rom64.parseToml(str + "\\actor.toml");
TomlArray var_arr = null;
if (toml != null)
{
if (toml.HasKey("Version"))
version = toml["Version"].AsInteger;
}
z64romactors.Add(new CustomActorz64rom(index,basename,version));
}
List<String> objects = rom64.getList("src\\object\\");
foreach (String str in objects)
{
string basename = "";
ushort index = 0;
if (!rom64.getNameAndIndex(str, ref basename, ref index))
continue;
z64romobjects.Add(new CustomObjectz64rom(index, basename));
}
}
foreach (XmlNode node in nodes)
{
XmlAttributeCollection nodeAtt = node.Attributes;
var values = new Dictionary<ushort, string>();
DatabaseCustomActor CustomActor;
int Key = Convert.ToInt32(nodeAtt["Key"].Value);
int Version = Convert.ToInt32(nodeAtt["Version"].Value);
string Name = nodeAtt["Name"].Value;
string FolderName = nodeAtt["FolderName"].Value;
bool HasCustomObject = (nodeAtt["HasCustomObject"] != null);
string ObjectName = (nodeAtt["ObjectName"] != null) ? nodeAtt["ObjectName"].Value : "";
int ActorID = (nodeAtt["ActorID"] != null) ? Convert.ToInt32(nodeAtt["ActorID"].Value,16) : 0;
int ObjectID = (nodeAtt["ObjectID"] != null) ? Convert.ToInt32(nodeAtt["ObjectID"].Value, 16) : 0;
string Category = nodeAtt["Category"].Value;
string Author = nodeAtt["Author"].Value;
string Notes = node.InnerText.TrimStart();
bool ForceUpdate = (nodeAtt["ForceUpdate"] != null);
List<string> Dependencies = ((nodeAtt["Dependencies"] != null) ? (nodeAtt["Dependencies"].Value.Split(',')).ToList() : new List<string>());
/*
string[] tmp = (nodeAtt["Dependencies"] != null) ? nodeAtt["Dependencies"].Value.Split(',') : null;
if (tmp != null)
{
foreach(string s in tmp)
{
Dependencies.Add(GetName(Convert.ToInt32(s)));
}
}*/
CustomActor = new DatabaseCustomActor(Key, Version, Name, FolderName, HasCustomObject, ObjectName, ActorID, ObjectID, Dependencies, Category, Author, Notes);
CustomActorz64rom match = z64romactors.Find(x => x.name == FolderName);
if (match != null)
{
CustomActor.Installed = true;
if (match.version == -1)
{
if (ForceUpdate) CustomActor.Outdated = true;
else CustomActor.Conflict = true;
}
else if (match.version < Version)
{
string ObjectFolder = ObjectName == "" ? FolderName : ObjectName;
CustomObjectz64rom z64romobject = z64romobjects.Find(x => x.name == ObjectFolder);
if (z64romobject != null || !HasCustomObject)
{
CustomActor.Outdated = true;
CustomActor.ActorID = match.ID;
if (HasCustomObject) CustomActor.ObjectID = z64romobject.ID;
}
else
{
CustomActor.ObjectConflict = true;
}
}
}
else
{
string ObjectFolder = ObjectName == "" ? FolderName : ObjectName;
CustomObjectz64rom z64romobject = z64romobjects.Find(x => x.name == ObjectFolder);
if (z64romobject != null)
{
CustomActor.ObjectConflict = true;
}
}
Database.Add(CustomActor);
}
fs.Close();
}
public void UpdateWindow()
{
ActorView.BeginUpdate();
ActorView.Nodes.Clear();
CustomActorNodes.Clear();
//SetActorButton.Enabled = false;
ActorDescription.Text = "";
bool show;
// ushort[] transitions = { 0x0009, 0x0023, 0x002E };
foreach (DatabaseCustomActor actor in Database)
{
show = false;
string specialfilter = "";
if (filter.Contains("#")) specialfilter = filter.Replace("#", "");
ActorView.Nodes.Add(new CustomActorNode(actor));
CustomActorNodes.Add(new CustomActorNode(actor));
if ((HideInstalledActors.Checked && actor.Installed) || (HideDependencyActors.Checked && actor.Category == "Dependency")) ;
else if (filter == "" || actor.Name.Contains(filter, StringComparison.OrdinalIgnoreCase) || (specialfilter != "" && actor.Category == specialfilter))
show = true;
if (!show) ActorView.Nodes[ActorView.Nodes.Count - 1].Remove();
}
foreach (CustomActorNode child in ActorView.Nodes)
{
if (child.Actor.Installed)
child.ForeColor = child.Actor.Conflict ? Color.Red : child.Actor.Outdated ? Color.Orange : Color.Green;
}
ActorView.EndUpdate();
}
private string GetName(int key)
{
foreach (CustomActorNode actor in ActorView.Nodes)
{
if (actor.Actor.Key == key)
return actor.Actor.FolderName;
}
return "";
}
private void UpdateActorInfo()
{
if (ActorView.SelectedNode != null)
{
CustomActorNode node = ((CustomActorNode)ActorView.SelectedNode);
ActorName.Text = node.Actor.Name;
ActorProperties.Text = "Category: " + node.Actor.Category + Environment.NewLine;
if (node.Actor.Author != "") ActorProperties.Text += "Author(s): " + node.Actor.Author + Environment.NewLine;
if (node.Actor.ActorID != 0) ActorProperties.Text += "Suggested ID: " + node.Actor.ActorID.ToString("X4") + Environment.NewLine;
if (node.Actor.HasCustomObject) ActorProperties.Text += "Uses custom object: " + (node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName) + Environment.NewLine;
ActorDescription.Text = node.Actor.Notes;
#if DEBUG
string imagefile = "Z:\\Users\\Noka\\Documents\\GitHub\\CustomActorDatabase\\CustomActors\\" + node.Actor.FolderName + "\\image.jpg";
ActorImage.Image = File.Exists(imagefile) ? Image.FromFile(imagefile) : null;
#else
try
{
using (client)
{
byte[] imageBytes = client.DownloadData(website + "master/CustomActors/" + node.Actor.FolderName + "/image.jpg");
using (var stream = new System.IO.MemoryStream(imageBytes))
{
ActorImage.Image = Image.FromStream(stream);
}
//ActorImage.Image = Image.FromFile(path + "CustomActors\\" + node.Actor.FolderName + "\\image.jpg");
}
}
catch (WebException)
{
ActorImage.Image = null;
}
#endif
InstallButton.Enabled = (!node.Actor.Installed || node.Actor.Outdated) && !node.Actor.Conflict;
InstallButton.Text = (!node.Actor.Outdated) ? "Install Actor" : "Update Actor";
}
else
{
ActorName.Text = "";
ActorProperties.Text = "";
}
}
private void GetDependency(CustomActorNode Node, ref List<CustomActorNode> Nodes)
{
foreach (string s in Node.Actor.Dependencies)
{
foreach (CustomActorNode node in CustomActorNodes)
{
if (!Nodes.Contains(node) && node.Actor.Dependencies.Count == 0 && node.Actor.FolderName == s)
{
Nodes.Add(node);
}
}
}
foreach (string s in Node.Actor.Dependencies)
{
foreach (CustomActorNode node in CustomActorNodes)
{
if (!Nodes.Contains(node) && node.Actor.Dependencies.Count != 0 && node.Actor.FolderName == s)
{
if (node.Actor.Dependencies.Count != 0)
{
GetDependency(node, ref Nodes);
}
Nodes.Add(node);
}
}
}
}
private void InstallButton_Click(object sender, EventArgs e)
{
bool proceed = false;
ushort ActorID = 0, ObjectID = 0;
List<CustomActorNode> installNodes = new List<CustomActorNode>();
installNodes.Add(((CustomActorNode)ActorView.SelectedNode));
if (installNodes[0].Actor.Dependencies.Count > 0)
{
GetDependency(installNodes[0], ref installNodes);
}
installNodes = installNodes.OrderBy(x => x.Actor.Dependencies.Count).ToList();
foreach (CustomActorNode node in installNodes)
{
if (!node.Actor.Outdated)
{
int objID = (ushort)node.Actor.ObjectID;
if (node.Actor.HasCustomObject)
{
string objname = node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName;
CustomObjectz64rom tmp = z64romobjects.Find(x => x.name == objname);
if (tmp != null)
{
objID = tmp.ID;
}
}
using (PickCustomActorID pickID = new PickCustomActorID((ushort)node.Actor.ActorID, (ushort)objID, node.Actor.ActorID == 0, node.Actor.HasCustomObject, false, node.Actor.FolderName, z64romactors,z64romobjects))
{
if (pickID.ShowDialog() == DialogResult.OK)
{
ActorID = pickID.ActorID;
ObjectID = pickID.ObjectID;
proceed = true;
}
}
}
else
{
if (MessageBox.Show($"WARNING! This will delete any modifications you made actor {node.Actor.FolderName}! continue?", "WARNING",
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
ActorID = (ushort)node.Actor.ActorID;
ObjectID = (ushort)node.Actor.ObjectID;
proceed = true;
}
}
if (proceed)
{
string temppath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp\\");
if (Directory.Exists(temppath))
{
Helpers.DeleteDirectory(temppath);
Directory.CreateDirectory(temppath);
}
else
{
Directory.CreateDirectory(temppath);
}
#if DEBUG
var zip = ZipFile.Read("Z:\\Users\\Noka\\Documents\\GitHub\\CustomActorDatabase\\CustomActors\\" + node.Actor.FolderName + "\\data.zip");
zip.ExtractAll(temppath, ExtractExistingFileAction.Throw);
#else
PleaseWait pleasewait = new PleaseWait(website + "master/CustomActors/" + node.Actor.FolderName + "/data.zip", tempw + "data.zip", temppath, false);
pleasewait.ShowDialog();
#endif
/*
using (var zip = ZipFile.Read(path + "CustomActors\\" + node.Actor.FolderName + "\\data.zip"))
zip.ExtractAll(temppath, ExtractExistingFileAction.Throw);*/
if (Directory.Exists(temppath + "include\\object\\"))
{
System.IO.DirectoryInfo di2 = new DirectoryInfo(temppath + "include\\object\\");
if (node.Actor.HasCustomObject)
{
FileInfo[] dirfiles2 = di2.GetFiles();
foreach (FileInfo dirfile in dirfiles2)
{
string sourcefile = Path.Combine(temppath + "include\\object\\", dirfile.Name);
string targetfile = Path.Combine(temppath + "include\\object\\", "0x" + ObjectID.ToString("X4") + "-" + (node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName) + dirfile.Extension);
File.Move(sourcefile, targetfile);
}
}
}
string[] renamer = new[] { "src\\actor\\", "rom\\actor\\", "src\\object\\", "rom\\object\\" };
ushort[] renamerID = new[] { ActorID, ActorID, ObjectID, ObjectID };
for (int i = 0; i < renamer.Length; i++)
{
if (i > 1 && !node.Actor.HasCustomObject) break;
System.IO.DirectoryInfo di = new DirectoryInfo(temppath + renamer[i]);
if (Directory.Exists(di.FullName))
foreach (DirectoryInfo dir in di.GetDirectories())
{
if (i == 0)
{
FileInfo[] dirfiles = dir.GetFiles();
foreach (FileInfo dirfile in dirfiles)
{
if (dirfile.Extension == ".c" || dirfile.Extension == ".h")
{
string targetfile = Path.Combine(dir.FullName, dirfile.Name);
Helpers.ReplaceLine("#define ACT_ID", "#define ACT_ID 0x" + ActorID.ToString("X4"), targetfile, 100);
int c = 2;
foreach (string dependency in node.Actor.Dependencies)
{
int dependencyID = z64romactors.Find(x => x.name == dependency).ID;
Helpers.ReplaceLine($"#define ACT{c}_ID", $"#define ACT{c}_ID 0x" + dependencyID.ToString("X4"), targetfile, 100);
c++;
}
if (node.Actor.HasCustomObject)
{
Helpers.ReplaceLine("#define OBJ_H", "#define OBJ_H \"object/0x" + ObjectID.ToString("X4") + "-" + (node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName) + ".h\"", targetfile, 100);
Helpers.ReplaceLine("#define OBJ_ID", "#define OBJ_ID 0x" + ObjectID.ToString("X4"), targetfile, 100);
}
}
}
}
string name = dir.Name;
string tmpdir = ((i > 1 && node.Actor.ObjectName != "") ? node.Actor.ObjectName : node.Actor.FolderName);
name = "0x" + renamerID[i].ToString("X4") + "-" + tmpdir;
if (!Directory.Exists(temppath + renamer[i] + name))
dir.MoveTo(temppath + renamer[i] + name);
}
}
DirectoryInfo sourcedir = new DirectoryInfo(temppath);
string destinationDir = rom64.getPath() + "\\";
// Get the files in the directory and copy them to the new location
//FileInfo[] files = sourcedir.GetFiles("",SearchOption.AllDirectories);
string[] files = Directory.GetFiles(temppath, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
string destination = Path.GetFullPath(file).Replace(temppath, destinationDir);
if (!Directory.Exists(Path.GetDirectoryName(destination)))
Directory.CreateDirectory(Path.GetDirectoryName(destination));
if (File.Exists(destination))
File.Delete(destination);
File.Move(file, destination);
}
// Directory.Delete(temppath);
if (!node.Actor.Outdated)
{
z64romactors.Add(new CustomActorz64rom(ActorID, node.Actor.FolderName, node.Actor.Version));
if (MainForm.ActorCache.ContainsKey(ActorID)) MainForm.ActorCache.Remove(ActorID);
MainForm.ActorCache.Add(ActorID, new ActorInfo(node.Actor.Name, new List<ActorProperty>(), "" + ObjectID.ToString("X4")));
if (node.Actor.HasCustomObject)
{
z64romobjects.Add(new CustomObjectz64rom(ObjectID, node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName));
if (MainForm.ObjectCache.ContainsKey(ObjectID)) MainForm.ObjectCache.Remove(ObjectID);
MainForm.ObjectCache.Add(ObjectID, new ObjectInfo(1, node.Actor.ObjectName != "" ? node.Actor.ObjectName : node.Actor.FolderName, "" + ActorID.ToString("X4")));
}
}
reload = true;
node.Actor.Installed = true;
node.Actor.Outdated = false;
UpdateWindow();
}
}
}
private void CloseButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void ActorView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
//InstallButton.Enabled = true;
}
private void ActorView_AfterSelect(object sender, TreeViewEventArgs e)
{
UpdateActorInfo();
}
private void SearchCategory(object sender, EventArgs e)
{
FilterTextBox.Text = "#" + ((ToolStripMenuItem)sender).Text;
GoButton_Click(sender, e);
}
private void GoButton_Click(object sender, EventArgs e)
{
filter = FilterTextBox.Text;
UpdateWindow();
}
private void HideInstalledActors_CheckedChanged(object sender, EventArgs e)
{
UpdateWindow();
}
private void CustomActorDatabase_FormClosed(object sender, FormClosedEventArgs e)
{
string tmp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp\\");
Helpers.DeleteDirectory(tmp);
Helpers.DeleteDirectory(tempw);
}
private void HideDependencyActors_CheckedChanged(object sender, EventArgs e)
{
UpdateWindow();
}
}
public class DatabaseCustomActor
{
public int Key;
public int Version;
public string Name;
public string FolderName;
public bool HasCustomObject;
public int ActorID;
public int ObjectID;
public string Category;
public string Notes;
public string Author;
public bool Installed = false;
public bool Outdated = false;
public bool Conflict = false;
public bool ObjectConflict = false;
public List<string> Dependencies = new List<string>();
public string ObjectName = "";
public DatabaseCustomActor(int _Key, int _Version, string _Name, string _FolderName, bool _HasCustomObject, string _ObjectName, int _ActorID, int _ObjectID, List<string> _Dependencies, string _Category, string _Author, string _Notes)
{
Key = _Key;
Name = _Name;
Version = _Version;
FolderName = _FolderName;
HasCustomObject = _HasCustomObject;
ObjectName = _ObjectName;
ActorID = _ActorID;
Category = _Category;
Author = _Author;
Notes = _Notes;
Dependencies = _Dependencies;
}
}
public class CustomActorNode : TreeNode
{
public int Value { get; set; }
public DatabaseCustomActor Actor { get; set; }
public CustomActorNode(DatabaseCustomActor actor) : base(actor.Name)
{
Value = actor.Key;
Actor = actor;
Text = Actor.Name + " " + (Actor.Conflict ? "[Actor Name Conflict]" : Actor.ObjectConflict ? "[Object Exists]" : Actor.Outdated ? "[Update Available]" : Actor.Installed ? "[Up to date]" : "");
}
}
public class CustomActorz64rom
{
public string name;
public int ID;
public int version;
public CustomActorz64rom(int _ID, string _name, int _version)
{
ID = _ID;
version = _version;
name = _name;
}
}
public class CustomObjectz64rom
{
public string name;
public int ID;
public int version;
public CustomObjectz64rom(int _ID, string _name)
{
ID = _ID;
name = _name;
}
}
}