Browse Source

change `BuildModFiles.py`to allow for various mods

Stephan Fuchs 1 month ago
parent
commit
85e9292241
1 changed files with 33 additions and 16 deletions
  1. 33 16
      mods/util/BuildModFiles.py

+ 33 - 16
mods/util/BuildModFiles.py

@@ -8,12 +8,12 @@ import sys
 modTag = "mod"
 
 file_path_html = 'glife.html'
-file_path_mod = 'mod.html'
-file_path_mod_online = 'mod.php'
+file_path_mod = 'mods'
+file_path_mod_online = 'mods'
 
 data = ''
-modData = ''
-scripts = ''
+modData = {}
+scripts = {}
 dataWithoutModStuff = ''
 
 with open(file_path_html, 'r') as file:
@@ -27,27 +27,44 @@ for match in re.finditer(regex,data,re.S):
     tags = match.group(3).split()
 
     if(modTag in tags):
-        modData += match.group(0)+'\n'
+        modId = 'default'
+        for tag in tags:
+            if tag.startswith('mod_'):
+                modId = tag[4:]
+        if modId not in modData:
+            modData[modId] = ''    
+        modData[modId] += match.group(0)+'\n'
         dataWithoutModStuff = dataWithoutModStuff.replace(match.group(0),'')
 
     i += 1
     if(i % 100 == 0):
         print(i)
 
-regex = r"""/\*\s*Start Mod:\s*"Mod"\s*\*/(.*?)/\*\s*End Mod:\s*"Mod"\s*\*/"""
+regex = r"""/\*\s*Start Mod:\s*"([^"]*)"\s*\*/(.*?)/\*\s*End Mod:\s*"Mod"\s*\*/"""
 for match in re.finditer(regex,data,re.S):
-    scripts += match.group(1)
+    modId = match.group(1).strip()
+    if not modId:
+        modId = 'default'
+    if modId not in scripts:
+        scripts[modId] = ''    
+    scripts[modId] += match.group(2)
     dataWithoutModStuff = dataWithoutModStuff.replace(match.group(0),'')
 
-modData += "\n.-.-.-. STORYDATA / SCRIPT .-.-.-.\n"
-
-modData += scripts
-
-with open(file_path_mod, 'w') as file:
-    file.write(modData)
-
-with open(file_path_mod_online, 'w') as file:
-    file.write(f'<?php header("Access-Control-Allow-Origin: *"); header(\'Content-type: text/javascript\'); echo($_GET[\'callback\']);?>(`{modData}\n`);')
+for modId, scriptContent in scripts.items():
+    if modId not in modData:
+        modData[modId] = ''  
+    modData[modId] += "\n.-.-.-. STORYDATA / SCRIPT .-.-.-.\n"
+    modData[modId] += scriptContent
+
+for modId, modContent in modData.items():
+    modPath = os.path.join(file_path_mod,modId+'.html')
+    with open(modPath, 'w') as file:
+        file.write(modContent)
+
+for modId, modContent in modData.items():
+    modPath = os.path.join(file_path_mod_online,modId+'.php')
+    with open(modPath, 'w') as file:
+        file.write(f'<?php header("Access-Control-Allow-Origin: *"); header(\'Content-type: text/javascript\'); echo($_GET[\'callback\']);?>(`{modContent}\n`);')
 
 with open(file_path_html, 'w') as file:
     file.write(dataWithoutModStuff)