Beispiel #1
0
        public static void GenCode(List <CSObject> datas)
        {
            Common.Utility.Logger.Log("\r\n");
            Common.Utility.Logger.Log("gen cs >>>");

            sb_manger.Clear();
            hadnsName = !string.IsNullOrEmpty(Define.UserDefine.global.nameSpace);
            tab       = hadnsName ? "\t" : string.Empty;

            int count = datas.Count;
            int max   = count - 1;

            for (int i = 0; i < count; i++)
            {
                CSObject item = datas[i];
                try
                {
                    GenCSCode(item);
                    GenReaderCSCode(item, i == max);
                }
                catch (Exception e)
                {
                    throw;
                }
            }
            GenManagerCSCoode();
        }
Beispiel #2
0
        public static void ParseFromExcel(List <Excel.ExcelData> excelDatas)
        {
            datas = new List <CSObject>();

            Common.Utility.Logger.Log("\r\n");
            Common.Utility.Logger.Log("parse excel to cs objects >>>");

            foreach (var item in excelDatas)
            {
                Common.Utility.Logger.Log("parse excel:{0}", item.name);
                CSObject cs = new CSObject(item);
                datas.Add(cs);
            }
        }
Beispiel #3
0
        private static void GenReaderCSCode(CSObject data, bool ismax)
        {
            string code = Define.UserDefine.global.readerTemplete.Replace("#tab#", tab);

            code = code.Replace("#classname#", data.name);
            if (hadnsName)
            {
                code = CSHelper.WrapNameSpace(Define.UserDefine.global.nameSpace, code);
            }
            code = codeHeader + code;
            string filePath = string.Format("{0}/{1}DataReader.cs", Define.UserDefine.global.path.tempNotComplierPath, data.name);

            Common.Utility.File.WriteString2File(filePath, code);

            sb_manger.Append("\t\t\t");
            sb_manger.Append(string.Format("RegisterReader<{0}>(new {1}DataReader());", data.name, data.name));

            if (!ismax)
            {
                sb_manger.Append("\r\n");
            }
        }
Beispiel #4
0
        private void SerializeCSData(Assembly assembly, CSObject data)
        {
            Common.Utility.Logger.Log(string.Format("complier {0}.cs", data.name));
            Type type = assembly.GetType(data.fullName, true);

            byte[] serialized = null;
            using (MemoryStream ms = new MemoryStream())
            {
                int count = data.dataCount;
                for (int i = 0; i < count; i++)
                {
                    CSObjectData obj = data.objectDatas[i];
                    SerializeCSObj(obj, type, ms, assembly, data, i);
                }
                serialized = ms.ToArray();
            }

            string filename = Define.UserDefine.global.path.tempBinPath + "/" + data.name + "." + Define.ConstDefine.BIN_FILE_SUFFIX;

            Common.Utility.File.WriteBytes2File(filename, serialized);
            Common.Utility.Logger.Log("write " + data.name + "." + Define.ConstDefine.BIN_FILE_SUFFIX);
        }
