8
8
import io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .FileUtils ;
9
9
import net .fabricmc .loader .api .FabricLoader ;
10
10
import net .fabricmc .loader .impl .launch .FabricLauncherBase ;
11
+ import org .jetbrains .annotations .NotNull ;
11
12
import org .spongepowered .include .com .google .common .collect .ImmutableList ;
12
13
13
14
import java .io .File ;
14
- import java .io .FileInputStream ;
15
15
import java .io .IOException ;
16
16
import java .net .URISyntaxException ;
17
17
import java .nio .file .*;
18
+ import java .nio .file .attribute .BasicFileAttributes ;
18
19
import java .util .*;
19
20
import java .util .stream .Collectors ;
20
- import java .util .zip .ZipEntry ;
21
- import java .util .zip .ZipInputStream ;
22
21
23
22
public class ModDiscoverer {
24
23
private static final Map <String , List <String >> EXCLUDED = new HashMap <>();
@@ -44,8 +43,8 @@ protected static void init(List<ModRemapper> modRemappers, boolean remapClassEdi
44
43
if (!Files .exists (cacheFolder )) Files .createDirectories (cacheFolder );
45
44
else io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .FileUtils .emptyDir (cacheFolder );
46
45
47
- mods .addAll (discoverModsInFolder (mcSubFolder . toFile () , cacheFolder . toFile () ));
48
- } catch (IOException e ) {
46
+ mods .addAll (discoverModsInFolder (mcSubFolder , cacheFolder ));
47
+ } catch (IOException | URISyntaxException e ) {
49
48
e .printStackTrace ();
50
49
}
51
50
}
@@ -97,7 +96,7 @@ private static Map<Path, Path> excludeClassEdits(Map<Path, Path> modPaths) {
97
96
for (Map .Entry <Path , Path > entry : convertMap .entrySet ()) {
98
97
try {
99
98
if (Files .isDirectory (entry .getKey ())) {
100
- io . github . fabriccompatibiltylayers . modremappingapi . impl . utils . FileUtils .zipFolder (entry .getKey (), entry .getValue ());
99
+ FileUtils .zipFolder (entry .getKey (), entry .getValue ());
101
100
} else {
102
101
Files .copy (entry .getKey (), entry .getValue (), StandardCopyOption .REPLACE_EXISTING );
103
102
}
@@ -114,97 +113,90 @@ private static Map<Path, Path> excludeClassEdits(Map<Path, Path> modPaths) {
114
113
return map ;
115
114
}
116
115
117
- private static List <ModEntry > discoverModsInFolder (File folder , File destination ) {
118
- List <ModEntry > mods = new ArrayList <>();
116
+ private static Optional <ModEntry > discoverFolderMod (Path folder , Path destinationFolder ) throws IOException {
117
+ String name = folder .getFileName ().toString ().replace (" " , "_" );
118
+ Path destination = destinationFolder .resolve (name + ".zip" );
119
119
120
- File [] folderFiles = folder .listFiles ();
121
- if (!folder .isDirectory () || folderFiles == null ) return ImmutableList .of ();
120
+ final boolean [] hasClasses = {false };
122
121
123
- for (File file : folderFiles ) {
124
- String name = file .getName ().replace (" " , "_" );
125
- if (file .isDirectory () || (file .isFile () && (name .endsWith (".jar" ) || name .endsWith (".zip" )))) {
126
- File remappedFile = new File (destination , name );
122
+ Files .walkFileTree (folder , new SimpleFileVisitor <Path >() {
123
+ @ Override
124
+ public @ NotNull FileVisitResult visitFile (Path file , @ NotNull BasicFileAttributes attrs ) throws IOException {
125
+ if (file .toString ().endsWith (".class" )) {
126
+ hasClasses [0 ] = true ;
127
+ return FileVisitResult .TERMINATE ;
128
+ }
127
129
128
- List <ModEntry > modName = new ArrayList <>();
130
+ return super .visitFile (file , attrs );
131
+ }
132
+ });
133
+
134
+ if (hasClasses [0 ]) {
135
+ return Optional .of (
136
+ new DefaultModEntry (
137
+ name ,
138
+ folder .toFile (),
139
+ destination .toFile ()
140
+ )
141
+ );
142
+ }
129
143
130
- boolean hasClass = false ;
131
- boolean fabric = false ;
144
+ return Optional . empty () ;
145
+ }
132
146
133
- File [] fileFiles = file .listFiles ();
147
+ private static Optional <ModEntry > discoverFileMod (Path file , Path destinationFolder ) throws IOException {
148
+ String fileName = file .getFileName ().toString ().replace (" " , "_" );
149
+ String modName = fileName .replace (".jar" , "" ).replace (".zip" , "" );
134
150
135
- if (file .isDirectory () && fileFiles != null ) {
136
- remappedFile = new File (destination , name + ".zip" );
137
- for (File subFile : fileFiles ) {
138
- String subName = subFile .getName ();
139
- if (subFile .isFile ()) {
140
- if (subName .endsWith (".class" )) {
141
- hasClass = true ;
142
- }
143
- }
144
- }
151
+ List <String > entries = FileUtils .listZipContent (file );
145
152
146
- if (/* modName.isEmpty() && */ hasClass ) {
147
- modName .add (new DefaultModEntry (
148
- name .replace (".zip" , "" ).replace (".jar" , "" ),
149
- remappedFile ,
150
- file
151
- ));
152
- }
153
+ for (String entry : entries ) {
154
+ if (Objects .equals (entry , "/fabric.mod.json" )) break ;
153
155
154
- if (!modName .isEmpty () && EXCLUDED .containsKey (modName .get (0 ).modName )) {
155
- for (String excluded :
156
- EXCLUDED .get (modName .get (0 ).modName )) {
157
- File excludedFile = new File (file , excluded );
158
- if (excludedFile .delete ()) {
159
- Constants .MAIN_LOGGER .debug ("File deleted: " + excludedFile .getName ());
160
- }
161
- }
162
- }
163
- } else {
164
- try {
165
- FileInputStream fileinputstream = new FileInputStream (file );
166
- ZipInputStream zipinputstream = new ZipInputStream (fileinputstream );
167
- while (true ) {
168
- ZipEntry zipentry = zipinputstream .getNextEntry ();
169
- if (zipentry == null ) {
170
- zipinputstream .close ();
171
- fileinputstream .close ();
172
- break ;
173
- }
174
-
175
- String s1 = zipentry .getName ();
176
- String [] ss = s1 .split ("/" );
177
- String s2 = ss [ss .length - 1 ];
178
- if (!zipentry .isDirectory ()) {
179
- if (s2 .equals ("fabric.mod.json" )) {
180
- // modName.clear();
181
- fabric = true ;
182
- break ;
183
- } else if (s2 .endsWith (".class" )) {
184
- hasClass = true ;
185
- }
186
- }
187
- }
156
+ if (entry .endsWith (".class" )) {
157
+ return Optional .of (
158
+ new DefaultModEntry (
159
+ modName ,
160
+ file .toFile (),
161
+ destinationFolder .resolve (fileName ).toFile ()
162
+ )
163
+ );
164
+ }
165
+ }
188
166
189
- if (/* modName.isEmpty() && */ hasClass && !fabric ) {
190
- modName .add (new DefaultModEntry (
191
- name .replace (".zip" , "" ).replace (".jar" , "" ),
192
- remappedFile ,
193
- file
194
- ));
195
- }
167
+ return Optional .empty ();
168
+ }
169
+
170
+ private static List <ModEntry > discoverModsInFolder (Path folder , Path destination ) throws IOException , URISyntaxException {
171
+ List <ModEntry > mods = new ArrayList <>();
172
+
173
+ if (!Files .isDirectory (folder )) return ImmutableList .of ();
174
+
175
+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (folder )) {
176
+ for (Path path : stream ) {
177
+ String name = path .getFileName ().toString ();
196
178
197
- if (!modName .isEmpty ()) {
198
- if (EXCLUDED .containsKey (modName .get (0 ).modName )) {
199
- FileUtils .removeEntriesFromZip (file .toPath (), EXCLUDED .get (modName .get (0 ).modName ));
200
- }
179
+ if (Files .isDirectory (path )) {
180
+ discoverFolderMod (path , destination )
181
+ .ifPresent (mods ::add );
182
+ } else if (Files .exists (path ) && (name .endsWith (".jar" ) || name .endsWith (".zip" ))) {
183
+ discoverFileMod (path , destination )
184
+ .ifPresent (mods ::add );
185
+ }
186
+ }
187
+ }
188
+
189
+ for (ModEntry modEntry : mods ) {
190
+ if (EXCLUDED .containsKey (modEntry .modId )) {
191
+ if (Files .isDirectory (modEntry .file .toPath ())) {
192
+ for (String excluded : EXCLUDED .get (modEntry .modId )) {
193
+ if (Files .deleteIfExists (modEntry .file .toPath ().resolve (excluded ))) {
194
+ Constants .MAIN_LOGGER .debug ("File deleted: " + modEntry .file .toPath ().resolve (excluded ));
201
195
}
202
- } catch (IOException | URISyntaxException e ) {
203
- throw new RuntimeException (e );
204
196
}
197
+ } else {
198
+ FileUtils .removeEntriesFromZip (modEntry .file .toPath (), EXCLUDED .get (modEntry .modId ));
205
199
}
206
-
207
- mods .addAll (modName );
208
200
}
209
201
}
210
202
0 commit comments