From e17502cc29a5830dce10d7ba082d70b2c4ede3d8 Mon Sep 17 00:00:00 2001 From: Frotty Date: Sun, 3 Mar 2024 14:36:42 +0100 Subject: [PATCH] allow unsafe injection of lua files --- .../languageserver/requests/MapRequest.java | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java index f0bdc6198..4b3b1227e 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java @@ -45,11 +45,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.time.Duration; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -477,6 +475,7 @@ protected void injectMapData(WurstGui gui, Optional testMap, CompilationRe String mapScriptName; if (runArgs.isLua()) { mapScriptName = "war3map.lua"; + injectExternalLuaFiles(result.script); } else { mapScriptName = "war3map.j"; } @@ -490,4 +489,42 @@ protected void injectMapData(WurstGui gui, Optional testMap, CompilationRe mpqEditor.insertFile(mapScriptName, result.script); } } + + private void injectExternalLuaFiles(File script) { + File luaDir; + try { + luaDir = new File(workspaceRoot.getFile(), "lua"); + } catch (FileNotFoundException e) { + throw new RuntimeException("Cannot get build dir", e); + } + if (luaDir.exists()) { + File[] children = luaDir.listFiles(); + if (children != null) { + Arrays.stream(children).forEach(child -> { + try { + byte[] bytes = java.nio.file.Files.readAllBytes(child.toPath()); + if (child.getName().startsWith("pre_")) { + byte[] existingBytes = java.nio.file.Files.readAllBytes(script.toPath()); + java.nio.file.Files.write( + script.toPath(), + bytes); + java.nio.file.Files.write( + script.toPath(), + existingBytes, + StandardOpenOption.APPEND); + } else { + java.nio.file.Files.write( + script.toPath(), + bytes, + StandardOpenOption.APPEND); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + } + } + + }