Add project files.
This commit is contained in:
165
Assets/Cryville/Crtr/Skin.cs
Normal file
165
Assets/Cryville/Crtr/Skin.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Pdt;
|
||||
using Cryville.Crtr.Components;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Cryville.Crtr {
|
||||
public class Skin {
|
||||
[JsonRequired]
|
||||
public long format;
|
||||
|
||||
public string @base;
|
||||
|
||||
[JsonRequired]
|
||||
public string ruleset;
|
||||
|
||||
[JsonRequired]
|
||||
public string pdt;
|
||||
|
||||
/*[JsonIgnore][Obsolete]
|
||||
public CompiledSkin c;*/
|
||||
|
||||
[JsonIgnore]
|
||||
public PdtSkin 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 SkinInterpreter(src, null).Interpret();
|
||||
/*#pragma warning disable CS0612
|
||||
c = PdtReader.Read<CompiledSkin>(src);
|
||||
#pragma warning restore CS0612*/
|
||||
// c.PreEval(new EvalImpl());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Obsolete
|
||||
#if false
|
||||
[Obsolete]
|
||||
public class CompiledSkin : Element { }
|
||||
|
||||
[Obsolete]
|
||||
[Binder(typeof(ElementBinder))]
|
||||
public class Element {
|
||||
[ElementList]
|
||||
public Dictionary<SkinSelectors, Element> elements
|
||||
= new Dictionary<SkinSelectors, Element>();
|
||||
|
||||
[ComponentList]
|
||||
public List<string> components
|
||||
= new List<string>();
|
||||
|
||||
[PropertyList]
|
||||
public Dictionary<string, Expression> properties
|
||||
= new Dictionary<string, Expression>();
|
||||
|
||||
public bool IsDynamic {
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public void PreEval(IEvaluator etor, byte depth = 1) {
|
||||
IsDynamic = false;
|
||||
foreach (var e in properties.Values) {
|
||||
e.PreEval(etor, depth);
|
||||
if (e.IsDynamic)
|
||||
IsDynamic = true;
|
||||
}
|
||||
foreach (var e in elements.Values) {
|
||||
e.PreEval(etor, depth);
|
||||
if (e.IsDynamic)
|
||||
IsDynamic = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public class ElementBinder : EmptyBinder {
|
||||
public override object ChangeType(object value, Type type, CultureInfo culture) {
|
||||
if (value is string) {
|
||||
if (type == typeof(SkinSelectors)) {
|
||||
return new SkinSelectors((string)value);
|
||||
}
|
||||
}
|
||||
return base.ChangeType(value, type, culture);
|
||||
}
|
||||
}
|
||||
[Obsolete]
|
||||
public interface IConstructable {
|
||||
void Load(object data, IEvaluator etor);
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
public class PdtSkin : SkinElement { }
|
||||
|
||||
[Binder(typeof(SkinElementBinder))]
|
||||
public class SkinElement {
|
||||
[ElementList]
|
||||
public Dictionary<SkinSelectors, SkinElement> elements
|
||||
= new Dictionary<SkinSelectors, SkinElement>();
|
||||
|
||||
[PropertyList]
|
||||
public Dictionary<SkinPropertyKey, PdtExpression> properties
|
||||
= new Dictionary<SkinPropertyKey, PdtExpression>();
|
||||
|
||||
public bool IsDynamic {
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public void Optimize(PdtEvaluatorBase etor) {
|
||||
IsDynamic = true;
|
||||
foreach (var e in properties.Values) {
|
||||
etor.Optimize(e);
|
||||
if (!e.IsConstant)
|
||||
IsDynamic = true;
|
||||
}
|
||||
foreach (var e in elements) {
|
||||
e.Key.Optimize(etor);
|
||||
e.Value.Optimize(etor);
|
||||
if (e.Value.IsDynamic)
|
||||
IsDynamic = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
public struct SkinPropertyKey {
|
||||
public Type Component;
|
||||
public string Name;
|
||||
}
|
||||
public class SkinElementBinder : EmptyBinder {
|
||||
public override object ChangeType(object value, Type type, CultureInfo culture) {
|
||||
if (value is string && type == typeof(SkinPropertyKey)) {
|
||||
var cp = ((string)value).Split('.');
|
||||
switch (cp.Length) {
|
||||
case 1:
|
||||
var key = cp[0];
|
||||
if (key[0] == '*')
|
||||
return new SkinPropertyKey { Component = GetComponentByName(key.Substring(1)) };
|
||||
else
|
||||
return new SkinPropertyKey { Component = typeof(TransformInterface), Name = key };
|
||||
case 2:
|
||||
return new SkinPropertyKey { Component = GetComponentByName(cp[0]), Name = cp[1] };
|
||||
}
|
||||
}
|
||||
return base.ChangeType(value, type, culture);
|
||||
}
|
||||
|
||||
static readonly char[] nssep = new char[]{':'};
|
||||
Type GetComponentByName(string name) {
|
||||
var nstuple = name.Split(nssep, 2);
|
||||
var ns = nssep.Length == 2 ? nstuple[0] : "generic";
|
||||
name = nssep.Length == 2 ? nstuple[1] : nstuple[0];
|
||||
if (ns == "generic")
|
||||
return GenericResources.Components[name];
|
||||
throw new ArgumentException(string.Format("Component type {0} not found", name));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user