Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c00b611

Browse files
authored
Merge branch 'master' into statemapgenerator-update
2 parents d65298b + c109c12 commit c00b611

File tree

5 files changed

+193
-14
lines changed

5 files changed

+193
-14
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ Scripts included:
1414
- hoi4ideagfxentry.py - Idea GFX entry generator- generates idea GFX entries for all ideas in a given file.
1515
- hoi4fileformatter.py - Indents files for readability and consistency.
1616
- USAElectionGenerator.py - Generates events simulating first-past-the-post elections (US style) using a .csv spreadsheet.
17+
- hoi4statemapgenerator.py - Generates an image file of a map with every state/strategic region having a separate color and ID. Examples: Vanilla: https://cdn.discordapp.com/attachments/463044308002406402/465588079579758602/out.png EaW: https://cdn.discordapp.com/attachments/463044308002406402/465591100237676554/out.png
1718

1819
MIT license (LICENSE) applies to every file in this repository.

‎python2/hoi4localisationadder.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@
3838
#############################
3939
def readfile(name):
4040
print("Reading file " + name + "...")
41-
with open(name, "r") as f:
42-
lines = f.read().splitlines()
41+
try:
42+
with open(name, "r") as f:
43+
lines = f.read().splitlines()
44+
except:
45+
try:
46+
with open(name, "r", encoding='utf-8') as f:
47+
lines = f.read().splitlines()
48+
except:
49+
try:
50+
with open(name, "r", encoding='utf-8-sig') as f:
51+
lines = f.read().splitlines()
52+
except:
53+
print("Could not read file " + name + "!")
4354
tags = collections.OrderedDict()
4455

4556
open_blocks = 0
@@ -120,15 +131,27 @@ def readfile(name):
120131
#if not parsed_file[1][0] and not parsed_file[1][1] and not parsed_file[1][2:]:
121132
# sys.exit("File " + args.input + " is not a valid event, national_focus or ideas file.")
122133
lines = list()
123-
with open(args.output,"r") as f:
124-
lines = f.read().splitlines()
134+
try:
135+
with open(args.output, "r") as f:
136+
lines = f.read().splitlines()
137+
except:
138+
try:
139+
with open(args.output, "r", encoding='utf-8') as f:
140+
lines = f.read().splitlines()
141+
except:
142+
try:
143+
with open(args.output, "r", encoding='utf-8-sig') as f:
144+
lines = f.read().splitlines()
145+
except:
146+
print("Could not read file " + args.output + "!")
125147
output_lines = list()
126148
if len(lines) < 1:
127149
print("Output file " + args.output + " is empty or doesn't exist, creating a new english localisation file.")
128150
output_lines.append("l_english:")
129151
for line in lines:
130-
for i,parsed_line in enumerate(parsed_file[0]):
131-
if parsed_line in line:
152+
for i, parsed_line in enumerate(parsed_file[0]):
153+
match = re.match(r"^([^#:]*):", line)
154+
if match and match.group(1).strip() == parsed_line.strip():
132155
print(parsed_line + " already in output file, skipping")
133156
parsed_file[0].remove(parsed_file[0][i])
134157
if len(parsed_file[0]) > 0:

‎python3/hoi4fileformatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def formatfile(name, remove_whitespace, ignore_comments):
7070
line = ('\t' * open_blocks) + line
7171
line = re.sub(r"\s*$", "", line)
7272
new_lines.append(line)
73-
open_blocks = open_blocks + line.count('{')
74-
open_blocks = open_blocks - line.count('}')
73+
open_blocks = open_blocks + re.sub(r'\".*?\"', '', line).count('{')
74+
open_blocks = open_blocks - re.sub(r'\".*?\"', '', line).count('}')
7575
with open(name, "w") as f:
7676
f.writelines(str(line) + "\n" for line in new_lines)
7777

