Code structure cleanup.

This commit is contained in:
2023-08-24 15:47:34 +08:00
parent e40c98ae1b
commit 1f58390298
137 changed files with 439 additions and 362 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: af16092e83ce23d46b46254e2d9798f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3063149610959f4e853ced6341a9d36
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,81 @@
using Cryville.Common;
using Cryville.Common.Pdt;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cryville.Crtr.Ruleset {
internal struct JudgeActionResult {
public bool BreakExecution;
public bool PreventRecycle;
}
public abstract class JudgeAction {
public static JudgeAction Construct(HashSet<string> a, string k) {
if (a.Remove("pass")) {
return new Pass(a, from i in k.Split(',') select new Identifier(i.Trim()));
}
else if (a.Remove("call")) {
return new Call(a, new Identifier(k));
}
else if (a.Remove("score")) {
return new Score(a, k);
}
else if (a.Remove("var")) {
return new Variable(a, new Identifier(k));
}
throw new FormatException("Invalid judge action format.");
}
public readonly HashSet<string> annotations;
public JudgeAction(IEnumerable<string> a) {
annotations = a.ToHashSet();
}
public virtual void Optimize(PdtEvaluatorBase etor, PdtExpression value) { etor.Optimize(value); }
internal abstract JudgeActionResult Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, bool onMiss, int depth, int index);
public class Call : JudgeAction {
readonly Identifier _target;
public Call(IEnumerable<string> a, Identifier k) : base(a) {
_target = k;
}
internal override JudgeActionResult Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, bool onMiss, int depth, int index) {
judge.Call(ev, time, _target, onMiss, index);
return new JudgeActionResult { BreakExecution = true, PreventRecycle = true };
}
}
public class Pass : JudgeAction {
readonly Identifier[] _targets;
public Pass(IEnumerable<string> a, IEnumerable<Identifier> k) : base(a) {
_targets = k.ToArray();
}
internal override JudgeActionResult Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, bool onMiss, int depth, int index) {
return new JudgeActionResult { BreakExecution = judge.Pass(ev, time, _targets, onMiss, depth) };
}
}
public class Score : JudgeAction {
readonly ScoreOperation _op;
public Score(IEnumerable<string> a, string k) : base(a) {
_op = new ScoreOperation(k);
}
public Score(ScoreOperation op) : base(Enumerable.Empty<string>()) {
_op = op;
}
public override void Optimize(PdtEvaluatorBase etor, PdtExpression value) {
base.Optimize(etor, value);
if (_op.op != default(Identifier)) PdtExpression.PatchCompound(_op.name.Key, _op.op.Key, value);
}
internal override JudgeActionResult Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, bool onMiss, int depth, int index) {
judge.UpdateScore(_op, exp);
return new JudgeActionResult();
}
}
public class Variable : JudgeAction {
readonly Identifier _target;
public Variable(IEnumerable<string> a, Identifier k) : base(a) {
_target = k;
}
internal override JudgeActionResult Execute(IJudge judge, JudgeEvent ev, float time, PdtExpression exp, bool onMiss, int depth, int index) {
// throw new NotImplementedException();
return new JudgeActionResult();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 52c6416297266354999ce5ff46a568dc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,216 @@
using Cryville.Common;
using Cryville.Common.Collections.Generic;
using Cryville.Common.Pdt;
using Cryville.Crtr.Extension;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Cryville.Crtr.Ruleset {
public class RulesetDefinition : MetaInfo {
public const long CURRENT_FORMAT = 2;
[JsonRequired]
public long format;
public string @base;
[JsonIgnore]
public PdtRuleset Root { get; private set; }
public void LoadPdt(DirectoryInfo dir) {
using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + data + ".pdt", Encoding.UTF8)) {
var src = pdtreader.ReadToEnd();
Root = (PdtRuleset)new RulesetInterpreter(src, null).Interpret(typeof(PdtRuleset));
}
}
}
[Binder(typeof(PdtBinder))]
public class PdtRuleset {
public Dictionary<Identifier, ConfigDefinition> configs;
public Dictionary<Identifier, MotionDefinition> motions;
public Dictionary<Identifier, InputDefinition> inputs;
public Dictionary<Identifier, PdtExpression> areas;
public Dictionary<Identifier, JudgeDefinition> judges;
public Dictionary<Identifier, ScoreDefinition> scores;
public Constraint constraints;
public void Optimize(PdtEvaluatorBase etor) {
foreach (var i in inputs) {
var input = i.Value;
if (input.pass != null) foreach (var e in input.pass) {
etor.Optimize(e.Value);
}
}
if (areas != null) foreach (var a in areas) {
etor.Optimize(a.Value);
}
foreach (var j in judges) {
var judge = j.Value;
if (judge.hit != null) etor.Optimize(judge.hit);
if (judge.on_hit != null) OptimizeJudgeActions(judge.on_hit, etor);
if (judge.on_miss != null) OptimizeJudgeActions(judge.on_miss, etor);
}
foreach (var s in scores) {
var score = s.Value;
if (score.value != null) etor.Optimize(score.value);
}
constraints.Optimize(etor);
}
void OptimizeJudgeActions(PairList<JudgeAction, PdtExpression> actions, PdtEvaluatorBase etor) {
foreach (var a in actions) a.Key.Optimize(etor, a.Value);
}
public void PrePatch(Chart chart) {
constraints.PrePatch(chart);
}
}
public class ConfigDefinition {
public string category;
public ConfigType type;
public PdtExpression @default;
public PdtExpression range;
public PdtExpression value;
}
public enum ConfigType {
unknown, number, number_stepped,
}
public class MotionDefinition {
// TODO
}
public class InputDefinition {
public int dim;
public string pdim;
public bool notnull;
public PairList<Identifier, PdtExpression> pass;
}
public class JudgeDefinition {
public int stack;
public int prop;
public PdtExpression clip;
public PdtExpression input;
public PdtExpression hit;
public PdtExpression persist;
public PairList<JudgeAction, PdtExpression> on_hit;
public PairList<JudgeAction, PdtExpression> on_miss;
#pragma warning disable IDE1006
public PairList<ScoreOperation, PdtExpression> scores {
set {
if (on_hit == null) on_hit = new PairList<JudgeAction, PdtExpression>();
int i = 0;
foreach (var s in value) {
on_hit.Insert(i++, new JudgeAction.Score(s.Key), s.Value);
}
}
}
public Identifier[] pass {
set {
if (on_hit == null) on_hit = new PairList<JudgeAction, PdtExpression>();
on_hit.Add(new JudgeAction.Pass(Enumerable.Empty<string>(), value), PdtExpression.Empty);
}
}
public Identifier[] miss {
set {
if (on_miss == null) on_miss = new PairList<JudgeAction, PdtExpression>();
on_miss.Add(new JudgeAction.Pass(Enumerable.Empty<string>(), value), PdtExpression.Empty);
}
}
#pragma warning restore IDE1006
}
public class ScoreOperation {
public Identifier name;
public Identifier op;
public ScoreOperation(Identifier name, Identifier op) {
this.name = name;
this.op = op;
}
public ScoreOperation(string str) {
var m = Regex.Match(str, @"^(\S+)\s*?(\S+)?$");
name = new Identifier(m.Groups[1].Value);
if (!m.Groups[2].Success) return;
op = new Identifier(m.Groups[2].Value);
}
public override string ToString() {
if (op == default(Identifier)) return name.ToString();
else return string.Format("{0} {1}", name, op);
}
}
public class ScoreDefinition {
public PdtExpression value;
public float init = 0;
public string format = "";
}
public class Constraint {
static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary();
[ElementList]
public PairList<RulesetSelectors, Constraint> Elements = new PairList<RulesetSelectors, Constraint>();
[PropertyList]
public PairList<ConstraintKey, PdtExpression> Properties = new PairList<ConstraintKey, PdtExpression>();
public void Optimize(PdtEvaluatorBase etor) {
foreach (var e in Properties) {
etor.Optimize(e.Value);
}
foreach (var e in Elements) {
e.Key.Optimize(etor);
e.Value.Optimize(etor);
}
}
public void PrePatch(ChartEvent ev) {
var etor = PdtEvaluator.Instance;
PropSrc src;
etor.ContextCascadeInsert();
etor.ContextEvent = ev;
foreach (var prop in Properties) {
var name = prop.Key.Name;
switch (prop.Key.Type) {
case ConstraintType.Property:
if (ev.PropSrcs.TryGetValue(name, out src))
etor.ContextSelfValue = src;
etor.Evaluate(ev.PropOps[name], prop.Value);
etor.ContextSelfValue = null;
break;
case ConstraintType.Variable:
_arbop.Name = name;
etor.Evaluate(_arbop, prop.Value);
break;
default: throw new NotSupportedException("Unknown property key type");
}
}
etor.ContextEvent = null;
foreach (var el in Elements) {
var targets = el.Key.Match(ev);
if (targets == null) continue;
foreach (var target in targets)
el.Value.PrePatch(target);
}
etor.ContextCascadeDiscard();
}
}
public class ConstraintKey {
public ConstraintType Type { get; private set; }
public int Name { get; private set; }
public ConstraintKey(ConstraintType type, string name) {
Type = type;
Name = IdentifierManager.Shared.Request(name);
}
public override string ToString() {
switch (Type) {
case ConstraintType.Property: return (string)IdentifierManager.Shared.Retrieve(Name);
case ConstraintType.Variable: return string.Format("@var {0}", IdentifierManager.Shared.Retrieve(Name));
default: return string.Format("<{0}> {1}", Type, IdentifierManager.Shared.Retrieve(Name));
}
}
}
public enum ConstraintType {
Property,
Variable,
}
public class RulesetViolationException : Exception {
public RulesetViolationException() { }
public RulesetViolationException(string message) : base(message) { }
public RulesetViolationException(string message, Exception innerException) : base(message, innerException) { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d7a277a6e9217e4591c39d1a220cbcf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,78 @@
using Cryville.Common.Collections.Generic;
using Cryville.Common.Pdt;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Cryville.Crtr.Ruleset {
internal class RulesetInterpreter : PdtInterpreter {
public RulesetInterpreter(string src, Binder binder) : base(src, binder) { }
readonly List<RulesetSelector> s = new List<RulesetSelector>();
readonly HashSet<string> a = new HashSet<string>();
protected override object InterpretKey(Type type) {
if (PairCollection<JudgeAction, PdtExpression>.IsPairCollection(type))
return InterpretJudgeAction();
else if (type == typeof(Constraint))
return InterpretConstraintKey();
else
return base.InterpretKey(type);
}
object InterpretJudgeAction() {
a.Clear();
while (true) {
int pp = Position;
switch (cc) {
case '@':
GetChar();
a.Add(GetIdentifier());
break;
default:
return JudgeAction.Construct(a, (string)base.InterpretKey(null));
}
ws();
if (Position == pp) throw new FormatException("Invalid judge action format.");
}
}
object InterpretConstraintKey() {
s.Clear(); a.Clear();
string key = "";
while (true) {
int pp = Position;
switch (cc) {
case '@':
GetChar();
a.Add(GetIdentifier());
break;
case '$':
GetChar();
s.Add(new RulesetSelector.CreateItem());
key = null;
break;
case '#':
GetChar();
s.Add(new RulesetSelector.Index());
break;
case '>':
GetChar();
s.Add(new RulesetSelector.Property(GetExp()));
break;
case ';':
case ':':
return new ConstraintKey(a.Contains("var") ? ConstraintType.Variable : ConstraintType.Property, key);
case '{':
return new RulesetSelectors(s);
case '}':
return null;
default:
var p4 = GetIdentifier();
s.Add(new RulesetSelector.EventType(p4));
if (key != null) key += p4;
break;
}
ws();
if (Position == pp) throw new FormatException("Invalid selector or key format.");
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 78904c737c51e254ab55b6686d964837
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,102 @@
using Cryville.Common.Pdt;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cryville.Crtr.Ruleset {
public class RulesetSelectors {
readonly RulesetSelector[] selectors;
public RulesetSelectors(IEnumerable<RulesetSelector> s) {
selectors = s.ToArray();
}
public void Optimize(PdtEvaluatorBase etor) {
for (int i = 0; i < selectors.Length; i++) {
selectors[i].Optimize(etor);
}
}
public IEnumerable<ChartEvent> Match(ChartEvent ev) {
IEnumerable<ChartEvent> result = new ChartEvent[] { ev };
foreach (var sel in selectors) {
IEnumerable<ChartEvent> temp = new ChartEvent[0];
foreach (var e in result) {
var m = sel.Match(e);
if (m != null) temp = temp.Concat(m);
}
result = temp;
}
return result;
}
public override string ToString() {
if (selectors.Length == 0) return "";
bool flag = false;
string r = "";
foreach (var s in selectors) {
if (flag) r += " " + s.ToString();
else { r += s.ToString(); flag = true; }
}
return r;
}
}
public abstract class RulesetSelector {
public virtual void Optimize(PdtEvaluatorBase etor) { }
public abstract IEnumerable<ChartEvent> Match(ChartEvent ev);
public class CreateItem : RulesetSelector {
public override string ToString() {
return "$";
}
public override IEnumerable<ChartEvent> Match(ChartEvent ev) {
if (!(ev is EventList)) throw new ArgumentException("Event is not event list");
var tev = (EventList)ev;
var result = tev.Create();
tev.Events.Add(result); // TODO create at
return new ChartEvent[] { result };
}
}
public class EventType : RulesetSelector {
readonly string _type;
public EventType(string type) { _type = type; }
public override string ToString() {
return _type;
}
public override IEnumerable<ChartEvent> Match(ChartEvent ev) {
if (!(ev is EventContainer)) throw new ArgumentException("Event is not container");
var tev = (EventContainer)ev;
return new ChartEvent[] { tev.GetEventsOfType(_type) };
}
}
public class Index : RulesetSelector {
public override string ToString() {
return "#";
}
public override IEnumerable<ChartEvent> Match(ChartEvent ev) {
if (!(ev is EventList)) throw new ArgumentException("Event is not event list");
var tev = (EventList)ev;
return tev.Events; // TODO select at
}
}
public class Property : RulesetSelector {
readonly PdtExpression _exp;
readonly PdtOperator _op;
bool _flag;
public Property(PdtExpression exp) {
_exp = exp;
_op = new PropOp.Boolean(v => _flag = v);
}
public override string ToString() {
return string.Format("> {{{0}}}", _exp);
}
public override void Optimize(PdtEvaluatorBase etor) {
etor.Optimize(_exp);
}
public override IEnumerable<ChartEvent> Match(ChartEvent ev) {
PdtEvaluator.Instance.ContextEvent = ev;
if (!PdtEvaluator.Instance.Evaluate(_op, _exp))
throw new EvaluationFailureException();
PdtEvaluator.Instance.ContextEvent = null;
if (_flag) return new ChartEvent[] { ev };
else return null;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 770e66e317b9b3848a16736b68414b43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: