Ejemplo n.º 1
0
        private void WriteItem(AwesomeWriter aw, DTBItem item, ref int lineNumber)
        {
            if (item is IntegerItem)
            {
                aw.Write((int)item.NumericValue);
                aw.Write((int)((IntegerItem)item).Integer);
            }
            else if (item is FloatItem)
            {
                aw.Write((int)item.NumericValue);
                aw.Write((float)((FloatItem)item).Float);
            }
            else if (item is StringItem)
            {
                aw.Write((int)item.NumericValue);
                aw.Write((string)((StringItem)item).String);
            }
            else if (item is ParentItem)
            {
                aw.Write((int)item.NumericValue);

                ParentItem parent = item as ParentItem;

                switch (Encoding)
                {
                default:
                case DTBEncoding.Classic:
                    // 6 bytes (2 count, 4 id)
                    aw.Write((short)parent.Count);
                    aw.Write((int)lineNumber);
                    break;

                case DTBEncoding.FME:
                    // TODO: Verify unknown value is constant

                    // 10 bytes (4 unk, 4 count, 2 id)
                    aw.Write((int)1);
                    aw.Write((int)parent.Count);
                    aw.Write((short)lineNumber);
                    break;

                case DTBEncoding.RBVR:
                    // 8 bytes (4 unk, 2 count, 2 id)
                    aw.Write((int)1);
                    aw.Write((short)parent.Count);
                    aw.Write((short)lineNumber);
                    break;
                }

                lineNumber++;
                foreach (DTBItem sub in parent)
                {
                    WriteItem(aw, sub, ref lineNumber);
                }
            }
            else
            {
                throw new Exception("Invalid DTB item type!");
            }
        }
Ejemplo n.º 2
0
        private static string GetBracket(ParentItem parent, bool start)
        {
            switch (parent.ParentType)
            {
            case ParentType.Default:
                return(start ? "(" : ")");

            case ParentType.Script:
                return(start ? "{" : "}");

            case ParentType.Property:
                return(start ? "[" : "]");

            default:
                return("");
            }
        }
