Add project files.
This commit is contained in:
286
Assets/Cryville/Crtr/Ruleset.cs
Normal file
286
Assets/Cryville/Crtr/Ruleset.cs
Normal file
@@ -0,0 +1,286 @@
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Pdt;
|
||||
using Cryville.Crtr.Components;
|
||||
using Microsoft.Windows.PropSys;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine.Animations;
|
||||
|
||||
namespace Cryville.Crtr {
|
||||
public class Ruleset {
|
||||
[JsonRequired]
|
||||
public long format;
|
||||
|
||||
[JsonRequired]
|
||||
public string @base;
|
||||
|
||||
[JsonRequired]
|
||||
public string pdt;
|
||||
|
||||
/*[JsonIgnore][Obsolete]
|
||||
public CompiledRuleset c;*/
|
||||
|
||||
[JsonIgnore]
|
||||
public PdtRuleset Root { get; private set; }
|
||||
|
||||
public void LoadPdt(DirectoryInfo dir) {
|
||||
using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + pdt + ".pdt", Encoding.UTF8)) {
|
||||
var src = pdtreader.ReadToEnd();
|
||||
Root = new RulesetInterpreter(src, null).Interpret();
|
||||
/*c = PdtReader.Read<CompiledRuleset>(src);
|
||||
c.PreEval(new Evaluator());*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Obsolete
|
||||
#if false
|
||||
[Binder(typeof(RulesetBinder))][Obsolete]
|
||||
public class CompiledRuleset {
|
||||
public Dictionary<string, PrimaryJudge> primary_judges;
|
||||
public Dictionary<string, SecondaryJudge> secondary_judges;
|
||||
public Dictionary<string, TertiaryJudge> tertiary_judges;
|
||||
public Dictionary<string, Score> scores;
|
||||
|
||||
public void PreEval(Evaluator etor) {
|
||||
foreach (var j in primary_judges.Values) {
|
||||
foreach (var i in j.pass)
|
||||
j.PassCh.Add(i, secondary_judges[i]);
|
||||
if (j.time != null) j.time.PreEval(etor);
|
||||
}
|
||||
foreach (var j in secondary_judges.Values) {
|
||||
if (j.call != null) foreach (var i in j.call)
|
||||
j.CallCh.Add(i, secondary_judges[i]);
|
||||
if (j.pass != null) foreach (var i in j.pass)
|
||||
j.PassCh.Add(i, tertiary_judges[i]);
|
||||
if (j.time != null) j.time.PreEval(etor);
|
||||
etor.Context = new EvaluatorContext() { Reservations = new List<string>() { "npos" } };
|
||||
if (j.area != null) j.area.PreEval(etor);
|
||||
etor.Context = default(EvaluatorContext);
|
||||
}
|
||||
var scoreNames = scores.Keys.ToList();
|
||||
foreach (var j in tertiary_judges.Values) {
|
||||
if (j.time != null) j.time.PreEval(etor);
|
||||
etor.Context = new EvaluatorContext() { Reservations = new List<string>() { "npos" } };
|
||||
if (j.area != null) j.area.PreEval(etor);
|
||||
etor.Context = new EvaluatorContext() { Reservations = scoreNames };
|
||||
foreach (var s in j.scores.Values) {
|
||||
s.PreEval(etor);
|
||||
}
|
||||
etor.Context = default(EvaluatorContext);
|
||||
}
|
||||
}
|
||||
|
||||
/*public void PatchChart(EventBatcher b) {
|
||||
var etor = new EvalImpl();
|
||||
List<StampedEvent> app = new List<StampedEvent>();
|
||||
foreach (var ev in b.stampedEvents) {
|
||||
if (ev.Event is Chart.Note) {
|
||||
var tev = (Chart.Note)ev.Event;
|
||||
if (tev.judge != null)
|
||||
PatchJudge(tev, primary_judges[tev.judge], ev.Time, etor, app);
|
||||
if (tev.endjudge != null)
|
||||
PatchJudge(tev, primary_judges[tev.endjudge], ev.Time + ev.Duration, etor, app);
|
||||
}
|
||||
}
|
||||
b.stampedEvents.AddRange(app);
|
||||
}*/
|
||||
public static void PatchJudge(Chart.Note tev, PrimaryJudge pj, float t, IEvaluator etor, List<StampedEvent.Judge> l, bool endjudge = false) {
|
||||
float? min = null; float? max = null;
|
||||
if (pj.time != null) {
|
||||
var r = pj.time.Eval<List<float>>(etor);
|
||||
min = r[0]; max = r[1];
|
||||
}
|
||||
else {
|
||||
foreach (var sj in pj.PassCh.Values) {
|
||||
if (sj.time == null) continue;
|
||||
var r = sj.time.Eval<List<float>>(etor);
|
||||
if (r[0] > min || min == null) min = r[0];
|
||||
if (r[1] < max || max == null) max = r[1];
|
||||
}
|
||||
}
|
||||
var sev = new StampedEvent.Judge() {
|
||||
Time = t + (min ?? 0),
|
||||
NoteTime = t,
|
||||
Container = tev,
|
||||
TargetJudge = pj,
|
||||
IsEndJudge = endjudge,
|
||||
};
|
||||
var scev = new StampedEvent.Judge() {
|
||||
Time = t,
|
||||
NoteTime = t,
|
||||
Container = tev,
|
||||
StartEvent = sev,
|
||||
IsEndJudge = endjudge,
|
||||
};
|
||||
var seev = new StampedEvent.Judge() {
|
||||
Time = t + (max ?? 0),
|
||||
NoteTime = t,
|
||||
Container = tev,
|
||||
TargetJudge = pj,
|
||||
StartEvent = sev,
|
||||
IsEndJudge = endjudge,
|
||||
};
|
||||
l.Add(sev); l.Add(scev); l.Add(seev);
|
||||
}
|
||||
}
|
||||
[Obsolete]
|
||||
public class RulesetBinder : EmptyBinder {
|
||||
public override object ChangeType(object value, Type type, System.Globalization.CultureInfo culture) {
|
||||
try {
|
||||
var etor = new Evaluator();
|
||||
var r = ((Expression)value).Eval(etor);
|
||||
return etor.Cast(type, r);
|
||||
}
|
||||
catch (Exception) {
|
||||
return base.ChangeType(value, type, culture);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
[Binder(typeof(PdtRulesetBinder))]
|
||||
public class PdtRuleset {
|
||||
public Dictionary<string, InputDefinition> inputs;
|
||||
public Dictionary<string, JudgeDefinition> judges;
|
||||
public Dictionary<string, ScoreDefinition> scores;
|
||||
public Constraint constraints;
|
||||
public void PrePatch(Chart chart) {
|
||||
constraints.PrePatch(chart);
|
||||
}
|
||||
}
|
||||
public class Constraint {
|
||||
class ArbitraryOp : PropOp {
|
||||
public string name;
|
||||
protected override void Execute() {
|
||||
var op = GetOperand(0);
|
||||
var value = new byte[op.Length];
|
||||
op.CopyTo(value, 0);
|
||||
ChartPlayer.etor.ContextCascadeUpdate(name, new PropSrc.Arbitrary(op.Type, value));
|
||||
}
|
||||
}
|
||||
static ArbitraryOp _arbop = new ArbitraryOp();
|
||||
[ElementList]
|
||||
public Dictionary<RulesetSelectors, Constraint> Elements = new Dictionary<RulesetSelectors, Constraint>();
|
||||
[PropertyList]
|
||||
public Dictionary<PropertyKey, PdtExpression> Properties = new Dictionary<PropertyKey, PdtExpression>();
|
||||
public void PrePatch(ChartEvent ev) {
|
||||
var etor = ChartPlayer.etor;
|
||||
PropSrc src;
|
||||
etor.ContextCascadeInsert();
|
||||
etor.ContextEvent = ev;
|
||||
foreach (var prop in Properties) {
|
||||
var name = prop.Key.Name;
|
||||
switch (prop.Key.Type) {
|
||||
case PropertyType.Property:
|
||||
if (ev.PropSrcs.TryGetValue(name, out src))
|
||||
etor.ContextSelfValue = src;
|
||||
etor.Evaluate(ev.PropOps[name], prop.Value);
|
||||
etor.ContextSelfValue = null;
|
||||
break;
|
||||
case PropertyType.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 PropertyKey {
|
||||
public PropertyType Type { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public PropertyKey(PropertyType type, string name) {
|
||||
Type = type;
|
||||
Name = name;
|
||||
}
|
||||
public override string ToString() {
|
||||
switch (Type) {
|
||||
case PropertyType.Property: return Name;
|
||||
case PropertyType.Variable: return "@var " + Name;
|
||||
default: return string.Format("<{0}> {1}", Type, Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
public enum PropertyType {
|
||||
Property,
|
||||
Variable,
|
||||
}
|
||||
public class PdtRulesetBinder : EmptyBinder {
|
||||
public override object ChangeType(object value, Type type, CultureInfo culture) {
|
||||
if (value is PdtExpression) {
|
||||
var exp = (PdtExpression)value;
|
||||
if (type.Equals(typeof(bool))) {
|
||||
bool result = false;
|
||||
ChartPlayer.etor.Evaluate(new PropOp.Boolean(r => result = r), exp);
|
||||
return result;
|
||||
}
|
||||
else if (type.Equals(typeof(int))) {
|
||||
int result = 0;
|
||||
ChartPlayer.etor.Evaluate(new PropOp.Integer(r => result = r), exp);
|
||||
return result;
|
||||
}
|
||||
else if (type.Equals(typeof(float))) {
|
||||
float result = 0;
|
||||
ChartPlayer.etor.Evaluate(new PropOp.Float(r => result = r), exp);
|
||||
return result;
|
||||
}
|
||||
else if (type.Equals(typeof(string))) {
|
||||
string result = null;
|
||||
ChartPlayer.etor.Evaluate(new pop_identstr(r => result = r), exp);
|
||||
return result;
|
||||
}
|
||||
else if (type.Equals(typeof(string[]))) {
|
||||
string[] result = null;
|
||||
ChartPlayer.etor.Evaluate(new pop_identstrarr(r => result = r), exp);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return base.ChangeType(value, type, culture);
|
||||
}
|
||||
#pragma warning disable IDE1006
|
||||
class pop_identstr : PropOp {
|
||||
readonly Action<string> _cb;
|
||||
public pop_identstr(Action<string> cb) { _cb = cb; }
|
||||
protected override void Execute() {
|
||||
var op = GetOperand(0);
|
||||
if (op.Type == PdtInternalType.Undefined) _cb(op.AsIdentifier());
|
||||
else if (op.Type == PdtInternalType.String) _cb(op.AsString());
|
||||
else throw new InvalidCastException("Not an identifier or string");
|
||||
}
|
||||
}
|
||||
class pop_identstrarr : PdtOperator {
|
||||
readonly Action<string[]> _cb;
|
||||
public pop_identstrarr(Action<string[]> cb) : base(16) { _cb = cb; }
|
||||
protected override void Execute() {
|
||||
var result = new string[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] = op.AsIdentifier();
|
||||
}
|
||||
_cb(result);
|
||||
}
|
||||
}
|
||||
#pragma warning restore IDE1006
|
||||
}
|
||||
public class RulesetViolationException : Exception {
|
||||
public RulesetViolationException() { }
|
||||
public RulesetViolationException(string message) : base(message) { }
|
||||
public RulesetViolationException(string message, Exception innerException) : base(message, innerException) { }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user