Skip to content

Commit 5e456ce

Browse files
committed
(KnuxTools) Refactor Directory Handling
Hoping to tidy up KnuxTools a bit and settle on consistent strings for the `version` flag.
1 parent 9aa2f8a commit 5e456ce

File tree

1 file changed

+109
-137
lines changed

1 file changed

+109
-137
lines changed

KnuxTools/Program.cs

+109-137
Original file line numberDiff line numberDiff line change
@@ -140,165 +140,137 @@ static void Main(string[] args)
140140
private static void HandleDirectory(string arg, string? version)
141141
{
142142
// If a version isn't specified, then ask the user which archive to save as.
143-
if (version == null)
143+
if (string.IsNullOrEmpty(version))
144144
{
145-
// List our supported archive types.
146-
Console.WriteLine
147-
(
148-
"Please specify the archive type to pack this directory into;\n" +
149-
"1. NiGHTS 2 Engine ONE File\n" +
150-
"2. Sonic The Portable Engine AMB File\n" +
151-
"3. Sonic Storybook Engine ONE File\n" +
152-
"4. Sonic Storybook Engine TXD File\n" +
153-
"5. Sonic World Adventure Wii ONE File.\n" +
154-
"6. Wayforward Engine PAK File.\n" +
155-
"R. Convert Hedgehog Engine Terrain Instances into a Hedgehog Engine Point Cloud."
156-
);
157-
158-
// Wait for the user to input an option.
159-
switch (Console.ReadKey().KeyChar)
145+
// List the supported archive types.
146+
Console.WriteLine("Please specify the archive type to pack this directory into, valid options are:");
147+
Console.WriteLine(" nights2\t\t\t(NiGHTS 2 Engine ONE File)");
148+
Console.WriteLine(" hh_instance2pointcloud\t(Convert Hedgehog Engine Terrain Instances into a Hedgehog Engine Point Cloud)");
149+
Console.WriteLine(" storybook\t\t\t(Sonic Storybook Engine ONE File)");
150+
Console.WriteLine(" storybook_texture\t\t(Sonic Storybook Engine TXD File)");
151+
Console.WriteLine(" portable\t\t\t(Sonic The Portable Engine AMB File)");
152+
Console.WriteLine(" swawii\t\t\t(Sonic World Adventure Wii Engine ONE File)");
153+
Console.WriteLine(" swawii_compressed\t\t(Sonic World Adventure Wii Engine Compressed ONZ File)");
154+
Console.WriteLine(" wayforward\t\t\t(Wayforward Engine PAK File)");
155+
156+
// Ask for the user's input.
157+
Console.Write("\nArchive Type: ");
158+
159+
// Wait for the user's input.
160+
version = Console.ReadLine().ToLower();
161+
162+
// Sanity check the input, abort if its still null or empty.
163+
if (string.IsNullOrEmpty(version))
160164
{
161-
// NiGHTS 2 Engine ONE Archives.
162-
case '1': version = "nights2_one"; break;
163-
164-
// Sonic The Portable Engine AMB Archives.
165-
case '2': version = "portable_amb"; break;
166-
167-
// Sonic Storybook Series Engine ONE Archives.
168-
case '3': version = "storybook_one"; break;
165+
Console.WriteLine("\nNo archive type specified! Aborting...\nPress any key to continue.");
166+
Console.ReadKey();
167+
return;
168+
}
169169

170-
// Sonic Storybook Series Engine Texture Directories.
171-
case '4': version = "storybook_txd"; break;
170+
// Add a line break.
171+
Console.WriteLine();
172+
}
172173

173-
// Sonic World Adventure Wii Engine ONE Archives.
174-
case '5':
175-
// List our supported versions for the Sonic World Adventure Wii ONE format.
176-
Console.WriteLine
177-
(
178-
"\n\nThis file has multiple file version options, please specifiy the version to save with;\n" +
179-
"1. Uncompressed ONE Archive.\n" +
180-
"2. Compressed ONZ Archive"
181-
);
174+
// Decide what to do based on the version value.
175+
// In most cases this will be writing a line for user feedback then running the ImportAndSaveArchive with the right type and extension.
176+
switch (version.ToLower())
177+
{
178+
// NiGHTS 2 Engine ONE Archives.
179+
case "nights2":
180+
Console.WriteLine("Packing directory for NiGHTS 2 Engine.");
181+
ImportAndSaveArchive(typeof(KnuxLib.Engines.NiGHTS2.ONE), arg, "one");
182+
break;
182183

183-
// Set the version to either swawii_one or swawii_onz depending on the user's selection.
184-
switch (Console.ReadKey().KeyChar)
185-
{
186-
case '1': version = "swawii_one"; break;
187-
case '2': version = "swawii_onz"; break;
188-
}
189-
break;
184+
// Hedgehog Engine Terrain Instance Conversion.
185+
case "hh_instance2pointcloud":
186+
Console.WriteLine("Converting Hedgehog Engine Terrain Instance Info files to Hedgehog Engine Point Cloud files.");
187+
KnuxLib.Engines.Hedgehog.InstanceInfo.ConvertDirectoryToPointCloud(arg);
188+
break;
190189

191-
// Wayforward Engine Packages.
192-
case '6': version = "wayforward_pak"; break;
190+
// Sonic Storybook Series ONE Archives.
191+
case "storybook":
192+
Console.WriteLine("Packing directory for Sonic Storybook Engine.");
193+
ImportAndSaveArchive(typeof(KnuxLib.Engines.Storybook.ONE), arg, "one");
194+
break;
193195

194-
// Hedgehog Engine Terrain Instance Conversion.
195-
case 'r': version = "hedgehog_terrain2pointcloud"; break;
196-
}
197-
}
196+
// Sonic Storybook Series Texture Directories.
197+
case "storybook_texture":
198+
Console.WriteLine("Packing directory for Sonic Storybook Engine.");
199+
ImportAndSaveArchive(typeof(KnuxLib.Engines.Storybook.TextureDirectory), arg, "txd");
200+
break;
198201

