diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayBinaryFormat.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayBinaryFormat.java deleted file mode 100644 index b7877754..00000000 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayBinaryFormat.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.faforever.commons.replay; - -public sealed interface ReplayBinaryFormat { - - record WithContext (byte[] bytes) implements ReplayBinaryFormat {} - - record BinarySCFA(byte[] bytes) implements ReplayBinaryFormat {} - -} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayContainer.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayContainer.java index 3f0421dc..f5f33399 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayContainer.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayContainer.java @@ -1,7 +1,7 @@ package com.faforever.commons.replay; -import com.faforever.commons.replay.body.Body; -import com.faforever.commons.replay.header.Header; +import com.faforever.commons.replay.body.ReplayBody; +import com.faforever.commons.replay.header.ReplayHeader; -public record ReplayContainer(ReplayMetadata metadata, Header header, Body body, byte[] bytes) { +public record ReplayContainer(ReplayMetadata metadata, ReplayHeader header, ReplayBody body, byte[] bytes) { } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayDataParser.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayDataParser.java index 193550db..1d156370 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayDataParser.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayDataParser.java @@ -1,10 +1,12 @@ package com.faforever.commons.replay; -import com.faforever.commons.replay.body.parse.Event; +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.shared.LuaTable; -import com.faforever.commons.replay.body.parse.Parser; -import com.faforever.commons.replay.body.token.Token; -import com.faforever.commons.replay.body.token.Tokenizer; +import com.faforever.commons.replay.body.ReplayBodyParser; +import com.faforever.commons.replay.body.ReplayBodyToken; +import com.faforever.commons.replay.body.ReplayBodyTokenizer; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; import com.google.common.io.BaseEncoding; @@ -67,10 +69,10 @@ public class ReplayDataParser { private List gameOptions; @Getter - private List tokens; + private List tokens; @Getter - private List events; + private List events; public ReplayDataParser(Path path, ObjectMapper objectMapper) throws IOException, CompressorException { this.path = path; @@ -212,7 +214,7 @@ private void parseHeader(LittleEndianDataInputStream dataStream) throws IOExcept randomSeed = dataStream.readInt(); } - private void interpretEvents(List events) { + private void interpretEvents(List events) { Integer player = -1; boolean desync = false; String previousChecksum = null; @@ -220,30 +222,30 @@ private void interpretEvents(List events) { Map lastTicks = new HashMap<>(); - for (Event event : events) { + for (ReplayBodyEvent event : events) { switch (event) { - case Event.Unprocessed(Token token, String reason) -> { + case ReplayBodyEvent.Unprocessed(ReplayBodyToken token, String reason) -> { } - case Event.ProcessingError(Token token, Exception exception) -> { + case ReplayBodyEvent.ProcessingError(ReplayBodyToken token, Exception exception) -> { } - case Event.Advance(int ticksToAdvance) -> { + case ReplayBodyEvent.Advance(int ticksToAdvance) -> { ticks += ticksToAdvance; } - case Event.SetCommandSource(int playerIndex) -> { + case ReplayBodyEvent.SetCommandSource(int playerIndex) -> { player = playerIndex; } - case Event.CommandSourceTerminated() -> { + case ReplayBodyEvent.CommandSourceTerminated() -> { lastTicks.put(player, ticks); } - case Event.VerifyChecksum(String hash, int tick) -> { + case ReplayBodyEvent.VerifyChecksum(String hash, int tick) -> { desync = tick == previousTick && !Objects.equals(previousChecksum, hash); previousChecksum = hash; previousTick = ticks; @@ -254,47 +256,47 @@ private void interpretEvents(List events) { } } - case Event.RequestPause() -> { + case ReplayBodyEvent.RequestPause() -> { } - case Event.RequestResume() -> { + case ReplayBodyEvent.RequestResume() -> { } - case Event.SingleStep() -> { + case ReplayBodyEvent.SingleStep() -> { } - case Event.CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) -> { + case ReplayBodyEvent.CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) -> { } - case Event.CreateProp(String blueprintId, float px, float pz, float heading) -> { + case ReplayBodyEvent.CreateProp(String blueprintId, float px, float pz, float heading) -> { } - case Event.DestroyEntity(int entityId) -> { + case ReplayBodyEvent.DestroyEntity(int entityId) -> { } - case Event.WarpEntity(int entityId, float px, float py, float pz) -> { + case ReplayBodyEvent.WarpEntity(int entityId, float px, float py, float pz) -> { } - case Event.ProcessInfoPair(int entityId, String arg1, String arg2) -> { + case ReplayBodyEvent.ProcessInfoPair(int entityId, String arg1, String arg2) -> { } - case Event.IssueCommand(Event.CommandUnits commandUnits, Event.CommandData commandData) -> { + case ReplayBodyEvent.IssueCommand(ReplayBodyEvent.CommandUnits commandUnits, ReplayBodyEvent.CommandData commandData) -> { commandsPerMinuteByPlayer .computeIfAbsent(player, p -> new HashMap<>()) .computeIfAbsent(ticks, t -> new AtomicInteger()) .incrementAndGet(); } - case Event.IssueFactoryCommand( - Event.CommandUnits commandUnits, Event.CommandData commandData + case ReplayBodyEvent.IssueFactoryCommand( + ReplayBodyEvent.CommandUnits commandUnits, ReplayBodyEvent.CommandData commandData ) -> { commandsPerMinuteByPlayer .computeIfAbsent(player, p -> new HashMap<>()) @@ -302,57 +304,57 @@ private void interpretEvents(List events) { .incrementAndGet(); } - case Event.IncreaseCommandCount(int commandId, int delta) -> { + case ReplayBodyEvent.IncreaseCommandCount(int commandId, int delta) -> { } - case Event.DecreaseCommandCount(int commandId, int delta) -> { + case ReplayBodyEvent.DecreaseCommandCount(int commandId, int delta) -> { } - case Event.SetCommandTarget(int commandId, Event.CommandTarget commandTarget) -> { + case ReplayBodyEvent.SetCommandTarget(int commandId, ReplayBodyEvent.CommandTarget commandTarget) -> { } - case Event.SetCommandType(int commandId, int targetId) -> { + case ReplayBodyEvent.SetCommandType(int commandId, int targetId) -> { } - case Event.SetCommandCells(int commandId, Object parametersLua, float px, float py, float pz) -> { + case ReplayBodyEvent.SetCommandCells(int commandId, Object parametersLua, float px, float py, float pz) -> { } - case Event.RemoveCommandFromQueue(int commandId, int unitId) -> { + case ReplayBodyEvent.RemoveCommandFromQueue(int commandId, int unitId) -> { } - case Event.DebugCommand() -> { + case ReplayBodyEvent.DebugCommand() -> { } - case Event.ExecuteLuaInSim(String luaCode) -> { + case ReplayBodyEvent.ExecuteLuaInSim(String luaCode) -> { } - case Event.LuaSimCallback( - String func, LuaTable.Table parametersLua, Event.CommandUnits commandUnits + case ReplayBodyEvent.LuaSimCallback( + String func, LuaTable.Table parametersLua, ReplayBodyEvent.CommandUnits commandUnits ) when func.equals("GiveResourcesToPlayer") -> { parseGiveResourcesToPlayer(parametersLua); } - case Event.LuaSimCallback( - String func, LuaTable.Table parametersLua, Event.CommandUnits commandUnits + case ReplayBodyEvent.LuaSimCallback( + String func, LuaTable.Table parametersLua, ReplayBodyEvent.CommandUnits commandUnits ) when func.equals("ModeratorEvent") -> { parseModeratorEvent(parametersLua, player); } - case Event.LuaSimCallback( - String func, LuaTable parametersLua, Event.CommandUnits commandUnits + case ReplayBodyEvent.LuaSimCallback( + String func, LuaTable parametersLua, ReplayBodyEvent.CommandUnits commandUnits ) -> { } - case Event.EndGame() -> { + case ReplayBodyEvent.EndGame() -> { } @@ -443,9 +445,9 @@ private void parse() throws IOException, CompressorException { readReplayData(path); try (LittleEndianDataInputStream dataStream = new LittleEndianDataInputStream(new ByteArrayInputStream(data))) { parseHeader(dataStream); - tokens = Tokenizer.tokenize(dataStream); + tokens = ReplayBodyTokenizer.tokenize(dataStream); } - events = Parser.parseTokens(tokens); + events = ReplayBodyParser.parseTokens(tokens); interpretEvents(events); } } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/Replay.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayLoader.java similarity index 62% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/Replay.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayLoader.java index d08c2a22..d180ff76 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/Replay.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/ReplayLoader.java @@ -1,10 +1,13 @@ package com.faforever.commons.replay; -import com.faforever.commons.replay.body.Body; -import com.faforever.commons.replay.header.Header; -import com.faforever.commons.replay.header.parse.Parser; -import com.faforever.commons.replay.header.token.Token; -import com.faforever.commons.replay.header.token.Tokenizer; +import com.faforever.commons.replay.body.ReplayBody; +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.fasterxml.jackson.databind.DeserializationFeature; import com.google.common.io.BaseEncoding; import com.google.common.io.LittleEndianDataInputStream; @@ -25,23 +28,23 @@ import java.util.List; import java.util.Objects; -public class Replay { +public class ReplayLoader { - private static Header loadSCFAReplayHeader(LittleEndianDataInputStream stream) throws IOException{ - Token headerToken = Tokenizer.tokenize(stream); - return Parser.parseHeader(headerToken); + private static ReplayHeader loadSCFAReplayHeader(LittleEndianDataInputStream stream) throws IOException{ + ReplayHeaderToken headerToken = ReplayHeaderTokenizer.tokenize(stream); + return ReplayHeaderParser.parseHeader(headerToken); } - private static Body loadSCFAReplayBody(LittleEndianDataInputStream stream) throws IOException{ - List bodyTokens = com.faforever.commons.replay.body.token.Tokenizer.tokenize(stream); - return new Body(com.faforever.commons.replay.body.parse.Parser.parseTokens(bodyTokens)); + private static ReplayBody loadSCFAReplayBody(LittleEndianDataInputStream stream) throws IOException{ + List bodyTokens = ReplayBodyTokenizer.tokenize(stream); + return new ReplayBody(ReplayBodyParser.parseTokens(bodyTokens)); } - private static ReplayContainer loadSCFAReplayFromMemory(ReplayMetadata metadata, ReplayBinaryFormat.BinarySCFA bytes) throws IOException { - try (LittleEndianDataInputStream stream = new LittleEndianDataInputStream((new ByteArrayInputStream(bytes.bytes())))) { - Header replayHead = loadSCFAReplayHeader(stream); - Body replayBody = loadSCFAReplayBody(stream); - return new ReplayContainer(metadata, replayHead, replayBody, bytes.bytes()); + 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); + return new ReplayContainer(metadata, replayHeader, replayBody, scfaReplayBytes); } } @@ -51,21 +54,21 @@ public static ReplayContainer loadSCFAReplayFromDisk(Path scfaReplayFile) throw } byte[] bytes = Files.readAllBytes(scfaReplayFile); - return loadSCFAReplayFromMemory(null, new ReplayBinaryFormat.BinarySCFA(bytes)); + return loadSCFAReplayFromMemory(null, bytes); } - private static ReplayContainer loadFAFReplayFromMemory(ReplayBinaryFormat.WithContext fafReplayBytes) throws IOException, CompressorException { - int separator = findSeparatorIndex(fafReplayBytes.bytes()); - byte[] metadataBytes = Arrays.copyOfRange(fafReplayBytes.bytes(), 0, separator); + private static ReplayContainer loadFAFReplayFromMemory(byte[] fafReplayBytes) throws IOException, CompressorException { + int separator = findSeparatorIndex(fafReplayBytes); + byte[] metadataBytes = Arrays.copyOfRange(fafReplayBytes, 0, separator); String metadataString = new String(metadataBytes, StandardCharsets.UTF_8); ObjectMapper parsedMetadata = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ReplayMetadata replayMetadata = parsedMetadata.readValue(metadataString, ReplayMetadata.class); - byte[] compressedReplayBytes = Arrays.copyOfRange(fafReplayBytes.bytes(), separator + 1, fafReplayBytes.bytes().length); - byte[] replayBytes = decompress(compressedReplayBytes, replayMetadata); + byte[] compressedReplayBytes = Arrays.copyOfRange(fafReplayBytes, separator + 1, fafReplayBytes.length); + byte[] scfaReplayBytes = decompress(compressedReplayBytes, replayMetadata); - return loadSCFAReplayFromMemory(replayMetadata, new ReplayBinaryFormat.BinarySCFA(replayBytes)); + return loadSCFAReplayFromMemory(replayMetadata, scfaReplayBytes); } public static ReplayContainer loadFAFReplayFromDisk(Path fafReplayFile) throws IOException, CompressorException, IllegalArgumentException { @@ -73,8 +76,8 @@ public static ReplayContainer loadFAFReplayFromDisk(Path fafReplayFile) throws I throw new IllegalArgumentException ("Unknown file format: " + fafReplayFile.getFileName()); } - byte[] replayBytes = Files.readAllBytes(fafReplayFile); - return loadFAFReplayFromMemory(new ReplayBinaryFormat.WithContext(replayBytes)); + byte[] fafReplayBytes = Files.readAllBytes(fafReplayFile); + return loadFAFReplayFromMemory(fafReplayBytes); } private static int findSeparatorIndex(byte[] replayData) { diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/Body.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/Body.java deleted file mode 100644 index da2e0af4..00000000 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/Body.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.faforever.commons.replay.body; - -import com.faforever.commons.replay.body.parse.Event; - -import java.util.List; - -public record Body(List events) { -} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBody.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBody.java new file mode 100644 index 00000000..68b394bf --- /dev/null +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBody.java @@ -0,0 +1,6 @@ +package com.faforever.commons.replay.body; + +import java.util.List; + +public record ReplayBody(List events) { +} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Event.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEvent.java similarity index 70% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Event.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEvent.java index 505378fc..e81262f3 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Event.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEvent.java @@ -1,166 +1,165 @@ -package com.faforever.commons.replay.body.parse; +package com.faforever.commons.replay.body; -import com.faforever.commons.replay.body.token.Token; import com.faforever.commons.replay.shared.LuaTable; import java.util.List; -public sealed interface Event { +public sealed interface ReplayBodyEvent { /** * Created by the parser to indicate we do not support this event yet. */ - record Unprocessed(Token token, String reason) implements Event { + record Unprocessed(ReplayBodyToken token, String reason) implements ReplayBodyEvent { } /** * Created by the parser to indicate we made a mistake */ - record ProcessingError(Token token, Exception exception) implements Event { + record ProcessingError(ReplayBodyToken token, Exception exception) implements ReplayBodyEvent { } /** * Created by the engine to advance the tick. */ - record Advance(int ticksToAdvance) implements Event { + record Advance(int ticksToAdvance) implements ReplayBodyEvent { } /** * Created by the engine to set the command source for the next tokens. */ - record SetCommandSource(int playerIndex) implements Event { + record SetCommandSource(int playerIndex) implements ReplayBodyEvent { } /** * Created by the engine when a player leaves the game. */ - record CommandSourceTerminated() implements Event { + record CommandSourceTerminated() implements ReplayBodyEvent { } /** * Created by the engine to check the state of the game */ - record VerifyChecksum(String hash, int tick) implements Event { + record VerifyChecksum(String hash, int tick) implements ReplayBodyEvent { } /** * Created by the User global `SessionRequestPause` to request a pause */ - record RequestPause() implements Event { + record RequestPause() implements ReplayBodyEvent { } /** * Created by the User global `SessionResume` to request a resume */ - record RequestResume() implements Event { + record RequestResume() implements ReplayBodyEvent { } /** * Created by the console command `wld_SingleStep` while the game is paused */ - record SingleStep() implements Event { + record SingleStep() implements ReplayBodyEvent { } /** * Created by the console command `CreateUnit` */ - record CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) implements Event { + record CreateUnit(int playerIndex, String blueprintId, float px, float pz, float heading) implements ReplayBodyEvent { } /** * Created by the console command `CreateProp` */ - record CreateProp(String blueprintId, float px, float pz, float heading) implements Event { + record CreateProp(String blueprintId, float px, float pz, float heading) implements ReplayBodyEvent { } /** * Created by the console commands `DestroySelectedUnits` and `DestroySelectedUnits` */ - record DestroyEntity(int entityId) implements Event { + record DestroyEntity(int entityId) implements ReplayBodyEvent { } /** * Created by the console command `TeleportSelectedUnits` */ - record WarpEntity(int entityId, float px, float py, float pz) implements Event { + record WarpEntity(int entityId, float px, float py, float pz) implements ReplayBodyEvent { } /** * Created by the UserUnit function `ProcessInfo` */ - record ProcessInfoPair(int entityId, String arg1, String arg2) implements Event { + record ProcessInfoPair(int entityId, String arg1, String arg2) implements ReplayBodyEvent { } /** * Created by the engine when the user creates a command by clicking */ - record IssueCommand(CommandUnits commandUnits, CommandData commandData) implements Event { + record IssueCommand(CommandUnits commandUnits, CommandData commandData) implements ReplayBodyEvent { } /** * Created by the User global function `IssueBlueprintCommand` */ - record IssueFactoryCommand(CommandUnits commandUnits, CommandData commandData) implements Event { + record IssueFactoryCommand(CommandUnits commandUnits, CommandData commandData) implements ReplayBodyEvent { } /** * Created by the User global function `IncreaseBuildCountInQueue` */ - record IncreaseCommandCount(int commandId, int delta) implements Event { + record IncreaseCommandCount(int commandId, int delta) implements ReplayBodyEvent { } /** * Created by the user global function `DecreaseBuildCountInQueue` */ - record DecreaseCommandCount(int commandId, int delta) implements Event { + record DecreaseCommandCount(int commandId, int delta) implements ReplayBodyEvent { } /** * Created by the engine when updating the target (entity or position) of a command */ - record SetCommandTarget(int commandId, CommandTarget commandTarget) implements Event { + record SetCommandTarget(int commandId, CommandTarget commandTarget) implements ReplayBodyEvent { } /** * Created by the engine when transforming the command (move to patrol) */ - record SetCommandType(int commandId, int targetId) implements Event { + record SetCommandType(int commandId, int targetId) implements ReplayBodyEvent { } /** * ?? */ - record SetCommandCells(int commandId, LuaTable parametersLua, float px, float py, float pz) implements Event { + record SetCommandCells(int commandId, LuaTable parametersLua, float px, float py, float pz) implements ReplayBodyEvent { } /** * Created by the User global function `DeleteCommand` */ - record RemoveCommandFromQueue(int commandId, int unitId) implements Event { + record RemoveCommandFromQueue(int commandId, int unitId) implements ReplayBodyEvent { } /** * ?? */ - record DebugCommand() implements Event { + record DebugCommand() implements ReplayBodyEvent { } /** * Created by the User global function `ExecLuaInSim` */ - record ExecuteLuaInSim(String luaCode) implements Event { + record ExecuteLuaInSim(String luaCode) implements ReplayBodyEvent { } /** * Created by the user global function `SimCallback` */ - record LuaSimCallback(String func, LuaTable parametersLua, CommandUnits commandUnits) implements Event { + record LuaSimCallback(String func, LuaTable parametersLua, CommandUnits commandUnits) implements ReplayBodyEvent { } /** * Created by the User global function `SessionEndGame` */ - record EndGame() implements Event { + record EndGame() implements ReplayBodyEvent { } sealed interface CommandTarget { @@ -185,7 +184,7 @@ record CommandFormation(int formationId, float orientation, float px, float py, * @param blueprintId * @param parametersLua */ - record CommandData(int commandId, EventCommandType commandType, CommandTarget commandTarget, + record CommandData(int commandId, ReplayBodyEventCommandType commandType, CommandTarget commandTarget, CommandFormation commandFormation, String blueprintId, LuaTable parametersLua) { } } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/EventCommandType.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEventCommandType.java similarity index 90% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/EventCommandType.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEventCommandType.java index 0bfd4efd..c761497f 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/EventCommandType.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyEventCommandType.java @@ -1,9 +1,9 @@ -package com.faforever.commons.replay.body.parse; +package com.faforever.commons.replay.body; import lombok.Getter; @Getter -public enum EventCommandType { +public enum ReplayBodyEventCommandType { // Order is crucial NONE("NONE"), STOP("Stop"), @@ -48,7 +48,7 @@ public enum EventCommandType { private final String string; - EventCommandType(String string) { + ReplayBodyEventCommandType(String string) { this.string = string; } } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Parser.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyParser.java similarity index 54% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Parser.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyParser.java index bbc04c06..4c5a1948 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/parse/Parser.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyParser.java @@ -1,36 +1,40 @@ -package com.faforever.commons.replay.body.parse; +package com.faforever.commons.replay.body; -import com.faforever.commons.replay.body.token.Token; import com.faforever.commons.replay.shared.LuaTable; import com.faforever.commons.replay.shared.Utils; import com.google.common.io.LittleEndianDataInputStream; +import org.jetbrains.annotations.Contract; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.*; -public class Parser { - public static List parseTokens(List tokens) throws IOException { +public class ReplayBodyParser { + + @Contract(pure = true) + public static List parseTokens(List tokens) throws IOException { return tokens.stream().parallel().map((token) -> { try { return parseToken(token); } catch (Exception exception) { - return new Event.ProcessingError(token, exception); + return new ReplayBodyEvent.ProcessingError(token, exception); } }).toList(); } - private static Event.CommandUnits parseCommandUnits(LittleEndianDataInputStream stream) throws IOException { + @Contract(pure = true) + private static ReplayBodyEvent.CommandUnits parseCommandUnits(LittleEndianDataInputStream stream) throws IOException { int unitCount = stream.readInt(); ArrayList unitIds = new ArrayList<>(unitCount); for (int k = 0; k < unitCount; k++) { unitIds.add(stream.readInt()); } - return new Event.CommandUnits(unitCount, unitIds); + return new ReplayBodyEvent.CommandUnits(unitCount, unitIds); } - private static Event.CommandFormation parseCommandFormation(LittleEndianDataInputStream stream) throws IOException { + @Contract(pure = true) + private static ReplayBodyEvent.CommandFormation parseCommandFormation(LittleEndianDataInputStream stream) throws IOException { float orientation = 0; float px = 0; float py = 0; @@ -46,22 +50,23 @@ private static Event.CommandFormation parseCommandFormation(LittleEndianDataInpu scale = stream.readFloat(); } - return new Event.CommandFormation(formation, orientation, px, py, pz, scale); + return new ReplayBodyEvent.CommandFormation(formation, orientation, px, py, pz, scale); } - private static Event.CommandTarget parseCommandTarget(LittleEndianDataInputStream stream) throws IOException { + @Contract(pure = true) + private static ReplayBodyEvent.CommandTarget parseCommandTarget(LittleEndianDataInputStream stream) throws IOException { CommandTargetType target = CommandTargetType.values()[stream.readByte()]; switch (target) { case ENTITY -> { int entityId = stream.readInt(); - return new Event.CommandTarget.Entity(entityId); + return new ReplayBodyEvent.CommandTarget.Entity(entityId); } case POSITION -> { float px = stream.readFloat(); float py = stream.readFloat(); float pz = stream.readFloat(); - return new Event.CommandTarget.Position(px, py, pz); + return new ReplayBodyEvent.CommandTarget.Position(px, py, pz); } default -> { @@ -70,19 +75,20 @@ private static Event.CommandTarget parseCommandTarget(LittleEndianDataInputStrea } } - private static Event.CommandData parseCommandData(LittleEndianDataInputStream stream) throws IOException { + @Contract(pure = true) + private static ReplayBodyEvent.CommandData parseCommandData(LittleEndianDataInputStream stream) throws IOException { int commandId = stream.readInt(); byte[] arg1 = stream.readNBytes(4); - EventCommandType commandType = EventCommandType.values()[stream.readByte()]; + ReplayBodyEventCommandType commandType = ReplayBodyEventCommandType.values()[stream.readByte()]; byte[] arg2 = stream.readNBytes(4); - Event.CommandTarget commandTarget = parseCommandTarget(stream); + ReplayBodyEvent.CommandTarget commandTarget = parseCommandTarget(stream); byte[] arg3 = stream.readNBytes(1); - Event.CommandFormation commandFormation = parseCommandFormation(stream); + ReplayBodyEvent.CommandFormation commandFormation = parseCommandFormation(stream); - String blueprintId = Utils.parseString(stream); + String blueprintId = Utils.readString(stream); byte[] arg4 = stream.readNBytes(12); byte[] arg5 = new byte[0]; @@ -91,62 +97,63 @@ private static Event.CommandData parseCommandData(LittleEndianDataInputStream st arg5 = stream.readNBytes(1); } - return new Event.CommandData( + return new ReplayBodyEvent.CommandData( commandId, commandType, commandTarget, commandFormation, blueprintId, parametersLua ); } - private static Event parseToken(Token token) throws IOException { + @Contract(pure = true) + private static ReplayBodyEvent parseToken(ReplayBodyToken token) throws IOException { try (LittleEndianDataInputStream stream = new LittleEndianDataInputStream((new ByteArrayInputStream(token.tokenContent())))) { return switch (token.tokenId()) { case CMDST_ADVANCE -> { int ticks = stream.readInt(); - yield new Event.Advance(ticks); + yield new ReplayBodyEvent.Advance(ticks); } case CMDST_SET_COMMAND_SOURCE -> { int playerIndex = stream.readByte(); - yield new Event.SetCommandSource(playerIndex); + yield new ReplayBodyEvent.SetCommandSource(playerIndex); } - case CMDST_COMMAND_SOURCE_TERMINATED -> new Event.CommandSourceTerminated(); + case CMDST_COMMAND_SOURCE_TERMINATED -> new ReplayBodyEvent.CommandSourceTerminated(); case CMDST_VERIFY_CHECKSUM -> { String hash = HexFormat.of().formatHex(stream.readNBytes(16)); int tick = stream.readInt(); - yield new Event.VerifyChecksum(hash, tick); + yield new ReplayBodyEvent.VerifyChecksum(hash, tick); } - case CMDST_REQUEST_PAUSE -> new Event.RequestPause(); + case CMDST_REQUEST_PAUSE -> new ReplayBodyEvent.RequestPause(); - case CMDST_RESUME -> new Event.RequestResume(); + case CMDST_RESUME -> new ReplayBodyEvent.RequestResume(); - case CMDST_SINGLE_STEP -> new Event.SingleStep(); + case CMDST_SINGLE_STEP -> new ReplayBodyEvent.SingleStep(); case CMDST_CREATE_UNIT -> { int playerIndex = stream.readByte(); - String blueprintId = Utils.parseString(stream); + String blueprintId = Utils.readString(stream); float px = stream.readFloat(); float pz = stream.readFloat(); float heading = stream.readFloat(); - yield new Event.CreateUnit(playerIndex, blueprintId, px, pz, heading); + yield new ReplayBodyEvent.CreateUnit(playerIndex, blueprintId, px, pz, heading); } case CMDST_CREATE_PROP -> { - String blueprintId = Utils.parseString(stream); + String blueprintId = Utils.readString(stream); float px = stream.readFloat(); float pz = stream.readFloat(); float heading = stream.readFloat(); - yield new Event.CreateProp(blueprintId, px, pz, heading); + yield new ReplayBodyEvent.CreateProp(blueprintId, px, pz, heading); } case CMDST_DESTROY_ENTITY -> { int entityId = stream.readInt(); - yield new Event.DestroyEntity(entityId); + yield new ReplayBodyEvent.DestroyEntity(entityId); } case CMDST_WARP_ENTITY -> { @@ -154,52 +161,52 @@ private static Event parseToken(Token token) throws IOException { float px = stream.readFloat(); float py = stream.readFloat(); float pz = stream.readFloat(); - yield new Event.WarpEntity(entityId, px, py, pz); + yield new ReplayBodyEvent.WarpEntity(entityId, px, py, pz); } case CMDST_PROCESS_INFO_PAIR -> { int entityId = stream.read(); - String arg1 = Utils.parseString(stream); - String arg2 = Utils.parseString(stream); - yield new Event.ProcessInfoPair(entityId, arg1, arg2); + String arg1 = Utils.readString(stream); + String arg2 = Utils.readString(stream); + yield new ReplayBodyEvent.ProcessInfoPair(entityId, arg1, arg2); } case CMDST_ISSUE_COMMAND -> { - Event.CommandUnits commandUnits = parseCommandUnits(stream); - Event.CommandData commandData = parseCommandData(stream); + ReplayBodyEvent.CommandUnits commandUnits = parseCommandUnits(stream); + ReplayBodyEvent.CommandData commandData = parseCommandData(stream); - yield new Event.IssueCommand(commandUnits, commandData); + yield new ReplayBodyEvent.IssueCommand(commandUnits, commandData); } case CMDST_ISSUE_FACTORY_COMMAND -> { - Event.CommandUnits commandUnits = parseCommandUnits(stream); - Event.CommandData commandData = parseCommandData(stream); + ReplayBodyEvent.CommandUnits commandUnits = parseCommandUnits(stream); + ReplayBodyEvent.CommandData commandData = parseCommandData(stream); - yield new Event.IssueFactoryCommand(commandUnits, commandData); + yield new ReplayBodyEvent.IssueFactoryCommand(commandUnits, commandData); } case CMDST_INCREASE_COMMAND_COUNT -> { int commandId = stream.readInt(); int delta = stream.readInt(); - yield new Event.IncreaseCommandCount(commandId, delta); + yield new ReplayBodyEvent.IncreaseCommandCount(commandId, delta); } case CMDST_DECRASE_COMMAND_COUNT -> { int commandId = stream.readInt(); int delta = stream.readInt(); - yield new Event.DecreaseCommandCount(commandId, delta); + yield new ReplayBodyEvent.DecreaseCommandCount(commandId, delta); } case CMDST_SET_COMMAND_TARGET -> { int commandId = stream.readInt(); - Event.CommandTarget commandTarget = parseCommandTarget(stream); - yield new Event.SetCommandTarget(commandId, commandTarget); + ReplayBodyEvent.CommandTarget commandTarget = parseCommandTarget(stream); + yield new ReplayBodyEvent.SetCommandTarget(commandId, commandTarget); } case CMDST_SET_COMMAND_TYPE -> { int commandId = stream.readInt(); int targetCommandType = stream.readInt(); - yield new Event.SetCommandType(commandId, targetCommandType); + yield new ReplayBodyEvent.SetCommandType(commandId, targetCommandType); } case CMDST_SET_COMMAND_CELLS -> { @@ -213,26 +220,26 @@ private static Event parseToken(Token token) throws IOException { float py = stream.readFloat(); float pz = stream.readFloat(); - yield new Event.SetCommandCells(commandId, parametersLua, px, py, pz); + yield new ReplayBodyEvent.SetCommandCells(commandId, parametersLua, px, py, pz); } case CMDST_REMOVE_COMMAND_FROM_QUEUE -> { int commandId = stream.readInt(); int unitId = stream.readInt(); - yield new Event.RemoveCommandFromQueue(commandId, unitId); + yield new ReplayBodyEvent.RemoveCommandFromQueue(commandId, unitId); } - case CMDST_DEBUG_COMMAND -> new Event.Unprocessed(token, "CMDST_DEBUG_COMMAND"); + case CMDST_DEBUG_COMMAND -> new ReplayBodyEvent.Unprocessed(token, "CMDST_DEBUG_COMMAND"); case CMDST_EXECUTE_LUA_IN_SIM -> { - String luaCode = Utils.parseString(stream); - yield new Event.ExecuteLuaInSim(luaCode); + String luaCode = Utils.readString(stream); + yield new ReplayBodyEvent.ExecuteLuaInSim(luaCode); } case CMDST_LUA_SIM_CALLBACK -> { - String func = Utils.parseString(stream); + String func = Utils.readString(stream); LuaTable args = Utils.parseLua(stream); - Event.CommandUnits commandUnits = null; + ReplayBodyEvent.CommandUnits commandUnits = null; // suspicion that this is just flat out wrong! Whether there's a selection in the data is not related to whether there are Lua arguments if (!(args instanceof LuaTable.Nil)) { @@ -242,12 +249,12 @@ private static Event parseToken(Token token) throws IOException { stream.readNBytes(4 + 3); } - yield new Event.LuaSimCallback(func, args, commandUnits); + yield new ReplayBodyEvent.LuaSimCallback(func, args, commandUnits); } - case CMDST_END_GAME -> new Event.EndGame(); + case CMDST_END_GAME -> new ReplayBodyEvent.EndGame(); - case null -> new Event.Unprocessed(token, "Unknown"); + case null -> new ReplayBodyEvent.Unprocessed(token, "Unknown"); }; } } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Token.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyToken.java similarity index 84% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Token.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyToken.java index 01943d19..72494168 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Token.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyToken.java @@ -1,6 +1,6 @@ -package com.faforever.commons.replay.body.token; +package com.faforever.commons.replay.body; -public record Token(TokenId tokenId, int tokenSize, byte[] tokenContent) { +public record ReplayBodyToken(TokenId tokenId, int tokenSize, byte[] tokenContent) { public enum TokenId { // Order is crucial diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Tokenizer.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyTokenizer.java similarity index 61% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Tokenizer.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyTokenizer.java index 23c6efc1..026eefe8 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/body/token/Tokenizer.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/body/ReplayBodyTokenizer.java @@ -1,4 +1,4 @@ -package com.faforever.commons.replay.body.token; +package com.faforever.commons.replay.body; import com.google.common.io.LittleEndianDataInputStream; @@ -10,20 +10,20 @@ import java.util.*; @Slf4j -public class Tokenizer { +public class ReplayBodyTokenizer { private static final int TOKEN_HEADER_LENGTH = 3; @Contract(pure = true) - public static List tokenize(@NotNull LittleEndianDataInputStream dataStream) throws IOException { - ArrayList tokens = new ArrayList<>(); + public static List tokenize(@NotNull LittleEndianDataInputStream dataStream) throws IOException { + ArrayList tokens = new ArrayList<>(); while (dataStream.available() > 0) { int tokenId = dataStream.readUnsignedByte(); int tokenLength = dataStream.readUnsignedShort(); byte[] tokenContent = dataStream.readNBytes(tokenLength - TOKEN_HEADER_LENGTH); - tokens.add(new Token(Token.TokenId.values()[tokenId], tokenLength, tokenContent)); + tokens.add(new ReplayBodyToken(ReplayBodyToken.TokenId.values()[tokenId], tokenLength, tokenContent)); } return tokens; diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Header.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Header.java deleted file mode 100644 index 7950504f..00000000 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Header.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.faforever.commons.replay.header; - -import java.util.List; - -/** - * Populated by the table that is passed to `CLobby:LaunchGame` - */ -public record Header(String gameVersion, String replayVersion, String pathToScenario, boolean cheatsEnabled, int seed, - List sources, - List mods, - GameOptions gameOptions, - List playerOptions -) { -} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeader.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeader.java new file mode 100644 index 00000000..68b4b36f --- /dev/null +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeader.java @@ -0,0 +1,14 @@ +package com.faforever.commons.replay.header; + +import java.util.List; + +/** + * Populated by the table that is passed to `CLobby:LaunchGame` + */ +public record ReplayHeader(String gameVersion, String replayVersion, String pathToScenario, boolean cheatsEnabled, int seed, + List sources, + List mods, + GameOptions gameOptions, + List playerOptions +) { +} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/parse/Parser.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderParser.java similarity index 80% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/header/parse/Parser.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderParser.java index 743e30db..98f3adfb 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/parse/Parser.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderParser.java @@ -1,10 +1,4 @@ -package com.faforever.commons.replay.header.parse; - -import com.faforever.commons.replay.header.GameMod; -import com.faforever.commons.replay.header.GameOptions; -import com.faforever.commons.replay.header.Header; -import com.faforever.commons.replay.header.PlayerOptions; -import com.faforever.commons.replay.header.token.Token; +package com.faforever.commons.replay.header; import com.faforever.commons.replay.shared.LuaTable; import com.google.common.io.LittleEndianDataInputStream; @@ -18,10 +12,10 @@ import static com.faforever.commons.replay.shared.Utils.parseLua; -public class Parser { +public class ReplayHeaderParser { @Contract(pure = true) - public static @Nullable Header parseHeader(Token token) throws IOException { + public static @Nullable ReplayHeader parseHeader(ReplayHeaderToken token) throws IOException { GameOptions gameOptions = parseGameOptions(token.gameOptions()); List gameMods = parseMod(token.mods()); @@ -34,7 +28,7 @@ public class Parser { } }).toList(); - return new Header( + return new ReplayHeader( token.gameVersion(), token.replayVersion(), token.pathToScenario(), token.cheatsEnabled(), token.seed(), token.sources(), gameMods, diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderToken.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderToken.java new file mode 100644 index 00000000..dbbfd526 --- /dev/null +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderToken.java @@ -0,0 +1,10 @@ +package com.faforever.commons.replay.header; + +import java.util.List; + +public record ReplayHeaderToken(String gameVersion, String replayVersion, String pathToScenario, boolean cheatsEnabled, int seed, + List sources, + byte[] mods, + byte[] gameOptions, + List playerOptions) { +} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Tokenizer.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderTokenizer.java similarity index 68% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Tokenizer.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderTokenizer.java index eb8e83f7..665ebb51 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Tokenizer.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/ReplayHeaderTokenizer.java @@ -1,6 +1,5 @@ -package com.faforever.commons.replay.header.token; +package com.faforever.commons.replay.header; -import com.faforever.commons.replay.header.Source; import com.faforever.commons.replay.shared.Utils; import com.google.common.io.LittleEndianDataInputStream; import org.jetbrains.annotations.Contract; @@ -9,18 +8,18 @@ import java.util.ArrayList; import java.util.List; -public class Tokenizer { +public class ReplayHeaderTokenizer { @Contract(pure = true) - public static Token tokenize(LittleEndianDataInputStream dataStream) throws IOException { + public static ReplayHeaderToken tokenize(LittleEndianDataInputStream dataStream) throws IOException { - String gameVersion = Utils.parseString(dataStream); - String arg1 = Utils.parseString(dataStream); // Always \r\n + String gameVersion = Utils.readString(dataStream); + String arg1 = Utils.readString(dataStream); // Always \r\n - String[] replayAndScenario = Utils.parseString(dataStream).split("\\r\\n"); + String[] replayAndScenario = Utils.readString(dataStream).split("\\r\\n"); String replayVersion = replayAndScenario[0]; String pathToScenario = replayAndScenario[1]; - String arg2 = Utils.parseString(dataStream); // always \r\n and some unknown character + String arg2 = Utils.readString(dataStream); // always \r\n and some unknown character int sizeModsInBytes = dataStream.readInt(); byte[] mods = dataStream.readNBytes(sizeModsInBytes); @@ -31,7 +30,7 @@ public static Token tokenize(LittleEndianDataInputStream dataStream) throws IOEx int numberOfClients = dataStream.readUnsignedByte(); List clients = new ArrayList<>(numberOfClients); for (int i = 0; i < numberOfClients; i++) { - String playerName = Utils.parseString(dataStream); + String playerName = Utils.readString(dataStream); int playerId = dataStream.readInt(); Source source = new Source(i, playerId, playerName); clients.add(source); @@ -54,6 +53,6 @@ public static Token tokenize(LittleEndianDataInputStream dataStream) throws IOEx int seed = dataStream.readInt(); - return new Token(gameVersion, replayVersion, pathToScenario, cheatsEnabled, seed, clients, mods, gameOptions, playerOptions); + return new ReplayHeaderToken(gameVersion, replayVersion, pathToScenario, cheatsEnabled, seed, clients, mods, gameOptions, playerOptions); } } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Source.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Source.java index 7a248ef2..c275ebb4 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Source.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/Source.java @@ -1,7 +1,7 @@ package com.faforever.commons.replay.header; /** - * Populated by the engine and it represents the clients that are connected to the game. + * Populated by the engine, it represents the clients that are connected to the game. * @param name * @param sourceId */ diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Token.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Token.java deleted file mode 100644 index 1be85b47..00000000 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/header/token/Token.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.faforever.commons.replay.header.token; - -import com.faforever.commons.replay.header.Source; - -import java.util.List; - -public record Token(String gameVersion, String replayVersion, String pathToScenario, boolean cheatsEnabled, int seed, - List sources, - byte[] mods, - byte[] gameOptions, - List playerOptions) { -} diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/ChatMessage.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ChatMessage.java similarity index 81% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/ChatMessage.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ChatMessage.java index 0231b7c7..a520616e 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/ChatMessage.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ChatMessage.java @@ -1,4 +1,4 @@ -package com.faforever.commons.replay; +package com.faforever.commons.replay.semantics; import lombok.Data; diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/ModeratorEvent.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ModeratorEvent.java similarity index 87% rename from faf-commons-data/src/main/java/com/faforever/commons/replay/ModeratorEvent.java rename to faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ModeratorEvent.java index 6ac3ab64..5a2e484c 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/ModeratorEvent.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/semantics/ModeratorEvent.java @@ -1,4 +1,4 @@ -package com.faforever.commons.replay; +package com.faforever.commons.replay.semantics; import java.time.Duration; diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/LuaTable.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/LuaTable.java index e6a8011d..3c2b26c5 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/LuaTable.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/LuaTable.java @@ -3,11 +3,9 @@ import java.util.Map; public sealed interface LuaTable { - record Number(float value) implements LuaTable {} record String(java.lang.String value) implements LuaTable {} record Nil() implements LuaTable {} record Table(Map value) implements LuaTable {} record Bool(boolean value) implements LuaTable {} - } diff --git a/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/Utils.java b/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/Utils.java index 1f91c7d8..c2733838 100644 --- a/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/Utils.java +++ b/faf-commons-data/src/main/java/com/faforever/commons/replay/shared/Utils.java @@ -26,7 +26,7 @@ private static int peek(LittleEndianDataInputStream dataStream) throws IOExcepti * @throws IOException */ @Contract(pure = true) - public static String parseString(LittleEndianDataInputStream dataStream) throws IOException { + public static String readString(LittleEndianDataInputStream dataStream) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte tempByte; while ((tempByte = dataStream.readByte()) != 0) { @@ -59,7 +59,7 @@ public static LuaTable parseLua(LittleEndianDataInputStream dataStream) throws I } case LUA_STRING -> { - String value = parseString(dataStream); + String value = readString(dataStream); return new LuaTable.String(value); } diff --git a/faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayTest.java b/faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayLoaderTest.java similarity index 81% rename from faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayTest.java rename to faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayLoaderTest.java index cabee30b..819c341b 100644 --- a/faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayTest.java +++ b/faf-commons-data/src/test/java/com/faforever/commons/replay/LoadReplayLoaderTest.java @@ -16,7 +16,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; -class LoadReplayTest { +class LoadReplayLoaderTest { @TempDir public Path temporaryFolder; @@ -29,7 +29,7 @@ public void parseBinary01() throws CompressorException, IOException { () -> { Path fafReplayFile = temporaryFolder.resolve("TestCommands01.fafreplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/TestCommands01.fafreplay")), fafReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); } ); } @@ -40,7 +40,7 @@ public void parseBinary02() throws CompressorException, IOException { () -> { Path fafReplayFile = temporaryFolder.resolve("TestModeratorEvents.fafreplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/TestModeratorEvents.fafreplay")), fafReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); } ); } @@ -51,7 +51,7 @@ public void parseBinary03() throws CompressorException, IOException { () -> { Path fafReplayFile = temporaryFolder.resolve("zstd_reference.fafreplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/zstd_reference.fafreplay")), fafReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); } ); } @@ -62,7 +62,7 @@ public void parseBinary04() throws CompressorException, IOException { () -> { Path fafReplayFile = temporaryFolder.resolve("test.fafreplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/test.fafreplay")), fafReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); } ); } @@ -75,8 +75,8 @@ public void compareBinary01() throws CompressorException, IOException { Path scfaReplayFile = temporaryFolder.resolve("22338092.scfareplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/load/22338092.scfareplay")), scfaReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); - ReplayContainer scfaReplayContainer = Replay.loadSCFAReplayFromDisk(scfaReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer scfaReplayContainer = ReplayLoader.loadSCFAReplayFromDisk(scfaReplayFile); assertEquals(scfaReplayContainer.body().events().size(), fafReplayContainer.body().events().size()); assertArrayEquals( scfaReplayContainer.bytes(), fafReplayContainer.bytes()); @@ -90,8 +90,8 @@ public void compareBinary02() throws CompressorException, IOException { Path scfaReplayFile = temporaryFolder.resolve("22373098.scfareplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/load/22373098.scfareplay")), scfaReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); - ReplayContainer scfaReplayContainer = Replay.loadSCFAReplayFromDisk(scfaReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer scfaReplayContainer = ReplayLoader.loadSCFAReplayFromDisk(scfaReplayFile); assertEquals(scfaReplayContainer.body().events().size(), fafReplayContainer.body().events().size()); assertEquals(Arrays.hashCode(scfaReplayContainer.bytes()), Arrays.hashCode(fafReplayContainer.bytes())); @@ -107,8 +107,8 @@ public void compareBinary03() throws CompressorException, IOException { Path scfaReplayFile = temporaryFolder.resolve("22425616.scfareplay"); Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/replay/load/22425616.scfareplay")), scfaReplayFile); - ReplayContainer fafReplayContainer = Replay.loadFAFReplayFromDisk(fafReplayFile); - ReplayContainer scfaReplayContainer = Replay.loadSCFAReplayFromDisk(scfaReplayFile); + ReplayContainer fafReplayContainer = ReplayLoader.loadFAFReplayFromDisk(fafReplayFile); + ReplayContainer scfaReplayContainer = ReplayLoader.loadSCFAReplayFromDisk(scfaReplayFile); assertEquals(scfaReplayContainer.body().events().size(), fafReplayContainer.body().events().size()); assertEquals(Arrays.hashCode(scfaReplayContainer.bytes()), Arrays.hashCode(fafReplayContainer.bytes())); diff --git a/faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayDataParserTest.java b/faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayLoaderDataParserTest.java similarity index 97% rename from faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayDataParserTest.java rename to faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayLoaderDataParserTest.java index b209b83f..54855602 100644 --- a/faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayDataParserTest.java +++ b/faf-commons-data/src/test/java/com/faforever/commons/replay/ReplayLoaderDataParserTest.java @@ -1,6 +1,7 @@ package com.faforever.commons.replay; -import ch.qos.logback.classic.encoder.JsonEncoder; +import com.faforever.commons.replay.semantics.ChatMessage; +import com.faforever.commons.replay.semantics.ModeratorEvent; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; @@ -18,14 +19,13 @@ import java.time.Duration; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.Objects; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; -class ReplayDataParserTest { +class ReplayLoaderDataParserTest { @TempDir public Path temporaryFolder;