From 74f0007280469a7c5e6c1516759bbb4515d7341f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 25 Apr 2020 23:30:17 +0200 Subject: [PATCH] fixed textinputwrapper not working on core --- MLEM/Misc/TextInputWrapper.cs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/MLEM/Misc/TextInputWrapper.cs b/MLEM/Misc/TextInputWrapper.cs index d894ad7..2316a3b 100644 --- a/MLEM/Misc/TextInputWrapper.cs +++ b/MLEM/Misc/TextInputWrapper.cs @@ -26,8 +26,8 @@ namespace MLEM.Misc { public class DesktopGl : TextInputWrapper { - private PropertyInfo key; - private PropertyInfo character; + private MemberInfo key; + private MemberInfo character; private readonly Action> addListener; public DesktopGl(Action> addListener) { @@ -40,14 +40,33 @@ namespace MLEM.Misc { public override void AddListener(GameWindow window, TextInputCallback callback) { this.addListener(window, (sender, args) => { + // the old versions of DesktopGL use a property here, while the + // core version uses a field. So much for "no breaking changes" if (this.key == null) - this.key = args.GetType().GetProperty("Key"); + this.key = GetMember(args, "Key"); if (this.character == null) - this.character = args.GetType().GetProperty("Character"); - callback.Invoke(sender, (Keys) this.key.GetValue(args), (char) this.character.GetValue(args)); + this.character = GetMember(args, "Character"); + callback.Invoke(sender, GetValue(this.key, args), GetValue(this.character, args)); }); } + private static MemberInfo GetMember(object args, string name) { + var ret = args.GetType().GetProperty(name); + if (ret != null) + return ret; + return args.GetType().GetField(name); + } + + private static U GetValue(MemberInfo member, object args) { + switch (member) { + case PropertyInfo p: + return (U) p.GetValue(args); + case FieldInfo f: + return (U) f.GetValue(args); + } + throw new ArgumentException(); + } + } public class Mobile : TextInputWrapper {