Esempio n. 1
0
 public static Type ConvertBaseToConcreteType(byte[] buffer, Type type, ref int startIndex)
 {
     if (TypeMapping.ContainsKey(type))
     {
         return(TypeIDMapping[type][buffer[startIndex++]]);
     }
     return(type);
 }
        /// <summary>
        /// Searches all whitelisted assemblies to find a type with the given full name
        /// </summary>
        /// <param name="name">The full name to check for</param>
        /// <param name="BaseType">An optional base type requirement</param>
        /// <param name="includeDerived">Whether or not to include types that inherit from the specified name type</param>
        /// <param name="targetNamespace">An optional restriction on the namespace of the search</param>
        /// <returns>A type matching the full name, or derived type</returns>
        public static Type GetTypeByFullName(string name, Type BaseType = null, bool includeDerived = false, string targetNamespace = "")
        {
            bool nameIsCached      = TypeMapping.ContainsKey(name);
            bool namespaceIsCached = nameIsCached && TypeMapping[name].ContainsKey(targetNamespace);

            if (!nameIsCached || !namespaceIsCached)
            {
                List <Type> matching = GetTypeByFullName(name).ToList();

                if (includeDerived)
                {
                    List <Type> derivedTypes = new List <Type>();

                    derivedTypes.AddRange(matching.Select(m => GetDerivedTypes(m)).SelectMany(m => m));

                    matching.AddRange(derivedTypes);
                }

                matching = matching.Where(t => string.IsNullOrEmpty(targetNamespace) || targetNamespace == t.Namespace).Distinct().ToList();

                Type targetType = null;

                foreach (Type t in matching)
                {
                    if (BaseType != null && !t.IsSubclassOf(BaseType))
                    {
                        continue;
                    }

                    if (targetType == null || targetType.IsSubclassOf(t))
                    {
                        targetType = t;
                    }
                    else if (targetType != null && !targetType.IsSubclassOf(t) && !t.IsSubclassOf(targetType))
                    {
                        throw new Exception("Found multiple noninherited types that match name " + name);
                    }
                }

                if (nameIsCached)
                {
                    TypeMapping[name].TryAdd(targetNamespace, targetType);
                }
                else
                {
                    ConcurrentDictionary <string, Type> namespaceDictionary = new ConcurrentDictionary <string, Type>();
                    namespaceDictionary.TryAdd(targetNamespace, targetType);

                    TypeMapping.TryAdd(name, namespaceDictionary);
                }
            }

            return(TypeMapping[name][targetNamespace]);
        }
Esempio n. 3
0
        static void GenerateDeserializerCallClassMethod(TypeBuilder typeBuilder, ILGenerator il, Type type, int valueLocalIndex, int startIndexLocalIndex, bool useStream = false)
        {
            MethodBuilder method;
            var           hasTypeMapping = TypeMapping.ContainsKey(type);

            if (hasTypeMapping)
            {
                var index           = 0;
                var typeMapping     = TypeMapping[type];
                var count           = typeMapping.Count;
                var types           = typeMapping.Select(kv => kv.Key);
                var needBranchLabel = count > 1;
                var branchLabel     = needBranchLabel ? il.DefineLabel() : DefaultLabel;
                var valueTypeLocal  = il.DeclareLocal(TypeType);

                if (useStream)
                {
                    il.Emit(OpCodes.Ldarg_1);
                    il.Emit(OpCodes.Call, ReadFullyMethod);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_1);
                }


                il.Emit(OpCodes.Ldtoken, type);
                il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                il.Emit(OpCodes.Ldloca_S, startIndexLocalIndex);
                il.Emit(OpCodes.Call, ConvertBaseToConcreteTypeMethod);

                il.Emit(OpCodes.Stloc, valueTypeLocal.LocalIndex);

                foreach (var mapType in types)
                {
                    index++;
                    var isLastIndex     = index == count;
                    var isLastCondition = isLastIndex && needBranchLabel;
                    var conditionLabel  = !isLastCondition?il.DefineLabel() : DefaultLabel;

                    var currentConditionLabel = isLastCondition ? branchLabel : conditionLabel;
                    il.Emit(OpCodes.Ldloc, valueTypeLocal.LocalIndex);
                    il.Emit(OpCodes.Ldtoken, mapType);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                    il.Emit(OpCodes.Call, GetTypeOpEqualityMethod);
                    il.Emit(OpCodes.Brfalse, currentConditionLabel);

                    method = GenerateDeserializerClass(typeBuilder, mapType);
                    il.Emit(OpCodes.Newobj, mapType.GetConstructor(Type.EmptyTypes));
                    il.Emit(OpCodes.Stloc, valueLocalIndex);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldloc, valueLocalIndex);
                    if (mapType.IsClass)
                    {
                        il.Emit(OpCodes.Castclass, mapType);
                    }
                    else
                    {
                        il.Emit(OpCodes.Unbox_Any, mapType);
                    }
                    if (useStream)
                    {
                        il.Emit(OpCodes.Ldarg_1);
                        il.Emit(OpCodes.Call, ReadFullyMethod);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldarg_1);
                    }
                    il.Emit(OpCodes.Ldloca_S, startIndexLocalIndex);
                    il.Emit(OpCodes.Callvirt, method);

                    if (!isLastIndex)
                    {
                        il.Emit(OpCodes.Br, branchLabel);
                    }
                    il.MarkLabel(currentConditionLabel);
                }
            }
            else
            {
                method = GenerateDeserializerClass(typeBuilder, type);
                var isTypeClass = !type.IsValueType;
                if (isTypeClass)
                {
                    il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
                    il.Emit(OpCodes.Stloc, valueLocalIndex);
                }
                else
                {
                    il.Emit(OpCodes.Ldloca, valueLocalIndex);
                    il.Emit(OpCodes.Initobj, type);
                }
                il.Emit(OpCodes.Ldarg_0);
                if (isTypeClass)
                {
                    il.Emit(OpCodes.Ldloc, valueLocalIndex);
                }
                else
                {
                    il.Emit(OpCodes.Ldloca, valueLocalIndex);
                }
                if (useStream)
                {
                    il.Emit(OpCodes.Ldarg_1);
                    il.Emit(OpCodes.Call, ReadFullyMethod);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_1);
                }
                il.Emit(OpCodes.Ldloca_S, startIndexLocalIndex);
                il.Emit(OpCodes.Callvirt, method);
            }
        }
