forked from Steamodded/smods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamoddedNX_injector.py
225 lines (180 loc) · 7.94 KB
/
steamoddedNX_injector.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
import subprocess
import os
import sys
import tempfile
import zipfile
import shutil
import platform
import requests
def download_file(url, output_path):
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
return True
return False
# def decompile_lua(decompiler_path, lua_path, output_dir):
# subprocess.run([decompiler_path, lua_path, '--output', output_dir])
def merge_directory_contents(directory_path):
directory_content = ""
core_file_name = "core.lua"
if os.path.exists(directory_path):
print(f"Processing directory: {directory_path}")
# Process core.lua first if it exists
core_file_path = os.path.join(directory_path, core_file_name)
if os.path.isfile(core_file_path):
try:
with open(core_file_path, "r", encoding="utf-8") as file:
directory_content += file.read() + "\n" # Append the core file content first
print(f"Appended {core_file_name} to the directory content")
except IOError as e:
print(f"Error reading {core_file_path}: {e}")
# Process the rest of the .lua files
for file_name in os.listdir(directory_path):
if file_name.endswith(".lua") and file_name != core_file_name: # Skip the core.lua file
file_path = os.path.join(directory_path, file_name)
try:
with open(file_path, "r", encoding="utf-8") as file:
file_content = file.read()
directory_content += "\n" + file_content
print(f"Appended {file_name} to the directory content")
except IOError as e:
print(f"Error reading {file_path}: {e}")
else:
print(f"Directory not found: {directory_path}")
return directory_content
def modify_main_lua(main_lua_path, base_dir, directories):
print(f"Modifying {main_lua_path} with files from {directories} in {base_dir}")
try:
with open(main_lua_path, "r", encoding="utf-8") as file:
main_lua_content = file.read()
except IOError as e:
print(f"Error reading {main_lua_path}: {e}")
return
for directory in directories:
directory_path = os.path.join(base_dir, directory)
print(f"Looking for directory: {directory_path}") # Debug print
directory_content = merge_directory_contents(directory_path)
main_lua_content += "\n" + directory_content
try:
with open(main_lua_path, "w", encoding="utf-8") as file:
file.write(main_lua_content)
except IOError as e:
print(f"Error writing to {main_lua_path}: {e}")
def modify_game_lua(game_lua_path):
try:
with open(game_lua_path, "r", encoding="utf-8") as file:
lines = file.readlines()
target_line = " self.SPEEDFACTOR = 1\n"
insert_line = " initSteamodded()\n" # Ensure proper indentation
target_index = None
for i, line in enumerate(lines):
if target_line in line:
target_index = i
break # Find the first occurrence and stop
if target_index is not None:
print("Target line found. Inserting new line.")
lines.insert(target_index + 1, insert_line)
with open(game_lua_path, "w", encoding="utf-8") as file:
file.writelines(lines)
print("Successfully modified game.lua.")
else:
print("Target line not found in game.lua.")
except IOError as e:
print(f"Error modifying game.lua: {e}")
print("Starting the process...")
# URL to download the LuaJIT decompiler
# luajit_decompiler_url = ""
# Temporary directory for operations
with tempfile.TemporaryDirectory() as decompiler_dir:
# This part was used to download the LuaJit decompiler
# luajit_decompiler_path = os.path.join(decompiler_dir, 'luajit-decompiler-v2.exe')
# # Download LuaJIT decompiler
# if not download_file(luajit_decompiler_url, luajit_decompiler_path):
# print("Failed to download LuaJIT decompiler.")
# sys.exit(1)
# print("LuaJIT Decompiler downloaded.")
# URL to download the 7-Zip suite
seven_zip_url = "https://github.com/JonJaded/SteamoddedNX/raw/main/7-zip/7z.zip"
# Temporary directory for 7-Zip suite
seven_zip_dir = tempfile.TemporaryDirectory()
print(seven_zip_dir.name)
print("Downloading and extracting 7-Zip suite...")
download_file(seven_zip_url, os.path.join(seven_zip_dir.name, "7z.zip"))
with zipfile.ZipFile(
os.path.join(seven_zip_dir.name, "7z.zip"), "r"
) as zip_ref:
zip_ref.extractall(seven_zip_dir.name)
# Check the operating system
#if os.name() == 'Linux':
# seven_zip_path = ['wine', os.path.join(seven_zip_dir.name, "7z.exe")]
#elif os.name == 'nt':
# seven_zip_path = os.path.join(seven_zip_dir.name, "7z.exe")
#else:
# # Handle other operating systems or raise an error
# raise NotImplementedError("This script only supports Windows and Linux.")
seven_zip_path = f"{seven_zip_dir.name}/7z.exe"
# Determine the operating system and prepare the 7-Zip command accordingly
if os.name == 'posix':
if platform.system() == 'Darwin':
# This is macOS
command = "7zz" # Update this path as necessary for macOS
else:
# This is Linux or another POSIX-compliant OS
command = "7zz"
else:
# This is for Windows
command = f"{seven_zip_dir.name}/7z.exe"
#command = seven_zip_dir + ["x", "-o" + temp_dir.name, sfx_archive_path]
# seven_zip_path = os.path.join(seven_zip_dir.name, "7z.exe")
# Check if the SFX archive path is provided
if len(sys.argv) < 2:
print("Please drag and drop the SFX archive onto this executable.")
seven_zip_dir.cleanup()
sys.exit(1)
sfx_archive_path = sys.argv[1]
print(f"SFX Archive received: {sfx_archive_path}")
# Temporary directory for extraction and modification
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)
# Extract the SFX archive
#subprocess.run([command, "x", "-o" + temp_dir.name, sfx_archive_path])
subprocess.run([command, "x", f"-o{temp_dir.name}", sfx_archive_path], check=True)
print("Extraction complete.")
# Path to main.lua and game.lua within the extracted files
main_lua_path = os.path.join(temp_dir.name, "main.lua")
game_lua_path = os.path.join(temp_dir.name, "game.lua")
decompile_output_path = os.path.join(temp_dir.name, "output")
os.makedirs(decompile_output_path, exist_ok=True) # Create the output directory
# This part was used to decompile to game data
# No longer needed
# decompile_lua(luajit_decompiler_path, main_lua_path, decompile_output_path)
# print("Decompilation of main.lua complete.")
main_lua_output_path = os.path.join(temp_dir.name, "main.lua")
# Determine the base directory (where the .exe is located)
if getattr(sys, "frozen", False):
# Running in a PyInstaller or Nuitka bundle
base_dir = os.path.dirname(sys.executable)
else:
# Running in a normal Python environment
base_dir = os.path.dirname(os.path.abspath(__file__))
# Modify main.lua
directories = ["core", "debug", "loader"]
modify_main_lua(main_lua_output_path, base_dir, directories)
print("Modification of main.lua complete.")
# Modify main.lua
modify_game_lua(game_lua_path)
print("Modification of game.lua complete.")
# Update the SFX archive with the modified main.lua
#subprocess.run([command, "a", sfx_archive_path, main_lua_output_path])
subprocess.run([command, "a", sfx_archive_path, main_lua_output_path], check=True)
# Update the SFX archive with the modified game.lua
#subprocess.run([command, "a", sfx_archive_path, game_lua_path])
subprocess.run([command, "a", sfx_archive_path, game_lua_path], check=True)
print("SFX Archive updated.")
seven_zip_dir.cleanup()
temp_dir.cleanup()
print("Process completed successfully.")
print("Press any key to exit...")
input()