Skip to content

Commit

Permalink
Implemented text data into json converter tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nifyr committed Oct 18, 2022
1 parent 37c100d commit a9d1161
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 11 deletions.
60 changes: 58 additions & 2 deletions Forms/JsonConverterForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class JsonConverterForm : Form
{
private const string pokemonFileName = "IOPokémon.json";
private const string trainersFileName = "IOTrainers.json";
private const string textFileName = "IOText.json";
private readonly List<string> typings;
private readonly List<string> items;
private readonly List<string> growthRates;
Expand Down Expand Up @@ -46,7 +47,7 @@ public partial class JsonConverterForm : Form

public readonly string[] modes = new string[]
{
"Pokémon", "Trainers"
"Pokémon", "Trainers", "Text"
};

public JsonConverterForm()
Expand Down Expand Up @@ -111,6 +112,10 @@ private void ExportJson(object sender, EventArgs e)
sfd.FileName = trainersFileName;
data = GenerateTrainerStructs(gameData.trainers);
break;
case 2:
sfd.FileName = textFileName;
data = GenerateTextStructs(gameData.messageFileSets.ToList());
break;
}
if (data == null)
return;
Expand Down Expand Up @@ -288,6 +293,26 @@ private void ExportJson(object sender, EventArgs e)
return output;
}

private static List<JsonConverterStructs.MessageFileSet> GenerateTextStructs(List<GameDataTypes.MessageFileSet> data)
{
List<JsonConverterStructs.MessageFileSet> output = new();
for (int languageIdx = 0; languageIdx < data.Count; languageIdx++)
{
GameDataTypes.MessageFileSet gmfs = data[languageIdx];
JsonConverterStructs.MessageFileSet jmfs = new()
{
textAssets = gmfs.messageFiles.Select(gmf => new JsonConverterStructs.MessageFile()
{
assetName = gmf.mName,
strings = gmf.labelDatas.Select(ld => ld.GetMacroString()).ToList()
}).ToList()
};

output.Add(jmfs);
}
return output;
}

private void ImportJson(object sender, EventArgs e)
{
OpenFileDialog ofd = new()
Expand Down Expand Up @@ -319,6 +344,13 @@ private void ImportJson(object sender, EventArgs e)
gameData.trainers = ParseTrainerStructs(t, gameData.trainers);
gameData.SetModified(GameDataSet.DataField.Trainers);
break;
case 2:
List<JsonConverterStructs.MessageFileSet> mfs =
JsonConvert.DeserializeObject<List<JsonConverterStructs.MessageFileSet>>
(File.ReadAllText(ofd.FileName));
gameData.messageFileSets = ParseTextStructs(mfs, gameData.messageFileSets.ToList()).ToArray();
gameData.SetModified(GameDataSet.DataField.MessageFileSets);
break;
}
ShowSuccessMessage();
}
Expand Down Expand Up @@ -419,7 +451,7 @@ private void ImportJson(object sender, EventArgs e)
personalEntries.Add(gp);

if (gp.dexID > dexEntries.Count)
throw new ArgumentException("Incorrect order. The dexID of " + gp.dexID + " shows up a little early, my main man.");
throw new ArgumentException("Incorrect order. The dexID of " + gp.dexID + " shows up a little early, my man.");

if (dexEntries.Count == gp.dexID)
{
Expand Down Expand Up @@ -518,6 +550,30 @@ private void ImportJson(object sender, EventArgs e)
return trainers;
}

private static List<GameDataTypes.MessageFileSet> ParseTextStructs(List<JsonConverterStructs.MessageFileSet> data, List<GameDataTypes.MessageFileSet> oldEntries)
{
if (data.Count != oldEntries.Count)
throw new ArgumentException("Incorrect number of languages in the json, friend.");
for (int languageIdx = 0; languageIdx < oldEntries.Count; languageIdx++)
{
JsonConverterStructs.MessageFileSet jmfs = data[languageIdx];
GameDataTypes.MessageFileSet gmfs = oldEntries[languageIdx];
if (jmfs.textAssets.Count != gmfs.messageFiles.Count)
throw new ArgumentException("Incorrect number of text assets in the json, my man.");
for (int assetIdx = 0; assetIdx < gmfs.messageFiles.Count; assetIdx++)
{
JsonConverterStructs.MessageFile jmf = jmfs.textAssets[assetIdx];
GameDataTypes.MessageFile gmf = gmfs.messageFiles[assetIdx];
if (jmf.strings.Count != gmf.labelDatas.Count)
throw new ArgumentException("Incorrect number of strings in " + jmf.assetName + ", homeboy.");
for (int stringIdx = 0; stringIdx < gmf.labelDatas.Count; stringIdx++)
gmf.labelDatas[stringIdx].SetMacroString(jmf.strings[stringIdx]);
}
}

return oldEntries;
}

private static int GetIndex(string[] source, string s) => GetIndex(source.ToList(), s);

private static int GetIndex(List<string> source, string s)
Expand Down
162 changes: 153 additions & 9 deletions Structs/GameDataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ImpostersOrdeal
Expand Down Expand Up @@ -525,6 +526,7 @@ public class LabelData : ICloneable
public List<int> attributeValues;
public List<TagData> tagDatas;
public List<WordData> wordDatas;

