ScriptLoader.java 3.06 KB
Newer Older
1
2
3
4
5
6
package emu.grasscutter.scripts;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
Melledy's avatar
Melledy committed
7
import java.util.Arrays;
8
9
10
11
12
13
14
15
16
import java.util.HashMap;
import java.util.Map;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

Melledy's avatar
Melledy committed
17
import org.luaj.vm2.LuaTable;
18
19
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
Melledy's avatar
Melledy committed
20
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
21
22
import org.luaj.vm2.script.LuajContext;

23
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
24
25
26
27
import emu.grasscutter.game.props.EntityType;
import emu.grasscutter.scripts.constants.EventType;
import emu.grasscutter.scripts.constants.ScriptGadgetState;
import emu.grasscutter.scripts.constants.ScriptRegionShape;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import emu.grasscutter.scripts.serializer.LuaSerializer;
import emu.grasscutter.scripts.serializer.Serializer;

public class ScriptLoader {
	private static ScriptEngineManager sm;
	private static ScriptEngine engine;
	private static ScriptEngineFactory factory;
	private static String fileType;
	private static Serializer serializer;
	
	private static Map<String, CompiledScript> scripts = new HashMap<>();
	
	public synchronized static void init() throws Exception {
		if (sm != null) {
			throw new Exception("Script loader already initialized");
		}
		
45
		// Create script engine
46
47
48
		sm = new ScriptEngineManager();
        engine = sm.getEngineByName("luaj");
        factory = getEngine().getFactory();
49
50
        
        // Lua stuff
51
52
        fileType = "lua";
        serializer = new LuaSerializer();
53
54
55
56
57
58
59
60
61
        
        // Set engine to replace require as a temporary fix to missing scripts
        LuajContext ctx = (LuajContext) engine.getContext();
		ctx.globals.set("require", new OneArgFunction() {
		    @Override
		    public LuaValue call(LuaValue arg0) {
		        return LuaValue.ZERO;
		    }
		});
Melledy's avatar
Melledy committed
62
63
64
65
66
67
68
69
		
		LuaTable table = new LuaTable();
		Arrays.stream(EntityType.values()).forEach(e -> table.set(e.name().toUpperCase(), e.getValue()));
		ctx.globals.set("EntityType", table);
		
		ctx.globals.set("EventType", CoerceJavaToLua.coerce(new EventType())); // TODO - make static class to avoid instantiating a new class every scene
		ctx.globals.set("GadgetState", CoerceJavaToLua.coerce(new ScriptGadgetState()));
		ctx.globals.set("RegionShape", CoerceJavaToLua.coerce(new ScriptRegionShape()));
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	}
	
	public static ScriptEngine getEngine() {
		return engine;
	}
	
	public static String getScriptType() {
		return fileType;
	}

	public static Serializer getSerializer() {
		return serializer;
	}

	public static CompiledScript getScriptByPath(String path) {
		CompiledScript sc = scripts.get(path);
		
		Grasscutter.getLogger().info("Loaded " + path);
		
		if (sc == null) {
			File file = new File(path);
			
			if (!file.exists()) return null;
			
			try (FileReader fr = new FileReader(file)) {
				sc = ((Compilable) getEngine()).compile(fr);
				scripts.put(path, sc);
			} catch (Exception e) {
				//e.printStackTrace();
				return null;
			}
		}
		
		return sc;
	}
}