Skip to content

Commit

Permalink
allow unsafe injection of lua files
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed Mar 3, 2024
1 parent 9bbab58 commit e17502c
Showing 1 changed file with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -477,6 +475,7 @@ protected void injectMapData(WurstGui gui, Optional<File> testMap, CompilationRe
String mapScriptName;
if (runArgs.isLua()) {
mapScriptName = "war3map.lua";
injectExternalLuaFiles(result.script);
} else {
mapScriptName = "war3map.j";
}
Expand All @@ -490,4 +489,42 @@ protected void injectMapData(WurstGui gui, Optional<File> 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);
}
});
}
}
}


}

0 comments on commit e17502c

Please sign in to comment.