Exemple #1
0
        public string GetValue(WDBField field)
        {
            string result = Value;

            if (string.IsNullOrEmpty(result))
            {
                result = field.DefaultValue;
            }
            return(result);
        }
        public string GetContent(WDBField field)
        {
            string content = Content;

            if (string.IsNullOrEmpty(content))
            {
                content = field.GetContent();
            }

            return(content);
        }
Exemple #3
0
        public override void Verify(WDBContext context)
        {
            WDBField field = GetField(context);
            WDBCell  cell  = GetCell(context);

            string cellContent = cell.GetContent(field);

            if (!bool.TryParse(cellContent, out _))
            {
                context.AppendError(string.Format(WDBErrorMessages.CELL_CONTENT_CONVERT_ERROR, cellContent, cell.Row, cell.Column, typeof(bool)));
            }
        }
        public void Check(WDBContext context)
        {
            WDBField field = context.Get <WDBField>(WDBContextKey.CURRENT_FIELD_NAME);

            context.Add(WDBContextKey.CURRENT_CELL_NAME, this);
            {
                var cellValidations = field.GetValidations();
                for (int i = 0; i < cellValidations.Length; i++)
                {
                    cellValidations[i].Verify(context);
                }
            }
            context.Remove(WDBContextKey.CURRENT_CELL_NAME);
        }
Exemple #5
0
        private static WDBField ReadFieldFromSheet(ISheet sheet, int fieldColumn)
        {
            sm_LogHandler?.Invoke(LogType.Info, LogMessage.INFO_START_READ_FIELD);

            IRow   typeRow       = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldTypeOffset);
            ICell  typeCell      = typeRow.GetCell(fieldColumn);
            string typeCellValue = GetCellValue(typeCell);

            if (string.IsNullOrEmpty(typeCellValue))
            {
                sm_LogHandler?.Invoke(LogType.Warning, LogMessage.WARN_EMPTY_TYPE_FIELD);
                return(null);
            }

            WDBField field = WDBFieldFactory.CreateField(fieldColumn, typeCellValue);

            if (field == null)
            {
                sm_LogHandler?.Invoke(LogType.Warning, string.Format(LogMessage.WARN_INVALID_TYPE_FIELD, typeCellValue));
                return(null);
            }

            IRow nameRow = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldNameOffset);

            field.Name = GetCellValue(nameRow.GetCell(fieldColumn));
            IRow descRow = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldDescOffset);

            field.Desc = GetCellValue(descRow.GetCell(fieldColumn));
            IRow platformRow = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldPlatformOffset);

            field.Platform = GetCellValue(platformRow.GetCell(fieldColumn));
            IRow defaultRow = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldDefaultOffset);

            if (defaultRow != null)
            {
                field.DefaultContent = GetCellValue(defaultRow.GetCell(fieldColumn));
            }
            IRow validationRow = sheet.GetRow(sm_Style.RowStartIndex + WDBConst.FieldValidationOffset);

            if (validationRow != null)
            {
                field.Validation = GetCellValue(validationRow.GetCell(fieldColumn));
            }

            sm_LogHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_CREATE_FIELD, field.ToString()));

            sm_LogHandler?.Invoke(LogType.Info, LogMessage.INFO_END_READ_FIELD);

            return(field);
        }
Exemple #6
0
        public static string WriteToLua(WDBSheet sheet, TargetPlatformType platformType)
        {
            if (sheet.FieldCount == 0 || sheet.RowCount == 0)
            {
                return(string.Empty);
            }

            WDBFieldPlatform platform = platformType == TargetPlatformType.Client ? WDBFieldPlatform.Client : WDBFieldPlatform.Server;

            StringBuilder builder = new StringBuilder();
            int           indent  = 0;

            builder.AppendLine($"local {sheet.Name} = {{");
            for (int r = 0; r < sheet.RowCount; r++)
            {
                WDBRow row = sheet.GetRowAtIndex(r);

                WDBField keyField = sheet.GetFieldAtIndex(0);
                WDBCell  keyCell  = row.GetCellByIndex(0);
                indent++;
                string keyStr = keyCell.GetContent(keyField);
                builder.AppendLine($"{GetIndent(indent)}[{keyStr}] = {{");

                for (int f = 0; f < sheet.FieldCount; f++)
                {
                    WDBField field = sheet.GetFieldAtIndex(f);
                    if (string.IsNullOrEmpty(field.Name))
                    {
                        continue;
                    }

                    if (field.FieldPlatform != WDBFieldPlatform.All && field.FieldPlatform != platform)
                    {
                        continue;
                    }
                    indent++;
                    WDBCell cell = row.GetCellByIndex(f);
                    AppendValueLine(builder, indent, field, cell);
                    indent--;
                }

                builder.AppendLine($"{GetIndent(indent)}}},");
                indent--;
            }
            builder.AppendLine("}");
            builder.AppendLine($"return {sheet.Name}");

            return(builder.ToString());
        }
