Skip to content

Commit 9268ce9

Browse files
committed
feat: 调整引擎初始化逻辑
Signed-off-by: MiaoWoo <admin@yumc.pw>
1 parent 3e4453a commit 9268ce9

13 files changed

Lines changed: 241 additions & 126 deletions

pom.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>pw.yumc</groupId>
44
<artifactId>MiaoScript</artifactId>
5-
<version>0.22.0</version>
5+
<version>0.22.3</version>
66
<developers>
77
<developer>
88
<id>502647092</id>
@@ -50,6 +50,7 @@
5050
<properties>
5151
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
5252
<update.changes>
53+
§622-05-25 §afeat: 兼容 1.7.10-1.18.2 版本;
5354
§622-05-21 §afeat: 优化 框架加载逻辑;
5455
§622-05-20 §afeat: 调整 require 主包逻辑;
5556
§622-04-09 §afeat: 优化 引擎初始化逻辑;
@@ -196,7 +197,8 @@
196197
<dependency>
197198
<groupId>org.projectlombok</groupId>
198199
<artifactId>lombok</artifactId>
199-
<version>1.18.22</version>
200+
<version>1.18.24</version>
201+
<scope>compile</scope>
200202
</dependency>
201203
<dependency>
202204
<groupId>org.kamranzafar</groupId>
@@ -219,6 +221,7 @@
219221
<groupId>net.md-5</groupId>
220222
<artifactId>bungeecord-api</artifactId>
221223
<version>1.16-R0.4</version>
224+
<scope>compile</scope>
222225
</dependency>
223226
<dependency>
224227
<groupId>cn.nukkit</groupId>
@@ -229,7 +232,7 @@
229232
<dependency>
230233
<groupId>org.springframework</groupId>
231234
<artifactId>spring-websocket</artifactId>
232-
<version>5.3.18</version>
235+
<version>5.3.19</version>
233236
<scope>compile</scope>
234237
</dependency>
235238
<dependency>
@@ -238,10 +241,5 @@
238241
<version>9.0.35</version>
239242
<scope>compile</scope>
240243
</dependency>
241-
<dependency>
242-
<groupId>com.zaxxer</groupId>
243-
<artifactId>HikariCP</artifactId>
244-
<version>4.0.3</version>
245-
</dependency>
246244
</dependencies>
247245
</project>

src/main/java/pw/yumc/MiaoScript/MiaoScriptBukkit.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.SneakyThrows;
44
import org.bukkit.plugin.java.JavaPlugin;
5+
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
56
import pw.yumc.MiaoScript.api.ScriptEngine;
67

