From 144838d708b92a9ddbcab609c9c595bf90f1da26 Mon Sep 17 00:00:00 2001 From: Knaapchen Date: Tue, 4 Oct 2022 23:47:00 +0200 Subject: [PATCH] Some refactoring but it's building! --- CMakeLists.txt | 4 ++- flyff-api/CMakeLists.txt | 2 +- flyff-api/include/flyff.h | 27 ++++++++++++++ flyff-api/src/import.h | 14 ++++++++ .../src/raw}/ItemProperty.h | 2 +- .../structs => flyff-api/src/raw}/Job.h | 0 .../structs => flyff-api/src/raw}/Mover.h | 2 +- .../src/raw}/MusicProperty.h | 2 +- .../src/raw}/ObjectType.h | 0 flyff-client/CMakeLists.txt | 2 +- fugg-api/CMakeLists.txt | 10 ++++++ {fugg-core => fugg-api}/include/fugg.h | 0 {fugg-core => fugg-api}/src/memory.h | 0 {fugg-core => fugg-api}/src/pointer.h | 0 {fugg-core => fugg-api}/src/stl.h | 4 +-- {fugg-core => fugg-api}/src/table.h | 0 {fugg-core => fugg-api}/src/wasm.h | 0 fugg-client/CMakeLists.txt | 14 ++++---- fugg-client/src/client.cpp | 28 +++++---------- fugg-client/src/client.h | 2 +- fugg-client/src/{interop => }/export/al.cpp | 0 .../src/{interop => }/export/callback.cpp | 0 .../src/{interop => }/export/embind.cpp | 0 fugg-client/src/{interop => }/export/embind.h | 0 fugg-client/src/{interop => }/export/env.cpp | 0 fugg-client/src/{interop => }/export/gl.cpp | 0 .../src/{interop => }/export/platform.cpp | 0 fugg-client/src/interop/flyff.h | 25 ------------- fugg-client/src/interop/imports.h | 19 ---------- fugg-client/src/main.cpp | 36 +++++++++---------- fugg-core/CMakeLists.txt | 10 ------ 31 files changed, 94 insertions(+), 109 deletions(-) create mode 100644 flyff-api/src/import.h rename {fugg-client/src/interop/structs => flyff-api/src/raw}/ItemProperty.h (99%) rename {fugg-client/src/interop/structs => flyff-api/src/raw}/Job.h (100%) rename {fugg-client/src/interop/structs => flyff-api/src/raw}/Mover.h (99%) rename {fugg-client/src/interop/structs => flyff-api/src/raw}/MusicProperty.h (97%) rename {fugg-client/src/interop/structs => flyff-api/src/raw}/ObjectType.h (100%) create mode 100644 fugg-api/CMakeLists.txt rename {fugg-core => fugg-api}/include/fugg.h (100%) rename {fugg-core => fugg-api}/src/memory.h (100%) rename {fugg-core => fugg-api}/src/pointer.h (100%) rename {fugg-core => fugg-api}/src/stl.h (98%) rename {fugg-core => fugg-api}/src/table.h (100%) rename {fugg-core => fugg-api}/src/wasm.h (100%) rename fugg-client/src/{interop => }/export/al.cpp (100%) rename fugg-client/src/{interop => }/export/callback.cpp (100%) rename fugg-client/src/{interop => }/export/embind.cpp (100%) rename fugg-client/src/{interop => }/export/embind.h (100%) rename fugg-client/src/{interop => }/export/env.cpp (100%) rename fugg-client/src/{interop => }/export/gl.cpp (100%) rename fugg-client/src/{interop => }/export/platform.cpp (100%) delete mode 100644 fugg-client/src/interop/flyff.h delete mode 100644 fugg-client/src/interop/imports.h delete mode 100644 fugg-core/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 1694040..c2bb55f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,5 +7,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin") set(CMAKE_EXECUTABLE_SUFFIX ".js") add_subdirectory(flyff-client) -add_subdirectory(fugg-core) +add_subdirectory(flyff-api) + +add_subdirectory(fugg-api) add_subdirectory(fugg-client) diff --git a/flyff-api/CMakeLists.txt b/flyff-api/CMakeLists.txt index c3ad4f2..061cce2 100644 --- a/flyff-api/CMakeLists.txt +++ b/flyff-api/CMakeLists.txt @@ -5,4 +5,4 @@ add_library ( ) target_include_directories(flyff-api PUBLIC "include") -target_link_libraries (flyff-api LINK_PUBLIC flyff-client) \ No newline at end of file +target_link_libraries (flyff-api PUBLIC flyff-client fugg-api) \ No newline at end of file diff --git a/flyff-api/include/flyff.h b/flyff-api/include/flyff.h index e69de29..c4611d3 100644 --- a/flyff-api/include/flyff.h +++ b/flyff-api/include/flyff.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include + +#include "../src/import.h" + +namespace flyff { + #include "../src/raw/ItemProperty.h" + #include "../src/raw/MusicProperty.h" + #include "../src/raw/Mover.h" + + namespace api { + // static_assert(sizeof(fugg::ModulePointer) == sizeof(u32), "ModulePointer needs the same size as u32"); + + extern "C" { + // Translate + // extern void translate(u32, u32); + // extern void w2c_f167(fugg::String*, std::string*); + extern void w2c_f167(u32, u32); + + extern u32 w2c_f208(u64); + }; + } + +} \ No newline at end of file diff --git a/flyff-api/src/import.h b/flyff-api/src/import.h new file mode 100644 index 0000000..cca1be9 --- /dev/null +++ b/flyff-api/src/import.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace flyff { + static const auto& init = &Z_client_init; + + static const auto& _wasm_call_ctors = &Z_clientZ____wasm_call_ctors; + static const auto& main = &Z_clientZ__main; + + static const auto& malloc = &Z_clientZ__malloc; + static const auto& free = &Z_clientZ__free; +} diff --git a/fugg-client/src/interop/structs/ItemProperty.h b/flyff-api/src/raw/ItemProperty.h similarity index 99% rename from fugg-client/src/interop/structs/ItemProperty.h rename to flyff-api/src/raw/ItemProperty.h index 7a2fec9..e443a87 100644 --- a/fugg-client/src/interop/structs/ItemProperty.h +++ b/flyff-api/src/raw/ItemProperty.h @@ -16,7 +16,7 @@ typedef struct ItemProperty ItemProperty, *PItemProperty; typedef struct TranslationArg TranslationArg, *PTranslationArg; -#include "fugg.h" +#include struct ItemProperty { int * * field0_0x0; diff --git a/fugg-client/src/interop/structs/Job.h b/flyff-api/src/raw/Job.h similarity index 100% rename from fugg-client/src/interop/structs/Job.h rename to flyff-api/src/raw/Job.h diff --git a/fugg-client/src/interop/structs/Mover.h b/flyff-api/src/raw/Mover.h similarity index 99% rename from fugg-client/src/interop/structs/Mover.h rename to flyff-api/src/raw/Mover.h index ce41ebb..5e3e95d 100644 --- a/fugg-client/src/interop/structs/Mover.h +++ b/flyff-api/src/raw/Mover.h @@ -1,4 +1,4 @@ -#include "fugg.h" +#include typedef unsigned char undefined; diff --git a/fugg-client/src/interop/structs/MusicProperty.h b/flyff-api/src/raw/MusicProperty.h similarity index 97% rename from fugg-client/src/interop/structs/MusicProperty.h rename to flyff-api/src/raw/MusicProperty.h index cee7556..5e42188 100644 --- a/fugg-client/src/interop/structs/MusicProperty.h +++ b/flyff-api/src/raw/MusicProperty.h @@ -14,7 +14,7 @@ typedef unsigned long long undefined8; typedef unsigned short ushort; typedef struct MusicProperty MusicProperty, *PMusicProperty; -#include "fugg.h" +#include struct MusicProperty { fugg::String name; diff --git a/fugg-client/src/interop/structs/ObjectType.h b/flyff-api/src/raw/ObjectType.h similarity index 100% rename from fugg-client/src/interop/structs/ObjectType.h rename to flyff-api/src/raw/ObjectType.h diff --git a/flyff-client/CMakeLists.txt b/flyff-client/CMakeLists.txt index 7aa31b0..a78b084 100644 --- a/flyff-client/CMakeLists.txt +++ b/flyff-client/CMakeLists.txt @@ -7,4 +7,4 @@ add_library ( ) target_include_directories(flyff-client PUBLIC "include") -target_link_libraries (flyff-client LINK_PUBLIC wasm-rt) \ No newline at end of file +target_link_libraries (flyff-client PUBLIC wasm-rt) \ No newline at end of file diff --git a/fugg-api/CMakeLists.txt b/fugg-api/CMakeLists.txt new file mode 100644 index 0000000..7393716 --- /dev/null +++ b/fugg-api/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library ( + fugg-api + + "include/fugg.h" +) + +set_target_properties(fugg-api PROPERTIES LINKER_LANGUAGE CXX) + +target_include_directories(fugg-api PUBLIC "include") +target_link_libraries (fugg-api PUBLIC flyff-client) \ No newline at end of file diff --git a/fugg-core/include/fugg.h b/fugg-api/include/fugg.h similarity index 100% rename from fugg-core/include/fugg.h rename to fugg-api/include/fugg.h diff --git a/fugg-core/src/memory.h b/fugg-api/src/memory.h similarity index 100% rename from fugg-core/src/memory.h rename to fugg-api/src/memory.h diff --git a/fugg-core/src/pointer.h b/fugg-api/src/pointer.h similarity index 100% rename from fugg-core/src/pointer.h rename to fugg-api/src/pointer.h diff --git a/fugg-core/src/stl.h b/fugg-api/src/stl.h similarity index 98% rename from fugg-core/src/stl.h rename to fugg-api/src/stl.h index 02484db..78869f1 100644 --- a/fugg-core/src/stl.h +++ b/fugg-api/src/stl.h @@ -153,9 +153,9 @@ namespace fugg { memcpy(&__l, &temp, sizeof(RawBasicString)); } - detail::ArrowProxy> operator->() { + detail::ArrowProxy> operator->() const { // Copy the data to the stack. - auto temp = *reinterpret_cast(&__l); + auto temp = *reinterpret_cast(&__l); // If the string is using the __long representation, we need to // change the pointer to pointer into the module memory. auto is_small = -1 < (char)__s.size; diff --git a/fugg-core/src/table.h b/fugg-api/src/table.h similarity index 100% rename from fugg-core/src/table.h rename to fugg-api/src/table.h diff --git a/fugg-core/src/wasm.h b/fugg-api/src/wasm.h similarity index 100% rename from fugg-core/src/wasm.h rename to fugg-api/src/wasm.h diff --git a/fugg-client/CMakeLists.txt b/fugg-client/CMakeLists.txt index a7c24e8..6ec7ca8 100644 --- a/fugg-client/CMakeLists.txt +++ b/fugg-client/CMakeLists.txt @@ -3,19 +3,19 @@ add_executable( "src/main.cpp" "src/client.cpp" # Symbols that flyff-client imports. - "src/interop/export/gl.cpp" - "src/interop/export/al.cpp" - "src/interop/export/callback.cpp" - "src/interop/export/embind.cpp" - "src/interop/export/env.cpp" - "src/interop/export/platform.cpp" + "src/export/gl.cpp" + "src/export/al.cpp" + "src/export/callback.cpp" + "src/export/embind.cpp" + "src/export/env.cpp" + "src/export/platform.cpp" ) # Flyff is based on c++14 add_definitions(-std=c++14 -Os) target_include_directories(fugg-client PUBLIC "include") -target_link_libraries (fugg-client LINK_PUBLIC fugg-core flyff-client) +target_link_libraries (fugg-client LINK_PUBLIC flyff-api) set_target_properties( fugg-client diff --git a/fugg-client/src/client.cpp b/fugg-client/src/client.cpp index 8c5edaa..8b9e0e7 100644 --- a/fugg-client/src/client.cpp +++ b/fugg-client/src/client.cpp @@ -1,6 +1,6 @@ #include "client.h" -#include "interop/imports.h" +#include #include @@ -13,15 +13,13 @@ namespace fugg // 4. Call main // 5. Post-run - using namespace interop; - using wasm::kMemory; using wasm::kTable; Client::Client() { // 1. Pre-run: There are no pre-runs in this binary. // 2. Init runtime: `___wasm_call_ctors` is added to on init runtime. - (*import::__wasm_call_ctors)(); + (*flyff::_wasm_call_ctors)(); } Client& Client::instance() { @@ -30,7 +28,7 @@ namespace fugg // Initialise the WebAssembly runtime. wasm_rt_init(); // Initialise the client. - import::init(); + flyff::init(); // Only init the module once. initialised = true; } @@ -48,29 +46,20 @@ namespace fugg // 3. Pre-main: There are no pre-mains in this binary. // 4. Call main: `_main` is main in the binary. // TODO: Fix arguments. - return (*import::_main)(0, 0); + return (*flyff::main)(0, 0); } } -#include "interop/flyff.h" - -void translate(const std::string& id) { +fugg::String translate(const std::string& id) { auto result = std::make_unique(); auto id_ = std::make_unique(id); - std::cout << "Result before: " << result->data << ", " << result->size << ", " << result->cap << std::endl; - flyff::api::w2c_f167( fugg::RuntimePointer(result.get()).as_raw(), fugg::RuntimePointer(id_.get()).as_raw() ); - std::cout << "Result after: " << reinterpret_cast(result->data) << ", " - << result->size << ", " - << reinterpret_cast(result->cap) << std::endl; - - auto showable = *reinterpret_cast(result.get()); - std::cout << "Result = " << showable->c_str() << std::endl; + return *reinterpret_cast(result.get()); } #include @@ -83,11 +72,10 @@ void translate(const std::string& id) { void before_main_loop() { auto& client = fugg::Client::instance(); - translate("ids_textclient_cannot_dropmoney"); + const auto& test = translate("ids_textclient_cannot_dropmoney"); + std::cout << test->c_str() << std::endl; } -#include "interop/flyff.h" - void after_main_loop() { // std::string test; diff --git a/fugg-client/src/client.h b/fugg-client/src/client.h index e44f71f..da70cc3 100644 --- a/fugg-client/src/client.h +++ b/fugg-client/src/client.h @@ -1,4 +1,4 @@ -#include "fugg.h" +#include namespace fugg { class Client { diff --git a/fugg-client/src/interop/export/al.cpp b/fugg-client/src/export/al.cpp similarity index 100% rename from fugg-client/src/interop/export/al.cpp rename to fugg-client/src/export/al.cpp diff --git a/fugg-client/src/interop/export/callback.cpp b/fugg-client/src/export/callback.cpp similarity index 100% rename from fugg-client/src/interop/export/callback.cpp rename to fugg-client/src/export/callback.cpp diff --git a/fugg-client/src/interop/export/embind.cpp b/fugg-client/src/export/embind.cpp similarity index 100% rename from fugg-client/src/interop/export/embind.cpp rename to fugg-client/src/export/embind.cpp diff --git a/fugg-client/src/interop/export/embind.h b/fugg-client/src/export/embind.h similarity index 100% rename from fugg-client/src/interop/export/embind.h rename to fugg-client/src/export/embind.h diff --git a/fugg-client/src/interop/export/env.cpp b/fugg-client/src/export/env.cpp similarity index 100% rename from fugg-client/src/interop/export/env.cpp rename to fugg-client/src/export/env.cpp diff --git a/fugg-client/src/interop/export/gl.cpp b/fugg-client/src/export/gl.cpp similarity index 100% rename from fugg-client/src/interop/export/gl.cpp rename to fugg-client/src/export/gl.cpp diff --git a/fugg-client/src/interop/export/platform.cpp b/fugg-client/src/export/platform.cpp similarity index 100% rename from fugg-client/src/interop/export/platform.cpp rename to fugg-client/src/export/platform.cpp diff --git a/fugg-client/src/interop/flyff.h b/fugg-client/src/interop/flyff.h deleted file mode 100644 index b06ab5a..0000000 --- a/fugg-client/src/interop/flyff.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "fugg.h" - -#include - -namespace flyff { - #include "structs/ItemProperty.h" - #include "structs/MusicProperty.h" - #include "structs/Mover.h" - - namespace api { - static_assert(sizeof(fugg::ModulePointer) == sizeof(u32), "ModulePointer needs the same size as u32"); - - extern "C" { - // Translate - // extern void translate(u32, u32); - // extern void w2c_f167(fugg::String*, std::string*); - extern void w2c_f167(u32, u32); - - extern u32 w2c_f208(u64); - }; - } - -} \ No newline at end of file diff --git a/fugg-client/src/interop/imports.h b/fugg-client/src/interop/imports.h deleted file mode 100644 index 4dd4d75..0000000 --- a/fugg-client/src/interop/imports.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -namespace fugg { - namespace interop { - namespace import { - static const auto& init = &Z_client_init; - - static const auto& __wasm_call_ctors = &Z_clientZ____wasm_call_ctors; - static const auto& _main = &Z_clientZ__main; - - - static const auto& _malloc = &Z_clientZ__malloc; - static const auto& _free = &Z_clientZ__free; - } - } -} diff --git a/fugg-client/src/main.cpp b/fugg-client/src/main.cpp index a11fe6d..359e5be 100644 --- a/fugg-client/src/main.cpp +++ b/fugg-client/src/main.cpp @@ -1,59 +1,57 @@ -#include "fugg.h" -#include "client.h" - #include - #include -#include "interop/imports.h" + +#include +#include + +#include "client.h" void *malloc (size_t __size) { // void * operator new(std::size_t __size) { - using namespace fugg::interop; // This malloc is always called from the runtime. - if((*import::_malloc) != nullptr) { + if((*flyff::malloc) != nullptr) { // Since this malloc is always called from the interop, we need to // transform it to point to the actual address where the WASM runtime memory // resides. const auto& ptr = fugg::ModulePointer { - (*import::_malloc)(__size) + (*flyff::malloc)(__size) }; -// #if TRACE_ALLOC +#if TRACE_ALLOC std::cout << "FLYFF: Allocated INSIDE " << __size << " bytes at " << reinterpret_cast(ptr.as_raw()) << std::endl; -// #endif +#endif return ptr; } else { auto ptr = emscripten_builtin_malloc(__size); -// #if TRACE_ALLOC +#if TRACE_ALLOC std::cout << "FUGG: Allocated OUTSIDE " << __size << " bytes at " << reinterpret_cast(ptr) << std::endl; -// #endif +#endif return ptr; } } void free (void *__ptr) { // void operator delete(void * __ptr) { - using namespace fugg::interop; // printf("Trying to free ptr %p\n", __ptr); - if((*import::_free) != nullptr) { + if((*flyff::free) != nullptr) { const auto& ptr = fugg::RuntimePointer { __ptr }; - (*import::_free)(ptr.as_raw()); + (*flyff::free)(ptr.as_raw()); -// #if TRACE_ALLOC +#if TRACE_ALLOC std::cout << "FLYFF: Free'd INSIDE pointer at " << reinterpret_cast(ptr.as_raw()) << std::endl; -// #endif +#endif } else { emscripten_builtin_free(__ptr); -// #if TRACE_ALLOC +#if TRACE_ALLOC std::cout << "FUGG: Free'd OUTSIDE pointer at " << reinterpret_cast(__ptr) << std::endl; -// #endif +#endif } } diff --git a/fugg-core/CMakeLists.txt b/fugg-core/CMakeLists.txt deleted file mode 100644 index 769f1dc..0000000 --- a/fugg-core/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -add_library ( - fugg-core - - "include/fugg.h" -) - -set_target_properties(fugg-core PROPERTIES LINKER_LANGUAGE CXX) - -target_include_directories(fugg-core PUBLIC "include") -target_link_libraries (fugg-core LINK_PUBLIC flyff-client) \ No newline at end of file