BuildBase.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/FileSystem.h>
  9. #include "../Subprocess/SubprocessSystem.h"
  10. #include "../Project/Project.h"
  11. #include "../ToolEnvironment.h"
  12. #include "BuildSystem.h"
  13. #include "BuildEvents.h"
  14. #include "BuildBase.h"
  15. #include "ResourcePackager.h"
  16. namespace ToolCore
  17. {
  18. BuildBase::BuildBase(Context * context, Project* project, PlatformID platform) : Object(context),
  19. platformID_(platform),
  20. containsMDL_(false),
  21. buildFailed_(false)
  22. {
  23. if (UseResourcePackager())
  24. resourcePackager_ = new ResourcePackager(context, this);
  25. project_ = project;
  26. }
  27. BuildBase::~BuildBase()
  28. {
  29. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  30. {
  31. delete resourceEntries_[i];
  32. }
  33. }
  34. #ifdef ATOMIC_PLATFORM_WINDOWS
  35. bool BuildBase::BuildClean(const String& path)
  36. {
  37. if (buildFailed_)
  38. {
  39. LOGERRORF("BuildBase::BuildClean - Attempt to clean directory of failed build, %s", path.CString());
  40. return false;
  41. }
  42. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  43. if (!fileSystem->DirExists(path))
  44. return true;
  45. // On Windows, do a little dance with the folder to avoid issues
  46. // with deleting folder and immediately recreating it
  47. String pathName, fileName, ext;
  48. SplitPath(path, pathName, fileName, ext);
  49. pathName = AddTrailingSlash(pathName);
  50. unsigned i = 0;
  51. while (true)
  52. {
  53. String newPath = ToString("%s%s_Temp_%u", pathName.CString(), fileName.CString(), i++);
  54. if (!fileSystem->DirExists(newPath))
  55. {
  56. if (!MoveFileExW(GetWideNativePath(path).CString(), GetWideNativePath(newPath).CString(), MOVEFILE_WRITE_THROUGH))
  57. {
  58. FailBuild(ToString("BuildBase::BuildClean: Unable to move directory %s -> ", path.CString(), newPath.CString()));
  59. return false;
  60. }
  61. // Remove the moved directory
  62. return BuildRemoveDirectory(newPath);
  63. }
  64. else
  65. {
  66. LOGWARNINGF("BuildBase::BuildClean - temp build folder exists, removing: %s", newPath.CString());
  67. fileSystem->RemoveDir(newPath, true);
  68. }
  69. if (i == 255)
  70. {
  71. FailBuild(ToString("BuildBase::BuildClean: Unable to move directory ( i == 255) %s -> ", path.CString(), newPath.CString()));
  72. return false;
  73. }
  74. }
  75. return false;
  76. }
  77. #else
  78. bool BuildBase::BuildClean(const String& path)
  79. {
  80. return BuildRemoveDirectory(path);
  81. }
  82. #endif
  83. bool BuildBase::BuildCreateDirectory(const String& path)
  84. {
  85. if (buildFailed_)
  86. {
  87. LOGERRORF("BuildBase::BuildCreateDirectory - Attempt to create directory of failed build, %s", path.CString());
  88. return false;
  89. }
  90. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  91. if (fileSystem->DirExists(path))
  92. return true;
  93. bool result = fileSystem->CreateDir(path);
  94. if (!result)
  95. {
  96. FailBuild(ToString("BuildBase::BuildCreateDirectory: Unable to create directory %s", path.CString()));
  97. return false;
  98. }
  99. return true;
  100. }
  101. bool BuildBase::BuildCopyFile(const String& srcFileName, const String& destFileName)
  102. {
  103. if (buildFailed_)
  104. {
  105. LOGERRORF("BuildBase::BuildCopyFile - Attempt to copy file of failed build, %s", srcFileName.CString());
  106. return false;
  107. }
  108. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  109. bool result = fileSystem->Copy(srcFileName, destFileName);
  110. if (!result)
  111. {
  112. FailBuild(ToString("BuildBase::BuildCopyFile: Unable to copy file %s -> %s", srcFileName.CString(), destFileName.CString()));
  113. return false;
  114. }
  115. return true;
  116. }
  117. bool BuildBase::BuildRemoveDirectory(const String& path)
  118. {
  119. if (buildFailed_)
  120. {
  121. LOGERRORF("BuildBase::BuildRemoveDirectory - Attempt to remove directory of failed build, %s", path.CString());
  122. return false;
  123. }
  124. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  125. if (!fileSystem->DirExists(path))
  126. return true;
  127. bool result = fileSystem->RemoveDir(path, true);
  128. if (!result)
  129. {
  130. FailBuild(ToString("BuildBase::BuildRemoveDirectory: Unable to remove directory %s", path.CString()));
  131. return false;
  132. }
  133. return true;
  134. }
  135. void BuildBase::BuildLog(const String& message, bool sendEvent)
  136. {
  137. buildLog_.Push(message);
  138. if (sendEvent)
  139. {
  140. String colorMsg = ToString("<color #D4FB79>%s</color>\n", message.CString());
  141. VariantMap buildOutput;
  142. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  143. SendEvent(E_BUILDOUTPUT, buildOutput);
  144. }
  145. }
  146. void BuildBase::BuildWarn(const String& warning, bool sendEvent)
  147. {
  148. buildWarnings_.Push(warning);
  149. if (sendEvent)
  150. {
  151. String colorMsg = ToString("<color #FFFF00>%s</color>\n", warning.CString());
  152. VariantMap buildOutput;
  153. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  154. SendEvent(E_BUILDOUTPUT, buildOutput);
  155. }
  156. }
  157. void BuildBase::BuildError(const String& error, bool sendEvent)
  158. {
  159. buildErrors_.Push(error);
  160. if (sendEvent)
  161. {
  162. String colorMsg = ToString("<color #FF0000>%s</color>\n", error.CString());
  163. VariantMap buildOutput;
  164. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  165. SendEvent(E_BUILDOUTPUT, buildOutput);
  166. }
  167. }
  168. void BuildBase::FailBuild(const String& message)
  169. {
  170. if (buildFailed_)
  171. {
  172. LOGERRORF("BuildBase::FailBuild - Attempt to fail already failed build: %s", message.CString());
  173. return;
  174. }
  175. buildFailed_ = true;
  176. BuildError(message);
  177. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  178. buildSystem->BuildComplete(platformID_, buildPath_, false, message);
  179. }
  180. void BuildBase::HandleSubprocessOutputEvent(StringHash eventType, VariantMap& eventData)
  181. {
  182. // E_SUBPROCESSOUTPUT
  183. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  184. // convert to a build output event and forward to subscribers
  185. VariantMap buildOutputData;
  186. buildOutputData[BuildOutput::P_TEXT] = text;
  187. SendEvent(E_BUILDOUTPUT, buildOutputData);
  188. }
  189. void BuildBase::GetDefaultResourcePaths(Vector<String>& paths)
  190. {
  191. paths.Clear();
  192. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  193. paths.Push(AddTrailingSlash(tenv->GetCoreDataDir()));
  194. paths.Push(AddTrailingSlash(tenv->GetPlayerDataDir()));
  195. }
  196. String BuildBase::GetSettingsDirectory()
  197. {
  198. return project_->GetProjectPath() + "/Settings";
  199. }
  200. void BuildBase::ScanResourceDirectory(const String& resourceDir)
  201. {
  202. Vector<String> fileNames;
  203. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  204. fileSystem->ScanDir(fileNames, resourceDir, "*.*", SCAN_FILES, true);
  205. for (unsigned i = 0; i < fileNames.Size(); i++)
  206. {
  207. const String& filename = fileNames[i];
  208. for (unsigned j = 0; j < resourceEntries_.Size(); j++)
  209. {
  210. const BuildResourceEntry* entry = resourceEntries_[j];
  211. if (entry->packagePath_ == filename)
  212. {
  213. BuildWarn(ToString("Resource Path: %s already exists", filename.CString()));
  214. continue;
  215. }
  216. }
  217. // TODO: Add additional filters
  218. if (GetExtension(filename) == ".psd")
  219. continue;
  220. BuildResourceEntry* newEntry = new BuildResourceEntry;
  221. // BEGIN LICENSE MANAGEMENT
  222. if (GetExtension(filename) == ".mdl")
  223. {
  224. containsMDL_ = true;
  225. }
  226. // END LICENSE MANAGEMENT
  227. newEntry->absolutePath_ = resourceDir + filename;
  228. newEntry->resourceDir_ = resourceDir;
  229. newEntry->packagePath_ = filename;
  230. resourceEntries_.Push(newEntry);
  231. //LOGINFOF("Adding resource: %s : %s", newEntry->absolutePath_.CString(), newEntry->packagePath_.CString());
  232. }
  233. }
  234. void BuildBase::BuildResourceEntries()
  235. {
  236. for (unsigned i = 0; i < resourceDirs_.Size(); i++)
  237. {
  238. ScanResourceDirectory(resourceDirs_[i]);
  239. }
  240. if (resourcePackager_.NotNull())
  241. {
  242. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  243. {
  244. BuildResourceEntry* entry = resourceEntries_[i];
  245. resourcePackager_->AddResourceEntry(entry);
  246. }
  247. }
  248. }
  249. void BuildBase::GenerateResourcePackage(const String& resourcePackagePath)
  250. {
  251. resourcePackager_->GeneratePackage(resourcePackagePath);
  252. }
  253. void BuildBase::AddResourceDir(const String& dir)
  254. {
  255. assert(!resourceDirs_.Contains(dir));
  256. resourceDirs_.Push(dir);
  257. }
  258. }