Esempio n. 4
0
        static void GenerateSerializerCallClassMethod(TypeBuilder typeBuilder, ILGenerator il, Type type, int bufferLocalIndex)
        {
            MethodBuilder method;

            if (TypeMapping.ContainsKey(type))
            {
                var isTypeClass     = !type.IsValueType;
                var index           = 0;
                var typeMapping     = TypeMapping[type];
                var count           = typeMapping.Count;
                var types           = typeMapping.Select(kv => kv.Key);
                var needBranchLabel = count > 1;
                var branchLabel     = needBranchLabel ? il.DefineLabel() : DefaultLabel;
                var valueTypeLocal  = il.DeclareLocal(TypeType);
                if (isTypeClass)
                {
                    il.Emit(OpCodes.Ldarg_1);
                }
                else
                {
                    il.Emit(OpCodes.Ldarga, 1);
                }
                il.Emit(OpCodes.Callvirt, GetTypeMethod);
                il.Emit(OpCodes.Stloc, valueTypeLocal.LocalIndex);

                foreach (var mapType in types)
                {
                    index++;
                    var isLastIndex     = index == count;
                    var isLastCondition = isLastIndex && needBranchLabel;
                    var conditionLabel  = !isLastCondition?il.DefineLabel() : DefaultLabel;

                    var currentConditionLabel = isLastCondition ? branchLabel : conditionLabel;
                    il.Emit(OpCodes.Ldloc, valueTypeLocal.LocalIndex);
                    il.Emit(OpCodes.Ldtoken, mapType);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                    il.Emit(OpCodes.Call, GetTypeOpEqualityMethod);
                    il.Emit(OpCodes.Brfalse, currentConditionLabel);

                    method = GenerateSerializerClass(typeBuilder, mapType, isEntryPoint: true, baseType: type);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldloc, bufferLocalIndex);
                    il.Emit(OpCodes.Ldarg_1);
                    if (mapType.IsClass)
                    {
                        il.Emit(OpCodes.Castclass, mapType);
                    }
                    else
                    {
                        il.Emit(OpCodes.Unbox_Any, mapType);
                    }
                    il.Emit(OpCodes.Ldc_I4_1);
                    il.Emit(OpCodes.Ldc_I4_0);
                    il.Emit(OpCodes.Callvirt, method);

                    if (!isLastIndex)
                    {
                        il.Emit(OpCodes.Br, branchLabel);
                    }
                    il.MarkLabel(currentConditionLabel);
                }
                return;
            }
            method = GenerateSerializerClass(typeBuilder, type, isEntryPoint: true);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldloc, bufferLocalIndex);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Callvirt, method);
        }
