Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 6ca117b

Browse files
committed
Fix strings boxed as Il2CppSystem.Objects
1 parent 113f2fd commit 6ca117b

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

src/Core/Runtime/Il2Cpp/Il2CppReflection.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,24 @@ public static Type GetMonoType(CppType cppType)
154154
/// <returns>The object, as the type (or a normal C# object) if successful or the input value if not.</returns>
155155
public static object Il2CppCast(object obj, Type castTo)
156156
{
157-
if (!(obj is Il2CppSystem.Object ilObj))
157+
if (!(obj is Il2CppSystem.Object cppObj))
158158
return obj;
159159

160160
if (!Il2CppTypeNotNull(castTo, out IntPtr castToPtr))
161161
return obj;
162162

163-
IntPtr castFromPtr = il2cpp_object_get_class(ilObj.Pointer);
163+
IntPtr castFromPtr = il2cpp_object_get_class(cppObj.Pointer);
164164

165165
if (!il2cpp_class_is_assignable_from(castToPtr, castFromPtr))
166166
return null;
167167

168168
if (RuntimeSpecificsStore.IsInjected(castToPtr))
169-
return UnhollowerBaseLib.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(ilObj.Pointer);
169+
return UnhollowerBaseLib.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(cppObj.Pointer);
170170

171-
return Activator.CreateInstance(castTo, ilObj.Pointer);
171+
if (castTo == typeof(string))
172+
return cppObj.ToString();
173+
174+
return Activator.CreateInstance(castTo, cppObj.Pointer);
172175
}
173176

174177
/// <summary>
@@ -319,6 +322,14 @@ internal static bool LoadModuleInternal(string fullPath)
319322
return false;
320323
}
321324

325+
public override void BoxStringToType(ref object value, Type castTo)
326+
{
327+
if (castTo == typeof(Il2CppSystem.String))
328+
value = (Il2CppSystem.String)(value as string);
329+
else
330+
value = (Il2CppSystem.Object)(value as string);
331+
}
332+
322333
// ~~~~~~~~~~ not used ~~~~~~~~~~~~
323334

324335
// cached il2cpp unbox methods

src/Core/Runtime/Mono/MonoReflection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public override bool LoadModule(string module)
2727

2828
public override string ProcessTypeNameInString(Type type, string theString, ref string typeName)
2929
=> theString;
30+
31+
// not necessary
32+
public override void BoxStringToType(ref object _string, Type castTo) { }
3033
}
3134
}
3235

src/Core/Runtime/ReflectionProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public ReflectionProvider()
2525
public abstract string ProcessTypeNameInString(Type type, string theString, ref string typeName);
2626

2727
public abstract bool LoadModule(string module);
28+
29+
public abstract void BoxStringToType(ref object _string, Type castTo);
2830
}
2931
}

src/Core/TestClass.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
namespace UnityExplorer
77
{
8-
#if CPP
98
public static class TestClass
109
{
10+
#if CPP
11+
public static string testStringOne = "Test";
12+
public static Il2CppSystem.Object testStringTwo = "string boxed as cpp object";
13+
public static Il2CppSystem.String testStringThree = "string boxed as cpp string";
14+
public static string nullString = null;
15+
1116
public static Il2CppSystem.Collections.Hashtable testHashset;
1217

1318
static TestClass()
@@ -17,6 +22,6 @@ static TestClass()
1722
testHashset.Add("key2", "itemTwo");
1823
testHashset.Add("key3", "itemThree");
1924
}
20-
}
2125
#endif
26+
}
2227
}

src/ExplorerCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
using UnityExplorer.Core.Input;
66
using UnityExplorer.Core.Runtime;
77
using UnityExplorer.UI;
8+
using UnityExplorer.UI.Inspectors;
89
using UnityExplorer.UI.Main;
910

1011
namespace UnityExplorer
1112
{
1213
public class ExplorerCore
1314
{
1415
public const string NAME = "UnityExplorer";
15-
public const string VERSION = "3.3.5";
16+
public const string VERSION = "3.3.6";
1617
public const string AUTHOR = "Sinai";
1718
public const string GUID = "com.sinai.unityexplorer";
1819

src/UI/InteractiveValues/InteractiveString.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using UnityExplorer.UI;
1010
using UnityExplorer.UI.Utility;
1111
using UnityExplorer.UI.CacheObject;
12+
using UnityExplorer.Core.Runtime;
1213

1314
namespace UnityExplorer.UI.InteractiveValues
1415
{
@@ -27,8 +28,8 @@ public override void OnValueUpdated()
2728
// strings boxed as Il2CppSystem.Objects can behave weirdly.
2829
// GetActualType will find they are a string, but if its boxed
2930
// then we need to unbox it like this...
30-
if (!(Value is string))
31-
Value = ((Il2CppSystem.Object)Value).ToString();
31+
if (!(Value is string) && Value is Il2CppSystem.Object cppobj)
32+
Value = cppobj.ToString();
3233
#endif
3334

3435
base.OnValueUpdated();
@@ -96,10 +97,18 @@ public override void RefreshUIForValue()
9697
m_labelLayout.flexibleWidth = 0;
9798
}
9899

99-
internal void OnApplyClicked()
100+
internal void SetValueFromInput()
100101
{
101102
Value = m_valueInput.text;
103+
104+
if (!typeof(string).IsAssignableFrom(Owner.FallbackType))
105+
ReflectionProvider.Instance.BoxStringToType(ref Value, Owner.FallbackType);
106+
102107
Owner.SetValue();
108+
109+
// revert back to string now
110+
OnValueUpdated();
111+
103112
RefreshUIForValue();
104113
}
105114

@@ -170,7 +179,7 @@ public override void ConstructSubcontent()
170179

171180
if (Owner.CanWrite)
172181
{
173-
var apply = UIFactory.CreateButton(groupObj, "ApplyButton", "Apply", OnApplyClicked, new Color(0.2f, 0.2f, 0.2f));
182+
var apply = UIFactory.CreateButton(groupObj, "ApplyButton", "Apply", SetValueFromInput, new Color(0.2f, 0.2f, 0.2f));
174183
UIFactory.SetLayoutElement(apply.gameObject, minWidth: 50, minHeight: 25, flexibleWidth: 0);
175184
}
176185
else

0 commit comments

Comments
 (0)