Exemple #7
0
        private static WDBField[] ReadFieldFromSheet(ISheet sheet)
        {
            List <WDBField> fields = new List <WDBField>();

            for (int i = sm_Style.ColumnStartIndex + 1; i <= sm_Style.ColumnEndIndex; i++)
            {
                WDBField field = ReadFieldFromSheet(sheet, i);
                if (field != null)
                {
                    fields.Add(field);
                }
            }

            return(fields.ToArray());
        }
        private static object GetValue(WDBField field, WDBCell cell)
        {
            string content = cell.GetContent(field);

            if (field.FieldType == WDBFieldType.Bool)
            {
                if (bool.TryParse(content, out bool result))
                {
                    return(result);
                }
                return(false);
            }
            else if (field.FieldType == WDBFieldType.Float)
            {
                if (float.TryParse(content, out float result))
                {
                    return(result);
                }
                return(0);
            }
            else if (field.FieldType == WDBFieldType.Int || field.FieldType == WDBFieldType.Ref)
            {
                if (int.TryParse(content, out int result))
                {
                    return(result);
                }
                return(0);
            }
            else if (field.FieldType == WDBFieldType.Long)
            {
                if (long.TryParse(content, out long result))
                {
                    return(result);
                }
                return(0);
            }
            else if (field.FieldType == WDBFieldType.String || field.FieldType == WDBFieldType.UAsset)
            {
                return(content ?? string.Empty);
            }
            else if (field.FieldType == WDBFieldType.DateTime)
            {
                var timeSpan = DateTime.Parse(content) - new DateTime(1970, 1, 1, 0, 0, 0);
                return((long)timeSpan.TotalMilliseconds);
            }
            return(content);
        }
Exemple #9
0
        private static NDBField[] GetFields(WDBSheet sheet)
        {
            List <NDBField> fields = new List <NDBField>();

            for (int i = 0; i < sheet.FieldCount; ++i)
            {
                WDBField wdbField = sheet.GetFieldAtIndex(i);

                NDBField ndbField = new NDBField()
                {
                    FieldType = GetFieldType(wdbField.FieldType),
                    Name      = wdbField.Name,
                };
                fields.Add(ndbField);
            }

            return(fields.ToArray());
        }
        public static string WriteToJson(WDBSheet sheet, TargetPlatformType platformType)
        {
            if (sheet.FieldCount == 0 || sheet.RowCount == 0)
            {
                return(string.Empty);
            }

            WDBFieldPlatform platform = platformType == TargetPlatformType.Client ? WDBFieldPlatform.Client : WDBFieldPlatform.Server;

            JObject sheetObject = new JObject();

            for (int r = 0; r < sheet.RowCount; r++)
            {
                WDBRow row = sheet.GetRowAtIndex(r);

                JObject rowObject = new JObject();

                for (int f = 0; f < sheet.FieldCount; f++)
                {
                    WDBField field = sheet.GetFieldAtIndex(f);
                    if (string.IsNullOrEmpty(field.Name))
                    {
                        continue;
                    }

                    if (field.FieldPlatform != WDBFieldPlatform.All && field.FieldPlatform != platform)
                    {
                        continue;
                    }

                    object value = GetValue(field, row.GetCellByIndex(f));
                    rowObject.Add(field.Name, JToken.FromObject(value));

                    if (f == 0)
                    {
                        sheetObject.Add(value.ToString(), rowObject);
                    }
                }
            }

            return(sheetObject.ToString(Formatting.Indented));
        }
        private static void ReadFieldFromSheet(WDBSheet sheetData, ISheet sheet)
        {
            logHandler?.Invoke(LogType.Info, LogMessage.INFO_START_READ_FIELD);

            MethodInfo createFieldMI = typeof(WDBUtility).GetMethod("CreateField", BindingFlags.Public | BindingFlags.Static);

            int firstRowNum = readerExcelStyle.RowStartIndex;
            int lastRowNum  = firstRowNum + readerExcelStyle.FieldRowCount;
            int firstColNum = sheet.GetRow(firstRowNum).FirstCellNum;
            int lastColNum  = sheet.GetRow(firstRowNum).LastCellNum;

            for (int c = firstColNum + 1; c < lastColNum; ++c)
            {
                object[] datas = new object[readerExcelStyle.FieldRowCount + 1];
                datas[0] = c;
                for (int r = firstRowNum; r < lastRowNum; ++r)
                {
                    IRow   row       = sheet.GetRow(r);
                    string cellValue = null;
                    if (row != null)
                    {
                        cellValue = GetCellStringValue(row.GetCell(c));
                    }
                    datas[r - firstRowNum + 1] = cellValue;
                }

                WDBField         field         = (WDBField)createFieldMI.Invoke(null, datas);
                WDBFieldPlatform fieldPlatform = field.FieldPlatform;
                if (fieldPlatform != WDBFieldPlatform.All && readerExcelStyle.TargetPlatform != WDBFieldPlatform.All && fieldPlatform != readerExcelStyle.TargetPlatform)
                {
                    continue;
                }
                logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_CREATE_FIELD, field));

                sheetData.AddField(field);
            }
            logHandler?.Invoke(LogType.Info, LogMessage.INFO_END_READ_FIELD);
        }
