Skip to content

Commit

Permalink
[unity]同步unreal下对std::string移除的修改
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 6, 2024
1 parent a9f9208 commit d4ca19b
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 25 deletions.
5 changes: 3 additions & 2 deletions unity/native_src/Inc/JSClassRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS

#include "pesapi.h"
#include "TypeInfo.hpp"
#include "PString.h"

#if USING_IN_UNREAL_ENGINE
static const FAnsiStringView EditorOnlyPropertySuffix = "_EditorOnly";
Expand Down Expand Up @@ -146,14 +147,14 @@ JSENV_API void OnClassNotFound(pesapi_class_not_found_callback Callback);

JSENV_API const JSClassDefinition* LoadClassByID(const void* TypeId);

JSENV_API const JSClassDefinition* FindCppTypeClassByName(const std::string& Name);
JSENV_API const JSClassDefinition* FindCppTypeClassByName(const PString& Name);

JSENV_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);

#if USING_IN_UNREAL_ENGINE
typedef void (*AddonRegisterFunc)(v8::Local<v8::Context> Context, v8::Local<v8::Object> Exports);

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name);
AddonRegisterFunc FindAddonRegisterFunc(const PString& Name);

void RegisterAddon(const char* Name, AddonRegisterFunc RegisterFunc);

Expand Down
224 changes: 224 additions & 0 deletions unity/native_src/Inc/PString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may
* be subject to their corresponding license terms. This file is subject to the terms and conditions defined in file 'LICENSE',
* which is part of this source code package.
*/

#pragma once

#include <cstring>
#include <functional>

namespace PUERTS_NAMESPACE
{
class PString
{
public:
// Constructors
PString() : data_(nullptr), size_(0)
{
data_ = new char[1];
data_[0] = '\0';
}

PString(const char* str)
{
if (str)
{
size_ = std::strlen(str);
data_ = new char[size_ + 1];
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, str, size_);
#else
strncpy(data_, str, size_);
#endif
data_[size_] = '\0';
}
else
{
size_ = 0;
data_ = new char[1];
data_[0] = '\0';
}
}

PString(const char* str, size_t length)
{
if (str)
{
size_ = length;
data_ = new char[size_ + 1];
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, str, length);
#else
strncpy(data_, str, length);
#endif
data_[size_] = '\0';
}
else
{
size_ = 0;
data_ = new char[1];
data_[0] = '\0';
}
}

PString(const PString& other)
{
size_ = other.size_;
data_ = new char[size_ + 1];
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, other.data_, size_);
#else
strncpy(data_, other.data_, size_);
#endif
data_[size_] = '\0';
}

PString& operator=(const PString& other)
{
if (this != &other)
{
delete[] data_;
size_ = other.size_;
data_ = new char[size_ + 1];
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, other.data_, size_);
#else
strncpy(data_, other.data_, size_);
#endif
data_[size_] = '\0';
}
return *this;
}

~PString()
{
delete[] data_;
}

PString operator+(const PString& other) const
{
PString result;
result.size_ = size_ + other.size_;
result.data_ = new char[result.size_ + 1];
#ifdef _MSC_VER
strncpy_s(result.data_, result.size_ + 1, data_, size_);
strncpy_s(result.data_ + size_, result.size_ - size_ + 1, other.data_, other.size_);
#else
strncpy(result.data_, data_, size_);
strncpy(result.data_ + size_, other.data_, other.size_);
#endif
result.data_[result.size_] = '\0';
return result;
}

friend PString operator+(const char* lhs, const PString& rhs)
{
PString result;
size_t lhs_size = std::strlen(lhs);
result.size_ = lhs_size + rhs.size_;
result.data_ = new char[result.size_ + 1];
#ifdef _MSC_VER
strncpy_s(result.data_, result.size_ + 1, lhs, lhs_size);
strncpy_s(result.data_ + lhs_size, result.size_ - lhs_size + 1, rhs.data_, rhs.size_);
#else
strncpy(result.data_, lhs, lhs_size);
strncpy(result.data_ + lhs_size, rhs.data_, rhs.size_);
#endif
result.data_[result.size_] = '\0';
return result;
}

PString& operator+=(const PString& other)
{
size_t new_size = size_ + other.size_;
char* new_data = new char[new_size + 1];
#ifdef _MSC_VER
strncpy_s(new_data, new_size + 1, data_, size_);
strncpy_s(new_data + size_, new_size - size_ + 1, other.data_, other.size_);
#else
strncpy(new_data, data_, size_);
strncpy(new_data + size_, other.data_, other.size_);
#endif
new_data[new_size] = '\0';

delete[] data_;
data_ = new_data;
size_ = new_size;

return *this;
}

PString& operator+=(const char* str)
{
if (str)
{
size_t str_size = std::strlen(str);
size_t new_size = size_ + str_size;
char* new_data = new char[new_size + 1];
#ifdef _MSC_VER
strncpy_s(new_data, new_size + 1, data_, size_);
strncpy_s(new_data + size_, new_size - size_ + 1, str, str_size);
#else
strncpy(new_data, data_, size_);
strncpy(new_data + size_, str, str_size);
#endif
new_data[new_size] = '\0';

delete[] data_;
data_ = new_data;
size_ = new_size;
}
return *this;
}

const char* c_str() const
{
return data_;
}

size_t size() const
{
return size_;
}

bool empty() const
{
return size_ == 0;
}

bool operator<(const PString& other) const
{
return std::strcmp(data_, other.data_) < 0;
}

bool operator==(const PString& other) const
{
return std::strcmp(data_, other.data_) == 0;
}

