config.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. static void ConfigSet_ComboDIY_Gamemode(std::vector<std::string>& configText, HWND hWnd1)
  128. {
  129. int selected = SendMessage(hGamemode, CB_GETCURSEL, 0, 0);
  130. std::string target;
  131. switch (selected)
  132. {
  133. case 0:
  134. for (auto& l : configText)
  135. {
  136. if (l.find(target) != std::string::npos)
  137. {
  138. l = target + "=";
  139. return;
  140. }
  141. }
  142. break;
  143. case 1:
  144. for (auto& l : configText)
  145. {
  146. if (l.find(target) != std::string::npos)
  147. {
  148. l = target + "=";
  149. return;
  150. }
  151. }
  152. break;
  153. case 2:
  154. for (auto& l : configText)
  155. {
  156. if (l.find(target) != std::string::npos)
  157. {
  158. l = target + "=";
  159. return;
  160. }
  161. }
  162. break;
  163. case 3:
  164. for (auto& l : configText)
  165. {
  166. if (l.find(target) != std::string::npos)
  167. {
  168. l = target + "=";
  169. return;
  170. }
  171. }
  172. break;
  173. }
  174. }
  175. static void ConfigSet_ComboDIY_Difficuty(std::vector<std::string>& configText, HWND hWnd1)
  176. {
  177. int selected = SendMessage(hDifficulty, CB_GETCURSEL, 0, 0);
  178. std::string target;
  179. switch (selected)
  180. {
  181. case 0:
  182. for (auto& l : configText)
  183. {
  184. if (l.find(target) != std::string::npos)
  185. {
  186. l = target + "=";
  187. return;
  188. }
  189. }
  190. break;
  191. case 1:
  192. for (auto& l : configText)
  193. {
  194. if (l.find(target) != std::string::npos)
  195. {
  196. l = target + "=";
  197. return;
  198. }
  199. }
  200. break;
  201. case 2:
  202. for (auto& l : configText)
  203. {
  204. if (l.find(target) != std::string::npos)
  205. {
  206. l = target + "=";
  207. return;
  208. }
  209. }
  210. break;
  211. case 3:
  212. for (auto& l : configText)
  213. {
  214. if (l.find(target) != std::string::npos)
  215. {
  216. l = target + "=";
  217. return;
  218. }
  219. }
  220. break;
  221. case 4:
  222. for (auto& l : configText)
  223. {
  224. if (l.find(target) != std::string::npos)
  225. {
  226. l = target + "=";
  227. return;
  228. }
  229. }
  230. break;
  231. }
  232. }
  233. static void ConfigSet_ComboDIY_Level(std::vector<std::string>& configText, HWND hWnd1)
  234. {
  235. int selected = SendMessage(hPermission, CB_GETCURSEL, 0, 0);
  236. std::string target;
  237. switch (selected)
  238. {
  239. case 0:
  240. for (auto& l : configText)
  241. {
  242. if (l.find(target) != std::string::npos)
  243. {
  244. l = target + "=";
  245. return;
  246. }
  247. }
  248. break;
  249. case 1:
  250. for (auto& l : configText)
  251. {
  252. if (l.find(target) != std::string::npos)
  253. {
  254. l = target + "=";
  255. return;
  256. }
  257. }
  258. break;
  259. case 2:
  260. for (auto& l : configText)
  261. {
  262. if (l.find(target) != std::string::npos)
  263. {
  264. l = target + "=";
  265. return;
  266. }
  267. }
  268. break;
  269. case 3:
  270. for (auto& l : configText)
  271. {
  272. if (l.find(target) != std::string::npos)
  273. {
  274. l = target + "=";
  275. return;
  276. }
  277. }
  278. break;
  279. }
  280. }
  281. //whole
  282. void Submit(HWND hWnd1)
  283. {
  284. std::vector<std::string> configText = ConfigRead();
  285. //EDIT
  286. ConfigSet_EDIT(configText, hWnd1,
  287. "server-name", CONF_ServerName);
  288. ConfigSet_EDIT(configText, hWnd1,
  289. "level-name", CONF_LevelName);
  290. ConfigSet_EDIT(configText, hWnd1,
  291. "level-seed", CONF_LevelSeed);
  292. ConfigSet_EDIT(configText, hWnd1,
  293. "max-players", CONF_MaxPlayer);
  294. //CHECK
  295. ConfigSet_CHECK_1(configText, hWnd1,
  296. "allow-cheats", CONF_Cheat, CONF_Cheat_NOEDIT);
  297. ConfigSet_CHECK_1(configText, hWnd1,
  298. "online-mode", CONF_OnlineMode, CONF_OnlineMode_NOEDIT);
  299. ConfigSet_CHECK_1(configText, hWnd1,
  300. "enable-lan-visibility", CONF_LanVisible, CONF_LanVisible_NOEDIT);
  301. ConfigSet_CHECK_1(configText, hWnd1,
  302. "allow-list", CONF_Whitelist, CONF_Whitelist_NOEDIT);
  303. ConfigSet_CHECK_1(configText, hWnd1,
  304. "texturepack-required", CONF_FroceTexture, CONF_FroceTexture_NOEDIT);
  305. ConfigSet_CHECK_1(configText, hWnd1,
  306. "disable-custom-skins", CONF_BanSkin, CONF_BanSkin_NOEDIT);
  307. //CheckDIY
  308. ConfigSet_CheckDIY_Mute(configText, hWnd1,
  309. "chat-restriction", CONF_Mute, CONF_Mute_NOEDIT);
  310. ConfigSet_CheckDIY_AntiCheat(configText, hWnd1);
  311. //ComboDIY
  312. ConfigSet_ComboDIY_Gamemode(configText, hWnd1);
  313. ConfigSet_ComboDIY_Difficuty(configText, hWnd1);
  314. ConfigSet_ComboDIY_Level(configText, hWnd1);
  315. //submit
  316. ConfigWrite(configText);
  317. MessageBox(hWnd,
  318. L"已尝试提交", TITLE,
  319. MB_OK);
  320. }