using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Newtonsoft.Json; using RecrownedAthenaeum.DataTypes; using RecrownedAthenaeum.Pipeline.NinePatch; using RecrownedAthenaeum.Pipeline.TextureAtlas; using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RecrownedAthenaeum.Pipeline { class TextureAtlasDataReader : ContentTypeReader { protected override DataTypes.TextureAtlas Read(ContentReader input, DataTypes.TextureAtlas existingInstance) { int length = input.ReadInt32(); byte[] compressedBytes = input.ReadBytes(length); byte[] decompressedBytes; using (MemoryStream inStream = new MemoryStream(compressedBytes)) { using (GZipStream gZStream = new GZipStream(inStream, CompressionLevel.Optimal)) { using (MemoryStream outStream = new MemoryStream()) { gZStream.CopyTo(outStream); decompressedBytes = outStream.ToArray(); } } } TextureAtlasData atlasData = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(decompressedBytes)); DataTypes.TextureAtlas atlas; Texture2D atlasTexture = input.ContentManager.Load(atlasData.textureName); DataTypes.TextureAtlas.TextureAtlasRegion[] regions = new DataTypes.TextureAtlas.TextureAtlasRegion[atlasData.regions.Length]; for (int regionID = 0; regionID < regions.Length; regionID++) { TextureAtlasData.TextureAtlasRegion regionData = atlasData.regions[regionID]; DataTypes.NinePatch nPatch = null; if (regionData.ninePatchData != null) { NinePatchData nPatchData = regionData.ninePatchData; nPatch = new DataTypes.NinePatch(atlasTexture, nPatchData.a, nPatchData.b, nPatchData.c, nPatchData.d); } regions[regionID] = new DataTypes.TextureAtlas.TextureAtlasRegion(regionData.name, regionData.location, nPatch, atlasTexture); } atlas = new DataTypes.TextureAtlas(atlasTexture, regions); return atlas; } } }