Initial pieces of payload rework; everything is read-only at the moment, pending next stages. Lumina is now used for on-demand property resolution, properties are only exposed for user-facing values, and mostly expose the lumina object(s) where applicable

This commit is contained in:
meli 2020-04-21 16:41:03 -07:00
parent 22e9eb89b8
commit f2cee5f5bc
17 changed files with 355 additions and 253 deletions

View file

@ -1,7 +1,10 @@
using Dalamud.Data.TransientSheet;
using Lumina.Excel.GeneratedSheets;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{
@ -9,78 +12,29 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{
public override PayloadType Type => PayloadType.AutoTranslateText;
public uint Group { get; set; }
public uint Key { get; set; }
public string Text { get; set; }
public override void Resolve()
private string text;
public string Text
{
// TODO: fixup once lumina DI is in
//if (string.IsNullOrEmpty(Text))
//{
// var sheet = dalamud.Data.GetExcelSheet<Completion>();
// Completion row = null;
// try
// {
// // try to get the row in the Completion table itself, because this is 'easiest'
// // The row may not exist at all (if the Key is for another table), or it could be the wrong row
// // (again, if it's meant for another table)
// row = sheet.GetRow(Key);
// }
// catch {} // don't care, row will be null
// if (row?.Group == Group)
// {
// // if the row exists in this table and the group matches, this is actually the correct data
// Text = $"{{ {row.Text} }} ";
// }
// else
// {
// Log.Verbose("row mismatch");
// try
// {
// // we need to get the linked table and do the lookup there instead
// // in this case, there will only be one entry for this group id
// row = sheet.GetRows().First(r => r.Group == Group);
// // many of the names contain valid id ranges after the table name, but we don't need those
// var actualTableName = row.LookupTable.Split('[')[0];
// var name = actualTableName switch
// {
// // TODO: rest of xref'd tables
// "Action" => dalamud.Data.GetExcelSheet<Data.TransientSheet.Action>().GetRow(Key).Name,
// "ClassJob" => dalamud.Data.GetExcelSheet<ClassJob>().GetRow(Key).Name,
// "CraftAction" => dalamud.Data.GetExcelSheet<CraftAction>().GetRow(Key).Name,
// "Mount" => dalamud.Data.GetExcelSheet<Mount>().GetRow(Key).Singular,
// "PlaceName" => dalamud.Data.GetExcelSheet<PlaceName>().GetRow(Key).Name,
// "Race" => dalamud.Data.GetExcelSheet<Race>().GetRow(Key).Masculine,
// _ => throw new Exception(actualTableName)
// };
// Text = $"{{ {name} }} ";
// }
// catch (Exception e)
// {
// Log.Error(e, $"AutoTranslatePayload - failed to resolve: {this}");
// }
// }
//}
get
{
this.text ??= Resolve();
return this.text;
}
}
private uint group;
private uint key;
public override byte[] Encode()
{
var keyBytes = MakeInteger(Key);
var keyBytes = MakeInteger(this.key);
var chunkLen = keyBytes.Length + 2;
var bytes = new List<byte>()
{
START_BYTE,
(byte)SeStringChunkType.AutoTranslateKey, (byte)chunkLen,
(byte)Group
(byte)this.group
};
bytes.AddRange(keyBytes);
bytes.Add(END_BYTE);
@ -90,16 +44,83 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
public override string ToString()
{
return $"{Type} - Group: {Group}, Key: {Key}, Text: {Text}";
return $"{Type} - Group: {group}, Key: {key}";
}
protected override void ProcessChunkImpl(BinaryReader reader, long endOfStream)
{
// this seems to always be a bare byte, and not following normal integer encoding
// the values in the table are all <70 so this is presumably ok
Group = reader.ReadByte();
this.group = reader.ReadByte();
Key = GetInteger(reader);
this.key = GetInteger(reader);
}
private string Resolve()
{
string value = null;
var sheet = this.dataResolver.GetExcelSheet<Completion>();
Completion row = null;
try
{
// try to get the row in the Completion table itself, because this is 'easiest'
// The row may not exist at all (if the Key is for another table), or it could be the wrong row
// (again, if it's meant for another table)
row = sheet.GetRow((int)this.key);
}
catch { } // don't care, row will be null
if (row?.Group == this.group)
{
// if the row exists in this table and the group matches, this is actually the correct data
value = row.Text;
}
else
{
try
{
// we need to get the linked table and do the lookup there instead
// in this case, there will only be one entry for this group id
row = sheet.GetRows().First(r => r.Group == this.group);
// many of the names contain valid id ranges after the table name, but we don't need those
var actualTableName = row.LookupTable.Split('[')[0];
var ikey = (int)this.key;
var name = actualTableName switch
{
"Action" => this.dataResolver.GetExcelSheet<Lumina.Excel.GeneratedSheets.Action>().GetRow(ikey).Name,
"ActionComboRoute" => this.dataResolver.GetExcelSheet<ActionComboRoute>().GetRow(ikey).Name,
"BuddyAction" => this.dataResolver.GetExcelSheet<BuddyAction>().GetRow(ikey).Name,
"ClassJob" => this.dataResolver.GetExcelSheet<ClassJob>().GetRow(ikey).Name,
"Companion" => this.dataResolver.GetExcelSheet<Companion>().GetRow(ikey).Singular,
"CraftAction" => this.dataResolver.GetExcelSheet<CraftAction>().GetRow(ikey).Name,
"GeneralAction" => this.dataResolver.GetExcelSheet<GeneralAction>().GetRow(ikey).Name,
"GuardianDeity" => this.dataResolver.GetExcelSheet<GuardianDeity>().GetRow(ikey).Name,
"MainCommand" => this.dataResolver.GetExcelSheet<MainCommand>().GetRow(ikey).Name,
"Mount" => this.dataResolver.GetExcelSheet<Mount>().GetRow(ikey).Singular,
"Pet" => this.dataResolver.GetExcelSheet<Pet>().GetRow(ikey).Name,
"PetAction" => this.dataResolver.GetExcelSheet<PetAction>().GetRow(ikey).Name,
// TODO: lumina doesn't have PetMirage
"PlaceName" => this.dataResolver.GetExcelSheet<PlaceName>().GetRow(ikey).Name,
"Race" => this.dataResolver.GetExcelSheet<Race>().GetRow(ikey).Masculine,
"TextCommand" => this.dataResolver.GetExcelSheet<TextCommand>().GetRow(ikey).Command,
"Tribe" => this.dataResolver.GetExcelSheet<Tribe>().GetRow(ikey).Masculine,
"Weather" => this.dataResolver.GetExcelSheet<Weather>().GetRow(ikey).Name,
_ => throw new Exception(actualTableName)
};
value = name;
}
catch (Exception e)
{
Log.Error(e, $"AutoTranslatePayload - failed to resolve: {this}");
}
}
return value;
}
}
}