private static TextureContent CompileTexture(IAssetFsFile assetFile, TargetPlatform platform, TextureContent monogameOutput, string[] importOptions, bool allowResizeToPowerOfTwo, bool allowMakeSquare, PipelineManager manager)
        {
            var dictionary = new OpaqueDataDictionary();

            dictionary["GenerateMipmaps"]    = importOptions.Contains("GenerateMipmaps");
            dictionary["ResizeToPowerOfTwo"] = allowResizeToPowerOfTwo && !importOptions.Contains("NoResizeToPowerOfTwo");
            dictionary["MakeSquare"]         = allowMakeSquare && !importOptions.Contains("MakeSquare");
            dictionary["TextureFormat"]      = importOptions.Contains("NoCompress") ? TextureProcessorOutputFormat.Color : TextureProcessorOutputFormat.Compressed;
            var            processor = manager.CreateProcessor("TextureProcessor", dictionary);
            var            context   = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
            TextureContent content;

            try
            {
                content = (TextureContent)processor.Process(monogameOutput, context);
            }
            catch (AccessViolationException)
            {
                // Sometimes the nvtt compressor crashes on some textures.  Workaround by turning off compression
                // in this case.
                Console.WriteLine("WARNING: Disabling texture compression on " + assetFile.Name + " due to nvtt bugs");
                dictionary["TextureFormat"] = TextureProcessorOutputFormat.Color;
                processor = manager.CreateProcessor("TextureProcessor", dictionary);
                context   = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
                content   = (TextureContent)processor.Process(monogameOutput, context);
            }

            return(content);
        }
        public async Task CompileAsync(IAssetFsFile assetFile, IAssetDependencies assetDependencies, TargetPlatform platform, IWritableSerializedAsset output)
        {
            if (IntPtr.Size == 4)
            {
                throw new NotSupportedException("Font compilation is only supported under a 64-bit process.");
            }

            var json = string.Empty;

            using (var reader = new StreamReader(await assetFile.GetContentStream().ConfigureAwait(false)))
            {
                json = await reader.ReadToEndAsync().ConfigureAwait(false);
            }

            var fontDefinition = JsonConvert.DeserializeObject <FontDefinition>(json);

            foreach (var fontDesc in this.GetDescriptionsForAsset(fontDefinition))
            {
                try
                {
                    var manager = new PipelineManager(
                        Environment.CurrentDirectory,
                        Environment.CurrentDirectory,
                        Environment.CurrentDirectory);
                    var dictionary = new OpaqueDataDictionary();
                    var processor  = manager.CreateProcessor("FontDescriptionProcessor", dictionary);
                    var context    = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
                    var content    = processor.Process(fontDesc, context);

                    output.SetLoader <IAssetLoader <FontAsset> >();
                    output.SetPlatform(platform);
                    output.SetByteArray("Data", CompileAndGetBytes(content));
                    output.SetString("FontName", fontDesc.FontName);
                    output.SetFloat("FontSize", fontDesc.Size);
                    output.SetBoolean("UseKerning", fontDesc.UseKerning);
                    output.SetFloat("Spacing", fontDesc.Spacing);

                    // Font compilation was successful.
                    return;
                }
                catch (ArgumentOutOfRangeException)
                {
                    // The user might not have the font installed...
                }
                catch (NullReferenceException)
                {
                    // The user might not have the font installed...
                }
            }

            throw new InvalidOperationException("Unable to locate any font with one of the names: " + fontDefinition.FontName);
        }
        public static CompiledEffectContent Compile(EffectContent output, string tempOutputPath, TargetPlatform platform, bool isDebug, string defines)
        {
            var processor = new EffectProcessor();
            var context   = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));

            context.ActualOutputFilename = tempOutputPath;
            processor.DebugMode          = isDebug
                ? EffectProcessorDebugMode.Debug
                : EffectProcessorDebugMode.Optimize;
            processor.Defines = defines + ";" + GetPlatformDefineForEffect(platform) + ";" +
                                (isDebug ? "CONFIGURATION_DEBUG" : "CONFIGURATION_RELEASE");
            return(processor.Process(output, context));
        }
        /// <summary>
        /// The compile.
        /// </summary>
        /// <param name="asset">
        /// The asset.
        /// </param>
        /// <param name="platform">
        /// The platform.
        /// </param>
        public void Compile(TextureAsset asset, TargetPlatform platform)
        {
            if (asset.RawData == null)
            {
                return;
            }

            var tempPath = System.IO.Path.GetTempFileName();

            try
            {
                using (var stream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    stream.Write(asset.RawData, 0, asset.RawData.Length);
                }

                var importer = new TextureImporter();
                var output   = importer.Import(tempPath, new DummyContentImporterContext());

                var manager = new PipelineManager(
                    Environment.CurrentDirectory,
                    Environment.CurrentDirectory,
                    Environment.CurrentDirectory);
                var dictionary = new OpaqueDataDictionary();
                var processor  = manager.CreateProcessor("TextureProcessor", dictionary);
                var context    = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
                var content    = processor.Process(output, context);

                asset.PlatformData = new PlatformData {
                    Platform = platform, Data = this.CompileAndGetBytes(content)
                };

                try
                {
                    asset.ReadyOnGameThread();
                }
                catch (NoAssetContentManagerException)
                {
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempPath);
                }
                catch
                {
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// The compile.
        /// </summary>
        /// <param name="asset">
        /// The asset.
        /// </param>
        /// <param name="platform">
        /// The platform.
        /// </param>
        public void Compile(TextureAsset asset, TargetPlatform platform)
        {
            if (asset.RawData == null)
            {
                return;
            }

            var output = new Texture2DContent();
            var bitmap = new Bitmap(new MemoryStream(asset.RawData));
            var width  = bitmap.Width;
            var height = bitmap.Height;

            if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                throw new InvalidDataException(
                          "8-bit indexed PNGs do not convert correctly (at least under Mono) to 32-bit ARGB.  Save " +
                          "your PNG files in a non-indexed format, such as 24-bit or 32-bit ARGB.");
            }

            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                var newBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                using (var graphics = Graphics.FromImage(newBitmap)) graphics.DrawImage(bitmap, 0, 0, width, height);
                bitmap = newBitmap;
            }

            output.Faces[0] = new MipmapChain(bitmap.ToXnaBitmap(true));
            bitmap.Dispose();

            var manager = new PipelineManager(
                Environment.CurrentDirectory,
                Environment.CurrentDirectory,
                Environment.CurrentDirectory);
            var dictionary = new OpaqueDataDictionary();
            var processor  = manager.CreateProcessor("TextureProcessor", dictionary);
            var context    = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
            var content    = processor.Process(output, context);

            asset.PlatformData = new PlatformData {
                Platform = platform, Data = this.CompileAndGetBytes(content)
            };

            try
            {
                asset.ReloadTexture();
            }
            catch (NoAssetContentManagerException)
            {
            }
        }
