Skip to content

Commit

Permalink
Switched to ctrtool for extracting CIAs, since HackingToolkit9ds was …
Browse files Browse the repository at this point in the history
…not working for some ROMs.
  • Loading branch information
MeltyPlayer committed Jun 19, 2023
1 parent 9481cf7 commit 750263c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ThreeDsFileHierarchyExtractor {
public IFileHierarchy ExtractFromRom(
ISystemFile romFile,
ISet<string>? junkTerms = null) {
new HackingToolkit9ds().Run(romFile, out var fileHierarchy);
new CtrtoolCiaExtractor().Run(romFile, out var fileHierarchy);

var archiveExtractor = new SubArchiveExtractor();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using fin.io;
using fin.log;
using fin.util.asserts;

using uni.platforms.gcn.tools;
using uni.util.cmd;


namespace uni.platforms.threeDs.tools {
public class CtrtoolCiaExtractor {
private static readonly object CTRTOOL_LOCK = new();

public bool Run(ISystemFile romFile, out IFileHierarchy hierarchy) {
Asserts.Equal(
".cia",
romFile.Extension,
$"Cannot dump ROM because it is not a CIA: {romFile}");
Asserts.True(
romFile.Exists,
$"Cannot dump ROM because it does not exist: {romFile}");

var didChange = false;

var finalDirectoryPath = romFile.FullNameWithoutExtension;
var finalDirectory = new FinDirectory(finalDirectoryPath);
if (!finalDirectory.Exists) {
didChange = true;

lock (CtrtoolCiaExtractor.CTRTOOL_LOCK) {
var beforeFiles = ThreeDsToolsConstants.CTRTOOL_DIRECTORY
.GetExistingFiles()
.ToHashSet();
var beforeSubdirs = ThreeDsToolsConstants
.CTRTOOL_DIRECTORY
.GetExistingSubdirs()
.ToHashSet();

var directoryPath = Path.Join(
ThreeDsToolsConstants.CTRTOOL_DIRECTORY.FullName,
"romfs");
var directory = new FinDirectory(directoryPath);

if (!directory.Exists) {
this.DumpRom_(romFile);
Asserts.True(directory.Exists,
$"Failed to find expected ROM filesystem output directory. There may be something with the following CIA: {romFile.FullName}");
}

Directory.Move(directoryPath, finalDirectoryPath);
Asserts.True(finalDirectory.Exists,
$"Directory was not created: {finalDirectory}");

var afterFiles = ThreeDsToolsConstants.CTRTOOL_DIRECTORY
.GetExistingFiles()
.ToArray();
var afterSubdirs = ThreeDsToolsConstants.CTRTOOL_DIRECTORY
.GetExistingSubdirs()
.ToArray();

// Cleans up unneeded files & directories
foreach (var afterFile in afterFiles) {
if (!beforeFiles.Contains(afterFile)) {
afterFile.Delete();
}
}

foreach (var afterSubdir in afterSubdirs) {
if (!beforeSubdirs.Contains(afterSubdir)) {
afterSubdir.Delete(true);
}
}
}
}

hierarchy = new FileHierarchy(finalDirectory);
return didChange;
}

private void DumpRom_(ISystemFile romFile) {
var logger = Logging.Create<CtrtoolCiaExtractor>();
logger.LogInformation($"Dumping ROM {romFile}...");

Files.RunInDirectory(
ThreeDsToolsConstants.CTRTOOL_DIRECTORY,
() => {
ProcessUtil.ExecuteBlockingSilently(
ThreeDsToolsConstants.EXTRACT_CIA_BAT,
$"\"{romFile.FullName}\"");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

namespace uni.platforms.gcn.tools {
public static class ThreeDsToolsConstants {
public static ISystemDirectory CTRTOOL_DIRECTORY =
DirectoryConstants.TOOLS_DIRECTORY.GetSubdir("ctrtool");

public static ISystemFile EXTRACT_CIA_BAT =
ThreeDsToolsConstants.CTRTOOL_DIRECTORY.GetExistingFile(
"extract_cia.bat");


public static ISystemDirectory HACKING_TOOLKIT_9DS_DIRECTORY =
DirectoryConstants.TOOLS_DIRECTORY.GetSubdir(
"HackingToolkit9DSv12");
Expand Down

0 comments on commit 750263c

Please sign in to comment.