/tools/PhyreFly/src/PFAssetProcessor.cs
C# | 185 lines | 153 code | 25 blank | 7 comment | 25 complexity | 28a6033398fcb1858a77783e45c14561 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- using System.Windows.Forms;
- using System.Threading;
- using System.Drawing;
-
- namespace PhyreFly
- {
- public enum AssetProcessAction
- {
- Clean,
- Build,
- Rebuild,
- None
- }
-
- class PFAssetProcessor
- {
- public static string AssetProcessorExe = "Tools\\PhyreAssetProcessor.exe";
- public static string AssetGatherExe = "Tools\\PhyreAssetGather.exe";
- public static string WorkingDir = string.Empty; // If all paths are absolute, will be ""
- public delegate void APEvent();
- public static event APEvent OnStart;
- public static event APEvent OnFinish;
- private static volatile bool m_isWorking = false;
- public static bool AutoClose = false;
-
- public static bool IsWorking
- {
- get { return m_isWorking; }
- }
-
- private static void SpawnProcess(string program, string args)
- {
- if (m_isWorking)
- return;
-
- m_isWorking = true;
-
- ProcessStartInfo pinfo = new ProcessStartInfo(program, args);
- pinfo.UseShellExecute = false;
- pinfo.RedirectStandardOutput = true;
- pinfo.CreateNoWindow = true;
- string workingDir = string.Empty;
-
- // Get working directory (user might cancel this phase)
- if (!GetWorkingDir(ref workingDir))
- return;
- pinfo.WorkingDirectory = workingDir;
-
- // Ensure project root is set correctly
- if (AssetManager.GetProjectRoot() == string.Empty)
- return;
-
- Process p = new Process();
- try
- {
- if (OnStart != null)
- OnStart();
-
- p.StartInfo = pinfo;
- p.Start();
- string temp = p.StandardOutput.ReadToEnd();
- p.WaitForExit();
- PFConsole.Write(PFConsole.Severity.INFO, temp);
-
- if (OnFinish != null)
- OnFinish();
- }
- catch (Exception e)
- {
- PFConsole.Write(PFConsole.Severity.ERROR, e.Message);
- }
- finally
- {
- m_isWorking = false;
- string processName = Path.GetFileName(p.StartInfo.FileName);
- if (p.ExitCode == 0)
- PFConsole.WriteLine(Color.Lime, processName + " Ran Successfully");
- else
- PFConsole.WriteLine(PFConsole.Severity.ERROR, processName + " Failed");
- if (AutoClose)
- Application.Exit();
-
- }
- }
-
- public static bool GetWorkingDir(ref string workingDirectory)
- {
- workingDirectory = WorkingDir;
-
- // If relative paths are in use & WorkingDir has not yet been setup...
- if (AssetManager.HasRelativePaths() && WorkingDir == string.Empty)
- {
- GetWorkingDirForm popup = new GetWorkingDirForm(PFGlobals.XmlFileDir);
- if (popup.ShowDialog() != DialogResult.OK)
- return false;
-
- workingDirectory = WorkingDir = popup.ChosenDirectory;
- }
-
- return true;
- }
-
- public static void ProcessSingleAsset(string source, string platform, AssetProcessAction action)
- {
- if (m_isWorking)
- {
- PFConsole.WriteLine(PFConsole.Severity.ERROR, "Busy, please wait...");
- return;
- }
-
- // First, lets make sure the file exists (even if we are using relative paths)
- if (!PFUtils.EnsureAssetExists(source))
- return;
-
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "{0} [{1}][{2}]", PFConsole.SpreadText(action.ToString().ToUpper()), Path.GetFileName(source), platform);
-
- // Uses PhyreAssetProcessor.exe
- string program = Path.Combine(PFGlobals.PhyreEngineSDKRoot, AssetProcessorExe);
- string args = String.Format("-fi=\"{0}\" -platform={1} -modcheck={2}", source, platform, (action == AssetProcessAction.Rebuild)? "none" : "source");
- SpawnProcess(program, args);
- }
-
- private static void BuildAll(string xmlFile, string platform)
- {
- if (m_isWorking)
- {
- PFConsole.WriteLine(PFConsole.Severity.ERROR, "Busy, please wait...");
- return;
- }
-
- string program = Path.Combine(PFGlobals.PhyreEngineSDKRoot, AssetGatherExe);
- string args = String.Format("-config={0}|Win32 \"{1}\"", platform, xmlFile);
- SpawnProcess(program, args);
- }
-
- public static void ProcessAllAssets(string xmlFile, string platform, AssetProcessAction action)
- {
- // Uses PhyreAssetGather.exe
- switch (action)
- {
- case AssetProcessAction.Build:
- {
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "BUILDING ALL [{0}]", platform);
- BuildAll(xmlFile, platform);
- break;
- }
- case AssetProcessAction.Rebuild:
- {
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "RE-BUILDING ALL [{0}]", platform);
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "CLEANING [{0}]", platform);
- CleanAll(platform);
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "BUILDING ALL [{0}]", platform);
- BuildAll(xmlFile, platform);
- break;
- }
- case AssetProcessAction.Clean:
- {
- PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "CLEANING [{0}]", platform);
- CleanAll(platform);
- break;
- }
- }
- }
-
- public static int nCleanCount = 0;
- private static void CleanAll(string platform)
- {
- string basePath = AssetManager.GetProjectRoot();
- if (basePath == string.Empty)
- return;
-
- // Delete all "Generic" folders and per platform folders
- nCleanCount = 0;
- PFUtils.RecursiveDeleteFolders(basePath, new string[] { "Generic", platform });
- if (nCleanCount == 0)
- PFConsole.WriteLine(PFConsole.Severity.INFO, "No files to clean");
- }
- }
- }