This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
LevelObj.cs
723 lines (642 loc) · 22.9 KB
/
LevelObj.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 EditorCore;
using EditorCore.Interfaces;
using ExtensionMethods;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Media.Media3D;
using static EditorCore.PropertyGridTypes;
namespace OdysseyExt
{
public class LevelObj : ILevelObj
{
public static ILevelObj FromNode(dynamic bymlNode)
{
if (!(bymlNode is Dictionary<string, dynamic>)) throw new Exception("Not a dictionary");
var node = (Dictionary<string, dynamic>)bymlNode;
if (node.ContainsKey(N_Id) && ((string)node[N_Id]).StartsWith("rail"))
return new Rail(node);
else
return new LevelObj(node);
}
[Browsable(false)]
public bool ReadOnly { get; set; } = false; //if the model is static and doesn't appear in the list (mk8 courses)
public const string N_Translate = "Translate";
public const string N_Rotate = "Rotate";
public const string N_Scale = "Scale";
public const string N_Id = "Id";
public const string N_Name = "UnitConfigName";
public const string N_ModelName = "ModelName";
public const string N_Links = "Links";
public const string N_LinkDest = "IsLinkDest";
public const string N_LayerConfigName = "LayerConfigName";
public const string N_PlacementFileName = "PlacementFileName";
public const string N_Comment = "comment";
public const string N_LinkReferenceList = "SrcUnitLayerList";
public const string N_ResourceCategory = "SrcUnitLayerList";
public static readonly string[] CantRemoveNames = { N_Translate, N_Rotate, N_Scale, N_Id , N_Name , N_Links, N_LinkDest, N_LayerConfigName , N_PlacementFileName };
public static readonly string[] ModelFieldNames = { N_Name, N_ModelName };
public const string N_UnitConfig = "UnitConfig";
public const string N_UnitConfigModel = "DisplayName";
public const string N_UnitConfigPos = "DisplayTranslate";
public const string N_UnitConfigRot = "DisplayRotate";
public const string N_UnitConfigScale = "DisplayScale";
public const string N_UnitConfigGenList = "GenerateCategory";
public const string N_UnitConfigBaseClass = "ParameterConfigName";
public const string N_UnitConfigGenTarget = "PlacementTargetFile";
[System.ComponentModel.DisplayName("Properties")]
[TypeConverter(typeof(DictionaryConverter))]
[Description("This contains every property of this object")]
public Dictionary<string, dynamic> Prop { get; set; } = new Dictionary<string, dynamic>();
public LevelObj(Dictionary<string, dynamic> bymlNode)
{
Prop = bymlNode;
if (Prop.ContainsKey(N_Links) && !(Prop[N_Links] is LinksNode)) Prop[N_Links] = new LinksNode(Prop[N_Links]);
}
public LevelObj(bool empty = false)
{
if (empty) return;
Prop.Add(N_Translate, NodeVec3(0,0,0));
Prop.Add(N_Rotate, NodeVec3(0,0,0));
Prop.Add(N_Scale, NodeVec3(1, 1, 1));
Prop.Add(N_Links, new LinksNode());
this[N_Name] = "newObj";
this[N_Id] = "obj0";
this[N_LinkDest] = false;
this[N_LayerConfigName] = "Common";
this[N_PlacementFileName] = "Undefined";
//this[N_Comment] = null;
Prop.Add(N_UnitConfig, new Dictionary<string, dynamic>());
Prop[N_UnitConfig].Add(N_UnitConfigPos, NodeVec3(0, 0, 0));
Prop[N_UnitConfig].Add(N_UnitConfigRot, NodeVec3(0, 0, 0));
Prop[N_UnitConfig].Add(N_UnitConfigScale, NodeVec3(1, 1, 1));
Prop[N_UnitConfig].Add(N_UnitConfigModel, "");
Prop[N_UnitConfig].Add(N_UnitConfigGenList, "Default");
Prop[N_UnitConfig].Add(N_UnitConfigBaseClass, "");
Prop[N_UnitConfig].Add(N_UnitConfigGenTarget, "Map");
}
public dynamic this [string name]
{
get
{
if (Prop.ContainsKey(name)) return Prop[name];
else return null;
}
set
{
if (Prop.ContainsKey(name)) Prop[name] = value;
else Prop.Add(name,value);
}
}
public bool ContainsKey(string name) => Prop.ContainsKey(name);
[DisplayName("Position")]
[TypeConverter(typeof(PropertyGridTypes.Vector3DConverter))]
[Category(" Transform")]
public virtual Vector3D Pos
{
get { return new Vector3D(this[N_Translate]["X"], this[N_Translate]["Y"], this[N_Translate]["Z"]); }
set {
this[N_Translate]["X"] = (Single)value.X;
this[N_Translate]["Y"] = (Single)value.Y;
this[N_Translate]["Z"] = (Single)value.Z;
}
}
[System.ComponentModel.DisplayName("Rotation")]
[TypeConverter(typeof(PropertyGridTypes.Vector3DConverter))]
[Category(" Transform")]
public Vector3D Rot
{
get { return new Vector3D(this[N_Rotate]["X"], this[N_Rotate]["Y"], this[N_Rotate]["Z"]); }
set
{
this[N_Rotate]["X"] = (Single)value.X;
this[N_Rotate]["Y"] = (Single)value.Y;
this[N_Rotate]["Z"] = (Single)value.Z;
}
}
[Browsable(false)]
public int ID_int
{
get
{
int res = -1;
if (ID.StartsWith("obj"))
int.TryParse(ID.Substring(3), out res);
return res;
}
set
{
ID = "obj" + value.ToString();
}
}
[Browsable(false)]
public Vector3D ModelView_Pos
{
get => new Vector3D(this[N_Translate]["X"], -this[N_Translate]["Z"], this[N_Translate]["Y"]);
set => Pos = new Vector3D(value.X, value.Z, -value.Y);
}
[Browsable(false)]
public Vector3D ModelView_Rot
{
get { return new Vector3D(this[N_Rotate]["X"], -this[N_Rotate]["Z"], this[N_Rotate]["Y"]); } //TODO: check if it matches in-game
}
[System.ComponentModel.DisplayName("Scale")]
[TypeConverter(typeof(PropertyGridTypes.Vector3DConverter))]
[Category(" Transform")]
public Vector3D Scale
{
get { return new Vector3D(this[N_Scale]["X"], this[N_Scale]["Y"], this[N_Scale]["Z"]); }
set
{
this[N_Scale]["X"] = (Single)value.X;
this[N_Scale]["Y"] = (Single)value.Y;
this[N_Scale]["Z"] = (Single)value.Z;
}
}
[Browsable(false)]
public Vector3D ModelView_Scale
{
get { return new Vector3D(this[N_Scale]["X"], this[N_Scale]["Z"], this[N_Scale]["Y"]); }
}
[Browsable(false)]
public Transform transform
{
get => new Transform() { Pos = Pos, Rot = Rot, Scale = Scale };
set
{
Pos = value.Pos;
Rot = value.Rot;
Scale = value.Scale;
}
}
public string ID
{
get { return this[N_Id]; }
set { this[N_Id] = value;}
}
public string Name
{
get { return this.ToString(); }
set { this[N_Name] = value; }
}
public string ModelName
{
get => Prop.ContainsKey(N_ModelName) && Prop[N_ModelName] != null ? Prop[N_ModelName] : this.ToString();
}
public override string ToString()
{
string name = this[N_Name];
if (name == null) name = "LevelObj id: " + this[N_Id];
if (name == null) name = "LevelObj";
return name;
}
public LevelObj Clone()
{
return new LevelObj(DeepCloneDictArr.DeepClone(Prop));
}
object ICloneable.Clone()
{
return Clone();
}
static Dictionary<string,dynamic> NodeVec3(float x, float y, float z)
{
var res = new Dictionary<string, dynamic>();
res.Add("X", (Single)x);
res.Add("Y", (Single)y);
res.Add("Z", (Single)z);
return res;
}
}
[Editor("OdysseyExt.LinksEditor", typeof(UITypeEditor))] //needs to be string to work (?)
public class LinksNode : Dictionary<string, dynamic>, ICloneable //wrapper so we can use the custom editor
{
public LinksNode(Dictionary<string, dynamic> dict) : base(dict)
{
}
public LinksNode() : base()
{
}
public LinksNode Clone()
{
return new LinksNode(DeepCloneDictArr.DeepClone(this));
}
object ICloneable.Clone()
{
return Clone();
}
}
public class LinksEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
LinksNode node = value as LinksNode;
/*context should be of type System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry
OwnerGrid is not a public property, we get it through reflection
A reference to the parent form is needed to add the ObjList to the ListEditingStack*/
Type t = context.GetType();
if (!(t.GetProperty("OwnerGrid").GetValue(context, null) is PropertyGrid targetGrid))
throw new Exception("context is not of the expected type");
if (!(targetGrid.ParentForm is IEditorFormContext TargetForm))
throw new Exception("context is not of the expected type");
if (node != null)
{
using (var form = new EditorFroms.LinksEditor(node, TargetForm))
{
form.ShowDialog();
}
}
return node;
}
}
///*
//class LevelObjEditor : UITypeEditor
//{
// public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
// {
// return UITypeEditorEditStyle.Modal;
// }
// public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
// {
// IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
// Dictionary<string, dynamic> v = value as Dictionary<string, dynamic>;
// if (svc != null && v != null)
// {
// using (FrmObjEditor form = new FrmObjEditor(v))
// {
// form.ShowDialog();
// v = form.Value.Prop;
// }
// }
// return v; // can also replace the wrapper object here
// }
//}*/
////[Editor(typeof(C0ListEditor), typeof(UITypeEditor))]
//public class C0List : ICloneable
//{
// List<LevelObj> l = new List<LevelObj>();
// public List<LevelObj> List
// {
// get { return l; }
// set { l = value; }
// }
// public C0List Clone()
// {
// C0List C = new C0List();
// foreach (LevelObj lev in l) C.l.Add(lev.Clone());
// return C;
// }
// public override string ToString()
// {
// return "C0 list";
// }
// object ICloneable.Clone()
// {
// return Clone();
// }
//}
///*
//class C0ListEditor : UITypeEditor
//{
// public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
// {
// return UITypeEditorEditStyle.Modal;
// }
// public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
// {
// IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
// C0List v = value as C0List;
// if (svc != null && v != null)
// {
// ((Form1)System.Windows.Forms.Application.OpenForms["Form1"]).EditC0List(v.List);
// }
// return v;
// }
//}
//*/
//[TypeConverter(typeof(ExpandableObjectConverter))]
//public class Rail : ICloneable
//{
// public object this[string propertyName]
// {
// get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
// set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
// }
// public Point3D[] GetPointArray()
// {
// List<Point3D> points = new List<Point3D>();
// foreach (Rail.Point p in Points) points.Add(new Point3D(p.X, -p.Z, p.Y));
// return points.ToArray();
// }
// List<int> _Args = new List<int>();
// string _LayerName;
// internal List<Point> _points = new List<Point>();
// //Multi file name ?
// public string _closed;
// int _l_id;
// string _name;
// int _no;
// string _type;
// public Rail(bool Adding = false, Vector3D? BasePosition = null)
// {
// _LayerName = "共通";
// _closed = "FALSE";
// _name = "empty rail";
// _type = "Bezier";
// if (Adding) _points.Add(new Point());
// if (Adding) _points.Add(new Point(1));
// if (BasePosition != null)
// {
// _points[0].X = (float)((Vector3D)BasePosition).X;
// _points[0].Y = (float)((Vector3D)BasePosition).Z;
// _points[0].Z = -(float)((Vector3D)BasePosition).Y;
// _points[1].X = (float)((Vector3D)BasePosition).X;
// _points[1].Y = (float)((Vector3D)BasePosition).Z;
// _points[1].Z = -(float)((Vector3D)BasePosition).Y;
// }
// }
// public override string ToString()
// {
// return _name;
// }
// public string Name
// {
// set
// {
// _name = JapChars(value);
// }
// get { return _name; }
// }
// public string Type
// {
// set { _type = value; }
// get { return _type; }
// }
// public bool Closed
// {
// set { _closed = value == false ? "OPEN" : "CLOSE"; }
// get { return _closed == "OPEN" ? false : true; }
// }
// public int no
// {
// set { _no = value; }
// get { return _no; }
// }
// public int l_id
// {
// set { _l_id = value; }
// get { return _l_id; }
// }
// public List<int> Arg
// {
// set { _Args = value; }
// get { return _Args; }
// }
// internal string JapChars(string input)
// {
// byte[] bytes = Encoding.Default.GetBytes(input);
// Encoding Enc = Encoding.GetEncoding(932);
// return input;//Enc.GetString(bytes);
// }
// public Rail Clone()
// {
// Rail R = new Rail();
// foreach (int i in _Args) R._Args.Add(i);
// R.LayerName = (string)_LayerName.Clone();
// foreach (Point p in _points) R._points.Add(p.Clone());
// R._closed = (string)_closed.Clone();
// R.l_id = _l_id;
// R.Name = (string)_name.Clone();
// R.no = _no;
// R.Type = (string)_type.Clone();
// return R;
// }
// object ICloneable.Clone()
// {
// return Clone();
// }
// public string LayerName
// {
// set {
// _LayerName = JapChars(value);
// }
// get { return _LayerName; }
// }
// // [Editor(typeof(RailPointEditor), typeof(UITypeEditor))]
// public List<Point> Points
// {
// set { _points = value; }
// get { return _points; }
// }
// // [TypeConverter(typeof(ExpandableObjectConverter))]
// public class Point : ICloneable
// {
// List<int> _Args = new List<int>();
// int _ID;
// public List<Single> _X = new List<Single>();
// public List<Single> _Y = new List<Single>();
// public List<Single> _Z = new List<Single>();
// public Point(int id = 0)
// {
// _ID = id;
// }
// public override string ToString()
// {
// return "Point ID: " + _ID.ToString();
// }
// public Point Clone()
// {
// Point N = new Point();
// foreach (int i in _Args) N._Args.Add(i);
// N.ID = _ID;
// foreach (int s in _X) N._X.Add(s);
// foreach (int s in _Y) N._Y.Add(s);
// foreach (int s in _Z) N._Z.Add(s);
// return N;
// }
// public Point Clone_increment()
// {
// Point N = new Point();
// foreach (int i in _Args) N._Args.Add(i);
// N.ID = _ID + 1;
// foreach (int s in _X) N._X.Add(s + 100);
// foreach (int s in _Y) N._Y.Add(s);
// foreach (int s in _Z) N._Z.Add(s);
// return N;
// }
// object ICloneable.Clone()
// {
// return Clone();
// }
// public List<int> Args
// {
// set { _Args = value; }
// get { return _Args; }
// }
// public int ID
// {
// set { _ID = value; }
// get { return _ID; }
// }
// public Single X
// {
// set { _X.Clear(); _X.Add(value); _X.Add(value); _X.Add(value); }
// get {
// if (_X.Count == 0) X = 0;
// return _X[0];
// }
// }
// public Single Y
// {
// set { _Y.Clear(); _Y.Add(value); _Y.Add(value); _Y.Add(value); }
// get {
// if (_Y.Count == 0) Y = 0;
// return _Y[0];
// }
// }
// public Single Z
// {
// set { _Z.Clear(); _Z.Add(value); _Z.Add(value); _Z.Add(value); }
// get {
// if (_Z.Count == 0) Z = 0;
// return _Z[0];
// }
// }
// }
//}
///*
//class RailPointEditor : UITypeEditor
//{
// public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
// {
// return UITypeEditorEditStyle.Modal;
// }
// public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
// {
// IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
// List<Rail.Point> v = value as List<Rail.Point>;
// if (svc != null && v != null)
// {
// using (FrmRailPointEditor form = new FrmRailPointEditor(v))
// {
// form.ShowDialog();
// v = form.Value;
// }
// }
// return v; // can also replace the wrapper object here
// }
//}*/
//[TypeConverter(typeof(ExpandableObjectConverter))]
//class Node : ICloneable
//{
// string _StringValue;
// public string _StringNodeType;
// NodeTypes _NodeType;
// [Description("This is the value of this node as a string, change it respecting the type")]
// public string StringValue
// {
// set
// {
// if (_NodeType == NodeTypes.Int) Int32.Parse(value); //Crashes if the value is invalid
// else if (_NodeType == NodeTypes.Single) Single.Parse(value);
// _StringValue = value;
// }
// get { return _StringValue; }
// }
// [Description("The node type can't be changed, it tells what kind of data contains the node")]
// public NodeTypes NodeType
// {
// //set { _NodeType = value; }
// get { return _NodeType; }
// }
// public override string ToString()
// {
// string Prev = "";
// if (_NodeType == NodeTypes.Empty) Prev += "Empty";
// else if (_NodeType == NodeTypes.String) Prev += "String";
// else if (_NodeType == NodeTypes.Int) Prev += "Int";
// else if (_NodeType == NodeTypes.Single) Prev += "Single";
// else Prev += string.Format("Unk Type ({0})", _StringNodeType);
// Prev += " : ";
// Prev += _StringValue;
// return Prev;
// }
// public enum NodeTypes
// {
// String = 0xA0,
// Empty = 0xA1,
// Int = 0xD1,
// Single = 0xD2,
// Other
// }
// string JapChars(string input)
// {
// byte[] bytes = Encoding.Default.GetBytes(input);
// Encoding Enc = Encoding.GetEncoding(932);
// return input; //Enc.GetString(bytes);
// }
// public Node(string _stringValue, string _type)
// {
// _NodeType = NodeTypes.Other;
// if (_type == "A0") _NodeType = NodeTypes.String;
// else if (_type == "A1") _NodeType = NodeTypes.Empty;
// else if (_type == "D1") _NodeType = NodeTypes.Int;
// else if (_type == "D2") _NodeType = NodeTypes.Single;
// _StringNodeType = _type;
// ApplyValue(_stringValue, _NodeType);
// }
// void ApplyValue(string _stringValue, NodeTypes _type)
// {
// _StringValue = _stringValue;
// switch (_type)
// {
// case NodeTypes.String:
// _StringValue = JapChars(_StringValue);
// _NodeType = NodeTypes.String;
// break;
// case NodeTypes.Empty:
// _NodeType = NodeTypes.Empty;
// break;
// default:
// _NodeType = _type;
// break;
// }
// }
// public Node Clone()
// {
// return new Node(this._StringValue, this._StringNodeType);
// }
// object ICloneable.Clone()
// {
// return Clone();
// }
// public static bool operator ==(Node first, Node second)
// {
// return first.NodeType == second.NodeType && first.StringValue == second.StringValue;
// }
// public static bool operator !=(Node first, Node second)
// {
// return first.NodeType != second.NodeType || first.StringValue != second.StringValue;
// }
// public override bool Equals(object Obj)
// {
// return (Node)Obj == this;
// }
//}
/*String = 0xA0,
Empty = 0xA1,
Int = 0xD1,
Single = 0xD2,
*/
}