68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
// 檢查收件匣(persistentDataPath 根目錄)裡的曲包 zip 並匯入:
|
|
// - Android:用檔案管理員「開啟」或「分享」給本 app 的 zip,由 RhyculatorActivity.java 複製到收件匣
|
|
// - 也可以直接把 zip 放進收件匣(adb push、檔案管理員複製)
|
|
// C# 端不呼叫 Java,只看檔案;開啟遊戲、回到前景時立即檢查,之後每秒檢查一次
|
|
public class PackImportWatcher : MonoBehaviour
|
|
{
|
|
private float nextCheck;
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
static void Init()
|
|
{
|
|
var go = new GameObject("PackImportWatcher");
|
|
DontDestroyOnLoad(go);
|
|
go.AddComponent<PackImportWatcher>();
|
|
}
|
|
|
|
void OnApplicationFocus(bool focus)
|
|
{
|
|
if (focus) nextCheck = 0f;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Time.unscaledTime < nextCheck) return;
|
|
nextCheck = Time.unscaledTime + 1f;
|
|
Check();
|
|
}
|
|
|
|
void Check()
|
|
{
|
|
var errors = new List<string>();
|
|
ReadReceiveError(errors);
|
|
var imported = PackImporter.ImportInbox(errors);
|
|
|
|
if (imported.Count == 0 && errors.Count == 0) return;
|
|
SongLibrary.Reload();
|
|
foreach (var e in errors) Toast.Show("匯入失敗:" + e);
|
|
if (imported.Count == 0) return;
|
|
|
|
SongLibrary.Current = SongLibrary.Find(imported[imported.Count - 1].id);
|
|
Toast.Show(imported.Count == 1 ? $"已匯入「{imported[0].name}」" : $"已匯入 {imported.Count} 個曲包");
|
|
|
|
// 遊玩中不打斷;其他畫面直接跳到選歌
|
|
if (SceneManager.GetActiveScene().name != "PlayingScene")
|
|
SceneManager.LoadScene("ChooseScene");
|
|
}
|
|
|
|
// Java 端讀取 content URI 失敗時留下的訊息
|
|
static void ReadReceiveError(List<string> errors)
|
|
{
|
|
string path = Path.Combine(SongLibrary.InboxDir, SongLibrary.ImportErrorFile);
|
|
if (!File.Exists(path)) return;
|
|
string detail = "";
|
|
try { detail = File.ReadAllText(path); } catch { }
|
|
try { File.Delete(path); } catch { }
|
|
|
|
Debug.LogWarning("[PackImport] " + detail);
|
|
errors.Add(detail.Contains("Permission Denial") || detail.Contains("SecurityException")
|
|
? "沒有讀取這個檔案的權限,請從檔案管理員用「開啟」或「分享」送給 Rhyculator"
|
|
: "無法讀取檔案");
|
|
}
|
|
}
|