Move part of the input module to Cryville.Input.

This commit is contained in:
2023-05-05 00:40:51 +08:00
parent b143fb49ce
commit 0b2ea3ddbc
62 changed files with 1417 additions and 1602 deletions

View File

@@ -1,20 +1,23 @@
using Cryville.Common;
using Cryville.Common.Logging;
using Cryville.Common.Pdt;
using Cryville.Common.Reflection;
using Cryville.Common.Unity.Input;
using Cryville.Crtr.Config;
using Cryville.Input;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using RVector3 = UnityEngine.Vector3;
using System.Threading;
using UnityEngine;
using UnityEngine.Profiling;
using Logger = Cryville.Common.Logging.Logger;
using RVector4 = UnityEngine.Vector4;
namespace Cryville.Crtr {
public class InputProxy : IDisposable {
readonly PdtEvaluator _etor;
readonly PdtRuleset _ruleset;
readonly Judge _judge;
public InputProxy(PdtRuleset ruleset, Judge judge) {
readonly InputVector _screenSize;
public InputProxy(PdtRuleset ruleset, Judge judge, Vector2 screenSize) {
for (int i = 0; i <= MAX_DEPTH; i++) {
var vecsrc = new InputVectorSrc();
_vecsrcs[i] = vecsrc;
@@ -23,6 +26,7 @@ namespace Cryville.Crtr {
_etor = judge != null ? judge._etor : ChartPlayer.etor;
_ruleset = ruleset;
_judge = judge;
_screenSize = new InputVector(screenSize.x, screenSize.y);
foreach (var i in ruleset.inputs) {
_use.Add(i.Key, 0);
_rev.Add(i.Key, new List<Identifier>());
@@ -42,10 +46,15 @@ namespace Cryville.Crtr {
public event EventHandler<ProxyChangedEventArgs> ProxyChanged;
public void LoadFrom(Dictionary<string, RulesetConfig.InputEntry> config) {
foreach (var cfg in config) {
var handler = Game.InputManager.GetHandlerByTypeName(cfg.Value.handler);
if (handler == null) {
Logger.Log("main", 3, "Input", "Uninitialized or unknown handler in ruleset config: {0}", cfg.Value.handler);
continue;
}
Set(new InputProxyEntry {
Target = new Identifier(cfg.Key),
Source = new InputSource {
Handler = Game.InputManager.GetHandler(cfg.Value.handler),
Handler = handler,
Type = cfg.Value.type
}
});
@@ -55,7 +64,7 @@ namespace Cryville.Crtr {
config.Clear();
foreach (var p in _tproxies) {
config.Add((string)p.Key.Name, new RulesetConfig.InputEntry {
handler = TypeNameHelper.GetNamespaceQualifiedName(p.Value.Source.Value.Handler.GetType()),
handler = p.Value.Source.Value.Handler.GetType().AssemblyQualifiedName,
type = p.Value.Source.Value.Type
});
}
@@ -176,7 +185,7 @@ namespace Cryville.Crtr {
static readonly int _var_value = IdentifierManager.Shared.Request("value");
const int MAX_DEPTH = 15;
const int MAX_DIMENSION = 3;
const int MAX_DIMENSION = 4;
readonly InputVectorSrc[] _vecsrcs = new InputVectorSrc[MAX_DEPTH + 1];
readonly InputVectorOp[] _vecops = new InputVectorOp[MAX_DEPTH + 1];
unsafe class InputVectorSrc : PropSrc.FixedBuffer {
@@ -186,9 +195,9 @@ namespace Cryville.Crtr {
}
}
public bool IsNull { get; set; }
public void Set(RVector3 vec) {
public void Set(RVector4 vec) {
fixed (byte* _ptr = buf) {
*(RVector3*)_ptr = vec;
*(RVector4*)_ptr = vec;
}
Invalidate();
}
@@ -204,7 +213,7 @@ namespace Cryville.Crtr {
_src.IsNull = true;
}
else {
var vec = new RVector3();
var vec = new RVector4();
int dim;
if (op.Type == PdtInternalType.Number) dim = 1;
else if (op.Type == PdtInternalType.Vector) {
@@ -228,19 +237,32 @@ namespace Cryville.Crtr {
readonly Dictionary<InputIdentifier, float> _vect = new Dictionary<InputIdentifier, float>();
readonly Dictionary<ProxiedInputIdentifier, PropSrc> _vecs = new Dictionary<ProxiedInputIdentifier, PropSrc>();
double? _lockTime = null;
unsafe void OnInput(InputIdentifier id, InputVector vec) {
lock (_etor) {
unsafe void OnInput(InputIdentifier id, InputFrame frame) {
var rc = id.Source.Handler.ReferenceCue;
if (rc.RelativeUnit == RelativeUnit.Pixel) {
frame = rc.InverseTransform(frame, _screenSize);
var vec = frame.Vector;
vec.X /= _screenSize.X; vec.Y /= _screenSize.Y;
vec.X -= 0.5f; vec.Y -= 0.5f;
vec.X *= ChartPlayer.hitRect.width; vec.Y *= ChartPlayer.hitRect.height;
frame.Vector = vec;
}
else frame = rc.InverseTransform(frame);
bool locked = false;
try {
Profiler.BeginSample("InputProxy.OnInput");
Monitor.Enter(_etor, ref locked);
InputProxyEntry proxy;
if (_sproxies.TryGetValue(id.Source, out proxy)) {
_etor.ContextCascadeInsert();
float ft, tt = (float)(_lockTime != null ? _lockTime.Value : (vec.Time - _timeOrigins[id.Source.Handler]));
float ft, tt = (float)(_lockTime != null ? _lockTime.Value : (frame.Time - _timeOrigins[id.Source.Handler]));
if (!_vect.TryGetValue(id, out ft)) ft = tt;
if (vec.IsNull) {
if (frame.IsNull) {
_etor.ContextCascadeUpdate(_var_value, PropSrc.Null);
OnInput(id, proxy.Target, ft, tt, true);
}
else {
_vecsrcs[0].Set(vec.Vector);
_vecsrcs[0].Set(new RVector4(frame.Vector.X, frame.Vector.Y, frame.Vector.Z, frame.Vector.W));
_etor.ContextCascadeUpdate(_var_value, _vecsrcs[0]);
OnInput(id, proxy.Target, ft, tt, false);
}
@@ -248,6 +270,10 @@ namespace Cryville.Crtr {
_etor.ContextCascadeDiscard();
}
}
finally {
if (locked) Monitor.Exit(_etor);
Profiler.EndSample();
}
}
static readonly int _var_fv = IdentifierManager.Shared.Request("input_vec_from");
static readonly int _var_tv = IdentifierManager.Shared.Request("input_vec_to");
@@ -295,7 +321,7 @@ namespace Cryville.Crtr {
foreach (var s in _sproxies) {
var src = s.Key;
if (_activeCounts[src] == 0) {
OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputVector(_lockTime != null ? _lockTime.Value : src.Handler.GetCurrentTimestamp()));
OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputFrame(_lockTime != null ? _lockTime.Value : src.Handler.GetCurrentTimestamp()));
}
}
}