From 498c3b84bc2f882cabac6bb09d74f96820d467a5 Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Thu, 12 Sep 2019 00:17:07 -0700 Subject: [PATCH] + Added lua reading and writing basics --- src/commands/dew/CommandDew.java | 2 +- src/commands/dew/CommandDewLuaTest.java | 57 +++++++++++++ src/commands/dew/CommandDewStart.java | 36 -------- src/commands/dew/core/impl/DewLuaCore.java | 85 ++++++++++++------- .../dew/core/messages/DewMessageMovement.java | 8 ++ 5 files changed, 121 insertions(+), 67 deletions(-) create mode 100644 src/commands/dew/CommandDewLuaTest.java delete mode 100644 src/commands/dew/CommandDewStart.java create mode 100644 src/commands/dew/core/messages/DewMessageMovement.java diff --git a/src/commands/dew/CommandDew.java b/src/commands/dew/CommandDew.java index a6a6a9e..55c5a36 100644 --- a/src/commands/dew/CommandDew.java +++ b/src/commands/dew/CommandDew.java @@ -28,7 +28,7 @@ public class CommandDew extends Command { { super(level, rating); - framework.addCommand("start", new CommandDewStart(KittyRole.Dev, KittyRating.Safe)); + framework.addCommand("luatest", new CommandDewLuaTest(KittyRole.Dev, KittyRating.Safe)); } } diff --git a/src/commands/dew/CommandDewLuaTest.java b/src/commands/dew/CommandDewLuaTest.java new file mode 100644 index 0000000..c3eddd9 --- /dev/null +++ b/src/commands/dew/CommandDewLuaTest.java @@ -0,0 +1,57 @@ +package commands.dew; + +import commands.dew.core.data.*; +import commands.dew.core.impl.DewLuaCore; +import core.SubCommand; +import core.SubCommandFormattable; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; +import dataStructures.UserInput; + +public class CommandDewLuaTest extends SubCommand +{ + public CommandDewLuaTest(KittyRole roleLevel, KittyRating contentRating) + { + super(roleLevel, contentRating); + } + + private String asCodeBlock(String string) + { + return "```lua\n" + string + "```"; + } + + private String asCodeLine(String string) + { + return "`" + string + "`"; + } + + @Override + public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) + { + String out = ""; + + try + { + out += "**Testing writing...\n**"; + String out1 = DewLuaCore.toLua(new DewMap()); + out += asCodeBlock(out1); + + String out2 = DewLuaCore.toLua(new DewMovementInfo()); + out += asCodeBlock(out2); + + out += "**Testing Reading...\n**"; + out += asCodeLine(DewLuaCore.fromLua(DewMovementInfo.class, out1).toString()) + "\n"; + out += asCodeLine(DewLuaCore.fromLua(DewMovementInfo.class, out2).toString()) + "\n"; + } + catch (Exception e) + { + e.printStackTrace(System.err); + } + + + return new SubCommandFormattable(out); + } +} diff --git a/src/commands/dew/CommandDewStart.java b/src/commands/dew/CommandDewStart.java deleted file mode 100644 index f7d7f4a..0000000 --- a/src/commands/dew/CommandDewStart.java +++ /dev/null @@ -1,36 +0,0 @@ -package commands.dew; - -import commands.dew.core.data.*; -import commands.dew.core.impl.DewLuaCore; -import core.SubCommand; -import core.SubCommandFormattable; -import dataStructures.KittyChannel; -import dataStructures.KittyGuild; -import dataStructures.KittyRating; -import dataStructures.KittyRole; -import dataStructures.KittyUser; -import dataStructures.UserInput; - -public class CommandDewStart extends SubCommand -{ - public CommandDewStart(KittyRole roleLevel, KittyRating contentRating) - { - super(roleLevel, contentRating); - } - - private String asCodeBlock(String string) - { - return "```lua\n" + string + "```"; - } - - @Override - public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) - { - String out = ""; - - out += asCodeBlock(DewLuaCore.toLua(new DewMap())); - out += asCodeBlock(DewLuaCore.toLua(new DewMovementInfo())); - - return new SubCommandFormattable(out); - } -} diff --git a/src/commands/dew/core/impl/DewLuaCore.java b/src/commands/dew/core/impl/DewLuaCore.java index f5f3769..d1fe643 100644 --- a/src/commands/dew/core/impl/DewLuaCore.java +++ b/src/commands/dew/core/impl/DewLuaCore.java @@ -1,36 +1,56 @@ package commands.dew.core.impl; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Varargs; +import org.luaj.vm2.lib.jse.JsePlatform; import commands.dew.core.api.IDewLuaCore; import commands.dew.core.api.IDewLuaSerializable; public class DewLuaCore implements IDewLuaCore { + private static Globals globalLuaCore = null; + public static String toLua(IDewLuaSerializable item) { - return toLua(item, 1); + return toLua(item, 0); + } + + private static String tab(int num) + { + String line = ""; + + for(int i = 0; i < num; ++i) + line += "\t"; + + return line; } public static String toLua(IDewLuaSerializable item, int tabs) { - String out = "{"; + String out = tab(tabs) + "return {\n"; + // Set up type information + out += tab(tabs + 1) + "type=\"" + item.getClass().getTypeName() + "\",\n"; + + + // Set up data information + out += tab(tabs + 1) + "data={\n"; + + // Write data Field[] fields = item.getClass().getDeclaredFields(); for(int i = 0; i < fields.length; ++i) { Field current = fields[i]; try { - String line = "\n"; - - String fieldTypeRaw = current.getType().toString().toLowerCase(); - int fieldLastDotIndex = fieldTypeRaw.lastIndexOf('.'); - String fieldType = fieldLastDotIndex <= 0 ? fieldTypeRaw : fieldTypeRaw.substring(fieldLastDotIndex + 1); - - for(int j = 0; j < tabs; ++j) - line += "\t"; + String line = ""; + String fieldType = current.getType().toString().toLowerCase(); + line += tab(tabs + 2); line += current.getName() + "="; switch(fieldType) @@ -39,12 +59,10 @@ public class DewLuaCore implements IDewLuaCore line += "" + current.get(item); break; - case "string": + case "class java.lang.string": String content = (String)current.get(item); - if(content == null) - line += "nil"; - else if(content.length() == 0) + if(content == null || content.length() == 0) line += "\"\""; else line += "\"" + content + "\""; @@ -56,40 +74,47 @@ public class DewLuaCore implements IDewLuaCore break; default: - System.out.print("Found a type we couldn't parse: Raw - " + fieldTypeRaw + " | Parsed - " + fieldType + "\n"); + line += "nil"; + System.err.print("Type could not be foramtted as lua: " + fieldType + "\n"); break; } // If we're not the last line, add a comma. if(i != fields.length - 1) - { line += ","; - } - out += line; + out += line + "\n"; } catch (Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } } - out += "\n}\n"; + out += tab(tabs + 1) + "}\n"; + out += tab(tabs)+ "}\n"; return out; } - public static IDewLuaSerializable fromLua(String item) + @SuppressWarnings({ "null", "unchecked" }) + public static T fromLua(T t, String lua) { - for(Field field : item.getClass().getDeclaredFields()) + if(globalLuaCore == null) { - try - { - System.out.println(field.getName() + " " + field.getType() + " " + field.get(item)); - } - catch (IllegalArgumentException | IllegalAccessException e) - { - e.printStackTrace(); - } + globalLuaCore = JsePlatform.standardGlobals(); + } + + LuaValue val = globalLuaCore.load(lua).call(); + String type = val.get("type").toString(); + + try + { + Class concreteClass = Class.forName(type); + return (T)concreteClass; + } + catch (ClassNotFoundException e) + { + e.printStackTrace(System.err); } return null; diff --git a/src/commands/dew/core/messages/DewMessageMovement.java b/src/commands/dew/core/messages/DewMessageMovement.java new file mode 100644 index 0000000..ddd9abf --- /dev/null +++ b/src/commands/dew/core/messages/DewMessageMovement.java @@ -0,0 +1,8 @@ +package commands.dew.core.messages; + +import commands.dew.core.api.IDewLuaSerializable; + +public class DewMessageMovement implements IDewLuaSerializable +{ + +}