2
0

BsScriptBuildManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 },
  163. { 32 },
  164. { 48 },
  165. { 64 },
  166. { 96 },
  167. { 128 },
  168. { 192 },
  169. { 256 }
  170. };
  171. HTexture icon = gResources().load(winPlatformInfo->icon);
  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<ProjectLibrary::FileEntry*> buildResources = gProjectLibrary().getResourcesForBuild();
  198. for (auto& entry : buildResources)
  199. {
  200. if (entry->meta == nullptr)
  201. {
  202. LOGWRN("Cannot include resource in build, missing meta file for: " + entry->path.toString());
  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. LOGWRN("Cannot include resource in build, missing imported asset for: " + entry->path.toString());
  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. LOGWRN("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. BS_ASSERT(gResources().getUUIDFromFilePath(entry, uuid));
  262. Path sourcePath = gProjectLibrary().uuidToPath(uuid);
  263. if (sourcePath.isEmpty()) // Resource not part of library, meaning its built-in and we don't need to copy those here
  264. continue;
  265. SPtr<ProjectResourceMeta> resMeta = gProjectLibrary().findResourceMeta(sourcePath);
  266. assert(resMeta != nullptr);
  267. Path destPath = outputPath;
  268. destPath.setFilename(entry.getFilename());
  269. // Create library -> packaged resource mapping
  270. Path relSourcePath = sourcePath;
  271. if (sourcePath.isAbsolute())
  272. relSourcePath.makeRelative(libraryDir);
  273. Path relDestPath = GAME_RESOURCES_FOLDER_NAME;
  274. relDestPath.setFilename(entry.getFilename());
  275. resourceMap->add(relSourcePath, relDestPath);
  276. // If resource is prefab make sure to update it in case any of the prefabs it is referencing changed
  277. if (resMeta->getTypeID() == TID_Prefab)
  278. {
  279. bool reload = gResources().isLoaded(uuid);
  280. HPrefab prefab = static_resource_cast<Prefab>(gProjectLibrary().load(sourcePath));
  281. prefab->_updateChildInstances();
  282. // Clear prefab diffs as they're not used in standalone
  283. Stack<HSceneObject> todo;
  284. todo.push(prefab->_getRoot());
  285. while (!todo.empty())
  286. {
  287. HSceneObject current = todo.top();
  288. todo.pop();
  289. current->_clearPrefabDiff();
  290. UINT32 numChildren = current->getNumChildren();
  291. for (UINT32 i = 0; i < numChildren; i++)
  292. {
  293. HSceneObject child = current->getChild(i);
  294. todo.push(child);
  295. }
  296. }
  297. gResources().save(prefab, destPath, false);
  298. // Need to unload this one as we modified it in memory, and we don't want to persist those changes past
  299. // this point
  300. gResources().release(prefab);
  301. if (reload)
  302. gProjectLibrary().load(sourcePath);
  303. }
  304. else
  305. FileSystem::copy(entry, destPath);
  306. }
  307. // Save icon
  308. Path iconFolder = BuiltinResources::getIconFolder();
  309. Path sourceRoot = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platformInfo->type);
  310. iconFolder.makeRelative(sourceRoot);
  311. Path destRoot = BuildManager::instance().getBuildFolder(BuildFolder::DestinationRoot, platformInfo->type);
  312. Path destIconFile = destRoot;
  313. destIconFile.append(iconFolder);
  314. destIconFile.setFilename(BuiltinResources::IconTextureName + ".asset");
  315. switch (platformInfo->type)
  316. {
  317. case PlatformType::Windows:
  318. {
  319. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  320. HTexture icon = gResources().load(winPlatformInfo->icon);
  321. if (icon != nullptr)
  322. gResources().save(icon, destIconFile, true);
  323. }
  324. break;
  325. default:
  326. break;
  327. };
  328. // Save manifest
  329. Path manifestPath = outputPath;
  330. manifestPath.append(GAME_RESOURCE_MANIFEST_NAME);
  331. Path internalResourcesFolder = gEditorApplication().getProjectPath();
  332. internalResourcesFolder.append(PROJECT_INTERNAL_DIR);
  333. SPtr<ResourceManifest> manifest = gProjectLibrary()._getManifest();
  334. ResourceManifest::save(manifest, manifestPath, internalResourcesFolder);
  335. // Save resource map
  336. Path mappingPath = outputPath;
  337. mappingPath.append(GAME_RESOURCE_MAPPING_NAME);
  338. FileEncoder fe(mappingPath);
  339. fe.encode(resourceMap.get());
  340. }
  341. void ScriptBuildManager::internal_CreateStartupSettings(MonoString* buildFolder, ScriptPlatformInfo* info)
  342. {
  343. SPtr<PlatformInfo> platformInfo;
  344. if (info != nullptr)
  345. platformInfo = info->getPlatformInfo();
  346. SPtr<GameSettings> gameSettings;
  347. if (platformInfo != nullptr)
  348. {
  349. gameSettings = bs_shared_ptr_new<GameSettings>();
  350. gameSettings->mainSceneUUID = platformInfo->mainScene.getUUID();
  351. gameSettings->fullscreen = platformInfo->fullscreen;
  352. gameSettings->resolutionWidth = platformInfo->windowedWidth;
  353. gameSettings->resolutionHeight = platformInfo->windowedHeight;
  354. switch (platformInfo->type)
  355. {
  356. case(PlatformType::Windows) :
  357. {
  358. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  359. gameSettings->titleBarText = winPlatformInfo->titlebarText;
  360. }
  361. break;
  362. default:
  363. break;
  364. }
  365. }
  366. Path outputPath = MonoUtil::monoToString(buildFolder);
  367. outputPath.append(GAME_SETTINGS_NAME);
  368. FileEncoder fe(outputPath);
  369. fe.encode(gameSettings.get());
  370. }
  371. }