+ Added lua reading and writing basics
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 <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)
|
||||
{
|
||||
globalLuaCore = JsePlatform.standardGlobals();
|
||||
}
|
||||
|
||||
LuaValue val = globalLuaCore.load(lua).call();
|
||||
String type = val.get("type").toString();
|
||||
|
||||
try
|
||||
{
|
||||
System.out.println(field.getName() + " " + field.getType() + " " + field.get(item));
|
||||
Class<?> concreteClass = Class.forName(type);
|
||||
return (T)concreteClass;
|
||||
}
|
||||
catch (IllegalArgumentException | IllegalAccessException e)
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package commands.dew.core.messages;
|
||||
|
||||
import commands.dew.core.api.IDewLuaSerializable;
|
||||
|
||||
public class DewMessageMovement implements IDewLuaSerializable<DewMessageMovement>
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user