forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCW.cpp
More file actions
166 lines (145 loc) · 7.09 KB
/
CCW.cpp
File metadata and controls
166 lines (145 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "il2cpp-config.h"
#include "il2cpp-object-internals.h"
#include "il2cpp-class-internals.h"
#include "vm/Object.h"
#include "vm/CCW.h"
#include "vm/Class.h"
#include "vm/CachedCCWBase.h"
#include "vm/Exception.h"
#include "vm/MetadataCache.h"
#include "vm/Method.h"
#include "vm/RCW.h"
#include "vm/Runtime.h"
#include "vm/ScopedThreadAttacher.h"
#include "vm/String.h"
namespace il2cpp
{
namespace vm
{
struct ManagedObject : CachedCCWBase<ManagedObject>
{
inline ManagedObject(Il2CppObject* obj) :
CachedCCWBase<ManagedObject>(obj)
{
}
virtual il2cpp_hresult_t STDCALL QueryInterface(const Il2CppGuid& iid, void** object) IL2CPP_OVERRIDE
{
if (::memcmp(&iid, &Il2CppIUnknown::IID, sizeof(Il2CppGuid)) == 0
|| ::memcmp(&iid, &Il2CppIInspectable::IID, sizeof(Il2CppGuid)) == 0
|| ::memcmp(&iid, &Il2CppIAgileObject::IID, sizeof(Il2CppGuid)) == 0)
{
*object = GetIdentity();
AddRefImpl();
return IL2CPP_S_OK;
}
if (::memcmp(&iid, &Il2CppIManagedObjectHolder::IID, sizeof(Il2CppGuid)) == 0)
{
*object = static_cast<Il2CppIManagedObjectHolder*>(this);
AddRefImpl();
return IL2CPP_S_OK;
}
if (::memcmp(&iid, &Il2CppIMarshal::IID, sizeof(Il2CppGuid)) == 0)
{
*object = static_cast<Il2CppIMarshal*>(this);
AddRefImpl();
return IL2CPP_S_OK;
}
if (::memcmp(&iid, &Il2CppIWeakReferenceSource::IID, sizeof(Il2CppGuid)) == 0)
{
*object = static_cast<Il2CppIWeakReferenceSource*>(this);
AddRefImpl();
return IL2CPP_S_OK;
}
*object = NULL;
return IL2CPP_E_NOINTERFACE;
}
};
Il2CppIUnknown* CCW::CreateCCW(Il2CppObject* obj)
{
// check for ccw create function, which is implemented by objects that implement COM or Windows Runtime interfaces
const Il2CppInteropData* interopData = obj->klass->interopData;
if (interopData != NULL)
{
const CreateCCWFunc createCcw = interopData->createCCWFunction;
if (createCcw != NULL)
return createCcw(obj);
}
// otherwise create generic ccw object that "only" implements IUnknown, IMarshal, IInspectable, IAgileObject and IManagedObjectHolder interfaces
void* memory = utils::Memory::Malloc(sizeof(ManagedObject));
if (memory == NULL)
Exception::RaiseOutOfMemoryException();
return static_cast<Il2CppIManagedObjectHolder*>(new(memory) ManagedObject(obj));
}
Il2CppObject* CCW::Unpack(Il2CppIUnknown* unknown)
{
Il2CppIManagedObjectHolder* managedHolder;
il2cpp_hresult_t hr = unknown->QueryInterface(Il2CppIManagedObjectHolder::IID, reinterpret_cast<void**>(&managedHolder));
Exception::RaiseIfFailed(hr, true);
Il2CppObject* instance = managedHolder->GetManagedObject();
managedHolder->Release();
IL2CPP_ASSERT(instance);
return instance;
}
static Il2CppString* ValueToStringFallbackToEmpty(Il2CppObject* value)
{
Il2CppClass* klass = il2cpp::vm::Object::GetClass(value);
const MethodInfo* toStringMethod = il2cpp::vm::Class::GetMethodFromName(klass, "ToString", 0);
// check if the method we are to call is expecting a value type 'this' parameter or not.
// handles edge case of enums where the 'value' is SomeEnum but the ToString method is Enum::ToString.
// In this case, a boxed 'this' parameter is expected even though the object value is a valuetype.
bool isValueTypeMethod = il2cpp::vm::Class::IsValuetype(il2cpp::vm::Method::GetClass(toStringMethod));
Il2CppException* exception = NULL;
Il2CppString* result = (Il2CppString*)il2cpp::vm::Runtime::Invoke(toStringMethod, isValueTypeMethod ? Object::Unbox(value) : value, NULL, &exception);
if (exception != NULL)
return String::Empty();
return result;
}
static il2cpp_hresult_t HandleInvalidIPropertyConversionImpl(const std::string& exceptionMessage)
{
ScopedThreadAttacher scopedThreadAttacher; // Make sure we're attached before we create exceptions (aka allocate managed memory)
Il2CppException* exception = Exception::GetInvalidCastException(exceptionMessage.c_str());
Exception::PrepareExceptionForThrow(exception);
Exception::StoreExceptionInfo(exception, ValueToStringFallbackToEmpty(exception));
return exception->hresult;
}
il2cpp_hresult_t CCW::HandleInvalidIPropertyConversion(const char* fromType, const char* toType)
{
std::string message = il2cpp::utils::StringUtils::Printf("Object in an IPropertyValue is of type '%s', which cannot be converted to a '%s'.", fromType, toType);
return HandleInvalidIPropertyConversionImpl(message);
}
il2cpp_hresult_t CCW::HandleInvalidIPropertyConversion(Il2CppObject* value, const char* fromType, const char* toType)
{
Il2CppString* valueStr = ValueToStringFallbackToEmpty(value);
std::string message = il2cpp::utils::StringUtils::Printf(
"Object in an IPropertyValue is of type '%s' with value '%s', which cannot be converted to a '%s'.",
fromType,
utils::StringUtils::Utf16ToUtf8(valueStr->chars, valueStr->length).c_str(),
toType);
return HandleInvalidIPropertyConversionImpl(message);
}
il2cpp_hresult_t CCW::HandleInvalidIPropertyArrayConversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
{
std::string message = il2cpp::utils::StringUtils::Printf(
"Object in an IPropertyValue is of type '%s' which cannot be converted to a '%s[]' due to array element '%d': Object in an IPropertyValue is of type '%s', which cannot be converted to a '%s'.",
fromArrayType,
toElementType,
static_cast<int>(index),
fromElementType,
toElementType);
return HandleInvalidIPropertyConversionImpl(message);
}
il2cpp_hresult_t CCW::HandleInvalidIPropertyArrayConversion(Il2CppObject* value, const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
{
Il2CppString* valueStr = ValueToStringFallbackToEmpty(value);
std::string message = il2cpp::utils::StringUtils::Printf(
"Object in an IPropertyValue is of type '%s' which cannot be converted to a '%s[]' due to array element '%d': Object in an IPropertyValue is of type '%s' with value '%s', which cannot be converted to a '%s'.",
fromArrayType,
toElementType,
static_cast<int>(index),
fromElementType,
utils::StringUtils::Utf16ToUtf8(valueStr->chars, valueStr->length).c_str(),
toElementType);
return HandleInvalidIPropertyConversionImpl(message);
}
} /* namespace vm */
} /* namespace il2cpp */