4040
4141# Directory containing all Lua game files
4242GAMES_DIR = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'games' )
43+ GAMES_ZIP = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'games.zip' )
4344# Directory containing game fix zip files
4445FIX_FILES_DIR = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'game-fix-files' )
4546
47+ def get_lua_content (app_id ):
48+ """
49+ Unified helper to get Lua content from either:
50+ 1. The 'games/' folder (direct file)
51+ 2. The 'games.zip' archive (compressed)
52+ Returns (content_bytes, filename) or (None, None)
53+ """
54+ filename = f"{ app_id } .lua"
55+
56+ # 1. Check direct file system first
57+ file_path = os .path .join (GAMES_DIR , filename )
58+ if os .path .exists (file_path ):
59+ with open (file_path , 'rb' ) as f :
60+ return f .read (), filename
61+
62+ # 2. Check games.zip if it exists
63+ if os .path .exists (GAMES_ZIP ):
64+ try :
65+ with zipfile .ZipFile (GAMES_ZIP , 'r' ) as zf :
66+ if filename in zf .namelist ():
67+ with zf .open (filename ) as f :
68+ return f .read (), filename
69+ except Exception as e :
70+ print (f"Error reading { GAMES_ZIP } : { e } " )
71+
72+ return None , None
73+
4674def require_token (f ):
4775 @wraps (f )
4876 def decorated_function (* args , ** kwargs ):
@@ -194,14 +222,14 @@ def admin_panel():
194222@require_token
195223def serve_lua (filename ):
196224 """Serve a specific Lua file by filename (e.g., 730.lua)"""
197- if not filename . endswith ( '.lua' ):
198- filename = f" { filename } . lua"
225+ # Extract app_id from filename
226+ app_id = filename . replace ( '. lua' , '' )
199227
200- file_path = os . path . join ( GAMES_DIR , filename )
201- if not os . path . exists ( file_path ) :
202- abort (404 , description = f"Lua file '{ filename } ' not found" )
228+ content , actual_filename = get_lua_content ( app_id )
229+ if not content :
230+ abort (404 , description = f"Lua file '{ filename } ' not found in folder or ZIP " )
203231
204- return send_from_directory ( GAMES_DIR , filename , mimetype = 'text/plain' )
232+ return Response ( content , mimetype = 'text/plain' )
205233
206234
207235@app .route ('/fix/<filename>' )
@@ -242,17 +270,15 @@ def free_download():
242270 import re
243271 app_id = re .sub (r'[^0-9]' , '' , str (app_id ))
244272
245- filename = f"{ app_id } .lua"
246- file_path = os .path .join (GAMES_DIR , filename )
247-
248- if not os .path .exists (file_path ):
249- return jsonify ({'error' : f'Game patch { app_id } not found' }), 404
273+ content , filename = get_lua_content (app_id )
274+ if not content :
275+ return jsonify ({'error' : f'Game patch { app_id } not found in store' }), 404
250276
251277 try :
252- # Create ZIP in memory
278+ # Create ZIP in memory for the single requested file
253279 memory_file = io .BytesIO ()
254280 with zipfile .ZipFile (memory_file , 'w' , zipfile .ZIP_DEFLATED ) as zf :
255- zf .write ( file_path , arcname = filename )
281+ zf .writestr ( filename , content )
256282
257283 memory_file .seek (0 )
258284
@@ -272,11 +298,10 @@ def free_download():
272298@require_token
273299def check_availability (app_id ):
274300 """Check if a specific app ID has a Lua file available"""
275- file_path = os .path .join (GAMES_DIR , f"{ app_id } .lua" )
276- exists = os .path .exists (file_path )
301+ content , _ = get_lua_content (app_id )
277302 return jsonify ({
278303 'app_id' : app_id ,
279- 'available' : exists
304+ 'available' : content is not None
280305 })
281306
282307
0 commit comments