geode/loader/src/platform/android/gdstdlib.cpp

98 lines
3.2 KiB
C++
Raw Normal View History

#include <Geode/c++stl/gdstdlib.hpp>
2023-12-23 07:44:29 -05:00
#include "../../c++stl/string-impl.hpp"
2023-12-23 10:02:13 -05:00
#ifdef GEODE_IS_ANDROID
#if defined(GEODE_IS_ANDROID32)
static constexpr ptrdiff_t MENULAYER_SCENE = 0x309068 - 0x10000;
static constexpr ptrdiff_t STRING_EMPTY = 0xaa1c3c - 0x10000;
2023-12-24 23:11:17 -05:00
static constexpr ptrdiff_t STRING_DTOR = 0x7514c8 - 0x10000 + 1;
2023-12-23 10:02:13 -05:00
static constexpr ptrdiff_t STRING_COPY = 0x753a44 - 0x10000 + 1;
#elif defined(GEODE_IS_ANDROID64)
static constexpr ptrdiff_t MENULAYER_SCENE = 0x6a62ec - 0x100000;
2024-01-01 09:54:20 -05:00
static constexpr ptrdiff_t STRING_EMPTY = 0x12d8550 - 0x100000;
2023-12-24 23:11:17 -05:00
static constexpr ptrdiff_t STRING_DTOR = 0xdb9778 - 0x100000; // it's inlined but it exists !!!!
2023-12-23 10:02:13 -05:00
static constexpr ptrdiff_t STRING_COPY = 0xdb5fdc - 0x100000;
#endif
2023-12-20 11:58:47 -05:00
// 2.2 addition
// zmx please fix this
namespace geode::base {
uintptr_t get() {
2023-12-23 10:02:13 -05:00
static uintptr_t base = (reinterpret_cast<uintptr_t>(&MenuLayer::scene) - MENULAYER_SCENE) & (~0x1);
// static uintptr_t base = reinterpret_cast<uintptr_t>(dlopen("libcocos2dcpp.so", RTLD_NOW));
return base;
}
}
2023-12-22 16:09:58 -05:00
namespace geode::stl {
static inline auto emptyInternalString() {
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
);
}
void StringImpl::setEmpty() {
data.m_data = emptyInternalString();
}
void StringImpl::free() {
if (data.m_data == nullptr || data.m_data == emptyInternalString()) return;
2023-12-22 16:09:58 -05:00
// TODO: reimplement this
2023-12-24 23:11:17 -05:00
reinterpret_cast<void (*)(StringData*)>(geode::base::get() + STRING_DTOR)(&data);
}
char* StringImpl::getStorage() {
return reinterpret_cast<char*>(data.m_data);
}
2023-12-23 09:10:36 -05:00
// TODO: add a copyFrom(string const&) to take advantage
// of gnustl refcounted strings
void StringImpl::setStorage(const std::string_view str) {
2023-12-22 16:09:58 -05:00
this->free();
if (str.size() == 0) {
this->setEmpty();
return;
}
// TODO: should be using (char*, size_t) at the very least, or yknow, just reimplement it :-)
2023-12-23 10:02:13 -05:00
reinterpret_cast<void (*)(StringData*, char const*)>(geode::base::get() + STRING_COPY)(&data, str.data());
return;
2023-12-23 09:10:36 -05:00
// TODO: this crashes because we need to use gd's operator new...
#if 0
StringData::Internal internal;
internal.m_size = str.size();
internal.m_capacity = str.size();
internal.m_refcount = 0;
auto* buffer = static_cast<char*>(operator new(str.size() + 1 + sizeof(internal)));
std::memcpy(buffer, &internal, sizeof(internal));
std::memcpy(buffer + sizeof(internal), str.data(), str.size());
buffer[sizeof(internal) + str.size()] = 0;
data.m_data = reinterpret_cast<StringData::Internal*>(buffer + sizeof(internal));
2023-12-23 09:10:36 -05:00
#endif
}
size_t StringImpl::getSize() {
return data.m_data[-1].m_size;
}
void StringImpl::setSize(size_t size) {
2023-12-22 16:09:58 -05:00
// TODO: implement this, remember its copy-on-write...
}
size_t StringImpl::getCapacity() {
return data.m_data[-1].m_capacity;
}
void StringImpl::setCapacity(size_t cap) {
2023-12-22 16:09:58 -05:00
// TODO: implement this, remember its copy-on-write...
}
}
#endif