Add backward compatibility for skin.

This commit is contained in:
2023-02-17 16:24:24 +08:00
parent db0165d145
commit 675ce68073
5 changed files with 32 additions and 17 deletions

View File

@@ -54,15 +54,14 @@ namespace Cryville.Common.Pdt {
/// <param name="binder">The binder.</param> /// <param name="binder">The binder.</param>
/// <returns>The interpreted object.</returns> /// <returns>The interpreted object.</returns>
public static T Interpret<T>(string src, Binder binder) { public static T Interpret<T>(string src, Binder binder) {
return (T)new PdtInterpreter(src, typeof(T), binder).Interpret(); return (T)new PdtInterpreter(src, binder).Interpret(typeof(T));
} }
/// <summary> /// <summary>
/// The source string. /// The source string.
/// </summary> /// </summary>
public string Source { get; private set; } public string Source { get; private set; }
readonly Type _type; Binder _binder;
readonly Binder _binder;
/// <summary> /// <summary>
/// The current position in the string being parsed by the interpreter. /// The current position in the string being parsed by the interpreter.
/// </summary> /// </summary>
@@ -167,23 +166,27 @@ namespace Cryville.Common.Pdt {
/// Creates an instance of the <see cref="PdtInterpreter" /> class. /// Creates an instance of the <see cref="PdtInterpreter" /> class.
/// </summary> /// </summary>
/// <param name="src">The source string.</param> /// <param name="src">The source string.</param>
/// <param name="type">The destination type.</param>
/// <param name="binder">The binder. May be <c>null</c>.</param> /// <param name="binder">The binder. May be <c>null</c>.</param>
public PdtInterpreter(string src, Type type, Binder binder) { public PdtInterpreter(string src, Binder binder) {
Source = src; Source = src;
_type = type;
_binder = binder; _binder = binder;
if (_binder == null) }
_binder = BinderAttribute.CreateBinderOfType(_type); int[] m_formatVersion;
public int[] GetFormatVersion() {
if (m_formatVersion == null) InterpretDirectives();
return m_formatVersion;
} }
/// <summary> /// <summary>
/// Interprets the source to an object. /// Interprets the source to an object.
/// </summary> /// </summary>
/// <param name="type">The output type.</param>
/// <returns>The interpreted object.</returns> /// <returns>The interpreted object.</returns>
public object Interpret() { public object Interpret(Type type) {
try { try {
InterpretDirectives(); if (m_formatVersion == null) InterpretDirectives();
return InterpretObject(_type); if (_binder == null)
_binder = BinderAttribute.CreateBinderOfType(type);
return InterpretObject(type);
} }
catch (Exception ex) { catch (Exception ex) {
throw new PdtParsingException(this, ex); throw new PdtParsingException(this, ex);
@@ -201,7 +204,10 @@ namespace Cryville.Common.Pdt {
break; break;
case "format": case "format":
ws(); ws();
if (GetNumber() != "1") m_formatVersion = (from i in GetNumber().Split('.') select int.Parse(i)).ToArray();
if (m_formatVersion.Length == 0)
throw new FormatException("Invalid format version");
if (m_formatVersion[0] != 1)
throw new NotSupportedException("Format not supported"); throw new NotSupportedException("Format not supported");
flag = true; flag = true;
break; break;

View File

@@ -22,7 +22,7 @@ namespace Cryville.Crtr {
public void LoadPdt(DirectoryInfo dir) { public void LoadPdt(DirectoryInfo dir) {
using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + data + ".pdt", Encoding.UTF8)) { using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + data + ".pdt", Encoding.UTF8)) {
var src = pdtreader.ReadToEnd(); var src = pdtreader.ReadToEnd();
Root = (PdtRuleset)new RulesetInterpreter(src, null).Interpret(); Root = (PdtRuleset)new RulesetInterpreter(src, null).Interpret(typeof(PdtRuleset));
} }
} }
} }

View File

@@ -5,7 +5,7 @@ using System.Reflection;
namespace Cryville.Crtr { namespace Cryville.Crtr {
internal class RulesetInterpreter : PdtInterpreter { internal class RulesetInterpreter : PdtInterpreter {
public RulesetInterpreter(string src, Binder binder) : base(src, typeof(PdtRuleset), binder) { } public RulesetInterpreter(string src, Binder binder) : base(src, binder) { }
readonly List<RulesetSelector> s = new List<RulesetSelector>(); readonly List<RulesetSelector> s = new List<RulesetSelector>();
readonly List<string> a = new List<string>(); readonly List<string> a = new List<string>();

View File

@@ -26,12 +26,21 @@ namespace Cryville.Crtr {
public void LoadPdt(DirectoryInfo dir) { public void LoadPdt(DirectoryInfo dir) {
using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + data + ".pdt", Encoding.UTF8)) { using (StreamReader pdtreader = new StreamReader(dir.FullName + "/" + data + ".pdt", Encoding.UTF8)) {
var src = pdtreader.ReadToEnd(); var src = pdtreader.ReadToEnd();
Root = (PdtSkin)new SkinInterpreter(src, null).Interpret(); var interpreter = new SkinInterpreter(src, null);
var format = interpreter.GetFormatVersion();
if (format.Length == 1) {
Root = new PdtSkin();
Root.elements = (SkinElement)new SkinInterpreter(src, new PdtBinder()).Interpret(typeof(SkinElement));
}
else {
if (format[1] == 1) {
Root = (PdtSkin)new SkinInterpreter(src, new PdtBinder()).Interpret(typeof(PdtSkin));
}
}
} }
} }
} }
[Binder(typeof(PdtBinder))]
public class PdtSkin { public class PdtSkin {
public Dictionary<Identifier, AnimationSpan> animations public Dictionary<Identifier, AnimationSpan> animations
= new Dictionary<Identifier, AnimationSpan>(); = new Dictionary<Identifier, AnimationSpan>();

View File

@@ -7,7 +7,7 @@ using System.Reflection;
namespace Cryville.Crtr { namespace Cryville.Crtr {
public class SkinInterpreter : PdtInterpreter { public class SkinInterpreter : PdtInterpreter {
public SkinInterpreter(string src, Binder binder) : base(src, typeof(PdtSkin), binder) { } public SkinInterpreter(string src, Binder binder) : base(src, binder) { }
readonly List<SkinSelector> s = new List<SkinSelector>(); readonly List<SkinSelector> s = new List<SkinSelector>();
readonly HashSet<string> a = new HashSet<string>(); readonly HashSet<string> a = new HashSet<string>();