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

169 lines
6.5 KiB
C#
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.
using System;
using System.IO;
using UnityEngine;
// 曲包資料夾的位置
// - 有「所有檔案存取權」(Android 11+)或儲存權限(Android 10 以下):手機根目錄的 Rhyculator/package,
// 接上電腦(USB)或用任何檔案管理員都看得到,像插上一個隨身碟
// - 沒有權限:app 自己的資料夾 Android/data/<套件名>/files/package(USB 通常也看得到,但檔案管理員看不到)
// 位置的判斷與 RhyculatorActivity.packageDir() 一致,Java 收到的 zip 會直接放進同一個資料夾
public static class PackStorage
{
public const string FolderName = "Rhyculator";
public const string PackageFolder = "package";
// Java 端寫的訊息檔(在 app 自己的資料夾)
public const string ImportErrorFile = "import-error.txt";
public const string PendingDownloadFile = "pending-download.txt";
// 測試用:改放到別的資料夾
public static string rootOverride;
private static string privateRoot, cacheRoot, sharedRoot;
// 在主執行緒呼叫一次(Application.* 只能在主執行緒讀)
public static void Init()
{
privateRoot = Application.persistentDataPath;
cacheRoot = Path.Combine(Application.persistentDataPath, "cache", "packs");
#if UNITY_ANDROID && !UNITY_EDITOR
try
{
using var env = new AndroidJavaClass("android.os.Environment");
using var dir = env.CallStatic<AndroidJavaObject>("getExternalStorageDirectory");
sharedRoot = Path.Combine(dir.Call<string>("getAbsolutePath"), FolderName);
}
catch (Exception e) { Debug.LogWarning("[PackStorage] " + e.Message); }
#endif
}
public static string PrivateRoot => rootOverride ?? privateRoot;
public static string CacheDir => rootOverride != null ? Path.Combine(rootOverride, "cache") : cacheRoot;
public static bool SupportsShared => sharedRoot != null && rootOverride == null;
public static string Root => SupportsShared && HasSharedAccess() ? sharedRoot : PrivateRoot;
public static string PackageDir => Path.Combine(Root, PackageFolder);
public static bool HasSharedAccess()
{
#if UNITY_ANDROID && !UNITY_EDITOR
try { return Activity().CallStatic<bool>("hasStorageAccess", CurrentActivity()); }
catch (Exception e) { Debug.LogWarning("[PackStorage] " + e.Message); }
#endif
return false;
}
// 開啟系統的授權畫面;結果在回到遊戲時(OnApplicationFocus)重新檢查
public static void RequestSharedAccess()
{
#if UNITY_ANDROID && !UNITY_EDITOR
try { Activity().CallStatic("requestStorageAccess", CurrentActivity()); }
catch (Exception e) { Debug.LogWarning("[PackStorage] " + e.Message); }
#endif
}
#if UNITY_ANDROID && !UNITY_EDITOR
static AndroidJavaClass Activity() => new AndroidJavaClass("com.rhyculator.RhyculatorActivity");
static AndroidJavaObject CurrentActivity()
{
using var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
return player.GetStatic<AndroidJavaObject>("currentActivity");
}
#endif
// 把舊位置的曲包搬到目前的 package 資料夾(背景執行緒可呼叫):
// - 舊版匯入的 songs/<id>/ 資料夾
// - app 資料夾根目錄的 *.zip(舊版的收件匣、adb push)
// - 取得權限前放在 app 資料夾的 package/
public static void Migrate(string packageDir)
{
Directory.CreateDirectory(packageDir);
string root = PrivateRoot;
string privatePackage = Path.Combine(root, PackageFolder);
// 曲包裡的音樂不要出現在手機的音樂 app
try
{
string nomedia = Path.Combine(Path.GetDirectoryName(packageDir), ".nomedia");
if (!File.Exists(nomedia)) File.WriteAllBytes(nomedia, Array.Empty<byte>());
}
catch { }
string songs = Path.Combine(root, "songs");
if (Directory.Exists(songs))
{
foreach (var dir in Directory.GetDirectories(songs))
MoveInto(dir, packageDir);
TryDelete(songs);
}
foreach (var zip in Directory.GetFiles(root, "*.zip"))
MoveInto(zip, packageDir);
if (!SamePath(privatePackage, packageDir) && Directory.Exists(privatePackage))
{
foreach (var f in Directory.GetFiles(privatePackage)) MoveInto(f, packageDir);
foreach (var d in Directory.GetDirectories(privatePackage)) MoveInto(d, packageDir);
TryDelete(privatePackage);
}
}
static void MoveInto(string path, string dir)
{
try
{
string name = Path.GetFileName(path);
string dest = UniquePath(dir, Path.GetFileNameWithoutExtension(name), Path.GetExtension(name));
if (Directory.Exists(path)) MoveDirectory(path, dest);
else
{
File.Copy(path, dest); // 可能跨儲存區,用複製再刪除
File.Delete(path);
}
}
catch (Exception e) { Debug.LogWarning($"[PackStorage] 無法移動 {path}:{e.Message}"); }
}
static void MoveDirectory(string src, string dest)
{
try { Directory.Move(src, dest); return; }
catch (IOException) { }
Directory.CreateDirectory(dest);
foreach (var f in Directory.GetFiles(src)) File.Copy(f, Path.Combine(dest, Path.GetFileName(f)));
Directory.Delete(src, true);
}
// dir/name.ext,已存在時加上 (2)、(3)…
public static string UniquePath(string dir, string name, string ext)
{
string path = Path.Combine(dir, name + ext);
for (int i = 2; File.Exists(path) || Directory.Exists(path); i++)
path = Path.Combine(dir, $"{name} ({i}){ext}");
return path;
}
static bool SamePath(string a, string b)
=> string.Equals(Path.GetFullPath(a).TrimEnd('/', '\\'), Path.GetFullPath(b).TrimEnd('/', '\\'), StringComparison.OrdinalIgnoreCase);
static void TryDelete(string dir)
{
try { Directory.Delete(dir, true); } catch { }
}
// 刪除一個曲包的來源(zip 或資料夾)
public static bool DeleteSource(SongInfo song)
{
try
{
if (File.Exists(song.source)) File.Delete(song.source);
else if (Directory.Exists(song.source)) Directory.Delete(song.source, true);
return true;
}
catch (Exception e)
{
Debug.LogWarning("[PackStorage] " + e.Message);
return false;
}
}
}