forked from dexyfex/CodeWalker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldInfoForm.cs
520 lines (469 loc) · 19 KB
/
WorldInfoForm.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
using CodeWalker.GameFiles;
using CodeWalker.Rendering;
using CodeWalker.Utils;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodeWalker
{
public partial class WorldInfoForm : Form
{
WorldForm WorldForm;
MapSelection Selection;
List<MapSelection> SelectionItems;
string SelectionMode = "";
bool MouseSelectEnable = false;
public WorldInfoForm(WorldForm worldForm)
{
WorldForm = worldForm;
InitializeComponent();
//SelectionModeComboBox.SelectedIndex = 0;
}
private void MouseSelectCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (MouseSelectCheckBox.Checked != MouseSelectEnable)
{
MouseSelectEnable = MouseSelectCheckBox.Checked;
WorldForm.OnInfoFormSelectionModeChanged(SelectionMode, MouseSelectEnable);
}
}
private void SelectionModeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectionModeComboBox.Text != SelectionMode)
{
SelectionMode = SelectionModeComboBox.Text;
WorldForm.OnInfoFormSelectionModeChanged(SelectionMode, MouseSelectEnable);
}
}
public void SetSelectionMode(string mode, bool enable)
{
SelectionMode = mode;
MouseSelectEnable = enable;
SelectionModeComboBox.Text = mode;
MouseSelectCheckBox.Checked = enable;
}
public void SetSelection(MapSelection item, List<MapSelection> multiItems)
{
Selection = item;
SelectionItems = multiItems;
SelectionNameTextBox.Text = item.GetNameString("Nothing selected");
//SelEntityPropertyGrid.SelectedObject = item.EntityDef;
SelArchetypePropertyGrid.SelectedObject = item.Archetype;
SelDrawablePropertyGrid.SelectedObject = item.Drawable;
SelDrawableModelPropertyGrid.SelectedObject = null;
SelDrawableModelsTreeView.Nodes.Clear();
SelDrawableTexturesTreeView.Nodes.Clear();
SelDrawableTexturePropertyGrid.SelectedObject = null;
SelDrawableTexturePictureBox.Image = null;
HierarchyTreeView.Nodes.Clear();
if (item.Drawable != null)
{
AddSelectionDrawableModelsTreeNodes(item.Drawable.DrawableModelsHigh, "High Detail", true);
AddSelectionDrawableModelsTreeNodes(item.Drawable.DrawableModelsMedium, "Medium Detail", false);
AddSelectionDrawableModelsTreeNodes(item.Drawable.DrawableModelsLow, "Low Detail", false);
AddSelectionDrawableModelsTreeNodes(item.Drawable.DrawableModelsVeryLow, "Very Low Detail", false);
//AddSelectionDrawableModelsTreeNodes(item.Drawable.DrawableModelsX, "X Detail", false);
}
if (item.EntityDef != null)
{
AddSelectionEntityHierarchyNodes(item.EntityDef);
}
if (item.MultipleSelection)
{
SelectionEntityTabPage.Text = "Multiple items";
SelEntityPropertyGrid.SelectedObject = multiItems.ToArray();
}
else if (item.TimeCycleModifier != null)
{
SelectionEntityTabPage.Text = "Time Cycle Modifier";
SelEntityPropertyGrid.SelectedObject = item.TimeCycleModifier;
}
else if (item.CarGenerator != null)
{
SelectionEntityTabPage.Text = "Car Generator";
SelEntityPropertyGrid.SelectedObject = item.CarGenerator;
}
else if (item.DistantLodLights != null)
{
SelectionEntityTabPage.Text = "Distant LOD Lights";
SelEntityPropertyGrid.SelectedObject = item.DistantLodLights;
}
else if (item.GrassBatch != null)
{
SelectionEntityTabPage.Text = "Grass";
SelEntityPropertyGrid.SelectedObject = item.GrassBatch;
}
else if (item.BoxOccluder != null)
{
SelectionEntityTabPage.Text = "Box Occluder";
SelEntityPropertyGrid.SelectedObject = item.BoxOccluder;
}
else if (item.OccludeModel != null)
{
SelectionEntityTabPage.Text = "Occlude Model";
SelEntityPropertyGrid.SelectedObject = item.OccludeModel;
}
else if (item.WaterQuad != null)
{
SelectionEntityTabPage.Text = "Water Quad";
SelEntityPropertyGrid.SelectedObject = item.WaterQuad;
}
else if (item.NavPoly != null)
{
SelectionEntityTabPage.Text = "Nav Poly";
SelEntityPropertyGrid.SelectedObject = item.NavPoly;
}
else if (item.NavPoint != null)
{
SelectionEntityTabPage.Text = "Nav Point";
SelEntityPropertyGrid.SelectedObject = item.NavPoint;
}
else if (item.NavPortal != null)
{
SelectionEntityTabPage.Text = "Nav Portal";
SelEntityPropertyGrid.SelectedObject = item.NavPortal;
}
else if (item.PathNode != null)
{
SelectionEntityTabPage.Text = "Path Node";
SelEntityPropertyGrid.SelectedObject = item.PathNode;
}
else if (item.TrainTrackNode != null)
{
SelectionEntityTabPage.Text = "Train Track Node";
SelEntityPropertyGrid.SelectedObject = item.TrainTrackNode;
}
else if (item.ScenarioNode != null)
{
SelectionEntityTabPage.Text = item.ScenarioNode.FullTypeName;
SelEntityPropertyGrid.SelectedObject = item.ScenarioNode;
}
else if (item.Audio != null)
{
SelectionEntityTabPage.Text = item.Audio.FullTypeName;
SelEntityPropertyGrid.SelectedObject = item.Audio;
}
else
{
SelectionEntityTabPage.Text = "Entity";
SelEntityPropertyGrid.SelectedObject = item.EntityDef;
}
if (item.EntityExtension != null)
{
SelectionExtensionTabPage.Text = "Entity Extension";
SelExtensionPropertyGrid.SelectedObject = item.EntityExtension;
}
else if (item.ArchetypeExtension != null)
{
SelectionExtensionTabPage.Text = "Archetype Extension";
SelExtensionPropertyGrid.SelectedObject = item.ArchetypeExtension;
}
else if (item.CollisionBounds != null)
{
SelectionExtensionTabPage.Text = "Collision Bounds";
SelExtensionPropertyGrid.SelectedObject = item.CollisionBounds;
}
else
{
SelectionExtensionTabPage.Text = "Extension";
SelExtensionPropertyGrid.SelectedObject = null;
}
}
private void AddSelectionDrawableModelsTreeNodes(ResourcePointerList64<DrawableModel> models, string prefix, bool check)
{
if (models == null) return;
if (models.data_items == null) return;
for (int mi = 0; mi < models.data_items.Length; mi++)
{
var model = models.data_items[mi];
string mprefix = prefix + " " + (mi + 1).ToString();
var mnode = SelDrawableModelsTreeView.Nodes.Add(mprefix + " " + model.ToString());
mnode.Tag = model;
mnode.Checked = check;
var tmnode = SelDrawableTexturesTreeView.Nodes.Add(mprefix + " " + model.ToString());
tmnode.Tag = model;
if ((model.Geometries == null) || (model.Geometries.data_items == null)) continue;
foreach (var geom in model.Geometries.data_items)
{
var gname = geom.ToString();
var gnode = mnode.Nodes.Add(gname);
gnode.Tag = geom;
gnode.Checked = true;// check;
var tgnode = tmnode.Nodes.Add(gname);
tgnode.Tag = geom;
if ((geom.Shader != null) && (geom.Shader.ParametersList != null) && (geom.Shader.ParametersList.Hashes != null))
{
var pl = geom.Shader.ParametersList;
var h = pl.Hashes;
var p = pl.Parameters;
for (int ip = 0; ip < h.Length; ip++)
{
var hash = pl.Hashes[ip];
var parm = pl.Parameters[ip];
var tex = parm.Data as TextureBase;
if (tex != null)
{
var t = tex as Texture;
var tstr = tex.Name.Trim();
if (t != null)
{
tstr = string.Format("{0} ({1}x{2}, embedded)", tex.Name, t.Width, t.Height);
}
var tnode = tgnode.Nodes.Add(hash.ToString().Trim() + ": " + tstr);
tnode.Tag = tex;
}
}
tgnode.Expand();
}
}
mnode.Expand();
tmnode.Expand();
}
}
private void AddSelectionEntityHierarchyNodes(YmapEntityDef entity)
{
if (entity == null) return;
var e = entity;
TreeNode tn = null;
TreeNode seltn = null;
while (e != null)
{
var newtn = new TreeNode(e.Name);
newtn.Tag = e;
if (tn != null)
{
newtn.Nodes.Add(tn);
}
else
{
seltn = newtn;
}
if (e.Children != null)
{
foreach (var c in e.Children)
{
if ((tn != null) && (c == tn.Tag)) continue;
var ctn = new TreeNode(c.Name);
ctn.Tag = c;
newtn.Nodes.Add(ctn);
}
}
tn = newtn;
e = e.Parent;
}
if (tn != null)
{
HierarchyTreeView.Nodes.Add(tn);
tn.ExpandAll();
}
if (seltn != null)
{
HierarchyTreeView.SelectedNode = seltn;
}
}
private void DisplayTexture(Texture tex, int mip)
{
try
{
int cmip = Math.Min(Math.Max(mip, 0), tex.Levels - 1);
byte[] pixels = DDSIO.GetPixels(tex, cmip);
int w = tex.Width >> cmip;
int h = tex.Height >> cmip;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
if (pixels != null)
{
var BoundsRect = new System.Drawing.Rectangle(0, 0, w, h);
BitmapData bmpData = bmp.LockBits(BoundsRect, ImageLockMode.WriteOnly, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * bmp.Height;
Marshal.Copy(pixels, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
}
SelDrawableTexturePictureBox.Image = bmp;
SelTextureDimensionsLabel.Text = w.ToString() + " x " + h.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Error reading texture mip:\n" + ex.ToString());
SelDrawableTexturePictureBox.Image = null;
}
}
private void SelectTexture(TextureBase texbase, bool mipchange)
{
Texture tex = texbase as Texture;
YtdFile ytd = null;
string errstr = string.Empty;
if ((tex == null)&&(texbase!=null))
{
//need to load from txd.
var arch = Selection.Archetype;
uint texhash = texbase.NameHash;
uint txdHash = (arch != null) ? arch.TextureDict.Hash : 0;
tex = TryGetTextureFromYtd(texhash, txdHash, out ytd);
if (tex == null)
{ //search parent ytds...
uint ptxdhash = WorldForm.GameFileCache.TryGetParentYtdHash(txdHash);
while ((ptxdhash != 0) && (tex == null))
{
tex = TryGetTextureFromYtd(texhash, ptxdhash, out ytd);
if (tex == null)
{
ptxdhash = WorldForm.GameFileCache.TryGetParentYtdHash(ptxdhash);
}
else
{ }
}
if (tex == null)
{
ytd = WorldForm.GameFileCache.TryGetTextureDictForTexture(texhash);
if (ytd != null)
{
int tries = 0;
while (!ytd.Loaded && (tries < 500)) //wait upto ~5 sec
{
System.Threading.Thread.Sleep(10);
tries++;
}
if (ytd.Loaded)
{
tex = ytd.TextureDict.Lookup(texhash);
}
}
if (tex == null)
{
ytd = null;
errstr = "<Couldn't find texture!>";
}
}
}
}
if (tex != null)
{
int mip = 0;
if (mipchange)
{
mip = SelTextureMipTrackBar.Value;
if (mip >= tex.Levels) mip = tex.Levels - 1;
}
else
{
SelTextureMipTrackBar.Maximum = tex.Levels - 1;
}
DisplayTexture(tex, mip);
//try get owner drawable to get the name for the dictionary textbox...
object owner = null;
if (Selection.Drawable != null)
{
owner = Selection.Drawable.Owner;
}
YdrFile ydr = owner as YdrFile;
YddFile ydd = owner as YddFile;
YftFile yft = owner as YftFile;
SelTextureNameTextBox.Text = tex.Name;
SelTextureDictionaryTextBox.Text = (ytd != null) ? ytd.Name : (ydr != null) ? ydr.Name : (ydd != null) ? ydd.Name : (yft != null) ? yft.Name : string.Empty;
}
else
{
SelDrawableTexturePictureBox.Image = null;
SelTextureNameTextBox.Text = errstr;
SelTextureDictionaryTextBox.Text = string.Empty;
SelTextureMipTrackBar.Value = 0;
SelTextureMipTrackBar.Maximum = 0;
SelTextureDimensionsLabel.Text = "-";
}
}
private Texture TryGetTextureFromYtd(uint texHash, uint txdHash, out YtdFile ytd)
{
if (txdHash != 0)
{
ytd = WorldForm.GameFileCache.GetYtd(txdHash);
if (ytd != null)
{
int tries = 0;
while (!ytd.Loaded && (tries < 500)) //wait upto ~5 sec
{
System.Threading.Thread.Sleep(10);
tries++;
}
if (ytd.Loaded)
{
return ytd.TextureDict.Lookup(texHash);
}
}
}
ytd = null;
return null;
}
private void WorldInfoForm_Load(object sender, EventArgs e)
{
SetSelection(Selection, SelectionItems);
}
private void WorldInfoForm_FormClosed(object sender, FormClosedEventArgs e)
{
WorldForm.OnInfoFormClosed();
}
public void SyncSelDrawableModelsTreeNode(TreeNode node)
{
//called by the world form when a selection treeview node is checked/unchecked.
foreach (TreeNode mnode in SelDrawableModelsTreeView.Nodes)
{
if (mnode.Tag == node.Tag)
{
if (mnode.Checked != node.Checked)
{
mnode.Checked = node.Checked;
}
}
foreach (TreeNode gnode in mnode.Nodes)
{
if (gnode.Tag == node.Tag)
{
if (gnode.Checked != node.Checked)
{
gnode.Checked = node.Checked;
}
}
}
}
}
private void SelDrawableModelsTreeView_AfterCheck(object sender, TreeViewEventArgs e)
{
WorldForm.SyncSelDrawableModelsTreeNode(e.Node);
}
private void SelDrawableModelsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
SelDrawableModelPropertyGrid.SelectedObject = e.Node?.Tag;
}
private void SelDrawableTexturesTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
SelDrawableTexturePropertyGrid.SelectedObject = e.Node?.Tag;
TextureBase texbase = e.Node?.Tag as TextureBase;
SelTextureMipTrackBar.Value = 0;
SelTextureMipLabel.Text = "0";
SelectTexture(texbase, false);
}
private void SelTextureMipTrackBar_Scroll(object sender, EventArgs e)
{
var node = SelDrawableTexturesTreeView.SelectedNode;
TextureBase texbase = node?.Tag as TextureBase;
SelTextureMipLabel.Text = SelTextureMipTrackBar.Value.ToString();
SelectTexture(texbase, true);
}
private void HierarchyTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
var sele = HierarchyTreeView.SelectedNode?.Tag as YmapEntityDef;
HierarchyPropertyGrid.SelectedObject = sele;
}
}
}