-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ipc: add custom serializer for IGameObject
fixes #1924
- Loading branch information
Showing
3 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Dalamud/Plugin/Ipc/Internal/Converters/GameObjectConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.IO; | ||
|
||
using Dalamud.Game.ClientState.Objects; | ||
using Dalamud.Game.ClientState.Objects.Types; | ||
using Dalamud.Utility; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Dalamud.Plugin.Ipc.Internal.Converters; | ||
|
||
/// <summary> | ||
/// JSON converter for IGameObject and its derived types. | ||
/// </summary> | ||
internal sealed class GameObjectConverter : JsonConverter<IGameObject> | ||
{ | ||
/// <inheritdoc/> | ||
public override void WriteJson(JsonWriter writer, IGameObject? value, JsonSerializer serializer) => | ||
writer.WriteValue(value?.Address.ToString()); | ||
|
||
/// <inheritdoc/> | ||
public override IGameObject? ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
IGameObject? existingValue, | ||
bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
return null; | ||
|
||
if (reader.TokenType != JsonToken.String) | ||
throw new InvalidDataException("String is expected."); | ||
|
||
if (!nint.TryParse(reader.Value as string, out var v)) | ||
throw new InvalidDataException("Could not parse address."); | ||
|
||
if (!ThreadSafety.IsMainThread) | ||
throw new InvalidOperationException("Cannot send GameObjects from non-main thread over IPC."); | ||
|
||
var ot = Service<ObjectTable>.Get(); | ||
foreach (var go in ot) | ||
{ | ||
if (go.Address == v) | ||
return go; | ||
} | ||
|
||
return ot.CreateObjectReference(v); | ||
} | ||
} |