Merge branch 'AdAstra-LD:main' into main

This commit is contained in:
Miguel 2024-07-13 21:40:11 +02:00 committed by GitHub
commit fe8eff7dea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 1367 additions and 531 deletions

View File

@ -190,6 +190,7 @@
<Compile Include="LearnsetEditor.designer.cs">
<DependentUpon>LearnsetEditor.cs</DependentUpon>
</Compile>
<Compile Include="MessageEnc\EncryptText.cs" />
<Compile Include="OverlayEditor.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -7813,7 +7813,7 @@ namespace DSPRE {
private void SetupTrainerEditor() {
Helpers.DisableHandlers();
SetTrainerNameMaxLen();
//SetTrainerNameMaxLen();
SetupTrainerClassEncounterMusicTable();
/* Extract essential NARCs sub-archives*/
Helpers.statusLabelMessage("Setting up Trainer Editor...");
@ -8324,15 +8324,15 @@ namespace DSPRE {
UpdateCurrentTrainerName(newName: trainerNameTextBox.Text);
UpdateCurrentTrainerShownName();
if (trainerNameTextBox.Text.Length > RomInfo.trainerNameMaxLen - 1) { //Subtract 1 to account for special end character.
if (trainerNameTextBox.Text.Length > RomInfo.trainerNameMaxLen) { //Subtract 1 to account for special end character.
//Expose a smaller limit to the user
if (RomInfo.trainerNameLenOffset >= 0) {
MessageBox.Show($"Trainer File saved successfully. However:\nYou attempted to save a Trainer whose name exceeds {RomInfo.trainerNameMaxLen-1} characters.\nThis may lead to issues in game." +
MessageBox.Show($"Trainer File saved successfully. However:\nYou attempted to save a Trainer whose name exceeds {RomInfo.trainerNameMaxLen} characters.\nThis may lead to issues in game." +
(PatchToolboxDialog.flag_TrainerNamesExpanded ? "\n\nIt's recommended that you use a shorter name." : "\n\nRefer to the Patch Toolbox to extend Trainer names."),
"Saved successfully, but...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} else {
MessageBox.Show($"Trainer File saved successfully. However:\nThe Trainer name length could not be safely determined for this ROM.\n" +
$"You attempted to save a Trainer whose name exceeds {RomInfo.trainerNameMaxLen-1} characters.\nThis will most likely lead to issues in game.",
$"You attempted to save a Trainer whose name exceeds {RomInfo.trainerNameMaxLen} characters.\nThis will most likely lead to issues in game.",
"Saved successfully, but...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} else {

View File

@ -0,0 +1,384 @@
using DSPRE.Resources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static DSPRE.RomInfo;
namespace DSPRE.MessageEnc
{
public static class EncryptText
{
private static readonly Dictionary<int, string> GetCharDictionary = TextDatabase.readTextDictionary;
private static readonly Dictionary<int, int> WriteCharDictionary = TextDatabase.writeTextDictionary;
private static string DecodeCharacter(int textChar)
{
if (GetCharDictionary.TryGetValue(textChar, out var character))
{
return character;
}
else
{
return $"\\x{textChar:X4}";
}
}
public static string DecodeMessage(BinaryReader reader, int key, int offset, int size)
{
bool hasSpecialCharacter = false;
bool isCompressed = false;
reader.BaseStream.Position = offset;
StringBuilder decode = new StringBuilder("");
for (int j = 0; j < size; j++)
{
int textChar = (reader.ReadUInt16()) ^ key;
switch (textChar)
{
case 0xE000:
decode.Append("\\n");
break;
case 0x25BC:
decode.Append("\\r");
break;
case 0x25BD:
decode.Append("\\f");
break;
case 0xF100:
isCompressed = true;
break;
case 0xFFFE:
decode.Append("\\v");
hasSpecialCharacter = true;
break;
case 0xFFFF:
decode.Append("");
break;
default:
if (hasSpecialCharacter)
{
decode.Append(textChar.ToString("X4"));
hasSpecialCharacter = false;
} else if (isCompressed) {
int shift = 0;
int trans = 0;
while (true){
int compChar = textChar >> shift;
if (shift >= 0xF)
{
shift -= 0xF;
if (shift > 0)
{
compChar = (trans | ((textChar << (9 - shift)) & 0x1FF));
if ((compChar & 0xFF) == 0xFF)
{
break;
}
if (compChar != 0x0 && compChar != 0x1)
{
decode.Append(DecodeCharacter(compChar));
}
}
}
else
{
compChar = (textChar >> shift) & 0x1FF;
if ((compChar & 0xFF) == 0xFF)
{
break;
}
if (compChar != 0x0 && compChar != 0x1)
{
decode.Append(DecodeCharacter(compChar));
}
shift += 9;
if (shift < 0xF)
{
trans = (textChar >> shift) & 0x1FF;
shift += 9;
}
key += 0x493D;
key &= 0xFFFF;
textChar = Convert.ToUInt16(reader.ReadUInt16() ^ key);
j++;
}
}
decode.Append("");
}
else
{
decode.Append(DecodeCharacter(textChar));
}
break;
}
key += 0x493D;
key &= 0xFFFF;
}
return decode.ToString();
}
public static List<string> ReadMessageArchive(FileStream fileStream, bool discardLines)
{
int initialKey = 0;
int stringCount = 0;
List<string> messagesString = new List<string>();
bool success = false;
using (BinaryReader reader = new BinaryReader(fileStream))
{
try
{
stringCount = reader.ReadUInt16();
success = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
reader.Close();
fileStream.Close();
throw;
}
if (success)
{
try
{
initialKey = reader.ReadUInt16();
if (!discardLines)
{
int[] offsets = new int[stringCount];
int[] sizes = new int[stringCount];
int key = (initialKey * 0x2FD) & 0xFFFF;
for (int i = 0; i < stringCount; i++)
{
int key2 = (key * (i + 1) & 0xFFFF);
int actualKey = key2 | (key2 << 16);
offsets[i] = ((int)reader.ReadUInt32()) ^ actualKey;
sizes[i] = ((int)reader.ReadUInt32()) ^ actualKey;
}
for (int i = 0; i < stringCount; i++)
{
key = (0x91BD3 * (i + 1)) & 0xFFFF;
messagesString.Add(DecodeMessage(reader, key, offsets[i], sizes[i]));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
reader.Close();
fileStream.Close();
throw;
}
}
}
fileStream.Close();
return messagesString;
}
private static int[] EncodeMessage(string message, bool isTrainerName = false)
{
List<int> encoded = new List<int>();
int compressionBuffer = 0;
int bit = 0;
if (isTrainerName)
{
encoded.Add(0xF100);
}
var charArray = message.ToCharArray();
string characterId;
for (int i = 0; i < charArray.Length; i++)
{
switch (charArray[i])
{
case '\\':
switch (charArray[i + 1])
{
case 'r':
encoded.Add(0x25BC);
i++;
break;
case 'n':
encoded.Add(0xE000);
i++;
break;
case 'f':
encoded.Add(0x25BD);
i++;
break;
case 'v':
encoded.Add(0xFFFE);
characterId = $"{charArray[i + 2]}{charArray[i + 3]}{charArray[i + 4]}{charArray[i + 5]}";
encoded.Add((int)Convert.ToUInt32(characterId, 16));
i += 5;
break;
case 'x':
if (charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '0')
{
encoded.Add(0x0000);
i += 5;
break;
}
else if (charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '1')
{
encoded.Add(0x0001);
i += 5;
break;
}
else
{
characterId = $"{charArray[i + 2]}{charArray[i + 3]}{charArray[i + 4]}{charArray[i + 5]}";
encoded.Add((int)Convert.ToUInt32(characterId, 16));
i += 5;
break;
}
}
break;
case '[':
switch (charArray[i + 1])
{
case 'P':
encoded.Add(0x01E0);
i += 3;
break;
case 'M':
encoded.Add(0x01E1);
i += 3;
break;
}
break;
default:
WriteCharDictionary.TryGetValue(charArray[i], out int code);
if (isTrainerName)
{
compressionBuffer |= code << bit;
bit += 9;
if (bit >= 15)
{
bit -= 15;
encoded.Add((int)Convert.ToUInt32(compressionBuffer & 0x7FFF));
compressionBuffer >>= 15;
}
}
else
{
encoded.Add(code);
}
break;
}
}
if (isTrainerName && bit > 1)
{
compressionBuffer |= (0xFFFF << bit);
encoded.Add((int)Convert.ToUInt32(compressionBuffer & 0x7FFF));
}
encoded.Add(0xFFFF);
return encoded.ToArray();
}
private static List<int[]> EncodeMessages(List<string> messages, bool isTrainerName = false)
{
List<int[]> encoded = new List<int[]>();
foreach (var message in messages)
{
encoded.Add(EncodeMessage(message, isTrainerName));
}
return encoded;
}
private static int GetInitialKey(string filePath)
{
int initialKey = 0;
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (BinaryReader reader = new BinaryReader(fileStream))
{
try
{
reader.BaseStream.Position = 2;
initialKey = reader.ReadUInt16();
reader.Close();
fileStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
reader.Close();
fileStream.Close();
throw;
}
}
return initialKey;
}
public static bool WriteMessageArchive(int messageArchiveId, List<string> messages, bool isTrainerName = false)
{
string filePath = $"{gameDirs[DirNames.textArchives].unpackedDir}\\{messageArchiveId:D4}";
int initialKey = GetInitialKey(filePath);
var stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
try
{
List<int[]> encoded = EncodeMessages(messages, isTrainerName);
int encodedSize = encoded.Count;
writer.Write((ushort)encodedSize);
writer.Write((ushort)initialKey);
int key = (initialKey * 0x2FD) & 0xFFFF;
int key2 = 0;
int actualKey = 0;
int offset = 0x4 + (encodedSize * 8);
int[] stringLengths = new int[encodedSize];
for (int i = 0; i < encodedSize; i++)
{
key2 = (key * (i + 1) & 0xFFFF);
actualKey = key2 | (key2 << 16);
writer.Write(offset ^ actualKey);
int[] currentString = encoded[i];
int length = encoded[i].Length;
stringLengths[i] = length;
writer.Write(length ^ actualKey);
offset += length * 2;
}
for (int i = 0; i < encodedSize; i++)
{
key = (0x91BD3 * (i + 1)) & 0xFFFF;
int[] currentMessage = encoded[i];
for (int j = 0; j < stringLengths[i] - 1; j++)
{
writer.Write((ushort)(currentMessage[j] ^ key));
key += 0x493D;
key &= 0xFFFF;
}
writer.Write((ushort)(0xFFFF ^ key));
System.IO.File.WriteAllBytes(filePath, stream.ToArray());
}
}
catch (Exception ex)
{
writer.Close();
Console.WriteLine(ex.Message);
return false;
throw;
}
}
return true;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -127,7 +127,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="arm9patchCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -137,7 +137,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="BDHCamCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -147,7 +147,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="sentenceCaseCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -157,7 +157,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="itemNumbersCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -167,7 +167,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="expandedMatrixCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -177,7 +177,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="repointScrcmdCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -187,7 +187,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="dynamicHeadersPatchCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -197,7 +197,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="disableTextureAnimationsCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vgAADr4B6kKxwAAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb
@ -207,7 +207,7 @@ This patch is necessary to accomplish most ASM edits.
<data name="expandTrainerNamesCB.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vQAADr0BR/uQrQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
vAAADrwBlbxySQAAAOFJREFUOE+tj8ERgkAMRddx0Js90ItFeJS7B3vxTDO04FiCdaxJ2ISwmywjcPiT
bPLz+IQY464yh1s0Ne9LXK3rERAGkPt/hLDwPAt0WiTg7MtLYhgL3xaQZzUVsC1AD0Y7baK6AKzBaD8z
Ya0APVi4NR/xFCZ481LLhbWHF4ToxecZ2UAmD/Y4kQ+Ag3hrB7VdSjZ6dEJpvENrBrAEguIBIb4J0FIw
upmAncykgSX9hgfNYHyTqg2kaiXFN87xo5nSrQ+kXic1kuVygUkD6d58CYZ1nPWgzlMB3EvmcL1i+AEb

View File

@ -7,196 +7,96 @@ using System.Reflection;
using System.Windows.Forms;
using DSPRE.Resources;
using static DSPRE.RomInfo;
using DSPRE.MessageEnc;
namespace DSPRE.ROMFiles {
namespace DSPRE.ROMFiles
{
/// <summary>
/// Class to store message data from DS Pokémon games
/// </summary>
public class TextArchive : RomFile {
public class TextArchive : RomFile
{
#region Fields (2)
public List<string> messages;
public int initialKey;
#endregion Fields
#endregion Fields (2)
#region Constructors (1)
public TextArchive(FileStream messageStream, List<string> msg, bool discardLines = false) {
Dictionary<int, string> GetCharDictionary = TextDatabase.readTextDictionary;
BinaryReader readText = new BinaryReader(messageStream);
int stringCount;
try {
stringCount = readText.ReadUInt16();
} catch (EndOfStreamException) {
MessageBox.Show("Error loading text file.\n", "Unexpected EOF", MessageBoxButtons.OK, MessageBoxIcon.Error);
readText.Close();
return;
}
if (msg == null) {
messages = new List<string>();
} else {
messages = msg;
}
initialKey = readText.ReadUInt16();
if (!discardLines) {
int key1 = (initialKey * 0x2FD) & 0xFFFF;
bool specialCharON = false;
int[] currentOffset = new int[stringCount];
int[] currentSize = new int[stringCount];
bool compressed = new bool();
for (int i = 0; i < stringCount; i++) { // Reads and stores string offsets and sizes
int key2 = (key1 * (i + 1) & 0xFFFF);
int realKey = key2 | (key2 << 16);
currentOffset[i] = ((int)readText.ReadUInt32()) ^ realKey;
currentSize[i] = ((int)readText.ReadUInt32()) ^ realKey;
}
for (int i = 0; i < stringCount; i++) { // Adds new string
key1 = (0x91BD3 * (i + 1)) & 0xFFFF;
readText.BaseStream.Position = currentOffset[i];
StringBuilder pokemonText = new StringBuilder("");
for (int j = 0; j < currentSize[i]; j++) // Adds new characters to string
{
int car = (readText.ReadUInt16()) ^ key1;
switch (car) { // Special characters
case 0xE000:
pokemonText.Append(@"\n");
break;
case 0x25BC:
pokemonText.Append(@"\r");
break;
case 0x25BD:
pokemonText.Append(@"\f");
break;
case 0xF100:
compressed = true;
break;
case 0xFFFE:
pokemonText.Append(@"\v");
specialCharON = true;
break;
case 0xFFFF:
pokemonText.Append("");
break;
default:
if (specialCharON) {
pokemonText.Append(car.ToString("X4"));
specialCharON = false;
} else if (compressed) {
#region Compressed String
int shift = 0;
int trans = 0;
string uncomp = "";
while (true) {
int tmp = car >> shift;
int tmp1 = tmp;
if (shift >= 0xF) {
shift -= 0xF;
if (shift > 0) {
tmp1 = (trans | ((car << (9 - shift)) & 0x1FF));
if ((tmp1 & 0xFF) == 0xFF) {
break;
}
if (tmp1 != 0x0 && tmp1 != 0x1) {
string character = "";
if (!GetCharDictionary.TryGetValue(tmp1, out character)) {
pokemonText.Append(@"\x" + tmp1.ToString("X4"));
} else {
pokemonText.Append(character);
}
}
}
} else {
tmp1 = ((car >> shift) & 0x1FF);
if ((tmp1 & 0xFF) == 0xFF) {
break;
}
if (tmp1 != 0x0 && tmp1 != 0x1) {
string character = "";
if (!GetCharDictionary.TryGetValue(tmp1, out character))
pokemonText.Append(@"\x" + tmp1.ToString("X4"));
else {
pokemonText.Append(character);
}
}
shift += 9;
if (shift < 0xF) {
trans = ((car >> shift) & 0x1FF);
shift += 9;
}
key1 += 0x493D;
key1 &= 0xFFFF;
car = Convert.ToUInt16(readText.ReadUInt16() ^ key1);
j++;
}
}
#endregion
pokemonText.Append(uncomp);
} else {
if (GetCharDictionary.TryGetValue(car, out string character)) {
pokemonText.Append(character);
} else {
pokemonText.Append(@"\x" + car.ToString("X4"));
}
}
break;
}
key1 += 0x493D;
key1 &= 0xFFFF;
}
messages.Add(pokemonText.ToString());
}
}
readText.Dispose();
public TextArchive(FileStream messageStream, List<string> msg, bool discardLines = false)
{
messages = msg ?? EncryptText.ReadMessageArchive(messageStream, discardLines);
}
public TextArchive(int ID, List<string> msg = null, bool discardLines = false) : this(new FileStream(RomInfo.gameDirs[DirNames.textArchives].unpackedDir + "\\" + ID.ToString("D4"), FileMode.Open), msg, discardLines) {
public TextArchive(int ID, List<string> msg = null, bool discardLines = false) : this(new FileStream($"{gameDirs[DirNames.textArchives].unpackedDir}\\{ID:D4}", FileMode.Open), msg, discardLines)
{
}
#endregion
#endregion Constructors (1)
#region Methods (2)
public int[] EncodeString(string currentMessage, int stringIndex, int stringSize) { // Converts string to hex characters
public int[] EncodeString(string currentMessage, int stringIndex, int stringSize)
{ // Converts string to hex characters
ResourceManager GetByte = new ResourceManager("DSPRE.Resources.WriteText", Assembly.GetExecutingAssembly());
int[] pokemonMessage = new int[stringSize - 1];
var charArray = currentMessage.ToCharArray();
int count = 0;
try {
for (int i = 0; i < currentMessage.Length; i++) {
if (charArray[i] == '\\') {
if (charArray[i + 1] == 'r') {
try
{
for (int i = 0; i < currentMessage.Length; i++)
{
if (charArray[i] == '\\')
{
if (charArray[i + 1] == 'r')
{
pokemonMessage[count] = 0x25BC;
i++;
} else {
if (charArray[i + 1] == 'n') {
}
else
{
if (charArray[i + 1] == 'n')
{
pokemonMessage[count] = 0xE000;
i++;
} else {
if (charArray[i + 1] == 'f') {
}
else
{
if (charArray[i + 1] == 'f')
{
pokemonMessage[count] = 0x25BD;
i++;
} else {
if (charArray[i + 1] == 'v') {
}
else
{
if (charArray[i + 1] == 'v')
{
pokemonMessage[count] = 0xFFFE;
count++;
string characterID = ((char)charArray[i + 2]).ToString() + ((char)charArray[i + 3]).ToString() + ((char)charArray[i + 4]).ToString() + ((char)charArray[i + 5]).ToString();
pokemonMessage[count] = (int)Convert.ToUInt32(characterID, 16);
i += 5;
} else {
}
else
{
//This looks like it can be optimized
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '0') {
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '0')
{
pokemonMessage[count] = 0x0000;
i += 5;
} else {
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '1') {
}
else
{
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '1')
{
pokemonMessage[count] = 0x0001;
i += 5;
} else {
}
else
{
string characterID = ((char)charArray[i + 2]).ToString() + ((char)charArray[i + 3]).ToString() + ((char)charArray[i + 4]).ToString() + ((char)charArray[i + 5]).ToString();
pokemonMessage[count] = (int)Convert.ToUInt32(characterID, 16);
i += 5;
@ -206,56 +106,87 @@ namespace DSPRE.ROMFiles {
}
}
}
} else {
if (charArray[i] == '[') {
if (charArray[i + 1] == 'P') {
}
else
{
if (charArray[i] == '[')
{
if (charArray[i + 1] == 'P')
{
pokemonMessage[count] = 0x01E0;
i += 3;
}
if (charArray[i + 1] == 'M') {
if (charArray[i + 1] == 'M')
{
pokemonMessage[count] = 0x01E1;
i += 3;
}
} else {
}
else
{
pokemonMessage[count] = (int)Convert.ToUInt32(GetByte.GetString(((int)charArray[i]).ToString()), 16);
}
}
count++;
}
} catch (FormatException) {
}
catch (FormatException)
{
MessageBox.Show("Format exception. Assembled so far: " + Environment.NewLine + pokemonMessage);
}
return pokemonMessage;
}
public int GetStringLength(string currentMessage) { // Calculates string length
public int GetStringLength(string currentMessage)
{ // Calculates string length
int count = 0;
var charArray = currentMessage.ToCharArray();
for (int i = 0; i < currentMessage.Length; i++) {
if (charArray[i] == '\\') {
if (charArray[i + 1] == 'r') {
for (int i = 0; i < currentMessage.Length; i++)
{
if (charArray[i] == '\\')
{
if (charArray[i + 1] == 'r')
{
count++;
i++;
} else {
if (charArray[i + 1] == 'n') {
}
else
{
if (charArray[i + 1] == 'n')
{
count++;
i++;
} else {
if (charArray[i + 1] == 'f') {
}
else
{
if (charArray[i + 1] == 'f')
{
count++;
i++;
} else {
if (charArray[i + 1] == 'v') {
}
else
{
if (charArray[i + 1] == 'v')
{
count += 2;
i += 5;
} else {
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '0') {
}
else
{
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '0')
{
count++;
i += 5;
} else {
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '1') {
}
else
{
if (charArray[i + 1] == 'x' && charArray[i + 2] == '0' && charArray[i + 3] == '0' && charArray[i + 4] == '0' && charArray[i + 5] == '1')
{
count++;
i += 5;
} else {
}
else
{
count++;
i += 5;
}
@ -264,17 +195,24 @@ namespace DSPRE.ROMFiles {
}
}
}
} else {
if (charArray[i] == '[') {
if (charArray[i + 1] == 'P') {
}
else
{
if (charArray[i] == '[')
{
if (charArray[i + 1] == 'P')
{
count++;
i += 3;
}
if (charArray[i + 1] == 'M') {
if (charArray[i + 1] == 'M')
{
count++;
i += 3;
}
} else {
}
else
{
count++;
}
}
@ -282,9 +220,12 @@ namespace DSPRE.ROMFiles {
count++;
return count;
}
private byte[] ToByteArray(List<string> msgSource) {
private byte[] ToByteArray(List<string> msgSource)
{
MemoryStream newData = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(newData)) {
using (BinaryWriter writer = new BinaryWriter(newData))
{
writer.Write((ushort)msgSource.Count);
writer.Write((ushort)initialKey);
@ -294,7 +235,8 @@ namespace DSPRE.ROMFiles {
int offset = 0x4 + (msgSource.Count * 8);
int[] stringSize = new int[msgSource.Count];
for (int i = 0; i < msgSource.Count; i++) { // Reads and stores string offsets and sizes
for (int i = 0; i < msgSource.Count; i++)
{ // Reads and stores string offsets and sizes
key2 = (key * (i + 1) & 0xFFFF);
realKey = key2 | (key2 << 16);
writer.Write(offset ^ realKey);
@ -304,11 +246,13 @@ namespace DSPRE.ROMFiles {
offset += length * 2;
}
for (int i = 0; i < msgSource.Count; i++) { // Encodes strings and writes them to file
for (int i = 0; i < msgSource.Count; i++)
{ // Encodes strings and writes them to file
key = (0x91BD3 * (i + 1)) & 0xFFFF;
int[] currentString = EncodeString(msgSource[i], i, stringSize[i]);
for (int j = 0; j < stringSize[i] - 1; j++) {
for (int j = 0; j < stringSize[i] - 1; j++)
{
writer.Write((ushort)(currentString[j] ^ key));
key += 0x493D;
key &= 0xFFFF;
@ -319,19 +263,30 @@ namespace DSPRE.ROMFiles {
return newData.ToArray();
}
public override string ToString() {
public override string ToString()
{
return string.Join(Environment.NewLine, messages);
}
public override byte[] ToByteArray() {
public override byte[] ToByteArray()
{
return this.ToByteArray(messages);
}
public void SaveToFileDefaultDir(int IDtoReplace, bool showSuccessMessage = true) {
SaveToFileDefaultDir(DirNames.textArchives, IDtoReplace, showSuccessMessage);
public void SaveToFileDefaultDir(int IDtoReplace, bool showSuccessMessage = true)
{
bool success = EncryptText.WriteMessageArchive(IDtoReplace, messages, IDtoReplace == trainerNamesMessageNumber);
if (showSuccessMessage && success)
{
MessageBox.Show("Saved successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public void SaveToFileExplorePath(string suggestedFileName, bool showSuccessMessage = true) {
public void SaveToFileExplorePath(string suggestedFileName, bool showSuccessMessage = true)
{
SaveToFileExplorePath("Gen IV Text Archive", "msg", suggestedFileName, showSuccessMessage);
}
#endregion
#endregion Methods (2)
}
}

View File

@ -321,7 +321,7 @@ namespace DSPRE.ROMFiles {
}
}
public class TrainerFile : RomFile {
public const int defaultNameLen = 7; //Does not include special \0 end character!
public const int defaultNameLen = 10; //Does not include special \0 end character!
public const int POKE_IN_PARTY = 6;
public static readonly string NAME_NOT_FOUND = "NAME READ ERROR";

View File

@ -678,7 +678,9 @@ namespace DSPRE.Resources {
[0x02C1] = "ShowSaveBox",
[0x02C2] = "HideSaveBox",
[0x02C3] = "ScopeMode"
[0x02C3] = "ScopeMode",
[0x0333] = "SetFieldVolume",
};
public static Dictionary<ushort, byte[]> DPPtScrCmdParameters = new Dictionary<ushort, byte[]>() {
[0x0000] = new byte[1] { 0 },
@ -1024,14 +1026,14 @@ namespace DSPRE.Resources {
[0x0154] = new byte[1] { 0 },
[0x0155] = new byte[] { 2, 2 },
[0x0156] = new byte[] { 2 },
[0x0157] = new byte[1] { 0 },
[0x0157] = new byte[] { 2 },
[0x0158] = new byte[1] { 0 },
[0x0159] = new byte[1] { 0 },
[0x0159] = new byte[] { 2 },
[0x015A] = new byte[1] { 0 },
[0x015B] = new byte[] { 2, 2 },
[0x015C] = new byte[] { 2 },
[0x015D] = new byte[] { 2 },
[0x015E] = new byte[1] { 0 },
[0x015E] = new byte[] { 2 },
[0x015F] = new byte[1] { 0 },
[0x0160] = new byte[] { 2 },
[0x0161] = new byte[1] { 0 },
@ -1060,12 +1062,12 @@ namespace DSPRE.Resources {
[0x0178] = new byte[] { 1 },
[0x0179] = new byte[] { 2 },
[0x017A] = new byte[] { 2, 2 },
[0x017B] = new byte[] { 2 },
[0x017B] = new byte[] { 1, 2, 2 },
[0x017C] = new byte[] { 1, 2 },
[0x017D] = new byte[] { 2 },
[0x017E] = new byte[] { 2 },
[0x017F] = new byte[] { 2 },
[0x0180] = new byte[] { 1 },
[0x0180] = new byte[] { 2 },
[0x0181] = new byte[] { 2 },
[0x0182] = new byte[] { 2 },
[0x0183] = new byte[] { 2 },
@ -1494,8 +1496,11 @@ namespace DSPRE.Resources {
[0x0302] = "CheckUsedRotomAppliances",
[0x0314] = "GetBattleOutcome",
[0x0315] = "GetCurrentWeather",
[0x0317] = "GetPlayerPositionFull",
[0x0318] = "WildBattleFateful",
[0x0319] = "GiratinaBattle",
[0x031A] = "RegisterSeenPokemon",
@ -1635,7 +1640,7 @@ namespace DSPRE.Resources {
[0x0331] = new byte[1] { 0 },
[0x0332] = new byte[1] { 0 },
[0x0333] = new byte[] { 2 },
[0x0334] = new byte[1] { 0 },
[0x0334] = new byte[] { 2, 2 },
[0x0335] = new byte[] { 2, 4 },
[0x0336] = new byte[] { 2 },
@ -2088,7 +2093,14 @@ namespace DSPRE.Resources {
[0x0290] = "CheckHeadbuttCompatibility",
[0x02AB] = "GetBattleOutcome",
[0x02AC] = "GetCurrentWeather",
[0x02AD] = "GetPlayerPositionFull",
[0x02AE] = "WildBattleFateful",
[0x02AF] = "RegisterSeenPokemon",
[0x02BF] = "SetFieldVolume",
[0x02C4] = "FloorTrapAnimation",
@ -2850,8 +2862,8 @@ namespace DSPRE.Resources {
[0x02AA] = new byte[] { 2 },
[0x02AB] = new byte[] { 2 },
[0x02AC] = new byte[] { 2 },
[0x02AD] = new byte[] { 2, 2 },
[0x02AE] = new byte[1] { 0 },
[0x02AD] = new byte[] { 2, 2, 2 },
[0x02AE] = new byte[] { 2, 2 },
[0x02AF] = new byte[] { 2 },
[0x02B0] = new byte[] { 2 },
[0x02B1] = new byte[] { 2 },
@ -2868,7 +2880,7 @@ namespace DSPRE.Resources {
[0x02BC] = new byte[] { 2 },
[0x02BD] = new byte[] { 2, 2, 2, 2 },
[0x02BE] = new byte[1] { 0 },
[0x02BF] = new byte[1] { 0 },
[0x02BF] = new byte[] { 2 },
[0x02C0] = new byte[] { 2 },
[0x02C1] = new byte[1] { 0 },
[0x02C2] = new byte[] { 2 },

File diff suppressed because it is too large Load Diff