5 Commits

Author SHA1 Message Date
fcc159ab6c Code cleanup. 2022-11-12 01:02:30 +08:00
f7454eb514 Add active counting for input sources for force tick. 2022-11-12 01:02:00 +08:00
35040e4ebd Update input feeding logic. 2022-11-12 01:00:31 +08:00
d10e6ea18b Implement judge event preparation. 2022-11-12 00:57:41 +08:00
8af09a7167 Prevents duplicated event hooks. 2022-11-12 00:52:44 +08:00
3 changed files with 52 additions and 27 deletions

View File

@@ -4,6 +4,7 @@ using Cryville.Common.Unity.Input;
using System;
using System.Collections.Generic;
using UnityEngine;
using Logger = Cryville.Common.Logger;
namespace Cryville.Crtr {
public class InputProxy {
@@ -42,6 +43,7 @@ namespace Cryville.Crtr {
if (_use[proxy.Target] > 0)
throw new InvalidOperationException("Input already assigned");
if (proxy.Source != null) {
proxy.Source.Value.Handler.OnInput -= OnInput; // Prevent duplicated hooks, no exception will be thrown
proxy.Source.Value.Handler.OnInput += OnInput;
_tproxies.Add(proxy.Target, proxy);
_sproxies.Add(proxy.Source.Value, proxy);
@@ -101,25 +103,22 @@ namespace Cryville.Crtr {
#endregion
#region Handling
public void Activate() { foreach (var src in _sproxies.Keys) src.Handler.Activate(); }
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();
}
public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); }
readonly object _lock = new object();
static readonly int _var_value = IdentifierManager.SharedInstance.Request("value");
static readonly int _var_ft = IdentifierManager.SharedInstance.Request("ft");
static readonly int _var_tt = IdentifierManager.SharedInstance.Request("tt");
static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary();
readonly byte[] _numbuf = new byte[sizeof(float)];
readonly byte[] _numbuf2 = new byte[sizeof(float)];
readonly byte[] _vecbuf = new byte[3 * sizeof(float) + sizeof(int)];
readonly Dictionary<InputSource, int> _activeCounts = new Dictionary<InputSource, int>(); // TODO used for force tick
readonly Dictionary<InputIdentifier, float> _vect = new Dictionary<InputIdentifier, float>();
readonly Dictionary<ProxiedInputIdentifier, PropSrc.Arbitrary> _vecs = new Dictionary<ProxiedInputIdentifier, PropSrc.Arbitrary>();
unsafe void LoadNum(float value) {
fixed (byte* ptr = _numbuf) *(float*)ptr = value;
}
unsafe void LoadNum2(float value) {
fixed (byte* ptr = _numbuf2) *(float*)ptr = value;
}
static readonly byte[] _nullvalue = new byte[0];
unsafe void OnInput(InputIdentifier id, InputVector vec) {
lock (_lock) {
InputProxyEntry proxy;
@@ -127,11 +126,8 @@ namespace Cryville.Crtr {
_etor.ContextCascadeInsert();
float ft, tt = (float)vec.Time;
if (!_vect.TryGetValue(id, out ft)) ft = tt;
LoadNum(ft); _etor.ContextCascadeUpdate(_var_ft, new PropSrc.Arbitrary(PdtInternalType.Number, _numbuf));
LoadNum2(tt); _etor.ContextCascadeUpdate(_var_tt, new PropSrc.Arbitrary(PdtInternalType.Number, _numbuf2));
_vect[id] = tt;
if (vec.IsNull) {
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Null, new byte[0]));
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Null, _nullvalue));
}
else {
fixed (byte* ptr = _vecbuf) {
@@ -139,35 +135,45 @@ namespace Cryville.Crtr {
}
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Vector, _vecbuf));
}
OnInput(id, proxy.Target);
OnInput(id, proxy.Target, ft, tt);
_vect[id] = tt;
_etor.ContextCascadeDiscard();
}
}
}
static readonly int _var_fv = IdentifierManager.SharedInstance.Request("fv");
static readonly int _var_tv = IdentifierManager.SharedInstance.Request("tv");
unsafe void OnInput(InputIdentifier id, string target) {
unsafe void OnInput(InputIdentifier id, Identifier target, float ft, float tt) {
var def = _ruleset.inputs[target];
if (def.pass != null) {
foreach (var p in def.pass) {
_etor.ContextCascadeInsert();
_arbop.Name = _var_value;
_etor.Evaluate(_arbop, p.Value);
OnInput(id, p.Key);
OnInput(id, p.Key, ft, tt);
_etor.ContextCascadeDiscard();
}
}
else {
var pid = new ProxiedInputIdentifier { Source = id, Target = target };
PropSrc.Arbitrary fv, tv = _etor.ContextCascadeLookup(_var_value);
if (!_vecs.TryGetValue(pid, out fv)) fv = new PropSrc.Arbitrary(PdtInternalType.Null, new byte[0]);
if (!_vecs.TryGetValue(pid, out fv)) fv = new PropSrc.Arbitrary(PdtInternalType.Null, _nullvalue);
if (fv.Type != PdtInternalType.Null || tv.Type != PdtInternalType.Null) {
if (fv.Type == PdtInternalType.Null) {
_activeCounts[id.Source]++;
Logger.Log("main", 0, "Input/Proxy", "active {0} # {1} (+)", id.Source, _activeCounts[id.Source]);
}
_etor.ContextCascadeInsert();
_etor.ContextCascadeUpdate(_var_tv, tv);
_etor.ContextCascadeUpdate(_var_fv, fv);
_judge.Feed(target);
_etor.ContextCascadeUpdate(_var_tv, tv);
_judge.Feed(target, ft, tt);
_etor.ContextCascadeDiscard();
if (tv.Type == PdtInternalType.Null) {
_activeCounts[id.Source]--;
Logger.Log("main", 0, "Input/Proxy", "active {0} # {1} (-)", id.Source, _activeCounts[id.Source]);
}
}
_judge.Cleanup(target, ft, tt);
_vecs[pid] = tv;
}
}
@@ -193,7 +199,7 @@ namespace Cryville.Crtr {
public struct ProxiedInputIdentifier : IEquatable<ProxiedInputIdentifier> {
public InputIdentifier Source { get; set; }
public string Target { get; set; }
public Identifier Target { get; set; }
public override bool Equals(object obj) {
if (obj == null || !(obj is ProxiedInputIdentifier)) return false;
return Equals((ProxiedInputIdentifier)obj);

View File

@@ -1,11 +1,20 @@
using Cryville.Common;
using Cryville.Common.Pdt;
using Cryville.Common.Unity.Input;
using Cryville.Crtr.Event;
using System.Collections.Generic;
namespace Cryville.Crtr {
public class Judge {
readonly PdtRuleset _rs;
readonly Dictionary<Identifier, float> ct
= new Dictionary<Identifier, float>();
readonly Dictionary<Identifier, Dictionary<float, JudgeEvent>> evs
= new Dictionary<Identifier, Dictionary<float, JudgeEvent>>();
struct JudgeEvent {
public float EndTime;
public JudgeDefinition Definition { get; set; }
public ContainerState State { get; set; }
}
public Judge(PdtRuleset rs) {
_rs = rs;
foreach (var s in rs.scores) {
@@ -14,10 +23,19 @@ namespace Cryville.Crtr {
scores.Add(name, s.Value.init);
}
}
public void Feed(string target) {
public void Prepare(float time, Identifier input, JudgeDefinition def, ContainerState container) {
Dictionary<float, JudgeEvent> list;
if (!evs.TryGetValue(input, out list)) {
ct.Add(input, 0);
evs.Add(input, list = new Dictionary<float, JudgeEvent>());
}
list.Add(time + def.clip[0], new JudgeEvent { EndTime = time + def.clip[1], Definition = def, State = container });
}
public void Feed(InputEvent ev) {
public void Feed(string target, float ft, float tt) {
Logger.Log("main", 0, "Judge", "Feed {0}: {1}->{2}", target, ft, tt);
}
public void Cleanup(string target, float ft, float tt) {
Logger.Log("main", 0, "Judge", "Cleanup {0}: {1}->{2}", target, ft, tt);
}
public readonly Dictionary<int, ScoreDefinition> scoreDefs = new Dictionary<int, ScoreDefinition>();
public readonly Dictionary<int, float> scores = new Dictionary<int, float>();

View File

@@ -95,10 +95,11 @@ namespace Cryville.Crtr {
else if (ev.Unstamped == null) { }
else if (ev.Unstamped is Chart.Judge) {
var tev = (Chart.Judge)ev.Unstamped;
Identifier name;
Identifier name = default(Identifier);
ChartPlayer.etor.ContextEvent = tev;
ChartPlayer.etor.ContextState = s;
ChartPlayer.etor.Evaluate(new PropOp.Identifier(v => name = new Identifier(v)), ruleset.judges[tev.Id].input);
judge.Prepare(ps.Time, name, ruleset.judges[tev.Id], cs);
ChartPlayer.etor.ContextState = null;
ChartPlayer.etor.ContextEvent = null;
}