1
+ import json
2
+ import os
3
+ import sys
4
+ import subprocess
5
+ from pathlib import Path
6
+
7
+
8
+ def load_config (config_path = "config/base_config.json" ):
9
+ """Load the engine configuration file."""
10
+ try :
11
+ with open (config_path , 'r' ) as f :
12
+ return json .load (f )
13
+ except FileNotFoundError :
14
+ print (f"Error: Configuration file not found at { config_path } " )
15
+ sys .exit (1 )
16
+
17
+
18
+ def find_uproject_file (solution_path ):
19
+ """Find the .uproject file in the solution directory."""
20
+ solution_dir = Path (solution_path )
21
+ if solution_dir .is_file ():
22
+ solution_dir = solution_dir .parent
23
+
24
+ uproject_files = list (solution_dir .glob ("*.uproject" ))
25
+
26
+ if not uproject_files :
27
+ print (f"Error: No .uproject file found in { solution_dir } " )
28
+ sys .exit (1 )
29
+
30
+ if len (uproject_files ) > 1 :
31
+ print (f"Warning: Multiple .uproject files found. Using { uproject_files [0 ]} " )
32
+
33
+ return uproject_files [0 ]
34
+
35
+
36
+ def get_engine_version (uproject_path ):
37
+ """Extract engine version from .uproject file."""
38
+ try :
39
+ with open (uproject_path , 'r' ) as f :
40
+ project_data = json .load (f )
41
+ return project_data .get ("EngineAssociation" )
42
+ except (FileNotFoundError , json .JSONDecodeError , KeyError ) as e :
43
+ print (f"Error reading engine version from { uproject_path } : { e } " )
44
+ sys .exit (1 )
45
+
46
+
47
+ def get_build_bat_path (engine_base_path ):
48
+ """Construct path to Build.bat."""
49
+ engine_root = Path (engine_base_path ).parent
50
+ build_bat_path = engine_root / "Build" / "BatchFiles" / "Build.bat"
51
+
52
+ print (f"Looking for Build.bat at: { build_bat_path } " )
53
+
54
+ if not build_bat_path .exists ():
55
+ print (f"Error: Build.bat not found at { build_bat_path } " )
56
+ sys .exit (1 )
57
+
58
+ return build_bat_path
59
+
60
+
61
+ def generate_project_files (solution_path ):
62
+ """Main function to generate project files."""
63
+ # Load config
64
+ config = load_config ()
65
+
66
+ # Find .uproject file
67
+ uproject_path = find_uproject_file (solution_path )
68
+ print (f"Found uproject at: { uproject_path } " )
69
+
70
+ # Get engine version
71
+ engine_version = get_engine_version (uproject_path )
72
+ print (f"Engine version: { engine_version } " )
73
+
74
+ # Get engine path from config
75
+ if engine_version not in config ["engine_path" ]:
76
+ print (f"Error: Engine version { engine_version } not found in config" )
77
+ sys .exit (1 )
78
+
79
+ engine_path = config ["engine_path" ][engine_version ]
80
+
81
+ # Get Build.bat path
82
+ build_bat_path = get_build_bat_path (engine_path )
83
+
84
+ # Construct command
85
+ cmd = [
86
+ str (build_bat_path ),
87
+ "-projectfiles" ,
88
+ f"-project={ uproject_path } " ,
89
+ "-game" ,
90
+ "-rocket" ,
91
+ "-progress"
92
+ ]
93
+
94
+ print (f"Executing: { ' ' .join (cmd )} " )
95
+
96
+ try :
97
+ # Start process with pipe for stdout and stderr
98
+ process = subprocess .Popen (
99
+ cmd ,
100
+ stdout = subprocess .PIPE ,
101
+ stderr = subprocess .PIPE ,
102
+ text = True ,
103
+ bufsize = 1 , # Line buffered
104
+ universal_newlines = True
105
+ )
106
+
107
+ # Read output in real-time
108
+ while True :
109
+ output = process .stdout .readline ()
110
+ if output == '' and process .poll () is not None :
111
+ break
112
+ if output :
113
+ print (output .rstrip ())
114
+
115
+ # Get the return code
116
+ return_code = process .poll ()
117
+
118
+ # Print any remaining stderr
119
+ for line in process .stderr :
120
+ print (line .rstrip (), file = sys .stderr )
121
+
122
+ if return_code != 0 :
123
+ print (f"Process exited with code { return_code } " )
124
+ sys .exit (return_code )
125
+
126
+ except subprocess .CalledProcessError as e :
127
+ print (f"Error generating project files: { e } " )
128
+ print (e .stderr )
129
+ sys .exit (1 )
130
+ except KeyboardInterrupt :
131
+ print ("\n Process interrupted by user" )
132
+ process .kill ()
133
+ sys .exit (1 )
134
+
135
+
136
+ if __name__ == "__main__" :
137
+ if len (sys .argv ) != 2 :
138
+ print ("Usage: python generate_project_files.py <solution_path>" )
139
+ sys .exit (1 )
140
+
141
+ generate_project_files (sys .argv [1 ])
0 commit comments