Skip to content

Commit

Permalink
add mod containers to platform service
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Mar 12, 2024
1 parent 902cc01 commit c754d86
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package dev.upcraft.sparkweave.api.platform;

import java.nio.file.Path;

public interface ModContainer {

ModMetadata metadata();

Path rootPath();

default Path getPath(String file) {
Path root = rootPath();
return root.resolve(file.replace("/", root.getFileSystem().getSeparator()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface PlatformService {

Optional<ModContainer> getModContainer(String modid);

List<ModContainer> getActiveMods();

RuntimeEnvironmentType getEnvironmentType();

List<String> getLaunchArguments(boolean hideSensitive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public NeoForgeModMetadata(ModContainer modContainer) {
delegate = modContainer.getModInfo();
}

public IModInfo modInfo() {
return delegate;
}

@Override
public String id() {
return delegate.getModId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

import dev.upcraft.sparkweave.api.platform.ModContainer;

import java.nio.file.Path;

public record NeoforgeModContainer(NeoForgeModMetadata metadata) implements ModContainer {

public static NeoforgeModContainer of(net.neoforged.fml.ModContainer delegate) {
return new NeoforgeModContainer(new NeoForgeModMetadata(delegate));
}

@Override
public Path rootPath() {
return metadata().modInfo().getOwningFile().getFile().getSecureJar().getRootPath();
}

@Override
public Path getPath(String file) {
return metadata().modInfo().getOwningFile().getFile().getSecureJar().getPath(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class NeoPlatformService extends BasePlatformService implements PlatformService {

Expand Down Expand Up @@ -88,6 +89,12 @@ public Optional<ModContainer> getModContainer(String modid) {
return ModList.get().getModContainerById(modid).map(NeoforgeModContainer::of);
}

@Override
public List<ModContainer> getActiveMods() {
// can't use Stream#toList() because java compiler is dumb :(
return ModList.get().getSortedMods().stream().map(NeoforgeModContainer::of).collect(Collectors.toList());
}

@Override
public RuntimeEnvironmentType getEnvironmentType() {
return switch (FMLEnvironment.dist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

import dev.upcraft.sparkweave.api.platform.ModContainer;

public record QuiltModContainer(QuiltModMetadata metadata) implements ModContainer {
import java.nio.file.Path;

public record QuiltModContainer(org.quiltmc.loader.api.ModContainer delegate, QuiltModMetadata metadata) implements ModContainer {

public static QuiltModContainer of(org.quiltmc.loader.api.ModContainer delegate) {
return new QuiltModContainer(new QuiltModMetadata(delegate));
return new QuiltModContainer(delegate, new QuiltModMetadata(delegate));
}

@Override
public Path rootPath() {
return delegate().rootPath();
}

@Override
public Path getPath(String file) {
return delegate().getPath(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class QuiltPlatformService extends BasePlatformService implements PlatformService {

Expand Down Expand Up @@ -82,6 +83,12 @@ public Optional<ModContainer> getModContainer(String modid) {
return MOD_CONTAINERS.computeIfAbsent(modid, key -> QuiltLoader.getModContainer(key).map(QuiltModContainer::of));
}

@Override
public List<ModContainer> getActiveMods() {
// can't use Stream#toList() because java compiler is dumb :(
return QuiltLoader.getAllMods().stream().map(QuiltModContainer::of).collect(Collectors.toList());
}

@Override
public RuntimeEnvironmentType getEnvironmentType() {
return environmentType;
Expand Down

0 comments on commit c754d86

Please sign in to comment.