Skip to content

Commit

Permalink
USUM support and cro file fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisagi committed Nov 12, 2017
1 parent 2d5e3a8 commit d128c8e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 83 deletions.
43 changes: 21 additions & 22 deletions PokeTool/Handler/FileCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,30 @@ namespace PokeTool.Handler
{
static class FileCopy
{
public static bool CopyAllNecessaryFiles(int game, List<RomFsFile> files, string pathRomFs, bool croFilesCheck, List<string> croFiles, string codeBin)
public static bool CopyAllNecessaryFiles(Pokemon.Version game, List<RomFsFile> files, string pathRomFs, List<string> croFiles, string codeBin)
{
try
{
var appLocation = AppDomain.CurrentDomain.BaseDirectory;
var titleIdPath = Path.Combine(new string[] { appLocation, GetTitleId(game) }); // change to actual id
var titleIdPathRomFs = Path.Combine(new string[] { appLocation, GetTitleId(game), "romfs" }); // change to actual id
var titleIdPath = Path.Combine(new string[] { appLocation, GetTitleId(game) });
var titleIdPathRomFs = Path.Combine(new string[] { titleIdPath, "romfs" });
var titleIdPathExeFs = Path.Combine(Directory.GetParent(pathRomFs).FullName, "exefs");
if (!Directory.Exists(titleIdPathRomFs)) Directory.CreateDirectory(titleIdPathRomFs);

foreach (var romFsFile in files)
{
var targetFolder = romFsFile.GetDirectory(titleIdPathRomFs);
if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder);
var origFilePath = romFsFile.GetFullFilePath(pathRomFs);
var newFilePath = romFsFile.GetFullFilePath(titleIdPathRomFs);
if (!Directory.Exists(Path.Combine(titleIdPathRomFs, romFsFile.TopFolder))) Directory.CreateDirectory(Path.Combine(titleIdPathRomFs, romFsFile.TopFolder));
if (!Directory.Exists(Path.Combine(new string[] { titleIdPathRomFs, romFsFile.TopFolder, romFsFile.FirstFolder }))) Directory.CreateDirectory(Path.Combine(new string[] { titleIdPathRomFs, romFsFile.TopFolder, romFsFile.FirstFolder }));
if (!Directory.Exists(Path.Combine(new string[] { titleIdPathRomFs, romFsFile.TopFolder, romFsFile.FirstFolder, romFsFile.SecondFolder }))) Directory.CreateDirectory(Path.Combine(new string[] { titleIdPathRomFs, romFsFile.TopFolder, romFsFile.FirstFolder, romFsFile.SecondFolder }));
File.Copy(origFilePath, newFilePath, true);
}

// copy cro files if necessary
if (croFilesCheck)
// copy cro files
foreach (var croFile in croFiles)
{
foreach (var croFile in croFiles)
{
var origFilePath = Path.Combine(pathRomFs, croFile);
var newFilePath = Path.Combine(titleIdPathRomFs, croFile);
File.Copy(origFilePath, newFilePath, true);
}
var origFilePath = Path.Combine(pathRomFs, croFile);
var newFilePath = Path.Combine(titleIdPathRomFs, croFile);
File.Copy(origFilePath, newFilePath, true);
}

// copy and rename to 'code.bin'
Expand All @@ -52,22 +47,26 @@ public static bool CopyAllNecessaryFiles(int game, List<RomFsFile> files, string
}
}

private static string GetTitleId(int game)
private static string GetTitleId(Pokemon.Version game)
{
switch (game)
{
case 0:
case Pokemon.Version.X:
return "000400000011C400";
case 1:
case Pokemon.Version.Y:
return "000400000011C500";
case 2:
case Pokemon.Version.OmegaRuby:
return "0004000000055D00";
case 3:
case Pokemon.Version.AlphaSaphire:
return "0004000000055E00";
case 4:
case Pokemon.Version.Sun:
return "0004000000164800";
case 5:
case Pokemon.Version.Moon:
return "0004000000175E00";
case Pokemon.Version.UltraSun:
return "00040000001B5000";
case Pokemon.Version.UltraMoon:
return "00040000001B5100";
default:
return "titleID";
}
Expand Down
28 changes: 25 additions & 3 deletions PokeTool/Handler/FileHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using PokeTool.Objects;

namespace PokeTool.Handler
{
Expand Down Expand Up @@ -31,16 +32,37 @@ public List<string> GetFileListWithoutExtensions()
}
}

public List<string> GetCroFileList()
public List<string> GetCroFileList(Pokemon.Version version)
{
try
{
var files = Directory.GetFiles(Path, "*.cro");
var fileList = new List<string>();
foreach (var file in files)

switch (version)
{
fileList.Add(System.IO.Path.GetFileName(file));
case Pokemon.Version.X:
case Pokemon.Version.Y:
case Pokemon.Version.OmegaRuby:
case Pokemon.Version.AlphaSaphire:
var affectedFilesGen6 = new List<string> { "DllField", "DllPoke3Select", "DllBattle" };
foreach (var file in files)
{
if (affectedFilesGen6.Contains(System.IO.Path.GetFileNameWithoutExtension(file))) fileList.Add(System.IO.Path.GetFileName(file));
}
break;
case Pokemon.Version.Sun:
case Pokemon.Version.Moon:
case Pokemon.Version.UltraSun:
case Pokemon.Version.UltraMoon:
var affectedFilesGen7 = new List<string> { "Shop" };
foreach (var file in files)
{
if (affectedFilesGen7.Contains(System.IO.Path.GetFileNameWithoutExtension(file))) fileList.Add(System.IO.Path.GetFileName(file));
}
break;
}

return fileList;
}
catch (Exception)
Expand Down
29 changes: 29 additions & 0 deletions PokeTool/Objects/Pokemon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PokeTool.Objects
{
class Pokemon
{
public enum Version
{
X = 0,
Y = 1,
OmegaRuby = 2,
AlphaSaphire = 3,
Sun = 4,
Moon = 5,
UltraSun = 6,
UltraMoon = 7
}

public enum Validation
{
Backup,
RomFs
}
}
}
7 changes: 6 additions & 1 deletion PokeTool/Objects/RomFsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ public RomFsFile(string filename)
Filename = split[3];
}

public string GetDirectory(string romfsPath)
{
return Path.Combine(new string[] { romfsPath, TopFolder, FirstFolder, SecondFolder });
}

public string GetFullFilePath(string romfsPath)
{
return Path.Combine(new string[] { romfsPath, TopFolder, FirstFolder, SecondFolder, Filename });
return Path.Combine(new string[] { GetDirectory(romfsPath), Filename });
}

private string[] GetFilenameWithPath(string filenameFull)
Expand Down
12 changes: 7 additions & 5 deletions PokeTool/Form1.Designer.cs → PokeTool/PokeTool.Designer.cs

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

85 changes: 43 additions & 42 deletions PokeTool/Form1.cs → PokeTool/PokeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

namespace PokeTool
{
public partial class Form1 : Form
public partial class PokeTool : Form
{
private readonly PathValidator _pathValidator = new PathValidator();
private bool _checkBackup = false;
private bool _checkRomFs = false;
private bool _checkBackup;
private bool _checkRomFs;
private string _pathBackup;
private string _pathRomFs;

public Form1()
public PokeTool()
{
InitializeComponent();

cboGameSelection.SelectedIndex = 0;
cboGameSelection.SelectedIndex = 6;
new ToolTip().SetToolTip(cboGameSelection, "Please select the correct game, otherwise it will not work.");
new ToolTip().SetToolTip(btnBackup, "Select the game folder inside the pk3ds backup folder.");
new ToolTip().SetToolTip(btnRomFs, "Select the romfs folder from the extracted game files.");
Expand All @@ -38,51 +38,33 @@ private void btnBackup_Click(object sender, EventArgs e)
var path = _pathValidator.FolderSelector();
if (!_pathValidator.CheckBackupFolder(path))
{
lblBackupValidator.Text = @"invalid";
lblBackupValidator.ForeColor = Color.Red;
_checkBackup = false;
btnStart.Enabled = false;
ChangeValidationState(lblBackupValidator, false, Pokemon.Validation.Backup);
if (path != string.Empty) MessageBox.Show(this, @"Please make sure you selected the game path inside the backup folder of pk3ds!", @"Path not valid", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

lblBackupValidator.Text = @"valid";
lblBackupValidator.ForeColor = Color.Green;
_checkBackup = true;
ChangeValidationState(lblBackupValidator, true, Pokemon.Validation.Backup);
_pathBackup = path;
if (_checkBackup && _checkRomFs) AllChecksPositive();
}

private void btnRomFs_Click(object sender, EventArgs e)
{
var path = _pathValidator.FolderSelector();
if (!_pathValidator.CheckRomFsFolder(path))
{
lblRomFsValidator.Text = @"invalid";
lblRomFsValidator.ForeColor = Color.Red;
_checkRomFs = false;
btnStart.Enabled = false;
ChangeValidationState(lblRomFsValidator, false, Pokemon.Validation.RomFs);
if (path != string.Empty) MessageBox.Show(this, @"Please make sure you selected the correct romfs path!", @"Path not valid", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

lblRomFsValidator.Text = @"valid";
lblRomFsValidator.ForeColor = Color.Green;
_checkRomFs = true;
ChangeValidationState(lblRomFsValidator, true, Pokemon.Validation.RomFs);
_pathRomFs = path;
if (_checkBackup && _checkRomFs) AllChecksPositive();
}

private void AllChecksPositive()
{
btnStart.Enabled = true;
// add other useful code here
}

private void btnStart_Click(object sender, EventArgs e)
{
// check if read
var question = MessageBox.Show(this, $@"Start the process of copying the necessary files?{Environment.NewLine}This might take a few seconds so please be patient.", @"Please choose", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
var question = MessageBox.Show(this, $@"Start the copy process now?{Environment.NewLine}This might take a few seconds so please be patient.", @"Please choose", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (question != DialogResult.OK) return;

// get files in backup folder
Expand All @@ -102,20 +84,15 @@ private void btnStart_Click(object sender, EventArgs e)
}

// get cro files
var croFiles = new List<string>();
var croFilesCheck = false;
// OR:0, AS:1, X:2, Y:3, Su:4, Mo:5
if (cboGameSelection.SelectedIndex != 4 && cboGameSelection.SelectedIndex != 5)
// X:0, Y:1, OR:2, AS:3, Su:4, Mo:5, US: 6, UM: 7
var selectedGame = (Pokemon.Version)cboGameSelection.SelectedIndex;
var fileHandlerCro = new FileHandler(_pathRomFs);
var croFiles = fileHandlerCro.GetCroFileList(selectedGame);

if (croFiles.Count < 1)
{
var fileHandlerCro = new FileHandler(_pathRomFs);
var fileListCro = fileHandlerCro.GetCroFileList();
if (fileListCro.Count < 1)
{
MessageBox.Show(this, @"Error while parsing the .cro files!", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
croFiles = fileListCro;
croFilesCheck = true;
MessageBox.Show(this, @"Error while parsing the .cro files!", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// get code.bin
Expand All @@ -130,9 +107,33 @@ private void btnStart_Click(object sender, EventArgs e)
var codeBin = fileListCodeBin.First();

// copy process
var copyResult = FileCopy.CopyAllNecessaryFiles(cboGameSelection.SelectedIndex, romFsFiles, _pathRomFs, croFilesCheck, croFiles, codeBin);
var copyResult = FileCopy.CopyAllNecessaryFiles(selectedGame, romFsFiles, _pathRomFs, croFiles, codeBin);
if (copyResult) MessageBox.Show(this, @"Finished copying all files!", @"Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
else MessageBox.Show(this, $@"Error while copying the files!{Environment.NewLine}Check error-log.txt for more information.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

// reset if successful
if (!copyResult) return;
ChangeValidationState(lblBackupValidator, false, Pokemon.Validation.Backup);
ChangeValidationState(lblRomFsValidator, false, Pokemon.Validation.RomFs);
}

private void ChangeValidationState(Label lbl, bool valid, Pokemon.Validation folder)
{
lbl.Text = valid ? "valid" : "invalid";
lbl.ForeColor = valid ? Color.Green : Color.Red;

switch (folder)
{
case Pokemon.Validation.Backup:
_checkBackup = valid;
break;
case Pokemon.Validation.RomFs:
_checkRomFs = valid;
break;
}

if (!valid) btnStart.Enabled = false;
if (_checkBackup && _checkRomFs) btnStart.Enabled = true;
}
}
}
11 changes: 6 additions & 5 deletions PokeTool/PokeTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<Compile Include="Objects\Pokemon.cs" />
<Compile Include="PokeTool.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
<Compile Include="PokeTool.Designer.cs">
<DependentUpon>PokeTool.cs</DependentUpon>
</Compile>
<Compile Include="Handler\FileCopy.cs" />
<Compile Include="Handler\FileHandler.cs" />
Expand All @@ -61,8 +62,8 @@
<Compile Include="Objects\RomFsFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<EmbeddedResource Include="PokeTool.resx">
<DependentUpon>PokeTool.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion PokeTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Run(new PokeTool());
}
}
}
4 changes: 2 additions & 2 deletions PokeTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Loading

0 comments on commit d128c8e

Please sign in to comment.