2023-06-19 00:41:44 +08:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Threading;
|
|
|
|
using BITKit;
|
|
|
|
using BITKit.IO;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace BITKit;
|
|
|
|
|
|
|
|
public partial class PlaybackService : UXPanel
|
|
|
|
{
|
|
|
|
[Export] private string path;
|
|
|
|
[Export] private Node root;
|
|
|
|
[Export] private Label label;
|
|
|
|
[Export] private PackedScene template;
|
|
|
|
[Export] private PackedScene labelTemplate;
|
|
|
|
[Export] private DataPlayer dataPlayer;
|
2023-06-19 09:03:04 +08:00
|
|
|
private CancellationTokenSource cancellationTokenSource;
|
2023-06-19 00:41:44 +08:00
|
|
|
public PlaybackService()
|
|
|
|
{
|
|
|
|
BITApp.ServiceCollection.AddSingleton(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2023-06-19 09:03:04 +08:00
|
|
|
cancellationTokenSource = new CancellationTokenSource();
|
2023-06-19 00:41:44 +08:00
|
|
|
OnExit();
|
|
|
|
}
|
2023-06-19 09:03:04 +08:00
|
|
|
public override void _ExitTree()
|
|
|
|
{
|
|
|
|
cancellationTokenSource.Cancel();
|
|
|
|
}
|
2023-06-19 00:41:44 +08:00
|
|
|
public override void OnEntry()
|
|
|
|
{
|
2023-06-19 09:03:04 +08:00
|
|
|
cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
2023-06-19 00:41:44 +08:00
|
|
|
var files =
|
|
|
|
new DirectoryInfo(PathHelper.GetFolderPath("Demos", GetTree().CurrentScene.Name))
|
|
|
|
.GetFiles()
|
|
|
|
.Where(x => x.Extension is ".demo");
|
|
|
|
;
|
|
|
|
|
|
|
|
foreach (var x in files)
|
|
|
|
{
|
|
|
|
var playableInfo = BITAssets.ReadAs<PlayableInfo>(x.FullName);
|
|
|
|
var instance = template.Instantiate<Button>();
|
|
|
|
instance.Text = playableInfo.Name;
|
|
|
|
root.AddChild(instance);
|
|
|
|
|
|
|
|
instance.Pressed += OnClick;
|
|
|
|
|
|
|
|
async void OnClick()
|
|
|
|
{
|
|
|
|
BIT4Log.Log<PlaybackService>($"正在播放:{playableInfo.Name}");
|
|
|
|
|
|
|
|
Stopwatch stopwatch = new();
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
|
|
OnExit();
|
|
|
|
label.Text = $"正在加载:{playableInfo.Name},创建时间:{playableInfo.CreateTime}";
|
|
|
|
label.Show();
|
|
|
|
|
|
|
|
await UniTask.SwitchToTaskPool();
|
|
|
|
|
|
|
|
var array = BITAssets.Read<string[]>(x.FullName, "base64");
|
2023-06-19 09:03:04 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,
|
|
|
|
cancellationTokenSource.Token);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
stopwatch.Stop();
|
|
|
|
return;
|
|
|
|
}
|
2023-06-19 00:41:44 +08:00
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
|
|
BIT4Log.Log<PlaybackService>($"已加载:{playableInfo.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
|
|
dataPlayer.Play(array);
|
|
|
|
|
|
|
|
label.Text = $"已加载,耗时{stopwatch.ElapsedMilliseconds}ms,点击任意位置返回";
|
|
|
|
|
|
|
|
BIT4Log.Log<PlaybackService>($"正在播放:{playableInfo.Name}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnExit()
|
|
|
|
{
|
|
|
|
foreach (var x in root.GetChildren())
|
|
|
|
{
|
|
|
|
x.QueueFree();
|
|
|
|
}
|
|
|
|
label.Hide();
|
|
|
|
}
|
|
|
|
}
|