2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd
|
2023-01-01 18:41:48 +08:00
|
|
|
Copyright (c) 2021-2023 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Utils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <stdlib.h>
|
2020-08-26 12:46:51 +08:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
// for unix/linux kill
|
2021-12-06 13:00:44 +08:00
|
|
|
# include <unistd.h>
|
2020-08-26 12:46:51 +08:00
|
|
|
#endif
|
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
#include "openssl/evp.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/AsyncTaskPool.h"
|
|
|
|
#include "base/EventDispatcher.h"
|
|
|
|
#include "base/Constants.h"
|
|
|
|
#include "base/UTF8.h"
|
|
|
|
#include "renderer/CustomCommand.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/TextureCache.h"
|
|
|
|
#include "renderer/RenderState.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/Types.h"
|
2020-09-10 21:14:28 +08:00
|
|
|
#include "renderer/backend/PixelBufferDescriptor.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/Image.h"
|
|
|
|
#include "platform/FileUtils.h"
|
|
|
|
#include "2d/Sprite.h"
|
|
|
|
#include "2d/RenderTexture.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-01-01 18:07:18 +08:00
|
|
|
#include "base/base64.h"
|
2023-02-18 11:39:31 +08:00
|
|
|
#include "base/axstd.h"
|
2023-01-01 18:07:18 +08:00
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int ccNextPOT(int x)
|
|
|
|
{
|
|
|
|
x = x - 1;
|
|
|
|
x = x | (x >> 1);
|
|
|
|
x = x | (x >> 2);
|
|
|
|
x = x | (x >> 4);
|
|
|
|
x = x | (x >> 8);
|
2021-12-06 13:00:44 +08:00
|
|
|
x = x | (x >> 16);
|
2019-11-23 20:27:39 +08:00
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace utils
|
|
|
|
{
|
2023-01-01 18:07:18 +08:00
|
|
|
namespace base64
|
|
|
|
{
|
|
|
|
inline int encBound(int sourceLen)
|
|
|
|
{
|
|
|
|
return (sourceLen + 2) / 3 * 4;
|
|
|
|
}
|
|
|
|
inline int decBound(int sourceLen)
|
|
|
|
{
|
|
|
|
return sourceLen / 4 * 3 + 1;
|
|
|
|
}
|
|
|
|
} // namespace base64
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/*
|
|
|
|
* Capture screen interface
|
|
|
|
*/
|
2020-09-14 17:20:07 +08:00
|
|
|
static EventListenerCustom* s_captureScreenListener;
|
2020-09-11 11:57:55 +08:00
|
|
|
void captureScreen(std::function<void(RefPtr<Image>)> imageCallback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-09-14 17:20:07 +08:00
|
|
|
if (s_captureScreenListener)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Warning: CaptureScreen has been called already, don't call more than once in one frame.");
|
2020-09-14 17:20:07 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto renderer = director->getRenderer();
|
2020-09-14 17:20:07 +08:00
|
|
|
auto eventDispatcher = director->getEventDispatcher();
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
// !!!Metal: needs setFrameBufferOnly before draw
|
2022-07-16 10:43:05 +08:00
|
|
|
#if defined(AX_USE_METAL)
|
2021-12-06 13:00:44 +08:00
|
|
|
s_captureScreenListener =
|
|
|
|
eventDispatcher->addCustomEventListener(Director::EVENT_BEFORE_DRAW, [=](EventCustom* /*event*/) {
|
2021-04-22 22:01:47 +08:00
|
|
|
#else
|
2021-12-06 13:00:44 +08:00
|
|
|
s_captureScreenListener =
|
|
|
|
eventDispatcher->addCustomEventListener(Director::EVENT_AFTER_DRAW, [=](EventCustom* /*event*/) {
|
2021-04-22 22:01:47 +08:00
|
|
|
#endif
|
2021-12-06 13:00:44 +08:00
|
|
|
eventDispatcher->removeEventListener(s_captureScreenListener);
|
|
|
|
s_captureScreenListener = nullptr;
|
|
|
|
// !!!GL: AFTER_DRAW and BEFORE_END_FRAME
|
|
|
|
renderer->readPixels(renderer->getDefaultRenderTarget(), [=](const backend::PixelBufferDescriptor& pbd) {
|
|
|
|
if (pbd)
|
|
|
|
{
|
|
|
|
auto image = utils::makeInstance<Image>(&Image::initWithRawData, pbd._data.getBytes(),
|
|
|
|
pbd._data.getSize(), pbd._width, pbd._height, 8, false);
|
|
|
|
imageCallback(image);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
imageCallback(nullptr);
|
2020-09-14 17:43:44 +08:00
|
|
|
});
|
2021-12-06 13:00:44 +08:00
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::unordered_map<Node*, EventListenerCustom*> s_captureNodeListener;
|
2020-09-11 11:57:55 +08:00
|
|
|
void captureNode(Node* startNode, std::function<void(RefPtr<Image>)> imageCallback, float scale)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (s_captureNodeListener.find(startNode) != s_captureNodeListener.end())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Warning: current node has been captured already");
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto callback = [startNode, scale, imageCallback](EventCustom* /*event*/) {
|
2021-12-06 13:00:44 +08:00
|
|
|
auto director = Director::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto captureNodeListener = s_captureNodeListener[startNode];
|
|
|
|
director->getEventDispatcher()->removeEventListener((EventListener*)(captureNodeListener));
|
|
|
|
s_captureNodeListener.erase(startNode);
|
|
|
|
auto& size = startNode->getContentSize();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2020-09-13 19:16:59 +08:00
|
|
|
director->setNextDeltaTimeZero(true);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RenderTexture* finalRtx = nullptr;
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2022-11-28 08:37:22 +08:00
|
|
|
auto rtx =
|
|
|
|
RenderTexture::create(size.width, size.height, backend::PixelFormat::RGBA8, PixelFormat::D24S8, false);
|
2019-11-23 20:27:39 +08:00
|
|
|
// rtx->setKeepMatrix(true);
|
|
|
|
Point savedPos = startNode->getPosition();
|
|
|
|
Point anchor;
|
2021-12-06 13:00:44 +08:00
|
|
|
if (!startNode->isIgnoreAnchorPointForPosition())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
anchor = startNode->getAnchorPoint();
|
|
|
|
}
|
|
|
|
startNode->setPosition(Point(size.width * anchor.x, size.height * anchor.y));
|
|
|
|
rtx->begin();
|
|
|
|
startNode->visit();
|
|
|
|
rtx->end();
|
|
|
|
startNode->setPosition(savedPos);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
if (std::abs(scale - 1.0f) < 1e-6f /* no scale */)
|
2019-11-23 20:27:39 +08:00
|
|
|
finalRtx = rtx;
|
2021-12-06 13:00:44 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
/* scale */
|
|
|
|
auto finalRect = Rect(0, 0, size.width, size.height);
|
2021-12-06 13:00:44 +08:00
|
|
|
Sprite* sprite = Sprite::createWithTexture(rtx->getSprite()->getTexture(), finalRect);
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setAnchorPoint(Point(0, 0));
|
|
|
|
sprite->setFlippedY(true);
|
2021-12-06 13:00:44 +08:00
|
|
|
finalRtx = RenderTexture::create(size.width * scale, size.height * scale, backend::PixelFormat::RGBA8,
|
2022-11-28 08:37:22 +08:00
|
|
|
PixelFormat::D24S8, false);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
sprite->setScale(scale); // or use finalRtx->setKeepMatrix(true);
|
2019-11-23 20:27:39 +08:00
|
|
|
finalRtx->begin();
|
|
|
|
sprite->visit();
|
|
|
|
finalRtx->end();
|
|
|
|
}
|
2020-09-11 11:57:55 +08:00
|
|
|
|
2020-09-13 19:16:59 +08:00
|
|
|
director->getRenderer()->render();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
finalRtx->newImage(imageCallback);
|
|
|
|
};
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
auto listener =
|
|
|
|
Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_BEFORE_DRAW, callback);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
s_captureNodeListener[startNode] = listener;
|
|
|
|
}
|
|
|
|
|
2020-09-11 11:57:55 +08:00
|
|
|
// [DEPRECATED]
|
2021-12-31 12:12:40 +08:00
|
|
|
void captureScreen(std::function<void(bool, std::string_view)> afterCap, std::string_view filename)
|
2020-09-11 11:57:55 +08:00
|
|
|
{
|
|
|
|
std::string outfile;
|
|
|
|
if (FileUtils::getInstance()->isAbsolutePath(filename))
|
|
|
|
outfile = filename;
|
|
|
|
else
|
2021-12-31 12:12:40 +08:00
|
|
|
outfile = FileUtils::getInstance()->getWritablePath().append(filename);
|
2020-09-15 14:12:00 +08:00
|
|
|
|
|
|
|
captureScreen([_afterCap = std::move(afterCap), _outfile = std::move(outfile)](RefPtr<Image> image) mutable {
|
2021-12-06 13:00:44 +08:00
|
|
|
AsyncTaskPool::getInstance()->enqueue(
|
|
|
|
AsyncTaskPool::TaskType::TASK_IO,
|
|
|
|
[_afterCap = std::move(_afterCap), image = std::move(image), _outfile = std::move(_outfile)]() mutable {
|
|
|
|
bool ok = image->saveToFile(_outfile);
|
2023-01-01 18:41:48 +08:00
|
|
|
Director::getInstance()->getScheduler()->runOnAxmolThread(
|
2021-12-06 13:00:44 +08:00
|
|
|
[ok, _afterCap = std::move(_afterCap), _outfile = std::move(_outfile)] {
|
|
|
|
_afterCap(ok, _outfile);
|
|
|
|
});
|
|
|
|
});
|
2020-09-11 11:57:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::vector<Node*> findChildren(const Node& node, std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::vector<Node*> vec;
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
node.enumerateChildren(name, [&vec](Node* nodeFound) -> bool {
|
2022-08-08 13:18:33 +08:00
|
|
|
vec.emplace_back(nodeFound);
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_ITOA_BUFFER_SIZE 256
|
|
|
|
double atof(const char* str)
|
|
|
|
{
|
|
|
|
if (str == nullptr)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
char buf[MAX_ITOA_BUFFER_SIZE];
|
|
|
|
strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// strip string, only remain 7 numbers after '.'
|
|
|
|
char* dot = strchr(buf, '.');
|
2021-12-06 13:00:44 +08:00
|
|
|
if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
dot[8] = '\0';
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ::atof(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
double gettime()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
long long getTimeInMilliseconds()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2021-12-06 13:00:44 +08:00
|
|
|
gettimeofday(&tv, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
Rect getCascadeBoundingBox(Node* node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Rect cbb;
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 contentSize = node->getContentSize();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// check all children bounding box, get maximize box
|
|
|
|
Node* child = nullptr;
|
2021-12-06 13:00:44 +08:00
|
|
|
bool merge = false;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& object : node->getChildren())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
child = dynamic_cast<Node*>(object);
|
2021-12-06 13:00:44 +08:00
|
|
|
if (!child->isVisible())
|
|
|
|
continue;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const Rect box = getCascadeBoundingBox(child);
|
2021-12-06 13:00:44 +08:00
|
|
|
if (box.size.width <= 0 || box.size.height <= 0)
|
|
|
|
continue;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!merge)
|
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
cbb = box;
|
2019-11-23 20:27:39 +08:00
|
|
|
merge = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// merge content size
|
|
|
|
if (contentSize.width > 0 && contentSize.height > 0)
|
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
const Rect box = RectApplyAffineTransform(Rect(0, 0, contentSize.width, contentSize.height),
|
|
|
|
node->getNodeToWorldAffineTransform());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!merge)
|
|
|
|
{
|
|
|
|
cbb = box;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return cbb;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key)
|
|
|
|
{
|
|
|
|
Texture2D* texture = Director::getInstance()->getTextureCache()->getTextureForKey(key);
|
|
|
|
|
|
|
|
if (texture == nullptr)
|
|
|
|
{
|
|
|
|
unsigned char* decoded;
|
|
|
|
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
Image* image = new Image();
|
2020-08-31 21:04:47 +08:00
|
|
|
bool imageResult = image->initWithImageData(decoded, length, true);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(imageResult, "Failed to create image from base64!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
if (!imageResult)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(image);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(image, key);
|
|
|
|
image->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* sprite = Sprite::createWithTexture(texture);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* createSpriteFromBase64(const char* base64String)
|
|
|
|
{
|
|
|
|
unsigned char* decoded;
|
|
|
|
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
Image* image = new Image();
|
2020-08-31 21:04:47 +08:00
|
|
|
bool imageResult = image->initWithImageData(decoded, length, decoded);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(imageResult, "Failed to create image from base64!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
if (!imageResult)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(image);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
Texture2D* texture = new Texture2D();
|
2019-11-23 20:27:39 +08:00
|
|
|
texture->initWithImage(image);
|
|
|
|
texture->setAliasTexParameters();
|
|
|
|
image->release();
|
|
|
|
|
|
|
|
Sprite* sprite = Sprite::createWithTexture(texture);
|
|
|
|
texture->release();
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Node* findChild(Node* levelRoot, std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (levelRoot == nullptr || name.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find this node
|
|
|
|
auto target = levelRoot->getChildByName(name);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
|
|
|
|
// Find recursively
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& child : levelRoot->getChildren())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
target = findChild(child, name);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* findChild(Node* levelRoot, int tag)
|
|
|
|
{
|
|
|
|
if (levelRoot == nullptr || tag == Node::INVALID_TAG)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find this node
|
|
|
|
auto target = levelRoot->getChildByTag(tag);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
|
|
|
|
// Find recursively
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& child : levelRoot->getChildren())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
target = findChild(child, tag);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string getFileMD5Hash(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
FileUtils::getInstance()->getContents(filename, &data);
|
|
|
|
|
|
|
|
return getDataMD5Hash(data);
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
std::string getDataMD5Hash(const Data& data)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (data.isNull())
|
2021-12-06 15:02:53 +08:00
|
|
|
return std::string{};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
return computeDigest(std::string_view{(const char*)data.getBytes(), (size_t)data.getSize()}, "md5"sv);
|
|
|
|
}
|
|
|
|
|
2023-01-01 18:07:18 +08:00
|
|
|
std::string computeDigest(std::string_view data, std::string_view algorithm, bool toHex)
|
2021-12-16 11:14:39 +08:00
|
|
|
{
|
2023-08-31 18:14:26 +08:00
|
|
|
const EVP_MD* md = nullptr;
|
2021-12-16 11:14:39 +08:00
|
|
|
unsigned char mdValue[EVP_MAX_MD_SIZE] = {0};
|
|
|
|
unsigned int mdLen = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
OpenSSL_add_all_digests();
|
2023-08-31 18:14:26 +08:00
|
|
|
md = EVP_get_digestbyname(algorithm.data());
|
2021-12-16 11:14:39 +08:00
|
|
|
if (!md || data.empty())
|
|
|
|
return std::string{};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
EVP_MD_CTX* mdctx = EVP_MD_CTX_create();
|
2023-08-31 18:14:26 +08:00
|
|
|
EVP_DigestInit_ex(mdctx, md, nullptr);
|
2021-12-16 11:14:39 +08:00
|
|
|
EVP_DigestUpdate(mdctx, data.data(), data.size());
|
2023-08-31 18:14:26 +08:00
|
|
|
EVP_DigestFinal_ex(mdctx, mdValue, &mdLen);
|
2021-12-16 11:14:39 +08:00
|
|
|
EVP_MD_CTX_destroy(mdctx);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-01-01 18:07:18 +08:00
|
|
|
return toHex ? bin2hex(std::string_view{(const char*)mdValue, (size_t)mdLen})
|
|
|
|
: std::string{(const char*)mdValue, (size_t)mdLen};
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LanguageType getLanguageTypeByISO2(const char* code)
|
|
|
|
{
|
|
|
|
// this function is used by all platforms to get system language
|
2021-12-31 14:39:08 +08:00
|
|
|
// except windows: core/platform/win32/CCApplication-win32.cpp
|
2019-11-23 20:27:39 +08:00
|
|
|
LanguageType ret = LanguageType::ENGLISH;
|
|
|
|
|
|
|
|
if (strncmp(code, "zh", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::CHINESE;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "ja", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::JAPANESE;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "fr", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::FRENCH;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "it", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::ITALIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "de", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::GERMAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "es", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::SPANISH;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "nl", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::DUTCH;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "ru", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::RUSSIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "hu", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::HUNGARIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "pt", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::PORTUGUESE;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "ko", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::KOREAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "ar", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::ARABIC;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "nb", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::NORWEGIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "pl", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::POLISH;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "tr", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::TURKISH;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "uk", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::UKRAINIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "ro", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::ROMANIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "bg", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::BULGARIAN;
|
|
|
|
}
|
|
|
|
else if (strncmp(code, "be", 2) == 0)
|
|
|
|
{
|
|
|
|
ret = LanguageType::BELARUSIAN;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
backend::BlendFactor toBackendBlendFactor(int factor)
|
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
switch (factor)
|
|
|
|
{
|
|
|
|
case GLBlendConst::ONE:
|
|
|
|
return backend::BlendFactor::ONE;
|
|
|
|
case GLBlendConst::ZERO:
|
|
|
|
return backend::BlendFactor::ZERO;
|
|
|
|
case GLBlendConst::SRC_COLOR:
|
|
|
|
return backend::BlendFactor::SRC_COLOR;
|
|
|
|
case GLBlendConst::ONE_MINUS_SRC_COLOR:
|
|
|
|
return backend::BlendFactor::ONE_MINUS_SRC_COLOR;
|
|
|
|
case GLBlendConst::SRC_ALPHA:
|
|
|
|
return backend::BlendFactor::SRC_ALPHA;
|
|
|
|
case GLBlendConst::ONE_MINUS_SRC_ALPHA:
|
|
|
|
return backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
case GLBlendConst::DST_COLOR:
|
|
|
|
return backend::BlendFactor::DST_COLOR;
|
|
|
|
case GLBlendConst::ONE_MINUS_DST_COLOR:
|
|
|
|
return backend::BlendFactor::ONE_MINUS_DST_COLOR;
|
|
|
|
case GLBlendConst::DST_ALPHA:
|
|
|
|
return backend::BlendFactor::DST_ALPHA;
|
|
|
|
case GLBlendConst::ONE_MINUS_DST_ALPHA:
|
|
|
|
return backend::BlendFactor::ONE_MINUS_DST_ALPHA;
|
|
|
|
case GLBlendConst::SRC_ALPHA_SATURATE:
|
|
|
|
return backend::BlendFactor::SRC_ALPHA_SATURATE;
|
|
|
|
case GLBlendConst::BLEND_COLOR:
|
|
|
|
return backend::BlendFactor::BLEND_CLOLOR;
|
|
|
|
case GLBlendConst::CONSTANT_ALPHA:
|
|
|
|
return backend::BlendFactor::CONSTANT_ALPHA;
|
|
|
|
case GLBlendConst::ONE_MINUS_CONSTANT_ALPHA:
|
|
|
|
return backend::BlendFactor::ONE_MINUS_CONSTANT_ALPHA;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return backend::BlendFactor::ONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int toGLBlendFactor(backend::BlendFactor blendFactor)
|
|
|
|
{
|
|
|
|
int ret = GLBlendConst::ONE;
|
|
|
|
switch (blendFactor)
|
|
|
|
{
|
|
|
|
case backend::BlendFactor::ZERO:
|
|
|
|
ret = GLBlendConst::ZERO;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::ONE:
|
|
|
|
ret = GLBlendConst::ONE;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::SRC_COLOR:
|
|
|
|
ret = GLBlendConst::SRC_COLOR;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::ONE_MINUS_SRC_COLOR:
|
|
|
|
ret = GLBlendConst::ONE_MINUS_SRC_COLOR;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::SRC_ALPHA:
|
|
|
|
ret = GLBlendConst::SRC_ALPHA;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::ONE_MINUS_SRC_ALPHA:
|
|
|
|
ret = GLBlendConst::ONE_MINUS_SRC_ALPHA;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::DST_COLOR:
|
|
|
|
ret = GLBlendConst::DST_COLOR;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::ONE_MINUS_DST_COLOR:
|
|
|
|
ret = GLBlendConst::ONE_MINUS_DST_COLOR;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::DST_ALPHA:
|
|
|
|
ret = GLBlendConst::DST_ALPHA;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::ONE_MINUS_DST_ALPHA:
|
|
|
|
ret = GLBlendConst::ONE_MINUS_DST_ALPHA;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::SRC_ALPHA_SATURATE:
|
|
|
|
ret = GLBlendConst::SRC_ALPHA_SATURATE;
|
|
|
|
break;
|
|
|
|
case backend::BlendFactor::BLEND_CLOLOR:
|
|
|
|
ret = GLBlendConst::BLEND_COLOR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::SamplerFilter toBackendSamplerFilter(int mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case GLTexParamConst::LINEAR:
|
|
|
|
case GLTexParamConst::LINEAR_MIPMAP_LINEAR:
|
|
|
|
case GLTexParamConst::LINEAR_MIPMAP_NEAREST:
|
|
|
|
case GLTexParamConst::NEAREST_MIPMAP_LINEAR:
|
|
|
|
return backend::SamplerFilter::LINEAR;
|
|
|
|
case GLTexParamConst::NEAREST:
|
|
|
|
case GLTexParamConst::NEAREST_MIPMAP_NEAREST:
|
|
|
|
return backend::SamplerFilter::NEAREST;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "invalid GL sampler filter!");
|
2019-11-23 20:27:39 +08:00
|
|
|
return backend::SamplerFilter::LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::SamplerAddressMode toBackendAddressMode(int mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case GLTexParamConst::REPEAT:
|
|
|
|
return backend::SamplerAddressMode::REPEAT;
|
|
|
|
case GLTexParamConst::CLAMP:
|
|
|
|
case GLTexParamConst::CLAMP_TO_EDGE:
|
|
|
|
return backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
case GLTexParamConst::MIRROR_REPEAT:
|
|
|
|
return backend::SamplerAddressMode::MIRROR_REPEAT;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "invalid GL address mode");
|
2019-11-23 20:27:39 +08:00
|
|
|
return backend::SamplerAddressMode::REPEAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Mat4& getAdjustMatrix()
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
static ax::Mat4 adjustMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0.5, 0, 0, 0, 1};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return adjustMatrix;
|
|
|
|
}
|
|
|
|
|
2023-08-31 18:14:26 +08:00
|
|
|
std::vector<float> getNormalMat3OfMat4(const Mat4& mat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-08-31 18:14:26 +08:00
|
|
|
std::vector<float> normalMat(9);
|
2021-12-06 13:00:44 +08:00
|
|
|
Mat4 mvInverse = mat;
|
2019-11-23 20:27:39 +08:00
|
|
|
mvInverse.m[12] = mvInverse.m[13] = mvInverse.m[14] = 0.0f;
|
|
|
|
mvInverse.inverse();
|
|
|
|
mvInverse.transpose();
|
2023-08-31 18:14:26 +08:00
|
|
|
normalMat[0] = mvInverse.m[0];
|
|
|
|
normalMat[1] = mvInverse.m[1];
|
|
|
|
normalMat[2] = mvInverse.m[2];
|
|
|
|
normalMat[3] = mvInverse.m[4];
|
|
|
|
normalMat[4] = mvInverse.m[5];
|
|
|
|
normalMat[5] = mvInverse.m[6];
|
|
|
|
normalMat[6] = mvInverse.m[8];
|
|
|
|
normalMat[7] = mvInverse.m[9];
|
|
|
|
normalMat[8] = mvInverse.m[10];
|
|
|
|
return normalMat;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
std::vector<int> parseIntegerList(std::string_view intsString)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<int> result;
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
if (!intsString.empty())
|
|
|
|
{
|
|
|
|
const char* cStr = intsString.data();
|
|
|
|
char* endptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-20 08:58:34 +08:00
|
|
|
for (auto i = strtol(cStr, &endptr, 10); endptr != cStr; i = strtol(cStr, &endptr, 10))
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
|
|
|
if (errno == ERANGE)
|
|
|
|
{
|
|
|
|
errno = 0;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN("%s contains out of range integers", intsString.data());
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
2022-08-08 13:18:33 +08:00
|
|
|
result.emplace_back(static_cast<int>(i));
|
2021-12-06 13:00:44 +08:00
|
|
|
cStr = endptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
std::string bin2hex(std::string_view binary /*charstring also regard as binary in C/C++*/, int delim, bool prefix)
|
2020-01-04 22:25:01 +08:00
|
|
|
{
|
|
|
|
char low;
|
|
|
|
char high;
|
|
|
|
size_t len = binary.length();
|
|
|
|
|
|
|
|
bool delim_needed = delim != -1 || delim == ' ';
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result.reserve((len << 1) + (delim_needed ? len : 0) + (prefix ? (len << 1) : 0));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
auto ch = binary[i];
|
2021-12-06 13:00:44 +08:00
|
|
|
high = (ch >> 4) & 0x0f;
|
|
|
|
low = ch & 0x0f;
|
|
|
|
if (prefix)
|
|
|
|
{
|
2020-01-04 22:25:01 +08:00
|
|
|
result.push_back('0');
|
|
|
|
result.push_back('x');
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
auto hic = nibble2hex(high);
|
|
|
|
auto lic = nibble2hex(low);
|
2020-01-04 22:25:01 +08:00
|
|
|
result.push_back(hic);
|
|
|
|
result.push_back(lic);
|
|
|
|
if (delim_needed)
|
|
|
|
{
|
|
|
|
result.push_back(delim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-26 12:46:51 +08:00
|
|
|
void killCurrentProcess()
|
|
|
|
{
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
::kill(::getpid(), SIGKILL);
|
|
|
|
#else
|
|
|
|
::TerminateProcess(::GetCurrentProcess(), SIGTERM);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
std::string urlEncode(std::string_view s)
|
|
|
|
{
|
|
|
|
std::string encoded;
|
2021-12-06 15:02:53 +08:00
|
|
|
if (!s.empty())
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
2021-12-06 15:02:53 +08:00
|
|
|
encoded.reserve(s.length() * 3 / 2);
|
|
|
|
for (const char c : s)
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
2021-12-06 15:02:53 +08:00
|
|
|
if (isalnum((uint8_t)c) || c == '-' || c == '_' || c == '.' || c == '~')
|
|
|
|
{
|
|
|
|
encoded.push_back(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
encoded.push_back('%');
|
|
|
|
|
|
|
|
char hex[2];
|
|
|
|
encoded.append(char2hex(hex, c, 'A'), sizeof(hex));
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string urlDecode(std::string_view st)
|
|
|
|
{
|
|
|
|
std::string decoded;
|
2021-12-06 15:02:53 +08:00
|
|
|
if (!st.empty())
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
2021-12-06 15:02:53 +08:00
|
|
|
const char* s = st.data();
|
|
|
|
const size_t length = st.length();
|
|
|
|
decoded.reserve(length * 2 / 3);
|
|
|
|
for (unsigned int i = 0; i < length; ++i)
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
2021-12-06 15:02:53 +08:00
|
|
|
if (!s[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (s[i] == '%')
|
|
|
|
{
|
|
|
|
decoded.push_back(hex2char(s + i + 1));
|
|
|
|
i = i + 2;
|
|
|
|
}
|
|
|
|
else if (s[i] == '+')
|
|
|
|
{
|
|
|
|
decoded.push_back(' ');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
decoded.push_back(s[i]);
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return decoded;
|
|
|
|
}
|
|
|
|
|
2023-01-01 18:07:18 +08:00
|
|
|
AX_DLL std::string base64Encode(std::string_view s)
|
|
|
|
{
|
|
|
|
size_t n = ax::base64::encoded_size(s.length());
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
std::string ret;
|
2023-01-03 18:01:40 +08:00
|
|
|
/**
|
|
|
|
* @brief resize_and_overrite avaialbe on vs2022 17.1
|
|
|
|
* refer to:
|
|
|
|
* - https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-170
|
|
|
|
* - https://github.com/microsoft/STL/wiki/Changelog#vs-2022-171
|
|
|
|
* - https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
|
|
|
|
*
|
|
|
|
*/
|
2023-02-18 11:39:31 +08:00
|
|
|
#if _AX_HAS_CXX23
|
2023-01-01 18:41:48 +08:00
|
|
|
ret.resize_and_overwrite(n, [&](char* p, size_t) { return ax::base64::encode(p, s.data(), s.length()); });
|
|
|
|
#else
|
|
|
|
ret.resize(n);
|
|
|
|
ret.resize(ax::base64::encode(&ret.front(), s.data(), s.length()));
|
|
|
|
#endif
|
2023-01-01 18:07:18 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return std::string{};
|
|
|
|
}
|
|
|
|
|
|
|
|
AX_DLL std::string base64Decode(std::string_view s)
|
|
|
|
{
|
|
|
|
size_t n = ax::base64::decoded_size(s.length());
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
|
2023-02-18 11:39:31 +08:00
|
|
|
#if _AX_HAS_CXX23
|
2023-01-01 18:41:48 +08:00
|
|
|
ret.resize_and_overwrite(n, [&](char* p, size_t) { return ax::base64::decode(p, s.data(), s.length()); });
|
|
|
|
#else
|
|
|
|
ret.resize(n);
|
|
|
|
ret.resize(ax::base64::decode(&ret.front(), s.data(), s.length()));
|
|
|
|
#endif
|
2023-01-01 18:07:18 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return std::string{};
|
|
|
|
}
|
|
|
|
|
|
|
|
int base64Encode(const unsigned char* in, unsigned int inLength, char** out)
|
|
|
|
{
|
|
|
|
auto n = ax::base64::encoded_size(inLength);
|
|
|
|
// should be enough to store 8-bit buffers in 6-bit buffers
|
2023-01-17 10:06:09 +08:00
|
|
|
char* tmp = nullptr;
|
|
|
|
if (n > 0 && (tmp = (char*)malloc(n + 1)))
|
2023-01-01 18:07:18 +08:00
|
|
|
{
|
2023-01-17 10:06:09 +08:00
|
|
|
auto ret = ax::base64::encode(tmp, in, inLength);
|
|
|
|
tmp[ret] = '\0';
|
|
|
|
*out = tmp;
|
2023-01-01 18:07:18 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2023-01-17 10:06:09 +08:00
|
|
|
*out = nullptr;
|
2023-01-01 18:07:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AX_DLL int base64Decode(const unsigned char* in, unsigned int inLength, unsigned char** out)
|
|
|
|
{
|
|
|
|
size_t n = ax::base64::decoded_size(inLength);
|
2023-01-17 10:06:09 +08:00
|
|
|
unsigned char* tmp = nullptr;
|
|
|
|
if (n > 0 && (tmp = (unsigned char*)malloc(n)))
|
|
|
|
{
|
|
|
|
n = static_cast<int>(ax::base64::decode(tmp, reinterpret_cast<const char*>(in), inLength));
|
|
|
|
if (n > 0)
|
|
|
|
*out = tmp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out = nullptr;
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
*out = nullptr;
|
2023-01-01 18:07:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL uint32_t fourccValue(std::string_view str)
|
2022-06-17 22:16:55 +08:00
|
|
|
{
|
|
|
|
if (str.empty() || str[0] != '#')
|
|
|
|
return (uint32_t)-1;
|
|
|
|
uint32_t value = 0;
|
|
|
|
memcpy(&value, str.data() + 1, std::min(sizeof(value), str.size() - 1));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
} // namespace utils
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|