Skip to content

Commit

Permalink
bad bad MainAdd.js (
Browse files Browse the repository at this point in the history
  • Loading branch information
GlassSpirit committed May 16, 2020
1 parent 154821e commit 3255b4c
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions Common/src/main/java/cz/neumimto/rpg/common/scripting/JSLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;

import javax.inject.Inject;
Expand Down Expand Up @@ -131,11 +133,6 @@ public void initEngine() {
classGenerator.generateDynamicListener(eventListeners);

reloadSkills();

JSObject postInit = (JSObject) scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).get("runPostInitialization");
if (postInit != null && postInit.isFunction()) {
postInit.call(null);
}
} catch (Exception e) {
error("Could not load script engine", e);
}
Expand All @@ -162,33 +159,24 @@ private Path mergeScriptFiles() {
final StringBuilder bigChunkOfCode = new StringBuilder();
bigChunkOfCode.append(assetService.getAssetAsString("Main.js")).append(System.lineSeparator());

Path additionalMainPath = Paths.get(scripts_root + File.separator + "MainAdd.js");
if (!additionalMainPath.toFile().exists()) assetService.copyToFile("MainAdd.js", additionalMainPath);

try {
for (String line : Files.readAllLines(additionalMainPath))
bigChunkOfCode.append(line).append(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}

try {
Files.walkFileTree(scripts_root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".js") && !file.toString().endsWith("MainAdd.js")) {
Queue<Path> directories = new PriorityQueue<>();
directories.add(scripts_root);
while (directories.size() != 0) {
Files.list(directories.poll()).forEach((file -> {
if (file.toFile().isDirectory()) directories.add(file);
else if (file.toFile().getName().endsWith(".js")) {
bigChunkOfCode.append(System.lineSeparator()).append("// ").append(file.toFile().getName()).append(System.lineSeparator());
for (String line : Files.readAllLines(file))
bigChunkOfCode.append(line).append(System.lineSeparator());
try {
for (String line : Files.readAllLines(file))
bigChunkOfCode.append(line).append(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}));
}

try {
return Files.write(path, bigChunkOfCode.toString().getBytes(), StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new RuntimeException("Could not write .deployed.js ", e);
Expand Down

0 comments on commit 3255b4c

Please sign in to comment.