Skip to content

Commit

Permalink
.NET: Fix string interaction in older games
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 30, 2024
1 parent 361aeef commit 0899f7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion csharp-api/REFrameworkNET/ManagedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ namespace REFrameworkNET {
break;
}

const auto offset = field_type->IsValueType() ? field_type->GetField("_firstChar")->GetOffsetFromFieldPtr() : field_type->GetField("_firstChar")->GetOffsetFromBase();
const auto firstCharField = field_type->GetField("_firstChar");
uint32_t offset = 0;

if (firstCharField != nullptr) {
offset = field_type->IsValueType() ? firstCharField->GetOffsetFromFieldPtr() : firstCharField->GetOffsetFromBase();
} else {
const auto fieldOffset = *(uint32_t*)(*(uintptr_t*)strObject - sizeof(void*));
offset = fieldOffset + 4;
}

wchar_t* chars = (wchar_t*)((uintptr_t)strObject + offset);
result = gcnew System::String(chars);
Expand Down
10 changes: 9 additions & 1 deletion csharp-api/REFrameworkNET/Method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ bool Method::HandleInvokeMember_Internal(System::Object^ obj, System::String^ me
// Maybe don't create the GC version and just use the native one?
auto strObject = gcnew REFrameworkNET::ManagedObject((::REFrameworkManagedObjectHandle)tempResult->QWord);
auto strType = strObject->GetTypeDefinition();
const auto offset = strType->GetField("_firstChar")->GetOffsetFromBase();
const auto firstCharField = strType->GetField("_firstChar");
uint32_t offset = 0;

if (firstCharField != nullptr) {
offset = strType->GetField("_firstChar")->GetOffsetFromBase();
} else {
const auto fieldOffset = *(uint32_t*)(*(uintptr_t*)tempResult->QWord - sizeof(void*));
offset = fieldOffset + 4;
}

wchar_t* chars = (wchar_t*)((uintptr_t)strObject->Ptr() + offset);
result = gcnew System::String(chars);
Expand Down

0 comments on commit 0899f7f

Please sign in to comment.