Skip to content

Commit

Permalink
Updated text editor to work with expanded archive pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian committed Sep 2, 2019
1 parent 30b403c commit 1d8774a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 70 deletions.
12 changes: 8 additions & 4 deletions TH Hack Toolkit/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,18 @@ private void Button_CharacterEditor_Click(object sender, EventArgs e)

private void Button_ClassEditor_Click(object sender, EventArgs e)
{
Class_Editor editor = new Class_Editor();
editor.ShowDialog();
using (Class_Editor editor = new Class_Editor())
{
editor.ShowDialog();
}
}

private void Button_ItemEditor_Click(object sender, EventArgs e)
{
editors.Text_Editor editor = new editors.Text_Editor();
editor.ShowDialog();
using (editors.Text_Editor editor = new editors.Text_Editor())
{
editor.ShowDialog();
}
}
}
}
2 changes: 1 addition & 1 deletion TH Hack Toolkit/THForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public string encode_msgcode(string input)
string new_string = Encoding.UTF8.GetString(utf);

new_string = new_string.Replace("\r\n", "\n");
new_string = new Regex("<C([0-9])>(.*?)<\\/C[0-9]>").Replace(new_string, "\x001bC$1$C2\x001bR");
new_string = new Regex("<C([0-9])>(.*?)<\\/C[0-9]>").Replace(new_string, "\x001bC$1$2\x001bR");
new_string = new Regex("<ismale character=\"([0-9]{2})\">(.*?)<else/>(.*?)</ismale>").Replace(new_string, "\x001bEK$1$2\x001bEL$1$3\x001bEM$1");
new_string = new Regex("<string id=\"([0-9])\" />").Replace(new_string, "\x001bS$1");
new_string = new Regex("\\%").Replace(new_string, "\x001b%");
Expand Down
10 changes: 5 additions & 5 deletions TH Hack Toolkit/editors/Text_Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void Button_SaveArchive_Click(object sender, EventArgs e)

foreach(ListViewItem item in List_Strings.Items)
{
new_contents.Add(item.SubItems[0].Text);
new_contents.Add(item.SubItems[1].Text);
}

TextTable text_table = (TextTable)current_controller.Tables[current_table];
Expand All @@ -343,8 +343,8 @@ private void List_Strings_ItemActivate(object sender, EventArgs e)
archive_box = new Text_Edit_ArchiveT1(active_string);
}
else {
int character = text_table.get_args1(current_archive, index);
int voice = text_table.get_args2(current_archive, index);
int character = text_table.get_args(current_archive, index,1);
int voice = text_table.get_args(current_archive, index,2);
archive_box = new Text_Edit_ArchiveT2(active_string,character,voice);
}

Expand All @@ -358,8 +358,8 @@ private void List_Strings_ItemActivate(object sender, EventArgs e)
archive_strings[index] = data.ArchiveText;

text_table.set_contents(current_archive, archive_strings);
text_table.set_arg1(current_archive, data.ArchiveArg1, index);
text_table.set_arg2(current_archive, data.ArchiveArg1, index);
text_table.set_args(current_archive, index, data.ArchiveArg1, 1);
text_table.set_args(current_archive, index, data.ArchiveArg2, 2);

current_controller.Tables[current_table] = text_table;
item.SubItems[1].Text = data.ArchiveText;
Expand Down
97 changes: 37 additions & 60 deletions TH Hack Toolkit/tools/th_tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,30 +234,18 @@ public short get_type(int archive)
return archives[archive].type;
}

public int get_args1(int archive, int string_index)
public int get_args(int archive, int string_index, int arg)
{
if (get_type(archive) == 2)
return archives[archive].args1[string_index];
return archives[archive].get_pointer_args(string_index,arg);
else
return -1;
}

public int get_args2(int archive, int string_index)
public void set_args(int archive, int index, int value, int arg)
{
if (get_type(archive) == 2)
return archives[archive].args2[string_index];
else
return -1;
}

public void set_arg1(int archive, int arg, int index)
{
archives[archive].args1[index] = arg;
}

public void set_arg2(int archive, int arg, int index)
{
archives[archive].args2[index] = arg;
if (archives[archive].type == 2)
archives[archive].set_pointer_args(index, value, arg);
}

