Fix GameConfig's Initialize methods

TextFile no longer holds a static reference to GameConfig
This commit is contained in:
Evan Dixon
2017-05-29 08:52:53 -05:00
parent 7f20c9c370
commit 919b271d33
5 changed files with 51 additions and 38 deletions

1
.gitignore vendored
View File

@@ -229,3 +229,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk
/.vs

View File

@@ -120,7 +120,7 @@ public void InitializeLearnset()
public void InitializeGameText()
{
GARCGameText = getGARCData("gametext");
GameTextStrings = GARCGameText.Files.Select(file => new TextFile(file).Lines).ToArray();
GameTextStrings = GARCGameText.Files.Select(file => new TextFile(this, file).Lines).ToArray();
}
public void InitializeMoves()
{
@@ -177,7 +177,17 @@ private GARC.lzGARC getlzGARC(string file)
public GARCReference getGARCReference(string name) { return Files.FirstOrDefault(f => f.Name == name); }
public TextVariableCode getVariableCode(string name) { return Variables.FirstOrDefault(v => v.Name == name); }
public TextVariableCode getVariableName(int value) { return Variables.FirstOrDefault(v => v.Code == value); }
public TextReference getGameText(TextName name) { return GameText.FirstOrDefault(f => f.Name == name); }
private TextReference getGameText(TextName name) { return GameText.FirstOrDefault(f => f.Name == name); }
public string[] getText(TextName file)
{
return (string[])GameTextStrings[getGameText(file).Index].Clone();
}
public bool setText(TextName file, string[] strings)
{
GameTextStrings[getGameText(file).Index] = strings;
return true;
}
public string getGARCFileName(string requestedGARC)
{

View File

@@ -19,7 +19,7 @@ public class TextFile
private const bool SETEMPTYTEXT = false;
private static readonly byte[] emptyTextFile = { 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00 };
public TextFile(byte[] data = null)
public TextFile(GameConfig config, byte[] data = null)
{
Data = (byte[])(data ?? emptyTextFile).Clone();
@@ -29,7 +29,10 @@ public TextFile(byte[] data = null)
throw new Exception("Invalid Text File");
if (SectionLength != TotalLength)
throw new Exception("Section size and overall size do not match.");
Config = config;
}
private GameConfig Config { get; set; }
private ushort TextSections { get { return BitConverter.ToUInt16(Data, 0x0); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x0); } } // Always 0x0001
private ushort LineCount { get { return BitConverter.ToUInt16(Data, 0x2); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x2); } }
private uint TotalLength { get { return BitConverter.ToUInt32(Data, 0x4); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x4); } }
@@ -79,7 +82,7 @@ public string[] Lines
byte[] EncryptedLineData = new byte[lines[i].Length * 2];
Array.Copy(Data, lines[i].Offset, EncryptedLineData, 0, EncryptedLineData.Length);
byte[] DecryptedLineData = cryptLineData(EncryptedLineData, key);
result[i] = getLineString(DecryptedLineData);
result[i] = getLineString(Config, DecryptedLineData);
key += KEY_ADVANCE;
}
return result;
@@ -101,7 +104,7 @@ public string[] Lines
string text = (value[i] ?? "").Trim();
if (text.Length == 0 && SETEMPTYTEXT)
text = $"[~ {i}]";
byte[] DecryptedLineData = getLineData(text);
byte[] DecryptedLineData = getLineData(Config, text);
lineData[i] = cryptLineData(DecryptedLineData, key);
if (lineData[i].Length % 4 == 2)
Array.Resize(ref lineData[i], lineData[i].Length + 2);
@@ -120,7 +123,7 @@ public string[] Lines
}
public byte[] Data;
private static byte[] cryptLineData(byte[] data, ushort key)
private byte[] cryptLineData(byte[] data, ushort key)
{
byte[] result = new byte[data.Length];
for (int i = 0; i < result.Length; i += 2)
@@ -130,7 +133,7 @@ private static byte[] cryptLineData(byte[] data, ushort key)
}
return result;
}
private static byte[] getLineData(string line)
private byte[] getLineData(GameConfig config, string line)
{
if (line == null)
return new byte[2];
@@ -155,7 +158,7 @@ private static byte[] getLineData(string line)
if (bracket < 0)
throw new ArgumentException("Variable text is not capped properly.");
string varText = line.Substring(i, bracket - i);
var varValues = getVariableValues(varText);
var varValues = getVariableValues(config, varText);
foreach (ushort v in varValues) bw.Write(v);
i += 1 + varText.Length;
}
@@ -171,7 +174,7 @@ private static byte[] getLineData(string line)
return ms.ToArray();
}
}
private static string getLineString(byte[] data)
private string getLineString(GameConfig config, byte[] data)
{
if (data == null)
return null;
@@ -187,7 +190,7 @@ private static string getLineString(byte[] data)
switch (val)
{
case KEY_TERMINATOR: return s;
case KEY_VARIABLE: s += getVariableString(data, ref i); break;
case KEY_VARIABLE: s += getVariableString(config, data, ref i); break;
case '\n': s += @"\n"; break;
case '\\': s += @"\\"; break;
case '[': s += @"\["; break;
@@ -200,7 +203,7 @@ private static string getLineString(byte[] data)
}
return s; // Shouldn't get hit if the string is properly terminated.
}
private static string getVariableString(byte[] data, ref int i)
private string getVariableString(GameConfig config, byte[] data, ref int i)
{
string s = "";
ushort count = BitConverter.ToUInt16(data, i); i += 2;
@@ -220,7 +223,7 @@ private static string getVariableString(byte[] data, ref int i)
return $"[~ {line}]";
}
string varName = getVariableString(variable);
string varName = getVariableString(config, variable);
s += "[VAR" + " " + varName;
if (count > 1)
@@ -238,7 +241,7 @@ private static string getVariableString(byte[] data, ref int i)
s += "]";
return s;
}
private static IEnumerable<ushort> getEscapeValues(char esc)
private IEnumerable<ushort> getEscapeValues(char esc)
{
var vals = new List<ushort>();
switch (esc)
@@ -250,7 +253,7 @@ private static IEnumerable<ushort> getEscapeValues(char esc)
default: throw new Exception("Invalid terminated line: \"\\" + esc + "\"");
}
}
private static IEnumerable<ushort> getVariableValues(string variable)
private IEnumerable<ushort> getVariableValues(GameConfig config, string variable)
{
string[] split = variable.Split(' ');
if (split.Length < 2)
@@ -270,19 +273,19 @@ private static IEnumerable<ushort> getVariableValues(string variable)
vals.Add(Convert.ToUInt16(split[1]));
break;
case "VAR": // Text Variable
vals.AddRange(getVariableParameters(split[1]));
vals.AddRange(getVariableParameters(config, split[1]));
break;
default: throw new Exception("Unknown variable method type!");
}
return vals;
}
private static IEnumerable<ushort> getVariableParameters(string text)
private IEnumerable<ushort> getVariableParameters(GameConfig config, string text)
{
var vals = new List<ushort>();
int bracket = text.IndexOf('(');
bool noArgs = bracket < 0;
string variable = noArgs ? text : text.Substring(0, bracket);
ushort varVal = getVariableNumber(variable);
ushort varVal = getVariableNumber(config, variable);
if (!noArgs)
{
@@ -299,10 +302,9 @@ private static IEnumerable<ushort> getVariableParameters(string text)
return vals;
}
public static GameConfig Config = new GameConfig(GameVersion.SM);
private static ushort getVariableNumber(string variable)
private ushort getVariableNumber(GameConfig config, string variable)
{
var v = Config.getVariableCode(variable);
var v = config.getVariableCode(variable);
if (v != null)
return (ushort) v.Code;
@@ -312,22 +314,22 @@ private static ushort getVariableNumber(string variable)
}
catch { throw new ArgumentException($"Variable \"{variable}\" parse error."); }
}
private static string getVariableString(ushort variable)
private string getVariableString(GameConfig config, ushort variable)
{
var v = Config.getVariableName(variable);
var v = config.getVariableName(variable);
return v == null ? variable.ToString("X4") : v.Name;
}
// Exposed Methods
public static string[] getStrings(byte[] data)
public static string[] getStrings(GameConfig config, byte[] data)
{
TextFile t;
try { t = new TextFile(data); } catch { return null; }
try { t = new TextFile(config, data); } catch { return null; }
return t.Lines;
}
public static byte[] getBytes(string[] lines)
public static byte[] getBytes(GameConfig config, string[] lines)
{
return new TextFile { Lines = lines }.Data;
return new TextFile (config) { Lines = lines }.Data;
}
}
}

