Skip to content

Commit

Permalink
more async for monster and data dat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebanisu committed Oct 15, 2024
1 parent 8b7e4ae commit b600dda
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 79 deletions.
197 changes: 121 additions & 76 deletions Dat Dump/DumpMonsterAndCharacterDat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static async Task Process()
{
var xmlWriterSettings = new XmlWriterSettings
{
Async = true, // Enable async operations
Indent = true,
IndentChars = "\t", // note: default is two spaces
NewLineOnAttributes = true,
Expand All @@ -57,7 +58,7 @@ public static async Task Process()
{
await LoadMonsters();
//header for monster attacks
csv2File.WriteLine($"{nameof(Enemy)}{Ls}" +
await csv2File.WriteLineAsync($"{nameof(Enemy)}{Ls}" +
$"{nameof(Enemy.EII.Data.FileName)}{Ls}" +
$"{nameof(Abilities)}{Ls}" +
$"Number{Ls}" +
Expand All @@ -66,176 +67,220 @@ public static async Task Process()
$"ID{Ls}" +
$"Name{Ls}");
//header for animation info
csvFile.WriteLine($"Type{Ls}Type ID{Ls}Name{Ls}Animation Count{Ls}Sequence Count{Ls}Sequence ID{Ls}Offset{Ls}Bytes");
await csvFile.WriteLineAsync($"Type{Ls}Type ID{Ls}Name{Ls}Animation Count{Ls}Sequence Count{Ls}Sequence ID{Ls}Offset{Ls}Bytes");
using (var xmlWriter = XmlWriter.Create("SequenceDump.xml", xmlWriterSettings))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("dat");
await xmlWriter.WriteStartDocumentAsync();
await xmlWriter.WriteStartElementAsync(null, "dat", null);

XmlMonsterData(xmlWriter, csvFile, csv2File);
XmlCharacterData(xmlWriter, csvFile);
await XmlMonsterDataAsync(xmlWriter, csvFile, csv2File);
await XmlCharacterDataAsync(xmlWriter, csvFile);

xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
await xmlWriter.WriteEndElementAsync();
await xmlWriter.WriteEndDocumentAsync();
}
}
}

Console.Write("Press [Enter] key to continue... ");
Console.ReadLine();
}

private static string XmlAnimations(XmlWriter xmlWriter, DatFile battleDatFile)
private static async Task<string> XmlAnimationsAsync(XmlWriter xmlWriter, DatFile battleDatFile)
{
var count = $"{battleDatFile.Animations.Count}";
xmlWriter.WriteStartElement("animations");
xmlWriter.WriteAttributeString("Count", count);
xmlWriter.WriteEndElement();
await xmlWriter.WriteStartElementAsync(null, "animations", null);
await xmlWriter.WriteAttributeStringAsync(null, "Count", null, count);
await xmlWriter.WriteEndElementAsync();
return count;
}

private static void XmlCharacterData(XmlWriter xmlWriter, TextWriter csvFile)
private static async Task XmlCharacterDataAsync(XmlWriter xmlWriter, TextWriter csvFile)
{
xmlWriter.WriteStartElement("characters");
await xmlWriter.WriteStartElementAsync(null, "characters", null);
for (var i = 0; i <= 10; i++)
{
DatFile test = CharacterDatFile.CreateInstance(i, 0);
if (test != null && CharacterData.TryAdd(i, test))
{
// Character data added successfully
}

if (!CharacterData.TryGetValue(i, out var battleDat) || battleDat == null) continue;

const string type = "character";
xmlWriter.WriteStartElement(type);
await xmlWriter.WriteStartElementAsync(null, type, null);

var id = i.ToString();
xmlWriter.WriteAttributeString("ID", id);
await xmlWriter.WriteAttributeStringAsync(null, "ID", null, id);

var name = Memory.Strings.GetName((Characters)i);
xmlWriter.WriteAttributeString("name", name);
await xmlWriter.WriteAttributeStringAsync(null, "name", null, name);

var prefix0 = $"{type}{Ls}{id}{Ls}";
var prefix1 = $"{name}";
prefix1 += $"{Ls}{XmlAnimations(xmlWriter, battleDat)}";
XmlSequences(xmlWriter, battleDat, csvFile, $"{prefix0}{prefix1}");
XmlWeaponData(xmlWriter, i, ref battleDat, csvFile, prefix1);
xmlWriter.WriteEndElement();
prefix1 += $"{Ls}{await XmlAnimationsAsync(xmlWriter, battleDat)}"; // Assuming XmlAnimations is async-safe

await XmlSequencesAsync(xmlWriter, battleDat, csvFile, $"{prefix0}{prefix1}"); // Assuming XmlSequences is now async
await XmlWeaponDataAsync(xmlWriter, i, battleDat, csvFile, prefix1); // Assuming XmlWeaponData is async

await xmlWriter.WriteEndElementAsync(); // End of "character"
}
xmlWriter.WriteEndElement();
await xmlWriter.WriteEndElementAsync(); // End of "characters"
}

private static void XmlMonsterData(XmlWriter xmlWriter, StreamWriter csvFile, TextWriter csv2File)

private static async Task XmlMonsterDataAsync(XmlWriter xmlWriter, StreamWriter csvFile, TextWriter csv2File)
{
xmlWriter.WriteStartElement("monsters");
await xmlWriter.WriteStartElementAsync(null, "monsters", null);
for (var i = 0; i <= 200; i++)
{
if (!MonsterData.TryGetValue(i, out var battleDat) || battleDat == null) continue;

const string type = "monster";
var id = i.ToString();
var name = battleDat.Information.Name ?? new FF8String("");
var prefix = $"{type}{Ls}{id}{Ls}{name}";
xmlWriter.WriteStartElement(type);
xmlWriter.WriteAttributeString("ID", id);
xmlWriter.WriteAttributeString("name", name);
prefix += $"{Ls}{XmlAnimations(xmlWriter, battleDat)}";
XmlSequences(xmlWriter, battleDat, csvFile, prefix);
xmlWriter.WriteEndElement();

await xmlWriter.WriteStartElementAsync(null, type, null);
await xmlWriter.WriteAttributeStringAsync(null, "ID", null, id);
await xmlWriter.WriteAttributeStringAsync(null, "name", null, name);

prefix += $"{Ls}{await XmlAnimationsAsync(xmlWriter, battleDat)}"; // Assuming XmlAnimations is async-safe

await XmlSequencesAsync(xmlWriter, battleDat, csvFile, prefix); // Assuming XmlSequences is now async

await xmlWriter.WriteEndElementAsync(); // End of "monster"

var e = Enemy.Load(new EnemyInstanceInformation { Data = battleDat });
void addAbility(string fieldName, Abilities a, int number)

async Task addAbilityAsync(string fieldName, Abilities a, int number)
{
csv2File.WriteLine($"{name}{Ls}" +
$"{battleDat.FileName}{Ls}" +
$"{fieldName}{Ls}" +
$"{number}{Ls}" +
$"{a.Animation}{Ls}" +
$"{(a.Item != null ? nameof(a.Item) : a.Magic != null ? nameof(a.Magic) : a.Monster != null ? nameof(a.Monster) : "")}{Ls}" +
$"{a.Item?.ID ?? (a.Magic?.MagicDataID ?? (a.Monster?.EnemyAttackID ?? 0))}{Ls}" +
$"\"{(a.Item != null ? a.Item.Value.Name : a.Magic != null ? a.Magic.Name : a.Monster != null ? a.Monster.Name : new FF8String(""))}\"{Ls}");
await csv2File.WriteLineAsync($"{name}{Ls}" +
$"{battleDat.FileName}{Ls}" +
$"{fieldName}{Ls}" +
$"{number}{Ls}" +
$"{a.Animation}{Ls}" +
$"{(a.Item != null ? nameof(a.Item) : a.Magic != null ? nameof(a.Magic) : a.Monster != null ? nameof(a.Monster) : "")}{Ls}" +
$"{a.Item?.ID ?? (a.Magic?.MagicDataID ?? (a.Monster?.EnemyAttackID ?? 0))}{Ls}" +
$"\"{(a.Item != null ? a.Item.Value.Name : a.Magic != null ? a.Magic.Name : a.Monster != null ? a.Monster.Name : new FF8String(""))}\"{Ls}");
}
void addAbilities(string fieldName, IReadOnlyList<Abilities> abilities)

async Task addAbilitiesAsync(string fieldName, IReadOnlyList<Abilities> abilities)
{
if (abilities == null) return;
for (var number = 0; number < e.Info.AbilitiesLow.Length; number++)
{
var a = abilities[number];
addAbility(fieldName, a, number);
await addAbilityAsync(fieldName, a, number);
}
}
addAbilities(nameof(e.Info.AbilitiesLow), e.Info.AbilitiesLow);
addAbilities(nameof(e.Info.AbilitiesMed), e.Info.AbilitiesMed);
addAbilities(nameof(e.Info.AbilitiesHigh), e.Info.AbilitiesHigh);

await addAbilitiesAsync(nameof(e.Info.AbilitiesLow), e.Info.AbilitiesLow);
await addAbilitiesAsync(nameof(e.Info.AbilitiesMed), e.Info.AbilitiesMed);
await addAbilitiesAsync(nameof(e.Info.AbilitiesHigh), e.Info.AbilitiesHigh);
}
xmlWriter.WriteEndElement();
await xmlWriter.WriteEndElementAsync(); // End of "monsters"
}

