11package cam72cam .universalmodcore ;
22
3+ import com .google .gson .JsonObject ;
4+ import org .apache .commons .io .FileUtils ;
35import org .apache .commons .io .IOUtils ;
4- import org .gradle . api .Project ;
6+ import org .eclipse . jgit . api .errors . GitAPIException ;
57
68import java .io .*;
79import java .net .URL ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Paths ;
812import java .util .*;
13+ import java .util .stream .Collectors ;
914
1015public class Config {
11- public String modPackage ;
12- public String modClass ;
13- public String modName ;
14- public String modId ;
15- public String modVersion ;
16+ public final Mod mod ;
17+ public final Integration integration ;
18+ public final UMC umc ;
19+ private final Map <String , String > vars = new HashMap <>();
20+ public final String minecraftLoader ;
21+
22+ public static class Mod {
23+ public final String pkg ;
24+ public final String cls ;
25+ public final String name ;
26+ public final String id ;
27+ public final String version ;
28+ public final List <Dependency > dependencies ;
29+
30+ public static class Dependency {
31+ public final String id ;
32+ public final String versions ;
33+ public Dependency (String id , JsonObject data ) {
34+ this .id = id ;
35+ this .versions = data .get ("versions" ).getAsString ();
36+ }
37+ public Dependency (String id , String versions ) {
38+ this .id = id ;
39+ this .versions = versions ;
40+ }
41+ }
42+
43+ public Mod (JsonObject data ) {
44+ this .pkg = data .get ("pkg" ).getAsString ();
45+ this .cls = data .get ("cls" ).getAsString ();
46+ this .name = data .get ("name" ).getAsString ();
47+ this .id = data .get ("id" ).getAsString ();
48+ this .version = data .get ("version" ).getAsString ();
49+ this .dependencies = data .get ("dependencies" ).getAsJsonObject ().entrySet ().stream ()
50+ .map ((e ) -> new Dependency (e .getKey (), e .getValue ().getAsJsonObject ()))
51+ .collect (Collectors .toList ());
52+ }
53+ }
1654
17- public String umcVersion ;
18- public String umcPath ;
55+ public static class Integration {
56+ public final String repo ;
57+ public final String branch ;
58+ public final String path ;
1959
20- public Config (Project project ) {
21- // What to do here?
60+ public Integration (JsonObject data ) {
61+ this .repo = data .get ("repo" ).getAsString ();
62+ this .branch = data .get ("branch" ).getAsString ();
63+ this .path = data .get ("path" ).getAsString ();
64+ }
2265 }
2366
24- private final Map <String , String > vars = new HashMap <>();
67+ public static class UMC {
68+ public final String version ;
69+ public final String path ;
2570
26- public void init () {
27- vars .clear ();
28- vars .put ("PACKAGE" , require ("modPackage" , modPackage ));
29- vars .put ("PACKAGEPATH" , require ("modPackage" , modPackage ).replace ("." , File .separator ));
30- vars .put ("CLASS" , require ("modClass" , modClass ));
31- vars .put ("NAME" , require ("modName" , modName ));
32- vars .put ("ID" , require ("modId" , modId ));
33- vars .put ("VERSION" , require ("modVersion" , modVersion ));
34-
35- String loaderVersion = System .getProperty ("umc.loader" );
36- if (loaderVersion == null || loaderVersion .length () == 0 ) {
37- throw new RuntimeException ("Missing command argument: -D umc.loader=<loader>_<version>" );
71+ public UMC (JsonObject data ) {
72+ this .version = data .get ("version" ).getAsString ();
73+ this .path = data .has ("path" ) ? data .get ("path" ).getAsString () : null ;
3874 }
75+ }
3976
40- vars .put ("LOADER_VERSION" , require ("loaderVersion" , loaderVersion ));
77+ public Config (JsonObject data , String loaderBranch ) throws GitAPIException , IOException {
78+ mod = new Mod (data .get ("mod" ).getAsJsonObject ());
79+ integration = data .has ("integration" ) ? new Integration (data .get ("integration" ).getAsJsonObject ()) : null ;
80+ umc = new UMC (data .get ("umc" ).getAsJsonObject ());
81+ String minecraft = loaderBranch .split ("-" )[0 ];
82+ String loader = loaderBranch .split ("-" )[1 ];
83+ this .minecraftLoader = minecraft + "-" + loader ;
84+
85+ vars .clear ();
86+ vars .put ("PACKAGE" , require ("mod.package" , mod .pkg ));
87+ vars .put ("PACKAGEPATH" , require ("mod.package" , mod .pkg ).replace ("." , File .separator ));
88+ vars .put ("CLASS" , require ("mod.class" , mod .cls ));
89+ vars .put ("NAME" , require ("mod.name" , mod .name ));
90+ vars .put ("ID" , require ("mod.id" , mod .id ));
91+ vars .put ("VERSION" , require ("mod.version" , mod .version ));
92+ vars .put ("LOADER_VERSION" , loaderBranch );
93+ vars .put ("MINECRAFT" , minecraft );
94+ vars .put ("LOADER" , loader );
95+
96+ String version = require ("umc.version" , umc .version );
97+
98+ if (version .equals ("latest" )) {
99+ File path ;
100+ File temp = null ;
101+ if (umc .path == null ) {
102+ temp = Files .createTempDirectory ("umc-loader" ).toFile ();
103+ Util .gitClone ("https://github.com/TeamOpenIndustry/UniversalModCore.git" , loaderBranch , temp , false );
104+ path = temp ;
105+ } else {
106+ path = Paths .get (System .getProperty ("user.dir" ), umc .path ).toFile ();
107+ }
108+ version = Files .readAllLines (Paths .get (path .getPath (), "build.gradle" )).stream ()
109+ .filter (x -> x .startsWith ("String umcVersion = " ))
110+ .findFirst ()
111+ .get ()
112+ .replace ("String umcVersion = " , "" )
113+ .replace ("\" " , "" )
114+ .trim ();
115+ version += "-" + Util .gitRevision (path );
116+
117+ if (temp != null ) {
118+ FileUtils .deleteDirectory (temp );
119+ }
120+ }
41121
42- String version = require ("umcVersion" , umcVersion );
43- vars .put ("UMC_VERSION" , version );
44122 String [] versionParts = version .split ("\\ ." );
45123 int major = Integer .parseInt (versionParts [0 ]);
46124 int minor = Integer .parseInt (versionParts [1 ]);
47125 vars .put ("UMC_API" , String .format ("%d.%d" , major , minor ));
48126 vars .put ("UMC_API_NEXT" , String .format ("%d.%d" , major , minor + 1 ));
49127
128+ vars .put ("UMC_VERSION" , version );
50129
51- if (umcPath != null && umcPath .length () != 0 ) {
52- File jar = new File (umcPath , String .format ("UniversalModCore-%s-%s.jar" , loaderVersion , umcVersion ));
130+
131+ if (umc .path != null && umc .path .length () != 0 ) {
132+ File jar = new File (umc .path , String .format ("build/libs/UniversalModCore-%s-%s.jar" , loaderBranch , version ));
53133 if (!jar .exists ()) {
54134 throw new RuntimeException (String .format ("Unable to find UMC jar: %s" , jar ));
55135 }
56136 vars .put ("UMC_REPO" , String .format ("repositories { flatDir { dirs '%s' } }" , jar .getParent ()));
57137 vars .put ("UMC_DEPENDENCY" , String .format ("name: '%s'" , jar .getName ().replace (".jar" , "" )));
58138 vars .put ("UMC_FILE" , jar .getPath ());
59139 } else {
60- vars .put ("UMC_DOWNLOAD" , String .format ("https://teamopenindustry.cc/maven/cam72cam/universalmodcore/UniversalModCore/%s-%s/UniversalModCore-%s-%s.jar" , loaderVersion , version , loaderVersion , version ));
61- vars .put ("UMC_DEPENDENCY" , String .format ("'cam72cam.universalmodcore:UniversalModCore:%s-%s'" , loaderVersion , version ));
62140 vars .put ("UMC_REPO" , "repositories { maven { url = \" https://teamopenindustry.cc/maven\" }}" );
63-
141+ vars .put ("UMC_DEPENDENCY" , String .format ("'cam72cam.universalmodcore:UniversalModCore:%s-%s'" , loaderBranch , version ));
142+ vars .put ("UMC_DOWNLOAD" , String .format ("https://teamopenindustry.cc/maven/cam72cam/universalmodcore/UniversalModCore/%s-%s/UniversalModCore-%s-%s.jar" , loaderBranch , version , loaderBranch , version ));
64143 }
144+
145+ ArrayList <Mod .Dependency > dependencies = new ArrayList <>(mod .dependencies );
146+ dependencies .add (new Mod .Dependency (
147+ "universalmodcore" ,
148+ String .format ("[%s, %s)" , vars .get ("UMC_API" ), vars .get ("UMC_API_NEXT" ))
149+ ));
150+
151+
152+ String forgeStringDependencies = dependencies .stream ()
153+ .map (dep -> String .format ("required-after:%s@%s" , dep .id , dep .versions ))
154+ .collect (Collectors .joining ("; " ));
155+ vars .put ("FORGE_STRING_DEPENDENCIES" , forgeStringDependencies );
156+
157+ String forgeTomlDependencies = dependencies .stream ()
158+ .map (dep ->
159+ String .format ("[[dependencies.%s]]%n" , mod .id ) +
160+ String .format (" modId=\" %s\" %n" , dep .id ) +
161+ String .format (" mandatory=true%n" ) +
162+ String .format (" versionRange=\" %s\" %n" , dep .versions ) +
163+ String .format (" ordering=\" BEFORE\" %n" ) +
164+ String .format (" side=\" BOTH\" %n" )
165+ )
166+ .collect (Collectors .joining (System .lineSeparator () + System .lineSeparator ()));
167+ vars .put ("FORGE_TOML_DEPENDENCIES" , forgeTomlDependencies );
168+
169+ vars .put ("FABRIC_SOMETHING_DEPENDENCIES" , "TODO" );
65170 }
66171
67172
@@ -79,6 +184,10 @@ public InputStream openJarStream() throws IOException {
79184 return new FileInputStream (vars .get ("UMC_FILE" ));
80185 }
81186
187+ public String replace (String s ) {
188+ return replace (s , true );
189+ }
190+
82191 public String replace (String s , boolean hash ) {
83192 List <String > keys = new ArrayList <>(vars .keySet ());
84193 keys .sort (Comparator .comparingInt (String ::length ).reversed ());
@@ -94,7 +203,7 @@ public String replace(String s, boolean hash) {
94203
95204 public InputStream replaceAll (InputStream input , boolean hash ) throws IOException {
96205 String data = IOUtils .toString (input );
97- data = replace (data , true );
206+ data = replace (data );
98207 return new ByteArrayInputStream (data .getBytes ());
99208 }
100209}
0 commit comments