mirror of
https://github.com/Hunter-Raff/e-ReaderCardCreator.git
synced 2026-08-29 16:36:13 +01:00
Add project files.
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using AC_e_Reader_Card_Creator.References;
|
||||
|
||||
namespace AC_e_Reader_Card_Creator.Decompression.Functions
|
||||
{
|
||||
internal class Compress
|
||||
{
|
||||
public static void DECtoVPK(string greeting, string body, string closing, string stationery, string sender, string gift)
|
||||
{
|
||||
string new_greeting = greeting;
|
||||
int player_index = greeting.IndexOf("<Player>");
|
||||
|
||||
if(player_index != -1)
|
||||
{
|
||||
new_greeting = greeting.Substring(0, player_index) + greeting.Substring(player_index + 8);
|
||||
}
|
||||
|
||||
new_greeting = ACAsciiToBytes(new_greeting.PadRight(24, ' '));
|
||||
|
||||
string new_body = body.Replace('\r'.ToString(), "").PadRight(192, ' ');
|
||||
new_body = ACAsciiToBytes(new_body);
|
||||
|
||||
// should only be true if letter uses special characters
|
||||
if(new_body.Length < 384)
|
||||
{
|
||||
while(new_body.Length < 384)
|
||||
{
|
||||
new_body += "20";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// failsafe -- if programmed correctly, this should never run
|
||||
new_body = new_body.Substring(0, 384);
|
||||
}
|
||||
|
||||
string new_closing = closing.PadRight(31, ' ');
|
||||
new_closing = ACAsciiToBytes(new_closing);
|
||||
|
||||
Dictionary<string, string> StationeryMappings = ReadMappings(Common.STATIONERY_LIST);
|
||||
string new_stationery = StationeryMappings[stationery].Substring(2);
|
||||
|
||||
Dictionary<string, string> SenderMappings = ReadMappings(Common.SENDER_LIST);
|
||||
string new_sender = SenderMappings[sender].Substring(2);
|
||||
|
||||
string final = player_index.ToString("X2") + Random2ByteHex();
|
||||
|
||||
string bytesToWrite = new_greeting + new_body + new_closing + new_stationery + gift + new_sender + final;
|
||||
|
||||
byte[] body_bytes = HexStringToByteArray(bytesToWrite);
|
||||
string filePath = Common.DECOMPRESSED_GCN;
|
||||
File.WriteAllBytes(filePath, body_bytes);
|
||||
|
||||
ProcessStartInfo dec_to_vpk = new ProcessStartInfo
|
||||
{
|
||||
FileName = Common.NEVPK,
|
||||
Arguments = Common.NEVPK_ARGS_COMP(Common.DECOMPRESSED_GCN),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
using (Process process = Process.Start(dec_to_vpk)) { process.WaitForExit(); }
|
||||
|
||||
FixVPKChecksums();
|
||||
}
|
||||
|
||||
private static string Random2ByteHex()
|
||||
{
|
||||
Random random = new Random();
|
||||
int randomNumber = random.Next(0, 130);
|
||||
return randomNumber.ToString("X4");
|
||||
}
|
||||
|
||||
public static void VPKtoBIN()
|
||||
{
|
||||
if (File.Exists(Common.COMPRESSED_BIN))
|
||||
{
|
||||
File.Delete(Common.COMPRESSED_BIN);
|
||||
}
|
||||
|
||||
string[] VPKfiles = { Common.VPK_HEADER, Common.VPK_GBA, Common.VPK_GCN };
|
||||
|
||||
using (FileStream NewBIN = new FileStream(Common.COMPRESSED_BIN, FileMode.CreateNew))
|
||||
{
|
||||
foreach (var sourceFile in VPKfiles)
|
||||
{
|
||||
byte[] fileContent = File.ReadAllBytes(sourceFile);
|
||||
NewBIN.Write(fileContent, 0, fileContent.Length);
|
||||
}
|
||||
|
||||
long currentBINSize = NewBIN.Length;
|
||||
long paddingSize = 2112 - currentBINSize;
|
||||
|
||||
if (paddingSize > 0)
|
||||
{
|
||||
NewBIN.Seek(0, SeekOrigin.End);
|
||||
|
||||
// creating a padding buffer of 0x00 bytes to fill 2112 bytes
|
||||
byte[] padding = new byte[paddingSize];
|
||||
NewBIN.Write(padding, 0, padding.Length);
|
||||
}
|
||||
}
|
||||
|
||||
ProcessStartInfo fix_header_checksums = new ProcessStartInfo(Common.HEADERFIX, $"\"{Common.COMPRESSED_BIN}\"")
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
using (Process process = Process.Start(fix_header_checksums)) { process.WaitForExit(); }
|
||||
}
|
||||
|
||||
public static void BINtoRAW(bool custom)
|
||||
{
|
||||
if (custom)
|
||||
{
|
||||
SaveFileDialog saveRAWFile = new SaveFileDialog
|
||||
{
|
||||
Filter = "RAW files (*.raw)|*.raw",
|
||||
Title = "Save .RAW",
|
||||
FileName = "My-Custom-eCard.raw"
|
||||
};
|
||||
if (saveRAWFile.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
ProcessStartInfo bin_to_raw = new ProcessStartInfo
|
||||
{
|
||||
FileName = Common.NEDCENC,
|
||||
Arguments = Common.NEDCENC_ARGS_COMP(saveRAWFile.FileName),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
using (Process process = Process.Start(bin_to_raw)) { process.WaitForExit(); }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ProcessStartInfo bin_to_raw = new ProcessStartInfo
|
||||
{
|
||||
FileName = Common.NEDCENC,
|
||||
Arguments = Common.NEDCENC_ARGS_COMP(Common.RAW_ECARD),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
using (Process process = Process.Start(bin_to_raw)) { process.WaitForExit(); }
|
||||
}
|
||||
}
|
||||
|
||||
static Dictionary<string, string> ReadMappings(string filePath)
|
||||
{
|
||||
var map = new Dictionary<string, string>();
|
||||
foreach (var line in File.ReadAllLines(filePath))
|
||||
{
|
||||
var parts = line.Split(',');
|
||||
if (parts.Length >= 2)
|
||||
{
|
||||
string hexKey = parts[0].Trim();
|
||||
string value = parts[1].Trim().Trim('\'');
|
||||
map[value] = hexKey;
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void FixVPKChecksums()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(Common.VPK_GCN);
|
||||
long GCN_VPK_Length = 0;
|
||||
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
GCN_VPK_Length = fileInfo.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("ERROR: Failed to fetch GCN VPK");
|
||||
return;
|
||||
}
|
||||
|
||||
string GCN_VPK_Hex_Length = GCN_VPK_Length.ToString("X").PadLeft(4, '0');
|
||||
|
||||
if (!File.Exists(Common.VPK_GBA))
|
||||
{
|
||||
MessageBox.Show("ERROR: Failed to fetch GBA VPK");
|
||||
return;
|
||||
}
|
||||
byte[] fileContents = File.ReadAllBytes(Common.VPK_GBA);
|
||||
|
||||
if (fileContents.Length < 2)
|
||||
{
|
||||
MessageBox.Show("ERROR: GBA VPK exists, but is smaller in size than expected");
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] GCN_FileSize_Checksum = HexStringToByteArray(GCN_VPK_Hex_Length);
|
||||
|
||||
fileContents[fileContents.Length - 2] = GCN_FileSize_Checksum[1];
|
||||
fileContents[fileContents.Length - 1] = GCN_FileSize_Checksum[0];
|
||||
|
||||
File.WriteAllBytes(Common.VPK_GBA, fileContents);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"An error occurred: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string ACAsciiToBytes(string input)
|
||||
{
|
||||
Dictionary<string, string> map = ReadMappings(Common.SP_CHAR_LIST);
|
||||
var sb = new StringBuilder();
|
||||
var textElementEnumerator = StringInfo.GetTextElementEnumerator(input);
|
||||
|
||||
// a bit hacky and inefficient, but should work
|
||||
while (textElementEnumerator.MoveNext())
|
||||
{
|
||||
string textElement = textElementEnumerator.GetTextElement();
|
||||
if (textElement == "\n")
|
||||
{
|
||||
sb.Append("CD");
|
||||
}
|
||||
else if (textElement.Length == 1)
|
||||
{
|
||||
if (map.ContainsKey(textElement))
|
||||
{
|
||||
sb.Append(map[textElement].Substring(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
char c = textElement[0];
|
||||
if ((byte)c >= 32 && (byte)c <= 127)
|
||||
{
|
||||
byte char_byte = (byte)c;
|
||||
sb.Append(char_byte.ToString("X2"));
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("An unexpected error occurred!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (map.ContainsKey(textElement))
|
||||
{
|
||||
sb.Append(map[textElement].Substring(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Test!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
|
||||
static byte[] HexStringToByteArray(string hex)
|
||||
{
|
||||
int numberChars = hex.Length;
|
||||
byte[] bytes = new byte[numberChars / 2];
|
||||
for (int i = 0; i < numberChars; i += 2)
|
||||
{
|
||||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using AC_e_Reader_Card_Creator.References;
|
||||
|
||||
namespace AC_e_Reader_Card_Creator.Decompression.Functions
|
||||
{
|
||||
internal class Decompress
|
||||
{
|
||||
public static void BINtoVPK(string filePath, byte[] delimiter, string outputDirectory)
|
||||
{
|
||||
List<byte> buffer = new List<byte>();
|
||||
int chunkNumber = 1;
|
||||
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||
using (BinaryReader reader = new BinaryReader(fs))
|
||||
{
|
||||
while (reader.BaseStream.Position != reader.BaseStream.Length)
|
||||
{
|
||||
byte b = reader.ReadByte();
|
||||
buffer.Add(b);
|
||||
|
||||
if (buffer.Count >= delimiter.Length &&
|
||||
buffer.Skip(buffer.Count - delimiter.Length).SequenceEqual(delimiter))
|
||||
{
|
||||
WriteToFile(buffer.Take(buffer.Count - delimiter.Length).ToArray(), ref chunkNumber, outputDirectory, delimiter);
|
||||
buffer.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer.Count > 0)
|
||||
{
|
||||
WriteToFile(buffer.ToArray(), ref chunkNumber, outputDirectory, delimiter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RAWtoBIN(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo raw_to_bin = new ProcessStartInfo
|
||||
{
|
||||
FileName = Common.NEDCENC,
|
||||
Arguments = Common.NEDCENCE_ARGS_DECOMP(filePath),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
using (Process process = Process.Start(raw_to_bin)) { process.WaitForExit(); }
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Error processing file {filePath}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void VPK_Decompress()
|
||||
{
|
||||
string folderPath = Common.VPK_OUTPUT;
|
||||
string[] vpk_file_paths = Directory.GetFiles(folderPath);
|
||||
|
||||
int iteration = 1;
|
||||
|
||||
foreach (string vpk_file in vpk_file_paths)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (iteration == 1)
|
||||
{
|
||||
iteration++;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
ProcessStartInfo vpk_to_dec = new ProcessStartInfo
|
||||
{
|
||||
FileName = Common.NEVPK,
|
||||
Arguments = Common.NEVPK_ARGS_DECOMP(vpk_file, iteration),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
using (Process process = Process.Start(vpk_to_dec)) { process.WaitForExit(); }
|
||||
|
||||
iteration++;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Error processing file {vpk_file}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteToFile(byte[] data, ref int chunkNumber, string outputDirectory, byte[] delimiter)
|
||||
{
|
||||
string fileName = "";
|
||||
|
||||
switch (chunkNumber)
|
||||
{
|
||||
case 1:
|
||||
fileName = Path.Combine(outputDirectory, $"1_VPK_Header.vpk");
|
||||
break;
|
||||
case 2:
|
||||
fileName = Path.Combine(outputDirectory, $"2_VPK_GBA.vpk");
|
||||
break;
|
||||
case 3:
|
||||
fileName = Path.Combine(outputDirectory, $"3_VPK_GCN.vpk");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
using (FileStream outFile = new FileStream(fileName, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
if (chunkNumber != 1)
|
||||
{
|
||||
outFile.Write(delimiter, 0, delimiter.Length);
|
||||
outFile.Write(data, 0, data.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
outFile.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
chunkNumber++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using AC_e_Reader_Card_Creator.References;
|
||||
|
||||
namespace AC_e_Reader_Card_Creator.Decompression.Functions
|
||||
{
|
||||
internal class Decompressed
|
||||
{
|
||||
public static Dictionary<string, string> GetData()
|
||||
{
|
||||
byte[] GCN_Letter_Bytes = { };
|
||||
Dictionary<string, string> letterData = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
GCN_Letter_Bytes = File.ReadAllBytes(Common.GCN_LETTER_DATA);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Console.WriteLine($"An error occurred while reading the file: {ex.Message}");
|
||||
}
|
||||
|
||||
string letter_contents = ConvertBytesToACAscii(GCN_Letter_Bytes);
|
||||
string full_greeting = GetGreeting(letter_contents);
|
||||
|
||||
// bunch of hardcoding with the exact byte positions - source these out to variables
|
||||
int player_index = Convert.ToInt32("0x" + GCN_Letter_Bytes[253].ToString("X2"), 16) - 1;
|
||||
string letter_greeting = InsertPlayerVar(full_greeting, player_index).Trim();
|
||||
letterData.Add("letter_greeting", letter_greeting);
|
||||
|
||||
string letter_body = GetStringInRange(letter_contents, 24, 207);
|
||||
letterData.Add("letter_body", letter_body);
|
||||
|
||||
string letter_closing = GetStringInRange(letter_contents, 216, 246).Trim();
|
||||
letterData.Add("letter_closing", letter_closing);
|
||||
|
||||
byte[] stationeryID_bytes = { GCN_Letter_Bytes[247], GCN_Letter_Bytes[248] };
|
||||
string stationeryID = BytesToHex(stationeryID_bytes).Replace(" ", "");
|
||||
letterData.Add("letter_stationery", stationeryID);
|
||||
|
||||
byte[] giftID_bytes = { GCN_Letter_Bytes[249], GCN_Letter_Bytes[250] };
|
||||
string giftID = BytesToHex(giftID_bytes).Replace(" ", "");
|
||||
letterData.Add("letter_gift", giftID);
|
||||
|
||||
byte[] senderID_bytes = { GCN_Letter_Bytes[251], GCN_Letter_Bytes[252] };
|
||||
string senderID = BytesToHex(senderID_bytes).Replace(" ","");
|
||||
letterData.Add("letter_sender", senderID);
|
||||
|
||||
return letterData;
|
||||
}
|
||||
|
||||
public static string ConvertBytesToACAscii(byte[] byteArray)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
foreach (byte b in byteArray)
|
||||
{
|
||||
// handles newline
|
||||
if (b == 0xCD)
|
||||
{
|
||||
result.Append("\n");
|
||||
}
|
||||
// handles standard ASCII conversion
|
||||
else if (b >= 32 && b <= 127)
|
||||
{
|
||||
result.Append((char)b);
|
||||
}
|
||||
// handles AC's special characters; standard ASCII char conversion is faster,
|
||||
// so only lookup special characters instead of doing this for every character
|
||||
else if (b < 32 || b > 127)
|
||||
{
|
||||
string special_char = "0x" + b.ToString("X2");
|
||||
special_char = Common.LookupListValue(special_char, Common.SP_CHAR_LIST);
|
||||
result.Append(special_char);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Append("-");
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
public static int PlayerVarFinder(string greeting)
|
||||
{
|
||||
for (int i = greeting.Length - 2; i > 0; i--)
|
||||
{
|
||||
if (greeting[i] == ' ' && greeting[i + 1] != ' ')
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static string GetGreeting(string letter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(letter) || letter.Length < 24)
|
||||
{
|
||||
return letter;
|
||||
}
|
||||
|
||||
return letter.Substring(0, 24);
|
||||
}
|
||||
|
||||
public static string GetStringInRange(string letter, int startIndex, int endIndex)
|
||||
{
|
||||
|
||||
if (string.IsNullOrEmpty(letter))
|
||||
{
|
||||
throw new ArgumentException("The letter is null or empty.");
|
||||
}
|
||||
|
||||
if (startIndex < 0 || endIndex >= letter.Length || startIndex > endIndex)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Start or end index is out of range.");
|
||||
}
|
||||
|
||||
int length = endIndex - startIndex + 1;
|
||||
return letter.Substring(startIndex, length);
|
||||
}
|
||||
|
||||
public static string InsertPlayerVar(string greeting, int index)
|
||||
{
|
||||
if (index == 0 || index < 0 || index > greeting.Length)
|
||||
{
|
||||
string revisedGreeting = greeting.Insert(0, "<Player>");
|
||||
return revisedGreeting;
|
||||
}
|
||||
|
||||
return greeting.Substring(0, index) + " <Player>" + greeting.Substring(index + 1);
|
||||
}
|
||||
|
||||
public static string BytesToHex(byte[] byteArray)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
foreach (byte b in byteArray)
|
||||
{
|
||||
result.AppendFormat("{0:X2} ", b);
|
||||
}
|
||||
|
||||
return result.ToString().TrimEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user