From e4d1e0a40e89b3fb35bf3ee7a4c213919caae640 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Sat, 26 Mar 2016 18:27:07 -0400 Subject: mid-update commit for resync --- StardewModdingAPI/Config.cs | 8 +- StardewModdingAPI/Events/Mine.cs | 10 +- StardewModdingAPI/Extensions.cs | 17 ++- StardewModdingAPI/Inheritance/SBareObject.cs | 27 ++++ StardewModdingAPI/JsonResolver.cs | 215 +++++++++++++++++++++++++++ StardewModdingAPI/Log.cs | 45 +++--- StardewModdingAPI/StardewModdingAPI.csproj | 2 + 7 files changed, 297 insertions(+), 27 deletions(-) create mode 100644 StardewModdingAPI/Inheritance/SBareObject.cs create mode 100644 StardewModdingAPI/JsonResolver.cs diff --git a/StardewModdingAPI/Config.cs b/StardewModdingAPI/Config.cs index c2cd6db1..d5517535 100644 --- a/StardewModdingAPI/Config.cs +++ b/StardewModdingAPI/Config.cs @@ -56,7 +56,7 @@ namespace StardewModdingAPI try { //try to load the config from a json blob on disk - T c = JsonConvert.DeserializeObject(File.ReadAllText(ConfigLocation)); + T c = JsonConvert.DeserializeObject(File.ReadAllText(ConfigLocation), new JsonSerializerSettings() {ContractResolver = new JsonResolver()}); c.ConfigLocation = ConfigLocation; @@ -94,10 +94,10 @@ namespace StardewModdingAPI try { //default config - var b = JObject.FromObject(Instance().GenerateDefaultConfig()); + var b = JObject.FromObject(Instance().GenerateDefaultConfig(), new JsonSerializer() {ContractResolver = new JsonResolver()}); //user config - var u = JObject.FromObject(this); + var u = JObject.FromObject(this, new JsonSerializer() {ContractResolver = new JsonResolver()}); //overwrite default values with user values b.Merge(u, new JsonMergeSettings {MergeArrayHandling = MergeArrayHandling.Replace}); @@ -164,7 +164,7 @@ namespace StardewModdingAPI return; } - string s = JsonConvert.SerializeObject(baseConfig, typeof (T), Formatting.Indented, new JsonSerializerSettings()); + string s = JsonConvert.SerializeObject(baseConfig, typeof (T), Formatting.Indented, new JsonSerializerSettings() {ContractResolver = new JsonResolver()}); if (!Directory.Exists(baseConfig.ConfigDir)) Directory.CreateDirectory(baseConfig.ConfigDir); diff --git a/StardewModdingAPI/Events/Mine.cs b/StardewModdingAPI/Events/Mine.cs index ea23a8a3..2f4e9ba7 100644 --- a/StardewModdingAPI/Events/Mine.cs +++ b/StardewModdingAPI/Events/Mine.cs @@ -1,6 +1,14 @@ -namespace StardewModdingAPI.Events +using System; + +namespace StardewModdingAPI.Events { public static class MineEvents { + public static event EventHandler MineLevelChanged = delegate { }; + + public static void InvokeLocationsChanged(int currentMineLevel) + { + + } } } diff --git a/StardewModdingAPI/Extensions.cs b/StardewModdingAPI/Extensions.cs index 53c69c29..58430a6e 100644 --- a/StardewModdingAPI/Extensions.cs +++ b/StardewModdingAPI/Extensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using System.Reflection; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; @@ -21,16 +22,28 @@ namespace StardewModdingAPI return new Color(Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); } + [Obsolete("The usage of ToSingular has changed. Please update your call to use ToSingular")] public static string ToSingular(this IEnumerable ienum, string split = ", ") + { + Log.Error("The usage of ToSingular has changed. Please update your call to use ToSingular"); + return ""; + } + + public static string ToSingular(this IEnumerable ienum, string split = ", ")// where T : class { //Apparently Keys[] won't split normally :l - if (ienum is Keys[]) + if (typeof(T) == typeof(Keys)) { - return string.Join(split, (Keys[])ienum); + return string.Join(split, ienum.ToArray()); } return string.Join(split, ienum); } + /*public static string ToSingular(this IEnumerable ienum, string split = ", ") + { + return string.Join(split, ienum); + }*/ + public static bool IsInt32(this object o) { int i; diff --git a/StardewModdingAPI/Inheritance/SBareObject.cs b/StardewModdingAPI/Inheritance/SBareObject.cs new file mode 100644 index 00000000..905dac0d --- /dev/null +++ b/StardewModdingAPI/Inheritance/SBareObject.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.AccessControl; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Inheritance +{ + public struct SBareObject + { + public int parentSheetIndex { get; set; } + public int stack { get; set; } + public bool isRecipe { get; set; } + public int price { get; set; } + public int quality { get; set; } + + public SBareObject(int psi, int sta, bool ir, int pri, int qua) + { + parentSheetIndex = psi; + stack = sta; + isRecipe = ir; + price = pri; + quality = qua; + } + } +} diff --git a/StardewModdingAPI/JsonResolver.cs b/StardewModdingAPI/JsonResolver.cs new file mode 100644 index 00000000..bd5831e0 --- /dev/null +++ b/StardewModdingAPI/JsonResolver.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Design; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using StardewModdingAPI.Inheritance; + +namespace StardewModdingAPI +{ + class JsonResolver : DefaultContractResolver + { + protected override JsonContract CreateContract(Type objectType) + { + if (objectType == typeof(Rectangle) || objectType == typeof(Rectangle?)) + { + Console.WriteLine("FOUND A RECT"); + JsonContract contract = base.CreateObjectContract(objectType); + contract.Converter = new RectangleConverter(); + return contract; + } + if (objectType == typeof(StardewValley.Object)) + { + Log.Verbose("FOUND AN OBJECT"); + JsonContract contract = base.CreateObjectContract(objectType); + contract.Converter = new ObjectConverter(); + return contract; + + } + return base.CreateContract(objectType); + } + } + + public class ObjectConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + Log.Verbose("TRYING TO WRITE"); + var obj = (StardewValley.Object)value; + Log.Verbose("TRYING TO WRITE"); + + var jObject = GetObject(obj); + Log.Verbose("TRYING TO WRITE"); + + try + { + Log.Verbose(jObject.ToString()); + } + catch (Exception ex) + { + Log.Error(ex); + } + + Console.ReadKey(); + + jObject.WriteTo(writer); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var jObject = JObject.Load(reader); + + return GetObject(jObject); + } + + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + + protected static JObject GetObject(StardewValley.Object o) + { + try + { + var parentSheetIndex = o.parentSheetIndex; + var stack = o.stack; + var isRecipe = o.isRecipe; + var price = o.price; + var quality = o.quality; + + var oo = new SBareObject(parentSheetIndex, stack, isRecipe, price, quality); + Log.Success(JsonConvert.SerializeObject(oo)); + return JObject.FromObject(oo); + } + catch (Exception ex) + { + Log.Error(ex); + Console.ReadKey(); + } + return null; + } + + protected static StardewValley.Object GetObject(JObject jObject) + { + int? parentSheetIndex = GetTokenValue(jObject, "parentSheetIndex") as int?; + int? stack = GetTokenValue(jObject, "parentSheetIndex") as int?; + bool? isRecipe = GetTokenValue(jObject, "parentSheetIndex") as bool?; + int? price = GetTokenValue(jObject, "parentSheetIndex") as int?; + int? quality = GetTokenValue(jObject, "parentSheetIndex") as int?; + + return new StardewValley.Object(parentSheetIndex ?? 0, stack ?? 0, isRecipe ?? false, price ?? -1, quality ?? 0); + } + + protected static StardewValley.Object GetObject(JToken jToken) + { + var jObject = JObject.FromObject(jToken); + + return GetObject(jObject); + } + + protected static T GetTokenValue(JObject jObject, string tokenName) where T : class + { + JToken jToken; + jObject.TryGetValue(tokenName, StringComparison.InvariantCultureIgnoreCase, out jToken); + return jToken as T; + } + } + + public class RectangleConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + var rectangle = (Rectangle)value; + + var jObject = GetObject(rectangle); + + jObject.WriteTo(writer); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + Console.WriteLine(reader.ReadAsString()); + var jObject = JObject.Load(reader); + + return GetRectangle(jObject); + } + + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + + protected static JObject GetObject(Rectangle rectangle) + { + var x = rectangle.X; + var y = rectangle.Y; + var width = rectangle.Width; + var height = rectangle.Height; + + return JObject.FromObject(new { x, y, width, height }); + } + + protected static Rectangle GetRectangle(JObject jObject) + { + var x = GetTokenValue(jObject, "x") ?? 0; + var y = GetTokenValue(jObject, "y") ?? 0; + var width = GetTokenValue(jObject, "width") ?? 0; + var height = GetTokenValue(jObject, "height") ?? 0; + + return new Rectangle(x, y, width, height); + } + + protected static Rectangle GetRectangle(JToken jToken) + { + var jObject = JObject.FromObject(jToken); + + return GetRectangle(jObject); + } + + protected static int? GetTokenValue(JObject jObject, string tokenName) + { + JToken jToken; + return jObject.TryGetValue(tokenName, StringComparison.InvariantCultureIgnoreCase, out jToken) ? (int)jToken : (int?)null; + } + } + + public class RectangleListConverter : RectangleConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + var rectangleList = (IList)value; + + var jArray = new JArray(); + + foreach (var rectangle in rectangleList) + { + jArray.Add(GetObject(rectangle)); + } + + jArray.WriteTo(writer); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var rectangleList = new List(); + + var jArray = JArray.Load(reader); + + foreach (var jToken in jArray) + { + rectangleList.Add(GetRectangle(jToken)); + } + + return rectangleList; + } + + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + } +} diff --git a/StardewModdingAPI/Log.cs b/StardewModdingAPI/Log.cs index c6294194..6c338edc 100644 --- a/StardewModdingAPI/Log.cs +++ b/StardewModdingAPI/Log.cs @@ -32,15 +32,9 @@ namespace StardewModdingAPI } } - /// - /// Print provided parameters to the console/file as applicable - /// - /// Desired message - /// When true, writes to ONLY console and not the log file. - /// Additional params to be added to the message - private static void PrintLog(object message, bool disableLogging, params object[] values) + private static void PrintLog(object message, bool disableLogging) { - string logOutput = $"[{DateTime.Now.ToLongTimeString()}] {string.Format(message.ToString(), values)}"; + string logOutput = $"[{DateTime.Now.ToLongTimeString()}] {message?.ToString()}"; Console.WriteLine(logOutput); if (_logStream != null && !disableLogging) @@ -50,15 +44,26 @@ namespace StardewModdingAPI } } + /// + /// Print provided parameters to the console/file as applicable + /// + /// Desired message + /// When true, writes to ONLY console and not the log file. + /// Deprecated. Does nothing. + private static void PrintLog(object message, bool disableLogging, params object[] values) + { + PrintLog(message, disableLogging); + } + /// /// Successful message to display to console and logging. /// /// - /// + /// Deprecated. Do not use. public static void Success(object message, params object[] values) { Console.ForegroundColor = ConsoleColor.Green; - PrintLog(message?.ToString(), false, values); + PrintLog(message?.ToString(), false); Console.ForegroundColor = ConsoleColor.Gray; } @@ -66,11 +71,11 @@ namespace StardewModdingAPI /// Generic comment to display to console and logging. /// /// - /// + /// Deprecated. Do not use. public static void Verbose(object message, params object[] values) { Console.ForegroundColor = ConsoleColor.Gray; - PrintLog(message?.ToString(), false, values); + PrintLog(message?.ToString(), false); Console.ForegroundColor = ConsoleColor.Gray; } @@ -78,11 +83,11 @@ namespace StardewModdingAPI /// Additional comment to display to console and logging. /// /// - /// + /// Deprecated. Do not use. public static void Comment(object message, params object[] values) { Console.ForegroundColor = ConsoleColor.Yellow; - PrintLog(message?.ToString(), false, values); + PrintLog(message?.ToString(), false); Console.ForegroundColor = ConsoleColor.Gray; } @@ -90,11 +95,11 @@ namespace StardewModdingAPI /// Message for only console. Does not appear in logging. /// /// - /// + /// Deprecated. Do not use. public static void Info(object message, params object[] values) { Console.ForegroundColor = ConsoleColor.Gray; - PrintLog(message?.ToString(), true, values); + PrintLog(message?.ToString(), true); Console.ForegroundColor = ConsoleColor.Gray; } @@ -102,11 +107,11 @@ namespace StardewModdingAPI /// Important message indicating an error. /// /// - /// + /// Deprecated. Do not use. public static void Error(object message, params object[] values) { Console.ForegroundColor = ConsoleColor.Red; - PrintLog(message?.ToString(), false, values); + PrintLog(message?.ToString(), false); Console.ForegroundColor = ConsoleColor.Gray; } @@ -114,12 +119,12 @@ namespace StardewModdingAPI /// A message displayed only while in DEBUG mode /// /// - /// + /// Deprecated. Do not use. public static void Debug(object message, params object[] values) { #if DEBUG Console.ForegroundColor = ConsoleColor.Yellow; - Log.PrintLog(message.ToString(), false, values); + Log.PrintLog(message.ToString(), false); Console.ForegroundColor = ConsoleColor.Gray; #endif } diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index a2dd442a..76ebd86c 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -149,7 +149,9 @@ + + -- cgit