config.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "Win32.h"
  2. #include "Config.h"
  3. #include "Function.h"
  4. #include "SharedValue.h"
  5. #include <vector>
  6. //basic
  7. static std::vector<std::string> ConfigRead()
  8. {
  9. std::ifstream read("server.properties");
  10. Err(&read, _T("打开配置文件失败"));
  11. std::vector<std::string> configs;
  12. std::string config;
  13. while (getline(read, config))
  14. {
  15. configs.push_back(config);
  16. }
  17. read.close();
  18. return configs;
  19. }
  20. static void ConfigWrite(std::vector<std::string> &configText)
  21. {
  22. std::ofstream write("server.properties", std::ios::out);
  23. Err(&write, _T("写入配置文件失败"));
  24. for (const auto& in : configText)
  25. {
  26. write << in << "\n";
  27. }
  28. write.close();
  29. }
  30. //normal
  31. static void ConfigSet_EDIT(std::vector<std::string> &configText, HWND hWnd1, std::string target, int id)
  32. {
  33. char newText[1024] = { 0 };
  34. GetWindowTextA(GetDlgItem(hWnd1, id), newText, 1024);
  35. if (newText != "不修改")
  36. {
  37. for (auto& l : configText)
  38. {
  39. if (l.find(target) != std::string::npos)
  40. {
  41. l = target + '=' + newText;
  42. }
  43. return;
  44. }
  45. }
  46. }
  47. static void ConfigSet_CHECK_1(std::vector<std::string>& configText, HWND hWnd1, std::string target, int id, int no_id)
  48. {
  49. if (IsDlgButtonChecked(hWnd1, no_id) != BST_CHECKED)
  50. {
  51. for (auto& l : configText)
  52. {
  53. if (l.find(target) != std::string::npos)
  54. {
  55. if(IsDlgButtonChecked(hWnd1, id) == BST_CHECKED)
  56. l = target + "=true";
  57. else l = target + "=false";
  58. return;
  59. }
  60. }
  61. }
  62. }
  63. static void ConfigSet_CHECK_2(std::vector<std::string>& configText, HWND hWnd1, std::string target, int id, int no_id)
  64. {
  65. if (IsDlgButtonChecked(hWnd1, no_id) != BST_CHECKED)
  66. {
  67. for (auto& l : configText)
  68. {
  69. if (l.find(target) != std::string::npos)
  70. {
  71. if (IsDlgButtonChecked(hWnd1, id) == BST_CHECKED)
  72. l = target + "=false";
  73. else l = target + "=true";
  74. return;
  75. }
  76. }
  77. }
  78. }
  79. //DIY
  80. static void ConfigSet_CheckDIY_Mute(std::vector<std::string>& configText, HWND hWnd1, std::string target, int id, int no_id)
  81. {
  82. if (IsDlgButtonChecked(hWnd1, no_id) != BST_CHECKED)
  83. {
  84. for (auto& l : configText)
  85. {
  86. if (l.find(target) != std::string::npos)
  87. {
  88. if (IsDlgButtonChecked(hWnd1, id) == BST_CHECKED)
  89. l = target + "=Disabled";
  90. else l = target + "=None";
  91. return;
  92. }
  93. }
  94. }
  95. }
  96. static void ConfigSet_CheckDIY_AntiCheat(std::vector<std::string>& configText, HWND hWnd1)
  97. {
  98. if (IsDlgButtonChecked(hWnd1, CONF_AntiCheat_NOEDIT) != BST_CHECKED)
  99. {
  100. std::string targetA = "server-authoritative-movement-strict",
  101. targetB = "server-authoritative-dismount-strict",
  102. targetC = "server-authoritative-entity-interactions-strict";
  103. for (auto& l : configText)
  104. {
  105. bool check = (IsDlgButtonChecked(hWnd1, CONF_AntiCheat) == BST_CHECKED);
  106. if (l.find(targetA) != std::string::npos)
  107. {
  108. if(check)
  109. l = targetA + "=true";
  110. else l = targetA + "=false";
  111. }
  112. if (l.find(targetB) != std::string::npos)
  113. {
  114. if (check)
  115. l = targetB + "=true";
  116. else l = targetB + "=false";
  117. }
  118. if (l.find(targetC) != std::string::npos)
  119. {
  120. if (check)
  121. l = targetC + "=true";
  122. else l = targetC + "=false";
  123. }
  124. }
  125. }
  126. }
  127. //whole
  128. void Submit(HWND hWnd1)
  129. {
  130. std::vector<std::string> configText = ConfigRead();
  131. //EDIT
  132. ConfigSet_EDIT(configText, hWnd1,
  133. "server-name", CONF_ServerName);
  134. ConfigSet_EDIT(configText, hWnd1,
  135. "level-name", CONF_LevelName);
  136. ConfigSet_EDIT(configText, hWnd1,
  137. "level-seed", CONF_LevelSeed);
  138. ConfigSet_EDIT(configText, hWnd1,
  139. "max-players", CONF_MaxPlayer);
  140. //CHECK
  141. ConfigSet_CHECK_1(configText, hWnd1,
  142. "allow-cheats", CONF_Cheat, CONF_Cheat_NOEDIT);
  143. ConfigSet_CHECK_1(configText, hWnd1,
  144. "online-mode", CONF_OnlineMode, CONF_OnlineMode_NOEDIT);
  145. ConfigSet_CHECK_1(configText, hWnd1,
  146. "enable-lan-visibility", CONF_LanVisible, CONF_LanVisible_NOEDIT);
  147. ConfigSet_CHECK_1(configText, hWnd1,
  148. "allow-list", CONF_Whitelist, CONF_Whitelist_NOEDIT);
  149. ConfigSet_CHECK_1(configText, hWnd1,
  150. "texturepack-required", CONF_FroceTexture, CONF_FroceTexture_NOEDIT);
  151. ConfigSet_CHECK_1(configText, hWnd1,
  152. "disable-custom-skins", CONF_BanSkin, CONF_BanSkin_NOEDIT);
  153. //CheckDIY
  154. ConfigSet_CheckDIY_Mute(configText, hWnd1,
  155. "chat-restriction", CONF_Mute, CONF_Mute_NOEDIT);
  156. ConfigSet_CheckDIY_AntiCheat(configText, hWnd1);
  157. //submit
  158. ConfigWrite(configText);
  159. MessageBox(hWnd,
  160. L"已尝试提交", TITLE,
  161. MB_OK);
  162. }