private static void XmlSequences(XmlWriter xmlWriter, DatFile battleDatFile, TextWriter csvFile, string prefix)

private static async Task XmlSequencesAsync(XmlWriter xmlWriter, DatFile battleDatFile, TextWriter csvFile, string prefix)
{
xmlWriter.WriteStartElement("sequences");
await xmlWriter.WriteStartElementAsync(null, "sequences", null);
var count = $"{battleDatFile.Sequences?.Count ?? 0}";
xmlWriter.WriteAttributeString("Count", count);
await xmlWriter.WriteAttributeStringAsync(null, "Count", null, count);

if (battleDatFile.Sequences != null)
{
foreach (var s in battleDatFile.Sequences)
{
xmlWriter.WriteStartElement("sequence");
await xmlWriter.WriteStartElementAsync(null, "sequence", null);
var id = s.ID.ToString();
var offset = s.Offset.ToString("X");
var bytes = s.Count.ToString();

xmlWriter.WriteAttributeString("ID", id);
xmlWriter.WriteAttributeString("offset", offset);
xmlWriter.WriteAttributeString("bytes", bytes);
await xmlWriter.WriteAttributeStringAsync(null, "ID", null, id);
await xmlWriter.WriteAttributeStringAsync(null, "offset", null, offset);
await xmlWriter.WriteAttributeStringAsync(null, "bytes", null, bytes);

if (csvFile != null)
{
await csvFile.WriteAsync($"{prefix ?? ""}{Ls}{count}{Ls}{id}{Ls}{s.Offset}{Ls}{bytes}");
}

csvFile?.Write($"{prefix ?? ""}{Ls}{count}{Ls}{id}{Ls}{s.Offset}{Ls}{bytes}");
foreach (var b in s)
{
xmlWriter.WriteString($"{b:X2} ");
csvFile?.Write($"{Ls}{b}");
await xmlWriter.WriteStringAsync($"{b:X2} ");
if (csvFile != null)
{
await csvFile.WriteAsync($"{Ls}{b}");
}
}

if (csvFile != null)
{
await csvFile.WriteLineAsync();
}
csvFile?.Write(Environment.NewLine);
xmlWriter.WriteEndElement();

await xmlWriter.WriteEndElementAsync(); // End of "sequence"
}
csvFile?.Flush();
xmlWriter.WriteEndElement();
}

if (csvFile != null)
{
await csvFile.FlushAsync(); // Ensure async flush
}

await xmlWriter.WriteEndElementAsync(); // End of "sequences"
}

