2021-04-22 22:01:32 +08:00
|
|
|
// Copyright (c) 2018-2019 HALX99
|
2022-04-25 19:15:46 +08:00
|
|
|
// Copyright (c) 2020 C4games Ltd
|
2021-04-22 22:01:32 +08:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/FileStream.h"
|
|
|
|
#include "platform/PlatformConfig.h"
|
2023-06-11 14:39:19 +08:00
|
|
|
#include "base/posix_io.h"
|
2021-04-22 22:01:32 +08:00
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformMacros.h"
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "platform/android/FileUtils-android.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <jni.h>
|
|
|
|
# include <android/asset_manager.h>
|
|
|
|
# include <android/asset_manager_jni.h>
|
|
|
|
# include "base/ZipUtils.h"
|
2021-04-22 22:01:32 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2021-04-22 22:01:32 +08:00
|
|
|
|
|
|
|
struct UnzFileStream;
|
2021-12-25 10:04:45 +08:00
|
|
|
union PXFileHandle
|
|
|
|
{
|
2021-04-22 22:01:32 +08:00
|
|
|
int _fd = -1;
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
|
2021-04-22 22:01:32 +08:00
|
|
|
AAsset* _asset;
|
|
|
|
ZipFileStream _zfs;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PXIoF;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL PosixFileStream : public FileStream
|
2021-04-22 22:01:32 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream() = default;
|
|
|
|
virtual ~PosixFileStream();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream(const PosixFileStream& other) = delete;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream(PosixFileStream&& other) noexcept
|
2021-12-25 10:04:45 +08:00
|
|
|
: FileStream(std::move(other)), _handle(std::move(other._handle)), _iof(other._iof)
|
2021-04-24 07:22:27 +08:00
|
|
|
{
|
|
|
|
other.reset();
|
|
|
|
}
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream& operator=(const PosixFileStream& other) = delete;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream& operator=(PosixFileStream&& other) noexcept
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
2021-12-25 10:04:45 +08:00
|
|
|
FileStream::operator=(std::move(other));
|
2022-11-10 13:45:51 +08:00
|
|
|
_handle = std::move(other._handle);
|
|
|
|
_iof = other._iof;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
other.reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class Mode
|
|
|
|
{
|
2021-04-24 07:22:27 +08:00
|
|
|
READ,
|
|
|
|
WRITE,
|
|
|
|
APPEND,
|
|
|
|
};
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool open(std::string_view path, FileStream::Mode mode) override;
|
2021-04-24 07:22:27 +08:00
|
|
|
int close() override;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
int seek(int64_t offset, int origin) override;
|
2021-04-24 07:22:27 +08:00
|
|
|
int read(void* buf, unsigned int size) override;
|
|
|
|
int write(const void* buf, unsigned int size) override;
|
2021-08-20 01:20:17 +08:00
|
|
|
int64_t tell() override;
|
|
|
|
int64_t size() override;
|
2021-04-24 07:22:27 +08:00
|
|
|
bool isOpen() const override;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2023-03-26 22:42:13 +08:00
|
|
|
void* getNativeHandle() const override;
|
|
|
|
|
2021-04-22 22:01:32 +08:00
|
|
|
private:
|
2021-04-24 07:22:27 +08:00
|
|
|
int internalClose();
|
|
|
|
void reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-26 08:32:25 +08:00
|
|
|
PXFileHandle _handle{};
|
|
|
|
const PXIoF* _iof{};
|
2021-04-22 22:01:32 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|