ReadI/WriteI interface for modding (and removal of if/else tree) (#11000)

* progress

* progress

* moar progress

* commits every 5 seconds poggers (done with LogicBuild)

* implement interface

* oop forgor to remove part of the old ReadI

* unnecessary import removed

* who needs a space? i got a spare one

* implement great suggestion by [object Object]

* more of this

* slipped past me

* more consistency
This commit is contained in:
1ue999
2025-07-10 05:20:35 +02:00
committed by GitHub
parent b327f8be1f
commit 26b349e5dd
7 changed files with 114 additions and 36 deletions

View File

@@ -146,7 +146,7 @@ public class CanvasBlock extends Block{
return result;
}
public class CanvasBuild extends Building{
public class CanvasBuild extends Building implements LReadable, LWritable{
public @Nullable Texture texture;
public byte[] data = new byte[Mathf.ceil(canvasSize * canvasSize * bitsPerPixel / 8f)];
public int blending;
@@ -226,6 +226,25 @@ public class CanvasBlock extends Block{
}
}
public boolean readable(LExecutor exec){
return exec.privileged || this.team == exec.team;
}
@Override
public void read(LVar position, LVar output){
output.setnum(getPixel(position.numi()));
}
@Override
public boolean writable(LExecutor exec){
return exec.privileged || this.team == exec.team;
}
@Override
public void write(LVar position, LVar value){
setPixel(position.numi(), value.numi());
}
boolean blends(Tile other){
return other != null && other.build != null && other.build.block == block && other.build.tileX() == other.x && other.build.tileY() == other.y;
}

View File

@@ -238,7 +238,7 @@ public class LogicBlock extends Block{
}
}
public class LogicBuild extends Building implements Ranged{
public class LogicBuild extends Building implements Ranged, LReadable, LWritable{
/** logic "source code" as list of asm statements */
public String code = "";
public LExecutor executor = new LExecutor();
@@ -544,6 +544,38 @@ public class LogicBlock extends Block{
}
}
@Override
public boolean readable(LExecutor exec){
return exec.privileged || (this.team == exec.team && !this.block.privileged);
}
@Override
public void read(LVar position, LVar output){
if(position.isobj && position.objval instanceof String varName){
LVar ret = executor.optionalVar(varName);
if(ret == null){
output.setnum(Double.NaN);
return;
}
if(output.constant) return;
output.set(ret);
}
}
@Override
public boolean writable(LExecutor exec){
return exec.privileged || (this.team == exec.team && !this.block.privileged);
}
@Override
public void write(LVar position, LVar value){
if(position.isobj && position.objval instanceof String varName){
LVar at = executor.optionalVar(varName);
if(at == null || at.constant) return;
at.set(value);
}
}
@Override
public byte[] config(){
return compress(code, relativeConnections());

View File

@@ -37,7 +37,7 @@ public class MemoryBlock extends Block{
return accessible();
}
public class MemoryBuild extends Building{
public class MemoryBuild extends Building implements LReadable, LWritable{
public double[] memory = new double[memoryCapacity];
//massive byte size means picking up causes sync issues
@@ -56,6 +56,29 @@ public class MemoryBlock extends Block{
return accessible();
}
public boolean readable(LExecutor exec){
return exec.privileged || (this.team == exec.team && !this.block.privileged);
}
@Override
public void read(LVar position, LVar output){
int address = position.numi();
//Return null when out of bounds. (instead of 0)
output.setnum(address < 0 || address >= memory.length ? Double.NaN : memory[address]);
}
@Override
public boolean writable(LExecutor exec){
return exec.privileged || (this.team == exec.team && !this.block.privileged);
}
@Override
public void write(LVar position, LVar value){
int address = position.numi();
if(address < 0 || address >= memory.length) return;
memory[address] = value.num();
}
@Override
public double sense(LAccess sensor){
return switch(sensor){

View File

@@ -68,7 +68,7 @@ public class MessageBlock extends Block{
return accessible();
}
public class MessageBuild extends Building{
public class MessageBuild extends Building implements LReadable{
public StringBuilder message = new StringBuilder();
@Override
@@ -166,6 +166,17 @@ public class MessageBlock extends Block{
return !accessible() ? SystemCursor.arrow : super.getCursor();
}
@Override
public boolean readable(LExecutor exec){
return true;
}
@Override
public void read(LVar position, LVar output){
int address = position.numi();
output.setnum(address < 0 || address >= message.length() ? Double.NaN : message.charAt(address));
}
@Override
public double sense(LAccess sensor){
return switch(sensor){