Esempio n. 5
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("/*\r\n *\r\n * This code was generated from the Command Creator.\r\n *\r\n */\r\n\r\n#pragma " +
                       "once\r\n#include \"stdafx.h\"\r\n#include \"");

            #line 14 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(".h\"\r\n\r\n\r\nnamespace Commands\r\n{\r\n\t");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("::");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("(__int64 timestamp, ");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
            #line default
            #line hidden

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(TypeMapping[cpItem.TypeName].ToString()));

            #line default
            #line hidden

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
            #line default
            #line hidden

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.TypeName.ToString()));

            #line default
            #line hidden

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }

            #line default
            #line hidden
                this.Write(" ");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write(")\r\n\t\t: m_timestamp(timestamp), ");

            #line 20 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write(" m_");

            #line 20 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write("(");

            #line 20 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(") ");

            #line 20 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 20 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write("\t{\r\n\t}\r\n\r\n\t");

            #line 24 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("::~");

            #line 24 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("()\r\n\t{\r\n\t}\r\n\r\n\tstd::wstring ");

            #line 28 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("::Name()\r\n\t{\r\n\t\treturn L\"");

            #line 30 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));

            #line default
            #line hidden
            this.Write("\";\r\n\t}\r\n\r\n\tstd::wstring ");

            #line 33 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("::Description()\r\n\t{\r\n\t\treturn L\"");

            #line 35 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Description));

            #line default
            #line hidden
            this.Write("\";\r\n\t}\r\n\r\n\tstd::shared_ptr<std::vector<char>> ");

            #line 38 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(@"::Encode()
	{
		if (!hasEncoded)
		{
			// The following items will be resued 
			auto hash = std::hash<std::wstring>();
			size_t hashout = 0;		// Temp var for string hashes
			size_t strlen = 0;		// Temp var for string length
			size_t strbytes = 0;	// Temp var for string bytes

			size_t size = sizeof(__int32)	// len
				+ sizeof(short)				// code
				+ sizeof(__int64);			// timestamp
");

            #line 51 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                string typeName = "";
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
                    typeName = TypeMapping[cpItem.TypeName].ToString();
                }
                else
                {
                    typeName = cpItem.TypeName.ToString();
                }
                if (typeName.ToLower() == "std::wstring")
                {
            #line default
            #line hidden
                    this.Write(" \r\n\t\t\tsize_t strlen_");

            #line 57 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = (m_");

            #line 57 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(".length());\r\n\t\t\tsize_t strbytes_");

            #line 58 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = strlen_");

            #line 58 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" * sizeof(wchar_t) + sizeof(wchar_t);\r\n\t\t\tsize += sizeof(__int32);\r\n\t\t\tsize += si" +
                               "zeof(size_t);\r\n\t\t\tsize += strbytes_");

            #line 61 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(";\r\n\r\n");

            #line 63 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
            #line default
            #line hidden
                    this.Write("\t\t\tsize += sizeof(");

            #line 64 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write("); // Size of ");

            #line 64 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write("\r\n");

            #line 65 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }

            #line default
            #line hidden

            #line 66 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            }

            #line default
            #line hidden
            this.Write(@"			size += sizeof(short);	//Terminator
			

			auto vector = new char[size];
			short term = 0;

			// 
			auto v2 = (char*)memcpy(vector, &size, sizeof(__int32));
			v2 += sizeof(__int32);
			memcpy(v2, &code, sizeof(short));
			v2 += sizeof(short);
			memcpy(v2, &m_timestamp, sizeof(__int64));
			v2 += sizeof(__int64);

");

            #line 81 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                string typeName = "";
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
                    typeName = TypeMapping[cpItem.TypeName].ToString();
                }
                else
                {
                    typeName = cpItem.TypeName.ToString();
                }
                if (typeName.ToLower() == "std::wstring")
                {
            #line default
            #line hidden
                    this.Write(" \r\n\t\t\t// Regenerate the string information\r\n\t\t\thashout = hash(m_");

            #line 88 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(");\r\n\r\n\t\t\tmemcpy(v2, &strlen_");

            #line 90 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(", sizeof(__int32));\r\n\t\t\tv2 += sizeof(__int32);\r\n\t\t\tmemcpy(v2, &hashout, sizeof(si" +
                               "ze_t));\r\n\t\t\tv2 += sizeof(__int64);\r\n\t\t\tmemcpy(v2, m_");

            #line 94 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(".data(), strbytes_");

            #line 94 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(");\r\n\t\t\tv2 += strbytes_");

            #line 95 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(";\r\n\r\n");

            #line 97 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
            #line default
            #line hidden
                    this.Write("\t\t\tmemcpy(v2, &m_");

            #line 98 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(", sizeof(");

            #line 98 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write("));\r\n\t\t\tv2 += sizeof(");

            #line 99 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(");\r\n\r\n");

            #line 101 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }

            #line default
            #line hidden

            #line 102 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            }

            #line default
            #line hidden
            this.Write(@"			// Terminator
			memcpy(v2, &term, sizeof(short));
			v2 += sizeof(short);

			hasEncoded = true;
			std::vector<char> v(size);
			v.assign(vector, v2);

			m_internalvector = std::make_shared<std::vector<char>>(v);
		}
		return m_internalvector;
	}

	std::shared_ptr<ICommand> "    );

            #line 116 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("::Decode(std::shared_ptr<std::vector<char>> &data)\r\n\t{\r\n\t\treturn std::make_shared" +
                       "<");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(">(");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("(m_timestamp, ");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write(" m_");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(" ");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 118 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCpp.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write("));\r\n\t}\r\n}\r\n\r\n");
            return(this.GenerationEnvironment.ToString());
        }
