iFactory.Godot/Artists/Scripts/Factory/PlaybackService.cs

89 lines
2.5 KiB
C#
Raw Normal View History

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;
public PlaybackService()
{
BITApp.ServiceCollection.AddSingleton(this);
}
public override void _Ready()
{
OnExit();
}
public override void OnEntry()
{
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");
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
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();
}
}