From 4aefe1c5a36fd602931379f53ef53f07084e36ee Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Tue, 8 Sep 2020 04:33:27 +1000 Subject: [PATCH] a few tidy ups --- src/CachedObjects/Object/CacheDictionary.cs | 46 ++++++++++----------- src/CachedObjects/Other/CacheOther.cs | 2 +- src/UnstripFixes/GUIUnstrip.cs | 10 +---- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/CachedObjects/Object/CacheDictionary.cs b/src/CachedObjects/Object/CacheDictionary.cs index 868e090e..7623d898 100644 --- a/src/CachedObjects/Object/CacheDictionary.cs +++ b/src/CachedObjects/Object/CacheDictionary.cs @@ -56,10 +56,6 @@ private IDictionary Il2CppDictionaryToMono() { // note: "ValueType" is the Dictionary itself, TypeOfValues is the 'Dictionary.Values' type. - // make generic dictionary from key and value type - var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>) - .MakeGenericType(TypeOfKeys, TypeOfValues)); - // get keys and values var keys = ValueType.GetProperty("Keys") .GetValue(Value); var values = ValueType.GetProperty("Values").GetValue(Value); @@ -68,27 +64,15 @@ private IDictionary Il2CppDictionaryToMono() var keyList = new List(); var valueList = new List(); - // get keys enumerator and store keys - var keyEnumerator = keys.GetType().GetMethod("GetEnumerator").Invoke(keys, null); - var keyCollectionType = keyEnumerator.GetType(); - var keyMoveNext = keyCollectionType.GetMethod("MoveNext"); - var keyCurrent = keyCollectionType.GetProperty("Current"); - while ((bool)keyMoveNext.Invoke(keyEnumerator, null)) - { - keyList.Add(keyCurrent.GetValue(keyEnumerator)); - } + // store entries with reflection + EnumerateWithReflection(keys, keyList); + EnumerateWithReflection(values, valueList); - // get values enumerator and store values - var valueEnumerator = values.GetType().GetMethod("GetEnumerator").Invoke(values, null); - var valueCollectionType = valueEnumerator.GetType(); - var valueMoveNext = valueCollectionType.GetMethod("MoveNext"); - var valueCurrent = valueCollectionType.GetProperty("Current"); - while ((bool)valueMoveNext.Invoke(valueEnumerator, null)) - { - valueList.Add(valueCurrent.GetValue(valueEnumerator)); - } + // make actual mono dictionary + var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>) + .MakeGenericType(TypeOfKeys, TypeOfValues)); - // finally iterate into actual dictionary + // finally iterate into dictionary for (int i = 0; i < keyList.Count; i++) { dict.Add(keyList[i], valueList[i]); @@ -97,6 +81,22 @@ private IDictionary Il2CppDictionaryToMono() return dict; } + private void EnumerateWithReflection(object collection, List list) + { + // invoke GetEnumerator + var enumerator = collection.GetType().GetMethod("GetEnumerator").Invoke(collection, null); + // get the type of it + var enumeratorType = enumerator.GetType(); + // reflect MoveNext and Current + var moveNext = enumeratorType.GetMethod("MoveNext"); + var current = enumeratorType.GetProperty("Current"); + // iterate + while ((bool)moveNext.Invoke(enumerator, null)) + { + list.Add(current.GetValue(enumerator)); + } + } + private void GetGenericArguments() { if (this.MemInfo != null) diff --git a/src/CachedObjects/Other/CacheOther.cs b/src/CachedObjects/Other/CacheOther.cs index b77981b5..58aceb90 100644 --- a/src/CachedObjects/Other/CacheOther.cs +++ b/src/CachedObjects/Other/CacheOther.cs @@ -50,7 +50,7 @@ public override void DrawValue(Rect window, float width) } GUI.skin.button.alignment = TextAnchor.MiddleLeft; - if (GUILayout.Button("" + label + "", new GUILayoutOption[] { GUILayout.Width(width) })) + if (GUILayout.Button("" + label + "", new GUILayoutOption[] { GUILayout.Width(width - 15) })) { WindowManager.InspectObject(Value, out bool _); } diff --git a/src/UnstripFixes/GUIUnstrip.cs b/src/UnstripFixes/GUIUnstrip.cs index 6067a112..84fac682 100644 --- a/src/UnstripFixes/GUIUnstrip.cs +++ b/src/UnstripFixes/GUIUnstrip.cs @@ -41,8 +41,7 @@ private static GenericStack ScrollStack } } private static PropertyInfo m_scrollViewStatesInfo; - - // ======= public methods ======= // + public static Rect GetLastRect() { @@ -73,7 +72,6 @@ public static Vector2 BeginScrollView(Vector2 scroll, params GUILayoutOption[] o catch { ScrollFailed = true; - return scroll; } } @@ -86,10 +84,8 @@ public static Vector2 BeginScrollView(Vector2 scroll, params GUILayoutOption[] o } catch (Exception e) { - MelonLogger.Log("Exception on GUIUnstrip.BeginScrollView_ImplLayout: " + e.GetType() + ", " + e.Message + "\r\n" + e.StackTrace); - + MelonLogger.Log("Exception on manual BeginScrollView: " + e.GetType() + ", " + e.Message + "\r\n" + e.StackTrace); ManualUnstripFailed = true; - return scroll; } } @@ -113,8 +109,6 @@ public static void EndScrollView(bool handleScrollWheel = true) } } - // ======= private methods ======= // - private static Vector2 BeginScrollView_ImplLayout(Vector2 scrollPosition, bool alwaysShowHorizontal, bool alwaysShowVertical, GUIStyle horizontalScrollbar, GUIStyle verticalScrollbar, GUIStyle background, params GUILayoutOption[] options) {