BuildManager.cs 21 KB

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