Split off highly version dependent code from ReplayMod

This commit is contained in:
Jonas Herzig
2021-02-20 00:41:40 +01:00
parent bc9d8cb3d1
commit 0be2082fe7
12 changed files with 443 additions and 401 deletions

View File

@@ -0,0 +1,85 @@
package com.replaymod.core;
import com.replaymod.core.mixin.MinecraftAccessor;
import net.minecraft.client.resources.IResourcePack;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import java.util.List;
import static com.replaymod.core.ReplayMod.MOD_ID;
import static com.replaymod.core.ReplayMod.jGuiResourcePack;
import static com.replaymod.core.versions.MCVer.getMinecraft;
//#if MC<=10710
//$$ import net.minecraft.client.resources.FolderResourcePack;
//$$ import java.io.ByteArrayInputStream;
//$$ import java.io.File;
//$$ import java.io.IOException;
//$$ import java.io.InputStream;
//$$ import java.nio.charset.StandardCharsets;
//#endif
@Mod(modid = ReplayMod.MOD_ID,
useMetadata = true,
version = "@MOD_VERSION@",
acceptedMinecraftVersions = "@MC_VERSION@",
acceptableRemoteVersions = "*",
//#if MC>=10800
clientSideOnly = true,
updateJSON = "https://raw.githubusercontent.com/ReplayMod/ReplayMod/master/versions.json",
//#endif
guiFactory = "com.replaymod.core.gui.GuiFactory")
public class ReplayModBackend {
private final ReplayMod mod = new ReplayMod(this);
@Deprecated
public static Configuration config;
@EventHandler
public void init(FMLPreInitializationEvent event) {
config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
SettingsRegistry settingsRegistry = mod.getSettingsRegistry();
settingsRegistry.backend.setConfiguration(config);
settingsRegistry.save(); // Save default values to disk
}
@EventHandler
public void init(FMLInitializationEvent event) {
mod.initModules();
}
public String getVersion() {
return Loader.instance().getIndexedModList().get(MOD_ID).getVersion();
}
static { // Note: even preInit is too late and we'd have to issue another resource reload
List<IResourcePack> defaultResourcePacks = ((MinecraftAccessor) getMinecraft()).getDefaultResourcePacks();
if (jGuiResourcePack != null) {
defaultResourcePacks.add(jGuiResourcePack);
}
//#if MC<=10710
//$$ FolderResourcePack mainResourcePack = new FolderResourcePack(new File("../src/main/resources")) {
//$$ @Override
//$$ protected InputStream getInputStreamByName(String resourceName) throws IOException {
//$$ try {
//$$ return super.getInputStreamByName(resourceName);
//$$ } catch (IOException e) {
//$$ if ("pack.mcmeta".equals(resourceName)) {
//$$ return new ByteArrayInputStream(("{\"pack\": {\"description\": \"dummy pack for mod resources in dev-env\", \"pack_format\": 1}}").getBytes(StandardCharsets.UTF_8));
//$$ }
//$$ throw e;
//$$ }
//$$ }
//$$ };
//$$ defaultResourcePacks.add(mainResourcePack);
//#endif
}
}

View File

@@ -0,0 +1,131 @@
package com.replaymod.core.versions.scheduler;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.replaymod.core.mixin.MinecraftAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ReportedException;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
//#if MC<10800
//$$ import java.util.ArrayDeque;
//#endif
import static com.replaymod.core.versions.MCVer.FML_BUS;
public class SchedulerImpl implements Scheduler {
private static final Minecraft mc = Minecraft.getMinecraft();
@Override
public void runSync(Runnable runnable) throws InterruptedException, ExecutionException, TimeoutException {
if (mc.isCallingFromMinecraftThread()) {
runnable.run();
} else {
FutureTask<Void> future = new FutureTask<>(runnable, null);
runLater(future);
future.get(30, TimeUnit.SECONDS);
}
}
@Override
public void runPostStartup(Runnable runnable) {
runLater(runnable);
}
/**
* Set when the currently running code has been scheduled by runLater.
* If this is the case, subsequent calls to runLater have to be delayed until all scheduled tasks have been
* processed, otherwise a livelock may occur.
*/
private boolean inRunLater = false;
@Override
public void runLaterWithoutLock(Runnable runnable) {
runLater(() -> runLaterWithoutLock(runnable), runnable);
}
public void runLater(Runnable runnable) {
runLater(runnable, () -> runLater(runnable));
}
private void runLater(Runnable runnable, Runnable defer) {
if (mc.isCallingFromMinecraftThread() && inRunLater) {
//#if MC>=10800
FML_BUS.register(new Object() {
@SubscribeEvent
public void onRenderTick(TickEvent.RenderTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
FML_BUS.unregister(this);
defer.run();
}
}
});
//#else
//$$ FML_BUS.register(new RunLaterHelper(defer));
//#endif
return;
}
//#if MC>=10800
Queue<FutureTask<?>> tasks = ((MinecraftAccessor) mc).getScheduledTasks();
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (tasks) {
//#else
//$$ Queue<ListenableFutureTask<?>> tasks = scheduledTasks;
//$$ synchronized (scheduledTasks) {
//#endif
tasks.add(ListenableFutureTask.create(() -> {
inRunLater = true;
try {
runnable.run();
} catch (ReportedException e) {
e.printStackTrace();
System.err.println(e.getCrashReport().getCompleteReport());
mc.crashed(e.getCrashReport());
} finally {
inRunLater = false;
}
}, null));
}
}
//#if MC>=10800
@Override
public void runTasks() {
}
//#else
//$$ // 1.7.10: Cannot use MC's because it is processed only during ticks (so not at all when replay is paused)
//$$ private final Queue<ListenableFutureTask<?>> scheduledTasks = new ArrayDeque<>();
//$$
//$$ // in 1.7.10 apparently events can't be delivered to anonymous classes
//$$ public class RunLaterHelper {
//$$ private final Runnable defer;
//$$
//$$ private RunLaterHelper(Runnable defer) {
//$$ this.defer = defer;
//$$ }
//$$
//$$ @SubscribeEvent
//$$ public void onRenderTick(TickEvent.RenderTickEvent event) {
//$$ if (event.phase == TickEvent.Phase.START) {
//$$ FML_BUS.unregister(this);
//$$ defer.run();
//$$ }
//$$ }
//$$ }
//$$
//$$ @Override
//$$ public void runTasks() {
//$$ synchronized (scheduledTasks) {
//$$ while (!scheduledTasks.isEmpty()) {
//$$ scheduledTasks.poll().run();
//$$ }
//$$ }
//$$ }
//#endif
}

View File

@@ -0,0 +1,11 @@
package com.replaymod.core;
import net.minecraftforge.fml.ModList;
import static com.replaymod.core.ReplayMod.MOD_ID;
public class ReplayModBackend {
public String getVersion() {
return ModList.get().getModContainerById(MOD_ID).get().getModInfo().getVersion().toString();
}
}