diff options
-rw-r--r-- | src/StardewModdingAPI.sln | 1 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/Models/GitRelease.cs | 19 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/Models/SConfig.cs | 14 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/UpdateHelper.cs | 67 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 12 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.config.json | 13 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 2 |
7 files changed, 82 insertions, 46 deletions
diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index bc2bb8b2..9d7baa51 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -38,6 +38,7 @@ EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution StardewModdingAPI.Models\StardewModdingAPI.Models.projitems*{2aa02fb6-ff03-41cf-a215-2ee60ab4f5dc}*SharedItemsImports = 13 + StardewModdingAPI.Models\StardewModdingAPI.Models.projitems*{f1a573b0-f436-472c-ae29-0b91ea6b9f8f}*SharedItemsImports = 4 EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/StardewModdingAPI/Framework/Models/GitRelease.cs b/src/StardewModdingAPI/Framework/Models/GitRelease.cs deleted file mode 100644 index bc53468f..00000000 --- a/src/StardewModdingAPI/Framework/Models/GitRelease.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Newtonsoft.Json; - -namespace StardewModdingAPI.Framework.Models -{ - /// <summary>Metadata about a GitHub release tag.</summary> - internal class GitRelease - { - /********* - ** Accessors - *********/ - /// <summary>The display name.</summary> - [JsonProperty("name")] - public string Name { get; set; } - - /// <summary>The semantic version string.</summary> - [JsonProperty("tag_name")] - public string Tag { get; set; } - } -}
\ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/Models/SConfig.cs b/src/StardewModdingAPI/Framework/Models/SConfig.cs index b2ca4113..36799400 100644 --- a/src/StardewModdingAPI/Framework/Models/SConfig.cs +++ b/src/StardewModdingAPI/Framework/Models/SConfig.cs @@ -1,4 +1,4 @@ -namespace StardewModdingAPI.Framework.Models +namespace StardewModdingAPI.Framework.Models { /// <summary>The SMAPI configuration settings.</summary> internal class SConfig @@ -9,11 +9,17 @@ /// <summary>Whether to enable development features.</summary> public bool DeveloperMode { get; set; } - /// <summary>Whether to check if a newer version of SMAPI is available on startup.</summary> - public bool CheckForUpdates { get; set; } = true; + /// <summary>Whether to check for newer versions of SMAPI and mods on startup.</summary> + public bool CheckForUpdates { get; set; } + + /// <summary>SMAPI's GitHub project name, used to perform update checks.</summary> + public string GitHubProjectName { get; set; } + + /// <summary>The base URL for SMAPI's web API, used to perform update checks.</summary> + public string WebApiBaseUrl { get; set; } /// <summary>Whether SMAPI should log more information about the game context.</summary> - public bool VerboseLogging { get; set; } = false; + public bool VerboseLogging { get; set; } /// <summary>A list of mod versions which should be considered compatible or incompatible regardless of whether SMAPI detects incompatible code.</summary> public ModCompatibility[] ModCompatibility { get; set; } diff --git a/src/StardewModdingAPI/Framework/UpdateHelper.cs b/src/StardewModdingAPI/Framework/UpdateHelper.cs index e01e55c8..0ee57648 100644 --- a/src/StardewModdingAPI/Framework/UpdateHelper.cs +++ b/src/StardewModdingAPI/Framework/UpdateHelper.cs @@ -1,36 +1,69 @@ -using System.IO; +using System; +using System.Collections.Generic; +using System.IO; using System.Net; -using System.Reflection; using System.Threading.Tasks; using Newtonsoft.Json; -using StardewModdingAPI.Framework.Models; +using StardewModdingAPI.Models; namespace StardewModdingAPI.Framework { - /// <summary>Provides utility methods for mod updates.</summary> - internal class UpdateHelper + /// <summary>Provides methods for interacting with the SMAPI web API.</summary> + internal class WebApiClient { /********* + ** Properties + *********/ + /// <summary>The base URL for the web API.</summary> + private readonly Uri BaseUrl; + + /// <summary>The API version number.</summary> + private readonly ISemanticVersion Version; + + + /********* ** Public methods *********/ - /// <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) + /// <summary>Construct an instance.</summary> + /// <param name="baseUrl">The base URL for the web API.</param> + /// <param name="version">The web API version.</param> + public WebApiClient(string baseUrl, ISemanticVersion version) + { +#if !SMAPI_FOR_WINDOWS + baseUrl = baseUrl.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac +#endif + this.BaseUrl = new Uri(baseUrl); + this.Version = version; + } + + /// <summary>Get the latest SMAPI version.</summary> + /// <param name="modKeys">The mod keys for which to fetch the latest version.</param> + public async Task<IDictionary<string, ModInfoModel>> GetModInfoAsync(params string[] modKeys) { - // build request - // (avoid HttpClient for Mac compatibility) - HttpWebRequest request = WebRequest.CreateHttp($"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 + string url = $"v{this.Version}/mods?modKeys={Uri.EscapeDataString(string.Join(",", modKeys))}"; + return await this.GetAsync<Dictionary<string, ModInfoModel>>(url); + } + + + /********* + ** Private methods + *********/ + /// <summary>Fetch the response from the backend API.</summary> + /// <typeparam name="T">The expected response type.</typeparam> + /// <param name="url">The request URL, optionally excluding the base URL.</param> + private async Task<T> GetAsync<T>(string url) + { + // build request (avoid HttpClient for Mac compatibility) + HttpWebRequest request = WebRequest.CreateHttp(new Uri(this.BaseUrl, url).ToString()); + request.UserAgent = $"SMAPI/{this.Version}"; + + // fetch data using (WebResponse response = await request.GetResponseAsync()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { string responseText = reader.ReadToEnd(); - return JsonConvert.DeserializeObject<GitRelease>(responseText); + return JsonConvert.DeserializeObject<T>(responseText); } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 84af2777..df94b02a 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -21,6 +21,7 @@ using StardewModdingAPI.Framework.ModHelpers; using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Serialisation; +using StardewModdingAPI.Models; using StardewValley; using Monitor = StardewModdingAPI.Framework.Monitor; using SObject = StardewValley.Object; @@ -572,10 +573,13 @@ namespace StardewModdingAPI { try { - GitRelease release = UpdateHelper.GetLatestVersionAsync(Constants.GitHubRepository).Result; - ISemanticVersion latestVersion = new SemanticVersion(release.Tag); - if (latestVersion.IsNewerThan(Constants.ApiVersion)) - this.Monitor.Log($"You can update SMAPI from version {Constants.ApiVersion} to {latestVersion}", LogLevel.Alert); + var client = new WebApiClient(this.Settings.WebApiBaseUrl, Constants.ApiVersion); + string key = $"GitHub:{this.Settings.GitHubProjectName}"; + ModInfoModel info = client.GetModInfoAsync(key).Result[key]; + if (info.Error != null) + this.Monitor.Log($"Couldn't check for a new version of SMAPI. This won't affect your game, but you may not be notified of new versions if this keeps happening.\n{info.Error}"); + else if (new SemanticVersion(info.Version).IsNewerThan(Constants.ApiVersion)) + this.Monitor.Log($"You can update SMAPI from version {Constants.ApiVersion} to {info.Version}.", LogLevel.Alert); } catch (Exception ex) { diff --git a/src/StardewModdingAPI/StardewModdingAPI.config.json b/src/StardewModdingAPI/StardewModdingAPI.config.json index d393f5a9..67d8f270 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.config.json +++ b/src/StardewModdingAPI/StardewModdingAPI.config.json @@ -1,4 +1,4 @@ -/* +/* @@ -22,6 +22,17 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha "CheckForUpdates": true, /** + * SMAPI's GitHub project name, used to perform update checks. + */ + "GitHubProjectName": "Pathoschild/SMAPI", + + /** + * The base URL for SMAPI's web API, used to perform update checks. + * Note: the protocol will be changed to http:// on Linux/Mac due to OpenSSL issues with the game's bundled Mono. + */ + "WebApiBaseUrl": "https://api.smapi.io", + + /** * Whether SMAPI should log more information about the game context. */ "VerboseLogging": false, diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 8da93bf4..d5fffd3f 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -219,7 +219,6 @@ <Compile Include="LogLevel.cs" /> <Compile Include="Framework\ModRegistry.cs" /> <Compile Include="Framework\UpdateHelper.cs" /> - <Compile Include="Framework\Models\GitRelease.cs" /> <Compile Include="IMonitor.cs" /> <Compile Include="Events\ChangeType.cs" /> <Compile Include="Events\ItemStackChange.cs" /> @@ -274,6 +273,7 @@ <Install>false</Install> </BootstrapperPackage> </ItemGroup> + <Import Project="..\StardewModdingAPI.Models\StardewModdingAPI.Models.projitems" Label="Shared" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(SolutionDir)\common.targets" /> </Project>
\ No newline at end of file |