-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModContents.cs
723 lines (606 loc) · 20.2 KB
/
ModContents.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
using Microsoft.Xna.Framework.Graphics;
using ModEditor.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Xml;
using System.Xml.Serialization;
using WeifenLuo.WinFormsUI.Docking;
namespace ModEditor
{
/// <summary>
/// Wraps item, i.e Tech, Device, or generic data.
/// Stores active ui information concerned to selected node
/// </summary>
public class Item// : IDockContent
{
public Controller controller;
public Object target; // actual gamedata object
public Item prev, next;
public bool isBase = false;
public bool isNameEditable = true;
public string name = "";
protected string sourcePath = "";
public TreeNode node; // Assigned tree node
public PanelItemContainer page; // Assigned tab page
public DateTime fileTime; // Cached time to check if file was modified
protected bool isDirty = false;
public event EventHandler dataChanged;
public Item(Object item, Controller controller, string path)
{
target = item;
this.controller = controller;
this.sourcePath = path;
fileTime = File.GetLastWriteTime(Path);
dataChanged += Item_dataChanged;
}
public void RaiseDataChanged()
{
dataChanged(null, null);
}
void Item_dataChanged(object sender, EventArgs e)
{
if (HasVariants() && node != null)
node.BackColor = System.Drawing.Color.YellowGreen;
if (page != null)
page.Name = Name;
}
public string Path
{
get
{
return controller.Mod.RootPath + "/" + sourcePath;
}
}
public string Name
{
get
{
return name;
}
}
public Item Next
{
get
{
return next;
}
}
public Item Prev
{
get
{
return prev;
}
}
public bool IsBase()
{
return controller.IsBase();
}
public bool OnTabClosed()
{
page = null;
return true;
}
public bool Dirty
{
set
{
if (value)
{
if (!IsBase())
controller.Mod.MarkDirty();
}
isDirty = value;
}
}
// Set source path, relative to mod root folder
public void SetSourcePath(string path)
{
sourcePath = path;
fileTime = File.GetLastAccessTime(Path);
}
public bool FileExists()
{
FileInfo info = new FileInfo(Path);
return info.Exists;
}
public bool ModifiedOutside()
{
var newTime = File.GetLastWriteTime(Path);
int result = newTime.CompareTo(fileTime);
if (result > 0)
{
return true;
}
return false;
}
public void Refresh()
{
controller.ReloadItem(this);
}
public bool IsOverrided()
{
return next != null;
}
public virtual Object getTarget()
{
return target;
}
public ContextMenuStrip GenerateContextMenu()
{
return controller.GenerateContextMenu(this);
}
public Control GenerateControl()
{
return controller.GenerateControl(this);
}
// remove links with other items
public void UnLink()
{
if (prev != null)
{
prev.next = next;
prev.RaiseDataChanged();
}
if (next != null)
{
next.prev = prev;
next.RaiseDataChanged();
}
}
public bool HasVariants()
{
return Prev != null || Next != null;
}
public PanelItemContainer GetTabPage()
{
return page;
}
}
/// <summary>
/// Defines methods to interact with specific mod objects
/// </summary>
public abstract class Controller
{
public Item rootItem;
protected string groupName = "unnamed";
protected System.Type targetType;
public bool isBase;
// Local cache for items created by this controller/mod
protected Dictionary<string, Item> localCache = new Dictionary<string, Item>();
public List<string> fieldStringToken = new List<string>();
protected ModContents mod;
public Controller(ModContents mod)
{
this.mod = mod;
rootItem = new Item(null, this, "");
rootItem.controller = this;
rootItem.isNameEditable = false;
}
public ModContents Mod
{
get
{
return mod;
}
}
public System.Type TargetType
{
get
{
return targetType;
}
}
public Controller GetFrontController()
{
return ModContents.GetMod().GetController(this.groupName);
}
public bool IsBase()
{
return isBase;
}
// try to set new item name
public virtual bool RenameItem(Item item, string newName)
{
return false;
}
public string GetGroupFolder()
{
return groupName;
}
// Do not sure what does it do
public abstract void ClearCache();
public virtual Dictionary<string, Item> GetItems()
{
return null;
}
public Dictionary<string, Item> GetLocalItems()
{
return localCache;
}
// Generate control to edit item contents. Most times it is ItemExplorer property grid
public virtual Control GenerateControl(Item item)
{
if (item.Equals(rootItem))
return null;
if (targetType == null)
return null;
ItemView explorer = new ItemView();
explorer.Init(targetType, item);
return explorer;
}
// Check item contents
public static void CheckDataImpl(Action<ItemReport> reporter, Item item, string basePath, object source, Type type, List<ModEditorAttribute> attributes)
{
if (FieldEditorManager.IsPrimitiveType(type))
{
// Dealing with primitive type
foreach (ModEditorAttribute attribute in attributes)
{
try
{
if (!attribute.CheckValue(source))
reporter(attribute.GenerateReport(item, basePath, source));
}
catch (Exception)
{
}
}
}
else if (type.Name.Equals("List`1"))
{
Type storedType = type.GetGenericArguments()[0];
int index = 0;
foreach (var record in (source as IList))
{
CheckDataImpl(reporter, item, basePath + "[" + index + "]", record, storedType, attributes);
}
}
else
{
foreach (var field in type.GetFields())
{
List<ModEditorAttribute> overridenAttribs = FieldEditorManager.GetAttributes(field, type);
bool ignore = false;
if (field.IsStatic)
continue;
foreach (ModEditorAttribute attr in overridenAttribs)
{
if (attr is IgnoreByEditor)
ignore = true;
}
object value = field.GetValue(source);
// dealing with container type
if (ignore == false && value != null)
CheckDataImpl(reporter, item, basePath + "." + field.Name, value, field.FieldType, overridenAttribs);
}
}
}
public virtual void CheckDataIntegrity(Action<ItemReport> data)
{
}
public abstract void ObtainModData(string basePath, bool isBase);
public abstract void PopulateModOverview(TreeNodeCollection root);
public abstract void SaveAll(string dir);
public abstract void UpdateItems();
public virtual ContextMenuStrip GenerateContextMenu(Item item)
{
return null;
}
// Reloads item from file
public virtual void ReloadItem(Item item)
{
}
// Opens system editor
public virtual bool OpenItemFile(Item item)
{
if (item.Path != "")
{
try
{
System.Diagnostics.Process.Start(item.Path);
return true;
}
catch (Exception e)
{
MessageBox.Show("Cannot open editor for file \"" + item.Path + "\" : \n\n" + e.ToString());
}
}
return false;
}
public virtual void CheckRootModifications(List<Item> modifiedItems)
{
}
public void CheckCacheModifications(List<Item> modifiedItems)
{
foreach (var record in GetLocalItems())
{
if (record.Value.ModifiedOutside())
modifiedItems.Add(record.Value);
}
}
// Check group for modifications
public virtual void CheckExternalModifications(List<Item> modifiedItems)
{
CheckRootModifications(modifiedItems);
CheckCacheModifications(modifiedItems);
}
}
public class ModContents
{
// Editor references to other mods
public List<ModContents> references;
public interface Explorer
{
void ExploreItem(Item item);
}
ModContents next, prev;
protected static ModContents lastMod;
public bool mergeReferences;
public Explorer activeExplorer;
public TreeView treeView;
public TreeNode rootTreeNode;
public Ship_Game.ModInformation modInfo;
public Dictionary<string, Controller> controllers = new Dictionary<string, Controller>();
public enum State
{
Empty,
Unloaded,
Loaded
}
public State state = State.Empty;
ModEditor.Controllers.ModInfoController modInfoController;
ModEditor.Controllers.StringsController stringsController;
// Directory with mod data
protected string modRootPath = "Contents";
public ModContents(TreeView treeView, ModContents baseMod)
{
this.treeView = treeView;
prev = baseMod;
if (baseMod != null)
{
if (baseMod.next != null)
throw new Exception("base mod is already linked");
baseMod.next = this;
}
rootTreeNode = new TreeNode("Root Node");
treeView.Nodes.Add(rootTreeNode);
lastMod = this;
InitControllers();
/*
Races
Encounters
* ToolTips
*/
}
// Create new mod from specified XML entry and base mod data
public static ModContents CreateNewMod(TreeView treeView, string modXMLPath, ModContents baseMod)
{
string modRootPath = Path.GetDirectoryName(modXMLPath) + "/" + Path.GetFileNameWithoutExtension(modXMLPath);
try
{
ModContents result = new ModContents(treeView, baseMod);
result.modRootPath = modRootPath;
result.modInfo = new Ship_Game.ModInformation()
{
ModName = Path.GetFileNameWithoutExtension(modXMLPath)
};
result.UpdateModInfo();
result.PopulateData(result.modRootPath, false);
return result;
}
catch (Exception e)
{
PanelErrors.LogErrorString(e.Message);
}
return null;
}
static public ModContents GetMod()
{
return lastMod;
}
void InitControllers()
{
modInfoController = new ModEditor.Controllers.ModInfoController(this);
stringsController = new ModEditor.Controllers.StringsController(this);
AddController(new ModEditor.Controllers.ArtifactsSpec(this));
AddController(new ModEditor.Controllers.EventsGroup(this));
AddController(new ModEditor.Controllers.HullsGroup(this));
AddController(new ModEditor.Controllers.BuildingSpec(this));
AddController(new ModEditor.Controllers.ModuleSpec(this));
AddController(new ModEditor.Controllers.TechSpec(this));
AddController(new ModEditor.Controllers.TexturesGroup(this));
AddController(new ModEditor.Controllers.TroopSpec(this));
AddController(new ModEditor.Controllers.WeaponGroup(this));
AddController(new ModEditor.Controllers.RacesGroup(this));
AddController(new ModEditor.Controllers.DiplomacyDialogGroup(this));
AddController(new ModEditor.Controllers.EncounterDialogGroup(this));
AddController(modInfoController);
AddController(stringsController);
/// Ship group controller does not work
//AddController(new ModEditor.Controllers.ShipsGroup(this));
}
public void CheckDataIntegrity(Action<ItemReport> reporter, Action<int> progress)
{
int index = 0;
foreach (var record in controllers)
{
record.Value.CheckDataIntegrity(reporter);
progress(100 * (index++) / (controllers.Count));
}
}
public Controller GetController(string group)
{
return controllers[group];
}
public static string GetLocString(int index)
{
return Controllers.StringsController.GetLocString(index);
}
public void MarkDirty()
{
}
public bool Loaded()
{
return state == State.Loaded;
}
void UpdateModInfo()
{
modInfoController.rootItem.target = modInfo;
rootTreeNode.Name = modInfo.ModName;
rootTreeNode.Text = modInfo.ModName;
}
protected bool LoadModInfo(string ModEntryPath)
{
try
{
FileInfo FI = new FileInfo(ModEntryPath);
Stream file = FI.OpenRead();
modInfo = (Ship_Game.ModInformation)Ship_Game.ResourceManager.ModSerializer.Deserialize(file);
modRootPath = Path.GetDirectoryName(ModEntryPath) + "/" + Path.GetFileNameWithoutExtension(ModEntryPath);
UpdateModInfo();
file.Close();
file.Dispose();
return true;
}
catch (Exception e)
{
PanelErrors.LogErrorString(e.Message);
}
return false;
}
protected bool SaveModInfo(string ModEntryPath)
{
try
{
FileInfo FI = new FileInfo(ModEntryPath);
Stream file = FI.OpenWrite();
Ship_Game.ResourceManager.ModSerializer.Serialize(file, modInfo);
file.Close();
file.Dispose();
return true;
}
catch (Exception e)
{
PanelErrors.LogErrorString(e.Message);
}
return false;
}
// Change item field
public static void ChangeItemValue(Item item, System.Reflection.FieldInfo fieldInfo, Object value)
{
// TODO: undo/redo handling
fieldInfo.SetValue(item.target, value);
}
// Read value from item
public static object ReadItemValue(Item item, System.Reflection.FieldInfo fieldInfo)
{
return fieldInfo.GetValue(item.target);
}
// Check all items for external modificatios
public void CheckExternalModifications(List<Item> modifiedItems)
{
foreach (var record in controllers)
{
record.Value.CheckExternalModifications(modifiedItems);
}
}
public void SaveMod(string ModEntryPath)
{
if (RootPath.Equals("Content"))
return;
ModEntryPath = RootPath;
SaveModInfo(ModEntryPath+".xml");
string outputDir = ModEntryPath;
System.IO.Directory.CreateDirectory(outputDir);
foreach (var controller in controllers)
{
controller.Value.SaveAll(outputDir);
}
}
public void SetName(string Name)
{
if (modInfo != null)
modInfo.ModName = Name;
rootTreeNode.Name = Name;
rootTreeNode.Text = Name;
}
protected void AddController(Controller controller)
{
controllers.Add(controller.GetGroupFolder(), controller);
}
public string RootPath
{
get
{
if (modInfo != null)
return modRootPath;
return "Content";
}
}
public void LoadMod(string ModEntryPath)
{
try
{
string modRootDirectory = "Mods";
string modDirectory = modRootDirectory + "\\" + Path.GetFileNameWithoutExtension(ModEntryPath);
LoadModInfo(ModEntryPath);
// 1. Load contents
Ship_Game.ResourceManager.LoadMods(modDirectory);
// 2. Populate treeview
PopulateData(modDirectory, false);
state = State.Loaded;
}
catch (Exception e)
{
PanelErrors.LogErrorString(e.Message);
}
UpdateUI();
}
public void Reset()
{
//rootBase.Nodes.Clear();// = null;
rootTreeNode.Nodes.Clear();// = null;
//treeView.Nodes.Clear();
//treeView.Nodes.Add("Load mod to observe data");
foreach (var controller in controllers)
{
controller.Value.ClearCache();
}
modInfo = null;
}
public void PopulateData(string basePath, bool isBase)
{
foreach (var controller in controllers)
{
controller.Value.ObtainModData(basePath, isBase);
controller.Value.PopulateModOverview(rootTreeNode.Nodes);
}
state = ModContents.State.Loaded;
}
public void UpdateUI()
{
foreach (var controller in controllers)
{
controller.Value.UpdateItems();
}
}
#region Tools
public Controller GetGroupController(string group)
{
if(controllers.ContainsKey(group))
return this.controllers[group];
return null;
}
#endregion
}
}