public OtPropertyReader GetPropertyReader(OtFileNode node)
        {
            long size;
            var  buff = GetProperties(node, out size);

            return(new OtPropertyReader(new MemoryStream(buff, 0, (int)size)));
        }
        private byte[] GetProperties(OtFileNode node, out long size)
        {
            if (buffer == null || buffer.Length < node.PropsSize)
            {
                buffer = new byte[node.PropsSize];
            }

            fileStream.Seek(node.Start + 1, SeekOrigin.Begin);
            fileStream.Read(buffer, 0, (int)node.PropsSize);

            uint j       = 0;
            var  escaped = false;

            for (uint i = 0; i < node.PropsSize; ++i, ++j)
            {
                if (buffer[i] == OtFileNode.ESCAPE)
                {
                    ++i;
                    buffer[j] = buffer[i];
                    escaped   = true;
                }
                else if (escaped)
                {
                    buffer[j] = buffer[i];
                }
            }
            size = j;
            return(buffer);
        }
        public OtFileReader(string fileName)
        {
            fileStream = File.Open(fileName, FileMode.Open);
            reader     = new BinaryReader(fileStream);

            var version = reader.ReadUInt32();

            if (version > 0)
            {
                throw new Exception("Invalid file version.");
            }

            if (SafeSeek(4))
            {
                root = new OtFileNode {
                    Start = 4
                };

                if (reader.ReadByte() != OtFileNode.NODE_START || !ParseNode(root))
                {
                    throw new Exception("Invalid file format.");
                }
            }
            else
            {
                throw new Exception("Invalid file format.");
            }
        }
Пример #4
0
        public override void Deserialize(OtFileReader fileReader, OtFileNode node, OtPropertyReader reader, OtItems items)
        {
            base.Deserialize(fileReader, node, reader, items);

            var itemNode = node.Child;

            while (itemNode != null)
            {
                if ((OtMap.OtMapNodeTypes)itemNode.Type != OtMap.OtMapNodeTypes.ITEM)
                {
                    throw new Exception("Invalid item node inside container.");
                }

                OtPropertyReader props = fileReader.GetPropertyReader(itemNode);

                var itemId   = props.ReadUInt16();
                var itemType = items.GetItem(itemId);
                if (itemType == null)
                {
                    throw new Exception("Unkonw item type " + itemId + " inside container.");
                }

                var item = OtItem.Create(itemType);
                item.Deserialize(fileReader, itemNode, props, items);

                Items.Add(item);

                itemNode = itemNode.Next;
            }
        }
Пример #5
0
        private void ParseTowns(OtFileReader reader, OtFileNode otbNode)
        {
            OtFileNode nodeTown = otbNode.Child;

            while (nodeTown != null)
            {
                OtPropertyReader props = reader.GetPropertyReader(nodeTown);

                uint   townid         = props.ReadUInt32();
                string townName       = props.GetString();
                var    templeLocation = props.ReadLocation();

                var town = new OtTown {
                    Id = townid, Name = townName, TempleLocation = templeLocation
                };
                towns[townid] = town;

                nodeTown = nodeTown.Next;
            }
        }
Пример #6
0
        private void ParseTileArea(OtFileReader reader, OtFileNode otbNode, bool replaceTiles, ISet <ulong> tileLocations)
        {
            OtPropertyReader props = reader.GetPropertyReader(otbNode);

            int baseX = props.ReadUInt16();
            int baseY = props.ReadUInt16();
            int baseZ = props.ReadByte();

            OtFileNode nodeTile = otbNode.Child;

            while (nodeTile != null)
            {
                if (nodeTile.Type == (long)OtMapNodeTypes.TILE ||
                    nodeTile.Type == (long)OtMapNodeTypes.HOUSETILE)
                {
                    props = reader.GetPropertyReader(nodeTile);

                    var tileLocation = new Location(baseX + props.ReadByte(), baseY + props.ReadByte(), baseZ);

                    var tile = new OtTile(tileLocation);

                    if (nodeTile.Type == (long)OtMapNodeTypes.HOUSETILE)
                    {
                        tile.HouseId = props.ReadUInt32();
                    }

                    while (props.PeekChar() != -1)
                    {
                        byte attribute = props.ReadByte();
                        switch ((OtMapAttribute)attribute)
                        {
                        case OtMapAttribute.TILE_FLAGS: {
                            tile.Flags = props.ReadUInt32();
                            break;
                        }

                        case OtMapAttribute.ITEM: {
                            ushort itemId = props.ReadUInt16();

                            var itemType = Items.GetItem(itemId);
                            if (itemType == null)
                            {
                                throw new Exception("Unkonw item type " + itemId + " in position " + tileLocation + ".");
                            }

                            var item = OtItem.Create(itemType);
                            tile.InternalAddItem(item);

                            break;
                        }

                        default:
                            throw new Exception(string.Format("{0} Unknown tile attribute.", tileLocation));
                        }
                    }

                    OtFileNode nodeItem = nodeTile.Child;

                    while (nodeItem != null)
                    {
                        if (nodeItem.Type == (long)OtMapNodeTypes.ITEM)
                        {
                            props = reader.GetPropertyReader(nodeItem);

                            ushort itemId = props.ReadUInt16();

                            var itemType = Items.GetItem(itemId);
                            if (itemType == null)
                            {
                                throw new Exception("Unkonw item type " + itemId + " in position " + tileLocation + ".");
                            }

                            var item = OtItem.Create(itemType);
                            item.Deserialize(reader, nodeItem, props, Items);

                            tile.InternalAddItem(item);
                        }
                        else
                        {
                            throw new Exception(string.Format("{0} Unknown node type.", tileLocation));
                        }
                        nodeItem = nodeItem.Next;
                    }

                    var index   = tileLocation.ToIndex();
                    var hasTile = HasTile(index);

                    if (!hasTile)
                    {
                        SetTile(tile);
                        tileLocations.Add(tileLocation.ToIndex());
                    }
                    else if (replaceTiles)
                    {
                        SetTile(tile);
                    }
                }

                nodeTile = nodeTile.Next;
            }
        }
