optimized the lua serializer

This commit is contained in:
Akka 2022-05-17 11:17:38 +08:00 committed by Melledy
parent a63ed21213
commit c103841a03
15 changed files with 168 additions and 67 deletions

View File

@ -87,6 +87,7 @@ dependencies {
implementation group: 'org.luaj', name: 'luaj-jse', version: '3.0.1' implementation group: 'org.luaj', name: 'luaj-jse', version: '3.0.1'
implementation group: 'ch.ethz.globis.phtree', name : 'phtree', version: '2.5.0' implementation group: 'ch.ethz.globis.phtree', name : 'phtree', version: '2.5.0'
implementation group: 'com.esotericsoftware', name : 'reflectasm', version: '1.11.9'
protobuf files('proto/') protobuf files('proto/')

View File

@ -241,7 +241,7 @@ public class SceneScriptManager {
} }
public void spawnGadgetsInGroup(SceneGroup group, SceneSuite suite) { public void spawnGadgetsInGroup(SceneGroup group, SceneSuite suite) {
List<SceneGadget> gadgets = group.gadgets; var gadgets = group.gadgets.values();
if (suite != null) { if (suite != null) {
gadgets = suite.sceneGadgets; gadgets = suite.sceneGadgets;

View File

@ -6,6 +6,8 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.SceneIndexManager; import emu.grasscutter.scripts.SceneIndexManager;
import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.scripts.ScriptLoader;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import lombok.Setter;
import lombok.ToString;
import javax.script.Bindings; import javax.script.Bindings;
import javax.script.CompiledScript; import javax.script.CompiledScript;
@ -14,6 +16,8 @@ import java.util.List;
import static emu.grasscutter.Configuration.SCRIPT; import static emu.grasscutter.Configuration.SCRIPT;
@ToString
@Setter
public class SceneBlock { public class SceneBlock {
public int id; public int id;
public Position max; public Position max;

View File

@ -1,7 +1,11 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneConfig { public class SceneConfig {
public Position vision_anchor; public Position vision_anchor;
public Position born_pos; public Position born_pos;

View File

@ -1,5 +1,10 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneGadget extends SceneObject{ public class SceneGadget extends SceneObject{
public int gadget_id; public int gadget_id;
public int state; public int state;

View File

@ -3,19 +3,22 @@ package emu.grasscutter.scripts.data;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.scripts.ScriptLoader;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import lombok.Setter;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import lombok.ToString;
import javax.script.Bindings; import javax.script.Bindings;
import javax.script.CompiledScript; import javax.script.CompiledScript;
import javax.script.ScriptException; import javax.script.ScriptException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static emu.grasscutter.Configuration.SCRIPTS_FOLDER; import static emu.grasscutter.Configuration.SCRIPTS_FOLDER;
@ToString
@Setter
public class SceneGroup { public class SceneGroup {
public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference
@ -27,7 +30,10 @@ public class SceneGroup {
* ConfigId - Monster * ConfigId - Monster
*/ */
public Map<Integer,SceneMonster> monsters; public Map<Integer,SceneMonster> monsters;
public List<SceneGadget> gadgets; /**
* ConfigId - Gadget
*/
public Map<Integer, SceneGadget> gadgets;
public List<SceneTrigger> triggers; public List<SceneTrigger> triggers;
public List<SceneRegion> regions; public List<SceneRegion> regions;
public List<SceneSuite> suites; public List<SceneSuite> suites;
@ -76,8 +82,15 @@ public class SceneGroup {
// Set // Set
monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, bindings.get("monsters")).stream() monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, bindings.get("monsters")).stream()
.collect(Collectors.toMap(x -> x.config_id, y -> y)); .collect(Collectors.toMap(x -> x.config_id, y -> y));
gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets")); monsters.values().forEach(m -> m.groupId = id);
gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets")).stream()
.collect(Collectors.toMap(x -> x.config_id, y -> y));
gadgets.values().forEach(m -> m.groupId = id);
triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers")); triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers"));
triggers.forEach(t -> t.currentGroup = this);
suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites")); suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions")); regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions"));
init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config")); init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config"));
@ -85,35 +98,21 @@ public class SceneGroup {
// Add variables to suite // Add variables to suite
variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables")); variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));
// Add monsters to suite
// Add monsters to suite TODO optimize
Int2ObjectMap<Object> map = new Int2ObjectOpenHashMap<>();
monsters.entrySet().forEach(m -> map.put(m.getValue().config_id, m));
monsters.values().forEach(m -> m.groupId = id);
gadgets.forEach(m -> map.put(m.config_id, m));
gadgets.forEach(m -> m.groupId = id);
for (SceneSuite suite : suites) { for (SceneSuite suite : suites) {
suite.sceneMonsters = new ArrayList<>(suite.monsters.size()); suite.sceneMonsters = new ArrayList<>(
suite.monsters.forEach(id -> { suite.monsters.stream()
Object objEntry = map.get(id.intValue()); .filter(monsters::containsKey)
if (objEntry instanceof Map.Entry<?,?> monsterEntry) { .map(monsters::get)
Object monster = monsterEntry.getValue(); .toList()
if(monster instanceof SceneMonster sceneMonster){ );
suite.sceneMonsters.add(sceneMonster);
}
}
});
suite.sceneGadgets = new ArrayList<>(suite.gadgets.size()); suite.sceneGadgets = new ArrayList<>(
for (int id : suite.gadgets) { suite.gadgets.stream()
try { .filter(gadgets::containsKey)
SceneGadget gadget = (SceneGadget) map.get(id); .map(gadgets::get)
if (gadget != null) { .toList()
suite.sceneGadgets.add(gadget); );
}
} catch (Exception ignored) { }
}
} }
} catch (ScriptException e) { } catch (ScriptException e) {

View File

@ -1,7 +1,10 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position; import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneInitConfig { public class SceneInitConfig {
public int suite; public int suite;
public int end_suite; public int end_suite;

View File

@ -5,6 +5,9 @@ import ch.ethz.globis.phtree.v16.PhTree16;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.SceneIndexManager; import emu.grasscutter.scripts.SceneIndexManager;
import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.scripts.ScriptLoader;
import lombok.Data;
import lombok.Setter;
import lombok.ToString;
import javax.script.Bindings; import javax.script.Bindings;
import javax.script.CompiledScript; import javax.script.CompiledScript;
@ -15,6 +18,8 @@ import java.util.stream.Collectors;
import static emu.grasscutter.Configuration.SCRIPT; import static emu.grasscutter.Configuration.SCRIPT;
@ToString
@Setter
public class SceneMeta { public class SceneMeta {
public SceneConfig config; public SceneConfig config;

View File

@ -1,5 +1,10 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneMonster extends SceneObject{ public class SceneMonster extends SceneObject{
public int monster_id; public int monster_id;
} }

View File

@ -1,7 +1,11 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneObject { public class SceneObject {
public int level; public int level;
public int config_id; public int config_id;
@ -11,5 +15,5 @@ public class SceneObject {
/** /**
* not set by lua * not set by lua
*/ */
public int groupId; public transient int groupId;
} }

View File

@ -5,7 +5,12 @@ import emu.grasscutter.scripts.constants.ScriptRegionShape;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.ints.IntSet;
import lombok.Data;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneRegion { public class SceneRegion {
public int config_id; public int config_id;
public int shape; public int shape;

View File

@ -2,8 +2,11 @@ package emu.grasscutter.scripts.data;
import java.util.List; import java.util.List;
import emu.grasscutter.utils.Position; import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneSuite { public class SceneSuite {
public List<Integer> monsters; public List<Integer> monsters;
public List<Integer> gadgets; public List<Integer> gadgets;

View File

@ -1,5 +1,8 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import lombok.Setter;
@Setter
public class SceneTrigger { public class SceneTrigger {
public String name; public String name;
public int config_id; public int config_id;
@ -8,6 +11,7 @@ public class SceneTrigger {
public String condition; public String condition;
public String action; public String action;
public SceneGroup currentGroup;
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(obj instanceof SceneTrigger sceneTrigger){ if(obj instanceof SceneTrigger sceneTrigger){

View File

@ -1,5 +1,10 @@
package emu.grasscutter.scripts.data; package emu.grasscutter.scripts.data;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
public class SceneVar { public class SceneVar {
public String name; public String name;
public int value; public int value;

View File

@ -1,14 +1,23 @@
package emu.grasscutter.scripts.serializer; package emu.grasscutter.scripts.serializer;
import java.lang.reflect.Field; import com.esotericsoftware.reflectasm.ConstructorAccess;
import java.util.ArrayList; import com.esotericsoftware.reflectasm.MethodAccess;
import java.util.List; import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.FieldDefaults;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class LuaSerializer implements Serializer { public class LuaSerializer implements Serializer {
private final static Map<Class<?>, MethodAccess> methodAccessCache = new ConcurrentHashMap<>();
private final static Map<Class<?>, ConstructorAccess<?>> constructorCache = new ConcurrentHashMap<>();
private final static Map<Class<?>, Map<String, FieldMeta>> fieldMetaCache = new ConcurrentHashMap<>();
@Override @Override
public <T> List<T> toList(Class<T> type, Object obj) { public <T> List<T> toList(Class<T> type, Object obj) {
return serializeList(type, (LuaTable) obj); return serializeList(type, (LuaTable) obj);
@ -20,7 +29,7 @@ public class LuaSerializer implements Serializer {
} }
public <T> List<T> serializeList(Class<T> type, LuaTable table) { public <T> List<T> serializeList(Class<T> type, LuaTable table) {
List<T> list = new ArrayList(); List<T> list = new ArrayList<>();
try { try {
LuaValue[] keys = table.keys(); LuaValue[] keys = table.keys();
@ -70,30 +79,34 @@ public class LuaSerializer implements Serializer {
} }
try { try {
//noinspection ConfusingArgumentToVarargsMethod if(!methodAccessCache.containsKey(type)){
object = type.getDeclaredConstructor().newInstance(null); cacheType(type);
}
var methodAccess = methodAccessCache.get(type);
var fieldMetaMap = fieldMetaCache.get(type);
object = (T) constructorCache.get(type).newInstance();
LuaValue[] keys = table.keys(); LuaValue[] keys = table.keys();
for (LuaValue k : keys) { for (LuaValue k : keys) {
try { try {
Field field = getField(object.getClass(), k.checkjstring()); var keyName = k.checkjstring();
if (field == null) { if(!fieldMetaMap.containsKey(keyName)){
continue; continue;
} }
var fieldMeta = fieldMetaMap.get(keyName);
field.setAccessible(true);
LuaValue keyValue = table.get(k); LuaValue keyValue = table.get(k);
if (keyValue.istable()) { if (keyValue.istable()) {
field.set(object, serialize(field.getType(), keyValue.checktable())); methodAccess.invoke(object, fieldMeta.index, serialize(fieldMeta.getType(), keyValue.checktable()));
} else if (field.getType().equals(float.class)) { } else if (fieldMeta.getType().equals(float.class)) {
field.setFloat(object, keyValue.tofloat()); methodAccess.invoke(object, fieldMeta.index, keyValue.tofloat());
} else if (field.getType().equals(int.class)) { } else if (fieldMeta.getType().equals(int.class)) {
field.setInt(object, keyValue.toint()); methodAccess.invoke(object, fieldMeta.index, keyValue.toint());
} else if (field.getType().equals(String.class)) { } else if (fieldMeta.getType().equals(String.class)) {
field.set(object, keyValue.tojstring()); methodAccess.invoke(object, fieldMeta.index, keyValue.tojstring());
} else { } else {
field.set(object, keyValue); methodAccess.invoke(object, fieldMeta.index, keyValue.tojstring());
} }
} catch (Exception ex) { } catch (Exception ex) {
//ex.printStackTrace(); //ex.printStackTrace();
@ -107,16 +120,57 @@ public class LuaSerializer implements Serializer {
return object; return object;
} }
public <T> Field getField(Class<T> clazz, String name){ public <T> Map<String, FieldMeta> cacheType(Class<T> type){
try{ if(fieldMetaCache.containsKey(type)) {
return clazz.getField(name); return fieldMetaCache.get(type);
} catch (NoSuchFieldException ex) { }
try { if(!constructorCache.containsKey(type)){
return clazz.getDeclaredField(name); constructorCache.putIfAbsent(type, ConstructorAccess.get(type));
} catch (NoSuchFieldException e) { }
// ignore var methodAccess = Optional.ofNullable(methodAccessCache.get(type)).orElse(MethodAccess.get(type));
methodAccessCache.putIfAbsent(type, methodAccess);
var fieldMetaMap = new HashMap<String, FieldMeta>();
var methodNameSet = new HashSet<>(Arrays.stream(methodAccess.getMethodNames()).toList());
Arrays.stream(type.getDeclaredFields())
.filter(field -> methodNameSet.contains(getSetterName(field.getName())))
.forEach(field -> {
var setter = getSetterName(field.getName());
var index = methodAccess.getIndex(setter);
fieldMetaMap.put(field.getName(), new FieldMeta(field.getName(), setter, index, field.getType()));
});
Arrays.stream(type.getFields())
.filter(field -> !fieldMetaMap.containsKey(field.getName()))
.filter(field -> methodNameSet.contains(getSetterName(field.getName())))
.forEach(field -> {
var setter = getSetterName(field.getName());
var index = methodAccess.getIndex(setter);
fieldMetaMap.put(field.getName(), new FieldMeta(field.getName(), setter, index, field.getType()));
});
fieldMetaCache.put(type, fieldMetaMap);
return fieldMetaMap;
}
public String getSetterName(String fieldName){
if(fieldName == null || fieldName.length() == 0){
return null; return null;
} }
if(fieldName.length() == 1){
return "set" + fieldName.toUpperCase();
} }
return "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
}
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
static class FieldMeta{
String name;
String setter;
int index;
Class<?> type;
} }
} }