2023-09-14 08:49:24 -04:00
|
|
|
#include <Geode/c++stl/gdstdlib.hpp>
|
2023-12-23 07:44:29 -05:00
|
|
|
#include "../../c++stl/string-impl.hpp"
|
2023-09-14 08:49:24 -04:00
|
|
|
|
2023-12-23 10:02:13 -05:00
|
|
|
#if defined(GEODE_IS_ANDROID32)
|
|
|
|
|
2024-01-01 13:14:00 -05:00
|
|
|
static auto constexpr NEW_SYM = "_Znwj";
|
|
|
|
static constexpr uintptr_t STRING_EMPTY = 0xaa1c48 - 0x10000 - 0xc; // the internal struct size
|
2023-12-23 10:02:13 -05:00
|
|
|
|
|
|
|
#elif defined(GEODE_IS_ANDROID64)
|
|
|
|
|
2024-01-01 13:14:00 -05:00
|
|
|
static auto constexpr NEW_SYM = "_Znwm";
|
|
|
|
static constexpr uintptr_t STRING_EMPTY = 0x12d8568 - 0x100000 - 0x18; // the internal struct size
|
2023-12-23 10:02:13 -05:00
|
|
|
|
|
|
|
#endif
|
2023-09-14 08:49:24 -04:00
|
|
|
|
2024-01-01 13:14:00 -05:00
|
|
|
static auto constexpr DELETE_SYM = "_ZdlPv";
|
|
|
|
|
2023-12-20 11:58:47 -05:00
|
|
|
// 2.2 addition
|
|
|
|
// zmx please fix this
|
|
|
|
|
2024-01-01 13:14:00 -05:00
|
|
|
static void* getLibHandle() {
|
|
|
|
static void* handle = dlopen("libcocos2dcpp.so", RTLD_LAZY | RTLD_NOLOAD);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2024-01-12 21:26:24 -05:00
|
|
|
namespace geode::base {
|
|
|
|
uintptr_t get() {
|
|
|
|
static std::uintptr_t basePtr = 0u;
|
|
|
|
if (basePtr == 0u) {
|
|
|
|
auto handle = getLibHandle();
|
|
|
|
|
|
|
|
// JNI_OnLoad is present on all versions of GD
|
|
|
|
auto sym = dlsym(handle, "JNI_OnLoad");
|
|
|
|
assert(sym != nullptr);
|
|
|
|
|
|
|
|
Dl_info p;
|
|
|
|
auto dlAddrRes = dladdr(sym, &p);
|
|
|
|
assert(dlAddrRes != 0);
|
|
|
|
|
|
|
|
basePtr = reinterpret_cast<std::uintptr_t>(p.dli_fbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return basePtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-13 08:43:53 -05:00
|
|
|
void* gd::operatorNew(size_t size) {
|
2024-01-01 13:14:00 -05:00
|
|
|
static auto fnPtr = reinterpret_cast<void*(*)(size_t)>(dlsym(getLibHandle(), NEW_SYM));
|
|
|
|
return fnPtr(size);
|
|
|
|
}
|
|
|
|
|
2024-01-13 08:43:53 -05:00
|
|
|
void gd::operatorDelete(void* ptr) {
|
2024-01-01 13:14:00 -05:00
|
|
|
static auto fnPtr = reinterpret_cast<void(*)(void*)>(dlsym(getLibHandle(), DELETE_SYM));
|
|
|
|
return fnPtr(ptr);
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:09:58 -05:00
|
|
|
namespace geode::stl {
|
|
|
|
static inline auto emptyInternalString() {
|
2023-12-23 08:36:24 -05:00
|
|
|
return reinterpret_cast<StringData::Internal*>(
|
2023-12-23 10:02:13 -05:00
|
|
|
geode::base::get() + STRING_EMPTY + sizeof(StringData::Internal)
|
2023-12-22 16:09:58 -05:00
|
|
|
);
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
void StringImpl::setEmpty() {
|
|
|
|
data.m_data = emptyInternalString();
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
void StringImpl::free() {
|
|
|
|
if (data.m_data == nullptr || data.m_data == emptyInternalString()) return;
|
2024-01-01 13:14:00 -05:00
|
|
|
|
|
|
|
if (data.m_data[-1].m_refcount <= 0) {
|
2024-01-13 08:43:53 -05:00
|
|
|
gd::operatorDelete(&data.m_data[-1]);
|
2024-01-01 13:14:00 -05:00
|
|
|
data.m_data = nullptr;
|
|
|
|
} else {
|
|
|
|
--data.m_data[-1].m_refcount;
|
|
|
|
}
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
char* StringImpl::getStorage() {
|
|
|
|
return reinterpret_cast<char*>(data.m_data);
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
2023-12-23 09:10:36 -05:00
|
|
|
// TODO: add a copyFrom(string const&) to take advantage
|
|
|
|
// of gnustl refcounted strings
|
2023-12-23 08:36:24 -05:00
|
|
|
void StringImpl::setStorage(const std::string_view str) {
|
2023-12-22 16:09:58 -05:00
|
|
|
this->free();
|
2023-12-23 00:59:45 -05:00
|
|
|
|
|
|
|
if (str.size() == 0) {
|
|
|
|
this->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
StringData::Internal internal;
|
2023-12-23 00:59:45 -05:00
|
|
|
internal.m_size = str.size();
|
|
|
|
internal.m_capacity = str.size();
|
|
|
|
internal.m_refcount = 0;
|
|
|
|
|
2024-01-01 13:14:00 -05:00
|
|
|
// use char* so we can do easy pointer arithmetic with it
|
2024-01-13 08:43:53 -05:00
|
|
|
auto* buffer = static_cast<char*>(gd::operatorNew(str.size() + 1 + sizeof(internal)));
|
2023-12-23 08:36:24 -05:00
|
|
|
std::memcpy(buffer, &internal, sizeof(internal));
|
|
|
|
std::memcpy(buffer + sizeof(internal), str.data(), str.size());
|
|
|
|
data.m_data = reinterpret_cast<StringData::Internal*>(buffer + sizeof(internal));
|
2024-01-01 13:14:00 -05:00
|
|
|
|
|
|
|
this->getStorage()[str.size()] = 0;
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
size_t StringImpl::getSize() {
|
|
|
|
return data.m_data[-1].m_size;
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
2023-12-23 08:36:24 -05:00
|
|
|
void StringImpl::setSize(size_t size) {
|
2023-12-22 16:09:58 -05:00
|
|
|
// TODO: implement this, remember its copy-on-write...
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
|
2023-12-23 08:36:24 -05:00
|
|
|
size_t StringImpl::getCapacity() {
|
|
|
|
return data.m_data[-1].m_capacity;
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
2023-12-23 08:36:24 -05:00
|
|
|
void StringImpl::setCapacity(size_t cap) {
|
2023-12-22 16:09:58 -05:00
|
|
|
// TODO: implement this, remember its copy-on-write...
|
2023-09-14 08:49:24 -04:00
|
|
|
}
|
|
|
|
}
|