Custom Item converter in progress.

This commit is contained in:
Joshua Navarro 2019-01-10 18:15:31 -08:00
parent 01483cdaff
commit 1e9d253281
5 changed files with 131 additions and 1 deletions

View File

@ -201,6 +201,7 @@ namespace Revitalize.Framework.Objects
public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f) public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f)
{ {
Revitalize.ModCore.log("DRAW THE THING!!!"); Revitalize.ModCore.log("DRAW THE THING!!!");
Revitalize.ModCore.log(this.Name);
if (this.info == null) Revitalize.ModCore.log("info is null"); if (this.info == null) Revitalize.ModCore.log("info is null");
if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null"); if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null");
if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null"); if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null");

View File

@ -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<JsonConverter>()
{
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();
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
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<string>();
if(t== typeof(StardewValley.Tools.Axe).FullName.ToString())
{
Revitalize.ModCore.log("DESERIALIZE AXE!!!");
//return jo["Item"].Value<StardewValley.Tools.Axe>();
return JsonConvert.DeserializeObject<StardewValley.Tools.Axe>(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<Revitalize.Framework.Objects.MultiTiledObject>(jo["Item"].ToString(), this.settings);
// return jo["Item"].Value<Revitalize.Framework.Objects.MultiTiledObject>();
}
else if (t == typeof(Revitalize.Framework.Objects.MultiTiledComponent).FullName.ToString())
{
Revitalize.ModCore.log("DESERIALIZE Multi Tile Component!!!");
return JsonConvert.DeserializeObject<Revitalize.Framework.Objects.MultiTiledComponent>(jo["Item"].ToString(), this.settings);
// return jo["Item"].Value<Revitalize.Framework.Objects.MultiTiledObject>();
}
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);
}
/// <summary>
/// https://stackoverflow.com/questions/2742276/how-do-i-check-if-a-type-is-a-subtype-or-the-type-of-an-object
/// </summary>
/// <param name="potentialBase"></param>
/// <param name="potentialDescendant"></param>
/// <returns></returns>
public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
{
return potentialDescendant.IsSubclassOf(potentialBase)
|| potentialDescendant == potentialBase;
}
}
}

View File

@ -30,6 +30,7 @@ namespace Revitalize.Framework.Utilities
this.addConverter(new Framework.Utilities.Serialization.Converters.RectangleConverter()); this.addConverter(new Framework.Utilities.Serialization.Converters.RectangleConverter());
this.addConverter(new Framework.Utilities.Serialization.Converters.Texture2DConverter()); 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.NetFieldConverter());
//this.addConverter(new Framework.Utilities.Serialization.Converters.Vector2Converter()); //this.addConverter(new Framework.Utilities.Serialization.Converters.Vector2Converter());
} }

View File

@ -275,9 +275,10 @@ namespace Revitalize
private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e) private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e)
{ {
if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) 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"]); Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]);

View File

@ -76,6 +76,7 @@
<Compile Include="Framework\Utilities\BoundingBoxInfo.cs" /> <Compile Include="Framework\Utilities\BoundingBoxInfo.cs" />
<Compile Include="Framework\Utilities\InventoryManager.cs" /> <Compile Include="Framework\Utilities\InventoryManager.cs" />
<Compile Include="Framework\Utilities\Serialization\ContractResolvers\NetFieldContract.cs" /> <Compile Include="Framework\Utilities\Serialization\ContractResolvers\NetFieldContract.cs" />
<Compile Include="Framework\Utilities\Serialization\Converters\ItemCoverter.cs" />
<Compile Include="Framework\Utilities\Serialization\Converters\NetFieldConverter.cs" /> <Compile Include="Framework\Utilities\Serialization\Converters\NetFieldConverter.cs" />
<Compile Include="Framework\Utilities\Serialization\Converters\RectangleConverter.cs" /> <Compile Include="Framework\Utilities\Serialization\Converters\RectangleConverter.cs" />
<Compile Include="Framework\Utilities\Serialization\Converters\Texture2DConverter.cs" /> <Compile Include="Framework\Utilities\Serialization\Converters\Texture2DConverter.cs" />