78
/**
@@ -17,7 +18,7 @@ public class MiaoScriptBukkit extends JavaPlugin {
1718
public MiaoScriptBukkit() {
1819
ClassLoader origin = Thread.currentThread().getContextClassLoader();
1920
Thread.currentThread().setContextClassLoader(getClassLoader());
20-
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
21+
engine = MiaoScriptAPI.createEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
2122
Thread.currentThread().setContextClassLoader(origin);
2223
engine.loadEngine();
2324
}

src/main/java/pw/yumc/MiaoScript/MiaoScriptBungee.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.SneakyThrows;
44
import net.md_5.bungee.api.plugin.Plugin;
5+
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
56
import pw.yumc.MiaoScript.api.ScriptEngine;
67

78
/**
@@ -16,7 +17,7 @@ public class MiaoScriptBungee extends Plugin {
1617
@SneakyThrows
1718
public MiaoScriptBungee() {
1819
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
19-
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
20+
engine = MiaoScriptAPI.createEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
2021
engine.loadEngine();
2122
}
2223

src/main/java/pw/yumc/MiaoScript/MiaoScriptNukkit.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.nukkit.plugin.PluginBase;
44
import lombok.SneakyThrows;
5+
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
56
import pw.yumc.MiaoScript.api.ScriptEngine;
67

78
/**
@@ -13,7 +14,7 @@ public class MiaoScriptNukkit extends PluginBase {
1314
@SneakyThrows
1415
public MiaoScriptNukkit() {
1516
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
16-
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), super.getLogger(), this);
17+
engine = MiaoScriptAPI.createEngine(getDataFolder().getCanonicalPath(), super.getLogger(), this);
1718
engine.loadEngine();
1819
}
1920

src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import org.spongepowered.api.config.ConfigDir;
77
import org.spongepowered.api.event.Listener;
88
import org.spongepowered.api.event.game.GameReloadEvent;
9+
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
910
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
10-
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
1111
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
1212
import org.spongepowered.api.plugin.Plugin;
1313
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
@@ -33,14 +33,14 @@ public class MiaoScriptSponge {
3333

3434
@Listener
3535
@SneakyThrows
36-
public void onStarting(GameStartingServerEvent event) {
37-
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger, this);
36+
public void onPreInitialization(GamePreInitializationEvent event) {
37+
engine = MiaoScriptAPI.createEngine(pluginConfigDir.getCanonicalPath(), logger, this);
3838
engine.loadEngine();
3939
}
4040

4141
@Listener
4242
@SneakyThrows
43-
public void onStart(GameStartedServerEvent event) {
43+
public void onStarted(GameStartedServerEvent event) {
4444
engine.enableEngine();
4545
}
4646

@@ -55,7 +55,7 @@ public void onStop(GameStoppingServerEvent event) {
5555
@SneakyThrows
5656
public void reload(GameReloadEvent event) {
5757
engine.disableEngine();
58-
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger, this);
58+
System.gc();
5959
engine.loadEngine();
6060
engine.enableEngine();
6161
}

src/main/java/pw/yumc/MiaoScript/MiaoScriptSpring.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.stereotype.Component;
88
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
9+
import pw.yumc.MiaoScript.api.MiaoScriptAPI;
910
import pw.yumc.MiaoScript.api.ScriptEngine;
1011

1112
import java.io.File;
@@ -16,7 +17,7 @@ public class MiaoScriptSpring {
1617
@Bean
1718
@SneakyThrows
1819
public ScriptEngine buildScriptEngine(ApplicationContext applicationContext) {
19-
return new ScriptEngine(new File("MiaoScript").getCanonicalPath(), log, applicationContext);
20+
return MiaoScriptAPI.createEngine(new File("MiaoScript").getCanonicalPath(), log, applicationContext);
2021
}
2122

2223
@Bean

src/main/java/pw/yumc/MiaoScript/api/Base.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ public String getVersion() {
2525
}
2626

2727
public Class<?> getClass(String name) throws ClassNotFoundException {
28-
return Class.forName(name);
28+
try {
29+
return Class.forName(name);
30+
} catch (Throwable ignored) {
31+
}
32+
try {
33+
return Class.forName(name, true, instance.getClass().getClassLoader());
34+
} catch (Throwable ex) {
35+
return Class.forName(name, true, instance.getClass().getClassLoader().getParent());
36+
}
2937
}
3038

3139
public Object getInstance() {
@@ -44,6 +52,14 @@ public File[] loadMavenDepend(String groupId, String artifactId, String version)
4452
return MiaoScriptAPI.loadMavenDepend(groupId, artifactId, version);
4553
}
4654

55+
public File[] loadMavenDepend(String groupId, String artifactId, String version, ClassLoader classLoader) {
56+
return MiaoScriptAPI.loadMavenDepend(groupId, artifactId, version, classLoader);
57+
}
58+
59+
public File[] parentLoadMavenDepend(String groupId, String artifactId, String version) {
60+
return MiaoScriptAPI.parentLoadMavenDepend(groupId, artifactId, version);
61+
}
62+
4763
public String read(String path) throws IOException {
4864
return read(Paths.get(path));
4965
}

src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public class MiaoScriptAPI {
1414
private static ScriptEngine scriptEngine;
1515
private static PluginManager pluginManager;
1616

17+
public static ScriptEngine createEngine(String root, Object logger, Object instance) {
18+
MiaoScriptAPI.scriptEngine = new ScriptEngine(root, logger, instance);
19+
return MiaoScriptAPI.scriptEngine;
20+
}
21+
1722
public static String getRoot() {
1823
return root;
1924
}
@@ -45,4 +50,18 @@ public static File[] loadMavenDepend(String groupId, String artifactId, String v
4550
}
4651
return MavenDependLoader.load(MiaoScriptAPI.libPath, groupId, artifactId, version);
4752
}
53+
54+
public static File[] loadMavenDepend(String groupId, String artifactId, String version, ClassLoader classLoader) {
55+
if (root == null || scriptEngine == null) {
56+
throw new IllegalStateException("root can't be null before loadMavenDepend.");
57+
}
58+
return MavenDependLoader.load(MiaoScriptAPI.libPath, groupId, artifactId, version, classLoader);
59+
}
60+
61+
public static File[] parentLoadMavenDepend(String groupId, String artifactId, String version) {
62+
if (root == null || scriptEngine == null) {
63+
throw new IllegalStateException("root can't be null before loadMavenDepend.");
64+
}
65+
return MavenDependLoader.parentLoad(MiaoScriptAPI.libPath, groupId, artifactId, version);
66+
}
4867
}

src/main/java/pw/yumc/MiaoScript/api/ScriptEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ScriptEngine {
2020
private MiaoScriptEngine engine;
2121
private Object future;
2222

23-
public ScriptEngine(String root, Object logger, Object instance) {
23+
ScriptEngine(String root, Object logger, Object instance) {
2424
this.loader = Thread.currentThread().getContextClassLoader();
2525
this.root = root;
2626
this.logger = logger;
@@ -32,7 +32,7 @@ public ScriptEngine(String root, Object logger, Object instance) {
3232
public void createEngine() {
3333
synchronized (logger) {
3434
if (this.engine == null) {
35-
this.engine = new MiaoScriptEngine("nashorn", root);
35+
this.engine = new MiaoScriptEngine(root);
3636
this.engine.put("base", this.base);
3737
this.engine.put("ScriptEngineContextHolder", this);
3838
}

src/main/java/pw/yumc/MiaoScript/api/loader/JarLoader.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.net.URL;
1111

1212
public class JarLoader {
13+
private static sun.misc.Unsafe unsafe;
14+
private static long offset;
15+
private static Object parentUcp;
1316
private static Object ucp;
1417
private static MethodHandle addURLMethodHandle;
1518

@@ -23,12 +26,26 @@ public static File load(File file) {
2326
return file;
2427
}
2528

29+
@SneakyThrows
30+
public static File parentLoad(File file) {
31+
if (parentUcp == null)
32+
throw new IllegalStateException("parentUcp is null.");
33+
addURLMethodHandle.invoke(parentUcp, file.toURI().toURL());
34+
return file;
35+
}
36+
37+
@SneakyThrows
38+
public static File load(File file, ClassLoader loader) {
39+
addURLMethodHandle.invoke(unsafe.getObject(loader, offset), file.toURI().toURL());
40+
return file;
41+
}
42+
2643
private static void initReflect() {
2744
try {
28-
ClassLoader loader = ClassLoader.getSystemClassLoader();
45+
ClassLoader loader = JarLoader.class.getClassLoader();
2946
Field theUnsafe = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
3047
theUnsafe.setAccessible(true);
31-
sun.misc.Unsafe unsafe = (sun.misc.Unsafe) theUnsafe.get(null);
48+
unsafe = (sun.misc.Unsafe) theUnsafe.get(null);
3249
Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
3350
MethodHandles.Lookup lookup = (MethodHandles.Lookup) unsafe.getObject(unsafe.staticFieldBase(field), unsafe.staticFieldOffset(field));
3451
Field ucpField;
@@ -37,11 +54,13 @@ private static void initReflect() {
3754
} catch (NoSuchFieldException e) {
3855
ucpField = loader.getClass().getSuperclass().getDeclaredField("ucp");
3956
}
40-
long offset = unsafe.objectFieldOffset(ucpField);
57+
offset = unsafe.objectFieldOffset(ucpField);
4158
ucp = unsafe.getObject(loader, offset);
4259
Method method = ucp.getClass().getDeclaredMethod("addURL", URL.class);
4360
addURLMethodHandle = lookup.unreflect(method);
44-
} catch (Exception e) {
61+
if (loader.getParent() != null)
62+
parentUcp = unsafe.getObject(loader.getParent(), offset);
63+
} catch (Throwable e) {
4564
throw new RuntimeException(e);
4665
}
4766
}

0 commit comments

Comments
 (0)