Skip to content

Commit

Permalink
Introduce the first semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed May 9, 2024
1 parent a5467f0 commit d0e9566
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.faforever.commons.replay;

import com.faforever.commons.replay.body.ReplayBody;
import com.faforever.commons.replay.body.Event;
import com.faforever.commons.replay.header.ReplayHeader;
import com.faforever.commons.replay.semantics.records.TrackedEvent;

public record ReplayContainer(ReplayMetadata metadata, ReplayHeader header, ReplayBody body) {
import java.util.List;

public record ReplayContainer(ReplayMetadata metadata, ReplayHeader header, List<TrackedEvent> trackedEvents) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.faforever.commons.replay;

import com.faforever.commons.replay.body.ReplayBodyEvent;
import com.faforever.commons.replay.semantics.ChatMessage;
import com.faforever.commons.replay.semantics.ModeratorEvent;
import com.faforever.commons.replay.body.Event;
import com.faforever.commons.replay.semantics.records.ChatMessage;
import com.faforever.commons.replay.semantics.records.ModeratorEvent;
import com.faforever.commons.replay.shared.LuaTable;
import com.faforever.commons.replay.body.ReplayBodyParser;
import com.faforever.commons.replay.body.ReplayBodyToken;
Expand Down Expand Up @@ -72,7 +72,7 @@ public class ReplayDataParser {
private List<ReplayBodyToken> tokens;

@Getter
private List<ReplayBodyEvent> events;
private List<Event> events;

public ReplayDataParser(Path path, ObjectMapper objectMapper) throws IOException, CompressorException {
this.path = path;
Expand Down Expand Up @@ -209,38 +209,38 @@ private void parseHeader(LittleEndianDataInputStream dataStream) throws IOExcept
randomSeed = dataStream.readInt();
}

private void interpretEvents(List<ReplayBodyEvent> events) {
private void interpretEvents(List<Event> events) {
Integer player = -1;
boolean desync = false;
String previousChecksum = null;
int previousTick = -1;

Map<Integer, Integer> lastTicks = new HashMap<>();

for (ReplayBodyEvent event : events) {
for (Event event : events) {

switch (event) {
case ReplayBodyEvent.Unprocessed(ReplayBodyToken token, String reason) -> {
case Event.Unprocessed(ReplayBodyToken token, String reason) -> {

}

case ReplayBodyEvent.ProcessingError(ReplayBodyToken token, Exception exception) -> {
case Event.ProcessingError(ReplayBodyToken token, Exception exception) -> {

}

case ReplayBodyEvent.Advance(int ticksToAdvance) -> {
case Event.Advance(int ticksToAdvance) -> {
ticks += ticksToAdvance;
}

case ReplayBodyEvent.SetCommandSource(int playerIndex) -> {
case Event.SetCommandSource(int playerIndex) -> {
player = playerIndex;
}

case ReplayBodyEvent.CommandSourceTerminated() -> {
case Event.CommandSourceTerminated() -> {
lastTicks.put(player, ticks);
}

case ReplayBodyEvent.VerifyChecksum(String hash, int tick) -> {
case Event.VerifyChecksum(String hash, int tick) -> {
desync = tick == previousTick && !Objects.equals(previousChecksum, hash);
previousChecksum = hash;
previousTick = ticks;
Expand All @@ -251,103 +251,103 @@ private void interpretEvents(List<ReplayBodyEvent> events) {
}
}

case ReplayBodyEvent.RequestPause() -> {
case Event.RequestPause() -> {

}

case ReplayBodyEvent.RequestResume() -> {
case Event.RequestResume() -> {

}

case ReplayBodyEvent.SingleStep() -> {
case Event.SingleStep() -> {

}

case ReplayBodyEvent.CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) -> {
case Event.CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) -> {

}

case ReplayBodyEvent.CreateProp(String blueprintId, float px, float pz, float heading) -> {
case Event.CreateProp(String blueprintId, float px, float pz, float heading) -> {

}

case ReplayBodyEvent.DestroyEntity(int entityId) -> {
case Event.DestroyEntity(int entityId) -> {

}

case ReplayBodyEvent.WarpEntity(int entityId, float px, float py, float pz) -> {
case Event.WarpEntity(int entityId, float px, float py, float pz) -> {

}

case ReplayBodyEvent.ProcessInfoPair(int entityId, String arg1, String arg2) -> {
case Event.ProcessInfoPair(int entityId, String arg1, String arg2) -> {

}

case ReplayBodyEvent.IssueCommand(
ReplayBodyEvent.CommandUnits commandUnits, ReplayBodyEvent.CommandData commandData
case Event.IssueCommand(
Event.CommandUnits commandUnits, Event.CommandData commandData
) -> {
commandsPerMinuteByPlayer.computeIfAbsent(player, p -> new HashMap<>()).computeIfAbsent(ticks, t -> new AtomicInteger()).incrementAndGet();
}

case ReplayBodyEvent.IssueFactoryCommand(
ReplayBodyEvent.CommandUnits commandUnits, ReplayBodyEvent.CommandData commandData
case Event.IssueFactoryCommand(
Event.CommandUnits commandUnits, Event.CommandData commandData
) -> {
commandsPerMinuteByPlayer.computeIfAbsent(player, p -> new HashMap<>()).computeIfAbsent(ticks, t -> new AtomicInteger()).incrementAndGet();
}

case ReplayBodyEvent.IncreaseCommandCount(int commandId, int delta) -> {
case Event.IncreaseCommandCount(int commandId, int delta) -> {

}

case ReplayBodyEvent.DecreaseCommandCount(int commandId, int delta) -> {
case Event.DecreaseCommandCount(int commandId, int delta) -> {

}

case ReplayBodyEvent.SetCommandTarget(int commandId, ReplayBodyEvent.CommandTarget commandTarget) -> {
case Event.SetCommandTarget(int commandId, Event.CommandTarget commandTarget) -> {

}

case ReplayBodyEvent.SetCommandType(int commandId, int targetId) -> {
case Event.SetCommandType(int commandId, int targetId) -> {

}

case ReplayBodyEvent.SetCommandCells(int commandId, Object parametersLua, float px, float py, float pz) -> {
case Event.SetCommandCells(int commandId, Object parametersLua, float px, float py, float pz) -> {

}

case ReplayBodyEvent.RemoveCommandFromQueue(int commandId, int unitId) -> {
case Event.RemoveCommandFromQueue(int commandId, int unitId) -> {

}

case ReplayBodyEvent.DebugCommand(
String command, float px, float py, float pz, byte focusArmy, ReplayBodyEvent.CommandUnits units
case Event.DebugCommand(
String command, float px, float py, float pz, byte focusArmy, Event.CommandUnits units
) -> {

}

case ReplayBodyEvent.ExecuteLuaInSim(String luaCode) -> {
case Event.ExecuteLuaInSim(String luaCode) -> {

}

case ReplayBodyEvent.LuaSimCallback(
String func, LuaTable.Table parametersLua, ReplayBodyEvent.CommandUnits commandUnits
case Event.LuaSimCallback(
String func, LuaTable.Table parametersLua, Event.CommandUnits commandUnits
) when func.equals("GiveResourcesToPlayer") -> {
parseGiveResourcesToPlayer(parametersLua);
}

case ReplayBodyEvent.LuaSimCallback(
String func, LuaTable.Table parametersLua, ReplayBodyEvent.CommandUnits commandUnits
case Event.LuaSimCallback(
String func, LuaTable.Table parametersLua, Event.CommandUnits commandUnits
) when func.equals("ModeratorEvent") -> {
parseModeratorEvent(parametersLua, player);
}

case ReplayBodyEvent.LuaSimCallback(
String func, LuaTable parametersLua, ReplayBodyEvent.CommandUnits commandUnits
case Event.LuaSimCallback(
String func, LuaTable parametersLua, Event.CommandUnits commandUnits
) -> {

}

case ReplayBodyEvent.EndGame() -> {
case Event.EndGame() -> {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.faforever.commons.replay;

import com.faforever.commons.replay.body.ReplayBody;
import com.faforever.commons.replay.body.Event;
import com.faforever.commons.replay.body.ReplayBodyParser;
import com.faforever.commons.replay.body.ReplayBodyToken;
import com.faforever.commons.replay.body.ReplayBodyTokenizer;
import com.faforever.commons.replay.header.ReplayHeader;
import com.faforever.commons.replay.header.ReplayHeaderParser;
import com.faforever.commons.replay.header.ReplayHeaderToken;
import com.faforever.commons.replay.header.ReplayHeaderTokenizer;
import com.faforever.commons.replay.semantics.Semantics;
import com.faforever.commons.replay.semantics.records.TrackedEvent;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.google.common.io.BaseEncoding;
import com.google.common.io.LittleEndianDataInputStream;
Expand Down Expand Up @@ -39,16 +41,17 @@ private static ReplayHeader loadSCFAReplayHeader(LittleEndianDataInputStream str
}

@Contract(pure = true)
private static @NotNull ReplayBody loadSCFAReplayBody(LittleEndianDataInputStream stream) throws IOException{
private static @NotNull List<TrackedEvent> loadSCFAReplayBody(LittleEndianDataInputStream stream) throws IOException{
List<ReplayBodyToken> bodyTokens = ReplayBodyTokenizer.tokenize(stream);
return new ReplayBody(ReplayBodyParser.parseTokens(bodyTokens));
List<Event> bodyEvents = ReplayBodyParser.parseTokens(bodyTokens);
return Semantics.registerEvents(bodyEvents);
}

@Contract(pure = true)
private static ReplayContainer loadSCFAReplayFromMemory(ReplayMetadata metadata, byte[] scfaReplayBytes) throws IOException {
try (LittleEndianDataInputStream stream = new LittleEndianDataInputStream((new ByteArrayInputStream(scfaReplayBytes)))) {
ReplayHeader replayHeader = loadSCFAReplayHeader(stream);
ReplayBody replayBody = loadSCFAReplayBody(stream);
List<TrackedEvent> replayBody = loadSCFAReplayBody(stream);

if(stream.available() > 0) {
throw new EOFException();
Expand Down
Loading

0 comments on commit d0e9566

Please sign in to comment.