From 1e9d253281e8b4edfa26102779e5ec70d36ef1ce Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Thu, 10 Jan 2019 18:15:31 -0800 Subject: [PATCH] Custom Item converter in progress. --- .../Framework/Objects/MultiTiledComponent.cs | 1 + .../Serialization/Converters/ItemCoverter.cs | 126 ++++++++++++++++++ .../Utilities/Serialization/Serialization.cs | 1 + GeneralMods/Revitalize/ModCore.cs | 3 +- GeneralMods/Revitalize/Revitalize.csproj | 1 + 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/ItemCoverter.cs diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index b2601011..07a4f579 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -201,6 +201,7 @@ namespace Revitalize.Framework.Objects public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f) { Revitalize.ModCore.log("DRAW THE THING!!!"); + Revitalize.ModCore.log(this.Name); if (this.info == null) Revitalize.ModCore.log("info is null"); if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null"); if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null"); diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/ItemCoverter.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/ItemCoverter.cs new file mode 100644 index 00000000..ab6ed33c --- /dev/null +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Converters/ItemCoverter.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using StardewValley; + +namespace Revitalize.Framework.Utilities.Serialization.Converters +{ + public class ItemCoverter:Newtonsoft.Json.JsonConverter + { + + JsonSerializerSettings settings; + public ItemCoverter() + { + this.settings = new JsonSerializerSettings() + { + Converters = new List() + { + new Framework.Utilities.Serialization.Converters.RectangleConverter(), + new Framework.Utilities.Serialization.Converters.Texture2DConverter() + }, + Formatting = Formatting.Indented, + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + NullValueHandling = NullValueHandling.Include + }; + } + + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + + + string convertedString = JsonConvert.SerializeObject((Item)value, this.settings); + Revitalize.ModCore.log("SERIALIZE AN ITEM!: " + convertedString); + + + + DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver; + + + writer.WriteStartObject(); + writer.WritePropertyName("Type"); + serializer.Serialize(writer, value.GetType().FullName.ToString()); + writer.WritePropertyName("Item"); + serializer.Serialize(writer, convertedString); + + writer.WriteEndObject(); + } + + /// + /// Reads the JSON representation of the object. + /// + /// The to read from. + /// Type of the object. + /// The existing value of object being read. + /// The calling serializer. + /// The object value. + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.Null) + { + + throw new JsonSerializationException("Cant convert to item!"); + return null; + } + + JObject jo = JObject.Load(reader); + + string t = jo["Type"].Value(); + + if(t== typeof(StardewValley.Tools.Axe).FullName.ToString()) + { + Revitalize.ModCore.log("DESERIALIZE AXE!!!"); + //return jo["Item"].Value(); + return JsonConvert.DeserializeObject(jo["Item"].ToString(),this.settings); + } + else if (t == typeof(Revitalize.Framework.Objects.MultiTiledObject).FullName.ToString()) + { + + Revitalize.ModCore.log("DESERIALIZE Multi Tile Object!!!"); + return JsonConvert.DeserializeObject(jo["Item"].ToString(), this.settings); + // return jo["Item"].Value(); + + } + else if (t == typeof(Revitalize.Framework.Objects.MultiTiledComponent).FullName.ToString()) + { + + Revitalize.ModCore.log("DESERIALIZE Multi Tile Component!!!"); + return JsonConvert.DeserializeObject(jo["Item"].ToString(), this.settings); + // return jo["Item"].Value(); + + } + + else + { + + throw new NotImplementedException("CANT DESERIALIZE: " + t.ToString()); + } + + } + + public override bool CanWrite => true; + public override bool CanRead => true; + + public override bool CanConvert(Type objectType) + { + return IsSameOrSubclass(typeof(StardewValley.Item),objectType); + } + + /// + /// https://stackoverflow.com/questions/2742276/how-do-i-check-if-a-type-is-a-subtype-or-the-type-of-an-object + /// + /// + /// + /// + public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant) + { + return potentialDescendant.IsSubclassOf(potentialBase) + || potentialDescendant == potentialBase; + } + } +} diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index 8af52c9c..6c065c69 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -30,6 +30,7 @@ namespace Revitalize.Framework.Utilities this.addConverter(new Framework.Utilities.Serialization.Converters.RectangleConverter()); this.addConverter(new Framework.Utilities.Serialization.Converters.Texture2DConverter()); + this.addConverter(new Framework.Utilities.Serialization.Converters.ItemCoverter()); //this.addConverter(new Framework.Utilities.Serialization.Converters.NetFieldConverter()); //this.addConverter(new Framework.Utilities.Serialization.Converters.Vector2Converter()); } diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 4e7499d1..3900a308 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -275,9 +275,10 @@ namespace Revitalize private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e) { + if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) { - throw new Exception("Can't run in multiplayer!"); + //throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); } Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]); diff --git a/GeneralMods/Revitalize/Revitalize.csproj b/GeneralMods/Revitalize/Revitalize.csproj index 653dd888..a947f7e4 100644 --- a/GeneralMods/Revitalize/Revitalize.csproj +++ b/GeneralMods/Revitalize/Revitalize.csproj @@ -76,6 +76,7 @@ +