-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathupload_to_workshop.py
More file actions
210 lines (162 loc) · 6.33 KB
/
upload_to_workshop.py
File metadata and controls
210 lines (162 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import re
import subprocess
SEWT = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SpaceEngineers\\Bin64\\SEWorkshopTool.exe"
MODS_DIR = "SpaceEngineers\\Mods"
EXCLUDE_FILES = [".bat", ".psd", ".md", ".py", ".gitattributes", ".gitignore", ".sln", ".csproj", ".mod", ".sbmi"]
EXCLUDE_DIRS = ["bin", "obj", "WebWiki", ".git"]
UPLOAD_DESC = False
UPLOAD_PN = True
TAGS = ["Other", "ServerScripts", "NPC"]
MOD_NAME_OVERRIDE = "Modular Encounters Systems"
DRY_RUN = False
def md_to_steam_bbcode(md: str) -> str:
bbcode = []
open_quote = False
open_b = False
open_i = False
previous_new_l = ""
lines = md.splitlines()
for l in lines:
new_l = l
# Images with links [](WEB_LINK)
pattern = r'\[!\[\]\(([^)]+)\)\]\(([^)]+)\)'
matches = re.findall(pattern, new_l)
for match in matches:
image_link, web_link = match
new_l = new_l.replace(f"[]({web_link})", f"[url={web_link}][img]{image_link}[/img][/url]")
# Simple Images 
pattern = r'!\[([^\]]*)\]\(([^)]+)\)'
matches = re.findall(pattern, new_l)
for match in matches:
alt_text, image_link = match
new_l = new_l.replace(f"", f"[img]{image_link}[/img]")
# Simple URLs [LINK_TEXT](WEB_LINK)
pattern = r'\[([^\]]+)\]\(([^)]+)\)'
matches = re.findall(pattern, new_l)
for match in matches:
link_text, web_link = match
new_l = new_l.replace(f"[{link_text}]({web_link})", f"[url={web_link}]{link_text}[/url]")
# Headings
if new_l.startswith("### "):
new_l = new_l.replace("### ", "[h3]") + "[/h3]"
elif new_l.startswith("## "):
new_l = new_l.replace("## ", "[h2]") + "[/h2]"
elif new_l.startswith("# "):
new_l = new_l.replace("# ", "[h1]") + "[/h1]"
# Unordered Lists
if new_l.startswith("* "):
new_l = new_l.replace("* ", "[*]", 1)
if not bbcode == [] and not previous_new_l.startswith("[*]"):
bbcode[-1] = bbcode[-1] + "[list]" + "\n"
if lines.index(l) + 1 >= len(lines):
new_l += "\n[/list]"
else:
if previous_new_l.startswith("[*]") and not new_l.startswith("* "):
new_l = "[/list]\n" + new_l
# Quote
if new_l.startswith("> "):
new_l = new_l.replace("> ", "")
if not open_quote:
if not bbcode == [] and not previous_new_l.startswith("[quote]"):
bbcode[-1] = bbcode[-1] + "[quote]" + "\n"
open_quote = True
if open_quote and lines.index(l) + 1 >= len(lines):
new_l += "\n[/quote]"
else:
if open_quote:
new_l = "[/quote]\n" + new_l
open_quote = False
# Bold
if "**" in new_l:
while "**" in new_l:
if open_b:
new_l = new_l.replace("**", "[/b]", 1)
open_b = False
else:
new_l = new_l.replace("**", "[b]", 1)
open_b = True
# Cursive
if "*" in new_l:
while "*" in new_l and not (new_l.startswith("[*]") and new_l.count("*") <= 1):
if open_i:
new_l = new_l.replace("*", "[/i]", 1)
open_i = False
else:
new_l = new_l.replace("*", "[i]", 1)
open_i = True
if previous_new_l.endswith("[/h1]\n") or previous_new_l.endswith("[/h2]\n") or previous_new_l.endswith("[/h3]\n") or new_l.endswith("[/img]"):
pass
else:
new_l += "\n"
bbcode.append(new_l)
previous_new_l = new_l
bbcode_text = ""
for item in bbcode:
bbcode_text += item
return bbcode_text
def path_adjustments() -> str:
new_path = ""
current_path = os.path.dirname(__file__)
if current_path.endswith("\\Content"):
mod_name = current_path.split("\\")[-2]
elif current_path.endswith("\\modpack"):
mod_name = current_path.split("\\")[-2]
else:
mod_name = current_path.split("\\")[-1]
if MOD_NAME_OVERRIDE != "":
new_path = os.path.join(os.getenv('APPDATA'), MODS_DIR, MOD_NAME_OVERRIDE)
else:
new_path = os.path.join(os.getenv('APPDATA'), MODS_DIR, mod_name)
return new_path
def main():
mod_dir = path_adjustments()
os.chdir(mod_dir)
if DRY_RUN:
args = [SEWT, "push", "--dry-run", "--compile", "--mods", mod_dir]
else:
args = [SEWT, "push", "--compile", "--mods", mod_dir]
if len(EXCLUDE_FILES) > 0:
args.append("--exclude-ext")
for i in EXCLUDE_FILES:
args.append(i)
if len(EXCLUDE_DIRS) > 0:
args.append("--exclude-path")
for i in EXCLUDE_DIRS:
args.append(i)
if UPLOAD_DESC:
desc_src_path = os.path.join(mod_dir, "description.md")
if os.path.exists(desc_src_path):
args.append("--description")
args.append("description_tmp.md")
with open(desc_src_path, 'r') as f:
description_text = f.read()
f.close()
desc_path = os.path.join(mod_dir, "description_tmp.md")
desc_file = open(desc_path, "w")
desc_file.write(md_to_steam_bbcode(description_text))
desc_file.close()
if UPLOAD_PN:
pn_src_path = os.path.join(mod_dir, "patch_notes.md")
if os.path.exists(pn_src_path):
args.append("--message")
args.append("patch_notes_tmp.md")
with open(pn_src_path, 'r') as f:
patch_notes_text = f.read()
f.close()
pn_path = os.path.join(mod_dir, "patch_notes_tmp.md")
pn_file = open(pn_path, "w")
pn_file.write(md_to_steam_bbcode(patch_notes_text))
pn_file.close()
if len(TAGS) > 0:
args.append("--tags")
for tag in TAGS:
args.append(tag)
subprocess.call(args, cwd=mod_dir, shell=True)
if UPLOAD_DESC and os.path.exists(desc_path):
os.remove(desc_path)
if UPLOAD_PN and os.path.exists(pn_path):
os.remove(pn_path)
return
if __name__ == '__main__':
main()