[Refactoring] improved JNLua static class handling

This commit is contained in:
hartie95 2023-11-06 02:42:22 +01:00
parent 3e0b9b0eb6
commit c53939aa7c
3 changed files with 28 additions and 11 deletions

View File

@ -1,11 +1,17 @@
package org.anime_game_servers.jnlua_engine;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.val;
import org.terasology.jnlua.Converter;
import org.terasology.jnlua.DefaultConverter;
import org.terasology.jnlua.LuaState;
import org.terasology.jnlua.NamedJavaFunction;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class JNLuaConverter implements Converter {
@ -33,11 +39,14 @@ public class JNLuaConverter implements Converter {
}
return;
}
} /*else if (o instanceof ScriptLib lib) {
} else if (o instanceof StaticClassWrapper staticClassWrapper) {
luaState.newTable();
val methods = lib.getClass().getMethods();
Arrays.stream(methods).forEach(m -> {
val isStatic = Modifier.isStatic(m.getModifiers());
val staticClass = staticClassWrapper.getStaticClass();
val methods = staticClass.getMethods();
// todo handle static variables too
Arrays.stream(methods)
.filter(method -> Modifier.isStatic(method.getModifiers()))
.forEach(m -> {
class TempFunc implements NamedJavaFunction {
@Override
public String getName() {
@ -53,8 +62,7 @@ public class JNLuaConverter implements Converter {
args.add(luaState.checkJavaObject(i + 1, Object.class));
}
try {
Object caller = isStatic ? null : o;
Object ret = m.invoke(caller, args.toArray());
Object ret = m.invoke(null, args.toArray());
luaState.pushJavaObject(ret);
} catch (Exception e) {
//LuaEngine.logger.error("Error on invoking binding function. ", e);
@ -70,7 +78,7 @@ public class JNLuaConverter implements Converter {
);
return;
}*/
}
defaultConverter.convertJavaObject(luaState, o);
}
}

View File

@ -66,11 +66,9 @@ public class JNLuaEngine implements LuaEngine {
@Override
public boolean addGlobalStaticClass(String name, Class<?> staticClass) {
try {
bindings.put(name, staticClass.getConstructor().newInstance());
bindings.put(name, new StaticClassWrapper(staticClass));
return true;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
//logger.error("Failed to add static class to lua engine: " + name, e);
} catch (Exception e) {
}
return false;
}

View File

@ -0,0 +1,11 @@
package org.anime_game_servers.jnlua_engine;
import lombok.AllArgsConstructor;
import lombok.Data;
import javax.annotation.Nonnull;
@Data @AllArgsConstructor
public class StaticClassWrapper {
@Nonnull private Class<?> staticClass;
}