BITKit/Packages/Runtime/Player/DataRecorder.cs

85 lines
2.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit.IData;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sirenix.OdinInspector;
using Cysharp.Threading.Tasks;
using UnityEngine.Events;
using BITKit.IO;
namespace BITKit
{
public class DataRecorder : Provider<string>, IProvider<bool>
{
[SerializeReference, SubclassSelector] public NameProvider nameProvider;
public DataRecorder<string> recorder = new();
public Provider output;
public UnityEvent onStart = new();
public UnityEvent onStop = new();
DateTime createTime;
void Awake()
{
recorder.output += Output;
recorder.onStart += onStart.Invoke;
recorder.onStop += onStop.Invoke;
onStop.Invoke();
}
void OnDestroy()
{
Cancel();
}
public override string Get()
{
throw new System.NotImplementedException();
}
public override void Set(string t)
{
recorder.Set(t);
}
public void Record()
{
createTime = DateTime.Now;
recorder.Start();
}
public void Stop()
{
recorder.Stop();
}
public void Cancel() => recorder.Cancel();
void Output(List<string> jObject)
{
BITAssets assets = new();
PlayableInfo playableInfo = new()
{
Name = nameProvider.GetName(jObject),
CreateTime = createTime,
CompleteTime = DateTime.Now,
FileName = "base64",
};
var json = JsonConvert.SerializeObject(playableInfo, Formatting.Indented);
var base64Data = JsonConvert.SerializeObject(jObject, Formatting.Indented);
//base64Data = GZipHelper.GZipCompressString(base64Data);
assets.Add(new BITAsset(nameof(PlayableInfo), json));
assets.Add(new BITAsset(playableInfo.FileName, base64Data));
output?.Set(assets);
}
bool IProvider<bool>.Get()
{
return recorder.isPlaying;
}
public void Set(bool t)
{
if (t)
{
Record();
}
else
{
Stop();
}
}
}
}