From 157b53e1c310e38b6a551eecbd000467b88ab857 Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Wed, 10 Apr 2024 15:18:30 -0700 Subject: [PATCH] replace submodule with walksanatora/J65el02, BlockModificationEvent, and switch to using accessors over making everything public --- .gitattributes | 2 +- .gitmodules | 3 +- .../brokenmoon/redcontrol/api/Emulator.java | 26 +++++++------ .../brokenmoon/redcontrol/api/TempBus.java | 4 +- .../api/events/BlockModificationEvents.java | 22 +++++++++++ .../redcontrol/blockentities/CpuEntity.java | 10 +---- .../redcontrol/blocks/CpuBlock.java | 3 ++ .../redcontrol/blocks/MonitorBlock.java | 1 + .../redcontrol/blocks/NetworkCarrier.java | 9 +++-- .../redcontrol/mixin/BusAccessor.java | 12 ++++++ .../redcontrol/mixin/CpuAccessor.java | 15 ++++++++ .../redcontrol/mixin/RedBusAccessor.java | 12 ++++++ .../redcontrol/mixin/WorldMixin.java | 37 +++++++++++++++++++ .../tags/blocks/bus_conductive.json | 3 ++ src/main/resources/redcontrol.mixins.json | 19 ++++++---- 15 files changed, 142 insertions(+), 36 deletions(-) create mode 100644 src/main/java/net/brokenmoon/redcontrol/api/events/BlockModificationEvents.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/mixin/BusAccessor.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/mixin/CpuAccessor.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/mixin/RedBusAccessor.java create mode 100644 src/main/java/net/brokenmoon/redcontrol/mixin/WorldMixin.java create mode 100644 src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json diff --git a/.gitattributes b/.gitattributes index 097f9f9..3310a80 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ -# + # https://help.github.com/articles/dealing-with-line-endings/ # # Linux start script should use lf diff --git a/.gitmodules b/.gitmodules index cfa1edf..8f57d55 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ + [submodule "J65el02"] path = J65el02 - url = git@git.broken-moon.net:astoriaFloyd/J65el02MC.git + url = git@github.com:walksanatora/J65el02.git diff --git a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java index ac2521c..4d14d66 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java @@ -1,14 +1,14 @@ package net.brokenmoon.redcontrol.api; -import com.simon816.j65el02.Bus; import com.simon816.j65el02.Cpu; +import com.simon816.j65el02.CpuState; import com.simon816.j65el02.device.Memory; import com.simon816.j65el02.device.RedBus; -import com.simon816.j65el02.device.RedBusState; import net.brokenmoon.redcontrol.RedControl; +import net.brokenmoon.redcontrol.mixin.BusAccessor; +import net.brokenmoon.redcontrol.mixin.CpuAccessor; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.file.Paths; public class Emulator { @@ -21,17 +21,17 @@ public class Emulator { this.bus = bus; cpu = new Cpu(); cpu.setBus(new TempBus(bus)); - ram = new Memory(0x0000, 0x2000 - 1, cpu.redBusState); + 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"),0x00, 0x400); ram.loadFromFile(Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/redforth.img"), 0x400, 0x100); - } catch (IOException e) { throw new RuntimeException(e); } - cpu.getBus().addDevice(ram, cpu.redBusState); + cpu.getBus().addDevice(ram, ((CpuAccessor)cpu).getRedBusState()); cpu.reset(); - ram.write(0, 2, cpu.redBusState); - ram.write(1, 1, cpu.redBusState); + ram.write(0, 2, ((CpuAccessor)cpu).getRedBusState()); + ram.write(1, 1, ((CpuAccessor)cpu).getRedBusState()); System.out.println("Emulator made"); } @@ -42,7 +42,7 @@ public class Emulator { public void setBus(RedBus bus) { RedControl.LOGGER.info("Setting Emulator bus"); this.bus = bus; - this.cpu.getBus().setRedBus(bus); + ((BusAccessor)this.cpu.getBus()).setRedBus(bus); } public boolean isWaitingOnInterrupt(){ @@ -55,14 +55,16 @@ public class Emulator { public void step() { if(!isWaitingOnInterrupt()) { + CpuState state = ((CpuAccessor)this.cpu).getState(); + System.out.println(state.toTraceEvent()); this.cpu.step(); - this.cpu.getBus().update(cpu.redBusState); + this.cpu.getBus().update(((CpuAccessor)cpu).getRedBusState()); } } public void reset(){ cpu.reset(); - ram.write(0, 2, cpu.redBusState); - ram.write(1, 1, cpu.redBusState); + ram.write(0, 2, ((CpuAccessor)cpu).getRedBusState()); + ram.write(1, 1, ((CpuAccessor)cpu).getRedBusState()); } } diff --git a/src/main/java/net/brokenmoon/redcontrol/api/TempBus.java b/src/main/java/net/brokenmoon/redcontrol/api/TempBus.java index 93b861c..f267675 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/TempBus.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/TempBus.java @@ -14,14 +14,14 @@ public class TempBus extends Bus { @Override public void write(int address, int data, RedBusState state) { Device device = findDevice(address,state); - RedControl.LOGGER.info("Writing to bus with device " + device + "at address " + (address - device.startAddress(state)) + " with data " + data + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset); + //RedControl.LOGGER.info("Writing to bus with device " + device + "at address " + (address - device.startAddress(state)) + " with data " + data + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset); device.write(address - device.startAddress(state) % 0x2000, data, state); } @Override public int read(int address, boolean cpuAccess, RedBusState state) { Device device = findDevice(address,state); - RedControl.LOGGER.info("Reading from bus with device " + device + "at address " + (address - device.startAddress(state)) + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset); + //RedControl.LOGGER.info("Reading from bus with device " + device + "at address " + (address - device.startAddress(state)) + " with target " + state.activeDeviceId + " and redbus offset of " + state.offset); return device.read(address - device.startAddress(state) % 0x2000, cpuAccess, state) & 0xff; } } diff --git a/src/main/java/net/brokenmoon/redcontrol/api/events/BlockModificationEvents.java b/src/main/java/net/brokenmoon/redcontrol/api/events/BlockModificationEvents.java new file mode 100644 index 0000000..3434c4b --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/api/events/BlockModificationEvents.java @@ -0,0 +1,22 @@ +package net.brokenmoon.redcontrol.api.events; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.block.BlockState; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.NotNull; + + +public class BlockModificationEvents { + /** + * this event is triggered when a blockstate is changed. + */ + public static Event ON_CHANGE = EventFactory.createArrayBacked(OnStateChange.class, callbacks -> (world,blockPos,oldState,newState) -> { + for (OnStateChange callback : callbacks) { + callback.onChange(world, blockPos,oldState,newState); + } + }); + public @FunctionalInterface interface OnStateChange {void onChange(@NotNull World world,@NotNull BlockPos blockPos,@NotNull BlockState oldState, @NotNull BlockState newState);} + +} diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 4efb519..8eb5f9e 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -18,12 +18,6 @@ public class CpuEntity extends Peripheral{ public boolean notTicked = true; public Emulator core = new Emulator(new TempRedBus()); - private Path bootloader; - - - { - bootloader = Paths.get("/home/astoria/code/java/mods/RedControl/src/main/resources/assets/redcontrol/image/rpcboot.bin"); - } private int defaultMonitorId = 1; private int defaultDriveId = 2; @@ -81,9 +75,7 @@ public class CpuEntity extends Peripheral{ } @Override - public void update() { - - } + public void update() {} @Override public void setBus(RCWorldBus bus){ diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index 0c3b2b0..dcdac96 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -42,12 +42,15 @@ public class CpuBlock extends NetworkCarrier { @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + super.onUse(state,world,pos,player,hand,hit); if (!world.isClient) { CpuEntity peripheral = (CpuEntity) world.getBlockEntity(pos); + player.sendMessage(Text.literal("Cpu Debug: "), false); player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false); player.sendMessage(Text.literal(String.valueOf(peripheral.core.cpu.getBus().getRedBus().hashCode())), false); player.sendMessage(Text.literal(String.valueOf(peripheral.i)), false); player.sendMessage(Text.literal(String.valueOf(peripheral.core.isWaitingOnInterrupt())), false); + player.sendMessage(Text.literal(String.valueOf(peripheral.core.isWaitingOnInterrupt())), false); } return ActionResult.SUCCESS; diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java index 197df5c..1449689 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java @@ -34,6 +34,7 @@ public class MonitorBlock extends NetworkCarrier { @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + super.onUse(state,world,pos,player,hand,hit); if (!world.isClient) { MonitorEntity monitor = (MonitorEntity) world.getBlockEntity(pos); player.sendMessage(Text.literal(String.valueOf(monitor.getBus().hashCode())), false); diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index 07946bb..8e03ac8 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -4,6 +4,7 @@ import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.api.RCWorldBus; import net.brokenmoon.redcontrol.api.TempRedBus; import net.brokenmoon.redcontrol.blockentities.Peripheral; +import net.brokenmoon.redcontrol.mixin.RedBusAccessor; import net.minecraft.block.*; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; @@ -83,11 +84,13 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (!world.isClient) { Peripheral peripheral = (Peripheral) world.getBlockEntity(pos); + player.sendMessage(Text.literal("Network Carrier Debug: "), false); player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().hashCode())), false); player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().hashCode())), false); - for(int i = 0; i < peripheral.getBus().getRedBus().peripherals.length; i++){ - if (peripheral.getBus().getRedBus().peripherals[i] != null){ - player.sendMessage(Text.literal(String.valueOf(peripheral.getBus().getRedBus().peripherals[i])), false); + RedBus.Peripheral[] peripherals = ((RedBusAccessor)peripheral.getBus().getRedBus()).getPeripherals(); + for (RedBus.Peripheral value : peripherals) { + if (value != null) { + player.sendMessage(Text.literal(String.valueOf(value)), false); } } } diff --git a/src/main/java/net/brokenmoon/redcontrol/mixin/BusAccessor.java b/src/main/java/net/brokenmoon/redcontrol/mixin/BusAccessor.java new file mode 100644 index 0000000..0ec7097 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/mixin/BusAccessor.java @@ -0,0 +1,12 @@ +package net.brokenmoon.redcontrol.mixin; + +import com.simon816.j65el02.Bus; +import com.simon816.j65el02.device.RedBus; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(Bus.class) +public interface BusAccessor { + @Accessor(value = "redBus",remap = false) + void setRedBus(RedBus redBus); +} diff --git a/src/main/java/net/brokenmoon/redcontrol/mixin/CpuAccessor.java b/src/main/java/net/brokenmoon/redcontrol/mixin/CpuAccessor.java new file mode 100644 index 0000000..1dc6f48 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/mixin/CpuAccessor.java @@ -0,0 +1,15 @@ +package net.brokenmoon.redcontrol.mixin; + +import com.simon816.j65el02.Cpu; +import com.simon816.j65el02.CpuState; +import com.simon816.j65el02.device.RedBusState; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(Cpu.class) +public interface CpuAccessor { + @Accessor(value = "redBusState",remap = false) + RedBusState getRedBusState(); + @Accessor(value = "state",remap = false) + CpuState getState(); +} diff --git a/src/main/java/net/brokenmoon/redcontrol/mixin/RedBusAccessor.java b/src/main/java/net/brokenmoon/redcontrol/mixin/RedBusAccessor.java new file mode 100644 index 0000000..212b4cc --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/mixin/RedBusAccessor.java @@ -0,0 +1,12 @@ +package net.brokenmoon.redcontrol.mixin; + +import com.simon816.j65el02.device.RedBus; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(RedBus.class) +public interface RedBusAccessor { + @Accessor(value = "peripherals",remap = false) + RedBus.Peripheral[] getPeripherals(); + +} diff --git a/src/main/java/net/brokenmoon/redcontrol/mixin/WorldMixin.java b/src/main/java/net/brokenmoon/redcontrol/mixin/WorldMixin.java new file mode 100644 index 0000000..5fdccb6 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/mixin/WorldMixin.java @@ -0,0 +1,37 @@ +package net.brokenmoon.redcontrol.mixin; + +import net.brokenmoon.redcontrol.api.events.BlockModificationEvents; +import net.minecraft.block.BlockState; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Debug; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Debug(export = true, print = true) +@Mixin(World.class) +public abstract class WorldMixin { + @Unique + private BlockState old; + + @Shadow + public BlockState getBlockState(BlockPos pos) {return null;} + + @Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("RETURN")) + private void hookBlockPlace(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable cir) { + World world = (World)(Object)this; + //IDEA says that this is unreachable but it is reachable. + if (cir.getReturnValue() && !world.isClient()) { + BlockModificationEvents.ON_CHANGE.invoker().onChange(world,pos,this.old,state); + } + } + + @Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("HEAD")) + private void hookBlockRemove(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable cir) { + this.old = getBlockState(pos); + } +} diff --git a/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json new file mode 100644 index 0000000..dc0dbf0 --- /dev/null +++ b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json @@ -0,0 +1,3 @@ +{ + "values": ["minecraft:glass"] +} \ No newline at end of file diff --git a/src/main/resources/redcontrol.mixins.json b/src/main/resources/redcontrol.mixins.json index bc7e005..abe32fb 100644 --- a/src/main/resources/redcontrol.mixins.json +++ b/src/main/resources/redcontrol.mixins.json @@ -1,11 +1,14 @@ { - "required": true, - "package": "net.brokenmoon.redcontrol.mixin", - "compatibilityLevel": "JAVA_17", - "mixins": [ - "ExampleMixin" - ], - "injectors": { - "defaultRequire": 1 + "required": true, + "package": "net.brokenmoon.redcontrol.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "BusAccessor", + "CpuAccessor", + "ExampleMixin", + "RedBusAccessor" + ], + "injectors": { + "defaultRequire": 1 } } \ No newline at end of file