SysConf.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // Utilities to parse and modify a Wii SYSCONF file and its sections.
  4. #pragma once
  5. #include <cstring>
  6. #include <memory>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "Common/Assert.h"
  11. #include "Common/CommonTypes.h"
  12. #include "Common/NandPaths.h"
  13. #include "Common/Swap.h"
  14. namespace IOS::HLE::FS
  15. {
  16. class FileHandle;
  17. class FileSystem;
  18. } // namespace IOS::HLE::FS
  19. class SysConf final
  20. {
  21. public:
  22. explicit SysConf(std::shared_ptr<IOS::HLE::FS::FileSystem> fs);
  23. ~SysConf();
  24. void Clear();
  25. void Load();
  26. bool Save() const;
  27. struct Entry
  28. {
  29. enum Type : u8
  30. {
  31. BigArray = 1,
  32. SmallArray = 2,
  33. Byte = 3,
  34. Short = 4,
  35. Long = 5,
  36. LongLong = 6,
  37. // Should really be named Bool, but this conflicts with some random macro. :/
  38. ByteBool = 7,
  39. };
  40. Entry(Type type_, std::string name_);
  41. Entry(Type type_, std::string name_, std::vector<u8> bytes_);
  42. // Intended for use with the non array types.
  43. template <typename T>
  44. T GetData(T default_value) const
  45. {
  46. if (bytes.size() != sizeof(T))
  47. return default_value;
  48. T value;
  49. std::memcpy(&value, bytes.data(), bytes.size());
  50. return Common::FromBigEndian(value);
  51. }
  52. template <typename T>
  53. void SetData(T value)
  54. {
  55. ASSERT(sizeof(value) == bytes.size());
  56. value = Common::FromBigEndian(value);
  57. std::memcpy(bytes.data(), &value, bytes.size());
  58. }
  59. Type type;
  60. std::string name;
  61. std::vector<u8> bytes;
  62. };
  63. Entry& AddEntry(Entry&& entry);
  64. Entry* GetEntry(std::string_view key);
  65. const Entry* GetEntry(std::string_view key) const;
  66. Entry* GetOrAddEntry(std::string_view key, Entry::Type type);
  67. void RemoveEntry(std::string_view key);
  68. // Intended for use with the non array types.
  69. template <typename T>
  70. T GetData(std::string_view key, T default_value) const
  71. {
  72. const Entry* entry = GetEntry(key);
  73. return entry ? entry->GetData(default_value) : default_value;
  74. }
  75. template <typename T>
  76. void SetData(std::string_view key, Entry::Type type, T value)
  77. {
  78. GetOrAddEntry(key, type)->SetData(value);
  79. }
  80. private:
  81. void InsertDefaultEntries();
  82. bool LoadFromFile(const IOS::HLE::FS::FileHandle& file);
  83. std::vector<Entry> m_entries;
  84. std::shared_ptr<IOS::HLE::FS::FileSystem> m_fs;
  85. };