BuildManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Threading;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Contains a list of valid platforms that can be built for.
  10. /// </summary>
  11. public enum PlatformType // Note: Must match C++ enum PlatformType
  12. {
  13. Windows,
  14. Count // Keep at end
  15. }
  16. /// <summary>
  17. /// Contains build data for a specific platform.
  18. /// </summary>
  19. public abstract class PlatformInfo : ScriptObject
  20. {
  21. /// <summary>
  22. /// Creates a new platform info. For internal runtime use only.
  23. /// </summary>
  24. protected PlatformInfo()
  25. { }
  26. /// <summary>
  27. /// Returns the platform that this object contains data for.
  28. /// </summary>
  29. public PlatformType Type
  30. {
  31. get { return Internal_GetType(mCachedPtr); }
  32. }
  33. /// <summary>
  34. /// Initial scene that is loaded when application is first started.
  35. /// </summary>
  36. public Prefab MainScene
  37. {
  38. get { return Internal_GetMainScene(mCachedPtr); }
  39. set
  40. {
  41. IntPtr scenePtr = IntPtr.Zero;
  42. if (value != null)
  43. scenePtr = value.GetCachedPtr();
  44. Internal_SetMainScene(mCachedPtr, scenePtr);
  45. }
  46. }
  47. /// <summary>
  48. /// Determines should the application be started in fullscreen using the user's desktop resolution.
  49. /// </summary>
  50. public bool Fullscreen
  51. {
  52. get { return Internal_GetFullscreen(mCachedPtr); }
  53. set { Internal_SetFullscreen(mCachedPtr, value); }
  54. }
  55. /// <summary>
  56. /// Width of a window if the game is started in windowed mode. This is only relevant if <see cref="Fullscreen"/>
  57. /// is off.
  58. /// </summary>
  59. public int WindowedWidth
  60. {
  61. get
  62. {
  63. int width, height;
  64. Internal_GetResolution(mCachedPtr, out width, out height);
  65. return width;
  66. }
  67. set { Internal_SetResolution(mCachedPtr, value, WindowedHeight); }
  68. }
  69. /// <summary>
  70. /// Height of a window if the game is started in windowed mode. This is only relevant if <see cref="Fullscreen"/>
  71. /// is off.
  72. /// </summary>
  73. public int WindowedHeight
  74. {
  75. get
  76. {
  77. int width, height;
  78. Internal_GetResolution(mCachedPtr, out width, out height);
  79. return height;
  80. }
  81. set { Internal_SetResolution(mCachedPtr, WindowedWidth, value); }
  82. }
  83. /// <summary>
  84. /// Determines should the scripts be output in debug mode (worse performance but better error reporting).
  85. /// </summary>
  86. public bool Debug
  87. {
  88. get { return Internal_GetDebug(mCachedPtr); }
  89. set { Internal_SetDebug(mCachedPtr, value); }
  90. }
  91. /// <summary>
  92. /// A set of semicolon separated defines to use when compiling scripts for this platform.
  93. /// </summary>
  94. public string Defines
  95. {
  96. get { return Internal_GetDefines(mCachedPtr); }
  97. set { Internal_SetDefines(mCachedPtr, value); }
  98. }
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern PlatformType Internal_GetType(IntPtr thisPtr);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern string Internal_GetDefines(IntPtr thisPtr);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_SetDefines(IntPtr thisPtr, string value);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern Prefab Internal_GetMainScene(IntPtr thisPtr);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. static extern void Internal_SetMainScene(IntPtr thisPtr, IntPtr prefabPtr);
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. static extern bool Internal_GetFullscreen(IntPtr thisPtr);
  111. [MethodImpl(MethodImplOptions.InternalCall)]
  112. static extern void Internal_SetFullscreen(IntPtr thisPtr, bool fullscreen);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. static extern void Internal_GetResolution(IntPtr thisPtr, out int width, out int height);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. static extern void Internal_SetResolution(IntPtr thisPtr, int width, int height);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. static extern bool Internal_GetDebug(IntPtr thisPtr);
  119. [MethodImpl(MethodImplOptions.InternalCall)]
  120. static extern void Internal_SetDebug(IntPtr thisPtr, bool fullscreen);
  121. }
  122. /// <summary>
  123. /// Supported icon sizes for Windows platform.
  124. /// </summary>
  125. public enum WinIconSizes
  126. {
  127. Icon16 = 16,
  128. Icon32 = 32,
  129. Icon48 = 48,
  130. Icon64 = 64,
  131. Icon96 = 96,
  132. Icon128 = 128,
  133. Icon196 = 196,
  134. Icon256 = 256
  135. }
  136. /// <summary>
  137. /// Platform data specific to Windows.
  138. /// </summary>
  139. public class WinPlatformInfo : PlatformInfo
  140. {
  141. /// <summary>
  142. /// Texture that will be displayed on the application's executable.
  143. /// </summary>
  144. public Texture2D Icon
  145. {
  146. get { return Internal_GetIcon(mCachedPtr); }
  147. set
  148. {
  149. IntPtr texturePtr = IntPtr.Zero;
  150. if (value != null)
  151. texturePtr = value.GetCachedPtr();
  152. Internal_SetIcon(mCachedPtr, texturePtr);
  153. }
  154. }
  155. /// <summary>
  156. /// Text that will be displayed in the application's title bar.
  157. /// </summary>
  158. public string TitleText
  159. {
  160. get { return Internal_GetTitleText(mCachedPtr); }
  161. set { Internal_SetTitleText(mCachedPtr, value); }
  162. }
  163. [MethodImpl(MethodImplOptions.InternalCall)]
  164. private static extern Texture2D Internal_GetIcon(IntPtr thisPtr);
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern void Internal_SetIcon(IntPtr thisPtr, IntPtr texturePtr);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern string Internal_GetTitleText(IntPtr thisPtr);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern void Internal_SetTitleText(IntPtr thisPtr, string value);
  171. }
  172. /// <summary>
  173. /// Handles building of the game executable and packaging of all necessary resources, making the game be ready to ran
  174. /// as a standalone product.
  175. /// </summary>
  176. public static class BuildManager
  177. {
  178. /// <summary>
  179. /// Returns a list of all available platforms that can be built for.
  180. /// </summary>
  181. public static PlatformType[] AvailablePlatforms
  182. {
  183. get { return Internal_GetAvailablePlatforms(); }
  184. }
  185. /// <summary>
  186. /// Returns the currently active platform.
  187. /// </summary>
  188. public static PlatformType ActivePlatform
  189. {
  190. get { return Internal_GetActivePlatform(); }
  191. set { Internal_SetActivePlatform(value); }
  192. }
  193. /// <summary>
  194. /// Returns the data about the currently active platform.
  195. /// </summary>
  196. public static PlatformInfo ActivePlatformInfo
  197. {
  198. get { return Internal_GetActivePlatformInfo(); }
  199. }
  200. /// <summary>
  201. /// Returns absolute path to the folder where builds for the currently active platform are output.
  202. /// </summary>
  203. public static string OutputFolder
  204. {
  205. get { return GetBuildFolder(BuildFolder.DestinationRoot, ActivePlatform); }
  206. }
  207. /// <summary>
  208. /// Returns a path to a specific folder used in the build process. See entries of BuildFolder enum for explanations
  209. /// of individual folder types.
  210. /// </summary>
  211. /// <param name="folder">Type of folder to retrieve the path for.</param>
  212. /// <param name="platform">Platform to retrieve the path for.</param>
  213. /// <returns>Path for the requested folder. This can be absolute or relative, see <see cref="BuildFolder"/> enum
  214. /// for details.</returns>
  215. private static string GetBuildFolder(BuildFolder folder, PlatformType platform)
  216. {
  217. return Internal_GetBuildFolder(folder, platform);
  218. }
  219. /// <summary>
  220. /// Returns a list of names of all native binaries required for a specific platform.
  221. /// </summary>
  222. /// <param name="platform">Platform type for which to get the binaries for.</param>
  223. /// <returns>Array of names of native binary files.</returns>
  224. private static string[] GetNativeBinaries(PlatformType platform)
  225. {
  226. return Internal_GetNativeBinaries(platform);
  227. }
  228. /// <summary>
  229. /// Builds the executable and packages the game.
  230. /// </summary>
  231. public static void Build()
  232. {
  233. PlatformType activePlatform = ActivePlatform;
  234. PlatformInfo platformInfo = ActivePlatformInfo;
  235. string srcRoot = GetBuildFolder(BuildFolder.SourceRoot, activePlatform);
  236. string destRoot = GetBuildFolder(BuildFolder.DestinationRoot, activePlatform);
  237. // Prepare clean destination folder
  238. if(Directory.Exists(destRoot))
  239. Directory.Delete(destRoot, true);
  240. Directory.CreateDirectory(destRoot);
  241. // Compile game assembly
  242. string bansheeAssemblyFolder = GetBuildFolder(BuildFolder.BansheeAssemblies, activePlatform);
  243. string srcBansheeAssemblyFolder = Path.Combine(srcRoot, bansheeAssemblyFolder);
  244. string destBansheeAssemblyFolder = Path.Combine(destRoot, bansheeAssemblyFolder);
  245. Directory.CreateDirectory(destBansheeAssemblyFolder);
  246. CompilerInstance ci = ScriptCompiler.CompileAsync(ScriptAssemblyType.Game, ActivePlatform, platformInfo.Debug, destBansheeAssemblyFolder);
  247. // Copy engine assembly
  248. // TODO - Copy Release version of engine assembly if not in debug mode, and Debug otherwise
  249. {
  250. string srcFile = Path.Combine(srcBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  251. string destFile = Path.Combine(destBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  252. File.Copy(srcFile, destFile);
  253. }
  254. // Copy builtin data
  255. string dataFolder = GetBuildFolder(BuildFolder.Data, activePlatform);
  256. string srcData = Path.Combine(srcRoot, dataFolder);
  257. string destData = Path.Combine(destRoot, dataFolder);
  258. DirectoryEx.Copy(srcData, destData);
  259. // Copy native binaries
  260. string binaryFolder = GetBuildFolder(BuildFolder.NativeBinaries, activePlatform);
  261. string srcBin = Path.Combine(srcRoot, binaryFolder);
  262. string destBin = destRoot;
  263. string[] nativeBinaries = GetNativeBinaries(activePlatform);
  264. foreach (var entry in nativeBinaries)
  265. {
  266. string srcFile = Path.Combine(srcBin, entry);
  267. string destFile = Path.Combine(destBin, entry);
  268. File.Copy(srcFile, destFile);
  269. }
  270. // Copy .NET framework assemblies
  271. string frameworkAssemblyFolder = GetBuildFolder(BuildFolder.FrameworkAssemblies, activePlatform);
  272. string srcFrameworkAssemblyFolder = Path.Combine(srcRoot, frameworkAssemblyFolder);
  273. string destFrameworkAssemblyFolder = Path.Combine(destRoot, frameworkAssemblyFolder);
  274. Directory.CreateDirectory(destFrameworkAssemblyFolder);
  275. string[] frameworkAssemblies = GetFrameworkAssemblies(activePlatform);
  276. foreach (var entry in frameworkAssemblies)
  277. {
  278. string srcFile = Path.Combine(srcFrameworkAssemblyFolder, entry + ".dll");
  279. string destFile = Path.Combine(destFrameworkAssemblyFolder, entry + ".dll");
  280. File.Copy(srcFile, destFile);
  281. }
  282. // Copy Mono
  283. string monoFolder = GetBuildFolder(BuildFolder.Mono, activePlatform);
  284. string srcMonoFolder = Path.Combine(srcRoot, monoFolder);
  285. string destMonoFolder = Path.Combine(destRoot, monoFolder);
  286. DirectoryEx.Copy(srcMonoFolder, destMonoFolder);
  287. string srcExecFile = GetMainExecutable(activePlatform);
  288. string destExecFile = Path.Combine(destBin, Path.GetFileName(srcExecFile));
  289. File.Copy(srcExecFile, destExecFile);
  290. InjectIcons(destExecFile, platformInfo);
  291. PackageResources(destRoot, platformInfo);
  292. CreateStartupSettings(destRoot, platformInfo);
  293. // Wait until compile finishes
  294. while (!ci.IsDone)
  295. Thread.Sleep(200);
  296. ci.Dispose();
  297. }
  298. /// <summary>
  299. /// Injects icons specified in <see cref="PlatformInfo"/> into an executable at the specified path.
  300. /// </summary>
  301. /// <param name="filePath">Absolute path to the executable to inject icons in.</param>
  302. /// <param name="info">Object containing references to icons to inject.</param>
  303. private static void InjectIcons(string filePath, PlatformInfo info)
  304. {
  305. IntPtr infoPtr = IntPtr.Zero;
  306. if (info != null)
  307. infoPtr = info.GetCachedPtr();
  308. Internal_InjectIcons(filePath, infoPtr);
  309. }
  310. /// <summary>
  311. /// Finds all used resources by the build and packages them into an output folder.
  312. /// </summary>
  313. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the packaged resource
  314. /// folder be placed.</param>
  315. /// <param name="info">Platform information about the current build.</param>
  316. private static void PackageResources(string buildFolder, PlatformInfo info)
  317. {
  318. IntPtr infoPtr = IntPtr.Zero;
  319. if (info != null)
  320. infoPtr = info.GetCachedPtr();
  321. Internal_PackageResources(buildFolder, infoPtr);
  322. }
  323. /// <summary>
  324. /// Creates a game settings asset that contains necessary data for starting up the game (e.g. initial scene).
  325. /// </summary>
  326. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the settings assets
  327. /// will be output.</param>
  328. /// <param name="info">Platform information about the current build.</param>
  329. private static void CreateStartupSettings(string buildFolder, PlatformInfo info)
  330. {
  331. IntPtr infoPtr = IntPtr.Zero;
  332. if (info != null)
  333. infoPtr = info.GetCachedPtr();
  334. Internal_CreateStartupSettings(buildFolder, infoPtr);
  335. }
  336. /// <summary>
  337. /// Returns a list of .NET framework managed assemblies (without extension) to be included for the specified platform.
  338. /// </summary>
  339. /// <param name="type">Platform type to retrieve the list of assemblies for.</param>
  340. /// <returns>A list of .NET framework managed assemblies (without extension) that will be included with the build.</returns>
  341. internal static string[] GetFrameworkAssemblies(PlatformType type)
  342. {
  343. return Internal_GetFrameworkAssemblies(type);
  344. }
  345. /// <summary>
  346. /// Returns the absolute path to the executable for the provided platform.
  347. /// </summary>
  348. /// <param name="type">Platform type to retrieve the executable location for.</param>
  349. /// <returns>Absolute path to the executable.</returns>
  350. internal static string GetMainExecutable(PlatformType type)
  351. {
  352. return Internal_GetMainExecutable(type);
  353. }
  354. /// <summary>
  355. /// Returns a list of semicolon separated defines that will be used when compiling scripts for the specified
  356. /// platform.
  357. /// </summary>
  358. /// <param name="type">Platfrom type to retrieve the defines for.</param>
  359. /// <returns>Semicolor separated defines that will be passed along to the script compiler.</returns>
  360. internal static string GetDefines(PlatformType type)
  361. {
  362. return Internal_GetDefines(type);
  363. }
  364. /// <summary>
  365. /// Returns an object containing all platform specific build data.
  366. /// </summary>
  367. /// <param name="type">Platform type to retrieve the data for.</param>
  368. /// <returns>An object containing all platform specific build data</returns>
  369. internal static PlatformInfo GetPlatformInfo(PlatformType type)
  370. {
  371. return Internal_GetPlatformInfo(type);
  372. }
  373. /// <summary>
  374. /// Types of various folders used by the build manager.
  375. /// </summary>
  376. private enum BuildFolder // Note: Must match C++ enum ScriptBuildFolder
  377. {
  378. /// <summary>Absolute path to the root folder where all the prebuilt binaries and data exist.</summary>
  379. SourceRoot,
  380. /// <summary>Absolute path to the root folder for a build for a specific platform.</summary>
  381. DestinationRoot,
  382. /// <summary>Folder where native binaries are stored. Relative to root.</summary>
  383. NativeBinaries,
  384. /// <summary>Folder where Banshee specific assemblies are stored. Relative to root.</summary>
  385. BansheeAssemblies,
  386. /// <summary>Folder where .NET framework assemblies are stored. Relative to root.</summary>
  387. FrameworkAssemblies,
  388. /// <summary>Folder where miscelaneous Mono files are stored. Relative to root.</summary>
  389. Mono,
  390. /// <summary>Folder where builtin data is stored. Relative to root.</summary>
  391. Data
  392. }
  393. [MethodImpl(MethodImplOptions.InternalCall)]
  394. private static extern PlatformType[] Internal_GetAvailablePlatforms();
  395. [MethodImpl(MethodImplOptions.InternalCall)]
  396. private static extern PlatformType Internal_GetActivePlatform();
  397. [MethodImpl(MethodImplOptions.InternalCall)]
  398. private static extern void Internal_SetActivePlatform(PlatformType value);
  399. [MethodImpl(MethodImplOptions.InternalCall)]
  400. private static extern PlatformInfo Internal_GetActivePlatformInfo();
  401. [MethodImpl(MethodImplOptions.InternalCall)]
  402. private static extern PlatformInfo Internal_GetPlatformInfo(PlatformType type);
  403. [MethodImpl(MethodImplOptions.InternalCall)]
  404. private static extern string[] Internal_GetFrameworkAssemblies(PlatformType type);
  405. [MethodImpl(MethodImplOptions.InternalCall)]
  406. private static extern string Internal_GetMainExecutable(PlatformType type);
  407. [MethodImpl(MethodImplOptions.InternalCall)]
  408. private static extern string Internal_GetDefines(PlatformType type);
  409. [MethodImpl(MethodImplOptions.InternalCall)]
  410. private static extern string[] Internal_GetNativeBinaries(PlatformType type);
  411. [MethodImpl(MethodImplOptions.InternalCall)]
  412. private static extern string Internal_GetBuildFolder(BuildFolder folder, PlatformType platform);
  413. [MethodImpl(MethodImplOptions.InternalCall)]
  414. private static extern void Internal_InjectIcons(string filePath, IntPtr info);
  415. [MethodImpl(MethodImplOptions.InternalCall)]
  416. private static extern void Internal_PackageResources(string buildFolder, IntPtr info);
  417. [MethodImpl(MethodImplOptions.InternalCall)]
  418. private static extern void Internal_CreateStartupSettings(string buildFolder, IntPtr info);
  419. }
  420. }