Esempio n. 6
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("\r\n\r\n#pragma once\r\n#include \"ICommand.h\"\r\n\r\nnamespace Commands\r\n{\r\n    class ");

            #line 13 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(" :\r\n        public virtual ICommand\r\n    {\r\n    public:\r\n        ");

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("(__int64 timestamp, ");

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
            #line default
            #line hidden

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(TypeMapping[cpItem.TypeName].ToString()));

            #line default
            #line hidden

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                }
                else
                {
            #line default
            #line hidden

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.TypeName.ToString()));

            #line default
            #line hidden

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                }

            #line default
            #line hidden
                this.Write(" ");

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name.ToLower()));

            #line default
            #line hidden

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 17 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write(");\r\n        ~");

            #line 18 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(@"();
        virtual std::shared_ptr<std::vector<char>> Encode();
        virtual std::shared_ptr<ICommand> Decode(std::shared_ptr<std::vector<char>> &data);
        virtual std::wstring Name();
        virtual std::wstring Description();
        virtual short Code() { return code; }

    private:

");

            #line 27 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write("        ");

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
            #line default
            #line hidden

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(TypeMapping[cpItem.TypeName].ToString()));

            #line default
            #line hidden

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                }
                else
                {
            #line default
            #line hidden

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.TypeName.ToString()));

            #line default
            #line hidden

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                }

            #line default
            #line hidden
                this.Write(" m_");

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(";\r\n");

            #line 30 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorH.tt"
            }

            #line default
            #line hidden
            this.Write("        __int64 m_timestamp; // Always use a 64bit so the message doesn\'t change\r" +
                       "\n        std::shared_ptr<std::vector<char>> m_internalvector;\r\n        bool hasE" +
                       "ncoded;\r\n        short code;\r\n    };\r\n\r\n}\r\n");
            return(this.GenerationEnvironment.ToString());
        }
