Skip to content

Commit

Permalink
Version 1.7
Browse files Browse the repository at this point in the history
Added NKit support (both .gcz and .iso)
WIT and nod aren't included anymore, they will be downloaded on first run
  • Loading branch information
UltiNaruto committed Sep 24, 2020
1 parent 5ceaacb commit 152387f
Show file tree
Hide file tree
Showing 31 changed files with 613 additions and 10,296 deletions.
4 changes: 2 additions & 2 deletions MP1_Trilogy_Rando_Generator/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 78 additions & 24 deletions MP1_Trilogy_Rando_Generator/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Expand Down Expand Up @@ -31,17 +32,6 @@ bool IsMetroidPrimeTrilogyNTSC(string fileName)
}
}

bool IsMetroidPrimeNTSC(string fileName)
{
using (var bR = new BinaryReader(File.OpenRead(fileName)))
{
String GameID = Encoding.ASCII.GetString(bR.ReadBytes(6));
if (GameID.Substring(0, 3) != "GM8")
return false;
return GameID[3] == 'E';
}
}

String RandomizeDeveloperCode()
{
const String AllowedCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand Down Expand Up @@ -86,7 +76,10 @@ public Form1()

private void Form1_Load(object sender, EventArgs e)
{
if(File.Exists("udc.bin"))
if (!Directory.Exists(@".\tmp"))
Directory.CreateDirectory(@".\tmp");

if (File.Exists("udc.bin"))
UsedDeveloperCodes.AddRange(File.ReadAllLines("udc.bin"));
if (IsTemplateISOGenerated())
{
Expand All @@ -98,6 +91,32 @@ private void Form1_Load(object sender, EventArgs e)
this.label2.Text = "BashPrime's Randomizer found!";
this.textBox1.Text = appSettings.outputPath;
this.comboBox1.SelectedIndex = this.comboBox1.Items.IndexOf(appSettings.outputType);

if (!NodManager.Installed())
{
SetProgressStatus(1, 2);
SetStatus("Downloading and installing nod...");
if (!NodManager.Init())
{
MessageBox.Show("Couldn't download nod");
this.Close();
}
SetProgressStatus(2, 2);
SetStatus("Idle");
}

if (!WITManager.Installed())
{
SetProgressStatus(1, 2);
SetStatus("Downloading and installing WIT...");
if (!WITManager.Init())
{
MessageBox.Show("Couldn't download WIT");
this.Close();
}
SetProgressStatus(2, 2);
SetStatus("Idle");
}
}

private void button1_Click(object sender, EventArgs e)
Expand All @@ -106,15 +125,12 @@ private void button1_Click(object sender, EventArgs e)

MessageBox.Show(@"/!\ This operation can take 10 mins or more on a 5400 RPM HDD. So please be patient!");

if (Directory.Exists("tmp"))
Directory.Delete("tmp", true);

using (var openFileDialog = new OpenFileDialog())
{
var dialogResult = default(DialogResult);

openFileDialog.Title = "Select a NTSC-U iso of Metroid Prime Trilogy";
openFileDialog.Filter = "Wii ISO File|*.iso";
openFileDialog.Filter = "Wii ISO File|*.iso|Wii NKit ISO File|*.nkit.iso;*.nkit.iso";
openFileDialog.FileName = "";
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
while (!openFileDialog.FileName.ToLower().EndsWith(".iso"))
Expand Down Expand Up @@ -143,7 +159,44 @@ private void button1_Click(object sender, EventArgs e)

SetProgressStatus(0, 5);
SetStatus("Extracting Metroid Prime Trilogy ISO...");
if (!NodManager.ExtractISO(wii_iso_path, false))
if (wii_iso_path.ToLower().EndsWith(".nkit.iso") || wii_iso_path.ToLower().EndsWith(".nkit.gcz"))
{
if (!NKitManager.ExtractISO(wii_iso_path))
{
MessageBox.Show("Failed extracting wii nkit iso!");
SetProgressStatus(5, 5);
SetStatus("Idle");
return;
}

try {
File.WriteAllBytes(@".\tmp\nkit\nkit_files.zip", Properties.Resources.R3ME01_nkit);
ZipFile.ExtractToDirectory(@".\tmp\nkit\nkit_files.zip", @".\tmp\nkit\DATA");
File.Move(@".\tmp\nkit\DATA\files\main.dol", @".\tmp\nkit\DATA\sys\main.dol");
File.Delete(@".\tmp\nkit\nkit_files.zip");
File.Delete(@".\tmp\nkit\DATA\files\boot.bin");
File.Delete(@".\tmp\nkit\DATA\files\appldr.bin");
File.Delete(@".\tmp\nkit\DATA\files\bi2.bin");
File.Delete(@".\tmp\nkit\DATA\files\fst.bin");
File.Delete(@".\tmp\nkit\DATA\sys\R3MEhdr.bin");
File.Delete(@".\tmp\nkit\DATA\sys\hdr.bin");
Directory.Move(@".\tmp\nkit", @".\tmp\wii");
} catch {
Directory.Delete(@".\tmp\nkit", true);
}
}
else if (wii_iso_path.ToLower().EndsWith(".iso"))
{
if (!NodManager.ExtractISO(wii_iso_path, false))
{
MessageBox.Show("Failed extracting wii iso!");
SetProgressStatus(5, 5);
SetStatus("Idle");
return;
}
}

if (!Directory.Exists(@".\tmp\wii"))
{
MessageBox.Show("Failed extracting wii iso!");
SetProgressStatus(5, 5);
Expand All @@ -163,7 +216,6 @@ private void button1_Click(object sender, EventArgs e)
SetProgressStatus(2, 5);
SetStatus("Stripping MP2 and MP3 from Metroid Prime Trilogy...");

//FileUtils.NullifyFiles(".\\tmp\\wii\\DATA\\files\\fe", true);
FileUtils.NullifyFiles(".\\tmp\\wii\\DATA\\files\\MP2", true);
FileUtils.NullifyFiles(".\\tmp\\wii\\DATA\\files\\MP3", true);
FileUtils.NullifyFiles(".\\tmp\\wii\\DATA\\files\\", "*.dol", "rs5mp1_p.dol", "rs5fe_p.dol");
Expand Down Expand Up @@ -218,7 +270,7 @@ private void button2_Click(object sender, EventArgs e)
{
File.Delete("gc_template.iso");
Directory.Delete("tmp", true);
this.label1.Text = "No Trilogy ISO template for BashPrime's Randomizer detected!(Only NTSC - U supported for now)";
this.label1.Text = "No Trilogy ISO template for BashPrime's Randomizer detected! (Only NTSC - U supported for now)";
this.button1.Enabled = true;
this.button2.Enabled = false;
}
Expand Down Expand Up @@ -251,7 +303,7 @@ private void button3_Click(object sender, EventArgs e)
if (dialogResult == DialogResult.Cancel)
{
if (!appSettings.prime1RandomizerPath.EndsWith(".exe"))
this.label2.Text = "BashPrime's Randomizer not found! (v2.2.2 required at least)";
this.label2.Text = "BashPrime's Randomizer not found! (v2.5.0 or later)";
return;
}
}
Expand Down Expand Up @@ -371,14 +423,16 @@ private void button4_Click(object sender, EventArgs e)

DevCode = RandomizeDeveloperCode();

//new DOL_AddSection_Patch(Patches.MP1_Dol_Path, DOL_AddSection_Patch.SectionType.TEXT, 0x80001800, 0x800).Apply();

//Patches.DisableHintSystem(true);
Patches.ApplySkipCutscenePatch(true);
Patches.ApplySkipCutscenePatch();
Patches.ApplyHeatProtectionPatch(randomizerSettings.heatProtection);
Patches.ApplySuitDamageReductionPatch(randomizerSettings.suitDamageReduction);
Patches.ApplyScanDashPatch(true);
Patches.ApplyScanDashPatch();
Patches.ApplyUnderwaterSlopeJumpFixPatch(true);
Patches.SetSaveFilename(DevCode + ".bin");
//Patches.ApplyLJumpFixPatch(true);
//Patches.ApplyInputPatch();

/* */

Expand All @@ -388,7 +442,7 @@ private void button4_Click(object sender, EventArgs e)
if (((String)comboBox1.SelectedItem).ToLower().EndsWith(".ciso"))
{
WITManager.CreateCompressISO(".\\tmp\\mpt.ciso", false, "R3ME" + DevCode);
if (File.Exists(GameSettingsDolphinEmuPath + "R3ME01.ini"))
if (File.Exists(GameSettingsDolphinEmuPath + "R3ME01.ini") && !File.Exists(GameSettingsDolphinEmuPath + "R3ME" + DevCode + ".ini"))
File.Copy(GameSettingsDolphinEmuPath + "R3ME01.ini", GameSettingsDolphinEmuPath + "R3ME" + DevCode + ".ini");
}
else if (((String)comboBox1.SelectedItem).ToLower().EndsWith(".wbfs"))
Expand Down
19 changes: 18 additions & 1 deletion MP1_Trilogy_Rando_Generator/MP1_Trilogy_Rando_Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NKit">
<HintPath>refs\NKit.dll</HintPath>
</Reference>
<Reference Include="SharpCompress">
<HintPath>refs\SharpCompress.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -62,6 +74,8 @@
<Compile Include="HelpForm.Designer.cs">
<DependentUpon>HelpForm.cs</DependentUpon>
</Compile>
<Compile Include="NKitManager.cs" />
<Compile Include="Patcher\DOL_AddSection_Patch.cs" />
<Compile Include="Patcher\DOL_ScrollCode_Patch.cs" />
<Compile Include="Patcher\IDolPatch.cs" />
<Compile Include="Utils\DirectoryUtils.cs" />
Expand Down Expand Up @@ -95,6 +109,8 @@
<DesignTime>True</DesignTime>
</Compile>
<None Include="embed_res\dummy.thp" />
<None Include="embed_res\R3ME01_nkit.zip" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -111,6 +127,7 @@
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy ..\..\res . /S /Q /C /Y</PostBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
55 changes: 55 additions & 0 deletions MP1_Trilogy_Rando_Generator/NKitManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Nanook.NKit;
using System.IO;
using System.Text.RegularExpressions;

namespace MP1_Trilogy_Rando_Generator
{
class NKitManager
{
static bool IsValidFilename(string value)
{
return Regex.IsMatch(value, @"^[\w\-. ]+$");
}

public static bool ExtractISO(string filename)
{
bool result = true;
if (!Directory.Exists(@".\tmp\nkit"))
Directory.CreateDirectory(@".\tmp\nkit");
try
{
using(var ndisc = new NDisc(null, File.OpenRead(filename)))
{
if (ndisc == null)
throw new System.Exception();
if (ndisc.ExtractBasicInfo().Id.Substring(0,6) != "R3ME01")
throw new System.Exception();
ndisc.ExtractFiles(ext_f => true,
(f, ext_f) =>
{
var path = @".\tmp\nkit\DATA\" + (ext_f.PartitionId == null ? @"sys" : @"files");
if (ext_f.Path != "")
path += @"\" + ext_f.Path;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var stream = File.OpenWrite(path + @"\" + ext_f.Name))
f.Copy(stream, ext_f.Length);
});
}
}
catch
{
result = false;
}
if (Directory.Exists(@".\Dats"))
Directory.Delete(@".\Dats", true);
if (Directory.Exists(@".\Processed"))
Directory.Delete(@".\Processed", true);
if (Directory.Exists(@".\Recovery"))
Directory.Delete(@".\Recovery", true);
if (!result)
Directory.Delete(@".\tmp\nkit", true);
return result;
}
}
}
37 changes: 29 additions & 8 deletions MP1_Trilogy_Rando_Generator/NodManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Text;
using System.Threading;

namespace MP1_Trilogy_Rando_Generator
{
class NodManager
{
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string text);

private const string NOD_PATH = @"nod\nod.exe";
private const string NOD_EXE_URL = "https://github.com/AxioDL/nod/releases/download/v1.0/nodtool.v1.win64.exe";
private const string NOD_LICENSE_URL = "https://raw.githubusercontent.com/AxioDL/nod/master/LICENSE";

public static bool Installed()
{
return File.Exists(NOD_PATH);
}

public static bool Init()
{
try
{
if (Installed())
return true;
using (var client = new WebClientPlus())
{
if (!Directory.Exists(@".\nod"))
Directory.CreateDirectory(@".\nod");
client.DownloadFile(NOD_EXE_URL, NOD_PATH);
client.DownloadFile(NOD_LICENSE_URL, @".\nod\LICENSE");
return true;
}
}
catch
{
return false;
}
}

public static bool ExtractISO(string filename, bool isGC_ISO)
{
Expand All @@ -25,8 +50,6 @@ public static bool ExtractISO(string filename, bool isGC_ISO)
info.CreateNoWindow = true;
info.UseShellExecute = false;
Process proc = Process.Start(info);
Thread.Sleep(1000);
SetWindowText(proc.MainWindowHandle, "Extracting "+(isGC_ISO ? "GC" : "Wii")+" ISO...");
proc.WaitForExit();
return proc.ExitCode == 0;
}
Expand Down Expand Up @@ -78,8 +101,6 @@ public static bool CreateISO(string filename, bool isGC_ISO, String title_id)
info.CreateNoWindow = true;
info.UseShellExecute = false;
Process proc = Process.Start(info);
Thread.Sleep(1000);
SetWindowText(proc.MainWindowHandle, "Creating " + (isGC_ISO ? "GC" : "Wii") + " ISO...");
proc.WaitForExit();
return proc.ExitCode == 0;
}
Expand Down
Loading

0 comments on commit 152387f

Please sign in to comment.