Esempio n. 1
0
        public bool Convert(ExcelSheetData data, int flag)
        {
            if (!verifyHeadContent(data))
            {
                return(false);
            }
            markVarType(data);
            markCollectionType(data);
            //generate csharp code
            if ((flag & (int)ConvertFlag.CF_CSharpCode) == (int)ConvertFlag.CF_CSharpCode)
            {
                if (!generateCSharpCode(data))
                {
                    return(false);
                }
            }
            //generate table asset
            if ((flag & (int)ConvertFlag.CF_TableAsset) == (int)ConvertFlag.CF_TableAsset)
            {
                if (!generateTableAsset(data))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        protected bool verifyHeadContent(ExcelSheetData data)
        {
            if (null == data)
            {
                LogErrorFormat(errorDescriptions[(int)ErrorCode.EC_INVALID_SHEETDATA]);
                return(false);
            }
            //verify head contents
            for (int i = 0; i < data.declares.Length; ++i)
            {
                var match = ms_proto_head_reg.Match(data.declares[i].head);
                if (!match.Success)
                {
                    LogErrorFormat(errorDescriptions[(int)ErrorCode.EC_HEAD_INVALID], data.sheetName, i + 1);
                    return(false);
                }

                match = ms_proto_var_type_reg.Match(data.declares[i].type);
                if (!match.Success)
                {
                    LogErrorFormat(errorDescriptions[(int)ErrorCode.EC_TYPE_INVALID], data.sheetName, data.declares[i].type, i + 1);
                    return(false);
                }

                match = ms_proto_var_name_reg.Match(data.declares[i].name);
                if (!match.Success)
                {
                    LogErrorFormat(errorDescriptions[(int)ErrorCode.EC_NAME_INVALID], data.sheetName, data.declares[i].name, i + 1);
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 3
0
        protected string generateEnumDeclartions(ExcelSheetData data)
        {
            string enumContent = string.Empty;

            for (int i = 0; i < data.declares.Length; ++i)
            {
                var declare = data.declares[i];
                if (declare.eType != HeadData.VarType.VT_ENUM)
                {
                    continue;
                }

                string enumText = string.Format("public enum {0}\n", declare.name);
                enumText += "\t\t{\n";

                var lines = declare.description.Split('\r', '\n');
                for (int j = 0; j < lines.Length; ++j)
                {
                    var match = ms_enum_prop_reg.Match(lines[j]);
                    if (!match.Success)
                    {
                        continue;
                    }
                    enumText += string.Format("\t\t\t{0} = {1},\n", match.Groups[1].Value, match.Groups[2].Value);
                }

                enumText += "\t\t}\n";

                enumContent += enumText;
            }
            return(enumContent);
        }
Esempio n. 4
0
        protected void markVarType(ExcelSheetData data)
        {
            for (int i = 0; i < data.declares.Length; ++i)
            {
                if (string.Equals(data.declares[i].type, @"string"))
                {
                    data.declares[i].eType = HeadData.VarType.VT_STRING;
                    continue;
                }

                if (string.Equals(data.declares[i].type, @"sint32"))
                {
                    data.declares[i].eType = HeadData.VarType.VT_INT;
                    continue;
                }

                if (string.Equals(data.declares[i].type, @"bool"))
                {
                    data.declares[i].eType = HeadData.VarType.VT_BOOLEAN;
                    continue;
                }

                if (string.Equals(data.declares[i].type, @"float"))
                {
                    data.declares[i].eType = HeadData.VarType.VT_FLOAT;
                    continue;
                }

                if (string.Equals(data.declares[i].type, @"enum"))
                {
                    data.declares[i].eType = HeadData.VarType.VT_ENUM;
                    continue;
                }
            }
        }
Esempio n. 5
0
        protected bool generateCSharpCode(ExcelSheetData data)
        {
            var template_file = System.IO.Path.GetFullPath(Application.dataPath + "/SmartFrame/Tools/TableConvert/Editor/TableTemplate.cs");

            if (!System.IO.File.Exists(template_file))
            {
                LogErrorFormat(errorDescriptions[(int)ErrorCode.EC_TEMPLATE_FILE_NOT_EXIST], template_file);
                return(false);
            }

            var content = System.IO.File.ReadAllText(template_file);

            content = content.Replace("TableTemplate", data.sheetName);
            content = content.Replace("return 0;//<id>", "return ID;");
            content = content.Replace("//<enum>", generateEnumDeclartions(data));
            string varFields = string.Empty;

            for (int i = 0; i < data.declares.Length; ++i)
            {
                var    declare  = data.declares[i];
                string varField = string.Empty;
                if (declare.eCollectionType == HeadData.CollectionType.CT_UNIT)
                {
                    varField = HeadData.unit_implementations[(int)declare.eType];
                }
                else if (data.declares[i].eCollectionType == HeadData.CollectionType.CT_ARRAY)
                {
                    varField = HeadData.array_implementations[(int)declare.eType];
                }

                if (declare.eType == HeadData.VarType.VT_ENUM)
                {
                    varField = string.Format(varField, data.declares[i].name);
                }
                else
                {
                    varField = string.Format(varField, data.declares[i].name);
                }

                if (string.IsNullOrEmpty(varFields))
                {
                    varFields = varField;
                }
                else
                {
                    varFields += "\n\t\t" + varField;
                }
            }
            content = content.Replace("//<content>", varFields);
            var code_file = System.IO.Path.GetFullPath(Application.dataPath + string.Format("/SmartFrame/TableScripts/{0}.cs", data.sheetName));

            System.IO.File.WriteAllText(code_file, content);

            LogFormat("Convert [{0}.cs]: Succeed ...", data.sheetName);
            return(true);
        }
Esempio n. 6
0
 public void Close()
 {
     m_sheetData = null;
     if (null != m_workbook)
     {
         m_workbook.Close();
         m_workbook = null;
     }
     if (null != m_fileStream)
     {
         m_fileStream.Close();
         m_fileStream.Dispose();
         m_fileStream = null;
     }
 }
Esempio n. 7
0
        protected void markCollectionType(ExcelSheetData data)
        {
            for (int i = 0; i < data.declares.Length; ++i)
            {
                if (string.Equals(data.declares[i].head, @"required"))
                {
                    data.declares[i].eCollectionType = HeadData.CollectionType.CT_UNIT;
                    continue;
                }

                if (string.Equals(data.declares[i].head, @"repeated"))
                {
                    data.declares[i].eCollectionType = HeadData.CollectionType.CT_ARRAY;
                }
            }
        }
Esempio n. 8
0
        protected void makeEnumImpl(ExcelSheetData data)
        {
            for (int i = 0; i < data.declares.Length; ++i)
            {
                var declare = data.declares[i];
                if (declare.eType != HeadData.VarType.VT_ENUM)
                {
                    continue;
                }

                if (null == declare.enumValueSet)
                {
                    declare.enumValueSet = new Dictionary <int, int>();
                }
                else
                {
                    declare.enumValueSet.Clear();
                }

                var lines = declare.description.Split('\r', '\n');
                for (int j = 0; j < lines.Length; ++j)
                {
                    var line  = lines[j].Trim();
                    var match = ms_enum_prop_reg.Match(line);
                    if (!match.Success)
                    {
                        continue;
                    }
                    int enumValue = int.Parse(match.Groups[2].Value);
                    if (!declare.enumValueSet.ContainsKey(enumValue))
                    {
                        declare.enumValueSet.Add(enumValue, enumValue);
                    }
                }
            }
        }
Esempio n. 9
0
        protected bool generateTableAsset(ExcelSheetData data)
        {
            //var file = UnityEditor.AssetDatabase.
            var so = ScriptableObject.CreateInstance(string.Format("Smart.Table.{0}", data.sheetName));

            if (null == so)
            {
                LogErrorFormat("create [{0}] so failed ...", data.sheetName);
                return(false);
            }

            var type      = so.GetType();
            var fieldInfo = type.GetField("datas", BindingFlags.Public | BindingFlags.Instance);

            if (null == fieldInfo)
            {
                LogErrorFormat("[{0}] get field datas failed ...", data.sheetName);
                return(false);
            }

            var asset_file = string.Format("Assets/SmartFrame/DataAssets/Table/{0}.asset", data.sheetName);

            UnityEditor.AssetDatabase.CreateAsset(so, asset_file);

            //make enum implementation
            makeEnumImpl(data);

            var tableItems = Array.CreateInstance(type.Assembly.GetType(string.Format("Smart.Table.{0}Item", data.sheetName))
                                                  , data.datas.Length);

            for (int i = 0; i < data.datas.Length; ++i)
            {
                var rowData   = data.datas[i];
                var tableItem = type.Assembly.CreateInstance(string.Format("Smart.Table.{0}Item", data.sheetName));
                if (null == tableItem)
                {
                    LogErrorFormat("[0]:create tableItem failed ...", data.sheetName);
                    return(false);
                }
                var itemType = tableItem.GetType();

                for (int j = 0; j < rowData.datas.Length; ++j)
                {
                    var declare = data.declares[j];
                    if (declare.eCollectionType == HeadData.CollectionType.CT_UNIT)
                    {
                        var  fieldName  = declare.eType == HeadData.VarType.VT_ENUM ? "e" + declare.name : declare.name;
                        var  tableField = itemType.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance);
                        bool succeed    = false;
                        var  result     = generateData(ref data.sheetName, j + 6, declare, ref rowData.datas[j], out succeed);
                        if (!succeed)
                        {
                            return(false);
                        }
                        tableField.SetValue(tableItem, result);
                    }
                    else if (declare.eCollectionType == HeadData.CollectionType.CT_ARRAY)
                    {
                        var  fieldName  = declare.eType == HeadData.VarType.VT_ENUM ? "e" + declare.name : declare.name;
                        var  tableField = itemType.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance);
                        bool succeed    = false;
                        var  contents   = null == rowData.datas[j] ? new string[0] : rowData.datas[j].Split('|');
                        var  arrayItems = Array.CreateInstance(HeadData.array_element_types[(int)declare.eType], contents.Length);
                        for (int k = 0; k < contents.Length; ++k)
                        {
                            var result = generateData(ref data.sheetName, j + 6, declare, ref contents[k], out succeed);
                            if (!succeed)
                            {
                                return(false);
                            }
                            arrayItems.SetValue(result, k);
                        }
                        tableField.SetValue(tableItem, arrayItems);
                    }
                }

                tableItems.SetValue(tableItem, i);
            }

            fieldInfo.SetValue(so, tableItems);
            UnityEditor.EditorUtility.SetDirty(so);
            UnityEditor.AssetDatabase.SaveAssets();
            return(true);
        }
Esempio n. 10
0
        void printSheetContent(ExcelSheetData data)
        {
            if (null != data)
            {
                var append  = "\t\t";
                var content = @"[1]:";

                for (int i = 0; i < data.declares.Length; ++i)
                {
                    if (i != 0)
                    {
                        content += append;
                    }
                    content += data.declares[i].head;
                }
                LogFormat(content);

                content = @"[2]:";
                for (int i = 0; i < data.declares.Length; ++i)
                {
                    if (i != 0)
                    {
                        content += append;
                    }
                    content += data.declares[i].type;
                }
                LogFormat(content);

                content = @"[3]:";
                for (int i = 0; i < data.declares.Length; ++i)
                {
                    if (i != 0)
                    {
                        content += append;
                    }
                    content += data.declares[i].name;
                }
                LogFormat(content);

                content = @"[4]:";
                for (int i = 0; i < data.declares.Length; ++i)
                {
                    if (i != 0)
                    {
                        content += append;
                    }
                    content += data.declares[i].server;
                }
                LogFormat(content);

                content = @"[5]:";
                for (int i = 0; i < data.declares.Length; ++i)
                {
                    if (i != 0)
                    {
                        content += append;
                    }
                    content += data.declares[i].description;
                }
                LogFormat(content);

                for (int i = 0; i < data.datas.Length; ++i)
                {
                    content = "[" + (i + 6).ToString() + "]:";
                    for (int j = 0; j < data.datas[i].datas.Length; ++j)
                    {
                        if (j != 0)
                        {
                            content += append;
                        }
                        content += data.datas[i].datas[j];
                    }
                    LogFormat(content);
                }
            }
        }
Esempio n. 11
0
        public bool Open(string path, int sheetIndex = 0)
        {
            m_sheetData = new ExcelSheetData();
            mFileName   = System.IO.Path.GetFileNameWithoutExtension(path);

            if (!File.Exists(path))
            {
                LogFormat(errExpress[(int)ResultCode.RC_FILE_NOT_EXIST], mFileName);
                return(false);
            }

            try
            {
                m_fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (Exception e)
            {
                LogFormat(errExpress[(int)ResultCode.RC_READ_FILE_EXCEPTION], mFileName, e.Message);
                return(false);
            }

            if (null == m_fileStream)
            {
                LogFormat(errExpress[(int)ResultCode.RC_FAILED], mFileName);
                return(false);
            }

            m_workbook = new HSSFWorkbook(m_fileStream);
            m_sheet    = m_workbook.GetSheetAt(sheetIndex);
            if (null == m_sheet)
            {
                LogFormat(errExpress[(int)ResultCode.RC_NO_SHEET], mFileName, sheetIndex);
                return(false);
            }

            if (string.IsNullOrEmpty(m_sheet.SheetName))
            {
                LogFormat(errExpress[(int)ResultCode.EC_SHEET_NAME_INVALID], mFileName);
                return(false);
            }

            //LogFormat("Load [{0}] Sheet Succeed ...",mFileName);
            for (int i = 0; i < headRowNums; ++i)
            {
                var row = m_sheet.GetRow(i);
                if (null == row && i != optionRowInHead)
                {
                    LogFormat(errExpress[(int)ResultCode.EC_ROW_HEAD_ERROR], mFileName);
                    return(false);
                }
            }

            var firstRow    = m_sheet.GetRow(0);
            int coloumValue = getColoumsValue(firstRow);

            m_sheetData.sheetName = m_sheet.SheetName;
            m_sheetData.declares  = new HeadData[coloumValue];

            for (int i = 0; i < coloumValue; ++i)
            {
                m_sheetData.declares[i] = new HeadData();
            }

            for (int i = 0; i < headRowNums; ++i)
            {
                var row = m_sheet.GetRow(i);
                if (null != row)
                {
                    for (int j = 0; j < row.Cells.Count; ++j)
                    {
                        var cell = row.Cells[j];
                        if (cell.ColumnIndex < coloumValue)
                        {
                            if (i == 0)
                            {
                                m_sheetData.declares[cell.ColumnIndex].head = getCellValue(cell);
                            }
                            else if (i == 1)
                            {
                                m_sheetData.declares[cell.ColumnIndex].type = getCellValue(cell);
                            }
                            else if (i == 2)
                            {
                                m_sheetData.declares[cell.ColumnIndex].name = getCellValue(cell);
                            }
                            else if (i == 3)
                            {
                                m_sheetData.declares[cell.ColumnIndex].server = getCellValue(cell);
                            }
                            else if (i == 4)
                            {
                                m_sheetData.declares[cell.ColumnIndex].description = getCellValue(cell);
                            }
                        }
                    }
                }
            }

            List <RowData> datas = new List <RowData>(m_sheet.LastRowNum + 1);

            for (int i = headRowNums; i <= m_sheet.LastRowNum; ++i)
            {
                var row = m_sheet.GetRow(i);
                if (null == row)
                {
                    continue;
                }

                RowData data = new RowData();
                data.datas = new string[coloumValue];
                bool isEmpty = false;

                for (int j = 0; j < row.Cells.Count; ++j)
                {
                    var cell = row.Cells[j];
                    if (null == cell || cell.ColumnIndex >= coloumValue)
                    {
                        continue;
                    }
                    data.datas[cell.ColumnIndex] = getCellValue(cell);
                    if (string.IsNullOrEmpty(data.datas[cell.ColumnIndex]))
                    {
                        isEmpty = true;
                        break;
                    }
                }

                if (!isEmpty)
                {
                    datas.Add(data);
                }
            }
            m_sheetData.datas = datas.ToArray();

            LogFormat("Load TABLE=[<color=#00ff00>{0}</color>] SHEET=[<color=#00ff00>{1}</color>] succeed ROW=[{2}] COL=[{3}]", mFileName, m_sheet.SheetName, m_sheetData.datas.Length, m_sheetData.declares.Length);
            //printSheetContent(m_sheetData);

            return(true);
        }