199-
// Sanity check that version actually has a value.
200-
if (version != null)
201-
{
202-
switch (version)
203-
{
204-
// NiGHTS 2 Engine ONE Archives.
205-
case "nights2_one":
206-
using (KnuxLib.Engines.NiGHTS2.ONE one = new())
207-
{
208-
Console.WriteLine("\n");
209-
one.Import(arg);
210-
one.Save($@"{arg}.one");
211-
}
212-
break;
202+
// Sonic The Portable Engine AMB Archives.
203+
case "portable":
204+
Console.WriteLine("Packing directory for Sonic The Portable Engine.");
205+
ImportAndSaveArchive(typeof(KnuxLib.Engines.Portable.AMB), arg, "amb");
206+
break;
213207

214-
// Sonic The Portable Engine AMB Archives.
215-
case "portable_amb":
216-
using (KnuxLib.Engines.Portable.AMB amb = new())
217-
{
218-
Console.WriteLine("\n");
219-
amb.Import(arg);
220-
amb.Save($@"{arg}.amb");
221-
}
222-
break;
208+
// Sonic World Adventure Wii ONE Archives.
209+
case "swawii":
210+
case "swawii_compressed":
211+
Console.WriteLine("Packing directory for Sonic World Adventure Wii Engine.");
212+
ImportAndSaveArchive(typeof(KnuxLib.Engines.WorldAdventureWii.ONE), arg, "one");
223213

224-
// Sonic Storybook Series ONE Archives.
225-
case "storybook_one":
226-
using (KnuxLib.Engines.Storybook.ONE one = new())
227-
{
228-
Console.WriteLine("\n");
229-
one.Import(arg);
230-
one.Save($@"{arg}.one");
231-
}
232-
break;
214+
// If the version indicates that the archive needs to be compressed, then compress it.
215+
if (version == "swawii_compressed")
216+
{
217+
// Inform the user of the compression.
218+
Console.WriteLine("Compressing generated archive for Sonic World Adventure Wii Engine.");
233219

234-
// Sonic Storybook Series Texture Directories.
235-
case "storybook_txd":
236-
using (KnuxLib.Engines.Storybook.TextureDirectory textureDirectory = new())
237-
{
238-
Console.WriteLine("\n");
239-
textureDirectory.Import(arg);
240-
textureDirectory.Save($@"{arg}.txd");
241-
}
242-
break;
220+
// Set up PuyoTools' LZ11 Compression.
221+
PuyoTools.Core.Compression.Lz11Compression lz11 = new();
243222

244-
// Sonic World Adventure Wii ONE Archives.
245-
case "swawii_one":
246-
case "swawii_onz":
247-
using (KnuxLib.Engines.WorldAdventureWii.ONE one = new())
248-
{
249-
Console.WriteLine("\n");
250-
one.Import(arg);
251-
one.Save($@"{arg}.one");
252-
}
223+
// Set up a file stream of the uncompressed archive.
224+
var stream = File.OpenRead($@"{arg}.one");
253225

254-
// If the version indicates that the archive needs to be compressed, then compress it.
255-
if (version == "swawii_onz")
256-
{
257-
// Set up a buffer.
258-
MemoryStream buffer = new();
226+
// Compress the previously saved ONE archive into a buffer.
227+
MemoryStream buffer = lz11.Compress(stream);
259228

260-
// Set up PuyoTools' LZ11 Compression.
261-
PuyoTools.Core.Compression.Lz11Compression lz11 = new();
229+
// Write the buffer to disk.
230+
buffer.WriteTo(File.Create($@"{arg}.onz"));
262231

263-
// Set up a file stream of the uncompressed archive.
264-
var stream = File.OpenRead($@"{arg}.one");
232+
// Close the file stream so the uncompressed archive can be deleted.
233+
stream.Close();
265234

266-
// Compress the previously saved ONE archive into the buffer.
267-
buffer = lz11.Compress(stream);
235+
// Delete the temporary uncompressed file.
236+
File.Delete($@"{arg}.one");
237+
}
238+
break;
268239

269-
// Write the buffer to disk.
270-
buffer.WriteTo(File.Create($@"{arg}.onz"));
240+
// Wayforward Engine Packages.
241+
case "wayforward":
242+
Console.WriteLine("Packing directory for Wayforward Engine.");
243+
ImportAndSaveArchive(typeof(KnuxLib.Engines.Wayforward.Package), arg, "pak");
244+
break;
271245

272-
// Close the file stream so the uncompressed archive can be deleted.
273-
stream.Close();
246+
// If a command line argument without a corresponding format has been passed, then inform the user.
247+
default:
248+
Console.WriteLine($"Format identifer '{version}' is not valid for any currently supported archive types.\nPress any key to continue.");
249+
Console.ReadKey();
250+
return;
251+
}
274252

275-
// Delete the temporary uncompressed file.
276-
File.Delete($@"{arg}.one");
277-
}
278-
break;
253+
// Tell the user we're done (for if the cmd window is left open).
254+
Console.WriteLine("Done!");
255+
}
279256

