From a6b0b194c2c507a4bd5532b3f8834875f021988f Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Thu, 11 Apr 2024 11:40:11 -0700 Subject: [PATCH] move the ROM images into datapack --- .../net/brokenmoon/redcontrol/RedControl.java | 33 ++++++++++++++++++ .../brokenmoon/redcontrol/api/Emulator.java | 7 +--- .../redcontrol/blockentities/DriveEntity.java | 8 ++--- .../redcontrol/image/redforth.img | Bin .../redcontrol/image/rpcboot.bin | Bin .../redcontrol/image/slideshow.img | Bin 6 files changed, 37 insertions(+), 11 deletions(-) rename src/main/resources/{assets => data}/redcontrol/image/redforth.img (100%) rename src/main/resources/{assets => data}/redcontrol/image/rpcboot.bin (100%) rename src/main/resources/{assets => data}/redcontrol/image/slideshow.img (100%) diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControl.java b/src/main/java/net/brokenmoon/redcontrol/RedControl.java index e3fd360..7c72688 100644 --- a/src/main/java/net/brokenmoon/redcontrol/RedControl.java +++ b/src/main/java/net/brokenmoon/redcontrol/RedControl.java @@ -11,20 +11,30 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; +import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.BlockItem; import net.minecraft.registry.Registry; import net.minecraft.registry.Registries; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.resource.ResourceType; import net.minecraft.util.Identifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + public class RedControl implements ModInitializer { // This logger is used to write text to the console and the log file. // It is considered best practice to use your mod id as the logger's name. // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("redcontrol"); + public static final HashMap images = new HashMap<>(); //Blocks public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f)); @@ -43,6 +53,29 @@ public class RedControl implements ModInitializer { // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. + ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() { + @Override + public Identifier getFabricId() { + return Identifier.of("redcontrol","image"); + } + + @Override + public void reload(ResourceManager manager) { + images.clear(); //remove all previous images + for(Map.Entry entry : manager.findResources("image", path -> true).entrySet()) { + try(InputStream stream = entry.getValue().getInputStream();) { + String[] split = entry.getKey().getPath().split("/"); + String file = split[split.length-1]; + LOGGER.info("Found image {}",file); + images.put(file,stream.readAllBytes()); + } catch(Exception e) { + LOGGER.error("Error occurred while loading resource json {}", entry.getKey().toString(), e); + } + + } + } + }); + LOGGER.info("Initializing RedControl!"); Registry.register(Registries.BLOCK, new Identifier("redcontrol", "cpu"), CPU); Registry.register(Registries.BLOCK, new Identifier("redcontrol", "monitor"), MONITOR); diff --git a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java index a843ae7..dd88ca7 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java @@ -24,12 +24,7 @@ public class Emulator { cpu.setLogCallback(new LogConsumer()); cpu.setBus(new Bus(bus)); ram = new Memory(0x0000, 0x2000 - 1, ((CpuAccessor)cpu).getRedBusState()); - try { - ram.loadFromFile(Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin"),0x400, 0x100); - - } catch (IOException e) { - throw new RuntimeException(e); - } + ram.loadFromBytes(RedControl.images.get("rpcboot.bin"),0x400,0x100); cpu.getBus().addDevice(ram, ((CpuAccessor)cpu).getRedBusState()); cpu.reset(); ram.write(0, 2, ((CpuAccessor)cpu).getRedBusState()); diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java index a53dd4a..92f8a81 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java @@ -1,5 +1,6 @@ package net.brokenmoon.redcontrol.blockentities; +import com.simon816.j65el02.device.ByteDiskDriver; import com.simon816.j65el02.device.DiskDriver; import com.simon816.j65el02.device.FileDiskDriver; import net.brokenmoon.redcontrol.RedControl; @@ -26,11 +27,7 @@ public class DriveEntity extends Peripheral{ public DriveEntity(BlockPos pos, BlockState state) { super(RedControl.DRIVE_BLOCK_ENTITY, pos, state, 2); - try { - this.driver = new FileDiskDriver(Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/redforth.img"), "Forth", "FORTH", false); - } catch (IOException e) { - throw new RuntimeException(e); - } + this.driver = new ByteDiskDriver(RedControl.images.get("redforth.img"), "Forth", "FORTH"); this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE); byte[] name = driver.getDriveName(); byte[] serial = driver.getDriveSerial(); @@ -118,6 +115,7 @@ public class DriveEntity extends Peripheral{ } } catch (Exception e) { this.command = 0xff; + e.printStackTrace(); } } } diff --git a/src/main/resources/assets/redcontrol/image/redforth.img b/src/main/resources/data/redcontrol/image/redforth.img similarity index 100% rename from src/main/resources/assets/redcontrol/image/redforth.img rename to src/main/resources/data/redcontrol/image/redforth.img diff --git a/src/main/resources/assets/redcontrol/image/rpcboot.bin b/src/main/resources/data/redcontrol/image/rpcboot.bin similarity index 100% rename from src/main/resources/assets/redcontrol/image/rpcboot.bin rename to src/main/resources/data/redcontrol/image/rpcboot.bin diff --git a/src/main/resources/assets/redcontrol/image/slideshow.img b/src/main/resources/data/redcontrol/image/slideshow.img similarity index 100% rename from src/main/resources/assets/redcontrol/image/slideshow.img rename to src/main/resources/data/redcontrol/image/slideshow.img