BuildManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 ResourceRef<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 ResourceRef<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 ResourceRef<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 ResourceRef<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;
  243. if(platformInfo.Debug)
  244. bansheeAssemblyFolder = GetBuildFolder(BuildFolder.BansheeDebugAssemblies, activePlatform);
  245. else
  246. bansheeAssemblyFolder = GetBuildFolder(BuildFolder.BansheeReleaseAssemblies, activePlatform);
  247. string srcBansheeAssemblyFolder = Path.Combine(srcRoot, bansheeAssemblyFolder);
  248. string destBansheeAssemblyFolder = Path.Combine(destRoot, bansheeAssemblyFolder);
  249. Directory.CreateDirectory(destBansheeAssemblyFolder);
  250. CompilerInstance ci = ScriptCompiler.CompileAsync(ScriptAssemblyType.Game, ActivePlatform, platformInfo.Debug, destBansheeAssemblyFolder);
  251. // Copy engine assembly
  252. {
  253. string srcFile = Path.Combine(srcBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  254. string destFile = Path.Combine(destBansheeAssemblyFolder, EditorApplication.EngineAssemblyName);
  255. File.Copy(srcFile, destFile);
  256. }
  257. // Copy builtin data
  258. string dataFolder = GetBuildFolder(BuildFolder.Data, activePlatform);
  259. string srcData = Path.Combine(srcRoot, dataFolder);
  260. string destData = Path.Combine(destRoot, dataFolder);
  261. DirectoryEx.Copy(srcData, destData);
  262. // Copy native binaries
  263. string binaryFolder = GetBuildFolder(BuildFolder.NativeBinaries, activePlatform);
  264. string srcBin = Path.Combine(srcRoot, binaryFolder);
  265. string destBin = destRoot;
  266. string[] nativeBinaries = GetNativeBinaries(activePlatform);
  267. foreach (var entry in nativeBinaries)
  268. {
  269. string srcFile = Path.Combine(srcBin, entry);
  270. string destFile = Path.Combine(destBin, entry);
  271. File.Copy(srcFile, destFile);
  272. }
  273. // Copy .NET framework assemblies
  274. string frameworkAssemblyFolder = GetBuildFolder(BuildFolder.FrameworkAssemblies, activePlatform);
  275. string srcFrameworkAssemblyFolder = Path.Combine(srcRoot, frameworkAssemblyFolder);
  276. string destFrameworkAssemblyFolder = Path.Combine(destRoot, frameworkAssemblyFolder);
  277. Directory.CreateDirectory(destFrameworkAssemblyFolder);
  278. string[] frameworkAssemblies = GetFrameworkAssemblies(activePlatform);
  279. foreach (var entry in frameworkAssemblies)
  280. {
  281. string srcFile = Path.Combine(srcFrameworkAssemblyFolder, entry + ".dll");
  282. string destFile = Path.Combine(destFrameworkAssemblyFolder, entry + ".dll");
  283. File.Copy(srcFile, destFile);
  284. }
  285. // Copy Mono
  286. string monoFolder = GetBuildFolder(BuildFolder.Mono, activePlatform);
  287. string srcMonoFolder = Path.Combine(srcRoot, monoFolder);
  288. string destMonoFolder = Path.Combine(destRoot, monoFolder);
  289. DirectoryEx.Copy(srcMonoFolder, destMonoFolder);
  290. string srcExecFile = GetMainExecutable(activePlatform);
  291. string destExecFile = Path.Combine(destBin, Path.GetFileName(srcExecFile));
  292. File.Copy(srcExecFile, destExecFile);
  293. InjectIcons(destExecFile, platformInfo);
  294. PackageResources(destRoot, platformInfo);
  295. CreateStartupSettings(destRoot, platformInfo);
  296. // Wait until compile finishes
  297. while (!ci.IsDone)
  298. Thread.Sleep(200);
  299. ci.Dispose();
  300. }
  301. /// <summary>
  302. /// Injects icons specified in <see cref="PlatformInfo"/> into an executable at the specified path.
  303. /// </summary>
  304. /// <param name="filePath">Absolute path to the executable to inject icons in.</param>
  305. /// <param name="info">Object containing references to icons to inject.</param>
  306. private static void InjectIcons(string filePath, PlatformInfo info)
  307. {
  308. IntPtr infoPtr = IntPtr.Zero;
  309. if (info != null)
  310. infoPtr = info.GetCachedPtr();
  311. Internal_InjectIcons(filePath, infoPtr);
  312. }
  313. /// <summary>
  314. /// Finds all used resources by the build and packages them into an output folder.
  315. /// </summary>
  316. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the packaged resource
  317. /// folder be placed.</param>
  318. /// <param name="info">Platform information about the current build.</param>
  319. private static void PackageResources(string buildFolder, PlatformInfo info)
  320. {
  321. IntPtr infoPtr = IntPtr.Zero;
  322. if (info != null)
  323. infoPtr = info.GetCachedPtr();
  324. Internal_PackageResources(buildFolder, infoPtr);
  325. }
  326. /// <summary>
  327. /// Creates a game settings asset that contains necessary data for starting up the game (e.g. initial scene).
  328. /// </summary>
  329. /// <param name="buildFolder">Absolute path to the root folder of the build. This is where the settings assets
  330. /// will be output.</param>
  331. /// <param name="info">Platform information about the current build.</param>
  332. private static void CreateStartupSettings(string buildFolder, PlatformInfo info)
  333. {
  334. IntPtr infoPtr = IntPtr.Zero;
  335. if (info != null)
  336. infoPtr = info.GetCachedPtr();
  337. Internal_CreateStartupSettings(buildFolder, infoPtr);
  338. }
  339. /// <summary>
  340. /// Returns a list of .NET framework managed assemblies (without extension) to be included for the specified platform.
  341. /// </summary>
  342. /// <param name="type">Platform type to retrieve the list of assemblies for.</param>
  343. /// <returns>A list of .NET framework managed assemblies (without extension) that will be included with the build.</returns>
  344. internal static string[] GetFrameworkAssemblies(PlatformType type)
  345. {
  346. return Internal_GetFrameworkAssemblies(type);
  347. }
  348. /// <summary>
  349. /// Returns the absolute path to the executable for the provided platform.
  350. /// </summary>
  351. /// <param name="type">Platform type to retrieve the executable location for.</param>
  352. /// <returns>Absolute path to the executable.</returns>
  353. internal static string GetMainExecutable(PlatformType type)
  354. {
  355. return Internal_GetMainExecutable(type);
  356. }
  357. /// <summary>
  358. /// Returns a list of semicolon separated defines that will be used when compiling scripts for the specified
  359. /// platform.
  360. /// </summary>
  361. /// <param name="type">Platfrom type to retrieve the defines for.</param>
  362. /// <returns>Semicolor separated defines that will be passed along to the script compiler.</returns>
  363. internal static string GetDefines(PlatformType type)
  364. {
  365. return Internal_GetDefines(type);
  366. }
  367. /// <summary>
  368. /// Returns an object containing all platform specific build data.
  369. /// </summary>
  370. /// <param name="type">Platform type to retrieve the data for.</param>
  371. /// <returns>An object containing all platform specific build data</returns>
  372. internal static PlatformInfo GetPlatformInfo(PlatformType type)
  373. {
  374. return Internal_GetPlatformInfo(type);
  375. }
  376. /// <summary>
  377. /// Types of various folders used by the build manager.
  378. /// </summary>
  379. private enum BuildFolder // Note: Must match C++ enum ScriptBuildFolder
  380. {
  381. /// <summary>Absolute path to the root folder where all the prebuilt binaries and data exist.</summary>
  382. SourceRoot,
  383. /// <summary>Absolute path to the root folder for a build for a specific platform.</summary>
  384. DestinationRoot,
  385. /// <summary>Folder where native binaries are stored. Relative to root.</summary>
  386. NativeBinaries,
  387. /// <summary>Folder where Banshee specific debug assemblies are stored. Relative to root.</summary>
  388. BansheeDebugAssemblies,
  389. /// <summary>Folder where Banshee specific release assemblies are stored. Relative to root.</summary>
  390. BansheeReleaseAssemblies,
  391. /// <summary>Folder where .NET framework assemblies are stored. Relative to root.</summary>
  392. FrameworkAssemblies,
  393. /// <summary>Folder where miscelaneous Mono files are stored. Relative to root.</summary>
  394. Mono,
  395. /// <summary>Folder where builtin data is stored. Relative to root.</summary>
  396. Data
  397. }
  398. [MethodImpl(MethodImplOptions.InternalCall)]
  399. private static extern PlatformType[] Internal_GetAvailablePlatforms();
  400. [MethodImpl(MethodImplOptions.InternalCall)]
  401. private static extern PlatformType Internal_GetActivePlatform();
  402. [MethodImpl(MethodImplOptions.InternalCall)]
  403. private static extern void Internal_SetActivePlatform(PlatformType value);
  404. [MethodImpl(MethodImplOptions.InternalCall)]
  405. private static extern PlatformInfo Internal_GetActivePlatformInfo();
  406. [MethodImpl(MethodImplOptions.InternalCall)]
  407. private static extern PlatformInfo Internal_GetPlatformInfo(PlatformType type);
  408. [MethodImpl(MethodImplOptions.InternalCall)]
  409. private static extern string[] Internal_GetFrameworkAssemblies(PlatformType type);
  410. [MethodImpl(MethodImplOptions.InternalCall)]
  411. private static extern string Internal_GetMainExecutable(PlatformType type);
  412. [MethodImpl(MethodImplOptions.InternalCall)]
  413. private static extern string Internal_GetDefines(PlatformType type);
  414. [MethodImpl(MethodImplOptions.InternalCall)]
  415. private static extern string[] Internal_GetNativeBinaries(PlatformType type);
  416. [MethodImpl(MethodImplOptions.InternalCall)]
  417. private static extern string Internal_GetBuildFolder(BuildFolder folder, PlatformType platform);
  418. [MethodImpl(MethodImplOptions.InternalCall)]
  419. private static extern void Internal_InjectIcons(string filePath, IntPtr info);
  420. [MethodImpl(MethodImplOptions.InternalCall)]
  421. private static extern void Internal_PackageResources(string buildFolder, IntPtr info);
  422. [MethodImpl(MethodImplOptions.InternalCall)]
  423. private static extern void Internal_CreateStartupSettings(string buildFolder, IntPtr info);
  424. }
  425. }