From c63f631c6f8b277d02eb64a641e6d20fd4d9a87d Mon Sep 17 00:00:00 2001 From: Julien Poulton Date: Thu, 7 Sep 2023 10:55:45 +0200 Subject: [PATCH 1/2] fix(SDK): fixed backslashes being incorrectly parsed for Texts --- .../module/entities/Serializer.java | 6 ++--- .../view/entity-module/CommandParser.js | 24 ++++++++++++------- .../view/entity-module/properties.js | 12 ++++------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Serializer.java b/engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Serializer.java index 3c32d060..f5b9ed95 100644 --- a/engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Serializer.java +++ b/engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Serializer.java @@ -150,11 +150,11 @@ static String formatFrameTime(double t) { } static String escape(String text) { - String escaped = text.replaceAll("\\'", "\\\\'"); - if (escaped.contains(" ") || escaped.contains(";") || escaped.contains("\n")) { + if (text.contains(" ") || text.contains(";") || text.contains("\n") || text.contains("'")) { + String escaped = text.replaceAll("\\'", "\\\\'"); return "'" + escaped + "'"; } - return escaped; + return text; } private String serializeEntitiesStateDiff(Entity entity, EntityState diff, String frameInstant) { diff --git a/engine/modules/entities/src/main/resources/view/entity-module/CommandParser.js b/engine/modules/entities/src/main/resources/view/entity-module/CommandParser.js index 2418bebc..72ac66c3 100644 --- a/engine/modules/entities/src/main/resources/view/entity-module/CommandParser.js +++ b/engine/modules/entities/src/main/resources/view/entity-module/CommandParser.js @@ -10,9 +10,10 @@ function splitOnCharOutsideQuotes (text, charParam) { const res = [] let current = '' let idx = 0 - let lastChar = '' + let isEscaped = false let inQuotes = false + while (idx < text.length) { const char = text[idx++] if (char === charParam) { @@ -20,21 +21,26 @@ function splitOnCharOutsideQuotes (text, charParam) { res.push(current) current = '' } else { + if (isEscaped) { + current += '\\' + isEscaped = false + } current += char } - } else if (char === "'" && lastChar !== '\\') { + } else if (char === "'" && !isEscaped) { inQuotes = !inQuotes current += char - } else if (lastChar === '\\') { - if (char === "'") { - current += "\\'" - } else { + } else if (isEscaped) { current += '\\' + char - } - } else if (char !== '\\') { + isEscaped = false + } else if (!isEscaped && char === '\\') { + isEscaped = true + } else { current += char } - lastChar = char + } + if (isEscaped) { + current += '\\' } res.push(current) return res diff --git a/engine/modules/entities/src/main/resources/view/entity-module/properties.js b/engine/modules/entities/src/main/resources/view/entity-module/properties.js index f4800c1d..23b5a114 100644 --- a/engine/modules/entities/src/main/resources/view/entity-module/properties.js +++ b/engine/modules/entities/src/main/resources/view/entity-module/properties.js @@ -153,12 +153,10 @@ export const PROPERTIES = { } function unescape (text) { - // replace \' by ' - const unescaped = text.split("\\'").join("'") - - if (unescaped.includes(' ') || unescaped.includes(';') || unescaped.includes('\n')) { - return unescaped.slice(1, unescaped.length - 1) - } else { - return unescaped + if (text.length >= 2 && text[0] === "'" && text[text.length - 1] === "'") { + const unescaped = text.slice(1, text.length - 1) + // replace \' by ' + return unescaped.split("\\'").join("'") } + return text } From 084c0bceee8f38eae2b5a95048e6e7ed94196db9 Mon Sep 17 00:00:00 2001 From: Julien Poulton Date: Thu, 7 Sep 2023 10:58:43 +0200 Subject: [PATCH 2/2] doc(sdk): v4.4.4 --- playground/misc/misc-3-release-notes.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/playground/misc/misc-3-release-notes.md b/playground/misc/misc-3-release-notes.md index e6615e28..74d3ce66 100644 --- a/playground/misc/misc-3-release-notes.md +++ b/playground/misc/misc-3-release-notes.md @@ -2,6 +2,12 @@ The CodinGame SDK is regularly updated and improved. This document lets you know what changed in the latest releases. +## 4.4.4 + +### 🐞 Bug fix + +- Fixed `Text` not displaying or duplicating backslash characters in the viewer + ## 4.4.3 ### 🐞 Bug fix