1
1
package com .falsepattern .lib ;
2
2
3
- import com .falsepattern .lib .version .Version ;
4
3
import com .google .common .eventbus .EventBus ;
5
4
import cpw .mods .fml .common .DummyModContainer ;
6
5
import cpw .mods .fml .common .LoadController ;
7
6
import cpw .mods .fml .common .ModMetadata ;
8
- import lombok .NonNull ;
7
+ import lombok .Getter ;
9
8
import lombok .val ;
10
- import lombok .var ;
11
9
import net .minecraft .launchwrapper .Launch ;
12
- import net .minecraft .launchwrapper .LaunchClassLoader ;
13
10
import org .apache .logging .log4j .LogManager ;
14
11
import org .apache .logging .log4j .Logger ;
15
12
16
- import javax .net .ssl .HttpsURLConnection ;
17
- import java .io .*;
18
- import java .net .URL ;
19
- import java .util .*;
20
-
13
+ import java .util .Arrays ;
21
14
15
+ /**
16
+ * Utility class used by FalsePatternLib's internal code. This can change between versions without notice, so do not use this in your code!
17
+ */
22
18
public class FalsePatternLib extends DummyModContainer {
23
- public static Logger libLog = LogManager . getLogger ( ModInfo . MODNAME );
24
- public static final boolean developerEnvironment = ( boolean ) Launch . blackboard . get ( "fml.deobfuscatedEnvironment" );
19
+ @ Getter
20
+ private static final Logger log = LogManager . getLogger ( ModInfo . MODNAME );
25
21
26
- private static final Map <String , Version > loadedLibraries = new HashMap <>();
27
- private static final Map <String , String > loadedLibraryMods = new HashMap <>();
28
- private static final Set <String > mavenRepositories = new HashSet <>();
22
+ @ Getter
23
+ private static final boolean developerEnvironment = (boolean ) Launch .blackboard .get ("fml.deobfuscatedEnvironment" );
29
24
30
25
public FalsePatternLib () {
31
26
super (new ModMetadata ());
32
- libLog .info ("All your libraries are belong to us!" );
27
+ log .info ("All your libraries are belong to us!" );
33
28
val meta = getMetadata ();
34
29
meta .modId = ModInfo .MODID ;
35
30
meta .name = ModInfo .MODNAME ;
@@ -46,111 +41,4 @@ public FalsePatternLib() {
46
41
public boolean registerBus (EventBus bus , LoadController controller ) {
47
42
return true ;
48
43
}
49
-
50
- public static void addMavenRepo (String url ) {
51
- mavenRepositories .add (url );
52
- }
53
-
54
- @ SuppressWarnings ("ResultOfMethodCallIgnored" )
55
- public static void loadLibrary (String loadingModId , String groupId , String artifactId , @ NonNull Version minVersion , Version maxVersion , @ NonNull Version preferredVersion , String regularSuffix , String devSuffix ) {
56
- val suffix = developerEnvironment ? devSuffix : regularSuffix ;
57
- libLog .info ("Adding library {}:{}:{}{}, requested by mod {}" , groupId , artifactId , preferredVersion , suffix != null ? "-" + suffix : "" , loadingModId );
58
- var artifact = groupId + ":" + artifactId + ":" + suffix ;
59
- if (loadedLibraries .containsKey (artifact )) {
60
- val currentVer = loadedLibraries .get (artifact );
61
- if (currentVer .equals (preferredVersion )) return ;
62
- val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion ) + ")" ;
63
- if (minVersion .compareTo (currentVer ) > 0 || (maxVersion != null && maxVersion .compareTo (currentVer ) < 0 )) {
64
- for (int i = 0 ; i < 16 ; i ++) {
65
- libLog .fatal ("ALERT VVVVVVVVVVVV ALERT" );
66
- }
67
- libLog .fatal ("Library {}:{}{} already loaded with version {}, " +
68
- "but a version in the range {} was requested! Thing may go horribly wrong! " +
69
- "Requested by mod: {}, previously loaded by mod: {}" ,
70
- groupId , artifactId , suffix != null ? ":" + suffix : "" , currentVer ,
71
- rangeString ,
72
- loadingModId , loadedLibraryMods .get (artifact ));
73
- for (int i = 0 ; i < 16 ; i ++) {
74
- libLog .fatal ("ALERT ^^^^^^^^^^^^ ALERT" );
75
- }
76
- } else {
77
- libLog .info ("Attempted loading of library {}:{}{} with preferred version {}, " +
78
- "but version {} was already loaded, which matched the range {}. This is not an error. " +
79
- "Requested by mod: {}, previously loaded by mod: {}" ,
80
- groupId , artifactId , suffix != null ? ":" + suffix : "" , preferredVersion ,
81
- currentVer , rangeString ,
82
- loadingModId , loadedLibraryMods .get (artifact ));
83
- }
84
- return ;
85
- }
86
- val modsDir = new File (CoreLoadingPlugin .mcDir , "mods" );
87
- val mavenJarName = String .format ("%s-%s%s.jar" , artifactId , preferredVersion , (suffix != null ) ? ("-" + suffix ) : "" );
88
- val jarName = groupId + "-" + mavenJarName ;
89
- val libDir = new File (modsDir , "falsepattern" );
90
- if (!libDir .exists ()) {
91
- libDir .mkdirs ();
92
- }
93
- val file = new File (libDir , jarName );
94
- if (file .exists ()) {
95
- try {
96
- addToClasspath (file );
97
- loadedLibraries .put (artifact , preferredVersion );
98
- libLog .info ("Library {}:{}:{}{} successfully loaded from disk!" , groupId , artifactId , preferredVersion , (suffix != null ) ? ":" + suffix : "" );
99
- return ;
100
- } catch (RuntimeException e ) {
101
- libLog .warn ("Failed to load library {}:{}:{}{} from file! Redownloading..." , groupId , artifactId , preferredVersion , (suffix != null ) ? ":" + suffix : "" );
102
- file .delete ();
103
- }
104
- }
105
- for (var repo : mavenRepositories ) {
106
- try {
107
- if (!repo .endsWith ("/" )) repo = repo + "/" ;
108
- val url = new URL (String .format ("%s%s/%s/%s/%s" , repo , groupId .replace ('.' , '/' ), artifactId , preferredVersion , mavenJarName ));
109
-
110
- val connection = (HttpsURLConnection ) url .openConnection ();
111
- connection .setConnectTimeout (1500 );
112
- connection .setReadTimeout (1500 );
113
- connection .setRequestProperty ("User-Agent" , "FalsePatternLib Downloader" );
114
- if (connection .getResponseCode () != 200 ) {
115
- libLog .info ("Artifact {}:{}:{}{} was not found on repo {}" , groupId , artifactId , preferredVersion , (suffix != null ) ? ":" + suffix : "" , repo );
116
- connection .disconnect ();
117
- continue ;
118
- }
119
- libLog .info ("Downloading {}:{}:{}{} from {}" , groupId , artifactId , preferredVersion , (suffix != null ) ? ":" + suffix : "" , repo );
120
- download (connection .getInputStream (), file );
121
- libLog .info ("Downloaded {}:{}:{}{}" , groupId , artifactId , preferredVersion , (suffix != null ) ? ":" + suffix : "" );
122
- loadedLibraries .put (artifact , preferredVersion );
123
- loadedLibraryMods .put (artifact , loadingModId );
124
- addToClasspath (file );
125
- return ;
126
- } catch (IOException ignored ) {}
127
- }
128
- val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + ((suffix != null ) ? ":" + suffix : "" ) + " from any repository! Requested by mod: " + loadingModId ;
129
- libLog .fatal (errorMessage );
130
- throw new IllegalStateException (errorMessage );
131
- }
132
-
133
- private static void addToClasspath (File file ) {
134
- try {
135
- val cl = (LaunchClassLoader ) FalsePatternLib .class .getClassLoader ();
136
- cl .addURL (file .toURI ().toURL ());
137
- libLog .info ("Injected file {} into classpath!" , file .getPath ());
138
- } catch (Exception e ) {
139
- throw new RuntimeException ("Failed to add library to classpath: " + file .getAbsolutePath (), e );
140
- }
141
- }
142
-
143
- private static void download (InputStream is , File target ) throws IOException {
144
- if (target .exists ()) return ;
145
-
146
- var bytesRead = 0 ;
147
-
148
- val fileOutput = new BufferedOutputStream (new FileOutputStream (target ));
149
- byte [] smallBuffer = new byte [4096 ];
150
- while ((bytesRead = is .read (smallBuffer )) >= 0 ) {
151
- fileOutput .write (smallBuffer , 0 , bytesRead );
152
- }
153
- fileOutput .close ();
154
- is .close ();
155
- }
156
44
}
0 commit comments