public List<string> get_contents(int archive)
Expand Down Expand Up @@ -330,50 +318,37 @@ public class Text_Archive
{

byte[] header;
public List<uint> pointers = new List<uint>();
public List<byte[]> pointers = new List<byte[]>();
public short type = 1;

public List<int> args1 = new List<int>(); // only if type=2
public List<int> args2 = new List<int>(); // only if type=2

public Text_Archive(byte[] bin)
{
int header_size = BitConverter.ToInt16(bin, 12);
int pointer_size = BitConverter.ToInt16(bin, 10);
ushort header_size = BitConverter.ToUInt16(bin, 12);
ushort pointer_size = BitConverter.ToUInt16(bin, 10);

if (header_size >= 24)
type = 2;

header = bin.Take(header_size).ToArray();

int pointer_count = BitConverter.ToInt16(header, 8);
ushort pointer_count = BitConverter.ToUInt16(header, 8);
int pointer_end = pointer_count * pointer_size + header.Length;

for (int i = header.Length; i < pointer_end; i += pointer_size)
{
uint pointer = BitConverter.ToUInt32(bin, i);
byte[] pointer = new byte[pointer_size];
Array.Copy(bin,i,pointer,0,pointer_size);
pointers.Add(pointer);

if (type == 2)
{
int character = BitConverter.ToInt32(bin, i+4);
args1.Add(character);

int voice = BitConverter.ToInt32(bin, i + 8);
args2.Add(voice);

}

}

}

public List<string> load_contents(byte[] bin)
{
List<string> strings = new List<string>();
foreach (uint pointer in pointers)
foreach (byte[] pointer in pointers)
{
uint i = pointer + (uint)header.Length;
uint i = BitConverter.ToUInt32(pointer,0) + (uint)header.Length;
List<byte> current_string = new List<byte>();
while (i < bin.Length)
{
Expand All @@ -388,13 +363,22 @@ public List<string> load_contents(byte[] bin)
return strings;
}

public int get_pointer_args(int index, int arg)
{
return BitConverter.ToInt32(pointers[index], arg*4);
}

public void set_pointer_args(int index, int value, int arg)
{
Array.Copy(BitConverter.GetBytes(value), 0, pointers[index], arg * 4, 4);
}

public byte[] Write(List<string> contents)
{
List<uint> new_pointers = new List<uint>();
List<byte> new_contents = new List<byte>();
List<byte> pointers_raw = new List<byte>();

int pointer_size = BitConverter.ToInt16(header, 10);
ushort pointer_size = BitConverter.ToUInt16(header, 10);

int pos = contents.Count * pointer_size;
foreach (string s in contents)
Expand All @@ -410,41 +394,34 @@ public byte[] Write(List<string> contents)
byte[] new_header = header;

ushort pointer_count = (ushort)new_pointers.Count;
int pointer_end = pointer_count * pointer_size + header.Length;
ushort pointer_end = (ushort)(pointer_count * pointer_size + header.Length);

Array.Copy(BitConverter.GetBytes(pointer_count), 0, new_header, 8, 2);

foreach (int pointer in new_pointers)
List<byte> newbin = new_header.ToList();

for (int i=0; i < pointer_count; i++)
{
pointers_raw.AddRange(BitConverter.GetBytes(pointer));
Array.Copy(BitConverter.GetBytes(new_pointers[i]), 0, pointers[i], 0, 4);
newbin.AddRange(pointers[i]);
}

byte[] newbin = new_header.Concat(pointers_raw).Concat(new_contents).ToArray();
newbin.AddRange(new_contents);

int total_length = newbin.Length;
int total_length = newbin.Count;

while (total_length > ushort.MaxValue)
{
total_length -= ushort.MaxValue + 1;
}

Array.Copy(BitConverter.GetBytes((ushort)total_length), 0, newbin, 4, 2);
byte[] newbin_array = newbin.ToArray();

if (type == 2)
{
int current = 0;
for (int i = new_header.Length; i < pointer_end; i += pointer_size)
{
Array.Copy(BitConverter.GetBytes(args1[current]), 0, newbin, i + 4, 4);
Array.Copy(BitConverter.GetBytes(args2[current]), 0, newbin, i + 8, 4);
current++;
}
}

header = newbin.Take(header.Length).ToArray();
pointers = new_pointers;
Array.Copy(BitConverter.GetBytes((ushort)total_length), 0, newbin_array, 4, 2);

header = newbin_array.Take(header.Length).ToArray();

return newbin;
return newbin_array;
}

}
Expand Down

0 comments on commit 1d8774a

Please sign in to comment.