Exemplo n.º 1
0
        public void BuildTreeFromEmptyWorld()
        {
            var rawData = OTBMTestWorldsHelper.GetSerializedWorldData(OTBMTestWorldsHelper.Worlds.Empty);

            var tree = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: rawData);

            Assert.IsTrue(tree.Children.Count == 1);
        }
Exemplo n.º 2
0
        public void BuildTreeFromWorldWithAutoGeneratedGrass()
        {
            var rawData = OTBMTestWorldsHelper.GetSerializedWorldData(OTBMTestWorldsHelper.Worlds.AutoPlacedGrass);

            var tree = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: rawData);

            Assert.IsTrue(tree.Children.Count == 1);
        }
Exemplo n.º 3
0
        public void BuildTreeFromWorldWithDirtAndMexcalibur()
        {
            var rawData = OTBMTestWorldsHelper.GetSerializedWorldData(OTBMTestWorldsHelper.Worlds.DirtAndMexcalibur);

            var tree = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: rawData);

            Assert.IsTrue(tree.Children.Count == 1);
        }
Exemplo n.º 4
0
        public void BuildTreeFromEmptyWorld()
        {
            var rawData = OTBMTestWorldsHelper.GetSerializedWorldData(OTBMTestWorldsHelper.Worlds.Empty);

            var otbmFormatSpecifierLength = 4;
            var tree = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: rawData,
                skipFirstBytes: otbmFormatSpecifierLength);

            Assert.IsTrue(tree.Children.Count == 1);
        }
Exemplo n.º 5
0
        public void BuildTreeFromWorldWithAutoGeneratedGrass()
        {
            var rawData = OTBMTestWorldsHelper.GetSerializedWorldData(OTBMTestWorldsHelper.Worlds.AutoPlacedGrass);

            var otbmFormatSpecifierLength = 4;
            var tree = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: rawData,
                skipFirstBytes: otbmFormatSpecifierLength);

            Assert.IsTrue(tree.Children.Count == 1);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Loads a .otbm file, parse it's contents and returns a <see cref="COMMO.Server.World.World"/>.
        /// </summary>
        public static World LoadWorld(ReadOnlyMemory <byte> serializedWorldData)
        {
            var world = new World();

            var rootNode = OTBDeserializer.DeserializeOTBData(
                serializedOTBData: serializedWorldData);

            ParseOTBTreeRootNode(rootNode);

            var worldDataNode = rootNode.Children[0];

            ParseWorldDataNode(worldDataNode, world);

            //Console.WriteLine($"Tiles Loaded: {world.c}");

            return(world);
        }
Exemplo n.º 7
0
        public Dictionary <ushort, ItemType> LoadOTItems()
        {
            var itemDictionary = new Dictionary <UInt16, ItemType>();

            var attrsNotSuported = 0;
            var attrsNotValid    = 0;

            var fileTree = OTBDeserializer.DeserializeOTBData(new ReadOnlyMemory <byte>(ServerResourcesManager.GetItemsBytes("items.otb")));

            foreach (var itemChildren in fileTree.Children)
            {
                var current    = new ItemType();
                var itemStream = new OTBParsingStream(itemChildren.Data);

                var flags = itemStream.ReadUInt32();
                current.ParseOTFlags(flags);

                while (!itemStream.IsOver)
                {
                    var attr     = itemStream.ReadByte();
                    var dataSize = itemStream.ReadUInt16();

                    switch ((ItemAttributes)attr)
                    {
                    case ItemAttributes.ServerId:
                        var serverId = itemStream.ReadUInt16();

                        if (serverId == 4535)
                        {
                            serverId = 4535;
                        }

                        if (serverId > 30000 && serverId < 30100)
                        {
                            serverId -= 30000;
                        }

                        current.SetId(serverId);
                        break;

                    case ItemAttributes.ClientId:
                        current.SetClientId(itemStream.ReadUInt16());
                        break;

                    default:
                        itemStream.Skip(dataSize);
                        break;
                    }
                }
                itemDictionary.Add(current.TypeId, current);
            }

            var rootElement = XElement.Load(ServerResourcesManager.GetItems("items.xml"), LoadOptions.SetLineInfo);

            foreach (var element in rootElement.Elements("item"))
            {
                var id     = element.Attribute("id");
                var fromId = element.Attribute("fromid");
                var toId   = element.Attribute("toid");

                // Malformed element, missing id information, ignore it
                if (id == null && (fromId == null || toId == null))
                {
                    continue;
                }

                ushort serverId = 0;
                ushort aplyTo   = 1;
                if (id == null)
                {
                    // Ignore if can't parse the values or if fromId >= toId
                    if (!ushort.TryParse(fromId.Value, out serverId) || !ushort.TryParse(toId.Value, out aplyTo) || serverId >= aplyTo)
                    {
                        continue;
                    }

                    aplyTo -= serverId;
                }
                else
                {
                    if (!ushort.TryParse(id.Value, out serverId))
                    {
                        continue;
                    }
                }

                for (ushort key = serverId; key < serverId + aplyTo; key++)
                {
                    if (!itemDictionary.TryGetValue(key, out ItemType current))
                    {
                        continue;
                    }

                    var name = element.Attribute("name");
                    if (name != null)
                    {
                        current.SetName(name.Value);
                    }

                    foreach (var attribute in element.Elements("attribute"))
                    {
                        var attrName  = attribute.Attribute("key");
                        var attrValue = attribute.Attribute("value");

                        if (attrName == null || attrValue == null)
                        {
                            continue;
                        }

                        if (attrName.Value == "description")
                        {
                            current.SetDescription(attrValue.Value);
                            continue;
                        }

                        var lineInfo = (IXmlLineInfo)attribute;
                        var attr     = OpenTibiaTranslationMap.TranslateAttributeName(attrName.Value, out bool success);

                        if (success)
                        {
                            int  value   = -1;
                            bool setAttr = true;
                            switch (attrName.Value)
                            {
                            case "weaponType":
                                success = current.ParseOTWeaponType(attrValue.Value);
                                setAttr = false;
                                break;

                            case "fluidSource":
                                value = OpenTibiaTranslationMap.TranslateLiquidType(attrValue.Value, out success);
                                break;

                            case "corpseType":
                                value = OpenTibiaTranslationMap.TranslateCorpseType(attrValue.Value, out success);
                                break;

                            case "slotType":
                                value = OpenTibiaTranslationMap.TranslateSlotType(attrValue.Value, out success);
                                break;

                            default:
                                success = int.TryParse(attrValue.Value, out value);
                                break;
                            }

                            if (!success)
                            {
                                attrsNotValid++;
                                //Console.WriteLine($"[{Path.GetFileName(itemExtensionFilePath)}:{lineInfo.LineNumber}] \"{attrValue.Value}\" is not a valid value for attribute \"{attrName.Value}\"");
                            }
                            else if (setAttr)
                            {
                                current.SetAttribute(attr, value);
                            }
                        }
                        else
                        {
                            attrsNotSuported++;
                            //Console.WriteLine($"[{Path.GetFileName(itemExtensionFilePath)}:{lineInfo.LineNumber}] Attribute \"{attrName.Value}\" is not supported!");
                        }
                    }
                }
            }

            foreach (var type in itemDictionary)
            {
                type.Value.LockChanges();
            }

            Console.WriteLine($"Items with attributes not supported: {attrsNotSuported}");
            Console.WriteLine($"Not valid attributes: {attrsNotSuported}");

            return(itemDictionary);
        }