‎python3/hoi4newspaperheaderadded.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/python
2+
import argparse
3+
import os
4+
import sys
5+
import re
6+
import collections
7+
import glob
8+
9+
#############################
10+
###
11+
### HoI 4 News Event Title Header Adder by Yard1, originally for Equestria at War mod
12+
### Written in Python 3.7
13+
###
14+
### Copyright (c) 2018 Antoni Baum (Yard1)
15+
### Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16+
### The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
17+
### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
###
19+
### usage: hoi4newspaperheaderadded.py [-h] [--scripted_loc scripted_loc] mod_path
20+
###
21+
### Given a mod folder, add a scripted localisation call to every news event title
22+
### (including triggered titles).
23+
###
24+
### positional arguments:
25+
### mod_path Path to the root mod folder
26+
###
27+
### optional arguments:
28+
### -h, --help show this help message and exit
29+
### --scripted_loc scripted_loc
30+
### The full string (including brackets) to prefix
31+
### localisation values with (Default:
32+
### [This.GetNewspaperHeader])
33+
###
34+
#############################
35+
36+
def readable_dir(prospective_dir):
37+
if not os.path.isdir(prospective_dir):
38+
raise Exception("readable_dir:{0} is not a valid path".format(prospective_dir))
39+
if os.access(prospective_dir, os.R_OK):
40+
return prospective_dir
41+
else:
42+
raise Exception("readable_dir:{0} is not a readable dir".format(prospective_dir))
43+
44+
#############################
45+
46+
def read_event_file(name, loc_set):
47+
print("Reading file " + name + "...")
48+
lines = []
49+
try:
50+
with open(name, "r") as f:
51+
lines = f.read().splitlines()
52+
except:
53+
try:
54+
with open(name, "r", encoding='utf-8') as f:
55+
lines = f.read().splitlines()
56+
except:
57+
try:
58+
with open(name, "r", encoding='utf-8-sig') as f:
59+
lines = f.read().splitlines()
60+
except:
61+
print("Could not read file " + name + "!")
62+
63+
open_blocks = 0
64+
is_in_news_event = False
65+
is_in_title = False
66+
for line in lines:
67+
line = re.sub(r'#.*?$', "", line)
68+
if open_blocks == 0 and "news_event" in line:
69+
is_in_news_event = True
70+
if is_in_news_event:
71+
match = re.search(r'(^|\s)\s*title\s*=\s*([^\{]+?)(\s|$)', line)
72+
if match:
73+
loc_set.add(match.group(2).strip())
74+
else:
75+
match = re.search(r'(^|\s)\s*title\s*=\s*{', line)
76+
if match:
77+
is_in_title = True
78+
if is_in_title:
79+
match = re.search(r'(^|\s)\s*text\s*=\s*([^\{]+?)\s*($|\})', line)
80+
if match:
81+
loc_set.add(match.group(2).strip())
82+
open_blocks += line.count('{')
83+
open_blocks -= line.count('}')
84+
if open_blocks == 1:
85+
is_in_title = False
86+
if is_in_news_event and open_blocks == 0:
87+
is_in_news_event = False
88+
89+
print("File " + name + " read successfully!")
90+
return
91+
92+
def read_loc_file(name, loc_set, scripted_loc_re, scripted_loc):
93+
print("Reading file " + name + "...")
94+
lines = []
95+
try:
96+
with open(name, "r", encoding='utf-8-sig') as f:
97+
lines = f.read().splitlines()
98+
except:
99+
print("Could not read file " + name + "!")
100+
print("File " + name + " read successfully!")
101+
new_lines = []
102+
has_changed = False
103+
for line in lines:
104+
match = re.search(r'^\s*?([^#\s]*?):[0-9]+', line)
105+
if match:
106+
loc_key = match.group(1).strip()
107+
if loc_key in loc_set:
108+
line = scripted_loc_re.sub("\\1\\2" + scripted_loc, line)
109+
has_changed = True
110+
new_lines.append(line)
111+
if has_changed:
112+
with open(name, "w", encoding="utf-8-sig") as f:
113+
f.writelines(str(line) + "\n" for line in new_lines)
114+
print("File " + name + " modified successfully!")
115+
return
116+
###################################################################
117+
parser = argparse.ArgumentParser(description='Given a mod folder, add a scripted localisation call to every news event title (including triggered titles).')
118+
parser.add_argument('mod_path', metavar='mod_path',
119+
help='Path to the root mod folder')
120+
parser.add_argument( '--scripted_loc', metavar='scripted_loc', default="[This.GetNewspaperHeader]", required=False,
121+
help='The full string (including brackets) to prefix localisation values with (Default: [This.GetNewspaperHeader])')
122+
123+
args = parser.parse_args()
124+
125+
events_path = os.path.join(args.mod_path, 'events')
126+
loc_path = os.path.join(args.mod_path, 'localisation')
127+
128+
try:
129+
dir = readable_dir(events_path)
130+
except:
131+
print("{0} is not a directory or does not exist." % events_path)
132+
133+
try:
134+
dir = readable_dir(loc_path)
135+
except:
136+
print("{0} is not a directory or does not exist." % loc_path)
137+
138+
loc_set = set()
139+
140+
for file in glob.glob(os.path.join(events_path, '*.txt')):
141+
read_event_file(file, loc_set)
142+
143+
scripted_loc = args.scripted_loc.strip()
144+
scripted_loc_re_string = r'(\s*?[^\s]*?:[0-9]+\s*)(\")(?!' + re.escape(scripted_loc) + r')'
145+
scripted_loc_re = re.compile(scripted_loc_re_string, re.IGNORECASE)
146+
147+
for file in glob.glob(os.path.join(loc_path, '*.yml'), recursive=True):
148+
read_loc_file(file, loc_set, scripted_loc_re, scripted_loc)
149+
150+
try:
151+
dir = readable_dir(os.path.join(loc_path, "replace"))
152+
for file in glob.glob(os.path.join(loc_path, "replace", '*.yml'), recursive=True):
153+
read_loc_file(file, loc_set, scripted_loc_re, scripted_loc)
154+
except:
155+
pass

