Пример #1
0
        // loads xml files as streams
        private void LoadXml(string assetName)
        {
            // standardise
            assetName = assetName.Replace('\\', '/');

            // get xml type
            string xmlType = assetName.Split('/')[1];

            // get the asset pack and its relative path
            string[] assetNameParts = assetName.Split(':');
            string   assetPack      = assetNameParts[0];
            string   assetPath      = assetNameParts[1];

            // try getting the archive
            ZipArchive archive;

            if (!loadedArchives.TryGetValue(assetPack, out archive))
            {
                throw new Exception($"Asset pack '{assetPack}' does not exist or has not been loaded");
            }

            // find the file in the archive
            Stream stream = null;

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName == assetPath)
                {
                    stream = entry.Open();
                    break;
                }
            }

            // parse xml file
            switch (xmlType)
            {
            case "AtlasDefinitions":
                // load the atlas definition
                SpriteAtlasDefinition atlasDef = SerializationHelper.XmlDeserialize <SpriteAtlasDefinition>(stream);
                // ensure the sprite is loaded
                //Sprite sprite = new Sprite(Load<Texture2D>(atlasDef.BaseTexturePath));
                // instantiate and define a new atlas
                SpriteAtlas atlas = new SpriteAtlas();
                atlas.Define(atlasDef);
                // add the atlas to the loaded assets dictionary
                AddLoadedAsset(assetName, atlas);
                break;

            default:
                // memory stream is a better long term storage solution
                MemoryStream ms = new MemoryStream();
                stream.CopyTo(ms);
                AddLoadedAsset(assetName, ms);
                break;
            }
        }