Beispiel #5
0
        private static void GenCSCode(CSObject data)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(codeHeader);
            sb.Append("\r\n");
            sb.Append(string.Format("using {0};\r\n", CSHelper.GetUseNameSpace()));

            string nameSpace = Define.UserDefine.global.nameSpace;

            if (data.HadCollections)
            {
                sb.Append("using System.Collections.Generic;\r\n");
            }
            if (data.HadExtends && nameSpace != Define.ConstDefine.EXTENDS_NAMESPACE)
            {
                sb.Append(Define.ConstDefine.EXTENDS_NAMESPACE + ";\r\n");
            }

            sb.Append("\r\n");
            if (hadnsName)
            {
                sb.Append("namespace ");
                sb.Append(nameSpace);
                sb.Append("\r\n{");
                sb.Append("\r\n");
            }

            sb.Append(tab);
            sb.Append(CSHelper.ClassAttribute());
            sb.Append("\r\n");

            if (data.HadExtends)
            {
                int index = 1;
                if (data.HadVector2)
                {
                    sb.Append(tab);
                    sb.Append(string.Format("[ProtoInclude({0}, typeof(DataTable.Vector2))]\r\n", index));
                    index++;
                }

                if (data.HadVector3)
                {
                    sb.Append(tab);
                    sb.Append(string.Format("[ProtoInclude({0}, typeof(DataTable.Vector3))]\r\n", index));
                    index++;
                }

                if (data.HadColor)
                {
                    sb.Append(tab);
                    sb.Append(string.Format("[ProtoInclude({0}, typeof(DataTable.Color))]\r\n", index));
                    index++;
                }
            }

            sb.Append(tab);
            sb.Append("public partial class ");
            sb.Append(data.name);
            sb.Append("\r\n");
            sb.Append(tab);
            sb.Append("{");

            // construction
            sb.Append("\r\n");
            sb.Append(tab);
            sb.Append("\t");
            sb.Append("public ");
            sb.Append(data.name);
            sb.Append("() {");

            StringBuilder propertyCode = new StringBuilder();
            int           count        = data.typeCount;

            for (int i = 0; i < count; i++)
            {
                TypeDecorator type = data.type.datas[i];
                CSObjectPropertyGroup.Data propertry = data.property.datas[i];
                if (TypeAdapter.IsContainer(type.typeDecotrator) || TypeAdapter.HadExtends(type))
                {
                    sb.Append("\r\n");
                    sb.Append(tab);
                    sb.Append("\t\t");
                    sb.Append(propertry.name);
                    sb.Append(" = new ");
                    sb.Append(type.GetName());
                    sb.Append("();");
                }

                propertyCode.Append("\r\n");
                propertyCode.Append(tab);
                propertyCode.Append("\t");
                propertyCode.Append("/// <summary>\r\n");
                propertyCode.Append(tab);
                propertyCode.Append("\t/// ");
                propertyCode.Append(propertry.summary);
                propertyCode.Append("\r\n");
                propertyCode.Append(tab);
                propertyCode.Append("\t/// </summary>\r\n");

                propertyCode.Append(tab);
                propertyCode.Append("\t");
                int index = i + 1;
                propertyCode.Append(CSHelper.Attribute(index));

                propertyCode.Append("\r\n");
                propertyCode.Append(tab);
                propertyCode.Append("\t");
                propertyCode.Append("public ");
                propertyCode.Append(type.GetName());

                propertyCode.Append(" ");
                propertyCode.Append(propertry.name);
                propertyCode.Append(" { private set; get; }");
                propertyCode.Append("\r\n");
            }

            sb.Append("\r\n");
            sb.Append("\t");
            sb.Append(tab);
            sb.Append("}");
            sb.Append("\r\n");

            sb.Append(propertyCode.ToString());
            sb.Append(tab);
            sb.Append("}");

            if (hadnsName)
            {
                sb.Append("\r\n");
                sb.Append("}");
            }

            CSHelper.Write(data.name, sb.ToString());
        }
Beispiel #6
0
        private void SerializeCSObj(CSObjectData obj, Type type, MemoryStream stream, Assembly assembly, CSObject data, int row)
        {
            object instance = Activator.CreateInstance(type);

            PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            int key   = 0;
            int count = pis.Length;

            for (int i = 0; i < count; i++)
            {
                PropertyInfo  pi            = pis[i];
                string        value         = obj.datas[i];
                TypeDecorator typeDecorator = data.type.datas[i];
                try
                {
                    typeDecorator.runtimeType = pi.PropertyType;
                    object objvalue = ValueAdapter.Adapter(value, typeDecorator);
                    pi.SetValue(instance, objvalue, null);

                    if (i == 0 && pi.PropertyType == typeof(int))
                    {
                        key = (int)objvalue;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("[{0}] complier data is error, the fild is:{1}, value is:{2}, row:{3}, column:{4},  {5}{6} ",
                                                      data.name,
                                                      data.property.datas[i].name,
                                                      value,
                                                      row + Define.UserDefine.global.excel.startIndex,
                                                      typeDecorator.index,
                                                      "\r\n",
                                                      e.ToString()));
                }
            }

            byte[] data_byte = Serialize(instance);

            int length      = sizeof(int);
            int data_length = data_byte.Length;
            int all_length  = length + length + data_length;

            byte[] write       = new byte[all_length];
            byte[] length_byte = BitConverter.GetBytes(data_length);
            byte[] key_byte    = BitConverter.GetBytes(key);

            Buffer.BlockCopy(key_byte, 0, write, 0, length);
            Buffer.BlockCopy(length_byte, 0, write, length, length);
            Buffer.BlockCopy(data_byte, 0, write, length + length, data_length);

            stream.Write(write, 0, all_length);
        }