Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.function.Function;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import java.util.stream.Collectors;
  13.  
  14. public class Main {
  15.  
  16. public final static String eu4HistoryPath = "C:\\Program Files\\Steam\\steamapps\\common\\Europa Universalis IV\\history\\";
  17. public final static String pgvd4HistoryPath = "C:\\Program Files\\Steam\\steamapps\\workshop\\content\\236850\\1872558755\\history\\";
  18.  
  19.  
  20. public static void main(String[] args) throws Exception {
  21. File folder = new File(pgvd4HistoryPath + "provinces\\");
  22.  
  23.  
  24. List<Integer> ids = List.of(2768
  25.  
  26. ).stream().distinct().collect(Collectors.toList());
  27.  
  28. moveFiles(ids);
  29. List<File> files = Arrays.stream(folder.listFiles()).collect(Collectors.toList());
  30. files.stream().filter(e -> idMatcher(e.getName(), ids)).forEach(f -> doActionOnFile(f, s -> "", s -> s.startsWith("owner") || s.startsWith("add_core ") || s.startsWith("controller")));
  31.  
  32. /*moveFiles(List.of(286, 379, 1026, 206, 333, 4306, 303, 298, 184, 208, 87, 345,344,2470,1110,347,348,2464,2466,2460,346));
  33.  
  34. addCore(files, List.of(1751), "MOR", true);
  35. addCore(files, List.of(232, 231, 4558, 4555, 4548), "POR", true);
  36. addCore(files, List.of(4554, 206), "GAL", true);
  37. addCore(files, List.of(207, 1745, 209, 2989, 1746, 4552, 215, 4553, 208), "ASU", true);
  38. addCore(files, List.of(207), "ASU", true);
  39. addCore(files, List.of(224, 1749, 225, 1748, 4547, 221), "GRA", true);
  40.  
  41.  
  42. addCore(files, List.of(174, 175, 176, 173), "GUY", true);
  43. addCore(files, List.of(197, 196, 2753, 198, 199, 203, 4391, 200, 204, 2991, 4112, 190), "TOU", true);
  44. addCore(files, List.of(179, 195, 194, 4387, 180, 4386, 4111), "BER", true);
  45. addCore(files, List.of(89, 167, 168, 4385, 1879, 177, 178, 172, 4384, 169, 171, 170), "NRM", true);
  46. addCore(files, List.of(95, 98, 99, 4381, 4382, 100), "HOL", false);
  47. addCore(files, List.of(373), "MTH", true);
  48. addCore(files, List.of(4365), "KOI", true);
  49. addCore(files, List.of(242, 4366, 241, 4369), "WLS", true);
  50. addCore(files, List.of(4389), "ORL", true);
  51. addCore(files, List.of(78, 79, 66, 71, 70, 74, 72, 75, 1880), "PAL", false);
  52. addCore(files, List.of(2966, 4238, 264, 263), "BOH", false);
  53. addCore(files, List.of(73, 110, 1768), "TIR", true);
  54. addCore(files, List.of(1775, 13), "DAN", false);
  55. addCore(files, List.of(27, 28, 30, 31, 29, 4123, 19, 4152, 4113, 4151), "FIN", true);
  56. addCore(files, List.of(39, 1935, 273, 4241, 37, 1834, 271), "TEU", false);
  57. addCore(files, List.of(127, 2986), "SAR", true);
  58. addCore(files, List.of(124, 125, 2982), "SIC", true);
  59. addCore(files, List.of(275, 4242, 292, 4246, 771, 4243), "PLT", true);
  60. addCore(files, List.of(4245, 293, 297, 4244), "SMO", true);
  61. addCore(files, List.of(4254, 306, 2423), "NZH", true);
  62. addCore(files, List.of(1752), "TVE", true);
  63. addCore(files, List.of(4117), "YAR", true);
  64.  
  65. addCore(files, List.of(289, 2407, 4542, 280, 1944, 1943, 4540, 1942, 4247, 281, 4539, 2962, 279, 1940, 2961, 261, 4541, 2424, 1940, 4115), "KIE", true);
  66. addCore(files, List.of(1773, 145, 146), "ATH", true);
  67. addCore(files, List.of(149, 1764, 2750, 1765, 2746, 148, 3001, 1853, 147, 144, 143, 4175), "BYZ", true);
  68. addCore(files, List.of(150, 159), "BUL", true);
  69. addCore(files, List.of(2313, 377, 407, 1849, 405, 4271, 4270, 382, 4297, 378, 1855, 381, 1854, 364), "SYR", true);
  70. addCore(files, List.of(4269, 383, 4268, 2327, 380, 379, 365, 2315), "ANZ", true);
  71. addCore(files, List.of(2461, 337, 2459, 126, 4559), "TUN", false);
  72. addCore(files, List.of(1755, 4262, 4263, 314), "NOV", true);
  73. addCore(files, List.of(1945, 4543, 298, 291, 290), "CHR", true);
  74. addCore(files, List.of(283, 2406, 282), "ZAZ", true);
  75. addCore(files, List.of(355), "TUN", true);
  76. addCore(files, List.of(88), "PIC", true);
  77. addCore(files, List.of(247, 246, 4368), "NOL", true);
  78. addCore(files, List.of(2314), "QAR", true);
  79.  
  80. addCore(files, List.of(1826,131,152,1767), "CRO", true);
  81. addCore(files, List.of(162,1952,157,4127,158,4128,1951), "TRA", true);
  82. //addCore(files, List.of(), "", true);
  83. //removeCores(files, List.of(), "", "");
  84.  
  85. addDev();*/
  86. }
  87.  
  88. private static void addCots(List<File> files, List<Integer> ids) {
  89. files.stream()
  90. .filter(f -> idMatcher(f.getName(), ids))
  91. .forEach(file -> {
  92. List<String> strings;
  93. try {
  94. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. return;
  98. }
  99.  
  100. if (strings.stream().anyMatch(s -> s.startsWith("center_of_trade "))) {
  101. return;
  102. }
  103. for (int i = 0; i < strings.size(); i++) {
  104. String s = strings.get(i);
  105. if (s.startsWith("add_core")) {
  106. strings.add(i, "center_of_trade = 1");
  107. break;
  108. }
  109. }
  110. try {
  111. FileWriter fw = new FileWriter(file, false);
  112. fw.write(String.join("\n", strings));
  113. fw.close();
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. });
  119. }
  120.  
  121. private static void addDev() throws Exception {
  122. File folder = new File(pgvd4HistoryPath + "provinces\\");
  123. List<File> files = Arrays.stream(folder.listFiles()).collect(Collectors.toList());
  124.  
  125. addDev(files, List.of(289, 2407, 4542, 1944, 280, 1942, 4247), 1, 1, 2); //kiev
  126. addDev(files, List.of(2961, 289, 1945, 4545, 4242, 2409), 1, 2, 0); //kijów i okolice
  127. addDev(files, List.of(2456, 2450, 2453, 339, 354), 1, 2, 0); //tunis okolice
  128. //addDev(files, List.of(), );
  129. // ` addCore(files, List.of(), "", false);
  130. addCore(files, List.of(1024, 1837, 1023, 1835, 4359, 1030, 4360, 1839, 1029, 4189, 1020, 1832, 4187, 1021, 4185), "TKG", false);
  131. addCore(files, List.of(1818, 4182, 1014, 4181, 4180, 1012), "OUC", false);
  132. addCore(files, List.of(1830, 1019, 4184, 1018, 1825, 4183, 1017, 4186, 1819, 1820), "YMN", false);
  133. addCore(files, List.of(1026, 1025, 4131, 4192, 4193, 1847, 1031, 1852), "DTE", false);
  134.  
  135. addCots(files, List.of(1847, 286, 379, 4131, 1026, 206, 333, 4306, 303, 298, 184));
  136. removeCots(files, List.of(208, 214));
  137.  
  138. removeCores(files, List.of(345,344,2470,1110,347,348,2464,2466,2460,346), "MOR");
  139. //removeCores(files, List.of(), "", "");
  140.  
  141.  
  142. //files.forEach(Main::changeReligionInjapan);
  143. //changeJapanCountriesReligion();
  144. removeFort(files, List.of(87, 2753,246));
  145. }
  146.  
  147. private static void removeCores(List<File> files, List<Integer> ids, String whose) {
  148. files.stream().filter(f -> idMatcher(f.getName(), ids))
  149. .forEach(f -> doActionOnFile(f, (String s) -> null, s -> s.startsWith("add_core") && s.contains(whose)));
  150. }
  151.  
  152. private static void removeFort(List<File> files, List<Integer> ids) {
  153. files.stream().filter(f -> idMatcher(f.getName(), ids))
  154. .forEach(f -> doActionOnFile(f, (String s) -> null, s -> s.startsWith("fort_15th")));
  155. }
  156.  
  157. private static void removeCots(List<File> files, List<Integer> provinces) {
  158. files.stream()
  159. .filter(f -> idMatcher(f.getName(), provinces))
  160. .forEach(f -> doActionOnFile(f, (String s) -> null, (String s) -> s.startsWith("center_of_trade")));
  161. }
  162.  
  163. private static void doActionOnFile(File file, Function<String, String> action, Function<String, Boolean> checker) {
  164. List<String> strings;
  165. try {
  166. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. return;
  170. }
  171.  
  172. for (int i = 0; i < strings.size(); i++) {
  173. String s = strings.get(i);
  174. if (checker.apply(s)) {
  175. String ret = action.apply(s);
  176. if (s != null) {
  177. strings.set(i, ret);
  178. } else {
  179. strings.set(i, "");
  180. }
  181. break;
  182. }
  183. }
  184.  
  185. try {
  186. FileWriter fw = new FileWriter(file, false);
  187. fw.write(String.join("\n", strings));
  188. fw.close();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193.  
  194. private static void moveFiles(List<Integer> integers) {
  195. File from = new File(eu4HistoryPath + "provinces\\");
  196.  
  197. Arrays.stream(from.listFiles()).filter(f -> idMatcher(f.getName(), integers)).forEach(f -> {
  198. try {
  199.  
  200. File to = new File(pgvd4HistoryPath + "provinces\\" + f.getName());
  201.  
  202. if (to.exists()) {
  203. return;
  204. }
  205.  
  206. FileWriter fw = new FileWriter(pgvd4HistoryPath + "provinces\\" + f.getName());
  207.  
  208. fw.write(String.join("\n", Files.readAllLines(Paths.get(f.getPath()), StandardCharsets.ISO_8859_1)));
  209. fw.close();
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. }
  213. });
  214.  
  215. }
  216.  
  217. private static void changeJapanCountriesReligion() throws Exception {
  218. File folder = new File(pgvd4HistoryPath + "countries\\");
  219. List<File> files = Arrays.stream(folder.listFiles()).collect(Collectors.toList());
  220.  
  221. files.forEach(file -> {
  222. List<String> strings;
  223. try {
  224. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. return;
  228. }
  229.  
  230. for (int i = 0; i < strings.size(); i++) {
  231. String s = strings.get(i);
  232. if (s.startsWith("religion")) {
  233. strings.set(i, "religion = sunni");
  234. }
  235. if (s.startsWith("add_government_reform")) {
  236. strings.set(i, "add_government_reform = feudalism_reform");
  237. }
  238.  
  239. }
  240.  
  241. try {
  242. FileWriter fw = new FileWriter(file, false);
  243. fw.write(String.join("\n", strings));
  244. fw.close();
  245. } catch (IOException e) {
  246. e.printStackTrace();
  247. }
  248. });
  249. }
  250.  
  251. private static void changeReligionInjapan(File file) {
  252. List<String> strings;
  253. try {
  254. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  255. } catch (IOException e) {
  256. e.printStackTrace();
  257. return;
  258. }
  259.  
  260. for (int i = 0; i < strings.size(); i++) {
  261. String s = strings.get(i);
  262. if (s.startsWith("religion = shinto")) {
  263. strings.set(i, "religion = sunni #japan");
  264. }
  265. }
  266.  
  267. try {
  268. FileWriter fw = new FileWriter(file, false);
  269. fw.write(String.join("\n", strings));
  270. fw.close();
  271. } catch (IOException e) {
  272. e.printStackTrace();
  273. }
  274.  
  275. }
  276.  
  277. private static void addDev(List<File> files, List<Integer> provinces, int adm, int dip, int mil) {
  278. files.stream()
  279. .filter(i -> idMatcher(i.getName(), provinces))
  280. .forEach(i -> addDev(i, adm, dip, mil));
  281. }
  282.  
  283. private static void addDev(File file, int adm, int dip, int mil) {
  284. List<String> strings;
  285. Pattern pattern = Pattern.compile("[a-z_ =]*([0-9]+ *)");
  286. try {
  287. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  288. } catch (IOException e) {
  289. e.printStackTrace();
  290. return;
  291. }
  292.  
  293. for (int i = 0; i < strings.size(); i++) {
  294. String s = strings.get(i);
  295.  
  296. if (s.matches("base_[a-z]* = [0-9]+ *")) {
  297. Matcher matcher = pattern.matcher(s);
  298. if (!matcher.matches()) {
  299. System.console().writer().println("Didn't found dev!");
  300. break;
  301. }
  302. int dev = Integer.valueOf(matcher.group(1).trim());
  303. if (s.contains("base_tax")) {
  304. strings.set(i, "base_tax = " + (adm + dev));
  305. } else if (s.contains("base_production")) {
  306. strings.set(i, "base_production = " + (dip + dev));
  307. } else if (s.contains("base_manpower")) {
  308. strings.set(i, "base_manpower = " + (mil + dev));
  309. }
  310. }
  311. }
  312.  
  313. try {
  314. FileWriter fw = new FileWriter(file, false);
  315. fw.write(String.join("\n", strings));
  316. fw.close();
  317. } catch (IOException e) {
  318. e.printStackTrace();
  319. }
  320. }
  321.  
  322. private static void addCore(List<File> files, List<Integer> provinces, String tag, boolean removeOther) throws Exception {
  323. files.stream()
  324. .filter(i -> idMatcher(i.getName(), provinces))
  325. .forEach(f -> modify(f, tag, removeOther));
  326. }
  327.  
  328.  
  329. private static void modify(File file, String tag, boolean removeOther) {
  330. List<String> strings;
  331. try {
  332. strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  333. } catch (IOException e) {
  334. e.printStackTrace();
  335. return;
  336. }
  337.  
  338. int lastIndex = -1;
  339. int ownerIndex = 8;
  340. for (int i = 0; i < strings.size(); i++) {
  341. String s = strings.get(i);
  342.  
  343. if (removeOther && s.startsWith("add_core")) {
  344. strings.set(i, "#" + s);
  345. }
  346.  
  347. if (s.startsWith("owner =")) {
  348. strings.set(i, "owner = " + tag);
  349. ownerIndex = i;
  350. }
  351.  
  352. if (s.startsWith("controller =")) {
  353. strings.set(i, "controller = " + tag);
  354. }
  355.  
  356. if (s.matches("[0-9]{4}\\.[0-9]{1,2}.*")) {
  357. lastIndex = i;
  358. break;
  359. }
  360. }
  361.  
  362. strings.add(ownerIndex, "add_core = " + tag);
  363.  
  364. if (lastIndex != -1) {
  365. strings = strings.subList(0, lastIndex);
  366. }
  367.  
  368. String finalContent = String.join("\n", strings);
  369.  
  370. try {
  371. FileWriter fw = new FileWriter(new File(pgvd4HistoryPath + "provinces\\" + file.getName()), false);
  372. fw.write(finalContent);
  373. fw.close();
  374. } catch (IOException e) {
  375. e.printStackTrace();
  376. }
  377. }
  378.  
  379. static boolean idMatcher(String filename, List<Integer> ids) {
  380. return ids.stream().anyMatch(i -> idMatcher(filename, i));
  381. }
  382.  
  383.  
  384. static boolean idMatcher(String filename, Integer id) {
  385. Pattern pattern = Pattern.compile(id + " ?-.*");
  386. int tmp = filename.lastIndexOf("\\");
  387. return pattern.matcher(filename).matches();
  388. }
  389.  
  390. private static boolean filter(File file) {
  391. try {
  392. List<String> strings = Files.readAllLines(Paths.get(file.getPath()), StandardCharsets.ISO_8859_1);
  393.  
  394. for (String s : strings) {
  395. if (s.startsWith("discovered_by")) {
  396. return false;
  397. }
  398. if (s.startsWith("hre = yes")) {
  399. return true;
  400. }
  401. String[] france = new String[]{"FRA", "PRO", "BRE", "BUR", "NEV"};
  402. for (String token : france) {
  403. if (s.startsWith("controller = " + token)) {
  404. return true;
  405. }
  406. }
  407.  
  408. if (s.startsWith("#118 - Roma")) {
  409. return true;
  410. }
  411. }
  412. return false;
  413. } catch (IOException e) {
  414. e.printStackTrace();
  415. return false;
  416. }
  417. }
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement