Merge branch 'develop' into stable

This commit is contained in:
Jesse Plamondon-Willard 2017-11-01 17:42:18 -04:00
commit e0b72374cd
95 changed files with 2762 additions and 658 deletions

3
.gitignore vendored
View File

@ -23,3 +23,6 @@ _ReSharper*/
**/packages/*
*.nuget.props
*.nuget.targets
# sensitive files
appsettings.Development.json

View File

@ -2,5 +2,5 @@ using System.Reflection;
using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]

View File

@ -78,7 +78,7 @@
<!-- copy files into game directory and enable debugging (only in debug mode) -->
<Target Name="AfterBuild">
<CallTarget Targets="CopySMAPI;CopyTrainerMod" Condition="'$(Configuration)' == 'Debug'" />
<CallTarget Targets="CopySMAPI;CopyDefaultMod" Condition="'$(Configuration)' == 'Debug'" />
</Target>
<Target Name="CopySMAPI" Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI'">
<Copy SourceFiles="$(TargetDir)\$(TargetName).exe" DestinationFolder="$(GamePath)" />
@ -89,10 +89,10 @@
<Copy SourceFiles="$(TargetDir)\Newtonsoft.Json.dll" DestinationFolder="$(GamePath)" />
<Copy SourceFiles="$(TargetDir)\Mono.Cecil.dll" DestinationFolder="$(GamePath)" />
</Target>
<Target Name="CopyTrainerMod" Condition="'$(MSBuildProjectName)' == 'TrainerMod'">
<Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(GamePath)\Mods\TrainerMod" />
<Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(GamePath)\Mods\TrainerMod" Condition="Exists('$(TargetDir)\$(TargetName).pdb')" />
<Copy SourceFiles="$(TargetDir)\manifest.json" DestinationFolder="$(GamePath)\Mods\TrainerMod" />
<Target Name="CopyDefaultMod" Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.ConsoleCommands'">
<Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" />
<Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" Condition="Exists('$(TargetDir)\$(TargetName).pdb')" />
<Copy SourceFiles="$(TargetDir)\manifest.json" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" />
</Target>
<!-- launch SMAPI on debug -->

View File

@ -1,4 +1,32 @@
# Release notes
## 2.1
* For players:
* Added a log parser at [log.smapi.io](https://log.smapi.io).
* Added better Steam instructions to the SMAPI installer.
* Renamed the bundled _TrainerMod_ to _ConsoleCommands_ to make its purpose clearer.
* Removed the game's test messages from the console log.
* Improved update-check errors when playing offline.
* Fixed compatibility check for players with Stardew Valley 1.08.
* Fixed `player_setlevel` command not setting XP too.
* For modders:
* The reflection API now works with public code to simplify mod integrations.
* The content API now lets you invalidated multiple assets at once.
* The `InputEvents` have been improved:
* Added `e.IsActionButton` and `e.IsUseToolButton`.
* Added `ToSButton()` extension for the game's `Game1.options` button type.
* Deprecated `e.IsClick`, which is limited and unclear. Use `IsActionButton` or `IsUseToolButton` instead.
* Fixed `e.SuppressButton()` not correctly suppressing keyboard buttons.
* Fixed `e.IsClick` (now `e.IsActionButton`) ignoring custom key bindings.
* `SemanticVersion` can now be constructed from a `System.Version`.
* Fixed reflection API blocking access to some non-SMAPI members.
* Fixed content API allowing absolute paths as asset keys.
* Fixed content API failing to load custom map tilesheets that aren't preloaded.
* Fixed content API incorrectly detecting duplicate loaders when a mod implements `IAssetLoader` directly.
* For SMAPI developers:
* Added the installer version and platform to the installer window title to simplify troubleshooting.
## 2.0
### Release highlights
* **Mod update checks**
@ -18,7 +46,7 @@
SMAPI 2.0 adds several features to enable new kinds of mods (see
[API documentation](https://stardewvalleywiki.com/Modding:SMAPI_APIs)).
The **content API** lets you edit, inject, and reload XNB data loaded by the game at any time. This let SMAPI mods do
The **content API** lets you edit, inject, and reload XNB data loaded by the game at any time. This lets SMAPI mods do
anything previously only possible with XNB mods, and enables new mod scenarios not possible with XNB mods (e.g.
seasonal textures, NPC clothing that depend on the weather or location, etc).

View File

@ -3,16 +3,25 @@
This file provides more technical documentation about SMAPI. If you only want to use or create
mods, this section isn't relevant to you; see the main README to use or create mods.
## Contents
* [Development](#development)
# Contents
* [SMAPI](#smapi)
* [Development](#development)
* [Compiling from source](#compiling-from-source)
* [Debugging a local build](#debugging-a-local-build)
* [Preparing a release](#preparing-a-release)
* [Customisation](#customisation)
* [Customisation](#customisation)
* [Configuration file](#configuration-file)
* [Command-line arguments](#command-line-arguments)
* [Compile flags](#compile-flags)
* [SMAPI web services](#smapi-web-services)
* [Overview](#overview)
* [Log parser](#log-parser)
* [Mods API](#mods-api)
* [Development](#development-2)
* [Local development](#local-development)
* [Deploying to Amazon Beanstalk](#deploying-to-amazon-beanstalk)
# SMAPI
## Development
### Compiling from source
Using an official SMAPI release is recommended for most users.
@ -135,3 +144,81 @@ SMAPI uses a small number of conditional compilation constants, which you can se
flag | purpose
---- | -------
`SMAPI_FOR_WINDOWS` | Indicates that SMAPI is being compiled on Windows for players on Windows. Set automatically in `crossplatform.targets`.
# SMAPI web services
## Overview
The `StardewModdingAPI.Web` project provides an API and web UI hosted at `*.smapi.io`.
### Log parser
The log parser provides a web UI for uploading, parsing, and sharing SMAPI logs. The logs are
persisted in a compressed form to Pastebin.
The log parser lives at https://log.smapi.io.
### Mods API
The mods API provides version info for mods hosted by Chucklefish, GitHub, or Nexus Mods. It's used
by SMAPI to perform update checks. The `{version}` URL token is the version of SMAPI making the
request; it doesn't do anything currently, but lets us version breaking changes if needed.
Each mod is identified by a repository key and unique identifier (like `nexus:541`). The following
repositories are supported:
key | repository
------------- | ----------
`chucklefish` | A mod page on the [Chucklefish mod site](https://community.playstarbound.com/resources/categories/22), identified by the mod ID in the page URL.
`github` | A repository on [GitHub](https://github.com), identified by its owner and repository name (like `Zoryn4163/SMAPI-Mods`). This checks the version of the latest repository release.
`nexus` | A mod page on [Nexus Mods](https://www.nexusmods.com/stardewvalley), identified by the mod ID in the page URL.
The API accepts either `GET` or `POST` for convenience:
> ```
>GET https://api.smapi.io/v2.0/mods?modKeys=nexus:541,chucklefish:4228
>```
>```
>POST https://api.smapi.io/v2.0/mods
>{
> "ModKeys": [ "nexus:541", "chucklefish:4228" ]
>}
>```
It returns a response like this:
>```
>{
> "chucklefish:4228": {
> "name": "Entoarox Framework",
> "version": "1.8.0",
> "url": "https://community.playstarbound.com/resources/4228"
> },
> "nexus:541": {
> "name": "Lookup Anything",
> "version": "1.16",
> "url": "http://www.nexusmods.com/stardewvalley/mods/541"
> }
>}
>```
## Development
### Local development
`StardewModdingAPI.Web` is a regular ASP.NET MVC Core app, so you can just launch it from within
Visual Studio to run a local version.
There are two differences when it's run locally: all endpoints use HTTP instead of HTTPS, and the
subdomain portion becomes a route (e.g. `log.smapi.io` &rarr; `localhost:59482/log`).
Before running it locally, you need to enter your credentials in the `appsettings.Development.json`
file. See the next section for a description of each setting. This file is listed in `.gitignore`
to prevent accidentally committing credentials.
### Deploying to Amazon Beanstalk
The app can be deployed to a standard Amazon Beanstalk IIS environment. When creating the
environment, make sure to specify the following environment properties:
property name | description
------------------------------- | -----------------
`LogParser:PastebinDevKey` | The [Pastebin developer key](https://pastebin.com/api#1) used to authenticate with the Pastebin API.
`LogParser:PastebinUserKey` | The [Pastebin user key](https://pastebin.com/api#8) used to authenticate with the Pastebin API. Can be left blank to post anonymously.
`LogParser:SectionUrl` | The root URL of the log page, like `https://log.smapi.io/`.
`ModUpdateCheck:GitHubPassword` | The password with which to authenticate to GitHub when fetching release info.
`ModUpdateCheck:GitHubUsername` | The username with which to authenticate to GitHub when fetching release info.

View File

@ -48,6 +48,19 @@ namespace StardewModdingAPI.Common
this.Tag = this.GetNormalisedTag(tag);
}
/// <summary>Construct an instance.</summary>
/// <param name="version">The assembly version.</param>
/// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>
public SemanticVersionImpl(Version version)
{
if (version == null)
throw new ArgumentNullException(nameof(version), "The input version can't be null.");
this.Major = version.Major;
this.Minor = version.Minor;
this.Patch = version.Build;
}
/// <summary>Construct an instance.</summary>
/// <param name="version">The semantic version string.</param>
/// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>

View File

@ -7,6 +7,7 @@ using System.Reflection;
using System.Threading;
using Microsoft.Win32;
using StardewModdingApi.Installer.Enums;
using StardewModdingAPI.Common;
namespace StardewModdingApi.Installer
{
@ -97,6 +98,7 @@ namespace StardewModdingApi.Installer
// obsolete
yield return GetInstallPath("Mods/.cache"); // 1.3-1.4
yield return GetInstallPath("Mods/TrainerMod"); // *2.0 (renamed to ConsoleCommands)
yield return GetInstallPath("Mono.Cecil.Rocks.dll"); // 1.31.8
yield return GetInstallPath("StardewModdingAPI-settings.json"); // 1.0-1.4
if (modsDir.Exists)
@ -135,6 +137,13 @@ namespace StardewModdingApi.Installer
/// </remarks>
public void Run(string[] args)
{
/****
** Get platform & set window title
****/
Platform platform = this.DetectPlatform();
Console.Title = $"SMAPI {new SemanticVersionImpl(this.GetType().Assembly.GetName().Version)} installer on {platform}";
Console.WriteLine();
/****
** read command-line arguments
****/
@ -159,10 +168,6 @@ namespace StardewModdingApi.Installer
/****
** collect details
****/
// get platform
Platform platform = this.DetectPlatform();
this.PrintDebug($"Platform: {(platform == Platform.Windows ? "Windows" : "Linux or Mac")}.");
// get game path
DirectoryInfo installDir = this.InteractivelyGetInstallPath(platform, gamePathArg);
if (installDir == null)
@ -182,7 +187,9 @@ namespace StardewModdingApi.Installer
unixLauncher = Path.Combine(installDir.FullName, "StardewValley"),
unixLauncherBackup = Path.Combine(installDir.FullName, "StardewValley-original")
};
this.PrintDebug($"Install path: {installDir}.");
// show output
Console.WriteLine($"Your game folder: {installDir}.");
/****
** validate assumptions
@ -340,22 +347,26 @@ namespace StardewModdingApi.Installer
this.InteractivelyRemoveAppDataMods(platform, modsDir, packagedModsDir);
}
Console.WriteLine();
Console.WriteLine();
/****
** exit
** final instructions
****/
this.PrintColor("Done!", ConsoleColor.DarkGreen);
if (platform == Platform.Windows)
{
this.PrintColor(
action == ScriptAction.Install
? "Don't forget to launch StardewModdingAPI.exe instead of the normal game executable. See the readme.txt for details."
: "If you manually changed shortcuts or Steam to launch SMAPI, don't forget to change those back.",
ConsoleColor.DarkGreen
);
if (action == ScriptAction.Install)
{
this.PrintColor("SMAPI is installed! If you use Steam, set your launch options to enable achievements (see smapi.io/install):", ConsoleColor.DarkGreen);
this.PrintColor($" \"{Path.Combine(installDir.FullName, "StardewModdingAPI.exe")}\" %command%", ConsoleColor.DarkGreen);
Console.WriteLine();
this.PrintColor("If you don't use Steam, launch StardewModdingAPI.exe in your game folder to play with mods.", ConsoleColor.DarkGreen);
}
else
this.PrintColor("SMAPI is removed! If you configured Steam to launch SMAPI, don't forget to clear your launch options.", ConsoleColor.DarkGreen);
}
else if (action == ScriptAction.Install)
this.PrintColor("You can launch the game the same way as before to play with mods.", ConsoleColor.DarkGreen);
this.PrintColor("SMAPI is installed! Launch the game the same way as before to play with mods.", ConsoleColor.DarkGreen);
Console.ReadKey();
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
@ -50,6 +50,7 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="..\SMAPI.Common\StardewModdingAPI.Common.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\build\common.targets" />
<Import Project="..\..\build\prepare-install-package.targets" />

View File

@ -1,14 +1,14 @@
 ___ ___ ___ ___
/ /\ /__/\ / /\ / /\ ___
/ /:/_ | |::\ / /::\ / /::\ / /\
___ ___ ___ ___ ___
/ /\ /__/\ / /\ / /\ / /\
/ /:/_ | |::\ / /::\ / /::\ / /:/
/ /:/ /\ | |:|:\ / /:/\:\ / /:/\:\ / /:/
/ /:/ /::\ __|__|:|\:\ / /:/~/::\ / /:/~/:/ /__/::\
/__/:/ /:/\:\ /__/::::| \:\ /__/:/ /:/\:\ /__/:/ /:/ \__\/\:\__
\ \:\/:/~/:/ \ \:\~~\__\/ \ \:\/:/__\/ \ \:\/:/ \ \:\/\
/ /:/ /::\ __|__|:|\:\ / /:/~/::\ / /:/~/:/ / /::\ ___
/__/:/ /:/\:\ /__/::::| \:\ /__/:/ /:/\:\ /__/:/ /:/ /__/:/\:\ /\
\ \:\/:/~/:/ \ \:\~~\__\/ \ \:\/:/__\/ \ \:\/:/ \__\/ \:\/:/
\ \::/ /:/ \ \:\ \ \::/ \ \::/ \__\::/
\__\/ /:/ \ \:\ \ \:\ \ \:\ /__/:/
/__/:/ \ \:\ \ \:\ \ \:\ \__\/
\__\/ \__\/ \__\/ \__\/
\__\/ /:/ \ \:\ \ \:\ \ \:\ / /:/
/__/:/ \ \:\ \ \:\ \ \:\ /__/:/
\__\/ \__\/ \__\/ \__\/ \__\/
SMAPI lets you run Stardew Valley with mods. Don't forget to download mods separately.

View File

@ -5,5 +5,5 @@ using System.Runtime.InteropServices;
[assembly: AssemblyDescription("")]
[assembly: Guid("ea4f1e80-743f-4a1d-9757-ae66904a196a")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]

View File

@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StardewModdingAPI.ModBuildConfig</RootNamespace>
<AssemblyName>StardewModdingAPI.ModBuildConfig</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">

View File

@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Pathoschild.Stardew.ModBuildConfig</id>
<version>2.0.1</version>
<version>2.0.2</version>
<title>Build package for SMAPI mods</title>
<authors>Pathoschild</authors>
<owners>Pathoschild</owners>
@ -23,6 +23,9 @@
2.0.1:
- Fixed mod deploy failing to create subfolders if they don't already exist.
2.0.2:
- Fixed compatibility issue on Linux.
</releaseNotes>
</metadata>
<files>

View File

