Update Cryville.Input. Fix cleanup logic for input proxy.

This commit is contained in:
2023-05-07 21:30:00 +08:00
parent dad399facf
commit b4984c0b3a
10 changed files with 119 additions and 27 deletions

View File

@@ -148,21 +148,25 @@ namespace Cryville.Crtr {
#region Handling
public void Activate() {
_activeCounts.Clear();
_vect.Clear(); _vecs.Clear();
foreach (var src in _sproxies) {
_activeCounts.Add(src.Key, 0);
var isrc = src.Value.Source;
if (isrc != null) {
isrc.Value.Handler.OnInput += OnInput;
isrc.Value.Handler.OnBatch += OnBatch;
}
}
_targetActiveCount.Clear();
foreach (var i in _ruleset.inputs) {
if (i.Value.pass == null) _targetActiveCount.Add(i.Key, 0);
}
}
public void Deactivate() {
foreach (var src in _sproxies) {
var isrc = src.Value.Source;
if (isrc != null) {
isrc.Value.Handler.OnInput -= OnInput;
isrc.Value.Handler.OnBatch -= OnBatch;
}
}
}
@@ -233,7 +237,7 @@ namespace Cryville.Crtr {
}
}
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
readonly Dictionary<InputSource, int> _activeCounts = new Dictionary<InputSource, int>();
readonly Dictionary<Identifier, int> _targetActiveCount = new Dictionary<Identifier, int>();
readonly Dictionary<InputIdentifier, float> _vect = new Dictionary<InputIdentifier, float>();
readonly Dictionary<ProxiedInputIdentifier, PropSrc> _vecs = new Dictionary<ProxiedInputIdentifier, PropSrc>();
double? _lockTime = null;
@@ -255,7 +259,7 @@ namespace Cryville.Crtr {
InputProxyEntry proxy;
if (_sproxies.TryGetValue(id.Source, out proxy)) {
_etor.ContextCascadeInsert();
float ft, tt = (float)(_lockTime != null ? _lockTime.Value : (frame.Time - _timeOrigins[id.Source.Handler]));
float ft, tt = (float)GetSyncedTime(frame.Time, id.Source.Handler);
if (!_vect.TryGetValue(id, out ft)) ft = tt;
if (frame.IsNull) {
_etor.ContextCascadeUpdate(_var_value, PropSrc.Null);
@@ -299,16 +303,42 @@ namespace Cryville.Crtr {
PropSrc fv, tv = _etor.ContextCascadeLookup(_var_value);
if (!_vecs.TryGetValue(pid, out fv)) fv = PropSrc.Null;
if (fv.Type != PdtInternalType.Null || tv.Type != PdtInternalType.Null) {
if (fv.Type == PdtInternalType.Null) _activeCounts[id.Source]++;
if (fv.Type == PdtInternalType.Null) {
_targetActiveCount[target]++;
}
_etor.ContextCascadeInsert();
_etor.ContextCascadeUpdate(_var_fv, fv);
_etor.ContextCascadeUpdate(_var_tv, tv);
_judge.Feed(target, ft, tt);
_etor.ContextCascadeDiscard();
if (tv.Type == PdtInternalType.Null) _activeCounts[id.Source]--;
if (tv.Type == PdtInternalType.Null) {
_vecs.Remove(pid);
_targetActiveCount[target]--;
}
else _vecs[pid] = tv;
}
}
}
void OnBatch(InputHandler handler, double time) {
lock (_etor) {
foreach (var vec in _vecs) {
if (vec.Key.Source.Source.Handler != handler) continue;
InputProxyEntry proxy;
if (!_sproxies.TryGetValue(vec.Key.Source.Source, out proxy)) continue;
Cleanup(proxy.Target, (float)GetSyncedTime(time, handler));
}
}
}
void Cleanup(Identifier target, float tt, int depth = 0) {
if (depth >= MAX_DEPTH) throw new InputProxyException("Input propagation limit reached\nThe ruleset has invalid input definitions");
var def = _ruleset.inputs[target];
if (def.pass != null) {
foreach (var p in def.pass) {
Cleanup(p.Key, tt, depth + 1);
}
}
else if (_targetActiveCount[target] == 0) {
_judge.Cleanup(target, tt);
_vecs[pid] = tv;
}
}
public void SyncTime(double time) {
@@ -317,11 +347,12 @@ namespace Cryville.Crtr {
_timeOrigins[h] = h.GetCurrentTimestamp() - time;
}
}
const double batchDelayTolerance = 0.02;
public void ForceTick() {
foreach (var s in _sproxies) {
var src = s.Key;
if (_activeCounts[src] == 0) {
OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputFrame(_lockTime != null ? _lockTime.Value : src.Handler.GetCurrentTimestamp()));
lock (_etor) {
foreach (var s in _sproxies) {
var handler = s.Key.Handler;
Cleanup(s.Value.Target, (float)GetSyncedTime(handler.GetCurrentTimestamp() - batchDelayTolerance, handler));
}
}
}
@@ -333,6 +364,9 @@ namespace Cryville.Crtr {
}
return result / _sproxies.Count;
}
double GetSyncedTime(double time, InputHandler handler) {
return _lockTime != null ? _lockTime.Value : (time - _timeOrigins[handler]);
}
public void LockTime() { _lockTime = GetTimestampAverage(); }
public void UnlockTime() { _lockTime = null; }
#endregion