From 23029218b1af68d15877fb4044362a9f24c6f1fe Mon Sep 17 00:00:00 2001 From: Konstantin Koch Date: Wed, 4 Nov 2015 23:35:05 +0100 Subject: [PATCH] some code cleanup and implemented a dummy ReflectiveWriter for the compilation step --- .../ContentProcessorAttribute.cs | 2 ++ .../Graphics/DxtBitmapContent.cs | 7 +--- .../Serialization/Compiler/ContentCompiler.cs | 15 ++++---- .../Serialization/Compiler/ContentWriter.cs | 35 +++++++++++++++++-- .../Compiler/ReflectiveWriter.cs | 25 +++++++++++++ 5 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 ANX.Framework.Content.Pipeline/Serialization/Compiler/ReflectiveWriter.cs diff --git a/ANX.Framework.Content.Pipeline/ContentProcessorAttribute.cs b/ANX.Framework.Content.Pipeline/ContentProcessorAttribute.cs index a719b79e..239ac02a 100644 --- a/ANX.Framework.Content.Pipeline/ContentProcessorAttribute.cs +++ b/ANX.Framework.Content.Pipeline/ContentProcessorAttribute.cs @@ -12,6 +12,8 @@ using System.Text; namespace ANX.Framework.Content.Pipeline { + [AttributeUsage(AttributeTargets.Class)] + [Serializable] public class ContentProcessorAttribute : Attribute { public ContentProcessorAttribute() diff --git a/ANX.Framework.Content.Pipeline/Graphics/DxtBitmapContent.cs b/ANX.Framework.Content.Pipeline/Graphics/DxtBitmapContent.cs index 3e5d6b8f..11a25f44 100644 --- a/ANX.Framework.Content.Pipeline/Graphics/DxtBitmapContent.cs +++ b/ANX.Framework.Content.Pipeline/Graphics/DxtBitmapContent.cs @@ -1,4 +1,5 @@ #region Using Statements +using ANX.Framework.Graphics.PackedVector; using System; using System.Collections.Generic; using System.Linq; @@ -17,12 +18,6 @@ namespace ANX.Framework.Content.Pipeline.Graphics private int _blockSize; private byte[] _pixelData; - protected DxtBitmapContent(int blockSize) - { - //TODO: set _pixelData - _blockSize = blockSize; - } - protected DxtBitmapContent(int blockSize, int width, int height) : base(width, height) { diff --git a/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentCompiler.cs b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentCompiler.cs index 703a8adf..78786c1c 100644 --- a/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentCompiler.cs +++ b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentCompiler.cs @@ -48,7 +48,7 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler dependencies = new List(); - ContentTypeWriter contentTypeWriter; + ContentTypeWriter contentTypeWriter = null; if (!this.writerInstances.TryGetValue(type, out contentTypeWriter)) { if (type.IsGenericType) @@ -69,10 +69,6 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler } } } - else - { - contentTypeWriter = default(ContentTypeWriter); - } if (contentTypeWriter == null) { @@ -85,13 +81,14 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler contentTypeWriter = Activator.CreateInstance(typeof(ArrayWriter<>).MakeGenericType(new Type[] { type.GetElementType() })) as ContentTypeWriter; } - - if (type.IsEnum) + else if (type.IsEnum) { contentTypeWriter = Activator.CreateInstance(typeof(EnumWriter<>).MakeGenericType(new Type[] { type.GetElementType() })) as ContentTypeWriter; } - - //TODO: return new ReflectiveWriter(type); + else + { + contentTypeWriter = new ReflectiveWriter(type); + } } if (contentTypeWriter != null) diff --git a/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentWriter.cs b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentWriter.cs index dc72e1d5..261c94b1 100644 --- a/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentWriter.cs +++ b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ContentWriter.cs @@ -23,6 +23,7 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler private string referenceRelocationPath; private Dictionary typeTable = new Dictionary(); private List typeWriters = new List(); + private List sharedResources = new List(); private Stream outputStream; private MemoryStream header = new MemoryStream(); @@ -122,6 +123,9 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler int typeIndex; ContentTypeWriter typeWriter = this.GetTypeWriter(value.GetType(), out typeIndex); + if (typeWriter == null) + throw new InvalidOperationException(string.Format("Can't find a type writer for {0}.", value.GetType())); + base.Write7BitEncodedInt(typeIndex + 1); //TODO: test for recursive cyclic calls @@ -172,7 +176,21 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler public void WriteSharedResource(T value) { - throw new NotImplementedException(); + if (value == null) + { + //Index 0 is reserved for null + Write7BitEncodedInt(0); + } + else + { + int sharedResourceIndex = sharedResources.IndexOf(value); + if (sharedResourceIndex == -1) + { + sharedResourceIndex = sharedResources.Count; + sharedResources.Add(value); + } + base.Write7BitEncodedInt(sharedResourceIndex + 1); + } } public TargetPlatform TargetPlatform @@ -196,16 +214,27 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler private void WriteData() { + OutStream = content; + + //We wrote the main object previously in the content stream. + //We also have to execute the shared resources writer before we write the typeWriters list, + //because we may add additional type writers here. + foreach (var resource in sharedResources) + { + this.WriteObject(resource); + } + OutStream = header; + + //Write type writers Write7BitEncodedInt(this.typeWriters.Count); foreach (ContentTypeWriter current in this.typeWriters) { Write(current.GetRuntimeReader(TargetPlatform)); Write(current.TypeVersion); } - Write7BitEncodedInt((int)0); // number of shared resources - //TODO: write shared resources + Write7BitEncodedInt(sharedResources.Count); OutStream = outputStream; diff --git a/ANX.Framework.Content.Pipeline/Serialization/Compiler/ReflectiveWriter.cs b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ReflectiveWriter.cs new file mode 100644 index 00000000..3d3c6102 --- /dev/null +++ b/ANX.Framework.Content.Pipeline/Serialization/Compiler/ReflectiveWriter.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ANX.Framework.Content.Pipeline.Serialization.Compiler +{ + class ReflectiveWriter : ContentTypeWriter + { + public ReflectiveWriter(Type targetType) : base(targetType) + { + + } + + public override string GetRuntimeReader(TargetPlatform targetPlatform) + { + return" ANX.Framework.Content.ReflectiveReader"; + } + + protected internal override void Write(ContentWriter output, object value) + { + throw new NotImplementedException(); + } + } +}