From cd671cce486f39cc13597ec34c809a6c276a252e Mon Sep 17 00:00:00 2001 From: biroder <107300789+biroder@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:59:38 +0300 Subject: [PATCH] * i18n improving for Android [ci skip] --- app/app/build.gradle | 25 +++- .../github/doukutsu_rs/DownloadActivity.java | 126 +++++++++++++----- .../io/github/doukutsu_rs/MainActivity.java | 2 +- app/app/src/main/res/values/strings.xml | 13 ++ 4 files changed, 121 insertions(+), 45 deletions(-) diff --git a/app/app/build.gradle b/app/app/build.gradle index f1096ea..e3c2d8f 100644 --- a/app/app/build.gradle +++ b/app/app/build.gradle @@ -19,7 +19,7 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" ndk { - abiFilters 'x86', 'arm64-v8a', 'armeabi-v7a' + abiFilters 'arm64-v8a' stl = "c++_shared" } @@ -43,6 +43,15 @@ android { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + + ndk { + abiFilters 'x86', 'arm64-v8a', 'armeabi-v7a' + stl = "c++_shared" + } + } + debug { + jniDebuggable true + renderscriptDebuggable true } } @@ -80,13 +89,7 @@ println("cargo target: ${project.buildDir.getAbsolutePath()}/rust-target") println("ndk dir: ${android.ndkDirectory}") cargoNdk { - targets = [ - "x86", - "arm", - "arm64" - ] librariesNames = ["libdrsandroid.so"] - //targetDirectory = "${project.buildDir.getAbsolutePath()}/rust-target" module = "../drsandroid/" extraCargoEnv = ["ANDROID_NDK_HOME": android.ndkDirectory] extraCargoBuildArguments = [] @@ -95,9 +98,17 @@ cargoNdk { buildTypes { release { buildType = "release" + targets = [ + "x86", + "arm", + "arm64" + ] } debug { buildType = "debug" + targets = [ + "arm64" + ] } } } diff --git a/app/app/src/main/java/io/github/doukutsu_rs/DownloadActivity.java b/app/app/src/main/java/io/github/doukutsu_rs/DownloadActivity.java index b728152..c5b403f 100644 --- a/app/app/src/main/java/io/github/doukutsu_rs/DownloadActivity.java +++ b/app/app/src/main/java/io/github/doukutsu_rs/DownloadActivity.java @@ -10,7 +10,7 @@ import android.widget.TextView; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; -import java.util.Locale; +import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -41,18 +41,32 @@ public class DownloadActivity extends AppCompatActivity { } private class DownloadThread extends Thread { - private static final String DOWNLOAD_URL = "https://www.cavestory.org/downloads/cavestoryen.zip"; + private final ArrayList urls = new ArrayList<>(); + + private final ArrayList filesWhitelist = new ArrayList<>(); @Override public void run() { + this.filesWhitelist.add("data/"); + this.filesWhitelist.add("Doukutsu.exe"); + + //BE CAREFUL, DON'T SET `true` VALUE FOR TRANSLATIONS + this.urls.add(new DownloadEntry(R.string.download_entries_base, "https://www.cavestory.org/downloads/cavestoryen.zip", true)); + + for (DownloadEntry entry : this.urls) { + this.download(entry); + } + } + + private void download(DownloadEntry downloadEntry) { HttpURLConnection connection = null; try { - URL url = new URL(DOWNLOAD_URL); + URL url = new URL(downloadEntry.url); connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { - throw new IllegalStateException("Bad HTTP response code: " + connection.getResponseCode()); + throw new IllegalStateException(getString(R.string.download_status_error_http, connection.getResponseCode())); } int fileLength = connection.getContentLength(); @@ -80,8 +94,8 @@ public class DownloadActivity extends AppCompatActivity { if (last + 1000 >= now) { int speed = (int) ((downloaded - downloadedLast) / 1024.0); String text = (fileLength > 0) - ? String.format(Locale.ENGLISH, "Downloading... %d%% (%d/%d KiB, %d KiB/s)", downloaded * 100 / fileLength, downloaded / 1024, fileLength / 1024, speed) - : String.format(Locale.ENGLISH, "Downloading... --%% (%d KiB, %d KiB/s)", downloaded / 1024, speed); + ? getString(R.string.download_status_downloading, downloadEntry.name, downloaded * 100 / fileLength, downloaded / 1024, fileLength / 1024, speed) + : getString(R.string.download_status_downloading_null, downloadEntry.name, downloaded / 1024, speed); handler.post(() -> txtProgress.setText(text)); @@ -96,36 +110,9 @@ public class DownloadActivity extends AppCompatActivity { } new File(basePath).mkdirs(); - try (ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zipFile))) { - ZipEntry entry; - byte[] buffer = new byte[4096]; - while ((entry = in.getNextEntry()) != null) { - String entryName = entry.getName(); + this.unpack(zipFile, downloadEntry.isBase); - // strip prefix - if (entryName.startsWith("CaveStory/")) { - entryName = entryName.substring("CaveStory/".length()); - } - - final String s = entryName; - handler.post(() -> txtProgress.setText("Unpacking: " + s)); - - if (entry.isDirectory()) { - new File(basePath + entryName).mkdirs(); - } else { - try (FileOutputStream fos = new FileOutputStream(basePath + entryName)) { - int count; - while ((count = in.read(buffer)) != -1) { - fos.write(buffer, 0, count); - } - } - } - - in.closeEntry(); - } - } - - handler.post(() -> txtProgress.setText("Done!")); + handler.post(() -> txtProgress.setText(getString(R.string.download_status_done))); handler.post(() -> { Intent intent = new Intent(DownloadActivity.this, GameActivity.class); @@ -134,8 +121,8 @@ public class DownloadActivity extends AppCompatActivity { DownloadActivity.this.finish(); }); } catch (Exception e) { - handler.post(() -> { - if (txtProgress != null) + handler.post(() -> { + if (txtProgress != null) txtProgress.setText(e.getMessage()); }); e.printStackTrace(); @@ -143,5 +130,70 @@ public class DownloadActivity extends AppCompatActivity { if (connection != null) connection.disconnect(); } } + + private void unpack(byte[] zipFile, boolean isBase) throws IOException { + ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zipFile)); + ZipEntry entry; + byte[] buffer = new byte[4096]; + while ((entry = in.getNextEntry()) != null) { + String entryName = entry.getName(); + + // strip prefix + if (entryName.startsWith("CaveStory/")) { + entryName = entryName.substring("CaveStory/".length()); + } + + if (!this.entryInWhitelist(entryName)) { + continue; + } + + + final String s = entryName; + handler.post(() -> txtProgress.setText( + getString(R.string.download_status_unpacking, s) + )); + + if (entry.isDirectory()) { + new File(basePath + entryName).mkdirs(); + } else { + try (FileOutputStream fos = new FileOutputStream(basePath + entryName)) { + int count; + while ((count = in.read(buffer)) != -1) { + fos.write(buffer, 0, count); + } + } + } + + in.closeEntry(); + } + } + + private boolean entryInWhitelist(String entry) { + for (String file : this.filesWhitelist) { + if (entry.startsWith(file)) { + return true; + } + } + + return false; + } + } + + private class DownloadEntry { + public String name; //e.g. "Polish translation", "Base data files" + public String url; + public boolean isBase = false; + + DownloadEntry(String name, String url, boolean isBase) { + this.name = name; + this.url = url; + this.isBase = isBase; + } + + DownloadEntry(int name, String url, boolean isBase) { + this.name = getString(name); + this.url = url; + this.isBase = isBase; + } } } diff --git a/app/app/src/main/java/io/github/doukutsu_rs/MainActivity.java b/app/app/src/main/java/io/github/doukutsu_rs/MainActivity.java index e9bea3f..99bafe5 100644 --- a/app/app/src/main/java/io/github/doukutsu_rs/MainActivity.java +++ b/app/app/src/main/java/io/github/doukutsu_rs/MainActivity.java @@ -18,7 +18,7 @@ public class MainActivity extends AppCompatActivity { File f = new File(getFilesDir().getAbsolutePath() + "/data/"); String[] list = f.list(); if (!f.exists() || (list != null && list.length == 0)) { - messageBox("Missing data files", "No data files found, would you like to download them?", () -> { + messageBox(getString(R.string.download_title), getString(R.string.download_desc), () -> { Intent intent = new Intent(this, DownloadActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); diff --git a/app/app/src/main/res/values/strings.xml b/app/app/src/main/res/values/strings.xml index 69fc23f..4a21e77 100644 --- a/app/app/src/main/res/values/strings.xml +++ b/app/app/src/main/res/values/strings.xml @@ -1,4 +1,17 @@ doukutsu-rs doukutsu-rs game data + + Missing data files + No data files found, would you like to download them? + + Bad HTTP response code: %d + + Downloading %s… %d%% (%d/%d KiB, %d KiB/s) + Downloading %s… --%% (%d KiB, %d KiB/s) + Unpacking: %s + Done! + + + base game files