BsScriptBuildManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/BsScriptBuildManager.h"
  4. #include "BsMonoManager.h"
  5. #include "BsMonoClass.h"
  6. #include "Image/BsTexture.h"
  7. #include "BsMonoUtil.h"
  8. #include "Wrappers/BsScriptPlatformInfo.h"
  9. #include "FileSystem/BsFileSystem.h"
  10. #include "Utility/BsIconUtility.h"
  11. #include "CoreThread/BsCoreThread.h"
  12. #include "Utility/BsGameSettings.h"
  13. #include "Serialization/BsFileSerializer.h"
  14. #include "Library/BsProjectLibrary.h"
  15. #include "Library/BsProjectResourceMeta.h"
  16. #include "Resources/BsResources.h"
  17. #include "Scene/BsPrefab.h"
  18. #include "BsEditorApplication.h"
  19. #include "Resources/BsResourceManifest.h"
  20. #include "Resources/BsBuiltinResources.h"
  21. #include "Scene/BsSceneObject.h"
  22. #include "Debug/BsDebug.h"
  23. #include "Resources/BsGameResourceManager.h"
  24. namespace bs
  25. {
  26. ScriptBuildManager::ScriptBuildManager(MonoObject* instance)
  27. :ScriptObject(instance)
  28. { }
  29. void ScriptBuildManager::initRuntimeData()
  30. {
  31. metaData.scriptClass->addInternalCall("Internal_GetAvailablePlatforms", (void*)&ScriptBuildManager::internal_GetAvailablePlatforms);
  32. metaData.scriptClass->addInternalCall("Internal_GetActivePlatform", (void*)&ScriptBuildManager::internal_GetActivePlatform);
  33. metaData.scriptClass->addInternalCall("Internal_SetActivePlatform", (void*)&ScriptBuildManager::internal_SetActivePlatform);
  34. metaData.scriptClass->addInternalCall("Internal_GetActivePlatformInfo", (void*)&ScriptBuildManager::internal_GetActivePlatformInfo);
  35. metaData.scriptClass->addInternalCall("Internal_GetPlatformInfo", (void*)&ScriptBuildManager::internal_GetPlatformInfo);
  36. metaData.scriptClass->addInternalCall("Internal_GetFrameworkAssemblies", (void*)&ScriptBuildManager::internal_GetFrameworkAssemblies);
  37. metaData.scriptClass->addInternalCall("Internal_GetMainExecutable", (void*)&ScriptBuildManager::internal_GetMainExecutable);
  38. metaData.scriptClass->addInternalCall("Internal_GetDefines", (void*)&ScriptBuildManager::internal_GetDefines);
  39. metaData.scriptClass->addInternalCall("Internal_GetNativeBinaries", (void*)&ScriptBuildManager::internal_GetNativeBinaries);
  40. metaData.scriptClass->addInternalCall("Internal_GetBuildFolder", (void*)&ScriptBuildManager::internal_GetBuildFolder);
  41. metaData.scriptClass->addInternalCall("Internal_InjectIcons", (void*)&ScriptBuildManager::internal_InjectIcons);
  42. metaData.scriptClass->addInternalCall("Internal_PackageResources", (void*)&ScriptBuildManager::internal_PackageResources);
  43. metaData.scriptClass->addInternalCall("Internal_CreateStartupSettings", (void*)&ScriptBuildManager::internal_CreateStartupSettings);
  44. }
  45. MonoArray* ScriptBuildManager::internal_GetAvailablePlatforms()
  46. {
  47. const Vector<PlatformType>& availableType = BuildManager::instance().getAvailablePlatforms();
  48. ScriptArray outArray = ScriptArray::create<UINT32>((UINT32)availableType.size());
  49. UINT32 idx = 0;
  50. for (auto& type : availableType)
  51. outArray.set(idx++, type);
  52. return outArray.getInternal();
  53. }
  54. PlatformType ScriptBuildManager::internal_GetActivePlatform()
  55. {
  56. return BuildManager::instance().getActivePlatform();
  57. }
  58. void ScriptBuildManager::internal_SetActivePlatform(PlatformType value)
  59. {
  60. BuildManager::instance().setActivePlatform(value);
  61. }
  62. MonoObject* ScriptBuildManager::internal_GetActivePlatformInfo()
  63. {
  64. return ScriptPlatformInfo::create(BuildManager::instance().getActivePlatformInfo());
  65. }
  66. MonoObject* ScriptBuildManager::internal_GetPlatformInfo(PlatformType type)
  67. {
  68. return ScriptPlatformInfo::create(BuildManager::instance().getPlatformInfo(type));
  69. }
  70. MonoArray* ScriptBuildManager::internal_GetFrameworkAssemblies(PlatformType type)
  71. {
  72. Vector<String> frameworkAssemblies = BuildManager::instance().getFrameworkAssemblies(type);
  73. ScriptArray outArray = ScriptArray::create<String>((UINT32)frameworkAssemblies.size());
  74. UINT32 idx = 0;
  75. for (auto& assemblyName : frameworkAssemblies)
  76. outArray.set(idx++, MonoUtil::stringToMono(assemblyName));
  77. return outArray.getInternal();
  78. }
  79. MonoString* ScriptBuildManager::internal_GetMainExecutable(PlatformType type)
  80. {
  81. return MonoUtil::stringToMono(BuildManager::instance().getMainExecutable(type).toString());
  82. }
  83. MonoString* ScriptBuildManager::internal_GetDefines(PlatformType type)
  84. {
  85. return MonoUtil::stringToMono(BuildManager::instance().getDefines(type));
  86. }
  87. MonoArray* ScriptBuildManager::internal_GetNativeBinaries(PlatformType type)
  88. {
  89. Vector<Path> paths = BuildManager::instance().getNativeBinaries(type);
  90. UINT32 numEntries = (UINT32)paths.size();
  91. ScriptArray outArray = ScriptArray::create<String>(numEntries);
  92. for (UINT32 i = 0; i < numEntries; i++)
  93. {
  94. outArray.set(i, MonoUtil::stringToMono(paths[i].toString()));
  95. }
  96. return outArray.getInternal();
  97. }
  98. MonoString* ScriptBuildManager::internal_GetBuildFolder(ScriptBuildFolder folder, PlatformType platform)
  99. {
  100. Path path;
  101. if (folder == ScriptBuildFolder::FrameworkAssemblies)
  102. {
  103. Path assemblyFolder = MonoManager::instance().getFrameworkAssembliesFolder();
  104. Path sourceFolder = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platform);
  105. path = assemblyFolder.makeRelative(sourceFolder);
  106. }
  107. else if (folder == ScriptBuildFolder::Mono)
  108. {
  109. Path monoEtcFolder = MonoManager::instance().getMonoEtcFolder();
  110. Path sourceFolder = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platform);
  111. path = monoEtcFolder.makeRelative(sourceFolder);
  112. }
  113. else
  114. {
  115. BuildFolder nativeFolderType = BuildFolder::SourceRoot;
  116. switch (folder)
  117. {
  118. case ScriptBuildFolder::SourceRoot:
  119. nativeFolderType = BuildFolder::SourceRoot;
  120. break;
  121. case ScriptBuildFolder::DestinationRoot:
  122. nativeFolderType = BuildFolder::DestinationRoot;
  123. break;
  124. case ScriptBuildFolder::NativeBinaries:
  125. nativeFolderType = BuildFolder::NativeBinaries;
  126. break;
  127. case ScriptBuildFolder::BansheeDebugAssemblies:
  128. nativeFolderType = BuildFolder::BansheeDebugAssemblies;
  129. break;
  130. case ScriptBuildFolder::BansheeReleaseAssemblies:
  131. nativeFolderType = BuildFolder::BansheeReleaseAssemblies;
  132. break;
  133. case ScriptBuildFolder::Data:
  134. nativeFolderType = BuildFolder::Data;
  135. break;
  136. default:
  137. break;
  138. }
  139. path = BuildManager::instance().getBuildFolder(nativeFolderType, platform);
  140. }
  141. return MonoUtil::stringToMono(path.toString());
  142. }
  143. void ScriptBuildManager::internal_InjectIcons(MonoString* filePath, ScriptPlatformInfo* info)
  144. {
  145. if (info == nullptr)
  146. return;
  147. Path executablePath = MonoUtil::monoToString(filePath);
  148. Map<UINT32, SPtr<PixelData>> icons;
  149. SPtr<PlatformInfo> platformInfo = info->getPlatformInfo();
  150. switch (platformInfo->type)
  151. {
  152. case PlatformType::Windows:
  153. {
  154. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  155. struct IconData
  156. {
  157. UINT32 size;
  158. SPtr<PixelData> pixels;
  159. };
  160. IconData textures[] =
  161. {
  162. { 16, nullptr },
  163. { 32, nullptr },
  164. { 48, nullptr },
  165. { 64, nullptr },
  166. { 96, nullptr },
  167. { 128, nullptr },
  168. { 192, nullptr },
  169. { 256, nullptr }
  170. };
  171. HTexture icon = static_resource_cast<Texture>(gResources().loadFromUUID(winPlatformInfo->icon.getUUID()));
  172. if (icon.isLoaded())
  173. {
  174. auto& texProps = icon->getProperties();
  175. SPtr<PixelData> pixels = texProps.allocBuffer(0, 0);
  176. icon->readData(pixels);
  177. gCoreThread().submit(true);
  178. for (auto& entry : textures)
  179. {
  180. entry.pixels = PixelData::create(entry.size, entry.size, 1, PF_RGBA8);
  181. PixelUtil::scale(*pixels, *entry.pixels);
  182. icons[entry.size] = entry.pixels;
  183. }
  184. }
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. IconUtility::updateIconExe(executablePath, icons);
  191. }
  192. void ScriptBuildManager::internal_PackageResources(MonoString* buildFolder, ScriptPlatformInfo* info)
  193. {
  194. UnorderedSet<Path> usedResources;
  195. SPtr<ResourceMapping> resourceMap = ResourceMapping::create();
  196. // Get all resources manually included in build
  197. Vector<USPtr<ProjectLibrary::FileEntry>> buildResources = gProjectLibrary().getResourcesForBuild();
  198. for (auto& entry : buildResources)
  199. {
  200. if (entry->meta == nullptr)
  201. {
  202. BS_LOG(Warning, Editor, "Cannot include resource in build, missing meta file for: {0}", entry->path);
  203. continue;
  204. }
  205. auto& resourceMetas = entry->meta->getResourceMetaData();
  206. for(auto& resMeta : resourceMetas)
  207. {
  208. Path resourcePath;
  209. if (gResources().getFilePathFromUUID(resMeta->getUUID(), resourcePath))
  210. usedResources.insert(resourcePath);
  211. else
  212. BS_LOG(Warning, Editor, "Cannot include resource in build, missing imported asset for: {0}", entry->path);
  213. }
  214. }
  215. // Include main scene
  216. SPtr<PlatformInfo> platformInfo;
  217. if (info != nullptr)
  218. platformInfo = info->getPlatformInfo();
  219. if (platformInfo != nullptr)
  220. {
  221. Path resourcePath;
  222. if (gResources().getFilePathFromUUID(platformInfo->mainScene.getUUID(), resourcePath))
  223. usedResources.insert(resourcePath);
  224. else
  225. BS_LOG(Warning, Editor, "Cannot include main scene in build, missing imported asset.");
  226. }
  227. // Find dependencies of all resources
  228. Vector<Path> newResources;
  229. for (auto& entry : usedResources)
  230. newResources.push_back(entry);
  231. while (!newResources.empty())
  232. {
  233. Vector<Path> allDependencies;
  234. for (auto& entry : newResources)
  235. {
  236. Vector<UUID> curDependencies = gResources().getDependencies(entry);
  237. for (auto& entry : curDependencies)
  238. {
  239. Path resourcePath;
  240. if (gResources().getFilePathFromUUID(entry, resourcePath))
  241. {
  242. if (usedResources.find(resourcePath) == usedResources.end())
  243. {
  244. allDependencies.push_back(resourcePath);
  245. usedResources.insert(resourcePath);
  246. }
  247. }
  248. }
  249. }
  250. newResources = allDependencies;
  251. }
  252. // Copy resources
  253. Path buildPath = MonoUtil::monoToString(buildFolder);
  254. Path outputPath = buildPath;
  255. outputPath.append(GAME_RESOURCES_FOLDER_NAME);
  256. FileSystem::createDir(outputPath);
  257. Path libraryDir = gProjectLibrary().getResourcesFolder();
  258. for (auto& entry : usedResources)
  259. {
  260. UUID uuid;
  261. const bool found = gResources().getUUIDFromFilePath(entry, uuid);
  262. BS_ASSERT(found);
  263. Path sourcePath = gProjectLibrary().uuidToPath(uuid);
  264. if (sourcePath.isEmpty()) // Resource not part of library, meaning its built-in and we don't need to copy those here
  265. continue;
  266. SPtr<ProjectResourceMeta> resMeta = gProjectLibrary().findResourceMeta(sourcePath);
  267. assert(resMeta != nullptr);
  268. Path destPath = outputPath;
  269. destPath.setFilename(entry.getFilename());
  270. // Create library -> packaged resource mapping
  271. Path relSourcePath = sourcePath;
  272. if (sourcePath.isAbsolute())
  273. relSourcePath.makeRelative(libraryDir);
  274. Path relDestPath = GAME_RESOURCES_FOLDER_NAME;
  275. relDestPath.setFilename(entry.getFilename());
  276. resourceMap->add(relSourcePath, relDestPath);
  277. // If resource is prefab make sure to update it in case any of the prefabs it is referencing changed
  278. if (resMeta->getTypeID() == TID_Prefab)
  279. {
  280. bool reload = gResources().isLoaded(uuid);
  281. HPrefab prefab = static_resource_cast<Prefab>(gProjectLibrary().load(sourcePath));
  282. prefab->_updateChildInstances();
  283. // Clear prefab diffs as they're not used in standalone
  284. Stack<HSceneObject> todo;
  285. todo.push(prefab->_getRoot());
  286. while (!todo.empty())
  287. {
  288. HSceneObject current = todo.top();
  289. todo.pop();
  290. current->_clearPrefabDiff();
  291. UINT32 numChildren = current->getNumChildren();
  292. for (UINT32 i = 0; i < numChildren; i++)
  293. {
  294. HSceneObject child = current->getChild(i);
  295. todo.push(child);
  296. }
  297. }
  298. gResources().save(prefab, destPath, false);
  299. // Need to unload this one as we modified it in memory, and we don't want to persist those changes past
  300. // this point
  301. gResources().release(prefab);
  302. if (reload)
  303. gProjectLibrary().load(sourcePath);
  304. }
  305. else
  306. FileSystem::copy(entry, destPath);
  307. }
  308. // Save icon
  309. Path iconFolder = BuiltinResources::getIconFolder();
  310. Path sourceRoot = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platformInfo->type);
  311. iconFolder.makeRelative(sourceRoot);
  312. Path destRoot = BuildManager::instance().getBuildFolder(BuildFolder::DestinationRoot, platformInfo->type);
  313. Path destIconFile = destRoot;
  314. destIconFile.append(iconFolder);
  315. destIconFile.setFilename(String(BuiltinResources::IconTextureName) + ".asset");
  316. switch (platformInfo->type)
  317. {
  318. case PlatformType::Windows:
  319. {
  320. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  321. HTexture icon = static_resource_cast<Texture>(gResources().loadFromUUID(winPlatformInfo->icon.getUUID()));
  322. if (icon != nullptr)
  323. gResources().save(icon, destIconFile, true);
  324. }
  325. break;
  326. default:
  327. break;
  328. };
  329. // Save manifest
  330. Path manifestPath = outputPath;
  331. manifestPath.append(GAME_RESOURCE_MANIFEST_NAME);
  332. Path internalResourcesFolder = gEditorApplication().getProjectPath();
  333. internalResourcesFolder.append(PROJECT_INTERNAL_DIR);
  334. SPtr<ResourceManifest> manifest = gProjectLibrary()._getManifest();
  335. ResourceManifest::save(manifest, manifestPath, internalResourcesFolder);
  336. // Save resource map
  337. Path mappingPath = outputPath;
  338. mappingPath.append(GAME_RESOURCE_MAPPING_NAME);
  339. FileEncoder fe(mappingPath);
  340. fe.encode(resourceMap.get());
  341. }
  342. void ScriptBuildManager::internal_CreateStartupSettings(MonoString* buildFolder, ScriptPlatformInfo* info)
  343. {
  344. SPtr<PlatformInfo> platformInfo;
  345. if (info != nullptr)
  346. platformInfo = info->getPlatformInfo();
  347. SPtr<GameSettings> gameSettings;
  348. if (platformInfo != nullptr)
  349. {
  350. gameSettings = bs_shared_ptr_new<GameSettings>();
  351. gameSettings->mainSceneUUID = platformInfo->mainScene.getUUID();
  352. gameSettings->fullscreen = platformInfo->fullscreen;
  353. gameSettings->resolutionWidth = platformInfo->windowedWidth;
  354. gameSettings->resolutionHeight = platformInfo->windowedHeight;
  355. switch (platformInfo->type)
  356. {
  357. case(PlatformType::Windows) :
  358. {
  359. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  360. gameSettings->titleBarText = winPlatformInfo->titlebarText;
  361. }
  362. break;
  363. default:
  364. break;
  365. }
  366. }
  367. Path outputPath = MonoUtil::monoToString(buildFolder);
  368. outputPath.append(GAME_SETTINGS_NAME);
  369. FileEncoder fe(outputPath);
  370. fe.encode(gameSettings.get());
  371. }
  372. }