From 4b2399f28c87a208fdee3eac8655b65d53626998 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Oct 2024 15:10:58 +0800 Subject: [PATCH] [v3.8.5] Fix enum type conversion and support closure compiler to minify wasm glue code. (#17767) * Add a script for generating compile commands which used for writing embind code in VS Code. --- .../spine-wasm/.vscode/c_cpp_properties.json | 13 +++++ .../editor-support/spine-wasm/CMakeLists.txt | 25 +++++---- .../editor-support/spine-wasm/Vector2.cpp | 48 +++++++++++++++++ .../cocos/editor-support/spine-wasm/Vector2.h | 54 +++++++++++++++++++ .../spine-wasm/library_spine.js | 4 +- .../spine-wasm/library_spine_externs.js | 10 ++++ .../spine-wasm/scripts/CMakeLists.header.txt | 5 ++ .../generate_compile_commands_emscripten.sh | 21 ++++++++ .../spine-wasm/spine-skeleton-instance.h | 3 +- .../spine-wasm/spine-type-export.cpp | 30 ++++++++--- .../editor-support/spine/HasRendererObject.h | 2 +- 11 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 native/cocos/editor-support/spine-wasm/.vscode/c_cpp_properties.json create mode 100644 native/cocos/editor-support/spine-wasm/Vector2.cpp create mode 100644 native/cocos/editor-support/spine-wasm/Vector2.h create mode 100644 native/cocos/editor-support/spine-wasm/library_spine_externs.js create mode 100644 native/cocos/editor-support/spine-wasm/scripts/CMakeLists.header.txt create mode 100755 native/cocos/editor-support/spine-wasm/scripts/generate_compile_commands_emscripten.sh diff --git a/native/cocos/editor-support/spine-wasm/.vscode/c_cpp_properties.json b/native/cocos/editor-support/spine-wasm/.vscode/c_cpp_properties.json new file mode 100644 index 00000000000..2628c1c906f --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/.vscode/c_cpp_properties.json @@ -0,0 +1,13 @@ +{ + "configurations": [ + { + "name": "emscripten", + "includePath": [ + "${workspaceFolder}", + "${workspaceFolder}/.." + ], + "compileCommands": "${workspaceFolder}/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/CMakeLists.txt b/native/cocos/editor-support/spine-wasm/CMakeLists.txt index 0568e62b522..b1ba7deff18 100644 --- a/native/cocos/editor-support/spine-wasm/CMakeLists.txt +++ b/native/cocos/editor-support/spine-wasm/CMakeLists.txt @@ -1,33 +1,32 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.8) set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_BUILD_TYPE "MinSizeRel") -set(BUILD_WASM 1) - set(APP_NAME "spine" CACHE STRING "Project Name") project(${APP_NAME}_wasm) +set(BUILD_WASM 1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -frtti -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=1") -message("Current directory: ${CMAKE_CURRENT_SOURCE_DIR}") - -#include_directories(../ ../../) -include_directories(../ ../../ ../spine-creator-support/) -file(GLOB SPINE_CORE_SRC "../spine/*.cpp" ../spine-creator-support/Vector2.cpp) -file(GLOB COCOS_ADAPTER_SRC "./*.cpp") +message("Current directory: ${CMAKE_CURRENT_LIST_DIR}") -#file(GLOB SPINE_CORE_SRC ../spine-creator-support/Vector2.cpp) +include_directories(${CMAKE_CURRENT_LIST_DIR}/..) +file(GLOB SPINE_CORE_SRC "${CMAKE_CURRENT_LIST_DIR}/../spine/*.cpp") +file(GLOB COCOS_ADAPTER_SRC "${CMAKE_CURRENT_LIST_DIR}/*.cpp") -add_executable(${APP_NAME} ${SPINE_CORE_SRC} ${COCOS_ADAPTER_SRC} ) -#add_executable(${APP_NAME} ${COCOS_ADAPTER_SRC}) +add_executable(${APP_NAME} ${SPINE_CORE_SRC} ${COCOS_ADAPTER_SRC}) set(EMS_LINK_FLAGS "-O3 -s WASM=${BUILD_WASM} -s INITIAL_MEMORY=33554432 -s ALLOW_MEMORY_GROWTH=1 -s DYNAMIC_EXECUTION=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 \ -flto --no-entry --bind -s USE_ES6_IMPORT_META=0 -s EXPORT_ES6=1 -s MODULARIZE=1 -s EXPORT_NAME='spineWasm' \ -s ENVIRONMENT=web -s FILESYSTEM=0 -s NO_EXIT_RUNTIME=1 -s LLD_REPORT_UNDEFINED \ -s MIN_SAFARI_VERSION=110000 \ - --js-library ../library_spine.js") + -s EXPORTED_FUNCTIONS=['_spineListenerCallBackFromJS','_spineTrackListenerCallback'] \ + --js-library ../library_spine.js \ + --closure=1 \ + --closure-args=--externs=../library_spine_externs.js") set_target_properties(${APP_NAME} PROPERTIES CXX_STANDARD 11 LINK_FLAGS ${EMS_LINK_FLAGS}) \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/Vector2.cpp b/native/cocos/editor-support/spine-wasm/Vector2.cpp new file mode 100644 index 00000000000..3b18308506c --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/Vector2.cpp @@ -0,0 +1,48 @@ +#include "Vector2.h" +#include +namespace spine { + +Vector2::Vector2(): x(0), y(0) { + +} + +Vector2::Vector2(float x, float y) { + this->x = x; + this->y = y; +} + +Vector2::~Vector2() {} + +void Vector2::setX(float x) { + this->x = x; +} + +float Vector2::getX() { + return x; +} + +void Vector2::setY(float y) { + this->y = y; +} + +float Vector2::getY() { + return y; +} + +Vector2& Vector2::set(float x, float y) { + this->setX(x); + this->setY(y); + return *this; +} + +float Vector2::length() { + return sqrt(x * x + y * y); +} + +Vector2& Vector2::normalize() { + float invLen = 1.F / length(); + this->setX(x * invLen); + this->setY(y * invLen); + return *this; +} +} \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/Vector2.h b/native/cocos/editor-support/spine-wasm/Vector2.h new file mode 100644 index 00000000000..0f62883bc4c --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/Vector2.h @@ -0,0 +1,54 @@ +/****************************************************************************** + * Spine Runtimes License Agreement + * Last updated January 1, 2020. Replaces all prior versions. + * + * Copyright (c) 2013-2020, Esoteric Software LLC + * + * Integration of the Spine Runtimes into software or otherwise creating + * derivative works of the Spine Runtimes is permitted under the terms and + * conditions of Section 2 of the Spine Editor License Agreement: + * http://esotericsoftware.com/spine-editor-license + * + * Otherwise, it is permitted to integrate the Spine Runtimes into software + * or otherwise create derivative works of the Spine Runtimes (collectively, + * "Products"), provided that each user of the Products must obtain their own + * Spine Editor license and redistribution of the Products in any form must + * include this license and copyright notice. + * + * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, + * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +#pragma once + +#include "spine/spine.h" + +namespace spine { + class Vector2 + { + public: + float x, y; + public: + Vector2(); + Vector2(float x, float y); + ~Vector2(); + + void setX(float x); + float getX(); + + void setY(float y); + float getY(); + + Vector2 &set(float x, float y); + float length(); + Vector2 &normalize(); + }; +} \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/library_spine.js b/native/cocos/editor-support/spine-wasm/library_spine.js index 6c47c49341c..46d63f151d5 100644 --- a/native/cocos/editor-support/spine-wasm/library_spine.js +++ b/native/cocos/editor-support/spine-wasm/library_spine.js @@ -5,7 +5,7 @@ mergeInto(LibraryManager.library, { var trackEntry = wasmUtil.getCurrentTrackEntry(); var event = wasmUtil.getCurrentEvent(); var eventType = wasmUtil.getCurrentEventType(); - globalThis.TrackEntryListeners.emitListener(listenerID, trackEntry, event, eventType.value); + globalThis.TrackEntryListeners.emitListener(listenerID, trackEntry, event, eventType); }, spineTrackListenerCallback: function() { @@ -14,6 +14,6 @@ mergeInto(LibraryManager.library, { var eventType = wasmUtil.getCurrentEventType(); var trackEntry = wasmUtil.getCurrentTrackEntry(); var event = wasmUtil.getCurrentEvent(); - globalThis.TrackEntryListeners.emitTrackEntryListener(listenerID, trackEntry, event, eventType.value); + globalThis.TrackEntryListeners.emitTrackEntryListener(listenerID, trackEntry, event, eventType); } }); diff --git a/native/cocos/editor-support/spine-wasm/library_spine_externs.js b/native/cocos/editor-support/spine-wasm/library_spine_externs.js new file mode 100644 index 00000000000..d369120b70c --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/library_spine_externs.js @@ -0,0 +1,10 @@ +var globalThis = {}; +globalThis.TrackEntryListeners = {}; +globalThis.TrackEntryListeners.emitListener = function() {}; +globalThis.TrackEntryListeners.emitTrackEntryListener = function() {}; + +var SpineWasmUtil = {}; +SpineWasmUtil.getCurrentListenerID = function() {}; +SpineWasmUtil.getCurrentTrackEntry = function() {}; +SpineWasmUtil.getCurrentEvent = function() {}; +SpineWasmUtil.getCurrentEventType = function() {}; diff --git a/native/cocos/editor-support/spine-wasm/scripts/CMakeLists.header.txt b/native/cocos/editor-support/spine-wasm/scripts/CMakeLists.header.txt new file mode 100644 index 00000000000..cfa01c69663 --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/scripts/CMakeLists.header.txt @@ -0,0 +1,5 @@ + +cmake_minimum_required(VERSION 3.8) +project(spine_wasm_header CXX) + +include(${CMAKE_CURRENT_LIST_DIR}/../../CMakeLists.txt) \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/scripts/generate_compile_commands_emscripten.sh b/native/cocos/editor-support/spine-wasm/scripts/generate_compile_commands_emscripten.sh new file mode 100755 index 00000000000..1d79370dc9a --- /dev/null +++ b/native/cocos/editor-support/spine-wasm/scripts/generate_compile_commands_emscripten.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e + +if [ -n "$EMSDK" ]; then + echo "EMSDK=$EMSDK" +else + echo "[ERROR] EMSDK env variable is not set!" + exit 1 +fi + +rm -rf build +mkdir build +cd build +cp ../CMakeLists.header.txt ./CMakeLists.txt +emcmake cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -GNinja \ + -DCMAKE_CXX_FLAGS="-I${EMSDK}/upstream/emscripten/system/include -I${EMSDK}/upstream/include/c++/v1" + +cp ./compile_commands.json ../.. +cd .. +rm -rf build diff --git a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.h b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.h index a1a15bf1e92..735f4e5ea06 100644 --- a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.h +++ b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.h @@ -1,9 +1,8 @@ #ifndef _SPINE_SKELETON_INSTANCE_H_ #define _SPINE_SKELETON_INSTANCE_H_ #include -#include + #include -#include #include #include "mesh-type-define.h" #include "spine-model.h" diff --git a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp index 5765f49b5fc..7d8e28fcc9f 100644 --- a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp @@ -1,7 +1,5 @@ #include -#include -#include -#include +#include #include #include "spine-skeleton-instance.h" #include "spine-wasm.h" @@ -88,9 +86,31 @@ using SPVectorTimelinePtr = Vector; using SPVectorTrackEntryPtr = Vector; using SPVectorUpdatablePtr = Vector; +template static void register_integer(const char* name) { + using namespace internal; + using UnderlyingType = typename std::underlying_type::type; + _embind_register_integer(TypeID::get(), name, sizeof(T), std::numeric_limits::min(), + std::numeric_limits::max()); +} + +#define REGISTER_SPINE_ENUM(name) \ + register_integer("spine::" #name) + } // namespace EMSCRIPTEN_BINDINGS(spine) { + REGISTER_SPINE_ENUM(TimelineType); + REGISTER_SPINE_ENUM(MixDirection); + REGISTER_SPINE_ENUM(MixBlend); + REGISTER_SPINE_ENUM(EventType); + REGISTER_SPINE_ENUM(BlendMode); + REGISTER_SPINE_ENUM(TransformMode); + REGISTER_SPINE_ENUM(PositionMode); + REGISTER_SPINE_ENUM(SpacingMode); + REGISTER_SPINE_ENUM(RotateMode); + REGISTER_SPINE_ENUM(TextureFilter); + REGISTER_SPINE_ENUM(TextureWrap); + REGISTER_SPINE_ENUM(AttachmentType); register_vector("VectorFloat"); register_vector>("VectorVectorFloat"); @@ -313,10 +333,6 @@ EMSCRIPTEN_BINDINGS(spine) { std::string stdStr(obj.buffer(), obj.length()); return stdStr; }), allow_raw_pointers()); - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// - class_("Color") .constructor<>() .constructor() diff --git a/native/cocos/editor-support/spine/HasRendererObject.h b/native/cocos/editor-support/spine/HasRendererObject.h index 79d9d5894c7..f5ea7a0c938 100644 --- a/native/cocos/editor-support/spine/HasRendererObject.h +++ b/native/cocos/editor-support/spine/HasRendererObject.h @@ -30,7 +30,7 @@ #ifndef Spine_HasRendererObject_h #define Spine_HasRendererObject_h -#include "editor-support/spine/dll.h" +#include "spine/dll.h" #include namespace spine {