BuildBase.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. bool BuildBase::BuildCreateDirectory(const String& path)
  35. {
  36. if (buildFailed_)
  37. {
  38. LOGERRORF("BuildBase::BuildCreateDirectory - Attempt to create directory of failed build, %s", path.CString());
  39. return false;
  40. }
  41. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  42. if (fileSystem->DirExists(path))
  43. return true;
  44. bool result = fileSystem->CreateDir(path);
  45. if (!result)
  46. {
  47. FailBuild(ToString("BuildBase::BuildCreateDirectory: Unable to create directory %s", path.CString()));
  48. return false;
  49. }
  50. return true;
  51. }
  52. bool BuildBase::BuildCopyFile(const String& srcFileName, const String& destFileName)
  53. {
  54. if (buildFailed_)
  55. {
  56. LOGERRORF("BuildBase::BuildCopyFile - Attempt to copy file of failed build, %s", srcFileName.CString());
  57. return false;
  58. }
  59. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  60. bool result = fileSystem->Copy(srcFileName, destFileName);
  61. if (!result)
  62. {
  63. FailBuild(ToString("BuildBase::BuildCopyFile: Unable to copy file %s -> %s", srcFileName.CString(), destFileName.CString()));
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool BuildBase::BuildRemoveDirectory(const String& path)
  69. {
  70. if (buildFailed_)
  71. {
  72. LOGERRORF("BuildBase::BuildRemoveDirectory - Attempt to remove directory of failed build, %s", path.CString());
  73. return false;
  74. }
  75. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  76. if (!fileSystem->DirExists(path))
  77. return true;
  78. bool result = fileSystem->RemoveDir(path, true);
  79. if (!result)
  80. {
  81. FailBuild(ToString("BuildBase::BuildRemoveDirectory: Unable to remove directory %s", path.CString()));
  82. return false;
  83. }
  84. return true;
  85. }
  86. void BuildBase::BuildLog(const String& message, bool sendEvent)
  87. {
  88. buildLog_.Push(message);
  89. if (sendEvent)
  90. {
  91. String colorMsg = ToString("<color #D4FB79>%s</color>\n", message.CString());
  92. VariantMap buildOutput;
  93. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  94. SendEvent(E_BUILDOUTPUT, buildOutput);
  95. }
  96. }
  97. void BuildBase::BuildWarn(const String& warning, bool sendEvent)
  98. {
  99. buildWarnings_.Push(warning);
  100. if (sendEvent)
  101. {
  102. String colorMsg = ToString("<color #FFFF00>%s</color>\n", warning.CString());
  103. VariantMap buildOutput;
  104. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  105. SendEvent(E_BUILDOUTPUT, buildOutput);
  106. }
  107. }
  108. void BuildBase::BuildError(const String& error, bool sendEvent)
  109. {
  110. buildErrors_.Push(error);
  111. if (sendEvent)
  112. {
  113. String colorMsg = ToString("<color #FF0000>%s</color>\n", error.CString());
  114. VariantMap buildOutput;
  115. buildOutput[BuildOutput::P_TEXT] = colorMsg;
  116. SendEvent(E_BUILDOUTPUT, buildOutput);
  117. }
  118. }
  119. void BuildBase::FailBuild(const String& message)
  120. {
  121. if (buildFailed_)
  122. {
  123. LOGERRORF("BuildBase::FailBuild - Attempt to fail already failed build: %s", message.CString());
  124. return;
  125. }
  126. buildFailed_ = true;
  127. BuildError(message);
  128. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  129. buildSystem->BuildComplete(platformID_, buildPath_, false, message);
  130. }
  131. void BuildBase::HandleSubprocessOutputEvent(StringHash eventType, VariantMap& eventData)
  132. {
  133. // E_SUBPROCESSOUTPUT
  134. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  135. // convert to a build output event and forward to subscribers
  136. VariantMap buildOutputData;
  137. buildOutputData[BuildOutput::P_TEXT] = text;
  138. SendEvent(E_BUILDOUTPUT, buildOutputData);
  139. }
  140. void BuildBase::GetDefaultResourcePaths(Vector<String>& paths)
  141. {
  142. paths.Clear();
  143. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  144. paths.Push(AddTrailingSlash(tenv->GetCoreDataDir()));
  145. paths.Push(AddTrailingSlash(tenv->GetPlayerDataDir()));
  146. }
  147. void BuildBase::ScanResourceDirectory(const String& resourceDir)
  148. {
  149. Vector<String> fileNames;
  150. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  151. fileSystem->ScanDir(fileNames, resourceDir, "*.*", SCAN_FILES, true);
  152. for (unsigned i = 0; i < fileNames.Size(); i++)
  153. {
  154. const String& filename = fileNames[i];
  155. for (unsigned j = 0; j < resourceEntries_.Size(); j++)
  156. {
  157. const BuildResourceEntry* entry = resourceEntries_[j];
  158. if (entry->packagePath_ == filename)
  159. {
  160. BuildWarn(ToString("Resource Path: %s already exists", filename.CString()));
  161. continue;
  162. }
  163. }
  164. // TODO: Add additional filters
  165. if (GetExtension(filename) == ".psd")
  166. continue;
  167. BuildResourceEntry* newEntry = new BuildResourceEntry;
  168. // BEGIN LICENSE MANAGEMENT
  169. if (GetExtension(filename) == ".mdl")
  170. {
  171. containsMDL_ = true;
  172. }
  173. // END LICENSE MANAGEMENT
  174. newEntry->absolutePath_ = resourceDir + filename;
  175. newEntry->resourceDir_ = resourceDir;
  176. newEntry->packagePath_ = filename;
  177. resourceEntries_.Push(newEntry);
  178. //LOGINFOF("Adding resource: %s : %s", newEntry->absolutePath_.CString(), newEntry->packagePath_.CString());
  179. }
  180. }
  181. void BuildBase::BuildResourceEntries()
  182. {
  183. for (unsigned i = 0; i < resourceDirs_.Size(); i++)
  184. {
  185. ScanResourceDirectory(resourceDirs_[i]);
  186. }
  187. if (resourcePackager_.NotNull())
  188. {
  189. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  190. {
  191. BuildResourceEntry* entry = resourceEntries_[i];
  192. resourcePackager_->AddResourceEntry(entry);
  193. }
  194. }
  195. }
  196. void BuildBase::GenerateResourcePackage(const String& resourcePackagePath)
  197. {
  198. resourcePackager_->GeneratePackage(resourcePackagePath);
  199. }
  200. void BuildBase::AddResourceDir(const String& dir)
  201. {
  202. assert(!resourceDirs_.Contains(dir));
  203. resourceDirs_.Push(dir);
  204. }
  205. }