Skip to content

Commit

Permalink
[unity]尽量同步代码
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 12, 2024
1 parent 54eb4c8 commit 543913c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
60 changes: 55 additions & 5 deletions unity/native_src_il2cpp/Src/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ JSClassDefinition* JSClassDefinitionDuplicate(const JSClassDefinition* ClassDefi
Ret->Functions = PropertyInfoDuplicate(ClassDefinition->Functions);
Ret->Properties = PropertyInfoDuplicate(ClassDefinition->Properties);
Ret->Variables = PropertyInfoDuplicate(ClassDefinition->Variables);
#if USING_IN_UNREAL_ENGINE
Ret->ConstructorInfos = PropertyInfoDuplicate(ClassDefinition->ConstructorInfos);
Ret->MethodInfos = PropertyInfoDuplicate(ClassDefinition->MethodInfos);
Ret->FunctionInfos = PropertyInfoDuplicate(ClassDefinition->FunctionInfos);
Ret->PropertyInfos = PropertyInfoDuplicate(ClassDefinition->PropertyInfos);
Ret->VariableInfos = PropertyInfoDuplicate(ClassDefinition->VariableInfos);
#endif
return Ret;
}

Expand All @@ -56,13 +54,11 @@ void JSClassDefinitionDelete(JSClassDefinition* ClassDefinition)
delete[] ClassDefinition->Functions;
delete[] ClassDefinition->Properties;
delete[] ClassDefinition->Variables;
#if USING_IN_UNREAL_ENGINE
delete[] ClassDefinition->ConstructorInfos;
delete[] ClassDefinition->MethodInfos;
delete[] ClassDefinition->FunctionInfos;
delete[] ClassDefinition->PropertyInfos;
delete[] ClassDefinition->VariableInfos;
#endif
delete ClassDefinition;
}

Expand All @@ -85,7 +81,7 @@ class JSClassRegister

void ForeachRegisterClass(std::function<void(const JSClassDefinition* ClassDefinition)>);

const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad);
const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad = false);

void SetLazyLoadCallback(LoadTypeFunc InCallback)
{
Expand Down Expand Up @@ -147,6 +143,7 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)
CDataIdToClassDefinition[ClassDefinition.TypeId] = JSClassDefinitionDuplicate(&ClassDefinition);
std::string SN = ClassDefinition.ScriptName;
CDataNameToClassDefinition[SN] = CDataIdToClassDefinition[ClassDefinition.TypeId];
CDataIdToClassDefinition[ClassDefinition.TypeId]->ScriptName = CDataNameToClassDefinition.find(SN)->first.c_str();
}
#if USING_IN_UNREAL_ENGINE
else if (ClassDefinition.UETypeName)
Expand All @@ -162,6 +159,53 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)
#endif
}

void SetReflectoinInfo(JSFunctionInfo* Methods, const NamedFunctionInfo* MethodInfos)
{
std::map<std::string, std::tuple<int, const NamedFunctionInfo*>> InfoMap;
const NamedFunctionInfo* MethodInfo = MethodInfos;
while (MethodInfo->Name)
{
auto Iter = InfoMap.find(MethodInfo->Name);
if (Iter == InfoMap.end())
{
InfoMap[MethodInfo->Name] = std::make_tuple(1, MethodInfo);
}
else
{
std::get<0>(Iter->second) = 2;
}
++MethodInfo;
}

JSFunctionInfo* Method = Methods;
while (Method->Name)
{
auto Iter = InfoMap.find(Method->Name);
if (Iter != InfoMap.end() && std::get<0>(Iter->second) == 1)
{
Method->ReflectionInfo = std::get<1>(Iter->second)->Type;
}
++Method;
}
}

void JSClassRegister::SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos,
const NamedFunctionInfo* MethodInfos, const NamedFunctionInfo* FunctionInfos, const NamedPropertyInfo* PropertyInfos,
const NamedPropertyInfo* VariableInfos)
{
auto ClassDef = const_cast<JSClassDefinition*>(FindClassByID(TypeId));
if (ClassDef)
{
ClassDef->ConstructorInfos = PropertyInfoDuplicate(const_cast<NamedFunctionInfo*>(ConstructorInfos));
ClassDef->MethodInfos = PropertyInfoDuplicate(const_cast<NamedFunctionInfo*>(MethodInfos));
ClassDef->FunctionInfos = PropertyInfoDuplicate(const_cast<NamedFunctionInfo*>(FunctionInfos));
ClassDef->PropertyInfos = PropertyInfoDuplicate(const_cast<NamedPropertyInfo*>(PropertyInfos));
ClassDef->VariableInfos = PropertyInfoDuplicate(const_cast<NamedPropertyInfo*>(VariableInfos));
SetReflectoinInfo(ClassDef->Methods, ClassDef->MethodInfos);
SetReflectoinInfo(ClassDef->Functions, ClassDef->FunctionInfos);
}
}

const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId, bool TryLazyLoad)
{
std::lock_guard<std::recursive_mutex> guard(RegInfoMutex);
Expand Down Expand Up @@ -254,6 +298,12 @@ void RegisterJSClass(const JSClassDefinition& ClassDefinition)
GetJSClassRegister()->RegisterClass(ClassDefinition);
}

void SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos, const NamedFunctionInfo* MethodInfos,
const NamedFunctionInfo* FunctionInfos, const NamedPropertyInfo* PropertyInfos, const NamedPropertyInfo* VariableInfos)
{
GetJSClassRegister()->SetClassTypeInfo(TypeId, ConstructorInfos, MethodInfos, FunctionInfos, PropertyInfos, VariableInfos);
}

std::recursive_mutex& RegisterMutex()
{
return GetJSClassRegister()->RegisterMutex();
Expand Down
2 changes: 0 additions & 2 deletions unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,6 @@ MSVC_PRAGMA(warning(pop))
void pesapi_class_type_info(const char* proto_magic_id, const void* type_id, const void* constructor_info, const void* methods_info,
const void* functions_info, const void* properties_info, const void* variables_info)
{
#if USING_IN_UNREAL_ENGINE
if (strcmp(proto_magic_id, PUERTS_BINDING_PROTO_ID()) != 0)
{
return;
Expand All @@ -889,7 +888,6 @@ void pesapi_class_type_info(const char* proto_magic_id, const void* type_id, con
static_cast<const puerts::NamedFunctionInfo*>(methods_info), static_cast<const puerts::NamedFunctionInfo*>(functions_info),
static_cast<const puerts::NamedPropertyInfo*>(properties_info),
static_cast<const puerts::NamedPropertyInfo*>(variables_info));
#endif
}

const void* pesapi_find_type_id(const char* module_name, const char* type_name)
Expand Down

0 comments on commit 543913c

Please sign in to comment.