Skip to content

Commit

Permalink
[unity]把native_src_il2cpp合并到到native_src (#1915)
Browse files Browse the repository at this point in the history
* [unity]把native_src_il2cpp的功能合入到native_src

* FObjectCacheNode::Find递归调用声明为inline在一些编译器版本会报错

* [unreal]删除native_src_il2cpp
  • Loading branch information
chexiongsheng authored Nov 15, 2024
1 parent 82783ae commit 54e6d4c
Show file tree
Hide file tree
Showing 30 changed files with 84 additions and 3,779 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/unity_build_plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ on:
push:
paths:
- unity/native_src/**
- unity/native_src_il2cpp/**
- unity/cli/**
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.h
Expand Down Expand Up @@ -65,12 +64,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildAndroidIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/android/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp

ohos:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -106,12 +99,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildOHOSIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/ohos/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp

ios:
runs-on: macos-13
Expand Down Expand Up @@ -144,12 +131,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildiOSIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/ios/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp

osx:
runs-on: macos-13
Expand Down Expand Up @@ -182,12 +163,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildOSXIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/osx/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp

windows:
runs-on: windows-2019
Expand Down Expand Up @@ -220,13 +195,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildWindowsIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/windows/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp
arch: x64

linux64:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -259,12 +227,6 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: mult
- name: BuildLinuxIl2cpp
uses: ./.github/workflows/composites/unity-build-plugins/linux/
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
backend: v8_9.4.146.24
build_proj_dir: native_src_il2cpp

wasm:
runs-on: ubuntu-latest
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/unity_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
- unity/Assets/**
- unity/test/**
- unity/native_src/**
- unity/native_src_il2cpp/**
- unity/cli/**
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.h
Expand Down
10 changes: 5 additions & 5 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public JsEnv(ILoader loader, int debugPort = -1)
PuertsIl2cpp.NativeAPI.SetGlobalType_ArrayBuffer(typeof(ArrayBuffer));
PuertsIl2cpp.NativeAPI.SetGlobalType_JSObject(typeof(JSObject));

nativeJsEnv = PuertsIl2cpp.NativeAPI.CreateNativeJSEnv();
nativeJsEnv = PuertsIl2cpp.NativeAPI.CreateJSEngine(0);
nativePesapiEnv = PuertsIl2cpp.NativeAPI.GetPapiEnvRef(nativeJsEnv);
var objectPoolType = typeof(PuertsIl2cpp.ObjectPool);
nativeScriptObjectsRefsMgr = PuertsIl2cpp.NativeAPI.InitialPapiEnvRef(apis, nativePesapiEnv, objectPool, objectPoolType.GetMethod("Add"), objectPoolType.GetMethod("Remove"));
Expand All @@ -102,11 +102,11 @@ public JsEnv(ILoader loader, int debugPort = -1)
}
#endif

if (PuertsIl2cpp.NativeAPI.GetLibBackend() == 0)
if (PuertsIl2cpp.NativeAPI.GetLibBackend(nativeJsEnv) == 0)
Backend = new BackendV8(this);
else if (PuertsIl2cpp.NativeAPI.GetLibBackend() == 1)
else if (PuertsIl2cpp.NativeAPI.GetLibBackend(nativeJsEnv) == 1)
Backend = new BackendNodeJS(this);
else if (PuertsIl2cpp.NativeAPI.GetLibBackend() == 2)
else if (PuertsIl2cpp.NativeAPI.GetLibBackend(nativeJsEnv) == 2)
Backend = new BackendQuickJS(this);

PuertsIl2cpp.ExtensionMethodInfo.LoadExtensionMethodInfo();
Expand Down Expand Up @@ -298,7 +298,7 @@ protected virtual void Dispose(bool dispose)
{
if (disposed) return;
PuertsIl2cpp.NativeAPI.CleanupPapiEnvRef(apis, nativePesapiEnv);
PuertsIl2cpp.NativeAPI.DestroyNativeJSEnv(nativeJsEnv);
PuertsIl2cpp.NativeAPI.DestroyJSEngine(nativeJsEnv);
PuertsIl2cpp.NativeAPI.DestroyJSEnvPrivate(nativeScriptObjectsRefsMgr);
nativeScriptObjectsRefsMgr = IntPtr.Zero;
disposed = true;
Expand Down
16 changes: 8 additions & 8 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public class NativeAPI
#if (UNITY_IPHONE || UNITY_TVOS || UNITY_WEBGL || UNITY_SWITCH) && !UNITY_EDITOR
const string DLLNAME = "__Internal";
#else
const string DLLNAME = "puerts_il2cpp";
const string DLLNAME = "puerts";
#endif

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern void InitialPuerts(IntPtr PesapiImpl);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetLibBackend();
public static extern int GetLibBackend(IntPtr isolate);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateNativeJSEnv();
public static extern IntPtr CreateJSEngine(int backendType);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyNativeJSEnv(IntPtr jsEnv);
public static extern void DestroyJSEngine(IntPtr isolate);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetRegsterApi();
Expand All @@ -54,7 +54,7 @@ public class NativeAPI
public static extern IntPtr GetFFIApi();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetPapiEnvRef(IntPtr jsEnv);
public static extern IntPtr GetPapiEnvRef(IntPtr isolate);

[MethodImpl(MethodImplOptions.InternalCall)]
public static IntPtr InitialPapiEnvRef(IntPtr api, IntPtr envRef, Object obj, MethodBase addMethodBase, MethodBase removeMethodBase)
Expand Down Expand Up @@ -218,8 +218,8 @@ public static void LogImpl(string msg)

public static LogCallback Log = LogImpl;

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetLogCallback(IntPtr log);
//[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
//public static extern void SetLogCallback(IntPtr log);

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetLogCallbackInternal(IntPtr log);
Expand All @@ -234,7 +234,7 @@ public static void SetLogCallback(LogCallback log)

try
{
SetLogCallback(fn1);
//SetLogCallback(fn1);
SetLogCallbackInternal(fn1);
}
catch(DllNotFoundException)
Expand Down
8 changes: 0 additions & 8 deletions unity/cli/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ export async function unityTest(cwd, unityPath) {
`);
execUnityEditor(`-executeMethod TestBuilder.GenV2`);
rm("-rf", `${cwd}/Library/ScriptAssemblies`);

await runPuertsMake(join(cwd, '../../native_src_il2cpp'), {
backend: 'v8_9.4.146.24',
platform: platform,
config: 'Debug',
arch: 'x64',
websocket: 1
});

console.log("[Puer] Building testplayer for v2");
mkdir("-p", `${cwd}/build/v2`);
Expand Down
16 changes: 16 additions & 0 deletions unity/native_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ if("${JS_ENGINE}" MATCHES "^mult")
set(USING_MULT_BACKEND ON)
endif()

set(USING_QUICKJS OFF)
if("${JS_ENGINE}" MATCHES "^quickjs")
set(USING_QUICKJS ON)
endif()

set(BACKEND_ROOT ${PROJECT_SOURCE_DIR}/../native_src/.backends/${JS_ENGINE})

set(ThirdParty ${PROJECT_SOURCE_DIR}/../../unreal/Puerts/ThirdParty)
Expand Down Expand Up @@ -107,6 +112,17 @@ endif()
set(PUERTS_COMPILE_DEFINITIONS)
set(PUERTS_BACKEND_SRC)

if (NOT USING_MULT_BACKEND AND NOT USING_QUICKJS AND NOT WITHOUT_IL2CPP_OPTIMIZATION)
list(APPEND PUERTS_COMPILE_DEFINITIONS WITH_IL2CPP_OPTIMIZATION)
list(APPEND PUERTS_SRC
Src/CppObjectMapper.cpp
Src/DataTransfer.cpp
Src/JSClassRegister.cpp
Src/PesapiAddonLoad.cpp
Src/PesapiV8Impl.cpp
)
endif()

if ( WITH_WEBSOCKET )
list(APPEND PUERTS_COMPILE_DEFINITIONS WITH_WEBSOCKET)
if (USING_MULT_BACKEND)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ PRAGMA_DISABLE_UNDEFINED_IDENTIFIER_WARNINGS
PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS

#if !defined(MAPPER_ISOLATE_DATA_POS)
#define MAPPER_ISOLATE_DATA_POS 0
#define MAPPER_ISOLATE_DATA_POS 2
#endif
#if !defined(BACKENDENV_DATA_POS)
#define BACKENDENV_DATA_POS 1
#endif
#if !defined(PESAPI_PRIVATE_DATA_POS_IN_ISOLATE)
#define PESAPI_PRIVATE_DATA_POS_IN_ISOLATE 2
#define PESAPI_PRIVATE_DATA_POS_IN_ISOLATE 3
#endif

#define RELEASED_UOBJECT ((UObject*) 12)
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions unity/native_src/Inc/JSEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#ifdef MULT_BACKENDS
#include "IPuertsPlugin.h"
#endif
#ifdef WITH_IL2CPP_OPTIMIZATION
#include "pesapi.h"
#include "CppObjectMapper.h"
#include "DataTransfer.h"
#endif

#if WITH_NODEJS
#pragma warning(push, 0)
Expand Down Expand Up @@ -184,6 +189,10 @@ class JSEngine

JSFunction* ModuleExecutor = nullptr;

#ifdef WITH_IL2CPP_OPTIMIZATION
FCppObjectMapper CppObjectMapper;
#endif

public:
JSFunction* JSObjectValueGetter = nullptr;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FObjectCacheNode
delete Next;
}

V8_INLINE FObjectCacheNode* Find(const void* TypeId_)
FObjectCacheNode* Find(const void* TypeId_)
{
if (TypeId_ == TypeId)
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions unity/native_src/Src/JSEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ namespace PUERTS_NAMESPACE
Isolate, Context,
v8::FunctionTemplate::New(Isolate, &JSObjectValueGetterFunction)->GetFunction(Context).ToLocalChecked()
);

#ifdef WITH_IL2CPP_OPTIMIZATION
CppObjectMapper.Initialize(Isolate, Context);
Isolate->SetData(MAPPER_ISOLATE_DATA_POS, static_cast<ICppObjectMapper*>(&CppObjectMapper));
#endif

BackendEnv.StartPolling();
}
Expand Down Expand Up @@ -202,6 +207,9 @@ namespace PUERTS_NAMESPACE
ResultInfo.Context.Reset();
ResultInfo.Result.Reset();

#ifdef WITH_IL2CPP_OPTIMIZATION
CppObjectMapper.UnInitialize(MainIsolate);
#endif
BackendEnv.UnInitialize();

for (int i = 0; i < CallbackInfos.size(); ++i)
Expand Down
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions unity/native_src/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <cstring>
#include "V8Utils.h"
#include "Log.h"
#ifdef WITH_IL2CPP_OPTIMIZATION
#include "pesapi.h"
#include "CppObjectMapper.h"
#endif

#define API_LEVEL 34

Expand All @@ -22,6 +26,9 @@ using puerts::JsValueType;
LogCallback GLogCallback = nullptr;
LogCallback GLogWarningCallback = nullptr;
LogCallback GLogErrorCallback = nullptr;
#ifdef WITH_IL2CPP_OPTIMIZATION
extern pesapi_func_ptr reg_apis[];
#endif

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -70,6 +77,33 @@ V8_EXPORT void DestroyJSEngine(v8::Isolate *Isolate)
delete JsEngine;
}

#ifdef WITH_IL2CPP_OPTIMIZATION
V8_EXPORT pesapi_env_ref GetPapiEnvRef(v8::Isolate *Isolate)
{
#ifdef THREAD_SAFE
v8::Locker Locker(Isolate);
#endif
v8::Isolate::Scope IsolateScope(Isolate);
auto jsEnv = FV8Utils::IsolateData<JSEngine>(Isolate);
v8::HandleScope HandleScope(Isolate);
v8::Local<v8::Context> Context = jsEnv->BackendEnv.MainContext.Get(Isolate);
v8::Context::Scope ContextScope(Context);

auto env = reinterpret_cast<pesapi_env>(*Context); //TODO: 实现相关
return v8impl::g_pesapi_ffi.create_env_ref(env);
}

V8_EXPORT pesapi_ffi* GetFFIApi()
{
return &v8impl::g_pesapi_ffi;
}

V8_EXPORT pesapi_func_ptr* GetRegsterApi()
{
return reg_apis;
}
#endif

V8_EXPORT void SetGlobalFunction(v8::Isolate *Isolate, const char *Name, puerts::CSharpFunctionCallback Callback, int64_t Data)
{
auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
Expand Down
Loading

0 comments on commit 54e6d4c

Please sign in to comment.