From e350b9508d2cd78c528a6bc5011d6a6cf4a3ca72 Mon Sep 17 00:00:00 2001 From: praydog Date: Sat, 26 Oct 2024 11:38:24 -0700 Subject: [PATCH] Lua: Fix case where SSO could break StrProperty arguments --- lua-api/lib/src/ScriptUtility.cpp | 38 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/lua-api/lib/src/ScriptUtility.cpp b/lua-api/lib/src/ScriptUtility.cpp index 6fbd6dd4..7ea8d553 100644 --- a/lua-api/lib/src/ScriptUtility.cpp +++ b/lua-api/lib/src/ScriptUtility.cpp @@ -484,7 +484,7 @@ sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API bool ret_is_array{false}; //std::vector dynamic_data{}; - std::vector dynamic_strings{}; + std::vector> dynamic_strings{}; std::vector> dynamic_object_arrays{}; std::unordered_map prop_to_arg_index{}; // For out parameters @@ -531,19 +531,39 @@ sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API auto& fstr = *(FString*)¶ms[offset]; if (arg_obj.is()) { - dynamic_strings.push_back(arg_obj.as()); + const auto src = arg_obj.as(); + auto buffer = std::make_unique(src.size() + 1); + std::copy(src.begin(), src.end(), buffer.get()); + buffer[src.size()] = L'\0'; - fstr.count = dynamic_strings.back().size() + 1; - fstr.data = (wchar_t*)dynamic_strings.back().c_str(); + fstr.count = src.size() + 1; + fstr.capacity = fstr.count; + fstr.data = buffer.get(); + + dynamic_strings.push_back(std::move(buffer)); } else if (arg_obj.is()) { - dynamic_strings.push_back(::utility::widen(arg_obj.as())); + const auto src = ::utility::widen(arg_obj.as()); + auto buffer = std::make_unique(src.size() + 1); + std::copy(src.begin(), src.end(), buffer.get()); + buffer[src.size()] = L'\0'; + + fstr.count = src.size() + 1; + fstr.capacity = fstr.count; + fstr.data = buffer.get(); - fstr.count = dynamic_strings.back().size() + 1; - fstr.data = (wchar_t*)dynamic_strings.back().c_str(); + dynamic_strings.push_back(std::move(buffer)); } else if (arg_obj.is()) { - dynamic_strings.push_back(arg_obj.as()); + const auto src = std::wstring_view{arg_obj.as()}; + + auto buffer = std::make_unique(src.size() + 1); + std::copy(src.begin(), src.end(), buffer.get()); + buffer[src.size()] = L'\0'; + + fstr.count = src.size() + 1; + fstr.capacity = fstr.count; + fstr.data = buffer.get(); - fstr.count = dynamic_strings.back().size() + 1; + dynamic_strings.push_back(std::move(buffer)); } else { throw sol::error("Invalid argument type for FString"); }