config.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. for (auto& l : configText)
  101. {
  102. char targetA, targetB, targetC, targetD;
  103. if (l.find(targetA) != std::string::npos)
  104. {
  105. if (IsDlgButtonChecked(hWnd1, CONF_AntiCheat_OFF) == BST_CHECKED)
  106. l = targetA + "=Disabled";
  107. else if (IsDlgButtonChecked(hWnd1, CONF_AntiCheat_ON) == BST_CHECKED)
  108. l = targetA + "=Disabled";
  109. else l = targetA + "=None";
  110. return;
  111. }
  112. }
  113. }
  114. }
  115. //whole
  116. void Submit(HWND hWnd1)
  117. {
  118. std::vector<std::string> configText = ConfigRead();
  119. //EDIT
  120. ConfigSet_EDIT(configText, hWnd1,
  121. "server-name", CONF_ServerName);
  122. ConfigSet_EDIT(configText, hWnd1,
  123. "level-name", CONF_LevelName);
  124. ConfigSet_EDIT(configText, hWnd1,
  125. "level-seed", CONF_LevelSeed);
  126. ConfigSet_EDIT(configText, hWnd1,
  127. "max-players", CONF_MaxPlayer);
  128. //CHECK
  129. ConfigSet_CHECK_1(configText, hWnd1,
  130. "allow-cheats", CONF_Cheat, CONF_Cheat_NOEDIT);
  131. ConfigSet_CHECK_1(configText, hWnd1,
  132. "online-mode", CONF_OnlineMode, CONF_OnlineMode_NOEDIT);
  133. ConfigSet_CHECK_1(configText, hWnd1,
  134. "enable-lan-visibility", CONF_LanVisible, CONF_LanVisible_NOEDIT);
  135. ConfigSet_CHECK_1(configText, hWnd1,
  136. "allow-list", CONF_Whitelist, CONF_Whitelist_NOEDIT);
  137. ConfigSet_CHECK_1(configText, hWnd1,
  138. "texturepack-required", CONF_FroceTexture, CONF_FroceTexture_NOEDIT);
  139. ConfigSet_CHECK_1(configText, hWnd1,
  140. "disable-custom-skins", CONF_BanSkin, CONF_BanSkin_NOEDIT);
  141. //CheckDIY
  142. ConfigSet_CheckDIY_Mute(configText, hWnd1,
  143. "chat-restriction", CONF_Mute, CONF_Mute_NOEDIT);
  144. ConfigWrite(configText);
  145. MessageBox(hWnd,
  146. L"已尝试提交", TITLE,
  147. MB_OK);
  148. }