private:
char* data_;
size_t size_;
};
} // namespace PUERTS_NAMESPACE

namespace std
{
template <>
struct hash<puerts::PString>
{
size_t operator()(const puerts::PString& str) const
{
size_t hash = 5381; // DJB2
for (size_t i = 0; i < str.size(); ++i)
{
hash = ((hash << 5) + hash) + str.c_str()[i]; // hash * 33 + c
}
return hash;
}
};
} // namespace std
9 changes: 3 additions & 6 deletions unity/native_src/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
#include "CppObjectMapper.h"
#include "DataTransfer.h"
#include "pesapi.h"
#include "PString.h"

namespace PUERTS_NAMESPACE
{
template <typename T>
inline void __USE(T&&)
{
}

#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))

Expand All @@ -39,7 +36,7 @@ void FCppObjectMapper::LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& In
return;
}

std::string TypeName = *(v8::String::Utf8Value(Isolate, Info[0]));
PString TypeName = *(v8::String::Utf8Value(Isolate, Info[0]));

auto ClassDef = FindCppTypeClassByName(TypeName);
if (ClassDef)
Expand All @@ -48,7 +45,7 @@ void FCppObjectMapper::LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& In
}
else
{
const std::string ErrMsg = "can not find type: " + TypeName;
PString ErrMsg = "can not find type: " + TypeName;
ThrowException(Isolate, ErrMsg.c_str());
}
}
Expand Down
24 changes: 12 additions & 12 deletions unity/native_src/Src/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,22 @@ class JSClassRegister
return clsDef;
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name);
const JSClassDefinition* FindCppTypeClassByName(const PString& Name);

#if USING_IN_UNREAL_ENGINE
void RegisterAddon(const std::string& Name, AddonRegisterFunc RegisterFunc);
void RegisterAddon(const PString& Name, AddonRegisterFunc RegisterFunc);

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name);
AddonRegisterFunc FindAddonRegisterFunc(const PString& Name);

const JSClassDefinition* FindClassByType(UStruct* Type);
#endif

private:
std::map<const void*, JSClassDefinition*> CDataIdToClassDefinition;
std::map<std::string, JSClassDefinition*> CDataNameToClassDefinition;
std::map<PString, JSClassDefinition*> CDataNameToClassDefinition;
pesapi_class_not_found_callback ClassNotFoundCallback = nullptr;
#if USING_IN_UNREAL_ENGINE
std::map<std::string, AddonRegisterFunc> AddonRegisterInfos;
std::map<PString, AddonRegisterFunc> AddonRegisterInfos;
std::map<FString, JSClassDefinition*> StructNameToClassDefinition;
#endif
};
Expand Down Expand Up @@ -150,7 +150,7 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)
JSClassDefinitionDelete(cd_iter->second);
}
CDataIdToClassDefinition[ClassDefinition.TypeId] = JSClassDefinitionDuplicate(&ClassDefinition);
std::string SN = ClassDefinition.ScriptName;
PString SN = ClassDefinition.ScriptName;
CDataNameToClassDefinition[SN] = CDataIdToClassDefinition[ClassDefinition.TypeId];
CDataIdToClassDefinition[ClassDefinition.TypeId]->ScriptName = CDataNameToClassDefinition.find(SN)->first.c_str();
}
Expand All @@ -170,7 +170,7 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)

void SetReflectoinInfo(JSFunctionInfo* Methods, const NamedFunctionInfo* MethodInfos)
{
std::map<std::string, std::tuple<int, const NamedFunctionInfo*>> InfoMap;
std::map<PString, std::tuple<int, const NamedFunctionInfo*>> InfoMap;
const NamedFunctionInfo* MethodInfo = MethodInfos;
while (MethodInfo->Name)
{
Expand Down Expand Up @@ -232,7 +232,7 @@ const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId)
}
}

const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const std::string& Name)
const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const PString& Name)
{
auto Iter = CDataNameToClassDefinition.find(Name);
if (Iter == CDataNameToClassDefinition.end())
Expand All @@ -246,12 +246,12 @@ const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const std::stri
}

#if USING_IN_UNREAL_ENGINE
void JSClassRegister::RegisterAddon(const std::string& Name, AddonRegisterFunc RegisterFunc)
void JSClassRegister::RegisterAddon(const PString& Name, AddonRegisterFunc RegisterFunc)
{
AddonRegisterInfos[Name] = RegisterFunc;
}

AddonRegisterFunc JSClassRegister::FindAddonRegisterFunc(const std::string& Name)
AddonRegisterFunc JSClassRegister::FindAddonRegisterFunc(const PString& Name)
{
auto Iter = AddonRegisterInfos.find(Name);
if (Iter == AddonRegisterInfos.end())
Expand Down Expand Up @@ -329,7 +329,7 @@ const JSClassDefinition* LoadClassByID(const void* TypeId)
return GetJSClassRegister()->LoadClassByID(TypeId);
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name)
const JSClassDefinition* FindCppTypeClassByName(const PString& Name)
{
return GetJSClassRegister()->FindCppTypeClassByName(Name);
}
Expand Down Expand Up @@ -378,7 +378,7 @@ void RegisterAddon(const char* Name, AddonRegisterFunc RegisterFunc)
GetJSClassRegister()->RegisterAddon(Name, RegisterFunc);
}

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name)
AddonRegisterFunc FindAddonRegisterFunc(const PString& Name)
{
return GetJSClassRegister()->FindAddonRegisterFunc(Name);
}
Expand Down
Loading

0 comments on commit d4ca19b

Please sign in to comment.