From ea880cc53803fe54756e01ab34dac112a34198d0 Mon Sep 17 00:00:00 2001 From: Frotty Date: Wed, 4 Mar 2026 21:53:12 +0100 Subject: [PATCH] support lua folders --- .../languageserver/requests/MapRequest.java | 67 ++++++++++++------- 1 file changed, 42 insertions(+), 25 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 4fe1544ff..539941668 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 @@ -781,32 +781,49 @@ private void injectExternalLuaFiles(File script) { } 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); - } - }); + if (!luaDir.exists()) { + return; + } + + try (var fileStream = java.nio.file.Files.walk(luaDir.toPath())) { + List luaFiles = fileStream + .filter(java.nio.file.Files::isRegularFile) + .sorted(Comparator.comparing(path -> { + Path relative = luaDir.toPath().relativize(path); + return relative.toString().replace('\\', '/').toLowerCase(Locale.ROOT); + })) + .collect(Collectors.toList()); + + if (luaFiles.isEmpty()) { + return; + } + + List preChunks = new ArrayList<>(); + List postChunks = new ArrayList<>(); + for (Path luaFile : luaFiles) { + byte[] bytes = java.nio.file.Files.readAllBytes(luaFile); + String fileName = luaFile.getFileName().toString(); + if (fileName.startsWith("pre_")) { + preChunks.add(bytes); + } else { + postChunks.add(bytes); + } + } + + if (!preChunks.isEmpty()) { + byte[] existingBytes = java.nio.file.Files.readAllBytes(script.toPath()); + java.nio.file.Files.write(script.toPath(), new byte[0]); + for (byte[] preChunk : preChunks) { + java.nio.file.Files.write(script.toPath(), preChunk, StandardOpenOption.APPEND); + } + java.nio.file.Files.write(script.toPath(), existingBytes, StandardOpenOption.APPEND); } + + for (byte[] postChunk : postChunks) { + java.nio.file.Files.write(script.toPath(), postChunk, StandardOpenOption.APPEND); + } + } catch (IOException e) { + throw new RuntimeException(e); } }