summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-14 14:15:50 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-14 14:15:50 -0400
commitda630efc1d5c95816493c2969736ae883e723757 (patch)
treeb460810bc2702facf16bca9a263d53a2380a2c6c /src/StardewModdingAPI/Framework
parent02a4c40814caf26f21fc9afd9c64afd639da462f (diff)
downloadSMAPI-da630efc1d5c95816493c2969736ae883e723757.tar.gz
SMAPI-da630efc1d5c95816493c2969736ae883e723757.tar.bz2
SMAPI-da630efc1d5c95816493c2969736ae883e723757.zip
downgrade to .NET Framework 4.0 for better compatibility on Windows 7–8.1
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/InternalExtensions.cs29
-rw-r--r--src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs4
-rw-r--r--src/StardewModdingAPI/Framework/UpdateHelper.cs7
3 files changed, 32 insertions, 8 deletions
diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs
index 4ca79518..46c76656 100644
--- a/src/StardewModdingAPI/Framework/InternalExtensions.cs
+++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
+using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework
{
@@ -59,12 +60,36 @@ namespace StardewModdingAPI.Framework
/// <param name="handlers">The event handlers.</param>
/// <param name="sender">The event sender.</param>
/// <param name="args">The event arguments.</param>
- public static void SafelyRaiseGenericEvent<TEventArgs>(this IMonitor monitor, string name, IEnumerable<Delegate> handlers, object sender, TEventArgs args)
+ public static void SafelyRaiseGenericEvent<TEventArgs>(this IMonitor monitor, string name, IEnumerable<Delegate> handlers, object sender, TEventArgs args) where TEventArgs : EventArgs
{
if (handlers == null)
return;
- foreach (EventHandler<TEventArgs> handler in Enumerable.Cast<EventHandler<TEventArgs>>(handlers))
+ foreach (EventHandler<TEventArgs> handler in handlers.Cast<EventHandler<TEventArgs>>())
+ {
+ try
+ {
+ handler.Invoke(sender, args);
+ }
+ catch (Exception ex)
+ {
+ monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error);
+ }
+ }
+ }
+
+ /// <summary>Safely raise an <see cref="EventHandler{TEventArgs}"/> event, and intercept any exceptions thrown by its handlers.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ /// <param name="name">The event name for error messages.</param>
+ /// <param name="handlers">The event handlers.</param>
+ /// <param name="sender">The event sender.</param>
+ /// <param name="args">The event arguments.</param>
+ public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable<Delegate> handlers, object sender, IContentEventHelper args)
+ {
+ if (handlers == null)
+ return;
+
+ foreach (ContentEventHandler handler in handlers.Cast<ContentEventHandler>())
{
try
{
diff --git a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs
index 08204b7e..d89e8e44 100644
--- a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs
+++ b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs
@@ -60,7 +60,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
- return (TValue)this.PropertyInfo.GetValue(this.Parent);
+ return (TValue)this.PropertyInfo.GetValue(this.Parent, null);
}
catch (InvalidCastException)
{
@@ -78,7 +78,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
- this.PropertyInfo.SetValue(this.Parent, value);
+ this.PropertyInfo.SetValue(this.Parent, value, null);
}
catch (InvalidCastException)
{
diff --git a/src/StardewModdingAPI/Framework/UpdateHelper.cs b/src/StardewModdingAPI/Framework/UpdateHelper.cs
index e01e55c8..342a08cf 100644
--- a/src/StardewModdingAPI/Framework/UpdateHelper.cs
+++ b/src/StardewModdingAPI/Framework/UpdateHelper.cs
@@ -1,7 +1,6 @@
using System.IO;
using System.Net;
using System.Reflection;
-using System.Threading.Tasks;
using Newtonsoft.Json;
using StardewModdingAPI.Framework.Models;
@@ -15,17 +14,17 @@ namespace StardewModdingAPI.Framework
*********/
/// <summary>Get the latest release from a GitHub repository.</summary>
/// <param name="repository">The name of the repository from which to fetch releases (like "cjsu/SMAPI").</param>
- public static async Task<GitRelease> GetLatestVersionAsync(string repository)
+ public static GitRelease GetLatestVersion(string repository)
{
// build request
// (avoid HttpClient for Mac compatibility)
- HttpWebRequest request = WebRequest.CreateHttp($"https://api.github.com/repos/{repository}/releases/latest");
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.github.com/repos/{repository}/releases/latest");
AssemblyName assembly = typeof(UpdateHelper).Assembly.GetName();
request.UserAgent = $"{assembly.Name}/{assembly.Version}";
request.Accept = "application/vnd.github.v3+json";
// fetch data
- using (WebResponse response = await request.GetResponseAsync())
+ using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{