1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-30 02:05:13 +01:00

Cleanup MemoryStream code (#22593)

* Deduplicate code, use std::bit_ceil to compute new capacity

* Remove unused constructor overload

* Remove more unused functions

* Fix memory leak using assignment operator with move

* Make access explicit via constness, do not allow arbitrary access

* Move the template specialized Write/Read to private section

* Simplify a lot of code by using the right types

* Fix copy constructor

* Directly copy the member in copy constructor

* Fix little mistake

* Pluck a memory leak on Android, fix the build

* Update changelog.txt
This commit is contained in:
Matt
2024-08-22 20:24:00 +03:00
committed by GitHub
parent 9105fe0804
commit 590ab65b2a
5 changed files with 112 additions and 116 deletions

View File

@@ -111,13 +111,31 @@ public:
auto dataPtr = reinterpret_cast<uint8_t*>(ptr);
auto dataSize = this->GetFileSize(index);
return std::vector<uint8_t>(dataPtr, dataPtr + dataSize);
auto res = std::vector<uint8_t>(dataPtr, dataPtr + dataSize);
Memory::Free(dataPtr);
return res;
}
std::unique_ptr<IStream> GetFileStream(std::string_view path) const override
{
auto data = GetFileData(path);
return std::make_unique<MemoryStream>(std::move(data));
// retrieve the JNI environment.
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
jclass zipClass = env->GetObjectClass(_zip);
jstring javaPath = env->NewStringUTF(path.data());
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);
auto dataPtr = reinterpret_cast<uint8_t*>(ptr);
auto dataSize = this->GetFileSize(index);
// The Java code for getFile uses Java_io_openrct2_ZipArchive_allocBytes which
// allocates memory using Memory::Allocate so its safe for MemoryStream to take full ownershp.
return std::make_unique<MemoryStream>(dataPtr, dataSize, true);
}
void SetFileData(std::string_view path, std::vector<uint8_t>&& data) override