This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
/
BFLYT.cs
300 lines (265 loc) · 10.6 KB
/
BFLYT.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using Toolbox;
using System.Windows.Forms;
using Toolbox.Library;
using Toolbox.Library.Forms;
using Toolbox.Library.IO;
using FirstPlugin.Forms;
using Syroot.Maths;
using SharpYaml.Serialization;
using FirstPlugin;
using System.ComponentModel;
namespace LayoutBXLYT.Cafe
{
public class BFLYT : IFileFormat, IEditorForm<LayoutEditor>, IConvertableTextFormat
{
public FileType FileType { get; set; } = FileType.Layout;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Cafe Layout (GUI)" };
public string[] Extension { get; set; } = new string[] { "*.bflyt" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
{
return reader.CheckSignature(4, "FLYT");
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
types.Add(typeof(MenuExt));
return types.ToArray();
}
}
class MenuExt : IFileMenuExtension
{
public STToolStripItem[] NewFileMenuExtensions => newFileExt;
public STToolStripItem[] NewFromFileMenuExtensions => null;
public STToolStripItem[] ToolsMenuExtensions => null;
public STToolStripItem[] TitleBarExtensions => null;
public STToolStripItem[] CompressionMenuExtensions => null;
public STToolStripItem[] ExperimentalMenuExtensions => null;
public STToolStripItem[] EditMenuExtensions => null;
public ToolStripButton[] IconButtonMenuExtensions => null;
STToolStripItem[] newFileExt = new STToolStripItem[1];
public MenuExt()
{
newFileExt[0] = new STToolStripItem("Layout Editor");
newFileExt[0].Click += LoadNewLayoutEditor;
}
private void LoadNewLayoutEditor(object sender, EventArgs e)
{
LayoutEditor editor = new LayoutEditor();
editor.Show();
}
}
#region Text Converter Interface
public TextFileType TextFileType => TextFileType.Yaml;
public bool CanConvertBack => true;
public string ConvertToString()
{
var settings = new SerializerSettings();
settings.EmitTags = false;
settings.EmitAlias = false;
settings.EmitCapacityForList = false;
settings.EmitShortTypeName = false;
settings.ComparerForKeySorting = null;
settings.DefaultStyle = SharpYaml.YamlStyle.Any;
settings.LimitPrimitiveFlowSequence = 0;
// return FLYT.ToXml(header);
var serializer = new Serializer(settings);
string yaml = serializer.Serialize(header.RootPane, typeof(Header));
return yaml;
}
public void ConvertFromString(string text)
{
header = FLYT.FromXml(text);
header.FileInfo = this;
}
#endregion
public LayoutEditor OpenForm()
{
LayoutEditor editor = new LayoutEditor();
editor.Dock = DockStyle.Fill;
editor.LoadBxlyt(header);
return editor;
}
public void FillEditor(Form control) {
((LayoutEditor)control).LoadBxlyt(header);
}
public Header header;
public void Load(System.IO.Stream stream)
{
CanSave = true;
header = new Header();
header.Read(new FileReader(stream), this);
}
public List<GTXFile> GetShadersGTX()
{
List<GTXFile> shaders = new List<GTXFile>();
if (IFileInfo.ArchiveParent != null)
{
foreach (var file in IFileInfo.ArchiveParent.Files)
{
if (Utils.GetExtension(file.FileName) == ".gsh")
{
GTXFile bnsh = (GTXFile)file.OpenFile();
shaders.Add(bnsh);
}
}
}
return shaders;
}
public List<BNSH> GetShadersNX()
{
List<BNSH> shaders = new List<BNSH>();
if (IFileInfo.ArchiveParent != null)
{
foreach (var file in IFileInfo.ArchiveParent.Files)
{
if (Utils.GetExtension(file.FileName) == ".bnsh")
{
BNSH bnsh = (BNSH)file.OpenFile();
shaders.Add(bnsh);
}
}
}
return shaders;
}
public List<BFLYT> GetLayouts()
{
List<BFLYT> animations = new List<BFLYT>();
if (IFileInfo.ArchiveParent != null)
{
foreach (var file in IFileInfo.ArchiveParent.Files)
{
if (Utils.GetExtension(file.FileName) == ".bflyt")
{
BFLYT bflyt = (BFLYT)file.OpenFile();
animations.Add(bflyt);
}
}
}
return animations;
}
public List<BFLAN> GetAnimations()
{
List<BFLAN> animations = new List<BFLAN>();
if (IFileInfo.ArchiveParent != null)
{
foreach (var file in IFileInfo.ArchiveParent.Files)
{
if (Utils.GetExtension(file.FileName) == ".bflan" && file.FileFormat == null)
{
BFLAN bflan = (BFLAN)file.OpenFile();
file.FileFormat = bflan;
bflan.IFileInfo.ArchiveParent = IFileInfo.ArchiveParent;
//Disable saving unless the file gets edited
//Prevents broken files if version is unsupported
bflan.CanSave = false;
animations.Add(bflan);
}
}
}
return animations;
}
public Dictionary<string, STGenericTexture> GetTextures()
{
Dictionary<string, STGenericTexture> textures = new Dictionary<string, STGenericTexture>();
if (File.Exists(FilePath))
{
string folder = Path.GetDirectoryName(FilePath);
foreach (var file in Directory.GetFiles(folder))
{
try
{
if (Utils.GetExtension(file) == ".bflim")
{
BFLIM bflim = (BFLIM)STFileLoader.OpenFileFormat(file);
if (!textures.ContainsKey(bflim.FileName))
textures.Add(bflim.FileName, bflim);
}
if (Utils.GetExtension(file) == ".bntx")
{
BNTX bntx = (BNTX)STFileLoader.OpenFileFormat(file);
foreach (var tex in bntx.Textures)
{
if (!textures.ContainsKey(tex.Key))
textures.Add(tex.Key, tex.Value);
}
string fileName = Path.GetFileName(file);
if (!header.TextureManager.BinaryContainers.ContainsKey(fileName))
header.TextureManager.BinaryContainers.Add(fileName, bntx);
}
}
catch (Exception ex)
{
STErrorDialog.Show($"Failed to load texture {file}. ", "Layout Editor", ex.ToString());
}
}
}
foreach (var archive in PluginRuntime.SarcArchives)
{
foreach (var file in archive.Files)
{
try
{
if (Utils.GetExtension(file.FileName) == ".bntx")
{
BNTX bntx = (BNTX)file.OpenFile();
file.FileFormat = bntx;
foreach (var tex in bntx.Textures)
if (!textures.ContainsKey(tex.Key))
textures.Add(tex.Key, tex.Value);
if (!header.TextureManager.BinaryContainers.ContainsKey($"{archive.FileName}.bntx"))
header.TextureManager.BinaryContainers.Add($"{archive.FileName}.bntx", bntx);
}
if (Utils.GetExtension(file.FileName) == ".bflim")
{
BFLIM bflim = (BFLIM)file.OpenFile();
string name = bflim.FileName;
if (archive is SARC)
{
if (((SARC)archive).sarcData.HashOnly)
{
var sarcEntry = file as SARC.SarcEntry;
//Look through all textures and find a hash match
name = sarcEntry.TryGetHash(header.Textures, "timg");
name = Path.GetFileName(name);
}
}
file.FileFormat = bflim;
if (!textures.ContainsKey(bflim.FileName))
textures.Add(name, bflim);
}
}
catch (Exception ex)
{
STErrorDialog.Show($"Failed to load texture {file.FileName}. ", "Layout Editor", ex.ToString());
}
}
if (!header.TextureManager.ArchiveFile.ContainsKey(archive.FileName))
header.TextureManager.ArchiveFile.Add(archive.FileName, archive);
}
return textures;
}
public void Unload()
{
if (header != null)
{
}
}
public void Save(System.IO.Stream stream) {
header.Write(new FileWriter(stream));
}
}
}