Merge pull request #521 from daemitus/ipc3

This commit is contained in:
goaaats 2021-08-31 20:23:43 +02:00 committed by GitHub
commit 559b5e2904
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ using System.Reflection;
using Dalamud.Plugin.Ipc.Exceptions;
using Newtonsoft.Json;
using Serilog;
namespace Dalamud.Plugin.Ipc.Internal
{
@ -111,15 +112,46 @@ namespace Dalamud.Plugin.Ipc.Internal
{
var arg = args[i];
var paramType = paramTypes[i];
if (arg.GetType() != paramType)
var argType = arg.GetType();
if (argType != paramType)
{
// check the inheritance tree
var baseTypes = this.GenerateTypes(argType.BaseType);
if (baseTypes.Any(t => t == paramType))
{
// The source type inherits from the destination type
continue;
}
args[i] = this.ConvertObject(arg, paramType);
}
}
}
private IEnumerable<Type> GenerateTypes(Type type)
{
while (type != null && type != typeof(object))
{
yield return type;
type = type.BaseType;
}
}
private object? ConvertObject(object? obj, Type type)
{
if (type.IsInterface)
var json = JsonConvert.SerializeObject(obj);
try
{
return JsonConvert.DeserializeObject(json, type);
}
catch (Exception)
{
Log.Verbose($"Could not convert {obj.GetType().Name} to {type.Name}, will look for compatible type instead");
}
// If type -> type fails, try to find an object that matches.
var sourceType = obj.GetType();
var fieldNames = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Select(f => f.Name);
@ -140,11 +172,9 @@ namespace Dalamud.Plugin.Ipc.Internal
break;
}
}
}
try
{
var json = JsonConvert.SerializeObject(obj);
return JsonConvert.DeserializeObject(json, type);
}
catch (Exception ex)