rcjob.cpp 54 KB

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