forked from MapStudioProject/MapStudio.UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchiveEditor.cs
More file actions
256 lines (218 loc) · 8.68 KB
/
Copy pathArchiveEditor.cs
File metadata and controls
256 lines (218 loc) · 8.68 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Toolbox.Core;
using Toolbox.Core.ViewModels;
namespace MapStudio.UI
{
internal class ArchiveEditor
{
public static void Load(IArchiveFile file, NodeBase root)
{
CreateObjectHiearchy(root, file);
}
static NodeBase CreateObjectHiearchy(NodeBase parent, IArchiveFile archiveFile)
{
// build a TreeNode collection from the file list
foreach (var file in archiveFile.Files)
{
string[] paths = file.FileName.Split('/');
ProcessTree(parent, file, paths, 0);
}
return parent;
}
static void ProcessTree(NodeBase parent, ArchiveFileInfo file, string[] paths, int index)
{
string currentPath = paths[index];
if (paths.Length - 1 == index)
{
var fileNode = new FileNode(currentPath, file);
string ext = Utils.GetExtension(currentPath);
if (string.IsNullOrEmpty(Path.GetExtension(file.FileName)))
fileNode.Header += ".bin";
parent.AddChild(fileNode);
return;
}
var node = FindFolderNode(parent, currentPath);
if (node == null)
{
node = new FolderNode(currentPath);
node.Icon = "Folder";
parent.AddChild(node);
}
ProcessTree(node, file, paths, index + 1);
}
private static NodeBase FindFolderNode(NodeBase parent, string path)
{
NodeBase node = null;
foreach (var child in parent.Children.ToArray())
{
if (child.Header.Equals(path))
{
node = child;
break;
}
}
return node;
}
class FolderNode : NodeBase
{
public FolderNode(string name) : base(name)
{
Icon = IconManager.FOLDER_ICON.ToString();
}
}
class FileNode : NodeBase
{
MemoryEditor MemoryEditor = new MemoryEditor();
/// <summary>
/// The attached file information from an archive file.
/// </summary>
private ArchiveFileInfo FileInfo;
public FileNode(string name, ArchiveFileInfo fileInfo) : base(name)
{
CanRename = fileInfo.ParentArchiveFile.CanRenameFiles;
FileInfo = fileInfo;
Tag = fileInfo;
Icon = IconManager.FILE_ICON.ToString();
if (!string.IsNullOrEmpty(FileInfo.Icon))
Icon = FileInfo.Icon;
IconColor = FileInfo.IconColor;
if (name.EndsWith(".byaml") || name.EndsWith(".byml"))
IconColor = new System.Numerics.Vector4(0.564f, 0.792f, 0.97f, 1);
if (name.EndsWith(".bcmdl") || name.EndsWith(".bch"))
{
IconColor = new System.Numerics.Vector4(1, 0.5f, 0, 1);
Icon = '\uf1b2'.ToString();
}
TagUI.UIDrawer += delegate
{
RenderHexView();
};
ContextMenus.Add(new MenuItemModel("Rename", () => {
ActivateRename = true;
})
{ IsEnabled = fileInfo.ParentArchiveFile.CanRenameFiles });
ContextMenus.Add(new MenuItemModel("Export Raw Data", Export));
ContextMenus.Add(new MenuItemModel("Export Raw Data to File Location", ExportToFileLocation));
ContextMenus.Add(new MenuItemModel("Replace Raw Data", Replace) { IsEnabled = fileInfo.ParentArchiveFile.CanReplaceFiles });
ContextMenus.Add(new MenuItemModel(""));
ContextMenus.Add(new MenuItemModel("Delete", Delete) { IsEnabled = fileInfo.ParentArchiveFile.CanDeleteFiles });
OnSelected += delegate
{
//TODO this will work better when the file editor gets switched better for opened files
// if (IsSelected)
//Workspace.ActiveWorkspace.ActiveEditor = FileInfo.ParentArchiveFile as FileEditor;
};
OnHeaderRenamed += delegate
{
FileInfo.FileName = GetFullPath(this);
};
}
private static string GetFullPath(NodeBase node)
{
return String.Join(Path.AltDirectorySeparatorChar, GetPaths(node));
}
private static List<string> GetPaths(NodeBase node)
{
List<string> paths = new List<string>();
if (node.Parent != null && node.Parent is FolderNode)
GetParentPaths(ref paths, node.Parent);
paths.Add(node.Header);
return paths;
}
private static void GetParentPaths(ref List<string> paths , NodeBase node)
{
if (node.Parent != null && node.Parent is FolderNode)
GetParentPaths(ref paths, node.Parent);
paths.Add(node.Header);
}
private void Export()
{
ImguiFileDialog dlg = new ImguiFileDialog();
dlg.SaveDialog = true;
dlg.FilterAll = true;
dlg.FileName = this.Header;
if (dlg.ShowDialog())
ExportFile(dlg.FilePath);
}
private void ExportToFileLocation()
{
string folderPath = ((IFileFormat)FileInfo.ParentArchiveFile).FileInfo.FolderPath;
if (!Directory.Exists(folderPath))
return;
string filePath = Path.Combine(folderPath, Path.GetFileName(this.Header));
ExportFile(filePath);
}
private void ExportFile(string filePath)
{
if (FileInfo.FileFormat != null && FileInfo.FileFormat.CanSave)
{
var mem = new MemoryStream();
FileInfo.FileFormat.Save(mem);
File.WriteAllBytes(filePath, mem.ToArray());
}
else
{
File.WriteAllBytes(filePath, FileInfo.AsBytes());
}
}
private void Replace()
{
ImguiFileDialog dlg = new ImguiFileDialog();
dlg.FilterAll = true;
if (dlg.ShowDialog())
{
FileInfo.SetData(File.ReadAllBytes(dlg.FilePath));
if (FileInfo.FileFormat != null)
OpenFile();
}
}
private void Delete()
{
int result = TinyFileDialog.MessageBoxInfoYesNo($"Are you sure you want to remove file {Header}? This operation cannot be undone.");
if (result != 1)
return;
FileInfo.ParentArchiveFile.DeleteFile(FileInfo);
this.Parent.Children.Remove(this);
}
public override void OnDoubleClicked()
{
if (FileInfo.FileFormat == null)
OpenFile();
}
private void OpenFile()
{
FileInfo.FileFormat = FileInfo.OpenFile();
if (FileInfo.FileFormat == null)
return;
var editor = FileInfo.FileFormat as FileEditor;
editor.Scene.Init();
this.Tag = FileInfo.FileFormat;
this.TagUI.Tag = editor.Root.TagUI;
this.Children.Clear();
foreach (var child in editor.Root.Children)
this.Children.Add(child);
Workspace.ActiveWorkspace.ViewportWindow.Pipeline.AddFile(editor, this.Header);
Workspace.ActiveWorkspace.ActiveEditor = editor;
}
public void SaveFile()
{
if (FileInfo.FileFormat != null && FileInfo.FileFormat.CanSave)
{
var mem = new MemoryStream();
FileInfo.FileFormat.Save(mem);
//Compress if necessary
FileInfo.FileData = FileInfo.CompressData(mem);
}
}
private void RenderHexView()
{
byte[] data = FileInfo.AsBytes();
MemoryEditor.Draw(data, data.Length);
}
}
}
}