Exemple #6
0
        /// <summary>
        /// The compile font.
        /// </summary>
        /// <param name="asset">
        /// The asset.
        /// </param>
        /// <param name="platform">
        /// The platform.
        /// </param>
        private void CompileFont(FontAsset asset, TargetPlatform platform)
        {
            foreach (var fontDesc in this.GetDescriptionsForAsset(asset))
            {
                try
                {
                    var manager = new PipelineManager(
                        Environment.CurrentDirectory,
                        Environment.CurrentDirectory,
                        Environment.CurrentDirectory);
                    var dictionary = new OpaqueDataDictionary();
                    var processor  = manager.CreateProcessor("FontDescriptionProcessor", dictionary);
                    var context    = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));
                    var content    = processor.Process(fontDesc, context);

                    asset.PlatformData = new PlatformData
                    {
                        Platform = platform,
                        Data     = this.CompileAndGetBytes(content)
                    };

                    try
                    {
                        asset.ReloadFont();
                    }
                    catch (NoAssetContentManagerException)
                    {
                        // We might be running under a server where we can't load
                        // the actual texture (because we have no game).
                    }

                    // Font compilation was successful.
                    return;
                }
                catch (ArgumentOutOfRangeException)
                {
                    // The user might not have the font installed...
                }
                catch (NullReferenceException)
                {
                    // The user might not have the font installed...
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// The compile.
        /// </summary>
        /// <param name="asset">
        /// The asset.
        /// </param>
        /// <param name="platform">
        /// The platform.
        /// </param>
        public void Compile(EffectAsset asset, TargetPlatform platform)
        {
            if (string.IsNullOrEmpty(asset.Code))
            {
                return;
            }

            var output = new EffectContent();

            output.EffectCode = this.GetEffectPrefixCode() + asset.Code;

            var tempPath = Path.GetTempFileName();

            using (var writer = new StreamWriter(tempPath))
            {
                writer.Write(output.EffectCode);
            }

            output.Identity = new ContentIdentity(tempPath);

            var tempOutputPath = Path.GetTempFileName();

            var processor = new EffectProcessor();
            var context   = new DummyContentProcessorContext(TargetPlatformCast.ToMonoGamePlatform(platform));

            context.ActualOutputFilename = tempOutputPath;
            var content = processor.Process(output, context);

            asset.PlatformData = new PlatformData {
                Platform = platform, Data = content.GetEffectCode()
            };

            File.Delete(tempPath);
            File.Delete(tempOutputPath);

            try
            {
                asset.ReloadEffect();
            }
            catch (NoAssetContentManagerException)
            {
            }
        }