1
1
import os
2
2
import zipfile
3
3
from enum import StrEnum
4
+ from typing import Optional
4
5
5
6
6
7
class IntermediaryType (StrEnum ):
@@ -11,59 +12,38 @@ class IntermediaryType(StrEnum):
11
12
Ornithe = "net.fabricmc.intermediary.ornithe.pre-1.6.json"
12
13
13
14
14
- def mkdir_if_not_exists (path : str ):
15
- if not os .path .exists (path ):
16
- os .mkdir (path )
15
+ def mkdirs (* paths : str ):
16
+ for path in paths :
17
+ if not os .path .exists (path ):
18
+ os .mkdir (path )
17
19
18
20
19
21
class Generator :
20
- def __init__ (self , loader_version : str , minecraft_version : str , lwjgl_version : str ,
21
- intermediary_type : IntermediaryType , path : str = "temp" ):
22
- self .lwjgl_version : str = lwjgl_version
23
- self .minecraft_version : str = minecraft_version
24
- self .loader_version : str = loader_version
25
- self .intermediary_type : IntermediaryType = intermediary_type
26
- self .path : str = path
27
- self .minecraft_version_additions = ""
22
+ def __init__ (self , minecraft_version : str , lwjgl_version : str , intermediary_type : IntermediaryType , path : str = "temp" ):
23
+ self .lwjgl_version = lwjgl_version
24
+ self .minecraft_version = minecraft_version
25
+ self .intermediary_type = intermediary_type
26
+ self .path = path
27
+ self .minecraft_version_additions = self .fix_version (version )
28
28
29
29
def process (self , subject : str ) -> str :
30
- subject = subject .replace ("${loader_version}" , self . loader_version )
30
+ subject = subject .replace ("${loader_version}" , LOADER_VERSION )
31
31
subject = subject .replace ("${minecraft_version}" , self .minecraft_version + self .minecraft_version_additions )
32
32
subject = subject .replace ("${lwjgl_version}" , self .lwjgl_version )
33
33
subject = subject .replace ("${lwjgl_name}" , "LWJGL 3" if self .lwjgl_version .startswith ("3" ) else "LWJGL 2" )
34
34
subject = subject .replace ("${lwjgl_uid}" , "org.lwjgl3" if self .lwjgl_version .startswith ("3" ) else "org.lwjgl" )
35
35
return subject
36
36
37
37
def prepare_skeleton (self ):
38
- mkdir_if_not_exists ("temp" )
39
-
40
- with open ("skel/mmc-pack.json" , "r" ) as f :
41
- with open ("temp/mmc-pack.json" , "w" ) as t :
42
- t .write (self .process (f .read ()))
43
-
44
- with open ("skel/instance.cfg" , "r" ) as f :
45
- with open ("temp/instance.cfg" , "w" ) as t :
46
- t .write (self .process (f .read ()))
47
-
48
- # ornithe naming convention
49
- if self .minecraft_version == "1.0" :
50
- self .minecraft_version_additions += ".0"
51
- if int (self .minecraft_version .split ("." )[1 ]) < 3 :
52
- self .minecraft_version_additions += "-client"
53
-
54
- mkdir_if_not_exists ("temp/patches" )
55
- with open (f"skel/patches/{ self .intermediary_type } " , "r" ) as f :
56
- with open ("temp/patches/net.fabricmc.intermediary.json" , "w" ) as t :
57
- t .write (self .process (f .read ()))
38
+ mkdirs ("temp" , "temp/patches" )
39
+ self .process_file ("mmc-pack.json" , "instance.cfg" )
40
+ self .process_file (f"patches/{ self .intermediary_type } " , out = "patches/net.fabricmc.intermediary.json" )
58
41
59
42
def create_zip (self ):
60
- with zipfile .ZipFile (
61
- f"out/{ self .minecraft_version } +loader.{ self .loader_version } .zip" , "w" ) as z :
43
+ with zipfile .ZipFile (f"out/{ self .minecraft_version } +loader.{ LOADER_VERSION } .zip" , "w" ) as z :
62
44
z .write ("temp/mmc-pack.json" , "mmc-pack.json" )
63
45
z .write ("temp/instance.cfg" , "instance.cfg" )
64
- z .write ("temp/patches/net.fabricmc.intermediary.json" ,
65
- "patches/net.fabricmc.intermediary.json" )
66
-
46
+ z .write ("temp/patches/net.fabricmc.intermediary.json" , "patches/net.fabricmc.intermediary.json" )
67
47
self .cleanup ()
68
48
69
49
def cleanup (self ):
@@ -72,9 +52,24 @@ def cleanup(self):
72
52
os .remove (os .path .join (root , file ))
73
53
for directory in dirs :
74
54
os .rmdir (os .path .join (root , directory ))
75
-
76
55
os .rmdir (self .path )
77
56
57
+ def process_file (self , * files : str , out : Optional [str ] = None ):
58
+ for file in files :
59
+ with open (f"skel/{ file } " , "r" ) as f :
60
+ with open (f"temp/{ out if out is not None else file } " , "w" ) as t :
61
+ t .write (self .process (f .read ()))
62
+
63
+ @staticmethod
64
+ def fix_version (version : str ) -> str :
65
+ # accounts for the ornithe naming convention
66
+ addition = ""
67
+ if version == "1.0" :
68
+ addition += ".0"
69
+ if int (version .split ("." )[1 ]) < 3 :
70
+ addition += "-client"
71
+ return addition
72
+
78
73
79
74
versions = [
80
75
("1.13.2" , "3.1.6" , IntermediaryType .LegacyFabric ),
@@ -93,11 +88,11 @@ def cleanup(self):
93
88
("1.0" , "2.9.0" , IntermediaryType .Ornithe )
94
89
]
95
90
96
- loader = "0.14.24 "
97
- mkdir_if_not_exists ("out" )
91
+ LOADER_VERSION = "0.15.0 "
92
+ mkdirs ("out" )
98
93
99
94
for version , lwjgl , intermediary in versions :
100
95
print (f"generating { version } with LWJGL { lwjgl } ..." )
101
- g = Generator (loader , version , lwjgl , intermediary )
96
+ g = Generator (version , lwjgl , intermediary )
102
97
g .prepare_skeleton ()
103
98
g .create_zip ()
0 commit comments