+ Added lua reading and writing basics

This commit is contained in:
Matthew Cech
2019-09-12 00:17:07 -07:00
parent 4b8b125d05
commit 498c3b84bc
5 changed files with 121 additions and 67 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ public class CommandDew extends Command {
{ {
super(level, rating); super(level, rating);
framework.addCommand("start", new CommandDewStart(KittyRole.Dev, KittyRating.Safe)); framework.addCommand("luatest", new CommandDewLuaTest(KittyRole.Dev, KittyRating.Safe));
} }
} }
+57
View File
@@ -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);
}
}
-36
View File
@@ -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);
}
}
+55 -30
View File
@@ -1,36 +1,56 @@
package commands.dew.core.impl; package commands.dew.core.impl;
import java.lang.reflect.Field; 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.IDewLuaCore;
import commands.dew.core.api.IDewLuaSerializable; import commands.dew.core.api.IDewLuaSerializable;
public class DewLuaCore implements IDewLuaCore public class DewLuaCore implements IDewLuaCore
{ {
private static Globals globalLuaCore = null;
public static String toLua(IDewLuaSerializable<?> item) 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) 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(); Field[] fields = item.getClass().getDeclaredFields();
for(int i = 0; i < fields.length; ++i) for(int i = 0; i < fields.length; ++i)
{ {
Field current = fields[i]; Field current = fields[i];
try try
{ {
String line = "\n"; String line = "";
String fieldType = current.getType().toString().toLowerCase();
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";
line += tab(tabs + 2);
line += current.getName() + "="; line += current.getName() + "=";
switch(fieldType) switch(fieldType)
@@ -39,12 +59,10 @@ public class DewLuaCore implements IDewLuaCore
line += "" + current.get(item); line += "" + current.get(item);
break; break;
case "string": case "class java.lang.string":
String content = (String)current.get(item); String content = (String)current.get(item);
if(content == null) if(content == null || content.length() == 0)
line += "nil";
else if(content.length() == 0)
line += "\"\""; line += "\"\"";
else else
line += "\"" + content + "\""; line += "\"" + content + "\"";
@@ -56,40 +74,47 @@ public class DewLuaCore implements IDewLuaCore
break; break;
default: 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; break;
} }
// If we're not the last line, add a comma. // If we're not the last line, add a comma.
if(i != fields.length - 1) if(i != fields.length - 1)
{
line += ","; line += ",";
}
out += line; out += line + "\n";
} }
catch (Exception e) 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; return out;
} }
public static <T> IDewLuaSerializable<T> fromLua(String item) @SuppressWarnings({ "null", "unchecked" })
public static <T> T fromLua(T t, String lua)
{ {
for(Field field : item.getClass().getDeclaredFields()) if(globalLuaCore == null)
{ {
try globalLuaCore = JsePlatform.standardGlobals();
{ }
System.out.println(field.getName() + " " + field.getType() + " " + field.get(item));
} LuaValue val = globalLuaCore.load(lua).call();
catch (IllegalArgumentException | IllegalAccessException e) String type = val.get("type").toString();
{
e.printStackTrace(); try
} {
Class<?> concreteClass = Class.forName(type);
return (T)concreteClass;
}
catch (ClassNotFoundException e)
{
e.printStackTrace(System.err);
} }
return null; return null;
@@ -0,0 +1,8 @@
package commands.dew.core.messages;
import commands.dew.core.api.IDewLuaSerializable;
public class DewMessageMovement implements IDewLuaSerializable<DewMessageMovement>
{
}