1
1
package io .github .fabriccompatibiltylayers .modremappingapi .impl .context .v1 ;
2
2
3
3
import fr .catcore .modremapperapi .utils .Constants ;
4
+ import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .ModCandidate ;
5
+ import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .ModDiscovererConfig ;
6
+ import io .github .fabriccompatibilitylayers .modremappingapi .impl .DefaultModCandidate ;
4
7
import io .github .fabriccompatibilitylayers .modremappingapi .impl .InternalCacheHandler ;
5
8
import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .MappingBuilder ;
6
9
import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .ModRemapper ;
9
12
import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .BaseModRemapperContext ;
10
13
import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .MappingsRegistryInstance ;
11
14
import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .MixinData ;
15
+ import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .v2 .V2ModDiscoverer ;
12
16
import io .github .fabriccompatibiltylayers .modremappingapi .impl .mappings .MappingsRegistry ;
13
17
import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .ModTrRemapper ;
14
18
import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .RemappingFlags ;
15
19
import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .SoftLockFixer ;
16
20
import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .visitor .MRAApplyVisitor ;
21
+ import io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .FileUtils ;
17
22
import net .fabricmc .loader .api .FabricLoader ;
18
- import net .fabricmc .loader .impl .launch .FabricLauncherBase ;
19
23
import net .fabricmc .tinyremapper .TinyRemapper ;
20
24
21
25
import java .io .IOException ;
22
26
import java .io .InputStream ;
27
+ import java .net .URISyntaxException ;
28
+ import java .nio .file .Files ;
23
29
import java .nio .file .Path ;
24
30
import java .util .*;
25
31
import java .util .function .Supplier ;
32
+ import java .util .stream .Collectors ;
26
33
27
34
public class ModRemapperV1Context extends BaseModRemapperContext <ModRemapper > {
28
35
private final Set <RemappingFlags > remapFlags = new HashSet <>();
@@ -31,7 +38,6 @@ public class ModRemapperV1Context extends BaseModRemapperContext<ModRemapper> {
31
38
private final InternalCacheHandler cacheHandler = new V1CacheHandler ();
32
39
private final MappingsRegistryInstance mappingsRegistry = new MappingsRegistryInstance (cacheHandler );
33
40
private final LibraryHandler libraryHandler = new LibraryHandler ();
34
- private final V1ModDiscoverer modDiscoverer = new V1ModDiscoverer ();
35
41
36
42
public static ModRemapperV1Context INSTANCE ;
37
43
@@ -79,7 +85,7 @@ public void init() {
79
85
this .mappingsRegistry .generateAdditionalMappings ();
80
86
}
81
87
82
- public void remapMods (Map <io . github . fabriccompatibilitylayers . modremappingapi . api . v2 . ModCandidate , Path > pathMap ) {
88
+ public void remapMods (Map <ModCandidate , Path > pathMap ) {
83
89
Constants .MAIN_LOGGER .debug ("Starting jar remapping!" );
84
90
SoftLockFixer .preloadClasses ();
85
91
TinyRemapper remapper = ModTrRemapper .makeRemapper (this );
@@ -95,23 +101,115 @@ public void afterRemap() {
95
101
remappers .forEach (ModRemapper ::afterRemap );
96
102
}
97
103
104
+ private List <ModCandidate > collectCandidates (ModDiscovererConfig config , Path modPath , List <String > entries ) {
105
+ boolean fabric = false ;
106
+ boolean hasClass = false ;
107
+
108
+ for (String entry : entries ) {
109
+ if (entry .endsWith ("fabric.mod.json" ) || entry .endsWith ("quilt.mod.json" ) || entry .endsWith ("quilt.mod.json5" )) {
110
+ fabric = true ;
111
+ break ;
112
+ }
113
+
114
+ if (entry .endsWith (".class" )) {
115
+ hasClass = true ;
116
+ }
117
+ }
118
+
119
+ List <ModCandidate > list = new ArrayList <>();
120
+
121
+ if (hasClass && !fabric ) {
122
+ list .add (new DefaultModCandidate (modPath , config ));
123
+ }
124
+
125
+ return list ;
126
+ }
127
+
98
128
@ Override
99
129
public List <ModRemapper > discoverMods (boolean remapClassEdits ) {
100
- Map <ModCandidate , Path > modPaths = this .modDiscoverer .init (remappers , remapClassEdits , this );
130
+ Map <String , List <String >> excluded = new HashMap <>();
131
+
132
+ Set <String > modFolders = new HashSet <>();
101
133
102
- for (ModCandidate candidate : modPaths .keySet ()) {
103
- mappingsRegistry .addModMappings (candidate .original );
134
+ for (ModRemapper remapper : remappers ) {
135
+ Collections .addAll (modFolders , remapper .getJarFolders ());
136
+
137
+ if (remapper instanceof V0ModRemapper ) {
138
+ excluded .putAll (((V0ModRemapper ) remapper ).getExclusions ());
139
+ }
140
+ }
141
+
142
+ List <ModCandidate > candidates = new ArrayList <>();
143
+ Map <ModDiscovererConfig , V2ModDiscoverer > config2Discoverer = new HashMap <>();
144
+
145
+ for (String modFolder : modFolders ) {
146
+ ModDiscovererConfig config = ModDiscovererConfig .builder (modFolder )
147
+ .fileNameMatcher ("(.+).(jar|zip)$" )
148
+ .candidateCollector (this ::collectCandidates )
149
+ .build ();
150
+ V2ModDiscoverer discoverer = new V2ModDiscoverer (config );
151
+ config2Discoverer .put (config , discoverer );
152
+ candidates .addAll (discoverer .collect ());
153
+ }
154
+
155
+ try {
156
+ this .handleV0Excluded (candidates , excluded );
157
+ } catch (IOException | URISyntaxException e ) {
158
+ throw new RuntimeException (e );
159
+ }
160
+
161
+ Map <ModDiscovererConfig , List <ModCandidate >> config2Candidates =
162
+ candidates .stream ().collect (Collectors .groupingBy (ModCandidate ::getDiscovererConfig ));
163
+
164
+ for (Map .Entry <ModDiscovererConfig , List <ModCandidate >> entry : config2Candidates .entrySet ()) {
165
+ ModDiscovererConfig config = entry .getKey ();
166
+
167
+ try {
168
+ config2Discoverer .get (config ).excludeClassEdits (entry .getValue (), this .cacheHandler , this .mappingsRegistry );
169
+ } catch (IOException | URISyntaxException e ) {
170
+ throw new RuntimeException (e );
171
+ }
172
+ }
173
+
174
+ for (ModCandidate candidate : candidates ) {
175
+ mappingsRegistry .addModMappings (candidate .getPath ());
104
176
}
105
177
106
178
mappingsRegistry .generateModMappings ();
107
179
108
- // this.remapMods(modPaths );
180
+ Map < ModCandidate , Path > candidateToOutput = new HashMap <>( );
109
181
110
- modPaths .values ().forEach (FabricLauncherBase .getLauncher ()::addToClassPath );
182
+ for (Map .Entry <ModDiscovererConfig , List <ModCandidate >> entry : config2Candidates .entrySet ()) {
183
+ ModDiscovererConfig config = entry .getKey ();
184
+
185
+ candidateToOutput .putAll (
186
+ config2Discoverer .get (config ).computeDestinations (entry .getValue (), this .cacheHandler )
187
+ );
188
+ }
189
+
190
+ if (!candidateToOutput .isEmpty ()) this .remapMods (candidateToOutput );
191
+
192
+ // modPaths.values().forEach(FabricLauncherBase.getLauncher()::addToClassPath);
111
193
112
194
return new ArrayList <>();
113
195
}
114
196
197
+ private void handleV0Excluded (List <ModCandidate > mods , Map <String , List <String >> excludedMap ) throws IOException , URISyntaxException {
198
+ for (ModCandidate modCandidate : mods ) {
199
+ if (excludedMap .containsKey (modCandidate .getId ())) {
200
+ if (Files .isDirectory (modCandidate .getPath ())) {
201
+ for (String excluded : excludedMap .get (modCandidate .getId ())) {
202
+ if (Files .deleteIfExists (modCandidate .getPath ().resolve (excluded ))) {
203
+ Constants .MAIN_LOGGER .debug ("File deleted: " + modCandidate .getPath ().resolve (excluded ));
204
+ }
205
+ }
206
+ } else {
207
+ FileUtils .removeEntriesFromZip (modCandidate .getPath (), excludedMap .get (modCandidate .getId ()));
208
+ }
209
+ }
210
+ }
211
+ }
212
+
115
213
private static final String v0EntrypointName = "mod-remapper-api:modremapper" ;
116
214
private static final String v1EntrypointName = "mod-remapper-api:modremapper_v1" ;
117
215
0 commit comments