Skip to content

Commit

Permalink
fix crash when accessing modlist too early
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Sep 18, 2024
1 parent 9e87c04 commit 2470fc3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package dev.upcraft.sparkweave.client.render;

import dev.upcraft.sparkweave.SparkweaveMod;
import dev.upcraft.sparkweave.api.SparkweaveApi;
import dev.upcraft.sparkweave.api.annotation.Mod;
import dev.upcraft.sparkweave.logging.SparkweaveLogging;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

@Mod.Context(SparkweaveMod.MODID)
public class RenderDocHelper {

private static final Logger LOGGER = SparkweaveLogging.getLogger();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.upcraft.sparkweave.platform;

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

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class SimpleModContainer implements ModContainer {

private final ModMetadata metadata;
private final List<Path> rootPaths;
private final Function<String, Optional<Path>> pathFinder;

public SimpleModContainer(ModMetadata metadata, List<Path> rootPaths, Function<String, Optional<Path>> pathFinder) {
this.metadata = metadata;
this.rootPaths = rootPaths;
this.pathFinder = pathFinder;
}

@Override
public ModMetadata metadata() {
return metadata;
}

@Override
public List<Path> rootPaths() {
return rootPaths;
}

@Override
public Optional<Path> findPath(String path) {
return pathFinder.apply(path);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.upcraft.sparkweave.neoforge.impl.mod;

import dev.upcraft.sparkweave.api.platform.ModMetadata;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.loading.moddiscovery.ModFileInfo;
import net.neoforged.neoforgespi.language.IModInfo;
import org.jetbrains.annotations.Nullable;
Expand All @@ -13,8 +12,8 @@ public class NeoForgeModMetadata implements ModMetadata {

private final IModInfo delegate;

public NeoForgeModMetadata(ModContainer modContainer) {
delegate = modContainer.getModInfo();
public NeoForgeModMetadata(IModInfo delegate) {
this.delegate = delegate;
}

public IModInfo modInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public record NeoforgeModContainer(NeoForgeModMetadata metadata) implements ModContainer {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import dev.upcraft.sparkweave.api.platform.ModContainer;
import dev.upcraft.sparkweave.api.platform.RuntimeEnvironmentType;
import dev.upcraft.sparkweave.api.platform.services.PlatformService;
import dev.upcraft.sparkweave.neoforge.impl.mod.NeoForgeModMetadata;
import dev.upcraft.sparkweave.neoforge.impl.mod.NeoforgeModContainer;
import dev.upcraft.sparkweave.platform.BasePlatformService;
import dev.upcraft.sparkweave.platform.SimpleModContainer;
import net.minecraft.SharedConstants;
import net.neoforged.fml.ModList;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.fml.loading.LoadingModList;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.util.JavaVersion;
import oshi.SystemInfo;

import java.nio.file.Path;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -86,7 +91,18 @@ public Path getConfigDir() {

@Override
public Optional<ModContainer> getModContainer(String modid) {
return ModList.get().getModContainerById(modid).map(NeoforgeModContainer::of);
var modList = ModList.get();
if(modList == null) { // ModList not loaded yet
@Nullable var modFile = LoadingModList.get().getModFileById(modid);
if(modFile == null) {
return Optional.empty();
}

var meta = new NeoForgeModMetadata(modFile.getMods().stream().filter(it -> modid.equals(it.getModId())).findFirst().orElseThrow(() -> new NoSuchElementException("LoadingModList did not contain mod with ID '%s'!".formatted(modid))));
return Optional.of(new SimpleModContainer(meta, List.of(modFile.getFile().getSecureJar().getRootPath()), path -> Optional.ofNullable(modFile.getFile().getSecureJar().getPath(path))));
}

return modList.getModContainerById(modid).map(NeoforgeModContainer::of);
}

@Override
Expand Down

0 comments on commit 2470fc3

Please sign in to comment.