2
0

BsScriptBuildManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. namespace BansheeEngine
  24. {
  25. ScriptBuildManager::ScriptBuildManager(MonoObject* instance)
  26. :ScriptObject(instance)
  27. { }
  28. void ScriptBuildManager::initRuntimeData()
  29. {
  30. metaData.scriptClass->addInternalCall("Internal_GetAvailablePlatforms", &ScriptBuildManager::internal_GetAvailablePlatforms);
  31. metaData.scriptClass->addInternalCall("Internal_GetActivePlatform", &ScriptBuildManager::internal_GetActivePlatform);
  32. metaData.scriptClass->addInternalCall("Internal_SetActivePlatform", &ScriptBuildManager::internal_SetActivePlatform);
  33. metaData.scriptClass->addInternalCall("Internal_GetActivePlatformInfo", &ScriptBuildManager::internal_GetActivePlatformInfo);
  34. metaData.scriptClass->addInternalCall("Internal_GetPlatformInfo", &ScriptBuildManager::internal_GetPlatformInfo);
  35. metaData.scriptClass->addInternalCall("Internal_GetFrameworkAssemblies", &ScriptBuildManager::internal_GetFrameworkAssemblies);
  36. metaData.scriptClass->addInternalCall("Internal_GetMainExecutable", &ScriptBuildManager::internal_GetMainExecutable);
  37. metaData.scriptClass->addInternalCall("Internal_GetDefines", &ScriptBuildManager::internal_GetDefines);
  38. metaData.scriptClass->addInternalCall("Internal_GetNativeBinaries", &ScriptBuildManager::internal_GetNativeBinaries);
  39. metaData.scriptClass->addInternalCall("Internal_GetBuildFolder", &ScriptBuildManager::internal_GetBuildFolder);
  40. metaData.scriptClass->addInternalCall("Internal_InjectIcons", &ScriptBuildManager::internal_InjectIcons);
  41. metaData.scriptClass->addInternalCall("Internal_PackageResources", &ScriptBuildManager::internal_PackageResources);
  42. metaData.scriptClass->addInternalCall("Internal_CreateStartupSettings", &ScriptBuildManager::internal_CreateStartupSettings);
  43. }
  44. MonoArray* ScriptBuildManager::internal_GetAvailablePlatforms()
  45. {
  46. const Vector<PlatformType>& availableType = BuildManager::instance().getAvailablePlatforms();
  47. ScriptArray outArray = ScriptArray::create<UINT32>((UINT32)availableType.size());
  48. UINT32 idx = 0;
  49. for (auto& type : availableType)
  50. outArray.set(idx++, type);
  51. return outArray.getInternal();
  52. }
  53. PlatformType ScriptBuildManager::internal_GetActivePlatform()
  54. {
  55. return BuildManager::instance().getActivePlatform();
  56. }
  57. void ScriptBuildManager::internal_SetActivePlatform(PlatformType value)
  58. {
  59. BuildManager::instance().setActivePlatform(value);
  60. }
  61. MonoObject* ScriptBuildManager::internal_GetActivePlatformInfo()
  62. {
  63. return ScriptPlatformInfo::create(BuildManager::instance().getActivePlatformInfo());
  64. }
  65. MonoObject* ScriptBuildManager::internal_GetPlatformInfo(PlatformType type)
  66. {
  67. return ScriptPlatformInfo::create(BuildManager::instance().getPlatformInfo(type));
  68. }
  69. MonoArray* ScriptBuildManager::internal_GetFrameworkAssemblies(PlatformType type)
  70. {
  71. Vector<WString> frameworkAssemblies = BuildManager::instance().getFrameworkAssemblies(type);
  72. ScriptArray outArray = ScriptArray::create<WString>((UINT32)frameworkAssemblies.size());
  73. UINT32 idx = 0;
  74. for (auto& assemblyName : frameworkAssemblies)
  75. outArray.set(idx++, MonoUtil::wstringToMono(assemblyName));
  76. return outArray.getInternal();
  77. }
  78. MonoString* ScriptBuildManager::internal_GetMainExecutable(PlatformType type)
  79. {
  80. return MonoUtil::wstringToMono(BuildManager::instance().getMainExecutable(type).toWString());
  81. }
  82. MonoString* ScriptBuildManager::internal_GetDefines(PlatformType type)
  83. {
  84. return MonoUtil::wstringToMono(BuildManager::instance().getDefines(type));
  85. }
  86. MonoArray* ScriptBuildManager::internal_GetNativeBinaries(PlatformType type)
  87. {
  88. Vector<Path> paths = BuildManager::instance().getNativeBinaries(type);
  89. UINT32 numEntries = (UINT32)paths.size();
  90. ScriptArray outArray = ScriptArray::create<WString>(numEntries);
  91. for (UINT32 i = 0; i < numEntries; i++)
  92. {
  93. outArray.set(i, MonoUtil::wstringToMono(paths[i].toWString()));
  94. }
  95. return outArray.getInternal();
  96. }
  97. MonoString* ScriptBuildManager::internal_GetBuildFolder(ScriptBuildFolder folder, PlatformType platform)
  98. {
  99. Path path;
  100. if (folder == ScriptBuildFolder::FrameworkAssemblies)
  101. {
  102. Path assemblyFolder = MonoManager::instance().getFrameworkAssembliesFolder();
  103. assemblyFolder.makeAbsolute(FileSystem::getWorkingDirectoryPath());
  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. monoEtcFolder.makeAbsolute(FileSystem::getWorkingDirectoryPath());
  111. Path sourceFolder = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platform);
  112. path = monoEtcFolder.makeRelative(sourceFolder);
  113. }
  114. else
  115. {
  116. BuildFolder nativeFolderType = BuildFolder::SourceRoot;
  117. switch (folder)
  118. {
  119. case ScriptBuildFolder::SourceRoot:
  120. nativeFolderType = BuildFolder::SourceRoot;
  121. break;
  122. case ScriptBuildFolder::DestinationRoot:
  123. nativeFolderType = BuildFolder::DestinationRoot;
  124. break;
  125. case ScriptBuildFolder::NativeBinaries:
  126. nativeFolderType = BuildFolder::NativeBinaries;
  127. break;
  128. case ScriptBuildFolder::BansheeDebugAssemblies:
  129. nativeFolderType = BuildFolder::BansheeDebugAssemblies;
  130. break;
  131. case ScriptBuildFolder::BansheeReleaseAssemblies:
  132. nativeFolderType = BuildFolder::BansheeReleaseAssemblies;
  133. break;
  134. case ScriptBuildFolder::Data:
  135. nativeFolderType = BuildFolder::Data;
  136. break;
  137. }
  138. path = BuildManager::instance().getBuildFolder(nativeFolderType, platform);
  139. }
  140. return MonoUtil::wstringToMono(path.toWString());
  141. }
  142. void ScriptBuildManager::internal_InjectIcons(MonoString* filePath, ScriptPlatformInfo* info)
  143. {
  144. if (info == nullptr)
  145. return;
  146. Path executablePath = MonoUtil::monoToWString(filePath);
  147. Map<UINT32, PixelDataPtr> icons;
  148. SPtr<PlatformInfo> platformInfo = info->getPlatformInfo();
  149. switch (platformInfo->type)
  150. {
  151. case PlatformType::Windows:
  152. {
  153. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  154. struct IconData
  155. {
  156. UINT32 size;
  157. PixelDataPtr pixels;
  158. };
  159. IconData textures[] =
  160. {
  161. { 16 },
  162. { 32 },
  163. { 48 },
  164. { 64 },
  165. { 96 },
  166. { 128 },
  167. { 192 },
  168. { 256 }
  169. };
  170. HTexture icon = gResources().load(winPlatformInfo->icon);
  171. if (icon.isLoaded())
  172. {
  173. auto& texProps = icon->getProperties();
  174. PixelDataPtr pixels = texProps.allocateSubresourceBuffer(0);
  175. icon->readSubresource(gCoreAccessor(), 0, pixels);
  176. gCoreAccessor().submitToCoreThread(true);
  177. for (auto& entry : textures)
  178. {
  179. entry.pixels = PixelData::create(entry.size, entry.size, 1, PF_R8G8B8A8);
  180. PixelUtil::scale(*pixels, *entry.pixels);
  181. icons[entry.size] = entry.pixels;
  182. }
  183. }
  184. }
  185. break;
  186. }
  187. IconUtility::updateIconExe(executablePath, icons);
  188. }
  189. void ScriptBuildManager::internal_PackageResources(MonoString* buildFolder, ScriptPlatformInfo* info)
  190. {
  191. UnorderedSet<Path> usedResources;
  192. // Get all resources manually included in build
  193. Vector<ProjectLibrary::ResourceEntry*> buildResources = gProjectLibrary().getResourcesForBuild();
  194. for (auto& entry : buildResources)
  195. {
  196. if (entry->meta == nullptr)
  197. {
  198. LOGWRN("Cannot include resource in build, missing meta file for: " + entry->path.toString());
  199. continue;
  200. }
  201. Path resourcePath;
  202. if (gResources().getFilePathFromUUID(entry->meta->getUUID(), resourcePath))
  203. usedResources.insert(resourcePath);
  204. else
  205. LOGWRN("Cannot include resource in build, missing imported asset for: " + entry->path.toString());
  206. }
  207. // Include main scene
  208. SPtr<PlatformInfo> platformInfo;
  209. if (info != nullptr)
  210. platformInfo = info->getPlatformInfo();
  211. if (platformInfo != nullptr)
  212. {
  213. Path resourcePath;
  214. if (gResources().getFilePathFromUUID(platformInfo->mainScene.getUUID(), resourcePath))
  215. usedResources.insert(resourcePath);
  216. else
  217. LOGWRN("Cannot include main scene in build, missing imported asset.");
  218. }
  219. // Find dependencies of all resources
  220. Vector<Path> newResources;
  221. for (auto& entry : usedResources)
  222. newResources.push_back(entry);
  223. while (!newResources.empty())
  224. {
  225. Vector<Path> allDependencies;
  226. for (auto& entry : newResources)
  227. {
  228. Vector<String> curDependencies = gResources().getDependencies(entry);
  229. for (auto& entry : curDependencies)
  230. {
  231. Path resourcePath;
  232. if (gResources().getFilePathFromUUID(entry, resourcePath))
  233. {
  234. if (usedResources.find(resourcePath) == usedResources.end())
  235. {
  236. allDependencies.push_back(resourcePath);
  237. usedResources.insert(resourcePath);
  238. }
  239. }
  240. }
  241. }
  242. newResources = allDependencies;
  243. }
  244. // Copy resources
  245. Path outputPath = MonoUtil::monoToWString(buildFolder);
  246. outputPath.append(GAME_RESOURCES_FOLDER_NAME);
  247. FileSystem::createDir(outputPath);
  248. for (auto& entry : usedResources)
  249. {
  250. String uuid;
  251. bool foundUUID = gResources().getUUIDFromFilePath(entry, uuid);
  252. BS_ASSERT(foundUUID);
  253. Path sourcePath = gProjectLibrary().uuidToPath(uuid);
  254. if (sourcePath.isEmpty()) // Resource not part of library, meaning its built-in and we don't need to copy those here
  255. continue;
  256. ProjectLibrary::LibraryEntry* libEntry = gProjectLibrary().findEntry(sourcePath);
  257. assert(libEntry != nullptr && libEntry->type == ProjectLibrary::LibraryEntryType::File);
  258. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(libEntry);
  259. Path destPath = outputPath;
  260. destPath.setFilename(entry.getFilename());
  261. // If resource is prefab make sure to update it in case any of the prefabs it is referencing changed
  262. if (resEntry->meta->getTypeID() == TID_Prefab)
  263. {
  264. bool reload = gResources().isLoaded(uuid);
  265. HPrefab prefab = static_resource_cast<Prefab>(gProjectLibrary().load(sourcePath));
  266. prefab->_updateChildInstances();
  267. // Clear prefab diffs as they're not used in standalone
  268. Stack<HSceneObject> todo;
  269. todo.push(prefab->_getRoot());
  270. while (!todo.empty())
  271. {
  272. HSceneObject current = todo.top();
  273. todo.pop();
  274. current->_clearPrefabDiff();
  275. UINT32 numChildren = current->getNumChildren();
  276. for (UINT32 i = 0; i < numChildren; i++)
  277. {
  278. HSceneObject child = current->getChild(i);
  279. todo.push(child);
  280. }
  281. }
  282. gResources().save(prefab, destPath, false);
  283. // Need to unload this one as we modified it in memory, and we don't want to persist those changes past
  284. // this point
  285. gResources().release(prefab);
  286. if (reload)
  287. gProjectLibrary().load(sourcePath);
  288. }
  289. else
  290. FileSystem::copy(entry, destPath);
  291. }
  292. // Save icon
  293. Path iconFolder = FileSystem::getWorkingDirectoryPath();
  294. iconFolder.append(BuiltinResources::getIconFolder());
  295. Path sourceRoot = BuildManager::instance().getBuildFolder(BuildFolder::SourceRoot, platformInfo->type);
  296. iconFolder.makeRelative(sourceRoot);
  297. Path destRoot = BuildManager::instance().getBuildFolder(BuildFolder::DestinationRoot, platformInfo->type);
  298. Path destIconFile = destRoot;
  299. destIconFile.append(iconFolder);
  300. destIconFile.setFilename(BuiltinResources::IconTextureName + L".asset");
  301. switch (platformInfo->type)
  302. {
  303. case PlatformType::Windows:
  304. {
  305. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  306. HTexture icon = gResources().load(winPlatformInfo->icon);
  307. if (icon != nullptr)
  308. gResources().save(icon, destIconFile, true);
  309. }
  310. };
  311. // Save manifest
  312. Path manifestPath = outputPath;
  313. manifestPath.append(GAME_RESOURCE_MANIFEST_NAME);
  314. Path internalResourcesFolder = gEditorApplication().getProjectPath();
  315. internalResourcesFolder.append(PROJECT_INTERNAL_DIR);
  316. ResourceManifestPtr manifest = gProjectLibrary()._getManifest();
  317. ResourceManifest::save(manifest, manifestPath, internalResourcesFolder);
  318. }
  319. void ScriptBuildManager::internal_CreateStartupSettings(MonoString* buildFolder, ScriptPlatformInfo* info)
  320. {
  321. SPtr<PlatformInfo> platformInfo;
  322. if (info != nullptr)
  323. platformInfo = info->getPlatformInfo();
  324. SPtr<GameSettings> gameSettings;
  325. if (platformInfo != nullptr)
  326. {
  327. gameSettings = bs_shared_ptr_new<GameSettings>();
  328. gameSettings->mainSceneUUID = platformInfo->mainScene.getUUID();
  329. gameSettings->fullscreen = platformInfo->fullscreen;
  330. gameSettings->resolutionWidth = platformInfo->windowedWidth;
  331. gameSettings->resolutionHeight = platformInfo->windowedHeight;
  332. switch (platformInfo->type)
  333. {
  334. case(PlatformType::Windows) :
  335. {
  336. SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
  337. gameSettings->titleBarText = winPlatformInfo->titlebarText;
  338. }
  339. break;
  340. }
  341. }
  342. Path outputPath = MonoUtil::monoToWString(buildFolder);
  343. outputPath.append(GAME_SETTINGS_NAME);
  344. FileEncoder fe(outputPath);
  345. fe.encode(gameSettings.get());
  346. }
  347. }