280-
// Wayforward Engine Packages.
281-
case "wayforward_pak":
282-
using (KnuxLib.Engines.Wayforward.Package pak = new())
283-
{
284-
Console.WriteLine("\n");
285-
pak.Import(arg);
286-
pak.Save($@"{arg}.pak");
287-
}
288-
break;
257+
/// <summary>
258+
/// Handles importing files to an archive class and saving it.
259+
/// </summary>
260+
/// <param name="type">The type of archive to use</param>
261+
/// <param name="path">The path to the directory we're packing.</param>
262+
/// <param name="extension">The extension to save the packaged archive with.</param>
263+
private static void ImportAndSaveArchive(Type type, string path, string extension)
264+
{
265+
// Create an object for the specified archive type.
266+
object archive = Activator.CreateInstance(type);
289267

290-
// Hedgehog Engine Terrain Instance Conversion.
291-
case "hedgehog_terrain2pointcloud":
292-
KnuxLib.Engines.Hedgehog.InstanceInfo.ConvertDirectoryToPointCloud(arg);
293-
break;
268+
// Invoke this archive's import method with the specified directory as an argument.
269+
type.GetMethod("Import").Invoke(archive, new string[1] { path });
294270

295-
// If a command line argument without a corresponding format has been passed, then inform the user.
296-
default:
297-
Console.WriteLine($"\n\nFormat identifer {version} is not valid for any currently supported archive types.\nPress any key to continue.");
298-
Console.ReadKey();
299-
break;
300-
}
301-
}
271+
// Invoke this archive's save method with the specified directory and extension as arguments.
272+
type.GetMethod("Save", new[] {typeof(string)}).Invoke(archive, new string[1] { $@"{path}.{extension}" });
273+
302274
}
303275

304276
/// <summary>

0 commit comments

Comments
 (0)