Пример #7
0
        public void Load(string fileName, bool replaceTiles)
        {
            if (!File.Exists(fileName))
            {
                throw new Exception(string.Format("File not found {0}.", fileName));
            }

            string spawnFile     = null;
            string houseFile     = null;
            var    tileLocations = new HashSet <ulong>();

            using (var reader = new OtFileReader(fileName)) {
                OtFileNode node = reader.GetRootNode();

                OtPropertyReader props = reader.GetPropertyReader(node);

                props.ReadByte(); // junk?

                var version = props.ReadUInt32();
                props.ReadUInt16();
                props.ReadUInt16();

                var majorVersionItems = props.ReadUInt32();
                var minorVersionItems = props.ReadUInt32();

                if (version <= 0)
                {
                    //In otbm version 1 the count variable after splashes/fluidcontainers and stackables
                    //are saved as attributes instead, this solves alot of problems with items
                    //that is changed (stackable/charges/fluidcontainer/splash) during an update.
                    throw new Exception(
                              "This map needs to be upgraded by using the latest map editor version to be able to load correctly.");
                }

                if (version > 3)
                {
                    throw new Exception("Unknown OTBM version detected.");
                }

                if (majorVersionItems < 3)
                {
                    throw new Exception(
                              "This map needs to be upgraded by using the latest map editor version to be able to load correctly.");
                }

                if (majorVersionItems > Items.MajorVersion)
                {
                    throw new Exception("The map was saved with a different items.otb version, an upgraded items.otb is required.");
                }

                if (minorVersionItems > Items.MinorVersion)
                {
                    Trace.WriteLine("This map needs an updated items.otb.");
                }

                node = node.Child;

                if ((OtMapNodeTypes)node.Type != OtMapNodeTypes.MAP_DATA)
                {
                    throw new Exception("Could not read data node.");
                }

                props = reader.GetPropertyReader(node);

                while (props.PeekChar() != -1)
                {
                    byte attribute = props.ReadByte();
                    switch ((OtMapAttribute)attribute)
                    {
                    case OtMapAttribute.DESCRIPTION:
                        var description = props.GetString();
                        //Descriptions.Add(description);
                        break;

                    case OtMapAttribute.EXT_SPAWN_FILE:
                        spawnFile = props.GetString();
                        break;

                    case OtMapAttribute.EXT_HOUSE_FILE:
                        houseFile = props.GetString();
                        break;

                    default:
                        throw new Exception("Unknown header node.");
                    }
                }

                OtFileNode nodeMapData = node.Child;

                while (nodeMapData != null)
                {
                    switch ((OtMapNodeTypes)nodeMapData.Type)
                    {
                    case OtMapNodeTypes.TILE_AREA:
                        ParseTileArea(reader, nodeMapData, replaceTiles, tileLocations);
                        break;

                    case OtMapNodeTypes.TOWNS:
                        ParseTowns(reader, nodeMapData);
                        break;
                    }
                    nodeMapData = nodeMapData.Next;
                }
            }
        }
        private bool ParseNode(OtFileNode node)
        {
            var currentNode = node;
            int val;

            while (true)
            {
                // read node type
                val = fileStream.ReadByte();
                if (val != -1)
                {
                    currentNode.Type = val;
                    var setPropSize = false;

                    while (true)
                    {
                        // search child and next node
                        val = fileStream.ReadByte();


                        if (val == OtFileNode.NODE_START)
                        {
                            var childNode = new OtFileNode {
                                Start = fileStream.Position
                            };
                            setPropSize = true;

                            currentNode.PropsSize = fileStream.Position - currentNode.Start - 2;
                            currentNode.Child     = childNode;

                            if (!ParseNode(childNode))
                            {
                                return(false);
                            }
                        }
                        else if (val == OtFileNode.NODE_END)
                        {
                            if (!setPropSize)
                            {
                                currentNode.PropsSize = fileStream.Position - currentNode.Start - 2;
                            }

                            val = fileStream.ReadByte();

                            if (val != -1)
                            {
                                if (val == OtFileNode.NODE_START)
                                {
                                    // start next node
                                    var nextNode = new OtFileNode {
                                        Start = fileStream.Position
                                    };
                                    currentNode.Next = nextNode;
                                    currentNode      = nextNode;
                                    break;
                                }

                                if (val == OtFileNode.NODE_END)
                                {
                                    // up 1 level and move 1 position back
                                    // safeTell(pos) && safeSeek(pos)
                                    fileStream.Seek(-1, SeekOrigin.Current);
                                    return(true);
                                }

                                // bad format
                                return(false);
                            }

                            // end of file?
                            return(true);
                        }
                        else if (val == OtFileNode.ESCAPE)
                        {
                            fileStream.ReadByte();
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
        }
Пример #9
0
 public virtual void Deserialize(OtFileReader fileReader, OtFileNode itemNode, OtPropertyReader reader, OtItems items)
 {
     Deserialize(reader);
 }