Skip to content

Commit

Permalink
fix: make FString a local lua param
Browse files Browse the repository at this point in the history
This fixes some issues related to FString lifetime causing it to become
garbage data, FString should be stored by value instead.

Credits: @Yangff
  • Loading branch information
localcc authored and Buckminsterfullerene02 committed Mar 7, 2024
1 parent 79e017c commit 21b1717
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion UE4SS/include/LuaType/LuaFString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace RC::LuaType
return "FString";
}
};
class FString : public RemoteObjectBase<Unreal::FString, FStringName>
class FString : public LocalObjectBase<Unreal::FString, FStringName>
{
private:
explicit FString(Unreal::FString* object);
Expand Down
12 changes: 6 additions & 6 deletions UE4SS/src/LuaType/LuaFString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace RC::LuaType
{
FString::FString(Unreal::FString* object) : RemoteObjectBase<Unreal::FString, FStringName>(object)
FString::FString(Unreal::FString* object) : LocalObjectBase<Unreal::FString, FStringName>(std::move(Unreal::FString(*object)))
{
}

Expand All @@ -20,7 +20,7 @@ namespace RC::LuaType
if (lua.is_nil(-1))
{
lua.discard_value(-1);
LuaMadeSimple::Type::RemoteObject<Unreal::FString>::construct(lua, lua_object);
lua.prepare_new_table();
setup_metamethods(lua_object);
setup_member_functions<LuaMadeSimple::Type::IsFinal::Yes>(table);
lua.new_metatable<LuaType::FString>(metatable_name, lua_object.get_metamethods());
Expand Down Expand Up @@ -52,9 +52,9 @@ namespace RC::LuaType
auto FString::setup_member_functions(const LuaMadeSimple::Lua::Table& table) -> void
{
table.add_pair("ToString", [](const LuaMadeSimple::Lua& lua) -> int {
const auto& lua_object = lua.get_userdata<LuaType::FString>();
auto& lua_object = lua.get_userdata<LuaType::FString>();

const wchar_t* string_data = lua_object.get_remote_cpp_object()->GetCharArray();
const wchar_t* string_data = lua_object.get_local_cpp_object().GetCharArray();
if (string_data)
{
lua.set_string(to_string(string_data));
Expand All @@ -68,9 +68,9 @@ namespace RC::LuaType
});

table.add_pair("Clear", [](const LuaMadeSimple::Lua& lua) -> int {
const auto& lua_object = lua.get_userdata<LuaType::FString>();
auto& lua_object = lua.get_userdata<LuaType::FString>();

lua_object.get_remote_cpp_object()->Clear();
lua_object.get_local_cpp_object().Clear();

return 0;
});
Expand Down
2 changes: 1 addition & 1 deletion UE4SS/src/LuaType/LuaUObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ namespace RC::LuaType
else if (params.lua.is_userdata())
{
auto& rhs = params.lua.get_userdata<LuaType::FString>();
string->SetCharArray(rhs.get_remote_cpp_object()->GetCharTArray());
string->SetCharArray(rhs.get_local_cpp_object().GetCharTArray());
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions assets/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Fixed BPModLoaderMod giving "bad conversion" errors.
### UHT Dumper

### Lua API
Fixed FString use after free.

Fixed the "IterateGameDirectories" global function throwing "bad conversion" errors.

Fixed `FText` not working as a parameter (in `RegisterHook`, etc.).
Expand Down

0 comments on commit 21b1717

Please sign in to comment.