From c63f631c6f8b277d02eb64a641e6d20fd4d9a87d Mon Sep 17 00:00:00 2001 From: Julien Poulton Date: Thu, 7 Sep 2023 10:55:45 +0200 Subject: [PATCH] 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 }