View File

@@ -140,7 +140,7 @@ private void formClosing(object sender, FormClosingEventArgs e)
if (Config == null) return;
var g = Config.GARCGameText;
string[][] files = Config.GameTextStrings;
g.Files = files.Select(TextFile.getBytes).ToArray();
g.Files = files.Select(x => TextFile.getBytes(Config, x)).ToArray();
g.Save();
}
@@ -327,7 +327,6 @@ private bool checkIfRomFS(string path)
RomFSPath = path;
Config = cfg;
TextFile.Config = cfg;
Randomizer.MaxSpeciesID = cfg.MaxSpeciesID;
return true;
}
@@ -439,7 +438,7 @@ private void B_GameText_Click(object sender, EventArgs e)
var g = Config.GARCGameText;
string[][] files = Config.GameTextStrings;
Invoke((Action)(() => new TextEditor(files, "gametext").ShowDialog()));
g.Files = files.Select(TextFile.getBytes).ToArray();
g.Files = files.Select(x => TextFile.getBytes(Main.Config, x)).ToArray();
g.Save();
}).Start();
}
@@ -449,9 +448,9 @@ private void B_StoryText_Click(object sender, EventArgs e)
new Thread(() =>
{
var g = Config.getGARCData("storytext");
string[][] files = g.Files.Select(file => new TextFile(file).Lines).ToArray();
string[][] files = g.Files.Select(file => new TextFile(Config, file).Lines).ToArray();
Invoke((Action)(() => new TextEditor(files, "storytext").ShowDialog()));
g.Files = files.Select(TextFile.getBytes).ToArray();
g.Files = files.Select(x => TextFile.getBytes(Main.Config, x)).ToArray();
g.Save();
}).Start();
}
@@ -622,7 +621,7 @@ private void runOWSE6()
// Don't set any data back. Just view.
{
var g = Config.getGARCData("storytext");
string[][] tfiles = g.Files.Select(file => new TextFile(file).Lines).ToArray();
string[][] tfiles = g.Files.Select(file => new TextFile(Config, file).Lines).ToArray();
Invoke((Action)(() => new OWSE().Show()));
Invoke((Action)(() => new TextEditor(tfiles, "storytext").Show()));
while (Application.OpenForms.Count > 1)
@@ -646,7 +645,7 @@ private void runOWSE7()
var wd = Config.getlzGARCData(files[2]);
var g = Config.getGARCData("storytext");
string[][] tfiles = g.Files.Select(file => new TextFile(file).Lines).ToArray();
string[][] tfiles = g.Files.Select(file => new TextFile(Config, file).Lines).ToArray();
Invoke((Action)(() => new TextEditor(tfiles, "storytext").Show()));
Invoke((Action)(() => new OWSE7(ed, zd, wd).Show()));
while (Application.OpenForms.Count > 1)
@@ -1164,14 +1163,15 @@ private void threadSet(string outfile, string infolder, int padBytes, bool PB =
}
// Text Requests
[Obsolete("Use Main.Config.getText instead")]
internal static string[] getText(TextName file)
{
return (string[])Config.GameTextStrings[Config.getGameText(file).Index].Clone();
return Config.getText(file);
}
[Obsolete("Use Main.Config.getText instead")]
internal static bool setText(TextName file, string[] strings)
{
Config.GameTextStrings[Config.getGameText(file).Index] = strings;
return true;
return Config.setText(file, strings);
}
// Update RichTextBox

View File

@@ -382,7 +382,7 @@ private void updateStarterText()
var s = Main.Config.getGARCByReference(sr);
byte[][] storytextdata = s.Files;
string[] storyText = TextFile.getStrings(storytextdata[41]);
string[] storyText = TextFile.getStrings(Main.Config, storytextdata[41]);
for (int j = 0; j < 3; j++)
{
@@ -402,7 +402,7 @@ private void updateStarterText()
storyText[1 + j] = line;
}
storytextdata[41] = TextFile.getBytes(storyText);
storytextdata[41] = TextFile.getBytes(Main.Config, storyText);
s.Files = storytextdata;
s.Save();
}