Files
SahikandClaude Opus 5.5 ddd5fdc28b Rebuild the game as a single code-driven scene with a package-folder pack source
- One empty scene; App builds the canvas, safe-area layout and a shared 5x6
  Grid in code. Pages (Intro, Main, Choose, Play, Account, Settings) replace
  5 scenes, 5 prefabs and the per-cell spawner/display components.
- Packs: the game reads Rhyculator/package/ (with All files access) or the
  app folder, like a plugged-in USB stick. Zips stay where they are and are
  extracted to a cache keyed by name/size/mtime; folders are read directly.
  Scans run every second on a background thread. Old songs/ imports and inbox
  zips are migrated automatically.
- Download packs by 8-char code from the new pack website, or via
  rhyculator://pack/<code> links; opened/shared zips go into the package folder.
- Play: pause/resume/retry, auto-pause on background, combo and progress bar;
  judging and scoring unchanged. Choose page fits 9:16 screens; empty state,
  pack delete. Android back key handled everywhere.
- Slimming: removed TextMesh Pro, unused 2D/Timeline/VisualScripting/etc.
  packages and modules, scene template, unused sprites; menu theme wav (20MB)
  -> ogg (2.3MB). Build profile replaced by explicit settings in BuildAndroid.
- Tests: PackLibrary/PackReader/migration/serializer tests (39 passing).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-24 14:18:54 +08:00

150 lines
5.8 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.rhyculator;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.OpenableColumns;
import android.provider.Settings;
import com.unity3d.player.UnityPlayerGameActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
// 遊戲的 activity:
// - 收到「開啟 / 分享」進來的 zip:在背景執行緒複製到曲包資料夾(package/),C# 端掃描資料夾時就會讀到
// - 曲包網站的連結 rhyculator://pack/<code>:把代碼寫到 app 資料夾的 pending-download.txt,C# 端負責下載
// - 曲包資料夾的位置與權限(C# 的 PackStorage 呼叫這裡的 static 方法)
public class RhyculatorActivity extends UnityPlayerGameActivity {
// 與 C# 的 PackStorage 一致
static final String ERROR_FILE = "import-error.txt";
static final String PENDING_DOWNLOAD_FILE = "pending-download.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
receive(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
receive(intent);
}
// ---------- 曲包資料夾 ----------
// 有「所有檔案存取權」(Android 11+)或儲存權限(Android 10 以下)時,曲包放在手機根目錄的 Rhyculator/package
public static boolean hasStorageAccess(Context ctx) {
if (Build.VERSION.SDK_INT >= 30) return Environment.isExternalStorageManager();
return ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
public static void requestStorageAccess(Activity activity) {
if (Build.VERSION.SDK_INT >= 30) {
try {
Intent i = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
Uri.parse("package:" + activity.getPackageName()));
activity.startActivity(i);
} catch (Exception e) {
activity.startActivity(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION));
}
} else {
activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
static File packageDir(Context ctx) {
File root = hasStorageAccess(ctx)
? new File(Environment.getExternalStorageDirectory(), "Rhyculator")
: ctx.getExternalFilesDir(null);
File dir = new File(root, "package");
dir.mkdirs();
return dir;
}
// ---------- 收到的 intent ----------
private void receive(Intent intent) {
if (intent == null) return;
Uri uri = null;
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
uri = intent.getData();
} else if (Intent.ACTION_SEND.equals(intent.getAction())) {
uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
if (uri == null) return;
// 標記為已處理,避免 activity 重建時又處理一次
intent.setAction(Intent.ACTION_MAIN);
intent.setData(null);
intent.removeExtra(Intent.EXTRA_STREAM);
final File appDir = getExternalFilesDir(null);
if (appDir == null) return;
if ("rhyculator".equals(uri.getScheme())) {
// rhyculator://pack/<code>
String code = uri.getLastPathSegment();
if (code != null) writeText(new File(appDir, PENDING_DOWNLOAD_FILE), code);
return;
}
final Uri source = uri;
final File dir = packageDir(this);
final String name = displayName(uri);
new Thread(() -> copy(source, dir, name, appDir), "PackReceive").start();
}
private String displayName(Uri uri) {
String name = null;
try (Cursor c = getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
if (c != null && c.moveToFirst()) name = c.getString(0);
} catch (Exception ignored) {
}
if (name == null) name = uri.getLastPathSegment();
if (name == null) name = "pack";
name = name.replaceAll("[\\\\/:*?\"<>|]", "_");
if (name.toLowerCase().endsWith(".zip")) name = name.substring(0, name.length() - 4);
return name.isEmpty() ? "pack" : name;
}
private void copy(Uri uri, File dir, String name, File appDir) {
File part = new File(dir, "." + name + "-" + System.currentTimeMillis() + ".part");
try (InputStream in = getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(part)) {
if (in == null) throw new Exception("無法開啟 " + uri);
byte[] buf = new byte[1 << 16];
int n;
while ((n = in.read(buf)) > 0) out.write(buf, 0, n);
} catch (Exception e) {
part.delete();
writeText(new File(appDir, ERROR_FILE), e.toString());
return;
}
// 寫完才改名成 .zip,C# 不會讀到寫一半的檔案;同名時加上 (2)、(3)…
File target = new File(dir, name + ".zip");
for (int i = 2; target.exists(); i++) target = new File(dir, name + " (" + i + ").zip");
if (!part.renameTo(target)) {
part.delete();
writeText(new File(appDir, ERROR_FILE), "無法儲存檔案");
}
}
private static void writeText(File file, String text) {
try (OutputStream out = new FileOutputStream(file)) {
out.write(text.getBytes("UTF-8"));
} catch (Exception ignored) {
}
}
}