174 lines
6.1 KiB
C#
174 lines
6.1 KiB
C#
#nullable enable
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
using ShrinkDataSaver;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace ShrinkApp.Starter.Basic
|
|
{
|
|
public sealed class ShrinkAppBasicSampleController : MonoBehaviour
|
|
{
|
|
[SerializeField] private Text? statusText;
|
|
[SerializeField] private Text? detailText;
|
|
[SerializeField] private Text? outputText;
|
|
[SerializeField] private Text? moduleText;
|
|
[SerializeField] private Text? serviceText;
|
|
[SerializeField] private Text? saveMetaText;
|
|
[SerializeField] private Text? commandListText;
|
|
[SerializeField] private Text? networkText;
|
|
[SerializeField] private Button? saveButton;
|
|
[SerializeField] private Button? loadButton;
|
|
[SerializeField] private Button? commandButton;
|
|
[SerializeField] private Button? loopbackButton;
|
|
[SerializeField] private Button? refreshButton;
|
|
[SerializeField] private Button? clearOutputButton;
|
|
[SerializeField] private Button? disconnectLoopbackButton;
|
|
[SerializeField] private InputField? commandInputField;
|
|
|
|
private int _counter;
|
|
private readonly List<string> _outputLines = new();
|
|
|
|
private void Awake()
|
|
{
|
|
if (saveButton != null)
|
|
saveButton.onClick.AddListener(() => SaveAsync().Forget());
|
|
if (loadButton != null)
|
|
loadButton.onClick.AddListener(() => LoadAsync().Forget());
|
|
if (commandButton != null)
|
|
commandButton.onClick.AddListener(() => RunCommandAsync().Forget());
|
|
if (loopbackButton != null)
|
|
loopbackButton.onClick.AddListener(BindLoopback);
|
|
if (refreshButton != null)
|
|
refreshButton.onClick.AddListener(RefreshPanels);
|
|
if (clearOutputButton != null)
|
|
clearOutputButton.onClick.AddListener(ClearOutput);
|
|
if (disconnectLoopbackButton != null)
|
|
disconnectLoopbackButton.onClick.AddListener(DisconnectLoopback);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
var settings = ShrinkAppBasicDemoRuntime.GetOrCreateSettings();
|
|
if (commandInputField != null)
|
|
commandInputField.text = settings.defaultCommandText;
|
|
|
|
var autoLoopbackMessage = ShrinkAppBasicDemoRuntime.AutoBindLoopbackIfEnabled();
|
|
RefreshStatus("ShrinkApp Basic starter ready.");
|
|
if (!string.IsNullOrWhiteSpace(autoLoopbackMessage))
|
|
AppendOutput(autoLoopbackMessage);
|
|
RefreshPanels();
|
|
|
|
if (settings.autoRunDefaultCommandOnStart)
|
|
RunCommandAsync().Forget();
|
|
}
|
|
|
|
private async UniTaskVoid SaveAsync()
|
|
{
|
|
_counter++;
|
|
ShrinkSettings.Set("ShrinkApp.Basic.Counter", _counter);
|
|
await ShrinkSave.SaveSlotAsync(0);
|
|
RefreshStatus($"Saved counter={_counter}, recentSlot={ShrinkSave.GetRecentSlotIndex()}");
|
|
AppendOutput("save: slot 0");
|
|
RefreshPanels();
|
|
}
|
|
|
|
private async UniTaskVoid LoadAsync()
|
|
{
|
|
if (await ShrinkSave.SlotExistsAsync(0))
|
|
await ShrinkSave.LoadSlotAsync(0);
|
|
|
|
_counter = ShrinkSettings.Get("ShrinkApp.Basic.Counter", 0);
|
|
RefreshStatus($"Loaded counter={_counter}, loadedSlot={ShrinkSave.LoadedSlot}");
|
|
AppendOutput("load: slot 0");
|
|
RefreshPanels();
|
|
}
|
|
|
|
private async UniTaskVoid RunCommandAsync()
|
|
{
|
|
var message = await ShrinkAppBasicDemoRuntime.ExecuteCommandAsync(commandInputField?.text, AppendOutput);
|
|
RefreshStatus(message);
|
|
RefreshPanels();
|
|
}
|
|
|
|
private void BindLoopback()
|
|
{
|
|
var message = ShrinkAppBasicDemoRuntime.EnsureLoopbackReady();
|
|
AppendOutput(message);
|
|
RefreshStatus(message);
|
|
RefreshPanels();
|
|
}
|
|
|
|
private void DisconnectLoopback()
|
|
{
|
|
var message = ShrinkAppBasicDemoRuntime.DisconnectLoopback();
|
|
AppendOutput(message);
|
|
RefreshStatus(message);
|
|
RefreshPanels();
|
|
}
|
|
|
|
private void RefreshPanels()
|
|
{
|
|
RefreshDetail(ShrinkAppBasicDemoRuntime.BuildDetailedStatus());
|
|
RefreshInspectorText().Forget();
|
|
}
|
|
|
|
private void RefreshStatus(string text)
|
|
{
|
|
if (statusText != null)
|
|
statusText.text = text;
|
|
|
|
Debug.Log("[ShrinkApp.Starter.Basic] " + text);
|
|
}
|
|
|
|
private void RefreshDetail(string text)
|
|
{
|
|
if (detailText != null)
|
|
detailText.text = text;
|
|
}
|
|
|
|
private void AppendOutput(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return;
|
|
|
|
var settings = ShrinkAppBasicDemoRuntime.GetOrCreateSettings();
|
|
_outputLines.Add(text.Trim());
|
|
while (_outputLines.Count > settings.maxCommandOutputLines)
|
|
_outputLines.RemoveAt(0);
|
|
|
|
if (outputText != null)
|
|
outputText.text = string.Join("\n", _outputLines);
|
|
}
|
|
|
|
private void ClearOutput()
|
|
{
|
|
_outputLines.Clear();
|
|
if (outputText != null)
|
|
outputText.text = string.Empty;
|
|
}
|
|
|
|
private async UniTaskVoid RefreshInspectorText()
|
|
{
|
|
if (saveMetaText != null)
|
|
{
|
|
var metas = await ShrinkSave.GetAllMetaAsync();
|
|
saveMetaText.text = ShrinkAppBasicDemoRuntime.BuildSaveMetaSummary(metas);
|
|
}
|
|
|
|
if (moduleText != null)
|
|
moduleText.text = ShrinkAppBasicDemoRuntime.BuildModuleSummary();
|
|
|
|
if (serviceText != null)
|
|
serviceText.text = ShrinkAppBasicDemoRuntime.BuildServiceSummary();
|
|
|
|
if (commandListText != null)
|
|
commandListText.text = ShrinkAppBasicDemoRuntime.BuildCommandListSummary();
|
|
|
|
if (networkText != null)
|
|
networkText.text = ShrinkAppBasicDemoRuntime.BuildNetworkSummary();
|
|
}
|
|
}
|
|
}
|