Skip to content

Commit

Permalink
Merge remote-tracking branch '1.11/1.11.X' into 1.12.X
Browse files Browse the repository at this point in the history
  • Loading branch information
boq committed Dec 27, 2017
2 parents 0ddeb7e + f71ba56 commit c5841bd
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 51 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ def fmlManifest = manifest {
)
}

if (project.hasProperty('keyStore')) {
task signJar(type: SignJar, dependsOn: reobfJar) {
keyStore = project.keyStore
alias = project.keyStoreAlias
storePass = project.keyStorePass
keyPass = project.keyStoreKeyPass

inputFile = jar.archivePath
outputFile = jar.archivePath
}

build.dependsOn signJar
}

jar {
manifest {
from jenkinsManifest, gitManifest, fmlManifest
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/openmods/OpenMods.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import openmods.world.DelayedActionTickHandler;
import openmods.world.DropCapture;

@Mod(modid = OpenMods.MODID, name = OpenMods.MODID, version = OpenMods.VERSION, dependencies = OpenMods.DEPENDENCIES, guiFactory = OpenMods.GUI_FACTORY, updateJSON = OpenMods.UPDATE_JSON)
@Mod(modid = OpenMods.MODID, name = OpenMods.MODID, version = OpenMods.VERSION, dependencies = OpenMods.DEPENDENCIES, guiFactory = OpenMods.GUI_FACTORY, updateJSON = OpenMods.UPDATE_JSON, certificateFingerprint = OpenMods.CERTIFICATE_FINGERPRINT)
public class OpenMods {

public static final String MODID = "openmods";
Expand All @@ -73,6 +73,7 @@ public class OpenMods {
public static final String DEPENDENCIES = "required-after:openmodscore";
public static final String GUI_FACTORY = "openmods.GuiFactory";
public static final String UPDATE_JSON = "http://openmods.info/versions/openmodslib.json";
public static final String CERTIFICATE_FINGERPRINT = "d2a9a8e8440196e26a268d1f3ddc01b2e9c572a5";

private static final int ENTITY_BLOCK_ID = 804;

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/openmods/sync/SyncMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ public SyncFieldException(Throwable cause, int index) {
}
}

// Note: try... methods are non-throwing variants of read/write methods
// They are convenient when methods can be called via client-side HUD modifiers

public abstract void read(NBTTagCompound tag);

public abstract boolean tryRead(NBTTagCompound tag);

public abstract void write(NBTTagCompound tag);

public abstract void safeWrite(NBTTagCompound tag);
public abstract boolean tryWrite(NBTTagCompound tag);

public abstract void readIntializationData(PacketBuffer dis) throws IOException;

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/openmods/sync/SyncMapClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ public void read(NBTTagCompound tag) {
throw new UnsupportedOperationException();
}

@Override
public boolean tryRead(NBTTagCompound tag) {
return false; // NO-OP
}

@Override
public void write(NBTTagCompound tag) {
throw new UnsupportedOperationException();
}

@Override
public void safeWrite(NBTTagCompound tag) {
// NO-OP
public boolean tryWrite(NBTTagCompound tag) {
return false; // NO-OP
}

@Override
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/openmods/sync/SyncMapEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class SyncMapEntity extends SyncMapServer {

private final Entity owner;

public SyncMapEntity(Entity owner) {
public SyncMapEntity(Entity owner, UpdateStrategy strategy) {
super(strategy);
this.owner = owner;
}

Expand Down Expand Up @@ -48,9 +49,4 @@ protected Set<EntityPlayerMP> getPlayersWatching() {
protected boolean isInvalid() {
return owner.isDead;
}

@Override
protected IUpdateStrategy createUpdateStrategy() {
return new SendInitialPacketStrategy();
}
}
99 changes: 68 additions & 31 deletions src/main/java/openmods/sync/SyncMapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ public Entry(String name, ISyncableObject obj) {

private final Map<ISyncableObject, Integer> objectToId = Maps.newIdentityHashMap();

private boolean firstDataSent = false;
private boolean firstRemoteObjectInitialized = false;

private int bitmapLength;

private final IUpdateStrategy updateStrategy;

public SyncMapServer(UpdateStrategy strategy) {
this.updateStrategy = strategy.create(this);
}

@Override
public void registerObject(String name, ISyncableObject value) {
Preconditions.checkState(!firstDataSent, "Can't add fields to object that has already sent data to clients");
Preconditions.checkState(!firstRemoteObjectInitialized, "Can't add fields to object that has already sent data to clients");

{
final ISyncableObject prev = objects.put(name, value);
Expand Down Expand Up @@ -76,6 +82,12 @@ public void read(NBTTagCompound tag) {
}
}

@Override
public boolean tryRead(NBTTagCompound tag) {
read(tag);
return true;
}

@Override
public void write(NBTTagCompound tag) {
for (Map.Entry<String, ISyncableObject> entry : objects.entrySet()) {
Expand All @@ -90,8 +102,9 @@ public void write(NBTTagCompound tag) {
}

@Override
public void safeWrite(NBTTagCompound tag) {
public boolean tryWrite(NBTTagCompound tag) {
write(tag);
return true;
}

@Override
Expand All @@ -106,8 +119,17 @@ public void readUpdate(PacketBuffer dis) {

@Override
public void writeInitializationData(PacketBuffer dos) throws IOException {
if (!firstDataSent) {
firstDataSent = true;
updateStrategy.writeInitializationData(dos);
}

private void writeOwnerInfo(PacketBuffer dos) {
dos.writeVarInt(getOwnerType());
writeOwnerData(dos);
}

private void writeSyncObjectInitialization(PacketBuffer dos) throws IOException {
if (!firstRemoteObjectInitialized) {
firstRemoteObjectInitialized = true;
bitmapLength = (objects.size() + 7) / 8;
}

Expand All @@ -123,15 +145,8 @@ public void writeInitializationData(PacketBuffer dos) throws IOException {
}
}

private void writeInitialDataWithPrefix(PacketBuffer dos) throws IOException {
writePrefix(dos);
writeInitializationData(dos);
}

private void writeUpdatePacket(PacketBuffer dos, Set<ISyncableObject> changes) throws IOException {
Preconditions.checkState(firstDataSent, "Initial data not sent to clients");

writePrefix(dos);
Preconditions.checkState(firstRemoteObjectInitialized, "Remote objects not intialized yet");

final ByteBuf bitmapData = dos.slice(dos.writerIndex(), bitmapLength);
bitmapData.clear();
Expand All @@ -156,20 +171,15 @@ public void acceptByte(int b) {
bitmap.flush();
}

private void writePrefix(PacketBuffer dos) {
dos.writeVarInt(getOwnerType());
writeOwnerData(dos);
}

protected interface IUpdateStrategy {
public void sendUpdates(Set<ISyncableObject> changedObjects);

public boolean sendsFirstPacket();
}
public void writeInitializationData(PacketBuffer dos) throws IOException;

private final IUpdateStrategy updateStrategy = createUpdateStrategy();
public boolean canSendUpdates();
}

protected class AutomaticInitialPacketStrategy implements IUpdateStrategy {
private class SeparateInitializationPacketStrategy implements IUpdateStrategy {

@Override
public void sendUpdates(Set<ISyncableObject> changedObjects) {
Expand All @@ -179,22 +189,27 @@ public void sendUpdates(Set<ISyncableObject> changedObjects) {

try {
final PacketBuffer deltaPayload = new PacketBuffer(Unpooled.buffer());
writeOwnerInfo(deltaPayload);
writeUpdatePacket(deltaPayload, changedObjects);
SyncChannelHolder.INSTANCE.sendPayloadToPlayers(deltaPayload, players);
} catch (IOException e) {
Log.warn(e, "IOError during delta sync");
}

}

@Override
public boolean sendsFirstPacket() {
return false;
public void writeInitializationData(PacketBuffer dos) throws IOException {
// owner info not required, as initialization packet is assumed to already be directed
writeSyncObjectInitialization(dos);
}

@Override
public boolean canSendUpdates() {
return firstRemoteObjectInitialized;
}
}

protected class SendInitialPacketStrategy implements IUpdateStrategy {
private class SelfInitializingUpdateStrategy implements IUpdateStrategy {

private Set<Integer> knownUsers = Sets.newHashSet();

Expand All @@ -218,6 +233,7 @@ public void sendUpdates(Set<ISyncableObject> changes) {
try {
if (!deltaPacketTargets.isEmpty()) {
final PacketBuffer deltaPayload = new PacketBuffer(Unpooled.buffer());
writeOwnerInfo(deltaPayload);
writeUpdatePacket(deltaPayload, changes);
SyncChannelHolder.INSTANCE.sendPayloadToPlayers(deltaPayload, deltaPacketTargets);
}
Expand All @@ -228,7 +244,8 @@ public void sendUpdates(Set<ISyncableObject> changes) {
try {
if (!fullPacketTargets.isEmpty()) {
final PacketBuffer fullPayload = new PacketBuffer(Unpooled.buffer());
writeInitialDataWithPrefix(fullPayload);
writeOwnerInfo(fullPayload);
writeSyncObjectInitialization(fullPayload);
SyncChannelHolder.INSTANCE.sendPayloadToPlayers(fullPayload, fullPacketTargets);
}
} catch (IOException e) {
Expand All @@ -237,10 +254,32 @@ public void sendUpdates(Set<ISyncableObject> changes) {
}

@Override
public boolean sendsFirstPacket() {
public void writeInitializationData(PacketBuffer dos) {
// use other strategy, if you want to send update packet
throw new UnsupportedOperationException();
}

@Override
public boolean canSendUpdates() {
return true;
}
}

public enum UpdateStrategy {
WITHOUT_INITIAL_PACKET {
@Override
protected IUpdateStrategy create(SyncMapServer owner) {
return owner.new SelfInitializingUpdateStrategy();
}
},
WITH_INITIAL_PACKET {
@Override
protected IUpdateStrategy create(SyncMapServer owner) {
return owner.new SeparateInitializationPacketStrategy();
}
};

protected abstract IUpdateStrategy create(SyncMapServer owner);
}

private Set<ISyncableObject> listChanges() {
Expand Down Expand Up @@ -273,7 +312,7 @@ public void removeUpdateListener(ISyncListener dispatcher) {

@Override
public void sendUpdates() {
if (isInvalid() || (!updateStrategy.sendsFirstPacket() && !firstDataSent)) return;
if (isInvalid() || !updateStrategy.canSendUpdates()) return;

final Set<ISyncableObject> changedObjects = listChanges();
updateStrategy.sendUpdates(changedObjects);
Expand Down Expand Up @@ -305,8 +344,6 @@ public int getObjectId(ISyncableObject object) {
return result;
}

protected abstract IUpdateStrategy createUpdateStrategy();

protected abstract int getOwnerType();

protected abstract void writeOwnerData(PacketBuffer output);
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/openmods/sync/SyncMapTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class SyncMapTile extends SyncMapServer {

private final SyncedTileEntity owner;

public SyncMapTile(SyncedTileEntity owner) {
public SyncMapTile(SyncedTileEntity owner, UpdateStrategy strategy) {
super(strategy);
this.owner = owner;
}

Expand Down Expand Up @@ -52,10 +53,4 @@ protected Set<EntityPlayerMP> getPlayersWatching() {
protected boolean isInvalid() {
return owner.isInvalid();
}

@Override
protected IUpdateStrategy createUpdateStrategy() {
return new AutomaticInitialPacketStrategy();
}

}
7 changes: 4 additions & 3 deletions src/main/java/openmods/tileentity/SyncedTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import openmods.sync.ISyncableObject;
import openmods.sync.SyncMap;
import openmods.sync.SyncMapClient;
import openmods.sync.SyncMapServer.UpdateStrategy;
import openmods.sync.SyncMapTile;
import openmods.sync.SyncObjectScanner;
import openmods.sync.drops.DropTagSerializer;
Expand All @@ -40,7 +41,7 @@ public SyncedTileEntity() {
}

private void createSyncMap(World world) {
final SyncMap syncMap = world.isRemote? new SyncMapClient() : new SyncMapTile(this);
final SyncMap syncMap = world.isRemote? new SyncMapClient() : new SyncMapTile(this, UpdateStrategy.WITH_INITIAL_PACKET);

SyncObjectScanner.INSTANCE.registerAllFields(syncMap, this);

Expand Down Expand Up @@ -125,14 +126,14 @@ public SyncMap getSyncMap() {
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
getSyncMap().safeWrite(tag);
getSyncMap().tryWrite(tag);
return tag;
}

@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
getSyncMap().read(tag);
getSyncMap().tryRead(tag);
}

private NBTTagCompound serializeInitializationData(NBTTagCompound tag) {
Expand Down

0 comments on commit c5841bd

Please sign in to comment.