From a47faf00490503e085d89e0ce4e68ad7f409b941 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Sat, 12 Nov 2022 16:59:39 +0800 Subject: [PATCH] Implement force tick and sync time for input proxy. --- Assets/Cryville/Crtr/ChartPlayer.cs | 10 ++-------- Assets/Cryville/Crtr/InputProxy.cs | 25 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Assets/Cryville/Crtr/ChartPlayer.cs b/Assets/Cryville/Crtr/ChartPlayer.cs index 5f5920e..7956351 100644 --- a/Assets/Cryville/Crtr/ChartPlayer.cs +++ b/Assets/Cryville/Crtr/ChartPlayer.cs @@ -115,16 +115,9 @@ namespace Cryville.Crtr { ? 1f / Application.targetFrameRate : Time.deltaTime; firstFrame = false; + inputProxy.ForceTick(); cbus.ForwardByTime(dt); bbus.ForwardByTime(dt); - UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.FeedJudge"); - /*judge.StartFrame(); - Game.InputManager.EnumerateEvents(ev => { - // Logger.Log("main", 0, "Input", ev.ToString()); - judge.Feed(ev); - }); - judge.EndFrame();*/ - UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward"); UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy"); bbus.CopyTo(2, tbus); @@ -410,6 +403,7 @@ namespace Cryville.Crtr { Game.AudioSequencer.Playing = true; Thread.Sleep((int)(Game.AudioClient.BufferPosition - Game.AudioClient.Position)); // TODO SyncTime(cbus.Time); + inputProxy.SyncTime(cbus.Time); started = true; } catch (Exception ex) { diff --git a/Assets/Cryville/Crtr/InputProxy.cs b/Assets/Cryville/Crtr/InputProxy.cs index a0df3f1..33b15f8 100644 --- a/Assets/Cryville/Crtr/InputProxy.cs +++ b/Assets/Cryville/Crtr/InputProxy.cs @@ -105,9 +105,11 @@ namespace Cryville.Crtr { #region Handling public void Activate() { _activeCounts.Clear(); - foreach (var p in _tproxies.Values) _activeCounts.Add(p.Source.Value, 0); _vect.Clear(); _vecs.Clear(); - foreach (var src in _sproxies.Keys) src.Handler.Activate(); + foreach (var src in _sproxies.Keys) { + _activeCounts.Add(src, 0); + src.Handler.Activate(); + } } public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); } @@ -115,7 +117,8 @@ namespace Cryville.Crtr { static readonly int _var_value = IdentifierManager.SharedInstance.Request("value"); static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary(); readonly byte[] _vecbuf = new byte[3 * sizeof(float) + sizeof(int)]; - readonly Dictionary _activeCounts = new Dictionary(); // TODO used for force tick + readonly Dictionary _timeOrigins = new Dictionary(); + readonly Dictionary _activeCounts = new Dictionary(); readonly Dictionary _vect = new Dictionary(); readonly Dictionary _vecs = new Dictionary(); static readonly byte[] _nullvalue = new byte[0]; @@ -124,7 +127,7 @@ namespace Cryville.Crtr { InputProxyEntry proxy; if (_sproxies.TryGetValue(id.Source, out proxy)) { _etor.ContextCascadeInsert(); - float ft, tt = (float)vec.Time; + float ft, tt = (float)(vec.Time - _timeOrigins[id.Source.Handler]); if (!_vect.TryGetValue(id, out ft)) ft = tt; if (vec.IsNull) { _etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Null, _nullvalue)); @@ -177,6 +180,20 @@ namespace Cryville.Crtr { _vecs[pid] = tv; } } + public void SyncTime(double time) { + foreach (var s in _sproxies.Keys) { + var h = s.Handler; + if (!_timeOrigins.ContainsKey(h)) + _timeOrigins.Add(h, h.GetCurrentTimestamp() - time); + } + } + public void ForceTick() { + foreach (var src in _sproxies.Keys) { + if (_activeCounts[src] == 0) { + OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputVector(src.Handler.GetCurrentTimestamp())); + } + } + } #endregion }