/// <summary>
        /// Execute the engine, compiling the assets as needed.
        /// </summary>
        /// <param name="platforms">
        /// The target platforms for compilation.
        /// </param>
        /// <param name="output">
        /// The output directory.
        /// </param>
        public void Execute(List<string> platforms, string output)
        {
            // Set up the compiled asset saver.
            var compiledAssetSaver = new CompiledAssetSaver();

            // Retrieve the asset manager.
            var assetManager = this.m_Kernel.Get<LocalAssetManager>();
            assetManager.AllowSourceOnly = true;
            assetManager.SkipCompilation = true;

            // Retrieve the transparent asset compiler.
            var assetCompiler = this.m_Kernel.Get<ITransparentAssetCompiler>();

            // Retrieve all of the asset savers.
            var savers = this.m_Kernel.GetAll<IAssetSaver>();

            // For each of the platforms, perform the compilation of assets.
            foreach (var platformName in platforms)
            {
                Console.WriteLine("Starting compilation for " + platformName);
                var platform = (TargetPlatform)Enum.Parse(typeof(TargetPlatform), platformName);
                var outputPath = Path.Combine(output, platformName);
                assetManager.RescanAssets();

                // Create the output directory if it doesn't exist.
                if (!Directory.Exists(outputPath))
                {
                    Directory.CreateDirectory(outputPath);
                }

                // Clean up any compiled assets that no longer have source files.
                this.m_AssetCleanup.Cleanup(outputPath);

                // Get a list of asset names that we need to recompile for this platform.
                var assetNames = this.m_AssetOutOfDateCalculator.GetAssetsForRecompilation(outputPath);

                foreach (var asset in assetNames.Select(assetManager.GetUnresolved))
                {
                    assetCompiler.HandlePlatform(asset, platform, true);

                    foreach (var saver in savers)
                    {
                        var canSave = false;
                        try
                        {
                            canSave = saver.CanHandle(asset);
                        }
                        catch (Exception)
                        {
                        }

                        if (canSave)
                        {
                            try
                            {
                                var result = saver.Handle(asset, AssetTarget.CompiledFile);
                                compiledAssetSaver.SaveCompiledAsset(
                                    outputPath,
                                    asset.Name,
                                    result,
                                    result is CompiledAsset);
                                Console.WriteLine("Compiled " + asset.Name + " for " + platform);
                                break;
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ERROR: Unable to compile " + asset.Name + " for " + platform);
                                Console.WriteLine("ERROR: " + ex.GetType().FullName + ": " + ex.Message);
                                break;
                            }
                        }
                    }

                    assetManager.Dirty(asset.Name);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Compiles the built-in embedded resources.
        /// </summary>
        private static void BuiltinCompile()
        {
            // Create kernel.
            var kernel = new StandardKernel();
            kernel.Load<ProtogameAssetIoCModule>();
            kernel.Load<ProtogameScriptIoCModule>();
            var services = new GameServiceContainer();
            var assetContentManager = new AssetContentManager(services);
            kernel.Bind<IAssetContentManager>().ToMethod(x => assetContentManager);
            kernel.Bind<IRenderBatcher>().To<NullRenderBatcher>();

            // Only allow source and raw load strategies.
            kernel.Unbind<ILoadStrategy>();
            kernel.Bind<ILoadStrategy>().To<LocalSourceLoadStrategy>();
            var assetModule = new ProtogameAssetIoCModule();
            assetModule.LoadRawAssetStrategies(kernel);

            // Set up remaining bindings.
            kernel.Bind<IAssetCleanup>().To<DefaultAssetCleanup>();
            kernel.Bind<IAssetOutOfDateCalculator>().To<DefaultAssetOutOfDateCalculator>();
            kernel.Bind<IAssetCompilationEngine>().To<DefaultAssetCompilationEngine>();

            // Rebind for builtin compilation.
            kernel.Rebind<IRawAssetLoader>().To<BuiltinRawAssetLoader>();

            // Set up the compiled asset saver.
            var compiledAssetSaver = new CompiledAssetSaver();

            // Retrieve the asset manager.
            var assetManager = kernel.Get<LocalAssetManager>();
            assetManager.AllowSourceOnly = true;
            assetManager.SkipCompilation = true;

            // Retrieve the transparent asset compiler.
            var assetCompiler = kernel.Get<ITransparentAssetCompiler>();

            // Retrieve all of the asset savers.
            var savers = kernel.GetAll<IAssetSaver>();

            var rawLoader = kernel.Get<IRawAssetLoader>();

            // For each of the platforms, perform the compilation of assets.
            foreach (var platformName in new[]
                {
                    "Android",
                    "iOS",
                    "Linux",
                    "MacOSX",
                    "Ouya",
                    "Windows",
                })
            {
                Console.WriteLine("Starting compilation for " + platformName);
                var platform = (TargetPlatform)Enum.Parse(typeof(TargetPlatform), platformName);
                var outputPath = Environment.CurrentDirectory;
                assetManager.RescanAssets();

                // Create the output directory if it doesn't exist.
                if (!Directory.Exists(outputPath))
                {
                    Directory.CreateDirectory(outputPath);
                }

                // Get a list of asset names that we need to recompile for this platform.
                var assetNames = rawLoader.ScanRawAssets();

                foreach (var asset in assetNames.Select(assetManager.GetUnresolved))
                {
                    Console.Write("Compiling " + asset.Name + " for " + platform + "... ");
                    try
                    {
                        assetCompiler.HandlePlatform(asset, platform, true);

                        foreach (var saver in savers)
                        {
                            var canSave = false;
                            try
                            {
                                canSave = saver.CanHandle(asset);
                            }
                            catch (Exception)
                            {
                            }

                            if (canSave)
                            {
                                try
                                {
                                    var result = saver.Handle(asset, AssetTarget.CompiledFile);
                                    compiledAssetSaver.SaveCompiledAsset(
                                        outputPath,
                                        asset.Name,
                                        result,
                                        result is CompiledAsset,
                                        platformName);
                                    Console.WriteLine("done.");
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("failed!");
                                    Console.WriteLine("ERROR: Unable to compile " + asset.Name + " for " + platform);
                                    Console.WriteLine("ERROR: " + ex.GetType().FullName + ": " + ex.Message);
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("failed!");
                        Console.WriteLine("ERROR: Unable to compile " + asset.Name + " for " + platform);
                        Console.WriteLine("ERROR: " + ex.GetType().FullName + ": " + ex.Message);
                        break;
                    }

                    assetManager.Dirty(asset.Name);
                }
            }
        }