Exemplo n.º 1
0
        private static void DeserializeCompileAndLoad <T>(string file, Action <T> doAsserts)
        {
            var result = Deserialize(file, doAsserts);

            var xnbStream = new MemoryStream();

#if XNA
            // In MS XNA the ContentCompiler is completely internal, so we need
            // to use just a little reflection to get access to what we need.
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            var ctor          = typeof(ContentCompiler).GetConstructors(flags)[0];
            var compiler      = (ContentCompiler)ctor.Invoke(null);
            var compileMethod = typeof(ContentCompiler).GetMethod("Compile", flags);
            compileMethod.Invoke(compiler, new object[] { xnbStream, result, TargetPlatform.Windows, GraphicsProfile.Reach,
                                                          false, Directory.GetCurrentDirectory(), "referenceRelocationPath" });
#else
            var compiler = new ContentCompiler();
            compiler.Compile(xnbStream, result, TargetPlatform.Windows, GraphicsProfile.Reach,
                             false, "rootDirectory", "referenceRelocationPath");
#endif

            var content = new TestContentManager(xnbStream);
            var loaded  = content.Load <T>("Whatever");

            doAsserts(loaded);
        }
        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();
            }

            var  type     = content.GetType();
            var  attrib   = type.GetCustomAttribute <CompressedContentAttribute>(true);
            bool compress = attrib == null && CompressContent;

            // Write the XNB.
            using (var fs = new FileStream(
                       pipelineEvent.DestFile, FileMode.Create, FileAccess.Write, FileShare.None))
                _compiler.Compile(fs, content, Platform, Profile, compress, 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);
        }
Exemplo n.º 3
0
        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);
        }
 void CompileAndLoadAssets <T>(T data, Action <T> validation)
 {
     foreach (var platform in Platforms)
     {
         foreach (var gfxProfile in GraphicsProfiles)
         {
             foreach (var compress in CompressContents)
             {
                 using (var xnbStream = new MemoryStream())
                 {
                     Compiler.Compile(xnbStream, data, platform, gfxProfile, compress, "", "");
                     using (var content = new TestContentManager(xnbStream))
                     {
                         var result = content.Load <T>("foo");
                         validation(result);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
        /// <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);
            }
        }