private void Deserialize(IDictionary <string, InstanceState> data, StardewValley.Network.OverlaidDictionary items) { var trash = new List <Vector2>(); foreach (var dict in items) { foreach (var item in dict) { if (item.Value is SObject obj && obj.name.Equals("(Entoarox.Framework.ICustomItem)")) { if (data.ContainsKey(obj.Type)) { string cls = data[obj.Type].Type; Type type = Type.GetType(cls); if (type == null) { this.Monitor.Log("Unable to deserialize custom item, type does not exist: " + cls, LogLevel.Error); items[item.Key] = new Placeholder(cls, data[obj.Type].Data); } else if (!typeof(Item).IsAssignableFrom(type)) { this.Monitor.Log("Unable to deserialize custom item, class does not inherit from StardewValley.Item in any form: " + cls, LogLevel.Error); items[item.Key] = new Placeholder(cls, data[obj.Type].Data); } else if (!type.GetInterfaces().Contains(typeof(ICustomItem))) { this.Monitor.Log("Unable to deserialize custom item, item class does not implement the ICustomItem interface: " + cls, LogLevel.Error); items[item.Key] = new Placeholder(cls, data[obj.Type].Data); } else { try { items[item.Key] = (SObject)data[obj.Type].Data.ToObject(type, Serializer); } catch (Exception err) { this.Monitor.Log("Unable to deserialize custom item of type " + cls + ", unknown error:\n" + err, LogLevel.Error); items[item.Key] = new Placeholder(cls, data[obj.Type].Data); } } } else { this.Monitor.Log("Unable to deserialize custom item, GUID does not exist: " + obj.Type, LogLevel.Error); trash.Add(item.Key); } } } } foreach (var vector in trash) { items.Remove(vector); } }
private void Serialize(Dictionary <string, InstanceState> data, StardewValley.Network.OverlaidDictionary items) { foreach (var dict in items) { foreach (var item in dict) { if (item.Value is ICustomItem && item.Value is SObject sobj) { string id = Guid.NewGuid().ToString(); int counter = 0; while (data.ContainsKey(id) && counter++ < 25) { id = Guid.NewGuid().ToString(); } if (counter >= 25) { throw new TimeoutException("Unable to assign a GUID to all items!"); } SObject obj = new SObject() { Stack = sobj.Stack, ParentSheetIndex = 0, Type = id, Name = "(Entoarox.Framework.ICustomItem)", Price = sobj.salePrice(), }; if (sobj is Placeholder pitm) { data.Add(id, new InstanceState(pitm.Id, pitm.Data)); } else { data.Add(id, new InstanceState(sobj.GetType().AssemblyQualifiedName, JToken.FromObject(sobj, Serializer))); } items[item.Key] = obj; } } } }