-
Notifications
You must be signed in to change notification settings - Fork 8
/
template generator.py
348 lines (300 loc) · 15 KB
/
template generator.py
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import os
import subprocess
import re
import datetime
import shutil
from uuid import uuid4
EDITOR_EXE= r"F:/UNEPIC GAMES/UE_4.27/Engine/Binaries/Win64/UE4Editor-Cmd.exe"
UBT_EXE = r"F:/UNEPIC GAMES/UE_4.27/Engine/Binaries/DotNET/UnrealBuildTool.exe"
UAT_EXE = r"F:/UNEPIC GAMES/UE_4.27/Engine/Build/BatchFiles/RunUAT.bat"
PROJECT_GEN_UPROJECT = r"F:/DRG Modding/Project Generator/UE4GameProjectGenerator4.27/GameProjectGenerator.uproject"
HEADER_DUMP = r"C:/Program Files (x86)/Steam/steamapps/common/Deep Rock Galactic/FSD/Binaries/Win64/UHTHeaderDump"
PAK_FILE = r"C:/Program Files (x86)/Steam/steamapps/common/Deep Rock Galactic/FSD/Content/Paks/FSD-WindowsNoEditor.pak"
UNPACKED_FILES = r"F:/DRG Modding/DRGPacker"
PROJECT_FILE = r"F:/DRG Modding/DRGPacker/_unpacked/FSD/FSD.uproject"
PLUGIN_MANIFEST = r"F:/DRG Modding/DRGPacker/_unpacked/FSD/Plugins/FSD.upluginmanifest"
VERSION_CONFIG = r"F:/DRG Modding/DRGPacker/_unpacked/FSD/Config/DefaultGame.ini"
OUTPUT_DIR_START = r"F:/DRG Modding/Project Generator"
GITHUB_REPO = r"F:/Github Projects/Other/DRG Modding Org/FSD-Template"
TEST_COOK_OUTPUT = r"F:/DRG Modding/Project Generator/Output"
def check_drive_space():
_, _, free = shutil.disk_usage(os.path.splitdrive(OUTPUT_DIR_START)[0])
free = free // (2 ** 30)
if free < 25:
print("Not enough space on drive to run generator - needs at least 25GB")
return False
return True
def get_project_name():
uuid = str(uuid4()).replace("-", "")
uuid = re.sub(r"\d", "", uuid)
return uuid[:12]
def unpack_files():
print("============================================================")
print(" UNPACKING FILES ")
print("============================================================")
print("Deleting old unpacked files...")
os.system(
"rmdir " +
'"' +
os.path.join(UNPACKED_FILES, "_unpacked") +
'"' +
" /S /Q"
)
print("Unpacking files...")
subprocess.run([
os.path.join(UNPACKED_FILES, "repak.exe"),
"unpack",
os.path.join(PAK_FILE),
os.path.join(UNPACKED_FILES, "_unpacked")
])
def get_game_version():
print("============================================================")
print(" GET GAME VERSION ")
print("============================================================")
with open(VERSION_CONFIG, "r") as f:
for line in f:
if line.startswith("ProjectVersion="):
print("Game version is v" + line.split("=")[1].strip())
return "v" + line.split("=")[1].strip()
def run_project_gen(name):
print("============================================================")
print(" RUNNING PROJECT GENERATOR ")
print("============================================================")
os.mkdir(os.path.join(OUTPUT_DIR_START, name))
subprocess.run([
EDITOR_EXE,
PROJECT_GEN_UPROJECT,
"-run=ProjectGenerator",
"-HeaderRoot=" + HEADER_DUMP,
"-ProjectFile=" + PROJECT_FILE,
"-PluginManifest=" + PLUGIN_MANIFEST,
"-OutputDir=" + os.path.join(OUTPUT_DIR_START, name),
"-stdout",
"-unattended",
"-NoLogTimes"
])
def copy_modio_sources(name):
print("============================================================")
print(" COPYING MOD.IO SOURCE CODE ")
print("============================================================")
# Delete the folder called Modio inside of OutputDir/Plugins
os.system(
"rmdir " +
'"' +
os.path.join(OUTPUT_DIR_START, name, "Plugins", "Modio") +
'"' +
" /S /Q"
)
# Copy the new sources from Modio Source to template/Plugins/Modio
subprocess.run([
"xcopy",
os.path.join(OUTPUT_DIR_START, "Modio"),
os.path.join(OUTPUT_DIR_START, name, "Plugins", "Modio"),
"/E",
"/I",
"/Y"
])
def copy_config_files(name):
print("============================================================")
print(" COPYING CONFIG FILES ")
print("============================================================")
shutil.copytree(os.path.join(OUTPUT_DIR_START, "Config"), os.path.join(OUTPUT_DIR_START, name, "Config"))
def change_lines(file_name, line_num, text, is_replace, is_regex = False, match_group = ""):
if not os.path.exists(file_name): return
lines = open(file_name, 'r').readlines()
if is_regex:
for i, line in enumerate(lines):
if re.search(text, line):
lines[i] = re.sub(text, match_group, line)
print("Replaced line " + str(i) + " with " + match_group + " in " + file_name)
else:
if is_replace:
line_num -= 1
lines[line_num] = text
else: lines.insert(line_num, text)
print("Replaced line " + str(line_num) + " with " + text + " in " + file_name)
out = open(file_name, 'w')
out.writelines(lines)
out.close()
def modify_project_file(proj_name, file_name, line_number, is_replace, text, accessor = "Public", module = "FSD", is_regex = False, match_group = ""):
open_module = os.path.join(OUTPUT_DIR_START, proj_name, "Source", module)
if is_regex:
change_lines(file_name, line_number, text, is_replace, is_regex, match_group)
else:
change_lines(os.path.join(open_module, accessor, file_name), line_number, text, is_replace)
def run_rules(name):
print("============================================================")
print(" RUNNING THROUGH FIX RULES ")
print("============================================================")
# Re-add GameplayTasks and OnlineSubsystem to FSD.Build.cs
change_lines(os.path.join(OUTPUT_DIR_START, name, "Source", "FSD", "FSD.Build.cs"), 20, '\t\t\t"GameplayTasks",\n', False)
change_lines(os.path.join(OUTPUT_DIR_START, name, "Source", "FSD", "FSD.Build.cs"), 25, '\t\t\t"OnlineSubsystem",\n', False)
# CharacterSightSensor.h
modify_project_file(name, "CharacterSightSensor.h", 7, False, "DECLARE_DYNAMIC_MULTICAST_DELEGATE(FCharacterSightSensorDelegate);\n")
modify_project_file(name, "CharacterSightSensor.h", 8, False, "\n")
# FSDProjectileMovementComponent.h
modify_project_file(name, "FSDProjectileMovementComponent.h", 8, False, "DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnProjectilePenetrateDelegate);\n")
modify_project_file(name, "FSDProjectileMovementComponent.h", 9, False, "DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnProjectileOutOfPropulsion);\n")
modify_project_file(name, "FSDProjectileMovementComponent.h", 10, False, "\n")
# SubHealthComponent.h, line 60
modify_project_file(name, "SubHealthComponent.h", 60, True, "\t//UFUNCTION(BlueprintCallable)\n")
# HealthComponentBase.h, line 118
modify_project_file(name, "HealthComponentBase.h", 118, True, "\t//UFUNCTION(BlueprintCallable)\n")
# HealthComponent.h, line 102
modify_project_file(name, "HealthComponent.h", 102, True, "\t//UFUNCTION(BlueprintCallable)\n")
# EnemyHealthComponent.h, line 40
modify_project_file(name, "EnemyHealthComponent.h", 40, True, "\t//UFUNCTION(BlueprintCallable)\n")
# FriendlyHealthComponent.h, line 34
modify_project_file(name, "FriendlyHealthComponent.h", 34, True, "\t//UFUNCTION(BlueprintCallable)\n")
# GameFunctionLibrary.cpp, line 26
modify_project_file(name, "GameFunctionLibrary.cpp", 26, True, '\treturn true;\n', "Private")
# Fix Collision component setting in FriendlyParasite.cpp, lines 6 and 7
modify_project_file(name, "FriendlyParasite.cpp", 6, True, "AFriendlyParasite::AFriendlyParasite(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {\n")
modify_project_file(name, "FriendlyParasite.cpp", 7, True, "\tthis->Collision = CreateDefaultSubobject<USphereComponent>(TEXT(\"Collision\"));\n")
# Search every file in both modules and accessors for the following regex string: (const) ((\w+)\*\&) and replace it with $2
# UE does not like const reference pointers in UFUNCTIONs
for root, _, files in os.walk(os.path.join(OUTPUT_DIR_START, name, "Source")):
for file in files:
if file.endswith(".h") or file.endswith(".cpp"):
modify_project_file(name, os.path.join(root, file), 0, False, "(const) ((\w+)\*\&)", "", "", True, r"\2")
def generate_build_files(name):
print("============================================================")
print(" GENERATING BUILD FILES ")
print("============================================================")
date = datetime.datetime.now().strftime("%Y.%m.%d-%H.%M.%S")
subprocess.run([
UBT_EXE,
"-projectfiles",
"-project=" + os.path.join(OUTPUT_DIR_START, name, "FSD.uproject"),
"-game",
"-rocket",
"-progress",
"-log=" + os.path.join(OUTPUT_DIR_START, name, "Saved/Logs/UnrealVersionSelector-" + str(date) + ".log")
])
def compile_project(name):
print("============================================================")
print(" COMPILING PROJECT ")
print("============================================================")
subprocess.run([
UBT_EXE,
"Development",
"Win64",
"-Project=" + os.path.join(OUTPUT_DIR_START, name, "FSD.uproject"),
"-TargetType=Editor",
"-Progress",
"-NoEngineChanges",
"-NoHotReloadFromIDE"
])
def test_cook_project(name):
print("============================================================")
print(" COOKING PROJECT ")
print("============================================================")
subprocess.run([
UAT_EXE,
"BuildCookRun",
"-nocompileeditor",
"-installed",
"-nop4",
"-project=" + os.path.join(OUTPUT_DIR_START, name, "FSD.uproject"),
"-cook",
"-stage",
"-archive",
"-archivedirectory=" + TEST_COOK_OUTPUT,
"-package",
"-ue4exe=" + EDITOR_EXE,
"-ddc=InstalledDerivedDataBackendGraph",
"-prereqs",
"-nodebuginfo",
"-targetplatform=Win64",
"-build",
"-target=FSDGame",
"-clientconfig=Shipping",
"-utf8output",
"-unattended"
])
def copy_template_to_repo(name):
print("============================================================")
print(" COPYING TEMPLATE TO GITHUB REPO ")
print("============================================================")
# We are deleting the files first otherwise we will keep old files that the devs deleted
files_to_delete = ["Binaries", "Plugins", "Source", "Saved", "Intermediate", "Config", "FSD.sln", "FSD.uproject"]
for file in files_to_delete:
if os.path.exists(os.path.join(GITHUB_REPO, file)):
if os.path.isfile(os.path.join(GITHUB_REPO, file)): os.remove(os.path.join(GITHUB_REPO, file))
else: shutil.rmtree(os.path.join(GITHUB_REPO, file))
print("Deleted " + file)
files_to_copy = ["Binaries", "Plugins", "Source", "Config", "FSD.sln", "FSD.uproject"]
for file in files_to_copy:
file_path = os.path.join(OUTPUT_DIR_START, name, file)
if os.path.exists(file_path):
if os.path.isfile(file_path): shutil.copyfile(file_path, os.path.join(GITHUB_REPO, file))
else: shutil.copytree(file_path, os.path.join(GITHUB_REPO, file))
print("Copied " + file)
def check_tag_name(tag):
print("============================================================")
print(" CHECK TAG NAME ")
print("============================================================")
# If the tag is the same as the latest tag, then add -patch1 to the tag
# If the tag is the same as the latest tag + -patch1, then increment the number
latest_tag = subprocess.run(["git", "describe", "--tags", "--abbrev=0"], cwd=GITHUB_REPO, stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
if latest_tag == tag: tag += "-patch1"
elif latest_tag == tag + "-patch1": tag = tag + "-patch" + str(int(latest_tag.split("-patch")[1]) + 1)
print(tag)
return tag
def git_commit(commit_message, commit_tag, tag_message):
print("============================================================")
print(" GIT COMMIT ")
print("============================================================")
subprocess.run(["git", "add", "."], cwd=GITHUB_REPO)
subprocess.run(["git", "commit", "-m", commit_message], cwd=GITHUB_REPO)
subprocess.run(["git", "tag", "-a", commit_tag, "HEAD", "-m", tag_message], cwd=GITHUB_REPO)
def git_diff():
print("============================================================")
print(" GIT DIFF ")
print("============================================================")
subprocess.run(["git", "diff", "HEAD~1", "HEAD", "-U0", "--", ":!*.dll", ":!Binaries/"], cwd=GITHUB_REPO, stdout=open(os.path.join(GITHUB_REPO, "changes.diff"), "w"))
print("Changes saved to changes.diff")
def git_push(commit_tag):
print("============================================================")
print(" GIT PUSH ")
print("============================================================")
subprocess.run(["git", "push", "origin"], cwd=GITHUB_REPO)
subprocess.run(["git", "push", "origin", commit_tag], cwd=GITHUB_REPO)
def wait(stage):
input("Press enter to continue to next stage: [" + stage + "] ")
def main():
if not check_drive_space(): return
name = get_project_name()
print("Project name: " + name)
if input("Refresh unpacked files? [y/n] ") == 'y': unpack_files()
run_project_gen(name)
copy_modio_sources(name)
copy_config_files(name)
run_rules(name)
wait("generate build files")
generate_build_files(name)
compile_project(name)
wait("cook project")
test_cook_project(name)
wait("copy template to repo")
copy_template_to_repo(name)
version = get_game_version()
version = check_tag_name(version)
tag_message = input("Primary game version (e.g. U37P11): ")
semver = input("PATCH, MINOR or MAJOR version?").upper() + " - " + tag_message
commit_message = semver + " - " + input("Commit message (e.g. UE4SS update): ")
wait("git commit - REMEMBER TO BE LOGGED INTO THE RIGHT GITHUB ACCOUNT ON GITKRAKEN YOU ABSOLUTE BABOON")
git_commit(commit_message, version, tag_message)
wait("git push")
git_push(version)
def ue4ss_test(): # just for testing if ue4ss changes still allows the project to compile
if not check_drive_space(): return
name = get_project_name()
run_project_gen(name)
copy_modio_sources(name)
run_rules(name)
generate_build_files(name)
compile_project(name)
if __name__ == "__main__":
main()
#ue4ss_test()