BsScriptBuildManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. }
  139. path = BuildManager::instance().getBuildFolder(nativeFolderType, platform);
  140. }
  141. return MonoUtil::wstringToMono(path.toWString());
  142. }
  143. void ScriptBuildManager::internal_InjectIcons(MonoString* filePath, ScriptPlatformInfo* info)
  144. {
  145. if (info == nullptr)
  146. return;
  147. Path executablePath = MonoUtil::monoToWString(filePath);
  148. Map<UINT32, PixelDataPtr> 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. PixelDataPtr 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. PixelDataPtr pixels = texProps.allocateSubresourceBuffer(0);
  176. icon->readSubresource(gCoreAccessor(), 0, pixels);
  177. gCoreAccessor().submitToCoreThread(true);
  178. for (auto& entry : textures)
  179. {
  180. entry.pixels = PixelData::create(entry.size, entry.size, 1, PF_R8G8B8A8);
  181. PixelUtil::scale(*pixels, *entry.pixels);
  182. icons[entry.size] = entry.pixels;
  183. }
  184. }
  185. }
  186. break;
  187. }
  188. IconUtility::updateIconExe(executablePath, icons);
  189. }
  190. void ScriptBuildManager::internal_PackageResources(MonoString* buildFolder, ScriptPlatformInfo* info)
  191. {
  192. UnorderedSet<Path> usedResources;
  193. SPtr<ResourceMapping> resourceMap = ResourceMapping::create();
  194. // Get all resources manually included in build
  195. Vector<ProjectLibrary::FileEntry*> buildResources = gProjectLibrary().getResourcesForBuild();
  196. for (auto& entry : buildResources)
  197. {
  198. if (entry->meta == nullptr)
  199. {
  200. LOGWRN("Cannot include resource in build, missing meta file for: " + entry->path.toString());
  201. continue;
  202. }
  203. auto& resourceMetas = entry->meta->getResourceMetaData();
  204. for(auto& resMeta : resourceMetas)
  205. {
  206. Path resourcePath;
  207. if (gResources().getFilePathFromUUID(resMeta->getUUID(), resourcePath))
  208. usedResources.insert(resourcePath);
  209. else
  210. LOGWRN("Cannot include resource in build, missing imported asset for: " + entry->path.toString());
  211. }
  212. }
  213. // Include main scene
  214. SPtr<PlatformInfo> platformInfo;
  215. if (info != nullptr)
  216. platformInfo = info->getPlatformInfo();
  217. if (platformInfo != nullptr)
  218. {
  219. Path resourcePath;
  220. if (gResources().getFilePathFromUUID(platformInfo->mainScene.getUUID(), resourcePath))
  221. usedResources.insert(resourcePath);
  222. else
  223. LOGWRN("Cannot include main scene in build, missing imported asset.");
  224. }
  225. // Find dependencies of all resources
  226. Vector<Path> newResources;
  227. for (auto& entry : usedResources)
  228. newResources.push_back(entry);
  229. while (!newResources.empty())
  230. {
  231. Vector<Path> allDependencies;
  232. for (auto& entry : newResources)
  233. {
  234. Vector<String> curDependencies = gResources().getDependencies(entry);
  235. for (auto& entry : curDependencies)
  236. {
  237. Path resourcePath;
  238. if (gResources().getFilePathFromUUID(entry, resourcePath))
  239. {
  240. if (usedResources.find(resourcePath) == usedResources.end())
  241. {
  242. allDependencies.push_back(resourcePath);
  243. usedResources.insert(resourcePath);
  244. }
  245. }
  246. }
  247. }
  248. newResources = allDependencies;
  249. }
  250. // Copy resources
  251. Path buildPath = MonoUtil::monoToWString(buildFolder);
  252. Path outputPath = buildPath;
  253. outputPath.append(GAME_RESOURCES_FOLDER_NAME);
  254. FileSystem::createDir(outputPath);
  255. Path libraryDir = gProjectLibrary().getResourcesFolder();
  256. for (auto& entry : usedResources)
  257. {
  258. String uuid;
  259. bool foundUUID = gResources().getUUIDFromFilePath(entry, uuid);
  260. BS_ASSERT(foundUUID);
  261. Path sourcePath = gProjectLibrary().uuidToPath(uuid);
  262. if (sourcePath.isEmpty()) // Resource not part of library, meaning its built-in and we don't need to copy those here
  263. continue;
  264. ProjectResourceMetaPtr resMeta = gProjectLibrary().findResourceMeta(sourcePath);
  265. assert(resMeta != nullptr);
  266. Path destPath = outputPath;
  267. destPath.setFilename(entry.getFilename());
  268. // Create library -> packaged resource mapping
  269. Path relSourcePath = sourcePath;
  270. if (sourcePath.isAbsolute())
  271. relSourcePath.makeRelative(libraryDir);
  272. Path relDestPath = GAME_RESOURCES_FOLDER_NAME;
  273. relDestPath.setFilename(entry.getFilename());
  274. resourceMap->add(relSourcePath, relDestPath);
  275. // If resource is prefab make sure to update it in case any of the prefabs it is referencing changed
  276. if (resMeta->getTypeID() == TID_Prefab)
  277. {
  278. bool reload = gResources().isLoaded(uuid);
  279. HPrefab prefab = static_resource_cast<Prefab>(gProjectLibrary().load(sourcePath));
  280. prefab->_updateChildInstances();
  281. // Clear prefab diffs as they're not used in standalone
  282. Stack<HSceneObject> todo;
  283. todo.push(prefab->_getRoot());
  284. while (!todo.empty())
  285. {
  286. HSceneObject current = todo.top();
  287. todo.pop();
  288. current->_clearPrefabDiff();
  289. UINT32 numChildren = current->getNumChildren();
  290. for (UINT32 i = 0; i < numChildren; i++)
  291. {
  292. HSceneObject child = current->getChild(i);
  293. todo.push(child);
  294. }
  295. }
  296. gResources().save(prefab, destPath, false);
  297. // Need to unload this one as we modified it in memory, and we don't want to persist those changes past
  298. // this point
  299. gResources().release(prefab);
  300. if (reload)
  301. gProjectLibrary().load(sourcePath);
  302. }
  303. else
  304. FileSystem::copy(entry, destPath);
  305. }
  306. // Save icon
  307. Path iconFolder = FileSystem::getWorkingDirectoryPath();
  308. iconFolder.append(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 + L".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. };
  325. // Save manifest
  326. Path manifestPath = outputPath;
  327. manifestPath.append(GAME_RESOURCE_MANIFEST_NAME);
  328. Path internalResourcesFolder = gEditorApplication().getProjectPath();
  329. internalResourcesFolder.append(PROJECT_INTERNAL_DIR);
  330. ResourceManifestPtr manifest = gProjectLibrary()._getManifest();
  331. ResourceManifest::save(manifest, manifestPath, internalResourcesFolder);
  332. // Save resource map
  333. Path mappingPath = outputPath;
  334. mappingPath.append(GAME_RESOURCE_MAPPING_NAME);
  335. FileEncoder fe(mappingPath);
  336. fe.encode(resourceMap.get());
  337. }
  338. void ScriptBuildManager::internal_CreateStartupSettings(MonoString* buildFolder, ScriptPlatformInfo* info)
  339. {
  340. SPtr<PlatformInfo> platformInfo;
  341. if (info != nullptr)
  342. platformInfo = info->getPlatformInfo();
  343. SPtr<GameSettings> gameSettings;
  344. if (platformInfo != nullptr)
  345. {
  346. gameSettings = bs_shared_ptr_new<GameSettings>();
  347. gameSettings->mainSceneUUID = platformInfo->mainScene.getUUID();
  348. gameSettings->fullscreen = platformInfo->fullscreen;
  349. gameSettings->resolutionWidth = platformInfo->windowedWidth;
  350. gameSettings->resolutionHeight = platformInfo->windowedHeight;
  351. switch (platformInfo->type)
  352. {
  353. case(PlatformType::Windows) :
  354. {
  355. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  356. gameSettings->titleBarText = winPlatformInfo->titlebarText;
  357. }
  358. break;
  359. }
  360. }
  361. Path outputPath = MonoUtil::monoToWString(buildFolder);
  362. outputPath.append(GAME_SETTINGS_NAME);
  363. FileEncoder fe(outputPath);
  364. fe.encode(gameSettings.get());
  365. }
  366. }