public override bool Compile(Uri baseUri, BinaryAsset asset, XmlNode node, GameDefinition game, string trace, ref int position, out string ErrorDescription)
        {
            bool isFound = false;

            if (IsAttribute)
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (attribute.Name == id)
                    {
                        isFound = true;
                        FileHelper.SetUInt(StringHasher.Hash(((IsUpperCase) ? attribute.Value : attribute.Value.ToLowerInvariant())), position, asset.Content);
                        break;
                    }
                }
            }
            else
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.Name == "#text")
                    {
                        isFound = true;
                        FileHelper.SetUInt(StringHasher.Hash(((IsUpperCase) ? childNode.InnerText : childNode.InnerText.ToLowerInvariant())), position, asset.Content);
                        break;
                    }
                    if (childNode.Name == id)
                    {
                        isFound = true;
                        FileHelper.SetUInt(StringHasher.Hash(((IsUpperCase) ? childNode.InnerXml : childNode.InnerXml.ToLowerInvariant())), position, asset.Content);
                        break;
                    }
                }
            }
            if (!isFound && IsRequired)
            {
                if (IsAttribute)
                {
                    ErrorDescription = string.Format(RequiredNotFoundAttribute, id, trace);
                }
                else
                {
                    ErrorDescription = string.Format(RequiredNotFoundElement, id, trace);
                }
                return(false);
            }
            position        += 4;
            ErrorDescription = string.Empty;
            return(true);
        }
Example #2
0
        public Asset(string type, uint typeHash, string instance, uint instanceHash, string source, bool isTokenized = false) // TODO: check definition
        {
            TypeId       = StringHasher.Hash(type);
            InstanceId   = StringHasher.Hash(instance.ToLowerInvariant());
            TypeHash     = typeHash;
            InstanceHash = instanceHash;

            AssetReferences     = new List <uint[]>();
            WeakAssetReferences = new List <uint[]>();

            Type        = type;
            Name        = instance;
            SourceFile  = source;
            IsTokenized = isTokenized;

            IsNew = false;
        }
        public override bool Compile(Uri baseUri, BinaryAsset asset, XmlNode node, GameDefinition game, string trace, ref int position, out string ErrorDescription)
        {
            bool   isFound = false;
            string value   = string.Empty;

            if (IsAttribute)
            {
                if (value == string.Empty && Default != null)
                {
                    value = Default;
                }
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (attribute.Name == id)
                    {
                        isFound = true;
                        value   = attribute.Value;
                        break;
                    }
                }
            }
            else
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.Name == "#text")
                    {
                        isFound = true;
                        value   = childNode.InnerText;
                        break;
                    }
                    else if (childNode.Name == id)
                    {
                        isFound = true;
                        value   = childNode.InnerXml;
                        break;
                    }
                }
            }
            if (!isFound && IsRequired)
            {
                if (IsAttribute)
                {
                    ErrorDescription = string.Format(RequiredNotFoundAttribute, id, trace);
                }
                else
                {
                    ErrorDescription = string.Format(RequiredNotFoundElement, id, trace);
                }
                return(false);
            }
            if (value != string.Empty)
            {
                string[] name = value.Split(':');
                if (name.Length == 1)
                {
                    asset.AssetImports.Add(position, new uint[] { StringHasher.Hash(AssetType), StringHasher.Hash(name[0].ToLowerInvariant()) });
                }
                else
                {
                    asset.AssetImports.Add(position, new uint[] { StringHasher.Hash(name[0]), StringHasher.Hash(name[1].ToLowerInvariant()) });
                }
            }
            position        += 4;
            ErrorDescription = string.Empty;
            return(true);
        }
        public override bool Compile(Uri baseUri, BinaryAsset asset, XmlNode node, GameDefinition game, string trace, ref int position, out string ErrorDescription)
        {
            List <XmlNode> nodes = new List <XmlNode>();

            foreach (XmlNode childNode in node.ChildNodes)
            {
                foreach (EntryType entry in Entries)
                {
                    if (childNode.Name == entry.id)
                    {
                        nodes.Add(childNode);
                        break;
                    }
                }
            }
            if (nodes.Count < MinLength)
            {
                ErrorDescription = string.Format(LessThenMinElements, trace, MinLength, id);
                return(false);
            }
            if (MaxLength != 0 && nodes.Count > MaxLength)
            {
                ErrorDescription = string.Format(MoreThenMaxElements, trace, MaxLength, id);
                return(false);
            }
            if (nodes.Count == 0)
            {
                if (MaxLength == 1)
                {
                    position += 4;
                }
                else
                {
                    position += 8;
                }
            }
            else
            {
                if (MaxLength == 1)
                {
                    foreach (EntryType entry in Entries)
                    {
                        if (nodes[0].Name == entry.id)
                        {
                            foreach (BaseAssetType choiceBaseAsset in game.Assets.AssetTypes)
                            {
                                if (choiceBaseAsset.id == entry.AssetType)
                                {
                                    int         length      = choiceBaseAsset.GetLength(game);
                                    BinaryAsset assetChoice = new BinaryAsset(length);
                                    FileHelper.SetUInt(StringHasher.Hash(entry.AssetType), position, asset.Content);
                                    asset.SubAssets.Add(-1, assetChoice);
                                    trace += '.' + id;
                                    AssetType choiceAsset = choiceBaseAsset as AssetType;
                                    int       subPosition = 0;
                                    foreach (BaseEntryType relocationBaseEntry in choiceAsset.Entries)
                                    {
                                        if (!relocationBaseEntry.Compile(baseUri, assetChoice, nodes[0], game, trace, ref subPosition, out ErrorDescription))
                                        {
                                            return(false);
                                        }
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    position += 4;
                }
                else
                {
                    FileHelper.SetInt(nodes.Count, position, asset.Content);
                    position += 4;
                    if (nodes.Count != 0)
                    {
                        BinaryAsset choiceList = new BinaryAsset(nodes.Count * 4);
                        asset.SubAssets.Add(position, choiceList);
                        int listPosition = 0;
                        foreach (XmlNode childNode in nodes)
                        {
                            foreach (EntryType entry in Entries)
                            {
                                if (childNode.Name == entry.id)
                                {
                                    foreach (BaseAssetType choiceBaseAsset in game.Assets.AssetTypes)
                                    {
                                        if (choiceBaseAsset.id == entry.AssetType)
                                        {
                                            int         length      = choiceBaseAsset.GetLength(game);
                                            BinaryAsset assetChoice = new BinaryAsset(length + 4);
                                            FileHelper.SetUInt(StringHasher.Hash(entry.AssetType), 0, assetChoice.Content);
                                            choiceList.SubAssets.Add(listPosition, assetChoice);
                                            listPosition += 4;
                                            string    choiceTrace = trace + '.' + id;
                                            AssetType choiceAsset = choiceBaseAsset as AssetType;
                                            int       subPosition = 4;
                                            foreach (BaseEntryType relocationBaseEntry in choiceAsset.Entries)
                                            {
                                                if (!relocationBaseEntry.Compile(baseUri, assetChoice, childNode, game, choiceTrace, ref subPosition, out ErrorDescription))
                                                {
                                                    return(false);
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    position += 4;
                }
            }
            ErrorDescription = string.Empty;
            return(true);
        }