SettingsManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #include "SettingsManager.h"
  10. // Qt
  11. #include <QDateTime>
  12. // Editor
  13. #include "Settings.h"
  14. #include "MainWindow.h"
  15. #include "ToolBox.h"
  16. #include "QtViewPaneManager.h"
  17. #define TOOLBOX_FILE "ToolBox.xml"
  18. #define TOOLBOX_NODE "ToolBox"
  19. CSettingsManager::CSettingsManager(EditorSettingsManagerType managerType)
  20. {
  21. if (managerType < eSettingsManagerLast)
  22. {
  23. m_managerType = managerType;
  24. }
  25. else
  26. {
  27. managerType = eSettingsManagerMemoryStorage;
  28. }
  29. if (m_managerType == eSettingsManagerMemoryStorage)
  30. {
  31. m_pSettingsManagerMemoryNode = XmlHelpers::CreateXmlNode(EDITOR_SETTINGS_ROOT_NODE);
  32. }
  33. // Main frame layouts must be processed as first
  34. AddToolName(QStringLiteral(MAINFRM_LAYOUT_NORMAL), QStringLiteral("Sandbox Layout"));
  35. AddToolName(QStringLiteral(MAINFRM_LAYOUT_PREVIEW), QStringLiteral("Sandbox Preview Layout"));
  36. CreateDefaultLayoutSettingsFile();
  37. QDateTime theTime = QDateTime::currentDateTimeUtc();
  38. SaveLogEventSetting("EditorStart", "time", theTime.toString(LOG_DATETIME_FORMAT));
  39. }
  40. CSettingsManager::~CSettingsManager()
  41. {
  42. }
  43. void CSettingsManager::SaveLayoutSettings(const QByteArray& layout, const QString& toolName)
  44. {
  45. if (m_managerType == eSettingsManagerMemoryStorage)
  46. {
  47. XmlNodeRef rootLayoutNode = m_pSettingsManagerMemoryNode->findChild(EDITOR_LAYOUT_ROOT_NODE);
  48. if (rootLayoutNode)
  49. {
  50. XmlNodeRef xmlDockingLayoutNode = rootLayoutNode->findChild(EDITOR_LAYOUT_NODE);
  51. if (!xmlDockingLayoutNode)
  52. {
  53. return;
  54. }
  55. XmlNodeRef toolNode = xmlDockingLayoutNode->findChild(toolName.toUtf8().data());
  56. if (toolNode)
  57. {
  58. xmlDockingLayoutNode->removeChild(toolNode);
  59. }
  60. toolNode = XmlHelpers::CreateXmlNode(toolName.toUtf8().data());
  61. xmlDockingLayoutNode->addChild(toolNode);
  62. XmlNodeRef windowStateNode = XmlHelpers::CreateXmlNode("WindowState");
  63. toolNode->addChild(windowStateNode);
  64. windowStateNode->setContent(layout.toHex().data());
  65. }
  66. }
  67. }
  68. bool CSettingsManager::CreateDefaultLayoutSettingsFile()
  69. {
  70. XmlNodeRef pLayoutRootNode = XmlHelpers::CreateXmlNode(EDITOR_LAYOUT_ROOT_NODE);
  71. XmlNodeRef pEditorLayoutNode = XmlHelpers::CreateXmlNode(EDITOR_LAYOUT_NODE);
  72. pLayoutRootNode->addChild(pEditorLayoutNode);
  73. if (m_pSettingsManagerMemoryNode->findChild(EDITOR_LAYOUT_ROOT_NODE))
  74. {
  75. m_pSettingsManagerMemoryNode->removeChild(m_pSettingsManagerMemoryNode->findChild(EDITOR_LAYOUT_ROOT_NODE));
  76. }
  77. m_pSettingsManagerMemoryNode->addChild(pLayoutRootNode);
  78. return true;
  79. }
  80. AZStd::vector<AZStd::string> CSettingsManager::BuildSettingsList()
  81. {
  82. XmlNodeRef root = nullptr;
  83. root = m_pSettingsManagerMemoryNode;
  84. XmlNodeRef tmpNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  85. if (!tmpNode)
  86. {
  87. root->addChild(root->createNode(EDITOR_SETTINGS_CONTENT_NODE));
  88. tmpNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  89. if (!tmpNode)
  90. {
  91. AZ_Warning("Settings Manager", false, "Failed to build the settings list - could not find the root xml node.");
  92. return AZStd::vector<AZStd::string>();
  93. }
  94. }
  95. AZStd::vector<AZStd::string> result;
  96. BuildSettingsList_Helper(tmpNode, "", result);
  97. return result;
  98. }
  99. void CSettingsManager::BuildSettingsList_Helper(const XmlNodeRef& node, const AZStd::string_view& pathToNode, AZStd::vector<AZStd::string>& result)
  100. {
  101. for (int i = 0; i < node->getNumAttributes(); ++i)
  102. {
  103. const char* key = nullptr;
  104. const char* value = nullptr;
  105. node->getAttributeByIndex(i, &key, &value);
  106. if (!pathToNode.empty())
  107. {
  108. result.push_back(AZStd::string(pathToNode));
  109. }
  110. }
  111. if (node->getChildCount() > 0)
  112. {
  113. // Explore Children
  114. int numChildren = node->getChildCount();
  115. for (int i = 0; i < numChildren; ++i)
  116. {
  117. if (pathToNode.empty())
  118. {
  119. BuildSettingsList_Helper(
  120. node->getChild(i),
  121. node->getChild(i)->getTag(),
  122. result
  123. );
  124. }
  125. else
  126. {
  127. BuildSettingsList_Helper(
  128. node->getChild(i),
  129. AZStd::string(pathToNode) + "|" + node->getChild(i)->getTag(),
  130. result
  131. );
  132. }
  133. }
  134. }
  135. }
  136. //////////////////////////////////////////////////////////////////////////
  137. void CSettingsManager::SaveSetting(const QString& path, const QString& attr, const QString& val)
  138. {
  139. QStringList strNodes;
  140. QString fixedPath = path;
  141. // Make sure it ends with "\" before extraction of tokens
  142. QString slashChar = fixedPath.right(fixedPath.length() - 1);
  143. if (slashChar != "\\")
  144. {
  145. fixedPath += "\\";
  146. }
  147. Path::GetDirectoryQueue(fixedPath, strNodes);
  148. QString writeAttr = attr;
  149. // Spaces in node names not allowed
  150. writeAttr.replace(" ", "");
  151. XmlNodeRef root = nullptr;
  152. root = m_pSettingsManagerMemoryNode;
  153. XmlNodeRef tmpNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  154. if (!tmpNode)
  155. {
  156. root->addChild(root->createNode(EDITOR_SETTINGS_CONTENT_NODE));
  157. tmpNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  158. if (!tmpNode)
  159. {
  160. return;
  161. }
  162. }
  163. for (int i = 0; i < strNodes.size(); ++i)
  164. {
  165. if (!tmpNode->findChild(strNodes[i].toUtf8().data()))
  166. {
  167. tmpNode->addChild(tmpNode->createNode(strNodes[i].toUtf8().data()));
  168. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  169. }
  170. else
  171. {
  172. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  173. }
  174. }
  175. if (!tmpNode->findChild(writeAttr.toUtf8().data()))
  176. {
  177. tmpNode->addChild(tmpNode->createNode(writeAttr.toUtf8().data()));
  178. tmpNode = tmpNode->findChild(writeAttr.toUtf8().data());
  179. }
  180. else
  181. {
  182. tmpNode = tmpNode->findChild(writeAttr.toUtf8().data());
  183. }
  184. if (!tmpNode)
  185. {
  186. return;
  187. }
  188. tmpNode->setAttr(EDITOR_SETTINGS_ATTRIB_NAME, val.toUtf8().data());
  189. }
  190. void CSettingsManager::SaveSetting(const QString& path, const QString& attr, bool bVal)
  191. {
  192. SaveSetting(path, attr, QString ::number(bVal));
  193. }
  194. void CSettingsManager::SaveSetting(const QString& path, const QString& attr, float fVal)
  195. {
  196. SaveSetting(path, attr, QString::number(fVal));
  197. }
  198. void CSettingsManager::SaveSetting(const QString& path, const QString& attr, int iVal)
  199. {
  200. SaveSetting(path, attr, QString::number(iVal));
  201. }
  202. void CSettingsManager::SaveSetting(const QString& path, const QString& attr, QColor color)
  203. {
  204. SaveSetting(path, attr, color.name());
  205. }
  206. //////////////////////////////////////////////////////////////////////////
  207. XmlNodeRef CSettingsManager::LoadSetting(const QString& path, const QString& attr, QString& val)
  208. {
  209. // Make sure it ends with "\" before extraction of tokens
  210. QString fixedPath = path;
  211. QString slashChar = fixedPath.right(fixedPath.length() - 1);
  212. if (slashChar != "\\")
  213. {
  214. fixedPath += "\\";
  215. }
  216. QStringList strNodes;
  217. Path::GetDirectoryQueue(fixedPath, strNodes);
  218. QString readAttr = attr;
  219. // Spaces in node names not allowed
  220. readAttr.replace(" ", "");
  221. XmlNodeRef root = nullptr;
  222. root = m_pSettingsManagerMemoryNode;
  223. XmlNodeRef tmpNode = nullptr;
  224. if (NeedSettingsNode(path))
  225. {
  226. tmpNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  227. }
  228. else
  229. {
  230. tmpNode = root;
  231. }
  232. if (!tmpNode)
  233. {
  234. return nullptr;
  235. }
  236. for (int i = 0; i < strNodes.size(); ++i)
  237. {
  238. if (tmpNode->findChild(strNodes[i].toUtf8().data()))
  239. {
  240. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  241. }
  242. else
  243. {
  244. return nullptr;
  245. }
  246. }
  247. if (!tmpNode->findChild(readAttr.toUtf8().data()))
  248. {
  249. return nullptr;
  250. }
  251. else
  252. {
  253. tmpNode = tmpNode->findChild(readAttr.toUtf8().data());
  254. }
  255. if (tmpNode->haveAttr(EDITOR_SETTINGS_ATTRIB_NAME))
  256. {
  257. tmpNode->getAttr(EDITOR_SETTINGS_ATTRIB_NAME, val);
  258. }
  259. return tmpNode;
  260. }
  261. void CSettingsManager::LoadSetting(const QString& path, const QString& attr, bool& bVal)
  262. {
  263. QString defaultVal = QString::number(bVal);
  264. LoadSetting(path, attr, defaultVal);
  265. bVal = defaultVal.toInt() != 0;
  266. }
  267. void CSettingsManager::LoadSetting(const QString& path, const QString& attr, int& iVal)
  268. {
  269. QString defaultVal = QString::number(iVal);
  270. LoadSetting(path, attr, defaultVal);
  271. iVal = defaultVal.toInt();
  272. }
  273. void CSettingsManager::LoadSetting(const QString& path, const QString& attr, float& fVal)
  274. {
  275. QString defaultVal = QString::number(fVal, 'g');
  276. LoadSetting(path, attr, defaultVal);
  277. fVal = defaultVal.toFloat();
  278. }
  279. void CSettingsManager::LoadSetting(const QString& path, const QString& attr, QColor &val)
  280. {
  281. QString defaultVal = val.name();
  282. LoadSetting(path, attr, defaultVal);
  283. val = QColor(defaultVal);
  284. }
  285. void CSettingsManager::AddToolVersion(const QString& toolName, const QString& toolVersion)
  286. {
  287. if (toolName.isEmpty())
  288. {
  289. return;
  290. }
  291. if (stl::find_in_map(m_toolNames, toolName, nullptr) == "")
  292. {
  293. if (!toolVersion.isEmpty())
  294. {
  295. m_toolVersions[toolName] = toolVersion;
  296. }
  297. else
  298. {
  299. m_toolVersions[toolName] = "";
  300. }
  301. }
  302. };
  303. void CSettingsManager::AddToolName(const QString& toolName, const QString& humanReadableName)
  304. {
  305. if (toolName == "")
  306. {
  307. return;
  308. }
  309. if (stl::find_in_map(m_toolNames, toolName, nullptr) == "")
  310. {
  311. if (!humanReadableName.isEmpty())
  312. {
  313. m_toolNames[toolName] = humanReadableName;
  314. }
  315. else
  316. {
  317. m_toolNames[toolName] = toolName;
  318. }
  319. }
  320. };
  321. void CSettingsManager::AddSettingsNode(XmlNodeRef newNode)
  322. {
  323. QString nodeStr = newNode->getTag();
  324. XmlNodeRef oldNode = m_pSettingsManagerMemoryNode->findChild(nodeStr.toUtf8().data());
  325. if (oldNode)
  326. {
  327. m_pSettingsManagerMemoryNode->removeChild(oldNode);
  328. }
  329. m_pSettingsManagerMemoryNode->addChild(newNode);
  330. }
  331. void CSettingsManager::ExportSettings(XmlNodeRef exportNode, QString fileName)
  332. {
  333. exportNode->saveToFile(fileName.toUtf8().data());
  334. }
  335. void CSettingsManager::Export()
  336. {
  337. // Feed in-memory node of CSettingsManager with global Sandbox settings
  338. gSettings.Load();
  339. if (m_ExportFilePath.isEmpty())
  340. {
  341. return;
  342. }
  343. // Update to the latest layout
  344. UpdateLayoutNode();
  345. // Save CVars
  346. SerializeCVars(m_pSettingsManagerMemoryNode, false);
  347. m_pSettingsManagerMemoryNode->saveToFile(m_ExportFilePath.toUtf8().data());
  348. GetIEditor()->SetStatusText("Export Successful");
  349. }
  350. void CSettingsManager::UpdateLayoutNode()
  351. {
  352. QtViewPaneManager::instance()->SaveLayout();
  353. XmlNodeRef rootLayoutNode = m_pSettingsManagerMemoryNode->findChild(EDITOR_LAYOUT_ROOT_NODE);
  354. if (!rootLayoutNode)
  355. {
  356. return;
  357. }
  358. XmlNodeRef xmlDockingLayoutNode = rootLayoutNode->findChild(EDITOR_LAYOUT_NODE);
  359. if (!xmlDockingLayoutNode)
  360. {
  361. return;
  362. }
  363. xmlDockingLayoutNode->removeAllChilds();
  364. // serialize layout for main window
  365. XmlNodeRef xmlMainFrameLayoutNode = XmlHelpers::CreateXmlNode(MAINFRM_LAYOUT_NORMAL);
  366. xmlDockingLayoutNode->addChild(xmlMainFrameLayoutNode);
  367. QtViewPaneManager::instance()->SerializeLayout(xmlMainFrameLayoutNode);
  368. // serialize layout for panes
  369. TToolNamesMap::const_iterator tIt;
  370. for (tIt = m_toolNames.begin(); tIt != m_toolNames.end(); ++tIt)
  371. {
  372. const QString& toolName = tIt->first;
  373. QMainWindow* pane = FindViewPane<QMainWindow>(tIt->second);
  374. if (pane)
  375. {
  376. SaveLayoutSettings(pane->saveState(), toolName);
  377. }
  378. }
  379. }
  380. void CSettingsManager::GetMatchingLayoutNames(TToolNamesMap& foundTools, XmlNodeRef& resultNode, QString file)
  381. {
  382. // Need to populate in-memory node with available layouts.
  383. UpdateLayoutNode();
  384. XmlNodeRef root = XmlHelpers::LoadXmlFromFile(file.toUtf8().data());
  385. if (!root)
  386. {
  387. return;
  388. }
  389. root = root->findChild(EDITOR_LAYOUT_ROOT_NODE);
  390. if (!root)
  391. {
  392. return;
  393. }
  394. root = root->findChild(EDITOR_LAYOUT_NODE);
  395. if (!root)
  396. {
  397. return;
  398. }
  399. TToolNamesMap* toolNames = nullptr;
  400. if (!foundTools.empty())
  401. {
  402. toolNames = &foundTools;
  403. }
  404. else
  405. {
  406. toolNames = GetToolNames();
  407. }
  408. if (!toolNames)
  409. {
  410. return;
  411. }
  412. if (toolNames->empty())
  413. {
  414. return;
  415. }
  416. // toolIT->first is tool layout node name in XML file
  417. TToolNamesMap::const_iterator toolIT = toolNames->begin();
  418. for (; toolIT != toolNames->end(); ++toolIT)
  419. {
  420. if (root->findChild(toolIT->first.toUtf8().data()))
  421. {
  422. foundTools[toolIT->first] = toolIT->second;
  423. if (resultNode)
  424. {
  425. resultNode->addChild(root->findChild(toolIT->first.toUtf8().data()));
  426. }
  427. }
  428. }
  429. }
  430. void CSettingsManager::ImportSettings(QString file)
  431. {
  432. XmlNodeRef root = XmlHelpers::LoadXmlFromFile(file.toUtf8().data());
  433. if (!root)
  434. {
  435. return;
  436. }
  437. XmlNodeRef importedSettingsContentNode = root->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  438. if (!importedSettingsContentNode)
  439. {
  440. return;
  441. }
  442. // Remove old settings node
  443. XmlNodeRef oldSettingsContentNode = m_pSettingsManagerMemoryNode->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  444. m_pSettingsManagerMemoryNode->removeChild(oldSettingsContentNode);
  445. // Add new, imported settings node
  446. m_pSettingsManagerMemoryNode->addChild(importedSettingsContentNode);
  447. // Force global settings to reload from memory node instead of registry
  448. gSettings.bSettingsManagerMode = true;
  449. gSettings.Load();
  450. gSettings.bSettingsManagerMode = false;
  451. // Dump ToolBox node on disk, replacing the old one
  452. XmlNodeRef toolBoxNode = root->findChild(TOOLBOX_NODE);
  453. if (toolBoxNode)
  454. {
  455. toolBoxNode->saveToFile(TOOLBOX_FILE);
  456. }
  457. // Dump UserTools node on disk, replacing the old one
  458. XmlNodeRef userToolsNode = root->findChild(TOOLBOXMACROS_NODE);
  459. if (userToolsNode)
  460. {
  461. QString macroFilePath;
  462. GetIEditor()->GetToolBoxManager()->GetSaveFilePath(macroFilePath);
  463. userToolsNode->saveToFile(macroFilePath.toUtf8().data());
  464. GetIEditor()->GetToolBoxManager()->Load();
  465. }
  466. // Get and update CVars
  467. SerializeCVars(root, true);
  468. GetIEditor()->SetStatusText("Import Successful");
  469. }
  470. bool CSettingsManager::NeedSettingsNode(const QString& path)
  471. {
  472. if ((path != EDITOR_LAYOUT_ROOT_NODE) && (path != TOOLBOX_NODE) && (path != TOOLBOXMACROS_NODE))
  473. {
  474. return true;
  475. }
  476. else
  477. {
  478. return false;
  479. }
  480. }
  481. void CSettingsManager::SerializeCVars(XmlNodeRef& node, bool bLoad)
  482. {
  483. int nNumberOfVariables(0);
  484. int nCurrentVariable(0);
  485. IConsole* piConsole(nullptr);
  486. ICVar* piVariable(nullptr);
  487. AZStd::vector<AZStd::string_view> cszVariableNames;
  488. char* szKey(nullptr);
  489. char* szValue(nullptr);
  490. ICVar* piCVar(nullptr);
  491. piConsole = gEnv->pConsole;
  492. if (!piConsole)
  493. {
  494. return;
  495. }
  496. if (bLoad)
  497. {
  498. XmlNodeRef readNode = nullptr;
  499. XmlNodeRef inputCVarsNode = node->findChild(CVARS_NODE);
  500. if (!inputCVarsNode)
  501. {
  502. return;
  503. }
  504. // Remove Entities with ID`s from the list.
  505. for (int childNo = 0; childNo < inputCVarsNode->getChildCount(); ++childNo)
  506. {
  507. readNode = inputCVarsNode->getChild(childNo);
  508. for (int i = 0; i < readNode->getNumAttributes(); ++i)
  509. {
  510. readNode->getAttributeByIndex(i, (const char**)&szKey, (const char**)&szValue);
  511. piCVar = piConsole->GetCVar(szKey);
  512. if (!piCVar)
  513. {
  514. continue;
  515. }
  516. piCVar->Set(szValue);
  517. }
  518. }
  519. }
  520. else
  521. {
  522. XmlNodeRef newCVarNode = nullptr;
  523. XmlNodeRef oldCVarsNode = node->findChild(CVARS_NODE);
  524. if (oldCVarsNode)
  525. {
  526. node->removeChild(oldCVarsNode);
  527. }
  528. XmlNodeRef cvarsNode = XmlHelpers::CreateXmlNode(CVARS_NODE);
  529. nNumberOfVariables = piConsole->GetNumVisibleVars();
  530. cszVariableNames.resize(nNumberOfVariables);
  531. if (piConsole->GetSortedVars(cszVariableNames) != nNumberOfVariables)
  532. {
  533. assert(false);
  534. return;
  535. }
  536. for (nCurrentVariable = 0; nCurrentVariable < cszVariableNames.size(); ++nCurrentVariable)
  537. {
  538. if (_stricmp(cszVariableNames[nCurrentVariable].data(), "_TestFormatMessage") == 0)
  539. {
  540. continue;
  541. }
  542. piVariable = piConsole->GetCVar(cszVariableNames[nCurrentVariable].data());
  543. if (!piVariable)
  544. {
  545. assert(false);
  546. continue;
  547. }
  548. newCVarNode = XmlHelpers::CreateXmlNode(CVAR_NODE);
  549. newCVarNode->setAttr(cszVariableNames[nCurrentVariable].data(), piVariable->GetString());
  550. cvarsNode->addChild(newCVarNode);
  551. }
  552. node->addChild(cvarsNode);
  553. }
  554. }
  555. void CSettingsManager::ReadValueStr(XmlNodeRef& sourceNode, const QString& path, const QString& attr, QString& val)
  556. {
  557. // Make sure the path has "\" at the end
  558. QString fixedPath = path;
  559. // Make sure it ends with "\" before extraction of tokens
  560. QString slashChar = fixedPath.right(fixedPath.length() - 1);
  561. if (slashChar != "\\")
  562. {
  563. fixedPath += "\\";
  564. }
  565. QStringList strNodes;
  566. Path::GetDirectoryQueue(fixedPath, strNodes);
  567. QString readAttr = attr;
  568. // Spaces in node names not allowed
  569. readAttr.replace(" ", "");
  570. XmlNodeRef root = nullptr;
  571. XmlNodeRef tmpNode = nullptr;
  572. if (NeedSettingsNode(path))
  573. {
  574. tmpNode = sourceNode->findChild(EDITOR_SETTINGS_CONTENT_NODE);
  575. }
  576. else
  577. {
  578. tmpNode = sourceNode;
  579. }
  580. if (!tmpNode)
  581. {
  582. return;
  583. }
  584. for (int i = 0; i < strNodes.size(); ++i)
  585. {
  586. if (tmpNode->findChild(strNodes[i].toUtf8().data()))
  587. {
  588. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  589. }
  590. else
  591. {
  592. return;
  593. }
  594. }
  595. if (!tmpNode->findChild(readAttr.toUtf8().data()))
  596. {
  597. return;
  598. }
  599. else
  600. {
  601. tmpNode = tmpNode->findChild(readAttr.toUtf8().data());
  602. }
  603. if (tmpNode->haveAttr(EDITOR_SETTINGS_ATTRIB_NAME))
  604. {
  605. tmpNode->getAttr(EDITOR_SETTINGS_ATTRIB_NAME, val);
  606. }
  607. return;
  608. }
  609. void CSettingsManager::RegisterEvent(const SEventLog& event)
  610. {
  611. if (event.m_eventName.isEmpty())
  612. {
  613. return;
  614. }
  615. QString eventName = event.m_eventName;
  616. QString path(eventName);
  617. path.replace(" ", "");
  618. SaveLogEventSetting(path, EVENT_LOG_EVENT_NAME, event.m_eventName);
  619. QString subPath = path + "\\" + EVENT_LOG_EVENT_NAME;
  620. SaveLogEventSetting(subPath, EVENT_LOG_CALLER_VERSION, event.m_callerVersion);
  621. SaveLogEventSetting(subPath, "state", event.m_eventState);
  622. QDateTime theTime = QDateTime::currentDateTimeUtc();
  623. SaveLogEventSetting(subPath, "time", theTime.toString(LOG_DATETIME_FORMAT));
  624. }
  625. void CSettingsManager::UnregisterEvent(const SEventLog& event)
  626. {
  627. XmlNodeRef root = XmlHelpers::LoadXmlFromFile(EDITOR_EVENT_LOG_FILE_PATH);
  628. if (!root)
  629. {
  630. return;
  631. }
  632. QString eventName = event.m_eventName;
  633. QString path(eventName);
  634. path.replace(" ", "");
  635. QString subPath = path + "\\" + EVENT_LOG_EVENT_NAME;
  636. XmlNodeRef resNode = LoadLogEventSetting(subPath, EDITOR_EVENT_LOG_ATTRIB_NAME, eventName, root);
  637. if (!resNode)
  638. {
  639. return;
  640. }
  641. root->removeChild(resNode->getParent());
  642. root->saveToFile(EDITOR_EVENT_LOG_FILE_PATH);
  643. }
  644. bool CSettingsManager::IsEventSafe(const SEventLog& event)
  645. {
  646. XmlNodeRef root = XmlHelpers::LoadXmlFromFile(EDITOR_EVENT_LOG_FILE_PATH);
  647. if (!root)
  648. {
  649. return true;
  650. }
  651. QString eventName = event.m_eventName;
  652. QString path(eventName);
  653. path += "\\";
  654. path += EVENT_LOG_EVENT_NAME;
  655. path.replace(" ", "");
  656. XmlNodeRef resNode = LoadLogEventSetting(path, EDITOR_EVENT_LOG_ATTRIB_NAME, eventName, root);
  657. // Log entry not found, so it is safe to start
  658. if (!resNode)
  659. {
  660. return true;
  661. }
  662. XmlNodeRef callerVersion = resNode->findChild(EVENT_LOG_CALLER_VERSION);
  663. if (callerVersion)
  664. {
  665. QString callerVersionStr;
  666. if (callerVersion->haveAttr(EDITOR_EVENT_LOG_ATTRIB_NAME))
  667. {
  668. callerVersionStr = callerVersion->getAttr(EDITOR_EVENT_LOG_ATTRIB_NAME);
  669. }
  670. if (!callerVersionStr.isEmpty())
  671. {
  672. if (callerVersionStr != GetToolVersion(eventName))
  673. {
  674. return true;
  675. }
  676. }
  677. // The same version of tool/level found
  678. return false;
  679. }
  680. return true;
  681. }
  682. //////////////////////////////////////////////////////////////////////////
  683. void CSettingsManager::SaveLogEventSetting(const QString& path, const QString& attr, const QString& val)
  684. {
  685. QStringList strNodes;
  686. QString fixedPath = path;
  687. // Make sure it ends with "\" before extraction of tokens
  688. QString slashChar = fixedPath.right(fixedPath.length() - 1);
  689. if (slashChar != "\\")
  690. {
  691. fixedPath += "\\";
  692. }
  693. Path::GetDirectoryQueue(fixedPath, strNodes);
  694. QString writeAttr = attr;
  695. // Simple cleanup of node names - remove all characters except letters, digits,
  696. // underscores, colons, periods, and hyphens.
  697. // If this ever needs to be 100% compliant, the following rules would need to
  698. // be added:
  699. // - name must only *start* with a letter or underscore (not a digit, colon, period, or hyphen)
  700. // - name must not start with "xml" in any case combination
  701. const QRegularExpression xmlNodeCleanupRegex("[^a-zA-Z0-9_:.\\-]*");
  702. writeAttr.remove(xmlNodeCleanupRegex);
  703. for (auto& node : strNodes)
  704. {
  705. node.remove(xmlNodeCleanupRegex);
  706. }
  707. XmlNodeRef root = XmlHelpers::LoadXmlFromFile(EDITOR_EVENT_LOG_FILE_PATH);
  708. if (!root)
  709. {
  710. root = XmlHelpers::CreateXmlNode(EDITOR_EVENT_LOG_ROOT_NODE);
  711. root->saveToFile(EDITOR_LAYOUT_FILE_PATH);
  712. }
  713. XmlNodeRef tmpNode = root;
  714. for (int i = 0; i < strNodes.size(); ++i)
  715. {
  716. if (!tmpNode->findChild(strNodes[i].toUtf8().data()))
  717. {
  718. tmpNode->addChild(tmpNode->createNode(strNodes[i].toUtf8().data()));
  719. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  720. }
  721. else
  722. {
  723. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  724. }
  725. }
  726. if (!tmpNode->findChild(writeAttr.toUtf8().data()))
  727. {
  728. tmpNode->addChild(tmpNode->createNode(writeAttr.toUtf8().data()));
  729. tmpNode = tmpNode->findChild(writeAttr.toUtf8().data());
  730. }
  731. else
  732. {
  733. tmpNode = tmpNode->findChild(writeAttr.toUtf8().data());
  734. }
  735. if (!tmpNode)
  736. {
  737. return;
  738. }
  739. tmpNode->setAttr(EDITOR_EVENT_LOG_ATTRIB_NAME, val.toUtf8().data());
  740. root->saveToFile(EDITOR_EVENT_LOG_FILE_PATH);
  741. }
  742. XmlNodeRef CSettingsManager::LoadLogEventSetting(const QString& path, const QString& attr, [[maybe_unused]] QString& val, XmlNodeRef& root)
  743. {
  744. // Make sure it ends with "\" before extraction of tokens
  745. QString fixedPath = path;
  746. QString slashChar = fixedPath.right(fixedPath.length() - 1);
  747. if (slashChar != "\\")
  748. {
  749. fixedPath += "\\";
  750. }
  751. QStringList strNodes;
  752. Path::GetDirectoryQueue(fixedPath, strNodes);
  753. QString readAttr = attr;
  754. // Spaces in node names not allowed
  755. readAttr.replace(" ", "");
  756. if (!root)
  757. {
  758. return nullptr;
  759. }
  760. XmlNodeRef tmpNode = nullptr;
  761. tmpNode = root;
  762. if (!tmpNode)
  763. {
  764. return nullptr;
  765. }
  766. for (int i = 0; i < strNodes.size(); ++i)
  767. {
  768. if (tmpNode->findChild(strNodes[i].toUtf8().data()))
  769. {
  770. tmpNode = tmpNode->findChild(strNodes[i].toUtf8().data());
  771. }
  772. else
  773. {
  774. return nullptr;
  775. }
  776. }
  777. if (tmpNode->haveAttr(readAttr.toUtf8().data()))
  778. {
  779. return tmpNode;
  780. }
  781. return nullptr;
  782. }
  783. QString CSettingsManager::GenerateContentHash(XmlNodeRef& node, QString sourceName)
  784. {
  785. QString hashStr("");
  786. if (node->getChildCount() == 0)
  787. {
  788. return sourceName;
  789. }
  790. uint32 hash = AZ::Crc32(node->getXML(0));
  791. hashStr = QString::number(hash);
  792. return hashStr;
  793. }