mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-18 22:07:44 +01:00
Merge pull request #521 from daemitus/ipc3
This commit is contained in:
commit
559b5e2904
1 changed files with 49 additions and 19 deletions
|
|
@ -5,6 +5,7 @@ using System.Reflection;
|
||||||
|
|
||||||
using Dalamud.Plugin.Ipc.Exceptions;
|
using Dalamud.Plugin.Ipc.Exceptions;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Dalamud.Plugin.Ipc.Internal
|
namespace Dalamud.Plugin.Ipc.Internal
|
||||||
{
|
{
|
||||||
|
|
@ -111,40 +112,69 @@ namespace Dalamud.Plugin.Ipc.Internal
|
||||||
{
|
{
|
||||||
var arg = args[i];
|
var arg = args[i];
|
||||||
var paramType = paramTypes[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);
|
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)
|
private object? ConvertObject(object? obj, Type type)
|
||||||
{
|
{
|
||||||
if (type.IsInterface)
|
var json = JsonConvert.SerializeObject(obj);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var sourceType = obj.GetType();
|
return JsonConvert.DeserializeObject(json, type);
|
||||||
var fieldNames = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
}
|
||||||
.Select(f => f.Name);
|
catch (Exception)
|
||||||
var propNames = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
{
|
||||||
.Select(p => p.Name);
|
Log.Verbose($"Could not convert {obj.GetType().Name} to {type.Name}, will look for compatible type instead");
|
||||||
|
}
|
||||||
|
|
||||||
var assignableTypes = type.Assembly.GetTypes()
|
// If type -> type fails, try to find an object that matches.
|
||||||
.Where(t => type.IsAssignableFrom(t) && type != t)
|
var sourceType = obj.GetType();
|
||||||
.ToArray();
|
var fieldNames = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||||
|
.Select(f => f.Name);
|
||||||
|
var propNames = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||||
|
.Select(p => p.Name);
|
||||||
|
|
||||||
foreach (var assignableType in assignableTypes)
|
var assignableTypes = type.Assembly.GetTypes()
|
||||||
|
.Where(t => type.IsAssignableFrom(t) && type != t)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var assignableType in assignableTypes)
|
||||||
|
{
|
||||||
|
var matchesFields = assignableType.GetFields().All(f => fieldNames.Contains(f.Name));
|
||||||
|
var matchesProps = assignableType.GetProperties().All(p => propNames.Contains(p.Name));
|
||||||
|
if (matchesFields && matchesProps)
|
||||||
{
|
{
|
||||||
var matchesFields = assignableType.GetFields().All(f => fieldNames.Contains(f.Name));
|
type = assignableType;
|
||||||
var matchesProps = assignableType.GetProperties().All(p => propNames.Contains(p.Name));
|
break;
|
||||||
if (matchesFields && matchesProps)
|
|
||||||
{
|
|
||||||
type = assignableType;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var json = JsonConvert.SerializeObject(obj);
|
|
||||||
return JsonConvert.DeserializeObject(json, type);
|
return JsonConvert.DeserializeObject(json, type);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue