no message

This commit is contained in:
gem
2025-02-18 15:21:31 +08:00
commit 2d133e56d7
1980 changed files with 465595 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/****************************************************************************
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "core/utils/IDGenerator.h"
#include "base/Random.h"
#include "boost/uuid/uuid.hpp"
#include "boost/uuid/uuid_generators.hpp"
#include "boost/uuid/uuid_io.hpp"
namespace cc {
IDGenerator globalIdGenerator("global");
IDGenerator::IDGenerator(const ccstd::string &category) {
// Tnit with a random id to emphasize that the returns id should not be stored in persistence data.
_id = static_cast<uint32_t>(RandomHelper::randomInt(0, 998));
_prefix = (category + nonUuidMark);
}
ccstd::string IDGenerator::getNewId() {
#if CC_EDITOR
if (_prefix == "Node." || _prefix == "Comp.") {
static boost::uuids::random_generator_mt19937 generator;
boost::uuids::uuid id = generator();
return boost::uuids::to_string(id);
}
#endif
return _prefix + std::to_string(++_id);
}
} // namespace cc

View File

@@ -0,0 +1,57 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include "base/std/container/string.h"
namespace cc {
static const char *nonUuidMark = ".";
/**
* ID generator for runtime.
*/
class IDGenerator {
public:
/**
* @param [category] You can specify a unique category to avoid id collision with other instance of IdGenerator.
*/
explicit IDGenerator(const ccstd::string &category);
ccstd::string getNewId();
private:
uint32_t _id{0};
ccstd::string _prefix;
};
/*
* The global id generator might have a conflict problem once every 365 days,
* if the game runs at 60 FPS and each frame 4760273 counts of new id are requested.
*/
extern IDGenerator globalIdGenerator;
} // namespace cc

View File

@@ -0,0 +1,93 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "core/utils/ImageUtils.h"
#include "base/Log.h"
#include "renderer/gfx-base/GFXDef-common.h"
namespace {
uint8_t *convertRGB2RGBA(uint32_t length, uint8_t *src) {
auto *dst = reinterpret_cast<uint8_t *>(malloc(length));
for (uint32_t i = 0; i < length; i += 4) {
dst[i] = *src++;
dst[i + 1] = *src++;
dst[i + 2] = *src++;
dst[i + 3] = 255;
}
return dst;
}
uint8_t *convertIA2RGBA(uint32_t length, uint8_t *src) {
auto *dst = reinterpret_cast<uint8_t *>(malloc(length));
for (uint32_t i = 0; i < length; i += 4) {
dst[i] = *src;
dst[i + 1] = *src;
dst[i + 2] = *src++;
dst[i + 3] = *src++;
}
return dst;
}
uint8_t *convertI2RGBA(uint32_t length, uint8_t *src) {
auto *dst = reinterpret_cast<uint8_t *>(malloc(length));
for (uint32_t i = 0; i < length; i += 4) {
dst[i] = *src;
dst[i + 1] = *src;
dst[i + 2] = *src++;
dst[i + 3] = 255;
}
return dst;
}
} // namespace
namespace cc {
void ImageUtils::convert2RGBA(Image *image) {
if (!image->_isCompressed && image->_renderFormat != gfx::Format::RGBA8) {
image->_dataLen = image->_width * image->_height * 4;
uint8_t *dst = nullptr;
uint32_t length = static_cast<uint32_t>(image->_dataLen);
uint8_t *src = image->_data;
switch (image->_renderFormat) {
case gfx::Format::A8:
case gfx::Format::LA8:
dst = convertIA2RGBA(length, src);
break;
case gfx::Format::L8:
case gfx::Format::R8:
case gfx::Format::R8I:
dst = convertI2RGBA(length, src);
break;
case gfx::Format::RGB8:
dst = convertRGB2RGBA(length, src);
break;
default:
CC_LOG_INFO("cannot convert to RGBA: unknown image format");
break;
}
if (dst != image->_data) free(image->_data);
image->_data = dst;
image->_renderFormat = gfx::Format::RGBA8;
}
}
} // namespace cc

View File

@@ -0,0 +1,35 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include "cocos/platform/Image.h"
namespace cc {
class ImageUtils {
public:
static void convert2RGBA(Image *image);
};
} // namespace cc

216
cocos/core/utils/Path.cpp Normal file
View File

@@ -0,0 +1,216 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "core/utils/Path.h"
#include <cstring>
#include "base/StringUtil.h"
namespace cc {
namespace {
const ccstd::string EMPTY_STRING;
ccstd::string &removeLastSlash(ccstd::string &path) {
if (!path.empty()) {
if (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
path = path.substr(0, path.length() - 1);
} else if (path.length() > 1 && path[path.length() - 1] == '\\' && path[path.length() - 2] == '\\') {
path = path.substr(0, path.length() - 2);
}
}
return path;
}
} // namespace
ccstd::string join(const ccstd::vector<ccstd::string> &segments) {
ccstd::string result;
for (const auto &segment : segments) {
if (!result.empty()) {
result += "/";
}
result += segment;
removeLastSlash(result);
}
return result;
}
ccstd::string extname(const ccstd::string &path) {
if (path.empty()) {
return EMPTY_STRING;
}
ccstd::string newPath = path;
size_t index = path.find_first_of('?');
if (index != ccstd::string::npos && index > 0) {
newPath = newPath.substr(0, index);
}
index = newPath.find_last_of('.');
if (index == ccstd::string::npos) {
return EMPTY_STRING;
}
return newPath.substr(index);
}
ccstd::string mainFileName(const ccstd::string &fileName) {
if (!fileName.empty()) {
size_t idx = fileName.find_last_of('.');
if (idx != ccstd::string::npos) {
return fileName.substr(0, idx);
}
}
return fileName;
}
ccstd::string basename(const ccstd::string &path, const ccstd::string &extName /* = ""*/) {
ccstd::string newPath = path;
size_t index = path.find_first_of('?');
if (index != ccstd::string::npos && index > 0) {
newPath = newPath.substr(0, index);
}
removeLastSlash(newPath);
index = newPath.find_last_of("/\\");
if (index == ccstd::string::npos) {
return newPath;
}
ccstd::string baseName = newPath.substr(index + 1);
if (!extName.empty() && extName.length() < newPath.length()) {
ccstd::string extInPath = newPath.substr(newPath.length() - extName.length());
ccstd::string expectedExtName = extName;
if (StringUtil::tolower(extInPath) == StringUtil::tolower(expectedExtName)) {
baseName = baseName.substr(0, baseName.length() - extName.length());
}
}
return baseName;
}
ccstd::string dirname(const ccstd::string &path) {
size_t index = path.find_last_of("/\\");
if (index == ccstd::string::npos) {
return "";
}
ccstd::string dir = path.substr(0, index);
removeLastSlash(dir);
return dir;
}
ccstd::string changeExtname(const ccstd::string &path, const ccstd::string &extName /* = ""*/) {
size_t index = path.find_first_of('?');
ccstd::string newPath = path;
ccstd::string tempStr;
if (index != ccstd::string::npos && index > 0) {
tempStr = path.substr(index);
newPath = path.substr(0, index);
}
index = newPath.find_last_of('.');
if (index == ccstd::string::npos) {
return newPath + extName + tempStr;
}
return newPath.substr(0, index) + extName + tempStr;
}
ccstd::string changeBasename(const ccstd::string &path, const ccstd::string &baseName, bool isSameExt /* = false*/) {
if (baseName.find_last_of('.') == 0) {
return changeExtname(path, baseName);
}
size_t index = path.find_last_of('?');
ccstd::string tempStr;
ccstd::string newPath = path;
const ccstd::string ext = isSameExt ? extname(path) : "";
if (index != ccstd::string::npos && index > 0) {
tempStr = path.substr(index);
newPath = path.substr(0, index);
}
index = newPath.find_last_of("/\\");
if (index == ccstd::string::npos) {
index = 0;
} else if (index > 0) {
++index;
}
return newPath.substr(0, index) + baseName + ext + tempStr;
}
ccstd::string normalize(const ccstd::string &url) {
ccstd::string oldUrl = url;
ccstd::string newUrl = url;
// remove all ../
do {
oldUrl = newUrl;
size_t index = newUrl.find("../");
if (index == ccstd::string::npos) {
index = newUrl.find("..\\");
}
size_t previousSlashIndex = ccstd::string::npos;
size_t previousTwiceSlashIndex = ccstd::string::npos;
if (index != ccstd::string::npos && index > 0) {
previousSlashIndex = newUrl.find_last_of("/\\", index - 1);
if (previousSlashIndex != ccstd::string::npos) {
previousTwiceSlashIndex = newUrl.find_last_of("/\\", previousSlashIndex - 1);
}
}
if (previousTwiceSlashIndex == ccstd::string::npos) {
if (previousSlashIndex != ccstd::string::npos) {
newUrl = newUrl.substr(index + strlen("../"));
}
} else if (previousSlashIndex != ccstd::string::npos) {
newUrl = newUrl.substr(0, previousTwiceSlashIndex) + '/' + newUrl.substr(index + strlen("../"));
}
} while (oldUrl.length() != newUrl.length());
return newUrl;
}
ccstd::string stripSep(const ccstd::string &path) {
ccstd::string result = path;
removeLastSlash(result);
return result;
}
char getSeperator() {
#if (CC_PLATFORM == CC_PLATFORM_WINDOWS)
return '\\';
#else
return '/';
#endif
}
} // namespace cc

87
cocos/core/utils/Path.h Normal file
View File

@@ -0,0 +1,87 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include "base/std/container/string.h"
#include "base/std/container/vector.h"
namespace cc {
/**
* @en Join strings to be a path.
* @zh 拼接字符串为路径。
* @example {@link cocos/core/utils/CCPath/join.js}
*/
ccstd::string join(const ccstd::vector<ccstd::string> &segments);
/**
* @en Get the ext name of a path including '.', like '.png'.
* @zh 返回 Path 的扩展名,包括 '.',例如 '.png'。
* @example {@link cocos/core/utils/CCPath/extname.js}
*/
ccstd::string extname(const ccstd::string &path);
/**
* @en Get the main name of a file name.
* @zh 获取文件名的主名称。
* @deprecated
*/
ccstd::string mainFileName(const ccstd::string &fileName);
/**
* @en Get the file name of a file path.
* @zh 获取文件路径的文件名。
* @example {@link cocos/core/utils/CCPath/basename.js}
*/
ccstd::string basename(const ccstd::string &path, const ccstd::string &extName = "");
/**
* @en Get dirname of a file path.
* @zh 获取文件路径的目录名。
* @example {@link cocos/core/utils/CCPath/dirname.js}
*/
ccstd::string dirname(const ccstd::string &path);
/**
* @en Change extname of a file path.
* @zh 更改文件路径的扩展名。
* @example {@link cocos/core/utils/CCPath/changeExtname.js}
*/
ccstd::string changeExtname(const ccstd::string &path, const ccstd::string &extName = "");
/**
* @en Change file name of a file path.
* @zh 更改文件路径的文件名。
* @example {@link cocos/core/utils/CCPath/changeBasename.js}
*/
ccstd::string changeBasename(const ccstd::string &path, const ccstd::string &baseName, bool isSameExt = false);
ccstd::string normalize(const ccstd::string &url);
ccstd::string stripSep(const ccstd::string &path);
char getSeperator();
} // namespace cc

184
cocos/core/utils/Pool.h Normal file
View File

@@ -0,0 +1,184 @@
/****************************************************************************
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
/**
* @en
* A fixed-length object pool designed for general type.<br>
* The implementation of this object pool is very simple,
* it can helps you to improve your game performance for objects which need frequent release and recreate operations<br/>
* @zh
* 长度固定的对象缓存池,可以用来缓存各种对象类型。<br/>
* 这个对象池的实现非常精简,它可以帮助您提高游戏性能,适用于优化对象的反复创建和销毁。
* @class js.Pool
* @example
* ```
*
* Example 1:
*
* function Details () {
* uuidList = [];
* };
* Details.prototype.reset = function () {
* uuidList.length = 0;
* };
* Details.pool = new js.Pool(function (obj) {
* obj.reset();
* }, 5);
* Details.pool.get = function () {
* return _get() || new Details();
* };
*
* var detail = Details.pool.get();
* ...
* Details.pool.put(detail);
*
* Example 2:
*
* function Details (buffer) {
* uuidList = buffer;
* };
* ...
* Details.pool.get = function (buffer) {
* var cached = _get();
* if (cached) {
* cached.uuidList = buffer;
* return cached;
* }
* else {
* return new Details(buffer);
* }
* };
*
* var detail = Details.pool.get( [] );
* ...
* ```
*/
#include <functional>
#include <memory>
#include "base/Macros.h"
template <typename T>
class Pool {
public:
using CleanUpFunction = std::function<bool(T &)>;
/**
* @en
* Get and initialize an object from pool. This method defaults to null and requires the user to implement it.
* @zh
* 获取并初始化对象池中的对象。这个方法默认为空,需要用户自己实现。
* @param args - parameters to used to initialize the object
*/
std::function<T()> get{};
/**
* 使用构造函数来创建一个指定对象类型的对象池,您可以传递一个回调函数,用于处理对象回收时的清理逻辑。
* @method constructor
* @param {Function} [cleanupFunc] - the callback method used to process the cleanup logic when the object is recycled.
* @param {Object} cleanupFunc.obj
* @param {Number} size - initializes the length of the array
*/
Pool(const CleanUpFunction &cleanup, int32_t size) {
_count = 0;
_pool.resize(size);
_cleanup = cleanup;
}
/**
* 使用构造函数来创建一个指定对象类型的对象池,您可以传递一个回调函数,用于处理对象回收时的清理逻辑。
* @method constructor
* @param {Function} [cleanupFunc] - the callback method used to process the cleanup logic when the object is recycled.
* @param {Object} cleanupFunc.obj
* @param {Number} size - initializes the length of the array
*/
explicit Pool(int32_t size)
: Pool{nullptr, size} {}
~Pool() = default;
/**
* @en
* Get an object from pool, if no available object in the pool, null will be returned.
* @zh
* 获取对象池中的对象,如果对象池没有可用对象,则返回空。
*/
T _get() { //NOLINT(readability-identifier-naming)
if (_count > 0) {
--_count;
T cache = _pool[_count];
_pool[_count] = nullptr;
return cache;
}
return nullptr;
}
/**
* @en Put an object into the pool.
* @zh 向对象池返还一个不再需要的对象。
*/
void put(T &obj) {
if (_count < static_cast<int32_t>(_pool.size())) {
if (_cleanup != nullptr && !_cleanup(obj)) {
return;
}
_pool[_count] = obj;
++_count;
}
}
/**
* @en Resize the pool.
* @zh 设置对象池容量。
*/
void resize(int32_t length) {
if (length >= 0) {
_pool.resize(length);
if (_count > length) {
_count = length;
}
}
}
int32_t getCount() const { return _count; }
private:
ccstd::vector<T> _pool;
CleanUpFunction _cleanup{nullptr};
/**
* @en
* The current number of available objects, the default is 0, it will gradually increase with the recycle of the object,
* the maximum will not exceed the size specified when the constructor is called.
* @zh
* 当前可用对象数量,一开始默认是 0随着对象的回收会逐渐增大最大不会超过调用构造函数时指定的 size。
* @default 0
*/
int32_t _count{0};
CC_DISALLOW_COPY_MOVE_ASSIGN(Pool);
};