Exemple #1
0
 private static void ExportJSON(DBDefinition definition, string target)
 {
     Console.WriteLine("Saving JSON to " + target);
     using (StreamWriter file = File.CreateText(target))
     {
         JsonSerializer serializer = new JsonSerializer();
         serializer.NullValueHandling = NullValueHandling.Ignore;
         serializer.Serialize(file, definition);
     }
 }
        public void Serialize(TextWriter textWriter, DBDefinition definition)
        {
            var proxy = new SerializableDBDefinition()
            {
                columnDefinitions  = new SerializableColumnDefinition(_fieldLookup, definition.columnDefinitions),
                versionDefinitions = definition.versionDefinitions
            };

            _serializer.Serialize(textWriter, proxy);
        }
Exemple #3
0
        public static bool GetVersionDefinitionByLayoutHash(DBDefinition definition, string layoutHash, out VersionDefinitions?versionToUse)
        {
            foreach (var versionDefinition in definition.versionDefinitions)
            {
                if (versionDefinition.layoutHashes.Contains(layoutHash))
                {
                    versionToUse = versionDefinition;
                    return(true);
                }
            }

            versionToUse = null;
            return(false);
        }
Exemple #4
0
        public static bool GetVersionDefinitionByBuild(DBDefinition definition, Build build, out VersionDefinitions?versionToUse)
        {
            foreach (var versionDefinition in definition.versionDefinitions)
            {
                if (versionDefinition.builds.Contains(build))
                {
                    versionToUse = versionDefinition;
                    return(true);
                }

                foreach (var range in versionDefinition.buildRanges)
                {
                    if (range.Contains(build))
                    {
                        versionToUse = versionDefinition;
                        return(true);
                    }
                }
            }

            versionToUse = null;
            return(false);
        }
 public void Serialize(string filename, DBDefinition definition)
 {
     using (StreamWriter writer = File.CreateText(filename))
         Serialize(writer, definition);
 }
