Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.8.5] Fix enum type conversion and support closure compiler to minify wasm glue code. #17767

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"configurations": [
{
"name": "emscripten",
"includePath": [
"${workspaceFolder}",
"${workspaceFolder}/.."
],
"compileCommands": "${workspaceFolder}/compile_commands.json"
}
],
"version": 4
}
25 changes: 12 additions & 13 deletions native/cocos/editor-support/spine-wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add closure compiler option


set_target_properties(${APP_NAME} PROPERTIES CXX_STANDARD 11 LINK_FLAGS ${EMS_LINK_FLAGS})
48 changes: 48 additions & 0 deletions native/cocos/editor-support/spine-wasm/Vector2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Vector2.h"
#include <math.h>
namespace spine {

Vector2::Vector2(): x(0), y(0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy from ../spine-creator-support/Vector2.cpp to make spine-wasm project independent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it conflict with spine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector2.h/.cpp was not the code in spine-runtime.
spine-creator-support directory is for JSB and spine-wasm is for WASM.
We should not include a file (Vector2.h/.cpp) in spine-creator-support directory since it is used for JSB.


}

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;
}
}
54 changes: 54 additions & 0 deletions native/cocos/editor-support/spine-wasm/Vector2.h
Original file line number Diff line number Diff line change
@@ -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();
};
}
4 changes: 2 additions & 2 deletions native/cocos/editor-support/spine-wasm/library_spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventType is number now, so doesn't need to add .value.

globalThis.TrackEntryListeners.emitTrackEntryListener(listenerID, trackEntry, event, eventType);
}
});
10 changes: 10 additions & 0 deletions native/cocos/editor-support/spine-wasm/library_spine_externs.js
Original file line number Diff line number Diff line change
@@ -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() {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell closure compiler that these methods should not be obfuscated.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

cmake_minimum_required(VERSION 3.8)
project(spine_wasm_header CXX)

include(${CMAKE_CURRENT_LIST_DIR}/../../CMakeLists.txt)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#ifndef _SPINE_SKELETON_INSTANCE_H_
#define _SPINE_SKELETON_INSTANCE_H_
#include <spine/spine.h>
#include <functional>

#include <map>
#include <memory>
#include <string>
#include "mesh-type-define.h"
#include "spine-model.h"
Expand Down
30 changes: 23 additions & 7 deletions native/cocos/editor-support/spine-wasm/spine-type-export.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <emscripten/bind.h>
#include <spine/spine.h>
#include <functional>
#include <memory>
#include <type_traits>
#include <vector>
#include "spine-skeleton-instance.h"
#include "spine-wasm.h"
Expand Down Expand Up @@ -88,9 +86,31 @@ using SPVectorTimelinePtr = Vector<Timeline*>;
using SPVectorTrackEntryPtr = Vector<TrackEntry*>;
using SPVectorUpdatablePtr = Vector<Updatable*>;

template <typename T> static void register_integer(const char* name) {
using namespace internal;
using UnderlyingType = typename std::underlying_type<T>::type;
_embind_register_integer(TypeID<T>::get(), name, sizeof(T), std::numeric_limits<UnderlyingType>::min(),
std::numeric_limits<UnderlyingType>::max());
}

#define REGISTER_SPINE_ENUM(name) \
register_integer<spine::name>("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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle enums like integers.


register_vector<float>("VectorFloat");
register_vector<std::vector<float>>("VectorVectorFloat");
Expand Down Expand Up @@ -313,10 +333,6 @@ EMSCRIPTEN_BINDINGS(spine) {
std::string stdStr(obj.buffer(), obj.length());
return stdStr; }), allow_raw_pointers());


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class_<Color>("Color")
.constructor<>()
.constructor<float, float, float, float>()
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/editor-support/spine/HasRendererObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#ifndef Spine_HasRendererObject_h
#define Spine_HasRendererObject_h

#include "editor-support/spine/dll.h"
#include "spine/dll.h"
#include <cstdlib>

namespace spine {
Expand Down
Loading