= read/write first pass - seems to work! :D

This commit is contained in:
Matthew Cech
2019-09-12 02:35:59 -07:00
parent 498c3b84bc
commit 96b4533b39
2 changed files with 52 additions and 12 deletions
+12 -4
View File
@@ -36,15 +36,23 @@ public class CommandDewLuaTest extends SubCommand
try
{
out += "**Testing writing...\n**";
String out1 = DewLuaCore.toLua(new DewMap());
DewMap m = new DewMap();
m.mapHeight = 25;
String out1 = DewLuaCore.toLua(m);
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";
out += "**Testing Reading...**\n";
DewMap map = new DewMap();
DewLuaCore.fromLua(map, out1);
out += "Height from 1st write: " + map.mapHeight + "\n";
DewMovementInfo move = new DewMovementInfo();
DewLuaCore.fromLua(move, out2);
out += move + "\n";
}
catch (Exception e)
{
+40 -8
View File
@@ -69,6 +69,7 @@ public class DewLuaCore implements IDewLuaCore
break;
// wrong name
case "dewmaptile ":
line += "\"" + current.get(item).toString() + "\"";
break;
@@ -96,27 +97,58 @@ public class DewLuaCore implements IDewLuaCore
return out;
}
@SuppressWarnings({ "null", "unchecked" })
public static <T> T fromLua(T t, String lua)
public static <T> void fromLua(T obj, String lua)
{
if(globalLuaCore == null)
{
globalLuaCore = JsePlatform.standardGlobals();
}
LuaValue val = globalLuaCore.load(lua).call();
String type = val.get("type").toString();
LuaValue luaResult = globalLuaCore.load(lua).call();
String type = luaResult.get("type").toString();
try
{
Class<?> concreteClass = Class.forName(type);
return (T)concreteClass;
LuaValue data = luaResult.get("data");
Class<T> concreteClass = (Class<T>) Class.forName(type);
for(Field field : concreteClass.getDeclaredFields())
{
String name = field.getName();
LuaValue value = data.get(name);
if(!value.isnil())
{
String fieldType = field.getType().toString().toLowerCase();
switch(fieldType)
{
case "int":
field.set(obj, value.toint());
break;
case "class java.lang.string":
field.set(obj, value.toString());
break;
// Not the right name
case "dewmaptile":
default:
System.err.print("Could not set field of type: " + fieldType + "\n");
break;
}
catch (ClassNotFoundException e)
}
else
{
System.err.println("Field was not in lua: " + name);
}
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
return null;
}
}