BuildManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 in the taskbar when the application is running.
  143. /// </summary>
  144. public Texture2D TaskbarIcon
  145. {
  146. get { return Internal_GetTaskbarIcon(mCachedPtr); }
  147. set
  148. {
  149. IntPtr texturePtr = IntPtr.Zero;
  150. if (value != null)
  151. texturePtr = value.GetCachedPtr();
  152. Internal_SetTaskbarIcon(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. /// <summary>
  164. /// Returns a texture of a specific icon size that will be added to the executable.
  165. /// </summary>
  166. /// <param name="size">Type of icon to retrieve the texture for.</param>
  167. /// <returns>Texture for the specified icon size.</returns>
  168. public Texture2D GetIcon(WinIconSizes size)
  169. {
  170. return Internal_GetIcon(mCachedPtr, (int)size);
  171. }
  172. /// <summary>
  173. /// Sets a texture of a specific icon size that will be added to the executable.
  174. /// </summary>
  175. /// <param name="size">Type of icon to set the texture for.</param>
  176. /// <param name="texture">Texture for the specified icon size.</param>
  177. public void SetIcon(WinIconSizes size, Texture2D texture)
  178. {
  179. IntPtr texturePtr = IntPtr.Zero;
  180. if (texture != null)
  181. texturePtr = texture.GetCachedPtr();
  182. Internal_SetIcon(mCachedPtr, (int)size, texturePtr);
  183. }
  184. [MethodImpl(MethodImplOptions.InternalCall)]
  185. private static extern Texture2D Internal_GetIcon(IntPtr thisPtr, int size);
  186. [MethodImpl(MethodImplOptions.InternalCall)]
  187. private static extern void Internal_SetIcon(IntPtr thisPtr, int size, IntPtr texturePtr);
  188. [MethodImpl(MethodImplOptions.InternalCall)]
  189. private static extern Texture2D Internal_GetTaskbarIcon(IntPtr thisPtr);
  190. [MethodImpl(MethodImplOptions.InternalCall)]
  191. private static extern void Internal_SetTaskbarIcon(IntPtr thisPtr, IntPtr texturePtr);
  192. [MethodImpl(MethodImplOptions.InternalCall)]
  193. private static extern string Internal_GetTitleText(IntPtr thisPtr);
  194. [MethodImpl(MethodImplOptions.InternalCall)]
  195. private static extern void Internal_SetTitleText(IntPtr thisPtr, string value);
  196. }
  197. /// <summary>
  198. /// Handles building of the game executable and packaging of all necessary resources, making the game be ready to ran
  199. /// as a standalone product.
  200. /// </summary>
  201. public static class BuildManager
  202. {
  203. /// <summary>
  204. /// Returns a list of all available platforms that can be built for.
  205. /// </summary>
  206. public static PlatformType[] AvailablePlatforms
  207. {
  208. get { return Internal_GetAvailablePlatforms(); }
  209. }
  210. /// <summary>
  211. /// Returns the currently active platform.
  212. /// </summary>
  213. public static PlatformType ActivePlatform
  214. {
  215. get { return Internal_GetActivePlatform(); }
  216. set { Internal_SetActivePlatform(value); }
  217. }
  218. /// <summary>
  219. /// Returns the data about the currently active platform.
  220. /// </summary>
  221. public static PlatformInfo ActivePlatformInfo
  222. {
  223. get { return Internal_GetActivePlatformInfo(); }
  224. }
  225. /// <summary>
  226. /// Returns a path to a specific folder used in the build process. See entries of BuildFolder enum for explanations
  227. /// of individual folder types.
  228. /// </summary>
  229. /// <param name="folder">Type of folder to retrieve the path for.</param>
  230. /// <param name="platform">Platform to retrieve the path for.</param>
  231. /// <returns>Path for the requested folder. This can be absolute or relative, see <see cref="BuildFolder"/> enum
  232. /// for details.</returns>
  233. private static string GetBuildFolder(BuildFolder folder, PlatformType platform)
  234. {
  235. return Internal_GetBuildFolder(folder, platform);
  236. }
  237. /// <summary>
  238. /// Returns a list of names of all native binaries required for a specific platform.
  239. /// </summary>
  240. /// <param name="platform">Platform type for which to get the binaries for.</param>
  241. /// <returns>Array of names of native binary files.</returns>
  242. private static string[] GetNativeBinaries(PlatformType platform)
  243. {
  244. return Internal_GetNativeBinaries(platform);
  245. }
  246. /// <summary>
  247. /// Builds the executable and packages the game.
  248. /// </summary>
  249. public static void Build()
  250. {
  251. PlatformType activePlatform = ActivePlatform;
  252. PlatformInfo platformInfo = ActivePlatformInfo;
  253. string srcRoot = GetBuildFolder(BuildFolder.SourceRoot, activePlatform);
  254. string destRoot = GetBuildFolder(BuildFolder.DestinationRoot, activePlatform);
  255. // Prepare clean destination folder
  256. Directory.Delete(destRoot, true);
  257. Directory.CreateDirectory(destRoot);
  258. // Compile game assembly
  259. string bansheeAssemblyFolder = GetBuildFolder(BuildFolder.BansheeAssemblies, activePlatform);
  260. string srcBansheeAssemblyFolder = Path.Combine(srcRoot, bansheeAssemblyFolder);
  261. string destBansheeAssemblyFolder = Path.Combine(destRoot, bansheeAssemblyFolder);
  262. Directory.CreateDirectory(destBansheeAssemblyFolder);
  263. CompilerInstance ci = ScriptCompiler.CompileAsync(ScriptAssemblyType.Game, ActivePlatform, platformInfo.Debug, destBansheeAssemblyFolder);
  264. // Copy engine assembly
  265. // TODO - Copy Release version of engine assembly if not in debug mode, and Debug otherwise
  266. {
  267. string srcFile = Path.Combine(srcBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  268. string destFile = Path.Combine(destBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  269. File.Copy(srcFile, destFile);
  270. }
  271. // Copy builtin data
  272. string dataFolder = GetBuildFolder(BuildFolder.Data, activePlatform);
  273. string srcData = Path.Combine(srcRoot, dataFolder);
  274. string destData = Path.Combine(destRoot, dataFolder);
  275. DirectoryEx.Copy(srcData, destData);
  276. // Copy native binaries
  277. string binaryFolder = GetBuildFolder(BuildFolder.NativeBinaries, activePlatform);
  278. string srcBin = Path.Combine(srcRoot, binaryFolder);
  279. string destBin = Path.Combine(destRoot, binaryFolder);
  280. Directory.CreateDirectory(destBin);
  281. string[] nativeBinaries = GetNativeBinaries(activePlatform);
  282. foreach (var entry in nativeBinaries)
  283. {
  284. string srcFile = Path.Combine(srcBin, entry);
  285. string destFile = Path.Combine(destBin, entry);
  286. File.Copy(srcFile, destFile);
  287. }
  288. // Copy .NET framework assemblies
  289. string frameworkAssemblyFolder = GetBuildFolder(BuildFolder.FrameworkAssemblies, activePlatform);
  290. string srcFrameworkAssemblyFolder = Path.Combine(srcRoot, frameworkAssemblyFolder);
  291. string destFrameworkAssemblyFolder = Path.Combine(destRoot, frameworkAssemblyFolder);
  292. Directory.CreateDirectory(destFrameworkAssemblyFolder);
  293. string[] frameworkAssemblies = GetFrameworkAssemblies(activePlatform);
  294. foreach (var entry in frameworkAssemblies)
  295. {
  296. string srcFile = Path.Combine(srcFrameworkAssemblyFolder, entry + ".dll");
  297. string destFile = Path.Combine(destFrameworkAssemblyFolder, entry + ".dll");
  298. File.Copy(srcFile, destFile);
  299. }
  300. // Copy Mono
  301. string monoFolder = GetBuildFolder(BuildFolder.Mono, activePlatform);
  302. string srcMonoFolder = Path.Combine(srcRoot, monoFolder);
  303. string destMonoFolder = Path.Combine(destRoot, monoFolder);
  304. DirectoryEx.Copy(srcMonoFolder, destMonoFolder);
  305. string srcExecFile = GetMainExecutable(activePlatform);
  306. string destExecFile = Path.Combine(destBin, Path.GetFileName(srcExecFile));
  307. File.Copy(srcExecFile, destExecFile);
  308. InjectIcons(destExecFile, platformInfo);
  309. PackageResources(destRoot, platformInfo);
  310. CreateStartupSettings(destRoot, platformInfo);
  311. // Wait until compile finishes
  312. while (!ci.IsDone)
  313. Thread.Sleep(200);
  314. ci.Dispose();
  315. }
  316. /// <summary>
  317. /// Injects icons specified in <see cref="PlatformInfo"/> into an executable at the specified path.
  318. /// </summary>
  319. /// <param name="filePath">Absolute path to the executable to inject icons in.</param>
  320. /// <param name="info">Object containing references to icons to inject.</param>
  321. private static void InjectIcons(string filePath, PlatformInfo info)
  322. {
  323. IntPtr infoPtr = IntPtr.Zero;
  324. if (info != null)
  325. infoPtr = info.GetCachedPtr();
  326. Internal_InjectIcons(filePath, infoPtr);
  327. }
  328. /// <summary>
  329. /// Finds all used resources by the build and packages them into an output folder.
  330. /// </summary>
  331. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the packaged resource
  332. /// folder be placed.</param>
  333. /// <param name="info">Platform information about the current build.</param>
  334. private static void PackageResources(string buildFolder, PlatformInfo info)
  335. {
  336. IntPtr infoPtr = IntPtr.Zero;
  337. if (info != null)
  338. infoPtr = info.GetCachedPtr();
  339. Internal_PackageResources(buildFolder, infoPtr);
  340. }
  341. /// <summary>
  342. /// Creates a game settings asset that contains necessary data for starting up the game (e.g. initial scene).
  343. /// </summary>
  344. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the settings assets
  345. /// will be output.</param>
  346. /// <param name="info">Platform information about the current build.</param>
  347. private static void CreateStartupSettings(string buildFolder, PlatformInfo info)
  348. {
  349. IntPtr infoPtr = IntPtr.Zero;
  350. if (info != null)
  351. infoPtr = info.GetCachedPtr();
  352. Internal_CreateStartupSettings(buildFolder, infoPtr);
  353. }
  354. /// <summary>
  355. /// Returns a list of .NET framework managed assemblies (without extension) to be included for the specified platform.
  356. /// </summary>
  357. /// <param name="type">Platform type to retrieve the list of assemblies for.</param>
  358. /// <returns>A list of .NET framework managed assemblies (without extension) that will be included with the build.</returns>
  359. internal static string[] GetFrameworkAssemblies(PlatformType type)
  360. {
  361. return Internal_GetFrameworkAssemblies(type);
  362. }
  363. /// <summary>
  364. /// Returns the absolute path to the executable for the provided platform.
  365. /// </summary>
  366. /// <param name="type">Platform type to retrieve the executable location for.</param>
  367. /// <returns>Absolute path to the executable.</returns>
  368. internal static string GetMainExecutable(PlatformType type)
  369. {
  370. return Internal_GetMainExecutable(type);
  371. }
  372. /// <summary>
  373. /// Returns a list of semicolon separated defines that will be used when compiling scripts for the specified
  374. /// platform.
  375. /// </summary>
  376. /// <param name="type">Platfrom type to retrieve the defines for.</param>
  377. /// <returns>Semicolor separated defines that will be passed along to the script compiler.</returns>
  378. internal static string GetDefines(PlatformType type)
  379. {
  380. return Internal_GetDefines(type);
  381. }
  382. /// <summary>
  383. /// Returns an object containing all platform specific build data.
  384. /// </summary>
  385. /// <param name="type">Platform type to retrieve the data for.</param>
  386. /// <returns>An object containing all platform specific build data</returns>
  387. internal static PlatformInfo GetPlatformInfo(PlatformType type)
  388. {
  389. return Internal_GetPlatformInfo(type);
  390. }
  391. /// <summary>
  392. /// Types of various folders used by the build manager.
  393. /// </summary>
  394. private enum BuildFolder // Note: Must match C++ enum ScriptBuildFolder
  395. {
  396. /// <summary>Absolute path to the root folder where all the prebuilt binaries and data exist.</summary>
  397. SourceRoot,
  398. /// <summary>Absolute path to the root folder for a build for a specific platform.</summary>
  399. DestinationRoot,
  400. /// <summary>Folder where native binaries are stored. Relative to root.</summary>
  401. NativeBinaries,
  402. /// <summary>Folder where Banshee specific assemblies are stored. Relative to root.</summary>
  403. BansheeAssemblies,
  404. /// <summary>Folder where .NET framework assemblies are stored. Relative to root.</summary>
  405. FrameworkAssemblies,
  406. /// <summary>Folder where miscelaneous Mono files are stored. Relative to root.</summary>
  407. Mono,
  408. /// <summary>Folder where builtin data is stored. Relative to root.</summary>
  409. Data
  410. }
  411. [MethodImpl(MethodImplOptions.InternalCall)]
  412. private static extern PlatformType[] Internal_GetAvailablePlatforms();
  413. [MethodImpl(MethodImplOptions.InternalCall)]
  414. private static extern PlatformType Internal_GetActivePlatform();
  415. [MethodImpl(MethodImplOptions.InternalCall)]
  416. private static extern void Internal_SetActivePlatform(PlatformType value);
  417. [MethodImpl(MethodImplOptions.InternalCall)]
  418. private static extern PlatformInfo Internal_GetActivePlatformInfo();
  419. [MethodImpl(MethodImplOptions.InternalCall)]
  420. private static extern PlatformInfo Internal_GetPlatformInfo(PlatformType type);
  421. [MethodImpl(MethodImplOptions.InternalCall)]
  422. private static extern string[] Internal_GetFrameworkAssemblies(PlatformType type);
  423. [MethodImpl(MethodImplOptions.InternalCall)]
  424. private static extern string Internal_GetMainExecutable(PlatformType type);
  425. [MethodImpl(MethodImplOptions.InternalCall)]
  426. private static extern string Internal_GetDefines(PlatformType type);
  427. [MethodImpl(MethodImplOptions.InternalCall)]
  428. private static extern string[] Internal_GetNativeBinaries(PlatformType type);
  429. [MethodImpl(MethodImplOptions.InternalCall)]
  430. private static extern string Internal_GetBuildFolder(BuildFolder folder, PlatformType platform);
  431. [MethodImpl(MethodImplOptions.InternalCall)]
  432. private static extern void Internal_InjectIcons(string filePath, IntPtr info);
  433. [MethodImpl(MethodImplOptions.InternalCall)]
  434. private static extern void Internal_PackageResources(string buildFolder, IntPtr info);
  435. [MethodImpl(MethodImplOptions.InternalCall)]
  436. private static extern void Internal_CreateStartupSettings(string buildFolder, IntPtr info);
  437. }
  438. }