2020-08-27 00:21:44 +08:00
|
|
|
// Copyright (c) 2018-2019 HALX99
|
2022-04-25 19:15:46 +08:00
|
|
|
// Copyright (c) 2020 C4games Ltd
|
2019-11-24 14:54:45 +08:00
|
|
|
#pragma once
|
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
#include "platform/IFileStream.h"
|
|
|
|
#include "platform/PlatformConfig.h"
|
2019-11-24 14:54:45 +08:00
|
|
|
#include <string>
|
2023-08-25 16:49:08 +08:00
|
|
|
#include <functional>
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformMacros.h"
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
|
|
|
# include <android/asset_manager.h>
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
struct ZipEntryInfo;
|
|
|
|
|
|
|
|
union PXFileHandle
|
|
|
|
{
|
|
|
|
void* value;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
osfhnd_t osfh; // underlying OS file HANDLE
|
|
|
|
bool append; // append mode?
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
osfhnd_t osfh = -1; // underlying OS file HANDLE
|
|
|
|
#endif
|
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
|
|
|
AAsset* aasset;
|
|
|
|
ZipEntryInfo* zentry;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PXIoF;
|
|
|
|
|
|
|
|
class AX_DLL FileStream : public IFileStream
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
public:
|
2023-08-25 16:49:08 +08:00
|
|
|
FileStream() = default;
|
|
|
|
virtual ~FileStream();
|
|
|
|
|
|
|
|
FileStream(const FileStream& other) = delete;
|
2020-08-27 11:35:55 +08:00
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
FileStream(FileStream&& other) noexcept
|
|
|
|
: IFileStream(std::move(other)), _handle(std::move(other._handle)), _iof(other._iof)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-08-25 16:49:08 +08:00
|
|
|
other.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileStream& operator=(const FileStream& other) = delete;
|
|
|
|
|
|
|
|
FileStream& operator=(FileStream&& other) noexcept
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
IFileStream::operator=(std::move(other));
|
|
|
|
_handle = std::move(other._handle);
|
|
|
|
_iof = other._iof;
|
|
|
|
|
|
|
|
other.reset();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool open(std::string_view path, IFileStream::Mode mode) override;
|
|
|
|
int close() override;
|
|
|
|
|
|
|
|
int64_t seek(int64_t offset, int origin) override;
|
|
|
|
int read(void* buf, unsigned int size) override;
|
|
|
|
int write(const void* buf, unsigned int size) override;
|
|
|
|
int64_t size() override;
|
|
|
|
bool resize(int64_t size) override;
|
|
|
|
bool isOpen() const override;
|
|
|
|
|
|
|
|
osfhnd_t nativeHandle() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int internalClose();
|
|
|
|
void reset() { _iof = nullptr; }
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
PXFileHandle _handle{};
|
|
|
|
const PXIoF* _iof{nullptr};
|
2021-04-22 19:49:43 +08:00
|
|
|
};
|
|
|
|
|
2023-03-26 22:42:13 +08:00
|
|
|
NS_AX_END
|