BuildBase.cpp 18 KB

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