diff --git a/src/main/java/li/cil/oc2/common/Config.java b/src/main/java/li/cil/oc2/common/Config.java index 1c4ff161..ce97475f 100644 --- a/src/main/java/li/cil/oc2/common/Config.java +++ b/src/main/java/li/cil/oc2/common/Config.java @@ -1,9 +1,83 @@ package li.cil.oc2.common; -public final class Config { - public static long maxAllocatedData = 512 * Constants.MEGABYTE; +import li.cil.oc2.api.API; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModLoadingContext; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.config.ModConfig; +import org.apache.commons.lang3.tuple.Pair; +@Mod.EventBusSubscriber(modid = API.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD) +public final class Config { + private static final CommonSettings COMMON_INSTANCE; + private static final ForgeConfigSpec COMMON_SPEC; + + /////////////////////////////////////////////////////////////////// + + public static long maxAllocatedMemory = 512 * Constants.MEGABYTE; public static int maxMemorySize = 8 * Constants.MEGABYTE; public static int maxHardDriveSize = 8 * Constants.MEGABYTE; public static int maxFlashMemorySize = 4 * Constants.KILOBYTE; + + /////////////////////////////////////////////////////////////////// + + static { + final Pair commonConfig = new ForgeConfigSpec.Builder().configure(CommonSettings::new); + COMMON_INSTANCE = commonConfig.getKey(); + COMMON_SPEC = commonConfig.getValue(); + } + + /////////////////////////////////////////////////////////////////// + + public static void initialize() { + ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, COMMON_SPEC); + } + + /////////////////////////////////////////////////////////////////// + + @SubscribeEvent + public static void handleModConfigEvent(final ModConfig.ModConfigEvent event) { + if (event.getConfig().getSpec() == COMMON_SPEC) { + maxAllocatedMemory = COMMON_INSTANCE.maxAllocatedMemory.get(); + maxMemorySize = COMMON_INSTANCE.maxMemorySize.get(); + maxHardDriveSize = COMMON_INSTANCE.maxHardDriveSize.get(); + maxFlashMemorySize = COMMON_INSTANCE.maxFlashMemorySize.get(); + } + } + + /////////////////////////////////////////////////////////////////// + + private static final class CommonSettings { + public ForgeConfigSpec.LongValue maxAllocatedMemory; + public ForgeConfigSpec.IntValue maxMemorySize; + public ForgeConfigSpec.IntValue maxHardDriveSize; + public ForgeConfigSpec.IntValue maxFlashMemorySize; + + public CommonSettings(final ForgeConfigSpec.Builder builder) { + builder.push("vm"); + + maxAllocatedMemory = builder + .translation(Constants.CONFIG_MAX_ALLOCATED_MEMORY) + .comment("The maximum amount of memory that may be allocated to run virtual machines.") + .defineInRange("maxAllocatedMemory", Config.maxAllocatedMemory, 0L, 64L * Constants.GIGABYTE); + + maxMemorySize = builder + .translation(Constants.CONFIG_MAX_MEMORY_SIZE) + .comment("The maximum size of a single memory device.") + .defineInRange("maxMemorySize", Config.maxMemorySize, 0, 256 * Constants.MEGABYTE); + + maxHardDriveSize = builder + .translation(Constants.CONFIG_MAX_HARD_DRIVE_SIZE) + .comment("The maximum size of a single hard drive device.") + .defineInRange("maxHardDriveSize", Config.maxHardDriveSize, 0, 512 * Constants.MEGABYTE); + + maxFlashMemorySize = builder + .translation(Constants.CONFIG_MAX_FLASH_MEMORY_SIZE) + .comment("The maximum size of a single flash memory device.") + .defineInRange("maxFlashMemorySize", Config.maxFlashMemorySize, 0, 128 * Constants.MEGABYTE); + + builder.pop(); + } + } } diff --git a/src/main/java/li/cil/oc2/common/Constants.java b/src/main/java/li/cil/oc2/common/Constants.java index 64834830..be7c24e3 100644 --- a/src/main/java/li/cil/oc2/common/Constants.java +++ b/src/main/java/li/cil/oc2/common/Constants.java @@ -3,6 +3,7 @@ package li.cil.oc2.common; public final class Constants { public static final int KILOBYTE = 1024; public static final int MEGABYTE = 1024 * KILOBYTE; + public static final int GIGABYTE = 1024 * MEGABYTE; /////////////////////////////////////////////////////////////////// @@ -27,6 +28,13 @@ public final class Constants { /////////////////////////////////////////////////////////////////// + public static final String CONFIG_MAX_ALLOCATED_MEMORY = "config.oc2.maxAllocatedMemory"; + public static final String CONFIG_MAX_MEMORY_SIZE = "config.oc2.maxMemorySize"; + public static final String CONFIG_MAX_HARD_DRIVE_SIZE = "config.oc2.maxHardDriveSize"; + public static final String CONFIG_MAX_FLASH_MEMORY_SIZE = "config.oc2.maxFlashMemorySize"; + + /////////////////////////////////////////////////////////////////// + public static final String COMPUTER_SCREEN_CAPTURE_INPUT_CAPTION = "gui.oc2.computer.capture_input.capt"; public static final String COMPUTER_SCREEN_CAPTURE_INPUT_DESCRIPTION = "gui.oc2.computer.capture_input.desc"; public static final String COMPUTER_SCREEN_POWER_CAPTION = "gui.oc2.computer.power.capt"; diff --git a/src/main/java/li/cil/oc2/common/OpenComputers.java b/src/main/java/li/cil/oc2/common/OpenComputers.java index 2b77d72c..fc15918c 100644 --- a/src/main/java/li/cil/oc2/common/OpenComputers.java +++ b/src/main/java/li/cil/oc2/common/OpenComputers.java @@ -3,7 +3,6 @@ package li.cil.oc2.common; import li.cil.ceres.Ceres; import li.cil.oc2.api.API; import li.cil.oc2.client.ClientSetup; -import li.cil.oc2.common.CommonSetup; import li.cil.oc2.common.block.Blocks; import li.cil.oc2.common.bus.device.DeviceTypes; import li.cil.oc2.common.bus.device.data.BaseBlockDevices; @@ -19,6 +18,16 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @Mod(API.MOD_ID) public final class OpenComputers { public OpenComputers() { + // Do class lookup in a separate thread to avoid blocking for too long. + // Specifically, this is to run detection of annotated types via the Reflections + // library in the serialization library and the device tree registry. + new Thread(() -> { + Ceres.initialize(); + DeviceTreeRegistry.initialize(); + }).start(); + + Config.initialize(); + Items.initialize(); Blocks.initialize(); TileEntities.initialize(); @@ -30,13 +39,5 @@ public final class OpenComputers { FMLJavaModLoadingContext.get().getModEventBus().addListener(CommonSetup::run); FMLJavaModLoadingContext.get().getModEventBus().addListener(ClientSetup::run); - - // Do class lookup in a separate thread to avoid blocking for too long. - // Specifically, this is to run detection of annotated types via the Reflections - // library in the serialization library and the device tree registry. - new Thread(() -> { - Ceres.initialize(); - DeviceTreeRegistry.initialize(); - }).start(); } } diff --git a/src/main/java/li/cil/oc2/common/vm/Allocator.java b/src/main/java/li/cil/oc2/common/vm/Allocator.java index 72101ffd..a024b39a 100644 --- a/src/main/java/li/cil/oc2/common/vm/Allocator.java +++ b/src/main/java/li/cil/oc2/common/vm/Allocator.java @@ -94,7 +94,7 @@ public final class Allocator { if (size < 0) { throw new IllegalArgumentException(); } - return Config.maxAllocatedData - size >= allocated; + return Config.maxAllocatedMemory - size >= allocated; } private static final class Allocation {