Exemple #6
0
        public void Save(DBDefinition definition, string target, bool sort = false)
        {
            if (!Directory.Exists(Path.GetDirectoryName(target)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(target));
            }

            if (sort)
            {
                var sortedDBDefinitions = definition.versionDefinitions.ToList();
                sortedDBDefinitions.Sort(new DBDVersionsComparer(false));
                definition.versionDefinitions = sortedDBDefinitions.ToArray();
            }

            using (StreamWriter writer = new StreamWriter(target))
            {
                writer.NewLine = "\n";

                writer.WriteLine("COLUMNS");
                foreach (var columnDefinition in definition.columnDefinitions)
                {
                    if (columnDefinition.Value.type == "uint")
                    {
                        writer.Write("int");
                    }
                    else
                    {
                        writer.Write(columnDefinition.Value.type);
                    }

                    if (!string.IsNullOrEmpty(columnDefinition.Value.foreignTable) && !string.IsNullOrEmpty(columnDefinition.Value.foreignColumn))
                    {
                        writer.Write("<" + columnDefinition.Value.foreignTable + "::" + Utils.NormalizeColumn(columnDefinition.Value.foreignColumn) + ">");
                    }

                    var normalizedColumnName = Utils.NormalizeColumn(columnDefinition.Key);
                    writer.Write(" " + normalizedColumnName);
                    if (definition.columnDefinitions[columnDefinition.Key].type == "locstring" && !columnDefinition.Key.EndsWith("_lang"))
                    {
                        writer.Write("_lang");
                    }

                    if (columnDefinition.Value.verified == false)
                    {
                        writer.Write("?");
                    }

                    if (!string.IsNullOrWhiteSpace(columnDefinition.Value.comment))
                    {
                        writer.Write(" // " + columnDefinition.Value.comment);
                    }

                    writer.Write(writer.NewLine);
                }

                // New line between COLUMNS and BUILD definitions
                writer.Write(writer.NewLine);

                for (var i = 0; i < definition.versionDefinitions.Length; i++)
                {
                    var versionDefinition = definition.versionDefinitions[i];

                    if (versionDefinition.layoutHashes.Length > 0)
                    {
                        writer.WriteLine("LAYOUT " + string.Join(", ", versionDefinition.layoutHashes));
                    }

                    if (versionDefinition.builds.Length > 0)
                    {
                        var sortedVersionlist = new List <Build>();
                        sortedVersionlist.AddRange(versionDefinition.builds);
                        sortedVersionlist.Sort();
                        sortedVersionlist.Reverse();
                        versionDefinition.builds = sortedVersionlist.ToArray();

                        var buildsByMajor = new Dictionary <string, List <Build> >();
                        for (var b = 0; b < versionDefinition.builds.Length; b++)
                        {
                            var major = versionDefinition.builds[b].expansion + "." + versionDefinition.builds[b].major + "." + versionDefinition.builds[b].minor;
                            if (!buildsByMajor.ContainsKey(major))
                            {
                                buildsByMajor.Add(major, new List <Build>());
                            }

                            buildsByMajor[major].Add(versionDefinition.builds[b]);
                        }

                        foreach (var buildList in buildsByMajor)
                        {
                            buildList.Value.Reverse();
                            writer.Write("BUILD ");
                            for (var b = 0; b < buildList.Value.Count; b++)
                            {
                                writer.Write(buildList.Value[b].ToString());
                                if (b + 1 < buildList.Value.Count)
                                {
                                    writer.Write(", ");
                                }
                            }
                            writer.Write(writer.NewLine);
                        }
                    }

                    if (versionDefinition.buildRanges.Length > 0)
                    {
                        var sortedBuildRangeList = new List <BuildRange>();
                        sortedBuildRangeList.AddRange(versionDefinition.buildRanges);
                        if (sort)
                        {
                            sortedBuildRangeList.Sort((x, y) => x.CompareTo(y) * -1); // invert build ranges to follow def sort
                        }
                        else
                        {
                            sortedBuildRangeList.Sort();
                        }
                        versionDefinition.buildRanges = sortedBuildRangeList.ToArray();
                        foreach (var buildRange in versionDefinition.buildRanges)
                        {
                            writer.WriteLine("BUILD " + buildRange.ToString());
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(versionDefinition.comment))
                    {
                        writer.WriteLine("COMMENT " + versionDefinition.comment);
                    }

                    foreach (var column in versionDefinition.definitions)
                    {
                        if (column.isID || column.isNonInline || column.isRelation)
                        {
                            writer.Write("$");

                            if (column.isNonInline)
                            {
                                if (column.isID)
                                {
                                    writer.Write("noninline,id");
                                }
                                else if (column.isRelation)
                                {
                                    writer.Write("noninline,relation");
                                }
                            }
                            else
                            {
                                if (column.isID)
                                {
                                    writer.Write("id");
                                }
                                else if (column.isRelation)
                                {
                                    writer.Write("relation");
                                }
                            }

                            writer.Write("$");
                        }

                        var normalizedColumnName = Utils.NormalizeColumn(column.name);

                        writer.Write(normalizedColumnName);

                        // locstrings should always have _lang
                        if (definition.columnDefinitions[column.name].type == "locstring" && !column.name.EndsWith("_lang"))
                        {
                            writer.Write("_lang");
                        }

                        if (column.size > 0)
                        {
                            if (column.isSigned)
                            {
                                writer.Write("<" + column.size + ">");
                            }
                            else
                            {
                                writer.Write("<u" + column.size + ">");
                            }
                        }

                        if (column.arrLength > 0)
                        {
                            writer.Write("[" + column.arrLength + "]");
                        }

                        if (!string.IsNullOrWhiteSpace(column.comment))
                        {
                            writer.Write(" // " + column.comment);
                        }

                        writer.Write(writer.NewLine);
                    }

                    if (i + 1 < definition.versionDefinitions.Length)
                    {
                        writer.Write(writer.NewLine);
                    }
                }

                writer.Flush();
                writer.Close();
            }
        }
Exemple #7
0
        public void Save(DBDefinition definition, string target)
        {
            if (!Directory.Exists(Path.GetDirectoryName(target)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(target));
            }

            using (StreamWriter writer = new StreamWriter(target))
            {
                writer.NewLine = "\n";

                writer.WriteLine("COLUMNS");
                foreach (var columnDefinition in definition.columnDefinitions)
                {
                    writer.Write(columnDefinition.Value.type);

                    if (!string.IsNullOrEmpty(columnDefinition.Value.foreignTable) && !string.IsNullOrEmpty(columnDefinition.Value.foreignColumn))
                    {
                        writer.Write("<" + columnDefinition.Value.foreignTable + "::" + Utils.NormalizeColumn(columnDefinition.Value.foreignColumn) + ">");
                    }

                    var normalizedColumnName = Utils.NormalizeColumn(columnDefinition.Key);
                    writer.Write(" " + normalizedColumnName);
                    if (definition.columnDefinitions[columnDefinition.Key].type == "locstring" && !columnDefinition.Key.EndsWith("_lang"))
                    {
                        writer.Write("_lang");
                    }

                    if (columnDefinition.Value.verified == false)
                    {
                        writer.Write("?");
                    }

                    if (!string.IsNullOrWhiteSpace(columnDefinition.Value.comment))
                    {
                        writer.Write(" // " + columnDefinition.Value.comment);
                    }

                    writer.Write(writer.NewLine);
                }

                // New line between COLUMNS and BUILD definitions
                writer.Write(writer.NewLine);

                for (var i = 0; i < definition.versionDefinitions.Length; i++)
                {
                    var versionDefinition = definition.versionDefinitions[i];
                    if (versionDefinition.layoutHashes.Length > 0)
                    {
                        writer.WriteLine("LAYOUT " + string.Join(", ", versionDefinition.layoutHashes));
                    }

                    if (versionDefinition.builds.Length > 0)
                    {
                        writer.Write("BUILD ");
                        for (var b = 0; b < versionDefinition.builds.Length; b++)
                        {
                            writer.Write(Utils.BuildToString(versionDefinition.builds[b]));
                            if (b + 1 < versionDefinition.builds.Length)
                            {
                                writer.Write(", ");
                            }
                        }
                        writer.Write(writer.NewLine);
                    }

                    if (!string.IsNullOrWhiteSpace(versionDefinition.comment))
                    {
                        writer.WriteLine("COMMENT " + versionDefinition.comment);
                    }

                    if (versionDefinition.buildRanges.Length > 0)
                    {
                        foreach (var buildRange in versionDefinition.buildRanges)
                        {
                            writer.WriteLine("BUILD " + Utils.BuildToString(buildRange.minBuild) + "-" + Utils.BuildToString(buildRange.maxBuild));
                        }
                    }

                    foreach (var column in versionDefinition.definitions)
                    {
                        if (!string.IsNullOrWhiteSpace(column.typeOverride))
                        {
                            writer.Write("#" + column.typeOverride + "#");
                        }

                        if (column.isID && !column.isNonInline)
                        {
                            writer.Write("$id$");
                        }

                        if (column.isID && column.isNonInline)
                        {
                            writer.Write("$noninlineid$");
                        }

                        if (column.isRelation)
                        {
                            writer.Write("$relation$");
                        }

                        var normalizedColumnName = Utils.NormalizeColumn(column.name);

                        writer.Write(normalizedColumnName);

                        // locstrings should always have _lang
                        if (definition.columnDefinitions[column.name].type == "locstring" && !column.name.EndsWith("_lang"))
                        {
                            writer.Write("_lang");
                        }

                        if (column.size > 0)
                        {
                            writer.Write("<" + column.size + ">");
                        }

                        if (column.arrLength > 0)
                        {
                            writer.Write("[" + column.arrLength + "]");
                        }

                        if (!string.IsNullOrWhiteSpace(column.comment))
                        {
                            writer.Write(" // " + column.comment);
                        }

                        writer.Write(writer.NewLine);
                    }

                    if (i + 1 < definition.versionDefinitions.Length)
                    {
                        writer.Write(writer.NewLine);
                    }
                }

                writer.Flush();
                writer.Close();
            }
        }
        private static void SetupParameter(SQLiteCommand cmd, BinaryReader dataBin, DBDefinition dbd, Definition field)
        {
            var fieldName = field.name;

            if (field.name.ToLower() == "default")
            {
                fieldName = "_Default";
            }

            if (field.name.ToLower() == "order")
            {
                fieldName = "_Order";
            }
            if (field.name.ToLower() == "index")
            {
                fieldName = "_Index";
            }

            if (field.arrLength > 0)
            {
                for (var i = 0; i < field.arrLength; i++)
                {
                    if (dataBin.BaseStream.Position == dataBin.BaseStream.Length)
                    {
                        continue;
                    }

                    if (field.size == 0)
                    {
                        if (dbd.columnDefinitions[field.name].type == "float")
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadSingle());
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadCString());
                        }
                    }
                    else
                    {
                        switch (field.size)
                        {
                        case 8:
                            if (field.isSigned)
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadSByte());
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadByte());
                            }
                            break;

                        case 16:
                            if (field.isSigned)
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadInt16());
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadUInt16());
                            }
                            break;

                        case 32:
                            if (field.isSigned)
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadInt32());
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadUInt32());
                            }
                            break;

                        case 64:
                            if (field.isSigned)
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadInt64());
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@" + fieldName + "_" + i, dataBin.ReadUInt16());
                            }
                            break;
                        }
                    }

                    cmd.Parameters.AddWithValue("@U" + fieldName + "_" + i, cmd.Parameters["@" + fieldName + "_" + i].Value);
                }
            }
            else
            {
                if (field.size == 0)
                {
                    if (dbd.columnDefinitions[field.name].type == "float")
                    {
                        cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadSingle());
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadCString());
                    }
                }
                else
                {
                    switch (field.size)
                    {
                    case 8:
                        if (field.isSigned)
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadSByte());
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadByte());
                        }
                        break;

                    case 16:
                        if (field.isSigned)
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadInt16());
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadUInt16());
                        }
                        break;

                    case 32:
                        if (field.isSigned)
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadInt32());
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadUInt32());
                        }
                        break;

                    case 64:
                        if (field.isSigned)
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadInt64());
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@" + fieldName, dataBin.ReadUInt16());
                        }
                        break;
                    }
                }

                if (!field.isID)
                {
                    cmd.Parameters.AddWithValue("@U" + fieldName, cmd.Parameters["@" + fieldName].Value);
                }
            }
        }
Exemple #9
0
        public void Save(DBDefinition definition, string target)
        {
            if (!Directory.Exists(Path.GetDirectoryName(target)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(target));
            }

            using (StreamWriter writer = new StreamWriter(target))
            {
                writer.NewLine = "\n";

                writer.WriteLine("COLUMNS");
                foreach (var columnDefinition in definition.columnDefinitions)
                {
                    if (columnDefinition.Value.type == "uint")
                    {
                        writer.Write("int");
                    }
                    else
                    {
                        writer.Write(columnDefinition.Value.type);
                    }

                    if (!string.IsNullOrEmpty(columnDefinition.Value.foreignTable) && !string.IsNullOrEmpty(columnDefinition.Value.foreignColumn))
                    {
                        writer.Write("<" + columnDefinition.Value.foreignTable + "::" + Utils.NormalizeColumn(columnDefinition.Value.foreignColumn) + ">");
                    }

                    var normalizedColumnName = Utils.NormalizeColumn(columnDefinition.Key);
                    writer.Write(" " + normalizedColumnName);
                    if (definition.columnDefinitions[columnDefinition.Key].type == "locstring" && !columnDefinition.Key.EndsWith("_lang"))
                    {
                        writer.Write("_lang");
                    }

                    if (columnDefinition.Value.verified == false)
                    {
                        writer.Write("?");
                    }

                    if (!string.IsNullOrWhiteSpace(columnDefinition.Value.comment))
                    {
                        writer.Write(" // " + columnDefinition.Value.comment);
                    }

                    writer.Write(writer.NewLine);
                }

                // New line between COLUMNS and BUILD definitions
                writer.Write(writer.NewLine);

                for (var i = 0; i < definition.versionDefinitions.Length; i++)
                {
                    var versionDefinition = definition.versionDefinitions[i];
                    if (versionDefinition.layoutHashes.Length > 0)
                    {
                        writer.WriteLine("LAYOUT " + string.Join(", ", versionDefinition.layoutHashes));
                    }

                    if (versionDefinition.builds.Length > 0)
                    {
                        writer.Write("BUILD ");
                        var sortedVersionlist = new List <Build>();
                        sortedVersionlist.AddRange(versionDefinition.builds);
                        sortedVersionlist.Sort();
                        versionDefinition.builds = sortedVersionlist.ToArray();
                        for (var b = 0; b < versionDefinition.builds.Length; b++)
                        {
                            writer.Write(versionDefinition.builds[b].ToString());
                            if (b + 1 < versionDefinition.builds.Length)
                            {
                                writer.Write(", ");
                            }
                        }
                        writer.Write(writer.NewLine);
                    }

                    if (!string.IsNullOrWhiteSpace(versionDefinition.comment))
                    {
                        writer.WriteLine("COMMENT " + versionDefinition.comment);
                    }

                    if (versionDefinition.buildRanges.Length > 0)
                    {
                        foreach (var buildRange in versionDefinition.buildRanges)
                        {
                            writer.WriteLine("BUILD " + buildRange.ToString());
                        }
                    }

                    foreach (var column in versionDefinition.definitions)
                    {
                        if (column.isID || column.isNonInline || column.isRelation)
                        {
                            writer.Write("$");

                            if (column.isNonInline)
                            {
                                if (column.isID)
                                {
                                    writer.Write("noninline,id");
                                }
                                else if (column.isRelation)
                                {
                                    writer.Write("noninline,relation");
                                }
                            }
                            else
                            {
                                if (column.isID)
                                {
                                    writer.Write("id");
                                }
                                else if (column.isRelation)
                                {
                                    writer.Write("relation");
                                }
                            }

                            writer.Write("$");
                        }

                        var normalizedColumnName = Utils.NormalizeColumn(column.name);

                        writer.Write(normalizedColumnName);

                        // locstrings should always have _lang
                        if (definition.columnDefinitions[column.name].type == "locstring" && !column.name.EndsWith("_lang"))
                        {
                            writer.Write("_lang");
                        }

                        if (column.size > 0)
                        {
                            if (column.isSigned)
                            {
                                writer.Write("<" + column.size + ">");
                            }
                            else
                            {
                                writer.Write("<u" + column.size + ">");
                            }
                        }

                        if (column.arrLength > 0)
                        {
                            writer.Write("[" + column.arrLength + "]");
                        }

                        if (!string.IsNullOrWhiteSpace(column.comment))
                        {
                            writer.Write(" // " + column.comment);
                        }

                        writer.Write(writer.NewLine);
                    }

                    if (i + 1 < definition.versionDefinitions.Length)
                    {
                        writer.Write(writer.NewLine);
                    }
                }

                writer.Flush();
                writer.Close();
            }
        }