diff --git a/Source/Demos/Features/Content/Content.mgcb b/Source/Demos/Features/Content/Content.mgcb index 0f748aca..2ef3dafd 100644 --- a/Source/Demos/Features/Content/Content.mgcb +++ b/Source/Demos/Features/Content/Content.mgcb @@ -75,6 +75,7 @@ #begin Animations/zombie-atlas.json /importer:JsonContentImporter /processor:JsonContentProcessor +/processorParam:ContentType=TextureAtlas /build:Animations/zombie-atlas.json #begin Fonts/impact-32.fnt diff --git a/Source/Demos/Features/Demos/AnimationsDemo.cs b/Source/Demos/Features/Demos/AnimationsDemo.cs index 43690cab..7b5a7344 100644 --- a/Source/Demos/Features/Demos/AnimationsDemo.cs +++ b/Source/Demos/Features/Demos/AnimationsDemo.cs @@ -1,11 +1,13 @@ using System; using System.Linq; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MonoGame.Extended; using MonoGame.Extended.Animations; using MonoGame.Extended.Animations.SpriteSheets; +using MonoGame.Extended.Serialization; using MonoGame.Extended.Sprites; using MonoGame.Extended.TextureAtlases; @@ -23,6 +25,8 @@ namespace Features.Demos public AnimationsDemo(GameMain game) : base(game) { + ContentTypeReaderManager.AddTypeCreator("TextureAtlas", () => new TextureAtlasJsonContentTypeReader()); + ContentTypeReaderManager.AddTypeCreator("Default", () => new JsonContentTypeReader()); } protected override void Initialize() diff --git a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessor.cs b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessor.cs index ea4d2126..6be4ac36 100644 --- a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessor.cs +++ b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessor.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using Microsoft.Xna.Framework.Content.Pipeline; namespace MonoGame.Extended.Content.Pipeline.Json @@ -6,11 +7,18 @@ namespace MonoGame.Extended.Content.Pipeline.Json [ContentProcessor(DisplayName = "JSON Processor - MonoGame.Extended")] public class JsonContentProcessor : ContentProcessor, JsonContentProcessorResult> { + [DefaultValue(typeof(Type), "System.Object")] + public string ContentType { get; set; } + public override JsonContentProcessorResult Process(ContentImporterResult input, ContentProcessorContext context) { try { - var output = new JsonContentProcessorResult { Json = input.Data }; + var output = new JsonContentProcessorResult + { + ContentType = ContentType, + Json = input.Data + }; return output; } catch (Exception ex) diff --git a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessorResult.cs b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessorResult.cs index cd073cbc..eaef99eb 100644 --- a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessorResult.cs +++ b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentProcessorResult.cs @@ -2,6 +2,7 @@ { public class JsonContentProcessorResult { + public string ContentType { get; set; } public string Json { get; set; } } } \ No newline at end of file diff --git a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentTypeWriter.cs b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentTypeWriter.cs index eea12719..c221922b 100644 --- a/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentTypeWriter.cs +++ b/Source/MonoGame.Extended.Content.Pipeline/Json/JsonContentTypeWriter.cs @@ -1,29 +1,27 @@ using Microsoft.Xna.Framework.Content.Pipeline; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler; -using MonoGame.Extended.Sprites; namespace MonoGame.Extended.Content.Pipeline.Json { [ContentTypeWriter] public class JsonContentTypeWriter : ContentTypeWriter { + private string _runtimeType; + protected override void Write(ContentWriter writer, JsonContentProcessorResult result) { + _runtimeType = result.ContentType; writer.Write(result.Json); } public override string GetRuntimeReader(TargetPlatform targetPlatform) { - return "MonoGame.Extended.Serialization.JsonContentTypeReader, MonoGame.Extended"; + return _runtimeType;// "MonoGame.Extended.Serialization.JsonContentTypeReader, MonoGame.Extended"; } public override string GetRuntimeType(TargetPlatform targetPlatform) { - var type = typeof(Sprite); - var readerType = type.Namespace + ".JsonContentTypeReader, " + type.AssemblyQualifiedName; - return "GetRuntimeType"; - - //return typeof(object).AssemblyQualifiedName;// "MonoGame.Extended.Serialization.JsonContentTypeReader, MonoGame.Extended"; + return _runtimeType;// "MonoGame.Extended.Serialization.JsonContentTypeReader, MonoGame.Extended"; } } } \ No newline at end of file diff --git a/Source/MonoGame.Extended/Serialization/JsonContentTypeReader.cs b/Source/MonoGame.Extended/Serialization/JsonContentTypeReader.cs index 8182c800..c97829b9 100644 --- a/Source/MonoGame.Extended/Serialization/JsonContentTypeReader.cs +++ b/Source/MonoGame.Extended/Serialization/JsonContentTypeReader.cs @@ -8,19 +8,9 @@ using Newtonsoft.Json; namespace MonoGame.Extended.Serialization { - public class JsonContentTypeReader : ContentTypeReader + public class JsonContentTypeReader : ContentTypeReader { - //protected override object Read(ContentReader input, object existingInstance) - //{ - // var json = input.ReadString(); - // return null; - //} - - public JsonContentTypeReader() : base(typeof(TextureAtlas)) - { - } - - protected override object Read(ContentReader reader, object existingInstance) + protected override T Read(ContentReader reader, T existingInstance) { var json = reader.ReadString(); @@ -28,32 +18,104 @@ namespace MonoGame.Extended.Serialization using (var jsonReader = new JsonTextReader(stringReader)) { var serializer = new JsonSerializer(); - var texturePackerFile = serializer.Deserialize(jsonReader); - - - var assetName = reader.GetRelativeAssetName(texturePackerFile.Metadata.Image); - var texture = reader.ContentManager.Load(assetName); - var atlas = new TextureAtlas(assetName, texture); - - var regionCount = texturePackerFile.Regions.Count; - - for (var i = 0; i < regionCount; i++) - { - atlas.CreateRegion( - ContentReaderExtensions.RemoveExtension(texturePackerFile.Regions[i].Filename), - texturePackerFile.Regions[i].Frame.X, - texturePackerFile.Regions[i].Frame.Y, - texturePackerFile.Regions[i].Frame.Width, - texturePackerFile.Regions[i].Frame.Height); - } - - return atlas; - - - - - return texturePackerFile; + return serializer.Deserialize(jsonReader); } } } + + public class TextureAtlasJsonContentTypeReader : JsonContentTypeReader + { + private TexturePackerFile Load(ContentReader reader) + { + var json = reader.ReadString(); + + using (var stringReader = new StringReader(json)) + using (var jsonReader = new JsonTextReader(stringReader)) + { + var serializer = new JsonSerializer(); + return serializer.Deserialize(jsonReader); + } + } + + protected override TextureAtlas Read(ContentReader reader, TextureAtlas existingInstance) + { + var texturePackerFile = Load(reader); + var assetName = reader.GetRelativeAssetName(texturePackerFile.Metadata.Image); + var texture = reader.ContentManager.Load(assetName); + var atlas = new TextureAtlas(assetName, texture); + + var regionCount = texturePackerFile.Regions.Count; + + for (var i = 0; i < regionCount; i++) + { + atlas.CreateRegion( + ContentReaderExtensions.RemoveExtension(texturePackerFile.Regions[i].Filename), + texturePackerFile.Regions[i].Frame.X, + texturePackerFile.Regions[i].Frame.Y, + texturePackerFile.Regions[i].Frame.Width, + texturePackerFile.Regions[i].Frame.Height); + } + + return atlas; + } + } + + //public abstract class JsonContentTypeReader : ContentTypeReader + //{ + // protected abstract TOutput OnDeserialize(ContentReader reader, TInput texturePackerFile); + + // protected override TOutput Read(ContentReader reader, TOutput existingInstance) + // { + // var json = reader.ReadString(); + + // using (var stringReader = new StringReader(json)) + // using (var jsonReader = new JsonTextReader(stringReader)) + // { + // var serializer = new JsonSerializer(); + // var input = serializer.Deserialize(jsonReader); + // return OnDeserialize(reader, input); + // } + // } + //} + + //public class JsonContentTypeReader : ContentTypeReader + //{ + // public JsonContentTypeReader(string blah) : base(typeof(object)) + // { + // } + + // protected override void Initialize(ContentTypeReaderManager manager) + // { + // base.Initialize(manager); + // } + + // protected override object Read(ContentReader input, object existingInstance) + // { + // return null; + // } + //} + + //public class TexturePackerJsonContentTypeReader : JsonContentTypeReader + //{ + // protected override TextureAtlas OnDeserialize(ContentReader reader, TexturePackerFile texturePackerFile) + // { + // var assetName = reader.GetRelativeAssetName(texturePackerFile.Metadata.Image); + // var texture = reader.ContentManager.Load(assetName); + // var atlas = new TextureAtlas(assetName, texture); + + // var regionCount = texturePackerFile.Regions.Count; + + // for (var i = 0; i < regionCount; i++) + // { + // atlas.CreateRegion( + // ContentReaderExtensions.RemoveExtension(texturePackerFile.Regions[i].Filename), + // texturePackerFile.Regions[i].Frame.X, + // texturePackerFile.Regions[i].Frame.Y, + // texturePackerFile.Regions[i].Frame.Width, + // texturePackerFile.Regions[i].Frame.Height); + // } + + // return atlas; + // } + //} } \ No newline at end of file