-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix RuntimeException while restoring a saved game #11
Changes from all commits
c26933b
80adc39
44a1895
9de4575
6fcd0c4
ade1b40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,166 @@ | ||
package cz.matejcik.openwig; | ||
|
||
import se.krka.kahlua.stdlib.BaseLib; | ||
import se.krka.kahlua.vm.*; | ||
|
||
import java.io.*; | ||
|
||
public class EventTable implements LuaTable, Serializable { | ||
|
||
public LuaTable table = new LuaTableImpl(); | ||
|
||
private LuaTable metatable = new LuaTableImpl(); | ||
|
||
private static class TostringJavaFunc implements JavaFunction { | ||
|
||
public EventTable parent; | ||
|
||
public TostringJavaFunc (EventTable parent) { | ||
this.parent = parent; | ||
} | ||
|
||
public int call (LuaCallFrame callFrame, int nArguments) { | ||
callFrame.push(parent.luaTostring()); | ||
return 1; | ||
} | ||
}; | ||
|
||
protected String luaTostring () { return "a ZObject instance"; } | ||
|
||
public EventTable() { | ||
metatable.rawset("__tostring", new TostringJavaFunc(this)); | ||
} | ||
|
||
public void serialize (DataOutputStream out) throws IOException { | ||
Engine.instance.savegame.storeValue(table, out); | ||
} | ||
|
||
public void deserialize (DataInputStream in) throws IOException { | ||
Engine.instance.savegame.restoreValue(in, this); | ||
//setTable(table); | ||
} | ||
|
||
public String name, description; | ||
public ZonePoint position = null; | ||
protected boolean visible = false; | ||
|
||
public Media media, icon; | ||
|
||
public byte[] getMedia () throws IOException { | ||
return Engine.mediaFile(media); | ||
} | ||
|
||
public byte[] getIcon () throws IOException { | ||
return Engine.mediaFile(icon); | ||
} | ||
|
||
public boolean isVisible() { return visible; } | ||
|
||
public void setPosition(ZonePoint location) { | ||
position = location; | ||
table.rawset("ObjectLocation", location); | ||
} | ||
|
||
public boolean isLocated() { | ||
return position != null; | ||
} | ||
|
||
protected void setItem(String key, Object value) { | ||
if ("Name".equals(key)) { | ||
name = BaseLib.rawTostring(value); | ||
} else if ("Description".equals(key)) { | ||
description = Engine.removeHtml(BaseLib.rawTostring(value)); | ||
} else if ("Visible".equals(key)) { | ||
visible = LuaState.boolEval(value); | ||
} else if ("ObjectLocation".equals(key)) { | ||
//setPosition(ZonePoint.copy((ZonePoint)value)); | ||
// i know there was need to copy. but why? it is messing up deserialization | ||
position = (ZonePoint)value; | ||
} else if ("Media".equals(key)) { | ||
media = (Media)value; | ||
} else if ("Icon".equals(key)) { | ||
icon = (Media)value; | ||
} | ||
} | ||
|
||
protected Object getItem (String key) { | ||
if ("CurrentDistance".equals(key)) { | ||
if (isLocated()) return LuaState.toDouble(position.distance(Engine.instance.player.position)); | ||
else return LuaState.toDouble(-1); | ||
} else if ("CurrentBearing".equals(key)) { | ||
if (isLocated()) | ||
return LuaState.toDouble(ZonePoint.angle2azimuth(position.bearing(Engine.instance.player.position))); | ||
else return LuaState.toDouble(0); | ||
} else return table.rawget(key); | ||
} | ||
|
||
public void setTable (LuaTable table) { | ||
Object n = null; | ||
while ((n = table.next(n)) != null) { | ||
Object val = table.rawget(n); | ||
rawset(n, val); | ||
//if (n instanceof String) setItem((String)n, val); | ||
} | ||
} | ||
|
||
public void callEvent(String name, Object param) { | ||
try { | ||
Object o = table.rawget(name); | ||
if (o instanceof LuaClosure) { | ||
Engine.log("EVNT: " + toString() + "." + name + (param!=null ? " (" + param.toString() + ")" : ""), Engine.LOG_CALL); | ||
LuaClosure event = (LuaClosure) o; | ||
Engine.state.call(event, this, param, null); | ||
Engine.log("EEND: " + toString() + "." + name, Engine.LOG_CALL); | ||
} | ||
} catch (Throwable t) { | ||
Engine.stacktrace(t); | ||
} | ||
} | ||
|
||
public boolean hasEvent(String name) { | ||
return (table.rawget(name)) instanceof LuaClosure; | ||
} | ||
|
||
public String toString() { | ||
return (name == null ? "(unnamed)" : name); | ||
} | ||
|
||
public void rawset(Object key, Object value) { | ||
// TODO unify rawset/setItem | ||
if (key instanceof String) { | ||
setItem((String) key, value); | ||
} | ||
table.rawset(key, value); | ||
Engine.log("PROP: " + toString() + "." + key + " is set to " + (value == null ? "nil" : value.toString()), Engine.LOG_PROP); | ||
} | ||
|
||
public void setMetatable (LuaTable metatable) { } | ||
|
||
public LuaTable getMetatable () { return metatable; } | ||
|
||
public Object rawget (Object key) { | ||
// TODO unify rawget/getItem | ||
if (key instanceof String) | ||
return getItem((String)key); | ||
else | ||
return table.rawget(key); | ||
} | ||
|
||
public Object next (Object key) { return table.next(key); } | ||
|
||
public int len () { return table.len(); } | ||
} | ||
package cz.matejcik.openwig; | ||
|
||
import se.krka.kahlua.stdlib.BaseLib; | ||
import se.krka.kahlua.vm.*; | ||
|
||
import java.io.*; | ||
|
||
public class EventTable implements LuaTable, Serializable { | ||
|
||
public LuaTable table = new LuaTableImpl(); | ||
|
||
private LuaTable metatable = new LuaTableImpl(); | ||
|
||
private boolean isDeserializing = false; | ||
|
||
private static class TostringJavaFunc implements JavaFunction { | ||
|
||
public EventTable parent; | ||
|
||
public TostringJavaFunc (EventTable parent) { | ||
this.parent = parent; | ||
} | ||
|
||
public int call (LuaCallFrame callFrame, int nArguments) { | ||
callFrame.push(parent.luaTostring()); | ||
return 1; | ||
} | ||
}; | ||
|
||
protected String luaTostring () { return "a ZObject instance"; } | ||
|
||
public EventTable() { | ||
metatable.rawset("__tostring", new TostringJavaFunc(this)); | ||
} | ||
|
||
public void serialize (DataOutputStream out) throws IOException { | ||
Engine.instance.savegame.storeValue(table, out); | ||
} | ||
|
||
public void deserialize (DataInputStream in) throws IOException { | ||
isDeserializing = true; | ||
Engine.instance.savegame.restoreValue(in, this); | ||
isDeserializing = false; | ||
//setTable(table); | ||
} | ||
|
||
public String name, description; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
public ZonePoint position = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
protected boolean visible = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
public Media media, icon; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
public byte[] getMedia () throws IOException { | ||
return Engine.mediaFile(media); | ||
} | ||
|
||
public byte[] getIcon () throws IOException { | ||
return Engine.mediaFile(icon); | ||
} | ||
|
||
public boolean isVisible() { return visible; } | ||
|
||
public void setPosition(ZonePoint location) { | ||
position = location; | ||
table.rawset("ObjectLocation", location); | ||
} | ||
|
||
public boolean isLocated() { | ||
return position != null; | ||
} | ||
|
||
protected void setItem(String key, Object value) { | ||
if ("Name".equals(key)) { | ||
name = BaseLib.rawTostring(value); | ||
} else if ("Description".equals(key)) { | ||
description = Engine.removeHtml(BaseLib.rawTostring(value)); | ||
} else if ("Visible".equals(key)) { | ||
visible = LuaState.boolEval(value); | ||
} else if ("ObjectLocation".equals(key)) { | ||
//setPosition(ZonePoint.copy((ZonePoint)value)); | ||
// i know there was need to copy. but why? it is messing up deserialization | ||
position = (ZonePoint)value; | ||
} else if ("Media".equals(key)) { | ||
media = (Media)value; | ||
} else if ("Icon".equals(key)) { | ||
icon = (Media)value; | ||
} | ||
} | ||
|
||
protected Object getItem (String key) { | ||
if ("CurrentDistance".equals(key)) { | ||
if (isLocated()) return LuaState.toDouble(position.distance(Engine.instance.player.position)); | ||
else return LuaState.toDouble(-1); | ||
} else if ("CurrentBearing".equals(key)) { | ||
if (isLocated()) | ||
return LuaState.toDouble(ZonePoint.angle2azimuth(position.bearing(Engine.instance.player.position))); | ||
else return LuaState.toDouble(0); | ||
} else return table.rawget(key); | ||
} | ||
|
||
public void setTable (LuaTable table) { | ||
Object n = null; | ||
while ((n = table.next(n)) != null) { | ||
Object val = table.rawget(n); | ||
rawset(n, val); | ||
//if (n instanceof String) setItem((String)n, val); | ||
} | ||
} | ||
|
||
public void callEvent(String name, Object param) { | ||
/* | ||
workaround: suppress RuntimeException when callEvent() is called at deserialiation | ||
@see https://github.com/cgeo/openWIG/issues/8#issuecomment-612182631 | ||
TODO actually fix EventTable and ALL of its subclasses as described in the link | ||
*/ | ||
if (isDeserializing) { | ||
return; | ||
} | ||
/* end workaround */ | ||
try { | ||
Object o = table.rawget(name); | ||
if (o instanceof LuaClosure) { | ||
Engine.log("EVNT: " + toString() + "." + name + (param!=null ? " (" + param.toString() + ")" : ""), Engine.LOG_CALL); | ||
LuaClosure event = (LuaClosure) o; | ||
Engine.state.call(event, this, param, null); | ||
Engine.log("EEND: " + toString() + "." + name, Engine.LOG_CALL); | ||
} | ||
} catch (Throwable t) { | ||
Engine.stacktrace(t); | ||
} | ||
} | ||
|
||
public boolean hasEvent(String name) { | ||
return (table.rawget(name)) instanceof LuaClosure; | ||
} | ||
|
||
public String toString() { | ||
return (name == null ? "(unnamed)" : name); | ||
} | ||
|
||
|
||
public void rawset(Object key, Object value) { | ||
// TODO unify rawset/setItem | ||
if (key instanceof String) { | ||
setItem((String) key, value); | ||
} | ||
table.rawset(key, value); | ||
Engine.log("PROP: " + toString() + "." + key + " is set to " + (value == null ? "nil" : value.toString()), Engine.LOG_PROP); | ||
} | ||
|
||
public void setMetatable (LuaTable metatable) { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue found: Document empty method body |
||
|
||
public LuaTable getMetatable () { return metatable; } | ||
|
||
public Object rawget (Object key) { | ||
// TODO unify rawget/getItem | ||
if (key instanceof String) | ||
return getItem((String)key); | ||
else | ||
return table.rawget(key); | ||
} | ||
|
||
public Object next (Object key) { return table.next(key); } | ||
|
||
public int len () { return table.len(); } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue found: Avoid unused imports such as 'se.krka.kahlua.vm'