Exemple #12
0
        private static bool VerifyField(WDBField field)
        {
            WDBFieldVerify fieldVerify = fieldVerifyDic[field];

            List <string> errors = (List <string>)context.Get(WDBContextIENames.CONTEXT_ERRORS_NAME);

            if (string.IsNullOrEmpty(field.Name))
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_FIELD_NAME_EMPTY_ERR, field.Col));
                return(false);
            }
            if (!Regex.IsMatch(field.Name, WDBVerifyConst.VERIFY_FIELD_NAME_REGEX))
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_FIELD_NAME_REGEX_ERR, field.Col));
                return(false);
            }
            if (fieldVerify.FieldType == WDBFieldType.None)
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_FIELD_TYPE_NONE_ERR, field.Col, field.Name));
                return(false);
            }
            bool result = true;

            if (fieldVerify.ValueValidations != null && fieldVerify.ValueValidations.Length > 0)
            {
                foreach (var validation in fieldVerify.ValueValidations)
                {
                    if (validation.GetType() == typeof(ErrorValidation))
                    {
                        errors.Add(string.Format(WDBVerifyConst.VERIFY_FIELD_VALIDATIONS_ERR, field.Col, field.Name, field.ValidationRule));
                        result = false;
                    }
                }
            }
            return(result);
        }
Exemple #13
0
        private static void AppendValueLine(StringBuilder builder, int indent, WDBField field, WDBCell cell)
        {
            string content = cell.GetContent(field);

            if (field.FieldType == WDBFieldType.Bool)
            {
                if (!bool.TryParse(content, out bool result))
                {
                    result = false;
                }
                builder.AppendLine($"{GetIndent(indent)}{field.Name} = {result.ToString().ToLower()},");
            }
            else if (field.FieldType == WDBFieldType.Float)
            {
                if (float.TryParse(content, out float result))
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = {content},");
                }
                else
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = 0,");
                }
            }
            else if (field.FieldType == WDBFieldType.Int || field.FieldType == WDBFieldType.Ref)
            {
                if (int.TryParse(content, out int result))
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = {content},");
                }
                else
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = 0,");
                }
            }
            else if (field.FieldType == WDBFieldType.Long)
            {
                if (long.TryParse(content, out long result))
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = {content},");
                }
                else
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = 0,");
                }
            }
            else if (field.FieldType == WDBFieldType.String || field.FieldType == WDBFieldType.UAsset)
            {
                string value = content ?? string.Empty;
                builder.AppendLine($"{GetIndent(indent)}{field.Name} = [[{value}]],");
            }
            else if (field.FieldType == WDBFieldType.DateTime)
            {
                if (!DateTime.TryParse(content, out var result))
                {
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = 0,");
                }
                else
                {
                    var timeSpan = result - new DateTime(1970, 1, 1, 0, 0, 0);
                    builder.AppendLine($"{GetIndent(indent)}{field.Name} = {(long)timeSpan.TotalMilliseconds},");
                }
            }
        }
