/// <summary> /// Initialize the writer. /// </summary> /// <param name="compiler">Compiler instance calling this writer.</param> protected override void Initialize(ContentCompiler compiler) { base.Initialize(compiler); underlyingType = Enum.GetUnderlyingType(typeof(T)); elementWriter = compiler.GetTypeWriter(underlyingType); }
protected override void Initialize(ContentCompiler compiler) { _compiler = compiler; var type = ReflectionHelpers.GetBaseType(TargetType); if (type != null && type != typeof(object) && !TargetType.IsValueType) { _baseType = type; } var runtimeType = TargetType.GetCustomAttributes(typeof(ContentSerializerRuntimeTypeAttribute), false).FirstOrDefault() as ContentSerializerRuntimeTypeAttribute; if (runtimeType != null) { _runtimeType = runtimeType.RuntimeType; } var typeVersion = TargetType.GetCustomAttributes(typeof(ContentSerializerTypeVersionAttribute), false).FirstOrDefault() as ContentSerializerTypeVersionAttribute; if (typeVersion != null) { _typeVersion = typeVersion.TypeVersion; } _properties = TargetType.GetAllProperties().Where(IsValidProperty).ToArray(); _fields = TargetType.GetAllFields().Where(IsValidField).ToArray(); }
/// <summary> /// Initialize the writer. /// </summary> /// <param name="compiler">Compiler instance calling this writer.</param> protected override void Initialize(ContentCompiler compiler) { base.Initialize(compiler); keyWriter = compiler.GetTypeWriter(typeof(K)); valueWriter = compiler.GetTypeWriter(typeof(V)); }
protected override void Initialize(ContentCompiler compiler) { base.Initialize(compiler); if (TargetType.IsGenericType) { _genericTypes = new List <ContentTypeWriter>(); var arguments = TargetType.GetGenericArguments(); foreach (var arg in arguments) { _genericTypes.Add(compiler.GetTypeWriter(arg)); } } }
/// <summary> /// Creates a new instance of ContentWriter. /// </summary> /// <param name="compiler">The compiler object that created this writer.</param> /// <param name="output">The stream to write the XNB file to.</param> /// <param name="targetPlatform">The platform the XNB is intended for.</param> /// <param name="targetProfile">The graphics profile of the target.</param> /// <param name="compressContent">True if the content should be compressed.</param> /// <param name="rootDirectory">The root directory of the content.</param> /// <param name="referenceRelocationPath">The path of the XNB file, used to calculate relative paths for external references.</param> internal ContentWriter(ContentCompiler compiler, Stream output, TargetPlatform targetPlatform, GraphicsProfile targetProfile, bool compressContent, string rootDirectory, string referenceRelocationPath) : base(output) { this.compiler = compiler; this.targetPlatform = targetPlatform; this.targetProfile = targetProfile; this.compressContent = compressContent; this.rootDirectory = rootDirectory; // Normalize the directory format so PathHelper.GetRelativePath will compute external references correctly. this.referenceRelocationPath = PathHelper.NormalizeDirectory(referenceRelocationPath); outputStream = this.OutStream; bodyStream = new MemoryStream(); this.OutStream = bodyStream; }
/// <summary> /// Creates a new instance of ContentWriter. /// </summary> /// <param name="compiler">The compiler object that created this writer.</param> /// <param name="output">The stream to write the XNB file to.</param> /// <param name="targetPlatform">The platform the XNB is intended for.</param> /// <param name="targetProfile">The graphics profile of the target.</param> /// <param name="compressContent">True if the content should be compressed.</param> /// <param name="rootDirectory">The root directory of the content.</param> /// <param name="referenceRelocationPath">The path of the XNB file, used to calculate relative paths for external references.</param> internal ContentWriter(ContentCompiler compiler, Stream output, TargetPlatform targetPlatform, GraphicsProfile targetProfile, bool compressContent, string rootDirectory, string referenceRelocationPath) : base(output) { if (compressContent) { throw new NotSupportedException("Content compression is not supported at this time."); } this.compiler = compiler; this.targetPlatform = targetPlatform; this.targetProfile = targetProfile; this.compressContent = compressContent; this.rootDirectory = rootDirectory; this.referenceRelocationPath = referenceRelocationPath; outputStream = this.OutStream; headerStream = new MemoryStream(); bodyStream = new MemoryStream(); this.OutStream = bodyStream; }
/// <summary> /// Compile the specified input object and return the XNB file as a byte array. /// </summary> /// <param name="content"> /// The content to compile. /// </param> /// <returns> /// The compiled XNB file as a byte array. /// </returns> protected byte[] CompileAndGetBytes(object content) { var temp = Path.GetTempFileName(); var compiler = new ContentCompiler(); try { using (var stream = new FileStream(temp, FileMode.Open, FileAccess.Write)) { compiler.Compile( stream, content, MonoGamePlatform.Windows, GraphicsProfile.Reach, false, Environment.CurrentDirectory, Environment.CurrentDirectory); } byte[] result; using (var stream = new FileStream(temp, FileMode.Open, FileAccess.Read)) { stream.Position = 0; using (var reader = new BinaryReader(stream)) { result = reader.ReadBytes((int)stream.Length); } } File.Delete(temp); return result; } finally { File.Delete(temp); } }
protected virtual void Initialize(ContentCompiler compiler) { }
protected virtual void Initialize(ContentCompiler compiler) { compiler.Writers.Add(TargetType, this); }
protected override void Initialize(ContentCompiler compiler) { base.Initialize(compiler); _underlyingType = Enum.GetUnderlyingType(typeof(T)); }
/// <summary> /// Retrieves and caches nested type writers and allows for reflection over the target data type. Called by the framework at creation time. /// </summary> /// <param name="compiler">The content compiler.</param> protected virtual void Initialize(ContentCompiler compiler) { }
protected virtual void Initialize(ContentCompiler compiler) { compiler.RegisterTypeWriter(this); }
internal void RegisterAndInitialize(ContentCompiler compiler) { Initialize(compiler); }
protected virtual void Initialize(ContentCompiler compiler) { compiler.RegisterTypeWriter(this); }
internal void RegisterAndInitialize(ContentCompiler compiler) { Initialize(compiler); }
private void WriteXnb(object content, PipelineBuildEvent pipelineEvent) { // Make sure the output directory exists. var outputFileDir = Path.GetDirectoryName(pipelineEvent.DestFile); Directory.CreateDirectory(outputFileDir); if (_compiler == null) _compiler = new ContentCompiler(); // Write the XNB. using (var stream = new FileStream(pipelineEvent.DestFile, FileMode.Create, FileAccess.Write, FileShare.None)) _compiler.Compile(stream, content, Platform, Profile, false, OutputDirectory, outputFileDir); // Store the last write time of the output XNB here // so we can verify it hasn't been tampered with. pipelineEvent.DestTime = File.GetLastWriteTime(pipelineEvent.DestFile); }
/// <summary> /// Initialize the writer. /// </summary> /// <param name="compiler">Compiler instance calling this writer.</param> protected override void Initialize(ContentCompiler compiler) { base.Initialize(compiler); elementWriter = compiler.GetTypeWriter(typeof(T)); }
public override bool Execute() { Log.LogMessage("Building content:"); XBuildLogger logger = new XBuildLogger(this.Log); ContentCompiler compiler = new ContentCompiler(PipelineAssemblies); foreach (ITaskItem sourceItem in SourceAssets) { //foreach (string name in sourceItem.MetadataNames) // Log.LogMessage(name + " : " + sourceItem.GetMetadata(name)); string assetName = sourceItem.GetMetadata("Name"); Log.LogMessage("Building " + assetName); Stream outputStream = new FileStream(OutputDirectory + assetName + ".xnb", FileMode.OpenOrCreate); ContentWriter contentWriter = new ContentWriter(outputStream, getTargetPlatform(), CompressContent); string importerName = sourceItem.GetMetadata("Importer"); string processorName = sourceItem.GetMetadata("Processor"); IContentImporter importer = getImporterInstance(importerName); if (importer == null) Log.LogError("Could not find the importer (" + importerName + ")"); IContentProcessor processor = getProcessorInstance(processorName); if (importer == null) Log.LogError("Could not find the processor (" + processorName + ")"); Log.LogMessage("Using " + importerName + " and " + processorName); ContentImporterContext importerContext = new ContentImporterContext(this, IntermediateDirectory, OutputDirectory, logger); ContentProcessorContext processorContext = new ContentProcessorContext(); processor.Process(importer.Import(sourceItem.GetMetadata("Include"), importerContext), processorContext); } return true; }