SysConf.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include "Common/Common.h"
  11. // This class is meant to edit the values in a given Wii SYSCONF file
  12. // It currently does not add/remove/rearrange sections,
  13. // instead only modifies exiting sections' data
  14. #define SYSCONF_SIZE 0x4000
  15. enum SysconfType
  16. {
  17. Type_BigArray = 1,
  18. Type_SmallArray,
  19. Type_Byte,
  20. Type_Short,
  21. Type_Long,
  22. Type_Unknown,
  23. Type_Bool
  24. };
  25. struct SSysConfHeader
  26. {
  27. char version[4];
  28. u16 numEntries;
  29. };
  30. struct SSysConfEntry
  31. {
  32. u16 offset;
  33. SysconfType type;
  34. u8 nameLength;
  35. char name[32];
  36. u16 dataLength;
  37. u8* data;
  38. template<class T>
  39. T GetData() { return *(T*)data; }
  40. bool GetArrayData(u8* dest, u16 destSize)
  41. {
  42. if (dest && destSize >= dataLength)
  43. {
  44. memcpy(dest, data, dataLength);
  45. return true;
  46. }
  47. return false;
  48. }
  49. bool SetArrayData(u8* buffer, u16 bufferSize)
  50. {
  51. if (buffer)
  52. {
  53. memcpy(data, buffer, std::min<u16>(bufferSize, dataLength));
  54. return true;
  55. }
  56. return false;
  57. }
  58. };
  59. class SysConf
  60. {
  61. public:
  62. SysConf();
  63. ~SysConf();
  64. bool IsValid() { return m_IsValid; }
  65. template<class T>
  66. T GetData(const char* sectionName)
  67. {
  68. if (!m_IsValid)
  69. {
  70. PanicAlertT("Trying to read from invalid SYSCONF");
  71. return 0;
  72. }
  73. std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
  74. for (; index < m_Entries.end() - 1; ++index)
  75. {
  76. if (strcmp(index->name, sectionName) == 0)
  77. break;
  78. }
  79. if (index == m_Entries.end() - 1)
  80. {
  81. PanicAlertT("Section %s not found in SYSCONF", sectionName);
  82. return 0;
  83. }
  84. return index->GetData<T>();
  85. }
  86. bool GetArrayData(const char* sectionName, u8* dest, u16 destSize)
  87. {
  88. if (!m_IsValid)
  89. {
  90. PanicAlertT("Trying to read from invalid SYSCONF");
  91. return 0;
  92. }
  93. std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
  94. for (; index < m_Entries.end() - 1; ++index)
  95. {
  96. if (strcmp(index->name, sectionName) == 0)
  97. break;
  98. }
  99. if (index == m_Entries.end() - 1)
  100. {
  101. PanicAlertT("Section %s not found in SYSCONF", sectionName);
  102. return 0;
  103. }
  104. return index->GetArrayData(dest, destSize);
  105. }
  106. bool SetArrayData(const char* sectionName, u8* buffer, u16 bufferSize)
  107. {
  108. if (!m_IsValid)
  109. return false;
  110. std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
  111. for (; index < m_Entries.end() - 1; ++index)
  112. {
  113. if (strcmp(index->name, sectionName) == 0)
  114. break;
  115. }
  116. if (index == m_Entries.end() - 1)
  117. {
  118. PanicAlertT("Section %s not found in SYSCONF", sectionName);
  119. return false;
  120. }
  121. return index->SetArrayData(buffer, bufferSize);
  122. }
  123. template<class T>
  124. bool SetData(const char* sectionName, T newValue)
  125. {
  126. if (!m_IsValid)
  127. return false;
  128. std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
  129. for (; index < m_Entries.end() - 1; ++index)
  130. {
  131. if (strcmp(index->name, sectionName) == 0)
  132. break;
  133. }
  134. if (index == m_Entries.end() - 1)
  135. {
  136. PanicAlertT("Section %s not found in SYSCONF", sectionName);
  137. return false;
  138. }
  139. *(T*)index->data = newValue;
  140. return true;
  141. }
  142. bool Save();
  143. bool SaveToFile(const std::string& filename);
  144. bool LoadFromFile(const std::string& filename);
  145. bool Reload();
  146. // This function is used when the NAND root is changed
  147. void UpdateLocation();
  148. private:
  149. bool LoadFromFileInternal(FILE *fh);
  150. void GenerateSysConf();
  151. void Clear();
  152. std::string m_Filename;
  153. std::string m_FilenameDefault;
  154. std::vector<SSysConfEntry> m_Entries;
  155. bool m_IsValid;
  156. };