Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Install textures as a zip if supported #16314

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Core/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@ bool TextureReplacer::PopulateLevelFromZip(ReplacedTextureLevel &level, bool ign
if (zsize != INVALID_ZIP_SIZE)
pngdata.resize(zsize);
if (!pngdata.empty()) {
pngdata.resize(zip_fread(zf, pngdata.data(), pngdata.size()));
pngdata.resize(zip_fread(zf, &pngdata[0], pngdata.size()));
}

if (png_image_begin_read_from_memory(&png, pngdata.data(), pngdata.size())) {
if (png_image_begin_read_from_memory(&png, &pngdata[0], pngdata.size())) {
// We pad files that have been hashrange'd so they are the same texture size.
level.w = png.width;
level.h = png.height;
Expand Down Expand Up @@ -1168,10 +1168,10 @@ void ReplacedTexture::PrepareData(int level) {
if (zsize != INVALID_ZIP_SIZE)
pngdata.resize(zsize);
if (!pngdata.empty()) {
pngdata.resize(zip_fread(zf, pngdata.data(), pngdata.size()));
pngdata.resize(zip_fread(zf, &pngdata[0], pngdata.size()));
}

if (!png_image_begin_read_from_memory(&png, pngdata.data(), pngdata.size())) {
if (!png_image_begin_read_from_memory(&png, &pngdata[0], pngdata.size())) {
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s (zip)", info.file.c_str(), png.message);
cleanup();
return;
Expand Down
57 changes: 57 additions & 0 deletions Core/Util/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ bool GameManager::InstallGame(Path url, Path fileName, bool deleteAfter) {
if (DetectTexturePackDest(z, info.textureIniIndex, dest)) {
INFO_LOG(HLE, "Installing '%s' into '%s'", fileName.c_str(), dest.c_str());
File::CreateFullPath(dest);
// Install as a zip file if textures.ini is in the root. Performs better on Android.
if (info.stripChars == 0)
return InstallMemstickZip(z, fileName, dest / "textures.zip", info, deleteAfter);
File::CreateEmptyFile(dest / ".nomedia");
return InstallMemstickGame(z, fileName, dest, info, true, deleteAfter);
} else {
Expand Down Expand Up @@ -617,6 +620,60 @@ bool GameManager::InstallMemstickGame(struct zip *z, const Path &zipfile, const
return false;
}

bool GameManager::InstallMemstickZip(struct zip *z, const Path &zipfile, const Path &dest, const ZipFileInfo &info, bool deleteAfter) {
size_t allBytes = 0;
size_t bytesCopied = 0;

auto sy = GetI18NCategory("System");

// We don't need the zip anymore, as we're going to copy it as-is.
zip_close(z);
z = nullptr;

// Not using File::Copy() so we can report progress.
FILE *inf = File::OpenCFile(zipfile, "rb");
if (!inf)
return false;

allBytes = (size_t)File::GetFileSize(inf);
FILE *outf = File::OpenCFile(dest, "wb");
if (!outf) {
SetInstallError(sy->T("Storage full"));
fclose(inf);
return false;
}

const size_t blockSize = 1024 * 128;
u8 *buffer = new u8[blockSize];
while (bytesCopied < allBytes) {
size_t readSize = std::min(blockSize, allBytes - bytesCopied);
if (fread(buffer, readSize, 1, inf) != 1)
break;
if (fwrite(buffer, readSize, 1, outf) != 1)
break;
bytesCopied += readSize;
installProgress_ = (float)allBytes / (float)allBytes;
}

delete[] buffer;
fclose(inf);
fclose(outf);

if (bytesCopied < allBytes) {
File::Delete(dest);
SetInstallError(sy->T("Storage full"));
return false;
}

installProgress_ = 1.0f;
if (deleteAfter) {
File::Delete(zipfile);
}
InstallDone();
ResetInstallError();
return true;
}

bool GameManager::InstallZippedISO(struct zip *z, int isoFileIndex, const Path &zipfile, bool deleteAfter) {
// Let's place the output file in the currently selected Games directory.

Expand Down
1 change: 1 addition & 0 deletions Core/Util/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class GameManager {
private:
bool InstallGame(Path url, Path tempFileName, bool deleteAfter);
bool InstallMemstickGame(struct zip *z, const Path &zipFile, const Path &dest, const ZipFileInfo &info, bool allowRoot, bool deleteAfter);
bool InstallMemstickZip(struct zip *z, const Path &zipFile, const Path &dest, const ZipFileInfo &info, bool deleteAfter);
bool InstallZippedISO(struct zip *z, int isoFileIndex, const Path &zipfile, bool deleteAfter);
bool InstallRawISO(const Path &zipFile, const std::string &originalName, bool deleteAfter);
void InstallDone();
Expand Down