Fixed some multithreading bugs and crashes
This commit is contained in:
@@ -270,11 +270,19 @@ public class NetClient extends Module {
|
||||
Net.handleClient(FriendlyFireChangePacket.class, packet -> state.friendlyFire = packet.enabled);
|
||||
|
||||
Net.handleClient(ItemTransferPacket.class, packet -> {
|
||||
Tile tile = world.tile(packet.position);
|
||||
if(tile == null || tile.entity == null) return;
|
||||
Tile next = tile.getNearby(packet.rotation);
|
||||
tile.entity.items[packet.itemid] --;
|
||||
next.block().handleItem(Item.getByID(packet.itemid), next, tile);
|
||||
Runnable r = () -> {
|
||||
Tile tile = world.tile(packet.position);
|
||||
if (tile == null || tile.entity == null) return;
|
||||
Tile next = tile.getNearby(packet.rotation);
|
||||
tile.entity.items[packet.itemid]--;
|
||||
next.block().handleItem(Item.getByID(packet.itemid), next, tile);
|
||||
};
|
||||
|
||||
if(threads.isEnabled()){
|
||||
threads.run(r);
|
||||
}else{
|
||||
r.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.anuke.mindustry.core;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.util.Log;
|
||||
@@ -8,6 +9,7 @@ import io.anuke.ucore.util.Log;
|
||||
import static io.anuke.mindustry.Vars.logic;
|
||||
|
||||
public class ThreadHandler {
|
||||
private final Array<Runnable> toRun = new Array<>();
|
||||
private final ThreadProvider impl;
|
||||
private float delta = 1f;
|
||||
private long frame = 0;
|
||||
@@ -23,6 +25,12 @@ public class ThreadHandler {
|
||||
Timers.setDeltaProvider(() -> impl.isOnThread() ? delta : Gdx.graphics.getDeltaTime()*60f);
|
||||
}
|
||||
|
||||
public void run(Runnable r){
|
||||
synchronized (toRun) {
|
||||
toRun.add(r);
|
||||
}
|
||||
}
|
||||
|
||||
public int getFPS(){
|
||||
return (int)(60/delta);
|
||||
}
|
||||
@@ -70,6 +78,14 @@ public class ThreadHandler {
|
||||
try {
|
||||
while (true) {
|
||||
long time = TimeUtils.millis();
|
||||
|
||||
synchronized (toRun) {
|
||||
for(Runnable r : toRun){
|
||||
r.run();
|
||||
}
|
||||
toRun.clear();
|
||||
}
|
||||
|
||||
logic.update();
|
||||
|
||||
long elapsed = TimeUtils.timeSinceMillis(time);
|
||||
|
||||
@@ -255,10 +255,13 @@ public class Player extends SyncEntity{
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer data) {
|
||||
//TODO written angle is always 0
|
||||
|
||||
data.putFloat(x);
|
||||
data.putFloat(y);
|
||||
if(Net.client()) {
|
||||
data.putFloat(x);
|
||||
data.putFloat(y);
|
||||
}else{
|
||||
data.putFloat(interpolator.target.x);
|
||||
data.putFloat(interpolator.target.y);
|
||||
}
|
||||
data.putFloat(angle);
|
||||
data.putShort((short)health);
|
||||
data.put((byte)(dashing ? 1 : 0));
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.badlogic.gdx.utils.ObjectMap;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import io.anuke.mindustry.io.Platform;
|
||||
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
||||
import io.anuke.mindustry.net.Packet.UnimportantPacket;
|
||||
import io.anuke.mindustry.net.Packets.KickReason;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBegin;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBuilder;
|
||||
@@ -189,7 +190,7 @@ public class Net{
|
||||
if(clientLoaded || object instanceof ImportantPacket){
|
||||
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
|
||||
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
|
||||
}else{
|
||||
}else if(!(object instanceof UnimportantPacket)){
|
||||
packetQueue.add(object);
|
||||
Log.info("Queuing packet {0}.", ClassReflection.getSimpleName(object.getClass()));
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ public interface Packet {
|
||||
void write(ByteBuffer buffer);
|
||||
|
||||
interface ImportantPacket{}
|
||||
interface UnimportantPacket{}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.anuke.mindustry.entities.Player;
|
||||
import io.anuke.mindustry.entities.SyncEntity;
|
||||
import io.anuke.mindustry.io.Version;
|
||||
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
||||
import io.anuke.mindustry.net.Packet.UnimportantPacket;
|
||||
import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.ucore.entities.Entities;
|
||||
import io.anuke.ucore.entities.EntityGroup;
|
||||
@@ -502,7 +503,7 @@ public class Packets {
|
||||
public void read(ByteBuffer buffer) { }
|
||||
}
|
||||
|
||||
public static class ItemTransferPacket implements Packet{
|
||||
public static class ItemTransferPacket implements Packet, UnimportantPacket{
|
||||
public int position;
|
||||
public byte rotation;
|
||||
public byte itemid;
|
||||
|
||||
Reference in New Issue
Block a user