public object Clone()
{
LabelData ld = (LabelData)MemberwiseClone();
Expand Down Expand Up @@ -565,6 +567,60 @@ public bool IsDialogString()
return true;
return false;
}

public string GetMacroString()
{
string s = "";
foreach (WordData wd in wordDatas)
s += wd.str + wd.GetMacro();
return s;
}

public void SetMacroString(string s)
{
List<WordData> newWordDatas = new();
int i = 0;
while (s.Length > 0)
{
Match m = Regex.Match(s, @"\A[^\\]*\\");
WordData wd = null;

if (i < wordDatas.Count)
wd = wordDatas[i];
else
wd = new()
{
patternID = 7,
tagIndex = -1,
};

if (m.Success)
{
char c = s.ToCharArray()[m.Value.Length];
wd.eventID = c switch
{
'0' => 0,
'n' => 1,
'w' => 2,
'r' => 3,
'f' => 4,
'e' => 5,
_ => throw new ArgumentException("Unknown macro: \\" + c),
};
wd.str = s[..(m.Value.Length - 1)];
s = s[(m.Value.Length + 1)..];
}
else
{
wd.eventID = 7;
wd.str = s;
s = "";
}

wd.strWidth = wd.str.ToCharArray().Select(c => WordData.charWidths.TryGetValue(c, out float f) ? f : 0f).Sum();
i++;
}
}
}

public class WordData : ICloneable
Expand All @@ -576,6 +632,82 @@ public class WordData : ICloneable
public string str;
public float strWidth;

public static readonly Dictionary<char, float> charWidths = new()
{
{ 'A', 20.125f },
{ 'B', 17.3125f },
{ 'C', 20.25f },
{ 'D', 22.109375f },
{ 'E', 15.84375f },
{ 'F', 16.15625f },
{ 'G', 23.328125f },
{ 'H', 22.015625f },
{ 'I', 8.390625f },
{ 'J', 12.640625f },
{ 'K', 19.046875f },
{ 'L', 14.96875f },
{ 'M', 25.984375f },
{ 'N', 21.625f },
{ 'O', 24.390625f },
{ 'P', 16.28125f },
{ 'Q', 24.390625f },
{ 'R', 17.625f },
{ 'S', 15.453125f },
{ 'T', 17.125f },
{ 'U', 21.34375f },
{ 'V', 20.0f },
{ 'W', 28.640625f },
{ 'X', 20.28125f },
{ 'Y', 19.328125f },
{ 'Z', 18.171875f },
{ 'a', 15.296875f },
{ 'b', 17.25f },
{ 'c', 13.953125f },
{ 'd', 17.28125f },
{ 'e', 15.96875f },
{ 'é', 15.96875f },
{ 'f', 9.765625f },
{ 'g', 16.1875f },
{ 'h', 15.578125f },
{ 'i', 7.609375f },
{ 'j', 7.328125f },
{ 'k', 14.8125f },
{ 'l', 7.78125f },
{ 'm', 22.71875f },
{ 'n', 15.578125f },
{ 'o', 17.15625f },
{ 'p', 17.25f },
{ 'q', 17.28125f },
{ 'r', 9.65625f },
{ 's', 11.515625f },
{ 't', 10.046875f },
{ 'u', 15.578125f },
{ 'v', 14.078125f },
{ 'w', 19.8125f },
{ 'x', 14.46875f },
{ 'y', 14.375f },
{ 'z', 13.03125f },
{ '1', 0f },
{ '2', 0f },
{ '3', 0f },
{ '4', 0f },
{ '5', 0f },
{ '6', 0f },
{ '7', 0f },
{ '8', 0f },
{ '9', 0f },
{ '0', 0f },
{ '-', 11.203125f },
{ '!', 7.265625f },
{ '?', 14.46875f },
{ '“', 13.03125f },
{ '”', 13.03125f },
{ ' ', 8.671875f },
{ ',', 8.828125f },
{ '.', 7.90625f },
{ '’', 8.828125f }
};

public string GetEndChar()
{
return eventID switch
Expand All @@ -591,6 +723,21 @@ public string GetEndChar()
};
}

public string GetMacro()
{
return eventID switch
{
0 => "\\0", //No marker
1 => "\\n", //New line marker
2 => "\\w", //Wait marker
3 => "\\r", //New textbox marker
4 => "\\f", //Scroll textbox marker
5 => "\\e", //Start/join event marker?
7 => "", //End of message
_ => "\\0", //Unknown
};
}

public object Clone()
{
return MemberwiseClone();
Expand Down Expand Up @@ -1268,16 +1415,13 @@ public int GetID()

public string GetName()
{
switch (damageCategoryID)
return damageCategoryID switch
{
case 0:
return "Status";
case 1:
return "Physical";
case 2:
return "Special";
}
return null;
0 => "Status",
1 => "Physical",
2 => "Special",
_ => null,
};
}

public bool IsValid()
Expand Down
11 changes: 11 additions & 0 deletions Structs/JsonConverterStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,16 @@ public struct TrainerPokemon
public StatSpread ivs;
public StatSpread evs;
}

public struct MessageFileSet
{
public List<MessageFile> textAssets;
}

public struct MessageFile
{
public string assetName;
public List<string> strings;
}
}
}

0 comments on commit a9d1161

Please sign in to comment.