Code cleanup.

This commit is contained in:
2023-01-15 11:54:11 +08:00
parent 898fb7d557
commit 609bb317d0
5 changed files with 91 additions and 31 deletions

View File

@@ -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;

View File

@@ -10,16 +10,17 @@ namespace Cryville.Crtr.Event {
private set;
}
readonly Chart chart;
ContainerState rootState;
readonly Dictionary<ChartEvent, ContainerState> containerMap
= new Dictionary<ChartEvent, ContainerState>();
readonly Dictionary<EventContainer, ContainerState> stateMap
= new Dictionary<EventContainer, ContainerState>();
readonly Dictionary<ChartEvent, StampedEvent> map
= new Dictionary<ChartEvent, StampedEvent>();
public Dictionary<EventContainer, List<StampedEvent>> coeventMap
readonly Dictionary<EventContainer, List<StampedEvent>> coeventMap
= new Dictionary<EventContainer, List<StampedEvent>>();
public HashSet<ChartEvent> coevents = new HashSet<ChartEvent>();
public List<StampedEvent> stampedEvents = new List<StampedEvent>();
readonly HashSet<ChartEvent> coevents = new HashSet<ChartEvent>();
readonly List<StampedEvent> stampedEvents = new List<StampedEvent>();
readonly List<EventBatch> batches = new List<EventBatch>();
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<ChartEvent> 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;
}

View File

@@ -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) {

View File

@@ -116,14 +116,14 @@ namespace Cryville.Crtr {
etor.Optimize(_exp);
}
public override IEnumerable<Transform> 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<Transform>();
}
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);

View File

@@ -1,46 +1,91 @@
using System;
using System.Collections.Generic;
namespace Cryville.Crtr {
/// <summary>
/// A time-forward handler of a sequence of events.
/// </summary>
/// <typeparam name="T">The event type.</typeparam>
public abstract class StateBase<T> {
public int EventId;
public double Time;
public List<T> events;
/// <summary>
/// The current time of the state in seconds.
/// </summary>
public double Time { get; protected set; }
/// <summary>
/// The index of the event to be handled next.
/// </summary>
protected int EventId;
/// <summary>
/// The event list.
/// </summary>
protected readonly List<T> Events;
bool breakflag = false;
/// <summary>
/// Creates an instance of the <see cref="StateBase{T}" /> class.
/// </summary>
/// <param name="evs">The event list.</param>
public StateBase(List<T> evs) {
events = evs;
Events = evs;
EventId = 0;
Time = 0;
}
/// <summary>
/// Creates a copy of the current state.
/// </summary>
/// <returns>A copy of the current state.</returns>
/// <remarks>
/// <para><see cref="Events" /> is shared across copies.</para>
/// </remarks>
public virtual StateBase<T> Clone() {
return (StateBase<T>)MemberwiseClone();
}
/// <summary>
/// Copies the state to another existing state.
/// </summary>
/// <param name="dest">The state to be overridden.</param>
public virtual void CopyTo(StateBase<T> dest) {
dest.EventId = EventId;
dest.Time = Time;
dest.EventId = EventId;
dest.breakflag = breakflag;
}
/// <summary>
/// Interrupts the forward process.
/// </summary>
public virtual void Break() {
breakflag = true;
}
/// <summary>
/// Walks through all the remaining events.
/// </summary>
public void Forward() {
ForwardToTime(double.PositiveInfinity);
}
/// <summary>
/// Forwards the time by the specified span and walks through all the encountered events.
/// </summary>
/// <param name="time">The span in seconds.</param>
public void ForwardByTime(double time) {
ForwardToTime(Time + time);
}
/// <summary>
/// Forwards the time by the specified span but walks through at most one event.
/// </summary>
/// <param name="time">The span in seconds.</param>
public void ForwardOnceByTime(double time) {
ForwardOnceToTime(Time + time);
}
/// <summary>
/// Forwards the time to the specified time and walks through all the encountered events.
/// </summary>
/// <param name="toTime">The time in seconds.</param>
public void ForwardToTime(double toTime) {
breakflag = false;
ForwardOnceToTime(Time);
@@ -50,10 +95,20 @@ namespace Cryville.Crtr {
}
}
/// <summary>
/// Forwards the time by the specified span and walks through all the encountered events, with a specified step.
/// </summary>
/// <param name="time">The span in seconds.</param>
/// <param name="step">The step in seconds.</param>
public void ForwardStepByTime(double time, double step) {
ForwardStepToTime(Time + time, step);
}
/// <summary>
/// Forwards the time to the specified time and walks through all the encountered events, with a specified step.
/// </summary>
/// <param name="toTime">The time in seconds.</param>
/// <param name="step">The step in seconds.</param>
public void ForwardStepToTime(double toTime, double step) {
breakflag = false;
ForwardOnceToTime(Time);
@@ -63,7 +118,11 @@ namespace Cryville.Crtr {
if (breakflag) break;
}
}
/// <summary>
/// Forwards the time to the specified time but walks through at most one event.
/// </summary>
/// <param name="toTime">The time in seconds.</param>
public abstract void ForwardOnceToTime(double toTime);
}
}