- 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]>
118 lines
4.8 KiB
C#
118 lines
4.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
// 伺服器呼叫(回應一律用 MiniJson 解析)
|
|
public static class Api
|
|
{
|
|
public class Response
|
|
{
|
|
public Dictionary<string, object> data; // 解析失敗或連線失敗時為 null
|
|
public string error; // 給玩家看的錯誤;成功時為 null
|
|
|
|
public string Status => MiniJson.GetString(data, "status", "");
|
|
public string Reason => MiniJson.GetString(data, "reason", "");
|
|
public bool Ok => error == null && (Status == "success" || Status == "send" || Status == "found" || Status == "not_found");
|
|
public string Str(string key) => MiniJson.GetString(data, key, "");
|
|
public int Int(string key) => MiniJson.GetInt(data, key, 0);
|
|
}
|
|
|
|
public static IEnumerator Game(Dictionary<string, object> body, Action<Response> done)
|
|
=> Post(ServerConfig.GameServerUrl + "/", body, done);
|
|
|
|
public static IEnumerator Verify(Dictionary<string, object> body, Action<Response> done)
|
|
=> Post(ServerConfig.VerifyServerUrl + "/", body, done);
|
|
|
|
public static IEnumerator Post(string url, Dictionary<string, object> body, Action<Response> done)
|
|
{
|
|
using var www = new UnityWebRequest(url, "POST");
|
|
www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(MiniJson.Serialize(body)));
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
www.timeout = 10;
|
|
yield return www.SendWebRequest();
|
|
done(Parse(www));
|
|
}
|
|
|
|
public static IEnumerator Get(string url, Action<Response> done)
|
|
{
|
|
using var www = UnityWebRequest.Get(url);
|
|
www.timeout = 10;
|
|
yield return www.SendWebRequest();
|
|
done(Parse(www));
|
|
}
|
|
|
|
// 伺服器回 4xx/5xx 時 body 仍是 JSON,照樣解析
|
|
static Response Parse(UnityWebRequest www)
|
|
{
|
|
var r = new Response();
|
|
try { r.data = MiniJson.AsObject(MiniJson.Parse(www.downloadHandler.text)); }
|
|
catch { r.data = null; }
|
|
|
|
if (r.data == null)
|
|
r.error = www.result == UnityWebRequest.Result.ConnectionError ? "無法連線到伺服器" : "伺服器回傳異常";
|
|
else if (r.Status == "fail" || r.Status == "failed")
|
|
r.error = r.Reason;
|
|
if (r.error != null) Debug.LogWarning($"[Api] {www.url}: {r.error} ({www.error})");
|
|
return r;
|
|
}
|
|
|
|
// 以曲包代碼下載到 package 資料夾;progress 0~1;完成時 done(錯誤或 null, 曲名)
|
|
public static IEnumerator DownloadPack(string code, Action<float> progress, Action<string, string> done)
|
|
{
|
|
code = code.Trim().ToLowerInvariant();
|
|
Response info = null;
|
|
yield return Get($"{ServerConfig.GameServerUrl}/api/packs/{UnityWebRequest.EscapeURL(code)}", r => info = r);
|
|
if (info.error != null)
|
|
{
|
|
done(info.Reason == "not_found" ? "找不到這個代碼的曲包" : info.error, null);
|
|
yield break;
|
|
}
|
|
var pack = MiniJson.AsObject(MiniJson.Get(info.data, "pack"));
|
|
string title = MiniJson.GetString(pack, "title", code);
|
|
string url = ServerConfig.GameServerUrl + MiniJson.GetString(pack, "download", $"/p/{code}/download");
|
|
|
|
string dir = PackStorage.PackageDir;
|
|
Directory.CreateDirectory(dir);
|
|
string part = Path.Combine(dir, $".{code}.part");
|
|
using (var www = UnityWebRequest.Get(url))
|
|
{
|
|
www.downloadHandler = new DownloadHandlerFile(part) { removeFileOnAbort = true };
|
|
var op = www.SendWebRequest();
|
|
while (!op.isDone)
|
|
{
|
|
progress?.Invoke(www.downloadProgress);
|
|
yield return null;
|
|
}
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
try { File.Delete(part); } catch { }
|
|
done("下載失敗:" + www.error, title);
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
// 同一個曲包的舊版本(代碼相同)換成新下載的
|
|
var old = Library.Find(code);
|
|
if (old != null) PackStorage.DeleteSource(old);
|
|
|
|
string target = PackStorage.UniquePath(dir, $"{SafeName(title)} [{code}]", ".zip");
|
|
File.Move(part, target);
|
|
// 讓掃描不必等「剛寫入」的緩衝時間
|
|
File.SetLastWriteTimeUtc(target, DateTime.UtcNow - PackLibrary.SettleTime - TimeSpan.FromSeconds(1));
|
|
Library.Rescan();
|
|
done(null, title);
|
|
}
|
|
|
|
static string SafeName(string s)
|
|
{
|
|
foreach (char c in Path.GetInvalidFileNameChars()) s = s.Replace(c, '_');
|
|
foreach (char c in "\\/:*?\"<>|") s = s.Replace(c, '_');
|
|
s = s.Trim();
|
|
return s.Length > 60 ? s.Substring(0, 60) : (s == "" ? "pack" : s);
|
|
}
|
|
}
|