Exemple #14
0
        private static byte[] GetLineBytes(WDBSheet sheet, Dictionary <string, int> strOffsetDic)
        {
            MemoryStream stream = new MemoryStream();

            for (int i = 0; i < sheet.LineCount; ++i)
            {
                WDBLine line = sheet.GetLineAtIndex(i);
                for (int j = 0; j < sheet.FieldCount; ++j)
                {
                    WDBField field     = sheet.GetFieldAtIndex(j);
                    WDBCell  cell      = line.GetCellByCol(field.Col);
                    string   cellValue = cell.GetValue(field);

                    if (field.FieldType == WDBFieldType.String || field.FieldType == WDBFieldType.Address ||
                        field.FieldType == WDBFieldType.Lua)
                    {
                        string content = cellValue ?? "";
                        if (field.FieldType == WDBFieldType.Lua)
                        {
                            content = GetLuaFuncContent(content);
                        }
                        if (!strOffsetDic.TryGetValue(content, out var offset))
                        {
                            throw new Exception();
                        }
                        ByteWriter.WriteInt(stream, offset);
                    }
                    else if (field.FieldType == WDBFieldType.Bool)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !bool.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteBool(stream, false);
                        }
                        else
                        {
                            ByteWriter.WriteBool(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Int || field.FieldType == WDBFieldType.Text)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !int.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteInt(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteInt(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Id || field.FieldType == WDBFieldType.Ref)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !int.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteInt(stream, -1);
                        }
                        else
                        {
                            ByteWriter.WriteInt(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Long)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !long.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteLong(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteLong(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Float)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !float.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteFloat(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteFloat(stream, result);
                        }
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            return(stream.ToArray());
        }
Exemple #15
0
        private static bool VerifySheet(WDBSheet sheet)
        {
            List <string> errors = (List <string>)context.Get(WDBContextIENames.CONTEXT_ERRORS_NAME);

            if (string.IsNullOrEmpty(sheet.Name))
            {
                errors.Add(WDBVerifyConst.VERIFY_SHEET_NAME_EMPTY_ERR);
                return(false);
            }
            if (!Regex.IsMatch(sheet.Name, WDBVerifyConst.VERIFY_SHEET_NAME_REGEX))
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NAME_REGEX_ERR, sheet.Name));
                return(false);
            }

            if (sheet.FieldCount == 0)
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NO_FIELD_ERR, sheet.Name));
                return(false);
            }
            if (sheet.LineCount == 0)
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NO_ROW_ERR, sheet.Name));
                return(false);
            }
            bool result = true;

            for (int i = 0; i < sheet.FieldCount; ++i)
            {
                WDBField       field       = sheet.GetFieldAtIndex(i);
                WDBFieldVerify fieldVerify = new WDBFieldVerify()
                {
                    FieldType        = field.FieldType,
                    FieldPlatform    = field.FieldPlatform,
                    ValueValidations = field.ValueValidations,
                };
                fieldVerifyDic.Add(field, fieldVerify);
                if (!VerifyField(field))
                {
                    result = false;
                }
            }
            if (!result)
            {
                return(false);
            }

            context.Add(WDBContextIENames.CONTEXT_SHEET_NAME, sheet);
            for (int i = 0; i < sheet.LineCount; ++i)
            {
                WDBLine line = sheet.GetLineAtIndex(i);
                if (line.CellCount != sheet.FieldCount)
                {
                    result = false;
                    errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_FIELD_ROW_ERR, sheet.FieldCount, line.Row));
                }
            }
            if (result)
            {
                for (int i = 0; i < sheet.FieldCount; ++i)
                {
                    WDBField       field       = sheet.GetFieldAtIndex(i);
                    WDBFieldVerify fieldVerify = fieldVerifyDic[field];
                    context.Add(WDBContextIENames.CONTEXT_FIELD_NAME, field);
                    context.Add(WDBContextIENames.CONTEXT_FIELD_VERIFY_NAME, fieldVerify);

                    for (int j = 0; j < sheet.LineCount; ++j)
                    {
                        WDBLine line = sheet.GetLineAtIndex(j);
                        WDBCell cell = line.GetCellByIndex(i);
                        context.Add(WDBContextIENames.CONTEXT_LINE_NAME, line);
                        if (cell.Col != field.Col)
                        {
                            result = false;
                            errors.Add(string.Format(WDBVerifyConst.VERIFY_CELL_COL_NOTSAME_ERR, cell.Row, cell.Col));
                        }
                        else if (fieldVerify.ValueValidations != null && fieldVerify.ValueValidations.Length > 0)
                        {
                            context.Add(WDBContextIENames.CONTEXT_CELL_NAME, cell);

                            foreach (var cellValidation in fieldVerify.ValueValidations)
                            {
                                context.InjectTo(cellValidation);
                                if (!cellValidation.Verify())
                                {
                                    result = false;
                                }
                            }
                            context.Remove(WDBContextIENames.CONTEXT_CELL_NAME);
                        }
                        context.Remove(WDBContextIENames.CONTEXT_LINE_NAME);
                    }
                    context.Remove(WDBContextIENames.CONTEXT_FIELD_NAME);
                    context.Remove(WDBContextIENames.CONTEXT_FIELD_VERIFY_NAME);
                }
            }
            context.Remove(WDBContextIENames.CONTEXT_SHEET_NAME);

            return(result);
        }