diff options
author | Zoryn Aaron <zoryn4163@gmail.com> | 2016-03-26 18:27:07 -0400 |
---|---|---|
committer | Zoryn Aaron <zoryn4163@gmail.com> | 2016-03-26 18:27:07 -0400 |
commit | e4d1e0a40e89b3fb35bf3ee7a4c213919caae640 (patch) | |
tree | 2effaed98b6a7cb36cfaf319fe9cb89a774e52b8 | |
parent | e5f42badcc6e31fea4c98d7519142a1c578cf299 (diff) | |
download | SMAPI-e4d1e0a40e89b3fb35bf3ee7a4c213919caae640.tar.gz SMAPI-e4d1e0a40e89b3fb35bf3ee7a4c213919caae640.tar.bz2 SMAPI-e4d1e0a40e89b3fb35bf3ee7a4c213919caae640.zip |
mid-update commit for resync
-rw-r--r-- | StardewModdingAPI/Config.cs | 8 | ||||
-rw-r--r-- | StardewModdingAPI/Events/Mine.cs | 10 | ||||
-rw-r--r-- | StardewModdingAPI/Extensions.cs | 17 | ||||
-rw-r--r-- | StardewModdingAPI/Inheritance/SBareObject.cs | 27 | ||||
-rw-r--r-- | StardewModdingAPI/JsonResolver.cs | 215 | ||||
-rw-r--r-- | StardewModdingAPI/Log.cs | 45 | ||||
-rw-r--r-- | StardewModdingAPI/StardewModdingAPI.csproj | 2 |
7 files changed, 297 insertions, 27 deletions
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<T>(File.ReadAllText(ConfigLocation)); + T c = JsonConvert.DeserializeObject<T>(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<T>().GenerateDefaultConfig<T>()); + var b = JObject.FromObject(Instance<T>().GenerateDefaultConfig<T>(), 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<EventArgsCurrentLocationChanged> 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<T>")] public static string ToSingular(this IEnumerable ienum, string split = ", ") { + Log.Error("The usage of ToSingular has changed. Please update your call to use ToSingular<T>"); + return ""; + } + + public static string ToSingular<T>(this IEnumerable<T> 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<T>(split, ienum.ToArray<T>()); } return string.Join(split, ienum); } + /*public static string ToSingular<T>(this IEnumerable<T> 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<object>(jObject, "parentSheetIndex") as int?; + int? stack = GetTokenValue<object>(jObject, "parentSheetIndex") as int?; + bool? isRecipe = GetTokenValue<object>(jObject, "parentSheetIndex") as bool?; + int? price = GetTokenValue<object>(jObject, "parentSheetIndex") as int?; + int? quality = GetTokenValue<object>(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<T>(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<Rectangle>)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<Rectangle>(); + + 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 } } - /// <summary> - /// Print provided parameters to the console/file as applicable - /// </summary> - /// <param name="message">Desired message</param> - /// <param name="disableLogging">When true, writes to ONLY console and not the log file.</param> - /// <param name="values">Additional params to be added to the message</param> - 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) @@ -51,14 +45,25 @@ namespace StardewModdingAPI } /// <summary> + /// Print provided parameters to the console/file as applicable + /// </summary> + /// <param name="message">Desired message</param> + /// <param name="disableLogging">When true, writes to ONLY console and not the log file.</param> + /// <param name="values">Deprecated. Does nothing.</param> + private static void PrintLog(object message, bool disableLogging, params object[] values) + { + PrintLog(message, disableLogging); + } + + /// <summary> /// Successful message to display to console and logging. /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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. /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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. /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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. /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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. /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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 /// </summary> /// <param name="message"></param> - /// <param name="values"></param> + /// <param name="values">Deprecated. Do not use.</param> 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 @@ <Compile Include="Inheritance\Menus\SGameMenu.cs" />
<Compile Include="Inheritance\Menus\SInventoryPage.cs" />
<Compile Include="Inheritance\Minigames\SMinigameBase.cs" />
+ <Compile Include="Inheritance\SBareObject.cs" />
<Compile Include="Inheritance\SObject.cs" />
+ <Compile Include="JsonResolver.cs" />
<Compile Include="Log.cs" />
<Compile Include="Manifest.cs" />
<Compile Include="Mod.cs" />
|