‎python3/hoi4statemapgenerator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ def load_provinces(name):
137137
def load_definition(name):
138138
print("Reading file " + name + "...")
139139
try:
140-
with open(name, "r") as f:
140+
with open(name, "r", encoding='utf-8-sig') as f:
141141
lines = f.read().splitlines()
142142
except:
143143
try:
144144
with open(name, "r", encoding='utf-8') as f:
145145
lines = f.read().splitlines()
146146
except:
147147
try:
148-
with open(name, "r", encoding='utf-8-sig') as f:
148+
with open(name, "r") as f:
149149
lines = f.read().splitlines()
150150
except:
151151
print("Could not read file " + name + "!")
@@ -161,15 +161,15 @@ def load_state_file(name, states_dict):
161161
print("Reading file " + name + "...")
162162
file_str = ""
163163
try:
164-
with open(name, "r") as f:
164+
with open(name, "r", encoding='utf-8-sig') as f:
165165
file_str = f.read()
166166
except:
167167
try:
168168
with open(name, "r", encoding='utf-8') as f:
169169
file_str = f.read()
170170
except:
171171
try:
172-
with open(name, "r", encoding='utf-8-sig') as f:
172+
with open(name, "r") as f:
173173
file_str = f.read()
174174
except:
175175
print("Could not read file " + name + "!")
@@ -219,15 +219,15 @@ def load_state_file(name, states_dict):
219219
def load_pdx_colors_file(name):
220220
print("Reading file " + name + "...")
221221
try:
222-
with open(name, "r") as f:
222+
with open(name, "r", encoding='utf-8-sig') as f:
223223
file_str = f.read()
224224
except:
225225
try:
226226
with open(name, "r", encoding='utf-8') as f:
227227
file_str = f.read()
228228
except:
229229
try:
230-
with open(name, "r", encoding='utf-8-sig') as f:
230+
with open(name, "r") as f:
231231
file_str = f.read()
232232
except:
233233
print("Could not read file " + name + "!")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /