From 0cccb170c11f81a322aa279fceeaadce23a7ffe2 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Sun, 26 Feb 2023 16:20:25 +0800 Subject: [PATCH] Support extra annotations on skin property key. Code cleanup. --- Assets/Cryville/Crtr/SkinInterpreter.cs | 62 ++----------------------- Assets/Cryville/Crtr/SkinPropertyKey.cs | 62 +++++++++++++++++++++---- 2 files changed, 58 insertions(+), 66 deletions(-) diff --git a/Assets/Cryville/Crtr/SkinInterpreter.cs b/Assets/Cryville/Crtr/SkinInterpreter.cs index dc8a630..858d2c1 100644 --- a/Assets/Cryville/Crtr/SkinInterpreter.cs +++ b/Assets/Cryville/Crtr/SkinInterpreter.cs @@ -1,6 +1,4 @@ -using Cryville.Common; -using Cryville.Common.Pdt; -using Cryville.Crtr.Components; +using Cryville.Common.Pdt; using System; using System.Collections.Generic; using System.Reflection; @@ -58,41 +56,7 @@ namespace Cryville.Crtr { case ';': case ':': if (invalidKeyFlag) throw new FormatException("Invalid key format"); - if (a.Contains("has")) { - if (k.Count != 1) throw new FormatException("Invalid anchor name"); - return new SkinPropertyKey.CreateAnchor { - Name = IdentifierManager.SharedInstance.Request(k[0]) - }; - } - else if (a.Contains("at")) { - if (k.Count != 1) throw new FormatException("Invalid anchor name"); - return new SkinPropertyKey.SetAnchor { - Name = IdentifierManager.SharedInstance.Request(k[0]) - }; - } - else if (a.Contains("emit")) { - if (k.Count != 1) throw new FormatException("Invalid effect name"); - return new SkinPropertyKey.EmitEffect { - Name = IdentifierManager.SharedInstance.Request(k[0]) - }; - } - switch (k.Count) { - case 1: - if (compKeyFlag) return new SkinPropertyKey.CreateComponent { - Component = GetComponentByName(k[0]) - }; - else return new SkinPropertyKey.SetProperty { - Component = typeof(TransformInterface), - Name = IdentifierManager.SharedInstance.Request(k[0]) - }; - case 2: - return new SkinPropertyKey.SetProperty { - Component = GetComponentByName(k[0]), - Name = IdentifierManager.SharedInstance.Request(k[1]) - }; - default: - throw new FormatException("Unknown error"); // Unreachable - } + return SkinPropertyKey.Construct(a, k, compKeyFlag); case '{': return new SkinSelectors(s, a); case '}': @@ -127,7 +91,7 @@ namespace Cryville.Crtr { if (cc != '{') throw new FormatException("Invalid span format"); return new Clip(start, end); } - k.Clear(); + a.Clear(); k.Clear(); while (true) { int pp = Position; switch (cc) { @@ -139,20 +103,7 @@ namespace Cryville.Crtr { break; case ';': case ':': - switch (k.Count) { - case 1: - return new SkinPropertyKey.SetProperty { - Component = typeof(TransformInterface), - Name = IdentifierManager.SharedInstance.Request(k[0]) - }; - case 2: - return new SkinPropertyKey.SetProperty { - Component = GetComponentByName(k[0]), - Name = IdentifierManager.SharedInstance.Request(k[1]) - }; - default: - throw new FormatException("Unknown error"); // Unreachable - } + return SkinPropertyKey.Construct(a, k, false); case '{': throw new FormatException("Invalid token"); case '}': @@ -167,10 +118,5 @@ namespace Cryville.Crtr { if (Position == pp) throw new FormatException("Invalid selector or key format"); } } - static Type GetComponentByName(string name) { - Type result; - if (GenericResources.Components.TryGetValue(name, out result)) return result; - throw new ArgumentException(string.Format("Component type \"{0}\" not found", name)); - } } } diff --git a/Assets/Cryville/Crtr/SkinPropertyKey.cs b/Assets/Cryville/Crtr/SkinPropertyKey.cs index 3fd4da0..6727b43 100644 --- a/Assets/Cryville/Crtr/SkinPropertyKey.cs +++ b/Assets/Cryville/Crtr/SkinPropertyKey.cs @@ -2,16 +2,53 @@ using Cryville.Common.Pdt; using Cryville.Crtr.Components; using System; +using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Cryville.Crtr { public abstract class SkinPropertyKey { + public static SkinPropertyKey Construct(HashSet a, IReadOnlyList k, bool compKeyFlag) { + if (a.Remove("has")) { + if (k.Count != 1) throw new FormatException("Invalid anchor name"); + return new CreateAnchor(a, IdentifierManager.SharedInstance.Request(k[0])); + } + else if (a.Remove("at")) { + if (k.Count != 1) throw new FormatException("Invalid anchor name"); + return new SetAnchor(a, IdentifierManager.SharedInstance.Request(k[0])); + } + else if (a.Remove("emit")) { + if (k.Count != 1) throw new FormatException("Invalid effect name"); + return new EmitEffect(a, IdentifierManager.SharedInstance.Request(k[0])); + } + switch (k.Count) { + case 1: + if (compKeyFlag) return new CreateComponent(a, GetComponentByName(k[0])); + else return new SetProperty(a, typeof(TransformInterface), IdentifierManager.SharedInstance.Request(k[0])); + case 2: + return new SetProperty(a, GetComponentByName(k[0]), IdentifierManager.SharedInstance.Request(k[1])); + default: + throw new FormatException("Unknown error"); + } + static Type GetComponentByName(string name) { + Type result; + if (GenericResources.Components.TryGetValue(name, out result)) return result; + throw new ArgumentException(string.Format("Component type \"{0}\" not found", name)); + } + } + public readonly HashSet annotations; + public SkinPropertyKey(IEnumerable a) { + annotations = a.ToHashSet(); + } public abstract override string ToString(); public abstract bool IsValueRequired { get; } public abstract void ExecuteStatic(ISkinnableGroup group, RuntimeSkinContext ctx, PdtExpression exp); public abstract void ExecuteDynamic(ISkinnableGroup group, RuntimeSkinContext ctx, PdtExpression exp, int dl); public class CreateComponent : SkinPropertyKey { - public Type Component { get; set; } + public Type Component { get; private set; } + public CreateComponent(IEnumerable a, Type component) : base(a) { + Component = component; + } public override string ToString() { return string.Format("*{0}", Component.Name); } @@ -24,8 +61,12 @@ namespace Cryville.Crtr { } } public class SetProperty : SkinPropertyKey { - public Type Component { get; set; } - public int Name { get; set; } + public Type Component { get; private set; } + public int Name { get; private set; } + public SetProperty(IEnumerable a, Type component, int name) : base(a) { + Component = component; + Name = name; + } public override string ToString() { return string.Format("{0}.{1}", Component.Name, IdentifierManager.SharedInstance.Retrieve(Name)); } @@ -61,7 +102,10 @@ namespace Cryville.Crtr { } } public class CreateAnchor : SkinPropertyKey { - public int Name { get; set; } + public int Name { get; private set; } + public CreateAnchor(IEnumerable a, int name) : base(a) { + Name = name; + } public override string ToString() { return string.Format("@has {0}", IdentifierManager.SharedInstance.Retrieve(Name)); } @@ -74,8 +118,9 @@ namespace Cryville.Crtr { } } public class SetAnchor : SkinPropertyKey { - public int Name { get; set; } - public SetAnchor() { + public int Name { get; private set; } + public SetAnchor(IEnumerable a, int name) : base(a) { + Name = name; _timeOp = new PropOp.Float(v => _time = v); } public override string ToString() { @@ -97,8 +142,9 @@ namespace Cryville.Crtr { } } public class EmitEffect : SkinPropertyKey { - public int Name { get; set; } - public EmitEffect() { + public int Name { get; private set; } + public EmitEffect(IEnumerable a, int name) : base(a) { + Name = name; _op = new PropOp.Float(v => _index = v); } public override string ToString() {