BsScriptBuildManager.cpp 14 KB

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