PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/PhyreFly/src/PFAssetProcessor.cs

https://bitbucket.org/drummertom999/phyreballs
C# | 185 lines | 153 code | 25 blank | 7 comment | 25 complexity | 28a6033398fcb1858a77783e45c14561 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using System.Threading;
  9. using System.Drawing;
  10. namespace PhyreFly
  11. {
  12. public enum AssetProcessAction
  13. {
  14. Clean,
  15. Build,
  16. Rebuild,
  17. None
  18. }
  19. class PFAssetProcessor
  20. {
  21. public static string AssetProcessorExe = "Tools\\PhyreAssetProcessor.exe";
  22. public static string AssetGatherExe = "Tools\\PhyreAssetGather.exe";
  23. public static string WorkingDir = string.Empty; // If all paths are absolute, will be ""
  24. public delegate void APEvent();
  25. public static event APEvent OnStart;
  26. public static event APEvent OnFinish;
  27. private static volatile bool m_isWorking = false;
  28. public static bool AutoClose = false;
  29. public static bool IsWorking
  30. {
  31. get { return m_isWorking; }
  32. }
  33. private static void SpawnProcess(string program, string args)
  34. {
  35. if (m_isWorking)
  36. return;
  37. m_isWorking = true;
  38. ProcessStartInfo pinfo = new ProcessStartInfo(program, args);
  39. pinfo.UseShellExecute = false;
  40. pinfo.RedirectStandardOutput = true;
  41. pinfo.CreateNoWindow = true;
  42. string workingDir = string.Empty;
  43. // Get working directory (user might cancel this phase)
  44. if (!GetWorkingDir(ref workingDir))
  45. return;
  46. pinfo.WorkingDirectory = workingDir;
  47. // Ensure project root is set correctly
  48. if (AssetManager.GetProjectRoot() == string.Empty)
  49. return;
  50. Process p = new Process();
  51. try
  52. {
  53. if (OnStart != null)
  54. OnStart();
  55. p.StartInfo = pinfo;
  56. p.Start();
  57. string temp = p.StandardOutput.ReadToEnd();
  58. p.WaitForExit();
  59. PFConsole.Write(PFConsole.Severity.INFO, temp);
  60. if (OnFinish != null)
  61. OnFinish();
  62. }
  63. catch (Exception e)
  64. {
  65. PFConsole.Write(PFConsole.Severity.ERROR, e.Message);
  66. }
  67. finally
  68. {
  69. m_isWorking = false;
  70. string processName = Path.GetFileName(p.StartInfo.FileName);
  71. if (p.ExitCode == 0)
  72. PFConsole.WriteLine(Color.Lime, processName + " Ran Successfully");
  73. else
  74. PFConsole.WriteLine(PFConsole.Severity.ERROR, processName + " Failed");
  75. if (AutoClose)
  76. Application.Exit();
  77. }
  78. }
  79. public static bool GetWorkingDir(ref string workingDirectory)
  80. {
  81. workingDirectory = WorkingDir;
  82. // If relative paths are in use & WorkingDir has not yet been setup...
  83. if (AssetManager.HasRelativePaths() && WorkingDir == string.Empty)
  84. {
  85. GetWorkingDirForm popup = new GetWorkingDirForm(PFGlobals.XmlFileDir);
  86. if (popup.ShowDialog() != DialogResult.OK)
  87. return false;
  88. workingDirectory = WorkingDir = popup.ChosenDirectory;
  89. }
  90. return true;
  91. }
  92. public static void ProcessSingleAsset(string source, string platform, AssetProcessAction action)
  93. {
  94. if (m_isWorking)
  95. {
  96. PFConsole.WriteLine(PFConsole.Severity.ERROR, "Busy, please wait...");
  97. return;
  98. }
  99. // First, lets make sure the file exists (even if we are using relative paths)
  100. if (!PFUtils.EnsureAssetExists(source))
  101. return;
  102. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "{0} [{1}][{2}]", PFConsole.SpreadText(action.ToString().ToUpper()), Path.GetFileName(source), platform);
  103. // Uses PhyreAssetProcessor.exe
  104. string program = Path.Combine(PFGlobals.PhyreEngineSDKRoot, AssetProcessorExe);
  105. string args = String.Format("-fi=\"{0}\" -platform={1} -modcheck={2}", source, platform, (action == AssetProcessAction.Rebuild)? "none" : "source");
  106. SpawnProcess(program, args);
  107. }
  108. private static void BuildAll(string xmlFile, string platform)
  109. {
  110. if (m_isWorking)
  111. {
  112. PFConsole.WriteLine(PFConsole.Severity.ERROR, "Busy, please wait...");
  113. return;
  114. }
  115. string program = Path.Combine(PFGlobals.PhyreEngineSDKRoot, AssetGatherExe);
  116. string args = String.Format("-config={0}|Win32 \"{1}\"", platform, xmlFile);
  117. SpawnProcess(program, args);
  118. }
  119. public static void ProcessAllAssets(string xmlFile, string platform, AssetProcessAction action)
  120. {
  121. // Uses PhyreAssetGather.exe
  122. switch (action)
  123. {
  124. case AssetProcessAction.Build:
  125. {
  126. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "BUILDING ALL [{0}]", platform);
  127. BuildAll(xmlFile, platform);
  128. break;
  129. }
  130. case AssetProcessAction.Rebuild:
  131. {
  132. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "RE-BUILDING ALL [{0}]", platform);
  133. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "CLEANING [{0}]", platform);
  134. CleanAll(platform);
  135. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "BUILDING ALL [{0}]", platform);
  136. BuildAll(xmlFile, platform);
  137. break;
  138. }
  139. case AssetProcessAction.Clean:
  140. {
  141. PFConsole.WriteHeading(PFConsole.Severity.SUCCESS, "CLEANING [{0}]", platform);
  142. CleanAll(platform);
  143. break;
  144. }
  145. }
  146. }
  147. public static int nCleanCount = 0;
  148. private static void CleanAll(string platform)
  149. {
  150. string basePath = AssetManager.GetProjectRoot();
  151. if (basePath == string.Empty)
  152. return;
  153. // Delete all "Generic" folders and per platform folders
  154. nCleanCount = 0;
  155. PFUtils.RecursiveDeleteFolders(basePath, new string[] { "Generic", platform });
  156. if (nCleanCount == 0)
  157. PFConsole.WriteLine(PFConsole.Severity.INFO, "No files to clean");
  158. }
  159. }
  160. }