Esempio n. 7
0
        static void WriteSerializerCallClassMethod(TypeBuilder typeBuilder, ILGenerator il, Type type, MethodInfo valueMethod, int tag,
                                                   bool needClassHeader, Type callerType, Type ownerType = null)
        {
            MethodBuilder method;
            var           isTypeClass = callerType.IsClass;

            if (TypeMapping.ContainsKey(type))
            {
                var index              = 0;
                var typeMapping        = TypeMapping[type];
                var count              = typeMapping.Count;
                var types              = typeMapping.Select(kv => kv.Key);
                var needBranchLabel    = count > 1;
                var branchLabel        = needBranchLabel ? il.DefineLabel() : DefaultLabel;
                var valueLocal         = il.DeclareLocal(type);
                var valueTypeLocal     = il.DeclareLocal(TypeType);
                var nullConditionLabel = il.DefineLabel();

                if (isTypeClass)
                {
                    il.Emit(OpCodes.Ldarg_2);
                }
                else
                {
                    il.Emit(OpCodes.Ldarga, 2);
                }
                il.Emit(OpCodes.Call, valueMethod);
                il.Emit(OpCodes.Stloc, valueLocal.LocalIndex);


                il.Emit(OpCodes.Ldloc, valueLocal.LocalIndex);
                il.Emit(OpCodes.Brfalse, nullConditionLabel);


                il.Emit(OpCodes.Ldloc, valueLocal.LocalIndex);
                il.Emit(OpCodes.Callvirt, GetTypeMethod);
                il.Emit(OpCodes.Stloc, valueTypeLocal.LocalIndex);

                foreach (var mapType in types)
                {
                    index++;
                    var isLastIndex     = index == count;
                    var isLastCondition = isLastIndex && needBranchLabel;
                    var conditionLabel  = !isLastCondition?il.DefineLabel() : DefaultLabel;

                    var currentConditionLabel = isLastCondition ? branchLabel : conditionLabel;
                    il.Emit(OpCodes.Ldloc, valueTypeLocal.LocalIndex);
                    il.Emit(OpCodes.Ldtoken, mapType);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                    il.Emit(OpCodes.Call, GetTypeOpEqualityMethod);
                    il.Emit(OpCodes.Brfalse, currentConditionLabel);

                    method = GenerateSerializerClass(typeBuilder, mapType, baseType: type, ownerType: ownerType);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_1);
                    il.Emit(OpCodes.Ldloc, valueLocal.LocalIndex);
                    if (mapType.IsClass)
                    {
                        il.Emit(OpCodes.Castclass, mapType);
                    }
                    else
                    {
                        il.Emit(OpCodes.Unbox_Any, mapType);
                    }
                    il.Emit(OpCodes.Ldc_I4, tag);
                    il.Emit(OpCodes.Ldc_I4, needClassHeader ? 1 : 0);
                    il.Emit(OpCodes.Call, method);

                    if (!isLastIndex)
                    {
                        il.Emit(OpCodes.Br, branchLabel);
                    }
                    il.MarkLabel(currentConditionLabel);
                }

                il.MarkLabel(nullConditionLabel);
                return;
            }
            method = GenerateSerializerClass(typeBuilder, type, ownerType: ownerType);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            if (isTypeClass)
            {
                il.Emit(OpCodes.Ldarg_2);
            }
            else
            {
                il.Emit(OpCodes.Ldarga, 2);
            }
            il.Emit(OpCodes.Call, valueMethod);//Virt
            il.Emit(OpCodes.Ldc_I4, tag);
            il.Emit(OpCodes.Ldc_I4, needClassHeader ? 1 : 0);
            il.Emit(OpCodes.Call, method);
        }
        static void WriteDeserializerCallClassMethod(TypeBuilder typeBuilder, ILGenerator il, Type type, int tag, MethodInfo setMethod, Type ownerType = null)
        {
            MethodBuilder method;
            var           local          = il.DeclareLocal(type);
            var           hasTypeMapping = TypeMapping.ContainsKey(type);

            il.Emit(OpCodes.Ldc_I4, tag);
            il.Emit(OpCodes.Ldarg_3);
            il.Emit(OpCodes.Call, MoveToNextBytesMethod);

            if (hasTypeMapping)
            {
                var index           = 0;
                var typeMapping     = TypeMapping[type];
                var count           = typeMapping.Count;
                var types           = typeMapping.Select(kv => kv.Key);
                var needBranchLabel = count > 1;
                var branchLabel     = needBranchLabel ? il.DefineLabel() : DefaultLabel;
                var valueTypeLocal  = il.DeclareLocal(TypeType);

                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldtoken, type);
                il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                il.Emit(OpCodes.Ldarg_3);
                il.Emit(OpCodes.Call, ConvertBaseToConcreteTypeMethod);

                il.Emit(OpCodes.Stloc, valueTypeLocal.LocalIndex);

                foreach (var mapType in types)
                {
                    index++;
                    var isLastIndex     = index == count;
                    var isLastCondition = isLastIndex && needBranchLabel;
                    var conditionLabel  = !isLastCondition?il.DefineLabel() : DefaultLabel;

                    var currentConditionLabel = isLastCondition ? branchLabel : conditionLabel;
                    il.Emit(OpCodes.Ldloc, valueTypeLocal.LocalIndex);
                    il.Emit(OpCodes.Ldtoken, mapType);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                    il.Emit(OpCodes.Call, GetTypeOpEqualityMethod);
                    il.Emit(OpCodes.Brfalse, currentConditionLabel);

                    method = GenerateDeserializerClass(typeBuilder, mapType, ownerType: ownerType);
                    il.Emit(OpCodes.Newobj, mapType.GetConstructor(Type.EmptyTypes));
                    il.Emit(OpCodes.Stloc, local.LocalIndex);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldloc, local.LocalIndex);
                    if (mapType.IsClass)
                    {
                        il.Emit(OpCodes.Castclass, mapType);
                    }
                    else
                    {
                        il.Emit(OpCodes.Unbox_Any, mapType);
                    }
                    il.Emit(OpCodes.Ldarg_2);
                    il.Emit(OpCodes.Ldarg_3);
                    il.Emit(OpCodes.Call, method);

                    if (!isLastIndex)
                    {
                        il.Emit(OpCodes.Br, branchLabel);
                    }
                    il.MarkLabel(currentConditionLabel);
                }
            }
            else
            {
                method = GenerateDeserializerClass(typeBuilder, type, ownerType: ownerType);
                var isTypeClass = type.IsClass;
                if (isTypeClass)
                {
                    il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
                    il.Emit(OpCodes.Stloc, local.LocalIndex);
                }
                else
                {
                    il.Emit(OpCodes.Ldloca, local.LocalIndex);
                    il.Emit(OpCodes.Initobj, type);
                }
                il.Emit(OpCodes.Ldarg_0);
                if (isTypeClass)
                {
                    il.Emit(OpCodes.Ldloc, local.LocalIndex);
                }
                else
                {
                    il.Emit(OpCodes.Ldloca, local.LocalIndex);
                }
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_3);
                il.Emit(OpCodes.Call, method);
            }

            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Ldloc, local.LocalIndex);
            il.Emit(OpCodes.Call, setMethod);
        }
        static void WriteDeserializerReadValue(TypeBuilder typeBuilder, ILGenerator il, Type type, int tag, int itemLocalIndex)
        {
            var isCollection = type.IsCollectionType();
            var isClass      = !isCollection && type.IsComplexType();

            if (isClass)
            {
                MethodBuilder method;
                var           hasTypeMapping = TypeMapping.ContainsKey(type);

                if (hasTypeMapping)
                {
                    var index           = 0;
                    var typeMapping     = TypeMapping[type];
                    var count           = typeMapping.Count;
                    var types           = typeMapping.Select(kv => kv.Key);
                    var needBranchLabel = count > 1;
                    var branchLabel     = needBranchLabel ? il.DefineLabel() : DefaultLabel;
                    var valueTypeLocal  = il.DeclareLocal(TypeType);

                    il.Emit(OpCodes.Ldarg_2);
                    il.Emit(OpCodes.Ldtoken, type);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                    il.Emit(OpCodes.Ldarg_3);
                    il.Emit(OpCodes.Call, ConvertBaseToConcreteTypeMethod);

                    il.Emit(OpCodes.Stloc, valueTypeLocal.LocalIndex);

                    foreach (var mapType in types)
                    {
                        index++;
                        var isLastIndex     = index == count;
                        var isLastCondition = isLastIndex && needBranchLabel;
                        var conditionLabel  = !isLastCondition?il.DefineLabel() : DefaultLabel;

                        var currentConditionLabel = isLastCondition ? branchLabel : conditionLabel;
                        il.Emit(OpCodes.Ldloc, valueTypeLocal.LocalIndex);
                        il.Emit(OpCodes.Ldtoken, mapType);
                        il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                        il.Emit(OpCodes.Call, GetTypeOpEqualityMethod);
                        il.Emit(OpCodes.Brfalse, currentConditionLabel);

                        method = GenerateDeserializerClass(typeBuilder, mapType);
                        il.Emit(OpCodes.Newobj, mapType.GetConstructor(Type.EmptyTypes));
                        il.Emit(OpCodes.Stloc, itemLocalIndex);
                        il.Emit(OpCodes.Ldarg_0);
                        il.Emit(OpCodes.Ldloc, itemLocalIndex);
                        if (mapType.IsClass)
                        {
                            il.Emit(OpCodes.Castclass, mapType);
                        }
                        else
                        {
                            il.Emit(OpCodes.Unbox_Any, mapType);
                        }
                        il.Emit(OpCodes.Ldarg_2);
                        il.Emit(OpCodes.Ldarg_3);
                        il.Emit(OpCodes.Call, method);

                        if (!isLastIndex)
                        {
                            il.Emit(OpCodes.Br, branchLabel);
                        }
                        il.MarkLabel(currentConditionLabel);
                    }
                }
                else
                {
                    method = GenerateDeserializerClass(typeBuilder, type);
                    var isTypeClass = type.IsClass;
                    if (isTypeClass)
                    {
                        il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
                        il.Emit(OpCodes.Stloc, itemLocalIndex);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldloca, itemLocalIndex);
                        il.Emit(OpCodes.Initobj, type);
                    }
                    il.Emit(OpCodes.Ldarg_0);
                    if (isTypeClass)
                    {
                        il.Emit(OpCodes.Ldloc, itemLocalIndex);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldloca, itemLocalIndex);
                    }
                    il.Emit(OpCodes.Ldarg_2);
                    il.Emit(OpCodes.Ldarg_3);
                    il.Emit(OpCodes.Call, method);
                }
            }
            else if (isCollection)
            {
                WriteDeserializerClass(typeBuilder, il, type, tag, null, itemLocalIndex: itemLocalIndex);
            }
            else
            {
                var nonNullableType   = type.GetNonNullableType();
                var isTypeEnum        = type.IsEnum;
                var needTypeForReader = PrimitiveReadersWithTypes.Contains(isTypeEnum ? EnumType : nonNullableType);
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_3);
                il.Emit(OpCodes.Ldc_I4, 1);
                il.Emit(OpCodes.Ldc_I4, type.IsBufferedTypeInt());
                il.Emit(OpCodes.Call, ReadNextBytesMethod);
                if (needTypeForReader)
                {
                    il.Emit(OpCodes.Ldtoken, type);
                    il.Emit(OpCodes.Call, GetTypeFromHandleMethod);
                }
                if (isTypeEnum)
                {
                    il.Emit(OpCodes.Call, PrimitiveReaderMethods[EnumType]);
                }
                else
                {
                    il.Emit(OpCodes.Call, PrimitiveReaderMethods[nonNullableType]);
                    if (type.IsNullable())
                    {
                        il.Emit(OpCodes.Newobj, type.GetNullableTypeCtor());
                    }
                }
                if (needTypeForReader)
                {
                    il.Emit(OpCodes.Unbox_Any, type);
                }
                il.Emit(OpCodes.Stloc, itemLocalIndex);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("/*\r\n *\r\n * This code was generated from the Command Creator.\r\n *\r\n *\r\n */\r\n\r\nusin" +
                       "g System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.T" +
                       "ext;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ChainsAPM.Commands.");

            #line 19 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Namespace));

            #line default
            #line hidden
            this.Write("\r\n{\r\n    public class ");

            #line 21 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write(" : Interfaces.ICommand<byte>\r\n    {\r\n\r\n         public DateTime TimeStamp { get; " +
                       "set; }\r\n");

            #line 25 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var enumn in this.EnumerationList)
            {
            #line default
            #line hidden
                this.Write("        public enum ");

            #line 27 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(enumn.EnumerationName));

            #line default
            #line hidden
                this.Write("\r\n        {\r\n");

            #line 29 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                foreach (var enumItem in enumn.Items)
                {
            #line default
            #line hidden

            #line 31 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    if (enumn.Items.Last().Name != enumItem.Name)
                    {
            #line default
            #line hidden
                        this.Write("            ");

            #line 33 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(enumItem.Name));

            #line default
            #line hidden
                        this.Write(" = ");

            #line 33 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(enumItem.Value));

            #line default
            #line hidden
                        this.Write(",\r\n");

            #line 34 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    }
                    else
                    {
            #line default
            #line hidden
                        this.Write("            ");

            #line 36 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(enumItem.Name));

            #line default
            #line hidden
                        this.Write(" = ");

            #line 36 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(enumItem.Value));

            #line default
            #line hidden
                        this.Write("\r\n");

            #line 37 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    }

            #line default
            #line hidden

            #line 38 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }

            #line default
            #line hidden
                this.Write("        }\r\n");

            #line 40 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write("\r\n");

            #line 42 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write("        public ");

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
            #line default
            #line hidden

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(TypeMapping[cpItem.TypeName].ToString()));

            #line default
            #line hidden

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
            #line default
            #line hidden

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.TypeName.ToString()));

            #line default
            #line hidden

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }

            #line default
            #line hidden
                this.Write(" ");

            #line 44 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(" { get; set; }\r\n");

            #line 45 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write("\r\n        public ");

            #line 47 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("()\r\n        {\r\n\r\n        }\r\n\r\n        public ");

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("(System.Int64 timeStamp, ");

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
            #line default
            #line hidden

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(TypeMapping[cpItem.TypeName].ToString()));

            #line default
            #line hidden

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
            #line default
            #line hidden

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.TypeName.ToString()));

            #line default
            #line hidden

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }

            #line default
            #line hidden
                this.Write("  ");

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name.ToLower()));

            #line default
            #line hidden

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 52 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write(")\r\n        {\r\n            TimeStamp = DateTime.FromFileTimeUtc(timestamp);\r\n");

            #line 55 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write("            ");

            #line 57 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(" = ");

            #line 57 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name.ToLower()));

            #line default
            #line hidden
                this.Write(";\r\n");

            #line 58 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write("        }\r\n\r\n        public string Name\r\n        {\r\n            get { return \"");

            #line 63 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Name));

            #line default
            #line hidden
            this.Write("\"; }\r\n        }\r\n        public ushort Code\r\n        {\r\n            get { return " +
                       "0x");

            #line 67 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Code.ToString("x4")));

            #line default
            #line hidden
            this.Write("; }\r\n        }\r\n        public string Description\r\n        {\r\n            get { r" +
                       "eturn \"");

            #line 71 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.Description));

            #line default
            #line hidden
            this.Write("\"; }\r\n        }\r\n        public Type CommandType\r\n        {\r\n            get { re" +
                       "turn typeof(");

            #line 75 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.CommandType.ToString()));

            #line default
            #line hidden
            this.Write(@"); }
        }
        public Interfaces.ICommand<byte> Decode(ArraySegment<byte> input)
        {

            if (input.Count != 0)
            {
                Helpers.ArraySegmentStream segstream = new Helpers.ArraySegmentStream(input);
                int size = segstream.GetInt32();
                if (input.Count == size)
                {
                    short code = segstream.GetInt16();
                    if (code == Code)
                    {
                    var timestamp = segstream.GetInt64();
");

            #line 90 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                string typeName = "";
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
                    typeName = TypeMapping[cpItem.TypeName].ToString();
                }
                else
                {
                    typeName = cpItem.TypeName.ToString();
                }
                if (typeName == typeof(string).FullName)
                {
            #line default
            #line hidden
                    this.Write("                        var stringlen");

            #line 94 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetInt32();\r\n                        var decode");

            #line 95 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write("Hash = segstream.GetInt32();\r\n                        var decode");

            #line 96 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetUnicode(stringlen");

            #line 96 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(");\r\n\r\n");

            #line 98 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int32).FullName)
                {
            #line default
            #line hidden
                    this.Write("                        var decode");

            #line 99 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetInt32();\r\n\r\n");

            #line 101 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int16).FullName)
                {
            #line default
            #line hidden
                    this.Write("                        var decode");

            #line 102 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetInt16();\r\n\r\n");

            #line 104 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int64).FullName)
                {
            #line default
            #line hidden
                    this.Write("                        var decode");

            #line 105 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetInt64();\r\n\r\n");

            #line 107 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(byte).FullName)
                {
            #line default
            #line hidden
                    this.Write("                        var decode");

            #line 108 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = segstream.GetByte();\r\n\r\n");

            #line 110 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
            #line default
            #line hidden
                    this.Write("                        var decode");

            #line 111 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(" = (");

            #line 111 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(")segstream.GetInt32();\r\n\r\n");

            #line 113 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }

            #line default
            #line hidden

            #line 114 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write(@"                       
                        var term = segstream.GetInt16();

                        if (term != 0)
                        {
                            throw new System.Runtime.Serialization.SerializationException(""Terminator is a non zero value. Please check the incoming byte stream for possible errors."");
                        }
                        return new ");

            #line 121 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(this.ClassName));

            #line default
            #line hidden
            this.Write("(timestamp, ");

            #line 121 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write("decode");

            #line 121 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden

            #line 121 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                if (this.ClassProperties.Last().Name != cpItem.Name)
                {
            #line default
            #line hidden
                    this.Write(", ");

            #line 121 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
                }
            }

            #line default
            #line hidden
            this.Write(@");
                    }
                    else
                    {
                        throw new System.Runtime.Serialization.SerializationException(""Invalid command code detected. Please check the incoming byte stream for possible errors."");
                    }
                }
                else
                {
                    throw new System.Runtime.Serialization.SerializationException(""Size of message does not match size of byte stream. Please check the incoming byte stream for possible errors."");
                }
            }
            else
            {
                throw new System.Runtime.Serialization.SerializationException(""Size of message is zero. Please check the incoming byte stream for possible errors. "");
            }
        }
        public byte[] Encode()
        {
            int byteSize 4;
");

            #line 141 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
                string typeName = "";
                if (TypeMapping.ContainsKey(cpItem.TypeName))
                {
                    typeName = TypeMapping[cpItem.TypeName].ToString();
                }
                else
                {
                    typeName = cpItem.TypeName.ToString();
                }
                if (typeName == typeof(string).FullName)
                {
            #line default
            #line hidden
                    this.Write("            byteSize += 4; // Length Bytes\r\n            byteSize += 4; // Hash By" +
                               "tes\r\n            byteSize += ");

            #line 147 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                    this.Write(".Length; // Hash Bytes\r\n");

            #line 148 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int32).FullName)
                {
            #line default
            #line hidden
                    this.Write("            byteSize += sizeof(");

            #line 149 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(");\r\n");

            #line 150 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int16).FullName)
                {
            #line default
            #line hidden
                    this.Write("            byteSize += sizeof(");

            #line 151 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(");\r\n");

            #line 152 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(Int64).FullName)
                {
            #line default
            #line hidden
                    this.Write("            byteSize += sizeof(");

            #line 153 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(");\r\n");

            #line 154 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else if (typeName == typeof(byte).FullName)
                {
            #line default
            #line hidden
                    this.Write("            byteSize += sizeof(");

            #line 155 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

            #line default
            #line hidden
                    this.Write(");\r\n");

            #line 156 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }
                else
                {
            #line default
            #line hidden
                    this.Write("            byteSize += sizeof(Int32);\r\n");

            #line 158 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                }

            #line default
            #line hidden

            #line 159 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write(@"            var buffer = new List<byte>(byteSize);
            buffer.AddRange(BitConverter.GetBytes(byteSize)); // 4 bytes for size, 2 byte for code, 8 bytes for data, 8 bytes for data, 8 bytes for TS, 2 bytes for term
            buffer.AddRange(BitConverter.GetBytes(Code));
            buffer.AddRange(BitConverter.GetBytes(TimeStamp.ToFileTimeUtc()));
");

            #line 164 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            foreach (var cpItem in this.ClassProperties)
            {
            #line default
            #line hidden
                this.Write("            buffer.AddRange(BitConverter.GetBytes(");

            #line 165 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(cpItem.Name));

            #line default
            #line hidden
                this.Write(")); \r\n");

            #line 166 "C:\Users\James\Source\Repos\chainsapm\CommandCreator\CommandGeneratorCS.tt"
            }

            #line default
            #line hidden
            this.Write("            buffer.AddRange(BitConverter.GetBytes((short)0));\r\n            return" +
                       " buffer.ToArray();\r\n            return null;\r\n        }\r\n    }\r\n\r\n}\r\n\r\n");
            return(this.GenerationEnvironment.ToString());
        }