Skip to content

Commit ddf7ce1

Browse files
committed
Merge branch 'master' into alpha
2 parents 11af736 + a9ecbda commit ddf7ce1

File tree

11 files changed

+403
-0
lines changed

11 files changed

+403
-0
lines changed

modules/dist/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
<type>zip</type>
6464
<version>${project.version}</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>${project.groupId}</groupId>
68+
<artifactId>scriptrunner</artifactId>
69+
<type>zip</type>
70+
<version>${project.version}</version>
71+
</dependency>
6672
<dependency>
6773
<groupId>${project.groupId}</groupId>
6874
<artifactId>serverpacks</artifactId>

modules/mods/scriptrunner/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>scriptrunner</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<name>scriptrunner</name>
9+
10+
<parent>
11+
<groupId>org.gotti.wurmunlimited</groupId>
12+
<artifactId>server-modlauncher-parent</artifactId>
13+
<version>0.22</version>
14+
<relativePath>../../..</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.gotti.wurmunlimited</groupId>
20+
<artifactId>server-modlauncher</artifactId>
21+
<version>0.22</version>
22+
</dependency>
23+
</dependencies>
24+
25+
<properties>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
</properties>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<artifactId>maven-assembly-plugin</artifactId>
33+
<version>2.6</version>
34+
<configuration>
35+
<descriptors>
36+
<descriptor>src/assembly/mod-package.xml</descriptor>
37+
</descriptors>
38+
<finalName>${project.name}-${project.version}</finalName>
39+
<appendAssemblyId>false</appendAssemblyId>
40+
</configuration>
41+
<executions>
42+
<execution>
43+
<id>make-assembly</id>
44+
<phase>package</phase>
45+
<goals>
46+
<goal>single</goal>
47+
</goals>
48+
</execution>
49+
</executions>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
4+
<id>mod</id>
5+
<includeBaseDirectory>false</includeBaseDirectory>
6+
<formats>
7+
<format>zip</format>
8+
</formats>
9+
<fileSets>
10+
<fileSet>
11+
<directory>${project.basedir}/src/dist</directory>
12+
<outputDirectory>mods/</outputDirectory>
13+
</fileSet>
14+
</fileSets>
15+
<files>
16+
<file>
17+
<source>${project.build.directory}/${project.name}-${project.version}.jar</source>
18+
<outputDirectory>mods/${project.name}</outputDirectory>
19+
<destName>${project.name}.jar</destName>
20+
</file>
21+
</files>
22+
</assembly>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
classname=org.gotti.wurmunlimited.mods.scriptrunner.ScriptRunnerMod
2+
classpath=scriptrunner.jar
3+
#
4+
# Set the scripts folder
5+
#
6+
#scriptsFolder=scriptrunner/scripts
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Greet the player with a personal message
3+
//
4+
5+
var ProtoConstants = Packages.com.wurmonline.shared.constants.ProtoConstants;
6+
7+
function onPlayerLogin(player) {
8+
var message = "Welcome to Wurm, " + player.getName() + "!";
9+
player.getCommunicator().sendSafeServerMessage(message, ProtoConstants.M_SYSTEM);
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Elevate all logged in players to admin level
3+
//
4+
5+
var ProtoConstants = Packages.com.wurmonline.shared.constants.ProtoConstants;
6+
7+
function onPlayerLogin(player) {
8+
if (player.getPower() < 5) {
9+
player.setPower(5);
10+
11+
var message = "You are granted admin powers. Use them wisely!";
12+
player.getCommunicator().sendSafeServerMessage(message, ProtoConstants.M_SYSTEM);
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// log all player messages to player.playername.channel
3+
//
4+
5+
var Logger = Packages.java.util.logging.Logger;
6+
var Level = Packages.java.util.logging.Level;
7+
8+
function onPlayerMessage(communicator, message, title) {
9+
var logger = "players." + communicator.getPlayer().getName().toLowerCase() + "." + title.replace(/^:/,"").toLowerCase();
10+
Logger.getLogger(logger).log(Level.INFO, message);
11+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.gotti.wurmunlimited.mods.scriptrunner;
2+
3+
import java.io.IOException;
4+
import java.io.Reader;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import javax.script.Invocable;
11+
import javax.script.ScriptEngine;
12+
import javax.script.ScriptEngineManager;
13+
import javax.script.ScriptException;
14+
15+
public class ScriptManager {
16+
17+
private static ScriptManager instance;
18+
19+
private ScriptEngineManager manager;
20+
private Map<Path, ScriptEngine> engines;
21+
22+
private ScriptManager() {
23+
this.manager = new ScriptEngineManager();
24+
this.engines = new HashMap<>();
25+
}
26+
27+
public static synchronized ScriptManager getInstance() {
28+
if (instance == null) {
29+
instance = new ScriptManager();
30+
}
31+
return instance;
32+
}
33+
34+
public ScriptEngine refresh(Path scriptPath) throws IOException, ScriptException {
35+
ScriptEngine engine = manager.getEngineByName("nashorn");
36+
37+
try (Reader reader = Files.newBufferedReader(scriptPath)) {
38+
engine.eval(reader);
39+
}
40+
41+
this.engines.put(scriptPath, engine);
42+
return engine;
43+
}
44+
45+
public Object invoke(Path scriptPath, String methodName, Map<String, Object> context, Object... args) throws IOException, ScriptException {
46+
ScriptEngine engine = engines.get(scriptPath);
47+
if (engine == null) {
48+
engine = refresh(scriptPath);
49+
}
50+
51+
synchronized (engine) {
52+
engine.put("context", context);
53+
54+
Invocable invocable = (Invocable) engine;
55+
try {
56+
return invocable.invokeFunction(methodName, args);
57+
} catch (NoSuchMethodException e) {
58+
throw new ScriptException(e);
59+
}
60+
}
61+
}
62+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.gotti.wurmunlimited.mods.scriptrunner;
2+
3+
import java.io.IOException;
4+
import java.nio.file.DirectoryStream;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.attribute.FileTime;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.StreamSupport;
16+
17+
import javax.script.ScriptException;
18+
19+
public class ScriptRunner {
20+
21+
private static class ScriptState {
22+
private Path file;
23+
boolean checked = false;
24+
private long size;
25+
private FileTime mtime;
26+
27+
public ScriptState(Path file) {
28+
this.file = file;
29+
}
30+
31+
public boolean check() throws IOException {
32+
if (checked && size == Files.size(file) && this.mtime.equals(Files.getLastModifiedTime(file))) {
33+
return true;
34+
}
35+
this.checked = true;
36+
this.size = Files.size(file);
37+
this.mtime = Files.getLastModifiedTime(file);
38+
return false;
39+
}
40+
}
41+
42+
private final boolean refresh;
43+
private final Path folder;
44+
private final Map<Path, ScriptState> states;
45+
46+
private List<Path> scripts = Collections.emptyList();
47+
private String methodName;
48+
49+
public ScriptRunner(Path folder, String methodName, boolean refresh) {
50+
this.refresh = refresh;
51+
this.folder = folder;
52+
this.methodName = methodName;
53+
this.states = new HashMap<>();
54+
55+
refreshScriptNames();
56+
}
57+
58+
public void refreshScriptNames() {
59+
if (!Files.exists(folder)) {
60+
scripts = Collections.emptyList();
61+
return;
62+
}
63+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, "*.js")) {
64+
scripts = StreamSupport.stream(stream.spliterator(), false)
65+
.filter(Files::isRegularFile)
66+
.collect(Collectors.toList());
67+
} catch (IOException e) {
68+
throw new RuntimeException(e);
69+
}
70+
}
71+
72+
private Object runScript(Path file, Map<String, Object> context, Object... args) {
73+
try {
74+
if (refresh && !states.computeIfAbsent(file, f -> new ScriptState(f)).check()) {
75+
ScriptManager.getInstance().refresh(file);
76+
}
77+
return ScriptManager.getInstance().invoke(file, methodName, context, args);
78+
} catch (IOException | ScriptException e) {
79+
String logger = String.format("%s.%s.%s", ScriptRunner.class.getName(), methodName, file.getFileName());
80+
Logger.getLogger(logger).log(Level.SEVERE, e.getMessage(), e);
81+
return null;
82+
}
83+
}
84+
85+
public List<Object> runScripts(Object... args) {
86+
if (refresh) {
87+
refreshScriptNames();
88+
}
89+
Map<String, Object> context = new HashMap<>();
90+
return scripts.stream().map(path -> runScript(path, context, args)).collect(Collectors.toList());
91+
}
92+
93+
}

0 commit comments

Comments
 (0)