11package io .github .fabriccompatibiltylayers .modremappingapi .impl .context .v1 ;
22
33import 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 ;
47import io .github .fabriccompatibilitylayers .modremappingapi .impl .InternalCacheHandler ;
58import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .MappingBuilder ;
69import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .ModRemapper ;
912import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .BaseModRemapperContext ;
1013import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .MappingsRegistryInstance ;
1114import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .MixinData ;
15+ import io .github .fabriccompatibiltylayers .modremappingapi .impl .context .v2 .V2ModDiscoverer ;
1216import io .github .fabriccompatibiltylayers .modremappingapi .impl .mappings .MappingsRegistry ;
1317import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .ModTrRemapper ;
1418import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .RemappingFlags ;
1519import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .SoftLockFixer ;
1620import io .github .fabriccompatibiltylayers .modremappingapi .impl .remapper .visitor .MRAApplyVisitor ;
21+ import io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .FileUtils ;
1722import net .fabricmc .loader .api .FabricLoader ;
18- import net .fabricmc .loader .impl .launch .FabricLauncherBase ;
1923import net .fabricmc .tinyremapper .TinyRemapper ;
2024
2125import java .io .IOException ;
2226import java .io .InputStream ;
27+ import java .net .URISyntaxException ;
28+ import java .nio .file .Files ;
2329import java .nio .file .Path ;
2430import java .util .*;
2531import java .util .function .Supplier ;
32+ import java .util .stream .Collectors ;
2633
2734public class ModRemapperV1Context extends BaseModRemapperContext <ModRemapper > {
2835 private final Set <RemappingFlags > remapFlags = new HashSet <>();
@@ -31,7 +38,6 @@ public class ModRemapperV1Context extends BaseModRemapperContext<ModRemapper> {
3138 private final InternalCacheHandler cacheHandler = new V1CacheHandler ();
3239 private final MappingsRegistryInstance mappingsRegistry = new MappingsRegistryInstance (cacheHandler );
3340 private final LibraryHandler libraryHandler = new LibraryHandler ();
34- private final V1ModDiscoverer modDiscoverer = new V1ModDiscoverer ();
3541
3642 public static ModRemapperV1Context INSTANCE ;
3743
@@ -79,7 +85,7 @@ public void init() {
7985 this .mappingsRegistry .generateAdditionalMappings ();
8086 }
8187
82- public void remapMods (Map <io . github . fabriccompatibilitylayers . modremappingapi . api . v2 . ModCandidate , Path > pathMap ) {
88+ public void remapMods (Map <ModCandidate , Path > pathMap ) {
8389 Constants .MAIN_LOGGER .debug ("Starting jar remapping!" );
8490 SoftLockFixer .preloadClasses ();
8591 TinyRemapper remapper = ModTrRemapper .makeRemapper (this );
@@ -95,23 +101,115 @@ public void afterRemap() {
95101 remappers .forEach (ModRemapper ::afterRemap );
96102 }
97103
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+
98128 @ Override
99129 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 <>();
101133
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 ());
104176 }
105177
106178 mappingsRegistry .generateModMappings ();
107179
108- // this.remapMods(modPaths );
180+ Map < ModCandidate , Path > candidateToOutput = new HashMap <>( );
109181
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);
111193
112194 return new ArrayList <>();
113195 }
114196
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+
115213 private static final String v0EntrypointName = "mod-remapper-api:modremapper" ;
116214 private static final String v1EntrypointName = "mod-remapper-api:modremapper_v1" ;
117215
0 commit comments