Skip to content

Commit a9c6d7a

Browse files
author
Builder
committed
Merge branch 'fix-tmi-in-replays' into 'master'
fix(SDK): Generate demo replay with only useful info See merge request codingame/game-engine!202
2 parents ae316ea + a98b509 commit a9c6d7a

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

playground/misc/misc-3-release-notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The CodinGame SDK is regularly updated and improved. This document lets you know what changed in the latest releases.
44

5+
## Next Release
6+
7+
### 🐞 Bug fix
8+
9+
- The intro replay file `demo.js` is now smaller when generated from scratch.
10+
511
## 3.4.5
612

713
### 🐞 Bug fix

runner/src/main/java/com/codingame/gameengine/runner/Renderer.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
import com.google.gson.GsonBuilder;
4949
import com.google.gson.JsonArray;
5050
import com.google.gson.JsonElement;
51+
import com.google.gson.JsonIOException;
5152
import com.google.gson.JsonObject;
5253
import com.google.gson.JsonParser;
5354
import com.google.gson.JsonPrimitive;
55+
import com.google.gson.JsonSyntaxException;
5456

5557
import io.undertow.Handlers;
5658
import io.undertow.Undertow;
@@ -235,14 +237,14 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
235237
List<String> content = Files.readAllLines(f);
236238
String newContent = "";
237239
Pattern regex = Pattern.compile("<page.*file=\\\"([^\\\"]+)\\\".*\\/>"); // looking for the font resources
238-
for(String line : content) {
240+
for (String line : content) {
239241
Matcher ressourcesMatcher = regex.matcher(line);
240242
String newLine = "";
241243
int startCpy = 0;
242-
while(ressourcesMatcher.find()) {
244+
while (ressourcesMatcher.find()) {
243245
int startMatch = ressourcesMatcher.start(1);
244246
int endMatch = ressourcesMatcher.end(1);
245-
newLine = newLine.concat(line.substring(startCpy,startMatch));
247+
newLine = newLine.concat(line.substring(startCpy, startMatch));
246248
Path assetPath = f.getParent().resolve(ressourcesMatcher.group(1));
247249
newLine = newLine.concat(hashAsset(assetPath));
248250
startCpy = endMatch;
@@ -255,7 +257,8 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
255257
Files.write(
256258
tmpdir.resolve("hashed_assets").resolve(newName),
257259
newContent.getBytes(),
258-
StandardOpenOption.CREATE);
260+
StandardOpenOption.CREATE
261+
);
259262
} else {
260263
fonts.add(
261264
tmpdir.relativize(f).toString().replace("\\", "/")
@@ -295,9 +298,11 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
295298
private static boolean isSpriteJson(Path f) {
296299
return "json".equals(FilenameUtils.getExtension(f.toString()));
297300
}
301+
298302
private static boolean isFont(Path f) {
299303
return "fnt".equals(FilenameUtils.getExtension(f.toString()));
300304
}
305+
301306
public static List<Path> generateView(String jsonResult, String assetsPath) {
302307
List<Path> paths;
303308

@@ -664,11 +669,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
664669

665670
try (PrintWriter out = new PrintWriter(demoFile)) {
666671
out.println("export const demo = ");
667-
668-
List<String> lines = Files.readAllLines(gameFile.toPath());
669-
for (String line : lines) {
670-
out.println(line);
671-
}
672+
out.print(extractDemoFromGameJson(gameFile).toString());
672673
out.println(";");
673674
}
674675

@@ -683,6 +684,22 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
683684
}
684685
}
685686

687+
private JsonObject extractDemoFromGameJson(File gameFile) {
688+
JsonObject result = new JsonObject();
689+
JsonParser parser = new JsonParser();
690+
try {
691+
JsonElement obj = parser.parse(new FileReader(gameFile));
692+
JsonElement views = obj.getAsJsonObject().get("views");
693+
JsonElement agents = obj.getAsJsonObject().get("agents");
694+
result.add("views", views);
695+
result.add("agents", agents);
696+
697+
} catch (IllegalStateException | JsonIOException | JsonSyntaxException | FileNotFoundException e) {
698+
e.printStackTrace();
699+
}
700+
return result;
701+
}
702+
686703
private void sendException(HttpServerExchange exchange, Exception e, int statusCode) {
687704
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
688705
exchange.setStatusCode(statusCode);

0 commit comments

Comments
 (0)