ketura

stat.tt.v.2

Aug 24th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.15 KB | None | 0 0
  1. <#@ template debug="true" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ assembly name="$(SolutionDir)test\bin\Debug\test.dll" #>
  4. <#@ import namespace="System.Linq" #>
  5. <#@ import namespace="System.Text" #>
  6. <#@ import namespace="System.Collections.Generic" #>
  7. <#@ import namespace="test" #>
  8.  
  9.  
  10. <#@ output extension=".cs" #>
  11.  
  12. <#
  13.  
  14. Dictionary<string, TypeInfo> types = new Dictionary<string, TypeInfo>()
  15. {
  16.     { "Decimal", new TypeInfo() { Type = typeof(decimal), TypeName = "Decimal", Name = "Decimal", Signed = true, Decimal = true, MateName = null, BitCount = 128,
  17.                                 ImplicitCasts = new List<string>(),
  18.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int", "UInt", "Long", "ULong", "Float", "Double", "Decimal" } } },
  19.    
  20.     { "Double", new TypeInfo() { Type = typeof(double), TypeName = "Double", Name = "Double", Signed = true, Decimal = true, MateName = null, BitCount = 64,
  21.                                 ImplicitCasts = new List<string>() {  },
  22.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int", "UInt", "Long", "ULong", "Float", "Double", "Decimal" } } },
  23.    
  24.     { "Float", new TypeInfo() { Type = typeof(Single), TypeName = "Single", Name = "Float", Signed = true, Decimal = true, MateName = null, BitCount = 32,
  25.                                 ImplicitCasts = new List<string>() { "Float", "Double" },
  26.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int", "UInt", "Long", "ULong", "Decimal" } } },
  27.    
  28.     { "Byte", new TypeInfo() { Type = typeof(byte), TypeName = "Byte", Name = "Byte", Signed = false, Decimal = false, MateName = "SByte", BitCount = 8,
  29.                                 ImplicitCasts = new List<string>() { "Byte", "Short", "UShort", "Int", "UInt", "Long", "ULong", "Float", "Double", "Decimal" },
  30.                                 ExplicitCasts = new List<string>() { "SByte" } } },
  31.    
  32.     { "SByte", new TypeInfo() { Type = typeof(sbyte), TypeName = "SByte", Name = "SByte", Signed = true, Decimal = false, MateName = "Byte", BitCount = 8,
  33.                                 ImplicitCasts = new List<string>() { "SByte", "Short", "Int", "Long", "Float", "Double", "Decimal" },
  34.                                 ExplicitCasts = new List<string>() { "Byte", "UShort", "UInt", "ULong" } } },
  35.    
  36.     { "Short", new TypeInfo() { Type = typeof(Int16), TypeName = "Int16", Name = "Short", Signed = true, Decimal = false, MateName = "UShort", BitCount = 16,
  37.                                 ImplicitCasts = new List<string>() { "Short", "Int", "Long", "Float", "Double", "Decimal" },
  38.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "UShort", "UInt", "ULong" } } },
  39.    
  40.     { "UShort", new TypeInfo() { Type = typeof(UInt16), TypeName = "UInt16", Name = "UShort", Signed = false, Decimal = false, MateName = "Short", BitCount = 16,
  41.                                 ImplicitCasts = new List<string>() { "UShort", "Int", "UInt", "Long", "ULong", "Float", "Double", "Decimal" },
  42.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short" } } },
  43.    
  44.     { "Int", new TypeInfo() { Type = typeof(Int32), TypeName = "Int32", Name = "Int", Signed = true, Decimal = false, MateName = "UInt", BitCount = 32,
  45.                                 ImplicitCasts = new List<string>() { "Int", "Long", "Float", "Double", "Decimal" },
  46.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "UInt", "ULong" } } },
  47.    
  48.     { "UInt", new TypeInfo() { Type = typeof(UInt32), TypeName = "UInt32", Name = "UInt", Signed = false, Decimal = false, MateName = "Int", BitCount = 32,
  49.                                 ImplicitCasts = new List<string>() { "UInt", "Long", "ULong", "Float", "Double", "Decimal" },
  50.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int" } } },
  51.    
  52.     { "Long", new TypeInfo() { Type = typeof(Int64), TypeName = "Int64", Name = "Long", Signed = true, Decimal = false, MateName = "ULong", BitCount = 64,
  53.                                 ImplicitCasts = new List<string>() { "Long", "Float", "Double", "Decimal" },
  54.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int", "UInt", "ULong" } } },
  55.    
  56.     { "ULong", new TypeInfo() { Type = typeof(UInt64), TypeName = "UInt64", Name = "ULong", Signed = false, Decimal = false, MateName = "Long", BitCount = 64,
  57.                                 ImplicitCasts = new List<string>() { "ULong", "Float", "Double", "Decimal" },
  58.                                 ExplicitCasts = new List<string>() { "SByte", "Byte", "Short", "UShort", "Int", "UInt", "Long" } } }
  59. };
  60.  
  61.  
  62. #>
  63.  
  64. using System;
  65.  
  66. //using XGEFCore;
  67. using test;
  68.  
  69. namespace Stats
  70. {
  71. <# foreach(var pair in types) { #>
  72. <# string T = pair.Value.Type.Name; #>
  73. <# string Class = pair.Key + "Stat"; #>
  74.     public class <#= Class #> : NumericStat<<#= T #>>
  75.     {
  76.         public override <#= T #> MaxValue { get { return Math.Min(<#= T #>.MaxValue, MaxLimit); } }
  77.         public override <#= T #> MinValue { get { return Math.Max(<#= T #>.MinValue, MinLimit); } }
  78.  
  79.         public override int CompareTo(object obj)
  80.         {
  81.             if (obj == null)
  82.                 return 1;
  83.             //if(obj is <#= T #> t)
  84.             if(obj is <#= T #>)
  85.             {
  86.                 return Value.CompareTo(obj);
  87.             }
  88.             <# foreach(string str in types.Keys) {
  89.                 string name = types[str].Name + "Stat";#>
  90. if(obj is <#= name #>)
  91.             {
  92.                 return Value.CompareTo(((<#= name #>)obj).Value);
  93.             }
  94.  
  95.             <# } #>
  96.  
  97.             return Value.CompareTo(obj);
  98.         }
  99.  
  100.         #region IConvertible
  101.  
  102.         public override TypeCode GetTypeCode()
  103.         {
  104.             return TypeCode.<#= pair.Value.TypeName #>;
  105.         }
  106.  
  107.         public override bool ToBoolean(IFormatProvider provider)
  108.         {
  109.             if(Value == 0)
  110.                 return false;
  111.             return true;
  112.         }
  113.  
  114.         public override char ToChar(IFormatProvider provider)
  115.         {
  116.             return Convert.ToChar(Value);
  117.         }
  118.  
  119.         public override sbyte ToSByte(IFormatProvider provider)
  120.         {
  121.             return Convert.ToSByte(Value);
  122.         }
  123.  
  124.         public override byte ToByte(IFormatProvider provider)
  125.         {
  126.             return Convert.ToByte(Value);
  127.         }
  128.  
  129.         public override short ToInt16(IFormatProvider provider)
  130.         {
  131.             return Convert.ToInt16(Value);
  132.         }
  133.  
  134.         public override ushort ToUInt16(IFormatProvider provider)
  135.         {
  136.             return Convert.ToUInt16(Value);
  137.         }
  138.  
  139.         public override int ToInt32(IFormatProvider provider)
  140.         {
  141.             return Convert.ToInt32(Value);
  142.         }
  143.  
  144.         public override uint ToUInt32(IFormatProvider provider)
  145.         {
  146.             return Convert.ToUInt32(Value);
  147.         }
  148.  
  149.         public override long ToInt64(IFormatProvider provider)
  150.         {
  151.             return Convert.ToInt64(Value);
  152.         }
  153.  
  154.         public override ulong ToUInt64(IFormatProvider provider)
  155.         {
  156.             return Convert.ToUInt64(Value);
  157.         }
  158.  
  159.         public override float ToSingle(IFormatProvider provider)
  160.         {
  161.             return Convert.ToSingle(Value);
  162.         }
  163.  
  164.         public override double ToDouble(IFormatProvider provider)
  165.         {
  166.             return Convert.ToDouble(Value);
  167.         }
  168.  
  169.         public override decimal ToDecimal(IFormatProvider provider)
  170.         {
  171.             return Convert.ToDecimal(Value);
  172.         }
  173.  
  174.         public override DateTime ToDateTime(IFormatProvider provider)
  175.         {
  176.             return Convert.ToDateTime(Value);
  177.         }
  178.  
  179.         public override string ToString(IFormatProvider provider)
  180.         {
  181.             return Convert.ToString(Value);
  182.         }
  183.  
  184.         public override object ToType(Type conversionType, IFormatProvider provider)
  185.         {
  186.             return Convert.ChangeType(Value,conversionType);
  187.         }
  188.         #endregion
  189.  
  190.         #region Type Casting
  191.  
  192.         <# foreach(var typePair in types) {
  193.             string imp = typePair.Value.Type.Name;
  194.             string castingType = "explicit";
  195.  
  196.             if(typePair.Value.Decimal == pair.Value.Decimal)
  197.             {
  198.                 if(typePair.Value.Signed == pair.Value.Signed && pair.Value.BitCount <= typePair.Value.BitCount)
  199.                 {
  200.                     castingType = "implicit";
  201.                 }
  202.             }
  203.             else if(pair.Value.Decimal)
  204.                 castingType = "explicit";
  205.             else
  206.                 castingType = "implicit";#>
  207.  
  208.         public static <#= castingType #> operator <#= imp #>(<#= Class #> stat)
  209.         {
  210.             return (<#= imp #>)stat.Value;
  211.         }
  212.  
  213.         <# string castingType2 = "explicit";
  214.         if(typePair.Value.Decimal == pair.Value.Decimal)
  215.         {
  216.             if(typePair.Value.Signed == pair.Value.Signed && pair.Value.BitCount >= typePair.Value.BitCount)
  217.             {
  218.                 castingType2 = "implicit";
  219.             }
  220.         }
  221.         else if(pair.Value.Decimal)
  222.             castingType2 = "implicit";
  223.         else
  224.             castingType2 = "explicit";#>
  225. public static <#= castingType2 #> operator <#= Class #>(<#= imp #> num)
  226.         {
  227.             <#= Class #> stat = new <#= Class #>();
  228.             stat.Set((<#= T #>)num);
  229.             return stat;
  230.         }
  231.         <# } #>
  232.         #endregion
  233.  
  234.         public <#= Class #> Copy()
  235.         {
  236.             return new <#= Class #>(Name, Value, MinLimit, MaxLimit, Description, Notes);
  237.         }
  238.  
  239.         public <#= Class #>() : this("UnnamedStat", 0, 0, 0, null, null) { }
  240.         public <#= Class #>(string name) : this(name, 0, 0, 0, null, null) { }
  241.         public <#= Class #>(string name, <#= T #> value) : this(name, value, 0, 0, null, null) { }
  242.         public <#= Class #>(string name, <#= T #> value, <#= T #> min, <#= T #> max) : this(name, value, min, max, null, null) { }
  243.         public <#= Class #>(string name, <#= T #> value, <#= T #> min, <#= T #> max, string desc, string notes) : base(name, value, min, max, desc, notes) { }
  244.  
  245.     }
  246.  
  247. <# } #>
  248. }
  249.  
  250. <#+
  251. public struct TypeInfo
  252. {
  253.     public Type Type { get; set; }
  254.     public string Name { get; set; }
  255.     public string TypeName { get; set; }
  256.     public bool Signed { get; set; }
  257.     public bool Decimal { get; set; }
  258.     public string MateName { get; set; }
  259.     public int BitCount { get; set; }
  260.     public List<string> ImplicitCasts { get; set; }
  261.     public List<string> ExplicitCasts { get; set; }
  262. }
  263. #>
Advertisement
Add Comment
Please, Sign In to add comment