ProjectSettingsContainer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #pragma once
  9. #include "Platforms.h"
  10. #include "PlistDictionary.h"
  11. #include <AzCore/JSON/document.h>
  12. #include <AzCore/Outcome/Outcome.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <AzCore/std/containers/queue.h>
  16. #include <AzCore/std/containers/unordered_map.h>
  17. #include <AzCore/std/containers/variant.h>
  18. #include <AzFramework/StringFunc/StringFunc.h>
  19. namespace ProjectSettingsTool
  20. {
  21. struct SettingsError
  22. {
  23. SettingsError(const AZStd::string& error, const AZStd::string& reason, bool shouldAbort = false);
  24. // The error that occurred
  25. AZStd::string m_error;
  26. // The reason the error occurred
  27. AZStd::string m_reason;
  28. bool m_shouldAbort = false;
  29. };
  30. // Loads, saves, and provides access to all of the project settings files of all platforms.
  31. // It handles base settings and platform settings (android, ios) separately.
  32. // For base settings it uses json document and for platform settings it uses different documents
  33. // depending on the type, it supports json and plist formats.
  34. class ProjectSettingsContainer
  35. {
  36. public:
  37. using PlatformAndPath = AZStd::pair<PlatformId, AZStd::string>;
  38. using PlatformResources = AZStd::vector<PlatformAndPath>;
  39. template<class DocType>
  40. struct Settings
  41. {
  42. // File path to document
  43. AZStd::string m_path;
  44. // Raw string loaded from file
  45. AZStd::string m_rawData;
  46. // The document itself
  47. AZStd::unique_ptr<DocType> m_document;
  48. };
  49. using JsonSettings = Settings<rapidjson::Document>;
  50. using PlistSettings = Settings<XmlDocument>;
  51. using PlatformSettings = AZStd::variant<JsonSettings, PlistSettings>; // Platform data (Android, ios) can be either json or plist
  52. // Constructs the main manager of a document
  53. ProjectSettingsContainer(const AZStd::string& projectJsonFileName, const PlatformResources& platformResources);
  54. // Used to destroy valueDoesNotExist
  55. ~ProjectSettingsContainer();
  56. // Returns the PlatformSettings for given platform
  57. PlatformSettings* GetPlatformData(const Platform& plat);
  58. // Returns true if PlatformSettings are found for platform
  59. bool HasPlatformData(const Platform& plat) const;
  60. // Gets the earliest error not seen
  61. AZ::Outcome<void, SettingsError> GetError();
  62. // Save settings of platform data or project json data.
  63. void SaveSettings(const Platform& plat);
  64. // Saves project.json to disk
  65. void SaveProjectJsonData();
  66. // Reloads Project.json from disk
  67. void ReloadProjectJsonData();
  68. // Save all pLists back to disk
  69. void SaveAllPlatformsData();
  70. // Save platform's data back to disk
  71. void SavePlatformData(const Platform& plat);
  72. // Reloads all plists from disk
  73. void ReloadAllPlatformsData();
  74. // Returns a reference to the project.json Document
  75. rapidjson::Document& GetProjectJsonDocument();
  76. // Gets reference to value in project.json
  77. // returns null type if not found
  78. AZ::Outcome<rapidjson::Value*, void> GetProjectJsonValue(const char* key);
  79. AZStd::unique_ptr<PlistDictionary> CreatePlistDictionary(const Platform& plat);
  80. // Returns the allocator used by ProjectJson
  81. rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>& GetProjectJsonAllocator();
  82. static AZ::Outcome<rapidjson::Value*, void> GetJsonValue(rapidjson::Document& settings, const char* key);
  83. protected:
  84. void LoadJson(JsonSettings& jsonSettings);
  85. void SaveJson(const JsonSettings& jsonSettings);
  86. void LoadPlist(PlistSettings& plistSettings);
  87. void SavePlist(const PlistSettings& plistSettings);
  88. // Errors that have occurred
  89. AZStd::queue<SettingsError> m_errors;
  90. // The settings from project.json (base)
  91. JsonSettings m_projectJson;
  92. // The settings from platform resources (android, ios)
  93. AZStd::unordered_map<PlatformId, PlatformSettings> m_platformSettingsMap;
  94. private:
  95. AZ_DISABLE_COPY_MOVE(ProjectSettingsContainer);
  96. };
  97. } // namespace ProjectSettingsTool