rcjob.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "rcjob.h"
  9. #include <AzToolsFramework/UI/Logging/LogLine.h>
  10. #include <AzToolsFramework/Metadata/UuidUtils.h>
  11. #include <native/utilities/BuilderManager.h>
  12. #include <native/utilities/ThreadHelper.h>
  13. #include <QtConcurrent/QtConcurrentRun>
  14. #include <QElapsedTimer>
  15. #include "native/utilities/JobDiagnosticTracker.h"
  16. #include <qstorageinfo.h>
  17. #include <native/utilities/ProductOutputUtil.h>
  18. namespace
  19. {
  20. bool s_typesRegistered = false;
  21. // You have up to 60 minutes to finish processing an asset.
  22. // This was increased from 10 to account for PVRTC compression
  23. // taking up to an hour for large normal map textures, and should
  24. // be reduced again once we move to the ASTC compression format, or
  25. // find another solution to reduce processing times to be reasonable.
  26. const unsigned int g_jobMaximumWaitTime = 1000 * 60 * 60;
  27. const unsigned int g_sleepDurationForLockingAndFingerprintChecking = 100;
  28. const unsigned int g_timeoutInSecsForRetryingCopy = 30;
  29. const char* const s_tempString = "%TEMP%";
  30. const char* const s_jobLogFileName = "jobLog.xml";
  31. bool MoveCopyFile(QString sourceFile, QString productFile, bool isCopyJob = false)
  32. {
  33. if (!isCopyJob && (AssetUtilities::MoveFileWithTimeout(sourceFile, productFile, g_timeoutInSecsForRetryingCopy)))
  34. {
  35. //We do not want to rename the file if it is a copy job
  36. return true;
  37. }
  38. else if (AssetUtilities::CopyFileWithTimeout(sourceFile, productFile, g_timeoutInSecsForRetryingCopy))
  39. {
  40. // try to copy instead
  41. return true;
  42. }
  43. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to move OR copy file from Source directory: %s to Destination Directory: %s", sourceFile.toUtf8().data(), productFile.toUtf8().data());
  44. return false;
  45. }
  46. }
  47. using namespace AssetProcessor;
  48. bool Params::IsValidParams() const
  49. {
  50. return !m_cacheOutputDir.empty() && !m_intermediateOutputDir.empty() && !m_relativePath.empty();
  51. }
  52. bool RCParams::IsValidParams() const
  53. {
  54. return (
  55. (!m_rcExe.isEmpty()) &&
  56. (!m_rootDir.isEmpty()) &&
  57. (!m_inputFile.isEmpty()) &&
  58. Params::IsValidParams()
  59. );
  60. }
  61. namespace AssetProcessor
  62. {
  63. RCJob::RCJob(QObject* parent)
  64. : QObject(parent)
  65. , m_timeCreated(QDateTime::currentDateTime())
  66. , m_scanFolderID(0)
  67. {
  68. m_jobState = RCJob::pending;
  69. if (!s_typesRegistered)
  70. {
  71. qRegisterMetaType<RCParams>("RCParams");
  72. qRegisterMetaType<BuilderParams>("BuilderParams");
  73. qRegisterMetaType<JobOutputInfo>("JobOutputInfo");
  74. s_typesRegistered = true;
  75. }
  76. }
  77. RCJob::~RCJob()
  78. {
  79. }
  80. void RCJob::Init(JobDetails& details)
  81. {
  82. m_jobDetails = AZStd::move(details);
  83. m_queueElementID = QueueElementID(GetJobEntry().m_sourceAssetReference, GetPlatformInfo().m_identifier.c_str(), GetJobKey());
  84. }
  85. const JobEntry& RCJob::GetJobEntry() const
  86. {
  87. return m_jobDetails.m_jobEntry;
  88. }
  89. bool RCJob::HasMissingSourceDependency() const
  90. {
  91. return m_jobDetails.m_hasMissingSourceDependency;
  92. }
  93. QDateTime RCJob::GetTimeCreated() const
  94. {
  95. return m_timeCreated;
  96. }
  97. void RCJob::SetTimeCreated(const QDateTime& timeCreated)
  98. {
  99. m_timeCreated = timeCreated;
  100. }
  101. QDateTime RCJob::GetTimeLaunched() const
  102. {
  103. return m_timeLaunched;
  104. }
  105. void RCJob::SetTimeLaunched(const QDateTime& timeLaunched)
  106. {
  107. m_timeLaunched = timeLaunched;
  108. }
  109. QDateTime RCJob::GetTimeCompleted() const
  110. {
  111. return m_timeCompleted;
  112. }
  113. void RCJob::SetTimeCompleted(const QDateTime& timeCompleted)
  114. {
  115. m_timeCompleted = timeCompleted;
  116. }
  117. AZ::u32 RCJob::GetOriginalFingerprint() const
  118. {
  119. return m_jobDetails.m_jobEntry.m_computedFingerprint;
  120. }
  121. void RCJob::SetOriginalFingerprint(unsigned int fingerprint)
  122. {
  123. m_jobDetails.m_jobEntry.m_computedFingerprint = fingerprint;
  124. }
  125. RCJob::JobState RCJob::GetState() const
  126. {
  127. return m_jobState;
  128. }
  129. void RCJob::SetState(const JobState& state)
  130. {
  131. bool wasPending = (m_jobState == pending);
  132. m_jobState = state;
  133. if ((wasPending)&&(m_jobState == cancelled))
  134. {
  135. // if we were pending (had not started yet) and we are now canceled, we still have to emit the finished signal
  136. // so that all the various systems waiting for us can do their housekeeping.
  137. Q_EMIT Finished();
  138. }
  139. }
  140. void RCJob::SetJobEscalation(int jobEscalation)
  141. {
  142. m_JobEscalation = jobEscalation;
  143. }
  144. void RCJob::SetCheckExclusiveLock(bool value)
  145. {
  146. m_jobDetails.m_jobEntry.m_checkExclusiveLock = value;
  147. }
  148. QString RCJob::GetStateDescription(const RCJob::JobState& state)
  149. {
  150. switch (state)
  151. {
  152. case RCJob::pending:
  153. return tr("Pending");
  154. case RCJob::processing:
  155. return tr("Processing");
  156. case RCJob::completed:
  157. return tr("Completed");
  158. case RCJob::crashed:
  159. return tr("Crashed");
  160. case RCJob::terminated:
  161. return tr("Terminated");
  162. case RCJob::failed:
  163. return tr("Failed");
  164. case RCJob::cancelled:
  165. return tr("Cancelled");
  166. }
  167. return QString();
  168. }
  169. const AZ::Uuid& RCJob::GetInputFileUuid() const
  170. {
  171. return m_jobDetails.m_jobEntry.m_sourceFileUUID;
  172. }
  173. AZ::IO::Path RCJob::GetCacheOutputPath() const
  174. {
  175. return m_jobDetails.m_cachePath;
  176. }
  177. AZ::IO::Path RCJob::GetIntermediateOutputPath() const
  178. {
  179. return m_jobDetails.m_intermediatePath;
  180. }
  181. AZ::IO::Path RCJob::GetRelativePath() const
  182. {
  183. return m_jobDetails.m_relativePath;
  184. }
  185. const AssetBuilderSDK::PlatformInfo& RCJob::GetPlatformInfo() const
  186. {
  187. return m_jobDetails.m_jobEntry.m_platformInfo;
  188. }
  189. AssetBuilderSDK::ProcessJobResponse& RCJob::GetProcessJobResponse()
  190. {
  191. return m_processJobResponse;
  192. }
  193. void RCJob::PopulateProcessJobRequest(AssetBuilderSDK::ProcessJobRequest& processJobRequest)
  194. {
  195. processJobRequest.m_jobDescription.m_critical = IsCritical();
  196. processJobRequest.m_jobDescription.m_additionalFingerprintInfo = m_jobDetails.m_extraInformationForFingerprinting;
  197. processJobRequest.m_jobDescription.m_jobKey = GetJobKey().toUtf8().data();
  198. processJobRequest.m_jobDescription.m_jobParameters = AZStd::move(m_jobDetails.m_jobParam);
  199. processJobRequest.m_jobDescription.SetPlatformIdentifier(GetPlatformInfo().m_identifier.c_str());
  200. processJobRequest.m_jobDescription.m_priority = GetPriority();
  201. processJobRequest.m_platformInfo = GetPlatformInfo();
  202. processJobRequest.m_builderGuid = GetBuilderGuid();
  203. processJobRequest.m_sourceFile = GetJobEntry().m_sourceAssetReference.RelativePath().c_str();
  204. processJobRequest.m_sourceFileUUID = GetInputFileUuid();
  205. processJobRequest.m_watchFolder = GetJobEntry().m_sourceAssetReference.ScanFolderPath().c_str();
  206. processJobRequest.m_fullPath = GetJobEntry().GetAbsoluteSourcePath().toUtf8().data();
  207. processJobRequest.m_jobId = GetJobEntry().m_jobRunKey;
  208. }
  209. QString RCJob::GetJobKey() const
  210. {
  211. return m_jobDetails.m_jobEntry.m_jobKey;
  212. }
  213. AZ::Uuid RCJob::GetBuilderGuid() const
  214. {
  215. return m_jobDetails.m_jobEntry.m_builderGuid;
  216. }
  217. bool RCJob::IsCritical() const
  218. {
  219. return m_jobDetails.m_critical;
  220. }
  221. bool RCJob::IsAutoFail() const
  222. {
  223. return m_jobDetails.m_autoFail;
  224. }
  225. int RCJob::GetPriority() const
  226. {
  227. return m_jobDetails.m_priority;
  228. }
  229. const AZStd::vector<AssetProcessor::JobDependencyInternal>& RCJob::GetJobDependencies()
  230. {
  231. return m_jobDetails.m_jobDependencyList;
  232. }
  233. void RCJob::Start()
  234. {
  235. // the following trace can be uncommented if there is a need to deeply inspect job running.
  236. //AZ_TracePrintf(AssetProcessor::DebugChannel, "JobTrace Start(%i %s,%s,%s)\n", this, GetInputFileAbsolutePath().toUtf8().data(), GetPlatform().toUtf8().data(), GetJobKey().toUtf8().data());
  237. AssetUtilities::QuitListener listener;
  238. listener.BusConnect();
  239. RCParams rc(this);
  240. BuilderParams builderParams(this);
  241. //Create the process job request
  242. AssetBuilderSDK::ProcessJobRequest processJobRequest;
  243. PopulateProcessJobRequest(processJobRequest);
  244. builderParams.m_processJobRequest = processJobRequest;
  245. builderParams.m_cacheOutputDir = GetCacheOutputPath();
  246. builderParams.m_intermediateOutputDir = GetIntermediateOutputPath();
  247. builderParams.m_relativePath = GetRelativePath();
  248. builderParams.m_assetBuilderDesc = m_jobDetails.m_assetBuilderDesc;
  249. builderParams.m_sourceUuid = m_jobDetails.m_sourceUuid;
  250. // when the job finishes, record the results and emit Finished()
  251. connect(this, &RCJob::JobFinished, this, [this](AssetBuilderSDK::ProcessJobResponse result)
  252. {
  253. m_processJobResponse = AZStd::move(result);
  254. switch (m_processJobResponse.m_resultCode)
  255. {
  256. case AssetBuilderSDK::ProcessJobResult_Crashed:
  257. {
  258. SetState(crashed);
  259. }
  260. break;
  261. case AssetBuilderSDK::ProcessJobResult_Success:
  262. {
  263. SetState(completed);
  264. }
  265. break;
  266. case AssetBuilderSDK::ProcessJobResult_Cancelled:
  267. {
  268. SetState(cancelled);
  269. }
  270. break;
  271. default:
  272. {
  273. SetState(failed);
  274. }
  275. break;
  276. }
  277. Q_EMIT Finished();
  278. });
  279. if (!listener.WasQuitRequested())
  280. {
  281. QtConcurrent::run(&RCJob::ExecuteBuilderCommand, builderParams);
  282. }
  283. else
  284. {
  285. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Job canceled due to quit being requested.");
  286. SetState(terminated);
  287. Q_EMIT Finished();
  288. }
  289. listener.BusDisconnect();
  290. }
  291. void RCJob::ExecuteBuilderCommand(BuilderParams builderParams)
  292. {
  293. // Note: this occurs inside a worker thread.
  294. // Signal start and end of the job
  295. ScopedJobSignaler signaler;
  296. // listen for the user quitting (CTRL-C or otherwise)
  297. AssetUtilities::QuitListener listener;
  298. listener.BusConnect();
  299. QElapsedTimer ticker;
  300. ticker.start();
  301. AssetBuilderSDK::ProcessJobResponse result;
  302. AssetBuilderSDK::JobCancelListener cancelListener(builderParams.m_processJobRequest.m_jobId);
  303. if (builderParams.m_rcJob->m_jobDetails.m_autoFail)
  304. {
  305. // if this is an auto-fail job, we should avoid doing any additional work besides the work required to fail the job and
  306. // write the details into its log. This is because Auto-fail jobs have 'incomplete' job descriptors, and only exist to
  307. // force a job to fail with a reasonable log file stating the reason for failure. An example of where it is useful to
  308. // use auto-fail jobs is when, after compilation was successful, something goes wrong integrating the result into the
  309. // cache. (For example, files collide, or the product file name would be too long). The job will have at that point
  310. // already completed, the thread long gone, so we can 'append' to the log in this manner post-build by creating a new
  311. // job that will automatically fail and ingest the old (success) log along with additional fail reasons and then fail.
  312. AutoFailJob(builderParams);
  313. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  314. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  315. return;
  316. }
  317. // If requested, make sure we can open the file with exclusive permissions
  318. QString inputFile = builderParams.m_rcJob->GetJobEntry().GetAbsoluteSourcePath();
  319. if (builderParams.m_rcJob->GetJobEntry().m_checkExclusiveLock && QFile::exists(inputFile))
  320. {
  321. // We will only continue once we get exclusive lock on the source file
  322. while (!AssetUtilities::CheckCanLock(inputFile))
  323. {
  324. // Wait for a while before checking again, we need to let some time pass for the other process to finish whatever work it is doing
  325. QThread::msleep(g_sleepDurationForLockingAndFingerprintChecking);
  326. // If AP shutdown is requested, the job is canceled or we exceeded the max wait time, abort the loop and mark the job as canceled
  327. if (listener.WasQuitRequested() || cancelListener.IsCancelled() || (ticker.elapsed() > g_jobMaximumWaitTime))
  328. {
  329. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Cancelled;
  330. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  331. return;
  332. }
  333. }
  334. }
  335. Q_EMIT builderParams.m_rcJob->BeginWork();
  336. // We will actually start working on the job after this point and even if RcController gets the same job again, we will put it in the queue for processing
  337. builderParams.m_rcJob->DoWork(result, builderParams, listener);
  338. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  339. }
  340. void RCJob::AutoFailJob(BuilderParams& builderParams)
  341. {
  342. // force the fail data to be captured to the log file.
  343. // because this is being executed in a thread worker, this won't stomp the main thread's job id.
  344. AssetProcessor::SetThreadLocalJobId(builderParams.m_rcJob->GetJobEntry().m_jobRunKey);
  345. AssetUtilities::JobLogTraceListener jobLogTraceListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry);
  346. #if defined(AZ_ENABLE_TRACING)
  347. QString sourceFullPath(builderParams.m_processJobRequest.m_fullPath.c_str());
  348. auto failReason = builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC_CE(AssetProcessor::AutoFailReasonKey));
  349. if (failReason != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  350. {
  351. // you are allowed to have many lines in your fail reason.
  352. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failed processing %s", sourceFullPath.toUtf8().data());
  353. AZStd::vector<AZStd::string> delimited;
  354. AzFramework::StringFunc::Tokenize(failReason->second.c_str(), delimited, "\n");
  355. for (const AZStd::string& token : delimited)
  356. {
  357. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "%s", token.c_str());
  358. }
  359. }
  360. else
  361. {
  362. // since we didn't have a custom auto-fail reason, add a token to the log file that will help with
  363. // forensic debugging to differentiate auto-fails from regular fails (although it should also be
  364. // obvious from the output in other ways)
  365. AZ_TracePrintf("Debug", "(auto-failed)\n");
  366. }
  367. auto failLogFile = builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC_CE(AssetProcessor::AutoFailLogFile));
  368. if (failLogFile != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  369. {
  370. AzToolsFramework::Logging::LogLine::ParseLog(failLogFile->second.c_str(), failLogFile->second.size(),
  371. [](AzToolsFramework::Logging::LogLine& target)
  372. {
  373. switch (target.GetLogType())
  374. {
  375. case AzToolsFramework::Logging::LogLine::TYPE_DEBUG:
  376. AZ_TracePrintf(target.GetLogWindow().c_str(), "%s", target.GetLogMessage().c_str());
  377. break;
  378. case AzToolsFramework::Logging::LogLine::TYPE_MESSAGE:
  379. AZ_TracePrintf(target.GetLogWindow().c_str(), "%s", target.GetLogMessage().c_str());
  380. break;
  381. case AzToolsFramework::Logging::LogLine::TYPE_WARNING:
  382. AZ_Warning(target.GetLogWindow().c_str(), false, "%s", target.GetLogMessage().c_str());
  383. break;
  384. case AzToolsFramework::Logging::LogLine::TYPE_ERROR:
  385. AZ_Error(target.GetLogWindow().c_str(), false, "%s", target.GetLogMessage().c_str());
  386. break;
  387. case AzToolsFramework::Logging::LogLine::TYPE_CONTEXT:
  388. AZ_TracePrintf(target.GetLogWindow().c_str(), " %s", target.GetLogMessage().c_str());
  389. break;
  390. }
  391. });
  392. }
  393. #endif
  394. // note that this line below is printed out to be consistent with the output from a job that normally failed, so
  395. // applications reading log file will find it.
  396. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Builder indicated that the job has failed.\n");
  397. if (builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC_CE(AssetProcessor::AutoFailOmitFromDatabaseKey)) != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  398. {
  399. // we don't add Auto-fail jobs to the database if they have asked to be emitted.
  400. builderParams.m_rcJob->m_jobDetails.m_jobEntry.m_addToDatabase = false;
  401. }
  402. AssetProcessor::SetThreadLocalJobId(0);
  403. }
  404. void RCJob::DoWork(AssetBuilderSDK::ProcessJobResponse& result, BuilderParams& builderParams, AssetUtilities::QuitListener& listener)
  405. {
  406. // Setting job id for logging purposes
  407. AssetProcessor::SetThreadLocalJobId(builderParams.m_rcJob->GetJobEntry().m_jobRunKey);
  408. AssetUtilities::JobLogTraceListener jobLogTraceListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry);
  409. {
  410. AssetBuilderSDK::JobCancelListener JobCancelListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry.m_jobRunKey);
  411. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed; // failed by default
  412. #if defined(AZ_ENABLE_TRACING)
  413. for (const auto& warningMessage : builderParams.m_rcJob->m_jobDetails.m_warnings)
  414. {
  415. // you are allowed to have many lines in your warning message.
  416. AZStd::vector<AZStd::string> delimited;
  417. AzFramework::StringFunc::Tokenize(warningMessage.c_str(), delimited, "\n");
  418. for (const AZStd::string& token : delimited)
  419. {
  420. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "%s", token.c_str());
  421. }
  422. jobLogTraceListener.AddWarning();
  423. }
  424. #endif
  425. // create a temporary directory for Builder to work in.
  426. // lets make it as a subdir of a known temp dir
  427. QString workFolder;
  428. if (!AssetUtilities::CreateTempWorkspace(workFolder))
  429. {
  430. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Could not create temporary directory for Builder!\n");
  431. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  432. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  433. return;
  434. }
  435. builderParams.m_processJobRequest.m_tempDirPath = AZStd::string(workFolder.toUtf8().data());
  436. QString sourceFullPath(builderParams.m_processJobRequest.m_fullPath.c_str());
  437. if (sourceFullPath.length() >= ASSETPROCESSOR_WARN_PATH_LEN && sourceFullPath.length() < ASSETPROCESSOR_TRAIT_MAX_PATH_LEN)
  438. {
  439. AZ_Warning(
  440. AssetBuilderSDK::WarningWindow,
  441. false,
  442. "Source Asset: %s filepath length %d exceeds the suggested max path length (%d). This may not work on all platforms.\n",
  443. sourceFullPath.toUtf8().data(),
  444. sourceFullPath.length(),
  445. ASSETPROCESSOR_WARN_PATH_LEN);
  446. }
  447. if (sourceFullPath.length() >= ASSETPROCESSOR_TRAIT_MAX_PATH_LEN)
  448. {
  449. AZ_Warning(
  450. AssetBuilderSDK::WarningWindow,
  451. false,
  452. "Source Asset: %s filepath length %d exceeds the maximum path length (%d) allowed.\n",
  453. sourceFullPath.toUtf8().data(),
  454. sourceFullPath.length(),
  455. ASSETPROCESSOR_TRAIT_MAX_PATH_LEN);
  456. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  457. }
  458. else
  459. {
  460. if (!JobCancelListener.IsCancelled())
  461. {
  462. bool runProcessJob = true;
  463. if (m_jobDetails.m_checkServer)
  464. {
  465. AssetServerMode assetServerMode = AssetServerMode::Inactive;
  466. AssetServerBus::BroadcastResult(assetServerMode, &AssetServerBus::Events::GetRemoteCachingMode);
  467. QFileInfo fileInfo(builderParams.m_processJobRequest.m_sourceFile.c_str());
  468. builderParams.m_serverKey = QString("%1_%2_%3_%4")
  469. .arg(fileInfo.completeBaseName(),
  470. builderParams.m_processJobRequest.m_jobDescription.m_jobKey.c_str(),
  471. builderParams.m_processJobRequest.m_platformInfo.m_identifier.c_str())
  472. .arg(builderParams.m_rcJob->GetOriginalFingerprint());
  473. bool operationResult = false;
  474. if (assetServerMode == AssetServerMode::Server)
  475. {
  476. // sending process job command to the builder
  477. builderParams.m_assetBuilderDesc.m_processJobFunction(builderParams.m_processJobRequest, result);
  478. runProcessJob = false;
  479. if (result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  480. {
  481. auto beforeStoreResult = BeforeStoringJobResult(builderParams, result);
  482. if (beforeStoreResult.IsSuccess())
  483. {
  484. AssetProcessor::AssetServerBus::BroadcastResult(operationResult, &AssetProcessor::AssetServerBusTraits::StoreJobResult, builderParams, beforeStoreResult.GetValue());
  485. }
  486. else
  487. {
  488. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "Failed preparing store result for %s", builderParams.m_processJobRequest.m_sourceFile.c_str());
  489. }
  490. if (!operationResult)
  491. {
  492. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to save job (%s, %s, %s) with fingerprint (%u) to the server.\n",
  493. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  494. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str(), builderParams.m_rcJob->GetOriginalFingerprint());
  495. }
  496. else
  497. {
  498. for (auto& product : result.m_outputProducts)
  499. {
  500. product.m_outputFlags |= AssetBuilderSDK::ProductOutputFlags::CachedAsset;
  501. }
  502. }
  503. }
  504. }
  505. else if (assetServerMode == AssetServerMode::Client)
  506. {
  507. // running as client, check with the server whether it has already
  508. // processed this asset, if not or if the operation fails then process locally
  509. AssetProcessor::AssetServerBus::BroadcastResult(operationResult, &AssetProcessor::AssetServerBusTraits::RetrieveJobResult, builderParams);
  510. if (operationResult)
  511. {
  512. operationResult = AfterRetrievingJobResult(builderParams, jobLogTraceListener, result);
  513. }
  514. else
  515. {
  516. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to get job (%s, %s, %s) with fingerprint (%u) from the server. Processing locally.\n",
  517. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  518. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str(), builderParams.m_rcJob->GetOriginalFingerprint());
  519. }
  520. if (operationResult)
  521. {
  522. for (auto& product : result.m_outputProducts)
  523. {
  524. product.m_outputFlags |= AssetBuilderSDK::ProductOutputFlags::CachedAsset;
  525. }
  526. }
  527. runProcessJob = !operationResult;
  528. }
  529. }
  530. if(runProcessJob)
  531. {
  532. result.m_outputProducts.clear();
  533. // sending process job command to the builder
  534. builderParams.m_assetBuilderDesc.m_processJobFunction(builderParams.m_processJobRequest, result);
  535. }
  536. }
  537. }
  538. if (JobCancelListener.IsCancelled())
  539. {
  540. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Cancelled;
  541. }
  542. }
  543. bool shouldRemoveTempFolder = true;
  544. if (result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  545. {
  546. // do a final check of this job to make sure its not making colliding subIds.
  547. AZStd::unordered_map<AZ::u32, AZStd::string> subIdsFound;
  548. for (const AssetBuilderSDK::JobProduct& product : result.m_outputProducts)
  549. {
  550. if (!subIdsFound.insert({ product.m_productSubID, product.m_productFileName }).second)
  551. {
  552. // if this happens the element was already in the set.
  553. AZ_Error(AssetBuilderSDK::ErrorWindow, false,
  554. "The builder created more than one asset with the same subID (%u) when emitting product %.*s, colliding with %.*s\n Builders should set a unique m_productSubID value for each product, as this is used as part of the address of the asset.",
  555. product.m_productSubID,
  556. AZ_STRING_ARG(product.m_productFileName),
  557. AZ_STRING_ARG(subIdsFound[product.m_productSubID]));
  558. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  559. break;
  560. }
  561. }
  562. }
  563. if(result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  564. {
  565. bool handledDependencies = true; // True in case there are no outputs
  566. for (const AssetBuilderSDK::JobProduct& jobProduct : result.m_outputProducts)
  567. {
  568. handledDependencies = false; // False by default since there are outputs
  569. if(jobProduct.m_dependenciesHandled)
  570. {
  571. handledDependencies = true;
  572. break;
  573. }
  574. }
  575. if(!handledDependencies)
  576. {
  577. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "The builder (%s) has not indicated it handled outputting product dependencies for file %s. This is a programmer error.", builderParams.m_assetBuilderDesc.m_name.c_str(), builderParams.m_processJobRequest.m_sourceFile.c_str());
  578. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "For builders that output AZ serialized types, it is recommended to use AssetBuilderSDK::OutputObject which will handle outputting product depenedencies and creating the JobProduct. This is fine to use even if your builder never has product dependencies.");
  579. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "For builders that need custom depenedency parsing that cannot be handled by AssetBuilderSDK::OutputObject or ones that output non-AZ serialized types, add the dependencies to m_dependencies and m_pathDependencies on the JobProduct and then set m_dependenciesHandled to true.");
  580. jobLogTraceListener.AddWarning();
  581. }
  582. WarningLevel warningLevel = WarningLevel::Default;
  583. JobDiagnosticRequestBus::BroadcastResult(warningLevel, &JobDiagnosticRequestBus::Events::GetWarningLevel);
  584. const bool hasErrors = jobLogTraceListener.GetErrorCount() > 0;
  585. const bool hasWarnings = jobLogTraceListener.GetWarningCount() > 0;
  586. if(warningLevel == WarningLevel::FatalErrors && hasErrors)
  587. {
  588. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failing job, fatal errors setting is enabled");
  589. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  590. }
  591. else if(warningLevel == WarningLevel::FatalErrorsAndWarnings && (hasErrors || hasWarnings))
  592. {
  593. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failing job, fatal errors and warnings setting is enabled");
  594. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  595. }
  596. }
  597. switch (result.m_resultCode)
  598. {
  599. case AssetBuilderSDK::ProcessJobResult_Success:
  600. // make sure there's no subid collision inside a job.
  601. {
  602. if (!CopyCompiledAssets(builderParams, result))
  603. {
  604. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  605. shouldRemoveTempFolder = false;
  606. }
  607. shouldRemoveTempFolder = shouldRemoveTempFolder && !result.m_keepTempFolder && !s_createRequestFileForSuccessfulJob;
  608. }
  609. break;
  610. case AssetBuilderSDK::ProcessJobResult_Crashed:
  611. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicated that its process crashed!");
  612. break;
  613. case AssetBuilderSDK::ProcessJobResult_Cancelled:
  614. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicates that the job was cancelled.");
  615. break;
  616. case AssetBuilderSDK::ProcessJobResult_Failed:
  617. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicated that the job has failed.");
  618. shouldRemoveTempFolder = false;
  619. break;
  620. }
  621. if ((shouldRemoveTempFolder) || (listener.WasQuitRequested()))
  622. {
  623. QDir workingDir(QString(builderParams.m_processJobRequest.m_tempDirPath.c_str()));
  624. workingDir.removeRecursively();
  625. }
  626. // Setting the job id back to zero for error detection
  627. AssetProcessor::SetThreadLocalJobId(0);
  628. listener.BusDisconnect();
  629. JobDiagnosticRequestBus::Broadcast(&JobDiagnosticRequestBus::Events::RecordDiagnosticInfo, builderParams.m_rcJob->GetJobEntry().m_jobRunKey, JobDiagnosticInfo(aznumeric_cast<AZ::u32>(jobLogTraceListener.GetWarningCount()), aznumeric_cast<AZ::u32>(jobLogTraceListener.GetErrorCount())));
  630. }
  631. bool RCJob::CopyCompiledAssets(BuilderParams& params, AssetBuilderSDK::ProcessJobResponse& response)
  632. {
  633. if (response.m_outputProducts.empty())
  634. {
  635. // early out here for performance - no need to do anything at all here so don't waste time with IsDir or Exists or anything.
  636. return true;
  637. }
  638. AZ::IO::Path cacheDirectory = params.m_cacheOutputDir;
  639. AZ::IO::Path intermediateDirectory = params.m_intermediateOutputDir;
  640. AZ::IO::Path relativeFilePath = params.m_relativePath;
  641. QString tempFolder = params.m_processJobRequest.m_tempDirPath.c_str();
  642. QDir tempDir(tempFolder);
  643. if (params.m_cacheOutputDir.empty() || params.m_intermediateOutputDir.empty())
  644. {
  645. AZ_Assert(false, "CopyCompiledAssets: params.m_finalOutputDir or m_intermediateOutputDir is empty for an asset processor job. This should not happen and is because of a recent code change. Check history of any new builders or rcjob.cpp\n");
  646. return false;
  647. }
  648. if (!tempDir.exists())
  649. {
  650. AZ_Assert(false, "CopyCompiledAssets: params.m_processJobRequest.m_tempDirPath is empty for an asset processor job. This should not happen and is because of a recent code change! Check history of RCJob.cpp and any new builder code changes.\n");
  651. return false;
  652. }
  653. // copy the built products into the appropriate location in the real cache and update the job status accordingly.
  654. // note that we go to the trouble of first doing all the checking for disk space and existence of the source files
  655. // before we notify the AP or start moving any of the files so that failures cause the least amount of damage possible.
  656. // this vector is a set of pairs where the first of each pair is the source file (absolute) we intend to copy
  657. // and the second is the product destination we intend to copy it to.
  658. QList< QPair<QString, QString> > outputsToCopy;
  659. outputsToCopy.reserve(static_cast<int>(response.m_outputProducts.size()));
  660. QList<QPair<QString, AZ::Uuid>> intermediateOutputPaths;
  661. qint64 fileSizeRequired = 0;
  662. bool needCacheDirectory = false;
  663. bool needIntermediateDirectory = false;
  664. for (AssetBuilderSDK::JobProduct& product : response.m_outputProducts)
  665. {
  666. // each Output Product communicated by the builder will either be
  667. // * a relative path, which means we assume its relative to the temp folder, and we attempt to move the file
  668. // * an absolute path in the temp folder, and we attempt to move also
  669. // * an absolute path outside the temp folder, in which we assume you'd like to just copy a file somewhere.
  670. QString outputProduct = QString::fromUtf8(product.m_productFileName.c_str()); // could be a relative path.
  671. QFileInfo fileInfo(outputProduct);
  672. if (fileInfo.isRelative())
  673. {
  674. // we assume that its relative to the TEMP folder.
  675. fileInfo = QFileInfo(tempDir.absoluteFilePath(outputProduct));
  676. }
  677. QString absolutePathOfSource = fileInfo.absoluteFilePath();
  678. QString outputFilename = fileInfo.fileName();
  679. bool outputToCache = (product.m_outputFlags & AssetBuilderSDK::ProductOutputFlags::ProductAsset) == AssetBuilderSDK::ProductOutputFlags::ProductAsset;
  680. bool outputToIntermediate = (product.m_outputFlags & AssetBuilderSDK::ProductOutputFlags::IntermediateAsset) ==
  681. AssetBuilderSDK::ProductOutputFlags::IntermediateAsset;
  682. if (outputToCache && outputToIntermediate)
  683. {
  684. // We currently do not support both since intermediate outputs require the Common platform, which is not supported for cache outputs yet
  685. AZ_Error(AssetProcessor::ConsoleChannel, false, "Outputting an asset as both a product and intermediate is not supported. To output both, please split the job into two separate ones.");
  686. return false;
  687. }
  688. if (!outputToCache && !outputToIntermediate)
  689. {
  690. AZ_Error(AssetProcessor::ConsoleChannel, false, "An output asset must be flagged as either a product or an intermediate asset. "
  691. "Please update the output job to include either AssetBuilderSDK::ProductOutputFlags::ProductAsset "
  692. "or AssetBuilderSDK::ProductOutputFlags::IntermediateAsset");
  693. return false;
  694. }
  695. // Intermediates are required to output for the common platform only
  696. if (outputToIntermediate && params.m_processJobRequest.m_platformInfo.m_identifier != AssetBuilderSDK::CommonPlatformName)
  697. {
  698. AZ_Error(AssetProcessor::ConsoleChannel, false, "Intermediate outputs are only supported for the %s platform. "
  699. "Either change the Job platform to %s or change the output flag to AssetBuilderSDK::ProductOutputFlags::ProductAsset",
  700. AssetBuilderSDK::CommonPlatformName,
  701. AssetBuilderSDK::CommonPlatformName);
  702. return false;
  703. }
  704. // Common platform is not currently supported for product assets
  705. if (outputToCache && params.m_processJobRequest.m_platformInfo.m_identifier == AssetBuilderSDK::CommonPlatformName)
  706. {
  707. AZ_Error(
  708. AssetProcessor::ConsoleChannel, false,
  709. "Product asset outputs are not currently supported for the %s platform. "
  710. "Either change the Job platform to a normal platform or change the output flag to AssetBuilderSDK::ProductOutputFlags::IntermediateAsset",
  711. AssetBuilderSDK::CommonPlatformName);
  712. return false;
  713. }
  714. const bool isSourceMetadataEnabled = !params.m_sourceUuid.IsNull();
  715. if (isSourceMetadataEnabled)
  716. {
  717. // For metadata enabled files, the output file needs to be prefixed to handle multiple files with the same relative path.
  718. // This phase will just use a temporary prefix which is longer and less likely to result in accidental conflicts.
  719. // During AssetProcessed_Impl in APM, the prefixing will be resolved to figure out which file is highest priority and gets renamed
  720. // back to the non-prefixed, backwards compatible format and every other file with the same rel path will be re-prefixed to a finalized form.
  721. ProductOutputUtil::GetInterimProductPath(outputFilename, params.m_rcJob->GetJobEntry().m_sourceAssetReference.ScanFolderId());
  722. }
  723. if(outputToCache)
  724. {
  725. needCacheDirectory = true;
  726. if(!product.m_outputPathOverride.empty())
  727. {
  728. AZ_Error(AssetProcessor::ConsoleChannel, false, "%s specified m_outputPathOverride on a ProductAsset. This is not supported."
  729. " Please update the builder accordingly.", params.m_processJobRequest.m_sourceFile.c_str());
  730. return false;
  731. }
  732. if (!VerifyOutputProduct(
  733. QDir(cacheDirectory.c_str()), outputFilename, absolutePathOfSource, fileSizeRequired,
  734. outputsToCopy))
  735. {
  736. return false;
  737. }
  738. }
  739. if(outputToIntermediate)
  740. {
  741. needIntermediateDirectory = true;
  742. if(!product.m_outputPathOverride.empty())
  743. {
  744. relativeFilePath = product.m_outputPathOverride;
  745. }
  746. if (VerifyOutputProduct(
  747. QDir(intermediateDirectory.c_str()), outputFilename, absolutePathOfSource, fileSizeRequired, outputsToCopy))
  748. {
  749. // A null uuid indicates the source is not using metadata files.
  750. // The assumption for the UUID generated below is that the source UUID will not change. A type which has no metadata
  751. // file currently may be updated later to have a metadata file, which would break that assumption. In that case, stick
  752. // with the default path-based UUID.
  753. if (isSourceMetadataEnabled)
  754. {
  755. // Generate a UUID for the intermediate as:
  756. // SourceUuid:BuilderUuid:SubId
  757. auto uuid = AZ::Uuid::CreateName(AZStd::string::format(
  758. "%s:%s:%d",
  759. params.m_sourceUuid.ToFixedString().c_str(),
  760. params.m_assetBuilderDesc.m_busId.ToFixedString().c_str(),
  761. product.m_productSubID));
  762. // Add the product absolute path to the list of intermediates
  763. intermediateOutputPaths.append(QPair(outputsToCopy.back().second, uuid));
  764. }
  765. }
  766. else
  767. {
  768. return false;
  769. }
  770. }
  771. // update the productFileName to be the scanfolder relative path (without the platform)
  772. product.m_productFileName = (relativeFilePath / outputFilename.toUtf8().constData()).c_str();
  773. }
  774. // now we can check if there's enough space for ALL the files before we copy any.
  775. bool hasSpace = false;
  776. auto* diskSpaceInfoInterface = AZ::Interface<AssetProcessor::IDiskSpaceInfo>::Get();
  777. if (diskSpaceInfoInterface)
  778. {
  779. hasSpace = diskSpaceInfoInterface->CheckSufficientDiskSpace(fileSizeRequired, false);
  780. }
  781. if (!hasSpace)
  782. {
  783. AZ_Error(
  784. AssetProcessor::ConsoleChannel, false,
  785. "Cannot save file(s) to cache, not enough disk space to save all the products of %s. Total needed: %lli bytes",
  786. params.m_processJobRequest.m_sourceFile.c_str(), fileSizeRequired);
  787. return false;
  788. }
  789. // if we get here, we are good to go in terms of disk space and sources existing, so we make the best attempt we can.
  790. // if outputDirectory does not exist then create it
  791. unsigned int waitTimeInSecs = 3;
  792. if (needCacheDirectory && !AssetUtilities::CreateDirectoryWithTimeout(QDir(cacheDirectory.AsPosix().c_str()), waitTimeInSecs))
  793. {
  794. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create output directory: %s\n", cacheDirectory.c_str());
  795. return false;
  796. }
  797. if (needIntermediateDirectory && !AssetUtilities::CreateDirectoryWithTimeout(QDir(intermediateDirectory.AsPosix().c_str()), waitTimeInSecs))
  798. {
  799. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create intermediate directory: %s\n", intermediateDirectory.c_str());
  800. return false;
  801. }
  802. auto* uuidInterface = AZ::Interface<AzToolsFramework::IUuidUtil>::Get();
  803. if (!uuidInterface)
  804. {
  805. AZ_Assert(false, "Programmer Error - IUuidUtil interface is not available");
  806. return false;
  807. }
  808. // Go through all the intermediate products and output the assigned UUID
  809. for (auto [intermediateProduct, uuid] : intermediateOutputPaths)
  810. {
  811. if(!uuidInterface->CreateSourceUuid(intermediateProduct.toUtf8().constData(), uuid))
  812. {
  813. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create metadata file for intermediate product " AZ_STRING_FORMAT, AZ_STRING_ARG(intermediateProduct));
  814. }
  815. }
  816. bool anyFileFailed = false;
  817. for (const QPair<QString, QString>& filePair : outputsToCopy)
  818. {
  819. const QString& sourceAbsolutePath = filePair.first;
  820. const QString& productAbsolutePath = filePair.second;
  821. bool isCopyJob = !(sourceAbsolutePath.startsWith(tempFolder, Qt::CaseInsensitive));
  822. isCopyJob |= response.m_keepTempFolder; // Copy instead of Move if the builder wants to keep the Temp Folder.
  823. if (!MoveCopyFile(sourceAbsolutePath, productAbsolutePath, isCopyJob)) // this has its own traceprintf for failure
  824. {
  825. // MoveCopyFile will have output to the log. No need to double output here.
  826. anyFileFailed = true;
  827. continue;
  828. }
  829. //we now ensure that the file is writable - this is just a warning if it fails, not a complete failure.
  830. if (!AssetUtilities::MakeFileWritable(productAbsolutePath))
  831. {
  832. AZ_TracePrintf(AssetBuilderSDK::WarningWindow, "Unable to change permission for the file: %s.\n", productAbsolutePath.toUtf8().data());
  833. }
  834. }
  835. return !anyFileFailed;
  836. }
  837. bool RCJob::VerifyOutputProduct(
  838. QDir outputDirectory,
  839. QString outputFilename,
  840. QString absolutePathOfSource,
  841. qint64& totalFileSizeRequired,
  842. QList<QPair<QString, QString>>& outputsToCopy)
  843. {
  844. QString productFile = AssetUtilities::NormalizeFilePath(outputDirectory.filePath(outputFilename.toLower()));
  845. // Don't make productFile all lowercase for case-insensitive as this
  846. // breaks macOS. The case is already setup properly when the job
  847. // was created.
  848. if (productFile.length() >= ASSETPROCESSOR_WARN_PATH_LEN && productFile.length() < ASSETPROCESSOR_TRAIT_MAX_PATH_LEN)
  849. {
  850. AZ_Warning(
  851. AssetBuilderSDK::WarningWindow,
  852. false,
  853. "Product '%s' path length (%d) exceeds the suggested max path length (%d). This may not work on all platforms.\n",
  854. productFile.toUtf8().data(),
  855. productFile.length(),
  856. ASSETPROCESSOR_WARN_PATH_LEN);
  857. }
  858. if (productFile.length() >= ASSETPROCESSOR_TRAIT_MAX_PATH_LEN)
  859. {
  860. AZ_Error(
  861. AssetBuilderSDK::ErrorWindow,
  862. false,
  863. "Cannot copy file: Product '%s' path length (%d) exceeds the max path length (%d) allowed on disk\n",
  864. productFile.toUtf8().data(),
  865. productFile.length(),
  866. ASSETPROCESSOR_TRAIT_MAX_PATH_LEN);
  867. return false;
  868. }
  869. QFileInfo inFile(absolutePathOfSource);
  870. if (!inFile.exists())
  871. {
  872. AZ_Error(
  873. AssetBuilderSDK::ErrorWindow, false,
  874. "Cannot copy file - product file with absolute path '%s' attempting to save into cache could not be found",
  875. absolutePathOfSource.toUtf8().constData());
  876. return false;
  877. }
  878. totalFileSizeRequired += inFile.size();
  879. outputsToCopy.push_back(qMakePair(absolutePathOfSource, productFile));
  880. return true;
  881. }
  882. AZ::Outcome<AZStd::vector<AZStd::string>> RCJob::BeforeStoringJobResult(const BuilderParams& builderParams, AssetBuilderSDK::ProcessJobResponse jobResponse)
  883. {
  884. AZStd::string normalizedTempFolderPath = builderParams.m_processJobRequest.m_tempDirPath;
  885. AzFramework::StringFunc::Path::Normalize(normalizedTempFolderPath);
  886. AZStd::vector<AZStd::string> sourceFiles;
  887. for (AssetBuilderSDK::JobProduct& product : jobResponse.m_outputProducts)
  888. {
  889. // Try to handle Absolute paths within the temp folder
  890. AzFramework::StringFunc::Path::Normalize(product.m_productFileName);
  891. if (!AzFramework::StringFunc::Replace(product.m_productFileName, normalizedTempFolderPath.c_str(), s_tempString))
  892. {
  893. // From CopyCompiledAssets:
  894. // each Output Product communicated by the builder will either be
  895. // * a relative path, which means we assume its relative to the temp folder, and we attempt to move the file
  896. // * an absolute path in the temp folder, and we attempt to move also
  897. // * an absolute path outside the temp folder, in which we assume you'd like to just copy a file somewhere.
  898. // We need to handle case 3 here (Case 2 was above, case 1 is treated as relative within temp)
  899. // If the path was not absolute within the temp folder and not relative it should be an absolute path beneath our source (Including the source)
  900. // meaning a copy job which needs to be added to our archive.
  901. if (!AzFramework::StringFunc::Path::IsRelative(product.m_productFileName.c_str()))
  902. {
  903. AZStd::string sourceFile{ builderParams.m_rcJob->GetJobEntry().GetAbsoluteSourcePath().toUtf8().data() };
  904. AzFramework::StringFunc::Path::Normalize(sourceFile);
  905. AzFramework::StringFunc::Path::StripFullName(sourceFile);
  906. size_t sourcePathPos = product.m_productFileName.find(sourceFile.c_str());
  907. if(sourcePathPos != AZStd::string::npos)
  908. {
  909. sourceFiles.push_back(product.m_productFileName.substr(sourceFile.size()).c_str());
  910. AzFramework::StringFunc::Path::Join(s_tempString, product.m_productFileName.substr(sourceFile.size()).c_str(), product.m_productFileName);
  911. }
  912. else
  913. {
  914. AZ_Warning(AssetBuilderSDK::WarningWindow, false,
  915. "Failed to find source path %s or temp path %s in non relative path in %s",
  916. sourceFile.c_str(), normalizedTempFolderPath.c_str(), product.m_productFileName.c_str());
  917. }
  918. }
  919. }
  920. }
  921. AZStd::string responseFilePath;
  922. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), AssetBuilderSDK::s_processJobResponseFileName, responseFilePath, true);
  923. //Save ProcessJobResponse to disk
  924. if (!AZ::Utils::SaveObjectToFile(responseFilePath, AZ::DataStream::StreamType::ST_XML, &jobResponse))
  925. {
  926. return AZ::Failure();
  927. }
  928. AzToolsFramework::AssetSystem::JobInfo jobInfo;
  929. AzToolsFramework::AssetSystem::AssetJobLogResponse jobLogResponse;
  930. jobInfo.m_sourceFile = builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.RelativePath().c_str();
  931. jobInfo.m_platform = builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str();
  932. jobInfo.m_jobKey = builderParams.m_rcJob->GetJobKey().toUtf8().data();
  933. jobInfo.m_builderGuid = builderParams.m_rcJob->GetBuilderGuid();
  934. jobInfo.m_jobRunKey = builderParams.m_rcJob->GetJobEntry().m_jobRunKey;
  935. jobInfo.m_watchFolder = builderParams.m_processJobRequest.m_watchFolder;
  936. AssetUtilities::ReadJobLog(jobInfo, jobLogResponse);
  937. //Save joblog to disk
  938. AZStd::string jobLogFilePath;
  939. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_jobLogFileName, jobLogFilePath, true);
  940. if (!AZ::Utils::SaveObjectToFile(jobLogFilePath, AZ::DataStream::StreamType::ST_XML, &jobLogResponse))
  941. {
  942. return AZ::Failure();
  943. }
  944. return AZ::Success(sourceFiles);
  945. }
  946. bool RCJob::AfterRetrievingJobResult(const BuilderParams& builderParams, AssetUtilities::JobLogTraceListener& jobLogTraceListener, AssetBuilderSDK::ProcessJobResponse& jobResponse)
  947. {
  948. AZStd::string responseFilePath;
  949. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), AssetBuilderSDK::s_processJobResponseFileName, responseFilePath, true);
  950. if (!AZ::Utils::LoadObjectFromFileInPlace(responseFilePath.c_str(), jobResponse))
  951. {
  952. return false;
  953. }
  954. //Ensure that ProcessJobResponse have the correct absolute paths
  955. for (AssetBuilderSDK::JobProduct& product : jobResponse.m_outputProducts)
  956. {
  957. AzFramework::StringFunc::Replace(product.m_productFileName, s_tempString, builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_tempString);
  958. }
  959. AZStd::string jobLogFilePath;
  960. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_jobLogFileName, jobLogFilePath, true);
  961. AzToolsFramework::AssetSystem::AssetJobLogResponse jobLogResponse;
  962. if (!AZ::Utils::LoadObjectFromFileInPlace(jobLogFilePath.c_str(), jobLogResponse))
  963. {
  964. return false;
  965. }
  966. if (!jobLogResponse.m_isSuccess)
  967. {
  968. AZ_TracePrintf(AssetProcessor::DebugChannel, "Job log request was unsuccessful for job (%s, %s, %s) from the server.\n",
  969. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  970. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str());
  971. if(jobLogResponse.m_jobLog.find("No log file found") != AZStd::string::npos)
  972. {
  973. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to find job log from the server. This could happen if you are trying to use the server cache with a copy job, "
  974. "please check the assetprocessorplatformconfig.ini file and ensure that server cache is disabled for the job.\n");
  975. }
  976. return false;
  977. }
  978. // writing server logs
  979. AZ_TracePrintf(AssetProcessor::DebugChannel, "------------SERVER BEGIN----------\n");
  980. AzToolsFramework::Logging::LogLine::ParseLog(jobLogResponse.m_jobLog.c_str(), jobLogResponse.m_jobLog.size(),
  981. [&jobLogTraceListener](AzToolsFramework::Logging::LogLine& line)
  982. {
  983. jobLogTraceListener.AppendLog(line);
  984. });
  985. AZ_TracePrintf(AssetProcessor::DebugChannel, "------------SERVER END----------\n");
  986. return true;
  987. }
  988. AZStd::string BuilderParams::GetTempJobDirectory() const
  989. {
  990. return m_processJobRequest.m_tempDirPath;
  991. }
  992. QString BuilderParams::GetServerKey() const
  993. {
  994. return m_serverKey;
  995. }
  996. } // namespace AssetProcessor
  997. //////////////////////////////////////////////////////////////////////////