Skip to content

Commit

Permalink
Read out the scenario information
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed May 21, 2024
1 parent ff24884 commit 75af5b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faforever.commons.replay.header;

import java.util.Map;

public record GameScenario(String mapPath, Integer mapVersion, String mapDescription, String mapScript, String mapSave,
String mapName, Integer mapSizeX, Integer mapSizeZ, Integer reclaimMassValue,
Integer reclaimEnergyValue, GameOptions primaryGameOptions,
Map<String, String> secondaryGameOptions) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public record ReplayHeader(String gameVersion, String replayVersion, String pathToScenario, boolean cheatsEnabled, int seed,
List<Source> sources,
List<GameMod> mods,
GameOptions gameOptions,
GameScenario scenario,
List<PlayerOptions> playerOptions
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import com.faforever.commons.replay.shared.LoadUtils;
import com.faforever.commons.replay.shared.LuaData;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.google.common.io.LittleEndianDataInputStream;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.luaj.vm2.LuaTable;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;

import static com.faforever.commons.replay.shared.LoadUtils.parseLua;

Expand All @@ -35,7 +31,7 @@ public static ReplayHeader parse(LittleEndianDataInputStream dataStream) throws

int sizeGameOptionsInBytes = dataStream.readInt();
byte[] gameOptionBytes = dataStream.readNBytes(sizeGameOptionsInBytes);
GameOptions gameOptions = parseGameOptions((gameOptionBytes));
GameScenario gameScenario = parseGameScenario((gameOptionBytes));

int numberOfClients = dataStream.readUnsignedByte();
List<Source> sources = new ArrayList<>(numberOfClients);
Expand Down Expand Up @@ -65,7 +61,7 @@ public static ReplayHeader parse(LittleEndianDataInputStream dataStream) throws

int seed = dataStream.readInt();

return new ReplayHeader(gameVersion, replayVersion, pathToScenario, cheatsEnabled, seed, sources, mods, gameOptions, allPlayerOptions);
return new ReplayHeader(gameVersion, replayVersion, pathToScenario, cheatsEnabled, seed, sources, mods, gameScenario, allPlayerOptions);
}

@Contract(pure = true)
Expand Down Expand Up @@ -100,11 +96,46 @@ public static ReplayHeader parse(LittleEndianDataInputStream dataStream) throws
}

@Contract(pure = true)
private static @Nullable GameOptions parseGameOptions(byte[] bytes) throws IOException {
private static @Nullable GameScenario parseGameScenario(byte[] bytes) throws IOException {
try (LittleEndianDataInputStream stream = new LittleEndianDataInputStream((new ByteArrayInputStream(bytes)))) {
LuaData gameOptions = parseLua(stream);
LuaData gameScenario = parseLua(stream);

if (gameScenario instanceof LuaData.Table table) {

// retrieve and manage the game options
GameOptions primaryOptions = null;
HashMap<String, String> secondaryOptions = new HashMap<String, String>();
if (table.value().get("Options") instanceof LuaData.Table optionsTable) {

}

Integer sizeX = null;
Integer sizeZ = null;
if (table.getTable("size") instanceof LuaData.Table sizeTable) {
sizeX = sizeTable.getInteger("1.0");
sizeZ = sizeTable.getInteger("2.0");
}

Integer massReclaimValue = null;
Integer energyReclaimValue = null;
if (table.value().get("reclaim") instanceof LuaData.Table reclaimTable) {
massReclaimValue = reclaimTable.getInteger("1.0");
energyReclaimValue = reclaimTable.getInteger("2.0");
}

return new GameScenario(
table.getString("map"),
table.getInteger("map_version"),
table.getString("description"),
table.getString("script"),
table.getString("save"),
table.getString("name"),
sizeX, sizeZ,
massReclaimValue, energyReclaimValue,
primaryOptions, secondaryOptions
);

// TODO: needs implementation
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ public Integer getInteger(java.lang.String key) {

return null;
}

public LuaData.Table getTable(java.lang.String key) {
if (value.get(key) instanceof LuaData.Table table) {
return table;
}

return null;
}
}
}

0 comments on commit 75af5b7

Please sign in to comment.