Merge pull request #167 from ms2mml/payload-serialize

This commit is contained in:
goaaats 2020-09-08 23:45:29 +02:00 committed by GitHub
commit 671e800bb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 86 additions and 1 deletions

View file

@ -51,7 +51,7 @@ namespace Dalamud.Game.Chat.SeStringHandling
/// <summary> /// <summary>
/// The Lumina instance to use for any necessary data lookups. /// The Lumina instance to use for any necessary data lookups.
/// </summary> /// </summary>
protected DataManager DataResolver; public DataManager DataResolver;
// private for now, since subclasses shouldn't interact with this // private for now, since subclasses shouldn't interact with this
// To force-invalidate it, Dirty can be set to true // To force-invalidate it, Dirty can be set to true

View file

@ -6,6 +6,7 @@ using System.IO;
using System.Linq; using System.Linq;
using Dalamud.Data; using Dalamud.Data;
using Dalamud.Data.TransientSheet; using Dalamud.Data.TransientSheet;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -34,7 +35,10 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
} }
} }
[JsonProperty]
private uint group; private uint group;
[JsonProperty]
private uint key; private uint key;
internal AutoTranslatePayload() { } internal AutoTranslatePayload() { }

View file

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using Dalamud.Data; using Dalamud.Data;
using Lumina.Excel.GeneratedSheets; using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -22,6 +23,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public Item Item public Item Item
{ {
get get
@ -57,8 +59,10 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// Whether or not this item link is for a high-quality version of the item. /// Whether or not this item link is for a high-quality version of the item.
/// </summary> /// </summary>
[JsonProperty]
public bool IsHQ { get; private set; } = false; public bool IsHQ { get; private set; } = false;
[JsonProperty]
private uint itemId; private uint itemId;
internal ItemPayload() { } internal ItemPayload() { }

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Dalamud.Data; using Dalamud.Data;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -20,6 +21,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public Map Map public Map Map
{ {
get get
@ -36,6 +38,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public TerritoryType TerritoryType public TerritoryType TerritoryType
{ {
get get
@ -70,6 +73,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The readable y-coordinate position for this map link. This value is approximate and unrounded. /// The readable y-coordinate position for this map link. This value is approximate and unrounded.
/// </summary> /// </summary>
[JsonIgnore]
public float YCoord public float YCoord
{ {
get get
@ -82,6 +86,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// The printable map coordinates for this link. This value tries to match the in-game printable text as closely as possible /// The printable map coordinates for this link. This value tries to match the in-game printable text as closely as possible
/// but is an approximation and may be slightly off for some positions. /// but is an approximation and may be slightly off for some positions.
/// </summary> /// </summary>
[JsonIgnore]
public string CoordinateString public string CoordinateString
{ {
get get
@ -102,6 +107,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The region name for this map link. This corresponds to the upper zone name found in the actual in-game map UI. eg, "La Noscea" /// The region name for this map link. This corresponds to the upper zone name found in the actual in-game map UI. eg, "La Noscea"
/// </summary> /// </summary>
[JsonIgnore]
public string PlaceNameRegion public string PlaceNameRegion
{ {
get get
@ -115,6 +121,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The place name for this map link. This corresponds to the lower zone name found in the actual in-game map UI. eg, "Limsa Lominsa Upper Decks" /// The place name for this map link. This corresponds to the lower zone name found in the actual in-game map UI. eg, "Limsa Lominsa Upper Decks"
/// </summary> /// </summary>
[JsonIgnore]
public string PlaceName public string PlaceName
{ {
get get
@ -129,7 +136,10 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// </summary> /// </summary>
public string DataString => $"m:{TerritoryType.RowId},{Map.RowId},{RawX},{RawY}"; public string DataString => $"m:{TerritoryType.RowId},{Map.RowId},{RawX},{RawY}";
[JsonProperty]
private uint territoryTypeId; private uint territoryTypeId;
[JsonProperty]
private uint mapId; private uint mapId;
// there is no Z; it's purely in the text payload where applicable // there is no Z; it's purely in the text payload where applicable

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Dalamud.Data; using Dalamud.Data;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -14,10 +15,12 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
public override PayloadType Type => PayloadType.Player; public override PayloadType Type => PayloadType.Player;
[JsonProperty]
private string playerName; private string playerName;
/// <summary> /// <summary>
/// The player's displayed name. This does not contain the server name. /// The player's displayed name. This does not contain the server name.
/// </summary> /// </summary>
[JsonIgnore]
public string PlayerName public string PlayerName
{ {
get { return this.playerName; } get { return this.playerName; }
@ -35,6 +38,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public World World public World World
{ {
get get
@ -48,8 +52,10 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// A text representation of this player link matching how it might appear in-game. /// A text representation of this player link matching how it might appear in-game.
/// The world name will always be present. /// The world name will always be present.
/// </summary> /// </summary>
[JsonIgnore]
public string DisplayedName => $"{PlayerName}{(char)SeIconChar.CrossWorld}{World.Name}"; public string DisplayedName => $"{PlayerName}{(char)SeIconChar.CrossWorld}{World.Name}";
[JsonProperty]
private uint serverId; private uint serverId;
internal PlayerPayload() { } internal PlayerPayload() { }

View file

@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -20,6 +21,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
public override PayloadType Type => PayloadType.Unknown; public override PayloadType Type => PayloadType.Unknown;
[JsonProperty]
private byte[] data; private byte[] data;
// this is a bit different from the underlying data // this is a bit different from the underlying data
// We need to store just the chunk data for decode to behave nicely, but when reading data out // We need to store just the chunk data for decode to behave nicely, but when reading data out
@ -28,6 +30,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// The entire payload byte sequence for this payload. /// The entire payload byte sequence for this payload.
/// The returned data is a clone and modifications will not be persisted. /// The returned data is a clone and modifications will not be persisted.
/// </summary> /// </summary>
[JsonIgnore]
public byte[] Data public byte[] Data
{ {
get get
@ -38,8 +41,10 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
} }
} }
[JsonProperty]
private byte chunkType; private byte chunkType;
[JsonConstructor]
internal RawPayload(byte chunkType) internal RawPayload(byte chunkType)
{ {
this.chunkType = chunkType; this.chunkType = chunkType;

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Dalamud.Data; using Dalamud.Data;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -20,6 +21,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public Status Status public Status Status
{ {
get get
@ -29,6 +31,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
} }
} }
[JsonProperty]
private uint statusId; private uint statusId;
internal StatusPayload() { } internal StatusPayload() { }

View file

@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -13,11 +14,13 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
public override PayloadType Type => PayloadType.RawText; public override PayloadType Type => PayloadType.RawText;
// allow modifying the text of existing payloads on the fly // allow modifying the text of existing payloads on the fly
[JsonProperty]
private string text; private string text;
/// <summary> /// <summary>
/// The text contained in this payload. /// The text contained in this payload.
/// This may contain SE's special unicode characters. /// This may contain SE's special unicode characters.
/// </summary> /// </summary>
[JsonIgnore]
public string Text public string Text
{ {
get { return this.text; } get { return this.text; }

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Dalamud.Data; using Dalamud.Data;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -31,6 +32,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public UIColor UIColor public UIColor UIColor
{ {
get get
@ -43,6 +45,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The color key used as a lookup in the UIColor table for this foreground color. /// The color key used as a lookup in the UIColor table for this foreground color.
/// </summary> /// </summary>
[JsonIgnore]
public ushort ColorKey public ushort ColorKey
{ {
get { return this.colorKey; } get { return this.colorKey; }
@ -57,6 +60,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The Red/Green/Blue values for this foreground color, encoded as a typical hex color. /// The Red/Green/Blue values for this foreground color, encoded as a typical hex color.
/// </summary> /// </summary>
[JsonIgnore]
public uint RGB public uint RGB
{ {
get get
@ -65,6 +69,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
} }
} }
[JsonProperty]
private ushort colorKey; private ushort colorKey;
internal UIForegroundPayload() { } internal UIForegroundPayload() { }

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Dalamud.Data; using Dalamud.Data;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling.Payloads namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{ {
@ -31,6 +32,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <remarks> /// <remarks>
/// Value is evaluated lazily and cached. /// Value is evaluated lazily and cached.
/// </remarks> /// </remarks>
[JsonIgnore]
public UIColor UIColor public UIColor UIColor
{ {
get get
@ -43,6 +45,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The color key used as a lookup in the UIColor table for this glow color. /// The color key used as a lookup in the UIColor table for this glow color.
/// </summary> /// </summary>
[JsonIgnore]
public ushort ColorKey public ushort ColorKey
{ {
get { return this.colorKey; } get { return this.colorKey; }
@ -57,6 +60,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
/// <summary> /// <summary>
/// The Red/Green/Blue values for this glow color, encoded as a typical hex color. /// The Red/Green/Blue values for this glow color, encoded as a typical hex color.
/// </summary> /// </summary>
[JsonIgnore]
public uint RGB public uint RGB
{ {
get get
@ -65,6 +69,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
} }
} }
[JsonProperty]
private ushort colorKey; private ushort colorKey;
internal UIGlowPayload() { } internal UIGlowPayload() { }

View file

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Dalamud.Data;
using Dalamud.Game.Chat.SeStringHandling.Payloads; using Dalamud.Game.Chat.SeStringHandling.Payloads;
using Newtonsoft.Json;
namespace Dalamud.Game.Chat.SeStringHandling namespace Dalamud.Game.Chat.SeStringHandling
{ {
@ -38,6 +40,7 @@ namespace Dalamud.Game.Chat.SeStringHandling
/// Creates a new SeString from an ordered list of payloads. /// Creates a new SeString from an ordered list of payloads.
/// </summary> /// </summary>
/// <param name="payloads">The Payload objects to make up this string.</param> /// <param name="payloads">The Payload objects to make up this string.</param>
[JsonConstructor]
public SeString(List<Payload> payloads) public SeString(List<Payload> payloads)
{ {
Payloads = payloads; Payloads = payloads;
@ -100,5 +103,42 @@ namespace Dalamud.Game.Chat.SeStringHandling
return messageBytes.ToArray(); return messageBytes.ToArray();
} }
/// <summary>
/// Serializes the SeString to json
/// </summary>
/// <returns>An json representation of this object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings()
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto
});
}
/// <summary>
/// Creates a SeString from a json. (For testing - not recommended for production use.)
/// </summary>
/// <param name="json">A serialized SeString produced by ToJson() <see cref="ToJson"/></param>
/// <param name="dataManager">An initialized instance of DataManager for Lumina queries.</param>
/// <returns>A SeString initialized with values from the json</returns>
public static SeString FromJson(string json, DataManager dataManager)
{
var s = JsonConvert.DeserializeObject<SeString>(json, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
});
foreach(var payload in s.Payloads)
{
payload.DataResolver = dataManager;
}
return s;
}
} }
} }