2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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 "3d/CCBundleReader.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
BundleReader::BundleReader()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_buffer = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
_position = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_length = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
BundleReader::~BundleReader(){
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void BundleReader::init(char* buffer, ssize_t length)
|
|
|
|
{
|
|
|
|
_position = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_buffer = buffer;
|
|
|
|
_length = length;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t BundleReader::read(void* ptr, ssize_t size, ssize_t count)
|
|
|
|
{
|
|
|
|
if (!_buffer || eof())
|
|
|
|
{
|
|
|
|
CCLOG("warning: bundle reader out of range");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t validCount;
|
|
|
|
ssize_t validLength = _length - _position;
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t needLength = size * count;
|
|
|
|
char* ptr1 = (char*)ptr;
|
|
|
|
if (validLength < needLength)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
validCount = validLength / size;
|
|
|
|
ssize_t readLength = size * validCount;
|
|
|
|
memcpy(ptr1, (char*)_buffer + _position, readLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
ptr1 += readLength;
|
|
|
|
_position += readLength;
|
|
|
|
readLength = validLength - readLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (readLength > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memcpy(ptr1, (char*)_buffer + _position, readLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
_position += readLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
validCount += 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
CCLOG("warning: bundle reader out of range");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
memcpy(ptr1, (char*)_buffer + _position, needLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
_position += needLength;
|
|
|
|
validCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return validCount;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
char* BundleReader::readLine(int num, char* line)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!_buffer)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
char* buffer = (char*)_buffer + _position;
|
|
|
|
char* p = line;
|
2019-11-23 20:27:39 +08:00
|
|
|
char c;
|
|
|
|
ssize_t readNum = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
while ((c = *buffer) != 10 && readNum < (ssize_t)num && _position < _length)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
*p = c;
|
|
|
|
p++;
|
|
|
|
buffer++;
|
|
|
|
_position++;
|
|
|
|
readNum++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::eof()
|
|
|
|
{
|
|
|
|
if (!_buffer)
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ((ssize_t)tell()) >= length();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t BundleReader::length()
|
|
|
|
{
|
|
|
|
return _length;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t BundleReader::tell()
|
|
|
|
{
|
|
|
|
if (!_buffer)
|
|
|
|
return -1;
|
|
|
|
return _position;
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
bool BundleReader::seek(int32_t offset, int origin)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!_buffer)
|
|
|
|
return false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (origin == SEEK_CUR)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_position += offset;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (origin == SEEK_SET)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_position = offset;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (origin == SEEK_END)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_position = _length + offset;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::rewind()
|
|
|
|
{
|
|
|
|
if (_buffer != nullptr)
|
|
|
|
{
|
|
|
|
_position = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BundleReader::readString()
|
|
|
|
{
|
|
|
|
unsigned int length;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (read(&length, 4, 1) != 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t validLength = _length - _position;
|
|
|
|
if (length > 0 && static_cast<ssize_t>(length) <= validLength)
|
|
|
|
{
|
|
|
|
str.resize(length);
|
|
|
|
if (read(&str[0], 1, length) != length)
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BundleReader::readMatrix(float* m)
|
|
|
|
{
|
|
|
|
return (read(m, sizeof(float), 16) == 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|