@ -1,14 +1,13 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using TrainerMod.Framework.Commands;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands;
namespace TrainerMod
namespace StardewModdingAPI.Mods.ConsoleCommands
{
/// <summary>The main entry point for the mod.</summary>
public class TrainerMod : Mod
public class ConsoleCommandsMod : Mod
{
/*********
** Properties
@ -52,7 +51,7 @@ namespace TrainerMod
}
}
/// <summary>Handle a TrainerMod command.</summary>
/// <summary>Handle a console command.</summary>
/// <param name="command">The command to invoke.</param>
/// <param name="commandName">The command name specified by the user.</param>
/// <param name="args">The command arguments.</param>

View File

@ -2,9 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI;
namespace TrainerMod.Framework.Commands
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands
{
/// <summary>Provides methods for parsing command-line arguments.</summary>
internal class ArgumentParser : IReadOnlyList<string>

View File

@ -1,8 +1,6 @@
using StardewModdingAPI;
namespace TrainerMod.Framework.Commands
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands
{
/// <summary>A TrainerMod command to register.</summary>
/// <summary>A console command to register.</summary>
internal interface ITrainerCommand
{
/*********

View File

@ -1,7 +1,6 @@
using StardewModdingAPI;
using StardewValley;
using StardewValley;
namespace TrainerMod.Framework.Commands.Other
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
/// <summary>A command which sends a debug command to the game.</summary>
internal class DebugCommand : TrainerCommand

View File

@ -1,7 +1,6 @@
using System.Diagnostics;
using StardewModdingAPI;
namespace TrainerMod.Framework.Commands.Other
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
/// <summary>A command which shows the data files.</summary>
internal class ShowDataFilesCommand : TrainerCommand

View File

@ -1,7 +1,6 @@
using System.Diagnostics;
using StardewModdingAPI;
namespace TrainerMod.Framework.Commands.Other
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
/// <summary>A command which shows the game files.</summary>
internal class ShowGameFilesCommand : TrainerCommand

View File

@ -1,11 +1,10 @@
using System;
using System.Linq;
using StardewModdingAPI;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
using StardewValley;
using TrainerMod.Framework.ItemData;
using Object = StardewValley.Object;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which adds an item to the player inventory.</summary>
internal class AddCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using TrainerMod.Framework.ItemData;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which list item types.</summary>
internal class ListItemTypesCommand : TrainerCommand

View File

@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI;
using TrainerMod.Framework.ItemData;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which list items available to spawn.</summary>
internal class ListItemsCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using Microsoft.Xna.Framework;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the color of a player feature.</summary>
internal class SetColorCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current health.</summary>
internal class SetHealthCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current immunity.</summary>
internal class SetImmunityCommand : TrainerCommand

View File

@ -1,11 +1,33 @@
using StardewModdingAPI;
using System.Collections.Generic;
using StardewValley;
using SFarmer = StardewValley.Farmer;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current level for a skill.</summary>
internal class SetLevelCommand : TrainerCommand
{
/*********
** Properties
*********/
/// <summary>The experience points needed to reach each level.</summary>
/// <remarks>Derived from <see cref="SFarmer.checkForLevelGain"/>.</remarks>
private readonly IDictionary<int, int> LevelExp = new Dictionary<int, int>
{
[0] = 0,
[1] = 100,
[2] = 380,
[3] = 770,
[4] = 1300,
[5] = 2150,
[6] = 3300,
[7] = 4800,
[8] = 6900,
[9] = 10000,
[10] = 15000
};
/*********
** Public methods
*********/
@ -30,31 +52,37 @@ namespace TrainerMod.Framework.Commands.Player
{
case "luck":
Game1.player.LuckLevel = level;
Game1.player.experiencePoints[SFarmer.luckSkill] = this.LevelExp[level];
monitor.Log($"OK, your luck skill is now {Game1.player.LuckLevel}.", LogLevel.Info);
break;
case "mining":
Game1.player.MiningLevel = level;
Game1.player.experiencePoints[SFarmer.miningSkill] = this.LevelExp[level];
monitor.Log($"OK, your mining skill is now {Game1.player.MiningLevel}.", LogLevel.Info);
break;
case "combat":
Game1.player.CombatLevel = level;
Game1.player.experiencePoints[SFarmer.combatSkill] = this.LevelExp[level];
monitor.Log($"OK, your combat skill is now {Game1.player.CombatLevel}.", LogLevel.Info);
break;
case "farming":
Game1.player.FarmingLevel = level;
Game1.player.experiencePoints[SFarmer.farmingSkill] = this.LevelExp[level];
monitor.Log($"OK, your farming skill is now {Game1.player.FarmingLevel}.", LogLevel.Info);
break;
case "fishing":
Game1.player.FishingLevel = level;
Game1.player.experiencePoints[SFarmer.fishingSkill] = this.LevelExp[level];
monitor.Log($"OK, your fishing skill is now {Game1.player.FishingLevel}.", LogLevel.Info);
break;
case "foraging":
Game1.player.ForagingLevel = level;
Game1.player.experiencePoints[SFarmer.foragingSkill] = this.LevelExp[level];
monitor.Log($"OK, your foraging skill is now {Game1.player.ForagingLevel}.", LogLevel.Info);
break;
}

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's maximum health.</summary>
internal class SetMaxHealthCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's maximum stamina.</summary>
internal class SetMaxStaminaCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current money.</summary>
internal class SetMoneyCommand : TrainerCommand

View File

@ -1,7 +1,6 @@
using StardewModdingAPI;
using StardewValley;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's name.</summary>
internal class SetNameCommand : TrainerCommand

View File

@ -1,7 +1,6 @@
using StardewModdingAPI;
using StardewValley;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current added speed.</summary>
internal class SetSpeedCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits the player's current stamina.</summary>
internal class SetStaminaCommand : TrainerCommand

View File

@ -1,7 +1,6 @@
using StardewModdingAPI;
using StardewValley;
using StardewValley;
namespace TrainerMod.Framework.Commands.Player
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// <summary>A command which edits a player style.</summary>
internal class SetStyleCommand : TrainerCommand

View File

@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI;
namespace TrainerMod.Framework.Commands
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands
{
/// <summary>The base implementation for a trainer command.</summary>
internal abstract class TrainerCommand : ITrainerCommand

View File

@ -1,8 +1,7 @@
using StardewModdingAPI;
using StardewValley;
using StardewValley;
using StardewValley.Locations;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which moves the player to the next mine level.</summary>
internal class DownMineLevelCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which freezes the current time.</summary>
internal class FreezeTimeCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which sets the current day.</summary>
internal class SetDayCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which moves the player to the given mine level.</summary>
internal class SetMineLevelCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which sets the current season.</summary>
internal class SetSeasonCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which sets the current time.</summary>
internal class SetTimeCommand : TrainerCommand

View File

@ -1,8 +1,7 @@
using System.Linq;
using StardewModdingAPI;
using StardewValley;
namespace TrainerMod.Framework.Commands.World
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World
{
/// <summary>A command which sets the current year.</summary>
internal class SetYearCommand : TrainerCommand

View File

@ -1,4 +1,4 @@
namespace TrainerMod.Framework.ItemData
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData
{
/// <summary>An item type that can be searched and added to the player through the console.</summary>
internal enum ItemType

View File

@ -1,6 +1,6 @@
using StardewValley;
namespace TrainerMod.Framework.ItemData
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData
{
/// <summary>A game item with metadata.</summary>
internal class SearchableItem

View File

@ -1,12 +1,12 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
using StardewValley;
using StardewValley.Objects;
using StardewValley.Tools;
using TrainerMod.Framework.ItemData;
using SObject = StardewValley.Object;
namespace TrainerMod.Framework
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
{
/// <summary>Provides methods for searching and constructing items.</summary>
internal class ItemRepository

View File

@ -0,0 +1,6 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("SMAPI.Mods.ConsoleCommands")]
[assembly: AssemblyDescription("")]
[assembly: Guid("76791e28-b1b5-407c-82d6-50c3e5b7e037")]

View File

@ -7,8 +7,8 @@
<ProjectGuid>{28480467-1A48-46A7-99F8-236D95225359}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TrainerMod</RootNamespace>
<AssemblyName>TrainerMod</AssemblyName>
<RootNamespace>StardewModdingAPI.Mods.ConsoleCommands</RootNamespace>
<AssemblyName>ConsoleCommands</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
@ -16,7 +16,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>$(SolutionDir)\..\bin\Debug\Mods\TrainerMod\</OutputPath>
<OutputPath>$(SolutionDir)\..\bin\Debug\Mods\ConsoleCommands\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@ -27,7 +27,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>$(SolutionDir)\..\bin\Release\Mods\TrainerMod\</OutputPath>
<OutputPath>$(SolutionDir)\..\bin\Release\Mods\ConsoleCommands\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@ -38,6 +38,7 @@
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -80,7 +81,7 @@
<Compile Include="Framework\Commands\ITrainerCommand.cs" />
<Compile Include="Framework\ItemData\SearchableItem.cs" />
<Compile Include="Framework\ItemRepository.cs" />
<Compile Include="TrainerMod.cs" />
<Compile Include="ConsoleCommandsMod.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -1,5 +1,5 @@
{
"Name": "Trainer Mod",
"Name": "Console Commands",
"Author": "SMAPI",
"Version": {
"MajorVersion": 2,
@ -8,6 +8,6 @@
"Build": null
},
"Description": "Adds SMAPI console commands that let you manipulate the game.",
"UniqueID": "SMAPI.TrainerMod",
"EntryDll": "TrainerMod.dll"
"UniqueID": "SMAPI.ConsoleCommands",
"EntryDll": "ConsoleCommands.dll"
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using NUnit.Framework;
@ -16,7 +16,7 @@ namespace StardewModdingAPI.Tests.Utilities
/****
** Constructor
****/
[Test(Description = "Assert that the constructor sets the expected values for all valid versions.")]
[Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from a string.")]
[TestCase("1.0", ExpectedResult = "1.0")]
[TestCase("1.0.0", ExpectedResult = "1.0")]
[TestCase("3000.4000.5000", ExpectedResult = "3000.4000.5000")]
@ -28,7 +28,7 @@ namespace StardewModdingAPI.Tests.Utilities
return new SemanticVersion(input).ToString();
}
[Test(Description = "Assert that the constructor sets the expected values for all valid versions.")]
[Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from the individual numbers.")]
[TestCase(1, 0, 0, null, ExpectedResult = "1.0")]
[TestCase(3000, 4000, 5000, null, ExpectedResult = "3000.4000.5000")]
[TestCase(1, 2, 3, "", ExpectedResult = "1.2.3")]
@ -48,6 +48,22 @@ namespace StardewModdingAPI.Tests.Utilities
return version.ToString();
}
[Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from an assembly version.")]
[TestCase(1, 0, 0, ExpectedResult = "1.0")]
[TestCase(1, 2, 3, ExpectedResult = "1.2.3")]
[TestCase(3000, 4000, 5000, ExpectedResult = "3000.4000.5000")]
public string Constructor_FromAssemblyVersion(int major, int minor, int patch)
{
// act
ISemanticVersion version = new SemanticVersion(new Version(major, minor, patch));
// assert
Assert.AreEqual(major, version.MajorVersion, "The major version doesn't match the given value.");
Assert.AreEqual(minor, version.MinorVersion, "The minor version doesn't match the given value.");
Assert.AreEqual(patch, version.PatchVersion, "The patch version doesn't match the given value.");
return version.ToString();
}
[Test(Description = "Assert that the constructor throws the expected exception for invalid versions.")]
[TestCase(null)]
[TestCase("")]
@ -239,6 +255,7 @@ namespace StardewModdingAPI.Tests.Utilities
[TestCase("1.06")]
[TestCase("1.07")]
[TestCase("1.07a")]
[TestCase("1.08")]
[TestCase("1.1")]
[TestCase("1.11")]
[TestCase("1.2")]

View File

@ -0,0 +1,158 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using StardewModdingAPI.Web.Framework;
using StardewModdingAPI.Web.Framework.ConfigModels;
using StardewModdingAPI.Web.Framework.LogParser;
using StardewModdingAPI.Web.ViewModels;
namespace StardewModdingAPI.Web.Controllers
{
/// <summary>Provides a web UI and API for parsing SMAPI log files.</summary>
internal class LogParserController : Controller
{
/*********
** Properties
*********/
/// <summary>The log parser config settings.</summary>
private readonly LogParserConfig Config;
/// <summary>The underlying Pastebin client.</summary>
private readonly PastebinClient PastebinClient;
/// <summary>The first bytes in a valid zip file.</summary>
/// <remarks>See <a href="https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers"/>.</remarks>
private const uint GzipLeadBytes = 0x8b1f;
/*********
** Public methods
*********/
/***
** Constructor
***/
/// <summary>Construct an instance.</summary>
/// <param name="configProvider">The log parser config settings.</param>
public LogParserController(IOptions<LogParserConfig> configProvider)
{
// init Pastebin client
this.Config = configProvider.Value;
string version = this.GetType().Assembly.GetName().Version.ToString(3);
string userAgent = string.Format(this.Config.PastebinUserAgent, version);
this.PastebinClient = new PastebinClient(this.Config.PastebinBaseUrl, userAgent, this.Config.PastebinUserKey, this.Config.PastebinDevKey);
}
/***
** Web UI
***/
/// <summary>Render the log parser UI.</summary>
/// <param name="id">The paste ID.</param>
[HttpGet]
[Route("")]
[Route("log")]
[Route("log/{id}")]
public ViewResult Index(string id = null)
{
return this.View("Index", new LogParserModel(this.Config.SectionUrl, id));
}
/***
** JSON
***/
/// <summary>Fetch raw text from Pastebin.</summary>
/// <param name="id">The Pastebin paste ID.</param>
[HttpGet, Produces("application/json")]
[Route("log/fetch/{id}")]
public async Task<GetPasteResponse> GetAsync(string id)
{
GetPasteResponse response = await this.PastebinClient.GetAsync(id);
response.Content = this.DecompressString(response.Content);
return response;
}
/// <summary>Save raw log data.</summary>
/// <param name="content">The log content to save.</param>
[HttpPost, Produces("application/json"), AllowLargePosts]
[Route("log/save")]
public async Task<SavePasteResponse> PostAsync([FromBody] string content)
{
content = this.CompressString(content);
return await this.PastebinClient.PostAsync(content);
}
/*********
** Private methods
*********/
/// <summary>Compress a string.</summary>
/// <param name="text">The text to compress.</param>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks>
private string CompressString(string text)
{
// get raw bytes
byte[] buffer = Encoding.UTF8.GetBytes(text);
// compressed
byte[] compressedData;
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(stream, CompressionLevel.Optimal, leaveOpen: true))
zipStream.Write(buffer, 0, buffer.Length);
stream.Position = 0;
compressedData = new byte[stream.Length];
stream.Read(compressedData, 0, compressedData.Length);
}
// prefix length
var zipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, zipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, zipBuffer, 0, 4);
// return string representation
return Convert.ToBase64String(zipBuffer);
}
/// <summary>Decompress a string.</summary>
/// <param name="rawText">The compressed text.</param>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks>
private string DecompressString(string rawText)
{
// get raw bytes
byte[] zipBuffer;
try
{
zipBuffer = Convert.FromBase64String(rawText);
}
catch
{
return rawText; // not valid base64, wasn't compressed by the log parser
}
// skip if not gzip
if (BitConverter.ToUInt16(zipBuffer, 4) != LogParserController.GzipLeadBytes)
return rawText;
// decompress
using (MemoryStream memoryStream = new MemoryStream())
{
// read length prefix
int dataLength = BitConverter.ToInt32(zipBuffer, 0);
memoryStream.Write(zipBuffer, 4, zipBuffer.Length - 4);
// read data
var buffer = new byte[dataLength];
memoryStream.Position = 0;
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
gZipStream.Read(buffer, 0, buffer.Length);
// return original string
return Encoding.UTF8.GetString(buffer);
}
}
}
}

View File

@ -14,8 +14,8 @@ namespace StardewModdingAPI.Web.Controllers
{
/// <summary>Provides an API to perform mod update checks.</summary>
[Produces("application/json")]
[Route("api/{version:semanticVersion}/[controller]")]
internal class ModsController : Controller
[Route("api/v{version:semanticVersion}/mods")]
internal class ModsApiController : Controller
{
/*********
** Properties
@ -39,7 +39,7 @@ namespace StardewModdingAPI.Web.Controllers
/// <summary>Construct an instance.</summary>
/// <param name="cache">The cache in which to store mod metadata.</param>
/// <param name="configProvider">The config settings for mod update checks.</param>
public ModsController(IMemoryCache cache, IOptions<ModUpdateCheckConfig> configProvider)
public ModsApiController(IMemoryCache cache, IOptions<ModUpdateCheckConfig> configProvider)
{
ModUpdateCheckConfig config = configProvider.Value;

View File

@ -0,0 +1,52 @@
using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
namespace StardewModdingAPI.Web.Framework
{
/// <summary>A filter which increases the maximum request size for an endpoint.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/38360093/262123"/>.</remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AllowLargePostsAttribute : Attribute, IAuthorizationFilter, IOrderedFilter
{
/*********
** Properties
*********/
/// <summary>The underlying form options.</summary>
private readonly FormOptions FormOptions;
/*********
** Accessors
*********/
/// <summary>The attribute order.</summary>
public int Order { get; set; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
public AllowLargePostsAttribute()
{
this.FormOptions = new FormOptions
{
ValueLengthLimit = 200 * 1024 * 1024 // 200MB
};
}
/// <summary>Called early in the filter pipeline to confirm request is authorized.</summary>
/// <param name="context">The authorisation filter context.</param>
public void OnAuthorization(AuthorizationFilterContext context)
{
IFeatureCollection features = context.HttpContext.Features;
IFormFeature formFeature = features.Get<IFormFeature>();
if (formFeature?.Form == null)
{
// Request form has not been read yet, so set the limits
features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, this.FormOptions));
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StardewModdingAPI.Web.Framework
{
/// <summary>Reads configuration values from the AWS Beanstalk environment properties file (if present).</summary>
/// <remarks>This is a workaround for AWS Beanstalk injection not working with .NET Core apps.</remarks>
internal class BeanstalkEnvPropsConfigProvider : ConfigurationProvider, IConfigurationSource
{
/*********
** Properties
*********/
/// <summary>The absolute path to the container configuration file on an Amazon EC2 instance.</summary>
private const string ContainerConfigPath = @"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration";
/*********
** Public methods
*********/
/// <summary>Build the configuration provider for this source.</summary>
/// <param name="builder">The configuration builder.</param>
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new BeanstalkEnvPropsConfigProvider();
}
/// <summary>Load the environment properties.</summary>
public override void Load()
{
this.Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// get Beanstalk config file
FileInfo file = new FileInfo(BeanstalkEnvPropsConfigProvider.ContainerConfigPath);
if (!file.Exists)
return;
// parse JSON
JObject jsonRoot = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(file.FullName));
if (jsonRoot["iis"]?["env"] is JArray jsonProps)
{
foreach (string prop in jsonProps.Values<string>())
{
string[] parts = prop.Split('=', 2); // key=value
if (parts.Length == 2)
this.Data[parts[0]] = parts[1];
}
}
}
}
}

View File

@ -0,0 +1,24 @@
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
/// <summary>The config settings for the log parser.</summary>
internal class LogParserConfig
{
/*********
** Accessors
*********/
/// <summary>The root URL for the log parser controller.</summary>
public string SectionUrl { get; set; }
/// <summary>The base URL for the Pastebin API.</summary>
public string PastebinBaseUrl { get; set; }
/// <summary>The user agent for the Pastebin API client, where {0} is the SMAPI version.</summary>
public string PastebinUserAgent { get; set; }
/// <summary>The user key used to authenticate with the Pastebin API.</summary>
public string PastebinUserKey { get; set; }
/// <summary>The developer key used to authenticate with the Pastebin API.</summary>
public string PastebinDevKey { get; set; }
}
}

View File

@ -1,7 +1,7 @@
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
/// <summary>The config settings for mod update checks.</summary>
public class ModUpdateCheckConfig
internal class ModUpdateCheckConfig
{
/*********
** Accessors

View File

@ -0,0 +1,15 @@
namespace StardewModdingAPI.Web.Framework.LogParser
{
/// <summary>The response for a get-paste request.</summary>
internal class GetPasteResponse
{
/// <summary>Whether the log was successfully fetched.</summary>
public bool Success { get; set; }
/// <summary>The fetched paste content (if <see cref="Success"/> is <c>true</c>).</summary>
public string Content { get; set; }
/// <summary>The error message (if saving failed).</summary>
public string Error { get; set; }
}
}

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
namespace StardewModdingAPI.Web.Framework.LogParser
{
/// <summary>An API client for Pastebin.</summary>
internal class PastebinClient : IDisposable
{
/*********
** Properties
*********/
/// <summary>The underlying HTTP client.</summary>
private readonly IClient Client;
/// <summary>The user key used to authenticate with the Pastebin API.</summary>
private readonly string UserKey;
/// <summary>The developer key used to authenticate with the Pastebin API.</summary>
private readonly string DevKey;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="baseUrl">The base URL for the Pastebin API.</param>
/// <param name="userAgent">The user agent for the API client.</param>
/// <param name="userKey">The user key used to authenticate with the Pastebin API.</param>
/// <param name="devKey">The developer key used to authenticate with the Pastebin API.</param>
public PastebinClient(string baseUrl, string userAgent, string userKey, string devKey)
{
this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
this.UserKey = userKey;
this.DevKey = devKey;
}
/// <summary>Fetch a saved paste.</summary>
/// <param name="id">The paste ID.</param>
public async Task<GetPasteResponse> GetAsync(string id)
{
try
{
// get from API
string content = await this.Client
.GetAsync($"raw/{id}")
.AsString();
// handle Pastebin errors
if (string.IsNullOrWhiteSpace(content))
return new GetPasteResponse { Error = "Received an empty response from Pastebin." };
if (content.StartsWith("<!DOCTYPE"))
return new GetPasteResponse { Error = $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it." };
return new GetPasteResponse { Success = true, Content = content };
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
return new GetPasteResponse { Error = "There's no log with that ID." };
}
catch (Exception ex)
{
return new GetPasteResponse { Error = ex.ToString() };
}
}
public async Task<SavePasteResponse> PostAsync(string content)
{
try
{
// validate
if (string.IsNullOrWhiteSpace(content))
return new SavePasteResponse { Error = "The log content can't be empty." };
// post to API
string response = await this.Client
.PostAsync("api/api_post.php")
.WithBodyContent(new FormUrlEncodedContent(new Dictionary<string, string>
{
["api_option"] = "paste",
["api_user_key"] = this.UserKey,
["api_dev_key"] = this.DevKey,
["api_paste_private"] = "1", // unlisted
["api_paste_name"] = $"SMAPI log {DateTime.UtcNow:s}",
["api_paste_expire_date"] = "1W", // one week
["api_paste_code"] = content
}))
.AsString();
// handle Pastebin errors
if (string.IsNullOrWhiteSpace(response))
return new SavePasteResponse { Error = "Received an empty response from Pastebin." };
if (response.StartsWith("Bad API request"))
return new SavePasteResponse { Error = response };
if (!response.Contains("/"))
return new SavePasteResponse { Error = $"Received an unknown response: {response}" };
// return paste ID
string pastebinID = response.Split("/").Last();
return new SavePasteResponse { Success = true, ID = pastebinID };
}
catch (Exception ex)
{
return new SavePasteResponse { Success = false, Error = ex.ToString() };
}
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
this.Client.Dispose();
}
}
}

View File

@ -0,0 +1,15 @@
namespace StardewModdingAPI.Web.Framework.LogParser
{
/// <summary>The response for a save-log request.</summary>
internal class SavePasteResponse
{
/// <summary>Whether the log was successfully saved.</summary>
public bool Success { get; set; }
/// <summary>The saved paste ID (if <see cref="Success"/> is <c>true</c>).</summary>
public string ID { get; set; }
/// <summary>The error message (if saving failed).</summary>
public string Error { get; set; }
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
namespace StardewModdingAPI.Web.Framework.RewriteRules
{
/// <summary>Redirect requests to HTTPS.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" /> and <see cref="Microsoft.AspNetCore.Rewrite.Internal.RedirectToHttpsRule"/>.</remarks>
internal class ConditionalRedirectToHttpsRule : IRule
{
/*********
** Properties
*********/
/// <summary>A predicate which indicates when the rule should be applied.</summary>
private readonly Func<HttpRequest, bool> ShouldRewrite;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param>
public ConditionalRedirectToHttpsRule(Func<HttpRequest, bool> shouldRewrite = null)
{
this.ShouldRewrite = shouldRewrite ?? (req => true);
}
/// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
/// <param name="context">The rewrite context.</param>
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
// check condition
if (this.IsSecure(request) || !this.ShouldRewrite(request))
return;
// redirect request
HttpResponse response = context.HttpContext.Response;
response.StatusCode = (int)HttpStatusCode.RedirectKeepVerb;
response.Headers["Location"] = new StringBuilder()
.Append("https://")
.Append(request.Host.Host)
.Append(request.PathBase)
.Append(request.Path)
.Append(request.QueryString)
.ToString();
context.Result = RuleResult.EndResponse;
}
/// <summary>Get whether the request was received over HTTPS.</summary>
/// <param name="request">The request to check.</param>
public bool IsSecure(HttpRequest request)
{
return
request.IsHttps // HTTPS to server
|| string.Equals(request.Headers["x-forwarded-proto"], "HTTPS", StringComparison.OrdinalIgnoreCase); // HTTPS to AWS load balancer
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
namespace StardewModdingAPI.Web.Framework.RewriteRules
{
/// <summary>Rewrite requests to prepend the subdomain portion (if any) to the path.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" />.</remarks>
internal class ConditionalRewriteSubdomainRule : IRule
{
/*********
** Accessors
*********/
/// <summary>A predicate which indicates when the rule should be applied.</summary>
private readonly Func<HttpRequest, bool> ShouldRewrite;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param>
public ConditionalRewriteSubdomainRule(Func<HttpRequest, bool> shouldRewrite = null)
{
this.ShouldRewrite = shouldRewrite ?? (req => true);
}
/// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
/// <param name="context">The rewrite context.</param>
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
// check condition
if (!this.ShouldRewrite(request))
return;
// get host parts
string host = request.Host.Host;
string[] parts = host.Split('.');
if (parts.Length < 2)
return;
// prepend to path
request.Path = $"/{parts[0]}{request.Path}";
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
namespace StardewModdingAPI.Web.Framework.RewriteRules
{
/// <summary>Redirect requests to an external URL if they match a condition.</summary>
internal class RedirectToUrlRule : IRule
{
/*********
** Properties
*********/
/// <summary>A predicate which indicates when the rule should be applied.</summary>
private readonly Func<HttpRequest, bool> ShouldRewrite;
/// <summary>The new URL to which to redirect.</summary>
private readonly string NewUrl;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param>
/// <param name="url">The new URL to which to redirect.</param>
public RedirectToUrlRule(Func<HttpRequest, bool> shouldRewrite, string url)
{
this.ShouldRewrite = shouldRewrite ?? (req => true);
this.NewUrl = url;
}
/// <summary>Construct an instance.</summary>
/// <param name="pathRegex">A case-insensitive regex to match against the path.</param>
/// <param name="url">The external URL.</param>
public RedirectToUrlRule(string pathRegex, string url)
{
Regex regex = new Regex(pathRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
this.ShouldRewrite = req => req.Path.HasValue && regex.IsMatch(req.Path.Value);
this.NewUrl = url;
}
/// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
/// <param name="context">The rewrite context.</param>
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
// check condition
if (!this.ShouldRewrite(request))
return;
// redirect request
HttpResponse response = context.HttpContext.Response;
response.StatusCode = (int)HttpStatusCode.Redirect;
response.Headers["Location"] = this.NewUrl;
context.Result = RuleResult.EndResponse;
}
}
}

View File

@ -1,30 +0,0 @@
using System;
using Microsoft.AspNetCore.Rewrite;
namespace StardewModdingAPI.Web.Framework
{
/// <summary>Rewrite requests to prepend the subdomain portion (if any) to the path.</summary>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" />.</remarks>
internal class RewriteSubdomainRule : IRule
{
/// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
/// <param name="context">The rewrite context.</param>
public void ApplyRule(RewriteContext context)
{
context.Result = RuleResult.ContinueRules;
// get host parts
string host = context.HttpContext.Request.Host.Host;
string[] parts = host.Split('.');
// validate
if (parts.Length < 2)
return;
if (parts.Length < 3 && !"localhost".Equals(parts[1], StringComparison.InvariantCultureIgnoreCase))
return;
// prepend to path
context.HttpContext.Request.Path = $"/{parts[0]}{context.HttpContext.Request.Path}";
}
}
}

View File

@ -11,19 +11,10 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/1.0/mods?modKeys=nexus:541,chucklefish:4228,github:Zoryn4163/SMAPI-Mods",
"launchUrl": "log",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Dewdrop": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/1.0/mods?modKeys=nexus:541,chucklefish:4228,github:Zoryn4163/SMAPI-Mods",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:59483"
}
}
}

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StardewModdingAPI.Web.Framework;
using StardewModdingAPI.Web.Framework.ConfigModels;
using StardewModdingAPI.Web.Framework.RewriteRules;
namespace StardewModdingAPI.Web
{
@ -30,10 +31,9 @@ namespace StardewModdingAPI.Web
{
this.Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Add(new BeanstalkEnvPropsConfigProvider()) //.AddEnvironmentVariables()
.Build();
}
@ -43,6 +43,7 @@ namespace StardewModdingAPI.Web
{
services
.Configure<ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck"))
.Configure<LogParserConfig>(this.Configuration.GetSection("LogParser"))
.Configure<RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
.AddMemoryCache()
.AddMvc()
@ -63,7 +64,33 @@ namespace StardewModdingAPI.Web
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app
.UseRewriter(new RewriteOptions().Add(new RewriteSubdomainRule())) // convert subdomain.smapi.io => smapi.io/subdomain for routing
.UseCors(policy => policy
.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins("https://smapi.io", "https://*.smapi.io", "https://*.edge.smapi.io")
.SetIsOriginAllowedToAllowWildcardSubdomains()
)
.UseRewriter(new RewriteOptions()
// redirect to HTTPS (except API for Linux/Mac Mono compatibility)
.Add(new ConditionalRedirectToHttpsRule(
shouldRewrite: req =>
req.Host.Host != "localhost"
&& !req.Path.StartsWithSegments("/api")
))
// convert subdomain.smapi.io => smapi.io/subdomain for routing
.Add(new ConditionalRewriteSubdomainRule(
shouldRewrite: req =>
req.Host.Host != "localhost"
&& (req.Host.Host.StartsWith("api.") || req.Host.Host.StartsWith("log."))
&& !req.Path.StartsWithSegments("/content")
))
// shortcut redirects
.Add(new RedirectToUrlRule("^/docs$", "https://stardewvalleywiki.com/Modding:Index"))
.Add(new RedirectToUrlRule("^/install$", "https://stardewvalleywiki.com/Modding:Installing_SMAPI"))
)
.UseStaticFiles() // wwwroot folder
.UseMvc();
}
}

View File

@ -0,0 +1,31 @@
namespace StardewModdingAPI.Web.ViewModels
{
/// <summary>The view model for the log parser page.</summary>
public class LogParserModel
{
/*********
** Accessors
*********/
/// <summary>The root URL for the log parser controller.</summary>
public string SectionUrl { get; set; }
/// <summary>The paste ID.</summary>
public string PasteID { get; set; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
public LogParserModel() { }
/// <summary>Construct an instance.</summary>
/// <param name="sectionUrl">The root URL for the log parser controller.</param>
/// <param name="pasteID">The paste ID.</param>
public LogParserModel(string sectionUrl, string pasteID)
{
this.SectionUrl = sectionUrl;
this.PasteID = pasteID;
}
}
}

View File

@ -0,0 +1,119 @@
@{
ViewData["Title"] = "SMAPI log parser";
}
@model StardewModdingAPI.Web.ViewModels.LogParserModel
@section Head {
<link rel="stylesheet" href="~/Content/css/log-parser.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" crossorigin="anonymous"></script>
<script src="~/Content/js/log-parser.js"></script>
<style type="text/css" id="modflags"></style>
<script>
$(function() {
smapi.logParser('@Model.SectionUrl', '@Model.PasteID');
});
</script>
}
@*********
** Intro
*********@
<p id="blurb">This page lets you upload, view, and share a SMAPI log to help troubleshoot mod issues.</p>
<input type="button" id="upload-button" value="Share a new log" />
@if (Model.PasteID != null)
{
<h2>Parsed log</h2>
<ul id="tabs">
<li>TRACE</li>
<li>DEBUG</li>
<li class="active">INFO</li>
<li class="active">ALERT</li>
<li class="active">WARN</li>
<li class="active">ERROR</li>
<li class="notice">Click tabs to toggle message visibility</li>
</ul>
}
<div id="output" class="trace debug"></div>
<script class="template" id="template-body" type="text/html">
<div class="always">
<table id="gameinfo">
<caption>Game info:</caption>
<tr>
<td>SMAPI Version</td>
<td>{0}</td>
</tr>
<tr>
<td>Game Version</td>
<td>{1}</td>
</tr>
<tr>
<td>Platform</td>
<td>{2}</td>
</tr>
<tr>
<td>Mods path</td>
<td>{4}</td>
</tr>
<tr>
<td>Log started</td>
<td>{3}</td>
</tr>
</table>
<br />
<table id="modslist">
<caption>Installed Mods: <span id="modlink-r" class="notice btn">Remove all mod filters</span><span class="notice txt"><i>Click any mod to filter</i></span> <span id="modlink-a" class="notice btn txt">Select all</span></caption>
</table>
</div>
<table id="log"></table>
</script>
<script class="template" id="template-css" type="text/html">
#output.modfilter:not(.mod-{0}) .mod-{0} { display:none; } #output.modfilter.mod-{0} #modslist tr { background:#ffeeee; } #output.modfilter.mod-{0} #modslist tr#modlink-{0} { background:#eeffee; }
</script>
<script class="template" id="template-modentry" type="text/html">
<tr id="modlink-{0}">
<td>{1}</td>
<td>{2}</td>
<td>{3}</td>
<td class={4}>{5}</td>
</tr>
</script>
<script class="template" id="template-logentry" type="text/html">
<tr class="{0} mod mod-{1}">
<td>{2}</td>
<td>{3}</td>
<td data-title="{4}">{4}</td>
<td>{5}</td>
</tr>
</script>
<script class="template" id="template-lognotice" type="text/html">
<tr class="{0} mod-repeat mod mod-{1}">
<td colspan="3"></td>
<td><i>repeats [{2}] times.</i></td>
</tr>
</script>
<div id="popup-upload" class="popup">
<h1>Upload log file</h1>
<div class="frame">
<ol>
<li><a href="https://stardewvalleywiki.com/Modding:Player_FAQs#SMAPI_log" target="_blank">Find your SMAPI log</a>.</li>
<li>Drag the file onto the textbox below (or paste the text in).</li>
<li>Click <em>Parse</em>.</li>
<li>Share the URL of the new page.</li>
</ol>
<textarea id="input" placeholder="Paste or drag the log here"></textarea>
<div class="buttons">
<input type="button" id="submit" value="Parse"/>
<input type="button" id="cancel" value="Cancel"/>
</div>
</div>
</div>
<div id="popup-raw" class="popup">
<h1>Raw log file</h1>
<div class="frame">
<textarea id="dataraw"></textarea>
<div class="buttons">
<input type="button" id="closeraw" value="Close" />
</div>
</div>
</div>
<div id="uploader"></div>

View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewData["Title"] - SMAPI.io</title>
<link rel="stylesheet" href="~/Content/css/main.css" />
@RenderSection("Head", required: false)
</head>
<body>
<div id="sidebar">
<h4>SMAPI</h4>
<ul>
<li><a href="https://stardewvalleywiki.com/Modding:Index">FAQs & guides</a></li>
<li><a href="https://github.com/pathoschild/SMAPI/releases">Download SMAPI</a></li>
<li><a href="https://discord.gg/stardewvalley">Get help on Discord</a></li>
</ul>
</div>
<div id="content-column">
<div id="content">
<h1>@ViewData["Title"]</h1>
@RenderBody()
</div>
<div id="footer">
<div id="license">
Hi! You can <a href="https://github.com/pathoschild/SMAPI" title="view source">view the source code</a> or <a href="https://github.com/pathoschild/SMAPI/issues" title="report issue">report a bug or suggestion</a>.
</div>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}

View File

@ -1,4 +1,13 @@
{
/*
This file is committed to source control with the default settings, but added to .gitignore to
avoid accidentally committing login details.
*/
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
@ -6,5 +15,14 @@
"System": "Information",
"Microsoft": "Information"
}
},
"ModUpdateCheck": {
"GitHubUsername": null,
"GitHubPassword": null
},
"LogParser": {
"SectionUrl": "http://localhost:59482/log/",
"PastebinUserKey": null,
"PastebinDevKey": null
}
}

View File

@ -1,3 +1,11 @@
/*
This contains the default settings for the web app. Login credentials and contextual settings are
configured via appsettings.Development.json locally, or environment properties in AWS.
*/
{
"Logging": {
"IncludeScopes": false,
@ -19,12 +27,19 @@
"GitHubBaseUrl": "https://api.github.com",
"GitHubReleaseUrlFormat": "repos/{0}/releases/latest",
"GitHubAcceptHeader": "application/vnd.github.v3+json",
"GitHubUsername": null, /* set via environment properties */
"GitHubPassword": null, /* set via environment properties */
"GitHubUsername": null, // see top note
"GitHubPassword": null, // see top note
"NexusKey": "Nexus",
"NexusUserAgent": "Nexus Client v0.63.15",
"NexusBaseUrl": "http://www.nexusmods.com/stardewvalley",
"NexusModUrlFormat": "mods/{0}"
},
"LogParser": {
"SectionUrl": null, // see top note
"PastebinBaseUrl": "https://pastebin.com/",
"PastebinUserAgent": "SMAPI/{0} (+https://github.com/Pathoschild/SMAPI)",
"PastebinUserKey": null, // see top note
"PastebinDevKey": null // see top note
}
}

View File

@ -0,0 +1,348 @@
.mod-repeat {
font-size: 8pt;
}
.template {
display: none;
}
.popup, #uploader {
position: fixed;
top: 0px;
left: 0px;
right: 0px;
bottom: 0;
background-color: rgba(0, 0, 0, .33);
z-index: 2;
display: none;
padding: 5px;
}
#upload-button {
background: #ccf;
border: 1px solid #000088;
}
#upload-button {
background: #eef;
}
#uploader:after {
content: attr(data-text);
display: block;
width: 100px;
height: 24px;
line-height: 25px;
border: 1px solid #000;
background: #fff;
position: absolute;
top: 50%;
left: 50%;
margin: -12px -50px 0 0;
font-size: 18px;
font-weight: bold;
text-align: center;
border-radius: 5px;
}
.popup h1 {
position: absolute;
top: 10%;
left: 50%;
margin-left: -150px;
text-align: center;
width: 300px;
border: 1px solid #008;
border-radius: 5px;
background: #fff;
font-family: sans-serif;
font-size: 40px;
margin-top: -25px;
z-index: 10;
border-bottom: 0;
}
.frame {
margin: auto;
margin-top: 25px;
padding: 2em;
position: absolute;
top: 10%;
left: 10%;
right: 10%;
bottom: 10%;
padding-bottom: 30px;
background: #FFF;
border-radius: 5px;
border: 1px solid #008;
}
input[type="button"] {
font-size: 20px;
border-radius: 5px;
outline: none;
box-shadow: inset 0px 0px 1px 1px rgba(0, 0, 0, .2);
cursor: pointer;
}
#input[type="button"]:hover {
background-color: #fee;
}
#cancel, #closeraw {
border: 1px solid #880000;
background-color: #fcc;
}
#submit {
border: 1px solid #008800;
background-color: #cfc;
}
#submit:hover {
background-color: #efe;
}
#input, #dataraw {
width: 100%;
height: 30em;
max-height: 70%;
margin: auto;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #000088;
outline: none;
box-shadow: inset 0px 0px 1px 1px rgba(0, 0, 192, .2);
}
.color-red {
color: red;
}
.color-green {
color: green;
}
#tabs {
border-bottom: 0;
display: block;
margin: 0;
padding: 0;
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(210, 235, 249, 1) 100%);
}
#tabs li {
margin: 5px 1px 0 0;
height: 25px;
display: inline-block;
width: 75px;
border: 1px solid #000000;
border-bottom: 0;
border-radius: 5px 5px 0 0;
text-align: center;
font-family: monospace;
font-size: 18px;
cursor: pointer;
font-weight: bold;
color: #000;
text-shadow: 0px 0px 2px #fff;
border-color: #880000;
background-color: #fcc;
box-shadow: inset 0px 0px 1px 1px rgba(0, 0, 0, .2);
}
#tabs li:hover {
background-color: #fee;
}
#tabs li:first-child {
margin-left: 5px;
}
#tabs li.active {
background: #cfc;
border-color: #008800;
}
#tabs li.active:hover {
background: #efe;
}
#tabs li.notice {
color: #000000;
background: transparent;
border: 0;
padding-top: 1px;
font-size: 13px;
font-weight: normal;
width: auto;
margin-left: 5px;
cursor: default;
box-shadow: none;
font-style: italic;
}
#output {
border-top: 1px solid #888;
padding: 10px;
overflow: auto;
font-family: monospace;
}
#output > * {
display: block;
}
#output.trace .trace,
#output.debug .debug,
#output.info .info,
#output.alert .alert,
#output.warn .warn,
#output.error .error {
display: none;
}
#output .trace {
color: #999;
}
#output .debug {
color: #595959;
}
#output .info {
color: #000
}
#output .alert {
color: #b0b;
}
#output .warn {
color: #f80
}
#output .error {
color: #f00
}
#output .always {
font-weight: bold;
border-bottom: 1px dashed #888888;
padding-bottom: 10px;
margin-bottom: 5px;
}
caption {
text-align: left;
padding-top: 2px;
}
#log {
border-spacing: 0;
}
#log tr {
background: #fff;
}
#log td {
padding: 0 1px;
background: inherit;
border-bottom: 1px dotted #ccc;
border-top: 2px solid #fff;
vertical-align: top;
}
#log td:not(:last-child) {
max-width: 175px;
padding: 0 4px;
overflow: hidden;
white-space: nowrap;
}
#log td[data-title]:hover {
font-size: 1px;
overflow: inherit;
position: relative;
}
#log td:nth-child(3):hover:after {
content: attr(data-title);
display: block;
position: absolute;
border-radius: 4px;
box-shadow: 1px 1px 2px #ccc;
background: inherit;
border: 1px solid #ccc;
background: #efefef;
padding: 1px 1px 0 1px;
font-size: 10pt;
top: -2px;
left: 2px;
color: #000;
}
#log td:last-child {
width: 100%;
}
table#gameinfo,
table#modslist {
border: 1px solid #000000;
background: #ffffff;
border-radius: 5px;
border-spacing: 1px;
overflow: hidden;
cursor: default;
box-shadow: 1px 1px 1px 1px #dddddd;
}
#modslist {
min-width: 400px;
}
#gameinfo td:first-child {
padding-right: 5px;
}
#gameinfo tr,
#modslist tr {
background: #eee
}
#gameinfo tr:nth-child(even),
#modslist tr:nth-child(even) {
background: #fff
}
#modslist tr {
cursor: pointer;
}
span.notice {
font-weight: normal;
font-size: 11px;
position: relative;
top: -1px;
display: none;
}
span.notice.btn {
cursor: pointer;
border: 1px solid #000;
border-radius: 5px;
position: relative;
top: -1px;
padding: 0 2px;
background: #eee;
}
#output:not(.modfilter) span.notice.txt {
display: inline-block;
}
#output.modfilter span.notice.btn {
display: inline-block;
}