private static void XmlWeaponData(XmlWriter xmlWriter, int characterID, ref DatFile r, TextWriter csvFile, string prefix1)
private static async Task XmlWeaponDataAsync(XmlWriter xmlWriter, int characterID, DatFile r, TextWriter csvFile, string prefix1)
{
var weaponData = new ConcurrentDictionary<int, DatFile>();
xmlWriter.WriteStartElement("weapons");
await xmlWriter.WriteStartElementAsync(null, "weapons", null);

for (var i = 0; i <= 40; i++)
{
DatFile test;
if (characterID == 1 || characterID == 9)
test = WeaponDatFile.CreateInstance(characterID, i, r);
else
test = WeaponDatFile.CreateInstance(characterID, i);

if (test != null && weaponData.TryAdd(i, test))
{
// Weapon data added successfully
}

if (!weaponData.TryGetValue(i, out var battleDat) || battleDat == null) continue;

const string type = "weapon";
var id = i.ToString();
xmlWriter.WriteStartElement(type);
xmlWriter.WriteAttributeString("ID", id);
var index = ModuleBattleDebug.Weapons[(Characters) characterID]?.Select(((b, i1) => new {i, b}))
await xmlWriter.WriteStartElementAsync(null, type, null);
await xmlWriter.WriteAttributeStringAsync(null, "ID", null, id);

var index = ModuleBattleDebug.Weapons[(Characters)characterID]?.Select(((b, i1) => new { i, b }))
.FirstOrDefault(v => v.b == i)?.i;
if(!index.HasValue) continue;

if (!index.HasValue) continue;

var currentWeaponData = Memory.KernelBin.WeaponsData.FirstOrDefault(v =>
v.Character == (Characters) characterID && v.AltID == checked((byte)index.Value));
v.Character == (Characters)characterID && v.AltID == checked((byte)index.Value));

if (currentWeaponData != default)
{
xmlWriter.WriteAttributeString("name", currentWeaponData.Name);
await xmlWriter.WriteAttributeStringAsync(null, "name", null, currentWeaponData.Name);

var prefix = $"{type}{Ls}{id}{Ls}{currentWeaponData.Name}/{prefix1}"; //bringing over name from character.
//xmlWriter.WriteAttributeString("name", Memory.Strings.GetName((Characters)i));
var prefix = $"{type}{Ls}{id}{Ls}{currentWeaponData.Name}/{prefix1}"; // Bringing over name from character.

XmlAnimations(xmlWriter, battleDat);
XmlSequences(xmlWriter, battleDat, csvFile, prefix);
await XmlAnimationsAsync(xmlWriter, battleDat); // Assuming XmlAnimations is now async
await XmlSequencesAsync(xmlWriter, battleDat, csvFile, prefix); // Assuming XmlSequences is async
}
xmlWriter.WriteEndElement();

await xmlWriter.WriteEndElementAsync(); // End of "weapon"
}
xmlWriter.WriteEndElement();

await xmlWriter.WriteEndElementAsync(); // End of "weapons"
}


#endregion Methods
}
}
6 changes: 3 additions & 3 deletions Dat Dump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ internal class Program
private static void Main(string[] args)
{
Memory.Init(null, null, null, args);
//DumpEncounterInfo.Process().GetAwaiter().GetResult();
//DumpMonsterAndCharacterDat.Process().GetAwaiter().GetResult();
DumpMovieInfo.Process().GetAwaiter().GetResult();
DumpEncounterInfo.Process().GetAwaiter().GetResult();
DumpMonsterAndCharacterDat.Process().GetAwaiter().GetResult();
//DumpMovieInfo.Process().GetAwaiter().GetResult();
}

#endregion Methods
Expand Down

0 comments on commit b600dda

Please sign in to comment.