From c33186086cfe19e836354e2d1f38ca2f6b9ac2ca Mon Sep 17 00:00:00 2001 From: PopSlime Date: Wed, 9 Nov 2022 14:01:27 +0800 Subject: [PATCH] Tokenize ruleset keys. Remove MotionName. --- Assets/Cryville/Common/Identifier.cs | 34 +++++++++++ Assets/Cryville/Common/Identifier.cs.meta | 11 ++++ Assets/Cryville/Common/IdentifierManager.cs | 7 +++ Assets/Cryville/Crtr/Chart.cs | 20 +++---- Assets/Cryville/Crtr/ChartPlayer.cs | 2 +- Assets/Cryville/Crtr/Event/ContainerState.cs | 61 ++++++++++---------- Assets/Cryville/Crtr/Event/RMVPool.cs | 11 ++-- Assets/Cryville/Crtr/Game.cs | 2 +- Assets/Cryville/Crtr/Judge.cs | 11 ++-- Assets/Cryville/Crtr/Motion.cs | 35 ----------- Assets/Cryville/Crtr/NoteHandler.cs | 24 ++++---- Assets/Cryville/Crtr/PropOp.cs | 7 +++ Assets/Cryville/Crtr/Ruleset.cs | 40 ++++++++----- 13 files changed, 153 insertions(+), 112 deletions(-) create mode 100644 Assets/Cryville/Common/Identifier.cs create mode 100644 Assets/Cryville/Common/Identifier.cs.meta diff --git a/Assets/Cryville/Common/Identifier.cs b/Assets/Cryville/Common/Identifier.cs new file mode 100644 index 0000000..156fe68 --- /dev/null +++ b/Assets/Cryville/Common/Identifier.cs @@ -0,0 +1,34 @@ +using System; + +namespace Cryville.Common { + public struct Identifier : IEquatable { + public int Key { get; private set; } + public object Name { get { return IdentifierManager.SharedInstance.Retrieve(Key); } } + public Identifier(int key) { + Key = key; + } + public Identifier(object name) { + Key = IdentifierManager.SharedInstance.Request(name); + } + public override bool Equals(object obj) { + if (obj == null && Key == 0) return true; + if (obj == null || !(obj is Identifier)) return false; + return Equals((Identifier)obj); + } + public bool Equals(Identifier other) { + return Key == other.Key; + } + public override int GetHashCode() { + return Key; + } + public override string ToString() { + return Name.ToString(); + } + public static implicit operator Identifier(string identifier) { + return new Identifier(identifier); + } + public static implicit operator string(Identifier identifier) { + return identifier.ToString(); + } + } +} diff --git a/Assets/Cryville/Common/Identifier.cs.meta b/Assets/Cryville/Common/Identifier.cs.meta new file mode 100644 index 0000000..087325b --- /dev/null +++ b/Assets/Cryville/Common/Identifier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5b0f66eb5ae446e44a34dc61dac132b6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Cryville/Common/IdentifierManager.cs b/Assets/Cryville/Common/IdentifierManager.cs index 106a771..74e719b 100644 --- a/Assets/Cryville/Common/IdentifierManager.cs +++ b/Assets/Cryville/Common/IdentifierManager.cs @@ -15,6 +15,13 @@ namespace Cryville.Common { object _syncRoot = new object(); + /// + /// Creates an instance of the class. + /// + public IdentifierManager() { + Request(this); + } + /// /// Requests an integer ID for an identifier. /// diff --git a/Assets/Cryville/Crtr/Chart.cs b/Assets/Cryville/Crtr/Chart.cs index c27f841..d8576a4 100644 --- a/Assets/Cryville/Crtr/Chart.cs +++ b/Assets/Cryville/Crtr/Chart.cs @@ -276,8 +276,8 @@ namespace Cryville.Crtr { private void LoadFromString(string s) { Match m = Regex.Match(s, @"^(.+?)(#(\d+))?(@(.+?))?(\^(.+?))?(\*(.+?))?(:(.+))?$"); if (!m.Success) throw new ArgumentException(); // TODO - name = new MotionName(m.Groups[1].Value); - var registry = ChartPlayer.motionRegistry[name.MainName]; + name = new Identifier(m.Groups[1].Value); + var registry = ChartPlayer.motionRegistry[name]; if (m.Groups[3].Success) { ushort id = ushort.Parse(m.Groups[3].Value); Vec1 time = m.Groups[5].Success ? new Vec1(m.Groups[5].Value) : null; @@ -313,15 +313,15 @@ namespace Cryville.Crtr { return result; } - private MotionName name; + private Identifier name; [JsonIgnore] - public MotionName Name { + public Identifier Name { get { return name; } private set { MotionRegistry reg; - if (!ChartPlayer.motionRegistry.TryGetValue(value.MainName, out reg)) + if (!ChartPlayer.motionRegistry.TryGetValue(value, out reg)) throw new ArgumentException("Invalid motion name"); if (RelativeNode != null) RelativeNode.Value = reg.InitValue; else AbsoluteValue = reg.InitValue; @@ -351,16 +351,16 @@ namespace Cryville.Crtr { else return AbsoluteValue; })); SubmitPropOp("motion", new PropOp.String(v => motion = v)); - SubmitPropOp("name", new PropOp.String(v => { - var n = new MotionName(v); + SubmitPropOp("name", new PropOp.Identifier(v => { + var n = new Identifier(v); if (Name.Equals(n)) { } - else if (Name.Equals(default(MotionName))) Name = n; + else if (Name.Equals(default(Identifier))) Name = n; else throw new RulesetViolationException(string.Format( "Motion name not matched, expected {0}, got {1}", n, Name )); })); SubmitPropOp("value", new VectorOp(v => { - var vec = Vector.Construct(ChartPlayer.motionRegistry[Name.MainName].Type, v); + var vec = Vector.Construct(ChartPlayer.motionRegistry[Name].Type, v); if (RelativeNode != null) RelativeNode.Value = vec; else AbsoluteValue = vec; })); @@ -379,7 +379,7 @@ namespace Cryville.Crtr { public Note() { SubmitPropSrc("track", new PropSrc.Float(() => { - var i = motions.FirstOrDefault(m => m.RelativeNode == null && m.Name.MainName == "track"); + var i = motions.FirstOrDefault(m => m.RelativeNode == null && m.Name == "track"); if (i == null) return ((Vec1)ChartPlayer.motionRegistry["track"].InitValue).Value; else return ((Vec1)i.AbsoluteValue).Value; })); diff --git a/Assets/Cryville/Crtr/ChartPlayer.cs b/Assets/Cryville/Crtr/ChartPlayer.cs index 56a1558..4de4d3b 100644 --- a/Assets/Cryville/Crtr/ChartPlayer.cs +++ b/Assets/Cryville/Crtr/ChartPlayer.cs @@ -63,7 +63,7 @@ namespace Cryville.Crtr { static float startOffset = 0; public static float sv = 16f; - public static Dictionary motionRegistry = new Dictionary(); + public static Dictionary motionRegistry = new Dictionary(); public static PdtEvaluator etor; diff --git a/Assets/Cryville/Crtr/Event/ContainerState.cs b/Assets/Cryville/Crtr/Event/ContainerState.cs index a962843..e41f7f8 100644 --- a/Assets/Cryville/Crtr/Event/ContainerState.cs +++ b/Assets/Cryville/Crtr/Event/ContainerState.cs @@ -66,9 +66,9 @@ namespace Cryville.Crtr.Event { readonly RMVPool RMVPool = new RMVPool(); protected Dictionary PlayingMotions = new Dictionary(); - protected Dictionary Values = new Dictionary(); - protected Dictionary CachedValues = new Dictionary(); - protected Dictionary CachedValueStates = new Dictionary(); + protected Dictionary Values; + protected Dictionary CachedValues; + protected Dictionary CachedValueStates; /// /// Gets a motion value. @@ -76,12 +76,12 @@ namespace Cryville.Crtr.Event { /// The motion name. /// Returns a cloned motion value instead. /// A motion value. - RealtimeMotionValue GetMotionValue(MotionName name, bool clone = false) { + RealtimeMotionValue GetMotionValue(Identifier name, bool clone = false) { RealtimeMotionValue value; if (!Values.TryGetValue(name, out value)) { value = new RealtimeMotionValue().Init(Parent == null - ? ChartPlayer.motionRegistry[name.MainName].GlobalInitValue - : ChartPlayer.motionRegistry[name.MainName].InitValue + ? ChartPlayer.motionRegistry[name].GlobalInitValue + : ChartPlayer.motionRegistry[name].InitValue ); Values.Add(name, value); } @@ -89,7 +89,7 @@ namespace Cryville.Crtr.Event { return value; } - void InvalidateMotion(MotionName name) { + void InvalidateMotion(Identifier name) { CachedValueStates[name] = false; foreach (var c in Children) c.Value.InvalidateMotion(name); @@ -103,8 +103,11 @@ namespace Cryville.Crtr.Event { Parent = parent; } + Values = new Dictionary(ChartPlayer.motionRegistry.Count); + CachedValues = new Dictionary(ChartPlayer.motionRegistry.Count); + CachedValueStates = new Dictionary(ChartPlayer.motionRegistry.Count); foreach (var m in ChartPlayer.motionRegistry) - Values.Add(new MotionName(m.Key), new RealtimeMotionValue().Init(Parent == null ? m.Value.GlobalInitValue : m.Value.InitValue)); + Values.Add(m.Key, new RealtimeMotionValue().Init(Parent == null ? m.Value.GlobalInitValue : m.Value.InitValue)); } static void AddChild(EventContainer c, ContainerState s, ContainerState target) { @@ -117,19 +120,19 @@ namespace Cryville.Crtr.Event { public ContainerState Clone(byte ct) { var r = (ContainerState)MemberwiseClone(); - var mvs = new Dictionary(Values.Count); + var mvs = new Dictionary(ChartPlayer.motionRegistry.Count); foreach (var mv in Values) { mvs.Add(mv.Key, mv.Value.Clone()); } r.Values = mvs; - var cvs = new Dictionary(CachedValues.Count); + var cvs = new Dictionary(ChartPlayer.motionRegistry.Count); foreach (var cv in CachedValues) { cvs.Add(cv.Key, cv.Value.Clone()); } r.CachedValues = cvs; - var cvss = new Dictionary(CachedValueStates.Count); + var cvss = new Dictionary(ChartPlayer.motionRegistry.Count); foreach (var cv in CachedValueStates) { cvss.Add(cv.Key, cv.Value); } @@ -211,7 +214,7 @@ namespace Cryville.Crtr.Event { this.judge = judge; } - public T GetRawValue(MotionName key) where T : Vector { + public T GetRawValue(Identifier key) where T : Vector { Vector tr; if (!CachedValues.TryGetValue(key, out tr)) { tr = (Vector)ReflectionHelper.InvokeEmptyConstructor(typeof(T)); @@ -232,7 +235,15 @@ namespace Cryville.Crtr.Event { return r; } - static readonly MotionName n_dir = new MotionName("dir"); + static readonly Identifier n_pt = new Identifier("pt"); + public Vector2 ScreenPoint { + get { + var mv = GetRawValue(n_pt); + return mv.ToVector2(ChartPlayer.hitRect); + } + } + + static readonly Identifier n_dir = new Identifier("dir"); public Vector3 Direction { get { Vec3 r = GetRawValue(n_dir); @@ -240,7 +251,7 @@ namespace Cryville.Crtr.Event { } } - static readonly MotionName n_normal = new MotionName("normal"); + static readonly Identifier n_normal = new Identifier("normal"); public Vector3 Normal { get { Vec3 r = GetRawValue(n_normal); @@ -254,16 +265,8 @@ namespace Cryville.Crtr.Event { } } - static readonly MotionName n_pt = new MotionName("pt"); - public Vector2 ScreenPoint { - get { - var mv = GetRawValue(n_pt); - return mv.ToVector2(ChartPlayer.hitRect); - } - } - - static readonly MotionName n_sv = new MotionName("sv"); - static readonly MotionName n_svm = new MotionName("svm"); + static readonly Identifier n_sv = new Identifier("sv"); + static readonly Identifier n_svm = new Identifier("svm"); public float ScrollVelocity { get { return GetRawValue(n_sv).ToFloat(ChartPlayer.hitRect) @@ -271,7 +274,7 @@ namespace Cryville.Crtr.Event { } } - static readonly MotionName n_dist = new MotionName("dist"); + static readonly Identifier n_dist = new Identifier("dist"); public float Distance { get { var mv = GetRawValue(n_dist); @@ -279,15 +282,15 @@ namespace Cryville.Crtr.Event { } } - static readonly MotionName n_corner = new MotionName("corner"); + static readonly Identifier n_corner = new Identifier("corner"); public bool Corner { get { return GetRawValue(n_corner).Value % 2 >= 1; } } - static readonly MotionName n_ctrl0 = new MotionName("ctrl0"); - static readonly MotionName n_ctrl1 = new MotionName("ctrl1"); + static readonly Identifier n_ctrl0 = new Identifier("ctrl0"); + static readonly Identifier n_ctrl1 = new Identifier("ctrl1"); public Vector3 GetControlPoint(bool alt1, float deltaz) { var mv = GetRawValue(alt1 ? n_ctrl1 : n_ctrl0); if (alt1 && mv.IsZero()) { @@ -296,7 +299,7 @@ namespace Cryville.Crtr.Event { return mv.ToVector3(ChartPlayer.hitRect, deltaz); } - static readonly MotionName n_track = new MotionName("track"); + static readonly Identifier n_track = new Identifier("track"); public float Track { get { return GetRawValue(n_track).Value; diff --git a/Assets/Cryville/Crtr/Event/RMVPool.cs b/Assets/Cryville/Crtr/Event/RMVPool.cs index bf56319..f5f104d 100644 --- a/Assets/Cryville/Crtr/Event/RMVPool.cs +++ b/Assets/Cryville/Crtr/Event/RMVPool.cs @@ -1,4 +1,5 @@ -using Cryville.Common.Buffers; +using Cryville.Common; +using Cryville.Common.Buffers; using System.Collections.Generic; namespace Cryville.Crtr.Event { @@ -12,16 +13,16 @@ namespace Cryville.Crtr.Event { return new RealtimeMotionValue().Init(_reg.InitValue); } } - static Dictionary _buckets; + static Dictionary _buckets; public static void Prepare() { - _buckets = new Dictionary(ChartPlayer.motionRegistry.Count); + _buckets = new Dictionary(ChartPlayer.motionRegistry.Count); foreach (var reg in ChartPlayer.motionRegistry) _buckets.Add(reg.Key, new Bucket(reg.Key, 4096)); } readonly Dictionary _rented = new Dictionary(); - public RealtimeMotionValue Rent(MotionName name) { - var n = name.MainName; + public RealtimeMotionValue Rent(Identifier name) { + var n = name; var obj = _buckets[n].Rent(); _rented.Add(obj, n); return obj; diff --git a/Assets/Cryville/Crtr/Game.cs b/Assets/Cryville/Crtr/Game.cs index 81f1fe9..ec9310f 100644 --- a/Assets/Cryville/Crtr/Game.cs +++ b/Assets/Cryville/Crtr/Game.cs @@ -85,7 +85,7 @@ namespace Cryville.Crtr { AudioSequencer.Playing = true; AudioClient.Start(); - ChartPlayer.motionRegistry = new Dictionary { + ChartPlayer.motionRegistry = new Dictionary { { "pt" , new MotionRegistry(typeof(VecPt)) }, { "dir" , new MotionRegistry(typeof(Vec3)) }, { "normal" , new MotionRegistry(typeof(Vec3)) }, diff --git a/Assets/Cryville/Crtr/Judge.cs b/Assets/Cryville/Crtr/Judge.cs index eb306f2..e474132 100644 --- a/Assets/Cryville/Crtr/Judge.cs +++ b/Assets/Cryville/Crtr/Judge.cs @@ -9,7 +9,7 @@ namespace Cryville.Crtr { public Judge(PdtRuleset rs) { _rs = rs; foreach (var s in rs.scores) { - var name = IdentifierManager.SharedInstance.Request(s.Key); + var name = s.Key.Key; scoreDefs.Add(name, s.Value); scores.Add(name, s.Value.init); } @@ -45,14 +45,15 @@ namespace Cryville.Crtr { public class InputDefinition { public int dim; public bool notnull; - public Dictionary pass; + public Dictionary pass; } public class JudgeDefinition { public PdtExpression clip; + public Identifier input; public PdtExpression hit; - public string[] pass; - public string miss; - public Dictionary scores; + public Identifier[] pass; + public Identifier miss; + public Dictionary scores; } public class ScoreOperation { public int name; diff --git a/Assets/Cryville/Crtr/Motion.cs b/Assets/Cryville/Crtr/Motion.cs index 0a66580..ff46e9c 100644 --- a/Assets/Cryville/Crtr/Motion.cs +++ b/Assets/Cryville/Crtr/Motion.cs @@ -139,41 +139,6 @@ namespace Cryville.Crtr { } } - public struct MotionName : IEquatable { - public string MainName { get; private set; } - public string SubName { get; private set; } - public string FullName { get; private set; } - - readonly int hash; - public MotionName(string mainName, string subName) { - MainName = mainName; - SubName = subName; - FullName = subName == null ? mainName : string.Format("{0}!{1}", mainName, subName); - hash = FullName.GetHashCode(); - } - public MotionName(string fullName) { - var names = fullName.Split('!'); - if (names.Length > 2) throw new ArgumentException("Invalid name"); - FullName = fullName; - hash = FullName.GetHashCode(); - MainName = names[0]; - SubName = names.Length == 1 ? "" : names[1]; - } - public override bool Equals(object obj) { - if (!(obj is MotionName)) return false; - return Equals((MotionName)obj); - } - public bool Equals(MotionName other) { - return hash == other.hash; - } - public override int GetHashCode() { - return hash; - } - public override string ToString() { - return FullName; - } - } - public struct MotionRegistry { readonly Type m_Type; public Type Type { diff --git a/Assets/Cryville/Crtr/NoteHandler.cs b/Assets/Cryville/Crtr/NoteHandler.cs index 86b27d2..e263b19 100644 --- a/Assets/Cryville/Crtr/NoteHandler.cs +++ b/Assets/Cryville/Crtr/NoteHandler.cs @@ -91,28 +91,28 @@ namespace Cryville.Crtr { if (ev == null) { } else if (ev.Unstamped == null) { } else if (ev.Unstamped is Chart.Motion) { - var tev = (Chart.Motion)ev.Unstamped; - if (tev.Name.MainName != "judge") return; - phMotions.Add(tev, (Vec1)s.GetRawValue(tev.Name).Clone()); + /*var tev = (Chart.Motion)ev.Unstamped; + if (tev.Name != "judge") return; + phMotions.Add(tev, (Vec1)s.GetRawValue(tev.Name).Clone());*/ } else if (ev.Unstamped is InstantEvent) { - var oev = ((InstantEvent)ev.Unstamped).Original; + /*var oev = ((InstantEvent)ev.Unstamped).Original; if (oev is Chart.Motion) { var tev = (Chart.Motion)oev; - if (tev.Name.MainName != "judge") return; + if (tev.Name != "judge") return; var v0 = phMotions[tev]; var v1 = s.GetRawValue(tev.Name); // var etor = new Evaluator(); for (var vi = Mathf.Ceil(v0.Value); vi < v1.Value; vi++) { var v = new Vec1(vi); var t = MotionLerper.Delerp(v, ev.Time, v1, ev.Origin.Time, v0, tev.transition, tev.rate); - /*CompiledRuleset.PatchJudge( + CompiledRuleset.PatchJudge( Event, ChartPlayer.cruleset.primary_judges[tev.Name.SubName], t, etor, patchedJudgeEvents - );*/ + ); } phMotions.Remove(tev); - } + }*/ } } } @@ -137,12 +137,12 @@ namespace Cryville.Crtr { public override void MotionUpdate(byte ct, Chart.Motion ev) { base.MotionUpdate(ct, ev); if (ct == 0) { - if (ev.Name.MainName == "judge") { + /*if (ev.Name == "judge") { if (invalidated) return; - if (ev.Name.SubName == null) + if (ev.Name == null) throw new InvalidOperationException(); - // judge.IssueImmediate(this, ev.Name.SubName, GetFramePoint(cs.Parent, cs.Track)); - } + judge.IssueImmediate(this, ev.Name.SubName, GetFramePoint(cs.Parent, cs.Track)); + }*/ } else if (ct == 16) { /*var etor = new EvalImpl(); diff --git a/Assets/Cryville/Crtr/PropOp.cs b/Assets/Cryville/Crtr/PropOp.cs index d31eb51..1a38f8b 100644 --- a/Assets/Cryville/Crtr/PropOp.cs +++ b/Assets/Cryville/Crtr/PropOp.cs @@ -45,6 +45,13 @@ namespace Cryville.Crtr { _cb(GetOperand(0).AsString()); } } + public class Identifier : PropOp { + readonly Action _cb; + public Identifier(Action cb) { _cb = cb; } + protected override void Execute() { + _cb(GetOperand(0).AsIdentifier()); + } + } public class Enum : PropOp { readonly static Dictionary _cache = new Dictionary(); readonly Action _cb; diff --git a/Assets/Cryville/Crtr/Ruleset.cs b/Assets/Cryville/Crtr/Ruleset.cs index 3a5163b..4bd95f1 100644 --- a/Assets/Cryville/Crtr/Ruleset.cs +++ b/Assets/Cryville/Crtr/Ruleset.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; +using SIdentifier = Cryville.Common.Identifier; namespace Cryville.Crtr { public class Ruleset { @@ -31,9 +32,9 @@ namespace Cryville.Crtr { [Binder(typeof(PdtRulesetBinder))] public class PdtRuleset { - public Dictionary inputs; - public Dictionary judges; - public Dictionary scores; + public Dictionary inputs; + public Dictionary judges; + public Dictionary scores; public Constraint constraints; public void Optimize(PdtEvaluatorBase etor) { foreach (var i in inputs.Values) { @@ -142,39 +143,50 @@ namespace Cryville.Crtr { return result; } else if (type.Equals(typeof(string))) { - string result = null; + string result = default(string); + ChartPlayer.etor.Evaluate(new PropOp.String(r => result = r), exp); + return result; + } + else if (type.Equals(typeof(Identifier))) { + Identifier result = default(Identifier); ChartPlayer.etor.Evaluate(new pop_identstr(r => result = r), exp); return result; } - else if (type.Equals(typeof(string[]))) { - string[] result = null; + else if (type.Equals(typeof(Identifier[]))) { + Identifier[] result = null; ChartPlayer.etor.Evaluate(new pop_identstrarr(r => result = r), exp); return result; } } + else if (value is string) { + var exp = (string)value; + if (type.Equals(typeof(Identifier))) { + return (Identifier)exp; + } + } return base.ChangeType(value, type, culture); } #pragma warning disable IDE1006 class pop_identstr : PropOp { - readonly Action _cb; - public pop_identstr(Action cb) { _cb = cb; } + readonly Action _cb; + public pop_identstr(Action cb) { _cb = cb; } protected override void Execute() { var op = GetOperand(0); - if (op.Type == PdtInternalType.Undefined) _cb((string)IdentifierManager.SharedInstance.Retrieve(op.AsIdentifier())); - else if (op.Type == PdtInternalType.String) _cb(op.AsString()); + if (op.Type == PdtInternalType.Undefined) _cb(new SIdentifier(op.AsIdentifier())); + else if (op.Type == PdtInternalType.String) _cb(new SIdentifier(op.AsString())); else throw new InvalidCastException("Not an identifier or string"); } } class pop_identstrarr : PdtOperator { - readonly Action _cb; - public pop_identstrarr(Action cb) : base(16) { _cb = cb; } + readonly Action _cb; + public pop_identstrarr(Action cb) : base(16) { _cb = cb; } protected override void Execute() { - var result = new string[LoadedOperandCount]; + var result = new SIdentifier[LoadedOperandCount]; for (int i = 0; i < LoadedOperandCount; i++) { var op = GetOperand(i); if (op.Type != PdtInternalType.Undefined) throw new InvalidCastException("Not an identifier"); - result[i] = (string)IdentifierManager.SharedInstance.Retrieve(op.AsIdentifier()); + result[i] = new SIdentifier(op.AsIdentifier()); } _cb(result); }