BuildBase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/Resource/JSONFile.h>
  25. #include "../Subprocess/SubprocessSystem.h"
  26. #include "../Project/Project.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Assets/Asset.h"
  29. #include "../Assets/AssetDatabase.h"
  30. #include "BuildSystem.h"
  31. #include "BuildEvents.h"
  32. #include "BuildBase.h"
  33. #include "ResourcePackager.h"
  34. #include "AssetBuildConfig.h"
  35. namespace ToolCore
  36. {
  37. BuildBase::BuildBase(Context * context, Project* project, PlatformID platform) : Object(context),
  38. platformID_(platform),
  39. containsMDL_(false),
  40. buildFailed_(false),
  41. assetBuildTag_(String::EMPTY)
  42. {
  43. if (UseResourcePackager())
  44. resourcePackager_ = new ResourcePackager(context, this);
  45. project_ = project;
  46. ReadAssetBuildConfig();
  47. }
  48. BuildBase::~BuildBase()
  49. {
  50. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  51. {
  52. delete resourceEntries_[i];
  53. }
  54. }
  55. #ifdef ATOMIC_PLATFORM_WINDOWS
  56. bool BuildBase::BuildClean(const String& path)
  57. {
  58. if (buildFailed_)
  59. {
  60. LOGERRORF("BuildBase::BuildClean - Attempt to clean directory of failed build, %s", path.CString());
  61. return false;
  62. }
  63. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  64. if (!fileSystem->DirExists(path))
  65. return true;
  66. // On Windows, do a little dance with the folder to avoid issues
  67. // with deleting folder and immediately recreating it
  68. String pathName, fileName, ext;
  69. SplitPath(path, pathName, fileName, ext);
  70. pathName = AddTrailingSlash(pathName);
  71. unsigned i = 0;
  72. while (true)
  73. {
  74. String newPath = ToString("%s%s_Temp_%u", pathName.CString(), fileName.CString(), i++);
  75. if (!fileSystem->DirExists(newPath))
  76. {
  77. if (!MoveFileExW(GetWideNativePath(path).CString(), GetWideNativePath(newPath).CString(), MOVEFILE_WRITE_THROUGH))
  78. {
  79. FailBuild(ToString("BuildBase::BuildClean: Unable to move directory %s -> ", path.CString(), newPath.CString()));
  80. return false;
  81. }
  82. // Remove the moved directory
  83. return BuildRemoveDirectory(newPath);
  84. }
  85. else
  86. {
  87. LOGWARNINGF("BuildBase::BuildClean - temp build folder exists, removing: %s", newPath.CString());
  88. fileSystem->RemoveDir(newPath, true);
  89. }
  90. if (i == 255)
  91. {
  92. FailBuild(ToString("BuildBase::BuildClean: Unable to move directory ( i == 255) %s -> ", path.CString(), newPath.CString()));
  93. return false;
  94. }
  95. }
  96. return false;
  97. }
  98. #else
  99. bool BuildBase::BuildClean(const String& path)
  100. {
  101. return BuildRemoveDirectory(path);
  102. }
  103. #endif
  104. bool BuildBase::BuildCreateDirectory(const String& path)
  105. {
  106. if (buildFailed_)
  107. {
  108. LOGERRORF("BuildBase::BuildCreateDirectory - Attempt to create directory of failed build, %s", path.CString());
  109. return false;
  110. }
  111. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  112. if (fileSystem->DirExists(path))
  113. return true;
  114. bool result = fileSystem->CreateDir(path);
  115. if (!result)
  116. {
  117. FailBuild(ToString("BuildBase::BuildCreateDirectory: Unable to create directory %s", path.CString()));
  118. return false;
  119. }
  120. return true;
  121. }
  122. bool BuildBase::BuildCopyFile(const String& srcFileName, const String& destFileName)
  123. {
  124. if (buildFailed_)
  125. {
  126. LOGERRORF("BuildBase::BuildCopyFile - Attempt to copy file of failed build, %s", srcFileName.CString());
  127. return false;
  128. }
  129. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  130. bool result = fileSystem->Copy(srcFileName, destFileName);
  131. if (!result)
  132. {
  133. FailBuild(ToString("BuildBase::BuildCopyFile: Unable to copy file %s -> %s", srcFileName.CString(), destFileName.CString()));
  134. return false;
  135. }
  136. return true;
  137. }
  138. bool BuildBase::CheckIncludeResourceFile(const String & resourceDir, const String & fileName)
  139. {
  140. return (GetExtension(fileName) != ".psd");
  141. }
  142. bool BuildBase::BuildRemoveDirectory(const String& path)
  143. {
  144. if (buildFailed_)
  145. {
  146. LOGERRORF("BuildBase::BuildRemoveDirectory - Attempt to remove directory of failed build, %s", path.CString());
  147. return false;
  148. }
  149. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  150. if (!fileSystem->DirExists(path))
  151. return true;
  152. bool result = fileSystem->RemoveDir(path, true);
  153. if (!result)
  154. {
  155. FailBuild(ToString("BuildBase::BuildRemoveDirectory: Unable to remove directory %s", path.CString()));
  156. return false;
  157. }
  158. return true;
  159. }
  160. void BuildBase::BuildLog(const String& message, bool sendEvent)
  161. {
  162. buildLog_.Push(message);
  163. if (sendEvent)
  164. {
  165. String colorMsg = ToString("<color #D4FB79>%s</color>\n", message.CString());
  166. VariantMap buildOutput;
  167. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  168. SendEvent(E_BUILDOUTPUT, buildOutput);
  169. }
  170. }
  171. void BuildBase::BuildWarn(const String& warning, bool sendEvent)
  172. {
  173. buildWarnings_.Push(warning);
  174. if (sendEvent)
  175. {
  176. String colorMsg = ToString("<color #FFFF00>%s</color>\n", warning.CString());
  177. VariantMap buildOutput;
  178. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  179. SendEvent(E_BUILDOUTPUT, buildOutput);
  180. }
  181. }
  182. void BuildBase::BuildError(const String& error, bool sendEvent)
  183. {
  184. buildErrors_.Push(error);
  185. if (sendEvent)
  186. {
  187. String colorMsg = ToString("<color #FF0000>%s</color>\n", error.CString());
  188. VariantMap buildOutput;
  189. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  190. SendEvent(E_BUILDOUTPUT, buildOutput);
  191. }
  192. }
  193. void BuildBase::FailBuild(const String& message)
  194. {
  195. if (buildFailed_)
  196. {
  197. LOGERRORF("BuildBase::FailBuild - Attempt to fail already failed build: %s", message.CString());
  198. return;
  199. }
  200. buildFailed_ = true;
  201. BuildError(message);
  202. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  203. buildSystem->BuildComplete(platformID_, buildPath_, false, message);
  204. }
  205. void BuildBase::HandleSubprocessOutputEvent(StringHash eventType, VariantMap& eventData)
  206. {
  207. // E_SUBPROCESSOUTPUT
  208. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  209. // convert to a build output event and forward to subscribers
  210. VariantMap buildOutputData;
  211. buildOutputData[BuildOutput::P_TEXT] = text;
  212. SendEvent(E_BUILDOUTPUT, buildOutputData);
  213. }
  214. void BuildBase::GetDefaultResourcePaths(Vector<String>& paths)
  215. {
  216. paths.Clear();
  217. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  218. paths.Push(AddTrailingSlash(tenv->GetCoreDataDir()));
  219. paths.Push(AddTrailingSlash(tenv->GetPlayerDataDir()));
  220. }
  221. String BuildBase::GetSettingsDirectory()
  222. {
  223. return project_->GetProjectPath() + "/Settings";
  224. }
  225. void BuildBase::ScanResourceDirectory(const String& resourceDir)
  226. {
  227. {
  228. //LOGINFOF("Adding resource: %s : %s", newEntry->absolutePath_.CString(), newEntry->packagePath_.CString());
  229. }
  230. }
  231. void BuildBase::BuildDefaultResourceEntries()
  232. {
  233. for (unsigned i = 0; i < resourceDirs_.Size(); i++)
  234. {
  235. String resourceDir = resourceDirs_[i];
  236. Vector<String> fileNames;
  237. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  238. fileSystem->ScanDir(fileNames, resourceDir, "*.*", SCAN_FILES, true);
  239. for (unsigned i = 0; i < fileNames.Size(); i++)
  240. {
  241. const String& filename = fileNames[i];
  242. if (!CheckIncludeResourceFile(resourceDir, filename))
  243. continue;
  244. AddToResourcePackager(filename, resourceDir);
  245. }
  246. }
  247. }
  248. void BuildBase::BuildProjectResourceEntries()
  249. {
  250. if (AssetBuildConfig::IsLoaded() && !assetBuildTag_.Empty())
  251. {
  252. // add log comment
  253. BuildFilteredProjectResourceEntries();
  254. }
  255. else
  256. {
  257. // add log comment
  258. BuildAllProjectResourceEntries();
  259. }
  260. }
  261. void BuildBase::BuildAllProjectResourceEntries()
  262. {
  263. for (unsigned i = 0; i < projectResourceDir_.Size(); i++)
  264. {
  265. String projectResourceDir = projectResourceDir_[i];
  266. Vector<String> fileNamesInProject;
  267. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  268. fileSystem->ScanDir(fileNamesInProject, projectResourceDir, "*.*", SCAN_FILES, true);
  269. for (unsigned i = 0; i < fileNamesInProject.Size(); i++)
  270. {
  271. AddToResourcePackager(fileNamesInProject[i], projectResourceDir);
  272. }
  273. }
  274. }
  275. void BuildBase::BuildFilteredProjectResourceEntries()
  276. {
  277. // Loading up the assetbuildconfig.json,
  278. // obtaining a list of files to include in the build.
  279. VariantMap resourceTags;
  280. AssetBuildConfig::ApplyConfig(resourceTags);
  281. Vector<String> assetBuildConfigFiles;
  282. VariantMap::ConstIterator itr = resourceTags.Begin();
  283. while (itr != resourceTags.End())
  284. {
  285. if (itr->first_ == assetBuildTag_)
  286. {
  287. assetBuildConfigFiles = itr->second_.GetStringVector();
  288. break;
  289. }
  290. itr++;
  291. if (itr == resourceTags.End())
  292. {
  293. LOGERRORF("BuildBase::BuildFilteredProjectResourceEntries - Asset Build Tag \"%s\" not defined in .\\Settings\\assetbuildconfig.json", assetBuildTag_.CString());
  294. }
  295. }
  296. // find any folders defined in assetbuildconfig.json, and add the non-".asset" files in them.
  297. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  298. Vector<String> filesInFolderToAdd;
  299. for (unsigned i = 0; i < assetBuildConfigFiles.Size(); ++i)
  300. {
  301. String &filename = assetBuildConfigFiles[i];
  302. if (GetExtension(filename) == String::EMPTY &&
  303. fileSystem->DirExists(project_->GetResourcePath() + filename))
  304. {
  305. Vector<String> filesInFolder;
  306. fileSystem->ScanDir(filesInFolder, project_->GetResourcePath() + filename, "*.*", SCAN_FILES, true);
  307. for (unsigned j = 0; j < filesInFolder.Size(); ++j)
  308. {
  309. if (GetExtension(filesInFolder[j]) != ".asset")
  310. filesInFolderToAdd.Push(filesInFolder[j]);
  311. }
  312. }
  313. }
  314. // add the files defined using a folder in assetbuildconfig.json
  315. for (unsigned i = 0; i < filesInFolderToAdd.Size(); ++i)
  316. {
  317. assetBuildConfigFiles.Push(filesInFolderToAdd[i]);
  318. }
  319. // check if the files in assetbuildconfig.json exist,
  320. // as well as their corresponding .asset file
  321. Vector<String> filesInResourceFolder;
  322. Vector<String> resourceFilesToInclude;
  323. fileSystem->ScanDir(filesInResourceFolder, project_->GetResourcePath(), "*.*", SCAN_FILES, true);
  324. for (unsigned i = 0; i < assetBuildConfigFiles.Size(); ++i)
  325. {
  326. // .asset file is of primary importance since we used it to identify the associated cached file.
  327. // without the .asset file the resource is removed from being included in the build.
  328. String &filename = assetBuildConfigFiles[i];
  329. if (filesInResourceFolder.Contains(filename) &&
  330. filesInResourceFolder.Contains(filename + ".asset"))
  331. {
  332. resourceFilesToInclude.Push(filename);
  333. resourceFilesToInclude.Push(filename + ".asset");
  334. }
  335. }
  336. // add valid files included from the assetbuildconfig.json
  337. for (auto it = resourceFilesToInclude.Begin(); it != resourceFilesToInclude.End(); ++it)
  338. {
  339. AddToResourcePackager(*it, project_->GetResourcePath());
  340. }
  341. // Get associated cache GUID from the asset file
  342. Vector<String> filesWithGUIDtoInclude;
  343. for (auto it = resourceFilesToInclude.Begin(); it != resourceFilesToInclude.End(); ++it)
  344. {
  345. String &filename = *it;
  346. if (GetExtension(*it) == ".asset")
  347. {
  348. SharedPtr<File> file(new File(context_, project_->GetResourcePath() + *it));
  349. SharedPtr<JSONFile> json(new JSONFile(context_));
  350. json->Load(*file);
  351. file->Close();
  352. JSONValue root = json->GetRoot();
  353. int test = root.Get("version").GetInt();
  354. assert(root.Get("version").GetInt() == ASSET_VERSION);
  355. String guid = root.Get("guid").GetString();
  356. filesWithGUIDtoInclude.Push(guid);
  357. }
  358. }
  359. // Obtain files in cache folder,
  360. // Check if the file contains the guid, and add it to the resourceFilesToInclude
  361. Vector<String> filesInCacheFolder;
  362. Vector<String> cacheFilesToInclude;
  363. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  364. String cachePath = db->GetCachePath();
  365. fileSystem->ScanDir(filesInCacheFolder, cachePath, "*.*", SCAN_FILES, true);
  366. for (unsigned i = 0; i < filesWithGUIDtoInclude.Size(); ++i)
  367. {
  368. String &guid = filesWithGUIDtoInclude[i];
  369. for (unsigned j = 0; j < filesInCacheFolder.Size(); ++j)
  370. {
  371. String &filename = GetFileName(filesInCacheFolder[j]);
  372. if (filename.Contains(guid))
  373. {
  374. cacheFilesToInclude.Push(filesInCacheFolder[j]);
  375. // do not continue...
  376. // there might be multiple files with the same guid having an guid_animaiton extention.
  377. }
  378. }
  379. }
  380. // Add the DDS files when building in windows
  381. #ifdef ATOMIC_PLATFORM_DESKTOP
  382. Vector<String> filesInCacheDDSfolder;
  383. fileSystem->ScanDir(filesInCacheDDSfolder, cachePath + "DDS/", "*.dds", SCAN_FILES, true);
  384. for (unsigned i = 0; i < filesInCacheDDSfolder.Size(); ++i)
  385. {
  386. cacheFilesToInclude.Push(filesInCacheDDSfolder[i]);
  387. }
  388. #endif
  389. // Add the cache files to the resource packager
  390. for (auto it = cacheFilesToInclude.Begin(); it != cacheFilesToInclude.End(); ++it)
  391. {
  392. AddToResourcePackager(*it, cachePath);
  393. }
  394. }
  395. void BuildBase::AddToResourcePackager(const String& filename, const String& resourceDir)
  396. {
  397. // Check if the file is already included in the resourceEntries_ list
  398. for (unsigned j = 0; j < resourceEntries_.Size(); j++)
  399. {
  400. const BuildResourceEntry* entry = resourceEntries_[j];
  401. if (entry->packagePath_ == filename)
  402. {
  403. BuildWarn(ToString("Resource Path: %s already exists", filename.CString()));
  404. continue;
  405. }
  406. }
  407. // Add the file to the resourceEntries_ list
  408. // TODO: Add additional filters
  409. if (GetExtension(filename) == ".psd")
  410. return;
  411. BuildResourceEntry* newEntry = new BuildResourceEntry;
  412. // BEGIN LICENSE MANAGEMENT
  413. if (GetExtension(filename) == ".mdl")
  414. {
  415. containsMDL_ = true;
  416. }
  417. // END LICENSE MANAGEMENT
  418. newEntry->absolutePath_ = resourceDir + filename;
  419. newEntry->resourceDir_ = resourceDir;
  420. newEntry->packagePath_ = filename;
  421. resourceEntries_.Push(newEntry);
  422. assert(resourcePackager_.NotNull());
  423. resourcePackager_->AddResourceEntry(newEntry);
  424. }
  425. void BuildBase::GenerateResourcePackage(const String& resourcePackagePath)
  426. {
  427. resourcePackager_->GeneratePackage(resourcePackagePath);
  428. }
  429. void BuildBase::AddResourceDir(const String& dir)
  430. {
  431. assert(!resourceDirs_.Contains(dir));
  432. resourceDirs_.Push(dir);
  433. }
  434. void BuildBase::AddProjectResourceDir(const String& dir)
  435. {
  436. assert(!projectResourceDir_.Contains(dir));
  437. projectResourceDir_.Push(dir);
  438. }
  439. void BuildBase::ReadAssetBuildConfig()
  440. {
  441. String projectPath = project_->GetProjectPath();
  442. projectPath = RemoveTrailingSlash(projectPath);
  443. String filename = projectPath + "Settings/AssetBuildConfig.json";
  444. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  445. if (!fileSystem->FileExists(filename))
  446. return;
  447. if (AssetBuildConfig::LoadFromFile(context_, filename))
  448. {
  449. VariantMap assetBuildConfig;
  450. AssetBuildConfig::ApplyConfig(assetBuildConfig);
  451. }
  452. }
  453. }