From 609bb317d0acbd24fb008435196a6b92a7ef9f9e Mon Sep 17 00:00:00 2001 From: PopSlime Date: Sun, 15 Jan 2023 11:54:11 +0800 Subject: [PATCH] Code cleanup. --- Assets/Cryville/Crtr/ChartPlayer.cs | 2 +- Assets/Cryville/Crtr/Event/EventBatcher.cs | 35 ++++++----- Assets/Cryville/Crtr/Event/EventBus.cs | 4 +- Assets/Cryville/Crtr/SkinSelectors.cs | 8 +-- Assets/Cryville/Crtr/StateBase.cs | 73 +++++++++++++++++++--- 5 files changed, 91 insertions(+), 31 deletions(-) diff --git a/Assets/Cryville/Crtr/ChartPlayer.cs b/Assets/Cryville/Crtr/ChartPlayer.cs index 46243f7..2ad7cc8 100644 --- a/Assets/Cryville/Crtr/ChartPlayer.cs +++ b/Assets/Cryville/Crtr/ChartPlayer.cs @@ -516,10 +516,10 @@ namespace Cryville.Crtr { var batcher = new EventBatcher(chart); batcher.Forward(); cbus = batcher.Batch(); - Logger.Log("main", 0, "Load/WorkerThread", "Batched {0} event batches", cbus.events.Count); LoadSkin(info.skinFile); + Logger.Log("main", 0, "Load/WorkerThread", "Initializing judge and input"); judge = new Judge(pruleset); etor.ContextJudge = judge; diff --git a/Assets/Cryville/Crtr/Event/EventBatcher.cs b/Assets/Cryville/Crtr/Event/EventBatcher.cs index 256c628..6061eb7 100644 --- a/Assets/Cryville/Crtr/Event/EventBatcher.cs +++ b/Assets/Cryville/Crtr/Event/EventBatcher.cs @@ -10,16 +10,17 @@ namespace Cryville.Crtr.Event { private set; } readonly Chart chart; + ContainerState rootState; readonly Dictionary containerMap = new Dictionary(); readonly Dictionary stateMap = new Dictionary(); readonly Dictionary map = new Dictionary(); - public Dictionary> coeventMap + readonly Dictionary> coeventMap = new Dictionary>(); - public HashSet coevents = new HashSet(); - public List stampedEvents = new List(); + readonly HashSet coevents = new HashSet(); + readonly List stampedEvents = new List(); readonly List batches = new List(); double beat; @@ -29,10 +30,10 @@ namespace Cryville.Crtr.Event { chart = c; beat = chart.BeatPosition; tempo = (float)c.sigs[0].tempo; - events.Add(c); - events.Add(c.ReleaseEvent); + Events.Add(c); + Events.Add(c.ReleaseEvent); AddEventContainer(c); - events.Sort((a, b) => a.BeatPosition.CompareTo(b.BeatPosition)); + Events.Sort((a, b) => a.BeatPosition.CompareTo(b.BeatPosition)); } void AddEventContainer(EventContainer c, ContainerState parent = null) { @@ -40,7 +41,7 @@ namespace Cryville.Crtr.Event { stateMap.Add(c, cs); if (parent == null) { cs.Depth = 0; - RootState = cs; + rootState = cs; } else { cs.Depth = (ushort)(parent.Depth + 1); @@ -53,10 +54,10 @@ namespace Cryville.Crtr.Event { ev.endtime = c.endtime; } if (ev.IsLong) { - events.Add(ev.ReleaseEvent); + Events.Add(ev.ReleaseEvent); containerMap.Add(ev.ReleaseEvent, cs); } - events.Add(ev); + Events.Add(ev); containerMap.Add(ev, cs); if (ev is EventContainer) AddEventContainer((EventContainer)ev, cs); @@ -65,9 +66,9 @@ namespace Cryville.Crtr.Event { public override void ForwardOnceToTime(double toTime) { double toBeat = Math.Round(beat + (toTime - Time) * tempo / 60f, 6); - if (EventId >= events.Count) + if (EventId >= Events.Count) goto return_ahead; - double ebeat = events[EventId].BeatPosition; + double ebeat = Events[EventId].BeatPosition; double etime = Math.Round((ebeat - beat) / tempo * 60f + Time, 6); if (etime > toTime) goto return_ahead; @@ -113,18 +114,18 @@ namespace Cryville.Crtr.Event { } IOrderedEnumerable GetEventBatch() { - float cbeat = events[EventId].BeatPosition; + float cbeat = Events[EventId].BeatPosition; int b = EventId; - while (Mathf.Approximately(events[b].BeatPosition, cbeat)) { + while (Mathf.Approximately(Events[b].BeatPosition, cbeat)) { b--; if (b == -1) break; } int a = EventId; - while (Mathf.Approximately(events[a].BeatPosition, cbeat)) { + while (Mathf.Approximately(Events[a].BeatPosition, cbeat)) { a++; - if (a == events.Count) break; + if (a == Events.Count) break; } - return from ev in events.GetRange(b + 1, a - b - 1) orderby ev.Priority select ev; + return from ev in Events.GetRange(b + 1, a - b - 1) orderby ev.Priority select ev; } public EventBus Batch() { @@ -139,7 +140,7 @@ namespace Cryville.Crtr.Event { BatchCoevents(ev); } batches.Add(cb); - Bus = new EventBus(chart, RootState, batches); + Bus = new EventBus(rootState, batches); return Bus; } diff --git a/Assets/Cryville/Crtr/Event/EventBus.cs b/Assets/Cryville/Crtr/Event/EventBus.cs index 438f773..5fdb981 100644 --- a/Assets/Cryville/Crtr/Event/EventBus.cs +++ b/Assets/Cryville/Crtr/Event/EventBus.cs @@ -125,7 +125,7 @@ namespace Cryville.Crtr.Event { } public override void ForwardOnceToTime(double toTime) { - double time1 = EventId < events.Count ? events[EventId].Time : double.PositiveInfinity; + double time1 = EventId < Events.Count ? Events[EventId].Time : double.PositiveInfinity; double time2 = tempEvents.Count > 0 ? tempEvents[0].Time : double.PositiveInfinity; double time0 = Math.Min(time1, time2); if (time0 <= toTime && time0 != double.PositiveInfinity) { @@ -133,7 +133,7 @@ namespace Cryville.Crtr.Event { foreach (var s in workingStates) s.Handle(null); ValidateStates(); if (time1 == time0) { - var batch = events[EventId]; + var batch = Events[EventId]; for (var i = 0; i < batch.Count; i++) { var ev = batch[i]; if (ev.Container != null) { diff --git a/Assets/Cryville/Crtr/SkinSelectors.cs b/Assets/Cryville/Crtr/SkinSelectors.cs index e52cbf2..8099c53 100644 --- a/Assets/Cryville/Crtr/SkinSelectors.cs +++ b/Assets/Cryville/Crtr/SkinSelectors.cs @@ -116,14 +116,14 @@ namespace Cryville.Crtr { etor.Optimize(_exp); } public override IEnumerable MatchStatic(ContainerState h, Transform a) { - var result = Match(h, a); - if (result != null) return new Transform[] { Match(h, a) }; + var result = Match(a); + if (result != null) return new Transform[] { result }; else return Enumerable.Empty(); } public override Transform MatchDynamic(ContainerState h, Transform a) { - return Match(h, a, true); + return Match(a, true); } - public Transform Match(ContainerState h, Transform a, bool dyn = false) { + public Transform Match(Transform a, bool dyn = false) { ChartPlayer.etor.ContextTransform = a; try { ChartPlayer.etor.Evaluate(_op, _exp); diff --git a/Assets/Cryville/Crtr/StateBase.cs b/Assets/Cryville/Crtr/StateBase.cs index b9f2f41..4348ad0 100644 --- a/Assets/Cryville/Crtr/StateBase.cs +++ b/Assets/Cryville/Crtr/StateBase.cs @@ -1,46 +1,91 @@ -using System; using System.Collections.Generic; namespace Cryville.Crtr { + /// + /// A time-forward handler of a sequence of events. + /// + /// The event type. public abstract class StateBase { - public int EventId; - public double Time; - public List events; + /// + /// The current time of the state in seconds. + /// + public double Time { get; protected set; } + /// + /// The index of the event to be handled next. + /// + protected int EventId; + /// + /// The event list. + /// + protected readonly List Events; bool breakflag = false; + /// + /// Creates an instance of the class. + /// + /// The event list. public StateBase(List evs) { - events = evs; + Events = evs; EventId = 0; Time = 0; } + /// + /// Creates a copy of the current state. + /// + /// A copy of the current state. + /// + /// is shared across copies. + /// public virtual StateBase Clone() { return (StateBase)MemberwiseClone(); } + /// + /// Copies the state to another existing state. + /// + /// The state to be overridden. public virtual void CopyTo(StateBase dest) { - dest.EventId = EventId; dest.Time = Time; + dest.EventId = EventId; dest.breakflag = breakflag; } + /// + /// Interrupts the forward process. + /// public virtual void Break() { breakflag = true; } + /// + /// Walks through all the remaining events. + /// public void Forward() { ForwardToTime(double.PositiveInfinity); } + /// + /// Forwards the time by the specified span and walks through all the encountered events. + /// + /// The span in seconds. public void ForwardByTime(double time) { ForwardToTime(Time + time); } + /// + /// Forwards the time by the specified span but walks through at most one event. + /// + /// The span in seconds. public void ForwardOnceByTime(double time) { ForwardOnceToTime(Time + time); } + /// + /// Forwards the time to the specified time and walks through all the encountered events. + /// + /// The time in seconds. public void ForwardToTime(double toTime) { breakflag = false; ForwardOnceToTime(Time); @@ -50,10 +95,20 @@ namespace Cryville.Crtr { } } + /// + /// Forwards the time by the specified span and walks through all the encountered events, with a specified step. + /// + /// The span in seconds. + /// The step in seconds. public void ForwardStepByTime(double time, double step) { ForwardStepToTime(Time + time, step); } + /// + /// Forwards the time to the specified time and walks through all the encountered events, with a specified step. + /// + /// The time in seconds. + /// The step in seconds. public void ForwardStepToTime(double toTime, double step) { breakflag = false; ForwardOnceToTime(Time); @@ -63,7 +118,11 @@ namespace Cryville.Crtr { if (breakflag) break; } } - + + /// + /// Forwards the time to the specified time but walks through at most one event. + /// + /// The time in seconds. public abstract void ForwardOnceToTime(double toTime); } }