Added std::vectors shim to read from client memory

This commit is contained in:
Knaapchen 2022-09-22 19:01:13 +02:00
parent 2b66d01247
commit 94d5fc7e96
2 changed files with 65 additions and 28 deletions

View File

@ -68,12 +68,12 @@ void before_main_loop() {
void after_main_loop() { void after_main_loop() {
// std::string test; // std::string test;
auto& music_properties = fugg::module_ref<fugg::Vector<flyff::MusicProperty>>(0x00034f7c); // auto& music_properties = fugg::module_ref<fugg::Vector<flyff::MusicProperty>>(0x00034f7c);
auto& item_properties = fugg::module_ref<fugg::Vector<flyff::ItemProperty>>(0x00035194); // auto& item_properties = fugg::module_ref<fugg::Vector<flyff::ItemProperty>>(0x00035194);
for(auto& item : item_properties) { // for(auto& item : item_properties) {
std::cout << item.some_string->c_str() << std::endl; // std::cout << item.some_string->c_str() << std::endl;
} // }
// auto& item_properties = fugg::module_ref<fugg::Vector<flyff::ItemProperty>>(0x00035194); // auto& item_properties = fugg::module_ref<fugg::Vector<flyff::ItemProperty>>(0x00035194);
// std::cout << "Expecting " << reinterpret_cast<void*>(fugg::wasm::kMemory.address_of(0x00035194)) << std::endl; // std::cout << "Expecting " << reinterpret_cast<void*>(fugg::wasm::kMemory.address_of(0x00035194)) << std::endl;

View File

@ -20,24 +20,32 @@ namespace fugg {
} }
}; };
struct RawVector {
uintptr_t begin;
uintptr_t end;
uintptr_t end_capacity;
};
template<typename T> template<typename T>
struct RawUniquePointer { struct RawUniquePointer {
T* ptr; uintptr_t ptr;
}; };
template<typename T> template<typename T>
struct RawHashTable { struct RawHashTable {
RawUniquePointer<T*[]> bucket_list; RawUniquePointer<uintptr_t[]> bucket_list;
uintptr_t first_node;
size_t capacity;
float unknown;
}; };
template<typename T>
struct RawBasicString { struct RawBasicString {
T* data; uintptr_t data;
size_t size; size_t size;
size_t cap; size_t cap;
}; };
static_assert( static_assert(
sizeof(RawBasicString<char>) == sizeof(std::string), sizeof(RawBasicString) == sizeof(std::string),
"RawBasicString needs to have same size of std::string" "RawBasicString needs to have same size of std::string"
); );
@ -70,11 +78,7 @@ namespace fugg {
// A class that facilitates handling vectors that are allocated in a module. // A class that facilitates handling vectors that are allocated in a module.
template<typename T> template<typename T>
class Vector { class Vector {
struct RawVector { using RawVector = detail::RawVector;
uintptr_t begin;
uintptr_t end;
uintptr_t end_capacity;
};
ModulePointer<T> begin_; ModulePointer<T> begin_;
ModulePointer<T> end_; ModulePointer<T> end_;
@ -107,7 +111,7 @@ namespace fugg {
template<class T> template<class T>
class BasicString { class BasicString {
using value_type = T; using RawBasicString = detail::RawBasicString;
struct __long { struct __long {
ModulePointer<T> data; ModulePointer<T> data;
@ -115,11 +119,11 @@ namespace fugg {
size_t cap; size_t cap;
}; };
enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ? enum {__min_cap = (sizeof(__long) - 1) / sizeof(T) > 2 ?
(sizeof(__long) - 1)/sizeof(value_type) : 2}; (sizeof(__long) - 1) / sizeof(T) : 2};
struct __short { struct __short {
value_type data[__min_cap]; T data[__min_cap];
struct : detail::__padding<value_type> struct : detail::__padding<T>
{ {
unsigned char size; unsigned char size;
}; };
@ -130,12 +134,6 @@ namespace fugg {
__short __s; __short __s;
}; };
struct RawBasicString {
uintptr_t data;
size_t size;
size_t cap;
};
public: public:
detail::ArrowProxy<RawBasicString, std::basic_string<T>> operator->() { detail::ArrowProxy<RawBasicString, std::basic_string<T>> operator->() {
// Copy the data to the stack. // Copy the data to the stack.
@ -144,8 +142,6 @@ namespace fugg {
// change the pointer to pointer into the module memory. // change the pointer to pointer into the module memory.
auto is_small = -1 < (char)__s.size; auto is_small = -1 < (char)__s.size;
// std::cout << "Short version of string has size " << (int)__s.size << std::endl;
if(!is_small) { if(!is_small) {
temp.data = __l.data.as_pointer(); temp.data = __l.data.as_pointer();
} }
@ -162,4 +158,45 @@ namespace fugg {
using String = BasicString<char>; using String = BasicString<char>;
template<typename T>
class HashTable {
// struct __hash_node_base {
// using node_base_pointer = uintptr_t;
// using __first_node = __hash_node_base;
// node_base_pointer next;
// };
// struct __hash_node : public __hash_node_base {
// typedef T __node_value_type;
// size_t __hash_;
// __node_value_type __value_;
// };
// using __node_pointer = __hash_node_base::node_base_pointer;
// using __node_base_type = __hash_node_base;
// using __next_pointer = __node_pointer;
// using RawUniquePointer = detail::RawUniquePointer;
// using BucketList = RawUniquePointer<__next_pointer[]>;
// using __first_node = __hash_node_base::__first_node;
// BucketList bucketlist;
// __first_node first;
// typedef T* _NodePtr;
// typedef __hash_node_types<_NodePtr> type;
// typedef typename __make_hash_node_types<value_type>::type _NodeTypes;
// typedef typename _NodeTypes::__next_pointer __next_pointer;
// typedef unique_ptr<__next_pointer[]> __bucket_list;
// __bucket_list __bucket_list_;
// ModulePointer<
// RawUniquePointer<uintptr_t[]> bucket_list;
};
} }