GameExporter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "EditorDefs.h"
  9. // AzCore
  10. #include <AzCore/IO/IStreamer.h>
  11. #include <AzCore/std/parallel/binary_semaphore.h>
  12. // CryCommon
  13. #include <CryCommon/ILevelSystem.h>
  14. // Editor
  15. #include "GameExporter.h"
  16. #include "GameEngine.h"
  17. #include "CryEditDoc.h"
  18. #include "UsedResources.h"
  19. #include "WaitProgress.h"
  20. #include "Util/CryMemFile.h"
  21. #include "Objects/ObjectManager.h"
  22. #include "Objects/EntityObject.h"
  23. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  24. //////////////////////////////////////////////////////////////////////////
  25. #define MUSIC_LEVEL_LIBRARY_FILE "music.xml"
  26. #define MATERIAL_LEVEL_LIBRARY_FILE "materials.xml"
  27. #define RESOURCE_LIST_FILE "resourcelist.txt"
  28. #define USED_RESOURCE_LIST_FILE "usedresourcelist.txt"
  29. #define SHADER_LIST_FILE "shaderslist.txt"
  30. #define GetAValue(rgb) ((BYTE)((rgb) >> 24))
  31. //////////////////////////////////////////////////////////////////////////
  32. // SGameExporterSettings
  33. //////////////////////////////////////////////////////////////////////////
  34. SGameExporterSettings::SGameExporterSettings()
  35. : iExportTexWidth(4096)
  36. , nApplySS(1)
  37. {
  38. }
  39. //////////////////////////////////////////////////////////////////////////
  40. void SGameExporterSettings::SetLowQuality()
  41. {
  42. iExportTexWidth = 4096;
  43. nApplySS = 0;
  44. }
  45. //////////////////////////////////////////////////////////////////////////
  46. void SGameExporterSettings::SetHiQuality()
  47. {
  48. iExportTexWidth = 16384;
  49. nApplySS = 1;
  50. }
  51. CGameExporter* CGameExporter::m_pCurrentExporter = nullptr;
  52. //////////////////////////////////////////////////////////////////////////
  53. // CGameExporter
  54. //////////////////////////////////////////////////////////////////////////
  55. CGameExporter::CGameExporter()
  56. : m_bAutoExportMode(false)
  57. {
  58. m_pCurrentExporter = this;
  59. }
  60. CGameExporter::~CGameExporter()
  61. {
  62. m_pCurrentExporter = nullptr;
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. bool CGameExporter::Export(unsigned int flags, [[maybe_unused]] EEndian eExportEndian, const char* subdirectory)
  66. {
  67. CAutoDocNotReady autoDocNotReady;
  68. QWaitCursor waitCursor;
  69. IEditor* pEditor = GetIEditor();
  70. CGameEngine* pGameEngine = pEditor->GetGameEngine();
  71. if (pGameEngine->GetLevelPath().isEmpty())
  72. {
  73. return false;
  74. }
  75. bool exportSuccessful = true;
  76. CrySystemEventBus::Broadcast(&CrySystemEventBus::Events::OnCryEditorBeginLevelExport);
  77. bool usePrefabSystemForLevels = false;
  78. AzFramework::ApplicationRequests::Bus::BroadcastResult(
  79. usePrefabSystemForLevels, &AzFramework::ApplicationRequests::IsPrefabSystemEnabled);
  80. if (usePrefabSystemForLevels)
  81. {
  82. // Level.pak and all the data contained within it is unused when using the prefab system for levels, so there's nothing
  83. // to do here.
  84. CCryEditDoc* pDocument = pEditor->GetDocument();
  85. pDocument->SetLevelExported(true);
  86. }
  87. else
  88. {
  89. QDir::setCurrent(pEditor->GetPrimaryCDFolder());
  90. QString sLevelPath = Path::AddSlash(pGameEngine->GetLevelPath());
  91. if (subdirectory && subdirectory[0] && strcmp(subdirectory, ".") != 0)
  92. {
  93. sLevelPath = Path::AddSlash(sLevelPath + subdirectory);
  94. QDir().mkpath(sLevelPath);
  95. }
  96. m_levelPak.m_sPath = QString(sLevelPath) + GetLevelPakFilename();
  97. m_levelPath = Path::RemoveBackslash(sLevelPath);
  98. QString rootLevelPath = Path::AddSlash(pGameEngine->GetLevelPath());
  99. CCryEditDoc* pDocument = pEditor->GetDocument();
  100. if (flags & eExp_Fast)
  101. {
  102. m_settings.SetLowQuality();
  103. }
  104. else if (m_bAutoExportMode)
  105. {
  106. m_settings.SetHiQuality();
  107. }
  108. AZStd::scoped_lock autoLock(CGameEngine::GetPakModifyMutex());
  109. // Close this pak file.
  110. if (!CloseLevelPack(m_levelPak, true))
  111. {
  112. Error("Cannot close Pak file " + m_levelPak.m_sPath);
  113. exportSuccessful = false;
  114. }
  115. if (exportSuccessful && m_bAutoExportMode)
  116. {
  117. // Remove read-only flags.
  118. auto perms = QFile::permissions(m_levelPak.m_sPath) | QFile::Permission::WriteOwner;
  119. QFile::setPermissions(m_levelPak.m_sPath, perms);
  120. }
  121. //////////////////////////////////////////////////////////////////////////
  122. if (exportSuccessful)
  123. {
  124. if (!CFileUtil::OverwriteFile(m_levelPak.m_sPath))
  125. {
  126. Error("Cannot overwrite Pak file " + m_levelPak.m_sPath);
  127. exportSuccessful = false;
  128. }
  129. }
  130. if (exportSuccessful)
  131. {
  132. if (!OpenLevelPack(m_levelPak, false))
  133. {
  134. Error("Cannot open Pak file " + m_levelPak.m_sPath + " for writing.");
  135. exportSuccessful = false;
  136. }
  137. }
  138. ////////////////////////////////////////////////////////////////////////
  139. // Export all data to the game
  140. ////////////////////////////////////////////////////////////////////////
  141. if (exportSuccessful)
  142. {
  143. ////////////////////////////////////////////////////////////////////////
  144. // Exporting map setttings
  145. ////////////////////////////////////////////////////////////////////////
  146. //! Export Level data.
  147. CLogFile::WriteLine("Exporting leveldata.xml");
  148. ExportLevelData(sLevelPath);
  149. CLogFile::WriteLine("Exporting leveldata.xml done.");
  150. ExportLevelInfo(sLevelPath);
  151. ExportLevelResourceList(sLevelPath);
  152. ExportLevelUsedResourceList(sLevelPath);
  153. //////////////////////////////////////////////////////////////////////////
  154. // End Exporting Game data.
  155. //////////////////////////////////////////////////////////////////////////
  156. // Close all packs.
  157. CloseLevelPack(m_levelPak, false);
  158. // m_texturePakFile.Close();
  159. pEditor->SetStatusText(QObject::tr("Ready"));
  160. // Reopen this pak file.
  161. if (!OpenLevelPack(m_levelPak, true))
  162. {
  163. Error("Cannot open Pak file " + m_levelPak.m_sPath);
  164. exportSuccessful = false;
  165. }
  166. }
  167. if (exportSuccessful)
  168. {
  169. // Commit changes to the disk.
  170. _flushall();
  171. // finally create filelist.xml
  172. QString levelName = Path::GetFileName(pGameEngine->GetLevelPath());
  173. ExportFileList(sLevelPath, levelName);
  174. pDocument->SetLevelExported(true);
  175. }
  176. }
  177. // Always notify that we've finished exporting, whether it was successful or not.
  178. CrySystemEventBus::Broadcast(&CrySystemEventBus::Events::OnCryEditorEndLevelExport, exportSuccessful);
  179. if (exportSuccessful)
  180. {
  181. // Notify the level system that there's a new level, so that the level info is populated.
  182. gEnv->pSystem->GetILevelSystem()->Rescan(ILevelSystem::GetLevelsDirectoryName());
  183. CLogFile::WriteLine("Exporting was successful.");
  184. }
  185. return exportSuccessful;
  186. }
  187. //////////////////////////////////////////////////////////////////////////
  188. void CGameExporter::ExportLevelData(const QString& path, bool /*bExportMission*/)
  189. {
  190. IEditor* pEditor = GetIEditor();
  191. pEditor->SetStatusText(QObject::tr("Exporting leveldata.xml..."));
  192. char versionString[256];
  193. pEditor->GetFileVersion().ToString(versionString);
  194. XmlNodeRef root = XmlHelpers::CreateXmlNode("leveldata");
  195. root->setAttr("SandboxVersion", versionString);
  196. XmlNodeRef rootAction = XmlHelpers::CreateXmlNode("leveldataaction");
  197. rootAction->setAttr("SandboxVersion", versionString);
  198. //////////////////////////////////////////////////////////////////////////
  199. // Save Level Data XML
  200. //////////////////////////////////////////////////////////////////////////
  201. QString levelDataFile = path + "leveldata.xml";
  202. XmlString xmlData = root->getXML();
  203. CCryMemFile file;
  204. file.Write(xmlData.c_str(), static_cast<unsigned int>(xmlData.length()));
  205. m_levelPak.m_pakFile.UpdateFile(levelDataFile.toUtf8().data(), file);
  206. QString levelDataActionFile = path + "leveldataaction.xml";
  207. XmlString xmlDataAction = rootAction->getXML();
  208. CCryMemFile fileAction;
  209. fileAction.Write(xmlDataAction.c_str(), static_cast<unsigned int>(xmlDataAction.length()));
  210. m_levelPak.m_pakFile.UpdateFile(levelDataActionFile.toUtf8().data(), fileAction);
  211. AZStd::vector<char> entitySaveBuffer;
  212. AZ::IO::ByteContainerStream<AZStd::vector<char> > entitySaveStream(&entitySaveBuffer);
  213. bool savedEntities = false;
  214. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  215. savedEntities,
  216. &AzToolsFramework::EditorEntityContextRequestBus::Events::SaveToStreamForGame,
  217. entitySaveStream,
  218. AZ::DataStream::ST_BINARY);
  219. if (savedEntities)
  220. {
  221. QString entitiesFile;
  222. entitiesFile = QStringLiteral("%1%2.entities_xml").arg(path, "mission0");
  223. m_levelPak.m_pakFile.UpdateFile(entitiesFile.toUtf8().data(), entitySaveBuffer.begin(), static_cast<int>(entitySaveBuffer.size()));
  224. }
  225. }
  226. //////////////////////////////////////////////////////////////////////////
  227. void CGameExporter::ExportLevelInfo(const QString& path)
  228. {
  229. //////////////////////////////////////////////////////////////////////////
  230. // Export short level info xml.
  231. //////////////////////////////////////////////////////////////////////////
  232. IEditor* pEditor = GetIEditor();
  233. XmlNodeRef root = XmlHelpers::CreateXmlNode("LevelInfo");
  234. char versionString[256];
  235. pEditor->GetFileVersion().ToString(versionString);
  236. root->setAttr("SandboxVersion", versionString);
  237. QString levelName = pEditor->GetGameEngine()->GetLevelPath();
  238. root->setAttr("Name", levelName.toUtf8().data());
  239. //////////////////////////////////////////////////////////////////////////
  240. // Save LevelInfo file.
  241. //////////////////////////////////////////////////////////////////////////
  242. QString filename = path + "levelinfo.xml";
  243. XmlString xmlData = root->getXML();
  244. CCryMemFile file;
  245. file.Write(xmlData.c_str(), static_cast<unsigned int>(xmlData.length()));
  246. m_levelPak.m_pakFile.UpdateFile(filename.toUtf8().data(), file);
  247. }
  248. //////////////////////////////////////////////////////////////////////////
  249. void CGameExporter::ExportLevelResourceList(const QString& path)
  250. {
  251. auto pResList = gEnv->pCryPak->GetResourceList(AZ::IO::IArchive::RFOM_Level);
  252. // Write resource list to file.
  253. CCryMemFile memFile;
  254. for (const char* filename = pResList->GetFirst(); filename; filename = pResList->GetNext())
  255. {
  256. memFile.Write(filename, static_cast<unsigned int>(strlen(filename)));
  257. memFile.Write("\n", 1);
  258. }
  259. QString resFile = Path::Make(path, RESOURCE_LIST_FILE);
  260. m_levelPak.m_pakFile.UpdateFile(resFile.toUtf8().data(), memFile, true);
  261. }
  262. //////////////////////////////////////////////////////////////////////////
  263. void CGameExporter::ExportLevelUsedResourceList(const QString& path)
  264. {
  265. // Write used resource list to file.
  266. CCryMemFile memFile;
  267. CUsedResources resources;
  268. GetIEditor()->GetObjectManager()->GatherUsedResources(resources);
  269. for (CUsedResources::TResourceFiles::const_iterator it = resources.files.begin(); it != resources.files.end(); it++)
  270. {
  271. QString filePath = Path::MakeGamePath(*it).toLower();
  272. memFile.Write(filePath.toUtf8().data(), filePath.toUtf8().length());
  273. memFile.Write("\n", 1);
  274. }
  275. QString resFile = Path::Make(path, USED_RESOURCE_LIST_FILE);
  276. m_levelPak.m_pakFile.UpdateFile(resFile.toUtf8().data(), memFile, true);
  277. }
  278. //////////////////////////////////////////////////////////////////////////
  279. void CGameExporter::ExportFileList(const QString& path, const QString& levelName)
  280. {
  281. // process the folder of the specified map name, producing a filelist.xml file
  282. // that can later be used for map downloads
  283. AZStd::string newpath;
  284. AZStd::string filename = levelName.toUtf8().data();
  285. AZStd::string mapname = (filename + ".dds");
  286. AZStd::string metaname = (filename + ".xml");
  287. XmlNodeRef rootNode = gEnv->pSystem->CreateXmlNode("download");
  288. rootNode->setAttr("name", filename.c_str());
  289. rootNode->setAttr("type", "Map");
  290. XmlNodeRef indexNode = rootNode->newChild("index");
  291. if (indexNode)
  292. {
  293. indexNode->setAttr("src", "filelist.xml");
  294. indexNode->setAttr("dest", "filelist.xml");
  295. }
  296. XmlNodeRef filesNode = rootNode->newChild("files");
  297. if (filesNode)
  298. {
  299. newpath = GetIEditor()->GetGameEngine()->GetLevelPath().toUtf8().data();
  300. newpath += "/*";
  301. AZ::IO::ArchiveFileIterator handle = gEnv->pCryPak->FindFirst(newpath.c_str());
  302. if (!handle)
  303. {
  304. return;
  305. }
  306. do
  307. {
  308. // ignore "." and ".."
  309. if (handle.m_filename.front() == '.')
  310. {
  311. continue;
  312. }
  313. // do we need any files from sub directories?
  314. if ((handle.m_fileDesc.nAttrib & AZ::IO::FileDesc::Attribute::Subdirectory) == AZ::IO::FileDesc::Attribute::Subdirectory)
  315. {
  316. continue;
  317. }
  318. // only need the following files for multiplayer downloads
  319. if (!_stricmp(handle.m_filename.data(), GetLevelPakFilename())
  320. || !_stricmp(handle.m_filename.data(), mapname.c_str())
  321. || !_stricmp(handle.m_filename.data(), metaname.c_str()))
  322. {
  323. XmlNodeRef newFileNode = filesNode->newChild("file");
  324. if (newFileNode)
  325. {
  326. // TEMP: this is just for testing. src probably needs to be blank.
  327. // string src = "http://server41/updater/";
  328. // src += m_levelName;
  329. // src += "/";
  330. // src += fileinfo.name;
  331. newFileNode->setAttr("src", handle.m_filename.data());
  332. newFileNode->setAttr("dest", handle.m_filename.data());
  333. newFileNode->setAttr("size", handle.m_fileDesc.nSize);
  334. }
  335. }
  336. } while (handle = gEnv->pCryPak->FindNext(handle));
  337. gEnv->pCryPak->FindClose (handle);
  338. }
  339. // save filelist.xml
  340. newpath = path.toUtf8().data();
  341. newpath += "/filelist.xml";
  342. rootNode->saveToFile(newpath.c_str());
  343. }
  344. void CGameExporter::Error(const QString& error)
  345. {
  346. if (m_bAutoExportMode)
  347. {
  348. CLogFile::WriteLine((QString("Export failed! ") + error).toUtf8().data());
  349. }
  350. else
  351. {
  352. Warning((QString("Export failed! ") + error).toUtf8().data());
  353. }
  354. }
  355. bool CGameExporter::OpenLevelPack(SLevelPakHelper& lphelper, bool bCryPak)
  356. {
  357. bool bRet = false;
  358. assert(lphelper.m_bPakOpened == false);
  359. assert(lphelper.m_bPakOpenedCryPak == false);
  360. if (bCryPak)
  361. {
  362. assert(!lphelper.m_sPath.isEmpty());
  363. bRet = gEnv->pCryPak->OpenPack(lphelper.m_sPath.toUtf8().data());
  364. assert(bRet);
  365. lphelper.m_bPakOpenedCryPak = true;
  366. }
  367. else
  368. {
  369. bRet = lphelper.m_pakFile.Open(lphelper.m_sPath.toUtf8().data());
  370. assert(bRet);
  371. lphelper.m_bPakOpened = true;
  372. }
  373. return bRet;
  374. }
  375. bool CGameExporter::CloseLevelPack(SLevelPakHelper& lphelper, bool bCryPak)
  376. {
  377. bool bRet = false;
  378. if (bCryPak)
  379. {
  380. assert(lphelper.m_bPakOpenedCryPak == true);
  381. assert(!lphelper.m_sPath.isEmpty());
  382. bRet = gEnv->pCryPak->ClosePack(lphelper.m_sPath.toUtf8().data());
  383. assert(bRet);
  384. lphelper.m_bPakOpenedCryPak = false;
  385. }
  386. else
  387. {
  388. assert(lphelper.m_bPakOpened == true);
  389. lphelper.m_pakFile.Close();
  390. bRet = true;
  391. lphelper.m_bPakOpened = false;
  392. }
  393. assert(lphelper.m_bPakOpened == false);
  394. assert(lphelper.m_bPakOpenedCryPak == false);
  395. return bRet;
  396. }