View File

@ -0,0 +1,107 @@
/* tags */
html {
height: 100%;
}
body {
height: 100%;
font-family: sans-serif;
}
h1, h2, h3 {
font-weight: bold;
margin: 0.2em 0 0.1em 0;
padding-top: .5em;
}
h1 {
font-size: 1.5em;
color: #888;
margin-bottom: 0;
}
h2 {
font-size: 1.5em;
border-bottom: 1px solid #AAA;
}
h3 {
font-size: 1.2em;
border-bottom: 1px solid #AAA;
width: 50%;
}
a {
color: #006;
}
/* content */
#content-column {
position: absolute;
top: 1em;
left: 10em;
}
#content {
min-height: 140px;
padding: 0 1em 1em 1em;
border-left: 1px solid #CCC;
background: #FFF;
font-size: 0.9em;
}
#content p {
max-width: 55em;
}
.section {
border: 1px solid #CCC;
padding: 0.5em;
margin-bottom: 1em;
}
/* sidebar */
#sidebar {
margin-top: 3em;
min-height: 75%;
width: 12em;
background: url("../images/sidebar-bg.gif") no-repeat top right;
color: #666;
}
#sidebar h4 {
margin: 0 0 0.2em 0;
width: 10em;
border-bottom: 1px solid #CCC;
font-size: 0.8em;
font-weight: normal;
}
#sidebar a {
color: #77B;
border: 0;
}
#sidebar ul, #sidebar li {
margin: 0;
padding: 0;
list-style: none none;
font-size: 0.9em;
color: #888;
}
#sidebar li {
margin-left: 1em;
}
/* footer */
#footer {
margin: 1em;
padding: 1em;
font-size: 0.6em;
color: gray;
}
#footer a {
color: #669;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,278 @@
/* globals $ */
var smapi = smapi || {};
smapi.logParser = function(sectionUrl, pasteID) {
/*********
** Initialisation
*********/
var stage,
flags = $("#modflags"),
output = $("#output"),
filters = 0,
memory = "",
versionInfo,
modInfo,
modMap,
modErrors,
logInfo,
templateBody = $("#template-body").text(),
templateModentry = $("#template-modentry").text(),
templateCss = $("#template-css").text(),
templateLogentry = $("#template-logentry").text(),
templateLognotice = $("#template-lognotice").text(),
regexInfo = /\[[\d\:]+ INFO SMAPI] SMAPI (.*?) with Stardew Valley (.*?) on (.*?)\n/g,
regexMods = /\[[^\]]+\] Loaded \d+ mods:(?:\n\[[^\]]+\] .+)+/g,
regexLog = /\[([\d\:]+) (TRACE|DEBUG|INFO|WARN|ALERT|ERROR) ? ([^\]]+)\] ?((?:\n|.)*?)(?=(?:\[\d\d:|$))/g,
regexMod = /\[(?:.*?)\] *(.*?) (\d+\.?(?:.*?))(?: by (.*?))? \|(?:.*?)$/gm,
regexDate = /\[\d{2}:\d{2}:\d{2} TRACE SMAPI\] Log started at (.*?) UTC/g,
regexPath = /\[\d{2}:\d{2}:\d{2} DEBUG SMAPI\] Mods go here: (.*?)(?:\n|$)/g;
$("#tabs li:not(.notice)").on("click", function(evt) {
var t = $(evt.currentTarget);
t.toggleClass("active");
$("#output").toggleClass(t.text().toLowerCase());
});
$("#upload-button").on("click", function() {
memory = $("#input").val() || "";
$("#input").val("");
$("#popup-upload").fadeIn();
});
$("#popup-upload").on({
'dragover dragenter': function(e) {
e.preventDefault();
e.stopPropagation();
},
'drop': function(e) {
$("#uploader").attr("data-text", "Reading...");
$("#uploader").show();
var dataTransfer = e.originalEvent.dataTransfer;
if (dataTransfer && dataTransfer.files.length) {
e.preventDefault();
e.stopPropagation();
var file = dataTransfer.files[0];
var reader = new FileReader();
reader.onload = $.proxy(function(file, $input, event) {
$input.val(event.target.result);
$("#uploader").fadeOut();
$("#submit").click();
}, this, file, $("#input"));
reader.readAsText(file);
}
}
});
$("#submit").on("click", function() {
$("#popup-upload").fadeOut();
var paste = $("#input").val();
if (paste) {
memory = "";
$("#uploader").attr("data-text", "Saving...");
$("#uploader").fadeIn();
$
.ajax({
type: "POST",
url: sectionUrl + "/save",
data: JSON.stringify(paste),
contentType: "application/json" // sent to API
})
.fail(function(xhr, textStatus) {
$("#uploader").fadeOut();
$("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: Upload</p>Error: ' + textStatus + ': ' + xhr.responseText + "<hr /><pre>" + $("#input").val() + "</pre></div>");
})
.then(function(data) {
$("#uploader").fadeOut();
if (!data.success)
$("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: Upload</p>Error: ' + data.error + "<hr />" + $("#input").val() + "</div>");
else
location.href = (sectionUrl.replace(/\/$/, "") + "/" + data.id);
});
} else {
alert("Unable to parse log, the input is empty!");
$("#uploader").fadeOut();
}
});
$("#cancel").on("click", function() {
$("#popup-upload").fadeOut(400, function() {
$("#input").val(memory);
memory = "";
});
});
$("#closeraw").on("click", function() {
$("#popup-raw").fadeOut(400);
});
if (pasteID) {
getData(pasteID);
}
else
$("#popup-upload").fadeIn();
/*********
** Helpers
*********/
function modClicked(evt) {
var id = $(evt.currentTarget).attr("id").split("-")[1],
cls = "mod-" + id;
if (output.hasClass(cls))
filters--;
else
filters++;
output.toggleClass(cls);
if (filters === 0) {
output.removeClass("modfilter");
} else {
output.addClass("modfilter");
}
}
function removeFilter() {
for (var c = 0; c < modInfo.length; c++) {
output.removeClass("mod-" + c);
}
filters = 0;
output.removeClass("modfilter");
}
function selectAll() {
for (var c = 0; c < modInfo.length; c++) {
output.addClass("mod-" + c);
}
filters = modInfo.length;
output.addClass("modfilter");
}
function parseData() {
stage = "parseData.pre";
var data = $("#input").val();
if (!data) {
stage = "parseData.checkNullData";
throw new Error("Field `data` is null");
}
var dataInfo = regexInfo.exec(data) || regexInfo.exec(data) || regexInfo.exec(data),
dataMods = regexMods.exec(data) || regexMods.exec(data) || regexMods.exec(data),
dataDate = regexDate.exec(data) || regexDate.exec(data) || regexDate.exec(data),
dataPath = regexPath.exec(data) || regexPath.exec(data) || regexPath.exec(data),
match;
stage = "parseData.doNullCheck";
if (!dataInfo)
throw new Error("Field `dataInfo` is null");
if (!dataMods)
throw new Error("Field `dataMods` is null");
if (!dataPath)
throw new Error("Field `dataPath` is null");
dataMods = dataMods[0];
stage = "parseData.setupDefaults";
modMap = {
"SMAPI": 0
};
modErrors = {
"SMAPI": 0,
"Console.Out": 0
};
logInfo = [];
modInfo = [
["SMAPI", dataInfo[1], "Zoryn, CLxS & Pathoschild"]
];
stage = "parseData.parseInfo";
var date = dataDate ? new Date(dataDate[1] + "Z") : null;
versionInfo = [dataInfo[1], dataInfo[2], dataInfo[3], date ? date.getFullYear() + "-" + ("0" + date.getMonth().toString()).substr(-2) + "-" + ("0" + date.getDay().toString()).substr(-2) + " at " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + " " + date.toLocaleTimeString("en-us", { timeZoneName: "short" }).split(" ")[2] : "No timestamp found", dataPath[1]];
stage = "parseData.parseMods";
while ((match = regexMod.exec(dataMods))) {
modErrors[match[1]] = 0;
modMap[match[1]] = modInfo.length;
modInfo.push([match[1], match[2], match[3] ? ("by " + match[3]) : "Unknown author"]);
}
stage = "parseData.parseLog";
while ((match = regexLog.exec(data))) {
if (match[2] === "ERROR")
modErrors[match[3]]++;
logInfo.push([match[1], match[2], match[3], match[4]]);
}
stage = "parseData.post";
modMap["Console.Out"] = modInfo.length;
modInfo.push(["Console.Out", "", ""]);
}
function renderData() {
stage = "renderData.pre";
output.html(prepare(templateBody, versionInfo));
var modslist = $("#modslist"), log = $("#log"), modCache = [], y = 0;
for (; y < modInfo.length; y++) {
var errors = modErrors[modInfo[y][0]],
err, cls = "color-red";
if (errors === 0) {
err = "No Errors";
cls = "color-green";
} else if (errors === 1)
err = "1 Error";
else
err = errors + " Errors";
modCache.push(prepare(templateModentry, [y, modInfo[y][0], modInfo[y][1], modInfo[y][2], cls, err]));
}
modslist.append(modCache.join(""));
for (var z = 0; z < modInfo.length; z++)
$("#modlink-" + z).on("click", modClicked);
var flagCache = [];
for (var c = 0; c < modInfo.length; c++)
flagCache.push(prepare(templateCss, [c]));
flags.html(flagCache.join(""));
var logCache = [], dupeCount = 0, dupeMemory = "|||";
for (var x = 0; x < logInfo.length; x++) {
var dm = logInfo[x][1] + "|" + logInfo[x][2] + "|" + logInfo[x][3];
if (dupeMemory !== dm) {
if (dupeCount > 0)
logCache.push(prepare(templateLognotice, [logInfo[x - 1][1].toLowerCase(), modMap[logInfo[x - 1][2]], dupeCount]));
dupeCount = 0;
dupeMemory = dm;
logCache.push(prepare(templateLogentry, [logInfo[x][1].toLowerCase(), modMap[logInfo[x][2]], logInfo[x][0], logInfo[x][1], logInfo[x][2], logInfo[x][3].split(" ").join("&nbsp ").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br />")]));
}
else
dupeCount++;
}
log.append(logCache.join(""));
$("#modlink-r").on("click", removeFilter);
$("#modlink-a").on("click", selectAll);
}
function prepare(str, arr) {
var regex = /\{(\d)\}/g,
match;
while ((match = regex.exec(str)))
str = str.replace(match[0], arr[match[1]]);
return str;
}
function loadData() {
try {
stage = "loadData.Pre";
var start = performance.now();
parseData();
renderData();
var end = performance.now();
$(".always").prepend("<div>Log processed in: " + (Math.round((end - start) * 100) / 100) + ' ms (<a id="viewraw" href="#">View raw</a>)</div><br />');
$("#viewraw").on("click", function() {
$("#dataraw").val($("#input").val());
$("#popup-raw").fadeIn();
});
stage = "loadData.Post";
}
catch (err) {
$("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: ' + stage + "</p>" + err + '<hr /><pre id="rawlog"></pre></div>');
$("#rawlog").text($("#input").val());
}
}
function getData(pasteID) {
$("#uploader").attr("data-text", "Loading...");
$("#uploader").fadeIn();
$.get(sectionUrl + "/fetch/" + pasteID, function(data) {
if (data.success) {
$("#input").val(data.content);
loadData();
} else {
$("#output").html('<div id="log" class="color-red"><h1>Fetching the log failed!</h1><p>' + data.error + '</p><pre id="rawlog"></pre></div>');
$("#rawlog").text($("#input").val());
}
$("#uploader").fadeOut();
});
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
VisualStudioVersion = 15.0.27004.2002
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainerMod", "TrainerMod\TrainerMod.csproj", "{28480467-1A48-46A7-99F8-236D95225359}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Mods.ConsoleCommands", "SMAPI.Mods.ConsoleCommands\StardewModdingAPI.Mods.ConsoleCommands.csproj", "{28480467-1A48-46A7-99F8-236D95225359}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI", "SMAPI\StardewModdingAPI.csproj", "{F1A573B0-F436-472C-AE29-0B91EA6B9F8F}"
EndProject
@ -37,6 +37,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{EB35A917-6
..\docs\mod-build-config.md = ..\docs\mod-build-config.md
..\docs\README.md = ..\docs\README.md
..\docs\release-notes.md = ..\docs\release-notes.md
..\docs\technical-docs.md = ..\docs\technical-docs.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{09CF91E5-5BAB-4650-A200-E5EA9A633046}"
@ -51,6 +52,7 @@ EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
SMAPI.Common\StardewModdingAPI.Common.projitems*{2aa02fb6-ff03-41cf-a215-2ee60ab4f5dc}*SharedItemsImports = 13
SMAPI.Common\StardewModdingAPI.Common.projitems*{443ddf81-6aaf-420a-a610-3459f37e5575}*SharedItemsImports = 4
SMAPI.Common\StardewModdingAPI.Common.projitems*{ea4f1e80-743f-4a1d-9757-ae66904a196a}*SharedItemsImports = 4
SMAPI.Common\StardewModdingAPI.Common.projitems*{f1a573b0-f436-472c-ae29-0b91ea6b9f8f}*SharedItemsImports = 4
EndGlobalSection

View File

@ -29,7 +29,7 @@ namespace StardewModdingAPI
** Public
****/
/// <summary>SMAPI's current semantic version.</summary>
public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(2, 0, 0);
public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(2, 1, 0);
/// <summary>The minimum supported version of Stardew Valley.</summary>
public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.30");

View File

@ -2,7 +2,6 @@ using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Utilities;
using StardewValley;
namespace StardewModdingAPI.Events
@ -20,7 +19,14 @@ namespace StardewModdingAPI.Events
public ICursorPosition Cursor { get; set; }
/// <summary>Whether the input is considered a 'click' by the game for enabling action.</summary>
public bool IsClick { get; }
[Obsolete("Use " + nameof(EventArgsInput.IsActionButton) + " or " + nameof(EventArgsInput.IsUseToolButton) + " instead")] // deprecated in SMAPI 2.1
public bool IsClick => this.IsActionButton;
/// <summary>Whether the input should trigger actions on the affected tile.</summary>
public bool IsActionButton { get; }
/// <summary>Whether the input should use tools on the affected tile.</summary>
public bool IsUseToolButton { get; }
/*********
@ -29,12 +35,14 @@ namespace StardewModdingAPI.Events
/// <summary>Construct an instance.</summary>
/// <param name="button">The button on the controller, keyboard, or mouse.</param>
/// <param name="cursor">The cursor position.</param>
/// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
public EventArgsInput(SButton button, ICursorPosition cursor, bool isClick)
/// <param name="isActionButton">Whether the input should trigger actions on the affected tile.</param>
/// <param name="isUseToolButton">Whether the input should use tools on the affected tile.</param>
public EventArgsInput(SButton button, ICursorPosition cursor, bool isActionButton, bool isUseToolButton)
{
this.Button = button;
this.Cursor = cursor;
this.IsClick = isClick;
this.IsActionButton = isActionButton;
this.IsUseToolButton = isUseToolButton;
}
/// <summary>Prevent the game from handling the vurrent button press. This doesn't prevent other mods from receiving the event.</summary>
@ -49,7 +57,7 @@ namespace StardewModdingAPI.Events
{
// keyboard
if (this.Button.TryGetKeyboard(out Keys key))
Game1.oldKBState = new KeyboardState(Game1.oldKBState.GetPressedKeys().Except(new[] { key }).ToArray());
Game1.oldKBState = new KeyboardState(Game1.oldKBState.GetPressedKeys().Union(new[] { key }).ToArray());
// controller
else if (this.Button.TryGetController(out Buttons controllerButton))

View File

@ -1,6 +1,5 @@
using System;
using StardewModdingAPI.Framework;
using StardewModdingAPI.Utilities;
namespace StardewModdingAPI.Events
{
@ -24,20 +23,22 @@ namespace StardewModdingAPI.Events
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="button">The button on the controller, keyboard, or mouse.</param>
/// <param name="cursor">The cursor position.</param>
/// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
internal static void InvokeButtonPressed(IMonitor monitor, SButton button, ICursorPosition cursor, bool isClick)
/// <param name="isActionButton">Whether the input should trigger actions on the affected tile.</param>
/// <param name="isUseToolButton">Whether the input should use tools on the affected tile.</param>
internal static void InvokeButtonPressed(IMonitor monitor, SButton button, ICursorPosition cursor, bool isActionButton, bool isUseToolButton)
{
monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonPressed)}", InputEvents.ButtonPressed?.GetInvocationList(), null, new EventArgsInput(button, cursor, isClick));
monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonPressed)}", InputEvents.ButtonPressed?.GetInvocationList(), null, new EventArgsInput(button, cursor, isActionButton, isUseToolButton));
}
/// <summary>Raise a <see cref="ButtonReleased"/> event.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="button">The button on the controller, keyboard, or mouse.</param>
/// <param name="cursor">The cursor position.</param>
/// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
internal static void InvokeButtonReleased(IMonitor monitor, SButton button, ICursorPosition cursor, bool isClick)
/// <param name="isActionButton">Whether the input should trigger actions on the affected tile.</param>
/// <param name="isUseToolButton">Whether the input should use tools on the affected tile.</param>
internal static void InvokeButtonReleased(IMonitor monitor, SButton button, ICursorPosition cursor, bool isActionButton, bool isUseToolButton)
{
monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonReleased)}", InputEvents.ButtonReleased?.GetInvocationList(), null, new EventArgsInput(button, cursor, isClick));
monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonReleased)}", InputEvents.ButtonReleased?.GetInvocationList(), null, new EventArgsInput(button, cursor, isActionButton, isUseToolButton));
}
}
}

View File

@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Xna.Framework;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.Reflection;
using StardewValley;
namespace StardewModdingAPI.Framework.Content
{
/// <summary>A low-level wrapper around the content cache which handles reading, writing, and invalidating entries in the cache. This doesn't handle any higher-level logic like localisation, loading content, etc. It assumes all keys passed in are already normalised.</summary>
internal class ContentCache
{
/*********
** Properties
*********/
/// <summary>The underlying asset cache.</summary>
private readonly IDictionary<string, object> Cache;
/// <summary>The possible directory separator characters in an asset key.</summary>
private readonly char[] PossiblePathSeparators;
/// <summary>The preferred directory separator chaeacter in an asset key.</summary>
private readonly string PreferredPathSeparator;
/// <summary>Applies platform-specific asset key normalisation so it's consistent with the underlying cache.</summary>
private readonly Func<string, string> NormaliseAssetNameForPlatform;
/*********
** Accessors
*********/
/// <summary>Get or set the value of a raw cache entry.</summary>
/// <param name="key">The cache key.</param>
public object this[string key]
{
get => this.Cache[key];
set => this.Cache[key] = value;
}
/// <summary>The current cache keys.</summary>
public IEnumerable<string> Keys => this.Cache.Keys;
/*********
** Public methods
*********/
/****
** Constructor
****/
/// <summary>Construct an instance.</summary>
/// <param name="contentManager">The underlying content manager whose cache to manage.</param>
/// <param name="reflection">Simplifies access to private game code.</param>
/// <param name="possiblePathSeparators">The possible directory separator characters in an asset key.</param>
/// <param name="preferredPathSeparator">The preferred directory separator chaeacter in an asset key.</param>
public ContentCache(LocalizedContentManager contentManager, Reflector reflection, char[] possiblePathSeparators, string preferredPathSeparator)
{
// init
this.Cache = reflection.GetPrivateField<Dictionary<string, object>>(contentManager, "loadedAssets").GetValue();
this.PossiblePathSeparators = possiblePathSeparators;
this.PreferredPathSeparator = preferredPathSeparator;
// get key normalisation logic
if (Constants.TargetPlatform == Platform.Windows)
{
IPrivateMethod method = reflection.GetPrivateMethod(typeof(TitleContainer), "GetCleanPath");
this.NormaliseAssetNameForPlatform = path => method.Invoke<string>(path);
}
else
this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
}
/****
** Fetch
****/
/// <summary>Get whether the cache contains a given key.</summary>
/// <param name="key">The cache key.</param>
public bool ContainsKey(string key)
{
return this.Cache.ContainsKey(key);
}
/****
** Normalise
****/
/// <summary>Normalise path separators in a file path. For asset keys, see <see cref="NormaliseKey"/> instead.</summary>
/// <param name="path">The file path to normalise.</param>
[Pure]
public string NormalisePathSeparators(string path)
{
string[] parts = path.Split(this.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
string normalised = string.Join(this.PreferredPathSeparator, parts);
if (path.StartsWith(this.PreferredPathSeparator))
normalised = this.PreferredPathSeparator + normalised; // keep root slash
return normalised;
}
/// <summary>Normalise a cache key so it's consistent with the underlying cache.</summary>
/// <param name="key">The asset key.</param>
[Pure]
public string NormaliseKey(string key)
{
key = this.NormalisePathSeparators(key);
return key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase)
? key.Substring(0, key.Length - 4)
: this.NormaliseAssetNameForPlatform(key);
}
/****
** Remove
****/
/// <summary>Remove an asset with the given key.</summary>
/// <param name="key">The cache key.</param>
/// <param name="dispose">Whether to dispose the entry value, if applicable.</param>
/// <returns>Returns the removed key (if any).</returns>
public bool Remove(string key, bool dispose)
{
// get entry
if (!this.Cache.TryGetValue(key, out object value))
return false;
// dispose & remove entry
if (dispose && value is IDisposable disposable)
disposable.Dispose();
return this.Cache.Remove(key);
}
/// <summary>Purge matched assets from the cache.</summary>
/// <param name="predicate">Matches the asset keys to invalidate.</param>
/// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param>
/// <returns>Returns the removed keys (if any).</returns>
public IEnumerable<string> Remove(Func<string, Type, bool> predicate, bool dispose = false)
{
List<string> removed = new List<string>();
foreach (string key in this.Cache.Keys.ToArray())
{
Type type = this.Cache[key].GetType();
if (predicate(key, type))
{
this.Remove(key, dispose);
removed.Add(key);
}
}
return removed;
}
}
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
namespace StardewModdingAPI.Framework
@ -22,6 +22,7 @@ namespace StardewModdingAPI.Framework
["1.06"] = "1.0.6",
["1.07"] = "1.0.7",
["1.07a"] = "1.0.8-prerelease1",
["1.08"] = "1.0.8",
["1.11"] = "1.1.1"
};

View File

@ -4,7 +4,6 @@ using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Exceptions;
@ -74,12 +73,12 @@ namespace StardewModdingAPI.Framework.ModHelpers
this.ContentManager = contentManager;
this.ModFolderPath = modFolderPath;
this.ModName = modName;
this.ModFolderPathFromContent = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath);
this.ModFolderPathFromContent = this.ContentManager.GetRelativePath(modFolderPath);
this.Monitor = monitor;
}
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Texture2D"/> and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Map"/>, <see cref="Texture2D"/>, and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
@ -88,9 +87,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
{
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}: {reasonPhrase}.");
this.AssertValidAssetKeyFormat(key);
try
{
this.AssertValidAssetKeyFormat(key);
switch (source)
{
case ContentSource.GameContent:
@ -103,26 +102,14 @@ namespace StardewModdingAPI.Framework.ModHelpers
throw GetContentError($"there's no matching file at path '{file.FullName}'.");
// get asset path
string assetPath = this.GetModAssetPath(key, file.FullName);
string assetName = this.GetModAssetPath(key, file.FullName);
// try cache
if (this.ContentManager.IsLoaded(assetPath))
return this.ContentManager.Load<T>(assetPath);
if (this.ContentManager.IsLoaded(assetName))
return this.ContentManager.Load<T>(assetName);
// load content
switch (file.Extension.ToLower())
{
// XNB file
case ".xnb":
{
T asset = this.ContentManager.Load<T>(assetPath);
if (asset is Map)
this.FixLocalMapTilesheets(asset as Map, key);
return asset;
}
// unpacked map
case ".tbin":
// fix map tilesheets
if (file.Extension.ToLower() == ".tbin")
{
// validate
if (typeof(T) != typeof(Map))
@ -131,31 +118,15 @@ namespace StardewModdingAPI.Framework.ModHelpers
// fetch & cache
FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName);
this.FixLocalMapTilesheets(map, key);
this.FixCustomTilesheetPaths(map, key);
// inject map
this.ContentManager.Inject(assetPath, map);
this.ContentManager.Inject(assetName, map, this.ContentManager);
return (T)(object)map;
}
// unpacked image
case ".png":
// validate
if (typeof(T) != typeof(Texture2D))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// fetch & cache
using (FileStream stream = File.OpenRead(file.FullName))
{
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
this.ContentManager.Inject(assetPath, texture);
return (T)(object)texture;
}
default:
throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.png', '.tbin', or '.xnb'.");
}
// load through content manager
return this.ContentManager.Load<T>(assetName);
default:
throw GetContentError($"unknown content source '{source}'.");
@ -193,9 +164,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <returns>Returns whether the given asset key was cached.</returns>
public bool InvalidateCache(string key)
{
this.Monitor.Log($"Requested cache invalidation for '{key}'.", LogLevel.Trace);
string actualKey = this.GetActualAssetKey(key, ContentSource.GameContent);
return this.ContentManager.InvalidateCache((otherKey, type) => otherKey.Equals(actualKey, StringComparison.InvariantCultureIgnoreCase));
this.Monitor.Log($"Requested cache invalidation for '{actualKey}'.", LogLevel.Trace);
return this.ContentManager.InvalidateCache(asset => asset.AssetNameEquals(actualKey));
}
/// <summary>Remove all assets of the given type from the cache so they're reloaded on the next request. <b>This can be a very expensive operation and should only be used in very specific cases.</b> This will reload core game assets if needed, but references to the former assets will still show the previous content.</summary>
@ -207,28 +178,50 @@ namespace StardewModdingAPI.Framework.ModHelpers
return this.ContentManager.InvalidateCache((key, type) => typeof(T).IsAssignableFrom(type));
}
/// <summary>Remove matching assets from the content cache so they're reloaded on the next request. This will reload core game assets if needed, but references to the former asset will still show the previous content.</summary>
/// <param name="predicate">A predicate matching the assets to invalidate.</param>
/// <returns>Returns whether any cache entries were invalidated.</returns>
public bool InvalidateCache(Func<IAssetInfo, bool> predicate)
{
this.Monitor.Log("Requested cache invalidation for all assets matching a predicate.", LogLevel.Trace);
return this.ContentManager.InvalidateCache(predicate);
}
/*********
** Private methods
*********/
/// <summary>Fix the tilesheets for a map loaded from the mod folder.</summary>
/// <summary>Assert that the given key has a valid format.</summary>
/// <param name="key">The asset key to check.</param>
/// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception>
[SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")]
private void AssertValidAssetKeyFormat(string key)
{
this.ContentManager.AssertValidAssetKeyFormat(key);
if (Path.IsPathRooted(key))
throw new ArgumentException("The asset key must not be an absolute path.");
}
/// <summary>Fix custom map tilesheet paths so they can be found by the content manager.</summary>
/// <param name="map">The map whose tilesheets to fix.</param>
/// <param name="mapKey">The map asset key within the mod folder.</param>
/// <exception cref="ContentLoadException">The map tilesheets could not be loaded.</exception>
/// <exception cref="ContentLoadException">A map tilesheet couldn't be resolved.</exception>
/// <remarks>
/// The game's logic for tilesheets in <see cref="Game1.setGraphicsForSeason"/> is a bit specialised. It boils down to this:
/// * If the location is indoors or the desert, or the image source contains 'path' or 'object', it's loaded as-is relative to the <c>Content</c> folder.
/// The game's logic for tilesheets in <see cref="Game1.setGraphicsForSeason"/> is a bit specialised. It boils
/// down to this:
/// * If the location is indoors or the desert, or the image source contains 'path' or 'object', it's loaded
/// as-is relative to the <c>Content</c> folder.
/// * Else it's loaded from <c>Content\Maps</c> with a seasonal prefix.
///
/// That logic doesn't work well in our case, mainly because we have no location metadata at this point.
/// Instead we use a more heuristic approach: check relative to the map file first, then relative to
/// <c>Content\Maps</c>, then <c>Content</c>. If the image source filename contains a seasonal prefix, we try
/// for a seasonal variation and then an exact match.
/// <c>Content\Maps</c>, then <c>Content</c>. If the image source filename contains a seasonal prefix, try for a
/// seasonal variation and then an exact match.
///
/// While that doesn't exactly match the game logic, it's close enough that it's unlikely to make a difference.
/// </remarks>
private void FixLocalMapTilesheets(Map map, string mapKey)
private void FixCustomTilesheetPaths(Map map, string mapKey)
{
// check map info
// get map info
if (!map.TileSheets.Any())
return;
mapKey = this.ContentManager.NormaliseAssetName(mapKey); // Mono's Path.GetDirectoryName doesn't handle Windows dir separators
@ -239,7 +232,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
{
string imageSource = tilesheet.ImageSource;
// validate
// validate tilesheet path
if (Path.IsPathRooted(imageSource) || imageSource.Split(SContentManager.PossiblePathSeparators).Contains(".."))
throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded. Tilesheet paths must be a relative path without directory climbing (../).");
@ -264,8 +257,8 @@ namespace StardewModdingAPI.Framework.ModHelpers
try
{
string key =
this.TryLoadTilesheetImageSource(relativeMapFolder, seasonalImageSource)
?? this.TryLoadTilesheetImageSource(relativeMapFolder, imageSource);
this.GetTilesheetAssetName(relativeMapFolder, seasonalImageSource)
?? this.GetTilesheetAssetName(relativeMapFolder, imageSource);
if (key != null)
{
tilesheet.ImageSource = key;
@ -282,34 +275,23 @@ namespace StardewModdingAPI.Framework.ModHelpers
}
}
/// <summary>Load a tilesheet image source if the file exists.</summary>
/// <param name="relativeMapFolder">The folder path containing the map, relative to the mod folder.</param>
/// <summary>Get the actual asset name for a tilesheet.</summary>
/// <param name="modRelativeMapFolder">The folder path containing the map, relative to the mod folder.</param>
/// <param name="imageSource">The tilesheet image source to load.</param>
/// <returns>Returns the loaded asset key (if it was loaded successfully).</returns>
/// <remarks>See remarks on <see cref="FixLocalMapTilesheets"/>.</remarks>
private string TryLoadTilesheetImageSource(string relativeMapFolder, string imageSource)
/// <returns>Returns the asset name.</returns>
/// <remarks>See remarks on <see cref="FixCustomTilesheetPaths"/>.</remarks>
private string GetTilesheetAssetName(string modRelativeMapFolder, string imageSource)
{
if (imageSource == null)
return null;
// check relative to map file
{
string localKey = Path.Combine(relativeMapFolder, imageSource);
string localKey = Path.Combine(modRelativeMapFolder, imageSource);
FileInfo localFile = this.GetModFile(localKey);
if (localFile.Exists)
{
try
{
this.Load<Texture2D>(localKey);
}
catch (Exception ex)
{
throw new ContentLoadException($"The local '{imageSource}' tilesheet couldn't be loaded.", ex);
}
return this.GetActualAssetKey(localKey);
}
}
// check relative to content folder
{
@ -327,7 +309,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
catch
{
// ignore file-not-found errors
// TODO: while it's useful to suppress a asset-not-found error here to avoid
// TODO: while it's useful to suppress an asset-not-found error here to avoid
// confusion, this is a pretty naive approach. Even if the file doesn't exist,
// the file may have been loaded through an IAssetLoader which failed. So even
// if the content file doesn't exist, that doesn't mean the error here is a
@ -343,18 +325,6 @@ namespace StardewModdingAPI.Framework.ModHelpers
return null;
}
/// <summary>Assert that the given key has a valid format.</summary>
/// <param name="key">The asset key to check.</param>
/// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception>
[SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "Parameter is only used for assertion checks by design.")]
private void AssertValidAssetKeyFormat(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("The asset key or local path is empty.");
if (key.Intersect(Path.GetInvalidPathChars()).Any())
throw new ArgumentException("The asset key or local path contains invalid characters.");
}
/// <summary>Get a file from the mod folder.</summary>
/// <param name="path">The asset path relative to the mod folder.</param>
private FileInfo GetModFile(string path)
@ -400,81 +370,5 @@ namespace StardewModdingAPI.Framework.ModHelpers
return absolutePath;
#endif
}
/// <summary>Get a directory path relative to a given root.</summary>
/// <param name="rootPath">The root path from which the path should be relative.</param>
/// <param name="targetPath">The target file path.</param>
private string GetRelativePath(string rootPath, string targetPath)
{
// convert to URIs
Uri from = new Uri(rootPath + "/");
Uri to = new Uri(targetPath + "/");
if (from.Scheme != to.Scheme)
throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{rootPath}'.");
// get relative path
return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString())
.Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform
}
/// <summary>Premultiply a texture's alpha values to avoid transparency issues in the game. This is only possible if the game isn't currently drawing.</summary>
/// <param name="texture">The texture to premultiply.</param>
/// <returns>Returns a premultiplied texture.</returns>
/// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks>
private Texture2D PremultiplyTransparency(Texture2D texture)
{
// validate
if (Context.IsInDrawLoop)
throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop.");
// process texture
SpriteBatch spriteBatch = Game1.spriteBatch;
GraphicsDevice gpu = Game1.graphics.GraphicsDevice;
using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height))
{
// create blank render target to premultiply
gpu.SetRenderTarget(renderTarget);
gpu.Clear(Color.Black);
// multiply each color by the source alpha, and write just the color values into the final texture
spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState
{
ColorDestinationBlend = Blend.Zero,
ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue,
AlphaDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.SourceAlpha,
ColorSourceBlend = Blend.SourceAlpha
});
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// copy the alpha values from the source texture into the final one without multiplying them
spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState
{
ColorWriteChannels = ColorWriteChannels.Alpha,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.One,
ColorSourceBlend = Blend.One
});
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// release GPU
gpu.SetRenderTarget(null);
// extract premultiplied data
Color[] data = new Color[texture.Width * texture.Height];
renderTarget.GetData(data);
// unset texture from GPU to regain control
gpu.Textures[0] = null;
// update texture with premultiplied data
texture.SetData(data);
}
return texture;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Reflection;
using StardewModdingAPI.Framework.Reflection;
namespace StardewModdingAPI.Framework.ModHelpers
@ -42,8 +43,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <returns>Returns the field wrapper, or <c>null</c> if the field doesn't exist and <paramref name="required"/> is <c>false</c>.</returns>
public IPrivateField<TValue> GetPrivateField<TValue>(object obj, string name, bool required = true)
{
this.AssertAccessAllowed(obj);
return this.Reflector.GetPrivateField<TValue>(obj, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateField<TValue>(obj, name, required)
);
}
/// <summary>Get a private static field.</summary>
@ -53,8 +55,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
public IPrivateField<TValue> GetPrivateField<TValue>(Type type, string name, bool required = true)
{
this.AssertAccessAllowed(type);
return this.Reflector.GetPrivateField<TValue>(type, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateField<TValue>(type, name, required)
);
}
/****
@ -67,8 +70,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private property is not found.</param>
public IPrivateProperty<TValue> GetPrivateProperty<TValue>(object obj, string name, bool required = true)
{
this.AssertAccessAllowed(obj);
return this.Reflector.GetPrivateProperty<TValue>(obj, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateProperty<TValue>(obj, name, required)
);
}
/// <summary>Get a private static property.</summary>
@ -78,8 +82,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private property is not found.</param>
public IPrivateProperty<TValue> GetPrivateProperty<TValue>(Type type, string name, bool required = true)
{
this.AssertAccessAllowed(type);
return this.Reflector.GetPrivateProperty<TValue>(type, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateProperty<TValue>(type, name, required)
);
}
/****
@ -98,7 +103,6 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// </remarks>
public TValue GetPrivateValue<TValue>(object obj, string name, bool required = true)
{
this.AssertAccessAllowed(obj);
IPrivateField<TValue> field = this.GetPrivateField<TValue>(obj, name, required);
return field != null
? field.GetValue()
@ -117,7 +121,6 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// </remarks>
public TValue GetPrivateValue<TValue>(Type type, string name, bool required = true)
{
this.AssertAccessAllowed(type);
IPrivateField<TValue> field = this.GetPrivateField<TValue>(type, name, required);
return field != null
? field.GetValue()
@ -133,8 +136,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
public IPrivateMethod GetPrivateMethod(object obj, string name, bool required = true)
{
this.AssertAccessAllowed(obj);
return this.Reflector.GetPrivateMethod(obj, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateMethod(obj, name, required)
);
}
/// <summary>Get a private static method.</summary>
@ -143,8 +147,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
public IPrivateMethod GetPrivateMethod(Type type, string name, bool required = true)
{
this.AssertAccessAllowed(type);
return this.Reflector.GetPrivateMethod(type, name, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateMethod(type, name, required)
);
}
/****
@ -157,8 +162,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
public IPrivateMethod GetPrivateMethod(object obj, string name, Type[] argumentTypes, bool required = true)
{
this.AssertAccessAllowed(obj);
return this.Reflector.GetPrivateMethod(obj, name, argumentTypes, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateMethod(obj, name, argumentTypes, required)
);
}
/// <summary>Get a private static method.</summary>
@ -168,33 +174,60 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
public IPrivateMethod GetPrivateMethod(Type type, string name, Type[] argumentTypes, bool required = true)
{
this.AssertAccessAllowed(type);
return this.Reflector.GetPrivateMethod(type, name, argumentTypes, required);
return this.AssertAccessAllowed(
this.Reflector.GetPrivateMethod(type, name, argumentTypes, required)
);
}
/*********
** Private methods
*********/
/// <summary>Assert that mods can use the reflection helper to access the given type.</summary>
/// <param name="type">The type being accessed.</param>
private void AssertAccessAllowed(Type type)
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
/// <typeparam name="T">The field value type.</typeparam>
/// <param name="field">The field being accessed.</param>
/// <returns>Returns the same field instance for convenience.</returns>
private IPrivateField<T> AssertAccessAllowed<T>(IPrivateField<T> field)
{
// validate type namespace
if (type.Namespace != null)
{
string rootSmapiNamespace = typeof(Program).Namespace;
if (type.Namespace == rootSmapiNamespace || type.Namespace.StartsWith(rootSmapiNamespace + "."))
throw new InvalidOperationException($"SMAPI blocked access by {this.ModName} to its internals through the reflection API. Accessing the SMAPI internals is strongly discouraged since they're subject to change, which means the mod can break without warning.");
}
this.AssertAccessAllowed(field?.FieldInfo);
return field;
}
/// <summary>Assert that mods can use the reflection helper to access the given type.</summary>
/// <param name="obj">The object being accessed.</param>
private void AssertAccessAllowed(object obj)
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
/// <typeparam name="T">The property value type.</typeparam>
/// <param name="property">The property being accessed.</param>
/// <returns>Returns the same property instance for convenience.</returns>
private IPrivateProperty<T> AssertAccessAllowed<T>(IPrivateProperty<T> property)
{
if (obj != null)
this.AssertAccessAllowed(obj.GetType());
this.AssertAccessAllowed(property?.PropertyInfo);
return property;
}
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
/// <param name="method">The method being accessed.</param>
/// <returns>Returns the same method instance for convenience.</returns>
private IPrivateMethod AssertAccessAllowed(IPrivateMethod method)
{
this.AssertAccessAllowed(method?.MethodInfo);
return method;
}
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
/// <param name="member">The member being accessed.</param>
private void AssertAccessAllowed(MemberInfo member)
{
if (member == null)
return;
// get type which defines the member
Type declaringType = member.DeclaringType;
if (declaringType == null)
throw new InvalidOperationException($"Can't validate access to {member.MemberType} {member.Name} because it has no declaring type."); // should never happen
// validate access
string rootNamespace = typeof(Program).Namespace;
if (declaringType.Namespace == rootNamespace || declaringType.Namespace?.StartsWith(rootNamespace + ".") == true)
throw new InvalidOperationException($"SMAPI blocked access by {this.ModName} to its internals through the reflection API. Accessing the SMAPI internals is strongly discouraged since they're subject to change, which means the mod can break without warning. (Detected access to {declaringType.FullName}.{member.Name}.)");
}
}
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
namespace StardewModdingAPI.Framework.Reflection
@ -10,14 +10,14 @@ namespace StardewModdingAPI.Framework.Reflection
/*********
** Properties
*********/
/// <summary>The type that has the field.</summary>
private readonly Type ParentType;
/// <summary>The object that has the instance field (if applicable).</summary>
private readonly object Parent;
/// <summary>The display name shown in error messages.</summary>
private string DisplayName => $"{this.ParentType.FullName}::{this.PropertyInfo.Name}";
private readonly string DisplayName;
/// <summary>The underlying property getter.</summary>
private readonly Func<TValue> GetterDelegate;
/// <summary>The underlying property setter.</summary>
private readonly Action<TValue> SetterDelegate;
/*********
@ -39,20 +39,24 @@ namespace StardewModdingAPI.Framework.Reflection
/// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static field, or not null for a static field.</exception>
public PrivateProperty(Type parentType, object obj, PropertyInfo property, bool isStatic)
{
// validate
// validate input
if (parentType == null)
throw new ArgumentNullException(nameof(parentType));
if (property == null)
throw new ArgumentNullException(nameof(property));
// validate static
if (isStatic && obj != null)
throw new ArgumentException("A static property cannot have an object instance.");
if (!isStatic && obj == null)
throw new ArgumentException("A non-static property must have an object instance.");
// save
this.ParentType = parentType;
this.Parent = obj;
this.DisplayName = $"{parentType.FullName}::{property.Name}";
this.PropertyInfo = property;
this.GetterDelegate = (Func<TValue>)Delegate.CreateDelegate(typeof(Func<TValue>), obj, this.PropertyInfo.GetMethod);
this.SetterDelegate = (Action<TValue>)Delegate.CreateDelegate(typeof(Action<TValue>), obj, this.PropertyInfo.SetMethod);
}
/// <summary>Get the property value.</summary>
@ -60,7 +64,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
return (TValue)this.PropertyInfo.GetValue(this.Parent);
return this.GetterDelegate();
}
catch (InvalidCastException)
{
@ -78,7 +82,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
this.PropertyInfo.SetValue(this.Parent, value);
this.SetterDelegate(value);
}
catch (InvalidCastException)
{

View File

@ -38,7 +38,7 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a private instance field from a null object.");
// get field from hierarchy
IPrivateField<TValue> field = this.GetFieldFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic);
IPrivateField<TValue> field = this.GetFieldFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && field == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance field.");
return field;
@ -52,7 +52,7 @@ namespace StardewModdingAPI.Framework.Reflection
public IPrivateField<TValue> GetPrivateField<TValue>(Type type, string name, bool required = true)
{
// get field from hierarchy
IPrivateField<TValue> field = this.GetFieldFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Static);
IPrivateField<TValue> field = this.GetFieldFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public);
if (required && field == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static field.");
return field;
@ -73,7 +73,7 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a private instance property from a null object.");
// get property from hierarchy
IPrivateProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic);
IPrivateProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && property == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance property.");
return property;
@ -87,7 +87,7 @@ namespace StardewModdingAPI.Framework.Reflection
public IPrivateProperty<TValue> GetPrivateProperty<TValue>(Type type, string name, bool required = true)
{
// get field from hierarchy
IPrivateProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Static);
IPrivateProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (required && property == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static property.");
return property;
@ -107,7 +107,7 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a private instance method from a null object.");
// get method from hierarchy
IPrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic);
IPrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && method == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance method.");
return method;
@ -120,7 +120,7 @@ namespace StardewModdingAPI.Framework.Reflection
public IPrivateMethod GetPrivateMethod(Type type, string name, bool required = true)
{
// get method from hierarchy
IPrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Static);
IPrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (required && method == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static method.");
return method;
@ -141,7 +141,7 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a private instance method from a null object.");
// get method from hierarchy
PrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic, argumentTypes);
PrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, argumentTypes);
if (required && method == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance method with that signature.");
return method;
@ -155,7 +155,7 @@ namespace StardewModdingAPI.Framework.Reflection
public IPrivateMethod GetPrivateMethod(Type type, string name, Type[] argumentTypes, bool required = true)
{
// get field from hierarchy
PrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Static, argumentTypes);
PrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, argumentTypes);
if (required && method == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static method with that signature.");
return method;

View File

@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.Utilities;
using StardewModdingAPI.Metadata;
@ -15,7 +19,17 @@ using StardewValley;
namespace StardewModdingAPI.Framework
{
/// <summary>SMAPI's implementation of the game's content manager which lets it raise content events.</summary>
/// <summary>A thread-safe content manager which intercepts assets being loaded to let SMAPI mods inject or edit them.</summary>
/// <remarks>
/// This is the centralised content manager which manages all game assets. The game and mods don't use this class
/// directly; instead they use one of several <see cref="ContentManagerShim"/> instances, which proxy requests to
/// this class. That ensures that when the game disposes one content manager, the others can continue unaffected.
/// That notably requires this class to be thread-safe, since the content managers can be disposed asynchronously.
///
/// Note that assets in the cache have two identifiers: the asset name (like "bundles") and key (like "bundles.pt-BR").
/// For English and non-translatable assets, these have the same value. The underlying cache only knows about asset
/// keys, and the game and mods only know about asset names. The content manager handles resolving them.
/// </remarks>
internal class SContentManager : LocalizedContentManager
{
/*********
@ -27,11 +41,8 @@ namespace StardewModdingAPI.Framework
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/// <summary>The underlying content manager's asset cache.</summary>
private readonly IDictionary<string, object> Cache;
/// <summary>Applies platform-specific asset key normalisation so it's consistent with the underlying cache.</summary>
private readonly Func<string, string> NormaliseAssetNameForPlatform;
/// <summary>The underlying asset cache.</summary>
private readonly ContentCache Cache;
/// <summary>The private <see cref="LocalizedContentManager"/> method which generates the locale portion of an asset name.</summary>
private readonly IPrivateMethod GetKeyLocale;
@ -46,10 +57,13 @@ namespace StardewModdingAPI.Framework
private readonly ContextHash<string> AssetsBeingLoaded = new ContextHash<string>();
/// <summary>A lookup of the content managers which loaded each asset.</summary>
private readonly IDictionary<string, HashSet<ContentManager>> AssetLoaders = new Dictionary<string, HashSet<ContentManager>>();
private readonly IDictionary<string, HashSet<ContentManager>> ContentManagersByAssetKey = new Dictionary<string, HashSet<ContentManager>>();
/// <summary>An object locked to prevent concurrent changes to the underlying assets.</summary>
private readonly object Lock = new object();
/// <summary>The path prefix for assets in mod folders.</summary>
private readonly string ModContentPrefix;
/// <summary>A lock used to prevents concurrent changes to the cache while data is being read.</summary>
private readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
/*********
@ -71,121 +85,176 @@ namespace StardewModdingAPI.Framework
/*********
** Public methods
*********/
/****
** Constructor
****/
/// <summary>Construct an instance.</summary>
/// <param name="serviceProvider">The service provider to use to locate services.</param>
/// <param name="rootDirectory">The root directory to search for content.</param>
/// <param name="currentCulture">The current culture for which to localise content.</param>
/// <param name="languageCodeOverride">The current language code for which to localise content.</param>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
public SContentManager(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor)
/// <param name="reflection">Simplifies access to private code.</param>
public SContentManager(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor, Reflector reflection)
: base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride)
{
// validate
if (monitor == null)
throw new ArgumentNullException(nameof(monitor));
// initialise
var reflection = new Reflector();
this.Monitor = monitor;
// get underlying fields for interception
this.Cache = reflection.GetPrivateField<Dictionary<string, object>>(this, "loadedAssets").GetValue();
// init
this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor));
this.Cache = new ContentCache(this, reflection, SContentManager.PossiblePathSeparators, SContentManager.PreferredPathSeparator);
this.GetKeyLocale = reflection.GetPrivateMethod(this, "languageCode");
// get asset key normalisation logic
if (Constants.TargetPlatform == Platform.Windows)
{
IPrivateMethod method = reflection.GetPrivateMethod(typeof(TitleContainer), "GetCleanPath");
this.NormaliseAssetNameForPlatform = path => method.Invoke<string>(path);
}
else
this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
this.ModContentPrefix = this.GetRelativePath(Constants.ModPath);
// get asset data
this.CoreAssets = new CoreAssets(this.NormaliseAssetName);
this.KeyLocales = this.GetKeyLocales(reflection);
}
/****
** Asset key/name handling
****/
/// <summary>Normalise path separators in a file path. For asset keys, see <see cref="NormaliseAssetName"/> instead.</summary>
/// <param name="path">The file path to normalise.</param>
[Pure]
public string NormalisePathSeparators(string path)
{
string[] parts = path.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
string normalised = string.Join(SContentManager.PreferredPathSeparator, parts);
if (path.StartsWith(SContentManager.PreferredPathSeparator))
normalised = SContentManager.PreferredPathSeparator + normalised; // keep root slash
return normalised;
return this.Cache.NormalisePathSeparators(path);
}
/// <summary>Normalise an asset name so it's consistent with the underlying cache.</summary>
/// <param name="assetName">The asset key.</param>
[Pure]
public string NormaliseAssetName(string assetName)
{
assetName = this.NormalisePathSeparators(assetName);
if (assetName.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase))
return assetName.Substring(0, assetName.Length - 4);
return this.NormaliseAssetNameForPlatform(assetName);
return this.Cache.NormaliseKey(assetName);
}
/// <summary>Assert that the given key has a valid format.</summary>
/// <param name="key">The asset key to check.</param>
/// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception>
[SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")]
public void AssertValidAssetKeyFormat(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("The asset key or local path is empty.");
if (key.Intersect(Path.GetInvalidPathChars()).Any())
throw new ArgumentException("The asset key or local path contains invalid characters.");
}
/// <summary>Get a directory path relative to the content root.</summary>
/// <param name="targetPath">The target file path.</param>
public string GetRelativePath(string targetPath)
{
// convert to URIs
Uri from = new Uri(this.FullRootDirectory + "/");
Uri to = new Uri(targetPath + "/");
if (from.Scheme != to.Scheme)
throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{this.FullRootDirectory}'.");
// get relative path
return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString())
.Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform
}
/****
** Content loading
****/
/// <summary>Get the current content locale.</summary>
public string GetLocale()
{
return this.GetKeyLocale.Invoke<string>();
}
/// <summary>Get whether the content manager has already loaded and cached the given asset.</summary>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
public bool IsLoaded(string assetName)
{
lock (this.Lock)
{
assetName = this.NormaliseAssetName(assetName);
return this.IsNormalisedKeyLoaded(assetName);
}
assetName = this.Cache.NormaliseKey(assetName);
return this.WithReadLock(() => this.IsNormalisedKeyLoaded(assetName));
}
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <summary>Get the cached asset keys.</summary>
public IEnumerable<string> GetAssetKeys()
{
return this.WithReadLock(() =>
this.Cache.Keys
.Select(this.GetAssetName)
.Distinct()
);
}
/// <summary>Load an asset through the content pipeline. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected asset type.</typeparam>
/// <param name="assetName">The asset path relative to the content directory.</param>
public override T Load<T>(string assetName)
{
return this.LoadFor<T>(assetName, this);
}
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <summary>Load an asset through the content pipeline. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected asset type.</typeparam>
/// <param name="assetName">The asset path relative to the content directory.</param>
/// <param name="instance">The content manager instance for which to load the asset.</param>
/// <exception cref="ArgumentException">The <paramref name="assetName"/> is empty or contains invalid characters.</exception>
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
public T LoadFor<T>(string assetName, ContentManager instance)
{
lock (this.Lock)
{
// normalise asset key
this.AssertValidAssetKeyFormat(assetName);
assetName = this.NormaliseAssetName(assetName);
// skip if already loaded
if (this.IsNormalisedKeyLoaded(assetName))
// load game content
if (!assetName.StartsWith(this.ModContentPrefix))
return this.LoadImpl<T>(assetName, instance);
// load mod content
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading content asset '{assetName}': {reasonPhrase}.");
try
{
this.TrackAssetLoader(assetName, instance);
return base.Load<T>(assetName);
return this.WithWriteLock(() =>
{
// try cache
if (this.IsLoaded(assetName))
return this.LoadImpl<T>(assetName, instance);
// get file
FileInfo file = this.GetModFile(assetName);
if (!file.Exists)
throw GetContentError("the specified path doesn't exist.");
// load content
switch (file.Extension.ToLower())
{
// XNB file
case ".xnb":
return this.LoadImpl<T>(assetName, instance);
// unpacked map
case ".tbin":
throw GetContentError($"can't read unpacked map file '{assetName}' directly from the underlying content manager. It must be loaded through the mod's {typeof(IModHelper)}.{nameof(IModHelper.Content)} helper.");
// unpacked image
case ".png":
// validate
if (typeof(T) != typeof(Texture2D))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// fetch & cache
using (FileStream stream = File.OpenRead(file.FullName))
{
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
this.InjectWithoutLock(assetName, texture, instance);
return (T)(object)texture;
}
// load asset
T data;
if (this.AssetsBeingLoaded.Contains(assetName))
{
this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", LogLevel.Warn);
this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}", LogLevel.Trace);
data = base.Load<T>(assetName);
default:
throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.png', '.tbin', or '.xnb'.");
}
else
{
data = this.AssetsBeingLoaded.Track(assetName, () =>
{
IAssetInfo info = new AssetInfo(this.GetLocale(), assetName, typeof(T), this.NormaliseAssetName);
IAssetData asset = this.ApplyLoader<T>(info) ?? new AssetDataForObject(info, base.Load<T>(assetName), this.NormaliseAssetName);
asset = this.ApplyEditors<T>(info, asset);
return (T)asset.Data;
});
}
// update cache & return data
this.Cache[assetName] = data;
this.TrackAssetLoader(assetName, instance);
return data;
catch (Exception ex) when (!(ex is SContentLoadException))
{
throw new SContentLoadException($"The content manager failed loading content asset '{assetName}'.", ex);
}
}
@ -193,40 +262,15 @@ namespace StardewModdingAPI.Framework
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
public void Inject<T>(string assetName, T value)
/// <param name="instance">The content manager instance for which to load the asset.</param>
public void Inject<T>(string assetName, T value, ContentManager instance)
{
lock (this.Lock)
{
assetName = this.NormaliseAssetName(assetName);
this.Cache[assetName] = value;
this.TrackAssetLoader(assetName, this);
}
}
/// <summary>Get the current content locale.</summary>
public string GetLocale()
{
return this.GetKeyLocale.Invoke<string>();
}
/// <summary>Get the cached asset keys.</summary>
public IEnumerable<string> GetAssetKeys()
{
lock (this.Lock)
{
IEnumerable<string> GetAllAssetKeys()
{
foreach (string cacheKey in this.Cache.Keys)
{
this.ParseCacheKey(cacheKey, out string assetKey, out string _);
yield return assetKey;
}
}
return GetAllAssetKeys().Distinct();
}
this.WithWriteLock(() => this.InjectWithoutLock(assetName, value, instance));
}
/****
** Cache invalidation
****/
/// <summary>Purge assets from the cache that match one of the interceptors.</summary>
/// <param name="editors">The asset editors for which to purge matching assets.</param>
/// <param name="loaders">The asset loaders for which to purge matching assets.</param>
@ -239,21 +283,34 @@ namespace StardewModdingAPI.Framework
// get CanEdit/Load methods
MethodInfo canEdit = typeof(IAssetEditor).GetMethod(nameof(IAssetEditor.CanEdit));
MethodInfo canLoad = typeof(IAssetLoader).GetMethod(nameof(IAssetLoader.CanLoad));
if (canEdit == null || canLoad == null)
throw new InvalidOperationException("SMAPI could not access the interceptor methods."); // should never happen
// invalidate matching keys
return this.InvalidateCache((assetName, assetType) =>
return this.InvalidateCache(asset =>
{
// get asset metadata
IAssetInfo info = new AssetInfo(this.GetLocale(), assetName, assetType, this.NormaliseAssetName);
// check loaders
MethodInfo canLoadGeneric = canLoad.MakeGenericMethod(assetType);
if (loaders.Any(loader => (bool)canLoadGeneric.Invoke(loader, new object[] { info })))
MethodInfo canLoadGeneric = canLoad.MakeGenericMethod(asset.DataType);
if (loaders.Any(loader => (bool)canLoadGeneric.Invoke(loader, new object[] { asset })))
return true;
// check editors
MethodInfo canEditGeneric = canEdit.MakeGenericMethod(assetType);
return editors.Any(editor => (bool)canEditGeneric.Invoke(editor, new object[] { info }));
MethodInfo canEditGeneric = canEdit.MakeGenericMethod(asset.DataType);
return editors.Any(editor => (bool)canEditGeneric.Invoke(editor, new object[] { asset }));
});
}
/// <summary>Purge matched assets from the cache.</summary>
/// <param name="predicate">Matches the asset keys to invalidate.</param>
/// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param>
/// <returns>Returns whether any cache entries were invalidated.</returns>
public bool InvalidateCache(Func<IAssetInfo, bool> predicate, bool dispose = false)
{
string locale = this.GetLocale();
return this.InvalidateCache((assetName, type) =>
{
IAssetInfo info = new AssetInfo(locale, assetName, type, this.NormaliseAssetName);
return predicate(info);
});
}
@ -263,83 +320,81 @@ namespace StardewModdingAPI.Framework
/// <returns>Returns whether any cache entries were invalidated.</returns>
public bool InvalidateCache(Func<string, Type, bool> predicate, bool dispose = false)
{
lock (this.Lock)
return this.WithWriteLock(() =>
{
// find matching asset keys
HashSet<string> purgeCacheKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
HashSet<string> purgeAssetKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
foreach (string cacheKey in this.Cache.Keys)
// invalidate matching keys
HashSet<string> removeKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
HashSet<string> removeAssetNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
this.Cache.Remove((key, type) =>
{
this.ParseCacheKey(cacheKey, out string assetKey, out _);
Type type = this.Cache[cacheKey].GetType();
if (predicate(assetKey, type))
this.ParseCacheKey(key, out string assetName, out _);
if (removeAssetNames.Contains(assetName) || predicate(assetName, type))
{
purgeAssetKeys.Add(assetKey);
purgeCacheKeys.Add(cacheKey);
}
removeAssetNames.Add(assetName);
removeKeys.Add(key);
return true;
}
return false;
});
// purge assets
foreach (string key in purgeCacheKeys)
{
if (dispose && this.Cache[key] is IDisposable disposable)
disposable.Dispose();
this.Cache.Remove(key);
this.AssetLoaders.Remove(key);
}
// update reference tracking
foreach (string key in removeKeys)
this.ContentManagersByAssetKey.Remove(key);
// reload core game assets
int reloaded = 0;
foreach (string key in purgeAssetKeys)
foreach (string key in removeAssetNames)
{
if (this.CoreAssets.ReloadForKey(this, key))
reloaded++;
}
// report result
if (purgeCacheKeys.Any())
if (removeKeys.Any())
{
this.Monitor.Log($"Invalidated {purgeCacheKeys.Count} cache entries for {purgeAssetKeys.Count} asset keys: {string.Join(", ", purgeCacheKeys.OrderBy(p => p, StringComparer.InvariantCultureIgnoreCase))}. Reloaded {reloaded} core assets.", LogLevel.Trace);
this.Monitor.Log($"Invalidated {removeAssetNames.Count} asset names: {string.Join(", ", removeKeys.OrderBy(p => p, StringComparer.InvariantCultureIgnoreCase))}. Reloaded {reloaded} core assets.", LogLevel.Trace);
return true;
}
this.Monitor.Log("Invalidated 0 cache entries.", LogLevel.Trace);
return false;
}
});
}
/****
** Disposal
****/
/// <summary>Dispose assets for the given content manager shim.</summary>
/// <param name="shim">The content manager whose assets to dispose.</param>
internal void DisposeFor(ContentManagerShim shim)
{
this.Monitor.Log($"Content manager '{shim.Name}' disposed, disposing assets that aren't needed by any other asset loader.", LogLevel.Trace);
foreach (var entry in this.AssetLoaders)
this.WithWriteLock(() =>
{
foreach (var entry in this.ContentManagersByAssetKey)
entry.Value.Remove(shim);
this.InvalidateCache((key, type) => !this.AssetLoaders[key].Any(), dispose: true);
this.InvalidateCache((key, type) => !this.ContentManagersByAssetKey[key].Any(), dispose: true);
});
}
/*********
** Private methods
*********/
/// <summary>Get whether an asset has already been loaded.</summary>
/// <param name="normalisedAssetName">The normalised asset name.</param>
private bool IsNormalisedKeyLoaded(string normalisedAssetName)
/****
** Disposal
****/
/// <summary>Dispose held resources.</summary>
/// <param name="disposing">Whether the content manager is disposing (rather than finalising).</param>
protected override void Dispose(bool disposing)
{
return this.Cache.ContainsKey(normalisedAssetName)
|| this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke<string>()}"); // translated asset
}
/// <summary>Track that a content manager loaded an asset.</summary>
/// <param name="key">The asset key that was loaded.</param>
/// <param name="manager">The content manager that loaded the asset.</param>
private void TrackAssetLoader(string key, ContentManager manager)
{
if (!this.AssetLoaders.TryGetValue(key, out HashSet<ContentManager> hash))
hash = this.AssetLoaders[key] = new HashSet<ContentManager>();
hash.Add(manager);
this.Monitor.Log("Disposing SMAPI's main content manager. It will no longer be usable after this point.", LogLevel.Trace);
base.Dispose(disposing);
}
/****
** Asset name/key handling
****/
/// <summary>Get the locale codes (like <c>ja-JP</c>) used in asset keys.</summary>
/// <param name="reflection">Simplifies access to private game code.</param>
private IDictionary<string, LanguageCode> GetKeyLocales(Reflector reflection)
@ -367,11 +422,19 @@ namespace StardewModdingAPI.Framework
return map;
}
/// <summary>Get the asset name from a cache key.</summary>
/// <param name="cacheKey">The input cache key.</param>
private string GetAssetName(string cacheKey)
{
this.ParseCacheKey(cacheKey, out string assetName, out string _);
return assetName;
}
/// <summary>Parse a cache key into its component parts.</summary>
/// <param name="cacheKey">The input cache key.</param>
/// <param name="assetKey">The original asset key.</param>
/// <param name="assetName">The original asset name.</param>
/// <param name="localeCode">The asset locale code (or <c>null</c> if not localised).</param>
private void ParseCacheKey(string cacheKey, out string assetKey, out string localeCode)
private void ParseCacheKey(string cacheKey, out string assetName, out string localeCode)
{
// handle localised key
if (!string.IsNullOrWhiteSpace(cacheKey))
@ -382,7 +445,7 @@ namespace StardewModdingAPI.Framework
string suffix = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1);
if (this.KeyLocales.ContainsKey(suffix))
{
assetKey = cacheKey.Substring(0, lastSepIndex);
assetName = cacheKey.Substring(0, lastSepIndex);
localeCode = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1);
return;
}
@ -390,10 +453,117 @@ namespace StardewModdingAPI.Framework
}
// handle simple key
assetKey = cacheKey;
assetName = cacheKey;
localeCode = null;
}
/****
** Cache handling
****/
/// <summary>Get whether an asset has already been loaded.</summary>
/// <param name="normalisedAssetName">The normalised asset name.</param>
private bool IsNormalisedKeyLoaded(string normalisedAssetName)
{
return this.Cache.ContainsKey(normalisedAssetName)
|| this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke<string>()}"); // translated asset
}
/// <summary>Track that a content manager loaded an asset.</summary>
/// <param name="key">The asset key that was loaded.</param>
/// <param name="manager">The content manager that loaded the asset.</param>
private void TrackAssetLoader(string key, ContentManager manager)
{
if (!this.ContentManagersByAssetKey.TryGetValue(key, out HashSet<ContentManager> hash))
hash = this.ContentManagersByAssetKey[key] = new HashSet<ContentManager>();
hash.Add(manager);
}
/****
** Content loading
****/
/// <summary>Load an asset name without heuristics to support mod content.</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="instance">The content manager instance for which to load the asset.</param>
private T LoadImpl<T>(string assetName, ContentManager instance)
{
return this.WithWriteLock(() =>
{
// skip if already loaded
if (this.IsNormalisedKeyLoaded(assetName))
{
this.TrackAssetLoader(assetName, instance);
return base.Load<T>(assetName);
}
// load asset
T data;
if (this.AssetsBeingLoaded.Contains(assetName))
{
this.Monitor.Log($"Broke loop while loading asset '{assetName}'.", LogLevel.Warn);
this.Monitor.Log($"Bypassing mod loaders for this asset. Stack trace:\n{Environment.StackTrace}", LogLevel.Trace);
data = base.Load<T>(assetName);
}
else
{
data = this.AssetsBeingLoaded.Track(assetName, () =>
{
IAssetInfo info = new AssetInfo(this.GetLocale(), assetName, typeof(T), this.NormaliseAssetName);
IAssetData asset = this.ApplyLoader<T>(info) ?? new AssetDataForObject(info, base.Load<T>(assetName), this.NormaliseAssetName);
asset = this.ApplyEditors<T>(info, asset);
return (T)asset.Data;
});
}
// update cache & return data
this.InjectWithoutLock(assetName, data, instance);
return data;
});
}
/// <summary>Inject an asset into the cache without acquiring a write lock. This should only be called from within a write lock.</summary>
/// <typeparam name="T">The type of asset to inject.</typeparam>
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
/// <param name="value">The asset value.</param>
/// <param name="instance">The content manager instance for which to load the asset.</param>
private void InjectWithoutLock<T>(string assetName, T value, ContentManager instance)
{
assetName = this.NormaliseAssetName(assetName);
this.Cache[assetName] = value;
this.TrackAssetLoader(assetName, instance);
}
/// <summary>Get a file from the mod folder.</summary>
/// <param name="path">The asset path relative to the content folder.</param>
private FileInfo GetModFile(string path)
{
// try exact match
FileInfo file = new FileInfo(Path.Combine(this.FullRootDirectory, path));
// try with default extension
if (!file.Exists && file.Extension.ToLower() != ".xnb")
{
FileInfo result = new FileInfo(file.FullName + ".xnb");
if (result.Exists)
file = result;
}
return file;
}
/// <summary>Get a file from the game's content folder.</summary>
/// <param name="key">The asset key.</param>
private FileInfo GetContentFolderFile(string key)
{
// get file path
string path = Path.Combine(this.FullRootDirectory, key);
if (!path.EndsWith(".xnb"))
path += ".xnb";
// get file
return new FileInfo(path);
}
/// <summary>Load the initial asset from the registered <see cref="Loaders"/>.</summary>
/// <param name="info">The basic asset metadata.</param>
/// <returns>Returns the loaded asset metadata, or <c>null</c> if no loader matched.</returns>
@ -510,25 +680,123 @@ namespace StardewModdingAPI.Framework
{
foreach (var entry in entries)
{
IModMetadata metadata = entry.Key;
IModMetadata mod = entry.Key;
IList<T> interceptors = entry.Value;
// special case if mod is an interceptor
if (metadata.Mod is T modAsInterceptor)
yield return new KeyValuePair<IModMetadata, T>(metadata, modAsInterceptor);
// registered editors
foreach (T interceptor in interceptors)
yield return new KeyValuePair<IModMetadata, T>(metadata, interceptor);
yield return new KeyValuePair<IModMetadata, T>(mod, interceptor);
}
}
/// <summary>Dispose held resources.</summary>
/// <param name="disposing">Whether the content manager is disposing (rather than finalising).</param>
protected override void Dispose(bool disposing)
/// <summary>Premultiply a texture's alpha values to avoid transparency issues in the game. This is only possible if the game isn't currently drawing.</summary>
/// <param name="texture">The texture to premultiply.</param>
/// <returns>Returns a premultiplied texture.</returns>
/// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks>
private Texture2D PremultiplyTransparency(Texture2D texture)
{
this.Monitor.Log("Disposing SMAPI's main content manager. It will no longer be usable after this point.", LogLevel.Trace);
base.Dispose(disposing);
// validate
if (Context.IsInDrawLoop)
throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop.");
// process texture
SpriteBatch spriteBatch = Game1.spriteBatch;
GraphicsDevice gpu = Game1.graphics.GraphicsDevice;
using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height))
{
// create blank render target to premultiply
gpu.SetRenderTarget(renderTarget);
gpu.Clear(Color.Black);
// multiply each color by the source alpha, and write just the color values into the final texture
spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState
{
ColorDestinationBlend = Blend.Zero,
ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue,
AlphaDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.SourceAlpha,
ColorSourceBlend = Blend.SourceAlpha
});
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// copy the alpha values from the source texture into the final one without multiplying them
spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState
{
ColorWriteChannels = ColorWriteChannels.Alpha,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.One,
ColorSourceBlend = Blend.One
});
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// release GPU
gpu.SetRenderTarget(null);
// extract premultiplied data
Color[] data = new Color[texture.Width * texture.Height];
renderTarget.GetData(data);
// unset texture from GPU to regain control
gpu.Textures[0] = null;
// update texture with premultiplied data
texture.SetData(data);
}
return texture;
}
/****
** Concurrency logic
****/
/// <summary>Acquire a read lock which prevents concurrent writes to the cache while it's open.</summary>
/// <typeparam name="T">The action's return value.</typeparam>
/// <param name="action">The action to perform.</param>
private T WithReadLock<T>(Func<T> action)
{
try
{
this.Lock.EnterReadLock();
return action();
}
finally
{
this.Lock.ExitReadLock();
}
}
/// <summary>Acquire a write lock which prevents concurrent reads or writes to the cache while it's open.</summary>
/// <param name="action">The action to perform.</param>
private void WithWriteLock(Action action)
{
try
{
this.Lock.EnterWriteLock();
action();
}
finally
{
this.Lock.ExitWriteLock();
}
}
/// <summary>Acquire a write lock which prevents concurrent reads or writes to the cache while it's open.</summary>
/// <typeparam name="T">The action's return value.</typeparam>
/// <param name="action">The action to perform.</param>
private T WithWriteLock<T>(Func<T> action)
{
try
{
this.Lock.EnterReadLock();
return action();
}
finally
{
this.Lock.ExitReadLock();
}
}
}
}

