Introduce IdentifierManager to improve PDT evaluator performance.

This commit is contained in:
2022-11-01 13:47:04 +08:00
parent 3bfc7eb643
commit 2c9be2ef1e
12 changed files with 229 additions and 145 deletions

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
namespace Cryville.Common {
/// <summary>
/// A manager that assigns each given identifiers a unique integer ID.
/// </summary>
public class IdentifierManager {
/// <summary>
/// A shared instance of the <see cref="IdentifierManager" /> class.
/// </summary>
public static IdentifierManager SharedInstance = new IdentifierManager();
Dictionary<object, int> _idents = new Dictionary<object, int>();
List<object> _ids = new List<object>();
object _syncRoot = new object();
/// <summary>
/// Requests an integer ID for an identifier.
/// </summary>
/// <param name="ident">The identifier.</param>
/// <returns>The integer ID.</returns>
public int Request(object ident) {
lock (_syncRoot) {
int id;
if (!_idents.TryGetValue(ident, out id)) {
_idents.Add(ident, id = _idents.Count);
_ids.Add(ident);
}
return id;
}
}
/// <summary>
/// Retrieves the identifier assigned with an integer ID.
/// </summary>
/// <param name="id">The integer ID.</param>
/// <returns>The identifier.</returns>
public object Retrieve(int id) {
lock (_syncRoot) {
return _ids[id];
}
}
}
}

View File

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

View File

@@ -47,7 +47,7 @@ namespace Cryville.Common.Pdt {
var i = _rip.Value; var i = _rip.Value;
if (i is PdtInstruction.Operate) { if (i is PdtInstruction.Operate) {
int fc0 = _framecount; int fc0 = _framecount;
int fc1 = ((PdtInstruction.Operate)i).ParamCount; int fc1 = ((PdtInstruction.Operate)i).Signature.ParamCount;
try { i.Execute(this); } catch (Exception) { } try { i.Execute(this); } catch (Exception) { }
if (fc0 - _framecount == fc1) { if (fc0 - _framecount == fc1) {
unsafe { unsafe {
@@ -137,7 +137,7 @@ namespace Cryville.Common.Pdt {
_goffset += value.Length; _goffset += value.Length;
} }
} }
internal unsafe void PushVariable(ref string name) { internal unsafe void PushVariable(int name) {
fixed (StackFrame* frame = &_stack[_framecount++]) { fixed (StackFrame* frame = &_stack[_framecount++]) {
byte[] value; byte[] value;
GetVariable(name, out frame->Type, out value); GetVariable(name, out frame->Type, out value);
@@ -153,12 +153,12 @@ namespace Cryville.Common.Pdt {
/// <param name="name">The name of the variable.</param> /// <param name="name">The name of the variable.</param>
/// <param name="type">The type of the variable.</param> /// <param name="type">The type of the variable.</param>
/// <param name="value">The value of the variable.</param> /// <param name="value">The value of the variable.</param>
protected abstract void GetVariable(string name, out int type, out byte[] value); protected abstract void GetVariable(int name, out int type, out byte[] value);
internal void Operate(ref string name, int pc) { internal void Operate(PdtOperatorSignature sig) {
PdtOperator op; PdtOperator op;
try { op = GetOperator(name, pc); } try { op = GetOperator(sig); }
catch (Exception) { _framecount -= pc; return; } catch (Exception) { _framecount -= sig.ParamCount; return; }
Operate(op, pc); Operate(op, sig.ParamCount);
} }
/// <summary> /// <summary>
/// Gets an operator of the specified name and the suggested parameter count. /// Gets an operator of the specified name and the suggested parameter count.
@@ -167,7 +167,7 @@ namespace Cryville.Common.Pdt {
/// <param name="pc">Suggested parameter count.</param> /// <param name="pc">Suggested parameter count.</param>
/// <returns>An operator of the specific name.</returns> /// <returns>An operator of the specific name.</returns>
/// <remarks>The parameter count of the returned operator does not necessarily equal to <paramref name="pc" />.</remarks> /// <remarks>The parameter count of the returned operator does not necessarily equal to <paramref name="pc" />.</remarks>
protected abstract PdtOperator GetOperator(string name, int pc); protected abstract PdtOperator GetOperator(PdtOperatorSignature sig);
unsafe void Operate(PdtOperator op, int pc, bool noset = false) { unsafe void Operate(PdtOperator op, int pc, bool noset = false) {
fixed (byte* pmem = _mem) { fixed (byte* pmem = _mem) {
op.Begin(this); op.Begin(this);
@@ -179,7 +179,7 @@ namespace Cryville.Common.Pdt {
op.Call(pmem + _goffset, noset); op.Call(pmem + _goffset, noset);
} }
} }
internal unsafe void Collapse(ref string name, LinkedListNode<PdtInstruction> target) { internal unsafe void Collapse(int name, LinkedListNode<PdtInstruction> target) {
fixed (byte* pmem = _mem) { fixed (byte* pmem = _mem) {
var frame = _stack[--_framecount]; var frame = _stack[--_framecount];
if (Collapse(name, new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length))) { if (Collapse(name, new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length))) {
@@ -194,7 +194,7 @@ namespace Cryville.Common.Pdt {
/// <param name="name">The name of the collapse operator.</param> /// <param name="name">The name of the collapse operator.</param>
/// <param name="param">The top frame in the stack as the parameter.</param> /// <param name="param">The top frame in the stack as the parameter.</param>
/// <returns>Whether to jump to the target of the collapse instruction.</returns> /// <returns>Whether to jump to the target of the collapse instruction.</returns>
protected abstract bool Collapse(string name, PdtVariableMemory param); protected abstract bool Collapse(int name, PdtVariableMemory param);
internal unsafe PdtVariableMemory StackAlloc(int type, byte* ptr, int len) { internal unsafe PdtVariableMemory StackAlloc(int type, byte* ptr, int len) {
fixed (StackFrame* frame = &_stack[_framecount++]) { fixed (StackFrame* frame = &_stack[_framecount++]) {
frame->Type = type; frame->Type = type;

View File

@@ -50,43 +50,38 @@ namespace Cryville.Common.Pdt {
} }
} }
public class PushVariable : PdtInstruction { public class PushVariable : PdtInstruction {
private string m_name; public int Name { get; private set; }
public string Name { get { return m_name; } }
public PushVariable(string name) { public PushVariable(string name) {
m_name = name; Name = IdentifierManager.SharedInstance.Request(name);
} }
internal override void Execute(PdtEvaluatorBase etor) { internal override void Execute(PdtEvaluatorBase etor) {
etor.PushVariable(ref m_name); etor.PushVariable(Name);
} }
public override string ToString() { public override string ToString() {
return string.Format("pushv {0}", Name); return string.Format("pushv {0}", Name);
} }
} }
public class Operate : PdtInstruction { public class Operate : PdtInstruction {
private string m_name; public PdtOperatorSignature Signature { get; private set; }
public string Name { get { return m_name; } }
public int ParamCount { get; private set; }
public Operate(string name, int paramCount) { public Operate(string name, int paramCount) {
m_name = name; Signature = new PdtOperatorSignature(name, paramCount);
ParamCount = paramCount;
} }
internal override void Execute(PdtEvaluatorBase etor) { internal override void Execute(PdtEvaluatorBase etor) {
etor.Operate(ref m_name, ParamCount); etor.Operate(Signature);
} }
public override string ToString() { public override string ToString() {
return string.Format("op {0}({1})", Name, ParamCount); return string.Format("op {0}", Signature);
} }
} }
public class Collapse : PdtInstruction { public class Collapse : PdtInstruction {
private string m_name; public int Name { get; private set; }
public string Name { get { return m_name; } }
public LinkedListNode<PdtInstruction> Target { get; internal set; } public LinkedListNode<PdtInstruction> Target { get; internal set; }
public Collapse(string name, LinkedListNode<PdtInstruction> target) { public Collapse(string name, LinkedListNode<PdtInstruction> target) {
m_name = name; Name = IdentifierManager.SharedInstance.Request(name);
Target = target; Target = target;
} }
internal override void Execute(PdtEvaluatorBase etor) { internal override void Execute(PdtEvaluatorBase etor) {
etor.Collapse(ref m_name, Target); etor.Collapse(Name, Target);
} }
public override string ToString() { public override string ToString() {
return string.Format("col {0}{{{1}}}", Name, Target.Value); return string.Format("col {0}{{{1}}}", Name, Target.Value);

View File

@@ -74,4 +74,48 @@ namespace Cryville.Common.Pdt {
return _etor.StackAlloc(type, _prmem, len); return _etor.StackAlloc(type, _prmem, len);
} }
} }
/// <summary>
/// The signature of a <see cref="PdtOperator" />.
/// </summary>
public struct PdtOperatorSignature : IEquatable<PdtOperatorSignature> {
/// <summary>
/// The name of the operator.
/// </summary>
public int Name { get; private set; }
/// <summary>
/// The parameter count.
/// </summary>
public int ParamCount { get; private set; }
readonly int _hash;
/// <summary>
/// Creates an operator signature.
/// </summary>
/// <param name="name">The name of the operator.</param>
/// <param name="paramCount">The parameter count.</param>
public PdtOperatorSignature(string name, int paramCount)
: this(IdentifierManager.SharedInstance.Request(name), paramCount) { }
/// <summary>
/// Creates an operator signature.
/// </summary>
/// <param name="name">The identifier of the operator.</param>
/// <param name="paramCount">The parameter count.</param>
public PdtOperatorSignature(int name, int paramCount) {
Name = name;
ParamCount = paramCount;
_hash = Name ^ ((ParamCount << 16) | (ParamCount >> 16));
}
public override bool Equals(object obj) {
if (!(obj is PdtOperatorSignature)) return false;
return Equals((PdtOperatorSignature)obj);
}
public bool Equals(PdtOperatorSignature other) {
return Name == other.Name && ParamCount == other.ParamCount;
}
public override int GetHashCode() {
return _hash;
}
public override string ToString() {
return string.Format("{0}({1})", Name, ParamCount);
}
}
} }

View File

@@ -101,11 +101,10 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset on the span to start reading from.</param> /// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>The name of an undefined identifier.</returns> /// <returns>The name of an undefined identifier.</returns>
/// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception> /// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception>
public string AsIdentifier(int offset = 0) { public int AsIdentifier(int offset = 0) {
if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array) if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an identifier"); throw new InvalidCastException("Not an identifier");
var len = *(int*)(_ptr + offset); return *(int*)(_ptr + offset);
return new string((char*)(_ptr + offset + sizeof(int)), 0, len);
} }
internal void* TrustedAsOfLength(int len) { internal void* TrustedAsOfLength(int len) {
if (Length < len) if (Length < len)

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json; using Cryville.Common;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@@ -112,19 +113,19 @@ namespace Cryville.Crtr {
} }
[JsonIgnore] [JsonIgnore]
public Dictionary<string, PropSrc> PropSrcs { get; private set; } public Dictionary<int, PropSrc> PropSrcs { get; private set; }
protected void SubmitPropSrc(string name, PropSrc property) { protected void SubmitPropSrc(string name, PropSrc property) {
PropSrcs.Add(name, property); PropSrcs.Add(IdentifierManager.SharedInstance.Request(name), property);
} }
[JsonIgnore] [JsonIgnore]
public Dictionary<string, PropOp> PropOps { get; private set; } public Dictionary<int, PropOp> PropOps { get; private set; }
protected void SubmitPropOp(string name, PropOp property) { protected void SubmitPropOp(string name, PropOp property) {
PropOps.Add(name, property); PropOps.Add(IdentifierManager.SharedInstance.Request(name), property);
} }
protected ChartEvent() { protected ChartEvent() {
PropSrcs = new Dictionary<string, PropSrc>(); PropSrcs = new Dictionary<int, PropSrc>();
PropOps = new Dictionary<string, PropOp>(); PropOps = new Dictionary<int, PropOp>();
SubmitPropSrc("long", new PropSrc.Boolean(() => IsLong)); SubmitPropSrc("long", new PropSrc.Boolean(() => IsLong));
SubmitPropSrc("time", new PropSrc.BeatTime(() => time.Value)); SubmitPropSrc("time", new PropSrc.BeatTime(() => time.Value));
SubmitPropSrc("endtime", new PropSrc.BeatTime(() => endtime.Value)); SubmitPropSrc("endtime", new PropSrc.BeatTime(() => endtime.Value));

View File

@@ -115,12 +115,12 @@ namespace Cryville.Crtr {
cbus.ForwardByTime(dt); cbus.ForwardByTime(dt);
bbus.ForwardByTime(dt); bbus.ForwardByTime(dt);
UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.FeedJudge"); UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.FeedJudge");
judge.StartFrame(); /*judge.StartFrame();
Game.InputManager.EnumerateEvents(ev => { Game.InputManager.EnumerateEvents(ev => {
// Logger.Log("main", 0, "Input", ev.ToString()); // Logger.Log("main", 0, "Input", ev.ToString());
judge.Feed(ev); judge.Feed(ev);
}); });
judge.EndFrame(); judge.EndFrame();*/
UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.EndSample();
UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward"); UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward");
UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy"); UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy");

View File

@@ -1,3 +1,4 @@
using Cryville.Common;
using Cryville.Common.Pdt; using Cryville.Common.Pdt;
using Cryville.Common.Unity.Input; using Cryville.Common.Unity.Input;
using System.Collections.Generic; using System.Collections.Generic;
@@ -7,24 +8,22 @@ namespace Cryville.Crtr {
readonly PdtRuleset _rs; readonly PdtRuleset _rs;
public Judge(PdtRuleset rs) { public Judge(PdtRuleset rs) {
_rs = rs; _rs = rs;
foreach (var s in rs.scores) foreach (var s in rs.scores) {
scores.Add(s.Key, s.Value.init); var name = IdentifierManager.SharedInstance.Request(s.Key);
} scoreDefs.Add(name, s.Value);
public void StartFrame() { scores.Add(name, s.Value.init);
}
} }
public void Feed(InputEvent ev) { public void Feed(InputEvent ev) {
} }
public void EndFrame() { public readonly Dictionary<int, ScoreDefinition> scoreDefs = new Dictionary<int, ScoreDefinition>();
public readonly Dictionary<int, float> scores = new Dictionary<int, float>();
} readonly Dictionary<int, string> ScoreCache = new Dictionary<int, string>();
public readonly Dictionary<string, float> scores = new Dictionary<string, float>(); public Dictionary<int, string> GetFormattedScoreStrings() {
readonly Dictionary<string, string> ScoreCache = new Dictionary<string, string>();
public Dictionary<string, string> GetFormattedScoreStrings() {
if (ScoreCache.Count == 0) { if (ScoreCache.Count == 0) {
foreach (var s in scores) foreach (var s in scores)
ScoreCache.Add(s.Key, s.Value.ToString(_rs.scores[s.Key].format)); ScoreCache.Add(s.Key, s.Value.ToString(scoreDefs[s.Key].format));
} }
return ScoreCache; return ScoreCache;
} }
@@ -51,7 +50,7 @@ namespace Cryville.Crtr {
public Dictionary<string, PdtExpression> scores; public Dictionary<string, PdtExpression> scores;
} }
public class ScoreOperation { public class ScoreOperation {
public string name; public int name;
public PdtOperator op; public PdtOperator op;
} }
public class ScoreDefinition { public class ScoreDefinition {

View File

@@ -1,66 +1,54 @@
using Cryville.Common.Pdt; using Cryville.Common;
using Cryville.Common.Pdt;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace Cryville.Crtr { namespace Cryville.Crtr {
public class PdtEvaluator : PdtEvaluatorBase { public class PdtEvaluator : PdtEvaluatorBase {
static readonly Dictionary<OperatorSignature, PdtOperator> _shortops = new Dictionary<OperatorSignature, PdtOperator>(); static readonly Dictionary<PdtOperatorSignature, PdtOperator> _shortops = new Dictionary<PdtOperatorSignature, PdtOperator>();
static readonly Dictionary<string, PdtOperator> _longops = new Dictionary<string, PdtOperator>(); static readonly Dictionary<int, PdtOperator> _longops = new Dictionary<int, PdtOperator>();
readonly Dictionary<string, PdtOperator> _ctxops = new Dictionary<string, PdtOperator>(); readonly Dictionary<int, PdtOperator> _ctxops = new Dictionary<int, PdtOperator>();
struct OperatorSignature : IEquatable<OperatorSignature> {
public string Name { get; private set; }
public int ParamCount { get; private set; }
readonly int _hash;
public OperatorSignature(string name, int paramCount) {
Name = name;
ParamCount = paramCount;
_hash = name.GetHashCode() ^ paramCount;
}
public override bool Equals(object obj) {
if (!(obj is OperatorSignature)) return false;
return Equals((OperatorSignature)obj);
}
public bool Equals(OperatorSignature other) {
return Name == other.Name && ParamCount == other.ParamCount;
}
public override int GetHashCode() {
return _hash;
}
}
readonly byte[] _numbuf = new byte[4]; readonly byte[] _numbuf = new byte[4];
protected override void GetVariable(string name, out int type, out byte[] value) { static int _var_w = IdentifierManager.SharedInstance.Request("w");
switch (name) { static int _var_h = IdentifierManager.SharedInstance.Request("h");
case "w": LoadNum(ChartPlayer.hitRect.width); type = PdtInternalType.Number; value = _numbuf; return; static int _var_true = IdentifierManager.SharedInstance.Request("true");
case "h": LoadNum(ChartPlayer.hitRect.height); type = PdtInternalType.Number; value = _numbuf; return; static int _var_false = IdentifierManager.SharedInstance.Request("false");
case "true": LoadNum(1); type = PdtInternalType.Number; value = _numbuf; return; protected override void GetVariable(int name, out int type, out byte[] value) {
case "false": LoadNum(0); type = PdtInternalType.Number; value = _numbuf; return; if (name == _var_w) { LoadNum(ChartPlayer.hitRect.width); type = PdtInternalType.Number; value = _numbuf; }
default: else if (name == _var_h) { LoadNum(ChartPlayer.hitRect.height); type = PdtInternalType.Number; value = _numbuf; }
PropSrc prop; else if (name == _var_true) { LoadNum(1); type = PdtInternalType.Number; value = _numbuf; }
string str; else if (name == _var_false) { LoadNum(0); type = PdtInternalType.Number; value = _numbuf; }
if (ContextEvent != null && ContextEvent.PropSrcs.TryGetValue(name, out prop)) { else {
prop.Get(out type, out value); PropSrc prop;
} string str;
else if (ContextJudge != null && ContextJudge.GetFormattedScoreStrings().TryGetValue(name, out str)) { if (ContextEvent != null && ContextEvent.PropSrcs.TryGetValue(name, out prop)) {
type = PdtInternalType.String; prop.Get(out type, out value);
value = GetBytes(str); }
RevokePotentialConstant(); else if (ContextJudge != null && ContextJudge.GetFormattedScoreStrings().TryGetValue(name, out str)) {
} type = PdtInternalType.String;
else { value = GetBytes(str);
PropSrc.Arbitrary result; RevokePotentialConstant();
foreach (var cas in ContextCascade) { }
if (cas.TryGetValue(name, out result)) { else {
result.Get(out type, out value); PropSrc.Arbitrary result;
return; foreach (var cas in ContextCascade) {
} if (cas.TryGetValue(name, out result)) {
result.Get(out type, out value);
return;
} }
type = PdtInternalType.Undefined;
value = GetBytes(name);
} }
return; type = PdtInternalType.Undefined;
LoadNum(name);
value = _numbuf;
}
return;
} }
} }
unsafe void LoadNum(int value) {
fixed (byte* ptr = _numbuf) *(int*)ptr = value;
}
unsafe void LoadNum(float value) { unsafe void LoadNum(float value) {
fixed (byte* ptr = _numbuf) *(float*)ptr = value; fixed (byte* ptr = _numbuf) *(float*)ptr = value;
} }
@@ -75,31 +63,32 @@ namespace Cryville.Crtr {
} }
return result; return result;
} }
protected override PdtOperator GetOperator(string name, int pc) { static int _op_sep = IdentifierManager.SharedInstance.Request(",");
protected override PdtOperator GetOperator(PdtOperatorSignature sig) {
PdtOperator result; PdtOperator result;
if (name.Length == 1 && _shortops.TryGetValue(new OperatorSignature(name, pc), out result)) { if (_shortops.TryGetValue(sig, out result)) {
return result; return result;
} }
else if (name == ",") { else if (sig.Name == _op_sep) {
result = new op_arr(pc); result = new op_arr(sig.ParamCount);
_shortops.Add(new OperatorSignature(",", pc), result); _shortops.Add(new PdtOperatorSignature(",", sig.ParamCount), result);
return result; return result;
} }
else if (_longops.TryGetValue(name, out result)) { else if (_longops.TryGetValue(sig.Name, out result)) {
return result; return result;
} }
else if (_ctxops.TryGetValue(name, out result)) { else if (_ctxops.TryGetValue(sig.Name, out result)) {
return result; return result;
} }
else throw new KeyNotFoundException(string.Format("Undefined operator {0}({1})", name, pc)); else throw new KeyNotFoundException(string.Format("Undefined operator {0}", sig));
} }
protected override bool Collapse(string name, PdtVariableMemory param) { static int _colop_and = IdentifierManager.SharedInstance.Request("&");
switch (name) { static int _colop_or = IdentifierManager.SharedInstance.Request("|");
case "&": return param.AsNumber() == 0; protected override bool Collapse(int name, PdtVariableMemory param) {
case "|": return param.AsNumber() != 0; if (name == _colop_and) return param.AsNumber() == 0;
default: throw new KeyNotFoundException(string.Format("Undefined collapse operator {0}", name)); else if(name == _colop_or) return param.AsNumber() != 0;
} else throw new KeyNotFoundException(string.Format("Undefined collapse operator {0}", name));
} }
public ChartEvent ContextEvent { private get; set; } public ChartEvent ContextEvent { private get; set; }
@@ -107,11 +96,11 @@ namespace Cryville.Crtr {
public Judge ContextJudge { private get; set; } public Judge ContextJudge { private get; set; }
public PropSrc ContextSelfValue { private get; set; } public PropSrc ContextSelfValue { private get; set; }
readonly List<Dictionary<string, PropSrc.Arbitrary>> ContextCascade = new List<Dictionary<string, PropSrc.Arbitrary>>(); readonly List<Dictionary<int, PropSrc.Arbitrary>> ContextCascade = new List<Dictionary<int, PropSrc.Arbitrary>>();
public void ContextCascadeInsert() { public void ContextCascadeInsert() {
ContextCascade.Add(new Dictionary<string, PropSrc.Arbitrary>()); ContextCascade.Add(new Dictionary<int, PropSrc.Arbitrary>());
} }
public void ContextCascadeUpdate(string key, PropSrc.Arbitrary value) { public void ContextCascadeUpdate(int key, PropSrc.Arbitrary value) {
ContextCascade[ContextCascade.Count - 1][key] = value; ContextCascade[ContextCascade.Count - 1][key] = value;
} }
public void ContextCascadeDiscard() { public void ContextCascadeDiscard() {
@@ -119,29 +108,29 @@ namespace Cryville.Crtr {
} }
public PdtEvaluator() { public PdtEvaluator() {
_ctxops.Add("screen_edge", new func_screen_edge(() => ContextTransform)); _ctxops.Add(IdentifierManager.SharedInstance.Request("screen_edge"), new func_screen_edge(() => ContextTransform));
_ctxops.Add("int", new func_int(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("int"), new func_int(() => ContextSelfValue));
_ctxops.Add("clamp", new func_clamp(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("clamp"), new func_clamp(() => ContextSelfValue));
_ctxops.Add("min", new func_min(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("min"), new func_min(() => ContextSelfValue));
_ctxops.Add("max", new func_max(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("max"), new func_max(() => ContextSelfValue));
} }
static PdtEvaluator() { static PdtEvaluator() {
_shortops.Add(new OperatorSignature("*", 2), new op_mul_2()); _shortops.Add(new PdtOperatorSignature("*", 2), new op_mul_2());
_shortops.Add(new OperatorSignature("/", 2), new op_div_2()); _shortops.Add(new PdtOperatorSignature("/", 2), new op_div_2());
_shortops.Add(new OperatorSignature("%", 2), new op_mod_2()); _shortops.Add(new PdtOperatorSignature("%", 2), new op_mod_2());
_shortops.Add(new OperatorSignature("+", 1), new op_add_1()); _shortops.Add(new PdtOperatorSignature("+", 1), new op_add_1());
_shortops.Add(new OperatorSignature("+", 2), new op_add_2()); _shortops.Add(new PdtOperatorSignature("+", 2), new op_add_2());
_shortops.Add(new OperatorSignature("-", 1), new op_sub_1()); _shortops.Add(new PdtOperatorSignature("-", 1), new op_sub_1());
_shortops.Add(new OperatorSignature("-", 2), new op_sub_2()); _shortops.Add(new PdtOperatorSignature("-", 2), new op_sub_2());
_shortops.Add(new OperatorSignature("=", 2), new op_eq_2()); _shortops.Add(new PdtOperatorSignature("=", 2), new op_eq_2());
_shortops.Add(new OperatorSignature("<", 2), new op_lt_2()); _shortops.Add(new PdtOperatorSignature("<", 2), new op_lt_2());
_shortops.Add(new OperatorSignature(">", 2), new op_gt_2()); _shortops.Add(new PdtOperatorSignature(">", 2), new op_gt_2());
_shortops.Add(new OperatorSignature("!", 1), new op_not_1()); _shortops.Add(new PdtOperatorSignature("!", 1), new op_not_1());
_longops.Add("frame_seq", new func_frame_seq()); _longops.Add(IdentifierManager.SharedInstance.Request("frame_seq"), new func_frame_seq());
} }
#region Operators #region Operators
#pragma warning disable IDE1006 #pragma warning disable IDE1006

View File

@@ -1,4 +1,5 @@
using Cryville.Common.Pdt; using Cryville.Common;
using Cryville.Common.Pdt;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
@@ -36,14 +37,14 @@ namespace Cryville.Crtr {
} }
} }
public class Enum<T> : PropOp { public class Enum<T> : PropOp {
readonly static Dictionary<string, int> _cache = new Dictionary<string, int>(); readonly static Dictionary<int, int> _cache = new Dictionary<int, int>();
readonly Action<T> _cb; readonly Action<T> _cb;
public Enum(Action<T> cb) { public Enum(Action<T> cb) {
if (!typeof(T).IsEnum) if (!typeof(T).IsEnum)
throw new ArgumentException("Type is not enum"); throw new ArgumentException("Type is not enum");
var names = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static); var names = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0; i < names.Length; i++) for (int i = 0; i < names.Length; i++)
_cache[names[i].Name] = Convert.ToInt32(names[i].GetValue(null)); _cache[IdentifierManager.SharedInstance.Request(names[i].Name)] = Convert.ToInt32(names[i].GetValue(null));
_cb = cb; _cb = cb;
} }
protected override void Execute() { protected override void Execute() {

View File

@@ -59,7 +59,7 @@ namespace Cryville.Crtr {
} }
public class Constraint { public class Constraint {
class ArbitraryOp : PropOp { class ArbitraryOp : PropOp {
public string name; public int name;
protected override void Execute() { protected override void Execute() {
var op = GetOperand(0); var op = GetOperand(0);
var value = new byte[op.Length]; var value = new byte[op.Length];
@@ -114,15 +114,15 @@ namespace Cryville.Crtr {
} }
public class PropertyKey { public class PropertyKey {
public PropertyType Type { get; private set; } public PropertyType Type { get; private set; }
public string Name { get; private set; } public int Name { get; private set; }
public PropertyKey(PropertyType type, string name) { public PropertyKey(PropertyType type, string name) {
Type = type; Type = type;
Name = name; Name = IdentifierManager.SharedInstance.Request(name);
} }
public override string ToString() { public override string ToString() {
switch (Type) { switch (Type) {
case PropertyType.Property: return Name; case PropertyType.Property: return Name.ToString();
case PropertyType.Variable: return "@var " + Name; case PropertyType.Variable: return string.Format("@var {0}", Name);
default: return string.Format("<{0}> {1}", Type, Name); default: return string.Format("<{0}> {1}", Type, Name);
} }
} }
@@ -169,7 +169,7 @@ namespace Cryville.Crtr {
public pop_identstr(Action<string> cb) { _cb = cb; } public pop_identstr(Action<string> cb) { _cb = cb; }
protected override void Execute() { protected override void Execute() {
var op = GetOperand(0); var op = GetOperand(0);
if (op.Type == PdtInternalType.Undefined) _cb(op.AsIdentifier()); if (op.Type == PdtInternalType.Undefined) _cb((string)IdentifierManager.SharedInstance.Retrieve(op.AsIdentifier()));
else if (op.Type == PdtInternalType.String) _cb(op.AsString()); else if (op.Type == PdtInternalType.String) _cb(op.AsString());
else throw new InvalidCastException("Not an identifier or string"); else throw new InvalidCastException("Not an identifier or string");
} }
@@ -183,7 +183,7 @@ namespace Cryville.Crtr {
var op = GetOperand(i); var op = GetOperand(i);
if (op.Type != PdtInternalType.Undefined) if (op.Type != PdtInternalType.Undefined)
throw new InvalidCastException("Not an identifier"); throw new InvalidCastException("Not an identifier");
result[i] = op.AsIdentifier(); result[i] = (string)IdentifierManager.SharedInstance.Retrieve(op.AsIdentifier());
} }
_cb(result); _cb(result);
} }