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:
@@ -568,24 +568,15 @@ public class LExecutor{
|
||||
|
||||
@Override
|
||||
public void run(LExecutor exec){
|
||||
int address = position.numi();
|
||||
Building from = target.building();
|
||||
|
||||
if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged))){
|
||||
output.setnum(address < 0 || address >= mem.memory.length ? 0 : mem.memory[address]);
|
||||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){
|
||||
LVar fromVar = logic.executor.optionalVar(name);
|
||||
if(fromVar != null && !output.constant){
|
||||
output.objval = fromVar.objval;
|
||||
output.numval = fromVar.numval;
|
||||
output.isobj = fromVar.isobj;
|
||||
Object targetObj = target.obj();
|
||||
if(targetObj instanceof LReadable read){
|
||||
if(!read.readable(exec)) return;
|
||||
read.read(position, output);
|
||||
}else{
|
||||
int address = position.numi();
|
||||
if(targetObj instanceof CharSequence str){
|
||||
output.setnum(address < 0 || address >= str.length() ? Double.NaN : (int)str.charAt(address));
|
||||
}
|
||||
}else if(from instanceof MessageBuild msg){
|
||||
output.setnum(address < 0 || address >= msg.message.length() ? Double.NaN : (int)msg.message.charAt(address));
|
||||
}else if(target.isobj && target.objval instanceof CharSequence str){
|
||||
output.setnum(address < 0 || address >= str.length() ? Double.NaN : (int)str.charAt(address));
|
||||
}else if(from instanceof CanvasBuild canvas && (exec.privileged || (from.team == exec.team))){
|
||||
output.setnum(canvas.getPixel(address));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -604,20 +595,10 @@ public class LExecutor{
|
||||
|
||||
@Override
|
||||
public void run(LExecutor exec){
|
||||
int address = position.numi();
|
||||
Building from = target.building();
|
||||
|
||||
if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged)) && address >= 0 && address < mem.memory.length){
|
||||
mem.memory[address] = value.num();
|
||||
}else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){
|
||||
LVar toVar = logic.executor.optionalVar(name);
|
||||
if(toVar != null && !toVar.constant){
|
||||
toVar.objval = value.objval;
|
||||
toVar.numval = value.numval;
|
||||
toVar.isobj = value.isobj;
|
||||
}
|
||||
}else if(from instanceof CanvasBuild canvas && (exec.privileged || (from.team == exec.team))){
|
||||
canvas.setPixel(address, value.numi());
|
||||
Object targetObj = target.obj();
|
||||
if(targetObj instanceof LWritable write){
|
||||
if(!write.writable(exec)) return;
|
||||
write.write(position, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -660,7 +641,7 @@ public class LExecutor{
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(target instanceof CharSequence seq && sense == LAccess.size){
|
||||
if(target instanceof CharSequence seq && (sense == LAccess.size || sense == LAccess.bufferUsage)){
|
||||
to.setnum(seq.length());
|
||||
return;
|
||||
}
|
||||
|
||||
6
core/src/mindustry/logic/LReadable.java
Normal file
6
core/src/mindustry/logic/LReadable.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package mindustry.logic;
|
||||
|
||||
public interface LReadable{
|
||||
boolean readable(LExecutor exec);
|
||||
void read(LVar position, LVar output);
|
||||
}
|
||||
6
core/src/mindustry/logic/LWritable.java
Normal file
6
core/src/mindustry/logic/LWritable.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package mindustry.logic;
|
||||
|
||||
public interface LWritable{
|
||||
boolean writable(LExecutor exec);
|
||||
void write(LVar position, LVar value);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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){
|
||||
|
||||
@@ -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){
|
||||
|
||||
Reference in New Issue
Block a user