This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
262 lines (234 loc) · 9.52 KB
/
Program.cs
File metadata and controls
262 lines (234 loc) · 9.52 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
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Numerics;
using BrawlLib.Internal.IO;
using BrawlLib.Modeling.Collada;
using BrawlLib.SSBB.ResourceNodes;
using BrawlLib.SSBB.Types;
using BrawlLib.Wii.Compression;
using BrawlLib.Wii.Textures;
using Sledge.Formats.Map.Formats;
using Sledge.Formats.Map.Objects;
using Pfim;
using System.Runtime.InteropServices;
namespace StageHammer
{
public static class Program
{
public static void Main(string[] args)
{
// Args should have at least 3 parameters
if (args.Length < 3)
{
Console.WriteLine("Not enough arguments.");
return;
}
// Check to see if basename in args exists as a .pac in the same directory
// If it does, load it as an ARCNode
string basename = System.IO.Path.GetFileNameWithoutExtension(args[2]);
string pacPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(args[2]), basename + ".pac");
if (!File.Exists(pacPath))
{
// Copy the local (cwd) .pac file as a template
string localPacPath = System.IO.Path.Combine(Environment.CurrentDirectory, "template.pac");
if (!File.Exists(localPacPath))
{
Console.WriteLine("No template.pac file found in {0} or {1}.", System.IO.Path.GetDirectoryName(args[2]), Environment.CurrentDirectory);
return;
}
File.Copy(localPacPath, pacPath);
Console.WriteLine("Copied {0} to {1}.", localPacPath, pacPath);
}
ARCNode stage = NodeFactory.FromFile(null, pacPath) as ARCNode;
// Find first child with children (the first one usually doesn't have any)
ARCNode stageEntry = null;
foreach (ARCNode entry in stage.Children)
{
if (entry.Children.Count > 0)
{
stageEntry = entry;
break;
}
}
// Error if not found
if (stageEntry == null)
{
Console.WriteLine("No stage entries found in {0}.", pacPath);
return;
}
BRRESNode modelData = null;
foreach (ARCEntryNode entry in stageEntry.Children)
{
if (entry.isModelData())
{
modelData = entry as BRRESNode;
break;
}
}
// Error if not found
if (modelData == null)
{
Console.WriteLine("No model data found in {0}.", pacPath);
return;
}
// Find the 3DModels group
BRESGroupNode modelsGroup = null;
foreach (BRESGroupNode group in modelData.Children)
{
if (group.Name == "3DModels(NW4R)")
{
modelsGroup = group;
break;
}
}
// Error if not found
if (modelsGroup == null)
{
Console.WriteLine("No 3DModels group found in {0}.", pacPath);
return;
}
// Find the first MDL0Node
MDL0Node model = null;
foreach (MDL0Node node in modelsGroup.Children)
{
model = node;
break;
}
// Error if not found
if (model == null)
{
Console.WriteLine("No MDL0Node found in {0}.", pacPath);
return;
}
// Delete all valid images in the output directory
DirectoryInfo outputDir = new DirectoryInfo(System.IO.Path.GetDirectoryName(args[2]));
FileInfo[] outputFiles;
outputFiles = outputDir.GetFiles();
foreach (FileInfo info in outputFiles)
{
string ext = System.IO.Path.GetExtension(info.FullName).ToUpper();
if (ext == ".TGA")
{
info.Delete();
}
}
string vmfPath = args[2];
string colladaPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(args[2]), basename + ".dae");
// Delete the collada file if it exists
if (File.Exists(colladaPath))
{
File.Delete(colladaPath);
}
// Script path is CWD
string colladaScriptPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ColladaExport.py");
Console.WriteLine("Opening Blender...");
// Execute blender --background --python path/to/executable/ColladaExport.py -- <vmfPath> <colladaPath>
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "blender",
Arguments = string.Format("--background --python \"{0}\" -- \"{1}\" \"{2}\"", colladaScriptPath, vmfPath, colladaPath),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
System.Diagnostics.Process process = new System.Diagnostics.Process
{
StartInfo = startInfo
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
process.WaitForExit();
// Error if not found
if (!File.Exists(colladaPath))
{
Console.WriteLine("Collada model could not be generated.");
return;
}
MDL0Node colladaModel = new Collada {Text = $"Import Settings - {System.IO.Path.GetFileName(colladaPath)}"}.ImportModel(colladaPath,
Collada.ImportType.MDL0) as MDL0Node;
// Replace the model with the collada model
model.Replace(colladaModel);
// Delete the collada model
colladaModel.Dispose();
//File.Delete(colladaPath);
Console.WriteLine("Model replaced.");
// Find the texture data node
BRRESNode textureData = null;
foreach (ARCEntryNode entry in stageEntry.Children)
{
if (entry.isTextureData())
{
textureData = entry as BRRESNode;
break;
}
}
// Error if not found
if (textureData == null)
{
Console.WriteLine("No texture data found in {0}.", pacPath);
return;
}
// Find texture group
BRESGroupNode texturesGroup = null;
foreach (BRESGroupNode group in textureData.Children)
{
if (group.Name == "Textures(NW4R)")
{
texturesGroup = group;
break;
}
}
// Clear the texture data
texturesGroup.Children.Clear();
// Import textures from the working directory
DirectoryInfo dir = new DirectoryInfo(System.IO.Path.GetDirectoryName(args[2]));
FileInfo[] files;
files = dir.GetFiles();
foreach (FileInfo info in files)
{
string ext = System.IO.Path.GetExtension(info.FullName).ToUpper();
if (ext == ".TGA")
{
Console.WriteLine("Texture: {0}", info.FullName);
// Get width and height using bytes (TGA)
byte[] bytes = File.ReadAllBytes(info.FullName);
int width = BitConverter.ToInt16(bytes, 12);
int height = BitConverter.ToInt16(bytes, 14);
Console.WriteLine("Width: {0}, Height: {1}", width, height);
// Use Pfim
IImage pfImage = Pfimage.FromFile(info.FullName);
// Create bitmap
GCHandle pinnedArray = GCHandle.Alloc(pfImage.Data, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
Bitmap bitmap = new Bitmap(width, height, pfImage.Stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, pointer);
pinnedArray.Free();
// Convert the texture
TextureConverter format = TextureConverter.Get(WiiPixelFormat.RGB565);
FileMap textureFile = format.EncodeTEX0Texture(bitmap, 1);
bitmap.Dispose();
TEX0Node texture = textureData.CreateResource<TEX0Node>(System.IO.Path.GetFileNameWithoutExtension(info.FullName));
// Add the texture to the texture group
texture.ReplaceRaw(textureFile);
texturesGroup.AddChild(texture);
// Delete the file
info.Delete();
}
}
Console.WriteLine("Textures imported.");
// Save the stage
stage.Rebuild();
model.Rebuild();
modelsGroup.Rebuild();
modelData.Rebuild();
texturesGroup.Rebuild();
textureData.Rebuild();
stageEntry.Rebuild();
stage.Rebuild();
stage.Export(pacPath);
Console.WriteLine("Stage saved to {0}.", pacPath);
}
}
}