Skip to content

Commit

Permalink
First working end-to-end example of the new replay parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed May 7, 2024
1 parent bccff78 commit a8e7303
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +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.fasterxml.jackson.databind.DeserializationFeature;
import com.google.common.io.BaseEncoding;
import com.google.common.io.LittleEndianDataInputStream;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
Expand All @@ -16,12 +22,27 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class Replay {

private static ReplayContainer loadSCFAReplayFromMemory(ReplayMetadata metadata, ReplayBinaryFormat.BinarySCFA bytes) {
return new ReplayContainer(metadata, bytes);
private static Header loadSCFAReplayHeader(LittleEndianDataInputStream stream) throws IOException{
Token headerToken = Tokenizer.tokenize(stream);
return Parser.parseHeader(headerToken);
}

private static Body loadSCFAReplayBody(LittleEndianDataInputStream stream) throws IOException{
List<com.faforever.commons.replay.body.token.Token> bodyTokens = com.faforever.commons.replay.body.token.Tokenizer.tokenize(stream);
return new Body(com.faforever.commons.replay.body.parse.Parser.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());
}
}

public static ReplayContainer loadSCFAReplayFromDisk(Path scfaReplayFile) throws IOException, IllegalArgumentException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.faforever.commons.replay;

public record ReplayContainer(ReplayMetadata metadata, ReplayBinaryFormat.BinarySCFA replay) {
import com.faforever.commons.replay.body.Body;
import com.faforever.commons.replay.header.Header;

public record ReplayContainer(ReplayMetadata metadata, Header header, Body body, byte[] bytes) {
}
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.event.Event;
import com.faforever.commons.replay.body.event.LuaData;
import com.faforever.commons.replay.body.event.Parser;
import com.faforever.commons.replay.body.parse.Event;
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.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -335,19 +335,19 @@ private void interpretEvents(List<Event> events) {
}

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

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

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

}
Expand All @@ -360,11 +360,11 @@ private void interpretEvents(List<Event> events) {
}
}

private void parseGiveResourcesToPlayer(LuaData.Table lua) {
private void parseGiveResourcesToPlayer(LuaTable.Table lua) {
if (lua.value().containsKey("Msg") && lua.value().containsKey("From") && lua.value().containsKey("Sender")) {

// TODO: use the command source (player value) instead of the values from the callback. The values from the callback can be manipulated
if (!(lua.value().get("From") instanceof LuaData.Number(float luaFromArmy))) {
if (!(lua.value().get("From") instanceof LuaTable.Number(float luaFromArmy))) {
return;
}

Expand All @@ -373,20 +373,20 @@ private void parseGiveResourcesToPlayer(LuaData.Table lua) {
return;
}

if (!(lua.value().get("Msg") instanceof LuaData.Table(Map<String, LuaData> luaMsg))) {
if (!(lua.value().get("Msg") instanceof LuaTable.Table(Map<String, LuaTable> luaMsg))) {
return;
}

if (!(lua.value().get("Sender") instanceof LuaData.String(String luaSender))) {
if (!(lua.value().get("Sender") instanceof LuaTable.String(String luaSender))) {
return;
}

// This can either be a player name or a Map of something, in which case it's actually giving resources
if (!(luaMsg.get("to") instanceof LuaData.String(String luaMsgReceiver))) {
if (!(luaMsg.get("to") instanceof LuaTable.String(String luaMsgReceiver))) {
return;
}

if (!(luaMsg.get("text") instanceof LuaData.String(String luaMsgText))) {
if (!(luaMsg.get("text") instanceof LuaTable.String(String luaMsgText))) {
return;
}

Expand All @@ -398,18 +398,18 @@ private void parseGiveResourcesToPlayer(LuaData.Table lua) {
}


void parseModeratorEvent(LuaData.Table lua, Integer player) {
void parseModeratorEvent(LuaTable.Table lua, Integer player) {
String messageContent = null;
String playerNameFromArmy = null;
String playerNameFromCommandSource = null;
Integer activeCommandSource = null;
Integer fromArmy = null;

if (lua.value().get("Message") instanceof LuaData.String(String luaMessage)) {
if (lua.value().get("Message") instanceof LuaTable.String(String luaMessage)) {
messageContent = luaMessage;
}

if (lua.value().get("From") instanceof LuaData.Number(float luaFrom)) {
if (lua.value().get("From") instanceof LuaTable.Number(float luaFrom)) {
fromArmy = (int) luaFrom - 1;


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

import com.faforever.commons.replay.body.parse.Event;

import java.util.List;

public record Body(List<Event> events) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faforever.commons.replay.body.event;
package com.faforever.commons.replay.body.parse;

import com.faforever.commons.replay.body.token.Token;
import com.faforever.commons.replay.shared.LuaTable;

import java.util.List;

Expand Down Expand Up @@ -129,7 +130,7 @@ record SetCommandType(int commandId, int targetId) implements Event {
/**
* ??
*/
record SetCommandCells(int commandId, LuaData parametersLua, float px, float py, float pz) implements Event {
record SetCommandCells(int commandId, LuaTable parametersLua, float px, float py, float pz) implements Event {
}

/**
Expand All @@ -153,7 +154,7 @@ record ExecuteLuaInSim(String luaCode) implements Event {
/**
* Created by the user global function `SimCallback`
*/
record LuaSimCallback(String func, LuaData parametersLua, CommandUnits commandUnits) implements Event {
record LuaSimCallback(String func, LuaTable parametersLua, CommandUnits commandUnits) implements Event {
}

/**
Expand Down Expand Up @@ -185,6 +186,6 @@ record CommandFormation(int formationId, float orientation, float px, float py,
* @param parametersLua
*/
record CommandData(int commandId, EventCommandType commandType, CommandTarget commandTarget,
CommandFormation commandFormation, String blueprintId, LuaData parametersLua) {
CommandFormation commandFormation, String blueprintId, LuaTable parametersLua) {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.faforever.commons.replay.body.event;
package com.faforever.commons.replay.body.parse;

import lombok.Getter;

Expand All @@ -14,7 +14,7 @@ public enum EventCommandType {
BUILD_SILO_NUKE("BuildSiloNuke"),
BUILD_FACTORY("BuildFactory"),
BUILD_MOBILE("BuildMobile"),
BUILD_ASSIsT("BuildAssist"),
BUILD_ASSIST("BuildAssist"),
ATTACK("Attack"),
FORM_ATTACK("FormAttack"),
NUKE("Nuke"),
Expand Down
Loading

0 comments on commit a8e7303

Please sign in to comment.