View File

@ -12,7 +12,6 @@ using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Events;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.Utilities;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.BellsAndWhistles;
using StardewValley.Locations;
@ -180,7 +179,7 @@ namespace StardewModdingAPI.Framework
// override content manager
this.Monitor?.Log("Overriding content manager...", LogLevel.Trace);
this.SContentManager = new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, null, this.Monitor);
this.SContentManager = new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, Thread.CurrentThread.CurrentUICulture, null, this.Monitor, reflection);
this.Content = new ContentManagerShim(this.SContentManager, "SGame.Content");
Game1.content = new ContentManagerShim(this.SContentManager, "Game1.content");
reflection.GetPrivateField<LocalizedContentManager>(typeof(Game1), "_temporaryContent").SetValue(new ContentManagerShim(this.SContentManager, "Game1._temporaryContent")); // regenerate value with new content manager
@ -241,6 +240,9 @@ namespace StardewModdingAPI.Framework
return;
}
/*********
** Save events + suppress events during save
*********/
// While the game is writing to the save file in the background, mods can unexpectedly
// fail since they don't have exclusive access to resources (e.g. collection changed
// during enumeration errors). To avoid problems, events are not invoked while a save
@ -249,7 +251,7 @@ namespace StardewModdingAPI.Framework
if (Context.IsSaving)
{
// raise before-save
if (!this.IsBetweenSaveEvents)
if (Context.IsWorldReady && !this.IsBetweenSaveEvents)
{
this.IsBetweenSaveEvents = true;
this.Monitor.Log("Context: before save.", LogLevel.Trace);
@ -371,7 +373,8 @@ namespace StardewModdingAPI.Framework
SButton[] previousPressedKeys = this.PreviousPressedButtons;
SButton[] framePressedKeys = currentlyPressedKeys.Except(previousPressedKeys).ToArray();
SButton[] frameReleasedKeys = previousPressedKeys.Except(currentlyPressedKeys).ToArray();
bool isClick = framePressedKeys.Contains(SButton.MouseLeft) || (framePressedKeys.Contains(SButton.ControllerA) && !currentlyPressedKeys.Contains(SButton.ControllerX));
bool isUseToolButton = Game1.options.useToolButton.Any(p => framePressedKeys.Contains(p.ToSButton()));
bool isActionButton = !isUseToolButton && Game1.options.actionButton.Any(p => framePressedKeys.Contains(p.ToSButton()));
// get cursor position
ICursorPosition cursor;
@ -388,7 +391,7 @@ namespace StardewModdingAPI.Framework
// raise button pressed
foreach (SButton button in framePressedKeys)
{
InputEvents.InvokeButtonPressed(this.Monitor, button, cursor, isClick);
InputEvents.InvokeButtonPressed(this.Monitor, button, cursor, isActionButton, isUseToolButton);
// legacy events
if (button.TryGetKeyboard(out Keys key))
@ -408,10 +411,9 @@ namespace StardewModdingAPI.Framework
// raise button released
foreach (SButton button in frameReleasedKeys)
{
bool wasClick =
(button == SButton.MouseLeft && previousPressedKeys.Contains(SButton.MouseLeft)) // released left click
|| (button == SButton.ControllerA && previousPressedKeys.Contains(SButton.ControllerA) && !previousPressedKeys.Contains(SButton.ControllerX));
InputEvents.InvokeButtonReleased(this.Monitor, button, cursor, wasClick);
bool wasUseToolButton = (from opt in Game1.options.useToolButton let optButton = opt.ToSButton() where optButton == button && framePressedKeys.Contains(optButton) select optButton).Any();
bool wasActionButton = !wasUseToolButton && (from opt in Game1.options.actionButton let optButton = opt.ToSButton() where optButton == button && framePressedKeys.Contains(optButton) select optButton).Any();
InputEvents.InvokeButtonReleased(this.Monitor, button, cursor, wasActionButton, wasUseToolButton);
// legacy events
if (button.TryGetKeyboard(out Keys key))

View File

@ -1,9 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Input;
using Newtonsoft.Json;
using StardewModdingAPI.Utilities;
namespace StardewModdingAPI.Framework.Serialisation
{
@ -20,7 +19,9 @@ namespace StardewModdingAPI.Framework.Serialisation
ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection<T> values are duplicated each time the config is loaded
Converters = new List<JsonConverter>
{
new SelectiveStringEnumConverter(typeof(Buttons), typeof(Keys), typeof(SButton))
new StringEnumConverter<Buttons>(),
new StringEnumConverter<Keys>(),
new StringEnumConverter<SButton>()
}
};

View File

@ -1,37 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Converters;
namespace StardewModdingAPI.Framework.Serialisation
{
/// <summary>A variant of <see cref="StringEnumConverter"/> which only converts certain enums.</summary>
internal class SelectiveStringEnumConverter : StringEnumConverter
{
/*********
** Properties
*********/
/// <summary>The enum type names to convert.</summary>
private readonly HashSet<string> Types;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="types">The enum types to convert.</param>
public SelectiveStringEnumConverter(params Type[] types)
{
this.Types = new HashSet<string>(types.Select(p => p.FullName));
}
/// <summary>Get whether this instance can convert the specified object type.</summary>
/// <param name="type">The object type.</param>
public override bool CanConvert(Type type)
{
return
base.CanConvert(type)
&& this.Types.Contains((Nullable.GetUnderlyingType(type) ?? type).FullName);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using Newtonsoft.Json.Converters;
namespace StardewModdingAPI.Framework.Serialisation
{
/// <summary>A variant of <see cref="StringEnumConverter"/> which only converts a specified enum.</summary>
/// <typeparam name="T">The enum type.</typeparam>
internal class StringEnumConverter<T> : StringEnumConverter
{
/*********
** Public methods
*********/
/// <summary>Get whether this instance can convert the specified object type.</summary>
/// <param name="type">The object type.</param>
public override bool CanConvert(Type type)
{
return
base.CanConvert(type)
&& (Nullable.GetUnderlyingType(type) ?? type) == typeof(T);
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using xTile;
namespace StardewModdingAPI
{
@ -29,7 +30,7 @@ namespace StardewModdingAPI
** Public methods
*********/
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Texture2D"/> and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Map"/>, <see cref="Texture2D"/>, and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content file relative to the mod folder.</param>
/// <param name="source">Where to search for a matching content asset.</param>
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
@ -52,5 +53,10 @@ namespace StardewModdingAPI
/// <typeparam name="T">The asset type to remove from the cache.</typeparam>
/// <returns>Returns whether any assets were invalidated.</returns>
bool InvalidateCache<T>();
/// <summary>Remove matching assets from the content cache so they're reloaded on the next request. This will reload core game assets if needed, but references to the former asset will still show the previous content.</summary>
/// <param name="predicate">A predicate matching the assets to invalidate.</param>
/// <returns>Returns whether any cache entries were invalidated.</returns>
bool InvalidateCache(Func<IAssetInfo, bool> predicate);
}
}

View File

@ -3,9 +3,11 @@ using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading;
#if SMAPI_FOR_WINDOWS
using System.Management;
@ -77,6 +79,13 @@ namespace StardewModdingAPI
/// <summary>Whether the program has been disposed.</summary>
private bool IsDisposed;
/// <summary>Regex patterns which match console messages to suppress from the console and log.</summary>
private readonly Regex[] SuppressConsolePatterns =
{
new Regex(@"^TextBox\.Selected is now '(?:True|False)'\.$", RegexOptions.Compiled | RegexOptions.CultureInvariant),
new Regex(@"^(?:FRUIT )?TREE: IsClient:(?:True|False) randomOutput: \d+$", RegexOptions.Compiled | RegexOptions.CultureInvariant)
};
/*********
** Public methods
@ -510,8 +519,11 @@ namespace StardewModdingAPI
}
catch (Exception ex)
{
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.", LogLevel.Warn);
this.Monitor.Log($"Error: {ex.GetLogSummary()}");
this.Monitor.Log("Couldn't check for a new version of SMAPI. This won't affect your game, but you won't be notified of new versions if this keeps happening.", LogLevel.Warn);
this.Monitor.Log(ex is WebException && ex.InnerException == null
? $"Error: {ex.Message}"
: $"Error: {ex.GetLogSummary()}"
);
}
// check mod versions
@ -598,7 +610,11 @@ namespace StardewModdingAPI
}
catch (Exception ex)
{
this.Monitor.Log($"Couldn't check for new mod versions:\n{ex.GetLogSummary()}", LogLevel.Trace);
this.Monitor.Log("Couldn't check for new mod versions. This won't affect your game, but you won't be notified of mod updates if this keeps happening.", LogLevel.Warn);
this.Monitor.Log(ex is WebException && ex.InnerException == null
? ex.Message
: ex.ToString()
);
}
}).Start();
}
@ -910,7 +926,14 @@ namespace StardewModdingAPI
/// <param name="message">The message to log.</param>
private void HandleConsoleMessage(IMonitor monitor, string message)
{
LogLevel level = message.Contains("Exception") ? LogLevel.Error : LogLevel.Trace; // intercept potential exceptions
// detect exception
LogLevel level = message.Contains("Exception") ? LogLevel.Error : LogLevel.Trace;
// ignore suppressed message
if (level != LogLevel.Error && this.SuppressConsolePatterns.Any(p => p.IsMatch(message)))
return;
// forward to monitor
monitor.Log(message, level);
}

View File

@ -615,6 +615,18 @@ namespace StardewModdingAPI
return (SButton)(SButtonExtensions.ControllerOffset + key);
}
/// <summary>Get the <see cref="SButton"/> equivalent for the given button.</summary>
/// <param name="input">The Stardew Valley button to convert.</param>
internal static SButton ToSButton(this InputButton input)
{
// derived from InputButton constructors
if (input.mouseLeft)
return SButton.MouseLeft;
if (input.mouseRight)
return SButton.MouseRight;
return input.key.ToSButton();
}
/// <summary>Get the <see cref="Keys"/> equivalent for the given button.</summary>
/// <param name="input">The button to convert.</param>
/// <param name="key">The keyboard equivalent.</param>

View File

@ -49,6 +49,12 @@ namespace StardewModdingAPI
public SemanticVersion(string version)
: this(new SemanticVersionImpl(version)) { }
/// <summary>Construct an instance.</summary>
/// <param name="version">The assembly version.</param>
/// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>
public SemanticVersion(Version version)
: this(new SemanticVersionImpl(version)) { }
/// <summary>Get an integer indicating whether this version precedes (less than 0), supercedes (more than 0), or is equivalent to (0) the specified version.</summary>
/// <param name="other">The version to compare with this instance.</param>
/// <exception cref="ArgumentNullException">The <paramref name="other"/> value is null.</exception>

View File

@ -89,6 +89,7 @@
<Compile Include="..\..\build\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Framework\Content\ContentCache.cs" />
<Compile Include="Framework\Models\ModCompatibility.cs" />
<Compile Include="Framework\ModLoading\Finders\EventFinder.cs" />
<Compile Include="Framework\ModLoading\Finders\FieldFinder.cs" />
@ -173,7 +174,7 @@
<Compile Include="Framework\SContentManager.cs" />
<Compile Include="Framework\Exceptions\SParseException.cs" />
<Compile Include="Framework\Serialisation\JsonHelper.cs" />
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\StringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\SFieldConverter.cs" />
<Compile Include="IAssetEditor.cs" />
<Compile Include="IAssetInfo.cs" />

View File

@ -1,6 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("TrainerMod")]
[assembly: AssemblyDescription("")]
[assembly: Guid("76791e28-b1b5-407c-82d6-50c3e5b7e037")]