private void RunCall(IDataContainer obj, string func, object[] parameters, bool isSelf, FallbackAction fallback) { // This executes in the job thread. // Checks if this is still alive. if (IsDisposed) { return; } // Checks if the function still exists. LuaDataContainer dc = obj as LuaDataContainer; if (dc == null) { return; } IDataProvider lf = dc.GetProvider(func, isSelf); if (lf == null) { return; } // Calls the function. try { lf.Execute(parameters); } catch (InvalidOperationException ex) { // Last chance fallback action, if any. if (fallback != null) { fallback(ex); } else if (DefaultFallbackAction != null) { DefaultFallbackAction(ex); } else { // No fallback action: let's rethrow this. throw; } } catch (Exception) { // Other exceptions than InvalidOperationException are // immediately rethrown because they are, indeed, unexpected. throw; } }
private void readTable(BinaryReader input, LuaDataContainer obj) { string className = "unknown"; IDataProvider rawset = null; LuaDataContainer tab; object key = 1; if (obj != null) { className = obj.GetString("ClassName"); if (className != null) { rawset = obj.GetProvider("rawset", false); } } byte b = input.ReadByte(); while (b != 6) { // Key switch (b) { case 1: key = input.ReadByte() == 0 ? false : true; break; case 2: key = input.ReadDouble(); break; case 3: key = readString(input); break; default: throw new Exception(String.Format("Unsupported table key: {0} at byte {1}", b, input.BaseStream.Position)); } b = input.ReadByte(); // Value switch (b) { case 1: SetField(obj, key, input.ReadBoolean(), rawset); break; case 2: SetField(obj, key, input.ReadDouble(), rawset); break; case 3: SetField(obj, key, readByteArray(input), rawset); break; case 4: byte[] chunk = input.ReadBytes(input.ReadInt32()); SetField(obj, key, _dataFactory.LoadProvider(chunk, key.ToString()), rawset); break; case 5: tab = _dataFactory.CreateContainer(); SetField(obj, key, tab, rawset); readTable(input, tab); break; case 6: // End of table return; case 7: var objIndex = input.ReadInt16(); if (objIndex == -21555) { SetField(obj, key, _player, rawset); } else { SetField(obj, key, _allZObjects.GetContainer(objIndex), rawset); } break; case 8: tab = (LuaDataContainer)_dataFactory.CreateWherigoObject(readString(input)).DataContainer; SetField(obj, key, tab, rawset); // After an object, there is always a table with the content input.ReadByte(); readTable(input, tab); break; } b = input.ReadByte(); } }
/// <summary> /// Write the table obj to the binary writer output. /// </summary> /// <param name="output">BinaryWriter to write the table to.</param> /// <param name="obj">Table to write to binary writer.</param> private void writeTable(BinaryWriter output, LuaDataContainer obj) { output.Write((byte)5); var entry = obj.GetEnumerator(); while (entry.MoveNext()) { // Save key if (entry.Key is bool) { output.Write((byte)1); output.Write((bool)entry.Key ? (byte)1 : (byte)0); } if (entry.Key is double) { output.Write((byte)2); output.Write((double)entry.Key); } if (entry.Key is string) { output.Write((byte)3); writeString(output, (string)entry.Key); } // Save value if (entry.Value == null) { // Write false for null output.Write((byte)1); output.Write((byte)0); } if (entry.Value is bool) { output.Write((byte)1); output.Write((bool)entry.Value ? (byte)1 : (byte)0); } if (entry.Value is double || entry.Value is int) { output.Write((byte)2); output.Write((double)entry.Value); } if (entry.Value is string) { output.Write((byte)3); writeByteArray(output, obj.GetByteArray(entry.Key)); } if (entry.Value is LuaDataProvider) { output.Write((byte)4); byte[] array = toArray(_dataFactory.GetProviderAt("string.dump").FirstOrDefault <string>(entry.Value)); output.Write(array.Length); output.Write(array); } if (entry.Value is LuaDataContainer) { LuaDataContainer dc = (LuaDataContainer)entry.Value; string className = dc.GetString("ClassName"); if (className != null && (className.Equals("Distance") || className.Equals("ZonePoint") || className.Equals("ZCommand") || className.Equals("ZReciprocalCommand"))) { output.Write((byte)8); writeString(output, className); LuaDataContainer data = dc.CallSelf("serialize"); writeTable(output, data); } else if (className != null && (className.Equals("ZCartridge") || className.Equals("ZCharacter") || className.Equals("ZInput") || className.Equals("ZItem") || className.Equals("ZMedia") || className.Equals("Zone") || className.Equals("ZTask") || className.Equals("ZTimer"))) { output.Write((byte)7); output.Write(Convert.ToInt16(dc.GetInt("ObjIndex").Value)); } else { // New: It is a normal LuaTable or an unknown new ZObject type LuaDataContainer data = dc; if (className != null) { // New: If we are here, than this is a new ZObject class, so call serialize before. // New: That means, that it is a normal LuaTable, so save it LuaDataProvider lf = dc.GetProvider("serialize", true); if (lf != null) { data = lf.FirstContainerOrDefault(); } } writeTable(output, data); } } } output.Write((byte)6); }
private void readTable(BinaryReader input, LuaDataContainer obj) { string className = "unknown"; IDataProvider rawset = null; LuaDataContainer tab; object key = 1; if (obj != null) { className = obj.GetString("ClassName"); if (className != null) rawset = obj.GetProvider("rawset", false); } byte b = input.ReadByte(); while (b != 6) { // Key switch (b) { case 1: key = input.ReadByte() == 0 ? false : true; break; case 2: key = input.ReadDouble(); break; case 3: key = readString(input); break; default: throw new Exception(String.Format("Unsupported table key: {0} at byte {1}", b, input.BaseStream.Position)); } b = input.ReadByte(); // Value switch (b) { case 1: SetField(obj, key, input.ReadBoolean(), rawset); break; case 2: SetField(obj, key, input.ReadDouble(), rawset); break; case 3: SetField(obj, key, readByteArray(input), rawset); break; case 4: byte[] chunk = input.ReadBytes(input.ReadInt32()); SetField(obj, key, _dataFactory.LoadProvider(chunk, key.ToString()), rawset); break; case 5: tab = _dataFactory.CreateContainer(); SetField(obj, key, tab, rawset); readTable(input, tab); break; case 6: // End of table return; case 7: var objIndex = input.ReadInt16 (); if (objIndex == -21555) SetField (obj, key, _player, rawset); else SetField(obj, key, _allZObjects.GetContainer(objIndex), rawset); break; case 8: tab = (LuaDataContainer)_dataFactory.CreateWherigoObject(readString(input)).DataContainer; SetField(obj, key, tab, rawset); // After an object, there is always a table with the content input.ReadByte(); readTable(input, tab); break; } b = input.ReadByte(); } }