Files
OpenRCT2-Web/src/openrct2/core/ZipAndroid.cpp
T

203 lines
6.2 KiB
C++
Raw Normal View History

2017-06-15 14:22:15 +02:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2017-06-15 14:22:15 +02:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-06-15 14:22:15 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-06-15 14:22:15 +02:00
*****************************************************************************/
#ifdef __ANDROID__
# include "../platform/platform.h"
2018-07-21 16:17:06 +02:00
# include "IStream.hpp"
# include "Zip.h"
2017-06-15 14:22:15 +02:00
2018-07-21 16:17:06 +02:00
# include <SDL.h>
# include <jni.h>
2018-06-22 22:58:39 +02:00
2018-05-02 13:15:26 +01:00
class ZipArchive final : public IZipArchive
{
2017-06-15 14:22:15 +02:00
private:
jobject _zip;
public:
2021-01-07 01:22:22 +09:00
ZipArchive(std::string_view path, ZIP_ACCESS access)
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass jniClass = platform_android_find_class(env, "io/openrct2/ZipArchive");
2017-06-15 14:22:15 +02:00
jmethodID constructor = env->GetMethodID(jniClass, "<init>", "(Ljava/lang/String;)V");
2018-05-02 13:15:26 +01:00
jstring jniPath = env->NewStringUTF(path.data());
2017-06-15 14:22:15 +02:00
// TODO: Catch exceptions. Should probably be done on Java side, and just return null from a static method
jobject zip = env->NewObject(jniClass, constructor, jniPath);
_zip = env->NewGlobalRef(zip);
}
2018-05-02 13:15:26 +01:00
~ZipArchive() override
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass zipClass = env->GetObjectClass(_zip);
jmethodID closeMethod = env->GetMethodID(zipClass, "close", "()V");
env->CallVoidMethod(_zip, closeMethod);
env->DeleteGlobalRef(_zip);
}
2018-05-02 13:15:26 +01:00
size_t GetNumFiles() const override
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass zipClass = env->GetObjectClass(_zip);
jmethodID fileCountMethod = env->GetMethodID(zipClass, "getNumFiles", "()I");
return static_cast<size_t>(env->CallIntMethod(_zip, fileCountMethod));
2017-06-15 14:22:15 +02:00
}
2018-05-02 13:15:26 +01:00
std::string GetFileName(size_t index) const override
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass zipClass = env->GetObjectClass(_zip);
2018-06-22 22:58:39 +02:00
jmethodID fileNameMethod = env->GetMethodID(zipClass, "getFileName", "(I)Ljava/lang/String;");
2017-06-15 14:22:15 +02:00
2018-06-22 22:58:39 +02:00
jstring jniString = (jstring)env->CallObjectMethod(_zip, fileNameMethod, (jint)index);
2017-06-15 14:22:15 +02:00
2018-06-22 22:58:39 +02:00
const char* jniChars = env->GetStringUTFChars(jniString, nullptr);
2017-06-15 14:22:15 +02:00
2018-06-22 22:58:39 +02:00
utf8* string = (char*)malloc(strlen(jniChars) + 1);
std::memcpy((void*)string, jniChars, strlen(jniChars));
2017-06-15 14:22:15 +02:00
string[strlen(jniChars)] = 0x00;
env->ReleaseStringUTFChars(jniString, jniChars);
return string;
}
uint64_t GetFileSize(size_t index) const override
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass zipClass = env->GetObjectClass(_zip);
jmethodID fileSizeMethod = env->GetMethodID(zipClass, "getFileSize", "(I)J");
2018-06-22 22:58:39 +02:00
return (size_t)env->CallLongMethod(_zip, fileSizeMethod, (jint)index);
2017-06-15 14:22:15 +02:00
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> GetFileData(std::string_view path) const override
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
// retrieve the JNI environment.
2018-06-22 22:58:39 +02:00
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
2017-06-15 14:22:15 +02:00
jclass zipClass = env->GetObjectClass(_zip);
2018-05-02 13:15:26 +01:00
jstring javaPath = env->NewStringUTF(path.data());
2017-06-15 14:22:15 +02:00
jmethodID indexMethod = env->GetMethodID(zipClass, "getFileIndex", "(Ljava/lang/String;)I");
jint index = env->CallIntMethod(_zip, indexMethod, javaPath);
jmethodID fileMethod = env->GetMethodID(zipClass, "getFile", "(I)J");
jlong ptr = env->CallLongMethod(_zip, fileMethod, index);
2018-06-22 22:58:39 +02:00
auto dataPtr = reinterpret_cast<uint8_t*>(ptr);
2018-05-02 13:15:26 +01:00
auto dataSize = this->GetFileSize(index);
return std::vector<uint8_t>(dataPtr, dataPtr + dataSize);
2017-06-15 14:22:15 +02:00
}
2021-01-09 20:54:02 +00:00
std::unique_ptr<std::istream> GetFileStream(std::string_view path) const override
{
auto data = GetFileData(path);
return std::make_unique<memstream>(std::move(data));
}
2021-01-07 01:22:22 +09:00
void SetFileData(std::string_view path, std::vector<uint8_t>&& data) override
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
STUB();
}
2021-01-07 01:22:22 +09:00
void DeleteFile(std::string_view) override
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
STUB();
}
2021-01-07 01:22:22 +09:00
void RenameFile(std::string_view, std::string_view) override
2018-05-02 13:15:26 +01:00
{
2017-06-15 14:22:15 +02:00
STUB();
}
2021-01-09 20:54:02 +00:00
private:
class memstream final : public std::istream
{
private:
class vector_streambuf : public std::basic_streambuf<char, std::char_traits<char>>
{
public:
explicit vector_streambuf(const std::vector<uint8_t>& vec)
{
this->setg(
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data() + vec.size())));
}
};
std::vector<uint8_t> _data;
vector_streambuf _streambuf;
public:
memstream(std::vector<uint8_t>&& data)
: std::istream(&_streambuf)
, _data(data)
, _streambuf(_data)
{
}
};
2017-06-15 14:22:15 +02:00
};
2018-05-02 13:15:26 +01:00
namespace Zip
{
2021-01-07 01:22:22 +09:00
std::unique_ptr<IZipArchive> Open(std::string_view path, ZIP_ACCESS access)
2018-05-02 13:15:26 +01:00
{
2018-05-09 15:59:05 +02:00
return std::make_unique<ZipArchive>(path, access);
2017-06-15 14:22:15 +02:00
}
2021-01-07 01:22:22 +09:00
std::unique_ptr<IZipArchive> TryOpen(std::string_view path, ZIP_ACCESS access)
2018-05-02 13:15:26 +01:00
{
std::unique_ptr<IZipArchive> result;
try
{
2018-05-09 15:59:05 +02:00
result = std::make_unique<ZipArchive>(path, access);
2017-06-15 14:22:15 +02:00
}
2018-05-02 13:15:26 +01:00
catch (const std::exception&)
{
2017-06-15 14:22:15 +02:00
}
return result;
}
2018-06-22 22:58:39 +02:00
} // namespace Zip
2017-06-15 14:22:15 +02:00
extern "C" {
JNIEXPORT jlong JNICALL Java_io_openrct2_ZipArchive_allocBytes(JNIEnv* env, jclass, jbyteArray input, jint numBytes);
}
2017-06-15 14:22:15 +02:00
JNIEXPORT jlong JNICALL Java_io_openrct2_ZipArchive_allocBytes(JNIEnv* env, jclass, jbyteArray input, jint numBytes)
2018-06-22 22:58:39 +02:00
{
jbyte* bufferPtr = env->GetByteArrayElements(input, nullptr);
2017-06-15 14:22:15 +02:00
2018-06-22 22:58:39 +02:00
void* data = Memory::Allocate<void>((size_t)numBytes);
std::memcpy(data, bufferPtr, numBytes);
2017-06-15 14:22:15 +02:00
env->ReleaseByteArrayElements(input, bufferPtr, 0);
return reinterpret_cast<uintptr_t>(data);
2017-06-15 14:22:15 +02:00
}
#endif // __ANDROID__