Ejemplo n.º 3
0
        private ParentItem SearchForKeyword(ParentItem parent, string keyword)
        {
            foreach (DTBItem item in parent)
            {
                if (item is ParentItem)
                {
                    ParentItem subParent = SearchForKeyword(item as ParentItem, keyword);
                    if (subParent != null)
                    {
                        return(subParent);
                    }
                }
                else if (item.NumericValue == 0x05) // Checks if it's a keyword type
                {
                    // Returns parent if found
                    if (((StringItem)item).String == keyword)
                    {
                        return(parent);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private static void WriteItemToDTA4(StringBuilder sb, DTBItem item, int depth)
        {
            if (item is IntegerItem)
            {
                sb.Append(((IntegerItem)item).Integer);
            }
            else if (item is FloatItem)
            {
                sb.Append(((FloatItem)item).Float);
            }
            else if (item is StringItem)
            {
                StringItem str = item as StringItem;

                // Appends new line
                switch (str.StringType)
                {
                case StringType.IfDef:
                case StringType.Else:
                case StringType.EndIf:
                case StringType.Include:
                case StringType.Merge:
                case StringType.IfNDef:
                    sb.Append("\r\n");
                    break;
                }

                sb.Append(GetStringValue(str));

                // Appends new line
                switch (str.StringType)
                {
                case StringType.IfDef:
                case StringType.Else:
                case StringType.EndIf:
                case StringType.Include:
                case StringType.Merge:
                case StringType.IfNDef:
                    sb.Append("\r\n");
                    break;
                }
            }
            else if (item is ParentItem)
            {
                ParentItem parent = item as ParentItem;

                sb.Append(GetBracket(parent, true));
                bool newLine = (parent.Count > 2) ||
                               (parent.Count > 0 && parent.ContainsSameItems && parent[0] is ParentItem);

                for (int i = 0; i < parent.Count; i++)
                {
                    if (newLine && i != 0)
                    {
                        sb.Append("\r\n");
                        for (int ii = 0; ii < depth + 1; ii++)
                        {
                            sb.Append("\t");
                        }
                    }
                    else if (i != 0)
                    {
                        sb.Append(" ");
                    }

                    // Writes items values
                    WriteItemToDTA4(sb, parent[i], depth + 1);
                }

                sb.Append(GetBracket(parent, false));
                if (depth == 0)
                {
                    sb.Append("\r\n");
                }
            }
            else
            {
                throw new Exception("Invalid DTB item type!");
            }
        }
Ejemplo n.º 5
0
        private static void WriteItemToDTA3(StringBuilder sb, DTBItem item, int depth, bool root)
        {
            bool newLine = false;

            if (item is IntegerItem)
            {
                sb.Append(((IntegerItem)item).Integer);
            }
            else if (item is FloatItem)
            {
                sb.Append(((FloatItem)item).Float);
            }
            else if (item is StringItem)
            {
                StringItem str = item as StringItem;
                sb.Append(GetStringValue(str));

                // Appends new line
                switch (str.StringType)
                {
                case StringType.IfDef:
                case StringType.Else:
                case StringType.EndIf:
                case StringType.Include:
                case StringType.Merge:
                case StringType.IfNDef:
                    //sb.Append("\n");
                    newLine = true;
                    break;
                }

                if (newLine)
                {
                    sb.Append("\r\n");
                }
            }
            else if (item is ParentItem)
            {
                ParentItem parent = item as ParentItem;

                sb.Append(GetBracket(parent, true));

                bool firstItem = true;
                if (parent.Count < 3)
                {
                    foreach (DTBItem sub in parent)
                    {
                        if (!firstItem)
                        {
                            sb.Append(" ");
                        }
                        WriteItemToDTA(sb, sub, depth, false);
                        firstItem = false;
                    }

                    if (depth == 0 && root)
                    {
                        newLine = true;
                    }

                    //for (int i = 0; i < parent.Count; i++)
                    //{
                    //    if (i != 0) sb.Append(" ");
                    //    WriteItemToDTA(sb, parent[i], depth);

                    //    if (i != parent.Count - 1) sb.Append("\n");
                    //}
                }
                else
                {
                    newLine = true;
                    depth++;

                    //foreach (DTBItem sub in parent)
                    //{
                    //    if (!firstItem) for (int i = 0; i < depth; i++) sb.Append("\t");
                    //    WriteItemToDTA(sb, sub, depth);

                    //    sb.Append("\n");
                    //    firstItem = false;
                    //}

                    for (int i = 0; i < parent.Count; i++)
                    {
                        //  Adds tabs
                        if (i != 0)
                        {
                            for (int ii = 0; ii < depth; ii++)
                            {
                                sb.Append("\t");
                            }
                        }
                        WriteItemToDTA(sb, parent[i], depth, false);

                        if (i != parent.Count - 1)
                        {
                            sb.Append("\r\n");
                        }
                        else
                        {
                            int y = 9;
                        }
                    }
                }

                sb.Append(GetBracket(parent, false));
                //if (depth == 0) newLine = true;
            }
            else
            {
                throw new Exception("Invalid DTB item type!");
            }

            if (newLine)
            {
                sb.Append("\r\n");
            }
        }
Ejemplo n.º 6
0
        private static void WriteItemToDTA2(StringBuilder sb, DTBItem item, int depth, bool newLine)
        {
            if (item is IntegerItem)
            {
                sb.Append(((IntegerItem)item).Integer);
            }
            else if (item is FloatItem)
            {
                sb.Append(((FloatItem)item).Float);
            }
            else if (item is StringItem)
            {
                StringItem str = item as StringItem;
                sb.Append(GetStringValue(str));

                // Appends new line if it's a directive
                switch (str.StringType)
                {
                case StringType.IfDef:
                case StringType.Else:
                case StringType.EndIf:
                case StringType.Include:
                case StringType.Merge:
                case StringType.IfNDef:
                    //sb.Append("\n");
                    newLine = true;
                    break;
                }
            }
            else if (item is ParentItem)
            {
                ParentItem parent = item as ParentItem;

                sb.Append(GetBracket(parent, true));

                if (parent.Count < 3 || parent.ContainsSameItems)
                {
                    for (int i = 0; i < parent.Count; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(" ");
                        }

                        if (i != parent.Count - 1)
                        {
                            WriteItemToDTA(sb, parent[i], depth, false);
                        }
                        else
                        {
                            WriteItemToDTA(sb, parent[i], depth, true);
                        }
                    }
                }
                else
                {
                    depth++;

                    for (int i = 0; i < parent.Count; i++)
                    {
                        if (i != 0)
                        {
                            for (int ii = 0; ii < depth; ii++)
                            {
                                sb.Append("\t");
                            }
                        }

                        WriteItemToDTA(sb, parent[i], depth, true);
                    }
                }

                sb.Append(GetBracket(parent, false));
                //if (depth == 0) newLine = true;
            }
            else
            {
                throw new Exception("Invalid DTB item type!");
            }

            if (newLine)
            {
                sb.Append("\n");
            }
        }
Ejemplo n.º 7
0
        private static void ParseItem(AwesomeReader ar, ParentItem parent, DTBEncoding encoding)
        {
            int itemID = ar.ReadInt32();

            DTBItem item;

            switch (itemID)
            {
            // Integer
            case 0x00:
                item = new IntegerItem(ar.ReadInt32());
                break;

            // Float
            case 0x01:
                item = new FloatItem(ar.ReadSingle());
                break;

            // String
            case 0x02:     // Variable
            case 0x04:     // MiloEmbedded
            case 0x05:     // Keyword
            case 0x06:     // KDataUnhandled
            case 0x07:     // IfNDef
            case 0x08:     // Else
            case 0x09:     // EndIf
            case 0x12:     // Default
            case 0x20:     // Define
            case 0x21:     // Include
            case 0x22:     // Merge
            case 0x23:     // IfNDef
            case 0x24:     // Mysterious24
                item = new StringItem((StringType)itemID, ar.ReadString());
                break;

            case 0x10:     // Default
            case 0x11:     // Script
            case 0x13:     // Property
                item = new ParentItem((ParentType)itemID);

                short itemCount;

                if (encoding == DTBEncoding.Classic)
                {
                    itemCount = ar.ReadInt16();
                    ar.ReadInt32(); // Reads line number
                }
                else                // Fantasia / RBVR
                {
                    ar.ReadInt32(); // Unknown (RBVR - Always 1)
                    itemCount = (encoding == DTBEncoding.RBVR) ? ar.ReadInt16() : (short)ar.ReadInt32();
                    ar.ReadInt16();
                }

                for (int i = 0; i < itemCount; i++)
                {
                    // Recursion: Like a boss
                    ParseItem(ar, item as ParentItem, encoding);
                }

                break;

            default:
                throw new Exception(string.Format("Unknown chunk: {0} at {1}", itemID, ar.BaseStream.Position));
            }

            parent.Add(item);
        }
Ejemplo n.º 8
0
 public DTBFile()
 {
     Items    = new ParentItem(ParentType.Default);
     Embedded = false;
     Encoding = DTBEncoding.Classic;
 }