diff --git a/.editorconfig b/.editorconfig index d600d602..2aeaeadd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -22,6 +22,9 @@ insert_final_newline = false [README.txt] end_of_line=crlf +[*.{command,sh}] +end_of_line=lf + ########## ## C# formatting ## documentation: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference diff --git a/.gitattributes b/.gitattributes index 1161a204..00ae145b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,6 @@ # normalize line endings * text=auto -README.txt text=crlf +README.txt text eol=crlf + +*.command text eol=lf +*.sh text eol=lf diff --git a/.gitignore b/.gitignore index ed3052fd..89d611a4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ [Oo]bj/ # Visual Studio cache/options +.config/ .vs/ # ReSharper @@ -32,6 +33,10 @@ appsettings.Development.json # Azure generated files src/SMAPI.Web/Properties/PublishProfiles/*.pubxml +src/SMAPI.Web/Properties/ServiceDependencies/* - Web Deploy/ + +# macOS +.DS_Store # Loader Game Asserts src/Loader/Assets/Content/ diff --git a/build/0Harmony.dll b/build/0Harmony.dll index 2e893d0e..72ca2b61 100644 Binary files a/build/0Harmony.dll and b/build/0Harmony.dll differ diff --git a/build/0Harmony.xml b/build/0Harmony.xml new file mode 100644 index 00000000..f1b9b4cf --- /dev/null +++ b/build/0Harmony.xml @@ -0,0 +1,3693 @@ + + + + 0Harmony + + + + A factory to create delegate types + + + Default constructor + + + Creates a delegate type for a method + The method + The new delegate type + + + + A getter delegate type + Type that getter gets field/property value from + Type of the value that getter gets + The instance get getter uses + An delegate + + + + A setter delegate type + Type that setter sets field/property value for + Type of the value that setter sets + The instance the setter uses + The value the setter uses + An delegate + + + + A constructor delegate type + Type that constructor creates + An delegate + + + + A helper class for fast access to getters and setters + + + Creates an instantiation delegate + Type that constructor creates + The new instantiation delegate + + + + Creates an getter delegate for a property + Type that getter reads property from + Type of the property that gets accessed + The property + The new getter delegate + + + + Creates an getter delegate for a field + Type that getter reads field from + Type of the field that gets accessed + The field + The new getter delegate + + + + Creates an getter delegate for a field (with a list of possible field names) + Type that getter reads field/property from + Type of the field/property that gets accessed + A list of possible field names + The new getter delegate + + + + Creates an setter delegate + Type that setter assigns property value to + Type of the property that gets assigned + The property + The new setter delegate + + + + Creates an setter delegate for a field + Type that setter assigns field value to + Type of the field that gets assigned + The field + The new getter delegate + + + + A delegate to invoke a method + The instance + The method parameters + The method result + + + A helper class to invoke method with delegates + + + Creates a fast invocation handler from a method + The method to invoke + Controls if boxed value object is accessed/updated directly + The + + + The directBoxValueAccess option controls how value types passed by reference (e.g. ref int, out my_struct) are handled in the arguments array + passed to the fast invocation handler. + Since the arguments array is an object array, any value types contained within it are actually references to a boxed value object. + Like any other object, there can be other references to such boxed value objects, other than the reference within the arguments array. + For example, + + var val = 5; + var box = (object)val; + var arr = new object[] { box }; + handler(arr); // for a method with parameter signature: ref/out/in int + + + + + If directBoxValueAccess is true, the boxed value object is accessed (and potentially updated) directly when the handler is called, + such that all references to the boxed object reflect the potentially updated value. + In the above example, if the method associated with the handler updates the passed (boxed) value to 10, both box and arr[0] + now reflect the value 10. Note that the original val is not updated, since boxing always copies the value into the new boxed value object. + + + If directBoxValueAccess is false (default), the boxed value object in the arguments array is replaced with a "reboxed" value object, + such that potential updates to the value are reflected only in the arguments array. + In the above example, if the method associated with the handler updates the passed (boxed) value to 10, only arr[0] now reflects the value 10. + + + + + A low level memory helper + + + + Mark method for no inlining (currently only works on Mono) + The method/constructor to change + + + + Detours a method + The original method/constructor + The replacement method/constructor + An error string + + + + Writes a jump to memory + The memory address + Jump destination + An error string + + + + Gets the start of a method in memory + The method/constructor + [out] Details of the exception + The method start address + + + + special parameter names that can be used in prefix and postfix methods + + + Patch function helpers + + + Sorts patch methods by their priority rules + The original method + Patches to sort + Use debug mode + The sorted patch methods + + + + Creates new replacement method with the latest patches and detours the original method + The original method + Information describing the patches + The newly created replacement method + + + + Creates a patch sorter + Array of patches that will be sorted + Use debugging + + + Sorts internal PatchSortingWrapper collection and caches the results. + After first run the result is provided from the cache. + The original method + The sorted patch methods + + + Checks if the sorter was created with the same patch list and as a result can be reused to + get the sorted order of the patches. + List of patches to check against + true if equal + + + Removes one unresolved dependency from the least important patch. + + + Outputs all unblocked patches from the waiting list to results list + + + Adds patch to both results list and handled patches set + Patch to add + + + Wrapper used over the Patch object to allow faster dependency access and + dependency removal in case of cyclic dependencies + + + Create patch wrapper object used for sorting + Patch to wrap + + + Determines how patches sort + The other patch + integer to define sort order (-1, 0, 1) + + + Determines whether patches are equal + The other patch + true if equal + + + Hash function + A hash code + + + Bidirectionally registers Patches as after dependencies + List of dependencies to register + + + Bidirectionally registers Patches as before dependencies + List of dependencies to register + + + Bidirectionally removes Patch from after dependencies + Patch to remove + + + Bidirectionally removes Patch from before dependencies + Patch to remove + + + Specifies the type of method + + + + This is a normal method + + + This is a getter + + + This is a setter + + + This is a constructor + + + This is a static constructor + + + This targets the MoveNext method of the enumerator result + + + Specifies the type of argument + + + + This is a normal argument + + + This is a reference argument (ref) + + + This is an out argument (out) + + + This is a pointer argument (&) + + + Specifies the type of patch + + + + Any patch + + + A prefix patch + + + A postfix patch + + + A transpiler + + + A finalizer + + + A reverse patch + + + Specifies the type of reverse patch + + + + Use the unmodified original method (directly from IL) + + + Use the original as it is right now including previous patches but excluding future ones + + + Specifies the type of method call dispatching mechanics + + + + Call the method using dynamic dispatching if method is virtual (including overriden) + + + This is the built-in form of late binding (a.k.a. dynamic binding) and is the default dispatching mechanic in C#. + This directly corresponds with the instruction. + + + For virtual (including overriden) methods, the instance type's most-derived/overriden implementation of the method is called. + For non-virtual (including static) methods, same behavior as : the exact specified method implementation is called. + + + Note: This is not a fully dynamic dispatch, since non-virtual (including static) methods are still called non-virtually. + A fully dynamic dispatch in C# involves using + the dynamic type + (actually a fully dynamic binding, since even the name and overload resolution happens at runtime), which does not support. + + + + + Call the method using static dispatching, regardless of whether method is virtual (including overriden) or non-virtual (including static) + + + a.k.a. non-virtual dispatching, early binding, or static binding. + This directly corresponds with the instruction. + + + For both virtual (including overriden) and non-virtual (including static) methods, the exact specified method implementation is called, without virtual/override mechanics. + + + + + The base class for all Harmony annotations (not meant to be used directly) + + + + The common information for all attributes + + + Annotation to define your Harmony patch methods + + + + An empty annotation can be used together with TargetMethod(s) + + + + An annotation that specifies a class to patch + The declaring class/type + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The argument types of the method or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + An array of argument types to target overloads + Array of + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + An array of argument types to target overloads + Array of + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + The + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + An array of argument types to target overloads + An array of + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + The + + + + An annotation that specifies a method, property or constructor to patch + The + + + + An annotation that specifies a method, property or constructor to patch + The + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The + An array of argument types to target overloads + An array of + + + + An annotation that specifies a method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + An array of argument types to target overloads + An array of + + + + An annotation that specifies a method, property or constructor to patch + The full name of the declaring class/type + The name of the method, property or constructor to patch + The + + + + Annotation to define the original method for delegate injection + + + + An annotation that specifies a class to patch + The declaring class/type + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The argument types of the method or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + An array of argument types to target overloads + Array of + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The + An array of argument types to target overloads + Array of + + + + An annotation that specifies a method, property or constructor to patch + The declaring class/type + The name of the method, property or constructor to patch + The + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + An array of argument types to target overloads + An array of + + + + An annotation that specifies a method, property or constructor to patch + The name of the method, property or constructor to patch + The + + + + An annotation that specifies call dispatching mechanics for the delegate + The + + + + An annotation that specifies a method, property or constructor to patch + The + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + The + An array of argument types to target overloads + An array of + + + + An annotation that specifies a method, property or constructor to patch + An array of argument types to target overloads + + + + An annotation that specifies a method, property or constructor to patch + An array of argument types to target overloads + An array of + + + + Annotation to define your standin methods for reverse patching + + + + An annotation that specifies the type of reverse patching + The of the reverse patch + + + + A Harmony annotation to define that all methods in a class are to be patched + + + + A Harmony annotation + + + + A Harmony annotation to define patch priority + The priority + + + + A Harmony annotation + + + + A Harmony annotation to define that a patch comes before another patch + The array of harmony IDs of the other patches + + + + A Harmony annotation + + + A Harmony annotation to define that a patch comes after another patch + The array of harmony IDs of the other patches + + + + A Harmony annotation + + + A Harmony annotation to debug a patch (output uses to log to your Desktop) + + + + Specifies the Prepare function in a patch class + + + + Specifies the Cleanup function in a patch class + + + + Specifies the TargetMethod function in a patch class + + + + Specifies the TargetMethods function in a patch class + + + + Specifies the Prefix function in a patch class + + + + Specifies the Postfix function in a patch class + + + + Specifies the Transpiler function in a patch class + + + + Specifies the Finalizer function in a patch class + + + + A Harmony annotation + + + + The name of the original argument + + + + The index of the original argument + + + + The new name of the original argument + + + + An annotation to declare injected arguments by name + + + + An annotation to declare injected arguments by index + Zero-based index + + + + An annotation to declare injected arguments by renaming them + Name of the original argument + New name + + + + An annotation to declare injected arguments by index and renaming them + Zero-based index + New name + + + + An abstract wrapper around OpCode and their operands. Used by transpilers + + + + The opcode + + + + The operand + + + + All labels defined on this instruction + + + + All exception block boundaries defined on this instruction + + + + Creates a new CodeInstruction with a given opcode and optional operand + The opcode + The operand + + + + Create a full copy (including labels and exception blocks) of a CodeInstruction + The to copy + + + + Clones a CodeInstruction and resets its labels and exception blocks + A lightweight copy of this code instruction + + + + Clones a CodeInstruction, resets labels and exception blocks and sets its opcode + The opcode + A copy of this CodeInstruction with a new opcode + + + + Clones a CodeInstruction, resets labels and exception blocks and sets its operand + The operand + A copy of this CodeInstruction with a new operand + + + + Creates a CodeInstruction calling a method (CALL) + The class/type where the method is declared + The name of the method (case sensitive) + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A code instruction that calls the method matching the arguments + + + + Creates a CodeInstruction calling a method (CALL) + The target method in the form TypeFullName:MethodName, where the type name matches a form recognized by Type.GetType like Some.Namespace.Type. + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A code instruction that calls the method matching the arguments + + + + Creates a CodeInstruction calling a method (CALL) + The lambda expression using the method + + + + + Creates a CodeInstruction calling a method (CALL) + The lambda expression using the method + + + + + Creates a CodeInstruction calling a method (CALL) + The lambda expression using the method + + + + + Creates a CodeInstruction calling a method (CALL) + The lambda expression using the method + + + + + Returns an instruction to call the specified closure + The delegate type to emit + The closure that defines the method to call + A that calls the closure as a method + + + + Creates a CodeInstruction loading a field (LD[S]FLD[A]) + The class/type where the field is defined + The name of the field (case sensitive) + Use address of field + + + + Creates a CodeInstruction storing to a field (ST[S]FLD) + The class/type where the field is defined + The name of the field (case sensitive) + + + + Returns a string representation of the code instruction + A string representation of the code instruction + + + + Exception block types + + + + The beginning of an exception block + + + + The beginning of a catch block + + + + The beginning of an except filter block (currently not supported to use in a patch) + + + + The beginning of a fault block + + + + The beginning of a finally block + + + + The end of an exception block + + + + An exception block + + + + Block type + + + + Catch type + + + + Creates an exception block + The + The catch type + + + + The Harmony instance is the main entry to Harmony. After creating one with an unique identifier, it is used to patch and query the current application domain + + + + The unique identifier + + + + Set to true before instantiating Harmony to debug Harmony or use an environment variable to set HARMONY_DEBUG to '1' like this: cmd /C "set HARMONY_DEBUG=1 && game.exe" + This is for full debugging. To debug only specific patches, use the attribute + + + + Creates a new Harmony instance + A unique identifier (you choose your own) + A Harmony instance + + + + Searches the current assembly for Harmony annotations and uses them to create patches + This method can fail to use the correct assembly when being inlined. It calls StackTrace.GetFrame(1) which can point to the wrong method/assembly. If you are unsure or run into problems, use PatchAll(Assembly.GetExecutingAssembly()) instead. + + + + Creates a empty patch processor for an original method + The original method/constructor + A new instance + + + + Creates a patch class processor from an annotated class + The class/type + A new instance + + + + Creates a reverse patcher for one of your stub methods + The original method/constructor + The stand-in stub method as + A new instance + + + + Searches an assembly for Harmony annotations and uses them to create patches + The assembly + + + + Creates patches by manually specifying the methods + The original method/constructor + An optional prefix method wrapped in a object + An optional postfix method wrapped in a object + An optional transpiler method wrapped in a object + An optional finalizer method wrapped in a object + The replacement method that was created to patch the original method + + + + Patches a foreign method onto a stub method of yours and optionally applies transpilers during the process + The original method/constructor you want to duplicate + Your stub method as that will become the original. Needs to have the correct signature (either original or whatever your transpilers generates) + An optional transpiler as method that will be applied during the process + The replacement method that was created to patch the stub method + + + + Unpatches methods by patching them with zero patches. Fully unpatching is not supported. Be careful, unpatching is global + The optional Harmony ID to restrict unpatching to a specific Harmony instance + This method could be static if it wasn't for the fact that unpatching creates a new replacement method that contains your harmony ID + + + + Unpatches a method by patching it with zero patches. Fully unpatching is not supported. Be careful, unpatching is global + The original method/constructor + The + The optional Harmony ID to restrict unpatching to a specific Harmony instance + + + + Unpatches a method by patching it with zero patches. Fully unpatching is not supported. Be careful, unpatching is global + The original method/constructor + The patch method as method to remove + + + + Test for patches from a specific Harmony ID + The Harmony ID + True if patches for this ID exist + + + + Gets patch information for a given original method + The original method/constructor + The patch information as + + + + Gets the methods this instance has patched + An enumeration of original methods/constructors + + + + Gets all patched original methods in the appdomain + An enumeration of patched original methods/constructors + + + + Gets the original method from a given replacement method + A replacement method, for example from a stacktrace + The original method/constructor or null if not found + + + + Tries to get the method from a stackframe including dynamic replacement methods + The + For normal frames, frame.GetMethod() is returned. For frames containing patched methods, the replacement method is returned or null if no method can be found + + + + Gets the original method from the stackframe and uses original if method is a dynamic replacement + The + The original method from that stackframe + + + Gets Harmony version for all active Harmony instances + [out] The current Harmony version + A dictionary containing assembly versions keyed by Harmony IDs + + + + Under Mono, HarmonyException wraps IL compile errors with detailed information about the failure + + + + Default serialization constructor (not implemented) + The info + The context + + + + Get a list of IL instructions in pairs of offset+code + A list of key/value pairs which represent an offset and the code at that offset + + + + Get a list of IL instructions without offsets + A list of + + + + Get the error offset of the errornous IL instruction + The offset + + + + Get the index of the errornous IL instruction + The index into the list of instructions or -1 if not found + + + + A wrapper around a method to use it as a patch (for example a Prefix) + + + + The original method + + + + Class/type declaring this patch + + + + Patch method name + + + + Optional patch + + + + Array of argument types of the patch method + + + + of the patch + + + + Install this patch before patches with these Harmony IDs + + + + Install this patch after patches with these Harmony IDs + + + + Reverse patch type, see + + + + Create debug output for this patch + + + + Whether to use (true) or (false) mechanics + for -attributed delegate + + + + Default constructor + + + + Creates a patch from a given method + The original method + + + + Creates a patch from a given method + The original method + The patch + A list of harmony IDs that should come after this patch + A list of harmony IDs that should come before this patch + Set to true to generate debug output + + + + Creates a patch from a given method + The patch class/type + The patch method name + The optional argument types of the patch method (for overloaded methods) + + + + Gets the names of all internal patch info fields + A list of field names + + + + Merges annotations + The list of to merge + The merged + + + + Returns a string that represents the annotation + A string representation + + + + Annotation extensions + + + + Copies annotation information + The source + The destination + + + + Clones an annotation + The to clone + A copied + + + + Merges annotations + The master + The detail + A new, merged + + + + Gets all annotations on a class/type + The class/type + A list of all + + + + Gets merged annotations on a class/type + The class/type + The merged + + + + Gets all annotations on a method + The method/constructor + A list of + + + + Gets merged annotations on a method + The method/constructor + The merged + + + + + A mutable representation of an inline signature, similar to Mono.Cecil's CallSite. + Used by the calli instruction, can be used by transpilers + + + + + See + + + + See + + + + See + + + + The list of all parameter types or function pointer signatures received by the call site + + + + The return type or function pointer signature returned by the call site + + + + Returns a string representation of the inline signature + A string representation of the inline signature + + + + + A mutable representation of a parameter type with an attached type modifier, + similar to Mono.Cecil's OptionalModifierType / RequiredModifierType and C#'s modopt / modreq + + + + + Whether this is a modopt (optional modifier type) or a modreq (required modifier type) + + + + The modifier type attached to the parameter type + + + + The modified parameter type + + + + Returns a string representation of the modifier type + A string representation of the modifier type + + + + Patch serialization + + + + Control the binding of a serialized object to a type + Specifies the assembly name of the serialized object + Specifies the type name of the serialized object + The type of the object the formatter creates a new instance of + + + + Serializes a patch info + The + The serialized data + + + + Deserialize a patch info + The serialized data + A + + + + Compare function to sort patch priorities + The patch + Zero-based index + The priority + A standard sort integer (-1, 0, 1) + + + + Serializable patch information + + + + Prefixes as an array of + + + + Postfixes as an array of + + + + Transpilers as an array of + + + + Finalizers as an array of + + + + Returns if any of the patches wants debugging turned on + + + + Adds prefixes + An owner (Harmony ID) + The patch methods + + + + Adds a prefix + + + Removes prefixes + The owner of the prefixes, or * for all + + + + Adds postfixes + An owner (Harmony ID) + The patch methods + + + + Adds a postfix + + + Removes postfixes + The owner of the postfixes, or * for all + + + + Adds transpilers + An owner (Harmony ID) + The patch methods + + + + Adds a transpiler + + + Removes transpilers + The owner of the transpilers, or * for all + + + + Adds finalizers + An owner (Harmony ID) + The patch methods + + + + Adds a finalizer + + + Removes finalizers + The owner of the finalizers, or * for all + + + + Removes a patch using its method + The method of the patch to remove + + + + Gets a concatenated list of patches + The Harmony instance ID adding the new patches + The patches to add + The current patches + + + + Gets a list of patches with any from the given owner removed + The owner of the methods, or * for all + The current patches + + + + A serializable patch + + + + Zero-based index + + + + The owner (Harmony ID) + + + + The priority, see + + + + Keep this patch before the patches indicated in the list of Harmony IDs + + + + Keep this patch after the patches indicated in the list of Harmony IDs + + + + A flag that will log the replacement method via every time this patch is used to build the replacement, even in the future + + + + The method of the static patch method + + + + Creates a patch + The method of the patch + Zero-based index + An owner (Harmony ID) + The priority, see + A list of Harmony IDs for patches that should run after this patch + A list of Harmony IDs for patches that should run before this patch + A flag that will log the replacement method via every time this patch is used to build the replacement, even in the future + + + + Creates a patch + The method of the patch + Zero-based index + An owner (Harmony ID) + + + Get the patch method or a DynamicMethod if original patch method is a patch factory + The original method/constructor + The method of the patch + + + + Determines whether patches are equal + The other patch + true if equal + + + + Determines how patches sort + The other patch + integer to define sort order (-1, 0, 1) + + + + Hash function + A hash code + + + + A PatchClassProcessor used to turn on a class/type into patches + + + + Creates a patch class processor by pointing out a class. Similar to PatchAll() but without searching through all classes. + The Harmony instance + The class to process (need to have at least a [HarmonyPatch] attribute) + + + + Applies the patches + A list of all created replacement methods or null if patch class is not annotated + + + + A group of patches + + + + A collection of prefix + + + + A collection of postfix + + + + A collection of transpiler + + + + A collection of finalizer + + + + Gets all owners (Harmony IDs) or all known patches + The patch owners + + + + Creates a group of patches + An array of prefixes as + An array of postfixes as + An array of transpileres as + An array of finalizeres as + + + + A PatchProcessor handles patches on a method/constructor + + + + Creates an empty patch processor + The Harmony instance + The original method/constructor + + + + Adds a prefix + The prefix as a + A for chaining calls + + + + Adds a prefix + The prefix method + A for chaining calls + + + + Adds a postfix + The postfix as a + A for chaining calls + + + + Adds a postfix + The postfix method + A for chaining calls + + + + Adds a transpiler + The transpiler as a + A for chaining calls + + + + Adds a transpiler + The transpiler method + A for chaining calls + + + + Adds a finalizer + The finalizer as a + A for chaining calls + + + + Adds a finalizer + The finalizer method + A for chaining calls + + + + Gets all patched original methods in the appdomain + An enumeration of patched method/constructor + + + + Applies all registered patches + The generated replacement method + + + + Unpatches patches of a given type and/or Harmony ID + The patch type + Harmony ID or * for any + A for chaining calls + + + + Unpatches a specific patch + The method of the patch + A for chaining calls + + + + Gets patch information on an original + The original method/constructor + The patch information as + + + + Sort patch methods by their priority rules + The original method + Patches to sort + The sorted patch methods + + + + Gets Harmony version for all active Harmony instances + [out] The current Harmony version + A dictionary containing assembly version keyed by Harmony ID + + + + Creates a new empty generator to use when reading method bodies + A new + + + + Creates a new generator matching the method/constructor to use when reading method bodies + The original method/constructor to copy method information from + A new + + + + Returns the methods unmodified list of code instructions + The original method/constructor + Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used) + A list containing all the original + + + + Returns the methods unmodified list of code instructions + The original method/constructor + A new generator that now contains all local variables and labels contained in the result + A list containing all the original + + + + Returns the methods current list of code instructions after all existing transpilers have been applied + The original method/constructor + Apply only the first count of transpilers + Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used) + A list of + + + + Returns the methods current list of code instructions after all existing transpilers have been applied + The original method/constructor + A new generator that now contains all local variables and labels contained in the result + Apply only the first count of transpilers + A list of + + + + A low level way to read the body of a method. Used for quick searching in methods + The original method + All instructions as opcode/operand pairs + + + + A low level way to read the body of a method. Used for quick searching in methods + The original method + An existing generator that will be used to create all local variables and labels contained in the result + All instructions as opcode/operand pairs + + + + A patch priority + + + + Patch last + + + + Patch with very low priority + + + + Patch with low priority + + + + Patch with lower than normal priority + + + + Patch with normal priority + + + + Patch with higher than normal priority + + + + Patch with high priority + + + + Patch with very high priority + + + + Patch first + + + + A reverse patcher + + + + Creates a reverse patcher + The Harmony instance + The original method/constructor + Your stand-in stub method as + + + + Applies the patch + The type of patch, see + The generated replacement method + + + + A collection of commonly used transpilers + + + + A transpiler that replaces all occurrences of a given method with another one using the same signature + The enumeration of to act on + Method or constructor to search for + Method or constructor to replace with + Modified enumeration of + + + + A transpiler that alters instructions that match a predicate by calling an action + The enumeration of to act on + A predicate selecting the instructions to change + An action to apply to matching instructions + Modified enumeration of + + + + A transpiler that logs a text at the beginning of the method + The instructions to act on + The log text + Modified enumeration of + + + + A helper class for reflection related functions + + + + Shortcut for to simplify the use of reflections and make it work for any access level + + + + Shortcut for to simplify the use of reflections and make it work for any access level but only within the current type + + + + Enumerates all assemblies in the current app domain, excluding visual studio assemblies + An enumeration of + + + Gets a type by name. Prefers a full name with namespace but falls back to the first type matching the name otherwise + The name + A type or null if not found + + + + Gets all successfully loaded types from a given assembly + The assembly + An array of types + + This calls and returns , while catching any thrown . + If such an exception is thrown, returns the successfully loaded types (, + filtered for non-null values). + + + + + Enumerates all successfully loaded types in the current app domain, excluding visual studio assemblies + An enumeration of all in all assemblies, excluding visual studio assemblies + + + Applies a function going up the type hierarchy and stops at the first non-null result + Result type of func() + The class/type to start with + The evaluation function returning T + The first non-null result, or null if no match + + The type hierarchy of a class or value type (including struct) does NOT include implemented interfaces, + and the type hierarchy of an interface is only itself (regardless of whether that interface implements other interfaces). + The top-most type in the type hierarchy of all non-interface types (including value types) is . + + + + + Applies a function going into inner types and stops at the first non-null result + Generic type parameter + The class/type to start with + The evaluation function returning T + The first non-null result, or null if no match + + + + Gets the reflection information for a directly declared field + The class/type where the field is defined + The name of the field + A field or null when type/name is null or when the field cannot be found + + + + Gets the reflection information for a directly declared field + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A field or null when the field cannot be found + + + + Gets the reflection information for a field by searching the type and all its super types + The class/type where the field is defined + The name of the field (case sensitive) + A field or null when type/name is null or when the field cannot be found + + + + Gets the reflection information for a field by searching the type and all its super types + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A field or null when the field cannot be found + + + + Gets the reflection information for a field + The class/type where the field is declared + The zero-based index of the field inside the class definition + A field or null when type is null or when the field cannot be found + + + + Gets the reflection information for a directly declared property + The class/type where the property is declared + The name of the property (case sensitive) + A property or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for a directly declared property + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A property or null when the property cannot be found + + + + Gets the reflection information for the getter method of a directly declared property + The class/type where the property is declared + The name of the property (case sensitive) + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for the getter method of a directly declared property + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A method or null when the property cannot be found + + + + Gets the reflection information for the setter method of a directly declared property + The class/type where the property is declared + The name of the property (case sensitive) + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for the Setter method of a directly declared property + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A method or null when the property cannot be found + + + + Gets the reflection information for a property by searching the type and all its super types + The class/type + The name + A property or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for a property by searching the type and all its super types + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A property or null when the property cannot be found + + + + Gets the reflection information for the getter method of a property by searching the type and all its super types + The class/type + The name + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for the getter method of a property by searching the type and all its super types + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for the setter method of a property by searching the type and all its super types + The class/type + The name + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for the setter method of a property by searching the type and all its super types + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A method or null when type/name is null or when the property cannot be found + + + + Gets the reflection information for a directly declared method + The class/type where the method is declared + The name of the method (case sensitive) + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A method or null when type/name is null or when the method cannot be found + + + + Gets the reflection information for a directly declared method + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A method or null when the method cannot be found + + + + Gets the reflection information for a method by searching the type and all its super types + The class/type where the method is declared + The name of the method (case sensitive) + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A method or null when type/name is null or when the method cannot be found + + + + Gets the reflection information for a method by searching the type and all its super types + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + Optional parameters to target a specific overload of the method + Optional list of types that define the generic version of the method + A method or null when the method cannot be found + + + + Gets the method of an enumerator method + Enumerator method that creates the enumerator + The internal method of the enumerator or null if no valid enumerator is detected + + + Gets the names of all method that are declared in a type + The declaring class/type + A list of method names + + + + Gets the names of all method that are declared in the type of the instance + An instance of the type to search in + A list of method names + + + + Gets the names of all fields that are declared in a type + The declaring class/type + A list of field names + + + + Gets the names of all fields that are declared in the type of the instance + An instance of the type to search in + A list of field names + + + + Gets the names of all properties that are declared in a type + The declaring class/type + A list of property names + + + + Gets the names of all properties that are declared in the type of the instance + An instance of the type to search in + A list of property names + + + + Gets the type of any class member of + A member + The class/type of this member + + + + Test if a class member is actually an concrete implementation + A member + True if the member is a declared + + + + Gets the real implementation of a class member + A member + The member itself if its declared. Otherwise the member that is actually implemented in some base type + + + + Gets the reflection information for a directly declared constructor + The class/type where the constructor is declared + Optional parameters to target a specific overload of the constructor + Optional parameters to only consider static constructors + A constructor info or null when type is null or when the constructor cannot be found + + + + Gets the reflection information for a constructor by searching the type and all its super types + The class/type where the constructor is declared + Optional parameters to target a specific overload of the method + Optional parameters to only consider static constructors + A constructor info or null when type is null or when the method cannot be found + + + + Gets reflection information for all declared constructors + The class/type where the constructors are declared + Optional parameters to only consider static constructors + A list of constructor infos + + + + Gets reflection information for all declared methods + The class/type where the methods are declared + A list of methods + + + + Gets reflection information for all declared properties + The class/type where the properties are declared + A list of properties + + + + Gets reflection information for all declared fields + The class/type where the fields are declared + A list of fields + + + + Gets the return type of a method or constructor + The method/constructor + The return type + + + + Given a type, returns the first inner type matching a recursive search by name + The class/type to start searching at + The name of the inner type (case sensitive) + The inner type or null if type/name is null or if a type with that name cannot be found + + + + Given a type, returns the first inner type matching a recursive search with a predicate + The class/type to start searching at + The predicate to search with + The inner type or null if type/predicate is null or if a type with that name cannot be found + + + + Given a type, returns the first method matching a predicate + The class/type to start searching at + The predicate to search with + The method or null if type/predicate is null or if a type with that name cannot be found + + + + Given a type, returns the first constructor matching a predicate + The class/type to start searching at + The predicate to search with + The constructor info or null if type/predicate is null or if a type with that name cannot be found + + + + Given a type, returns the first property matching a predicate + The class/type to start searching at + The predicate to search with + The property or null if type/predicate is null or if a type with that name cannot be found + + + + Returns an array containing the type of each object in the given array + An array of objects + An array of types or an empty array if parameters is null (if an object is null, the type for it will be object) + + + + Creates an array of input parameters for a given method and a given set of potential inputs + The method/constructor you are planing to call + The possible input parameters in any order + An object array matching the method signature + + + + A readable/assignable reference delegate to an instance field of a class or static field (NOT an instance field of a struct) + + An arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including ), + implemented interface, or derived class of this type + + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The runtime instance to access the field (ignored and can be omitted for static fields) + A readable/assignable reference to the field + Null instance passed to a non-static field ref delegate + + Instance of invalid type passed to a non-static field ref delegate + (this can happen if is a parent class or interface of the field's declaring type) + + + + This delegate cannot be used for instance fields of structs, since a struct instance passed to the delegate would be passed by + value and thus would be a copy that only exists within the delegate's invocation. This is fine for a readonly reference, + but makes assignment futile. Use instead. + + + Note that is not required to be the field's declaring type. It can be a parent class (including ), + implemented interface, or a derived class of the field's declaring type ("instanceOfT is FieldDeclaringType" must be possible). + Specifically, must be assignable from OR to the field's declaring type. + Technically, this allows Nullable, although Nullable is only relevant for structs, and since only static fields of structs + are allowed for this delegate, and the instance passed to such a delegate is ignored, this hardly matters. + + + Similarly, is not required to be the field's field type, unless that type is a non-enum value type. + It can be a parent class (including object) or implemented interface of the field's field type. It cannot be a derived class. + This variance is not allowed for value types, since that would require boxing/unboxing, which is not allowed for ref values. + Special case for enum types: can also be the underlying integral type of the enum type. + Specifically, for reference types, must be assignable from + the field's field type; for non-enum value types, must be exactly the field's field type; for enum types, + must be either the field's field type or the underyling integral type of that field type. + + + This delegate supports static fields, even those defined in structs, for legacy reasons. + For such static fields, is effectively ignored. + Consider using (and StaticFieldRefAccess methods that return it) instead for static fields. + + + + + + Creates a field reference delegate for an instance field of a class + The class that defines the instance field, or derived class of this type + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The name of the field + A readable/assignable delegate + + + For backwards compatibility, there is no class constraint on . + Instead, the non-value-type check is done at runtime within the method. + + + + + + Creates an instance field reference for a specific instance of a class + The class that defines the instance field, or derived class of this type + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The instance + The name of the field + A readable/assignable reference to the field + + + This method is meant for one-off access to a field's value for a single instance. + If you need to access a field's value for potentially multiple instances, use instead. + FieldRefAccess<T, F>(instance, fieldName) is functionally equivalent to FieldRefAccess<T, F>(fieldName)(instance). + + + For backwards compatibility, there is no class constraint on . + Instead, the non-value-type check is done at runtime within the method. + + + + + + Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct) + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + + The type that defines the field, or derived class of this type; must not be a struct type unless the field is static + + The name of the field + + A readable/assignable delegate with T=object + (for static fields, the instance delegate parameter is ignored) + + + + This method is meant for cases where the given type is only known at runtime and thus can't be used as a type parameter T + in e.g. . + + + This method supports static fields, even those defined in structs, for legacy reasons. + Consider using (and other overloads) instead for static fields. + + + + + + Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct) + type of the field + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A readable/assignable delegate with T=object + + + Creates a field reference delegate for an instance field of a class or static field (NOT an instance field of a struct) + + An arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including ), + implemented interface, or derived class of this type ("instanceOfT is FieldDeclaringType" must be possible) + + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The field + A readable/assignable delegate + + + This method is meant for cases where the field has already been obtained, avoiding the field searching cost in + e.g. . + + + This method supports static fields, even those defined in structs, for legacy reasons. + For such static fields, is effectively ignored. + Consider using (and other overloads) instead for static fields. + + + For backwards compatibility, there is no class constraint on . + Instead, the non-value-type check is done at runtime within the method. + + + + + + Creates a field reference for an instance field of a class + + The type that defines the field; or a parent class (including ), implemented interface, or derived class of this type + ("instanceOfT is FieldDeclaringType" must be possible) + + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The instance + The field + A readable/assignable reference to the field + + + This method is meant for one-off access to a field's value for a single instance and where the field has already been obtained. + If you need to access a field's value for potentially multiple instances, use instead. + FieldRefAccess<T, F>(instance, fieldInfo) is functionally equivalent to FieldRefAccess<T, F>(fieldInfo)(instance). + + + For backwards compatibility, there is no class constraint on . + Instead, the non-value-type check is done at runtime within the method. + + + + + + A readable/assignable reference delegate to an instance field of a struct + The struct that defines the instance field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + A reference to the runtime instance to access the field + A readable/assignable reference to the field + + + + Creates a field reference delegate for an instance field of a struct + The struct that defines the instance field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The name of the field + A readable/assignable delegate + + + + Creates an instance field reference for a specific instance of a struct + The struct that defines the instance field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The instance + The name of the field + A readable/assignable reference to the field + + + This method is meant for one-off access to a field's value for a single instance. + If you need to access a field's value for potentially multiple instances, use instead. + StructFieldRefAccess<T, F>(ref instance, fieldName) is functionally equivalent to StructFieldRefAccess<T, F>(fieldName)(ref instance). + + + + + + Creates a field reference delegate for an instance field of a struct + The struct that defines the instance field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The field + A readable/assignable delegate + + + This method is meant for cases where the field has already been obtained, avoiding the field searching cost in + e.g. . + + + + + + Creates a field reference for an instance field of a struct + The struct that defines the instance field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The instance + The field + A readable/assignable reference to the field + + + This method is meant for one-off access to a field's value for a single instance and where the field has already been obtained. + If you need to access a field's value for potentially multiple instances, use instead. + StructFieldRefAccess<T, F>(ref instance, fieldInfo) is functionally equivalent to StructFieldRefAccess<T, F>(fieldInfo)(ref instance). + + + + + + A readable/assignable reference delegate to a static field + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + A readable/assignable reference to the field + + + + Creates a static field reference + The type (can be class or struct) the field is defined in + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The name of the field + A readable/assignable reference to the field + + + + Creates a static field reference + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The type (can be class or struct) the field is defined in + The name of the field + A readable/assignable reference to the field + + + + Creates a static field reference + The type of the field + The member in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + A readable/assignable reference to the field + + + + Creates a static field reference + An arbitrary type (by convention, the type the field is defined in) + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The field + A readable/assignable reference to the field + + The type parameter is only used in exception messaging and to distinguish between this method overload + and the overload (which returns a rather than a reference). + + + + + Creates a static field reference delegate + + The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), + a type that is assignable from that type; or if the field's type is an enum type, + either that type or the underlying integral type of that enum type + + The field + A readable/assignable delegate + + + + Creates a delegate to a given method + The delegate Type + The method to create a delegate from. + + Only applies for instance methods. If null (default), returned delegate is an open (a.k.a. unbound) instance delegate + where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) + instance delegate where the delegate invocation always applies to the given . + + + Only applies for instance methods. If true (default) and is virtual, invocation of the delegate + calls the instance method virtually (the instance type's most-derived/overriden implementation of the method is called); + else, invocation of the delegate calls the exact specified (this is useful for calling base class methods) + Note: if false and is an interface method, an ArgumentException is thrown. + + A delegate of given to given + + + Delegate invocation is more performant and more convenient to use than + at a one-time setup cost. + + + Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods, + and both class and struct methods. + + + + + + Creates a delegate to a given method + The delegate Type + The method in the form TypeFullName:MemberName, where TypeFullName matches the form recognized by Type.GetType like Some.Namespace.Type. + + Only applies for instance methods. If null (default), returned delegate is an open (a.k.a. unbound) instance delegate + where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) + instance delegate where the delegate invocation always applies to the given . + + + Only applies for instance methods. If true (default) and is virtual, invocation of the delegate + calls the instance method virtually (the instance type's most-derived/overriden implementation of the method is called); + else, invocation of the delegate calls the exact specified (this is useful for calling base class methods) + Note: if false and is an interface method, an ArgumentException is thrown. + + A delegate of given to given + + + Delegate invocation is more performant and more convenient to use than + at a one-time setup cost. + + + Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods, + and both class and struct methods. + + + + + + Creates a delegate for a given delegate definition, attributed with [] + The delegate Type, attributed with [] + + Only applies for instance methods. If null (default), returned delegate is an open (a.k.a. unbound) instance delegate + where an instance is supplied as the first argument to the delegate invocation; else, delegate is a closed (a.k.a. bound) + instance delegate where the delegate invocation always applies to the given . + + A delegate of given to the method specified via [] + attributes on + + This calls with the method and virtualCall arguments + determined from the [] attributes on , + and the given (for closed instance delegates). + + + + + Returns who called the current method + The calling method/constructor (excluding the caller) + + + + Rethrows an exception while preserving its stack trace (throw statement typically clobbers existing stack traces) + The exception to rethrow + + + + True if the current runtime is based on Mono, false otherwise (.NET) + + + + True if the current runtime is .NET Framework, false otherwise (.NET Core or Mono, although latter isn't guaranteed) + + + + True if the current runtime is .NET Core, false otherwise (Mono or .NET Framework) + + + + Throws a missing member runtime exception + The type that is involved + A list of names + + + + Gets default value for a specific type + The class/type + The default value + + + + Creates an (possibly uninitialized) instance of a given type + The class/type + The new instance + + + + Creates an (possibly uninitialized) instance of a given type + The class/type + The new instance + + + + + A cache for the or similar Add methods for different types. + + + + Makes a deep copy of any object + The type of the instance that should be created; for legacy reasons, this must be a class or interface + The original object + A copy of the original object but of type T + + + + Makes a deep copy of any object + The type of the instance that should be created + The original object + [out] The copy of the original object + Optional value transformation function (taking a field name and src/dst instances) + The optional path root to start with + + + + Makes a deep copy of any object + The original object + The type of the instance that should be created + Optional value transformation function (taking a field name and src/dst instances) + The optional path root to start with + The copy of the original object + + + + Tests if a type is a struct + The type + True if the type is a struct + + + + Tests if a type is a class + The type + True if the type is a class + + + + Tests if a type is a value type + The type + True if the type is a value type + + + + Tests if a type is an integer type + The type + True if the type represents some integer + + + + Tests if a type is a floating point type + The type + True if the type represents some floating point + + + + Tests if a type is a numerical type + The type + True if the type represents some number + + + + Tests if a type is void + The type + True if the type is void + + + + Test whether an instance is of a nullable type + Type of instance + An instance to test + True if instance is of nullable type, false if not + + + + Tests whether a type or member is static, as defined in C# + The type or member + True if the type or member is static + + + + Tests whether a type is static, as defined in C# + The type + True if the type is static + + + + Tests whether a property is static, as defined in C# + The property + True if the property is static + + + + Tests whether an event is static, as defined in C# + The event + True if the event is static + + + + Calculates a combined hash code for an enumeration of objects + The objects + The hash code + + + + A CodeInstruction match + + + The name of the match + + + The matched opcodes + + + The matched operands + + + The jumps from the match + + + The jumps to the match + + + The match predicate + + + Creates a code match + The optional opcode + The optional operand + The optional name + + + + Creates a code match that calls a method + The lambda expression using the method + The optional name + + + + Creates a code match that calls a method + The lambda expression using the method + The optional name + + + + Creates a code match + The CodeInstruction + An optional name + + + + Creates a code match + The predicate + An optional name + + + + Returns a string that represents the match + A string representation + + + + A CodeInstruction matcher + + + The current position + The index or -1 if out of bounds + + + + Gets the number of code instructions in this matcher + The count + + + + Checks whether the position of this CodeMatcher is within bounds + True if this CodeMatcher is valid + + + + Checks whether the position of this CodeMatcher is outside its bounds + True if this CodeMatcher is invalid + + + + Gets the remaining code instructions + The remaining count + + + + Gets the opcode at the current position + The opcode + + + + Gets the operand at the current position + The operand + + + + Gets the labels at the current position + The labels + + + + Gets the exception blocks at the current position + The blocks + + + + Creates an empty code matcher + + + Creates a code matcher from an enumeration of instructions + The instructions (transpiler argument) + An optional IL generator + + + + Makes a clone of this instruction matcher + A copy of this matcher + + + + Gets instructions at the current position + The instruction + + + + Gets instructions at the current position with offset + The offset + The instruction + + + + Gets all instructions + A list of instructions + + + + Gets all instructions as an enumeration + A list of instructions + + + + Gets some instructions counting from current position + Number of instructions + A list of instructions + + + + Gets all instructions within a range + The start index + The end index + A list of instructions + + + + Gets all instructions within a range (relative to current position) + The start offset + The end offset + A list of instructions + + + + Gets a list of all distinct labels + The instructions (transpiler argument) + A list of Labels + + + + Reports a failure + The method involved + The logger + True if current position is invalid and error was logged + + + + Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed) + Explanation of where/why the exception was thrown that will be added to the exception message + The same code matcher + + + + Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), + or if the matches do not match at current position + Explanation of where/why the exception was thrown that will be added to the exception message + Some code matches + The same code matcher + + + + Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), + or if the matches do not match at any point between current position and the end + Explanation of where/why the exception was thrown that will be added to the exception message + Some code matches + The same code matcher + + + + Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), + or if the matches do not match at any point between current position and the start + Explanation of where/why the exception was thrown that will be added to the exception message + Some code matches + The same code matcher + + + + Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), + or if the check function returns false + Explanation of where/why the exception was thrown that will be added to the exception message + Function that checks validity of current state. If it returns false, an exception is thrown + The same code matcher + + + + Sets an instruction at current position + The instruction to set + The same code matcher + + + + Sets instruction at current position and advances + The instruction + The same code matcher + + + + Sets opcode and operand at current position + The opcode + The operand + The same code matcher + + + + Sets opcode and operand at current position and advances + The opcode + The operand + The same code matcher + + + + Sets opcode at current position and advances + The opcode + The same code matcher + + + + Sets operand at current position and advances + The operand + The same code matcher + + + + Creates a label at current position + [out] The label + The same code matcher + + + + Creates a label at a position + The position + [out] The new label + The same code matcher + + + + Creates a label at a position + The offset + [out] The new label + The same code matcher + + + + Adds an enumeration of labels to current position + The labels + The same code matcher + + + + Adds an enumeration of labels at a position + The position + The labels + The same code matcher + + + + Sets jump to + Branch instruction + Destination for the jump + [out] The created label + The same code matcher + + + + Inserts some instructions + The instructions + The same code matcher + + + + Inserts an enumeration of instructions + The instructions + The same code matcher + + + + Inserts a branch + The branch opcode + Branch destination + The same code matcher + + + + Inserts some instructions and advances the position + The instructions + The same code matcher + + + + Inserts an enumeration of instructions and advances the position + The instructions + The same code matcher + + + + Inserts a branch and advances the position + The branch opcode + Branch destination + The same code matcher + + + + Removes current instruction + The same code matcher + + + + Removes some instruction from current position by count + Number of instructions + The same code matcher + + + + Removes the instructions in a range + The start + The end + The same code matcher + + + + Removes the instructions in a offset range + The start offset + The end offset + The same code matcher + + + + Advances the current position + The offset + The same code matcher + + + + Moves the current position to the start + The same code matcher + + + + Moves the current position to the end + The same code matcher + + + + Searches forward with a predicate and advances position + The predicate + The same code matcher + + + + Searches backwards with a predicate and reverses position + The predicate + The same code matcher + + + + Matches forward and advances position to beginning of matching sequence + Some code matches + The same code matcher + + + + Matches forward and advances position to ending of matching sequence + Some code matches + The same code matcher + + + + Matches backwards and reverses position to beginning of matching sequence + Some code matches + The same code matcher + + + + Matches backwards and reverses position to ending of matching sequence + Some code matches + The same code matcher + + + + Repeats a match action until boundaries are met + The match action + An optional action that is executed when no match is found + The same code matcher + + + + Gets a match by its name + The match name + An instruction + + + + General extensions for common cases + + + + Joins an enumeration with a value converter and a delimiter to a string + The inner type of the enumeration + The enumeration + An optional value converter (from T to string) + An optional delimiter + The values joined into a string + + + + Converts an array of types (for example methods arguments) into a human readable form + The array of types + A human readable description including brackets + + + + A full description of a type + The type + A human readable description + + + + A a full description of a method or a constructor without assembly details but with generics + The method/constructor + A human readable description + + + + A helper converting parameter infos to types + The array of parameter infos + An array of types + + + + A helper to access a value via key from a dictionary + The key type + The value type + The dictionary + The key + The value for the key or the default value (of T) if that key does not exist + + + + A helper to access a value via key from a dictionary with extra casting + The value type + The dictionary + The key + The value for the key or the default value (of T) if that key does not exist or cannot be cast to T + + + + Escapes Unicode and ASCII non printable characters + The string to convert + The string to convert + A string literal surrounded by + + + + Extensions for + + + + Returns if an is initialized and valid + The + + + + Shortcut for testing whether the operand is equal to a non-null value + The + The value + True if the operand has the same type and is equal to the value + + + + Shortcut for testing whether the operand is equal to a non-null value + The + The value + True if the operand is equal to the value + This is an optimized version of for + + + + Shortcut for code.opcode == opcode && code.OperandIs(operand) + The + The + The operand value + True if the opcode is equal to the given opcode and the operand has the same type and is equal to the given operand + + + + Shortcut for code.opcode == opcode && code.OperandIs(operand) + The + The + The operand value + True if the opcode is equal to the given opcode and the operand is equal to the given operand + This is an optimized version of for + + + + Tests for any form of Ldarg* + The + The (optional) index + True if it matches one of the variations + + + + Tests for Ldarga/Ldarga_S + The + The (optional) index + True if it matches one of the variations + + + + Tests for Starg/Starg_S + The + The (optional) index + True if it matches one of the variations + + + + Tests for any form of Ldloc* + The + The optional local variable + True if it matches one of the variations + + + + Tests for any form of Stloc* + The + The optional local variable + True if it matches one of the variations + + + + Tests if the code instruction branches + The + The label if the instruction is a branch operation or if not + True if the instruction branches + + + + Tests if the code instruction calls the method/constructor + The + The method + True if the instruction calls the method or constructor + + + + Tests if the code instruction loads a constant + The + True if the instruction loads a constant + + + + Tests if the code instruction loads an integer constant + The + The integer constant + True if the instruction loads the constant + + + + Tests if the code instruction loads a floating point constant + The + The floating point constant + True if the instruction loads the constant + + + + Tests if the code instruction loads an enum constant + The + The enum + True if the instruction loads the constant + + + + Tests if the code instruction loads a string constant + The + The string + True if the instruction loads the constant + + + + Tests if the code instruction loads a field + The + The field + Set to true if the address of the field is loaded + True if the instruction loads the field + + + + Tests if the code instruction stores a field + The + The field + True if the instruction stores this field + + + + Adds labels to the code instruction and return it + The + One or several to add + The same code instruction + + + Adds labels to the code instruction and return it + The + An enumeration of + The same code instruction + + + Extracts all labels from the code instruction and returns them + The + A list of + + + Moves all labels from the code instruction to another one + The to move the labels from + The other to move the labels to + The code instruction labels were moved from (now empty) + + + Moves all labels from another code instruction to the current one + The to move the labels to + The other to move the labels from + The code instruction that received the labels + + + Adds ExceptionBlocks to the code instruction and return it + The + One or several to add + The same code instruction + + + Adds ExceptionBlocks to the code instruction and return it + The + An enumeration of + The same code instruction + + + Extracts all ExceptionBlocks from the code instruction and returns them + The + A list of + + + Moves all ExceptionBlocks from the code instruction to another one + The to move the ExceptionBlocks from + The other to move the ExceptionBlocks to + The code instruction blocks were moved from (now empty) + + + Moves all ExceptionBlocks from another code instruction to the current one + The to move the ExceptionBlocks to + The other to move the ExceptionBlocks from + The code instruction that received the blocks + + + General extensions for collections + + + + A simple way to execute code for every element in a collection + The inner type of the collection + The collection + The action to execute + + + + A simple way to execute code for elements in a collection matching a condition + The inner type of the collection + The collection + The predicate + The action to execute + + + + A helper to add an item to a collection + The inner type of the collection + The collection + The item to add + The collection containing the item + + + + A helper to add an item to an array + The inner type of the collection + The array + The item to add + The array containing the item + + + + A helper to add items to an array + The inner type of the collection + The array + The items to add + The array containing the items + + + + General extensions for collections + + + + Tests a class member if it has an IL method body (external methods for example don't have a body) + The member to test + Returns true if the member has an IL body or false if not + + + A file log for debugging + + + + Set this to make Harmony write its log content to this stream + + + + Full pathname of the log file, defaults to a file called harmony.log.txt on your Desktop + + + + The indent character. The default is tab + + + + The current indent level + + + + Changes the indentation level + The value to add to the indentation level + + + + Log a string in a buffered way. Use this method only if you are sure that FlushBuffer will be called + or else logging information is incomplete in case of a crash + The string to log + + + + Logs a list of string in a buffered way. Use this method only if you are sure that FlushBuffer will be called + or else logging information is incomplete in case of a crash + A list of strings to log (they will not be re-indented) + + + + Returns the log buffer and optionally empties it + True to empty the buffer + The buffer. + + + + Replaces the buffer with new lines + The lines to store + + + + Flushes the log buffer to disk (use in combination with LogBuffered) + + + + Log a string directly to disk. Slower method that prevents missing information in case of a crash + The string to log. + + + + Log a string directly to disk if Harmony.DEBUG is true. Slower method that prevents missing information in case of a crash + The string to log. + + + + Resets and deletes the log + + + + Logs some bytes as hex values + The pointer to some memory + The length of bytes to log + + + + A helper class to retrieve reflection info for non-private methods + + + + Given a lambda expression that calls a method, returns the method info + The lambda expression using the method + The method in the lambda expression + + + + Given a lambda expression that calls a method, returns the method info + The generic type + The lambda expression using the method + The method in the lambda expression + + + + Given a lambda expression that calls a method, returns the method info + The generic type + The generic result type + The lambda expression using the method + The method in the lambda expression + + + + Given a lambda expression that calls a method, returns the method info + The lambda expression using the method + The method in the lambda expression + + + + A reflection helper to read and write private elements + The result type defined by GetValue() + + + + Creates a traverse instance from an existing instance + The existing instance + + + + Gets/Sets the current value + The value to read or write + + + + A reflection helper to read and write private elements + + + + Creates a new traverse instance from a class/type + The class/type + A instance + + + + Creates a new traverse instance from a class T + The class + A instance + + + + Creates a new traverse instance from an instance + The object + A instance + + + + Creates a new traverse instance from a named type + The type name, for format see + A instance + + + + Creates a new and empty traverse instance + + + + Creates a new traverse instance from a class/type + The class/type + + + + Creates a new traverse instance from an instance + The object + + + + Gets the current value + The value + + + + Gets the current value + The type of the value + The value + + + + Invokes the current method with arguments and returns the result + The method arguments + The value returned by the method + + + + Invokes the current method with arguments and returns the result + The type of the value + The method arguments + The value returned by the method + + + + Sets a value of the current field or property + The value + The same traverse instance + + + + Gets the type of the current field or property + The type + + + + Moves the current traverse instance to a inner type + The type name + A traverse instance + + + + Moves the current traverse instance to a field + The type name + A traverse instance + + + + Moves the current traverse instance to a field + The type of the field + The type name + A traverse instance + + + + Gets all fields of the current type + A list of field names + + + + Moves the current traverse instance to a property + The type name + Optional property index + A traverse instance + + + + Moves the current traverse instance to a field + The type of the property + The type name + Optional property index + A traverse instance + + + + Gets all properties of the current type + A list of property names + + + + Moves the current traverse instance to a method + The name of the method + The arguments defining the argument types of the method overload + A traverse instance + + + + Moves the current traverse instance to a method + The name of the method + The argument types of the method + The arguments for the method + A traverse instance + + + + Gets all methods of the current type + A list of method names + + + + Checks if the current traverse instance is for a field + True if its a field + + + + Checks if the current traverse instance is for a property + True if its a property + + + + Checks if the current traverse instance is for a method + True if its a method + + + + Checks if the current traverse instance is for a type + True if its a type + + + + Iterates over all fields of the current type and executes a traverse action + Original object + The action receiving a instance for each field + + + + Iterates over all fields of the current type and executes a traverse action + Original object + Target object + The action receiving a pair of instances for each field pair + + + + Iterates over all fields of the current type and executes a traverse action + Original object + Target object + The action receiving a dot path representing the field pair and the instances + + + + Iterates over all properties of the current type and executes a traverse action + Original object + The action receiving a instance for each property + + + + Iterates over all properties of the current type and executes a traverse action + Original object + Target object + The action receiving a pair of instances for each property pair + + + + Iterates over all properties of the current type and executes a traverse action + Original object + Target object + The action receiving a dot path representing the property pair and the instances + + + + A default field action that copies fields to fields + + + + Returns a string that represents the current traverse + A string representation + + + + diff --git a/build/common.targets b/build/common.targets index 2a569201..f7f57593 100644 --- a/build/common.targets +++ b/build/common.targets @@ -1,15 +1,44 @@ + - - - - - 3.7.6 + + 3.18.2 SMAPI - latest $(AssemblySearchPaths);{GAC} - $(DefineConstants);SMAPI_FOR_WINDOWS + $(DefineConstants);SMAPI_DEPRECATED;SMAPI_FOR_WINDOWS + true + + + enable + $(NoWarn);CS8632 + + + $(DefineConstants);SMAPI_FOR_WINDOWS + true + + + None + + + $(NoWarn);CS0612;CS0618 + $(NoWarn);CS0436;CA1416;CS0809;NU1701 @@ -19,52 +48,20 @@ - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - pdbonly true + + @@ -73,7 +70,6 @@ $(GamePath) - + - diff --git a/build/deploy-local-smapi.targets b/build/deploy-local-smapi.targets new file mode 100644 index 00000000..6ea5f0a2 --- /dev/null +++ b/build/deploy-local-smapi.targets @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/find-game-folder.targets b/build/find-game-folder.targets index f304d841..b73b1169 100644 --- a/build/find-game-folder.targets +++ b/build/find-game-folder.targets @@ -1,3 +1,9 @@ + @@ -11,23 +17,15 @@ $(HOME)/GOG Games/Stardew Valley/game $(HOME)/.steam/steam/steamapps/common/Stardew Valley $(HOME)/.local/share/Steam/steamapps/common/Stardew Valley + $(HOME)/.var/app/com.valvesoftware.Steam/data/Steam/steamapps/common/Stardew Valley - + /Applications/Stardew Valley.app/Contents/MacOS $(HOME)/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS - - C:\Program Files\GalaxyClient\Games\Stardew Valley - C:\Program Files\GOG Galaxy\Games\Stardew Valley - C:\Program Files\Steam\steamapps\common\Stardew Valley - - C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley - C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley - C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley - $([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\GOG.com\Games\1453375253', 'PATH', null, RegistryView.Registry32)) $([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150', 'InstallLocation', null, RegistryView.Registry64, RegistryView.Registry32)) @@ -35,13 +33,34 @@ <_SteamLibraryPath>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\SOFTWARE\Valve\Steam', 'SteamPath', null, RegistryView.Registry32)) $(_SteamLibraryPath)\steamapps\common\Stardew Valley + + + C:\Program Files\GalaxyClient\Games\Stardew Valley + C:\Program Files\GOG Galaxy\Games\Stardew Valley + C:\Program Files\GOG Games\Stardew Valley + C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley + C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley + C:\Program Files (x86)\GOG Games\Stardew Valley + + + + C:\Program Files\ModifiableWindowsApps\Stardew Valley + D:\Program Files\ModifiableWindowsApps\Stardew Valley + E:\Program Files\ModifiableWindowsApps\Stardew Valley + F:\Program Files\ModifiableWindowsApps\Stardew Valley + G:\Program Files\ModifiableWindowsApps\Stardew Valley + H:\Program Files\ModifiableWindowsApps\Stardew Valley + + + C:\Program Files\Steam\steamapps\common\Stardew Valley + C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley - - - - Stardew Valley - StardewValley - diff --git a/build/prepare-install-package.targets b/build/prepare-install-package.targets deleted file mode 100644 index 7b9d63f9..00000000 --- a/build/prepare-install-package.targets +++ /dev/null @@ -1,141 +0,0 @@ - - - - - windows - unix - - $(SolutionDir) - $(SolutionDir)\..\bin - - $(BuildRootPath)\SMAPI\bin\$(Configuration) - $(BuildRootPath)\SMAPI.Toolkit\bin\$(Configuration)\net4.5 - $(BuildRootPath)\SMAPI.Mods.ConsoleCommands\bin\$(Configuration) - $(BuildRootPath)\SMAPI.Mods.SaveBackup\bin\$(Configuration) - - $(OutRootPath)\SMAPI installer - $(OutRootPath)\SMAPI installer for developers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/build/unix/prepare-install-package.sh b/build/unix/prepare-install-package.sh new file mode 100755 index 00000000..1d805e00 --- /dev/null +++ b/build/unix/prepare-install-package.sh @@ -0,0 +1,213 @@ +#!/bin/bash + +# +# +# This is the Bash equivalent of ../windows/prepare-install-package.ps1. +# When making changes, both scripts should be updated. +# +# + + +########## +## Fetch values +########## +# paths +gamePath="/home/pathoschild/Stardew Valley" +bundleModNames=("ConsoleCommands" "ErrorHandler" "SaveBackup") + +# build configuration +buildConfig="Release" +folders=("linux" "macOS" "windows") +declare -A runtimes=(["linux"]="linux-x64" ["macOS"]="osx-x64" ["windows"]="win-x64") +declare -A msBuildPlatformNames=(["linux"]="Unix" ["macOS"]="OSX" ["windows"]="Windows_NT") + +# version number +version="$1" +if [ $# -eq 0 ]; then + echo "SMAPI release version (like '4.0.0'):" + read version +fi + + +########## +## Move to SMAPI root +########## +cd "`dirname "$0"`/../.." + + +########## +## Clear old build files +########## +echo "Clearing old builds..." +echo "-------------------------------------------------" +for path in bin */**/bin */**/obj; do + echo "$path" + rm -rf $path +done +echo "" + +########## +## Compile files +########## +. ${0%/*}/set-smapi-version.sh "$version" +for folder in ${folders[@]}; do + runtime=${runtimes[$folder]} + msbuildPlatformName=${msBuildPlatformNames[$folder]} + + echo "Compiling SMAPI for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained true + echo "" + echo "" + + echo "Compiling installer for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI.Installer --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" -p:PublishTrimmed=True -p:TrimMode=Link --self-contained true + echo "" + echo "" + + for modName in ${bundleModNames[@]}; do + echo "Compiling $modName for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI.Mods.$modName --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" + echo "" + echo "" + done +done + + +########## +## Prepare install package +########## +echo "Preparing install package..." +echo "-------------------------------------------------" + +# init paths +installAssets="src/SMAPI.Installer/assets" +packagePath="bin/SMAPI installer" +packageDevPath="bin/SMAPI installer for developers" + +# init structure +for folder in ${folders[@]}; do + mkdir "$packagePath/internal/$folder/bundle/smapi-internal" --parents +done + +# copy base installer files +for name in "install on Linux.sh" "install on macOS.command" "install on Windows.bat" "README.txt"; do + cp "$installAssets/$name" "$packagePath" +done + +# copy per-platform files +for folder in ${folders[@]}; do + runtime=${runtimes[$folder]} + + # get paths + smapiBin="src/SMAPI/bin/$buildConfig/$runtime/publish" + internalPath="$packagePath/internal/$folder" + bundlePath="$internalPath/bundle" + + # installer files + cp -r "src/SMAPI.Installer/bin/$buildConfig/$runtime/publish"/* "$internalPath" + rm -rf "$internalPath/assets" + + # runtime config for SMAPI + # This is identical to the one generated by the build, except that the min runtime version is + # set to 5.0.0 (instead of whatever version it was built with) and rollForward is set to latestMinor instead of + # minor. + cp "$installAssets/runtimeconfig.json" "$bundlePath/StardewModdingAPI.runtimeconfig.json" + + # installer DLL config + if [ $folder == "windows" ]; then + cp "$installAssets/windows-exe-config.xml" "$packagePath/internal/windows/install.exe.config" + fi + + # bundle root files + for name in "StardewModdingAPI" "StardewModdingAPI.dll" "StardewModdingAPI.pdb" "StardewModdingAPI.xml" "steam_appid.txt"; do + if [ $name == "StardewModdingAPI" ] && [ $folder == "windows" ]; then + name="$name.exe" + fi + + cp "$smapiBin/$name" "$bundlePath" + done + + # bundle i18n + cp -r "$smapiBin/i18n" "$bundlePath/smapi-internal" + + # bundle smapi-internal + for name in "0Harmony.dll" "0Harmony.xml" "Mono.Cecil.dll" "Mono.Cecil.Mdb.dll" "Mono.Cecil.Pdb.dll" "MonoMod.Common.dll" "Newtonsoft.Json.dll" "Pathoschild.Http.Client.dll" "Pintail.dll" "TMXTile.dll" "SMAPI.Toolkit.dll" "SMAPI.Toolkit.pdb" "SMAPI.Toolkit.xml" "SMAPI.Toolkit.CoreInterfaces.dll" "SMAPI.Toolkit.CoreInterfaces.pdb" "SMAPI.Toolkit.CoreInterfaces.xml" "System.Net.Http.Formatting.dll"; do + cp "$smapiBin/$name" "$bundlePath/smapi-internal" + done + + cp "$smapiBin/SMAPI.config.json" "$bundlePath/smapi-internal/config.json" + cp "$smapiBin/SMAPI.metadata.json" "$bundlePath/smapi-internal/metadata.json" + if [ $folder == "linux" ] || [ $folder == "macOS" ]; then + cp "$installAssets/unix-launcher.sh" "$bundlePath" + else + cp "$installAssets/windows-exe-config.xml" "$bundlePath/StardewModdingAPI.exe.config" + fi + + # copy .NET dependencies + if [ $folder == "windows" ]; then + cp "$smapiBin/System.Management.dll" "$bundlePath/smapi-internal" + fi + + # copy legacy .NET dependencies (remove in SMAPI 4.0.0) + cp "$smapiBin/System.Configuration.ConfigurationManager.dll" "$bundlePath/smapi-internal" + cp "$smapiBin/System.Runtime.Caching.dll" "$bundlePath/smapi-internal" + cp "$smapiBin/System.Security.Permissions.dll" "$bundlePath/smapi-internal" + + # copy bundled mods + for modName in ${bundleModNames[@]}; do + fromPath="src/SMAPI.Mods.$modName/bin/$buildConfig/$runtime/publish" + targetPath="$bundlePath/Mods/$modName" + + mkdir "$targetPath" --parents + + cp "$fromPath/$modName.dll" "$targetPath" + cp "$fromPath/$modName.pdb" "$targetPath" + cp "$fromPath/manifest.json" "$targetPath" + if [ -d "$fromPath/i18n" ]; then + cp -r "$fromPath/i18n" "$targetPath" + fi + done +done + +# mark scripts executable +for path in "install on Linux.sh" "install on macOS.command" "bundle/unix-launcher.sh"; do + if [ -f "$packagePath/$path" ]; then + chmod 755 "$packagePath/$path" + fi +done + +# split into main + for-dev folders +cp -r "$packagePath" "$packageDevPath" +for folder in ${folders[@]}; do + # disable developer mode in main package + sed --in-place --expression="s/\"DeveloperMode\": true/\"DeveloperMode\": false/" "$packagePath/internal/$folder/bundle/smapi-internal/config.json" + + # convert bundle folder into final 'install.dat' files + for path in "$packagePath/internal/$folder" "$packageDevPath/internal/$folder"; do + pushd "$path/bundle" > /dev/null + zip "install.dat" * --recurse-paths --quiet + popd > /dev/null + mv "$path/bundle/install.dat" "$path/install.dat" + rm -rf "$path/bundle" + done +done + + +########## +## Create release zips +########## +# rename folders +mv "$packagePath" "bin/SMAPI $version installer" +mv "$packageDevPath" "bin/SMAPI $version installer for developers" + +# package files +pushd bin > /dev/null +zip -9 "SMAPI $version installer.zip" "SMAPI $version installer" --recurse-paths --quiet +zip -9 "SMAPI $version installer for developers.zip" "SMAPI $version installer for developers" --recurse-paths --quiet +popd > /dev/null + +echo "" +echo "Done! Package created in $(pwd)/bin" diff --git a/build/unix/set-smapi-version.sh b/build/unix/set-smapi-version.sh new file mode 100755 index 00000000..0c0cbeb0 --- /dev/null +++ b/build/unix/set-smapi-version.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# +# +# This is the Bash equivalent of ../windows/set-smapi-version.ps1. +# When making changes, both scripts should be updated. +# +# + + +# get version number +version="$1" +if [ $# -eq 0 ]; then + echo "SMAPI release version (like '4.0.0'):" + read version +fi + +# move to SMAPI root +cd "`dirname "$0"`/../.." + +# apply changes +sed "s/.+<\/Version>/$version<\/Version>/" "build/common.targets" --in-place --regexp-extended +sed "s/RawApiVersion = \".+?\";/RawApiVersion = \"$version\";/" "src/SMAPI/Constants.cs" --in-place --regexp-extended +for modName in "ConsoleCommands" "ErrorHandler" "SaveBackup"; do + sed "s/\"(Version|MinimumApiVersion)\": \".+?\"/\"\1\": \"$version\"/g" "src/SMAPI.Mods.$modName/manifest.json" --in-place --regexp-extended +done diff --git a/build/windows/finalize-install-package.sh b/build/windows/finalize-install-package.sh new file mode 100755 index 00000000..0996e3ed --- /dev/null +++ b/build/windows/finalize-install-package.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +########## +## Read config +########## +# get SMAPI version +version="$1" +if [ $# -eq 0 ]; then + echo "SMAPI release version (like '4.0.0'):" + read version +fi + +# get Windows bin path +windowsBinPath="$2" +if [ $# -le 1 ]; then + echo "Windows compiled bin path:" + read windowsBinPath +fi + +# installer internal folders +buildFolders=("linux" "macOS" "windows") + + +########## +## Finalize release package +########## +for folderName in "SMAPI $version installer" "SMAPI $version installer for developers"; do + # move files to Linux filesystem + echo "Preparing $folderName.zip..." + echo "-------------------------------------------------" + echo "copying '$windowsBinPath/$folderName' to Linux filesystem..." + cp -r "$windowsBinPath/$folderName" . + + # fix permissions + echo "fixing permissions..." + find "$folderName" -type d -exec chmod 755 {} \; + find "$folderName" -type f -exec chmod 644 {} \; + find "$folderName" -name "*.sh" -exec chmod 755 {} \; + find "$folderName" -name "*.command" -exec chmod 755 {} \; + find "$folderName" -name "SMAPI.Installer" -exec chmod 755 {} \; + find "$folderName" -name "StardewModdingAPI" -exec chmod 755 {} \; + + # convert bundle folder into final 'install.dat' files + for build in ${buildFolders[@]}; do + echo "packaging $folderName/internal/$build/install.dat..." + pushd "$folderName/internal/$build/bundle" > /dev/null + zip "install.dat" * --recurse-paths --quiet + mv install.dat ../ + popd > /dev/null + + rm -rf "$folderName/internal/$build/bundle" + done + + # zip installer + echo "packaging installer..." + zip -9 "$folderName.zip" "$folderName" --recurse-paths --quiet + + # move zip back to Windows bin path + echo "moving release zip to $windowsBinPath/$folderName.zip..." + mv "$folderName.zip" "$windowsBinPath" + rm -rf "$folderName" + + echo "" + echo "" +done + +echo "Done!" diff --git a/build/windows/lib/in-place-regex.ps1 b/build/windows/lib/in-place-regex.ps1 new file mode 100644 index 00000000..7b309342 --- /dev/null +++ b/build/windows/lib/in-place-regex.ps1 @@ -0,0 +1,11 @@ +function In-Place-Regex { + param ( + [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory)][string]$Search, + [Parameter(Mandatory)][string]$Replace + ) + + $content = (Get-Content "$Path" -Encoding UTF8) + $content = ($content -replace "$Search", "$Replace") + [System.IO.File]::WriteAllLines((Get-Item "$Path").FullName, $content) +} diff --git a/build/windows/prepare-install-package.ps1 b/build/windows/prepare-install-package.ps1 new file mode 100644 index 00000000..71de1154 --- /dev/null +++ b/build/windows/prepare-install-package.ps1 @@ -0,0 +1,242 @@ +# +# +# This is the PowerShell equivalent of ../unix/prepare-install-package.sh, *except* that it doesn't +# set Linux permissions, create the install.dat files, or create the final zip (unless you specify +# --windows-only). Due to limitations in PowerShell, the final changes are handled by the +# windows/finalize-install-package.sh file in WSL. +# +# When making changes, make sure to update ../unix/prepare-install-package.ps1 too. +# +# + +. "$PSScriptRoot/lib/in-place-regex.ps1" + + +########## +## Fetch values +########## +# paths +$gamePath = "C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley" +$bundleModNames = "ConsoleCommands", "ErrorHandler", "SaveBackup" + +# build configuration +$buildConfig = "Release" +$folders = "linux", "macOS", "windows" +$runtimes = @{ linux = "linux-x64"; macOS = "osx-x64"; windows = "win-x64" } +$msBuildPlatformNames = @{ linux = "Unix"; macOS = "OSX"; windows = "Windows_NT" } + +# version number +$version = $args[0] +if (!$version) { + $version = Read-Host "SMAPI release version (like '4.0.0')" +} + +# Windows-only build +$windowsOnly = $false +foreach ($arg in $args) { + if ($arg -eq "--windows-only") { + $windowsOnly = $true + $folders = "windows" + $runtimes = @{ windows = "win-x64" } + $msBuildPlatformNames = @{ windows = "Windows_NT" } + } +} + + +########## +## Move to SMAPI root +########## +cd "$PSScriptRoot/../.." + + +########## +## Clear old build files +########## +echo "Clearing old builds..." +echo "-------------------------------------------------" + +foreach ($path in (dir -Recurse -Include ('bin', 'obj'))) { + echo "$path" + rm -Recurse -Force "$path" +} +echo "" + + +########## +## Compile files +########## +. "$PSScriptRoot/set-smapi-version.ps1" "$version" +foreach ($folder in $folders) { + $runtime = $runtimes[$folder] + $msbuildPlatformName = $msBuildPlatformNames[$folder] + + echo "Compiling SMAPI for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained true + echo "" + echo "" + + echo "Compiling installer for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI.Installer --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" -p:PublishTrimmed=True -p:TrimMode=Link --self-contained true + echo "" + echo "" + + foreach ($modName in $bundleModNames) { + echo "Compiling $modName for $folder..." + echo "-------------------------------------------------" + dotnet publish src/SMAPI.Mods.$modName --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" + echo "" + echo "" + } +} + + +########## +## Prepare install package +########## +echo "Preparing install package..." +echo "----------------------------" + +# init paths +$installAssets = "src/SMAPI.Installer/assets" +$packagePath = "bin/SMAPI installer" +$packageDevPath = "bin/SMAPI installer for developers" + +# init structure +foreach ($folder in $folders) { + mkdir "$packagePath/internal/$folder/bundle/smapi-internal" > $null +} + +# copy base installer files +foreach ($name in @("install on Linux.sh", "install on macOS.command", "install on Windows.bat", "README.txt")) { + if ($windowsOnly -and ($name -eq "install on Linux.sh" -or $name -eq "install on macOS.command")) { + continue; + } + + cp "$installAssets/$name" "$packagePath" +} + +# copy per-platform files +foreach ($folder in $folders) { + $runtime = $runtimes[$folder] + + # get paths + $smapiBin = "src/SMAPI/bin/$buildConfig/$runtime/publish" + $internalPath = "$packagePath/internal/$folder" + $bundlePath = "$internalPath/bundle" + + # installer files + cp "src/SMAPI.Installer/bin/$buildConfig/$runtime/publish/*" "$internalPath" -Recurse + rm -Recurse -Force "$internalPath/assets" + + # runtime config for SMAPI + # This is identical to the one generated by the build, except that the min runtime version is + # set to 5.0.0 (instead of whatever version it was built with) and rollForward is set to latestMinor instead of + # minor. + cp "$installAssets/runtimeconfig.json" "$bundlePath/StardewModdingAPI.runtimeconfig.json" + + # installer DLL config + if ($folder -eq "windows") { + cp "$installAssets/windows-exe-config.xml" "$packagePath/internal/windows/install.exe.config" + } + + # bundle root files + foreach ($name in @("StardewModdingAPI", "StardewModdingAPI.dll", "StardewModdingAPI.pdb", "StardewModdingAPI.xml", "steam_appid.txt")) { + if ($name -eq "StardewModdingAPI" -and $folder -eq "windows") { + $name = "$name.exe" + } + + cp "$smapiBin/$name" "$bundlePath" + } + + # bundle i18n + cp -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal" + + # bundle smapi-internal + foreach ($name in @("0Harmony.dll", "0Harmony.xml", "Mono.Cecil.dll", "Mono.Cecil.Mdb.dll", "Mono.Cecil.Pdb.dll", "MonoMod.Common.dll", "Newtonsoft.Json.dll", "Pathoschild.Http.Client.dll", "Pintail.dll", "TMXTile.dll", "SMAPI.Toolkit.dll", "SMAPI.Toolkit.pdb", "SMAPI.Toolkit.xml", "SMAPI.Toolkit.CoreInterfaces.dll", "SMAPI.Toolkit.CoreInterfaces.pdb", "SMAPI.Toolkit.CoreInterfaces.xml", "System.Net.Http.Formatting.dll")) { + cp "$smapiBin/$name" "$bundlePath/smapi-internal" + } + + if ($folder -eq "windows") { + cp "$smapiBin/VdfConverter.dll" "$bundlePath/smapi-internal" + } + + cp "$smapiBin/SMAPI.config.json" "$bundlePath/smapi-internal/config.json" + cp "$smapiBin/SMAPI.metadata.json" "$bundlePath/smapi-internal/metadata.json" + if ($folder -eq "linux" -or $folder -eq "macOS") { + cp "$installAssets/unix-launcher.sh" "$bundlePath" + } + else { + cp "$installAssets/windows-exe-config.xml" "$bundlePath/StardewModdingAPI.exe.config" + } + + # copy .NET dependencies + if ($folder -eq "windows") { + cp "$smapiBin/System.Management.dll" "$bundlePath/smapi-internal" + } + + # copy legacy .NET dependencies (remove in SMAPI 4.0.0) + cp "$smapiBin/System.Configuration.ConfigurationManager.dll" "$bundlePath/smapi-internal" + cp "$smapiBin/System.Runtime.Caching.dll" "$bundlePath/smapi-internal" + cp "$smapiBin/System.Security.Permissions.dll" "$bundlePath/smapi-internal" + + # copy bundled mods + foreach ($modName in $bundleModNames) { + $fromPath = "src/SMAPI.Mods.$modName/bin/$buildConfig/$runtime/publish" + $targetPath = "$bundlePath/Mods/$modName" + + mkdir "$targetPath" > $null + + cp "$fromPath/$modName.dll" "$targetPath" + cp "$fromPath/$modName.pdb" "$targetPath" + cp "$fromPath/manifest.json" "$targetPath" + if (Test-Path "$fromPath/i18n" -PathType Container) { + cp -Recurse "$fromPath/i18n" "$targetPath" + } + } +} + +# DISABLED: will be handled by Linux script +# mark scripts executable +#ForEach ($path in @("install on Linux.sh", "install on macOS.command", "bundle/unix-launcher.sh")) { +# if (Test-Path "$packagePath/$path" -PathType Leaf) { +# chmod 755 "$packagePath/$path" +# } +#} + +# split into main + for-dev folders +cp -Recurse "$packagePath" "$packageDevPath" +foreach ($folder in $folders) { + # disable developer mode in main package + In-Place-Regex -Path "$packagePath/internal/$folder/bundle/smapi-internal/config.json" -Search "`"DeveloperMode`": true" -Replace "`"DeveloperMode`": false" + + # convert bundle folder into final 'install.dat' files + if ($windowsOnly) + { + foreach ($path in @("$packagePath/internal/$folder", "$packageDevPath/internal/$folder")) + { + Compress-Archive -Path "$path/bundle/*" -CompressionLevel Optimal -DestinationPath "$path/install.zip" + mv "$path/install.zip" "$path/install.dat" + rm -Recurse -Force "$path/bundle" + } + } +} + + +########### +### Create release zips +########### +# rename folders +mv "$packagePath" "bin/SMAPI $version installer" +mv "$packageDevPath" "bin/SMAPI $version installer for developers" + +# package files +if ($windowsOnly) +{ + Compress-Archive -Path "bin/SMAPI $version installer" -DestinationPath "bin/SMAPI $version installer.zip" -CompressionLevel Optimal + Compress-Archive -Path "bin/SMAPI $version installer for developers" -DestinationPath "bin/SMAPI $version installer for developers.zip" -CompressionLevel Optimal +} + +echo "" +echo "Done! See docs/technical/smapi.md to create the release zips." diff --git a/build/windows/set-smapi-version.ps1 b/build/windows/set-smapi-version.ps1 new file mode 100644 index 00000000..ff6b2096 --- /dev/null +++ b/build/windows/set-smapi-version.ps1 @@ -0,0 +1,25 @@ +# +# +# This is the PowerShell equivalent of ../unix/set-smapi-version.sh. +# When making changes, both scripts should be updated. +# +# + + +. "$PSScriptRoot\lib\in-place-regex.ps1" + +# get version number +$version=$args[0] +if (!$version) { + $version = Read-Host "SMAPI release version (like '4.0.0')" +} + +# move to SMAPI root +cd "$PSScriptRoot/../.." + +# apply changes +In-Place-Regex -Path "build/common.targets" -Search ".+" -Replace "$version" +In-Place-Regex -Path "src/SMAPI/Constants.cs" -Search "RawApiVersion = `".+?`";" -Replace "RawApiVersion = `"$version`";" +ForEach ($modName in "ConsoleCommands","ErrorHandler","SaveBackup") { + In-Place-Regex -Path "src/SMAPI.Mods.$modName/manifest.json" -Search "`"(Version|MinimumApiVersion)`": `".+?`"" -Replace "`"`$1`": `"$version`"" +} diff --git a/docs/README.md b/docs/README.md index 4726c190..d3aaae64 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,9 +11,9 @@ doesn't change any of your game files. It serves seven main purposes: couldn't._ 3. **Rewrite mods for compatibility.** - _SMAPI rewrites mods' compiled code before loading them so they work on Linux/Mac/Windows - without the mods needing to handle differences between the Linux/Mac and Windows versions of the - game. In some cases it also rewrites code broken by a game update so the mod doesn't break._ + _SMAPI rewrites mods' compiled code before loading them so they work on Linux/macOS/Windows + without the mods needing to handle differences between the Linux/macOS and Windows versions of + the game. In some cases it also rewrites code broken by a game update so the mod doesn't break._ 5. **Intercept errors and automatically fix saves.** _SMAPI intercepts errors, shows the error info in the SMAPI console, and in most cases @@ -56,17 +56,24 @@ SMAPI rarely shows text in-game, so it only has a few translations. Contribution [Modding:Translations](https://stardewvalleywiki.com/Modding:Translations) on the wiki for help contributing translations. -locale | status ----------- | :---------------- -default | ✓ [fully translated](../src/SMAPI/i18n/default.json) -Chinese | ✓ [fully translated](../src/SMAPI/i18n/zh.json) -French | ✓ [fully translated](../src/SMAPI/i18n/fr.json) -German | ✓ [fully translated](../src/SMAPI/i18n/de.json) -Hungarian | ✓ [fully translated](../src/SMAPI/i18n/hu.json) -Italian | ✓ [fully translated](../src/SMAPI/i18n/it.json) -Japanese | ✓ [fully translated](../src/SMAPI/i18n/ja.json) -Korean | ✓ [fully translated](../src/SMAPI/i18n/ko.json) -Portuguese | ✓ [fully translated](../src/SMAPI/i18n/pt.json) -Russian | ✓ [fully translated](../src/SMAPI/i18n/ru.json) -Spanish | ✓ [fully translated](../src/SMAPI/i18n/es.json) -Turkish | ✓ [fully translated](../src/SMAPI/i18n/tr.json) +locale | status +----------- | :---------------- +default | ✓ [fully translated](../src/SMAPI/i18n/default.json) +Chinese | ✓ [fully translated](../src/SMAPI/i18n/zh.json) +French | ✓ [fully translated](../src/SMAPI/i18n/fr.json) +German | ✓ [fully translated](../src/SMAPI/i18n/de.json) +Hungarian | ✓ [fully translated](../src/SMAPI/i18n/hu.json) +Italian | ✓ [fully translated](../src/SMAPI/i18n/it.json) +Japanese | ✓ [fully translated](../src/SMAPI/i18n/ja.json) +Korean | ✓ [fully translated](../src/SMAPI/i18n/ko.json) +[Polish] | ✓ [fully translated](../src/SMAPI/i18n/pl.json) +Portuguese | ✓ [fully translated](../src/SMAPI/i18n/pt.json) +Russian | ✓ [fully translated](../src/SMAPI/i18n/ru.json) +Spanish | ✓ [fully translated](../src/SMAPI/i18n/es.json) +[Thai] | ✓ [fully translated](../src/SMAPI/i18n/th.json) +Turkish | ✓ [fully translated](../src/SMAPI/i18n/tr.json) +[Ukrainian] | ✓ [fully translated](../src/SMAPI/i18n/uk.json) + +[Polish]: https://www.nexusmods.com/stardewvalley/mods/3616 +[Thai]: https://www.nexusmods.com/stardewvalley/mods/7052 +[Ukrainian]: https://www.nexusmods.com/stardewvalley/mods/8427 diff --git a/docs/release-notes-archived.md b/docs/release-notes-archived.md index 9f8de3cb..9801c226 100644 --- a/docs/release-notes-archived.md +++ b/docs/release-notes-archived.md @@ -19,7 +19,7 @@ Released 13 September 2019 for Stardew Valley 1.3.36. * Added log parser instructions for Android. * Fixed log parser failing in some cases due to time format localization. -* For modders: +* For mod authors: * `this.Monitor.Log` now defaults to the `Trace` log level instead of `Debug`. The change will only take effect when you recompile the mod. * Fixed 'location list changed' verbose log not correctly listing changes. * Fixed mods able to directly load (and in some cases edit) a different mod's local assets using internal asset key forwarding. @@ -30,7 +30,7 @@ Released 13 September 2019 for Stardew Valley 1.3.36. Released 23 April 2019 for Stardew Valley 1.3.36. * For players: - * Fixed error when a custom map references certain vanilla tilesheets on Linux/Mac. + * Fixed error when a custom map references certain vanilla tilesheets on Linux/macOS. * Fixed compatibility with some Linux distros. ## 2.11.1 @@ -42,7 +42,7 @@ Released 17 March 2019 for Stardew Valley 1.3.36. * Updated mod compatibility list. * Fixed `world_clear` console command removing chests edited to have a debris name. -* For modders: +* For mod authors: * Added support for suppressing false-positive warnings in rare cases. * For the web UI: @@ -55,7 +55,7 @@ Released 01 March 2019 for Stardew Valley 1.3.36. * For players: * Updated for Stardew Valley 1.3.36. -* For modders: +* For mod authors: * Bumped all deprecation levels to _pending removal_. * For the web UI: @@ -68,7 +68,7 @@ Released 09 January 2019 for Stardew Valley 1.3.32–33. * For players: * SMAPI now keeps the first save backup created for the day, instead of the last one. - * Fixed save backup for some Linux/Mac players. (When compression isn't available, SMAPI will now create uncompressed backups instead.) + * Fixed save backup for some Linux/macOS players. (When compression isn't available, SMAPI will now create uncompressed backups instead.) * Fixed some common dependencies not linking to the mod page in 'missing mod' errors. * Fixed 'unknown mod' deprecation warnings showing a stack trace when developers mode not enabled. * Fixed 'unknown mod' deprecation warnings when they occur in the Mod constructor. @@ -80,7 +80,7 @@ Released 09 January 2019 for Stardew Valley 1.3.32–33. * Added beta status filter to compatibility list. * Fixed broken ModDrop links in the compatibility list. -* For modders: +* For mod authors: * Asset changes are now propagated into the parsed save being loaded if applicable. * Added locale to context trace logs. * Fixed error loading custom map tilesheets in some cases. @@ -90,7 +90,7 @@ Released 09 January 2019 for Stardew Valley 1.3.32–33. * Fixed 'unknown mod' deprecation warnings showing the wrong stack trace. * Fixed `e.Cursor` in input events showing wrong grab tile when player using a controller moves without moving the viewpoint. * Fixed incorrect 'bypassed safety checks' warning for mods using the new `Specialized.LoadStageChanged` event in 2.10. - * Deprecated `EntryDll` values whose capitalization don't match the actual file. (This works on Windows, but causes errors for Linux/Mac players.) + * Deprecated `EntryDll` values whose capitalization don't match the actual file. (This works on Windows, but causes errors for Linux/macOS players.) ## 2.10.1 Released 30 December 2018 for Stardew Valley 1.3.32–33. @@ -106,7 +106,7 @@ Released 29 December 2018 for Stardew Valley 1.3.32–33. * Minor performance improvements. * Tweaked installer to reduce antivirus false positives. -* For modders: +* For mod authors: * Added [events](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events): `GameLoop.OneSecondUpdateTicking`, `GameLoop.OneSecondUpdateTicked`, and `Specialized.LoadStageChanged`. * Added `e.IsCurrentLocation` event arg to `World` events. * You can now use `helper.Data.Read/WriteSaveData` as soon as the save is loaded (instead of once the world is initialized). @@ -133,7 +133,7 @@ Released 16 December 2018 for Stardew Valley 1.3.32. * Fixed game launch errors logged as `SMAPI` instead of `game`. * Fixed Windows installer adding unneeded Unix launcher to game folder. -* For modders: +* For mod authors: * Moved content pack methods into a new [content pack API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Content_Packs). * Fixed invalid NPC data propagated when a mod changes NPC dispositions. * Fixed `Display.RenderedWorld` event broken in SMAPI 2.9.1. @@ -162,7 +162,7 @@ Released 07 December 2018 for Stardew Valley 1.3.32. * Fixed empty "mods with warnings" list in some cases due to hidden warnings. * Fixed Console Commands' handling of tool upgrade levels for item commands. -* For modders: +* For mod authors: * Added ModDrop update keys (see [docs](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Manifest#Update_checks)). * Added `IsLocalPlayer` to new player events. * Added `helper.CreateTemporaryContentPack` to replace the deprecated `CreateTransitionalContentPack`. @@ -183,7 +183,7 @@ Released 07 December 2018 for Stardew Valley 1.3.32. ## 2.8.2 Released 19 November 2018 for Stardew Valley 1.3.32. -* Fixed game crash in MacOS with SMAPI 2.8. +* Fixed game crash in macOS with SMAPI 2.8. ## 2.8.1 Released 19 November 2018 for Stardew Valley 1.3.32. @@ -205,7 +205,7 @@ Released 19 November 2018 for Stardew Valley 1.3.32. * SMAPI now recommends a compatible SMAPI version if you have an older game version. * Improved various error messages to be more clear and intuitive. * Improved compatibility with various Linux shells (thanks to lqdev!), and prefer xterm when available. - * Fixed transparency issues on Linux/Mac for some mod images. + * Fixed transparency issues on Linux/macOS for some mod images. * Fixed error when a mod manifest is corrupted. * Fixed error when a mod adds an unnamed location. * Fixed friendly error no longer shown when SMAPI isn't run from the game folder. @@ -223,9 +223,9 @@ Released 19 November 2018 for Stardew Valley 1.3.32. * The log parser now has a separate filter for game messages. * The log parser now shows content pack authors (thanks to danvolchek!). * Tweaked log parser UI (thanks to danvolchek!). - * Fixed log parser instructions for Mac. + * Fixed log parser instructions for macOS. -* For modders: +* For mod authors: * Added [data API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Data) to store mod data in the save file or app data. * Added [multiplayer API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Multiplayer) and [events](https://stardewvalleywiki.com/Modding:Modder_Guide/Apis/Events#Multiplayer_2) to send/receive messages and get connected player info. * Added [verbose logging](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Logging#Verbose_logging) feature. @@ -267,7 +267,7 @@ Released 14 August 2018 for Stardew Valley 1.3.28. * Improved how mod issues are listed in the console and log. * Revamped installer. It now... * uses a new format that should be more intuitive; - * lets players on Linux/Mac choose the console color scheme (SMAPI will auto-detect it on Windows); + * lets players on Linux/macOS choose the console color scheme (SMAPI will auto-detect it on Windows); * and validates requirements earlier. * Fixed custom festival maps always using spring tilesheets. * Fixed `player_add` command not recognising return scepter. @@ -275,7 +275,7 @@ Released 14 August 2018 for Stardew Valley 1.3.28. * Fixed some SMAPI logs not deleted when starting a new session. * Updated compatibility list. -* For modders: +* For mod authors: * Added support for `.json` data files in the content API (including Content Patcher). * Added propagation for asset changes through the content API for... * child sprites; @@ -314,8 +314,8 @@ Released 01 August 2018 for Stardew Valley 1.3.27. * Removed the `player_setlevel` and `player_setspeed` commands, which weren't implemented in a useful way. Use a mod like CJB Cheats Menu if you need those. * Fixed `SEHException` errors for some players. * Fixed performance issues for some players. - * Fixed default color scheme on Mac or in PowerShell (configurable via `StardewModdingAPI.config.json`). - * Fixed installer error on Linux/Mac in some cases. + * Fixed default color scheme on macOS or in PowerShell (configurable via `StardewModdingAPI.config.json`). + * Fixed installer error on Linux/macOS in some cases. * Fixed installer not finding some game paths or showing duplicate paths. * Fixed installer not removing some SMAPI files. * Fixed launch issue for Linux players with some terminals. (Thanks to HanFox and kurumushi!) @@ -336,7 +336,7 @@ Released 01 August 2018 for Stardew Valley 1.3.27. * Fixed log parser mangling crossplatform paths in some cases. * Fixed `smapi.io/install` not linking to a useful page. -* For modders: +* For mod authors: * Added [input API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Input) for reading and suppressing keyboard, controller, and mouse input. * Added code analysis in the NuGet package to flag common issues as warnings. * Replaced `LocationEvents` to support multiplayer: @@ -345,7 +345,7 @@ Released 01 August 2018 for Stardew Valley 1.3.27. * each event now provides a list of added/removed values; * added buildings-changed event. * Added `Context.IsMultiplayer` and `Context.IsMainPlayer` flags. - * Added `Constants.TargetPlatform` which says whether the game is running on Linux, Mac, or Windows. + * Added `Constants.TargetPlatform` which says whether the game is running on Linux, macOS, or Windows. * Added `semanticVersion.IsPrerelease()` method. * Added support for launching multiple instances transparently. This removes the former `--log-path` command-line argument. * Added support for custom seasonal tilesheets when loading an unpacked `.tbin` map. @@ -376,7 +376,7 @@ Released 01 August 2018 for Stardew Valley 1.3.27. * Mod IDs should only contain letters, numbers, hyphens, dots, and underscores. That allows their use in many contexts like URLs. This restriction is now enforced. (In regex form: `^[a-zA-Z0-9_.-]+$`.) * For SMAPI developers: - * Added more consistent crossplatform handling, including MacOS detection. + * Added more consistent crossplatform handling, including macOS detection. * Added beta update channel. * Added optional mod metadata to the web API (including Nexus info, wiki metadata, etc). * Added early prototype of SMAPI 3.0 events via `helper.Events`. @@ -411,14 +411,14 @@ Released 26 March 2018 for Stardew Valley 1.2.30–1.2.33. * For players: * Fixed some textures not updated when a mod changes them. - * Fixed visual bug on Linux/Mac when mods overlay textures. + * Fixed visual bug on Linux/macOS when mods overlay textures. * Fixed error when mods remove an asset editor/loader. * Fixed minimum game version incorrectly increased in SMAPI 2.5.3. * For the [log parser](https://smapi.io/log): * Fixed error when log text contains certain tokens. -* For modders: +* For mod authors: * Updated to Json.NET 11.0.2. * For SMAPI developers: @@ -448,7 +448,7 @@ Released 13 March 2018 for Stardew Valley ~~1.2.30~~–1.2.33. ## 2.5.2 Released 25 February 2018 for Stardew Valley 1.2.30–1.2.33. -* For modders: +* For mod authors: * Fixed issue where replacing an asset through `asset.AsImage()` or `asset.AsDictionary()` didn't take effect. * For the [log parser](https://smapi.io/log): @@ -467,12 +467,12 @@ Released 24 February 2018 for Stardew Valley 1.2.30–1.2.33. * **Added support for [content packs](https://stardewvalleywiki.com/Modding:Content_packs)**. _Content packs are collections of files for a SMAPI mod to load. These can be installed directly under `Mods` like a normal SMAPI mod, get automatic update and compatibility checks, and provide convenient APIs to the mods that read them._ * Added mod detection for unhandled errors (so most errors now mention which mod caused them). - * Added install scripts for Linux/Mac (no more manual terminal commands!). + * Added install scripts for Linux/macOS (no more manual terminal commands!). * Added the missing mod's name and URL to dependency errors. - * Fixed uninstall script not reporting when done on Linux/Mac. + * Fixed uninstall script not reporting when done on Linux/macOS. * Updated compatibility list and enabled update checks for more mods. -* For modders: +* For mod authors: * Added support for content packs and new APIs to read them. * Added support for `ISemanticVersion` in JSON models. * Added `SpecializedEvents.UnvalidatedUpdateTick` event for specialized use cases. @@ -506,7 +506,7 @@ Released 24 January 2018 for Stardew Valley 1.2.30–1.2.33. * For the [log parser](https://smapi.io/log): * Fixed error parsing logs with zero installed mods. -* For modders: +* For mod authors: * Added `SaveEvents.BeforeCreate` and `AfterCreate` events. * Added `SButton` `IsActionButton()` and `IsUseToolButton()` extensions. * Improved JSON parse errors to provide more useful info for troubleshooting. @@ -524,10 +524,10 @@ Released 26 December 2017 for Stardew Valley 1.2.30–1.2.33. * For players: * Added a user-friendly [download page](https://smapi.io). - * Improved cryptic libgdiplus errors on Mac when Mono isn't installed. + * Improved cryptic libgdiplus errors on macOS when Mono isn't installed. * Fixed mod UIs hidden when menu backgrounds are enabled. -* For modders: +* For mod authors: * **Added mod-provided APIs** to allow simple integrations between mods, even without direct assembly references. * Added `GameEvents.FirstUpdateTick` event (called once after all mods are initialized). * Added `IsSuppressed` to input events so mods can optionally avoid handling keys another mod has already handled. @@ -545,9 +545,9 @@ Released 26 December 2017 for Stardew Valley 1.2.30–1.2.33. Released 02 December 2017 for Stardew Valley 1.2.30–1.2.33. * For players: - * Fixed error when a mod loads custom assets on Linux/Mac. - * Fixed error when checking for updates on Linux/Mac due to API HTTPS redirect. - * Fixed error when Mac adds an `mcs` symlink to the installer package. + * Fixed error when a mod loads custom assets on Linux/macOS. + * Fixed error when checking for updates on Linux/macOS due to API HTTPS redirect. + * Fixed error when macOS adds an `mcs` symlink to the installer package. * Fixed `player_add` command not handling tool upgrade levels. * Improved error when a mod has an invalid `EntryDLL` filename format. * Updated compatibility list. @@ -557,7 +557,7 @@ Released 02 December 2017 for Stardew Valley 1.2.30–1.2.33. * Fixed error when uploading very large logs. * Slightly improved the UI. -* For modders: +* For mod authors: * Added `helper.Content.NormalizeAssetName` method. * Added `SDate.DaysSinceStart` property. * Fixed input events' `e.SuppressButton(button)` method ignoring specified button. @@ -575,7 +575,7 @@ Released 01 November 2017 for Stardew Valley 1.2.30–1.2.33. * Fixed compatibility check for players with Stardew Valley 1.08. * Fixed `player_setlevel` command not setting XP too. -* For modders: +* For mod authors: * 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: @@ -600,7 +600,7 @@ Released 14 October 2017 for Stardew Valley 1.2.30–1.2.33. * **Mod update checks** SMAPI now checks if your mods have updates available, and will alert you in the console with a convenient link to the mod page. This works with mods from the Chucklefish mod site, GitHub, or Nexus Mods. SMAPI 2.0 launches with - update-check support for over 250 existing mods, and more will be added as modders enable the feature. + update-check support for over 250 existing mods, and more will be added as mod authors enable the feature. * **Mod stability warnings** SMAPI now detects when a mod contains code which can destabilise your game or corrupt your save, and shows a warning @@ -610,7 +610,7 @@ Released 14 October 2017 for Stardew Valley 1.2.30–1.2.33. The console is now simpler and easier to read, some commands have been streamlined, and the colors now adjust to fit your terminal background color. -* **New features for modders** +* **New features for mod authors** SMAPI 2.0 adds several features to enable new kinds of mods (see [API documentation](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs)). @@ -651,7 +651,7 @@ For players: * The console is now simpler and easier to read, and adjusts its colors to fit your terminal background color. * Renamed installer folder to avoid confusion. * Updated compatibility list. -* Fixed update check errors on Linux/Mac. +* Fixed update check errors on Linux/macOS. * Fixed collection-changed errors during startup for some players. For mod developers: @@ -685,10 +685,10 @@ For SMAPI developers: Released 09 September 2017 for Stardew Valley 1.2.30–1.2.33. For players: -* Fixed errors when loading some custom maps on Linux/Mac or using XNB Loader. +* Fixed errors when loading some custom maps on Linux/macOS or using XNB Loader. * Fixed errors in rare cases when a mod calculates an in-game date. -For modders: +For mod authors: * Added UTC timestamp to log file. For SMAPI developers: @@ -726,7 +726,7 @@ For players: * Fixed controller mod input broken in 1.15. * Fixed TrainerMod packaging unneeded files. -For modders: +For mod authors: * Fixed mod registry lookups by unique ID not being case-insensitive. ## 1.15 @@ -744,7 +744,7 @@ For players: * Fixed invalid `ObjectInformation.xnb` causing a flood of warnings; SMAPI now shows one error instead. * Updated mod compatibility list. -For modders: +For mod authors: * Added `SDate` utility for in-game date calculations (see [API reference](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Utilities#Dates)). * Added support for minimum dependency versions in `manifest.json` (see [API reference](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Manifest)). * Added more useful logging when loading mods. @@ -772,12 +772,12 @@ For players: * you have Stardew Valley 1.11 or earlier (which aren't compatible); * you run `install.exe` from within the downloaded zip file. * Fixed "unknown mod" deprecation warnings by improving how SMAPI detects the mod using the event. -* Fixed `libgdiplus.dylib` errors for some players on Mac. +* Fixed `libgdiplus.dylib` errors for some players on macOS. * Fixed rare crash when window loses focus for a few players. * Bumped minimum game version to 1.2.30. * Updated mod compatibility list. -For modders: +For mod authors: * You can now add dependencies to `manifest.json` (see [API reference](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Manifest)). * You can now translate your mod (see [API reference](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Translation)). * You can now load unpacked `.tbin` files from your mod folder through the content API. @@ -788,7 +788,7 @@ For modders: * Fixed `smapi-crash.txt` being copied from the default log even if a different path is specified with `--log-path`. * Fixed the content API not matching XNB filenames with two dots (like `a.b.xnb`) if you don't specify the `.xnb` extension. * Fixed `debug` command output not printed to console. -* Deprecated `TimeEvents.DayOfMonthChanged`, `SeasonOfYearChanged`, and `YearOfGameChanged`. These don't do what most modders think they do and aren't very reliable, since they depend on the SMAPI/game lifecycle which can change. You should use `TimeEvents.AfterDayStarted` or `SaveEvents.BeforeSave` instead. +* Deprecated `TimeEvents.DayOfMonthChanged`, `SeasonOfYearChanged`, and `YearOfGameChanged`. These don't do what most mod authors think they do and aren't very reliable, since they depend on the SMAPI/game lifecycle which can change. You should use `TimeEvents.AfterDayStarted` or `SaveEvents.BeforeSave` instead. ## 1.13.1 Released 19 May 2017 for Stardew Valley 1.2.26–1.2.29. @@ -805,8 +805,8 @@ For players: * SMAPI now recovers automatically from errors in the game loop when possible. * SMAPI now remembers if your game crashed and offers help next time you launch it. * Fixed installer sometimes finding redundant game paths. -* Fixed save events not being raised after the first day on Linux/Mac. -* Fixed error on Linux/Mac when a mod loads a PNG immediately after the save is loaded. +* Fixed save events not being raised after the first day on Linux/macOS. +* Fixed error on Linux/macOS when a mod loads a PNG immediately after the save is loaded. * Updated mod compatibility list for Stardew Valley 1.2. For mod developers: @@ -826,15 +826,15 @@ Released 03 May 2017 for Stardew Valley 1.2.26–1.2.29. For players: * The installer now lets you choose the install path if you have multiple copies of the game, instead of using the first path found. * Fixed mod draw errors breaking the game. -* Fixed mods on Linux/Mac no longer working after the game saves. -* Fixed `libgdiplus.dylib` errors on Mac when mods read PNG files. +* Fixed mods on Linux/macOS no longer working after the game saves. +* Fixed `libgdiplus.dylib` errors on macOS when mods read PNG files. * Adopted pufferchick. For mod developers: * Unknown mod manifest fields are now stored in `IManifest::ExtraFields`. * The content API now defaults to `ContentSource.ModFolder`. * Fixed content API error when loading a PNG during early game init (e.g. in mod's `Entry`). -* Fixed content API error when loading an XNB from the mod folder on Mac. +* Fixed content API error when loading an XNB from the mod folder on macOS. ## 1.11 Released 30 April 2017 for Stardew Valley 1.2.26. @@ -888,7 +888,7 @@ For players: * Fixed the game-needs-an-update error not pausing before exit. * Fixed installer errors for some players when deleting files. * Fixed installer not ignoring potential game folders that don't contain a Stardew Valley exe. -* Fixed installer not recognising Linux/Mac paths starting with `~/` or containing an escaped space. +* Fixed installer not recognising Linux/macOS paths starting with `~/` or containing an escaped space. * Fixed TrainerMod letting you add invalid items which may crash the game. * Fixed TrainerMod's `world_downminelevel` command not working. * Fixed rare issue where mod dependencies would override SMAPI dependencies and cause unpredictable bugs. @@ -912,7 +912,7 @@ For mod developers: * Removed the experimental `IConfigFile`. For SMAPI developers: -* Added support for debugging SMAPI on Linux/Mac if supported by the editor. +* Added support for debugging SMAPI on Linux/macOS if supported by the editor. ## 1.8 Released 04 February 2017 for Stardew Valley 1.1–1.11. @@ -1004,7 +1004,7 @@ For players: * Improved installer wording to reduce confusion. * Fixed the installer not removing TrainerMod from appdata if it's already in the game mods directory. * Fixed the installer not moving mods out of appdata if the game isn't installed on the same Windows partition. - * Fixed the SMAPI console not being shown on Linux and Mac. + * Fixed the SMAPI console not being shown on Linux and macOS. For developers: * Added a reflection API (via `helper.Reflection`) that simplifies robust access to the game's private fields and methods. @@ -1016,7 +1016,7 @@ For developers: Released 04 December 2016 for Stardew Valley 1.1–1.11. For players: - * You can now run most mods on any platform (e.g. run Windows mods on Linux/Mac). + * You can now run most mods on any platform (e.g. run Windows mods on Linux/macOS). * Fixed the normal uninstaller not removing files added by the 'SMAPI for developers' installer. ## 1.2 @@ -1063,7 +1063,7 @@ For developers: Released 11 November 2016 for Stardew Valley 1.1–1.11. For players: - * Added support for Linux and Mac. + * Added support for Linux and macOS. * Added installer to automate adding & removing SMAPI. * Added background update check on launch. * Fixed missing `steam_appid.txt` file. diff --git a/docs/release-notes.md b/docs/release-notes.md index 1115f482..9eb55b36 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -2,11 +2,759 @@ # Release notes +## 3.18.2 +Released 09 January 2023 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed empty save backups for some macOS players. + * Fixed `player_add` console command not handling custom slingshots correctly (thanks too DaLion!). + +* For mod authors: + * Added `DelegatingModHooks` utility for mods which need to override SMAPI's mod hooks directly. + * Updated to Newtonsoft.Json 13.0.2 (see [changes](https://github.com/JamesNK/Newtonsoft.Json/releases/tag/13.0.2)) and Pintail 2.2.2 (see [changes](https://github.com/Nanoray-pl/Pintail/blob/master/docs/release-notes.md#222)). + +## 3.18.1 +Released 01 December 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed mod texture edits sometimes cut off (thanks to atravita!). + +* For the web UI: + * The log parser no longer warns about missing Error Handler on Android, where it doesn't exist yet (thanks to AnotherPillow!). + +## 3.18.0 +Released 12 November 2022 for Stardew Valley 1.5.6 or later. See [release highlights](https://www.patreon.com/posts/74565278). + +* For players: + * You can now override the mod load order in `smapi-internal/config.json` (thanks to Shockah!). + * You can now disable console input in `smapi-internal/config.json`, which may reduce CPU usage on some Linux systems. + * Fixed map edits not always applied for farmhands in multiplayer (thanks to SinZ163!). + * Internal changes to prepare for the upcoming Stardew Valley 1.6 and SMAPI 4.0. + +* For mod authors: + * Optimized asset name comparisons (thanks to atravita!). + * Raised all deprecation messages to the 'pending removal' level. + * **This is the last major update before SMAPI 4.0.0, which will drop all deprecated APIs.** If you haven't [fixed deprecation warnings in your mod code](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0) (if any), you should do it soon. SMAPI 4.0.0 will release alongside the upcoming Stardew Valley 1.6. + +* For the web UI: + * The log parser now detects split-screen mode and shows which screen logged each message. + +## 3.17.2 +Released 21 October 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed installer crash if Steam's library data is invalid or in an old format; it'll now be ignored instead. +* For mod authors: + * Fixed image patches sometimes applied one pixel higher than expected after 3.17.0 (thanks to atravita!). + +## 3.17.1 +Released 10 October 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed installer error on Windows if the Steam library folder exists but doesn't contain Steam's `.vdf` library data file. + +## 3.17.0 +Released 09 October 2022 for Stardew Valley 1.5.6 or later. See [release highlights](https://www.patreon.com/posts/73090322). + +* For players: + * You can now download SMAPI 'strict mode' from [Nexus files](https://www.nexusmods.com/stardewvalley/mods/2400/?tab=files), which removes all deprecated APIs. This may significantly improve performance, but mods which still show deprecation warnings won't work. + * The SMAPI installer now also detects game folders in Steam's `.vdf` library data on Windows (thanks to pizzaoverhead!). + * SMAPI now prevents mods from enabling Harmony debug mode, which impacts performance and creates a file on your desktop. + _You can allow debug mode by editing `smapi-internal/config.json` in your game folder._ + * Optimized performance and memory usage (thanks to atravita!). + * Other internal optimizations. + * Added more file extensions to ignore when searching for mod folders: `.7z`, `.tar`, `.tar.gz`, and `.xcf` (thanks to atravita!). + * Removed transitional `UseRawImageLoading` option added in 3.15.0. This is now always enabled, except when PyTK is installed. + * Fixed update alerts incorrectly shown for prerelease versions on GitHub that aren't marked as prerelease. + +* For mod authors: + * When [providing a mod API in a C# mod](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations), you can now get the mod requesting it as an optional parameter (thanks to KhloeLeclair!). + * SMAPI now treats square brackets in the manifest `Name` field as round ones to avoid breaking tools which parse log files. + * Made deprecation message wording stronger for the upcoming SMAPI 4.0.0 release. + * The `Texture2D.Name` field is now set earlier to support mods like SpriteMaster. + * Updated dependencies: [Harmony](https://harmony.pardeike.net) 2.2.2 (see [changes](https://github.com/pardeike/Harmony/releases/tag/v2.2.2.0)) and [FluentHttpClient](https://github.com/Pathoschild/FluentHttpClient#readme) 4.2.0 (see [changes](https://github.com/Pathoschild/FluentHttpClient/blob/develop/RELEASE-NOTES.md#420)). + * Fixed `LocationListChanged` event not raised & memory leak occurring when a generated mine/volcano is removed (thanks to tylergibbs2!). + +## 3.16.2 +Released 31 August 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed `NoSuitableGraphicsDeviceException` launch error for some players with compatible GPUs since 3.16.0. + +## 3.16.1 +Released 29 August 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Updated PyTK compatibility mode for the latest PyTK version. + * Fixed broken mods sometimes incorrectly listed as duplicate. + +## 3.16.0 +Released 22 August 2022 for Stardew Valley 1.5.6 or later. See [release highlights](https://www.patreon.com/posts/70797008). + +* For players: + * Added error message if mod files are detected directly under `Mods` (instead of each mod having its own subfolder). + * SMAPI now sets a success/error code when the game exits. + _This is used by your OS (like Windows) to decide whether to keep the console window open when the game ends._ + * Fixed SMAPI on Windows applying different DPI awareness settings than the game (thanks to spacechase0!). + * Fixed Linux/macOS installer's color scheme question partly unreadable if the terminal background is dark. + * Fixed error message when a mod loads an invalid PNG file (thanks to atravita!). + * Fixed error message when a mod is duplicated, but one of the copies is also missing the DLL file. This now shows the duplicate-mod message instead of the missing-DLL message. + * Fixed macOS launcher using Terminal regardless of the system's default terminal (thanks to ishan!). + * Fixed best practices in Linux/macOS launcher scripts (thanks to ishan!). + * Improved translations. Thanks to KediDili (updated Turkish)! + +* For mod authors: + * While loading your mod, SMAPI now searches for indirect dependencies in your mod's folder (thanks to TehPers)! This mainly enables F# mods. + * **Raised deprecation message levels.** + _Deprecation warnings are now player-visible in the SMAPI console as faded `DEBUG` messages._ + * Updated to Pintail 2.2.1 (see [changes](https://github.com/Nanoray-pl/Pintail/blob/master/docs/release-notes.md#221)). + * Switched SMAPI's `.pdb` files to the newer 'portable' format. This has no effect on mods. + +* For the web UI: + * Added log parser warning about performance of PyTK 1.23.0 or earlier. + * Converted images to SVG (thanks to ishan!). + * Updated log parser for the new update alert format in SMAPI 3.15.1. + * Updated the JSON validator/schema for Content Patcher 1.28.0. + * Fixed log parsing for invalid content packs. + * Fixed log parsing if a mod logged a null character. + +## 3.15.1 +Released 06 July 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Added current version to update alerts (thanks to ishan!). + * Fixed lag for some players since Stardew Valley 1.5.5. + * Fixed `smapi-internal/config.user.json` overrides not applied after SMAPI 3.14.0. + * Fixed PyTK not rescaling images correctly in some cases. + _When PyTK 1.23.0 or earlier is installed, this will disable the main performance improvements in SMAPI 3.15.0._ + * Updated compatibility list. + +* For mod authors: + * The [FluentHttpClient package](https://github.com/Pathoschild/FluentHttpClient#readme) is now loaded by SMAPI. + * Fixed `TRACE` logs not tracking reloaded map tilesheets as a propagated asset. + +* For the web UI: + * Added log parser suggested fix for missing/outdated Error Handler, and improved visual styles. + * Updated the JSON validator/schema for Content Patcher 1.27.0. + * Fixed the mod count in the log parser metadata. + +## 3.15.0 +Released 17 June 2022 for Stardew Valley 1.5.6 or later. See [release highlights](https://www.patreon.com/posts/67877219). + +* For players: + * Optimized mod image file loading. + * Minor optimizations (thanks to Michael Kuklinski / Ameisen!). + * Updated compatibility list. + +* For mod authors: + * Added an [`IRawTextureData` asset type](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0#Raw_texture_data), to avoid creating full `Texture2D` instances in many cases. + * In `smapi-internal/config.json`, you can now enable verbose logging for specific mods (instead of all or nothing). + * Updated dependencies: + * Harmony 2.2.1 (see changes in [2.2.0](https://github.com/pardeike/Harmony/releases/tag/v2.2.0.0) and [2.2.1](https://github.com/pardeike/Harmony/releases/tag/v2.2.1.0)); + * Newtonsoft.Json 13.0.1 (see [changes](https://github.com/JamesNK/Newtonsoft.Json/releases/tag/13.0.1)); + * Pintail 2.2.0 (see [changes](https://github.com/Nanoray-pl/Pintail/blob/master/docs/release-notes.md#220)). + * Removed transitional `UsePintail` option added in 3.14.0 (now always enabled). + * Fixed `onBehalfOf` arguments in the new content API being case-sensitive. + * Fixed map edits which change warps sometimes rebuilding the NPC pathfinding cache unnecessarily, which could cause a noticeable delay for players. + +## 3.14.7 +Released 01 June 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Optimized reflection cache to reduce frame skips for some players. + +* For mod authors: + * Removed `runtimeconfig.json` setting which impacted hot reload support. + +## 3.14.6 +Released 27 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed error in split-screen mode when a mod provides a localized asset in one screen but not another. + * Minor optimizations. + +## 3.14.5 +Released 22 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Improved performance when mods change some asset types (including NPC portraits/sprites). + * Fixed _could not find file_ error if a mod provides a localized version of a normally unlocalized asset and then stops providing it. + * Fixed CurseForge update checks for the new CurseForge API. + +## 3.14.4 +Released 15 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Improved performance for mods using deprecated APIs. + +* For mod authors: + * Removed warning for mods which use `dynamic`. + _This no longer causes errors on Linux/macOS after Stardew Valley 1.5.5._ + +## 3.14.3 +Released 12 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Reduced in-game performance impact. + +* For mod authors: + * Refactored how event handling works under the hood, particularly the new content API. This should have no effect on mod usage. + * Verbose mode now logs the in-game time. + * Fixed error when loading a `.xnb` file through the old content API without the file extension. + * Fixed asset propagation for player sprites not fully updating recolor masks in some cases. + +* For the web UI: + * Updated the JSON validator/schema for Content Patcher 1.26.0. + +## 3.14.2 +Released 08 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Enabled case-insensitive file paths by default for Android and Linux players. + _This was temporarily disabled in SMAPI 3.14.1, and will remain disabled by default on macOS and Windows since their filesystems are already case-insensitive._ + * Various performance improvements. +* For mod authors: + * Dynamic content packs created via `helper.ContentPacks.CreateTemporary` or `CreateFake` are now listed in the log file. + * Fixed assets loaded through a fake content pack not working correctly since 3.14.0. + +## 3.14.1 +Released 06 May 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Improved performance for mods still using the previous content API. + * Disabled case-insensitive file paths (introduced in 3.14.0) by default. + _You can enable them by editing `smapi-internal/config.json` if needed. They'll be re-enabled in an upcoming version after they're reworked a bit._ + * Removed experimental 'aggressive memory optimizations' option. + _This was disabled by default and is no longer needed in most cases. Memory usage will be better reduced by reworked asset propagation in the upcoming SMAPI 4.0.0._ + * Fixed 'content file was not found' error when the game tries to load unlocalized text from a localizable mod data asset in 3.14.0. + * Fixed error reading empty JSON files. These are now treated as if they didn't exist (matching pre-3.14.0 behavior). + * Updated compatibility list. + +## 3.14.0 +Released 01 May 2022 for Stardew Valley 1.5.6 or later. See [release highlights](https://www.patreon.com/posts/65265507). + +### For players +This is a big update, but existing mods should all work fine. If the latest version of a mod breaks in SMAPI 3.14, please report it [on the SMAPI mod page](https://www.nexusmods.com/stardewvalley/mods/2400?tab=posts). + +* Improvements: + * SMAPI now ignores dot-prefixed files when searching for mod folders (thanks to Nuztalgia!). + * On Linux, SMAPI now fixes many case-sensitive mod path issues automatically. + * On Linux/macOS, added `--use-current-shell` [command-line argument](technical/smapi.md#command-line-arguments) to avoid opening a separate terminal window. + * Improved performance in some cases. + * Improved translations. Thanks to ChulkyBow (updated Ukrainian)! + * Dropped update checks for the unofficial 64-bit patcher (obsolete since SMAPI 3.12.6). +* Fixes: + * Fixed some movie theater textures not translated when loaded through SMAPI (specifically assets with the `_international` suffix). + * Fixed the warning text when a mod causes an asset load conflict with itself. + * Fixed `--no-terminal` [command-line argument](technical/smapi.md#command-line-arguments) on Linux/macOS still opening a terminal window, even if nothing is logged to it (thanks to Ryhon0!). + * Fixed `player_add` console command not handling journal scraps and secret notes correctly. + * Fixed `set_farm_type` console command not updating warps. +* For the web UI: + * Improved log parser UI (thanks to KhloeLeclair!): + * Added pagination for big logs. + * Added search box to filter the log. + * Added option to show/hide content packs in the mod list. + * Added jump links in the sidebar. + * The filter options now stick to the top of the screen when scrolling. + * Rewrote rendering to improve performance. + +### For mod authors +This is a big release that includes the new features planned for SMAPI 4.0.0. + +For C# mod authors: your mods should still work fine in SMAPI 3.14.0. However you should review the [migration to SMAPI 4.0](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0) guide and update your mods when possible. Deprecated code will be removed when SMAPI 4.0.0 releases later this year (no sooner than August 2022), and break any mods which haven't updated by that time. You can update affected mods now, there's no need to wait for 4.0.0. + +For content pack authors: SMAPI 3.14.0 and 4.0.0 don't affect content packs. They should work fine as long as +the C# mod that loads them is updated. + +* Major changes: + * Added [content events](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Content), which will replace `IAssetEditor` and `IAssetLoader` in SMAPI 4.0.0. + _These include new features not supported by the old API like load conflict resolution, edit priority, and content pack labels. They also support new cases like easily detecting when an asset has changed, and avoid data corruption issues in some edge cases._ + * Added [nullable reference type annotations](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0#Nullable_reference_type_annotations) for all APIs. + * Added [`helper.GameContent` and `helper.ModContent`](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0#Content_loading_API), which will replace `helper.Content` in SMAPI 4.0.0. + * Improved [mod-provided API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations#Mod-provided_APIs) proxying (thanks to Shockah!). + _This adds support for custom interfaces in return values or input arguments, custom enums if their values match, generic methods, and more. This is an internal change, you don't need to do anything different in your mod code._ + * Mod files loaded through SMAPI APIs (including `helper.Content.Load`) are now case-insensitive, even on Linux. + * Enabled deprecation notices for all deprecated APIs. These will only be shown in `TRACE` logs for at least a month after SMAPI 3.14.0 releases. +* Other improvements: + * Added `IAssetDataForImage.ExtendMap` to resize maps in asset editors. + * Added `IContentPack.ModContent` property to manage content pack assets. + * Added `Constants.ContentPath` to get the full path to the game's `Content` folder. + * Added `IAssetName` fields to the info received by `IAssetEditor`, `IAssetLoader`, and content event methods. + _This adds methods for working with asset names, parsed locales, etc._ + * Added `helper.Content.ParseAssetName` to get an `IAssetName` for an arbitrary asset key. + * Added [command-line arguments](technical/smapi.md#command-line-arguments) to toggle developer mode (thanks to Tondorian!). + * If an asset is loaded multiple times in the same tick, `IAssetLoader.CanLoad` and `IAssetEditor.CanEdit` are now cached unless invalidated by `helper.Content.InvalidateCache`. + * The `ISemanticVersion` comparison methods (`CompareTo`, `IsBetween`, `IsNewerThan`, and `IsOlderThan`) now allow null values. A null version is always considered older than any non-null version per [best practices](https://docs.microsoft.com/en-us/dotnet/api/system.icomparable-1.compareto#remarks). + * Deprecation notices now show a shorter stack trace in most cases, so it's clearer where the deprecated code is in the mod. +* Fixes: + * Fixed the `SDate` constructor being case-sensitive. + * Fixed support for using locale codes from custom languages in asset names (e.g. `Data/Achievements.eo-EU`). + * Fixed issue where suppressing `[Left|Right]Thumbstick[Down|Left]` keys would suppress the opposite direction instead. + * Fixed null handling in various edge cases. +* For the web UI: + * Updated the JSON validator/schema for Content Patcher 1.25.0. + * Added `data-*` attributes to the log parser page for external tools. + * Fixed JSON validator showing incorrect error for update keys without a subkey. + +### For SMAPI contributors +* You no longer need a Nexus API key to launch the `SMAPI.Web` project locally. + +## 3.13.4 +Released 16 January 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * Fixed Linux/macOS launch error in 3.13.3. + +## 3.13.3 +Released 16 January 2022 for Stardew Valley 1.5.6 or later. + +* For players: + * **SMAPI now needs Stardew Valley 1.5.6 or later.** + * Added automatic fix for custom maps which are missing a required tilesheet. + * Added automatic save recovery when a custom farm type isn't available anymore. + * Added the game's new build number to the SMAPI console + log. + * The installer now detects Xbox app game folders. + * Reduced mod loading time a bit. + * Fixed macOS launch issue when using some terminals (thanks to bruce2409!). + * Fixed Linux/macOS terminal ignoring backspaces in Stardew Valley 1.5.5+. + * Fixed extra newlines in the SMAPI console. + * Fixed outdated instructions in Steam error message. + * Fixed uninstaller not removing `StardewModdingAPI.deps.json` file. + * Simplified [running without a terminal on Linux/macOS](https://stardewvalleywiki.com/Modding:Player_Guide/Troubleshooting#SMAPI_doesn.27t_recognize_controller_.28Steam_only.29) when needed. + * Updated compatibility list. + * Improved translations. Thanks to ChulkyBow (added Ukrainian)! + +* For the web UI: + * Added log instructions for Xbox app on Windows. + * Added log download option. + * Redesigned log instruction UI. + * Fixed log parser not correctly handling multiple mods having the exact same name. + * Fixed JSON validator not recognizing manifest [update subkeys](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Update_checks#Update_subkeys). + +## 3.13.2 +Released 05 December 2021 for Stardew Valley 1.5.5 or later. + +* For players: + * You no longer need .NET 5 to install or use SMAPI. + * The installer now detects when the game folder contains an incompatible legacy game version. + * Updated for the latest Stardew Valley 1.5.5 hotfix. + * Updated compatibility list. + +* For the web UI: + * Fixed the JSON validator marking `.fnt` files invalid in Content Patcher files. + +* For SMAPI maintainers: + * Added [release package scripts](technical/smapi.md) to streamline preparing SMAPI releases. + +## 3.13.1 +Released 30 November 2021 for Stardew Valley 1.5.5 or later. + +* For players: + * Improved .NET 5 validation in Windows installer to better explain how to get the right version. + * Fixed installer failing on Windows when run from the game folder. + +## 3.13.0 +Released 30 November 2021 for Stardew Valley 1.5.5 or later. See [release highlights](https://www.patreon.com/posts/59348226). + +* For players: + * Updated for Stardew Valley 1.5.5. + * Added `set_farm_type` [console command](https://stardewvalleywiki.com/Modding:Console_commands#Console_commands) to change the current farm type. + * Fixed installer window closing immediately if the installer crashed. + * Updated compatibility list. + +* For mod authors: + * Migrated to 64-bit MonoGame and .NET 5 on all platforms (see [migration guide for mod authors](https://stardewvalleywiki.com/Modding:Migrate_to_Stardew_Valley_1.5.5)). + * Added support for [map overlays via `asset.AsMap().PatchMap`](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Content#Edit_a_map). + * Added support for loading BmFont `.fnt` files for [custom languages](https://stardewvalleywiki.com/Modding:Custom_languages) through the [content API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Content). + +* For the web UI: + * Updated the JSON validator/schema for Content Patcher 1.24.0. + +**Update note for players with older systems:** +The game now has two branches: the _main branch_ which you'll get by default, and an optional +[_compatibility branch_ for older systems](https://www.stardewvalley.net/compatibility/). The two +branches have identical content, but use [different technologies](https://stardewvalleywiki.com/Modding:Migrate_to_Stardew_Valley_1.5.5#Game_compatibility_branch). + +Unfortunately **SMAPI only supports the main branch of the game**. There are formidable difficulties +across all mods in supporting all three variations, 32-bit imposes significant restrictions on what +mods can do, and the [Steam hardware stats](https://store.steampowered.com/hwsurvey) show that 99.69% +of players now have 64-bit. + +## 3.12.8 +Released 18 October 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed mod edits to the farmhouse shifting the player down one tile in some cases. + * Fixed map tile rotations/flips not working for farmhands in split-screen mode. + * Improved translations. Thanks to ellipszist (added Thai) and Zangorr (added Polish)! + _These are custom languages which require Stardew Valley 1.5.5 and the [Polish](https://www.nexusmods.com/stardewvalley/mods/3616) or [Thai](https://www.nexusmods.com/stardewvalley/mods/7052) mod._ + +* For mod authors: + * SMAPI now intercepts dictionary duplicate-key errors and adds the key to the error message to simplify troubleshooting. (Due to Harmony limitations, this only works for the dictionary types used by the game.) + * Fixed barn/coop exit warps being reset when you edit their interior map. + +* For the web UI: + * Added support for unified [mod data overrides](https://stardewvalleywiki.com/Modding:Mod_compatibility#Mod_data_overrides) defined on the wiki. + * The mod compatibility list now shows separate beta stats when 'show advanced info' is enabled. + +## 3.12.7 +Released 18 September 2021 for Stardew Valley 1.5.4. + +* For players: + * Added more progress updates in the log during startup. + * Simplified asset load error message. + * Simplified exception logs. + * Fixed crash loading mods with corrupted translation files. + +* For mod authors: + * Added asset propagation for `LooseSprites\Giftbox`. + * Improved SMAPI's crossplatform read/writing of `Color`, `Point`, `Rectangle`, and `Vector2` in JSON to support nullable fields too. + +* For the web UI: + * The mod compatibility list now shows the beta status by default (if any). + * Fixed JSON validator line numbers sometimes incorrect. + +## 3.12.6 +Released 03 September 2021 for Stardew Valley 1.5.4. + +* For players: + * Added friendly error when using SMAPI 3.2._x_ with Stardew Valley 1.5.5 or later. + * Improved mod compatibility in 64-bit mode (thanks to spacechase0!). + * Reduced load time when scanning/rewriting many mods for compatibility. + * **Dropped support for unofficial 64-bit mode**. You can now use the [official 64-bit Stardew Valley 1.5.5 beta](https://stardewvalleywiki.com/Modding:Migrate_to_64-bit_on_Windows) instead. + * Updated compatibility list. + +* For mod authors: + * Added `PathUtilities.NormalizeAssetName` and `PathUtilities.PreferredAssetSeparator` to prepare for the upcoming Stardew Valley 1.5.5. + * **SMAPI no longer propagates changes to `Data/Bundles`.** + _You can still load/edit the asset like usual, but if bundles have already been loaded for a save, SMAPI will no longer dynamically update the in-game bundles to reflect the changes. Unfortunately this caused bundle corruption when playing in non-English._ + * Fixed content packs created via `helper.ContentPacks.CreateFake` or `CreateTemporary` not initializing translations correctly. + +* For console commands: + * Added `hurry_all` command which immediately warps all NPCs to their scheduled positions. + +**Update note for mod authors:** +Stardew Valley 1.5.5 will change how asset names are formatted. If you use `PathUtilities.NormalizePath` +to format asset names, you should switch to `PathUtilities.NormalizeAssetName` now so your code will +continue working in the next game update. + +## 3.12.5 +Released 26 August 2021 for Stardew Valley 1.5.4. + +* Fixed some mods in unofficial 64-bit mode no longer loading after SMAPI 3.12.3. + +## 3.12.4 +Released 25 August 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed error loading some mods in SMAPI 3.12.3. + +## 3.12.3 +Released 25 August 2021 for Stardew Valley 1.5.4. + +* For players: + * Added friendly error in 64-bit mode when a mod is 32-bit only. + * Fixed console encoding issues on Linux/macOS. + * Fixed some installer errors not showing info header. + +* For mod authors: + * Added `helper.Translation.GetInAllLocales` to get a translation in every available locale. + * Fixed Visual Studio debugger crash when any mods are rewritten for compatibility (thanks to spacechase0!). + * Fixed `helper.Data.WriteJsonFile` not deleting the file if the model is null, unlike the other `Write*` methods. + * Fixed error-handling for `StackOverflowException` thrown on Linux/macOS. + * Internal changes to prepare for Stardew Valley 1.5.5. + +* For the web API: + * Fixed update checks not shown for prerelease mod versions when you have a SMAPI beta. + * Fixed update checks shown for prerelease mod versions if you have a working non-prerelease version. + +## 3.12.2 +Released 05 August 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed error creating a new save or joining a multiplayer world in 3.12.1. + +* For mod authors: + * Reverted the `Constants.Save*` fix in SMAPI 3.12.1. + _The change caused a number of other issues, and is only needed for rare cases where the save folder was invalid. This may be revisited in a future version instead._ + * Fixed `NullReferenceException` in SMAPI's error-handling when trying to handle an invalid `ReflectionTypeLoadException`. + +## 3.12.1 +Released 03 August 2021 for Stardew Valley 1.5.4. + +* For players: + * The software conflict message is now shown as a warning to simplify troubleshooting. + * Fixed error loading older Harmony mods for some Windows players using unofficial 64-bit Stardew Valley. + * Updated compatibility list. + +* For mod authors: + * Fixed `Constants.Save*` fields incorrect if the save's folder name and ID don't match. + +## 3.12.0 +Released 01 August 2021 for Stardew Valley 1.5.4. See [release highlights](https://www.patreon.com/posts/54388616). + +* For players: + * Added save recovery when content mods leave null objects in the save (in _Error Handler_). + * Added error if the wrong SMAPI bitness is installed (e.g. 32-bit SMAPI with 64-bit game). + * Added error if some SMAPI files aren't updated correctly. + * Added `removable` option to the `world_clear` console command (in _Console Commands_, thanks to bladeoflight16!). + * Fixed handling of Unicode characters in console commands. + * Fixed intermittent error if a mod gets mod-provided APIs asynchronously. + * Fixed crash when creating a farm name containing characters that aren't allowed in a folder path. + +* For mod authors: + * **Updated Harmony 1.2.0.1 to 2.1.0 (see [_migrate to Harmony 2.0_](https://stardewvalleywiki.com/Modding:Migrate_to_Harmony_2.0) for more info).** + * SMAPI now intercepts `KeyNotFoundException` errors and adds the key to the error message to simplify troubleshooting. (Due to Harmony limitations, this only works for the dictionary types used by the game.) + * Fixed error loading `.xnb` files from the local mod folder. + * Fixed reloading a map not correctly reapplying interior doors. + +## 3.11.0 +Released 09 July 2021 for Stardew Valley 1.5.4. See [release highlights](https://www.patreon.com/posts/53514295). + +* For players: + * Updated for Stardew Valley 1.4.5 multiplayer hotfix on Linux/macOS. + * Fixed installer error on Windows when running as administrator (thanks to LostLogic!). + * Fixed installer error on some Windows systems (thanks to eddyballs!). + * Fixed error if SMAPI fails to dispose on game exit. + * Fixed `player_add` and `list_items` console commands not including some shirts _(in Console Commands)_. + +* For mod authors: + * Added `World.FurnitureListChanged` event (thanks to DiscipleOfEris!). + * Added asset propagation for building/house paint masks. + * Added log message for troubleshooting if Windows software which often causes issues is installed (currently MSI Afterburner and RivaTuner). + * Improved validation for the manifest `Dependencies` field. + * Fixed validation for mods with invalid version `0.0.0`. + * Fixed _loaded with custom settings_ trace log added when using default settings. + * Fixed `Constants.SaveFolderName` and `Constants.CurrentSavePath` not set correctly in rare cases. + +* For the web UI and JSON validator: + * Updated the JSON validator/schema for Content Patcher 1.23.0. + * Fixed [JSON schema](technical/web.md#using-a-schema-file-directly) in Visual Studio Code warning about comments and trailing commas. + * Fixed JSON schema for `i18n` files requiring the wrong value for the `$schema` field. + +## 3.10.1 +Released 03 May 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed installer leaving an unneeded `StardewModdingAPI-x64.exe` file in 32-bit game folders. + +## 3.10 +Released 03 May 2021 for Stardew Valley 1.5.4. See [release highlights](https://www.patreon.com/posts/50764911). + +* For players: + * Added full support for the [unofficial 64-bit Stardew Valley patch](https://stardewvalleywiki.com/Modding:Migrate_to_64-bit_on_Windows), which removes memory limits. The installer detects which version of SMAPI you need, and SMAPI shows update alerts for Stardew64Installer if applicable. + * Added smarter grouping for skipped mods, so it's easier to see root dependencies to update first. + * Added crash recovery when the game can't update a map's seasonal tilesheets _(in Error Handler)_. SMAPI will log an error and keep the previous tilesheets in that case. + * Added installer option to enter a custom game path even if it detected a game folder. + * `*.ico` files are now ignored when scanning for mods. + * Fixed error for non-English players after returning to title, reloading, and entering town with a completed movie theater. + * Fixed `world_clear` console command not removing resource clumps outside the farm and secret woods. + * Fixed error running SMAPI in a strict sandbox on Linux (thanks to kuesji!). + * Fixed `StardewModdingAPI.bin.osx` on macOS overwritten with an identical file on launch which would reset file permissions (thanks to 007wayne!). + * Fixed inconsistent spelling/style for 'macOS'. + +* For modders: + * Added support for [ignoring local map tilesheet files when loading a map](https://stardewvalleywiki.com/Modding:Maps#Local_copy_of_a_vanilla_tilesheet). + * Added asset propagation for `Data\Concessions`. + * Added SMAPI version and bitness to the console title before startup to simplify troubleshooting. + * If a map loads a tilesheet path with no file extension, SMAPI now automatically links it to a `.png` version in the map folder if possible. + * Improved error-handling during asset propagation. + * Fixed `Context.IsMainPlayer` returning true for a farmhand in split-screen mode before the screen is initialized. + * Fixed error when editing bundle data while a split-screen player is joining. + * Fixed update subkeys not working in file descriptions for Nexus mods marked as adult content. + +## 3.9.5 +Released 21 March 2021 for Stardew Valley 1.5.4. + +* For players: + * Added console command to reset community center bundles _(in Console Commands)_. + * Disabled aggressive memory optimization by default. + _The option was added in SMAPI 3.9.2 to reduce errors for some players, but it can cause multiplayer crashes with some mods. If you often see `OutOfMemoryException` errors, you can edit `smapi-internal/config.json` to re-enable it. We're experimenting with making Stardew Valley 64-bit to address memory issues more systematically._ + * Fixed bundles corrupted in non-English saves created after SMAPI 3.9.2. + _If you have an affected save, you can load your save and then enter the `regenerate_bundles confirm` command in the SMAPI console to fix it._ + * Internal changes to prepare for unofficial 64-bit. + +* For mod authors: + * Improved asset propagation: + * Added for interior door sprites. + * SMAPI now updates the NPC pathfinding cache when map warps are changed through the content API. + * Reduced performance impact of invalidating cached assets before a save is loaded. + * Fixed asset changes not reapplied in the edge case where you're playing in non-English, and the changes are only applied after the save is loaded, and the player returns to title and reloads a save, and the game reloads the target asset before the save is loaded. + * Added a second `KeybindList` constructor to simplify single-key default bindings. + * Added a `Constants.GameFramework` field which indicates whether the game is using XNA Framework or MonoGame. + _Note: mods don't need to handle the difference in most cases, but some players may use MonoGame on Windows in upcoming versions. Mods which check `Constants.TargetPlatform` should review usages as needed._ + +## 3.9.4 +Released 07 March 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed installer error if the `Mods` folder doesn't exist in 3.9.3. + +## 3.9.3 +Released 07 March 2021 for Stardew Valley 1.5.4. + +* For players: + * Added descriptive error if possible when a `PathTooLongException` crashes SMAPI or the installer. + * The installer window now tries to stay open if it crashed, so you can read the error and ask for help. + * Fixed console showing _found 1 mod with warnings_ with no mods listed in some cases. + +* For mod authors: + * Added three stages to the specialised [`LoadStageChanged` event](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Specialised): `CreatedInitialLocations`, `SaveAddedLocations`, and `ReturningToTitle`. + * Fixed `RewriteMods` option ignored when rewriting for OS compatibility. + * Fixed edge case when playing as a farmhand in non-English where translatable assets loaded via `IAssetLoader` weren't reapplied immediately when the server disconnects. + +* For the web UI: + * Updated the JSON validator/schema for Content Patcher 1.21. + +## 3.9.2 +Released 21 February 2021 for Stardew Valley 1.5.4. + +* For players: + * Added more aggressive memory optimization to reduce `OutOfMemoryException` errors with some mods. + * Improved error when `Stardew Valley.exe` exists but can't be loaded. + * Fixed error running `install on Windows.bat` in very rare cases. + * Fixed `world_settime` command not always updating outdoor ambient lighting _(in Console Commands)_. + +* For mod authors: + * Added early detection of disposed textures so the error details are more relevant _(in Error Handler)_. + * Added error details when an event command fails _(in Error Handler)_. + * Fixed asset propagation for `TileSheets/ChairTiles` not changing existing map seats. + * Fixed edge case when playing in non-English where translatable assets loaded via `IAssetLoader` would no longer be applied after returning to the title screen unless manually invalidated from the cache. + +* For the web UI: + * Updated compatibility list for the new wiki. + * Updated the JSON validator/schema for Content Patcher 1.20. + * Fixed mod compatibility list error if a mod has no name. + +* For SMAPI developers: + * Fixed SMAPI toolkit defaulting the mod type incorrectly if a mod's `manifest.json` has neither `EntryDll` nor `ContentPackFor`. This only affects external tools, since SMAPI itself validates those fields separately. + +## 3.9.1 +Released 25 January 2021 for Stardew Valley 1.5.4. + +* For players: + * Fixed _tile contains an invalid TileSheet reference_ crash after mods change certain maps. + * Fixed _patched game code_ issue shown for the bundled Error Handler mod. + +## 3.9 +Released 22 January 2021 for Stardew Valley 1.5.4. See [release highlights](https://www.patreon.com/posts/46553874). + +* For players: + * Updated for Stardew Valley 1.5.4. + * Improved game detection in the installer: + * The installer now prefers paths registered by Steam or GOG Galaxy. + * The installer now detects default manual GOG installs. + * Added clearer error text for empty mod folders created by Vortex. + * Fixed the game's map changes not always reapplied correctly after mods change certain maps, which caused issues like the community center resetting to its non-repaired texture. + * Fixed compatibility for very old content packs which still load maps from `.xnb` files. These were broken by map loading changes in Stardew Valley 1.5, but SMAPI now corrects them automatically. + * Fixed some broken mods incorrectly listed as XNB mods under 'skipped mods'. + +* For mod authors: + * Added new input APIs: + * Added an [API for multi-key bindings](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Input#KeybindList). + * Added a new [`Input.ButtonsChanged` event](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Input.ButtonsChanged). + * Added a `buttonState.IsDown()` extension. + * Added a `helper.Input.SuppressActiveKeybinds` method to suppress the active buttons in a keybind list. + * Improved multiplayer APIs: + * `PerScreen` now lets you get/set the value for any screen, get all active values, or clear all values. + * Peer data from the multiplayer API/events now includes `IsSplitScreen` and `ScreenID` fields. + * Fixed network messages through the multiplayer API being sent to players who don't have SMAPI installed in some cases. + * Improved asset propagation: + * Updated map propagation for the changes in Stardew Valley 1.5.4. + * Added propagation for some `Strings\StringsFromCSFiles` keys (mainly short day names). + * Fixed quarry bridge not fixed if the mountain map was reloaded. + * Added an option to disable rewriting mods for compatibility (thanks to Bpendragon!). This prevents older mods from loading, but bypasses a Visual Studio crash when debugging. + * Game errors shown in the chatbox are now logged. + * Moved vanilla error-handling into a new Error Handler mod. This simplifies the core SMAPI logic, and lets users disable it if needed. + +* For the Console Commands mod: + * Removed the `inf` option for `player_sethealth`, `player_setmoney`, and `player_setstamina`. You can use mods like [CJB Cheats Menu](https://www.nexusmods.com/stardewvalley/mods/4) instead for that. + +* For the Error Handler mod: + * Added a detailed message for the _Input string was not in a correct format_ error when the game fails to parse an item text description. + +* For the web UI: + * Fixed JSON validator incorrectly marking some manifest update keys as invalid. + +## 3.8.4 +Released 15 January 2021 for Stardew Valley 1.5.3 or later. + +* For players: + * Updated for Stardew Valley 1.5.3. + * Fixed issue where title screen music didn't stop after loading a save. + +* For mod authors: + * Fixed `SemanticVersion` comparisons returning wrong value in rare cases. + +## 3.8.3 +Released 08 January 2021 for Stardew Valley 1.5.2 or later. + +* For players: + * Updated for Stardew Valley 1.5.2. + * Reduced memory usage. + * You can now enter console commands for a specific screen in split-screen mode by adding `screen=ID` to the command. + * Typing `help` in the SMAPI console is now more helpful. + +* For mod authors: + * Simplified tilesheet order warning added in SMAPI 3.8.2. + +* For the Console Commands mod: + * Removed experimental `performance` command. Unfortunately this impacted SMAPI's memory usage and performance, and the data was often misinterpreted. This may be replaced with more automatic performance alerts in a future version. + +## 3.8.2 +Released 03 January 2021 for Stardew Valley 1.5.1 or later. + +* For players: + * SMAPI now blocks farm map replacements that would crash the game in Stardew Valley 1.5. + * On Linux, the SMAPI installer now auto-detects Flatpak Steam paths. + * Updated compatibility list. + * Fixed errors when multiple players join in split-screen mode. + * Fixed 'skipped mods' section repeating mods in some cases. + * Fixed out-of-date error text. + +* For mod authors: + * Added warning when a map replacement changes the order/IDs of the original tilesheets, which may cause errors and crashes. Doing so for a farm map is blocked outright since that causes a consistent crash in Stardew Valley 1.5. + * Message data from the `ModMessageReceived` event now uses the same serializer settings as the rest of SMAPI. That mainly adds support for sending crossplatform `Color`, `Point`, `Vector2`, `Rectangle`, and `SemanticVersion` fields through network messages. + * When a mod is blocked by SMAPI's compatibility override list, the `TRACE` messages while loading it now say so and indicate why. + * Fixed how the input API handles UI scaling. This mainly affects `ICursorPosition` values returned by the API; see [the wiki docs](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Input#ICursorPosition) for how to account for UI scaling. + +## 3.8.1 +Released 26 December 2020 for Stardew Valley 1.5.1 or later. + +* For players: + * Fixed broken community center bundles for non-English saves created in Stardew Valley 1.5. Affected saves will be fixed automatically on load. + +* For mod authors: + * World events are now raised for volcano dungeon levels. + * Added `apply_save_fix` command to reapply a save migration in exceptional cases. This should be used very carefully. Type `help apply_save_fix` for details. + * **Deprecation notice:** the `Helper.ConsoleCommands.Trigger` method is now deprecated and should no longer be used. See [integration APIs](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations) for better mod integration options. It will eventually be removed in SMAPI 4.0. + +For the web UI: + * Fixed edge cases in SMAPI log parsing. + +## 3.8 +Released 21 December 2020 for Stardew Valley 1.5 or later. See [release highlights](https://www.patreon.com/posts/45294737). + +* For players: + * Updated for Stardew Valley 1.5, including split-screen support. + * You can now run the installer from a subfolder of your game folder to auto-detect it. That simplifies installation if you have multiple copies of the game or it can't otherwise auto-detect the game path. + * Clarified error when the SMAPI installer is in the `Mods` folder. + +* For mod authors: + * Added `PerScreen` utility and new `Context` fields to simplify split-screen support in mods. + * Added screen ID to log when playing in split-screen mode. + +* For the Console Commands mod: + * Added `furniture` option to `world_clear`. + +* For the web UI: + * Updated the JSON validator/schema for Content Patcher 1.19. + ## 3.7.6 Released 21 November 2020 for Stardew Valley 1.4.1 or later. @@ -14,7 +762,7 @@ Released 21 November 2020 for Stardew Valley 1.4.1 or later. * Fixed error when heuristically rewriting an outdated mod in rare cases. * Fixed rare 'collection was modified' error when using `harmony summary` console command. -* For modders: +* For mod authors: * Updated TMXTile to 1.5.8 to fix exported `.tmx` files losing tile index properties. * For the Console Commands mod: @@ -24,7 +772,7 @@ Released 21 November 2020 for Stardew Valley 1.4.1 or later. ## 3.7.5 Released 16 October 2020 for Stardew Valley 1.4.1 or later. -* For modders: +* For mod authors: * Fixed changes to the town map asset not reapplying the game's community center, JojaMart, and Pam house changes. ## 3.7.4 @@ -34,7 +782,7 @@ Released 03 October 2020 for Stardew Valley 1.4.1 or later. * Improved performance on some older computers (thanks to millerscout!). * Fixed update alerts for Chucklefish forum mods broken by a recent site change. -* For modders: +* For mod authors: * Updated dependencies (including Mono.Cecil 0.11.2 → 0.11.3 and Platonymous.TMXTile 1.3.8 → 1.5.6). * Fixed asset propagation for `Data\MoviesReactions`. * Fixed error in content pack path handling when you pass a null path. @@ -49,11 +797,11 @@ Released 03 October 2020 for Stardew Valley 1.4.1 or later. Released 16 September 2020 for Stardew Valley 1.4.1 or later. * For players: - * Fixed errors on Linux/Mac due to content packs with incorrect filename case. + * Fixed errors on Linux/macOS due to content packs with incorrect filename case. * Fixed map rendering crash due to conflict between SMAPI and PyTK. * Fixed error in heuristically-rewritten mods in rare cases (thanks to collaboration with ZaneYork!). -* For modders: +* For mod authors: * File paths accessed through `IContentPack` are now case-insensitive (even on Linux). * For the web UI: @@ -65,7 +813,7 @@ Released 08 September 2020 for Stardew Valley 1.4.1 or later. * For players: * Fixed mod recipe changes not always applied in 3.7. -* For modders: +* For mod authors: * Renamed `PathUtilities.NormalizePathSeparators` to `NormalizePath`, and added normalization for more cases. ## 3.7.1 @@ -90,10 +838,10 @@ Released 07 September 2020 for Stardew Valley 1.4.1 or later. See [release highl * Removed the experimental `RewriteInParallel` option added in SMAPI 3.6 (it was already disabled by default). Unfortunately this caused intermittent and unpredictable errors when enabled. * Internal changes to prepare for upcoming game updates. -* For modders: +* For mod authors: * Added `PathUtilities` to simplify working with file/asset names. * You can now read/write `SDate` values to JSON (e.g. for `config.json`, network mod messages, etc). - * Fixed asset propagation not updating title menu buttons immediately on Linux/Mac. + * Fixed asset propagation not updating title menu buttons immediately on Linux/macOS. * For the web UI: * Updated the JSON validator/schema for Content Patcher 1.16 and 1.17. @@ -120,7 +868,7 @@ Released 02 August 2020 for Stardew Valley 1.4.1 or later. * Fixed spawned Floor TV not functional as a TV (thanks to Platonymous!). * Fixed spawned sturgeon roe having incorrect color. -* For modders: +* For mod authors: * Updated internal dependencies. * SMAPI now ignores more file types when scanning for mod folders (`.doc`, `.docx`, `.rar`, and `.zip`). * Added current GPU to trace logs to simplify troubleshooting. @@ -138,7 +886,7 @@ Released 20 June 2020 for Stardew Valley 1.4.1 or later. See [release highlights * Added experimental option to reduce startup time when loading mod DLLs (thanks to ZaneYork!). Enable `RewriteInParallel` in the `smapi-internal/config.json` to try it. * Reduced processing time when a mod loads many unpacked images (thanks to Entoarox!). * Mod load warnings are now listed alphabetically. - * MacOS files starting with `._` are now ignored and can no longer cause skipped mods. + * macOS files starting with `._` are now ignored and can no longer cause skipped mods. * Simplified paranoid warning logs and reduced their log level. * Fixed black maps on Android for mods which use `.tmx` files. * Fixed `BadImageFormatException` error detection. @@ -153,7 +901,7 @@ Released 20 June 2020 for Stardew Valley 1.4.1 or later. See [release highlights * Updated ModDrop URLs. * Internal changes to improve performance and reliability. -* For modders: +* For mod authors: * Added [event priorities](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Events#Custom_priority) (thanks to spacechase0!). * Added [update subkeys](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Update_checks#Update_subkeys). * Added [a custom build of Harmony](https://github.com/Pathoschild/Harmony#readme) to provide more useful stack traces in error logs. @@ -188,13 +936,13 @@ Released 27 April 2020 for Stardew Valley 1.4.1 or later. See [release highlight * Updated the JSON validator/schema for Content Patcher 1.13. * Fixed rare intermittent "CGI application encountered an error" errors. -* For modders: +* For mod authors: * Added map patching to the content API (via `asset.AsMap()`). * Added support for using patch helpers with arbitrary data (via `helper.Content.GetPatchHelper`). * Added `SDate` fields/methods: `SeasonIndex`, `FromDaysSinceStart`, `FromWorldDate`, `ToWorldDate`, and `ToLocaleString` (thanks to kdau!). * Added `SDate` translations taken from the Lookup Anything mod.¹ * Fixed asset propagation for certain maps loaded through temporary content managers. This notably fixes unreliable patches to the farmhouse and town maps. - * Fixed asset propagation on Linux/Mac for monster sprites, NPC dialogue, and NPC schedules. + * Fixed asset propagation on Linux/macOS for monster sprites, NPC dialogue, and NPC schedules. * Fixed asset propagation for NPC dialogue sometimes causing a spouse to skip marriage dialogue or not allow kisses. ¹ Date format translations were taken from the Lookup Anything mod; thanks to translators FixThisPlz (improved Russian), LeecanIt (added Italian), pomepome (added Japanese), S2SKY (added Korean), Sasara (added German), SteaNN (added Russian), ThomasGabrielDelavault (added Spanish), VincentRoth (added French), Yllelder (improved Spanish), and yuwenlan (added Chinese). Some translations for Korean, Hungarian, and Turkish were derived from the game translations. @@ -202,7 +950,7 @@ Released 27 April 2020 for Stardew Valley 1.4.1 or later. See [release highlight ## 3.4.1 Released 24 March 2020 for Stardew Valley 1.4.1 or later. -* For modders: +* For mod authors: * Asset changes now propagate to NPCs in an event (e.g. wedding sprites). * Fixed mouse input suppression not working in SMAPI 3.4. @@ -210,12 +958,12 @@ Released 24 March 2020 for Stardew Valley 1.4.1 or later. Released 22 March 2020 for Stardew Valley 1.4.1 or later. See [release highlights](https://www.patreon.com/posts/35161371). * For players: - * Fixed semi-transparency issues on Linux/Mac in recent versions of Mono (e.g. pink shadows). + * Fixed semi-transparency issues on Linux/macOS in recent versions of Mono (e.g. pink shadows). * Fixed `player_add` command error if you have broken XNB mods. * Removed invalid-location check now handled by the game. * Updated translations. Thanks to Annosz (added Hungarian)! -* For modders: +* For mod authors: * Added support for flipped and rotated map tiles (thanks to collaboration with Platonymous!). * Added support for `.tmx` maps using zlib compression (thanks to Platonymous!). * Added `this.Monitor.LogOnce` method. @@ -252,14 +1000,14 @@ Released 22 February 2020 for Stardew Valley 1.4.1 or later. See [release highli * Updated translations. Thanks to xCarloC (added Italian)! * For the Save Backup mod: - * Fixed warning on MacOS when you have no saves yet. + * Fixed warning on macOS when you have no saves yet. * Reduced log messages. * For the web UI: * Updated the JSON validator and Content Patcher schema for `.tmx` support. * The mod compatibility page now has a sticky table header. -* For modders: +* For mod authors: * Added support for [message sending](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations#Message_sending) to mods on the current computer (in addition to remote computers). * Added `ExtendImage` method to content API when editing files to resize textures. * Added `helper.Input.GetState` to get the low-level state of a button. @@ -294,7 +1042,7 @@ Released 01 February 2020 for Stardew Valley 1.4.1 or later. See [release highli * Fixed extra files under `Saves` (e.g. manual backups) not being ignored. * Fixed Android issue where game files were backed up. -* For modders: +* For mod authors: * Added support for `.tmx` map files. (Thanks to [Platonymous for the underlying library](https://github.com/Platonymous/TMXTile)!) * Added special handling for `Vector2` values in `.json` files, so they work consistently crossplatform. * Reworked the order that asset editors/loaders are called between multiple mods to support some framework mods like Content Patcher and Json Assets. Note that the order is undefined and should not be depended on. @@ -343,7 +1091,7 @@ Released 05 January 2019 for Stardew Valley 1.4.1 or later. See [release highlig * Fixed log parser not correctly handling content packs with no author (thanks to danvolchek!). * Fixed main sidebar link pointing to wiki instead of home page. -* For modders: +* For mod authors: * Added `World.ChestInventoryChanged` event (thanks to collaboration with wartech0!). * Added asset propagation for... * grass textures; @@ -372,7 +1120,7 @@ Released 02 December 2019 for Stardew Valley 1.4 or later. * If a log can't be uploaded to Pastebin (e.g. due to rate limits), it's now uploaded to Amazon S3 instead. Logs uploaded to S3 expire after one month. * Fixed JSON validator not letting you drag & drop a file. -* For modders: +* For mod authors: * `SemanticVersion` now supports [semver 2.0](https://semver.org/) build metadata. ## 3.0 @@ -402,7 +1150,7 @@ For players: * **Fixed many bugs and edge cases.** -For modders: +For mod authors: * **New event system.** SMAPI 3.0 removes the deprecated static events in favor of the new `helper.Events` API. The event engine is rewritten to make events more efficient, add events that weren't possible before, make @@ -486,7 +1234,7 @@ For modders: * Added instructions for Android. * The page now detects your OS and preselects the right instructions (thanks to danvolchek!). -### For modders +### For mod authors * Breaking changes: * Mods are now loaded much earlier in the game launch. This lets mods intercept any content asset, but the game is not fully initialized when `Entry` is called; use the `GameLaunched` event if you need to run code when the game is initialized. * Removed all deprecated APIs. diff --git a/docs/technical/mod-package.md b/docs/technical/mod-package.md index f7475e37..23f0b221 100644 --- a/docs/technical/mod-package.md +++ b/docs/technical/mod-package.md @@ -1,7 +1,7 @@ ← [SMAPI](../README.md) The **mod build package** is an open-source NuGet package which automates the MSBuild configuration -for SMAPI mods and related tools. The package is fully compatible with Linux, Mac, and Windows. +for SMAPI mods and related tools. The package is fully compatible with Linux, macOS, and Windows. ## Contents * [Use](#use) @@ -29,20 +29,19 @@ change how these work): * **Detect game path:** The package automatically finds your game folder by scanning the default install paths and Windows registry. It adds two MSBuild properties for use in your `.csproj` file if needed: - `$(GamePath)` and `$(GameExecutableName)`. + `$(GamePath)` and `$(GameModsPath)`. * **Add assembly references:** - The package adds assembly references to SMAPI, Stardew Valley, xTile, and the game framework - (MonoGame on Linux/Mac, XNA Framework on Windows). It automatically adjusts depending on which OS - you're compiling it on. If you use [Harmony](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Harmony), - it can optionally add a reference to that too. + The package adds assembly references to MonoGame, SMAPI, Stardew Valley, and xTile. It + automatically adjusts depending on which OS you're compiling it on. If you use + [Harmony](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Harmony), it can optionally add + a reference to that too. * **Copy files into the `Mods` folder:** The package automatically copies your mod's DLL and PDB files, `manifest.json`, [`i18n` - files](https://stardewvalleywiki.com/Modding:Translations) (if any), the `assets` folder (if - any), and [build output](https://stackoverflow.com/a/10828462/262123) into your game's `Mods` - folder when you rebuild the code, with a subfolder matching the mod's project name. That lets you - try the mod in-game right after building it. + files](https://stardewvalleywiki.com/Modding:Translations) (if any), and the `assets` folder (if + any) into the `Mods` folder when you rebuild the code, with a subfolder matching the mod's project + name. That lets you try the mod in-game right after building it. * **Create release zip:** The package adds a zip file in your project's `bin` folder when you rebuild the code, in the @@ -55,7 +54,7 @@ change how these work): breakpoints](https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019) in your code while the game is running, or [make simple changes to the mod code without needing to restart the game](https://docs.microsoft.com/en-us/visualstudio/debugger/edit-and-continue?view=vs-2019). - This is disabled on Linux/Mac due to limitations with the Mono wrapper. + This is disabled on Linux/macOS due to limitations with the Mono wrapper. * **Preconfigure common settings:** The package automatically enables `.pdb` files (so error logs show line numbers to simplify @@ -82,7 +81,7 @@ There are two places you can put them: 1. Open the home folder on your computer (see instructions for [Linux](https://superuser.com/questions/409218/where-is-my-users-home-folder-in-ubuntu), - [MacOS](https://www.cnet.com/how-to/how-to-find-your-macs-home-folder-and-add-it-to-finder/), + [macOS](https://www.cnet.com/how-to/how-to-find-your-macs-home-folder-and-add-it-to-finder/), or [Windows](https://www.computerhope.com/issues/ch000109.htm)). 2. Create a `stardewvalley.targets` file with this content: ```xml @@ -129,14 +128,6 @@ The absolute path to the folder containing the game's installed mods (defaults t -GameExecutableName - - -The filename for the game's executable (i.e. `StardewValley.exe` on Linux/Mac or -`Stardew Valley.exe` on Windows). This is auto-detected, and you should almost never change this. - - - @@ -197,11 +188,63 @@ The folder path where the release zip is created (defaults to the project's `bin effect -CopyModReferencesToBuildOutput +BundleExtraAssemblies -Whether to copy game and framework DLLs into the mod folder (default `false`). This is useful for -unit test projects, but not needed for mods that'll be run through SMAPI. +**Most mods should not change this option.** + +By default (when this is _not_ enabled), only the mod files [normally considered part of the +mod](#Features) will be added to the release `.zip` and copied into the `Mods` folder (i.e. +"deployed"). That includes the assembly files (`*.dll`, `*.pdb`, and `*.xml`) for your mod project, +but any other DLLs won't be deployed. + +Enabling this option will add _all_ dependencies to the build output, then deploy _some_ of them +depending on the comma-separated value(s) you set: + + + + + + + + + + + + + + + + + + + + + + +
optionresult
ThirdParty + +Assembly files which don't match any other category. + +
System + +Assembly files whose names start with `Microsoft.*` or `System.*`. + +
Game + +Assembly files which are part of MonoGame, SMAPI, or Stardew Valley. + +
All + +Equivalent to `System, Game, ThirdParty`. + +
+ +Most mods should omit the option. Some mods may need `ThirdParty` if they bundle third-party DLLs +with their mod. The other options are mainly useful for unit tests. + +When enabling this option, you should **manually review which files get deployed** and use the +`IgnoreModFilePaths` or `IgnoreModFilePatterns` options to exclude files as needed. @@ -213,6 +256,20 @@ Whether to configure the project so you can launch or debug the game through the Visual Studio (default `true`). There's usually no reason to change this, unless it's a unit test project. + + + +IgnoreModFilePaths + + +A comma-delimited list of literal file paths to ignore, relative to the mod's `bin` folder. Paths +are case-sensitive, but path delimiters are normalized automatically. For example, this ignores a +set of tilesheets: + +```xml +assets/paths.png, assets/springobjects.png +``` + @@ -291,6 +348,15 @@ Warning text: Your code accesses a field which is obsolete or no longer works. Use the suggested field instead. +### Wrong processor architecture +Warning text: +> The target platform should be set to 'Any CPU' for compatibility with both 32-bit and 64-bit +> versions of Stardew Valley (currently set to '{{current platform}}'). + +Mods can be used in either 32-bit or 64-bit mode. Your project's target platform isn't set to the +default 'Any CPU', so it won't work in both. You can fix it by [setting the target platform to +'Any CPU'](https://docs.microsoft.com/en-ca/visualstudio/ide/how-to-configure-projects-to-target-platforms). + ## FAQs ### How do I set the game path? The package detects where your game is installed automatically, so you usually don't need to set it @@ -306,22 +372,21 @@ To do that: PATH_HERE ``` -3. Replace `PATH_HERE` with your game's folder path. +3. Replace `PATH_HERE` with your game's folder path (don't add quotes). The configuration will check your custom path first, then fall back to the default paths (so it'll still compile on a different computer). ### How do I change which files are included in the mod deploy/zip? -For custom files, you can [add/remove them in the build output](https://stackoverflow.com/a/10828462/262123). -(If your project references another mod, make sure the reference is [_not_ marked 'copy -local'](https://msdn.microsoft.com/en-us/library/t1zz5y8c(v=vs.100).aspx).) - -To exclude a file the package copies by default, see `IgnoreModFilePatterns` under -[_configure_](#configure). +* For normal files, you can [add/remove them in the build output](https://stackoverflow.com/a/10828462/262123). +* For assembly files (`*.dll`, `*.exe`, `*.pdb`, or `*.xml`), see the + [`BundleExtraAssemblies` option](#configure). +* To exclude a file which the package copies by default, see the [`IgnoreModFilePaths` or + `IgnoreModFilePatterns` options](#configure). ### Can I use the package for non-mod projects? -You can use the package in non-mod projects too (e.g. unit tests or framework DLLs). Just disable -the mod-related package features (see [_configure_](#configure)): +Yep, this works in unit tests and framework projects too. Just disable the mod-related package +features (see [_configure_](#configure)): ```xml false @@ -329,9 +394,9 @@ the mod-related package features (see [_configure_](#configure)): false ``` -If you need to copy the referenced DLLs into your build output, add this too: +To copy referenced DLLs into your build output for unit tests, add this too: ```xml -true +All ``` ## For SMAPI developers @@ -347,11 +412,63 @@ The NuGet package is generated automatically in `StardewModdingAPI.ModBuildConfi when you compile it. ## Release notes -## 3.2.2 +### 4.1.0 +Released 08 January 2023. + +* Added `manifest.json` format validation on build (thanks to tylergibbs2!). +* Fixed game DLLs not excluded from the release zip when they're referenced explicitly but `BundleExtraAssemblies` isn't set. + +### 4.0.2 +Released 09 October 2022. + +* Switched to the newer crossplatform `portable` debug symbols (thanks to lanturnalis!). +* Fixed `BundleExtraAssemblies` option being partly case-sensitive. +* Fixed `BundleExtraAssemblies` not applying `All` value to game assemblies. + +### 4.0.1 +Released 14 April 2022. + +* Added detection for Xbox app game folders. +* Fixed "_conflicts between different versions of Microsoft.Win32.Registry_" warnings in recent SMAPI versions. +* Internal refactoring. + +### 4.0.0 +Released 30 November 2021. + +* Updated for Stardew Valley 1.5.5 and SMAPI 3.13.0. (Older versions are no longer supported.) +* Added `IgnoreModFilePaths` option to ignore literal paths. +* Added `BundleExtraAssemblies` option to copy bundled DLLs into the mod zip/folder. +* Removed the `GameExecutableName` and `GameFramework` options (since they now have the same value + on all platforms). +* Removed the `CopyModReferencesToBuildOutput` option (superseded by `BundleExtraAssemblies`). +* Improved analyzer performance by enabling parallel execution. + +**Migration guide for mod authors:** +1. See [_migrate to 64-bit_](https://stardewvalleywiki.com/Modding:Migrate_to_64-bit_on_Windows) and + [_migrate to Stardew Valley 1.5.5_](https://stardewvalleywiki.com/Modding:Migrate_to_Stardew_Valley_1.5.5). +2. Possible changes in your `.csproj` or `.targets` files: + * Replace `$(GameExecutableName)` with `Stardew Valley`. + * Replace `$(GameFramework)` with `MonoGame` and remove any XNA Framework-specific logic. + * Replace `true` with + `Game`. + * If you need to bundle extra DLLs besides your mod DLL, see the [`BundleExtraAssemblies` + documentation](#configure). + +### 3.3.0 +Released 30 March 2021. + +* Added a build warning when the mod isn't compiled for `Any CPU`. +* Added a `GameFramework` build property set to `MonoGame` or `Xna` based on the platform. This can + be overridden to change which framework it references. +* Added support for building mods against the 64-bit Linux version of the game on Windows. +* The package now suppresses the misleading 'processor architecture mismatch' warnings. + +### 3.2.2 Released 23 September 2020. * Reworked and streamlined how the package is compiled. -* Added [SMAPI-ModTranslationClassBuilder](https://github.com/Pathoschild/SMAPI-ModTranslationClassBuilder) files to the ignore list. +* Added [SMAPI-ModTranslationClassBuilder](https://github.com/Pathoschild/SMAPI-ModTranslationClassBuilder) + files to the ignore list. ### 3.2.1 Released 11 September 2020. @@ -359,19 +476,19 @@ Released 11 September 2020. * Added more detailed logging. * Fixed _path's format is not supported_ error when using default `Mods` path in 3.2. -### 3.2 +### 3.2.0 Released 07 September 2020. * Added option to change `Mods` folder path. * Rewrote documentation to make it easier to read. -### 3.1 +### 3.1.0 Released 01 February 2020. * Added support for semantic versioning 2.0. * `0Harmony.dll` is now ignored if the mod references Harmony directly (it's bundled with SMAPI). -### 3.0 +### 3.0.0 Released 26 November 2019. * Updated for SMAPI 3.0 and Stardew Valley 1.4. @@ -386,14 +503,14 @@ Released 26 November 2019. * Dropped support for older versions of SMAPI and Visual Studio. * Migrated package icon to NuGet's new format. -### 2.2 +### 2.2.0 Released 28 October 2018. * Added support for SMAPI 2.8+ (still compatible with earlier versions). * Added default game paths for 32-bit Windows. * Fixed valid manifests marked invalid in some cases. -### 2.1 +### 2.1.0 Released 27 July 2018. * Added support for Stardew Valley 1.3. @@ -413,7 +530,7 @@ Released 11 October 2017. * Fixed mod deploy failing to create subfolders if they don't already exist. -### 2.0 +### 2.0.0 Released 11 October 2017. * Added: mods are now copied into the `Mods` folder automatically (configurable). @@ -431,7 +548,7 @@ Released 28 July 2017. * The manifest/i18n files in the project now take precedence over those in the build output if both are present. -### 1.7 +### 1.7.0 Released 28 July 2017. * Added option to create release zips on build. @@ -448,19 +565,19 @@ Released 09 July 2017. * Improved crossplatform game path detection. -### 1.6 +### 1.6.0 Released 05 June 2017. * Added support for deploying mod files into `Mods` automatically. * Added a build error if a game folder is found, but doesn't contain Stardew Valley or SMAPI. -### 1.5 +### 1.5.0 Released 23 January 2017. * Added support for setting a custom game path globally. -* Added default GOG path on Mac. +* Added default GOG path on macOS. -### 1.4 +### 1.4.0 Released 11 January 2017. * Fixed detection of non-default game paths on 32-bit Windows. @@ -468,22 +585,22 @@ Released 11 January 2017. * Removed support for overriding the target platform (no longer needed since SMAPI crossplatforms mods automatically). -### 1.3 +### 1.3.0 Released 31 December 2016. * Added support for non-default game paths on Windows. -### 1.2 +### 1.2.0 Released 24 October 2016. * Exclude game binaries from mod build output. -### 1.1 +### 1.1.0 Released 21 October 2016. * Added support for overriding the target platform. -### 1.0 +### 1.0.0 Released 21 October 2016. * Initial release. diff --git a/docs/technical/smapi.md b/docs/technical/smapi.md index e2832710..d115aefa 100644 --- a/docs/technical/smapi.md +++ b/docs/technical/smapi.md @@ -11,11 +11,12 @@ This document is about SMAPI itself; see also [mod build package](mod-package.md * [Configuration file](#configuration-file) * [Command-line arguments](#command-line-arguments) * [Compile flags](#compile-flags) -* [For SMAPI developers](#for-smapi-developers) - * [Compiling from source](#compiling-from-source) - * [Debugging a local build](#debugging-a-local-build) - * [Preparing a release](#preparing-a-release) - * [Using a custom Harmony build](#using-a-custom-harmony-build) +* [Compile from source code](#compile-from-source-code) + * [Main project](#main-project) + * [Custom Harmony build](#custom-harmony-build) +* [Prepare a release](#prepare-a-release) + * [On any platform](#on-any-platform) + * [On Windows](#on-windows) * [Release notes](#release-notes) ## Customisation @@ -32,24 +33,28 @@ argument | purpose `--uninstall` | Preselects the uninstall action, skipping the prompt asking what the user wants to do. `--game-path "path"` | Specifies the full path to the folder containing the Stardew Valley executable, skipping automatic detection and any prompt to choose a path. If the path is not valid, the installer displays an error. -SMAPI itself recognises two arguments **on Windows only**, but these are intended for internal use -or testing and may change without warning. On Linux/Mac, see _environment variables_ below. +SMAPI itself recognises five arguments, but these are meant for internal use or testing, and might +change without warning. **On Linux/macOS**, command-line arguments won't work; see _environment +variables_ below instead. argument | purpose -------- | ------- -`--no-terminal` | SMAPI won't write anything to the console window. (Messages will still be written to the log file.) +`--developer-mode`
`--developer-mode-off` | Enable or disable features intended for mod developers. Currently this only makes `TRACE`-level messages appear in the console. +`--no-terminal` | SMAPI won't log anything to the console. On Linux/macOS only, this will also prevent the launch script from trying to open a terminal window. (Messages will still be written to the log file.) +`--use-current-shell` | On Linux/macOS only, the launch script won't try to open a terminal window. All console output will be sent to the shell running the launch script. `--mods-path` | The path to search for mods, if not the standard `Mods` folder. This can be a path relative to the game folder (like `--mods-path "Mods (test)"`) or an absolute path. ### Environment variables -The above SMAPI arguments don't work on Linux/Mac due to the way the game launcher works. You can -set temporary environment variables instead. For example: +The above SMAPI arguments may not work on Linux/macOS due to the way the game launcher works. You +can set temporary environment variables instead. For example: > SMAPI_MODS_PATH="Mods (multiplayer)" /path/to/StardewValley environment variable | purpose -------------------- | ------- -`SMAPI_NO_TERMINAL` | Equivalent to `--no-terminal` above. +`SMAPI_DEVELOPER_MODE` | Equivalent to `--developer-mode` and `--developer-mode-off` above. The value must be `true` or `false`. `SMAPI_MODS_PATH` | Equivalent to `--mods-path` above. - +`SMAPI_NO_TERMINAL` | Equivalent to `--no-terminal` above. +`SMAPI_USE_CURRENT_SHELL` | Equivalent to `--use-current-shell` above. ### Compile flags SMAPI uses a small number of conditional compilation constants, which you can set by editing the @@ -57,54 +62,114 @@ SMAPI uses a small number of conditional compilation constants, which you can se flag | purpose ---- | ------- -`SMAPI_FOR_WINDOWS` | Whether SMAPI is being compiled on Windows for players on Windows. Set automatically in `crossplatform.targets`. -`HARMONY_2` | Whether to enable experimental Harmony 2.0 support and rewrite existing Harmony 1._x_ mods for compatibility. Note that you need to replace `build/0Harmony.dll` with a Harmony 2.0 build (or switch to a package reference) to use this flag. +`SMAPI_FOR_WINDOWS` | Whether SMAPI is being compiled for Windows; if not set, the code assumes Linux/macOS. Set automatically in `common.targets`. +`SMAPI_DEPRECATED` | Whether to include deprecated code in the build. -## For SMAPI developers -### Compiling from source +## Compile from source code +### Main project Using an official SMAPI release is recommended for most users, but you can compile from source -directly if needed. There are no special steps (just open the project and compile), but SMAPI often -uses the latest C# syntax. You may need the latest version of your IDE to compile it. +directly if needed. Just open the project in an IDE like [Visual +Studio](https://visualstudio.microsoft.com/vs/community/) or [Rider](https://www.jetbrains.com/rider/), +and build the `SMAPI` project. The project will automatically adjust the build settings for your +current OS and Stardew Valley install path. -SMAPI uses build configuration derived from the [crossplatform mod config](https://smapi.io/package/readme) -to detect your current OS automatically and load the correct references. Compile output will be -placed in a `bin` folder at the root of the Git repository. - -### Debugging a local build Rebuilding the solution in debug mode will copy the SMAPI files into your game folder. Starting -the `SMAPI` project with debugging from Visual Studio (on Mac or Windows) will launch SMAPI with -the debugger attached, so you can intercept errors and step through the code being executed. That -doesn't work in MonoDevelop on Linux, unfortunately. +the `SMAPI` project with debugging from Visual Studio or Rider should launch SMAPI with the +debugger attached, so you can intercept errors and step through the code being executed. -### Preparing a release -To prepare a crossplatform SMAPI release, you'll need to compile it on two platforms. See -[crossplatforming info](https://stardewvalleywiki.com/Modding:Modder_Guide/Test_and_Troubleshoot#Testing_on_all_platforms) -on the wiki for the first-time setup. +### Custom Harmony build +SMAPI uses [a custom build of Harmony 2.2.2](https://github.com/Pathoschild/Harmony#readme), which +is included in the `build` folder. To use a different build, just replace `0Harmony.dll` in that +folder before compiling. -1. Update the version numbers in `build/common.targets`, `Constants`, and the `manifest.json` for - bundled mods. Make sure you use a [semantic version](https://semver.org). Recommended format: +## Prepare a release +### On any platform +**⚠ Ideally we'd have one set of instructions for all platforms. The instructions in this section +will produce a fully functional release for all supported platforms, _except_ that the application +icon for SMAPI on Windows will disappear due to [.NET runtime bug +3828](https://github.com/dotnet/runtime/issues/3828). Until that's fixed, see the _[on +Windows](#on-windows)_ section below to create a build that retains the icon.** + +#### First-time setup +1. On Windows only: + 1. [Install Windows Subsystem for Linux (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install). + 2. Run `sudo apt update` in WSL to update the package list. + 3. The rest of the instructions below should be run in WSL. +2. Install the required software: + 1. Install the [.NET 5 SDK](https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu). + _For Ubuntu-based systems, you can run `lsb_release -a` to get the Ubuntu version number._ + 2. [Install Steam](https://linuxconfig.org/how-to-install-steam-on-ubuntu-20-04-focal-fossa-linux). + 3. Launch `steam` and install the game like usual. + 4. Download and install your preferred IDE. For the [latest standalone Rider + version](https://www.jetbrains.com/help/rider/Installation_guide.html#prerequisites): + ```sh + wget "" -O rider-install.tar.gz + sudo tar -xzvf rider-install.tar.gz -C /opt + ln -s "/opt/JetBrains Rider-/bin/rider.sh" + ./rider.sh + ``` +3. Clone the SMAPI repo: + ```sh + git clone https://github.com/Pathoschild/SMAPI.git + ``` + +### Launch the game +1. Run these commands to start Steam: + ```sh + export TERM=xterm + steam + ``` +2. Launch the game through the Steam UI. + +### Prepare the release +1. Run `build/unix/prepare-install-package.sh VERSION_HERE` to create the release package in the + root `bin` folder. + + Make sure you use a [semantic version](https://semver.org). Recommended format: build type | format | example :--------- | :----------------------- | :------ - dev build | `-alpha.` | `3.0.0-alpha.20171230` - prerelease | `-beta.` | `3.0.0-beta.20171230` - release | `` | `3.0.0` + dev build | `-alpha.` | `4.0.0-alpha.20251230` + prerelease | `-beta.` | `4.0.0-beta.20251230` + release | `` | `4.0.0` -2. In Windows: - 1. Rebuild the solution with the _release_ solution configuration. - 2. Copy `bin/SMAPI installer` and `bin/SMAPI installer for developers` to Linux/Mac. +### On Windows +#### First-time setup +1. Set up Windows Subsystem for Linux (WSL): + 1. [Install WSL](https://docs.microsoft.com/en-us/windows/wsl/install). + 2. Run `sudo apt update` in WSL to update the package list. + 3. The rest of the instructions below should be run in WSL. +2. Install the required software: + 1. Install the [.NET 5 SDK](https://dotnet.microsoft.com/download/dotnet/5.0). + 2. Install [Stardew Valley](https://www.stardewvalley.net/). +3. Clone the SMAPI repo: + ```sh + git clone https://github.com/Pathoschild/SMAPI.git + ``` -3. In Linux/Mac: - 1. Rebuild the solution with the _release_ solution configuration. - 2. Add the `windows-install.*` files from Windows to the `bin/SMAPI installer` and - `bin/SMAPI installer for developers` folders compiled on Linux. - 3. Rename the folders to `SMAPI installer` and `SMAPI installer for developers`. - 4. Zip the two folders. +### Prepare the release +1. Run `build/windows/prepare-install-package.ps1 VERSION_HERE` in PowerShell to create the release + package folders in the root `bin` folder. -### Custom Harmony build -SMAPI uses [a custom build of Harmony](https://github.com/Pathoschild/Harmony#readme), which is -included in the `build` folder. To use a different build, just replace `0Harmony.dll` in that -folder before compiling. + Make sure you use a [semantic version](https://semver.org). Recommended format: + + build type | format | example + :--------- | :----------------------- | :------ + dev build | `-alpha.` | `4.0.0-alpha.20251230` + prerelease | `-beta.` | `4.0.0-beta.20251230` + release | `` | `4.0.0` + +2. Launch WSL and run this script: + ```bash + # edit to match the build created in steps 1 + # In WSL, `/mnt/c/example` accesses `C:\example` on the Windows filesystem. + version="4.0.0" + binFolder="/mnt/e/source/_Stardew/SMAPI/bin" + build/windows/finalize-install-package.sh "$version" "$binFolder" + ``` + +Note: to prepare a test Windows-only build, you can pass `--windows-only` in the first step and +skip the second one. ## Release notes See [release notes](../release-notes.md). diff --git a/docs/technical/web.md b/docs/technical/web.md index 50237bfe..f0d43fb1 100644 --- a/docs/technical/web.md +++ b/docs/technical/web.md @@ -367,7 +367,7 @@ accordingly. Initial setup: 1. Create an Azure Blob storage account for uploaded files. -2. Create an Azure App Services environment running the latest .NET Core on Linux or Windows. +2. Create an Azure App Services environment running the latest .NET on Linux or Windows. 3. Add these application settings in the new App Services environment: property name | description diff --git a/src/SMAPI.Installer/Framework/InstallerContext.cs b/src/SMAPI.Installer/Framework/InstallerContext.cs new file mode 100644 index 00000000..a2c63dd8 --- /dev/null +++ b/src/SMAPI.Installer/Framework/InstallerContext.cs @@ -0,0 +1,65 @@ +using System.IO; +using StardewModdingAPI.Toolkit; +using StardewModdingAPI.Toolkit.Framework.GameScanning; +using StardewModdingAPI.Toolkit.Utilities; + +namespace StardewModdingAPI.Installer.Framework +{ + /// The installer context. + internal class InstallerContext + { + /********* + ** Fields + *********/ + /// The underlying toolkit game scanner. + private readonly GameScanner GameScanner = new(); + + + /********* + ** Accessors + *********/ + /// The current OS. + public Platform Platform { get; } + + /// The human-readable OS name and version. + public string PlatformName { get; } + + /// Whether the installer is running on Windows. + public bool IsWindows => this.Platform == Platform.Windows; + + /// Whether the installer is running on a Unix OS (including Linux or macOS). + public bool IsUnix => !this.IsWindows; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public InstallerContext() + { + this.Platform = EnvironmentUtility.DetectPlatform(); + this.PlatformName = EnvironmentUtility.GetFriendlyPlatformName(this.Platform); + } + + /// Get the installer's version number. + public ISemanticVersion GetInstallerVersion() + { + var raw = this.GetType().Assembly.GetName().Version!; + return new SemanticVersion(raw); + } + + /// Get whether a folder seems to contain the game files. + /// The folder to check. + public bool LooksLikeGameFolder(DirectoryInfo dir) + { + return this.GameScanner.LooksLikeGameFolder(dir); + } + + /// Get whether a folder seems to contain the game, and which version it contains if so. + /// The folder to check. + public GameFolderType GetGameFolderType(DirectoryInfo dir) + { + return this.GameScanner.GetGameFolderType(dir); + } + } +} diff --git a/src/SMAPI.Installer/Framework/InstallerPaths.cs b/src/SMAPI.Installer/Framework/InstallerPaths.cs index ac6c3a8e..0976eceb 100644 --- a/src/SMAPI.Installer/Framework/InstallerPaths.cs +++ b/src/SMAPI.Installer/Framework/InstallerPaths.cs @@ -1,4 +1,5 @@ using System.IO; +using StardewModdingAPI.Toolkit.Framework; namespace StardewModdingAPI.Installer.Framework { @@ -44,17 +45,20 @@ namespace StardewModdingAPI.Installer.Framework /// The full path to the user's config overrides file. public string ApiUserConfigPath { get; } + /// The full path to the installed game DLL. + public string GameDllPath { get; } + /// The full path to the installed SMAPI executable file. - public string ExecutablePath { get; } + public string UnixSmapiExecutablePath { get; } - /// The full path to the vanilla game launcher on Linux/Mac. - public string UnixLauncherPath { get; } + /// The full path to the vanilla game launch script on Linux/macOS. + public string VanillaLaunchScriptPath { get; } - /// The full path to the installed SMAPI launcher on Linux/Mac before it's renamed. - public string UnixSmapiLauncherPath { get; } + /// The full path to the installed SMAPI launch script on Linux/macOS before it's renamed. + public string NewLaunchScriptPath { get; } - /// The full path to the vanilla game launcher on Linux/Mac after SMAPI is installed. - public string UnixBackupLauncherPath { get; } + /// The full path to the backed up game launch script on Linux/macOS after SMAPI is installed. + public string BackupLaunchScriptPath { get; } /********* @@ -63,19 +67,22 @@ namespace StardewModdingAPI.Installer.Framework /// Construct an instance. /// The directory path containing the files to copy into the game folder. /// The directory path for the installed game. - /// The name of the game's executable file for the current platform. - public InstallerPaths(DirectoryInfo bundleDir, DirectoryInfo gameDir, string gameExecutableName) + public InstallerPaths(DirectoryInfo bundleDir, DirectoryInfo gameDir) { + // base paths this.BundleDir = bundleDir; this.GameDir = gameDir; this.ModsDir = new DirectoryInfo(Path.Combine(gameDir.FullName, "Mods")); + this.GameDllPath = Path.Combine(gameDir.FullName, Constants.GameDllName); + // launch scripts + this.VanillaLaunchScriptPath = Path.Combine(gameDir.FullName, "StardewValley"); + this.NewLaunchScriptPath = Path.Combine(gameDir.FullName, "unix-launcher.sh"); + this.BackupLaunchScriptPath = Path.Combine(gameDir.FullName, "StardewValley-original"); + this.UnixSmapiExecutablePath = Path.Combine(gameDir.FullName, "StardewModdingAPI"); + + // internal files this.BundleApiUserConfigPath = Path.Combine(bundleDir.FullName, "smapi-internal", "config.user.json"); - - this.ExecutablePath = Path.Combine(gameDir.FullName, gameExecutableName); - this.UnixLauncherPath = Path.Combine(gameDir.FullName, "StardewValley"); - this.UnixSmapiLauncherPath = Path.Combine(gameDir.FullName, "StardewModdingAPI"); - this.UnixBackupLauncherPath = Path.Combine(gameDir.FullName, "StardewValley-original"); this.ApiConfigPath = Path.Combine(gameDir.FullName, "smapi-internal", "config.json"); this.ApiUserConfigPath = Path.Combine(gameDir.FullName, "smapi-internal", "config.user.json"); } diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index d0ef0b8d..d00a5df4 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -1,18 +1,19 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Linq; -using Microsoft.Win32; +using System.Reflection; using StardewModdingApi.Installer.Enums; using StardewModdingAPI.Installer.Framework; using StardewModdingAPI.Internal.ConsoleWriting; using StardewModdingAPI.Toolkit; +using StardewModdingAPI.Toolkit.Framework; +using StardewModdingAPI.Toolkit.Framework.GameScanning; using StardewModdingAPI.Toolkit.Framework.ModScanning; using StardewModdingAPI.Toolkit.Utilities; -#if !SMAPI_FOR_WINDOWS -using System.Diagnostics; -#endif namespace StardewModdingApi.Installer { @@ -25,35 +26,37 @@ namespace StardewModdingApi.Installer /// The absolute path to the directory containing the files to copy into the game folder. private readonly string BundlePath; - /// The value that represents Windows 7. - private readonly Version Windows7Version = new Version(6, 1); - /// The mod IDs which the installer should allow as bundled mods. - private readonly string[] BundledModIDs = new[] - { + private readonly string[] BundledModIDs = { "SMAPI.SaveBackup", - "SMAPI.ConsoleCommands" + "SMAPI.ConsoleCommands", + "SMAPI.ErrorHandler" }; /// Get the absolute file or folder paths to remove when uninstalling SMAPI. /// The folder for Stardew Valley and SMAPI. /// The folder for SMAPI mods. + [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "These are valid file names.")] private IEnumerable GetUninstallPaths(DirectoryInfo installDir, DirectoryInfo modsDir) { string GetInstallPath(string path) => Path.Combine(installDir.FullName, path); // current files - yield return GetInstallPath("libgdiplus.dylib"); // Linux/Mac only - yield return GetInstallPath("StardewModdingAPI"); // Linux/Mac only + yield return GetInstallPath("StardewModdingAPI"); // Linux/macOS only + yield return GetInstallPath("StardewModdingAPI.deps.json"); + yield return GetInstallPath("StardewModdingAPI.dll"); yield return GetInstallPath("StardewModdingAPI.exe"); yield return GetInstallPath("StardewModdingAPI.exe.config"); - yield return GetInstallPath("StardewModdingAPI.exe.mdb"); // Linux/Mac only + yield return GetInstallPath("StardewModdingAPI.exe.mdb"); // Linux/macOS only yield return GetInstallPath("StardewModdingAPI.pdb"); // Windows only + yield return GetInstallPath("StardewModdingAPI.runtimeconfig.json"); yield return GetInstallPath("StardewModdingAPI.xml"); yield return GetInstallPath("smapi-internal"); yield return GetInstallPath("steam_appid.txt"); +#if SMAPI_DEPRECATED // obsolete + yield return GetInstallPath("libgdiplus.dylib"); // before 3.13 (macOS only) yield return GetInstallPath(Path.Combine("Mods", ".cache")); // 1.3-1.4 yield return GetInstallPath(Path.Combine("Mods", "TrainerMod")); // *–2.0 (renamed to ConsoleCommands) yield return GetInstallPath("Mono.Cecil.Rocks.dll"); // 1.3–1.8 @@ -73,15 +76,14 @@ namespace StardewModdingApi.Installer yield return GetInstallPath("StardewModdingAPI.Toolkit.CoreInterfaces.dll"); // moved in 2.8 yield return GetInstallPath("StardewModdingAPI.Toolkit.CoreInterfaces.pdb"); // moved in 2.8 yield return GetInstallPath("StardewModdingAPI.Toolkit.CoreInterfaces.xml"); // moved in 2.8 - yield return GetInstallPath("System.Numerics.dll"); // moved in 2.8 - yield return GetInstallPath("System.Runtime.Caching.dll"); // moved in 2.8 - yield return GetInstallPath("System.ValueTuple.dll"); // moved in 2.8 + yield return GetInstallPath("StardewModdingAPI-x64.exe"); // before 3.13 if (modsDir.Exists) { foreach (DirectoryInfo modDir in modsDir.EnumerateDirectories()) yield return Path.Combine(modDir.FullName, ".cache"); // 1.4–1.7 } +#endif yield return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "ErrorLogs"); // remove old log files } @@ -109,13 +111,13 @@ namespace StardewModdingApi.Installer /// 2. Ask the user whether to install or uninstall. /// /// Uninstall logic: - /// 1. On Linux/Mac: if a backup of the launcher exists, delete the launcher and restore the backup. + /// 1. On Linux/macOS: if a backup of the launcher exists, delete the launcher and restore the backup. /// 2. Delete all files and folders in the game directory matching one of the values returned by . /// /// Install flow: /// 1. Run the uninstall flow. /// 2. Copy the SMAPI files from package/Windows or package/Mono into the game directory. - /// 3. On Linux/Mac: back up the game launcher and replace it with the SMAPI launcher. (This isn't possible on Windows, so the user needs to configure it manually.) + /// 3. On Linux/macOS: back up the game launcher and replace it with the SMAPI launcher. (This isn't possible on Windows, so the user needs to configure it manually.) /// 4. Create the 'Mods' directory. /// 5. Copy the bundled mods into the 'Mods' directory (deleting any existing versions). /// 6. Move any mods from app data into game's mods directory. @@ -128,54 +130,30 @@ namespace StardewModdingApi.Installer /**** ** Get basic info & set window title ****/ - ModToolkit toolkit = new ModToolkit(); - Platform platform = EnvironmentUtility.DetectPlatform(); - Console.Title = $"SMAPI {this.GetDisplayVersion(this.GetType().Assembly.GetName().Version)} installer on {platform} {EnvironmentUtility.GetFriendlyPlatformName(platform)}"; + ModToolkit toolkit = new(); + var context = new InstallerContext(); + Console.Title = $"SMAPI {context.GetInstallerVersion()} installer on {context.Platform} {context.PlatformName}"; Console.WriteLine(); /**** ** Check if correct installer ****/ #if SMAPI_FOR_WINDOWS - if (platform == Platform.Linux || platform == Platform.Mac) + if (context.IsUnix) { - this.PrintError($"This is the installer for Windows. Run the 'install on {platform}.{(platform == Platform.Linux ? "sh" : "command")}' file instead."); + this.PrintError($"This is the installer for Windows. Run the 'install on {context.Platform}.{(context.Platform == Platform.Mac ? "command" : "sh")}' file instead."); Console.ReadLine(); return; } #else - if (platform == Platform.Windows) + if (context.IsWindows) { - this.PrintError($"This is the installer for Linux/Mac. Run the 'install on Windows.exe' file instead."); + this.PrintError($"This is the installer for Linux/macOS. Run the 'install on Windows.exe' file instead."); Console.ReadLine(); return; } #endif - /**** - ** Check Windows dependencies - ****/ - if (platform == Platform.Windows) - { - // .NET Framework 4.5+ - if (!this.HasNetFramework45(platform)) - { - this.PrintError(Environment.OSVersion.Version >= this.Windows7Version - ? "Please install the latest version of .NET Framework before installing SMAPI." // Windows 7+ - : "Please install .NET Framework 4.5 before installing SMAPI." // Windows Vista or earlier - ); - this.PrintError("See the download page at https://www.microsoft.com/net/download/framework for details."); - Console.ReadLine(); - return; - } - if (!this.HasXna(platform)) - { - this.PrintError("You don't seem to have XNA Framework installed. Please run the game at least once before installing SMAPI, so it can perform its first-time setup."); - Console.ReadLine(); - return; - } - } - /**** ** read command-line arguments ****/ @@ -190,7 +168,7 @@ namespace StardewModdingApi.Installer } // get game path from CLI - string gamePathArg = null; + string? gamePathArg = null; { int pathIndex = Array.LastIndexOf(args, "--game-path") + 1; if (pathIndex >= 1 && args.Length >= pathIndex) @@ -199,10 +177,10 @@ namespace StardewModdingApi.Installer /********* - ** Step 2: choose a theme (can't auto-detect on Linux/Mac) + ** Step 2: choose a theme (can't auto-detect on Linux/macOS) *********/ MonitorColorScheme scheme = MonitorColorScheme.AutoDetect; - if (platform == Platform.Linux || platform == Platform.Mac) + if (context.IsUnix) { /**** ** print header @@ -215,8 +193,8 @@ namespace StardewModdingApi.Installer ** show theme selector ****/ // get theme writers - var lightBackgroundWriter = new ColorfulConsoleWriter(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.LightBackground)); - var darkBackgroundWriter = new ColorfulConsoleWriter(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.DarkBackground)); + ColorfulConsoleWriter lightBackgroundWriter = new(context.Platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.LightBackground)); + ColorfulConsoleWriter darkBackgroundWriter = new(context.Platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.DarkBackground)); // print question this.PrintPlain("Which text looks more readable?"); @@ -228,7 +206,7 @@ namespace StardewModdingApi.Installer Console.WriteLine(); // handle choice - string choice = this.InteractivelyChoose("Type 1 or 2, then press enter.", new[] { "1", "2" }); + string choice = this.InteractivelyChoose("Type 1 or 2, then press enter.", new[] { "1", "2" }, printLine: Console.WriteLine); switch (choice) { case "1": @@ -263,8 +241,7 @@ namespace StardewModdingApi.Installer ** collect details ****/ // get game path - this.PrintInfo("Where is your game folder?"); - DirectoryInfo installDir = this.InteractivelyGetInstallPath(platform, toolkit, gamePathArg); + DirectoryInfo? installDir = this.InteractivelyGetInstallPath(toolkit, context, gamePathArg); if (installDir == null) { this.PrintError("Failed finding your game path."); @@ -273,21 +250,22 @@ namespace StardewModdingApi.Installer } // get folders - DirectoryInfo bundleDir = new DirectoryInfo(this.BundlePath); - paths = new InstallerPaths(bundleDir, installDir, EnvironmentUtility.GetExecutableName(platform)); + DirectoryInfo bundleDir = new(this.BundlePath); + paths = new InstallerPaths(bundleDir, installDir); } - Console.Clear(); /********* ** Step 4: validate assumptions *********/ - if (!File.Exists(paths.ExecutablePath)) + // executable exists + if (!File.Exists(paths.GameDllPath)) { this.PrintError("The detected game install path doesn't contain a Stardew Valley executable."); Console.ReadLine(); return; } + Console.Clear(); /********* @@ -359,11 +337,11 @@ namespace StardewModdingApi.Installer ** Always uninstall old files ****/ // restore game launcher - if (platform.IsMono() && File.Exists(paths.UnixBackupLauncherPath)) + if (context.IsUnix && File.Exists(paths.BackupLaunchScriptPath)) { this.PrintDebug("Removing SMAPI launcher..."); - this.InteractivelyDelete(paths.UnixLauncherPath); - File.Move(paths.UnixBackupLauncherPath, paths.UnixLauncherPath); + this.InteractivelyDelete(paths.VanillaLaunchScriptPath); + File.Move(paths.BackupLaunchScriptPath, paths.VanillaLaunchScriptPath); } // remove old files @@ -380,8 +358,8 @@ namespace StardewModdingApi.Installer // move global save data folder (changed in 3.2) { string dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley"); - DirectoryInfo oldDir = new DirectoryInfo(Path.Combine(dataPath, "Saves", ".smapi")); - DirectoryInfo newDir = new DirectoryInfo(Path.Combine(dataPath, ".smapi")); + DirectoryInfo oldDir = new(Path.Combine(dataPath, "Saves", ".smapi")); + DirectoryInfo newDir = new(Path.Combine(dataPath, ".smapi")); if (oldDir.Exists) { @@ -406,38 +384,46 @@ namespace StardewModdingApi.Installer } // replace mod launcher (if possible) - if (platform.IsMono()) + if (context.IsUnix) { this.PrintDebug("Safely replacing game launcher..."); // back up & remove current launcher - if (File.Exists(paths.UnixLauncherPath)) + if (File.Exists(paths.VanillaLaunchScriptPath)) { - if (!File.Exists(paths.UnixBackupLauncherPath)) - File.Move(paths.UnixLauncherPath, paths.UnixBackupLauncherPath); + if (!File.Exists(paths.BackupLaunchScriptPath)) + File.Move(paths.VanillaLaunchScriptPath, paths.BackupLaunchScriptPath); else - this.InteractivelyDelete(paths.UnixLauncherPath); + this.InteractivelyDelete(paths.VanillaLaunchScriptPath); } // add new launcher - File.Move(paths.UnixSmapiLauncherPath, paths.UnixLauncherPath); + File.Move(paths.NewLaunchScriptPath, paths.VanillaLaunchScriptPath); - // mark file executable + // mark files executable // (MSBuild doesn't keep permission flags for files zipped in a build task.) - // (Note: exclude from Windows build because antivirus apps can flag the process start code as suspicious.) -#if !SMAPI_FOR_WINDOWS - new Process + foreach (string path in new[] { paths.VanillaLaunchScriptPath, paths.UnixSmapiExecutablePath }) { - StartInfo = new ProcessStartInfo + new Process { - FileName = "chmod", - Arguments = $"755 \"{paths.UnixLauncherPath}\"", - CreateNoWindow = true - } - }.Start(); -#endif + StartInfo = new ProcessStartInfo + { + FileName = "chmod", + Arguments = $"755 \"{path}\"", + CreateNoWindow = true + } + }.Start(); + } } + // copy the game's deps.json file + // (This is needed to resolve native DLLs like libSkiaSharp.) + File.Copy( + sourceFileName: Path.Combine(paths.GamePath, "Stardew Valley.deps.json"), + destFileName: Path.Combine(paths.GamePath, "StardewModdingAPI.deps.json"), + overwrite: true + ); + // create mods directory (if needed) if (!paths.ModsDir.Exists) { @@ -446,13 +432,13 @@ namespace StardewModdingApi.Installer } // add or replace bundled mods - DirectoryInfo bundledModsDir = new DirectoryInfo(Path.Combine(paths.BundlePath, "Mods")); + DirectoryInfo bundledModsDir = new(Path.Combine(paths.BundlePath, "Mods")); if (bundledModsDir.Exists && bundledModsDir.EnumerateDirectories().Any()) { this.PrintDebug("Adding bundled mods..."); - ModFolder[] targetMods = toolkit.GetModFolders(paths.ModsPath).ToArray(); - foreach (ModFolder sourceMod in toolkit.GetModFolders(bundledModsDir.FullName)) + ModFolder[] targetMods = toolkit.GetModFolders(paths.ModsPath, useCaseInsensitiveFilePaths: true).ToArray(); + foreach (ModFolder sourceMod in toolkit.GetModFolders(bundledModsDir.FullName, useCaseInsensitiveFilePaths: true)) { // validate source mod if (sourceMod.Manifest == null) @@ -467,8 +453,9 @@ namespace StardewModdingApi.Installer } // find target folder - ModFolder targetMod = targetMods.FirstOrDefault(p => p.Manifest?.UniqueID?.Equals(sourceMod.Manifest.UniqueID, StringComparison.OrdinalIgnoreCase) == true); - DirectoryInfo defaultTargetFolder = new DirectoryInfo(Path.Combine(paths.ModsPath, sourceMod.Directory.Name)); + // ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract -- avoid error if the Mods folder has invalid mods, since they're not validated yet + ModFolder? targetMod = targetMods.FirstOrDefault(p => p.Manifest?.UniqueID?.Equals(sourceMod.Manifest.UniqueID, StringComparison.OrdinalIgnoreCase) == true); + DirectoryInfo defaultTargetFolder = new(Path.Combine(paths.ModsPath, sourceMod.Directory.Name)); DirectoryInfo targetFolder = targetMod?.Directory ?? defaultTargetFolder; this.PrintDebug(targetFolder.FullName == defaultTargetFolder.FullName ? $" adding {sourceMod.Manifest.Name}..." @@ -493,8 +480,10 @@ namespace StardewModdingApi.Installer File.WriteAllText(paths.ApiConfigPath, text); } +#if SMAPI_DEPRECATED // remove obsolete appdata mods this.InteractivelyRemoveAppDataMods(paths.ModsDir, bundledModsDir); +#endif } } Console.WriteLine(); @@ -504,7 +493,7 @@ namespace StardewModdingApi.Installer /********* ** Step 7: final instructions *********/ - if (platform == Platform.Windows) + if (context.IsWindows) { if (action == ScriptAction.Install) { @@ -531,16 +520,6 @@ namespace StardewModdingApi.Installer /********* ** Private methods *********/ - /// Get the display text for an assembly version. - /// The assembly version. - private string GetDisplayVersion(Version version) - { - string str = $"{version.Major}.{version.Minor}"; - if (version.Build != 0) - str += $".{version.Build}"; - return str; - } - /// Get the display text for a color scheme. /// The color scheme. private string GetDisplayText(MonitorColorScheme scheme) @@ -560,58 +539,44 @@ namespace StardewModdingApi.Installer /// Print a message without formatting. /// The text to print. - private void PrintPlain(string text) => Console.WriteLine(text); + private void PrintPlain(string text) + { + Console.WriteLine(text); + } /// Print a debug message. /// The text to print. - private void PrintDebug(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Debug); + private void PrintDebug(string text) + { + this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Debug); + } /// Print a debug message. /// The text to print. - private void PrintInfo(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Info); + private void PrintInfo(string text) + { + this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Info); + } /// Print a warning message. /// The text to print. - private void PrintWarning(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Warn); + private void PrintWarning(string text) + { + this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Warn); + } /// Print a warning message. /// The text to print. - private void PrintError(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Error); + private void PrintError(string text) + { + this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Error); + } /// Print a success message. /// The text to print. - private void PrintSuccess(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Success); - - /// Get whether the current system has .NET Framework 4.5 or later installed. This only applies on Windows. - /// The current platform. - /// The current platform is not Windows. - private bool HasNetFramework45(Platform platform) + private void PrintSuccess(string text) { - switch (platform) - { - case Platform.Windows: - using (RegistryKey versionKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full")) - return versionKey?.GetValue("Release") != null; // .NET Framework 4.5+ - - default: - throw new NotSupportedException("The installed .NET Framework version can only be checked on Windows."); - } - } - - /// Get whether the current system has XNA Framework installed. This only applies on Windows. - /// The current platform. - /// The current platform is not Windows. - private bool HasXna(Platform platform) - { - switch (platform) - { - case Platform.Windows: - using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\XNA\Framework")) - return key != null; // XNA Framework 4.0+ - - default: - throw new NotSupportedException("The installed XNA Framework version can only be checked on Windows."); - } + this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Success); } /// Interactively delete a file or folder path, and block until deletion completes. @@ -622,7 +587,7 @@ namespace StardewModdingApi.Installer { try { - FileUtilities.ForceDelete(Directory.Exists(path) ? new DirectoryInfo(path) : (FileSystemInfo)new FileInfo(path)); + FileUtilities.ForceDelete(Directory.Exists(path) ? new DirectoryInfo(path) : new FileInfo(path)); break; } catch (Exception ex) @@ -638,7 +603,7 @@ namespace StardewModdingApi.Installer /// The file or folder to copy. /// The folder to copy into. /// A filter which matches directories and files to copy, or null to match all. - private void RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func filter = null) + private void RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func? filter = null) { if (filter != null && !filter(source)) return; @@ -653,8 +618,8 @@ namespace StardewModdingApi.Installer break; case DirectoryInfo sourceDir: - DirectoryInfo targetSubfolder = new DirectoryInfo(Path.Combine(targetFolder.FullName, sourceDir.Name)); - foreach (var entry in sourceDir.EnumerateFileSystemInfos()) + DirectoryInfo targetSubfolder = new(Path.Combine(targetFolder.FullName, sourceDir.Name)); + foreach (FileSystemInfo entry in sourceDir.EnumerateFileSystemInfos()) this.RecursiveCopy(entry, targetSubfolder, filter); break; @@ -664,22 +629,22 @@ namespace StardewModdingApi.Installer } /// Interactively ask the user to choose a value. - /// A callback which prints a message to the console. + /// A callback which prints a message to the console. /// The message to print. /// The allowed options (not case sensitive). /// The indentation to prefix to output. - private string InteractivelyChoose(string message, string[] options, string indent = "", Action print = null) + private string InteractivelyChoose(string message, string[] options, string indent = "", Action? printLine = null) { - print ??= this.PrintInfo; + printLine ??= this.PrintInfo; while (true) { - print(indent + message); + printLine(indent + message); Console.Write(indent); - string input = Console.ReadLine()?.Trim().ToLowerInvariant(); - if (!options.Contains(input)) + string? input = Console.ReadLine()?.Trim().ToLowerInvariant(); + if (input == null || !options.Contains(input)) { - print($"{indent}That's not a valid option."); + printLine($"{indent}That's not a valid option."); continue; } return input; @@ -687,100 +652,166 @@ namespace StardewModdingApi.Installer } /// Interactively locate the game install path to update. - /// The current platform. /// The mod toolkit. + /// The installer context. /// The path specified as a command-line argument (if any), which should override automatic path detection. - private DirectoryInfo InteractivelyGetInstallPath(Platform platform, ModToolkit toolkit, string specifiedPath) + private DirectoryInfo? InteractivelyGetInstallPath(ModToolkit toolkit, InstallerContext context, string? specifiedPath) { - // get executable name - string executableFilename = EnvironmentUtility.GetExecutableName(platform); - - // validate specified path + // use specified path if (specifiedPath != null) { + string errorPrefix = $"You specified --game-path \"{specifiedPath}\", but"; + var dir = new DirectoryInfo(specifiedPath); if (!dir.Exists) { - this.PrintError($"You specified --game-path \"{specifiedPath}\", but that folder doesn't exist."); + this.PrintError($"{errorPrefix} that folder doesn't exist."); return null; } - if (!dir.EnumerateFiles(executableFilename).Any()) + + switch (context.GetGameFolderType(dir)) { - this.PrintError($"You specified --game-path \"{specifiedPath}\", but that folder doesn't contain the Stardew Valley executable."); - return null; + case GameFolderType.Valid: + return dir; + + case GameFolderType.Legacy154OrEarlier: + this.PrintWarning($"{errorPrefix} that directory seems to have Stardew Valley 1.5.4 or earlier."); + this.PrintWarning("Please update your game to the latest version to use SMAPI."); + return null; + + case GameFolderType.LegacyCompatibilityBranch: + this.PrintWarning($"{errorPrefix} that directory seems to have the Stardew Valley legacy 'compatibility' branch."); + this.PrintWarning("Unfortunately SMAPI is only compatible with the modern version of the game."); + this.PrintWarning("Please update your game to the main branch to use SMAPI."); + return null; + + case GameFolderType.NoGameFound: + this.PrintWarning($"{errorPrefix} that directory doesn't contain a Stardew Valley executable."); + return null; + + default: + this.PrintWarning($"{errorPrefix} that directory doesn't seem to contain a valid game install."); + return null; } - return dir; } - // get installed paths - DirectoryInfo[] defaultPaths = toolkit.GetGameFolders().ToArray(); + // let user choose detected path + DirectoryInfo[] defaultPaths = this.DetectGameFolders(toolkit, context).ToArray(); if (defaultPaths.Any()) { - // only one path - if (defaultPaths.Length == 1) - return defaultPaths.First(); - - // let user choose path + this.PrintInfo("Where do you want to add or remove SMAPI?"); Console.WriteLine(); - this.PrintInfo("Found multiple copies of the game:"); for (int i = 0; i < defaultPaths.Length; i++) this.PrintInfo($"[{i + 1}] {defaultPaths[i].FullName}"); + this.PrintInfo($"[{defaultPaths.Length + 1}] Enter a custom game path."); Console.WriteLine(); - string[] validOptions = Enumerable.Range(1, defaultPaths.Length).Select(p => p.ToString(CultureInfo.InvariantCulture)).ToArray(); - string choice = this.InteractivelyChoose("Where do you want to add/remove SMAPI? Type the number next to your choice, then press enter.", validOptions); + string[] validOptions = Enumerable.Range(1, defaultPaths.Length + 1).Select(p => p.ToString(CultureInfo.InvariantCulture)).ToArray(); + string choice = this.InteractivelyChoose("Type the number next to your choice, then press enter.", validOptions); int index = int.Parse(choice, CultureInfo.InvariantCulture) - 1; - return defaultPaths[index]; - } - // ask user - this.PrintInfo("Oops, couldn't find the game automatically."); + if (index < defaultPaths.Length) + return defaultPaths[index]; + } + else + this.PrintInfo("Oops, couldn't find the game automatically."); + + // let user enter manual path while (true) { // get path from user - this.PrintInfo($"Type the file path to the game directory (the one containing '{executableFilename}'), then press enter."); - string path = Console.ReadLine()?.Trim(); + Console.WriteLine(); + this.PrintInfo($"Type the file path to the game directory (the one containing '{Constants.GameDllName}'), then press enter."); + string? path = Console.ReadLine()?.Trim(); if (string.IsNullOrWhiteSpace(path)) { - this.PrintInfo(" You must specify a directory path to continue."); + this.PrintWarning("You must specify a directory path to continue."); continue; } // normalize path - if (platform == Platform.Windows) - path = path.Replace("\"", ""); // in Windows, quotes are used to escape spaces and aren't part of the file path - if (platform == Platform.Linux || platform == Platform.Mac) - path = path.Replace("\\ ", " "); // in Linux/Mac, spaces in paths may be escaped if copied from the command line + path = context.IsWindows + ? path.Replace("\"", "") // in Windows, quotes are used to escape spaces and aren't part of the file path + : path.Replace("\\ ", " "); // in Linux/macOS, spaces in paths may be escaped if copied from the command line if (path.StartsWith("~/")) { - string home = Environment.GetEnvironmentVariable("HOME") ?? Environment.GetEnvironmentVariable("USERPROFILE"); + string home = Environment.GetEnvironmentVariable("HOME") ?? Environment.GetEnvironmentVariable("USERPROFILE")!; path = Path.Combine(home, path.Substring(2)); } // get directory if (File.Exists(path)) - path = Path.GetDirectoryName(path); - DirectoryInfo directory = new DirectoryInfo(path); + path = Path.GetDirectoryName(path)!; + DirectoryInfo directory = new(path); // validate path if (!directory.Exists) { - this.PrintInfo(" That directory doesn't seem to exist."); - continue; - } - if (!directory.EnumerateFiles(executableFilename).Any()) - { - this.PrintInfo(" That directory doesn't contain a Stardew Valley executable."); + this.PrintWarning("That directory doesn't seem to exist."); continue; } - // looks OK - this.PrintInfo(" OK!"); - return directory; + switch (context.GetGameFolderType(directory)) + { + case GameFolderType.Valid: + this.PrintInfo(" OK!"); + return directory; + + case GameFolderType.Legacy154OrEarlier: + this.PrintWarning("That directory seems to have Stardew Valley 1.5.4 or earlier."); + this.PrintWarning("Please update your game to the latest version to use SMAPI."); + continue; + + case GameFolderType.LegacyCompatibilityBranch: + this.PrintWarning("That directory seems to have the Stardew Valley legacy 'compatibility' branch."); + this.PrintWarning("Unfortunately SMAPI is only compatible with the modern version of the game."); + this.PrintWarning("Please update your game to the main branch to use SMAPI."); + continue; + + case GameFolderType.NoGameFound: + this.PrintWarning("That directory doesn't contain a Stardew Valley executable."); + continue; + + default: + this.PrintWarning("That directory doesn't seem to contain a valid game install."); + continue; + } } } - /// Interactively move mods out of the appdata directory. + /// Get the possible game paths to update. + /// The mod toolkit. + /// The installer context. + private IEnumerable DetectGameFolders(ModToolkit toolkit, InstallerContext context) + { + HashSet foundPaths = new HashSet(); + + // game folder which contains the installer, if any + { + DirectoryInfo? curPath = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + while (curPath?.Parent != null) // must be in a folder (not at the root) + { + if (context.LooksLikeGameFolder(curPath)) + { + foundPaths.Add(curPath.FullName); + yield return curPath; + break; + } + + curPath = curPath.Parent; + } + } + + // game paths detected by toolkit + foreach (DirectoryInfo dir in toolkit.GetGameFolders()) + { + if (foundPaths.Add(dir.FullName)) + yield return dir; + } + } + +#if SMAPI_DEPRECATED + /// Interactively move mods out of the app data directory. /// The directory which should contain all mods. /// The installer directory containing packaged mods. private void InteractivelyRemoveAppDataMods(DirectoryInfo properModsDir, DirectoryInfo packagedModsDir) @@ -790,7 +821,7 @@ namespace StardewModdingApi.Installer // get path string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley"); - DirectoryInfo modDir = new DirectoryInfo(Path.Combine(appDataPath, "Mods")); + DirectoryInfo modDir = new(Path.Combine(appDataPath, "Mods")); // check if migration needed if (!modDir.Exists) @@ -803,7 +834,7 @@ namespace StardewModdingApi.Installer { // get type bool isDir = entry is DirectoryInfo; - if (!isDir && !(entry is FileInfo)) + if (!isDir && entry is not FileInfo) continue; // should never happen // delete packaged mods (newer version bundled into SMAPI) @@ -840,7 +871,7 @@ namespace StardewModdingApi.Installer /// Move a filesystem entry to a new parent directory. /// The filesystem entry to move. /// The destination path. - /// We can't use or , because those don't work across partitions. + /// We can't use or , because those don't work across partitions. private void Move(FileSystemInfo entry, string newPath) { // file @@ -862,20 +893,18 @@ namespace StardewModdingApi.Installer directory.Delete(recursive: true); } } +#endif /// Get whether a file or folder should be copied from the installer files. /// The file or folder info. private bool ShouldCopy(FileSystemInfo entry) { - switch (entry.Name) + return entry.Name switch { - case "mcs": - return false; // ignore Mac symlink - case "Mods": - return false; // Mods folder handled separately - default: - return true; - } + "mcs" => false, // ignore macOS symlink + "Mods" => false, // Mods folder handled separately + _ => true + }; } } } diff --git a/src/SMAPI.Installer/Program.cs b/src/SMAPI.Installer/Program.cs index 6c479621..dc452a46 100644 --- a/src/SMAPI.Installer/Program.cs +++ b/src/SMAPI.Installer/Program.cs @@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Reflection; +using System.Threading; namespace StardewModdingApi.Installer { @@ -14,7 +15,7 @@ namespace StardewModdingApi.Installer *********/ /// The absolute path of the installer folder. [SuppressMessage("ReSharper", "AssignNullToNotNullAttribute", Justification = "The assembly location is never null in this context.")] - private static readonly string InstallerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + private static readonly string InstallerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; /// The absolute path of the folder containing the unzipped installer files. private static readonly string ExtractedBundlePath = Path.Combine(Path.GetTempPath(), $"SMAPI-installer-{Guid.NewGuid():N}"); @@ -30,8 +31,7 @@ namespace StardewModdingApi.Installer public static void Main(string[] args) { // find install bundle - PlatformID platform = Environment.OSVersion.Platform; - FileInfo zipFile = new FileInfo(Path.Combine(Program.InstallerPath, $"{(platform == PlatformID.Win32NT ? "windows" : "unix")}-install.dat")); + FileInfo zipFile = new(Path.Combine(Program.InstallerPath, "install.dat")); if (!zipFile.Exists) { Console.WriteLine($"Oops! Some of the installer files are missing; try re-downloading the installer. (Missing file: {zipFile.FullName})"); @@ -40,7 +40,7 @@ namespace StardewModdingApi.Installer } // unzip bundle into temp folder - DirectoryInfo bundleDir = new DirectoryInfo(Program.ExtractedBundlePath); + DirectoryInfo bundleDir = new(Program.ExtractedBundlePath); Console.WriteLine("Extracting install files..."); ZipFile.ExtractToDirectory(zipFile.FullName, bundleDir.FullName); @@ -49,7 +49,15 @@ namespace StardewModdingApi.Installer // launch installer var installer = new InteractiveInstaller(bundleDir.FullName); - installer.Run(args); + + try + { + installer.Run(args); + } + catch (Exception ex) + { + Program.PrintErrorAndExit($"The installer failed with an unexpected exception.\nIf you need help fixing this error, see https://smapi.io/help\n\n{ex}"); + } } /********* @@ -58,14 +66,14 @@ namespace StardewModdingApi.Installer /// Method called when assembly resolution fails, which may return a manually resolved assembly. /// The event sender. /// The event arguments. - private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e) + private static Assembly? CurrentDomain_AssemblyResolve(object? sender, ResolveEventArgs e) { try { - AssemblyName name = new AssemblyName(e.Name); + AssemblyName name = new(e.Name); foreach (FileInfo dll in new DirectoryInfo(Program.InternalFilesPath).EnumerateFiles("*.dll")) { - if (name.Name.Equals(AssemblyName.GetAssemblyName(dll.FullName).Name, StringComparison.OrdinalIgnoreCase)) + if (name.Name != null && name.Name.Equals(AssemblyName.GetAssemblyName(dll.FullName).Name, StringComparison.OrdinalIgnoreCase)) return Assembly.LoadFrom(dll.FullName); } return null; @@ -76,5 +84,19 @@ namespace StardewModdingApi.Installer return null; } } + + /// Write an error directly to the console and exit. + /// The error message to display. + private static void PrintErrorAndExit(string message) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(message); + Console.ResetColor(); + + Console.WriteLine("Game has ended. Press any key to exit."); + Thread.Sleep(100); + Console.ReadKey(); + Environment.Exit(0); + } } } diff --git a/src/SMAPI.Installer/SMAPI.Installer.csproj b/src/SMAPI.Installer/SMAPI.Installer.csproj index 44ed3bd1..928e5c18 100644 --- a/src/SMAPI.Installer/SMAPI.Installer.csproj +++ b/src/SMAPI.Installer/SMAPI.Installer.csproj @@ -2,9 +2,8 @@ StardewModdingAPI.Installer The SMAPI installer for players. - net45 + net5.0 Exe - x86 false @@ -18,5 +17,4 @@ - diff --git a/src/SMAPI.Installer/assets/README.txt b/src/SMAPI.Installer/assets/README.txt index 0da49a46..08e99887 100644 --- a/src/SMAPI.Installer/assets/README.txt +++ b/src/SMAPI.Installer/assets/README.txt @@ -14,31 +14,34 @@ SMAPI lets you run Stardew Valley with mods. Don't forget to download mods separately. -Player's guide +Automated install -------------------------------- See https://stardewvalleywiki.com/Modding:Player_Guide for help installing SMAPI, adding mods, etc. Manual install -------------------------------- -THIS IS NOT RECOMMENDED FOR MOST PLAYERS. See instructions above instead. +THIS IS NOT RECOMMENDED FOR MOST PLAYERS. See the instructions above instead. If you really want to install SMAPI manually, here's how. -1. Unzip "internal/windows-install.dat" (on Windows) or "internal/unix-install.dat" (on Linux/Mac). - You can change '.dat' to '.zip', it's just a normal zip file renamed to prevent confusion. +1. Unzip "internal/windows/install.dat" (on Windows) or "internal/unix/install.dat" (on Linux or + macOS). You can change '.dat' to '.zip', it's just a normal zip file renamed to prevent + confusion. + 2. Copy the files from the folder you just unzipped into your game folder. The `StardewModdingAPI.exe` file should be right next to the game's executable. -3. + +3. Copy `Stardew Valley.deps.json` in the game folder, and rename the copy to + `StardewModdingAPI.deps.json`. + +4. - Windows only: if you use Steam, see the install guide above to enable achievements and overlay. Otherwise, just run StardewModdingAPI.exe in your game folder to play with mods. - - Linux/Mac only: rename the "StardewValley" file (no extension) to "StardewValley-original", and + - Linux/macOS only: rename the "StardewValley" file (no extension) to "StardewValley-original", and "StardewModdingAPI" (no extension) to "StardewValley". Now just launch the game as usual to play with mods. -When installing on Linux or Mac: -- Make sure Mono is installed (normally the installer checks for you). While it's not required, - many mods won't work correctly without it. (Specifically, mods which load PNG images may crash or - freeze the game.) +When installing on Linux or macOS: - To configure the color scheme, edit the `smapi-internal/config.json` file and see instructions there for the 'ColorScheme' setting. diff --git a/src/SMAPI.Installer/assets/System.Numerics.dll b/src/SMAPI.Installer/assets/System.Numerics.dll deleted file mode 100644 index fed0f92c..00000000 Binary files a/src/SMAPI.Installer/assets/System.Numerics.dll and /dev/null differ diff --git a/src/SMAPI.Installer/assets/System.Runtime.Caching.dll b/src/SMAPI.Installer/assets/System.Runtime.Caching.dll deleted file mode 100644 index a062391d..00000000 Binary files a/src/SMAPI.Installer/assets/System.Runtime.Caching.dll and /dev/null differ diff --git a/src/SMAPI.Installer/assets/install on Linux.sh b/src/SMAPI.Installer/assets/install on Linux.sh new file mode 100644 index 00000000..3b7eae9c --- /dev/null +++ b/src/SMAPI.Installer/assets/install on Linux.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cd "`dirname "$0"`" +internal/linux/SMAPI.Installer diff --git a/src/SMAPI.Installer/assets/install on Windows.bat b/src/SMAPI.Installer/assets/install on Windows.bat new file mode 100644 index 00000000..b0d9ae81 --- /dev/null +++ b/src/SMAPI.Installer/assets/install on Windows.bat @@ -0,0 +1,41 @@ +@echo off +setlocal enabledelayedexpansion + +SET installerDir="%~dp0" + +REM make sure we're not running within a zip folder +echo %installerDir% | findstr /C:"%TEMP%" 1>nul +if %ERRORLEVEL% EQU 0 ( + echo Oops! It looks like you're running the installer from inside a zip file. Make sure you unzip the download first. + echo. + pause + exit +) + +REM make sure an antivirus hasn't deleted the installer DLL +if not exist %installerDir%"internal\windows\SMAPI.Installer.dll" ( + echo Oops! SMAPI is missing one of its files. Your antivirus might have deleted it. + echo Missing file: %installerDir%internal\windows\SMAPI.Installer.dll + echo. + pause + exit +) +if not exist %installerDir%"internal\windows\SMAPI.Installer.exe" ( + echo Oops! SMAPI is missing one of its files. Your antivirus might have deleted it. + echo Missing file: %installerDir%internal\windows\SMAPI.Installer.exe + echo. + pause + exit +) + +REM start installer +internal\windows\SMAPI.Installer.exe + +REM keep window open if it failed +if %ERRORLEVEL% NEQ 0 ( + echo. + echo Oops! The SMAPI installer seems to have failed. The error details may be shown above. + echo. + pause + exit +) diff --git a/src/SMAPI.Installer/assets/install on macOS.command b/src/SMAPI.Installer/assets/install on macOS.command new file mode 100644 index 00000000..abd21dc8 --- /dev/null +++ b/src/SMAPI.Installer/assets/install on macOS.command @@ -0,0 +1,6 @@ +#!/bin/bash + +cd "`dirname "$0"`" + +xattr -r -d com.apple.quarantine internal +internal/macOS/SMAPI.Installer diff --git a/src/SMAPI.Installer/assets/runtimeconfig.json b/src/SMAPI.Installer/assets/runtimeconfig.json new file mode 100644 index 00000000..bd6a5240 --- /dev/null +++ b/src/SMAPI.Installer/assets/runtimeconfig.json @@ -0,0 +1,17 @@ +{ + "runtimeOptions": { + "tfm": "net5.0", + "includedFrameworks": [ + { + "name": "Microsoft.NETCore.App", + "version": "5.0.0", + "rollForward": "latestMinor" + } + ], + "configProperties": { + // disable tiered runtime JIT: https://github.com/dotnet/runtime/blob/main/docs/design/features/tiered-compilation.md + // This is disabled by the base game, and causes issues with Harmony patches. + "System.Runtime.TieredCompilation": false + } + } +} diff --git a/src/SMAPI.Installer/assets/unix-install.sh b/src/SMAPI.Installer/assets/unix-install.sh deleted file mode 100644 index 6d0c86ce..00000000 --- a/src/SMAPI.Installer/assets/unix-install.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -# Run the SMAPI installer through Mono on Linux or Mac. - -# Move to script's directory -cd "`dirname "$0"`" - -# get cross-distro version of POSIX command -COMMAND="" -if command -v command >/dev/null 2>&1; then - COMMAND="command -v" -elif type type >/dev/null 2>&1; then - COMMAND="type" -fi - -# if $TERM is not set to xterm, mono will bail out when attempting to write to the console. -export TERM=xterm - -# validate Mono & run installer -if $COMMAND mono >/dev/null 2>&1; then - mono internal/unix-install.exe -else - echo "Oops! Looks like Mono isn't installed. Please install Mono from https://mono-project.com, reboot, and run this installer again." - read -fi diff --git a/src/SMAPI.Installer/assets/unix-launcher.sh b/src/SMAPI.Installer/assets/unix-launcher.sh index 1d97d487..778663d7 100644 --- a/src/SMAPI.Installer/assets/unix-launcher.sh +++ b/src/SMAPI.Installer/assets/unix-launcher.sh @@ -1,109 +1,164 @@ #!/usr/bin/env bash -# MonoKickstart Shell Script -# Written by Ethan "flibitijibibo" Lee -# Modified for SMAPI by various contributors -# Move to script's directory +########## +## Initial setup +########## +# move to script's directory cd "$(dirname "$0")" || exit $? -# Get the system architecture -UNAME=$(uname) -ARCH=$(uname -m) +# Whether to avoid opening a separate terminal window, and avoid logging anything to the console. +# This isn't recommended since you won't see errors, warnings, and update alerts. +SKIP_TERMINAL=false -# MonoKickstart picks the right libfolder, so just execute the right binary. -if [ "$UNAME" == "Darwin" ]; then - # ... Except on OSX. - export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:./osx/ +# Whether to avoid opening a separate terminal, but still send the usual log output to the console. +USE_CURRENT_SHELL=false - # El Capitan is a total idiot and wipes this variable out, making the - # Steam overlay disappear. This sidesteps "System Integrity Protection" - # and resets the variable with Valve's own variable (they provided this - # fix by the way, thanks Valve!). Note that you will need to update your - # launch configuration to the script location, NOT just the app location - # (i.e. Kick.app/Contents/MacOS/Kick, not just Kick.app). - # -flibit - if [ "$STEAM_DYLD_INSERT_LIBRARIES" != "" ] && [ "$DYLD_INSERT_LIBRARIES" == "" ]; then - export DYLD_INSERT_LIBRARIES="$STEAM_DYLD_INSERT_LIBRARIES" - fi - # this was here before - ln -sf mcs.bin.osx mcs - - # fix "DllNotFoundException: libgdiplus.dylib" errors when loading images in SMAPI - if [ -f libgdiplus.dylib ]; then - rm libgdiplus.dylib - fi - if [ -f /Library/Frameworks/Mono.framework/Versions/Current/lib/libgdiplus.dylib ]; then - ln -s /Library/Frameworks/Mono.framework/Versions/Current/lib/libgdiplus.dylib libgdiplus.dylib - fi - - # launch SMAPI - cp StardewValley.bin.osx StardewModdingAPI.bin.osx - open -a Terminal ./StardewModdingAPI.bin.osx "$@" -else - # choose launcher - LAUNCHER="" - if [ "$ARCH" == "x86_64" ]; then - ln -sf mcs.bin.x86_64 mcs - cp StardewValley.bin.x86_64 StardewModdingAPI.bin.x86_64 - LAUNCHER="./StardewModdingAPI.bin.x86_64" - else - ln -sf mcs.bin.x86 mcs - cp StardewValley.bin.x86 StardewModdingAPI.bin.x86 - LAUNCHER="./StardewModdingAPI.bin.x86" - fi - export LAUNCHER - - # get cross-distro version of POSIX command - COMMAND="" - if command -v command 2>/dev/null; then - COMMAND="command -v" - elif type type 2>/dev/null; then - COMMAND="type -p" - fi - - # select terminal (prefer xterm for best compatibility, then known supported terminals) - for terminal in xterm gnome-terminal kitty terminator xfce4-terminal konsole terminal termite alacritty mate-terminal x-terminal-emulator; do - if $COMMAND "$terminal" 2>/dev/null; then - export LAUNCHTERM=$terminal - break; - fi - done - - # find the true shell behind x-terminal-emulator - if [ "$LAUNCHTERM" = "x-terminal-emulator" ]; then - export LAUNCHTERM="$(basename "$(readlink -f $(COMMAND x-terminal-emulator))")" - fi - - # run in selected terminal and account for quirks - case $LAUNCHTERM in - terminal|termite) - # LAUNCHTERM consumes only one argument after -e - # options containing space characters are unsupported - exec $LAUNCHTERM -e "env TERM=xterm $LAUNCHER $@" - ;; - xterm|konsole|alacritty) - # LAUNCHTERM consumes all arguments after -e - exec $LAUNCHTERM -e env TERM=xterm $LAUNCHER "$@" - ;; - terminator|xfce4-terminal|mate-terminal) - # LAUNCHTERM consumes all arguments after -x - exec $LAUNCHTERM -x env TERM=xterm $LAUNCHER "$@" - ;; - gnome-terminal) - # LAUNCHTERM consumes all arguments after -- - exec $LAUNCHTERM -- env TERM=xterm $LAUNCHER "$@" - ;; - kitty) - # LAUNCHTERM consumes all trailing arguments - exec $LAUNCHTERM env TERM=xterm $LAUNCHER "$@" - ;; - *) - # If we don't know the terminal, just try to run it in the current shell. - env TERM=xterm $LAUNCHER "$@" - # if THAT fails, launch with no output - if [ $? -eq 127 ]; then - exec $LAUNCHER --no-terminal "$@" - fi - esac +########## +## Read environment variables +########## +if [ "$SMAPI_NO_TERMINAL" == "true" ]; then + SKIP_TERMINAL=true +fi +if [ "$SMAPI_USE_CURRENT_SHELL" == "true" ]; then + USE_CURRENT_SHELL=true +fi + + +########## +## Read command-line arguments +########## +while [ "$#" -gt 0 ]; do + case "$1" in + --skip-terminal ) SKIP_TERMINAL=true; shift ;; + --use-current-shell ) USE_CURRENT_SHELL=true; shift ;; + -- ) shift; break ;; + * ) shift ;; + esac +done + +if [ "$SKIP_TERMINAL" == "true" ]; then + USE_CURRENT_SHELL=true +fi + + +########## +## Open terminal if needed +########## +# on macOS, make sure we're running in a Terminal +# Besides letting the player see errors/warnings/alerts in the console, this is also needed because +# Steam messes with the PATH. +if [ "$(uname)" == "Darwin" ]; then + if [ ! -t 1 ]; then # not open in Terminal (https://stackoverflow.com/q/911168/262123) + # reopen in Terminal if needed + # https://stackoverflow.com/a/29511052/262123 + if [ "$USE_CURRENT_SHELL" == "false" ]; then + echo "Reopening in the Terminal app..." + echo '#!/bin/sh' > /tmp/open-smapi-terminal.command + echo "\"$0\" $@ --use-current-shell" >> /tmp/open-smapi-terminal.command + chmod +x /tmp/open-smapi-terminal.command + cat /tmp/open-smapi-terminal.command + open -W /tmp/open-smapi-terminal.command + rm /tmp/open-smapi-terminal.command + exit 0 + fi + fi +fi + + +########## +## Validate assumptions +########## +# script must be run from the game folder +if [ ! -f "Stardew Valley.dll" ]; then + printf "Oops! SMAPI must be placed in the Stardew Valley game folder.\nSee instructions: https://stardewvalleywiki.com/Modding:Player_Guide"; + read -r + exit 1 +fi + + +########## +## Launch SMAPI +########## +# macOS +if [ "$(uname)" == "Darwin" ]; then + ./StardewModdingAPI "$@" + +# Linux +else + # choose binary file to launch + LAUNCH_FILE="./StardewModdingAPI" + export LAUNCH_FILE + + # run in terminal + if [ "$USE_CURRENT_SHELL" == "false" ]; then + # select terminal (prefer xterm for best compatibility, then known supported terminals) + for terminal in xterm gnome-terminal kitty terminator xfce4-terminal konsole terminal termite alacritty mate-terminal x-terminal-emulator; do + if command -v "$terminal" 2>/dev/null; then + export TERMINAL_NAME=$terminal + break; + fi + done + + # find the true shell behind x-terminal-emulator + if [ "$TERMINAL_NAME" = "x-terminal-emulator" ]; then + TERMINAL_NAME="$(basename "$(readlink -f "$(command -v x-terminal-emulator)")")" + export TERMINAL_NAME + fi + + # run in selected terminal and account for quirks + TERMINAL_PATH="$(command -v "$TERMINAL_NAME")" + export TERMINAL_PATH + if [ -x "$TERMINAL_PATH" ]; then + case $TERMINAL_NAME in + terminal|termite) + # consumes only one argument after -e + # options containing space characters are unsupported + exec "$TERMINAL_NAME" -e "env TERM=xterm $LAUNCH_FILE $@" + ;; + + xterm|konsole|alacritty) + # consumes all arguments after -e + exec "$TERMINAL_NAME" -e env TERM=xterm $LAUNCH_FILE "$@" + ;; + + terminator|xfce4-terminal|mate-terminal) + # consumes all arguments after -x + exec "$TERMINAL_NAME" -x env TERM=xterm $LAUNCH_FILE "$@" + ;; + + gnome-terminal) + # consumes all arguments after -- + exec "$TERMINAL_NAME" -- env TERM=xterm $LAUNCH_FILE "$@" + ;; + + kitty) + # consumes all trailing arguments + exec "$TERMINAL_NAME" env TERM=xterm $LAUNCH_FILE "$@" + ;; + + *) + # If we don't know the terminal, just try to run it in the current shell. + # If THAT fails, launch with no output. + env TERM=xterm $LAUNCH_FILE "$@" + if [ $? -eq 127 ]; then + exec $LAUNCH_FILE --no-terminal "$@" + fi + esac + + ## terminal isn't executable; fallback to current shell or no terminal + else + echo "The '$TERMINAL_NAME' terminal isn't executable. SMAPI might be running in a sandbox or the system might be misconfigured? Falling back to current shell." + env TERM=xterm $LAUNCH_FILE "$@" + if [ $? -eq 127 ]; then + exec $LAUNCH_FILE --no-terminal "$@" + fi + fi + + # explicitly run without terminal + elif [ "$SKIP_TERMINAL" == "true" ]; then + exec $LAUNCH_FILE --no-terminal "$@" + else + exec $LAUNCH_FILE "$@" + fi fi diff --git a/src/SMAPI.Installer/assets/windows-install.bat b/src/SMAPI.Installer/assets/windows-install.bat deleted file mode 100644 index d02dd4c6..00000000 --- a/src/SMAPI.Installer/assets/windows-install.bat +++ /dev/null @@ -1,8 +0,0 @@ -@echo off -echo %~dp0 | findstr /C:"%TEMP%" 1>nul -if not errorlevel 1 ( - echo Oops! It looks like you're running the installer from inside a zip file. Make sure you unzip the download first. - pause -) else ( - start /WAIT /B internal/windows-install.exe -) diff --git a/src/SMAPI.Internal.Patching/BasePatcher.cs b/src/SMAPI.Internal.Patching/BasePatcher.cs new file mode 100644 index 00000000..c1936ccc --- /dev/null +++ b/src/SMAPI.Internal.Patching/BasePatcher.cs @@ -0,0 +1,54 @@ +using System; +using System.Reflection; +using HarmonyLib; + +namespace StardewModdingAPI.Internal.Patching +{ + /// Provides base implementation logic for instances. + internal abstract class BasePatcher : IPatcher + { + /********* + ** Public methods + *********/ + /// + public abstract void Apply(Harmony harmony, IMonitor monitor); + + + /********* + ** Protected methods + *********/ + /// Get a method and assert that it was found. + /// The type containing the method. + /// The method parameter types, or null if it's not overloaded. + protected ConstructorInfo RequireConstructor(params Type[] parameters) + { + return PatchHelper.RequireConstructor(parameters); + } + + /// Get a method and assert that it was found. + /// The type containing the method. + /// The method name. + /// The method parameter types, or null if it's not overloaded. + /// The method generic types, or null if it's not generic. + protected MethodInfo RequireMethod(string name, Type[]? parameters = null, Type[]? generics = null) + { + return PatchHelper.RequireMethod(name, parameters, generics); + } + + /// Get a Harmony patch method on the current patcher instance. + /// The method name. + /// The patch priority to apply, usually specified using Harmony's enum, or null to keep the default value. + protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null) + { + HarmonyMethod method = new( + AccessTools.Method(this.GetType(), name) + ?? throw new InvalidOperationException($"Can't find patcher method {PatchHelper.GetMethodString(this.GetType(), name)}.") + ); + + if (priority.HasValue) + method.priority = priority.Value; + + return method; + } + } +} diff --git a/src/SMAPI.Internal.Patching/HarmonyPatcher.cs b/src/SMAPI.Internal.Patching/HarmonyPatcher.cs new file mode 100644 index 00000000..6f30c241 --- /dev/null +++ b/src/SMAPI.Internal.Patching/HarmonyPatcher.cs @@ -0,0 +1,36 @@ +using System; +using HarmonyLib; + +namespace StardewModdingAPI.Internal.Patching +{ + /// Simplifies applying instances to the game. + internal static class HarmonyPatcher + { + /********* + ** Public methods + *********/ + /// Apply the given Harmony patchers. + /// The mod ID applying the patchers. + /// The monitor with which to log any errors. + /// The patchers to apply. + public static Harmony Apply(string id, IMonitor monitor, params IPatcher[] patchers) + { + Harmony harmony = new(id); + + foreach (IPatcher patcher in patchers) + { + try + { + patcher.Apply(harmony, monitor); + } + catch (Exception ex) + { + monitor.Log($"Couldn't apply runtime patch '{patcher.GetType().Name}' to the game. Some SMAPI features may not work correctly. See log file for details.", LogLevel.Error); + monitor.Log($"Technical details:\n{ex.GetLogSummary()}"); + } + } + + return harmony; + } + } +} diff --git a/src/SMAPI.Internal.Patching/IPatcher.cs b/src/SMAPI.Internal.Patching/IPatcher.cs new file mode 100644 index 00000000..a732d64f --- /dev/null +++ b/src/SMAPI.Internal.Patching/IPatcher.cs @@ -0,0 +1,16 @@ +using HarmonyLib; + +namespace StardewModdingAPI.Internal.Patching +{ + /// A set of Harmony patches to apply. + internal interface IPatcher + { + /********* + ** Public methods + *********/ + /// Apply the Harmony patches for this instance. + /// The Harmony instance. + /// The monitor with which to log any errors. + public void Apply(Harmony harmony, IMonitor monitor); + } +} diff --git a/src/SMAPI.Internal.Patching/PatchHelper.cs b/src/SMAPI.Internal.Patching/PatchHelper.cs new file mode 100644 index 00000000..edd8ef57 --- /dev/null +++ b/src/SMAPI.Internal.Patching/PatchHelper.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Text; +using HarmonyLib; + +namespace StardewModdingAPI.Internal.Patching +{ + /// Provides utility methods for patching game code with Harmony. + internal static class PatchHelper + { + /********* + ** Public methods + *********/ + /// Get a constructor and assert that it was found. + /// The type containing the method. + /// The method parameter types, or null if it's not overloaded. + /// The type has no matching constructor. + public static ConstructorInfo RequireConstructor(Type[]? parameters = null) + { + return + AccessTools.Constructor(typeof(TTarget), parameters) + ?? throw new InvalidOperationException($"Can't find constructor {PatchHelper.GetMethodString(typeof(TTarget), null, parameters)} to patch."); + } + + /// Get a method and assert that it was found. + /// The type containing the method. + /// The method name. + /// The method parameter types, or null if it's not overloaded. + /// The method generic types, or null if it's not generic. + /// The type has no matching method. + public static MethodInfo RequireMethod(string name, Type[]? parameters = null, Type[]? generics = null) + { + return + AccessTools.Method(typeof(TTarget), name, parameters, generics) + ?? throw new InvalidOperationException($"Can't find method {PatchHelper.GetMethodString(typeof(TTarget), name, parameters, generics)} to patch."); + } + + /// Get a human-readable representation of a method target. + /// The type containing the method. + /// The method name, or null for a constructor. + /// The method parameter types, or null if it's not overloaded. + /// The method generic types, or null if it's not generic. + public static string GetMethodString(Type type, string? name, Type[]? parameters = null, Type[]? generics = null) + { + StringBuilder str = new(); + + // type + str.Append(type.FullName); + + // method name (if not constructor) + if (name != null) + { + str.Append('.'); + str.Append(name); + } + + // generics + if (generics?.Any() == true) + { + str.Append('<'); + str.Append(string.Join(", ", generics.Select(p => p.FullName))); + str.Append('>'); + } + + // parameters + if (parameters?.Any() == true) + { + str.Append('('); + str.Append(string.Join(", ", parameters.Select(p => p.FullName))); + str.Append(')'); + } + + return str.ToString(); + } + } +} diff --git a/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.projitems b/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.projitems new file mode 100644 index 00000000..4fa2a062 --- /dev/null +++ b/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.projitems @@ -0,0 +1,17 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + 6c16e948-3e5c-47a7-bf4b-07a7469a87a5 + + + SMAPI.Internal.Patching + + + + + + + + \ No newline at end of file diff --git a/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.shproj b/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.shproj new file mode 100644 index 00000000..1a102c82 --- /dev/null +++ b/src/SMAPI.Internal.Patching/SMAPI.Internal.Patching.shproj @@ -0,0 +1,13 @@ + + + + 6c16e948-3e5c-47a7-bf4b-07a7469a87a5 + 14.0 + + + + + + + + diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs index 001840bf..4e5850ea 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs @@ -6,10 +6,26 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// The console color scheme options. internal class ColorSchemeConfig { + /********* + ** Accessors + *********/ /// The default color scheme ID to use, or to select one automatically. - public MonitorColorScheme UseScheme { get; set; } + public MonitorColorScheme UseScheme { get; } /// The available console color schemes. - public IDictionary> Schemes { get; set; } + public IDictionary> Schemes { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The default color scheme ID to use, or to select one automatically. + /// The available console color schemes. + public ColorSchemeConfig(MonitorColorScheme useScheme, IDictionary> schemes) + { + this.UseScheme = useScheme; + this.Schemes = schemes; + } } } diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index b5bd4600..78db0d65 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Internal.ConsoleWriting @@ -11,10 +12,11 @@ namespace StardewModdingAPI.Internal.ConsoleWriting ** Fields *********/ /// The console text color for each log level. - private readonly IDictionary Colors; + private readonly IDictionary? Colors; /// Whether the current console supports color formatting. - private readonly bool SupportsColor; + [MemberNotNullWhen(true, nameof(ColorfulConsoleWriter.Colors))] + private bool SupportsColor { get; } /********* @@ -72,10 +74,9 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// The colors here should be kept in sync with the SMAPI config file. public static ColorSchemeConfig GetDefaultColorSchemeConfig(MonitorColorScheme useScheme) { - return new ColorSchemeConfig - { - UseScheme = useScheme, - Schemes = new Dictionary> + return new ColorSchemeConfig( + useScheme: useScheme, + schemes: new Dictionary> { [MonitorColorScheme.DarkBackground] = new Dictionary { @@ -98,7 +99,7 @@ namespace StardewModdingAPI.Internal.ConsoleWriting [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen } } - }; + ); } @@ -129,12 +130,12 @@ namespace StardewModdingAPI.Internal.ConsoleWriting if (schemeID == MonitorColorScheme.AutoDetect) { schemeID = platform == Platform.Mac - ? MonitorColorScheme.LightBackground // MacOS doesn't provide console background color info, but it's usually white. + ? MonitorColorScheme.LightBackground // macOS doesn't provide console background color info, but it's usually white. : ColorfulConsoleWriter.IsDark(Console.BackgroundColor) ? MonitorColorScheme.DarkBackground : MonitorColorScheme.LightBackground; } // get colors for scheme - return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary scheme) + return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary? scheme) ? scheme : throw new NotSupportedException($"Unknown color scheme '{schemeID}'."); } diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs new file mode 100644 index 00000000..7edc0f62 --- /dev/null +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -0,0 +1,67 @@ +using System; +using System.Reflection; +using System.Text.RegularExpressions; + +namespace StardewModdingAPI.Internal +{ + /// Provides extension methods for handling exceptions. + internal static class ExceptionHelper + { + /********* + ** Public methods + *********/ + /// Get a string representation of an exception suitable for writing to the error log. + /// The error to summarize. + public static string GetLogSummary(this Exception? exception) + { + try + { + string message; + switch (exception) + { + case TypeLoadException ex: + message = $"Failed loading type '{ex.TypeName}': {exception}"; + break; + + case ReflectionTypeLoadException ex: + string summary = ex.ToString(); + foreach (Exception? childEx in ex.LoaderExceptions) + summary += $"\n\n{childEx?.GetLogSummary()}"; + message = summary; + break; + + default: + message = exception?.ToString() ?? $"\n{Environment.StackTrace}"; + break; + } + + return ExceptionHelper.SimplifyExtensionMessage(message); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed handling {exception?.GetType().FullName} (original message: {exception?.Message})", ex); + } + } + + /// Simplify common patterns in exception log messages that don't convey useful info. + /// The log message to simplify. + public static string SimplifyExtensionMessage(string message) + { + // remove namespace for core exception types + message = Regex.Replace( + message, + @"(?:StardewModdingAPI\.Framework\.Exceptions|Microsoft\.Xna\.Framework|System|System\.IO)\.([a-zA-Z]+Exception):", + "$1:" + ); + + // remove unneeded root build paths for SMAPI and Stardew Valley + message = message + .Replace(@"E:\source\_Stardew\SMAPI\src\", "") + .Replace(@"C:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\", ""); + + // remove placeholder info in Linux/macOS stack traces + return message + .Replace(@":0", ""); + } + } +} diff --git a/src/SMAPI.Internal/SMAPI.Internal.projitems b/src/SMAPI.Internal/SMAPI.Internal.projitems index 0d583a6d..41d356c0 100644 --- a/src/SMAPI.Internal/SMAPI.Internal.projitems +++ b/src/SMAPI.Internal/SMAPI.Internal.projitems @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticResult.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticResult.cs index 896c2cb8..845149bd 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticResult.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticResult.cs @@ -1,6 +1,7 @@ // -using Microsoft.CodeAnalysis; +// ReSharper disable All -- generated code using System; +using Microsoft.CodeAnalysis; namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.Helper.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.Helper.cs index 0247288e..4bda70ff 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.Helper.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.Helper.cs @@ -1,12 +1,14 @@ // -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Text; +// ReSharper disable All -- generated code + using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Text; namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { @@ -51,17 +53,17 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) { var projects = new HashSet(); - foreach (var document in documents) + foreach (Document document in documents) { projects.Add(document.Project); } var diagnostics = new List(); - foreach (var project in projects) + foreach (Project project in projects) { - var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer)); + CompilationWithAnalyzers compilationWithAnalyzers = project.GetCompilationAsync().Result!.WithAnalyzers(ImmutableArray.Create(analyzer)); var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; - foreach (var diag in diags) + foreach (Diagnostic diag in diags) { if (diag.Location == Location.None || diag.Location.IsInMetadata) { @@ -71,8 +73,8 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { for (int i = 0; i < documents.Length; i++) { - var document = documents[i]; - var tree = document.GetSyntaxTreeAsync().Result; + Document document = documents[i]; + SyntaxTree? tree = document.GetSyntaxTreeAsync().Result; if (tree == diag.Location.SourceTree) { diagnostics.Add(diag); @@ -113,7 +115,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework throw new ArgumentException("Unsupported Language"); } - var project = CreateProject(sources, language); + Project project = CreateProject(sources, language); var documents = project.Documents.ToArray(); if (sources.Length != documents.Length) @@ -124,17 +126,6 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework return documents; } - /// - /// Create a Document from a string through creating a project that contains it. - /// - /// Classes in the form of a string - /// The language the source code is in - /// A Document created from the source string - protected static Document CreateDocument(string source, string language = LanguageNames.CSharp) - { - return CreateProject(new[] { source }, language).Documents.First(); - } - /// /// Create a project using the inputted strings as sources. /// @@ -146,9 +137,9 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; - var projectId = ProjectId.CreateNewId(debugName: TestProjectName); + ProjectId projectId = ProjectId.CreateNewId(debugName: TestProjectName); - var solution = new AdhocWorkspace() + Solution solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .AddMetadataReference(projectId, DiagnosticVerifier.SelfReference) @@ -158,14 +149,14 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework .AddMetadataReference(projectId, CodeAnalysisReference); int count = 0; - foreach (var source in sources) + foreach (string source in sources) { - var newFileName = fileNamePrefix + count + "." + fileExt; - var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); + string newFileName = fileNamePrefix + count + "." + fileExt; + DocumentId documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } - return solution.GetProject(projectId); + return solution.GetProject(projectId)!; } #endregion } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.cs index edaaabd4..efe69e4a 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Framework/DiagnosticVerifier.cs @@ -1,4 +1,6 @@ // +// ReSharper disable All -- generated code + using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,18 +19,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework /// /// Get the CSharp analyzer being tested - to be implemented in non-abstract class /// - protected virtual DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return null; - } - - /// - /// Get the Visual Basic analyzer being tested (C#) - to be implemented in non-abstract class - /// - protected virtual DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() - { - return null; - } + protected abstract DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer(); #endregion #region Verifier wrappers @@ -41,18 +32,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework /// DiagnosticResults that should appear after the analyzer is run on the source protected void VerifyCSharpDiagnostic(string source, params DiagnosticResult[] expected) { - VerifyDiagnostics(new[] { source }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); - } - - /// - /// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as a source - /// Note: input a DiagnosticResult for each Diagnostic expected - /// - /// An array of strings to create source documents from to run the analyzers on - /// DiagnosticResults that should appear after the analyzer is run on the sources - protected void VerifyCSharpDiagnostic(string[] sources, params DiagnosticResult[] expected) - { - VerifyDiagnostics(sources, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); + this.VerifyDiagnostics(new[] { source }, LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzer(), expected); } /// @@ -65,8 +45,8 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework /// DiagnosticResults that should appear after the analyzer is run on the sources private void VerifyDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expected) { - var diagnostics = GetSortedDiagnostics(sources, language, analyzer); - VerifyDiagnosticResults(diagnostics, analyzer, expected); + var diagnostics = DiagnosticVerifier.GetSortedDiagnostics(sources, language, analyzer); + DiagnosticVerifier.VerifyDiagnosticResults(diagnostics, analyzer, expected); } #endregion @@ -86,7 +66,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework if (expectedCount != actualCount) { - string diagnosticsOutput = actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE."; + string diagnosticsOutput = actualResults.Any() ? DiagnosticVerifier.FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE."; Assert.IsTrue(false, string.Format("Mismatch between number of diagnostics returned, expected \"{0}\" actual \"{1}\"\r\n\r\nDiagnostics:\r\n{2}\r\n", expectedCount, actualCount, diagnosticsOutput)); @@ -103,12 +83,12 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { Assert.IsTrue(false, string.Format("Expected:\nA project diagnostic with No location\nActual:\n{0}", - FormatDiagnostics(analyzer, actual))); + DiagnosticVerifier.FormatDiagnostics(analyzer, actual))); } } else { - VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First()); + DiagnosticVerifier.VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First()); var additionalLocations = actual.AdditionalLocations.ToArray(); if (additionalLocations.Length != expected.Locations.Length - 1) @@ -116,12 +96,12 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework Assert.IsTrue(false, string.Format("Expected {0} additional locations but got {1} for Diagnostic:\r\n {2}\r\n", expected.Locations.Length - 1, additionalLocations.Length, - FormatDiagnostics(analyzer, actual))); + DiagnosticVerifier.FormatDiagnostics(analyzer, actual))); } for (int j = 0; j < additionalLocations.Length; ++j) { - VerifyDiagnosticLocation(analyzer, actual, additionalLocations[j], expected.Locations[j + 1]); + DiagnosticVerifier.VerifyDiagnosticLocation(analyzer, actual, additionalLocations[j], expected.Locations[j + 1]); } } @@ -129,21 +109,21 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { Assert.IsTrue(false, string.Format("Expected diagnostic id to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Id, actual.Id, FormatDiagnostics(analyzer, actual))); + expected.Id, actual.Id, DiagnosticVerifier.FormatDiagnostics(analyzer, actual))); } if (actual.Severity != expected.Severity) { Assert.IsTrue(false, string.Format("Expected diagnostic severity to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Severity, actual.Severity, FormatDiagnostics(analyzer, actual))); + expected.Severity, actual.Severity, DiagnosticVerifier.FormatDiagnostics(analyzer, actual))); } if (actual.GetMessage() != expected.Message) { Assert.IsTrue(false, string.Format("Expected diagnostic message to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Message, actual.GetMessage(), FormatDiagnostics(analyzer, actual))); + expected.Message, actual.GetMessage(), DiagnosticVerifier.FormatDiagnostics(analyzer, actual))); } } } @@ -161,7 +141,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework Assert.IsTrue(actualSpan.Path == expected.Path || (actualSpan.Path != null && actualSpan.Path.Contains("Test0.") && expected.Path.Contains("Test.")), string.Format("Expected diagnostic to be in file \"{0}\" was actually in file \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Path, actualSpan.Path, FormatDiagnostics(analyzer, diagnostic))); + expected.Path, actualSpan.Path, DiagnosticVerifier.FormatDiagnostics(analyzer, diagnostic))); var actualLinePosition = actualSpan.StartLinePosition; @@ -172,7 +152,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { Assert.IsTrue(false, string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic))); + expected.Line, actualLinePosition.Line + 1, DiagnosticVerifier.FormatDiagnostics(analyzer, diagnostic))); } } @@ -183,7 +163,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework { Assert.IsTrue(false, string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", - expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic))); + expected.Column, actualLinePosition.Character + 1, DiagnosticVerifier.FormatDiagnostics(analyzer, diagnostic))); } } } @@ -201,7 +181,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework var builder = new StringBuilder(); for (int i = 0; i < diagnostics.Length; ++i) { - builder.AppendLine("// " + diagnostics[i].ToString()); + builder.AppendLine("// " + diagnostics[i]); var analyzerType = analyzer.GetType(); var rules = analyzer.SupportedDiagnostics; @@ -220,11 +200,10 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework Assert.IsTrue(location.IsInSource, $"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}\r\n"); - string resultMethodName = diagnostics[i].Location.SourceTree.FilePath.EndsWith(".cs") ? "GetCSharpResultAt" : "GetBasicResultAt"; var linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition; builder.AppendFormat("{0}({1}, {2}, {3}.{4})", - resultMethodName, + "GetCSharpResultAt", linePosition.Line + 1, linePosition.Character + 1, analyzerType.Name, diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetCollection.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetCollection.cs index d160610e..8bedd583 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetCollection.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetCollection.cs @@ -1,10 +1,8 @@ // ReSharper disable CheckNamespace -- matches Stardew Valley's code -using System.Collections; -using System.Collections.Generic; using System.Collections.ObjectModel; namespace Netcode { /// A simplified version of Stardew Valley's Netcode.NetCollection for unit testing. - public class NetCollection : Collection, IList, ICollection, IEnumerable, IEnumerable { } + public class NetCollection : Collection { } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetFieldBase.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetFieldBase.cs index 140c6f59..8f6b8987 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetFieldBase.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetFieldBase.cs @@ -7,10 +7,13 @@ namespace Netcode public class NetFieldBase where TSelf : NetFieldBase { /// The synchronised value. - public T Value { get; set; } + public T? Value { get; set; } /// Implicitly convert a net field to the its type. /// The field to convert. - public static implicit operator T(NetFieldBase field) => field.Value; + public static implicit operator T?(NetFieldBase field) + { + return field.Value; + } } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetList.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetList.cs index 1699f71c..33e616fb 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetList.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/Netcode/NetList.cs @@ -1,9 +1,8 @@ // ReSharper disable CheckNamespace -- matches Stardew Valley's code -using System.Collections; using System.Collections.Generic; namespace Netcode { /// A simplified version of Stardew Valley's Netcode.NetObjectList for unit testing. - public class NetList : List, IList, ICollection, IEnumerable, IEnumerable { } + public class NetList : List { } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Farmer.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Farmer.cs index 13fab069..dbd05792 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Farmer.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Farmer.cs @@ -1,5 +1,5 @@ // ReSharper disable CheckNamespace, InconsistentNaming -- matches Stardew Valley's code -#pragma warning disable 649 // (never assigned) -- only used to test type conversions +// ReSharper disable UnusedMember.Global -- used dynamically for unit tests using System.Collections.Generic; namespace StardewValley @@ -8,6 +8,6 @@ namespace StardewValley internal class Farmer { /// A sample field which should be replaced with a different property. - public readonly IDictionary friendships; + public readonly IDictionary friendships = new Dictionary(); } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Item.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Item.cs index 1b6317c1..d50deb72 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Item.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Item.cs @@ -1,4 +1,5 @@ // ReSharper disable CheckNamespace, InconsistentNaming -- matches Stardew Valley's code +// ReSharper disable UnusedMember.Global -- used dynamically for unit tests using Netcode; namespace StardewValley @@ -7,27 +8,27 @@ namespace StardewValley public class Item { /// A net int field with an equivalent non-net Category property. - public readonly NetInt category = new NetInt { Value = 42 }; + public readonly NetInt category = new() { Value = 42 }; /// A generic net int field with no equivalent non-net property. - public readonly NetInt netIntField = new NetInt { Value = 42 }; + public readonly NetInt netIntField = new() { Value = 42 }; /// A generic net ref field with no equivalent non-net property. - public readonly NetRef netRefField = new NetRef(); + public readonly NetRef netRefField = new(); /// A generic net int property with no equivalent non-net property. - public NetInt netIntProperty = new NetInt { Value = 42 }; + public NetInt netIntProperty = new() { Value = 42 }; /// A generic net ref property with no equivalent non-net property. - public NetRef netRefProperty { get; } = new NetRef(); + public NetRef netRefProperty { get; } = new(); /// A sample net list. - public readonly NetList netList = new NetList(); + public readonly NetList netList = new(); /// A sample net object list. - public readonly NetObjectList netObjectList = new NetObjectList(); + public readonly NetObjectList netObjectList = new(); /// A sample net collection. - public readonly NetCollection netCollection = new NetCollection(); + public readonly NetCollection netCollection = new(); } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Object.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Object.cs index 3dd66a6d..151010a7 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Object.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/Mock/StardewValley/Object.cs @@ -7,6 +7,6 @@ namespace StardewValley public class Object : Item { /// A net int field with an equivalent non-net property. - public NetInt type = new NetInt { Value = 42 }; + public NetInt type = new() { Value = 42 }; } } diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs index 89bd1be5..a6fa5633 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs @@ -87,13 +87,13 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests [TestCase("SObject obj = null; if (obj.netRefProperty != null);", 24, "obj.netRefProperty", "NetRef", "object")] [TestCase("Item item = new Item(); object list = item.netList;", 38, "item.netList", "NetList", "object")] // ↓ NetList field converted to a non-interface type [TestCase("Item item = new Item(); object list = item.netCollection;", 38, "item.netCollection", "NetCollection", "object")] - [TestCase("Item item = new Item(); int x = (int)item.netIntField;", 32, "item.netIntField", "NetInt", "int")] // ↓ explicit conversion to invalid type + [TestCase("Item item = new Item(); int x = (int)item.netIntField;", 32, "item.netIntField", "NetFieldBase", "int")] // ↓ explicit conversion to invalid type [TestCase("Item item = new Item(); int x = item.netRefField as object;", 32, "item.netRefField", "NetRef", "object")] public void AvoidImplicitNetFieldComparisons_RaisesDiagnostic(string codeText, int column, string expression, string fromType, string toType) { // arrange string code = NetFieldAnalyzerTests.SampleProgram.Replace("{{test-code}}", codeText); - DiagnosticResult expected = new DiagnosticResult + DiagnosticResult expected = new() { Id = "AvoidImplicitNetFieldCast", Message = $"This implicitly converts '{expression}' from {fromType} to {toType}, but {fromType} has unintuitive implicit conversion rules. Consider comparing against the actual value instead to avoid bugs. See https://smapi.io/package/avoid-implicit-net-field-cast for details.", @@ -135,7 +135,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests { // arrange string code = NetFieldAnalyzerTests.SampleProgram.Replace("{{test-code}}", codeText); - DiagnosticResult expected = new DiagnosticResult + DiagnosticResult expected = new() { Id = "AvoidNetField", Message = $"'{expression}' is a {netType} field; consider using the {suggestedProperty} property instead. See https://smapi.io/package/avoid-net-field for details.", diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/ObsoleteFieldAnalyzerTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/ObsoleteFieldAnalyzerTests.cs index 12641e1a..76607b8e 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/ObsoleteFieldAnalyzerTests.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/ObsoleteFieldAnalyzerTests.cs @@ -64,7 +64,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests { // arrange string code = ObsoleteFieldAnalyzerTests.SampleProgram.Replace("{{test-code}}", codeText); - DiagnosticResult expected = new DiagnosticResult + DiagnosticResult expected = new() { Id = "AvoidObsoleteField", Message = $"The '{oldName}' field is obsolete and should be replaced with '{newName}'. See https://smapi.io/package/avoid-obsolete-field for details.", diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/SMAPI.ModBuildConfig.Analyzer.Tests.csproj b/src/SMAPI.ModBuildConfig.Analyzer.Tests/SMAPI.ModBuildConfig.Analyzer.Tests.csproj index d0123e93..1719d39b 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/SMAPI.ModBuildConfig.Analyzer.Tests.csproj +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/SMAPI.ModBuildConfig.Analyzer.Tests.csproj @@ -1,18 +1,14 @@  - - netcoreapp2.0 + net5.0 latest - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + + + + @@ -20,5 +16,4 @@ - diff --git a/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerReleases.Shipped.md b/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerReleases.Shipped.md new file mode 100644 index 00000000..9a46676d --- /dev/null +++ b/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerReleases.Shipped.md @@ -0,0 +1,7 @@ +## Release 2.1.0 +### New Rules +Rule ID | Category | Severity | Notes +------------------------- | ------------------ | -------- | ------------------------------------------------------------ +AvoidImplicitNetFieldCast | SMAPI.CommonErrors | Warning | See [documentation](https://smapi.io/package/code-warnings). +AvoidNetField | SMAPI.CommonErrors | Warning | See [documentation](https://smapi.io/package/code-warnings). +AvoidObsoleteField | SMAPI.CommonErrors | Warning | See [documentation](https://smapi.io/package/code-warnings). diff --git a/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerUtilities.cs b/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerUtilities.cs index 68b5001e..2e34cf71 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerUtilities.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer/AnalyzerUtilities.cs @@ -40,8 +40,8 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer // invalid fromExpression = null; - fromType = default(TypeInfo); - toType = default(TypeInfo); + fromType = default; + toType = default; return false; } @@ -64,7 +64,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer } // conditional access - if (node is ConditionalAccessExpressionSyntax conditionalAccess && conditionalAccess.WhenNotNull is MemberBindingExpressionSyntax conditionalBinding) + if (node is ConditionalAccessExpressionSyntax { WhenNotNull: MemberBindingExpressionSyntax conditionalBinding } conditionalAccess) { declaringType = semanticModel.GetTypeInfo(conditionalAccess.Expression).Type; memberType = semanticModel.GetTypeInfo(node); @@ -74,7 +74,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer // invalid declaringType = null; - memberType = default(TypeInfo); + memberType = default; memberName = null; return false; } diff --git a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs index a9b981bd..553aae99 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs @@ -132,7 +132,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer }; /// The diagnostic info for an implicit net field cast. - private readonly DiagnosticDescriptor AvoidImplicitNetFieldCastRule = new DiagnosticDescriptor( + private readonly DiagnosticDescriptor AvoidImplicitNetFieldCastRule = new( id: "AvoidImplicitNetFieldCast", title: "Netcode types shouldn't be implicitly converted", messageFormat: "This implicitly converts '{0}' from {1} to {2}, but {1} has unintuitive implicit conversion rules. Consider comparing against the actual value instead to avoid bugs. See https://smapi.io/package/avoid-implicit-net-field-cast for details.", @@ -143,7 +143,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer ); /// The diagnostic info for an avoidable net field access. - private readonly DiagnosticDescriptor AvoidNetFieldRule = new DiagnosticDescriptor( + private readonly DiagnosticDescriptor AvoidNetFieldRule = new( id: "AvoidNetField", title: "Avoid Netcode types when possible", messageFormat: "'{0}' is a {1} field; consider using the {2} property instead. See https://smapi.io/package/avoid-net-field for details.", @@ -174,6 +174,9 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer /// The analysis context. public override void Initialize(AnalysisContext context) { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + context.RegisterSyntaxNodeAction( this.AnalyzeMemberAccess, SyntaxKind.SimpleMemberAccessExpression, @@ -224,10 +227,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer // warn: implicit conversion if (this.IsInvalidConversion(memberType.Type, memberType.ConvertedType)) - { context.ReportDiagnostic(Diagnostic.Create(this.AvoidImplicitNetFieldCastRule, context.Node.GetLocation(), context.Node, memberType.Type.Name, memberType.ConvertedType)); - return; - } }); } @@ -312,7 +312,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer return false; // conversion to implemented interface is OK - if (fromType.AllInterfaces.Contains(toType)) + if (fromType.AllInterfaces.Contains(toType, SymbolEqualityComparer.Default)) return false; // avoid any other conversions diff --git a/src/SMAPI.ModBuildConfig.Analyzer/ObsoleteFieldAnalyzer.cs b/src/SMAPI.ModBuildConfig.Analyzer/ObsoleteFieldAnalyzer.cs index d071f0c1..ba089513 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/ObsoleteFieldAnalyzer.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer/ObsoleteFieldAnalyzer.cs @@ -24,7 +24,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer /// Describes the diagnostic rule covered by the analyzer. private readonly IDictionary Rules = new Dictionary { - ["AvoidObsoleteField"] = new DiagnosticDescriptor( + ["AvoidObsoleteField"] = new( id: "AvoidObsoleteField", title: "Reference to obsolete field", messageFormat: "The '{0}' field is obsolete and should be replaced with '{1}'. See https://smapi.io/package/avoid-obsolete-field for details.", @@ -56,6 +56,9 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer /// The analysis context. public override void Initialize(AnalysisContext context) { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + context.RegisterSyntaxNodeAction( this.AnalyzeObsoleteFields, SyntaxKind.SimpleMemberAccessExpression, @@ -74,7 +77,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer try { // get reference info - if (!AnalyzerUtilities.TryGetMemberInfo(context.Node, context.SemanticModel, out ITypeSymbol declaringType, out TypeInfo memberType, out string memberName)) + if (!AnalyzerUtilities.TryGetMemberInfo(context.Node, context.SemanticModel, out ITypeSymbol declaringType, out _, out string memberName)) return; // suggest replacement diff --git a/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj b/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj index 0d109b83..7ac3277e 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj +++ b/src/SMAPI.ModBuildConfig.Analyzer/SMAPI.ModBuildConfig.Analyzer.csproj @@ -9,8 +9,11 @@ - - + + + + + diff --git a/src/SMAPI.ModBuildConfig/DeployModTask.cs b/src/SMAPI.ModBuildConfig/DeployModTask.cs index 9ee6be12..3508a6db 100644 --- a/src/SMAPI.ModBuildConfig/DeployModTask.cs +++ b/src/SMAPI.ModBuildConfig/DeployModTask.cs @@ -7,7 +7,12 @@ using System.Reflection; using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +using Newtonsoft.Json; using StardewModdingAPI.ModBuildConfig.Framework; +using StardewModdingAPI.Toolkit.Framework; +using StardewModdingAPI.Toolkit.Serialization; +using StardewModdingAPI.Toolkit.Serialization.Models; +using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.ModBuildConfig { @@ -17,6 +22,10 @@ namespace StardewModdingAPI.ModBuildConfig /********* ** Accessors *********/ + /// The name (without extension or path) of the current mod's DLL. + [Required] + public string ModDllName { get; set; } + /// The name of the mod folder. [Required] public string ModFolderName { get; set; } @@ -45,9 +54,15 @@ namespace StardewModdingAPI.ModBuildConfig [Required] public bool EnableModZip { get; set; } - /// Custom comma-separated regex patterns matching files to ignore when deploying or zipping the mod. + /// A comma-separated list of regex patterns matching files to ignore when deploying or zipping the mod. public string IgnoreModFilePatterns { get; set; } + /// A comma-separated list of relative file paths to ignore when deploying or zipping the mod. + public string IgnoreModFilePaths { get; set; } + + /// A comma-separated list of values which indicate which extra DLLs to bundle. + public string BundleExtraAssemblies { get; set; } + /********* ** Public methods @@ -64,16 +79,52 @@ namespace StardewModdingAPI.ModBuildConfig this.Log.LogMessage(MessageImportance.High, $"[mod build package] Handling build with options {string.Join(", ", properties)}"); } + // skip if nothing to do + // (This must be checked before the manifest validation, to allow cases like unit test projects.) if (!this.EnableModDeploy && !this.EnableModZip) - return true; // nothing to do + return true; + // validate the manifest file + IManifest manifest; + { + try + { + string manifestPath = Path.Combine(this.ProjectDir, "manifest.json"); + if (!new JsonHelper().ReadJsonFileIfExists(manifestPath, out Manifest rawManifest)) + { + this.Log.LogError("[mod build package] The mod's manifest.json file doesn't exist."); + return false; + } + manifest = rawManifest; + } + catch (JsonReaderException ex) + { + // log the inner exception, otherwise the message will be generic + Exception exToShow = ex.InnerException ?? ex; + this.Log.LogError($"[mod build package] The mod's manifest.json file isn't valid JSON: {exToShow.Message}"); + return false; + } + + // validate manifest fields + if (!ManifestValidator.TryValidateFields(manifest, out string error)) + { + this.Log.LogError($"[mod build package] The mod's manifest.json file is invalid: {error}"); + return false; + } + } + + // deploy files try { + // parse extra DLLs to bundle + ExtraAssemblyTypes bundleAssemblyTypes = this.GetExtraAssembliesToBundleOption(); + // parse ignore patterns + string[] ignoreFilePaths = this.GetCustomIgnoreFilePaths().ToArray(); Regex[] ignoreFilePatterns = this.GetCustomIgnorePatterns().ToArray(); // get mod info - ModFileManager package = new ModFileManager(this.ProjectDir, this.TargetDir, ignoreFilePatterns, validateRequiredModFiles: this.EnableModDeploy || this.EnableModZip); + ModFileManager package = new(this.ProjectDir, this.TargetDir, ignoreFilePaths, ignoreFilePatterns, bundleAssemblyTypes, this.ModDllName, validateRequiredModFiles: this.EnableModDeploy || this.EnableModZip); // deploy mod files if (this.EnableModDeploy) @@ -86,7 +137,7 @@ namespace StardewModdingAPI.ModBuildConfig // create release zip if (this.EnableModZip) { - string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModFolderName} {package.GetManifestVersion()}.zip"); + string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModFolderName} {manifest.Version}.zip"); string zipPath = Path.Combine(this.ModZipPath, zipName); this.Log.LogMessage(MessageImportance.High, $"[mod build package] Generating the release zip at {zipPath}..."); @@ -134,6 +185,28 @@ namespace StardewModdingAPI.ModBuildConfig } } + /// Parse the extra assembly types which should be bundled with the mod. + private ExtraAssemblyTypes GetExtraAssembliesToBundleOption() + { + ExtraAssemblyTypes flags = ExtraAssemblyTypes.None; + + if (!string.IsNullOrWhiteSpace(this.BundleExtraAssemblies)) + { + foreach (string raw in this.BundleExtraAssemblies.Split(',')) + { + if (!Enum.TryParse(raw, out ExtraAssemblyTypes type)) + { + this.Log.LogWarning($"[mod build package] Ignored invalid <{nameof(this.BundleExtraAssemblies)}> value '{raw}', expected one of '{string.Join("', '", Enum.GetNames(typeof(ExtraAssemblyTypes)))}'."); + continue; + } + + flags |= type; + } + } + + return flags; + } + /// Get the custom ignore patterns provided by the user. private IEnumerable GetCustomIgnorePatterns() { @@ -157,6 +230,29 @@ namespace StardewModdingAPI.ModBuildConfig } } + /// Get the custom relative file paths provided by the user to ignore. + private IEnumerable GetCustomIgnoreFilePaths() + { + if (string.IsNullOrWhiteSpace(this.IgnoreModFilePaths)) + yield break; + + foreach (string raw in this.IgnoreModFilePaths.Split(',')) + { + string path; + try + { + path = PathUtilities.NormalizePath(raw); + } + catch (Exception ex) + { + this.Log.LogWarning($"[mod build package] Ignored invalid <{nameof(this.IgnoreModFilePaths)}> path {raw}:\n{ex}"); + continue; + } + + yield return path; + } + } + /// Copy the mod files into the game's mod folder. /// The files to include. /// The folder path to create with the mod files. @@ -167,8 +263,7 @@ namespace StardewModdingAPI.ModBuildConfig string fromPath = entry.Value.FullName; string toPath = Path.Combine(modFolderPath, entry.Key); - // ReSharper disable once AssignNullToNotNullAttribute -- not applicable in this context - Directory.CreateDirectory(Path.GetDirectoryName(toPath)); + Directory.CreateDirectory(Path.GetDirectoryName(toPath)!); File.Copy(fromPath, toPath, overwrite: true); } @@ -186,7 +281,7 @@ namespace StardewModdingAPI.ModBuildConfig // create zip file Directory.CreateDirectory(Path.GetDirectoryName(zipPath)!); using Stream zipStream = new FileStream(zipPath, FileMode.Create, FileAccess.Write); - using ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create); + using ZipArchive archive = new(zipStream, ZipArchiveMode.Create); foreach (var fileEntry in files) { diff --git a/src/SMAPI.ModBuildConfig/Framework/ExtraAssemblyType.cs b/src/SMAPI.ModBuildConfig/Framework/ExtraAssemblyType.cs new file mode 100644 index 00000000..571bf7c7 --- /dev/null +++ b/src/SMAPI.ModBuildConfig/Framework/ExtraAssemblyType.cs @@ -0,0 +1,21 @@ +using System; + +namespace StardewModdingAPI.ModBuildConfig.Framework +{ + /// An extra assembly type for the field. + [Flags] + internal enum ExtraAssemblyTypes + { + /// Don't include extra assemblies. + None = 0, + + /// Assembly files which are part of MonoGame, SMAPI, or Stardew Valley. + Game = 1, + + /// Assembly files whose names start with Microsoft.* or System.*. + System = 2, + + /// Assembly files which don't match any other category. + ThirdParty = 4 + } +} diff --git a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs index 6dd595e5..d47e492a 100644 --- a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs +++ b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; -using StardewModdingAPI.Toolkit.Serialization; -using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.ModBuildConfig.Framework @@ -21,6 +19,45 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// The files that are part of the package. private readonly IDictionary Files; + /// The file extensions used by assembly files. + private readonly ISet AssemblyFileExtensions = new HashSet(StringComparer.OrdinalIgnoreCase) + { + ".dll", + ".exe", + ".pdb", + ".xml" + }; + + /// The DLLs which match the type. + private readonly ISet GameDllNames = new HashSet + { + // SMAPI + "0Harmony", + "Mono.Cecil", + "Mono.Cecil.Mdb", + "Mono.Cecil.Pdb", + "MonoMod.Common", + "Newtonsoft.Json", + "StardewModdingAPI", + "SMAPI.Toolkit", + "SMAPI.Toolkit.CoreInterfaces", + "TMXTile", + + // game + framework + "BmFont", + "FAudio-CS", + "GalaxyCSharp", + "GalaxyCSharpGlue", + "Lidgren.Network", + "MonoGame.Framework", + "SkiaSharp", + "Stardew Valley", + "StardewValley.GameData", + "Steamworks.NET", + "TextCopy", + "xTile" + }; + /********* ** Public methods @@ -28,10 +65,13 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// Construct an instance. /// The folder containing the project files. /// The folder containing the build output. + /// The custom relative file paths provided by the user to ignore. /// Custom regex patterns matching files to ignore when deploying or zipping the mod. + /// The extra assembly types which should be bundled with the mod. + /// The name (without extension or path) for the current mod's DLL. /// Whether to validate that required mod files like the manifest are present. /// The mod package isn't valid. - public ModFileManager(string projectDir, string targetDir, Regex[] ignoreFilePatterns, bool validateRequiredModFiles) + public ModFileManager(string projectDir, string targetDir, string[] ignoreFilePaths, Regex[] ignoreFilePatterns, ExtraAssemblyTypes bundleAssemblyTypes, string modDllName, bool validateRequiredModFiles) { this.Files = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -47,7 +87,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework string relativePath = entry.Item1; FileInfo file = entry.Item2; - if (!this.ShouldIgnore(file, relativePath, ignoreFilePatterns)) + if (!this.ShouldIgnore(file, relativePath, ignoreFilePaths, ignoreFilePatterns, bundleAssemblyTypes, modDllName)) this.Files[relativePath] = file; } @@ -71,16 +111,6 @@ namespace StardewModdingAPI.ModBuildConfig.Framework return new Dictionary(this.Files, StringComparer.OrdinalIgnoreCase); } - /// Get a semantic version from the mod manifest. - /// The manifest is missing or invalid. - public string GetManifestVersion() - { - if (!this.Files.TryGetValue(this.ManifestFileName, out FileInfo manifestFile) || !new JsonHelper().ReadJsonFileIfExists(manifestFile.FullName, out Manifest manifest)) - throw new InvalidOperationException($"The mod does not have a {this.ManifestFileName} file."); // shouldn't happen since we validate in constructor - - return manifest.Version.ToString(); - } - /********* ** Private methods @@ -94,7 +124,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework // project manifest bool hasProjectManifest = false; { - FileInfo manifest = new FileInfo(Path.Combine(projectDir, this.ManifestFileName)); + FileInfo manifest = new(Path.Combine(projectDir, this.ManifestFileName)); if (manifest.Exists) { yield return Tuple.Create(this.ManifestFileName, manifest); @@ -104,7 +134,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework // project i18n files bool hasProjectTranslations = false; - DirectoryInfo translationsFolder = new DirectoryInfo(Path.Combine(projectDir, "i18n")); + DirectoryInfo translationsFolder = new(Path.Combine(projectDir, "i18n")); if (translationsFolder.Exists) { foreach (FileInfo file in translationsFolder.EnumerateFiles()) @@ -114,7 +144,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework // project assets folder bool hasAssetsFolder = false; - DirectoryInfo assetsFolder = new DirectoryInfo(Path.Combine(projectDir, "assets")); + DirectoryInfo assetsFolder = new(Path.Combine(projectDir, "assets")); if (assetsFolder.Exists) { foreach (FileInfo file in assetsFolder.EnumerateFiles("*", SearchOption.AllDirectories)) @@ -126,7 +156,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework } // build output - DirectoryInfo buildFolder = new DirectoryInfo(targetDir); + DirectoryInfo buildFolder = new(targetDir); foreach (FileInfo file in buildFolder.EnumerateFiles("*", SearchOption.AllDirectories)) { // get path info @@ -149,36 +179,83 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// Get whether a build output file should be ignored. /// The file to check. /// The file's relative path in the package. + /// The custom relative file paths provided by the user to ignore. /// Custom regex patterns matching files to ignore when deploying or zipping the mod. - private bool ShouldIgnore(FileInfo file, string relativePath, Regex[] ignoreFilePatterns) + /// The extra assembly types which should be bundled with the mod. + /// The name (without extension or path) for the current mod's DLL. + private bool ShouldIgnore(FileInfo file, string relativePath, string[] ignoreFilePaths, Regex[] ignoreFilePatterns, ExtraAssemblyTypes bundleAssemblyTypes, string modDllName) { - return - // release zips - this.EqualsInvariant(file.Extension, ".zip") + // apply custom patterns + if (ignoreFilePaths.Any(p => p == relativePath) || ignoreFilePatterns.Any(p => p.IsMatch(relativePath))) + return true; - // Harmony (bundled into SMAPI) - || this.EqualsInvariant(file.Name, "0Harmony.dll") + // ignore unneeded files + { + bool shouldIgnore = + // release zips + this.EqualsInvariant(file.Extension, ".zip") - // Json.NET (bundled into SMAPI) - || this.EqualsInvariant(file.Name, "Newtonsoft.Json.dll") - || this.EqualsInvariant(file.Name, "Newtonsoft.Json.pdb") - || this.EqualsInvariant(file.Name, "Newtonsoft.Json.xml") + // *.deps.json (only SMAPI's top-level one is used) + || file.Name.EndsWith(".deps.json") - // mod translation class builder (not used at runtime) - || this.EqualsInvariant(file.Name, "Pathoschild.Stardew.ModTranslationClassBuilder.dll") - || this.EqualsInvariant(file.Name, "Pathoschild.Stardew.ModTranslationClassBuilder.pdb") - || this.EqualsInvariant(file.Name, "Pathoschild.Stardew.ModTranslationClassBuilder.xml") + // code analysis files + || file.Name.EndsWith(".CodeAnalysisLog.xml", StringComparison.OrdinalIgnoreCase) + || file.Name.EndsWith(".lastcodeanalysissucceeded", StringComparison.OrdinalIgnoreCase) - // code analysis files - || file.Name.EndsWith(".CodeAnalysisLog.xml", StringComparison.OrdinalIgnoreCase) - || file.Name.EndsWith(".lastcodeanalysissucceeded", StringComparison.OrdinalIgnoreCase) + // translation class builder (not used at runtime) + || ( + file.Name.StartsWith("Pathoschild.Stardew.ModTranslationClassBuilder") + && this.AssemblyFileExtensions.Contains(file.Extension) + ) - // OS metadata files - || this.EqualsInvariant(file.Name, ".DS_Store") - || this.EqualsInvariant(file.Name, "Thumbs.db") + // OS metadata files + || this.EqualsInvariant(file.Name, ".DS_Store") + || this.EqualsInvariant(file.Name, "Thumbs.db"); + if (shouldIgnore) + return true; + } - // custom ignore patterns - || ignoreFilePatterns.Any(p => p.IsMatch(relativePath)); + // ignore by assembly type + ExtraAssemblyTypes type = this.GetExtraAssemblyType(file, modDllName); + switch (bundleAssemblyTypes) + { + // Only explicitly-referenced assemblies are in the build output. These should be added to the zip, + // since it's possible the game won't load them (except game assemblies which will always be loaded + // separately). If they're already loaded, SMAPI will just ignore them. + case ExtraAssemblyTypes.None: + if (type is ExtraAssemblyTypes.Game) + return true; + break; + + // All assemblies are in the build output (due to how .NET builds references), but only those which + // match the bundled type should be in the zip. + default: + if (type != ExtraAssemblyTypes.None && !bundleAssemblyTypes.HasFlag(type)) + return true; + break; + } + + return false; + } + + /// Get the extra assembly type for a file, assuming that the user specified one or more extra types to bundle. + /// The file to check. + /// The name (without extension or path) for the current mod's DLL. + private ExtraAssemblyTypes GetExtraAssemblyType(FileInfo file, string modDllName) + { + string baseName = Path.GetFileNameWithoutExtension(file.Name); + string extension = file.Extension; + + if (baseName == modDllName || !this.AssemblyFileExtensions.Contains(extension)) + return ExtraAssemblyTypes.None; + + if (this.GameDllNames.Contains(baseName)) + return ExtraAssemblyTypes.Game; + + if (baseName.StartsWith("System.", StringComparison.OrdinalIgnoreCase) || baseName.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase)) + return ExtraAssemblyTypes.System; + + return ExtraAssemblyTypes.ThirdParty; } /// Get whether a string is equal to another case-insensitively. diff --git a/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj b/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj index 1813f58b..badabfc7 100644 --- a/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj +++ b/src/SMAPI.ModBuildConfig/SMAPI.ModBuildConfig.csproj @@ -2,33 +2,35 @@ StardewModdingAPI.ModBuildConfig - net45 - x86 + netstandard2.0 latest true + true Pathoschild.Stardew.ModBuildConfig Build package for SMAPI mods - 3.2.2 + 4.1.0 Pathoschild - Automates the build configuration for crossplatform Stardew Valley SMAPI mods. For SMAPI 3.0 or later. + Automates the build configuration for crossplatform Stardew Valley SMAPI mods. For SMAPI 3.13.0 or later. MIT images/icon.png https://smapi.io/package/readme - - - Reworked and streamlined how the package is compiled. - - Added SMAPI-ModTranslationClassBuilder files to the ignore list. - false + + + true - - - - - + + + + + @@ -49,5 +51,4 @@ - diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets index 65544b12..b4fd312e 100644 --- a/src/SMAPI.ModBuildConfig/build/smapi.targets +++ b/src/SMAPI.ModBuildConfig/build/smapi.targets @@ -8,12 +8,14 @@ ** Set build options **********************************************--> - - pdbonly + true - - $(AssemblySearchPaths);{GAC} + + false + + + None $(MSBuildProjectName) @@ -23,7 +25,14 @@ true false true - false + + + + <_BundleExtraAssembliesForGame>$([System.Text.RegularExpressions.Regex]::IsMatch('$(BundleExtraAssemblies)', '\bGame|All\b', RegexOptions.IgnoreCase)) + <_BundleExtraAssembliesForAny>$([System.Text.RegularExpressions.Regex]::IsMatch('$(BundleExtraAssemblies)', '\bGame|System|ThirdParty|All\b', RegexOptions.IgnoreCase)) + + + true @@ -37,40 +46,36 @@ - - - - - - - - + + + + + - - - - - - - - + + + - - - + + + - - - + + + + + + + @@ -79,6 +84,7 @@ **********************************************--> diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs index e84445d7..66f2f105 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs @@ -1,7 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Globalization; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands @@ -53,7 +53,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands /// The parsed value. /// Whether to show an error if the argument is missing. /// Require that the argument match one of the given values (case-insensitive). - public bool TryGet(int index, string name, out string value, bool required = true, string[] oneOf = null) + public bool TryGet(int index, string name, [NotNullWhen(true)] out string? value, bool required = true, string[]? oneOf = null) { value = null; @@ -87,7 +87,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands value = 0; // get argument - if (!this.TryGet(index, name, out string raw, required)) + if (!this.TryGet(index, name, out string? raw, required)) return false; // parse @@ -107,38 +107,6 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands return true; } - /// Try to read a decimal argument. - /// The argument index. - /// The argument name for error messages. - /// The parsed value. - /// Whether to show an error if the argument is missing. - /// The minimum value allowed. - /// The maximum value allowed. - public bool TryGetDecimal(int index, string name, out decimal value, bool required = true, decimal? min = null, decimal? max = null) - { - value = 0; - - // get argument - if (!this.TryGet(index, name, out string raw, required)) - return false; - - // parse - if (!decimal.TryParse(raw, NumberStyles.Number, CultureInfo.InvariantCulture, out value)) - { - this.LogDecimalFormatError(index, name, min, max); - return false; - } - - // validate - if ((min.HasValue && value < min) || (max.HasValue && value > max)) - { - this.LogDecimalFormatError(index, name, min, max); - return false; - } - - return true; - } - /// Returns an enumerator that iterates through the collection. /// An enumerator that can be used to iterate through the collection. public IEnumerator GetEnumerator() @@ -180,22 +148,5 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands else this.LogError($"Argument {index} ({name}) must be an integer."); } - - /// Print an error for an invalid decimal argument. - /// The argument index. - /// The argument name for error messages. - /// The minimum value allowed. - /// The maximum value allowed. - private void LogDecimalFormatError(int index, string name, decimal? min, decimal? max) - { - if (min.HasValue && max.HasValue) - this.LogError($"Argument {index} ({name}) must be a decimal between {min} and {max}."); - else if (min.HasValue) - this.LogError($"Argument {index} ({name}) must be a decimal and at least {min}."); - else if (max.HasValue) - this.LogError($"Argument {index} ({name}) must be a decimal and at most {max}."); - else - this.LogError($"Argument {index} ({name}) must be a decimal."); - } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs similarity index 89% rename from src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs rename to src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs index 77a26c6a..44b7824e 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs @@ -4,8 +4,8 @@ using System.Linq; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands { - /// The base implementation for a trainer command. - internal abstract class TrainerCommand : ITrainerCommand + /// The base implementation for a console command. + internal abstract class ConsoleCommand : IConsoleCommand { /********* ** Accessors @@ -50,7 +50,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands /// The command description. /// Whether the command may need to perform logic when the player presses a button. /// Whether the command may need to perform logic when the game updates. - protected TrainerCommand(string name, string description, bool mayNeedInput = false, bool mayNeedUpdate = false) + protected ConsoleCommand(string name, string description, bool mayNeedInput = false, bool mayNeedUpdate = false) { this.Name = name; this.Description = description; @@ -78,8 +78,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands /// The data to display. /// The table header. /// Returns a set of fields for a data value. - /// Whether to right-align the data. - protected string GetTableString(IEnumerable data, string[] header, Func getRow, bool rightAlign = false) + protected string GetTableString(IEnumerable data, string[] header, Func getRow) { // get table data int[] widths = header.Select(p => p.Length).ToArray(); @@ -101,14 +100,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands List lines = new List(rows.Length + 2) { header, - header.Select((value, i) => "".PadRight(widths[i], '-')).ToArray() + header.Select((_, i) => "".PadRight(widths[i], '-')).ToArray() }; lines.AddRange(rows); return string.Join( Environment.NewLine, lines.Select(line => string.Join(" | ", - line.Select((field, i) => rightAlign ? field.PadRight(widths[i], ' ') : field.PadLeft(widths[i], ' ')) + line.Select((field, i) => field.PadLeft(widths[i], ' ')) )) ); } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs similarity index 97% rename from src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs rename to src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs index d4d36e5d..9c82bbd3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs @@ -1,7 +1,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands { /// A console command to register. - internal interface ITrainerCommand + internal interface IConsoleCommand { /********* ** Accessors diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs new file mode 100644 index 00000000..f2194cff --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using StardewValley; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other +{ + /// A command which runs one of the game's save migrations. + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ApplySaveFixCommand : ConsoleCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public ApplySaveFixCommand() + : base("apply_save_fix", "Apply one of the game's save migrations to the currently loaded save. WARNING: This may corrupt or make permanent changes to your save. DO NOT USE THIS unless you're absolutely sure.\n\nUsage: apply_save_fix list\nList all valid save IDs.\n\nUsage: apply_save_fix \nApply the named save fix.") { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // get fix ID + if (!args.TryGet(0, "fix_id", out string? rawFixId, required: false)) + { + monitor.Log("Invalid usage. Type 'help apply_save_fix' for details.", LogLevel.Error); + return; + } + rawFixId = rawFixId.Trim(); + + + // list mode + if (rawFixId == "list") + { + monitor.Log("Valid save fix IDs:\n - " + string.Join("\n - ", this.GetSaveIds()), LogLevel.Info); + return; + } + + // validate fix ID + if (!Enum.TryParse(rawFixId, ignoreCase: true, out SaveGame.SaveFixes fixId)) + { + monitor.Log($"Invalid save ID '{rawFixId}'. Type 'help apply_save_fix' for details.", LogLevel.Error); + return; + } + + // apply + monitor.Log("THIS MAY CAUSE PERMANENT CHANGES TO YOUR SAVE FILE. If you're not sure, exit your game without saving to avoid issues.", LogLevel.Warn); + monitor.Log($"Trying to apply save fix ID: '{fixId}'.", LogLevel.Warn); + try + { + Game1.applySaveFix(fixId); + monitor.Log("Save fix applied.", LogLevel.Info); + } + catch (Exception ex) + { + monitor.Log("Applying save fix failed. The save may be in an invalid state; you should exit your game now without saving to avoid issues.", LogLevel.Error); + monitor.Log($"Technical details: {ex}", LogLevel.Debug); + } + } + + + /********* + ** Private methods + *********/ + /// Get the valid save fix IDs. + private IEnumerable GetSaveIds() + { + foreach (SaveGame.SaveFixes id in Enum.GetValues(typeof(SaveGame.SaveFixes))) + { + if (id == SaveGame.SaveFixes.MAX) + continue; + + yield return id.ToString(); + } + } + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs index e4010111..cf1dcbce 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs @@ -1,9 +1,11 @@ -using StardewValley; +using System.Diagnostics.CodeAnalysis; +using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which sends a debug command to the game. - internal class DebugCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class DebugCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/PerformanceCounterCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/PerformanceCounterCommand.cs deleted file mode 100644 index 63851c9d..00000000 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/PerformanceCounterCommand.cs +++ /dev/null @@ -1,647 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using StardewModdingAPI.Framework; -using StardewModdingAPI.Framework.PerformanceMonitoring; - -namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other -{ - /// A set of commands which displays or configures performance monitoring. - internal class PerformanceCounterCommand : TrainerCommand - { - /********* - ** Fields - *********/ - /// The name of the command. - private const string CommandName = "performance"; - - /// The available commands. - private enum SubCommand - { - Summary, - Detail, - Reset, - Trigger, - Enable, - Disable, - Help - } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - public PerformanceCounterCommand() - : base(CommandName, PerformanceCounterCommand.GetDescription()) { } - - /// Handle the command. - /// Writes messages to the console and log file. - /// The command name. - /// The command arguments. - public override void Handle(IMonitor monitor, string command, ArgumentParser args) - { - // parse args - SubCommand subcommand = SubCommand.Summary; - { - if (args.TryGet(0, "command", out string subcommandStr, false) && !Enum.TryParse(subcommandStr, ignoreCase: true, out subcommand)) - { - this.LogUsageError(monitor, $"Unknown command {subcommandStr}"); - return; - } - } - - // handle - switch (subcommand) - { - case SubCommand.Summary: - this.HandleSummarySubCommand(monitor, args); - break; - - case SubCommand.Detail: - this.HandleDetailSubCommand(monitor, args); - break; - - case SubCommand.Reset: - this.HandleResetSubCommand(monitor, args); - break; - - case SubCommand.Trigger: - this.HandleTriggerSubCommand(monitor, args); - break; - - case SubCommand.Enable: - SCore.PerformanceMonitor.EnableTracking = true; - monitor.Log("Performance counter tracking is now enabled", LogLevel.Info); - break; - - case SubCommand.Disable: - SCore.PerformanceMonitor.EnableTracking = false; - monitor.Log("Performance counter tracking is now disabled", LogLevel.Info); - break; - - case SubCommand.Help: - this.OutputHelp(monitor, args.TryGet(1, "command", out _) ? subcommand : null as SubCommand?); - break; - - default: - this.LogUsageError(monitor, $"Unknown command {subcommand}"); - break; - } - } - - - /********* - ** Private methods - *********/ - /// Handles the summary sub command. - /// Writes messages to the console and log file. - /// The command arguments. - private void HandleSummarySubCommand(IMonitor monitor, ArgumentParser args) - { - if (!this.AssertEnabled(monitor)) - return; - - IEnumerable data = SCore.PerformanceMonitor.GetCollections(); - - double? threshold = null; - if (args.TryGetDecimal(1, "threshold", out decimal t, required: false)) - threshold = (double?)t; - - TimeSpan interval = TimeSpan.FromSeconds(60); - - StringBuilder report = new StringBuilder(); - report.AppendLine($"Summary over the last {interval.TotalSeconds} seconds:"); - report.AppendLine(this.GetTableString( - data: data, - header: new[] { "Collection", "Avg Calls/s", "Avg Exec Time (Game)", "Avg Exec Time (Mods)", "Avg Exec Time (Game+Mods)", "Peak Exec Time" }, - getRow: item => new[] - { - item.Name, - item.GetAverageCallsPerSecond().ToString(), - this.FormatMilliseconds(item.GetGameAverageExecutionTime(interval), threshold), - this.FormatMilliseconds(item.GetModsAverageExecutionTime(interval), threshold), - this.FormatMilliseconds(item.GetAverageExecutionTime(interval), threshold), - this.FormatMilliseconds(item.GetPeakExecutionTime(interval), threshold) - }, - true - )); - - monitor.Log(report.ToString(), LogLevel.Info); - } - - /// Handles the detail sub command. - /// Writes messages to the console and log file. - /// The command arguments. - private void HandleDetailSubCommand(IMonitor monitor, ArgumentParser args) - { - if (!this.AssertEnabled(monitor)) - return; - - // parse args - double thresholdMilliseconds = 0; - if (args.TryGetDecimal(1, "threshold", out decimal t, required: false)) - thresholdMilliseconds = (double)t; - - // get collections - var collections = SCore.PerformanceMonitor.GetCollections(); - - // render - TimeSpan averageInterval = TimeSpan.FromSeconds(60); - StringBuilder report = new StringBuilder($"Showing details for performance counters of {thresholdMilliseconds}+ milliseconds:\n\n"); - bool anyShown = false; - foreach (PerformanceCounterCollection collection in collections) - { - KeyValuePair[] data = collection.PerformanceCounters - .Where(p => p.Value.GetAverage(averageInterval) >= thresholdMilliseconds) - .ToArray(); - - if (data.Any()) - { - anyShown = true; - report.AppendLine($"{collection.Name}:"); - report.AppendLine(this.GetTableString( - data: data, - header: new[] { "Mod", $"Avg Exec Time (last {(int)averageInterval.TotalSeconds}s)", "Last Exec Time", "Peak Exec Time", $"Peak Exec Time (last {(int)averageInterval.TotalSeconds}s)" }, - getRow: item => new[] - { - item.Key, - this.FormatMilliseconds(item.Value.GetAverage(averageInterval), thresholdMilliseconds), - this.FormatMilliseconds(item.Value.GetLastEntry()?.ElapsedMilliseconds), - this.FormatMilliseconds(item.Value.GetPeak()?.ElapsedMilliseconds), - this.FormatMilliseconds(item.Value.GetPeak(averageInterval)?.ElapsedMilliseconds) - }, - true - )); - } - } - - if (!anyShown) - report.AppendLine("No performance counters found."); - - monitor.Log(report.ToString(), LogLevel.Info); - } - - /// Handles the trigger sub command. - /// Writes messages to the console and log file. - /// The command arguments. - private void HandleTriggerSubCommand(IMonitor monitor, ArgumentParser args) - { - if (!this.AssertEnabled(monitor)) - return; - - if (args.TryGet(1, "mode", out string mode, false)) - { - switch (mode) - { - case "list": - this.OutputAlertTriggers(monitor); - break; - - case "collection": - if (args.TryGet(2, "name", out string collectionName)) - { - if (args.TryGetDecimal(3, "threshold", out decimal threshold)) - { - if (!args.TryGet(4, "source", out string source, required: false)) - source = null; - this.ConfigureAlertTrigger(monitor, collectionName, source, threshold); - } - } - break; - - case "pause": - SCore.PerformanceMonitor.PauseAlerts = true; - monitor.Log("Alerts are now paused.", LogLevel.Info); - break; - - case "resume": - SCore.PerformanceMonitor.PauseAlerts = false; - monitor.Log("Alerts are now resumed.", LogLevel.Info); - break; - - case "dump": - this.OutputAlertTriggers(monitor, true); - break; - - case "clear": - this.ClearAlertTriggers(monitor); - break; - - default: - this.LogUsageError(monitor, $"Unknown mode {mode}. See '{CommandName} help trigger' for usage."); - break; - } - } - else - this.OutputAlertTriggers(monitor); - } - - /// Sets up an an alert trigger. - /// Writes messages to the console and log file. - /// The name of the collection. - /// The name of the source, or null for all sources. - /// The trigger threshold, or 0 to remove. - private void ConfigureAlertTrigger(IMonitor monitor, string collectionName, string sourceName, decimal threshold) - { - foreach (PerformanceCounterCollection collection in SCore.PerformanceMonitor.GetCollections()) - { - if (collection.Name.ToLowerInvariant().Equals(collectionName.ToLowerInvariant())) - { - if (sourceName == null) - { - if (threshold != 0) - { - collection.EnableAlerts = true; - collection.AlertThresholdMilliseconds = (double)threshold; - monitor.Log($"Set up alert triggering for '{collectionName}' with '{this.FormatMilliseconds((double?)threshold)}'", LogLevel.Info); - } - else - { - collection.EnableAlerts = false; - monitor.Log($"Cleared alert triggering for '{collection}'."); - } - - return; - } - else - { - foreach (var performanceCounter in collection.PerformanceCounters) - { - if (performanceCounter.Value.Source.ToLowerInvariant().Equals(sourceName.ToLowerInvariant())) - { - if (threshold != 0) - { - performanceCounter.Value.EnableAlerts = true; - performanceCounter.Value.AlertThresholdMilliseconds = (double)threshold; - monitor.Log($"Set up alert triggering for '{sourceName}' in collection '{collectionName}' with '{this.FormatMilliseconds((double?)threshold)}", LogLevel.Info); - } - else - performanceCounter.Value.EnableAlerts = false; - return; - } - } - - monitor.Log($"Could not find the source '{sourceName}' in collection '{collectionName}'", LogLevel.Warn); - return; - } - } - } - - monitor.Log($"Could not find the collection '{collectionName}'", LogLevel.Warn); - } - - - /// Clears alert triggering for all collections. - /// Writes messages to the console and log file. - private void ClearAlertTriggers(IMonitor monitor) - { - int clearedTriggers = 0; - foreach (PerformanceCounterCollection collection in SCore.PerformanceMonitor.GetCollections()) - { - if (collection.EnableAlerts) - { - collection.EnableAlerts = false; - clearedTriggers++; - } - - foreach (var performanceCounter in collection.PerformanceCounters) - { - if (performanceCounter.Value.EnableAlerts) - { - performanceCounter.Value.EnableAlerts = false; - clearedTriggers++; - } - } - - } - - monitor.Log($"Cleared {clearedTriggers} alert triggers.", LogLevel.Info); - } - - /// Lists all configured alert triggers. - /// Writes messages to the console and log file. - /// True to dump the triggers as commands. - private void OutputAlertTriggers(IMonitor monitor, bool asDump = false) - { - StringBuilder report = new StringBuilder(); - report.AppendLine("Configured triggers:"); - report.AppendLine(); - var collectionTriggers = new List(); - var sourceTriggers = new List(); - - foreach (PerformanceCounterCollection collection in SCore.PerformanceMonitor.GetCollections()) - { - if (collection.EnableAlerts) - collectionTriggers.Add(new CollectionTrigger(collection.Name, collection.AlertThresholdMilliseconds)); - - sourceTriggers.AddRange( - from counter in collection.PerformanceCounters - where counter.Value.EnableAlerts - select new SourceTrigger(collection.Name, counter.Value.Source, counter.Value.AlertThresholdMilliseconds) - ); - } - - if (collectionTriggers.Count > 0) - { - report.AppendLine("Collection Triggers:"); - report.AppendLine(); - - if (asDump) - { - foreach (var item in collectionTriggers) - report.AppendLine($"{CommandName} trigger {item.CollectionName} {item.Threshold}"); - } - else - { - report.AppendLine(this.GetTableString( - data: collectionTriggers, - header: new[] { "Collection", "Threshold" }, - getRow: item => new[] { item.CollectionName, this.FormatMilliseconds(item.Threshold) }, - true - )); - } - - report.AppendLine(); - } - else - report.AppendLine("No collection triggers."); - - if (sourceTriggers.Count > 0) - { - report.AppendLine("Source Triggers:"); - report.AppendLine(); - - if (asDump) - { - foreach (SourceTrigger item in sourceTriggers) - report.AppendLine($"{CommandName} trigger {item.CollectionName} {item.Threshold} {item.SourceName}"); - } - else - { - report.AppendLine(this.GetTableString( - data: sourceTriggers, - header: new[] { "Collection", "Source", "Threshold" }, - getRow: item => new[] { item.CollectionName, item.SourceName, this.FormatMilliseconds(item.Threshold) }, - true - )); - } - - report.AppendLine(); - } - else - report.AppendLine("No source triggers."); - - monitor.Log(report.ToString(), LogLevel.Info); - } - - /// Handles the reset sub command. - /// Writes messages to the console and log file. - /// The command arguments. - private void HandleResetSubCommand(IMonitor monitor, ArgumentParser args) - { - if (!this.AssertEnabled(monitor)) - return; - - if (args.TryGet(1, "type", out string type, false, new[] { "category", "source" })) - { - args.TryGet(2, "name", out string name); - - switch (type) - { - case "category": - SCore.PerformanceMonitor.ResetCollection(name); - monitor.Log($"All performance counters for category {name} are now cleared.", LogLevel.Info); - break; - case "source": - SCore.PerformanceMonitor.ResetSource(name); - monitor.Log($"All performance counters for source {name} are now cleared.", LogLevel.Info); - break; - } - } - else - { - SCore.PerformanceMonitor.Reset(); - monitor.Log("All performance counters are now cleared.", LogLevel.Info); - } - } - - /// Formats the given milliseconds value into a string format. Optionally - /// allows a threshold to return "-" if the value is less than the threshold. - /// The milliseconds to format. Returns "-" if null - /// The threshold. Any value below this is returned as "-". - /// The formatted milliseconds. - private string FormatMilliseconds(double? milliseconds, double? thresholdMilliseconds = null) - { - thresholdMilliseconds ??= 1; - return milliseconds != null && milliseconds >= thresholdMilliseconds - ? ((double)milliseconds).ToString("F2") - : "-"; - } - - /// Shows detailed help for a specific sub command. - /// The output monitor. - /// The subcommand. - private void OutputHelp(IMonitor monitor, SubCommand? subcommand) - { - StringBuilder report = new StringBuilder(); - report.AppendLine(); - - switch (subcommand) - { - case SubCommand.Detail: - report.AppendLine($" {CommandName} detail "); - report.AppendLine(); - report.AppendLine("Displays details for a specific collection."); - report.AppendLine(); - report.AppendLine("Arguments:"); - report.AppendLine(" Optional. The threshold in milliseconds. Any average execution time below that"); - report.AppendLine(" threshold is not reported."); - report.AppendLine(); - report.AppendLine("Examples:"); - report.AppendLine($"{CommandName} detail 5 Show counters exceeding an average of 5ms"); - break; - - case SubCommand.Summary: - report.AppendLine($"Usage: {CommandName} summary "); - report.AppendLine(); - report.AppendLine("Displays the performance counter summary."); - report.AppendLine(); - report.AppendLine("Arguments:"); - report.AppendLine(" Optional. Hides the actual execution time if it's below this threshold"); - report.AppendLine(); - report.AppendLine("Examples:"); - report.AppendLine($"{CommandName} summary Show all events"); - report.AppendLine($"{CommandName} summary 5 Shows events exceeding an average of 5ms"); - break; - - case SubCommand.Trigger: - report.AppendLine($"Usage: {CommandName} trigger "); - report.AppendLine($"Usage: {CommandName} trigger collection "); - report.AppendLine($"Usage: {CommandName} trigger collection "); - report.AppendLine(); - report.AppendLine("Manages alert triggers."); - report.AppendLine(); - report.AppendLine("Arguments:"); - report.AppendLine(" Optional. Specifies if a specific source or a specific collection should be triggered."); - report.AppendLine(" - list Lists current triggers"); - report.AppendLine(" - collection Sets up a trigger for a collection"); - report.AppendLine(" - clear Clears all trigger entries"); - report.AppendLine(" - pause Pauses triggering of alerts"); - report.AppendLine(" - resume Resumes triggering of alerts"); - report.AppendLine(" - dump Dumps all triggers as commands for copy and paste"); - report.AppendLine(" Defaults to 'list' if not specified."); - report.AppendLine(); - report.AppendLine(" Required if the mode 'collection' is specified."); - report.AppendLine(" Specifies the name of the collection to be triggered. Must be an exact match."); - report.AppendLine(); - report.AppendLine(" Optional. Specifies the name of a specific source. Must be an exact match."); - report.AppendLine(); - report.AppendLine(" Required if the mode 'collection' is specified."); - report.AppendLine(" Specifies the threshold in milliseconds (fractions allowed)."); - report.AppendLine(" Specify '0' to remove the threshold."); - report.AppendLine(); - report.AppendLine("Examples:"); - report.AppendLine(); - report.AppendLine($"{CommandName} trigger collection Display.Rendering 10"); - report.AppendLine(" Sets up an alert trigger which writes on the console if the execution time of all performance counters in"); - report.AppendLine(" the 'Display.Rendering' collection exceed 10 milliseconds."); - report.AppendLine(); - report.AppendLine($"{CommandName} trigger collection Display.Rendering 5 Pathoschild.ChestsAnywhere"); - report.AppendLine(" Sets up an alert trigger to write on the console if the execution time of Pathoschild.ChestsAnywhere in"); - report.AppendLine(" the 'Display.Rendering' collection exceed 5 milliseconds."); - report.AppendLine(); - report.AppendLine($"{CommandName} trigger collection Display.Rendering 0"); - report.AppendLine(" Removes the threshold previously defined from the collection. Note that source-specific thresholds are left intact."); - report.AppendLine(); - report.AppendLine($"{CommandName} trigger clear"); - report.AppendLine(" Clears all previously setup alert triggers."); - break; - - case SubCommand.Reset: - report.AppendLine($"Usage: {CommandName} reset "); - report.AppendLine(); - report.AppendLine("Resets performance counters."); - report.AppendLine(); - report.AppendLine("Arguments:"); - report.AppendLine(" Optional. Specifies if a collection or source should be reset."); - report.AppendLine(" If omitted, all performance counters are reset."); - report.AppendLine(); - report.AppendLine(" - source Clears performance counters for a specific source"); - report.AppendLine(" - collection Clears performance counters for a specific collection"); - report.AppendLine(); - report.AppendLine(" Required if a is given. Specifies the name of either the collection"); - report.AppendLine(" or the source. The name must be an exact match."); - report.AppendLine(); - report.AppendLine("Examples:"); - report.AppendLine($"{CommandName} reset Resets all performance counters"); - report.AppendLine($"{CommandName} reset source Pathoschild.ChestsAnywhere Resets all performance for the source named Pathoschild.ChestsAnywhere"); - report.AppendLine($"{CommandName} reset collection Display.Rendering Resets all performance for the collection named Display.Rendering"); - break; - } - - report.AppendLine(); - monitor.Log(report.ToString(), LogLevel.Info); - } - - /// Get the command description. - private static string GetDescription() - { - StringBuilder report = new StringBuilder(); - - report.AppendLine("Displays or configures performance monitoring to diagnose issues. Performance monitoring is disabled by default."); - report.AppendLine(); - report.AppendLine("For example, the counter collection named 'Display.Rendered' contains one performance"); - report.AppendLine("counter when the game executes the 'Display.Rendered' event, and another counter for each mod which handles it."); - report.AppendLine(); - report.AppendLine($"Usage: {CommandName} "); - report.AppendLine(); - report.AppendLine("Commands:"); - report.AppendLine(); - report.AppendLine(" summary Show a summary of collections."); - report.AppendLine(" detail Show a summary for a given collection."); - report.AppendLine(" reset Reset all performance counters."); - report.AppendLine(" trigger Configure alert triggers."); - report.AppendLine(" enable Enable performance counter recording."); - report.AppendLine(" disable Disable performance counter recording."); - report.AppendLine(" help Show verbose help for the available commands."); - report.AppendLine(); - report.AppendLine($"To get help for a specific command, use '{CommandName} help ', for example:"); - report.AppendLine($"{CommandName} help summary"); - report.AppendLine(); - report.AppendLine("Defaults to summary if no command is given."); - report.AppendLine(); - - return report.ToString(); - } - - /// Log a warning if performance monitoring isn't enabled. - /// Writes messages to the console and log file. - /// Returns whether performance monitoring is enabled. - private bool AssertEnabled(IMonitor monitor) - { - if (!SCore.PerformanceMonitor.EnableTracking) - { - monitor.Log($"Performance monitoring is currently disabled; enter '{CommandName} enable' to enable it.", LogLevel.Warn); - return false; - } - - return true; - } - - - /********* - ** Private models - *********/ - /// An alert trigger for a collection. - private class CollectionTrigger - { - /********* - ** Accessors - *********/ - /// The collection name. - public string CollectionName { get; } - - /// The trigger threshold. - public double Threshold { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The collection name. - /// The trigger threshold. - public CollectionTrigger(string collectionName, double threshold) - { - this.CollectionName = collectionName; - this.Threshold = threshold; - } - } - - /// An alert triggered for a source. - private class SourceTrigger : CollectionTrigger - { - /********* - ** Accessors - *********/ - /// The source name. - public string SourceName { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The collection name. - /// The source name. - /// The trigger threshold. - public SourceTrigger(string collectionName, string sourceName, double threshold) - : base(collectionName, threshold) - { - this.SourceName = sourceName; - } - } - } -} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs new file mode 100644 index 00000000..159d7c4a --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; +using Netcode; +using StardewValley; +using StardewValley.Network; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other +{ + /// A command which regenerates the game's bundles. + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class RegenerateBundlesCommand : ConsoleCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public RegenerateBundlesCommand() + : base("regenerate_bundles", $"Regenerate the game's community center bundle data. WARNING: this will reset all bundle progress, and may have unintended effects if you've already completed bundles. DO NOT USE THIS unless you're absolutely sure.\n\nUsage: regenerate_bundles confirm [] [ignore_seed]\nRegenerate all bundles for this save. If the is set to '{string.Join("' or '", Enum.GetNames(typeof(Game1.BundleType)))}', change the bundle type for the save. If an 'ignore_seed' option is included, remixed bundles are re-randomized without using the predetermined save seed.\n\nExample: regenerate_bundles remixed confirm") { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // get flags + var bundleType = Game1.bundleType; + bool confirmed = false; + bool useSeed = true; + foreach (string arg in args) + { + if (arg.Equals("confirm", StringComparison.OrdinalIgnoreCase)) + confirmed = true; + else if (arg.Equals("ignore_seed", StringComparison.OrdinalIgnoreCase)) + useSeed = false; + else if (Enum.TryParse(arg, ignoreCase: true, out Game1.BundleType type)) + bundleType = type; + else + { + monitor.Log($"Invalid option '{arg}'. Type 'help {command}' for usage.", LogLevel.Error); + return; + } + } + + // require confirmation + if (!confirmed) + { + monitor.Log($"WARNING: this may have unintended consequences (type 'help {command}' for details). Are you sure?", LogLevel.Warn); + + string[] newArgs = args.Concat(new[] { "confirm" }).ToArray(); + monitor.Log($"To confirm, enter this command: '{command} {string.Join(" ", newArgs)}'.", LogLevel.Info); + return; + } + + // need a loaded save + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + + // get private fields + IWorldState state = Game1.netWorldState.Value; + var bundleData = state.GetType().GetField("_bundleData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)?.GetValue(state) as IDictionary + ?? throw new InvalidOperationException("Can't access '_bundleData' field on world state."); + var netBundleData = state.GetType().GetField("netBundleData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)?.GetValue(state) as NetStringDictionary + ?? throw new InvalidOperationException("Can't access 'netBundleData' field on world state."); + + // clear bundle data + state.BundleData.Clear(); + state.Bundles.Clear(); + state.BundleRewards.Clear(); + bundleData.Clear(); + netBundleData.Clear(); + + // regenerate bundles + var locale = LocalizedContentManager.CurrentLanguageCode; + try + { + LocalizedContentManager.CurrentLanguageCode = LocalizedContentManager.LanguageCode.en; // the base bundle data needs to be unlocalized (the game will add localized names later) + + Game1.bundleType = bundleType; + Game1.GenerateBundles(bundleType, use_seed: useSeed); + } + finally + { + LocalizedContentManager.CurrentLanguageCode = locale; + } + + monitor.Log("Regenerated bundles and reset bundle progress.", LogLevel.Info); + monitor.Log("This may have unintended effects if you've already completed any bundles. If you're not sure, exit your game without saving to cancel.", LogLevel.Warn); + } + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs index 54d27185..a233d588 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs @@ -1,9 +1,11 @@ -using System.Diagnostics; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which shows the data files. - internal class ShowDataFilesCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ShowDataFilesCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs index 0257892f..745b821b 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs @@ -1,9 +1,11 @@ -using System.Diagnostics; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which shows the game files. - internal class ShowGameFilesCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ShowGameFilesCommand : ConsoleCommand { /********* ** Public methods @@ -18,8 +20,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other /// The command arguments. public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - Process.Start(Constants.ExecutionPath); - monitor.Log($"OK, opening {Constants.ExecutionPath}.", LogLevel.Info); + Process.Start(Constants.GamePath); + monitor.Log($"OK, opening {Constants.GamePath}.", LogLevel.Info); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs index 11aa10c3..8bf9f5db 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs @@ -1,9 +1,11 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which logs the keys being pressed for 30 seconds once enabled. - internal class TestInputCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class TestInputCommand : ConsoleCommand { /********* ** Fields @@ -37,9 +39,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other public override void OnUpdated(IMonitor monitor) { // handle expiry - if (this.ExpiryTicks == null) - return; - if (this.ExpiryTicks <= DateTime.UtcNow.Ticks) + if (this.ExpiryTicks != null && this.ExpiryTicks <= DateTime.UtcNow.Ticks) { monitor.Log("No longer logging input.", LogLevel.Info); this.ExpiryTicks = null; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 6cb2b624..74d3d9df 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -7,13 +7,13 @@ using Object = StardewValley.Object; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which adds an item to the player inventory. - internal class AddCommand : TrainerCommand + internal class AddCommand : ConsoleCommand { /********* ** Fields *********/ /// Provides methods for searching and constructing items. - private readonly ItemRepository Items = new ItemRepository(); + private readonly ItemRepository Items = new(); /// The type names recognized by this command. private readonly string[] ValidTypes = Enum.GetNames(typeof(ItemType)).Concat(new[] { "Name" }).ToArray(); @@ -40,7 +40,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } // read arguments - if (!args.TryGet(0, "item type", out string type, oneOf: this.ValidTypes)) + if (!args.TryGet(0, "item type", out string? type, oneOf: this.ValidTypes)) return; if (!args.TryGetInt(2, "count", out int count, min: 1, required: false)) count = 1; @@ -48,7 +48,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player quality = Object.lowQuality; // find matching item - SearchableItem match = Enum.TryParse(type, true, out ItemType itemType) + SearchableItem? match = Enum.TryParse(type, true, out ItemType itemType) ? this.FindItemByID(monitor, args, itemType) : this.FindItemByName(monitor, args); if (match == null) @@ -76,14 +76,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Writes messages to the console and log file. /// The command arguments. /// The item type. - private SearchableItem FindItemByID(IMonitor monitor, ArgumentParser args, ItemType type) + private SearchableItem? FindItemByID(IMonitor monitor, ArgumentParser args, ItemType type) { // read arguments if (!args.TryGetInt(1, "item ID", out int id, min: 0)) return null; // find matching item - SearchableItem item = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); + SearchableItem? item = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); if (item == null) monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error); return item; @@ -92,10 +92,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Get a matching item by its name. /// Writes messages to the console and log file. /// The command arguments. - private SearchableItem FindItemByName(IMonitor monitor, ArgumentParser args) + private SearchableItem? FindItemByName(IMonitor monitor, ArgumentParser args) { // read arguments - if (!args.TryGet(1, "item name", out string name)) + if (!args.TryGet(1, "item name", out string? name)) return null; // find matching items diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs index a835455e..ef35ad19 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs @@ -1,16 +1,18 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which list item types. - internal class ListItemTypesCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ListItemTypesCommand : ConsoleCommand { /********* ** Fields *********/ /// Provides methods for searching and constructing items. - private readonly ItemRepository Items = new ItemRepository(); + private readonly ItemRepository Items = new(); /********* diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs index 4232ce16..73d5b79d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs @@ -1,18 +1,20 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which list items available to spawn. - internal class ListItemsCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ListItemsCommand : ConsoleCommand { /********* ** Fields *********/ /// Provides methods for searching and constructing items. - private readonly ItemRepository Items = new ItemRepository(); + private readonly ItemRepository Items = new(); /********* @@ -59,15 +61,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player private IEnumerable GetItems(string[] searchWords) { // normalize search term - searchWords = searchWords?.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); - if (searchWords?.Any() == false) - searchWords = null; + searchWords = searchWords.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); + bool getAll = !searchWords.Any(); // find matches return ( from item in this.Items.GetAll() let term = $"{item.ID}|{item.Type}|{item.Name}|{item.DisplayName}" - where searchWords == null || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) + where getAll || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) select item ); } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs index f0815ef6..ea9f1d82 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs @@ -1,10 +1,12 @@ +using System.Diagnostics.CodeAnalysis; using Microsoft.Xna.Framework; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the color of a player feature. - internal class SetColorCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetColorCommand : ConsoleCommand { /********* ** Public methods @@ -20,9 +22,9 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "hair", "eyes", "pants" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "hair", "eyes", "pants" })) return; - if (!args.TryGet(1, "color", out string rawColor)) + if (!args.TryGet(1, "color", out string? rawColor)) return; // parse color @@ -61,7 +63,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// The color to set. private bool TryParseColor(string input, out Color color) { - string[] colorHexes = input.Split(new[] { ',' }, 3); + string[] colorHexes = input.Split(',', 3); if (int.TryParse(colorHexes[0], out int r) && int.TryParse(colorHexes[1], out int g) && int.TryParse(colorHexes[2], out int b)) { color = new Color(r, g, b); diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs new file mode 100644 index 00000000..b2035d42 --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs @@ -0,0 +1,224 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; +using System.Text; +using StardewValley; +using StardewValley.GameData; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player +{ + /// A command which changes the player's farm type. + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetFarmTypeCommand : ConsoleCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public SetFarmTypeCommand() + : base("set_farm_type", "Sets the current player's farm type.\n\nUsage: set_farm_type \n- farm type: the farm type to set. Enter `set_farm_type list` for a list of available farm types.") { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // validate + if (!Context.IsWorldReady) + { + monitor.Log("You must load a save to use this command.", LogLevel.Error); + return; + } + + // parse arguments + if (!args.TryGet(0, "farm type", out string? farmType)) + return; + bool isVanillaId = int.TryParse(farmType, out int vanillaId) && vanillaId is (>= 0 and < Farm.layout_max); + + // handle argument + if (farmType == "list") + this.HandleList(monitor); + else if (isVanillaId) + this.HandleVanillaFarmType(vanillaId, monitor); + else + this.HandleCustomFarmType(farmType, monitor); + } + + + /********* + ** Private methods + *********/ + /**** + ** Handlers + ****/ + /// Print a list of available farm types. + /// Writes messages to the console and log file. + private void HandleList(IMonitor monitor) + { + StringBuilder result = new(); + + // list vanilla types + result.AppendLine("The farm type can be one of these vanilla types:"); + foreach (var type in this.GetVanillaFarmTypes()) + result.AppendLine($" - {type.Key} ({type.Value})"); + result.AppendLine(); + + // list custom types + { + var customTypes = this.GetCustomFarmTypes(); + if (customTypes.Any()) + { + result.AppendLine("Or one of these custom farm types:"); + foreach (var type in customTypes.Values.OrderBy(p => p.ID)) + result.AppendLine($" - {type.ID} ({this.GetCustomName(type)})"); + } + else + result.AppendLine("Or a custom farm type (though none is loaded currently)."); + } + + // print + monitor.Log(result.ToString(), LogLevel.Info); + } + + /// Set a vanilla farm type. + /// The farm type. + /// Writes messages to the console and log file. + private void HandleVanillaFarmType(int type, IMonitor monitor) + { + if (Game1.whichFarm == type) + { + monitor.Log($"Your current farm is already set to {type} ({this.GetVanillaName(type)}).", LogLevel.Info); + return; + } + + this.SetFarmType(type, null); + this.PrintSuccess(monitor, $"{type} ({this.GetVanillaName(type)}"); + } + + /// Set a custom farm type. + /// The farm type ID. + /// Writes messages to the console and log file. + private void HandleCustomFarmType(string id, IMonitor monitor) + { + if (Game1.whichModFarm?.ID == id) + { + monitor.Log($"Your current farm is already set to {id} ({this.GetCustomName(Game1.whichModFarm)}).", LogLevel.Info); + return; + } + + if (!this.GetCustomFarmTypes().TryGetValue(id, out ModFarmType? customFarmType)) + { + monitor.Log($"Invalid farm type '{id}'. Enter `help set_farm_type` for more info.", LogLevel.Error); + return; + } + + this.SetFarmType(Farm.mod_layout, customFarmType); + this.PrintSuccess(monitor, $"{id} ({this.GetCustomName(customFarmType)})"); + } + + /// Change the farm type. + /// The farm type ID. + /// The custom farm type data, if applicable. + private void SetFarmType(int type, ModFarmType? customFarmData) + { + // set flags + Game1.whichFarm = type; + Game1.whichModFarm = customFarmData; + + // update farm map + Farm farm = Game1.getFarm(); + farm.mapPath.Value = $@"Maps\{Farm.getMapNameFromTypeInt(Game1.whichFarm)}"; + farm.reloadMap(); + farm.updateWarps(); + + // clear spouse area cache to avoid errors + FieldInfo? cacheField = farm.GetType().GetField("_baseSpouseAreaTiles", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + if (cacheField == null) + throw new InvalidOperationException("Failed to access '_baseSpouseAreaTiles' field to clear spouse area cache."); + if (cacheField.GetValue(farm) is not IDictionary cache) + throw new InvalidOperationException($"The farm's '_baseSpouseAreaTiles' field didn't match the expected {nameof(IDictionary)} type."); + cache.Clear(); + } + + private void PrintSuccess(IMonitor monitor, string label) + { + StringBuilder result = new(); + result.AppendLine($"Your current farm has been converted to {label}. Saving and reloading is recommended to make sure everything is updated for the change."); + result.AppendLine(); + result.AppendLine("This doesn't move items that are out of bounds on the new map. If you need to clean up, you can..."); + result.AppendLine(" - temporarily switch back to the previous farm type;"); + result.AppendLine(" - or use a mod like Noclip Mode: https://www.nexusmods.com/stardewvalley/mods/3900 ;"); + result.AppendLine(" - or use the world_clear console command (enter `help world_clear` for details)."); + + monitor.Log(result.ToString(), LogLevel.Warn); + } + + /**** + ** Vanilla farm types + ****/ + /// Get the display name for a vanilla farm type. + /// The farm type. + private string GetVanillaName(int type) + { + string? translationKey = type switch + { + Farm.default_layout => "Character_FarmStandard", + Farm.riverlands_layout => "Character_FarmFishing", + Farm.forest_layout => "Character_FarmForaging", + Farm.mountains_layout => "Character_FarmMining", + Farm.combat_layout => "Character_FarmCombat", + Farm.fourCorners_layout => "Character_FarmFourCorners", + Farm.beach_layout => "Character_FarmBeach", + _ => null + }; + + return translationKey != null + ? Game1.content.LoadString(@$"Strings\UI:{translationKey}").Split('_')[0] + : type.ToString(); + } + + /// Get the available vanilla farm types by ID. + private IDictionary GetVanillaFarmTypes() + { + IDictionary farmTypes = new Dictionary(); + + foreach (int id in Enumerable.Range(0, Farm.layout_max)) + farmTypes[id] = this.GetVanillaName(id); + + return farmTypes; + } + + /**** + ** Custom farm types + ****/ + /// Get the display name for a custom farm type. + /// The custom farm type. + private string? GetCustomName(ModFarmType? farmType) + { + if (string.IsNullOrWhiteSpace(farmType?.TooltipStringPath)) + return farmType?.ID; + + return Game1.content.LoadString(farmType.TooltipStringPath)?.Split('_')[0] ?? farmType.ID; + } + + /// Get the available custom farm types by ID. + private IDictionary GetCustomFarmTypes() + { + IDictionary farmTypes = new Dictionary(StringComparer.OrdinalIgnoreCase); + + foreach (ModFarmType farmType in Game1.content.Load>("Data\\AdditionalFarms")) + { + if (string.IsNullOrWhiteSpace(farmType.ID)) + continue; + + farmTypes[farmType.ID] = farmType; + } + + return farmTypes; + } + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs index 59bda5dd..f169159f 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs @@ -1,24 +1,19 @@ +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current health. - internal class SetHealthCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetHealthCommand : ConsoleCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's health at its maximum. - private bool InfiniteHealth; - - /********* ** Public methods *********/ /// Construct an instance. public SetHealthCommand() - : base("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth [value]\n- value: an integer amount, or 'inf' for infinite health.", mayNeedUpdate: true) { } + : base("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth [value]\n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +24,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // no-argument mode if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteHealth ? "infinite" : Game1.player.health.ToString())} health. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.health} health. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteHealth = true; - monitor.Log("OK, you now have infinite health.", LogLevel.Info); + Game1.player.health = amount; + monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); } else - { - this.InfiniteHealth = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.health = amount; - monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteHealth && Context.IsWorldReady) - Game1.player.health = Game1.player.maxHealth; + this.LogArgumentNotInt(monitor); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs index 9c66c4fe..1065bd21 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs @@ -1,10 +1,12 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current immunity. - internal class SetImmunityCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetImmunityCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs index f4ae0694..c2c4931d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs @@ -1,10 +1,12 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's maximum health. - internal class SetMaxHealthCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetMaxHealthCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs index 5bce5ea3..8c794e75 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs @@ -1,10 +1,12 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's maximum stamina. - internal class SetMaxStaminaCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetMaxStaminaCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs index 6e3d68b6..3afcc62b 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs @@ -1,24 +1,19 @@ +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current money. - internal class SetMoneyCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetMoneyCommand : ConsoleCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's money at a set value. - private bool InfiniteMoney; - - /********* ** Public methods *********/ /// Construct an instance. public SetMoneyCommand() - : base("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount, or 'inf' for infinite money.", mayNeedUpdate: true) { } + : base("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +24,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // validate if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteMoney ? "infinite" : Game1.player.Money.ToString())} gold. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.Money} gold. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteMoney = true; - monitor.Log("OK, you now have infinite money.", LogLevel.Info); + Game1.player.Money = amount; + monitor.Log($"OK, you now have {Game1.player.Money} gold.", LogLevel.Info); } else - { - this.InfiniteMoney = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.Money = amount; - monitor.Log($"OK, you now have {Game1.player.Money} gold.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteMoney && Context.IsWorldReady) - Game1.player.Money = 999999; + this.LogArgumentNotInt(monitor); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs index e8cb0584..37c02ed0 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs @@ -1,9 +1,11 @@ +using System.Diagnostics.CodeAnalysis; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's name. - internal class SetNameCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetNameCommand : ConsoleCommand { /********* ** Public methods @@ -19,9 +21,9 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "player", "farm" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "player", "farm" })) return; - args.TryGet(1, "name", out string name, required: false); + args.TryGet(1, "name", out string? name, required: false); // handle switch (target) diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs index 60a1dcb1..24718ace 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs @@ -1,24 +1,19 @@ +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current stamina. - internal class SetStaminaCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetStaminaCommand : ConsoleCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's stamina at its maximum. - private bool InfiniteStamina; - - /********* ** Public methods *********/ /// Construct an instance. public SetStaminaCommand() - : base("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina [value]\n- value: an integer amount, or 'inf' for infinite stamina.", mayNeedUpdate: true) { } + : base("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina [value]\n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +24,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // validate if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteStamina ? "infinite" : Game1.player.Stamina.ToString())} stamina. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.Stamina} stamina. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteStamina = true; - monitor.Log("OK, you now have infinite stamina.", LogLevel.Info); + Game1.player.Stamina = amount; + monitor.Log($"OK, you now have {Game1.player.Stamina} stamina.", LogLevel.Info); } else - { - this.InfiniteStamina = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.Stamina = amount; - monitor.Log($"OK, you now have {Game1.player.Stamina} stamina.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteStamina && Context.IsWorldReady) - Game1.player.stamina = Game1.player.MaxStamina; + this.LogArgumentNotInt(monitor); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs index 31f4107d..8193ff27 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs @@ -1,9 +1,11 @@ +using System.Diagnostics.CodeAnalysis; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits a player style. - internal class SetStyleCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetStyleCommand : ConsoleCommand { /********* ** Public methods @@ -19,7 +21,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "hair", "shirt", "acc", "skin", "shoe", "swim", "gender" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "hair", "shirt", "acc", "skin", "shoe", "swim", "gender" })) return; if (!args.TryGetInt(1, "style ID", out int styleID)) return; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs index 1190a4ab..4905b89a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs @@ -1,6 +1,7 @@ using System; -using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; +using Microsoft.Xna.Framework; using StardewValley; using StardewValley.Locations; using StardewValley.Objects; @@ -10,13 +11,14 @@ using SObject = StardewValley.Object; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which clears in-game objects. - internal class ClearCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class ClearCommand : ConsoleCommand { /********* ** Fields *********/ /// The valid types that can be cleared. - private readonly string[] ValidTypes = { "crops", "debris", "fruit-trees", "grass", "trees", "everything" }; + private readonly string[] ValidTypes = { "crops", "debris", "fruit-trees", "furniture", "grass", "trees", "removable", "everything" }; /// The resource clump IDs to consider debris. private readonly int[] DebrisClumps = { ResourceClump.stumpIndex, ResourceClump.hollowLogIndex, ResourceClump.meteoriteIndex, ResourceClump.boulderIndex }; @@ -31,8 +33,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World name: "world_clear", description: "Clears in-game entities in a given location.\n\n" + "Usage: world_clear \n" - + "- location: the location name for which to clear objects (like Farm), or 'current' for the current location.\n" - + " - object type: the type of object clear. You can specify 'crops', 'debris' (stones/twigs/weeds and dead crops), 'grass', and 'trees' / 'fruit-trees'. You can also specify 'everything', which includes things not removed by the other types (like furniture or resource clumps)." + + " - location: the location name for which to clear objects (like Farm), or 'current' for the current location.\n" + + " - object type: the type of object clear. You can specify 'crops', 'debris' (stones/twigs/weeds and dead crops), 'furniture', 'grass', and 'trees' / 'fruit-trees'. You can also specify 'removable' (remove everything that can be removed or destroyed during normal gameplay) or 'everything' (remove everything including permanent bushes)." ) { } @@ -50,13 +52,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World } // parse arguments - if (!args.TryGet(0, "location", out string locationName, required: true)) + if (!args.TryGet(0, "location", out string? locationName, required: true)) return; - if (!args.TryGet(1, "object type", out string type, required: true, oneOf: this.ValidTypes)) + if (!args.TryGet(1, "object type", out string? type, required: true, oneOf: this.ValidTypes)) return; // get target location - GameLocation location = Game1.locations.FirstOrDefault(p => p.Name != null && p.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)); + GameLocation? location = Game1.locations.FirstOrDefault(p => p.Name != null && p.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)); if (location == null && locationName == "current") location = Game1.currentLocation; if (location == null) @@ -93,11 +95,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World removed += this.RemoveObjects(location, obj => - !(obj is Chest) + obj is not Chest && ( - obj.Name == "Weeds" - || obj.Name == "Stone" - || (obj.ParentSheetIndex == 294 || obj.ParentSheetIndex == 295) + obj.Name is "Weeds" or "Stone" + || obj.ParentSheetIndex is 294 or 295 ) ) + this.RemoveResourceClumps(location, clump => this.DebrisClumps.Contains(clump.parentSheetIndex.Value)); @@ -113,6 +114,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World break; } + case "furniture": + { + int removed = this.RemoveFurniture(location, _ => true); + monitor.Log($"Done! Removed {removed} entities from {location.Name}.", LogLevel.Info); + break; + } + case "grass": { int removed = this.RemoveTerrainFeatures(location, feature => feature is Grass); @@ -127,14 +135,16 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World break; } + case "removable": case "everything": { + bool everything = type == "everything"; int removed = - this.RemoveFurniture(location, p => true) - + this.RemoveObjects(location, p => true) - + this.RemoveTerrainFeatures(location, p => true) - + this.RemoveLargeTerrainFeatures(location, p => true) - + this.RemoveResourceClumps(location, p => true); + this.RemoveFurniture(location, _ => true) + + this.RemoveObjects(location, _ => true) + + this.RemoveTerrainFeatures(location, _ => true) + + this.RemoveLargeTerrainFeatures(location, p => everything || p is not Bush bush || bush.isDestroyable(location, p.currentTileLocation)) + + this.RemoveResourceClumps(location, _ => true); monitor.Log($"Done! Removed {removed} entities from {location.Name}.", LogLevel.Info); break; } @@ -157,11 +167,11 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - foreach (var pair in location.Objects.Pairs.ToArray()) + foreach ((Vector2 tile, SObject? obj) in location.Objects.Pairs.ToArray()) { - if (shouldRemove(pair.Value)) + if (shouldRemove(obj)) { - location.Objects.Remove(pair.Key); + location.Objects.Remove(tile); removed++; } } @@ -177,11 +187,11 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - foreach (var pair in location.terrainFeatures.Pairs.ToArray()) + foreach ((Vector2 tile, TerrainFeature? feature) in location.terrainFeatures.Pairs.ToArray()) { - if (shouldRemove(pair.Value)) + if (shouldRemove(feature)) { - location.terrainFeatures.Remove(pair.Key); + location.terrainFeatures.Remove(tile); removed++; } } @@ -217,18 +227,17 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - // get resource clumps - IList resourceClumps = - (location as Farm)?.resourceClumps - ?? (IList)(location as Woods)?.stumps - ?? new List(); - - // remove matching clumps - foreach (var clump in resourceClumps.ToArray()) + foreach (ResourceClump clump in location.resourceClumps.Where(shouldRemove).ToArray()) { - if (shouldRemove(clump)) + location.resourceClumps.Remove(clump); + removed++; + } + + if (location is Woods woods) + { + foreach (ResourceClump clump in woods.stumps.Where(shouldRemove).ToArray()) { - resourceClumps.Remove(clump); + woods.stumps.Remove(clump); removed++; } } @@ -244,15 +253,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - if (location is DecoratableLocation decoratableLocation) + foreach (Furniture furniture in location.furniture.ToArray()) { - foreach (Furniture furniture in decoratableLocation.furniture.ToArray()) + if (shouldRemove(furniture)) { - if (shouldRemove(furniture)) - { - decoratableLocation.furniture.Remove(furniture); - removed++; - } + location.furniture.Remove(furniture); + removed++; } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs index 2cec0fd3..5b1a4a13 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs @@ -1,10 +1,12 @@ +using System.Diagnostics.CodeAnalysis; using StardewValley; using StardewValley.Locations; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which moves the player to the next mine level. - internal class DownMineLevelCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class DownMineLevelCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs index 736a93a0..16faa2fe 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which freezes the current time. - internal class FreezeTimeCommand : TrainerCommand + internal class FreezeTimeCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs new file mode 100644 index 00000000..09531720 --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using StardewValley; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World +{ + /// A command which immediately warps all NPCs to their scheduled positions. To hurry a single NPC, see debug hurry npc-name instead. + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class HurryAllCommand : ConsoleCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public HurryAllCommand() + : base( + name: "hurry_all", + description: "Immediately warps all NPCs to their scheduled positions. (To hurry a single NPC, use `debug hurry npc-name` instead.)\n\n" + + "Usage: hurry_all" + ) + { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // check context + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + + // hurry all NPCs + foreach (NPC npc in Utility.getAllCharacters()) + { + if (!npc.isVillager()) + continue; + + monitor.Log($"Hurrying {npc.Name}..."); + try + { + npc.warpToPathControllerDestination(); + } + catch (Exception ex) + { + monitor.Log($"Failed hurrying {npc.Name}. Technical details:\n{ex}", LogLevel.Error); + } + } + + monitor.Log("Done!", LogLevel.Info); + } + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs index 23c266ea..399fd934 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs @@ -1,11 +1,13 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewModdingAPI.Utilities; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current day. - internal class SetDayCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetDayCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs index b4f6d5b3..f977fce3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs @@ -1,10 +1,12 @@ using System; +using System.Diagnostics.CodeAnalysis; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which moves the player to the given mine level. - internal class SetMineLevelCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetMineLevelCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs index 676369fe..505c0d1d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Utilities; using StardewValley; @@ -5,7 +6,8 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current season. - internal class SetSeasonCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetSeasonCommand : ConsoleCommand { /********* ** Fields @@ -35,7 +37,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World } // parse arguments - if (!args.TryGet(0, "season", out string season, oneOf: this.ValidSeasons)) + if (!args.TryGet(0, "season", out string? season, oneOf: this.ValidSeasons)) return; // handle diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs index 9eae6741..8c4458dd 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs @@ -1,11 +1,13 @@ -using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; +using Microsoft.Xna.Framework; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current time. - internal class SetTimeCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetTimeCommand : ConsoleCommand { /********* ** Public methods @@ -45,12 +47,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World /// The time of day. private void SafelySetTime(int time) { - // define conversion between game time and TimeSpan - TimeSpan ToTimeSpan(int value) => new TimeSpan(0, value / 100, value % 100, 0); - int FromTimeSpan(TimeSpan span) => (span.Hours * 100) + span.Minutes; - // transition to new time - int intervals = (int)((ToTimeSpan(time) - ToTimeSpan(Game1.timeOfDay)).TotalMinutes / 10); + int intervals = Utility.CalculateMinutesBetweenTimes(Game1.timeOfDay, time) / 10; if (intervals > 0) { for (int i = 0; i < intervals; i++) @@ -60,10 +58,20 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { for (int i = 0; i > intervals; i--) { - Game1.timeOfDay = FromTimeSpan(ToTimeSpan(Game1.timeOfDay).Subtract(TimeSpan.FromMinutes(20))); // offset 20 minutes so game updates to next interval + Game1.timeOfDay = Utility.ModifyTime(Game1.timeOfDay, -20); // offset 20 mins so game updates to next interval Game1.performTenMinuteClockUpdate(); } } + + // reset ambient light + // White is the default non-raining color. If it's raining or dark out, UpdateGameClock + // below will update it automatically. + Game1.outdoorLight = Color.White; + Game1.ambientLight = Color.White; + + // run clock update (to correct lighting, etc) + Game1.gameTimeInterval = 0; + Game1.UpdateGameClock(Game1.currentGameTime); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs index 648830c1..a666a634 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs @@ -1,11 +1,13 @@ -using System.Linq; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using StardewModdingAPI.Utilities; using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current year. - internal class SetYearCommand : TrainerCommand + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")] + internal class SetYearCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs index 72d01eb7..3675a963 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs @@ -43,16 +43,6 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData this.Item = createItem(this); } - /// Construct an instance. - /// The item metadata to copy. - public SearchableItem(SearchableItem item) - { - this.Type = item.Type; - this.ID = item.ID; - this.CreateItem = item.CreateItem; - this.Item = item.Item; - } - /// Get whether the item name contains a case-insensitive substring. /// The substring to find. public bool NameContains(string substring) diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs index d1dd758b..c4619577 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs @@ -28,8 +28,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework ** Public methods *********/ /// Get all spawnable items. - [SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = "TryCreate invokes the lambda immediately.")] - public IEnumerable GetAll() + /// The item types to fetch (or null for any type). + /// Whether to include flavored variants like "Sunflower Honey". + [SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = $"{nameof(ItemRepository.TryCreate)} invokes the lambda immediately.")] + public IEnumerable GetAll(ItemType[]? itemTypes = null, bool includeVariants = true) { // // @@ -41,239 +43,321 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework // // - - IEnumerable GetAllRaw() + IEnumerable GetAllRaw() { - // get tools - for (int q = Tool.stone; q <= Tool.iridium; q++) - { - int quality = q; + HashSet? types = itemTypes?.Any() == true ? new HashSet(itemTypes) : null; + bool ShouldGet(ItemType type) => types == null || types.Contains(type); - yield return this.TryCreate(ItemType.Tool, ToolFactory.axe, _ => ToolFactory.getToolFromDescription(ToolFactory.axe, quality)); - yield return this.TryCreate(ItemType.Tool, ToolFactory.hoe, _ => ToolFactory.getToolFromDescription(ToolFactory.hoe, quality)); - yield return this.TryCreate(ItemType.Tool, ToolFactory.pickAxe, _ => ToolFactory.getToolFromDescription(ToolFactory.pickAxe, quality)); - yield return this.TryCreate(ItemType.Tool, ToolFactory.wateringCan, _ => ToolFactory.getToolFromDescription(ToolFactory.wateringCan, quality)); - if (quality != Tool.iridium) - yield return this.TryCreate(ItemType.Tool, ToolFactory.fishingRod, _ => ToolFactory.getToolFromDescription(ToolFactory.fishingRod, quality)); + // get tools + if (ShouldGet(ItemType.Tool)) + { + for (int q = Tool.stone; q <= Tool.iridium; q++) + { + int quality = q; + + yield return this.TryCreate(ItemType.Tool, ToolFactory.axe, _ => ToolFactory.getToolFromDescription(ToolFactory.axe, quality)); + yield return this.TryCreate(ItemType.Tool, ToolFactory.hoe, _ => ToolFactory.getToolFromDescription(ToolFactory.hoe, quality)); + yield return this.TryCreate(ItemType.Tool, ToolFactory.pickAxe, _ => ToolFactory.getToolFromDescription(ToolFactory.pickAxe, quality)); + yield return this.TryCreate(ItemType.Tool, ToolFactory.wateringCan, _ => ToolFactory.getToolFromDescription(ToolFactory.wateringCan, quality)); + if (quality != Tool.iridium) + yield return this.TryCreate(ItemType.Tool, ToolFactory.fishingRod, _ => ToolFactory.getToolFromDescription(ToolFactory.fishingRod, quality)); + } + yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset, _ => new MilkPail()); // these don't have any sort of ID, so we'll just assign some arbitrary ones + yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 1, _ => new Shears()); + yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 2, _ => new Pan()); + yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 3, _ => new Wand()); } - yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset, _ => new MilkPail()); // these don't have any sort of ID, so we'll just assign some arbitrary ones - yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 1, _ => new Shears()); - yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 2, _ => new Pan()); - yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 3, _ => new Wand()); // clothing + if (ShouldGet(ItemType.Clothing)) { - // items - HashSet clothingIds = new HashSet(); - foreach (int id in Game1.clothingInformation.Keys) - { - if (id < 0) - continue; // placeholder data for character customization clothing below - - clothingIds.Add(id); + foreach (int id in this.GetShirtIds()) yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID)); - } - - // character customization shirts (some shirts in this range have no data, but game has special logic to handle them) - for (int id = 1000; id <= 1111; id++) - { - if (!clothingIds.Contains(id)) - yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID)); - } } // wallpapers - for (int id = 0; id < 112; id++) - yield return this.TryCreate(ItemType.Wallpaper, id, p => new Wallpaper(p.ID) { Category = SObject.furnitureCategory }); + if (ShouldGet(ItemType.Wallpaper)) + { + for (int id = 0; id < 112; id++) + yield return this.TryCreate(ItemType.Wallpaper, id, p => new Wallpaper(p.ID) { Category = SObject.furnitureCategory }); + } // flooring - for (int id = 0; id < 56; id++) - yield return this.TryCreate(ItemType.Flooring, id, p => new Wallpaper(p.ID, isFloor: true) { Category = SObject.furnitureCategory }); + if (ShouldGet(ItemType.Flooring)) + { + for (int id = 0; id < 56; id++) + yield return this.TryCreate(ItemType.Flooring, id, p => new Wallpaper(p.ID, isFloor: true) { Category = SObject.furnitureCategory }); + } // equipment - foreach (int id in this.TryLoad("Data\\Boots").Keys) - yield return this.TryCreate(ItemType.Boots, id, p => new Boots(p.ID)); - foreach (int id in this.TryLoad("Data\\hats").Keys) - yield return this.TryCreate(ItemType.Hat, id, p => new Hat(p.ID)); + if (ShouldGet(ItemType.Boots)) + { + foreach (int id in this.TryLoad("Data\\Boots").Keys) + yield return this.TryCreate(ItemType.Boots, id, p => new Boots(p.ID)); + } + if (ShouldGet(ItemType.Hat)) + { + foreach (int id in this.TryLoad("Data\\hats").Keys) + yield return this.TryCreate(ItemType.Hat, id, p => new Hat(p.ID)); + } // weapons - foreach (int id in this.TryLoad("Data\\weapons").Keys) + if (ShouldGet(ItemType.Weapon)) { - yield return this.TryCreate(ItemType.Weapon, id, p => (p.ID >= 32 && p.ID <= 34) - ? (Item)new Slingshot(p.ID) - : new MeleeWeapon(p.ID) - ); + Dictionary weaponsData = this.TryLoad("Data\\weapons"); + foreach (KeyValuePair pair in weaponsData) + { + string rawFields = pair.Value; + yield return this.TryCreate(ItemType.Weapon, pair.Key, p => + { + string[] fields = rawFields.Split('/'); + bool isSlingshot = fields.Length > 8 && fields[8] == "4"; + return isSlingshot + ? new Slingshot(p.ID) + : new MeleeWeapon(p.ID); + }); + } } // furniture - foreach (int id in this.TryLoad("Data\\Furniture").Keys) + if (ShouldGet(ItemType.Furniture)) { - if (id == 1466 || id == 1468 || id == 1680) - yield return this.TryCreate(ItemType.Furniture, id, p => new TV(p.ID, Vector2.Zero)); - else - yield return this.TryCreate(ItemType.Furniture, id, p => new Furniture(p.ID, Vector2.Zero)); + foreach (int id in this.TryLoad("Data\\Furniture").Keys) + yield return this.TryCreate(ItemType.Furniture, id, p => Furniture.GetFurnitureInstance(p.ID)); } // craftables - foreach (int id in Game1.bigCraftablesInformation.Keys) - yield return this.TryCreate(ItemType.BigCraftable, id, p => new SObject(Vector2.Zero, p.ID)); + if (ShouldGet(ItemType.BigCraftable)) + { + foreach (int id in Game1.bigCraftablesInformation.Keys) + yield return this.TryCreate(ItemType.BigCraftable, id, p => new SObject(Vector2.Zero, p.ID)); + } // objects - foreach (int id in Game1.objectInformation.Keys) + if (ShouldGet(ItemType.Object) || ShouldGet(ItemType.Ring)) { - string[] fields = Game1.objectInformation[id]?.Split('/'); - - // secret notes - if (id == 79) + foreach (int id in Game1.objectInformation.Keys) { - foreach (int secretNoteId in this.TryLoad("Data\\SecretNotes").Keys) + string[]? fields = Game1.objectInformation[id]?.Split('/'); + + // ring + if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring { - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset + secretNoteId, _ => - { - SObject note = new SObject(79, 1); - note.name = $"{note.name} #{secretNoteId}"; - return note; - }); + if (ShouldGet(ItemType.Ring)) + yield return this.TryCreate(ItemType.Ring, id, p => new Ring(p.ID)); } - } - // ring - else if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring - yield return this.TryCreate(ItemType.Ring, id, p => new Ring(p.ID)); - - // item - else - { - // spawn main item - SObject item = null; - yield return this.TryCreate(ItemType.Object, id, p => + // journal scrap + else if (id == 842) { - return item = (p.ID == 812 // roe - ? new ColoredObject(p.ID, 1, Color.White) - : new SObject(p.ID, 1) - ); - }); - if (item == null) - continue; + if (ShouldGet(ItemType.Object)) + { + foreach (SearchableItem? journalScrap in this.GetSecretNotes(isJournalScrap: true)) + yield return journalScrap; + } + } - // flavored items - switch (item.Category) + // secret notes + else if (id == 79) { - // fruit products - case SObject.FruitsCategory: - // wine - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 2 + item.ParentSheetIndex, _ => new SObject(348, 1) - { - Name = $"{item.Name} Wine", - Price = item.Price * 3, - preserve = { SObject.PreserveType.Wine }, - preservedParentSheetIndex = { item.ParentSheetIndex } - }); + if (ShouldGet(ItemType.Object)) + { + foreach (SearchableItem? secretNote in this.GetSecretNotes(isJournalScrap: false)) + yield return secretNote; + } + } - // jelly - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 3 + item.ParentSheetIndex, _ => new SObject(344, 1) - { - Name = $"{item.Name} Jelly", - Price = 50 + item.Price * 2, - preserve = { SObject.PreserveType.Jelly }, - preservedParentSheetIndex = { item.ParentSheetIndex } - }); - break; + // object + else if (ShouldGet(ItemType.Object)) + { + // spawn main item + SObject? item = null; + yield return this.TryCreate(ItemType.Object, id, p => + { + return item = (p.ID == 812 // roe + ? new ColoredObject(p.ID, 1, Color.White) + : new SObject(p.ID, 1) + ); + }); + if (item == null) + continue; - // vegetable products - case SObject.VegetableCategory: - // juice - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 4 + item.ParentSheetIndex, _ => new SObject(350, 1) - { - Name = $"{item.Name} Juice", - Price = (int)(item.Price * 2.25d), - preserve = { SObject.PreserveType.Juice }, - preservedParentSheetIndex = { item.ParentSheetIndex } - }); - - // pickled - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + item.ParentSheetIndex, _ => new SObject(342, 1) - { - Name = $"Pickled {item.Name}", - Price = 50 + item.Price * 2, - preserve = { SObject.PreserveType.Pickle }, - preservedParentSheetIndex = { item.ParentSheetIndex } - }); - break; - - // flower honey - case SObject.flowersCategory: - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + item.ParentSheetIndex, _ => - { - SObject honey = new SObject(Vector2.Zero, 340, $"{item.Name} Honey", false, true, false, false) - { - Name = $"{item.Name} Honey", - preservedParentSheetIndex = { item.ParentSheetIndex } - }; - honey.Price += item.Price * 2; - return honey; - }); - break; - - // roe and aged roe (derived from FishPond.GetFishProduce) - case SObject.sellAtFishShopCategory when item.ParentSheetIndex == 812: - { - this.GetRoeContextTagLookups(out HashSet simpleTags, out List> complexTags); - - foreach (var pair in Game1.objectInformation) - { - // get input - SObject input = this.TryCreate(ItemType.Object, pair.Key, p => new SObject(p.ID, 1))?.Item as SObject; - var inputTags = input?.GetContextTags(); - if (inputTags?.Any() != true) - continue; - - // check if roe-producing fish - if (!inputTags.Any(tag => simpleTags.Contains(tag)) && !complexTags.Any(set => set.All(tag => input.HasContextTag(tag)))) - continue; - - // yield roe - SObject roe = null; - Color color = this.GetRoeColor(input); - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + item.ParentSheetIndex, _ => - { - roe = new ColoredObject(812, 1, color) - { - name = $"{input.Name} Roe", - preserve = { Value = SObject.PreserveType.Roe }, - preservedParentSheetIndex = { Value = input.ParentSheetIndex } - }; - roe.Price += input.Price / 2; - return roe; - }); - - // aged roe - if (roe != null && pair.Key != 698) // aged sturgeon roe is caviar, which is a separate item - { - yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + item.ParentSheetIndex, _ => new ColoredObject(447, 1, color) - { - name = $"Aged {input.Name} Roe", - Category = -27, - preserve = { Value = SObject.PreserveType.AgedRoe }, - preservedParentSheetIndex = { Value = input.ParentSheetIndex }, - Price = roe.Price * 2 - }); - } - } - } - break; + // flavored items + if (includeVariants) + { + foreach (SearchableItem? variant in this.GetFlavoredObjectVariants(item)) + yield return variant; + } } } } } - return GetAllRaw().Where(p => p != null); + return ( + from item in GetAllRaw() + where item != null + select item + ); } /********* ** Private methods *********/ + /// Get the individual secret note or journal scrap items. + /// Whether to get journal scraps. + /// Derived from . + private IEnumerable GetSecretNotes(bool isJournalScrap) + { + // get base item ID + int baseId = isJournalScrap ? 842 : 79; + + // get secret note IDs + var ids = this + .TryLoad("Data\\SecretNotes") + .Keys + .Where(isJournalScrap + ? id => (id >= GameLocation.JOURNAL_INDEX) + : id => (id < GameLocation.JOURNAL_INDEX) + ) + .Select(isJournalScrap + ? id => (id - GameLocation.JOURNAL_INDEX) + : id => id + ); + + // build items + foreach (int id in ids) + { + int fakeId = this.CustomIDOffset * 8 + id; + if (isJournalScrap) + fakeId += GameLocation.JOURNAL_INDEX; + + yield return this.TryCreate(ItemType.Object, fakeId, _ => + { + SObject note = new(baseId, 1); + note.Name = $"{note.Name} #{id}"; + return note; + }); + } + } + + /// Get flavored variants of a base item (like Blueberry Wine for Blueberry), if any. + /// A sample of the base item. + private IEnumerable GetFlavoredObjectVariants(SObject item) + { + int id = item.ParentSheetIndex; + + switch (item.Category) + { + // fruit products + case SObject.FruitsCategory: + // wine + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 2 + id, _ => new SObject(348, 1) + { + Name = $"{item.Name} Wine", + Price = item.Price * 3, + preserve = { SObject.PreserveType.Wine }, + preservedParentSheetIndex = { id } + }); + + // jelly + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 3 + id, _ => new SObject(344, 1) + { + Name = $"{item.Name} Jelly", + Price = 50 + item.Price * 2, + preserve = { SObject.PreserveType.Jelly }, + preservedParentSheetIndex = { id } + }); + break; + + // vegetable products + case SObject.VegetableCategory: + // juice + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 4 + id, _ => new SObject(350, 1) + { + Name = $"{item.Name} Juice", + Price = (int)(item.Price * 2.25d), + preserve = { SObject.PreserveType.Juice }, + preservedParentSheetIndex = { id } + }); + + // pickled + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + id, _ => new SObject(342, 1) + { + Name = $"Pickled {item.Name}", + Price = 50 + item.Price * 2, + preserve = { SObject.PreserveType.Pickle }, + preservedParentSheetIndex = { id } + }); + break; + + // flower honey + case SObject.flowersCategory: + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + id, _ => + { + SObject honey = new(Vector2.Zero, 340, $"{item.Name} Honey", false, true, false, false) + { + Name = $"{item.Name} Honey", + preservedParentSheetIndex = { id } + }; + honey.Price += item.Price * 2; + return honey; + }); + break; + + // roe and aged roe (derived from FishPond.GetFishProduce) + case SObject.sellAtFishShopCategory when id == 812: + { + this.GetRoeContextTagLookups(out HashSet simpleTags, out List> complexTags); + + foreach (var pair in Game1.objectInformation) + { + // get input + SObject? input = this.TryCreate(ItemType.Object, pair.Key, p => new SObject(p.ID, 1))?.Item as SObject; + if (input == null) + continue; + + HashSet inputTags = input.GetContextTags(); + if (!inputTags.Any()) + continue; + + // check if roe-producing fish + if (!inputTags.Any(tag => simpleTags.Contains(tag)) && !complexTags.Any(set => set.All(tag => input.HasContextTag(tag)))) + continue; + + // yield roe + SObject? roe = null; + Color color = this.GetRoeColor(input); + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + id, _ => + { + roe = new ColoredObject(812, 1, color) + { + name = $"{input.Name} Roe", + preserve = { Value = SObject.PreserveType.Roe }, + preservedParentSheetIndex = { Value = input.ParentSheetIndex } + }; + roe.Price += input.Price / 2; + return roe; + }); + + // aged roe + if (roe != null && pair.Key != 698) // aged sturgeon roe is caviar, which is a separate item + { + yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + id, _ => new ColoredObject(447, 1, color) + { + name = $"Aged {input.Name} Roe", + Category = -27, + preserve = { Value = SObject.PreserveType.AgedRoe }, + preservedParentSheetIndex = { Value = input.ParentSheetIndex }, + Price = roe.Price * 2 + }); + } + } + } + break; + } + } + /// Get optimized lookups to match items which produce roe in a fish pond. /// A lookup of simple singular tags which match a roe-producing fish. /// A list of tag sets which match roe-producing fish. @@ -299,6 +383,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// The asset value type. /// The data asset name. private Dictionary TryLoad(string assetName) + where TKey : notnull { try { @@ -315,7 +400,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// The item type. /// The unique ID (if different from the item's parent sheet index). /// Create an item instance. - private SearchableItem TryCreate(ItemType type, int id, Func createItem) + private SearchableItem? TryCreate(ItemType type, int id, Func createItem) { try { @@ -338,5 +423,43 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework ? new Color(61, 55, 42) : (TailoringMenu.GetDyeColor(fish) ?? Color.Orange); } + + /// Get valid shirt IDs. + /// + /// Shirts have a possible range of 1000–1999, but not all of those IDs are valid. There are two sets of IDs: + /// + /// + /// + /// Shirts which exist in . + /// + /// + /// Shirts with a dynamic ID and no entry in . These automatically + /// use the generic shirt entry with ID -1 and are mapped to a calculated position in the + /// Characters/Farmer/shirts spritesheet. There's no constant we can use, but some known valid + /// ranges are 1000–1111 (used in for the customization screen and + /// 1000–1127 (used in and ). + /// Based on the spritesheet, the max valid ID is 1299. + /// + /// + /// + private IEnumerable GetShirtIds() + { + // defined shirt items + foreach (int id in Game1.clothingInformation.Keys) + { + if (id < 0) + continue; // placeholder data for character customization clothing below + + yield return id; + } + + // dynamic shirts + HashSet clothingIds = new HashSet(Game1.clothingInformation.Keys); + for (int id = 1000; id <= 1299; id++) + { + if (!clothingIds.Contains(id)) + yield return id; + } + } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs index 5c4f3bba..dbfca815 100644 --- a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs +++ b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs @@ -13,13 +13,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands ** Fields *********/ /// The commands to handle. - private ITrainerCommand[] Commands; + private IConsoleCommand[] Commands = null!; /// The commands which may need to handle update ticks. - private ITrainerCommand[] UpdateHandlers; + private IConsoleCommand[] UpdateHandlers = null!; /// The commands which may need to handle input. - private ITrainerCommand[] InputHandlers; + private IConsoleCommand[] InputHandlers = null!; /********* @@ -31,7 +31,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands { // register commands this.Commands = this.ScanForCommands().ToArray(); - foreach (ITrainerCommand command in this.Commands) + foreach (IConsoleCommand command in this.Commands) helper.ConsoleCommands.Add(command.Name, command.Description, (name, args) => this.HandleCommand(command, name, args)); // cache commands @@ -50,18 +50,18 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The method invoked when a button is pressed. /// The event sender. /// The event arguments. - private void OnButtonPressed(object sender, ButtonPressedEventArgs e) + private void OnButtonPressed(object? sender, ButtonPressedEventArgs e) { - foreach (ITrainerCommand command in this.InputHandlers) + foreach (IConsoleCommand command in this.InputHandlers) command.OnButtonPressed(this.Monitor, e.Button); } /// The method invoked when the game updates its state. /// The event sender. /// The event arguments. - private void OnUpdateTicked(object sender, EventArgs e) + private void OnUpdateTicked(object? sender, EventArgs e) { - foreach (ITrainerCommand command in this.UpdateHandlers) + foreach (IConsoleCommand command in this.UpdateHandlers) command.OnUpdated(this.Monitor); } @@ -69,19 +69,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The command to invoke. /// The command name specified by the user. /// The command arguments. - private void HandleCommand(ITrainerCommand command, string commandName, string[] args) + private void HandleCommand(IConsoleCommand command, string commandName, string[] args) { - ArgumentParser argParser = new ArgumentParser(commandName, args, this.Monitor); + ArgumentParser argParser = new(commandName, args, this.Monitor); command.Handle(this.Monitor, commandName, argParser); } /// Find all commands in the assembly. - private IEnumerable ScanForCommands() + private IEnumerable ScanForCommands() { return ( from type in this.GetType().Assembly.GetTypes() - where !type.IsAbstract && typeof(ITrainerCommand).IsAssignableFrom(type) - select (ITrainerCommand)Activator.CreateInstance(type) + where !type.IsAbstract && typeof(IConsoleCommand).IsAssignableFrom(type) + select (IConsoleCommand)Activator.CreateInstance(type)! ); } } diff --git a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj index a570004c..d5a60504 100644 --- a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj +++ b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj @@ -3,40 +3,23 @@ ConsoleCommands StardewModdingAPI.Mods.ConsoleCommands - net45 + net5.0 false - x86 + + - + + + - - - - - - - - - - - - - - - - - - - - @@ -61,5 +44,4 @@ - diff --git a/src/SMAPI.Mods.ConsoleCommands/manifest.json b/src/SMAPI.Mods.ConsoleCommands/manifest.json index ddc55a73..6ababef0 100644 --- a/src/SMAPI.Mods.ConsoleCommands/manifest.json +++ b/src/SMAPI.Mods.ConsoleCommands/manifest.json @@ -1,9 +1,9 @@ { "Name": "Console Commands", "Author": "SMAPI", - "Version": "3.7.6", + "Version": "3.18.2", "Description": "Adds SMAPI console commands that let you manipulate the game.", "UniqueID": "SMAPI.ConsoleCommands", "EntryDll": "ConsoleCommands.dll", - "MinimumApiVersion": "3.7.6" + "MinimumApiVersion": "3.18.2" } diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs new file mode 100644 index 00000000..25056b5e --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs @@ -0,0 +1,95 @@ +using System; +using System.Reflection; +using StardewModdingAPI.Events; +using StardewModdingAPI.Internal.Patching; +#if SMAPI_DEPRECATED +using StardewModdingAPI.Mods.ErrorHandler.ModPatches; +#endif +using StardewModdingAPI.Mods.ErrorHandler.Patches; +using StardewValley; + +namespace StardewModdingAPI.Mods.ErrorHandler +{ + /// The main entry point for the mod. + public class ModEntry : Mod + { + /********* + ** Private methods + *********/ + /// Whether custom content was removed from the save data to avoid a crash. + private bool IsSaveContentRemoved; + + + /********* + ** Public methods + *********/ + /// The mod entry point, called after the mod is first loaded. + /// Provides simplified APIs for writing mods. + public override void Entry(IModHelper helper) + { + // get SMAPI core types + IMonitor monitorForGame = this.GetMonitorForGame(); + + // apply patches + HarmonyPatcher.Apply(this.ModManifest.UniqueID, this.Monitor, + // game patches + new DialoguePatcher(monitorForGame, this.Helper.Reflection), + new EventPatcher(monitorForGame), + new GameLocationPatcher(monitorForGame), + new IClickableMenuPatcher(), + new NpcPatcher(monitorForGame), + new ObjectPatcher(), + new SaveGamePatcher(this.Monitor, this.OnSaveContentRemoved), + new SpriteBatchPatcher(), + new UtilityPatcher() + +#if SMAPI_DEPRECATED + // mod patches + , new PyTkPatcher(helper.ModRegistry) +#endif + ); + + // hook events + this.Helper.Events.GameLoop.SaveLoaded += this.OnSaveLoaded; + } + + + /********* + ** Private methods + *********/ + /// Raised after custom content is removed from the save data to avoid a crash. + internal void OnSaveContentRemoved() + { + this.IsSaveContentRemoved = true; + } + + /// The method invoked when a save is loaded. + /// The event sender. + /// The event arguments. + private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e) + { + // show in-game warning for removed save content + if (this.IsSaveContentRemoved) + { + this.IsSaveContentRemoved = false; + Game1.addHUDMessage(new HUDMessage(this.Helper.Translation.Get("warn.invalid-content-removed"), HUDMessage.error_type)); + } + } + + /// Get the monitor with which to log game errors. + private IMonitor GetMonitorForGame() + { + // get SMAPI core + Type coreType = Type.GetType("StardewModdingAPI.Framework.SCore, StardewModdingAPI", throwOnError: false) + ?? throw new InvalidOperationException("Can't access SMAPI's core type. This mod may not work correctly."); + object core = coreType.GetProperty("Instance", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null) + ?? throw new InvalidOperationException("Can't access SMAPI's core instance. This mod may not work correctly."); + + // get monitor + MethodInfo getMonitorForGame = coreType.GetMethod("GetMonitorForGame") + ?? throw new InvalidOperationException("Can't access the SMAPI's 'GetMonitorForGame' method. This mod may not work correctly."); + + return (IMonitor?)getMonitorForGame.Invoke(core, Array.Empty()) ?? this.Monitor; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs b/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs new file mode 100644 index 00000000..f084902a --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/ModPatches/PyTkPatcher.cs @@ -0,0 +1,81 @@ +#if SMAPI_DEPRECATED +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using HarmonyLib; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using StardewModdingAPI.Framework; +using StardewModdingAPI.Framework.Content; +using StardewModdingAPI.Internal; +using StardewModdingAPI.Internal.Patching; + +// +// This is part of a three-part fix for PyTK 1.23.* and earlier. When removing this, search +// 'Platonymous.Toolkit' to find the other part in SMAPI and Content Patcher. +// + +namespace StardewModdingAPI.Mods.ErrorHandler.ModPatches +{ + /// Harmony patches for the PyTK mod for compatibility with newer SMAPI versions. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "'Platonymous' is part of the mod ID.")] + internal class PyTkPatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// The PyTK mod metadata, if it's installed. + private static IModMetadata? PyTk; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod registry from which to read PyTK metadata. + public PyTkPatcher(IModRegistry modRegistry) + { + IModMetadata? pyTk = (IModMetadata?)modRegistry.Get(@"Platonymous.Toolkit"); + if (pyTk is not null && pyTk.Manifest.Version.IsOlderThan("1.24.0")) + PyTkPatcher.PyTk = pyTk; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + try + { + // get mod info + IModMetadata? pyTk = PyTkPatcher.PyTk; + if (pyTk is null) + return; + + // get patch method + const string patchMethodName = "PatchImage"; + MethodInfo? patch = AccessTools.Method(pyTk.Mod!.GetType(), patchMethodName); + if (patch is null) + { + monitor.Log("Failed applying compatibility patch for PyTK. Its image scaling feature may not work correctly.", LogLevel.Warn); + monitor.Log($"Couldn't find patch method '{pyTk.Mod.GetType().FullName}.{patchMethodName}'."); + return; + } + + // apply patch + harmony = new($"{harmony.Id}.compatibility-patches.PyTK"); + harmony.Patch( + original: AccessTools.Method(typeof(AssetDataForImage), nameof(AssetDataForImage.PatchImage), new[] { typeof(Texture2D), typeof(Rectangle), typeof(Rectangle), typeof(PatchMode) }), + prefix: new HarmonyMethod(patch) + ); + } + catch (Exception ex) + { + monitor.Log("Failed applying compatibility patch for PyTK. Its image scaling feature may not work correctly.", LogLevel.Warn); + monitor.Log(ex.GetLogSummary()); + } + } + } +} +#endif diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs new file mode 100644 index 00000000..e98eec3c --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs @@ -0,0 +1,75 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal; +using StardewModdingAPI.Internal.Patching; +using StardewValley; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept invalid dialogue lines and logs an error instead of crashing. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class DialoguePatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Writes messages to the console and log file on behalf of the game. + private static IMonitor MonitorForGame = null!; + + /// Simplifies access to private code. + private static IReflectionHelper Reflection = null!; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Writes messages to the console and log file on behalf of the game. + /// Simplifies access to private code. + public DialoguePatcher(IMonitor monitorForGame, IReflectionHelper reflector) + { + DialoguePatcher.MonitorForGame = monitorForGame; + DialoguePatcher.Reflection = reflector; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireConstructor(typeof(string), typeof(NPC)), + finalizer: this.GetHarmonyMethod(nameof(DialoguePatcher.Finalize_Constructor)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call when the Dialogue constructor throws an exception. + /// The instance being patched. + /// The dialogue being parsed. + /// The NPC for which the dialogue is being parsed. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_Constructor(Dialogue __instance, string masterDialogue, NPC? speaker, Exception? __exception) + { + if (__exception != null) + { + // log message + string? name = !string.IsNullOrWhiteSpace(speaker?.Name) ? speaker.Name : null; + DialoguePatcher.MonitorForGame.Log($"Failed parsing dialogue string{(name != null ? $" for {name}" : "")}:\n{masterDialogue}\n{__exception.GetLogSummary()}", LogLevel.Error); + + // set default dialogue + IReflectedMethod parseDialogueString = DialoguePatcher.Reflection.GetMethod(__instance, "parseDialogueString"); + IReflectedMethod checkForSpecialDialogueAttributes = DialoguePatcher.Reflection.GetMethod(__instance, "checkForSpecialDialogueAttributes"); + parseDialogueString.Invoke("..."); + checkForSpecialDialogueAttributes.Invoke(); + } + + return null; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs new file mode 100644 index 00000000..073c62cc --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal.Patching; +using StardewValley; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept errors to log more details. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class EventPatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Writes messages to the console and log file on behalf of the game. + private static IMonitor MonitorForGame = null!; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Writes messages to the console and log file on behalf of the game. + public EventPatcher(IMonitor monitorForGame) + { + EventPatcher.MonitorForGame = monitorForGame; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod(nameof(Event.LogErrorAndHalt)), + postfix: this.GetHarmonyMethod(nameof(EventPatcher.After_LogErrorAndHalt)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call after . + /// The exception being logged. + private static void After_LogErrorAndHalt(Exception e) + { + EventPatcher.MonitorForGame.Log(e.ToString(), LogLevel.Error); + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs new file mode 100644 index 00000000..9247fa48 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs @@ -0,0 +1,78 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal.Patching; +using StardewValley; +using xTile; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept errors instead of crashing. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class GameLocationPatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Writes messages to the console and log file on behalf of the game. + private static IMonitor MonitorForGame = null!; + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Writes messages to the console and log file on behalf of the game. + public GameLocationPatcher(IMonitor monitorForGame) + { + GameLocationPatcher.MonitorForGame = monitorForGame; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod(nameof(GameLocation.checkEventPrecondition)), + finalizer: this.GetHarmonyMethod(nameof(GameLocationPatcher.Finalize_CheckEventPrecondition)) + ); + harmony.Patch( + original: this.RequireMethod(nameof(GameLocation.updateSeasonalTileSheets)), + finalizer: this.GetHarmonyMethod(nameof(GameLocationPatcher.Finalize_UpdateSeasonalTileSheets)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call when throws an exception. + /// The return value of the original method. + /// The precondition to be parsed. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_CheckEventPrecondition(ref int __result, string precondition, Exception? __exception) + { + if (__exception != null) + { + __result = -1; + GameLocationPatcher.MonitorForGame.Log($"Failed parsing event precondition ({precondition}):\n{__exception.InnerException}", LogLevel.Error); + } + + return null; + } + + /// The method to call when throws an exception. + /// The instance being patched. + /// The map whose tilesheets to update. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_UpdateSeasonalTileSheets(GameLocation __instance, Map map, Exception? __exception) + { + if (__exception != null) + GameLocationPatcher.MonitorForGame.Log($"Failed updating seasonal tilesheets for location '{__instance.NameOrUniqueName}': \n{__exception}", LogLevel.Error); + + return null; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs new file mode 100644 index 00000000..b65a695a --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs @@ -0,0 +1,44 @@ +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal.Patching; +using StardewValley; +using StardewValley.Menus; +using SObject = StardewValley.Object; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept crashes due to invalid items. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class IClickableMenuPatcher : BasePatcher + { + /********* + ** Public methods + *********/ + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod(nameof(IClickableMenu.drawToolTip)), + prefix: this.GetHarmonyMethod(nameof(IClickableMenuPatcher.Before_DrawTooltip)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call instead of . + /// The item for which to draw a tooltip. + /// Returns whether to execute the original method. + private static bool Before_DrawTooltip(Item hoveredItem) + { + // invalid edible item cause crash when drawing tooltips + if (hoveredItem is SObject obj && obj.Edibility != -300 && !Game1.objectInformation.ContainsKey(obj.ParentSheetIndex)) + return false; + + return true; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs new file mode 100644 index 00000000..11f7ec69 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal; +using StardewModdingAPI.Internal.Patching; +using StardewValley; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept crashes due to invalid schedule data. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class NpcPatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Writes messages to the console and log file on behalf of the game. + private static IMonitor MonitorForGame = null!; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Writes messages to the console and log file on behalf of the game. + public NpcPatcher(IMonitor monitorForGame) + { + NpcPatcher.MonitorForGame = monitorForGame; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod($"get_{nameof(NPC.CurrentDialogue)}"), + finalizer: this.GetHarmonyMethod(nameof(NpcPatcher.Finalize_CurrentDialogue)) + ); + + harmony.Patch( + original: this.RequireMethod(nameof(NPC.parseMasterSchedule)), + finalizer: this.GetHarmonyMethod(nameof(NpcPatcher.Finalize_ParseMasterSchedule)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call when throws an exception. + /// The instance being patched. + /// The return value of the original method. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_CurrentDialogue(NPC __instance, ref Stack __result, Exception? __exception) + { + if (__exception == null) + return null; + + NpcPatcher.MonitorForGame.Log($"Failed loading current dialogue for NPC {__instance.Name}:\n{__exception.GetLogSummary()}", LogLevel.Error); + __result = new Stack(); + + return null; + } + + /// The method to call instead of . + /// The raw schedule data to parse. + /// The instance being patched. + /// The patched method's return value. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_ParseMasterSchedule(string rawData, NPC __instance, ref Dictionary __result, Exception? __exception) + { + if (__exception != null) + { + NpcPatcher.MonitorForGame.Log($"Failed parsing schedule for NPC {__instance.Name}:\n{rawData}\n{__exception.GetLogSummary()}", LogLevel.Error); + __result = new Dictionary(); + } + + return null; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs new file mode 100644 index 00000000..09a6fbbd --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal.Patching; +using StardewValley; +using SObject = StardewValley.Object; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which intercept crashes due to invalid items. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class ObjectPatcher : BasePatcher + { + /********* + ** Public methods + *********/ + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + // object.getDescription + harmony.Patch( + original: this.RequireMethod(nameof(SObject.getDescription)), + prefix: this.GetHarmonyMethod(nameof(ObjectPatcher.Before_Object_GetDescription)) + ); + + // object.getDisplayName + harmony.Patch( + original: this.RequireMethod("loadDisplayName"), + finalizer: this.GetHarmonyMethod(nameof(ObjectPatcher.Finalize_Object_loadDisplayName)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call instead of . + /// The instance being patched. + /// The patched method's return value. + /// Returns whether to execute the original method. + private static bool Before_Object_GetDescription(SObject __instance, ref string __result) + { + // invalid bigcraftables crash instead of showing '???' like invalid non-bigcraftables + if (!__instance.IsRecipe && __instance.bigCraftable.Value && !Game1.bigCraftablesInformation.ContainsKey(__instance.ParentSheetIndex)) + { + __result = "???"; + return false; + } + + return true; + } + + /// The method to call after . + /// The patched method's return value. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_Object_loadDisplayName(ref string __result, Exception? __exception) + { + if (__exception is KeyNotFoundException) + { + __result = "???"; + return null; + } + + return __exception; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs new file mode 100644 index 00000000..490bbfb6 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using HarmonyLib; +using Microsoft.Xna.Framework.Content; +using StardewModdingAPI.Internal; +using StardewModdingAPI.Internal.Patching; +using StardewValley; +using StardewValley.Buildings; +using StardewValley.Locations; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which prevent some errors due to broken save data. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class SaveGamePatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Writes messages to the console and log file. + private static IMonitor Monitor = null!; + + /// A callback invoked when custom content is removed from the save data to avoid a crash. + private static Action OnContentRemoved = null!; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Writes messages to the console and log file. + /// A callback invoked when custom content is removed from the save data to avoid a crash. + public SaveGamePatcher(IMonitor monitor, Action onContentRemoved) + { + SaveGamePatcher.Monitor = monitor; + SaveGamePatcher.OnContentRemoved = onContentRemoved; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod(nameof(SaveGame.loadDataToLocations)), + prefix: this.GetHarmonyMethod(nameof(SaveGamePatcher.Before_LoadDataToLocations)) + ); + + harmony.Patch( + original: this.RequireMethod(nameof(SaveGame.LoadFarmType)), + finalizer: this.GetHarmonyMethod(nameof(SaveGamePatcher.Finalize_LoadFarmType)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call instead of . + /// The game locations being loaded. + /// Returns whether to execute the original method. + private static bool Before_LoadDataToLocations(List gamelocations) + { + // missing locations/NPCs + IDictionary npcs = Game1.content.Load>("Data\\NPCDispositions"); + if (SaveGamePatcher.RemoveBrokenContent(gamelocations, npcs)) + SaveGamePatcher.OnContentRemoved(); + + return true; + } + + /// The method to call after throws an exception. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_LoadFarmType(Exception? __exception) + { + // missing custom farm type + if (__exception?.Message.Contains("not a valid farm type") == true && !int.TryParse(SaveGame.loaded.whichFarm, out _)) + { + SaveGamePatcher.Monitor.Log(__exception.GetLogSummary(), LogLevel.Error); + SaveGamePatcher.Monitor.Log($"Removed invalid custom farm type '{SaveGame.loaded.whichFarm}' to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom farm type mod?)", LogLevel.Warn); + + SaveGame.loaded.whichFarm = Farm.default_layout.ToString(); + SaveGame.LoadFarmType(); + SaveGamePatcher.OnContentRemoved(); + + __exception = null; + } + + return __exception; + } + + /// Remove content which no longer exists in the game data. + /// The current game locations. + /// The NPC data. + private static bool RemoveBrokenContent(IEnumerable locations, IDictionary npcs) + { + bool removedAny = false; + + foreach (GameLocation location in locations) + removedAny |= SaveGamePatcher.RemoveBrokenContent(location, npcs); + + return removedAny; + } + + /// Remove content which no longer exists in the game data. + /// The current game location. + /// The NPC data. + private static bool RemoveBrokenContent(GameLocation? location, IDictionary npcs) + { + bool removedAny = false; + if (location == null) + return false; + + // check buildings + if (location is BuildableGameLocation buildableLocation) + { + foreach (Building building in buildableLocation.buildings.ToArray()) + { + try + { + BluePrint _ = new(building.buildingType.Value); + } + catch (ContentLoadException) + { + SaveGamePatcher.Monitor.Log($"Removed invalid building type '{building.buildingType.Value}' in {location.Name} ({building.tileX}, {building.tileY}) to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom building mod?)", LogLevel.Warn); + buildableLocation.buildings.Remove(building); + removedAny = true; + continue; + } + + SaveGamePatcher.RemoveBrokenContent(building.indoors.Value, npcs); + } + } + + // check NPCs + foreach (NPC npc in location.characters.ToArray()) + { + if (npc.isVillager() && !npcs.ContainsKey(npc.Name)) + { + try + { + npc.reloadSprite(); // this won't crash for special villagers like Bouncer + } + catch + { + SaveGamePatcher.Monitor.Log($"Removed invalid villager '{npc.Name}' in {location.Name} ({npc.getTileLocation()}) to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom NPC mod?)", LogLevel.Warn); + location.characters.Remove(npc); + removedAny = true; + } + } + } + + // check objects + foreach (var pair in location.objects.Pairs.ToArray()) + { + // SpaceCore can leave null values when removing its custom content + if (pair.Value == null) + { + location.Objects.Remove(pair.Key); + SaveGamePatcher.Monitor.Log($"Removed invalid null object in {location.Name} ({pair.Key}) to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom item mod?)", LogLevel.Warn); + removedAny = true; + } + } + + return removedAny; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs new file mode 100644 index 00000000..d369e0ef --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs @@ -0,0 +1,39 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using Microsoft.Xna.Framework.Graphics; +using StardewModdingAPI.Internal.Patching; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// Harmony patches for which validate textures earlier. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class SpriteBatchPatcher : BasePatcher + { + /********* + ** Public methods + *********/ + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod("CheckValid", new[] { typeof(Texture2D) }), + postfix: this.GetHarmonyMethod(nameof(SpriteBatchPatcher.After_CheckValid)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call after . + /// The texture to validate. + private static void After_CheckValid(Texture2D? texture) + { + if (texture?.IsDisposed == true) + throw new ObjectDisposedException("Cannot draw this texture because it's disposed."); + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs new file mode 100644 index 00000000..6d75a581 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs @@ -0,0 +1,43 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using HarmonyLib; +using StardewModdingAPI.Internal.Patching; +using StardewValley; + +namespace StardewModdingAPI.Mods.ErrorHandler.Patches +{ + /// A Harmony patch for methods to log more detailed errors. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class UtilityPatcher : BasePatcher + { + /********* + ** Public methods + *********/ + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + harmony.Patch( + original: this.RequireMethod(nameof(Utility.getItemFromStandardTextDescription)), + finalizer: this.GetHarmonyMethod(nameof(UtilityPatcher.Finalize_GetItemFromStandardTextDescription)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call when throws an exception. + /// The item text description to parse. + /// The delimiter by which to split the text description. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception? Finalize_GetItemFromStandardTextDescription(string description, char delimiter, ref Exception? __exception) + { + return __exception != null + ? new FormatException($"Failed to parse item text description \"{description}\" with delimiter \"{delimiter}\".", __exception) + : null; + } + } +} diff --git a/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj new file mode 100644 index 00000000..53c37e97 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj @@ -0,0 +1,27 @@ + + + ErrorHandler + StardewModdingAPI.Mods.ErrorHandler + net5.0 + false + + + + + + + + + + + + + + + + + + + + + diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/de.json b/src/SMAPI.Mods.ErrorHandler/i18n/de.json new file mode 100644 index 00000000..1de6301c --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/de.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Ungültiger Inhalt wurde entfernt, um einen Absturz zu verhindern (siehe SMAPI Konsole für weitere Informationen)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/default.json b/src/SMAPI.Mods.ErrorHandler/i18n/default.json new file mode 100644 index 00000000..b74dcea0 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/default.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Invalid content was removed to prevent a crash (see the SMAPI console for info)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/es.json b/src/SMAPI.Mods.ErrorHandler/i18n/es.json new file mode 100644 index 00000000..8ba10b70 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/es.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Se ha quitado contenido inválido para evitar un cierre forzoso (revisa la consola de SMAPI para más información)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/fr.json b/src/SMAPI.Mods.ErrorHandler/i18n/fr.json new file mode 100644 index 00000000..76978526 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/fr.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Le contenu non valide a été supprimé afin d'éviter un plantage (voir la console de SMAPI pour plus d'informations)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/hu.json b/src/SMAPI.Mods.ErrorHandler/i18n/hu.json new file mode 100644 index 00000000..92aca7d0 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/hu.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Érvénytelen elemek kerültek eltávolításra, hogy a játék ne omoljon össze (további információk a SMAPI konzolon)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/it.json b/src/SMAPI.Mods.ErrorHandler/i18n/it.json new file mode 100644 index 00000000..5182972e --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/it.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Contenuto non valido rimosso per prevenire un crash (Guarda la console di SMAPI per maggiori informazioni)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/ja.json b/src/SMAPI.Mods.ErrorHandler/i18n/ja.json new file mode 100644 index 00000000..559c7fbe --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/ja.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "クラッシュを防ぐために無効なコンテンツを取り除きました (詳細はSMAPIコンソールを参照)" +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/ko.json b/src/SMAPI.Mods.ErrorHandler/i18n/ko.json new file mode 100644 index 00000000..48f05c26 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/ko.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "충돌을 방지하기 위해 잘못된 컨텐츠가 제거되었습니다 (자세한 내용은 SMAPI 콘솔 참조)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/pl.json b/src/SMAPI.Mods.ErrorHandler/i18n/pl.json new file mode 100644 index 00000000..f080bcd4 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/pl.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Nieprawidłowa zawartość została usunięta, aby zapobiec awarii (zobacz konsolę SMAPI po więcej informacji)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/pt.json b/src/SMAPI.Mods.ErrorHandler/i18n/pt.json new file mode 100644 index 00000000..8ea8cec9 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/pt.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Conteúdo inválido foi removido para prevenir uma falha (veja o console do SMAPI para mais informações)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/ru.json b/src/SMAPI.Mods.ErrorHandler/i18n/ru.json new file mode 100644 index 00000000..e9c3b313 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/ru.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Недопустимое содержимое было удалено, чтобы предотвратить сбой (см. информацию в консоли SMAPI)" +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/th.json b/src/SMAPI.Mods.ErrorHandler/i18n/th.json new file mode 100644 index 00000000..e2a67dda --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/th.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "ทำการลบเนื้อหาที่ไม่ถูกต้องออก เพื่อป้องกันไฟล์เกมเสียหาย (ดูรายละเอียดที่หน้าคอลโซลของ SMAPI)" +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/tr.json b/src/SMAPI.Mods.ErrorHandler/i18n/tr.json new file mode 100644 index 00000000..a05ab152 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/tr.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Yanlış paketlenmiş bir içerik, oyunun çökmemesi için yüklenmedi (SMAPI konsol penceresinde detaylı bilgi mevcut)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/uk.json b/src/SMAPI.Mods.ErrorHandler/i18n/uk.json new file mode 100644 index 00000000..a58102ab --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/uk.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "Недійсний вміст видалено, щоб запобігти аварійному завершенню роботи (Додаткову інформацію див. на консолі SMAPI)." +} diff --git a/src/SMAPI.Mods.ErrorHandler/i18n/zh.json b/src/SMAPI.Mods.ErrorHandler/i18n/zh.json new file mode 100644 index 00000000..e959aa40 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/i18n/zh.json @@ -0,0 +1,4 @@ +{ + // warning messages + "warn.invalid-content-removed": "非法内容已移除以防游戏闪退(查看SMAPI控制台获得更多信息)" +} diff --git a/src/SMAPI.Mods.ErrorHandler/manifest.json b/src/SMAPI.Mods.ErrorHandler/manifest.json new file mode 100644 index 00000000..82630479 --- /dev/null +++ b/src/SMAPI.Mods.ErrorHandler/manifest.json @@ -0,0 +1,9 @@ +{ + "Name": "Error Handler", + "Author": "SMAPI", + "Version": "3.18.2", + "Description": "Handles some common vanilla errors to log more useful info or avoid breaking the game.", + "UniqueID": "SMAPI.ErrorHandler", + "EntryDll": "ErrorHandler.dll", + "MinimumApiVersion": "3.18.2" +} diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs index b8d3be1c..8a22a5f3 100644 --- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs +++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs @@ -1,9 +1,8 @@ using System; -using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using StardewValley; @@ -19,7 +18,7 @@ namespace StardewModdingAPI.Mods.SaveBackup private readonly int BackupsToKeep = 10; /// The absolute path to the folder in which to store save backups. - private readonly string BackupFolder = Path.Combine(Constants.ExecutionPath, "save-backups"); + private readonly string BackupFolder = Path.Combine(Constants.GamePath, "save-backups"); /// A unique label for the save backup to create. private readonly string BackupLabel = $"{DateTime.UtcNow:yyyy-MM-dd} - SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version}"; @@ -38,13 +37,13 @@ namespace StardewModdingAPI.Mods.SaveBackup try { // init backup folder - DirectoryInfo backupFolder = new DirectoryInfo(this.BackupFolder); + DirectoryInfo backupFolder = new(this.BackupFolder); backupFolder.Create(); // back up & prune saves Task .Run(() => this.CreateBackup(backupFolder)) - .ContinueWith(backupTask => this.PruneBackups(backupFolder, this.BackupsToKeep)); + .ContinueWith(_ => this.PruneBackups(backupFolder, this.BackupsToKeep)); } catch (Exception ex) { @@ -63,8 +62,8 @@ namespace StardewModdingAPI.Mods.SaveBackup try { // get target path - FileInfo targetFile = new FileInfo(Path.Combine(backupFolder.FullName, this.FileName)); - DirectoryInfo fallbackDir = new DirectoryInfo(Path.Combine(backupFolder.FullName, this.BackupLabel)); + FileInfo targetFile = new(Path.Combine(backupFolder.FullName, this.FileName)); + DirectoryInfo fallbackDir = new(Path.Combine(backupFolder.FullName, this.BackupLabel)); if (targetFile.Exists || fallbackDir.Exists) { this.Monitor.Log("Already backed up today."); @@ -72,7 +71,7 @@ namespace StardewModdingAPI.Mods.SaveBackup } // copy saves to fallback directory (ignore non-save files/folders) - DirectoryInfo savesDir = new DirectoryInfo(Constants.SavesPath); + DirectoryInfo savesDir = new(Constants.SavesPath); if (!this.RecursiveCopy(savesDir, fallbackDir, entry => this.MatchSaveFolders(savesDir, entry), copyRoot: false)) { this.Monitor.Log("No saves found."); @@ -80,7 +79,7 @@ namespace StardewModdingAPI.Mods.SaveBackup } // compress backup if possible - if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception compressError)) + if (!this.TryCompressDir(fallbackDir.FullName, targetFile, out Exception? compressError)) { this.Monitor.Log(Constants.TargetPlatform != GamePlatform.Android ? $"Backed up to {fallbackDir.FullName}." // expected to fail on Android @@ -135,19 +134,16 @@ namespace StardewModdingAPI.Mods.SaveBackup } } - /// Create a zip using the best available method. - /// The file or directory path to zip. + /// Try to create a compressed zip file for a directory. + /// The directory path to zip. /// The destination file to create. /// The error which occurred trying to compress, if applicable. This is if compression isn't supported on this platform. /// Returns whether compression succeeded. - private bool TryCompress(string sourcePath, FileInfo destination, out Exception error) + private bool TryCompressDir(string sourcePath, FileInfo destination, [NotNullWhen(false)] out Exception? error) { try { - if (Constants.TargetPlatform == GamePlatform.Mac) - this.CompressUsingMacProcess(sourcePath, destination); // due to limitations with the bundled Mono on Mac, we can't reference System.IO.Compression - else - this.CompressUsingNetFramework(sourcePath, destination); + ZipFile.CreateFromDirectory(sourcePath, destination.FullName, CompressionLevel.Fastest, false); error = null; return true; @@ -159,48 +155,6 @@ namespace StardewModdingAPI.Mods.SaveBackup } } - /// Create a zip using the .NET compression library. - /// The file or directory path to zip. - /// The destination file to create. - /// The compression libraries aren't available on this system. - private void CompressUsingNetFramework(string sourcePath, FileInfo destination) - { - // get compress method - MethodInfo createFromDirectory; - try - { - // create compressed backup - Assembly coreAssembly = Assembly.Load("System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") ?? throw new InvalidOperationException("Can't load System.IO.Compression assembly."); - Assembly fsAssembly = Assembly.Load("System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") ?? throw new InvalidOperationException("Can't load System.IO.Compression assembly."); - Type compressionLevelType = coreAssembly.GetType("System.IO.Compression.CompressionLevel") ?? throw new InvalidOperationException("Can't load CompressionLevel type."); - Type zipFileType = fsAssembly.GetType("System.IO.Compression.ZipFile") ?? throw new InvalidOperationException("Can't load ZipFile type."); - createFromDirectory = zipFileType.GetMethod("CreateFromDirectory", new[] { typeof(string), typeof(string), compressionLevelType, typeof(bool) }) ?? throw new InvalidOperationException("Can't load ZipFile.CreateFromDirectory method."); - } - catch (Exception ex) - { - throw new NotSupportedException("Couldn't load the .NET compression libraries on this system.", ex); - } - - // compress file - createFromDirectory.Invoke(null, new object[] { sourcePath, destination.FullName, CompressionLevel.Fastest, false }); - } - - /// Create a zip using a process command on MacOS. - /// The file or directory path to zip. - /// The destination file to create. - private void CompressUsingMacProcess(string sourcePath, FileInfo destination) - { - DirectoryInfo saveFolder = new DirectoryInfo(sourcePath); - ProcessStartInfo startInfo = new ProcessStartInfo - { - FileName = "zip", - Arguments = $"-rq \"{destination.FullName}\" \"{saveFolder.Name}\" -x \"*.DS_Store\" -x \"__MACOSX\"", - WorkingDirectory = $"{saveFolder.FullName}/../", - CreateNoWindow = true - }; - new Process { StartInfo = startInfo }.Start(); - } - /// Recursively copy a directory or file. /// The file or folder to copy. /// The folder to copy into. @@ -208,7 +162,7 @@ namespace StardewModdingAPI.Mods.SaveBackup /// A filter which matches the files or directories to copy, or null to copy everything. /// Derived from the SMAPI installer code. /// Returns whether any files were copied. - private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func filter, bool copyRoot = true) + private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func? filter, bool copyRoot = true) { if (!source.Exists || filter?.Invoke(source) == false) return false; @@ -242,7 +196,7 @@ namespace StardewModdingAPI.Mods.SaveBackup private bool MatchSaveFolders(DirectoryInfo savesFolder, FileSystemInfo entry) { // only need to filter top-level entries - string parentPath = (entry as FileInfo)?.DirectoryName ?? (entry as DirectoryInfo)?.Parent?.FullName; + string? parentPath = (entry as FileInfo)?.DirectoryName ?? (entry as DirectoryInfo)?.Parent?.FullName; if (parentPath != savesFolder.FullName) return true; diff --git a/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj b/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj index 0bb67bbe..36445aee 100644 --- a/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj +++ b/src/SMAPI.Mods.SaveBackup/SMAPI.Mods.SaveBackup.csproj @@ -3,17 +3,18 @@ SaveBackup StardewModdingAPI.Mods.SaveBackup - net45 + net5.0 false - x86 + + - + @@ -36,5 +37,4 @@ - diff --git a/src/SMAPI.Mods.SaveBackup/manifest.json b/src/SMAPI.Mods.SaveBackup/manifest.json index 0fe98909..e29a3ed3 100644 --- a/src/SMAPI.Mods.SaveBackup/manifest.json +++ b/src/SMAPI.Mods.SaveBackup/manifest.json @@ -1,9 +1,9 @@ { "Name": "Save Backup", "Author": "SMAPI", - "Version": "3.7.6", + "Version": "3.18.2", "Description": "Automatically backs up all your saves once per day into its folder.", "UniqueID": "SMAPI.SaveBackup", "EntryDll": "SaveBackup.dll", - "MinimumApiVersion": "3.7.6" + "MinimumApiVersion": "3.18.2" } diff --git a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs new file mode 100644 index 00000000..ac7bd338 --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs @@ -0,0 +1,46 @@ +using System; +using SMAPI.Tests.ModApiConsumer.Interfaces; + +namespace SMAPI.Tests.ModApiConsumer +{ + /// A simulated API consumer. + public class ApiConsumer + { + /********* + ** Public methods + *********/ + /// Call the event field on the given API. + /// The API to call. + /// Get the number of times the event was called and the last value received. + public void UseEventField(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues) + { + // act + int calls = 0; + int lastValue = -1; + api.OnEventRaised += (_, value) => + { + calls++; + lastValue = value; + }; + + getValues = () => (timesCalled: calls, actualValue: lastValue); + } + + /// Call the event property on the given API. + /// The API to call. + /// Get the number of times the event was called and the last value received. + public void UseEventProperty(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues) + { + // act + int calls = 0; + int lastValue = -1; + api.OnEventRaisedProperty += (_, value) => + { + calls++; + lastValue = value; + }; + + getValues = () => (timesCalled: calls, actualValue: lastValue); + } + } +} diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs new file mode 100644 index 00000000..c99605e4 --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using StardewModdingAPI.Utilities; + +namespace SMAPI.Tests.ModApiConsumer.Interfaces +{ + /// A mod-provided API which provides basic events, properties, and methods. + public interface ISimpleApi + { + /********* + ** Test interface + *********/ + /**** + ** Events + ****/ + /// A simple event field. + event EventHandler OnEventRaised; + + /// A simple event property with custom add/remove logic. + event EventHandler OnEventRaisedProperty; + + + /**** + ** Properties + ****/ + /// A simple numeric property. + int NumberProperty { get; set; } + + /// A simple object property. + object ObjectProperty { get; set; } + + /// A simple list property. + List ListProperty { get; set; } + + /// A simple list property with an interface. + IList ListPropertyWithInterface { get; set; } + + /// A property with nested generics. + IDictionary> GenericsProperty { get; set; } + + /// A property using an enum available to both mods. + BindingFlags EnumProperty { get; set; } + + /// A read-only property. + int GetterProperty { get; } + + + /**** + ** Methods + ****/ + /// A simple method with no return value. + void GetNothing(); + + /// A simple method which returns a number. + int GetInt(int value); + + /// A simple method which returns an object. + object GetObject(object value); + + /// A simple method which returns a list. + List GetList(string value); + + /// A simple method which returns a list with an interface. + IList GetListWithInterface(string value); + + /// A simple method which returns nested generics. + IDictionary> GetGenerics(string key, string value); + + /// A simple method which returns a lambda. + Func GetLambda(Func value); + + /// A simple method which returns out parameters. + bool TryGetOutParameter(int inputNumber, out int outNumber, out string outString, out PerScreen outReference, out IDictionary> outComplexType); + + + /**** + ** Inherited members + ****/ + /// A property inherited from a base class. + public string InheritedProperty { get; set; } + } +} diff --git a/src/SMAPI.Tests.ModApiConsumer/README.md b/src/SMAPI.Tests.ModApiConsumer/README.md new file mode 100644 index 00000000..ed0c6e3f --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/README.md @@ -0,0 +1,3 @@ +This project contains a simulated [mod-provided API] consumer used in the API proxying unit tests. + +[mod-provided API]: https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations diff --git a/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj b/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj new file mode 100644 index 00000000..7fef4ebd --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj @@ -0,0 +1,11 @@ + + + net5.0 + + + + + + + + diff --git a/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs b/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs new file mode 100644 index 00000000..77001e4c --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs @@ -0,0 +1,12 @@ +namespace SMAPI.Tests.ModApiProvider.Framework +{ + /// The base class for . + public class BaseApi + { + /********* + ** Test interface + *********/ + /// A property inherited from a base class. + public string? InheritedProperty { get; set; } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs new file mode 100644 index 00000000..c8781da5 --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs @@ -0,0 +1,125 @@ +// ReSharper disable UnusedMember.Global -- used dynamically through proxies + +using System; +using System.Collections.Generic; +using System.Reflection; +using StardewModdingAPI.Utilities; + +namespace SMAPI.Tests.ModApiProvider.Framework +{ + /// A mod-provided API which provides basic events, properties, and methods. + public class SimpleApi : BaseApi + { + /********* + ** Test interface + *********/ + /**** + ** Events + ****/ + /// A simple event field. + public event EventHandler? OnEventRaised; + + /// A simple event property with custom add/remove logic. + public event EventHandler OnEventRaisedProperty + { + add => this.OnEventRaised += value; + remove => this.OnEventRaised -= value; + } + + + /**** + ** Properties + ****/ + /// A simple numeric property. + public int NumberProperty { get; set; } + + /// A simple object property. + public object? ObjectProperty { get; set; } + + /// A simple list property. + public List? ListProperty { get; set; } + + /// A simple list property with an interface. + public IList? ListPropertyWithInterface { get; set; } + + /// A property with nested generics. + public IDictionary>? GenericsProperty { get; set; } + + /// A property using an enum available to both mods. + public BindingFlags EnumProperty { get; set; } + + /// A read-only property. + public int GetterProperty => 42; + + + /**** + ** Methods + ****/ + /// A simple method with no return value. + public void GetNothing() { } + + /// A simple method which returns a number. + public int GetInt(int value) + { + return value; + } + + /// A simple method which returns an object. + public object GetObject(object value) + { + return value; + } + + /// A simple method which returns a list. + public List GetList(string value) + { + return new() { value }; + } + + /// A simple method which returns a list with an interface. + public IList GetListWithInterface(string value) + { + return new List { value }; + } + + /// A simple method which returns nested generics. + public IDictionary> GetGenerics(string key, string value) + { + return new Dictionary> + { + [key] = new List { value } + }; + } + + /// A simple method which returns a lambda. + public Func GetLambda(Func value) + { + return value; + } + + /// A simple method which returns out parameters. + public bool TryGetOutParameter(int inputNumber, out int outNumber, out string outString, out PerScreen outReference, out IDictionary> outComplexType) + { + outNumber = inputNumber; + outString = inputNumber.ToString(); + outReference = new PerScreen(() => inputNumber); + outComplexType = new Dictionary> + { + [inputNumber] = new PerScreen(() => inputNumber) + }; + + return true; + } + + + /********* + ** Helper methods + *********/ + /// Raise the event. + /// The value to pass to the event. + public void RaiseEventField(int value) + { + this.OnEventRaised?.Invoke(null, value); + } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs b/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs new file mode 100644 index 00000000..c36e1c6d --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Reflection; +using SMAPI.Tests.ModApiProvider.Framework; + +namespace SMAPI.Tests.ModApiProvider +{ + /// A simulated mod instance. + public class ProviderMod + { + /// The underlying API instance. + private readonly SimpleApi Api = new(); + + /// Get the mod API instance. + public object GetModApi() + { + return this.Api; + } + + /// Raise the event. + /// The value to send as an event argument. + public void RaiseEvent(int value) + { + this.Api.RaiseEventField(value); + } + + /// Set the values for the API property. + public void SetPropertyValues(int number, object obj, string listValue, string listWithInterfaceValue, string dictionaryKey, string dictionaryListValue, BindingFlags enumValue, string inheritedValue) + { + this.Api.NumberProperty = number; + this.Api.ObjectProperty = obj; + this.Api.ListProperty = new List { listValue }; + this.Api.ListPropertyWithInterface = new List { listWithInterfaceValue }; + this.Api.GenericsProperty = new Dictionary> { [dictionaryKey] = new List { dictionaryListValue } }; + this.Api.EnumProperty = enumValue; + this.Api.InheritedProperty = inheritedValue; + } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/README.md b/src/SMAPI.Tests.ModApiProvider/README.md new file mode 100644 index 00000000..c79838e0 --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/README.md @@ -0,0 +1,3 @@ +This project contains simulated [mod-provided APIs] used in the API proxying unit tests. + +[mod-provided APIs]: https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations diff --git a/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj new file mode 100644 index 00000000..7fef4ebd --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj @@ -0,0 +1,11 @@ + + + net5.0 + + + + + + + + diff --git a/src/SMAPI.Tests/Core/AssetNameTests.cs b/src/SMAPI.Tests/Core/AssetNameTests.cs new file mode 100644 index 00000000..2d546ec7 --- /dev/null +++ b/src/SMAPI.Tests/Core/AssetNameTests.cs @@ -0,0 +1,335 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using NUnit.Framework; +using StardewModdingAPI; +using StardewModdingAPI.Framework.Content; +using StardewModdingAPI.Toolkit.Utilities; +using StardewValley; + +namespace SMAPI.Tests.Core +{ + /// Unit tests for . + [TestFixture] + internal class AssetNameTests + { + /********* + ** Unit tests + *********/ + /**** + ** Constructor + ****/ + [Test(Description = $"Assert that the {nameof(AssetName)} constructor creates an instance with the expected values.")] + [TestCase("SimpleName", "SimpleName", null, null)] + [TestCase("Data/Achievements", "Data/Achievements", null, null)] + [TestCase("Characters/Dialogue/Abigail", "Characters/Dialogue/Abigail", null, null)] + [TestCase("Characters/Dialogue/Abigail.fr-FR", "Characters/Dialogue/Abigail", "fr-FR", LocalizedContentManager.LanguageCode.fr)] + [TestCase("Characters/Dialogue\\Abigail.fr-FR", "Characters/Dialogue/Abigail.fr-FR", null, null)] + [TestCase("Characters/Dialogue/Abigail.fr-FR", "Characters/Dialogue/Abigail", "fr-FR", LocalizedContentManager.LanguageCode.fr)] + public void Constructor_Valid(string name, string expectedBaseName, string? expectedLocale, LocalizedContentManager.LanguageCode? expectedLanguageCode) + { + // arrange + name = PathUtilities.NormalizeAssetName(name); + + // act + IAssetName assetName = AssetName.Parse(name, parseLocale: _ => expectedLanguageCode); + + // assert + assetName.Name.Should() + .NotBeNull() + .And.Be(name.Replace("\\", "/")); + assetName.BaseName.Should() + .NotBeNull() + .And.Be(expectedBaseName); + assetName.LocaleCode.Should() + .Be(expectedLocale); + assetName.LanguageCode.Should() + .Be(expectedLanguageCode); + } + + [Test(Description = $"Assert that the {nameof(AssetName)} constructor throws an exception if the value is invalid.")] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + [TestCase("\t")] + [TestCase(" \t ")] + public void Constructor_NullOrWhitespace(string? name) + { + // act + ArgumentException exception = Assert.Throws(() => _ = AssetName.Parse(name!, _ => null))!; + + // assert + exception.ParamName.Should().Be("rawName"); + exception.Message.Should().Be("The asset name can't be null or empty. (Parameter 'rawName')"); + } + + + /**** + ** IsEquivalentTo + ****/ + [Test(Description = $"Assert that {nameof(AssetName.IsEquivalentTo)} compares names as expected when the locale is included.")] + + // exact match (ignore case) + [TestCase("Data/Achievements", "Data/Achievements", ExpectedResult = true)] + [TestCase("DATA/achievements", "data/ACHIEVEMENTS", ExpectedResult = true)] + + // exact match (ignore formatting) + [TestCase("Data/Achievements", "Data\\Achievements", ExpectedResult = true)] + [TestCase("DATA\\achievements", "data/ACHIEVEMENTS", ExpectedResult = true)] + [TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)] + + // whitespace-insensitive + [TestCase("Data/Achievements", " Data/Achievements ", ExpectedResult = true)] + [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)] + + // other is null or whitespace + [TestCase("Data/Achievements", null, ExpectedResult = false)] + [TestCase("Data/Achievements", "", ExpectedResult = false)] + [TestCase("Data/Achievements", " ", ExpectedResult = false)] + + // with locale codes + [TestCase("Data/Achievements", "Data/Achievements.fr-FR", ExpectedResult = false)] + [TestCase("Data/Achievements.fr-FR", "Data/Achievements", ExpectedResult = false)] + [TestCase("Data/Achievements.fr-FR", "Data/Achievements.fr-FR", ExpectedResult = true)] + public bool IsEquivalentTo_Name(string mainAssetName, string otherAssetName) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => LocalizedContentManager.LanguageCode.fr); + + // assert + return name.IsEquivalentTo(otherAssetName); + } + + [Test(Description = $"Assert that {nameof(AssetName.IsEquivalentTo)} compares names as expected when the locale is excluded.")] + + // a few samples from previous test to make sure + [TestCase("Data/Achievements", "Data/Achievements", ExpectedResult = true)] + [TestCase("DATA/achievements", "data/ACHIEVEMENTS", ExpectedResult = true)] + [TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)] + [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)] + [TestCase("Data/Achievements", " ", ExpectedResult = false)] + + // with locale codes + [TestCase("Data/Achievements", "Data/Achievements.fr-FR", ExpectedResult = false)] + [TestCase("Data/Achievements.fr-FR", "Data/Achievements", ExpectedResult = true)] + [TestCase("Data/Achievements.fr-FR", "Data/Achievements.fr-FR", ExpectedResult = false)] + public bool IsEquivalentTo_BaseName(string mainAssetName, string otherAssetName) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => LocalizedContentManager.LanguageCode.fr); + + // assert + return name.IsEquivalentTo(otherAssetName, useBaseName: true); + } + + + /**** + ** StartsWith + ****/ + [Test(Description = $"Assert that {nameof(AssetName.StartsWith)} compares names as expected for inputs that aren't affected by the input options.")] + + // exact match (ignore case and formatting) + [TestCase("Data/Achievements", "Data/Achievements", ExpectedResult = true)] + [TestCase("DATA/achievements", "data/ACHIEVEMENTS", ExpectedResult = true)] + [TestCase("Data/Achievements", "Data\\Achievements", ExpectedResult = true)] + [TestCase("DATA\\achievements", "data/ACHIEVEMENTS", ExpectedResult = true)] + [TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)] + + // whitespace-insensitive + [TestCase("Data/Achievements", " Data/Achievements", ExpectedResult = true)] + [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)] + [TestCase("Data/Achievements", " ", ExpectedResult = true)] + + // invalid prefixes + [TestCase("Data/Achievements", null, ExpectedResult = false)] + + // with locale codes + [TestCase("Data/Achievements.fr-FR", "Data/Achievements", ExpectedResult = true)] + + // prefix ends with path separator + [TestCase("Data/Events/Boop", "Data/Events/", ExpectedResult = true)] + [TestCase("Data/Events/Boop", "Data/Events\\", ExpectedResult = true)] + [TestCase("Data/Events", "Data/Events/", ExpectedResult = false)] + [TestCase("Data/Events", "Data/Events\\", ExpectedResult = false)] + public bool StartsWith_SimpleCases(string mainAssetName, string prefix) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => null); + + // assert value is the same for any combination of options + bool result = name.StartsWith(prefix); + foreach (bool allowPartialWord in new[] { true, false }) + { + foreach (bool allowSubfolder in new[] { true, true }) + { + if (allowPartialWord && allowSubfolder) + continue; + + name.StartsWith(prefix, allowPartialWord, allowSubfolder) + .Should().Be(result, $"the value returned for options ({nameof(allowPartialWord)}: {allowPartialWord}, {nameof(allowSubfolder)}: {allowSubfolder}) should match the base case"); + } + } + + // assert value + return result; + } + + [Test(Description = $"Assert that {nameof(AssetName.StartsWith)} compares names as expected for the 'allowPartialWord' option.")] + [TestCase("Data/AchievementsToIgnore", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/AchievementsToIgnore", "Data/Achievements", false, ExpectedResult = false)] + [TestCase("Data/Achievements X", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements X", "Data/Achievements", false, ExpectedResult = true)] + [TestCase("Data/Achievements.X", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements.X", "Data/Achievements", false, ExpectedResult = true)] + + // with locale codes + [TestCase("Data/Achievements.fr-FR", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements.fr-FR", "Data/Achievements", false, ExpectedResult = true)] + public bool StartsWith_PartialWord(string mainAssetName, string prefix, bool allowPartialWord) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => null); + + // assert value is the same for any combination of options + bool result = name.StartsWith(prefix, allowPartialWord: allowPartialWord, allowSubfolder: true); + name.StartsWith(prefix, allowPartialWord, allowSubfolder: false) + .Should().Be(result, "specifying allowSubfolder should have no effect for these inputs"); + + // assert value + return result; + } + + [Test(Description = $"Assert that {nameof(AssetName.StartsWith)} compares names as expected for the 'allowSubfolder' option.")] + + // simple cases + [TestCase("Data/Achievements/Path", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path", "Data/Achievements", false, ExpectedResult = false)] + [TestCase("Data/Achievements/Path", "Data\\Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path", "Data\\Achievements", false, ExpectedResult = false)] + + // trailing slash + [TestCase("Data/Achievements/Path", "Data/", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path", "Data/", false, ExpectedResult = false)] + + // normalize slash style + [TestCase("Data/Achievements/Path", "Data\\", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path", "Data\\", false, ExpectedResult = false)] + [TestCase("Data/Achievements/Path", "Data/\\/", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path", "Data/\\/", false, ExpectedResult = false)] + + // with locale code + [TestCase("Data/Achievements/Path.fr-FR", "Data/Achievements", true, ExpectedResult = true)] + [TestCase("Data/Achievements/Path.fr-FR", "Data/Achievements", false, ExpectedResult = false)] + public bool StartsWith_Subfolder(string mainAssetName, string otherAssetName, bool allowSubfolder) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => null); + + // assert value is the same for any combination of options + bool result = name.StartsWith(otherAssetName, allowPartialWord: true, allowSubfolder: allowSubfolder); + name.StartsWith(otherAssetName, allowPartialWord: false, allowSubfolder: allowSubfolder) + .Should().Be(result, "specifying allowPartialWord should have no effect for these inputs"); + + // assert value + return result; + } + + [TestCase("Mods/SomeMod/SomeSubdirectory", "Mods/Some", true, ExpectedResult = true)] + [TestCase("Mods/SomeMod/SomeSubdirectory", "Mods/Some", false, ExpectedResult = false)] + [TestCase("Mods/Jasper/Data", "Mods/Jas/Image", true, ExpectedResult = false)] + [TestCase("Mods/Jasper/Data", "Mods/Jas/Image", true, ExpectedResult = false)] + public bool StartsWith_PartialMatchInPathSegment(string mainAssetName, string otherAssetName, bool allowSubfolder) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => null); + + // assert value + return name.StartsWith(otherAssetName, allowPartialWord: true, allowSubfolder: allowSubfolder); + } + + // The enumerator strips the trailing path separator, so each of these cases has to be handled on each branch. + [TestCase("Mods/SomeMod", "Mods/", false, ExpectedResult = true)] + [TestCase("Mods/SomeMod", "Mods", false, ExpectedResult = false)] + [TestCase("Mods/Jasper/Data", "Mods/Jas/", false, ExpectedResult = false)] + [TestCase("Mods/Jasper/Data", "Mods/Jas", false, ExpectedResult = false)] + [TestCase("Mods/Jas", "Mods/Jas/", false, ExpectedResult = false)] + [TestCase("Mods/Jas", "Mods/Jas", false, ExpectedResult = true)] + public bool StartsWith_PrefixHasSeparator(string mainAssetName, string otherAssetName, bool allowSubfolder) + { + // arrange + mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName); + + // act + AssetName name = AssetName.Parse(mainAssetName, _ => null); + + // assert value + return name.StartsWith(otherAssetName, allowPartialWord: true, allowSubfolder: allowSubfolder); + } + + + /**** + ** GetHashCode + ****/ + [Test(Description = $"Assert that {nameof(AssetName.GetHashCode)} generates the same hash code for two asset names which differ only by capitalization.")] + public void GetHashCode_IsCaseInsensitive() + { + // arrange + string left = "data/ACHIEVEMENTS"; + string right = "DATA/achievements"; + + // act + int leftHash = AssetName.Parse(left, _ => null).GetHashCode(); + int rightHash = AssetName.Parse(right, _ => null).GetHashCode(); + + // assert + leftHash.Should().Be(rightHash, "two asset names which differ only by capitalization should produce the same hash code"); + } + + [Test(Description = $"Assert that {nameof(AssetName.GetHashCode)} generates few hash code collisions for an arbitrary set of asset names.")] + public void GetHashCode_HasFewCollisions() + { + // generate list of names + List names = new(); + { + Random random = new(); + string characters = "abcdefghijklmnopqrstuvwxyz1234567890/"; + + while (names.Count < 1000) + { + char[] name = new char[random.Next(5, 20)]; + for (int i = 0; i < name.Length; i++) + name[i] = characters[random.Next(0, characters.Length)]; + + names.Add(new string(name)); + } + } + + // get distinct hash codes + HashSet hashCodes = new(); + foreach (string name in names) + hashCodes.Add(AssetName.Parse(name, _ => null).GetHashCode()); + + // assert a collision frequency under 0.1% + float collisionFrequency = 1 - (hashCodes.Count / (names.Count * 1f)); + collisionFrequency.Should().BeLessOrEqualTo(0.001f, "hash codes should be relatively distinct with a collision rate under 0.1% for a small sample set"); + } + } +} diff --git a/src/SMAPI.Tests/Core/AssumptionTests.cs b/src/SMAPI.Tests/Core/AssumptionTests.cs new file mode 100644 index 00000000..efc9da3f --- /dev/null +++ b/src/SMAPI.Tests/Core/AssumptionTests.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using FluentAssertions.Execution; +using NUnit.Framework; +using StardewModdingAPI.Framework.Models; + +namespace SMAPI.Tests.Core +{ + /// Unit tests which validate assumptions about .NET used in the SMAPI implementation. + [TestFixture] + internal class AssumptionTests + { + /********* + ** Unit tests + *********/ + /**** + ** Constructor + ****/ + [Test(Description = $"Assert that {nameof(HashSet)} maintains insertion order when no elements are removed. If this fails, we'll need to change the implementation for the {nameof(SConfig.ModsToLoadEarly)} and {nameof(SConfig.ModsToLoadLate)} options.")] + [TestCase("construct from array")] + [TestCase("add incrementally")] + public void HashSet_MaintainsInsertionOrderWhenNoElementsAreRemoved(string populateMethod) + { + // arrange + string[] inserted = Enumerable.Range(0, 1000) + .Select(_ => Guid.NewGuid().ToString("N")) + .ToArray(); + + // act + HashSet set; + switch (populateMethod) + { + case "construct from array": + set = new(inserted, StringComparer.OrdinalIgnoreCase); + break; + + case "add incrementally": + set = new(StringComparer.OrdinalIgnoreCase); + foreach (string value in inserted) + set.Add(value); + break; + + default: + throw new AssertionFailedException($"Unknown populate method '{populateMethod}'."); + } + + // assert + string[] actualOrder = set.ToArray(); + actualOrder.Should().HaveCount(inserted.Length); + for (int i = 0; i < inserted.Length; i++) + { + string expected = inserted[i]; + string actual = actualOrder[i]; + + if (actual != expected) + throw new AssertionFailedException($"The hash set differed at index {i}: expected {expected}, but found {actual} instead."); + } + } + } +} diff --git a/src/SMAPI.Tests/Core/InterfaceProxyTests.cs b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs new file mode 100644 index 00000000..d14c116f --- /dev/null +++ b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs @@ -0,0 +1,397 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; +using FluentAssertions; +using NUnit.Framework; +using SMAPI.Tests.ModApiConsumer; +using SMAPI.Tests.ModApiConsumer.Interfaces; +using SMAPI.Tests.ModApiProvider; +using StardewModdingAPI.Framework.Reflection; +using StardewModdingAPI.Utilities; + +namespace SMAPI.Tests.Core +{ + /// Unit tests for . + [TestFixture] + internal class InterfaceProxyTests + { + /********* + ** Fields + *********/ + /// The mod ID providing an API. + private readonly string FromModId = "From.ModId"; + + /// The mod ID consuming an API. + private readonly string ToModId = "From.ModId"; + + /// The random number generator with which to create sample values. + private readonly Random Random = new(); + + /// The proxy factory to use in unit tests. + private static readonly IInterfaceProxyFactory[] ProxyFactories = { new InterfaceProxyFactory() }; + + + /********* + ** Unit tests + *********/ + /**** + ** Events + ****/ + /// Assert that an event field can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_EventField([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + ProviderMod providerMod = new(); + object implementation = providerMod.GetModApi(); + int expectedValue = this.Random.Next(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + new ApiConsumer().UseEventField(proxy, out Func<(int timesCalled, int lastValue)> getValues); + providerMod.RaiseEvent(expectedValue); + (int timesCalled, int lastValue) = getValues(); + + // assert + timesCalled.Should().Be(1, "Expected the proxied event to be raised once."); + lastValue.Should().Be(expectedValue, "The proxy received a different event argument than the implementation raised."); + } + + /// Assert that an event property can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_EventProperty([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + ProviderMod providerMod = new(); + object implementation = providerMod.GetModApi(); + int expectedValue = this.Random.Next(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + new ApiConsumer().UseEventProperty(proxy, out Func<(int timesCalled, int lastValue)> getValues); + providerMod.RaiseEvent(expectedValue); + (int timesCalled, int lastValue) = getValues(); + + // assert + timesCalled.Should().Be(1, "Expected the proxied event to be raised once."); + lastValue.Should().Be(expectedValue, "The proxy received a different event argument than the implementation raised."); + } + + /**** + ** Properties + ****/ + /// Assert that properties can be proxied correctly. + /// The proxy factory to test. + /// Whether to set the properties through the provider mod or proxy interface. + [Test] + public void CanProxy_Properties([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory, [Values("set via provider mod", "set via proxy interface")] string setVia) + { + // arrange + ProviderMod providerMod = new(); + object implementation = providerMod.GetModApi(); + int expectedNumber = this.Random.Next(); + int expectedObject = this.Random.Next(); + string expectedListValue = this.GetRandomString(); + string expectedListWithInterfaceValue = this.GetRandomString(); + string expectedDictionaryKey = this.GetRandomString(); + string expectedDictionaryListValue = this.GetRandomString(); + string expectedInheritedString = this.GetRandomString(); + BindingFlags expectedEnum = BindingFlags.Instance | BindingFlags.Public; + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + switch (setVia) + { + case "set via provider mod": + providerMod.SetPropertyValues( + number: expectedNumber, + obj: expectedObject, + listValue: expectedListValue, + listWithInterfaceValue: expectedListWithInterfaceValue, + dictionaryKey: expectedDictionaryKey, + dictionaryListValue: expectedDictionaryListValue, + enumValue: expectedEnum, + inheritedValue: expectedInheritedString + ); + break; + + case "set via proxy interface": + proxy.NumberProperty = expectedNumber; + proxy.ObjectProperty = expectedObject; + proxy.ListProperty = new() { expectedListValue }; + proxy.ListPropertyWithInterface = new List { expectedListWithInterfaceValue }; + proxy.GenericsProperty = new Dictionary> + { + [expectedDictionaryKey] = new List { expectedDictionaryListValue } + }; + proxy.EnumProperty = expectedEnum; + proxy.InheritedProperty = expectedInheritedString; + break; + + default: + throw new InvalidOperationException($"Invalid 'set via' option '{setVia}."); + } + + // assert number + this + .GetPropertyValue(implementation, nameof(proxy.NumberProperty)) + .Should().Be(expectedNumber); + proxy.NumberProperty + .Should().Be(expectedNumber); + + // assert object + this + .GetPropertyValue(implementation, nameof(proxy.ObjectProperty)) + .Should().Be(expectedObject); + proxy.ObjectProperty + .Should().Be(expectedObject); + + // assert list + (this.GetPropertyValue(implementation, nameof(proxy.ListProperty)) as IList) + .Should().NotBeNull() + .And.HaveCount(1) + .And.BeEquivalentTo(expectedListValue); + proxy.ListProperty + .Should().NotBeNull() + .And.HaveCount(1) + .And.BeEquivalentTo(expectedListValue); + + // assert list with interface + (this.GetPropertyValue(implementation, nameof(proxy.ListPropertyWithInterface)) as IList) + .Should().NotBeNull() + .And.HaveCount(1) + .And.BeEquivalentTo(expectedListWithInterfaceValue); + proxy.ListPropertyWithInterface + .Should().NotBeNull() + .And.HaveCount(1) + .And.BeEquivalentTo(expectedListWithInterfaceValue); + + // assert generics + (this.GetPropertyValue(implementation, nameof(proxy.GenericsProperty)) as IDictionary>) + .Should().NotBeNull() + .And.HaveCount(1) + .And.ContainKey(expectedDictionaryKey).WhoseValue.Should().BeEquivalentTo(expectedDictionaryListValue); + proxy.GenericsProperty + .Should().NotBeNull() + .And.HaveCount(1) + .And.ContainKey(expectedDictionaryKey).WhoseValue.Should().BeEquivalentTo(expectedDictionaryListValue); + + // assert enum + this + .GetPropertyValue(implementation, nameof(proxy.EnumProperty)) + .Should().Be(expectedEnum); + proxy.EnumProperty + .Should().Be(expectedEnum); + + // assert getter + this + .GetPropertyValue(implementation, nameof(proxy.GetterProperty)) + .Should().Be(42); + proxy.GetterProperty + .Should().Be(42); + + // assert inherited methods + this + .GetPropertyValue(implementation, nameof(proxy.InheritedProperty)) + .Should().Be(expectedInheritedString); + proxy.InheritedProperty + .Should().Be(expectedInheritedString); + } + + /// Assert that a simple method with no return value can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_Void([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + proxy.GetNothing(); + } + + /// Assert that a simple int method can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_Int([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + int expectedValue = this.Random.Next(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + int actualValue = proxy.GetInt(expectedValue); + + // assert + actualValue.Should().Be(expectedValue); + } + + /// Assert that a simple object method can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_Object([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + object expectedValue = new(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + object actualValue = proxy.GetObject(expectedValue); + + // assert + actualValue.Should().BeSameAs(expectedValue); + } + + /// Assert that a simple list method can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_List([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + string expectedValue = this.GetRandomString(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + IList actualValue = proxy.GetList(expectedValue); + + // assert + actualValue.Should().BeEquivalentTo(expectedValue); + } + + /// Assert that a simple list with interface method can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_ListWithInterface([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + string expectedValue = this.GetRandomString(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + IList actualValue = proxy.GetListWithInterface(expectedValue); + + // assert + actualValue.Should().BeEquivalentTo(expectedValue); + } + + /// Assert that a simple method which returns generic types can be proxied correctly. + /// The proxy factory to test. + [Test] + public void CanProxy_SimpleMethod_GenericTypes([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + string expectedKey = this.GetRandomString(); + string expectedValue = this.GetRandomString(); + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + IDictionary> actualValue = proxy.GetGenerics(expectedKey, expectedValue); + + // assert + actualValue + .Should().NotBeNull() + .And.HaveCount(1) + .And.ContainKey(expectedKey).WhoseValue.Should().BeEquivalentTo(expectedValue); + } + + /// Assert that a simple lambda method can be proxied correctly. + /// The proxy factory to test. + [Test] + [SuppressMessage("ReSharper", "ConvertToLocalFunction")] + public void CanProxy_SimpleMethod_Lambda([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + Func expectedValue = _ => "test"; + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + object actualValue = proxy.GetObject(expectedValue); + + // assert + actualValue.Should().BeSameAs(expectedValue); + } + + /// Assert that a method with out parameters can be proxied correctly. + /// The proxy factory to test. + [Test] + [SuppressMessage("ReSharper", "ConvertToLocalFunction")] + public void CanProxy_Method_OutParameters([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory) + { + // arrange + object implementation = new ProviderMod().GetModApi(); + const int expectedNumber = 42; + + // act + ISimpleApi proxy = this.GetProxy(proxyFactory, implementation); + bool result = proxy.TryGetOutParameter( + inputNumber: expectedNumber, + + out int outNumber, + out string outString, + out PerScreen outReference, + out IDictionary> outComplexType + ); + + // assert + result.Should().BeTrue(); + + outNumber.Should().Be(expectedNumber); + + outString.Should().Be(expectedNumber.ToString()); + + outReference.Should().NotBeNull(); + outReference.Value.Should().Be(expectedNumber); + + outComplexType.Should().NotBeNull(); + outComplexType.Count.Should().Be(1); + outComplexType.Keys.First().Should().Be(expectedNumber); + outComplexType.Values.First().Should().NotBeNull(); + outComplexType.Values.First().Value.Should().Be(expectedNumber); + } + + + /********* + ** Private methods + *********/ + /// Get a property value from an instance. + /// The instance whose property to read. + /// The property name. + private object? GetPropertyValue(object parent, string name) + { + if (parent is null) + throw new ArgumentNullException(nameof(parent)); + + Type type = parent.GetType(); + PropertyInfo? property = type.GetProperty(name); + if (property is null) + throw new InvalidOperationException($"The '{type.FullName}' type has no public property named '{name}'."); + + return property.GetValue(parent); + } + + /// Get a random test string. + private string GetRandomString() + { + return this.Random.Next().ToString(); + } + + /// Get a proxy API instance. + /// The proxy factory to use. + /// The underlying API instance. + private ISimpleApi GetProxy(IInterfaceProxyFactory proxyFactory, object implementation) + { + return proxyFactory.CreateProxy(implementation, this.FromModId, this.ToModId); + } + } +} diff --git a/src/SMAPI.Tests/Core/ModResolverTests.cs b/src/SMAPI.Tests/Core/ModResolverTests.cs index 78056ef7..70c782ab 100644 --- a/src/SMAPI.Tests/Core/ModResolverTests.cs +++ b/src/SMAPI.Tests/Core/ModResolverTests.cs @@ -10,7 +10,9 @@ using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.ModData; +using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization.Models; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; using SemanticVersion = StardewModdingAPI.SemanticVersion; namespace SMAPI.Tests.Core @@ -33,10 +35,13 @@ namespace SMAPI.Tests.Core Directory.CreateDirectory(rootFolder); // act - IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase()).ToArray(); + IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase(), useCaseInsensitiveFilePaths: true).ToArray(); // assert Assert.AreEqual(0, mods.Length, 0, $"Expected to find zero manifests, found {mods.Length} instead."); + + // cleanup + Directory.Delete(rootFolder, recursive: true); } [Test(Description = "Assert that the resolver correctly returns a failed metadata if there's an empty mod folder.")] @@ -48,13 +53,16 @@ namespace SMAPI.Tests.Core Directory.CreateDirectory(modFolder); // act - IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase()).ToArray(); - IModMetadata mod = mods.FirstOrDefault(); + IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase(), useCaseInsensitiveFilePaths: true).ToArray(); + IModMetadata? mod = mods.FirstOrDefault(); // assert Assert.AreEqual(1, mods.Length, 0, $"Expected to find one manifest, found {mods.Length} instead."); - Assert.AreEqual(ModMetadataStatus.Failed, mod.Status, "The mod metadata was not marked failed."); + Assert.AreEqual(ModMetadataStatus.Failed, mod!.Status, "The mod metadata was not marked failed."); Assert.IsNotNull(mod.Error, "The mod metadata did not have an error message set."); + + // cleanup + Directory.Delete(rootFolder, recursive: true); } [Test(Description = "Assert that the resolver correctly reads manifest data from a randomized file.")] @@ -87,13 +95,13 @@ namespace SMAPI.Tests.Core File.WriteAllText(filename, JsonConvert.SerializeObject(original)); // act - IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase()).ToArray(); - IModMetadata mod = mods.FirstOrDefault(); + IModMetadata[] mods = new ModResolver().ReadManifests(new ModToolkit(), rootFolder, new ModDatabase(), useCaseInsensitiveFilePaths: true).ToArray(); + IModMetadata? mod = mods.FirstOrDefault(); // assert Assert.AreEqual(1, mods.Length, 0, "Expected to find one manifest."); Assert.IsNotNull(mod, "The loaded manifest shouldn't be null."); - Assert.AreEqual(null, mod.DataRecord, "The data record should be null since we didn't provide one."); + Assert.AreEqual(null, mod!.DataRecord, "The data record should be null since we didn't provide one."); Assert.AreEqual(modFolder, mod.DirectoryPath, "The directory path doesn't match."); Assert.AreEqual(null, mod.Error, "The error should be null since parsing should have succeeded."); Assert.AreEqual(ModMetadataStatus.Found, mod.Status, "The status doesn't match."); @@ -114,6 +122,9 @@ namespace SMAPI.Tests.Core Assert.IsNotNull(mod.Manifest.Dependencies, "The dependencies field should not be null."); Assert.AreEqual(1, mod.Manifest.Dependencies.Length, "The dependencies field should contain one value."); Assert.AreEqual(originalDependency[nameof(IManifestDependency.UniqueID)], mod.Manifest.Dependencies[0].UniqueID, "The first dependency's unique ID doesn't match."); + + // cleanup + Directory.Delete(rootFolder, recursive: true); } /**** @@ -122,7 +133,7 @@ namespace SMAPI.Tests.Core [Test(Description = "Assert that validation doesn't fail if there are no mods installed.")] public void ValidateManifests_NoMods_DoesNothing() { - new ModResolver().ValidateManifests(new ModMetadata[0], apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(Array.Empty(), apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup, validateFilesExist: false); } [Test(Description = "Assert that validation skips manifests that have already failed without calling any other properties.")] @@ -133,7 +144,7 @@ namespace SMAPI.Tests.Core mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); // act - new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup, validateFilesExist: false); // assert mock.VerifyGet(p => p.Status, Times.Once, "The validation did not check the manifest status."); @@ -143,15 +154,14 @@ namespace SMAPI.Tests.Core public void ValidateManifests_ModStatus_AssumeBroken_Fails() { // arrange - Mock mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); - this.SetupMetadataForValidation(mock, new ModDataRecordVersionedFields + Mock mock = this.GetMetadata("Mod A", Array.Empty(), allowStatusChange: true); + mock.Setup(p => p.DataRecord).Returns(() => new ModDataRecordVersionedFields(this.GetModDataRecord()) { - Status = ModStatus.AssumeBroken, - AlternativeUrl = "https://example.org" + Status = ModStatus.AssumeBroken }); // act - new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup, validateFilesExist: false); // assert mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once, "The validation did not fail the metadata."); @@ -161,12 +171,11 @@ namespace SMAPI.Tests.Core public void ValidateManifests_MinimumApiVersion_Fails() { // arrange - Mock mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); + Mock mock = this.GetMetadata("Mod A", Array.Empty(), allowStatusChange: true); mock.Setup(p => p.Manifest).Returns(this.GetManifest(minimumApiVersion: "1.1")); - this.SetupMetadataForValidation(mock); // act - new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup, validateFilesExist: false); // assert mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once, "The validation did not fail the metadata."); @@ -176,32 +185,33 @@ namespace SMAPI.Tests.Core public void ValidateManifests_MissingEntryDLL_Fails() { // arrange - Mock mock = this.GetMetadata(this.GetManifest(id: "Mod A", version: "1.0", entryDll: "Missing.dll"), allowStatusChange: true); - this.SetupMetadataForValidation(mock); + string directoryPath = this.GetTempFolderPath(); + Mock mock = this.GetMetadata(this.GetManifest(id: "Mod A", version: "1.0", entryDll: "Missing.dll"), allowStatusChange: true, directoryPath: directoryPath); + Directory.CreateDirectory(directoryPath); // act - new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup); // assert mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once, "The validation did not fail the metadata."); + + // cleanup + Directory.Delete(directoryPath); } [Test(Description = "Assert that validation fails when multiple mods have the same unique ID.")] public void ValidateManifests_DuplicateUniqueID_Fails() { // arrange - Mock modA = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); + Mock modA = this.GetMetadata("Mod A", Array.Empty(), allowStatusChange: true); Mock modB = this.GetMetadata(this.GetManifest(id: "Mod A", name: "Mod B", version: "1.0"), allowStatusChange: true); - Mock modC = this.GetMetadata("Mod C", new string[0], allowStatusChange: false); - foreach (Mock mod in new[] { modA, modB, modC }) - this.SetupMetadataForValidation(mod); // act - new ModResolver().ValidateManifests(new[] { modA.Object, modB.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { modA.Object, modB.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup, validateFilesExist: false); // assert - modA.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once, "The validation did not fail the first mod with a unique ID."); - modB.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once, "The validation did not fail the second mod with a unique ID."); + modA.Verify(p => p.SetStatus(ModMetadataStatus.Failed, ModFailReason.Duplicate, It.IsAny(), It.IsAny()), Times.AtLeastOnce, "The validation did not fail the first mod with a unique ID."); + modB.Verify(p => p.SetStatus(ModMetadataStatus.Failed, ModFailReason.Duplicate, It.IsAny(), It.IsAny()), Times.AtLeastOnce, "The validation did not fail the second mod with a unique ID."); } [Test(Description = "Assert that validation fails when the manifest references a DLL that does not exist.")] @@ -213,20 +223,23 @@ namespace SMAPI.Tests.Core // create DLL string modFolder = Path.Combine(this.GetTempFolderPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(modFolder); - File.WriteAllText(Path.Combine(modFolder, manifest.EntryDll), ""); + File.WriteAllText(Path.Combine(modFolder, manifest.EntryDll!), ""); // arrange - Mock mock = new Mock(MockBehavior.Strict); + Mock mock = new(MockBehavior.Strict); mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.DataRecord).Returns(() => null); + mock.Setup(p => p.DataRecord).Returns(this.GetModDataRecordVersionedFields()); mock.Setup(p => p.Manifest).Returns(manifest); mock.Setup(p => p.DirectoryPath).Returns(modFolder); // act - new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: key => null); + new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, getFileLookup: this.GetFileLookup); // assert // if Moq doesn't throw a method-not-setup exception, the validation didn't override the status. + + // cleanup + Directory.Delete(modFolder, recursive: true); } /**** @@ -236,7 +249,7 @@ namespace SMAPI.Tests.Core public void ProcessDependencies_NoMods_DoesNothing() { // act - IModMetadata[] mods = new ModResolver().ProcessDependencies(new IModMetadata[0], new ModDatabase()).ToArray(); + IModMetadata[] mods = new ModResolver().ProcessDependencies(Array.Empty(), new ModDatabase()).ToArray(); // assert Assert.AreEqual(0, mods.Length, 0, "Expected to get an empty list of mods."); @@ -265,7 +278,7 @@ namespace SMAPI.Tests.Core public void ProcessDependencies_Skips_Failed() { // arrange - Mock mock = new Mock(MockBehavior.Strict); + Mock mock = new(MockBehavior.Strict); mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); // act @@ -380,7 +393,7 @@ namespace SMAPI.Tests.Core Mock modA = this.GetMetadata("Mod A"); Mock modB = this.GetMetadata("Mod B", dependencies: new[] { "Mod A" }); Mock modC = this.GetMetadata("Mod C", dependencies: new[] { "Mod B" }, allowStatusChange: true); - Mock modD = new Mock(MockBehavior.Strict); + Mock modD = new(MockBehavior.Strict); modD.Setup(p => p.Manifest).Returns(null); modD.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); @@ -470,6 +483,13 @@ namespace SMAPI.Tests.Core return Path.Combine(Path.GetTempPath(), "smapi-unit-tests", Guid.NewGuid().ToString("N")); } + /// Get a file lookup for a given directory. + /// The full path to the directory. + private IFileLookup GetFileLookup(string rootDirectory) + { + return MinimalFileLookup.GetCachedFor(rootDirectory); + } + /// Get a randomized basic manifest. /// The value, or null for a generated value. /// The value, or null for a generated value. @@ -478,20 +498,20 @@ namespace SMAPI.Tests.Core /// The value. /// The value. /// The value. - private Manifest GetManifest(string id = null, string name = null, string version = null, string entryDll = null, string contentPackForID = null, string minimumApiVersion = null, IManifestDependency[] dependencies = null) + private Manifest GetManifest(string? id = null, string? name = null, string? version = null, string? entryDll = null, string? contentPackForID = null, string? minimumApiVersion = null, IManifestDependency[]? dependencies = null) { - return new Manifest - { - UniqueID = id ?? $"{Sample.String()}.{Sample.String()}", - Name = name ?? id ?? Sample.String(), - Author = Sample.String(), - Description = Sample.String(), - Version = version != null ? new SemanticVersion(version) : new SemanticVersion(Sample.Int(), Sample.Int(), Sample.Int(), Sample.String()), - EntryDll = entryDll ?? $"{Sample.String()}.dll", - ContentPackFor = contentPackForID != null ? new ManifestContentPackFor { UniqueID = contentPackForID } : null, - MinimumApiVersion = minimumApiVersion != null ? new SemanticVersion(minimumApiVersion) : null, - Dependencies = dependencies - }; + return new Manifest( + uniqueId: id ?? $"{Sample.String()}.{Sample.String()}", + name: name ?? id ?? Sample.String(), + author: Sample.String(), + description: Sample.String(), + version: version != null ? new SemanticVersion(version) : new SemanticVersion(Sample.Int(), Sample.Int(), Sample.Int(), Sample.String()), + entryDll: entryDll ?? $"{Sample.String()}.dll", + contentPackFor: contentPackForID != null ? new ManifestContentPackFor(contentPackForID, null) : null, + minimumApiVersion: minimumApiVersion != null ? new SemanticVersion(minimumApiVersion) : null, + dependencies: dependencies ?? Array.Empty(), + updateKeys: Array.Empty() + ); } /// Get a randomized basic manifest. @@ -507,21 +527,27 @@ namespace SMAPI.Tests.Core /// Whether the code being tested is allowed to change the mod status. private Mock GetMetadata(string uniqueID, string[] dependencies, bool allowStatusChange = false) { - IManifest manifest = this.GetManifest(id: uniqueID, version: "1.0", dependencies: dependencies?.Select(dependencyID => (IManifestDependency)new ManifestDependency(dependencyID, null)).ToArray()); + IManifest manifest = this.GetManifest(id: uniqueID, version: "1.0", dependencies: dependencies.Select(dependencyID => (IManifestDependency)new ManifestDependency(dependencyID, null as ISemanticVersion)).ToArray()); return this.GetMetadata(manifest, allowStatusChange); } /// Get a randomized basic manifest. /// The mod manifest. /// Whether the code being tested is allowed to change the mod status. - private Mock GetMetadata(IManifest manifest, bool allowStatusChange = false) + /// The directory path the mod metadata should be pointed at, or null to generate a fake path. + private Mock GetMetadata(IManifest manifest, bool allowStatusChange = false, string? directoryPath = null) { - Mock mod = new Mock(MockBehavior.Strict); - mod.Setup(p => p.DataRecord).Returns(() => null); + directoryPath ??= this.GetTempFolderPath(); + + Mock mod = new(MockBehavior.Strict); + mod.Setup(p => p.DataRecord).Returns(this.GetModDataRecordVersionedFields()); mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID); + mod.Setup(p => p.DirectoryPath).Returns(directoryPath); mod.Setup(p => p.Manifest).Returns(manifest); mod.Setup(p => p.HasID(It.IsAny())).Returns((string id) => manifest.UniqueID == id); + mod.Setup(p => p.GetUpdateKeys(It.IsAny())).Returns(Enumerable.Empty()); + mod.Setup(p => p.GetRelativePathWithRoot()).Returns(directoryPath); if (allowStatusChange) { mod @@ -532,16 +558,16 @@ namespace SMAPI.Tests.Core return mod; } - /// Set up a mock mod metadata for . - /// The mock mod metadata. - /// The extra metadata about the mod from SMAPI's internal data (if any). - private void SetupMetadataForValidation(Mock mod, ModDataRecordVersionedFields modRecord = null) + /// Generate a default mod data record. + private ModDataRecord GetModDataRecord() { - mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mod.Setup(p => p.DataRecord).Returns(() => null); - mod.Setup(p => p.Manifest).Returns(this.GetManifest()); - mod.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath()); - mod.Setup(p => p.DataRecord).Returns(modRecord); + return new("Default Display Name", new ModDataModel("Sample ID", null, ModWarning.None)); + } + + /// Generate a default mod data versioned fields instance. + private ModDataRecordVersionedFields GetModDataRecordVersionedFields() + { + return new ModDataRecordVersionedFields(this.GetModDataRecord()); } } } diff --git a/src/SMAPI.Tests/Core/TranslationTests.cs b/src/SMAPI.Tests/Core/TranslationTests.cs index 457f9fad..a52df607 100644 --- a/src/SMAPI.Tests/Core/TranslationTests.cs +++ b/src/SMAPI.Tests/Core/TranslationTests.cs @@ -1,9 +1,14 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; using NUnit.Framework; using StardewModdingAPI; +using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.ModHelpers; +using StardewModdingAPI.Framework.ModLoading; +using StardewModdingAPI.Toolkit.Serialization.Models; using StardewValley; namespace SMAPI.Tests.Core @@ -16,7 +21,7 @@ namespace SMAPI.Tests.Core ** Data *********/ /// Sample translation text for unit tests. - public static string[] Samples = { null, "", " ", "boop", " boop " }; + public static string?[] Samples = { null, "", " ", "boop", " boop " }; /********* @@ -32,15 +37,15 @@ namespace SMAPI.Tests.Core var data = new Dictionary>(); // act - ITranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + ITranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); Translation translation = helper.Get("key"); - Translation[] translationList = helper.GetTranslations()?.ToArray(); + Translation[]? translationList = helper.GetTranslations()?.ToArray(); // assert Assert.AreEqual("en", helper.Locale, "The locale doesn't match the input value."); Assert.AreEqual(LocalizedContentManager.LanguageCode.en, helper.LocaleEnum, "The locale enum doesn't match the input value."); Assert.IsNotNull(translationList, "The full list of translations is unexpectedly null."); - Assert.AreEqual(0, translationList.Length, "The full list of translations is unexpectedly not empty."); + Assert.AreEqual(0, translationList!.Length, "The full list of translations is unexpectedly not empty."); Assert.IsNotNull(translation, "The translation helper unexpectedly returned a null translation."); Assert.AreEqual(this.GetPlaceholderText("key"), translation.ToString(), "The translation returned an unexpected value."); @@ -54,8 +59,8 @@ namespace SMAPI.Tests.Core var expected = this.GetExpectedTranslations(); // act - var actual = new Dictionary(); - TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + var actual = new Dictionary(); + TranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); foreach (string locale in expected.Keys) { this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en); @@ -79,7 +84,7 @@ namespace SMAPI.Tests.Core // act var actual = new Dictionary(); - TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + TranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); foreach (string locale in expected.Keys) { this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en); @@ -107,16 +112,16 @@ namespace SMAPI.Tests.Core [TestCase(" ", ExpectedResult = true)] [TestCase("boop", ExpectedResult = true)] [TestCase(" boop ", ExpectedResult = true)] - public bool Translation_HasValue(string text) + public bool Translation_HasValue(string? text) { return new Translation("pt-BR", "key", text).HasValue(); } [Test(Description = "Assert that the translation's ToString method returns the expected text for various inputs.")] - public void Translation_ToString([ValueSource(nameof(TranslationTests.Samples))] string text) + public void Translation_ToString([ValueSource(nameof(TranslationTests.Samples))] string? text) { // act - Translation translation = new Translation("pt-BR", "key", text); + Translation translation = new("pt-BR", "key", text); // assert if (translation.HasValue()) @@ -126,20 +131,20 @@ namespace SMAPI.Tests.Core } [Test(Description = "Assert that the translation's implicit string conversion returns the expected text for various inputs.")] - public void Translation_ImplicitStringConversion([ValueSource(nameof(TranslationTests.Samples))] string text) + public void Translation_ImplicitStringConversion([ValueSource(nameof(TranslationTests.Samples))] string? text) { // act - Translation translation = new Translation("pt-BR", "key", text); + Translation translation = new("pt-BR", "key", text); // assert if (translation.HasValue()) - Assert.AreEqual(text, (string)translation, "The translation returned an unexpected value given a valid input."); + Assert.AreEqual(text, (string?)translation, "The translation returned an unexpected value given a valid input."); else - Assert.AreEqual(this.GetPlaceholderText("key"), (string)translation, "The translation returned an unexpected value given a null or empty input."); + Assert.AreEqual(this.GetPlaceholderText("key"), (string?)translation, "The translation returned an unexpected value given a null or empty input."); } [Test(Description = "Assert that the translation returns the expected text given a use-placeholder setting.")] - public void Translation_UsePlaceholder([Values(true, false)] bool value, [ValueSource(nameof(TranslationTests.Samples))] string text) + public void Translation_UsePlaceholder([Values(true, false)] bool value, [ValueSource(nameof(TranslationTests.Samples))] string? text) { // act Translation translation = new Translation("pt-BR", "key", text).UsePlaceholder(value); @@ -154,7 +159,7 @@ namespace SMAPI.Tests.Core } [Test(Description = "Assert that the translation returns the expected text after setting the default.")] - public void Translation_Default([ValueSource(nameof(TranslationTests.Samples))] string text, [ValueSource(nameof(TranslationTests.Samples))] string @default) + public void Translation_Default([ValueSource(nameof(TranslationTests.Samples))] string? text, [ValueSource(nameof(TranslationTests.Samples))] string? @default) { // act Translation translation = new Translation("pt-BR", "key", text).Default(@default); @@ -182,7 +187,7 @@ namespace SMAPI.Tests.Core string expected = $"{start} tokens are properly replaced (including {middle} {middle}) {end}"; // act - Translation translation = new Translation("pt-BR", "key", input); + Translation translation = new("pt-BR", "key", input); switch (structure) { case "anonymous object": @@ -190,7 +195,7 @@ namespace SMAPI.Tests.Core break; case "class": - translation = translation.Tokens(new TokenModel { Start = start, Middle = middle, End = end }); + translation = translation.Tokens(new TokenModel(start, middle, end)); break; case "IDictionary": @@ -324,21 +329,63 @@ namespace SMAPI.Tests.Core return string.Format(Translation.PlaceholderText, key); } + /// Create a fake mod manifest. + private IModMetadata CreateModMetadata() + { + string id = $"smapi.unit-tests.fake-mod-{Guid.NewGuid():N}"; + + string tempPath = Path.Combine(Path.GetTempPath(), id); + return new ModMetadata( + displayName: "Mod Display Name", + directoryPath: tempPath, + rootPath: tempPath, + manifest: new Manifest( + uniqueID: id, + name: "Mod Name", + author: "Mod Author", + description: "Mod Description", + version: new SemanticVersion(1, 0, 0) + ), + dataRecord: null, + isIgnored: false + ); + } + /********* ** Test models *********/ /// A model used to test token support. + [SuppressMessage("ReSharper", "NotAccessedField.Local", Justification = "Used dynamically via translation helper.")] + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local", Justification = "Used dynamically via translation helper.")] private class TokenModel { + /********* + ** Accessors + *********/ /// A sample token property. - public string Start { get; set; } + public string Start { get; } /// A sample token property. - public string Middle { get; set; } + public string Middle { get; } /// A sample token field. public string End; + + + /********* + ** public methods + *********/ + /// Construct an instance. + /// A sample token property. + /// A sample token field. + /// A sample token property. + public TokenModel(string start, string middle, string end) + { + this.Start = start; + this.Middle = middle; + this.End = end; + } } } } diff --git a/src/SMAPI.Tests/SMAPI.Tests.csproj b/src/SMAPI.Tests/SMAPI.Tests.csproj index f16fe033..0b1fb638 100644 --- a/src/SMAPI.Tests/SMAPI.Tests.csproj +++ b/src/SMAPI.Tests/SMAPI.Tests.csproj @@ -1,37 +1,32 @@  - - SMAPI.Tests - SMAPI.Tests - net4.5 - false - latest - x86 + net5.0 - + + + + + - - - - + + + + + + + - - - $(GamePath)\$(GameExecutableName).exe - True - + + - - - diff --git a/src/SMAPI.Tests/Sample.cs b/src/SMAPI.Tests/Sample.cs index f4f0d88e..9587a100 100644 --- a/src/SMAPI.Tests/Sample.cs +++ b/src/SMAPI.Tests/Sample.cs @@ -9,7 +9,7 @@ namespace SMAPI.Tests ** Fields *********/ /// A random number generator. - private static readonly Random Random = new Random(); + private static readonly Random Random = new(); /********* diff --git a/src/SMAPI.Tests/Utilities/KeybindListTests.cs b/src/SMAPI.Tests/Utilities/KeybindListTests.cs new file mode 100644 index 00000000..c5fd5daf --- /dev/null +++ b/src/SMAPI.Tests/Utilities/KeybindListTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using StardewModdingAPI; +using StardewModdingAPI.Utilities; + +namespace SMAPI.Tests.Utilities +{ + /// Unit tests for . + [TestFixture] + internal class KeybindListTests + { + /********* + ** Unit tests + *********/ + /**** + ** TryParse + ****/ + /// Assert the parsed fields when constructed from a simple single-key string. + [TestCaseSource(nameof(KeybindListTests.GetAllButtons))] + public void TryParse_SimpleValue(SButton button) + { + // act + bool success = KeybindList.TryParse($"{button}", out KeybindList? parsed, out string[] errors); + + // assert + Assert.IsTrue(success, "Parsing unexpectedly failed."); + Assert.IsNotNull(parsed, "The parsed result should not be null."); + Assert.AreEqual(parsed!.ToString(), $"{button}"); + Assert.IsNotNull(errors, message: "The errors should never be null."); + Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); + } + + /// Assert the parsed fields when constructed from multi-key values. + [TestCase("", ExpectedResult = "None")] + [TestCase(" ", ExpectedResult = "None")] + [TestCase(null, ExpectedResult = "None")] + [TestCase("A + B", ExpectedResult = "A + B")] + [TestCase("A+B", ExpectedResult = "A + B")] + [TestCase(" A+ B ", ExpectedResult = "A + B")] + [TestCase("a +b", ExpectedResult = "A + B")] + [TestCase("a +b, LEFTcontrol + leftALT + LeftSHifT + delete", ExpectedResult = "A + B, LeftControl + LeftAlt + LeftShift + Delete")] + + [TestCase(",", ExpectedResult = "None")] + [TestCase("A,", ExpectedResult = "A")] + [TestCase(",A", ExpectedResult = "A")] + public string TryParse_MultiValues(string? input) + { + // act + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); + + // assert + Assert.IsTrue(success, "Parsing unexpectedly failed."); + Assert.IsNotNull(parsed, "The parsed result should not be null."); + Assert.IsNotNull(errors, message: "The errors should never be null."); + Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); + return parsed!.ToString(); + } + + /// Assert invalid values are rejected. + [TestCase("+", "Invalid empty button value")] + [TestCase("A+", "Invalid empty button value")] + [TestCase("+C", "Invalid empty button value")] + [TestCase("A + B +, C", "Invalid empty button value")] + [TestCase("A, TotallyInvalid", "Invalid button value 'TotallyInvalid'")] + [TestCase("A + TotallyInvalid", "Invalid button value 'TotallyInvalid'")] + public void TryParse_InvalidValues(string input, string expectedError) + { + // act + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); + + // assert + Assert.IsFalse(success, "Parsing unexpectedly succeeded."); + Assert.IsNull(parsed, "The parsed result should be null."); + Assert.IsNotNull(errors, message: "The errors should never be null."); + Assert.AreEqual(expectedError, string.Join("; ", errors), "The errors don't match the expected ones."); + } + + + /**** + ** GetState + ****/ + /// Assert that returns the expected result for a given input state. + // single value + [TestCase("A", "A:Held", ExpectedResult = SButtonState.Held)] + [TestCase("A", "A:Pressed", ExpectedResult = SButtonState.Pressed)] + [TestCase("A", "A:Released", ExpectedResult = SButtonState.Released)] + [TestCase("A", "A:None", ExpectedResult = SButtonState.None)] + + // multiple values + [TestCase("A + B + C, D", "A:Released, B:None, C:None, D:Pressed", ExpectedResult = SButtonState.Pressed)] // right pressed => pressed + [TestCase("A + B + C, D", "A:Pressed, B:Held, C:Pressed, D:None", ExpectedResult = SButtonState.Pressed)] // left pressed => pressed + [TestCase("A + B + C, D", "A:Pressed, B:Pressed, C:Released, D:None", ExpectedResult = SButtonState.None)] // one key released but other keys weren't down last tick => none + [TestCase("A + B + C, D", "A:Held, B:Held, C:Released, D:None", ExpectedResult = SButtonState.Released)] // all three keys were down last tick and now one is released => released + + // transitive + [TestCase("A, B", "A: Released, B: Pressed", ExpectedResult = SButtonState.Held)] + public SButtonState GetState(string input, string stateMap) + { + // act + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); + if (success && parsed?.Keybinds != null) + { + foreach (Keybind? keybind in parsed.Keybinds) + { +#pragma warning disable 618 // method is marked obsolete because it should only be used in unit tests + keybind.GetButtonState = key => this.GetStateFromMap(key, stateMap); +#pragma warning restore 618 + } + } + + // assert + Assert.IsTrue(success, "Parsing unexpected failed"); + Assert.IsNotNull(parsed, "The parsed result should not be null."); + Assert.IsNotNull(errors, message: "The errors should never be null."); + Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); + return parsed!.GetState(); + } + + + /********* + ** Private methods + *********/ + /// Get all defined buttons. + private static IEnumerable GetAllButtons() + { + foreach (SButton button in Enum.GetValues(typeof(SButton))) + yield return button; + } + + /// Get the button state defined by a mapping string. + /// The button to check. + /// The state map. + private SButtonState GetStateFromMap(SButton button, string stateMap) + { + foreach (string rawPair in stateMap.Split(',')) + { + // parse values + string[] parts = rawPair.Split(':', 2, StringSplitOptions.TrimEntries); + if (!Enum.TryParse(parts[0], ignoreCase: true, out SButton curButton)) + Assert.Fail($"The state map is invalid: unknown button value '{parts[0]}'"); + if (!Enum.TryParse(parts[1], ignoreCase: true, out SButtonState state)) + Assert.Fail($"The state map is invalid: unknown state value '{parts[1]}'"); + + // get state + if (curButton == button) + return state; + } + + Assert.Fail($"The state map doesn't define button value '{button}'."); + return SButtonState.None; + } + } +} diff --git a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs index b5494003..3219d89d 100644 --- a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs +++ b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; +using System.IO; using NUnit.Framework; using StardewModdingAPI.Toolkit.Utilities; @@ -5,6 +7,7 @@ namespace SMAPI.Tests.Utilities { /// Unit tests for . [TestFixture] + [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "These are standard game install paths.")] internal class PathUtilitiesTests { /********* @@ -13,136 +16,125 @@ namespace SMAPI.Tests.Utilities /// Sample paths used in unit tests. public static readonly SamplePath[] SamplePaths = { // Windows absolute path - new SamplePath - { - OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", + new( + OriginalPath: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", - Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" }, + Segments: new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" }, - NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley" + ), // Windows absolute path (with trailing slash) - new SamplePath - { - OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", + new( + OriginalPath: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", - Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley\" }, + Segments: new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley\" }, - NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", - NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley/" - }, + NormalizedOnWindows: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", + NormalizedOnUnix: @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley/" + ), // Windows relative path - new SamplePath - { - OriginalPath = @"Content\Characters\Dialogue\Abigail", + new( + OriginalPath: @"Content\Characters\Dialogue\Abigail", - Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" }, - SegmentsLimit3 = new [] { "Content", "Characters", @"Dialogue\Abigail" }, + Segments: new [] { "Content", "Characters", "Dialogue", "Abigail" }, + SegmentsLimit3: new [] { "Content", "Characters", @"Dialogue\Abigail" }, - NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail", - NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail" - }, + NormalizedOnWindows: @"Content\Characters\Dialogue\Abigail", + NormalizedOnUnix: @"Content/Characters/Dialogue/Abigail" + ), // Windows relative path (with directory climbing) - new SamplePath - { - OriginalPath = @"..\..\Content", + new( + OriginalPath: @"..\..\Content", - Segments = new [] { "..", "..", "Content" }, - SegmentsLimit3 = new [] { "..", "..", "Content" }, + Segments: new [] { "..", "..", "Content" }, + SegmentsLimit3: new [] { "..", "..", "Content" }, - NormalizedOnWindows = @"..\..\Content", - NormalizedOnUnix = @"../../Content" - }, + NormalizedOnWindows: @"..\..\Content", + NormalizedOnUnix: @"../../Content" + ), // Windows UNC path - new SamplePath - { - OriginalPath = @"\\unc\path", + new( + OriginalPath: @"\\unc\path", - Segments = new [] { "unc", "path" }, - SegmentsLimit3 = new [] { "unc", "path" }, + Segments: new [] { "unc", "path" }, + SegmentsLimit3: new [] { "unc", "path" }, - NormalizedOnWindows = @"\\unc\path", - NormalizedOnUnix = "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value - }, + NormalizedOnWindows: @"\\unc\path", + NormalizedOnUnix: "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value + ), // Linux absolute path - new SamplePath - { - OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley", + new( + OriginalPath: @"/home/.steam/steam/steamapps/common/Stardew Valley", - Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" }, + Segments: new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" }, - NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"\home\.steam\steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"/home/.steam/steam/steamapps/common/Stardew Valley" + ), // Linux absolute path (with trailing slash) - new SamplePath - { - OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley/", + new( + OriginalPath: @"/home/.steam/steam/steamapps/common/Stardew Valley/", - Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley/" }, + Segments: new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley/" }, - NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley\", - NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley/" - }, + NormalizedOnWindows: @"\home\.steam\steam\steamapps\common\Stardew Valley\", + NormalizedOnUnix: @"/home/.steam/steam/steamapps/common/Stardew Valley/" + ), // Linux absolute path (with ~) - new SamplePath - { - OriginalPath = @"~/.steam/steam/steamapps/common/Stardew Valley", + new( + OriginalPath: @"~/.steam/steam/steamapps/common/Stardew Valley", - Segments = new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" }, + Segments: new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" }, - NormalizedOnWindows = @"~\.steam\steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"~/.steam/steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"~\.steam\steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"~/.steam/steam/steamapps/common/Stardew Valley" + ), // Linux relative path - new SamplePath - { - OriginalPath = @"Content/Characters/Dialogue/Abigail", + new( + OriginalPath: @"Content/Characters/Dialogue/Abigail", - Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" }, - SegmentsLimit3 = new [] { "Content", "Characters", "Dialogue/Abigail" }, + Segments: new [] { "Content", "Characters", "Dialogue", "Abigail" }, + SegmentsLimit3: new [] { "Content", "Characters", "Dialogue/Abigail" }, - NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail", - NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail" - }, + NormalizedOnWindows: @"Content\Characters\Dialogue\Abigail", + NormalizedOnUnix: @"Content/Characters/Dialogue/Abigail" + ), // Linux relative path (with directory climbing) - new SamplePath - { - OriginalPath = @"../../Content", + new( + OriginalPath: @"../../Content", - Segments = new [] { "..", "..", "Content" }, - SegmentsLimit3 = new [] { "..", "..", "Content" }, + Segments: new [] { "..", "..", "Content" }, + SegmentsLimit3: new [] { "..", "..", "Content" }, - NormalizedOnWindows = @"..\..\Content", - NormalizedOnUnix = @"../../Content" - }, + NormalizedOnWindows: @"..\..\Content", + NormalizedOnUnix: @"../../Content" + ), // Mixed directory separators - new SamplePath - { - OriginalPath = @"C:\some/mixed\path/separators", + new( + OriginalPath: @"C:\some/mixed\path/separators", - Segments = new [] { "C:", "some", "mixed", "path", "separators" }, - SegmentsLimit3 = new [] { "C:", "some", @"mixed\path/separators" }, + Segments: new [] { "C:", "some", "mixed", "path", "separators" }, + SegmentsLimit3: new [] { "C:", "some", @"mixed\path/separators" }, - NormalizedOnWindows = @"C:\some\mixed\path\separators", - NormalizedOnUnix = @"C:/some/mixed/path/separators" - }, + NormalizedOnWindows: @"C:\some\mixed\path\separators", + NormalizedOnUnix: @"C:/some/mixed/path/separators" + ) }; @@ -175,9 +167,26 @@ namespace SMAPI.Tests.Utilities } /**** - ** NormalizePathSeparators + ** NormalizeAssetName ****/ - [Test(Description = "Assert that PathUtilities.NormalizePathSeparators normalizes paths correctly.")] + [Test(Description = "Assert that PathUtilities.NormalizeAssetName normalizes paths correctly.")] + [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))] + public void NormalizeAssetName(SamplePath path) + { + if (Path.IsPathRooted(path.OriginalPath) || path.OriginalPath.StartsWith('/') || path.OriginalPath.StartsWith('\\')) + Assert.Ignore("Absolute paths can't be used as asset names."); + + // act + string normalized = PathUtilities.NormalizeAssetName(path.OriginalPath); + + // assert + Assert.AreEqual(path.NormalizedOnUnix, normalized); // MonoGame uses the Linux format + } + + /**** + ** NormalizePath + ****/ + [Test(Description = "Assert that PathUtilities.NormalizePath normalizes paths correctly.")] [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))] public void NormalizePath(SamplePath path) { @@ -251,7 +260,7 @@ namespace SMAPI.Tests.Utilities [TestCase( @"~/parent", @"~/PARENT/child", - ExpectedResult = @"child" // note: incorrect on Linux and sometimes MacOS, but not worth the complexity of detecting whether the filesystem is case-sensitive for SMAPI's purposes + ExpectedResult = @"child" // note: incorrect on Linux and sometimes macOS, but not worth the complexity of detecting whether the filesystem is case-sensitive for SMAPI's purposes )] #endif public string GetRelativePath(string sourceDir, string targetPath) @@ -263,14 +272,14 @@ namespace SMAPI.Tests.Utilities /********* ** Private classes *********/ - public class SamplePath + /// A sample path in multiple formats. + /// The original path to pass to the . + /// The normalized path segments. + /// The normalized path segments, if we stop segmenting after the second one. + /// The normalized form on Windows. + /// The normalized form on Linux or macOS. + public record SamplePath(string OriginalPath, string[] Segments, string[] SegmentsLimit3, string NormalizedOnWindows, string NormalizedOnUnix) { - public string OriginalPath { get; set; } - public string[] Segments { get; set; } - public string[] SegmentsLimit3 { get; set; } - public string NormalizedOnWindows { get; set; } - public string NormalizedOnUnix { get; set; } - public override string ToString() { return this.OriginalPath; diff --git a/src/SMAPI.Tests/Utilities/SDateTests.cs b/src/SMAPI.Tests/Utilities/SDateTests.cs index 0461952e..b9c3d202 100644 --- a/src/SMAPI.Tests/Utilities/SDateTests.cs +++ b/src/SMAPI.Tests/Utilities/SDateTests.cs @@ -16,9 +16,12 @@ namespace SMAPI.Tests.Utilities /********* ** Fields *********/ - /// All valid seasons. + /// The valid seasons. private static readonly string[] ValidSeasons = { "spring", "summer", "fall", "winter" }; + /// Sample user inputs for season names. + private static readonly string[] SampleSeasonValues = SDateTests.ValidSeasons.Concat(new[] { " WIntEr " }).ToArray(); + /// All valid days of a month. private static readonly int[] ValidDays = Enumerable.Range(1, 28).ToArray(); @@ -55,19 +58,18 @@ namespace SMAPI.Tests.Utilities ** Constructor ****/ [Test(Description = "Assert that the constructor sets the expected values for all valid dates.")] - public void Constructor_SetsExpectedValues([ValueSource(nameof(SDateTests.ValidSeasons))] string season, [ValueSource(nameof(SDateTests.ValidDays))] int day, [Values(1, 2, 100)] int year) + public void Constructor_SetsExpectedValues([ValueSource(nameof(SDateTests.SampleSeasonValues))] string season, [ValueSource(nameof(SDateTests.ValidDays))] int day, [Values(1, 2, 100)] int year) { // act - SDate date = new SDate(day, season, year); + SDate date = new(day, season, year); // assert Assert.AreEqual(day, date.Day); - Assert.AreEqual(season, date.Season); + Assert.AreEqual(season.Trim().ToLowerInvariant(), date.Season); Assert.AreEqual(year, date.Year); } [Test(Description = "Assert that the constructor throws an exception if the values are invalid.")] - [TestCase(01, "Spring", 1)] // seasons are case-sensitive [TestCase(01, "springs", 1)] // invalid season name [TestCase(-1, "spring", 1)] // day < 0 [TestCase(0, "spring", 1)] // day zero @@ -252,9 +254,9 @@ namespace SMAPI.Tests.Utilities { foreach (int day in SDateTests.ValidDays) { - SDate date = new SDate(day, season, year); + SDate date = new(day, season, year); int hash = date.GetHashCode(); - if (hashes.TryGetValue(hash, out SDate otherDate)) + if (hashes.TryGetValue(hash, out SDate? otherDate)) Assert.Fail($"Received identical hash code {hash} for dates {otherDate} and {date}."); if (hash < lastHash) Assert.Fail($"Received smaller hash code for date {date} ({hash}) relative to {hashes[lastHash]} ({lastHash})."); @@ -294,7 +296,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)] - public bool Operators_Equals(string now, string other) + public bool Operators_Equals(string? now, string other) { return this.GetDate(now) == this.GetDate(other); } @@ -308,7 +310,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)] - public bool Operators_NotEquals(string now, string other) + public bool Operators_NotEquals(string? now, string other) { return this.GetDate(now) != this.GetDate(other); } @@ -322,7 +324,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)] - public bool Operators_LessThan(string now, string other) + public bool Operators_LessThan(string? now, string other) { return this.GetDate(now) < this.GetDate(other); } @@ -336,7 +338,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)] - public bool Operators_LessThanOrEqual(string now, string other) + public bool Operators_LessThanOrEqual(string? now, string other) { return this.GetDate(now) <= this.GetDate(other); } @@ -350,7 +352,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)] - public bool Operators_MoreThan(string now, string other) + public bool Operators_MoreThan(string? now, string other) { return this.GetDate(now) > this.GetDate(other); } @@ -364,7 +366,7 @@ namespace SMAPI.Tests.Utilities [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)] [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)] - public bool Operators_MoreThanOrEqual(string now, string other) + public bool Operators_MoreThanOrEqual(string? now, string other) { return this.GetDate(now) > this.GetDate(other); } @@ -375,7 +377,8 @@ namespace SMAPI.Tests.Utilities *********/ /// Convert a string date into a game date, to make unit tests easier to read. /// The date string like "dd MMMM yy". - private SDate GetDate(string dateStr) + [return: NotNullIfNotNull("dateStr")] + private SDate? GetDate(string? dateStr) { if (dateStr == null) return null; diff --git a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs index ac4ef39b..77c0da5f 100644 --- a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs +++ b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs @@ -61,10 +61,10 @@ namespace SMAPI.Tests.Utilities [TestCase("apple")] [TestCase("-apple")] [TestCase("-5")] - public void Constructor_FromString_WithInvalidValues(string input) + public void Constructor_FromString_WithInvalidValues(string? input) { if (input == null) - this.AssertAndLogException(() => new SemanticVersion(input)); + this.AssertAndLogException(() => new SemanticVersion(input!)); else this.AssertAndLogException(() => new SemanticVersion(input)); } @@ -91,7 +91,7 @@ namespace SMAPI.Tests.Utilities [TestCase("1.2.3.4-some-tag.4 ")] public void Constructor_FromString_Standard_DisallowsNonStandardVersion(string input) { - Assert.Throws(() => new SemanticVersion(input)); + Assert.Throws(() => _ = new SemanticVersion(input)); } /// Assert the parsed version when constructed from standard parts. @@ -110,7 +110,7 @@ namespace SMAPI.Tests.Utilities [TestCase(1, 2, 3, "some-tag.4 ", null, ExpectedResult = "1.2.3-some-tag.4")] [TestCase(1, 2, 3, "some-tag.4 ", "build.004", ExpectedResult = "1.2.3-some-tag.4+build.004")] [TestCase(1, 2, 0, null, "3.4.5-build.004", ExpectedResult = "1.2.0+3.4.5-build.004")] - public string Constructor_FromParts(int major, int minor, int patch, string prerelease, string build) + public string Constructor_FromParts(int major, int minor, int patch, string? prerelease, string? build) { // act ISemanticVersion version = new SemanticVersion(major, minor, patch, prerelease, build); @@ -217,11 +217,16 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = 1)] [TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = 1)] [TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = 1)] - public int CompareTo(string versionStrA, string versionStrB) + + // null + [TestCase("1.0.0", null, ExpectedResult = 1)] // null is always less than any value per CompareTo remarks + public int CompareTo(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = new SemanticVersion(versionStrB); + ISemanticVersion? versionB = versionStrB != null + ? new SemanticVersion(versionStrB) + : null; // assert return versionA.CompareTo(versionB); @@ -260,14 +265,19 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = false)] [TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = false)] [TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = false)] - public bool IsOlderThan(string versionStrA, string versionStrB) + + // null + [TestCase("1.0.0", null, ExpectedResult = false)] // null is always less than any value per CompareTo remarks + public bool IsOlderThan(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = new SemanticVersion(versionStrB); + ISemanticVersion? versionB = versionStrB != null + ? new SemanticVersion(versionStrB) + : null; // assert - Assert.AreEqual(versionA.IsOlderThan(versionB), versionA.IsOlderThan(versionB.ToString()), "The two signatures returned different results."); + Assert.AreEqual(versionA.IsOlderThan(versionB), versionA.IsOlderThan(versionB?.ToString()), "The two signatures returned different results."); return versionA.IsOlderThan(versionB); } @@ -304,14 +314,19 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = true)] [TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = true)] [TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = true)] - public bool IsNewerThan(string versionStrA, string versionStrB) + + // null + [TestCase("1.0.0", null, ExpectedResult = true)] // null is always less than any value per CompareTo remarks + public bool IsNewerThan(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = new SemanticVersion(versionStrB); + ISemanticVersion? versionB = versionStrB != null + ? new SemanticVersion(versionStrB) + : null; // assert - Assert.AreEqual(versionA.IsNewerThan(versionB), versionA.IsNewerThan(versionB.ToString()), "The two signatures returned different results."); + Assert.AreEqual(versionA.IsNewerThan(versionB), versionA.IsNewerThan(versionB?.ToString()), "The two signatures returned different results."); return versionA.IsNewerThan(versionB); } @@ -322,7 +337,7 @@ namespace SMAPI.Tests.Utilities /// The main version. /// The lower version number. /// The upper version number. - [Test(Description = "Assert that version.IsNewerThan returns the expected value.")] + [Test(Description = "Assert that version.IsBetween returns the expected value.")] // is between [TestCase("0.5.7-beta.3", "0.5.7-beta.3", "0.5.7-beta.3", ExpectedResult = true)] [TestCase("1.0", "1.0", "1.1", ExpectedResult = true)] @@ -330,6 +345,7 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0", "0.5", "1.1", ExpectedResult = true)] [TestCase("1.0-beta.2", "1.0-beta.1", "1.0-beta.3", ExpectedResult = true)] [TestCase("1.0-beta-2", "1.0-beta-1", "1.0-beta-3", ExpectedResult = true)] + [TestCase("1.0.0", null, "1.0.0", ExpectedResult = true)] // null is always less than any value per CompareTo remarks // is not between [TestCase("1.0-beta", "1.0", "1.1", ExpectedResult = false)] @@ -337,15 +353,20 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0-beta.2", "1.1", "1.0", ExpectedResult = false)] [TestCase("1.0-beta.2", "1.0-beta.10", "1.0-beta.3", ExpectedResult = false)] [TestCase("1.0-beta-2", "1.0-beta-10", "1.0-beta-3", ExpectedResult = false)] - public bool IsBetween(string versionStr, string lowerStr, string upperStr) + [TestCase("1.0.0", "1.0.0", null, ExpectedResult = false)] // null is always less than any value per CompareTo remarks + public bool IsBetween(string versionStr, string? lowerStr, string? upperStr) { // arrange - ISemanticVersion lower = new SemanticVersion(lowerStr); - ISemanticVersion upper = new SemanticVersion(upperStr); + ISemanticVersion? lower = lowerStr != null + ? new SemanticVersion(lowerStr) + : null; + ISemanticVersion? upper = upperStr != null + ? new SemanticVersion(upperStr) + : null; ISemanticVersion version = new SemanticVersion(versionStr); // assert - Assert.AreEqual(version.IsBetween(lower, upper), version.IsBetween(lower.ToString(), upper.ToString()), "The two signatures returned different results."); + Assert.AreEqual(version.IsBetween(lower, upper), version.IsBetween(lower?.ToString(), upper?.ToString()), "The two signatures returned different results."); return version.IsBetween(lower, upper); } @@ -361,11 +382,11 @@ namespace SMAPI.Tests.Utilities { // act string json = JsonConvert.SerializeObject(new SemanticVersion(versionStr)); - SemanticVersion after = JsonConvert.DeserializeObject(json); + SemanticVersion? after = JsonConvert.DeserializeObject(json); // assert Assert.IsNotNull(after, "The semantic version after deserialization is unexpectedly null."); - Assert.AreEqual(versionStr, after.ToString(), "The semantic version after deserialization doesn't match the input version."); + Assert.AreEqual(versionStr, after!.ToString(), "The semantic version after deserialization doesn't match the input version."); } @@ -395,7 +416,7 @@ namespace SMAPI.Tests.Utilities public void GameVersion(string versionStr) { // act - GameVersion version = new GameVersion(versionStr); + GameVersion version = new(versionStr); // assert Assert.AreEqual(versionStr, version.ToString(), "The game version did not round-trip to the same value."); @@ -413,7 +434,7 @@ namespace SMAPI.Tests.Utilities /// The prerelease tag. /// The build metadata. /// Whether the version should be marked as non-standard. - private void AssertParts(ISemanticVersion version, int major, int minor, int patch, string prerelease, string build, bool nonStandard) + private void AssertParts(ISemanticVersion version, int major, int minor, int patch, string? prerelease, string? build, bool nonStandard) { Assert.AreEqual(major, version.MajorVersion, "The major version doesn't match."); Assert.AreEqual(minor, version.MinorVersion, "The minor version doesn't match."); @@ -426,9 +447,8 @@ namespace SMAPI.Tests.Utilities /// Assert that the expected exception type is thrown, and log the action output and thrown exception. /// The expected exception type. /// The action which may throw the exception. - /// The message to log if the expected exception isn't thrown. [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "The message argument is deliberately only used in precondition checks since this is an assertion method.")] - private void AssertAndLogException(Func action, string message = null) + private void AssertAndLogException(Func action) where T : Exception { this.AssertAndLogException(() => @@ -443,7 +463,7 @@ namespace SMAPI.Tests.Utilities /// The action which may throw the exception. /// The message to log if the expected exception isn't thrown. [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "The message argument is deliberately only used in precondition checks since this is an assertion method.")] - private void AssertAndLogException(Action action, string message = null) + private void AssertAndLogException(Action action, string? message = null) where T : Exception { try @@ -455,7 +475,7 @@ namespace SMAPI.Tests.Utilities TestContext.WriteLine($"Exception thrown:\n{ex}"); return; } - catch (Exception ex) when (!(ex is AssertionException)) + catch (Exception ex) when (ex is not AssertionException) { TestContext.WriteLine($"Exception thrown:\n{ex}"); Assert.Fail(message ?? $"Didn't throw the expected exception; expected {typeof(T).FullName}, got {ex.GetType().FullName}."); diff --git a/src/SMAPI.Tests/WikiClient/ChangeDescriptorTests.cs b/src/SMAPI.Tests/WikiClient/ChangeDescriptorTests.cs new file mode 100644 index 00000000..8e7e1fb8 --- /dev/null +++ b/src/SMAPI.Tests/WikiClient/ChangeDescriptorTests.cs @@ -0,0 +1,136 @@ +using System.Collections.Generic; +using NUnit.Framework; +using StardewModdingAPI; +using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; + +namespace SMAPI.Tests.WikiClient +{ + /// Unit tests for . + [TestFixture] + internal class ChangeDescriptorTests + { + /********* + ** Unit tests + *********/ + /**** + ** Constructor + ****/ + [Test(Description = "Assert that Parse sets the expected values for valid and invalid descriptors.")] + public void Parse_SetsExpectedValues_Raw() + { + // arrange + string rawDescriptor = "-Nexus:2400, -B, XX → YY, Nexus:451,+A, XXX → YYY, invalidA →, → invalidB"; + string[] expectedAdd = { "Nexus:451", "A" }; + string[] expectedRemove = { "Nexus:2400", "B" }; + IDictionary expectedReplace = new Dictionary + { + ["XX"] = "YY", + ["XXX"] = "YYY" + }; + string[] expectedErrors = { + "Failed parsing ' invalidA →': can't map to a blank value. Use the '-value' format to remove a value.", + "Failed parsing ' → invalidB': can't map from a blank old value. Use the '+value' format to add a value." + }; + + // act + ChangeDescriptor parsed = ChangeDescriptor.Parse(rawDescriptor, out string[] errors); + + // assert + Assert.That(parsed.Add, Is.EquivalentTo(expectedAdd), $"{nameof(parsed.Add)} doesn't match the expected value."); + Assert.That(parsed.Remove, Is.EquivalentTo(expectedRemove), $"{nameof(parsed.Replace)} doesn't match the expected value."); + Assert.That(parsed.Replace, Is.EquivalentTo(expectedReplace), $"{nameof(parsed.Replace)} doesn't match the expected value."); + Assert.That(errors, Is.EquivalentTo(expectedErrors), $"{nameof(errors)} doesn't match the expected value."); + } + + [Test(Description = "Assert that Parse sets the expected values for descriptors when a format callback is specified.")] + public void Parse_SetsExpectedValues_Formatted() + { + // arrange + string rawDescriptor = "-1.0.1, -2.0-beta, 1.00 → 1.0, 1.0.0,+2.0-beta.15, 2.0 → 2.0-beta, invalidA →, → invalidB"; + string[] expectedAdd = { "1.0.0", "2.0.0-beta.15" }; + string[] expectedRemove = { "1.0.1", "2.0.0-beta" }; + IDictionary expectedReplace = new Dictionary + { + ["1.00"] = "1.0.0", + ["2.0.0"] = "2.0.0-beta" + }; + string[] expectedErrors = { + "Failed parsing ' invalidA →': can't map to a blank value. Use the '-value' format to remove a value.", + "Failed parsing ' → invalidB': can't map from a blank old value. Use the '+value' format to add a value." + }; + + // act + ChangeDescriptor parsed = ChangeDescriptor.Parse( + rawDescriptor, + out string[] errors, + formatValue: raw => SemanticVersion.TryParse(raw, out ISemanticVersion? version) + ? version.ToString() + : raw + ); + + // assert + Assert.That(parsed.Add, Is.EquivalentTo(expectedAdd), $"{nameof(parsed.Add)} doesn't match the expected value."); + Assert.That(parsed.Remove, Is.EquivalentTo(expectedRemove), $"{nameof(parsed.Replace)} doesn't match the expected value."); + Assert.That(parsed.Replace, Is.EquivalentTo(expectedReplace), $"{nameof(parsed.Replace)} doesn't match the expected value."); + Assert.That(errors, Is.EquivalentTo(expectedErrors), $"{nameof(errors)} doesn't match the expected value."); + } + + [Test(Description = "Assert that Apply returns the expected value for the given descriptor.")] + + // null input + [TestCase(null, "", ExpectedResult = null)] + [TestCase(null, "+Nexus:2400", ExpectedResult = "Nexus:2400")] + [TestCase(null, "-Nexus:2400", ExpectedResult = null)] + + // blank input + [TestCase("", null, ExpectedResult = "")] + [TestCase("", "", ExpectedResult = "")] + + // add value + [TestCase("", "+Nexus:2400", ExpectedResult = "Nexus:2400")] + [TestCase("Nexus:2400", "+Nexus:2400", ExpectedResult = "Nexus:2400")] + [TestCase("Nexus:2400", "Nexus:2400", ExpectedResult = "Nexus:2400")] + [TestCase("Nexus:2400", "+Nexus:2401", ExpectedResult = "Nexus:2400, Nexus:2401")] + [TestCase("Nexus:2400", "Nexus:2401", ExpectedResult = "Nexus:2400, Nexus:2401")] + + // remove value + [TestCase("", "-Nexus:2400", ExpectedResult = "")] + [TestCase("Nexus:2400", "-Nexus:2400", ExpectedResult = "")] + [TestCase("Nexus:2400", "-Nexus:2401", ExpectedResult = "Nexus:2400")] + + // replace value + [TestCase("", "Nexus:2400 → Nexus:2401", ExpectedResult = "")] + [TestCase("Nexus:2400", "Nexus:2400 → Nexus:2401", ExpectedResult = "Nexus:2401")] + [TestCase("Nexus:1", "Nexus: 2400 → Nexus: 2401", ExpectedResult = "Nexus:1")] + + // complex strings + [TestCase("", "+Nexus:A, Nexus:B, -Chucklefish:14, Nexus:2400 → Nexus:2401, Nexus:A→Nexus:B", ExpectedResult = "Nexus:A, Nexus:B")] + [TestCase("Nexus:2400", "+Nexus:A, Nexus:B, -Chucklefish:14, Nexus:2400 → Nexus:2401, Nexus:A→Nexus:B", ExpectedResult = "Nexus:2401, Nexus:A, Nexus:B")] + [TestCase("Nexus:2400, Nexus:2401, Nexus:B,Chucklefish:14", "+Nexus:A, Nexus:B, -Chucklefish:14, Nexus:2400 → Nexus:2401, Nexus:A→Nexus:B", ExpectedResult = "Nexus:2401, Nexus:2401, Nexus:B, Nexus:A")] + public string Apply_Raw(string input, string? descriptor) + { + ChangeDescriptor parsed = ChangeDescriptor.Parse(descriptor, out string[] errors); + + Assert.IsEmpty(errors, "Parsing the descriptor failed."); + + return parsed.ApplyToCopy(input); + } + + [Test(Description = "Assert that ToString returns the expected normalized descriptors.")] + [TestCase(null, ExpectedResult = "")] + [TestCase("", ExpectedResult = "")] + [TestCase("+ Nexus:2400", ExpectedResult = "+Nexus:2400")] + [TestCase(" Nexus:2400 ", ExpectedResult = "+Nexus:2400")] + [TestCase("-Nexus:2400", ExpectedResult = "-Nexus:2400")] + [TestCase(" Nexus:2400 →Nexus:2401 ", ExpectedResult = "Nexus:2400 → Nexus:2401")] + [TestCase("+Nexus:A, Nexus:B, -Chucklefish:14, Nexus:2400 → Nexus:2401, Nexus:A→Nexus:B", ExpectedResult = "+Nexus:A, +Nexus:B, -Chucklefish:14, Nexus:2400 → Nexus:2401, Nexus:A → Nexus:B")] + public string ToString(string? descriptor) + { + var parsed = ChangeDescriptor.Parse(descriptor, out string[] errors); + + Assert.IsEmpty(errors, "Parsing the descriptor failed."); + + return parsed.ToString(); + } + } +} diff --git a/src/SMAPI.Toolkit.CoreInterfaces/IManifest.cs b/src/SMAPI.Toolkit.CoreInterfaces/IManifest.cs index 7375f005..ee6cc0b6 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/IManifest.cs +++ b/src/SMAPI.Toolkit.CoreInterfaces/IManifest.cs @@ -21,16 +21,16 @@ namespace StardewModdingAPI ISemanticVersion Version { get; } /// The minimum SMAPI version required by this mod, if any. - ISemanticVersion MinimumApiVersion { get; } + ISemanticVersion? MinimumApiVersion { get; } /// The unique mod ID. string UniqueID { get; } /// The name of the DLL in the directory that has the Entry method. Mutually exclusive with . - string EntryDll { get; } + string? EntryDll { get; } /// The mod which will read this as a content pack. Mutually exclusive with . - IManifestContentPackFor ContentPackFor { get; } + IManifestContentPackFor? ContentPackFor { get; } /// The other mods that must be loaded before this mod. IManifestDependency[] Dependencies { get; } diff --git a/src/SMAPI.Toolkit.CoreInterfaces/IManifestContentPackFor.cs b/src/SMAPI.Toolkit.CoreInterfaces/IManifestContentPackFor.cs index f05a3873..52ac8f1c 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/IManifestContentPackFor.cs +++ b/src/SMAPI.Toolkit.CoreInterfaces/IManifestContentPackFor.cs @@ -7,6 +7,6 @@ namespace StardewModdingAPI string UniqueID { get; } /// The minimum required version (if any). - ISemanticVersion MinimumVersion { get; } + ISemanticVersion? MinimumVersion { get; } } } diff --git a/src/SMAPI.Toolkit.CoreInterfaces/IManifestDependency.cs b/src/SMAPI.Toolkit.CoreInterfaces/IManifestDependency.cs index e86cd1f4..58425eb2 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/IManifestDependency.cs +++ b/src/SMAPI.Toolkit.CoreInterfaces/IManifestDependency.cs @@ -10,7 +10,7 @@ namespace StardewModdingAPI string UniqueID { get; } /// The minimum required version (if any). - ISemanticVersion MinimumVersion { get; } + ISemanticVersion? MinimumVersion { get; } /// Whether the dependency must be installed to use the mod. bool IsRequired { get; } diff --git a/src/SMAPI.Toolkit.CoreInterfaces/ISemanticVersion.cs b/src/SMAPI.Toolkit.CoreInterfaces/ISemanticVersion.cs index b228b2d1..dc226b7c 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/ISemanticVersion.cs +++ b/src/SMAPI.Toolkit.CoreInterfaces/ISemanticVersion.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI { @@ -18,46 +19,55 @@ namespace StardewModdingAPI int PatchVersion { get; } /// An optional prerelease tag. - string PrereleaseTag { get; } + string? PrereleaseTag { get; } /// Optional build metadata. This is ignored when determining version precedence. - string BuildMetadata { get; } + string? BuildMetadata { get; } /********* ** Accessors *********/ /// Whether this is a prerelease version. +#if NET5_0_OR_GREATER + [MemberNotNullWhen(true, nameof(ISemanticVersion.PrereleaseTag))] +#endif bool IsPrerelease(); /// Get whether this version is older than the specified version. /// The version to compare with this instance. - bool IsOlderThan(ISemanticVersion other); + /// Although the parameter is nullable, it isn't optional. A null version is considered earlier than every possible valid version, so passing null to will always return false. + bool IsOlderThan(ISemanticVersion? other); /// Get whether this version is older than the specified version. - /// The version to compare with this instance. + /// The version to compare with this instance. A null value is never older. /// The specified version is not a valid semantic version. - bool IsOlderThan(string other); + /// Although the parameter is nullable, it isn't optional. A null version is considered earlier than every possible valid version, so passing null to will always return false. + bool IsOlderThan(string? other); /// Get whether this version is newer than the specified version. - /// The version to compare with this instance. - bool IsNewerThan(ISemanticVersion other); + /// The version to compare with this instance. A null value is always older. + /// Although the parameter is nullable, it isn't optional. A null version is considered earlier than every possible valid version, so passing null to will always return true. + bool IsNewerThan(ISemanticVersion? other); /// Get whether this version is newer than the specified version. - /// The version to compare with this instance. + /// The version to compare with this instance. A null value is always older. /// The specified version is not a valid semantic version. - bool IsNewerThan(string other); + /// Although the parameter is nullable, it isn't optional. A null version is considered earlier than every possible valid version, so passing null to will always return true. + bool IsNewerThan(string? other); /// Get whether this version is between two specified versions (inclusively). - /// The minimum version. - /// The maximum version. - bool IsBetween(ISemanticVersion min, ISemanticVersion max); + /// The minimum version. A null value is always older. + /// The maximum version. A null value is never newer. + /// Although the and parameters are nullable, they are not optional. A null version is considered earlier than every possible valid version. For example, passing null to will always return false, since no valid version can be earlier than null. + bool IsBetween(ISemanticVersion? min, ISemanticVersion? max); /// Get whether this version is between two specified versions (inclusively). - /// The minimum version. - /// The maximum version. + /// The minimum version. A null value is always older. + /// The maximum version. A null value is never newer. /// One of the specified versions is not a valid semantic version. - bool IsBetween(string min, string max); + /// Although the and parameters are nullable, they are not optional. A null version is considered earlier than every possible valid version. For example, passing null to will always return false, since no valid version can be earlier than null. + bool IsBetween(string? min, string? max); /// Get a string representation of the version. string ToString(); diff --git a/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj b/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj index 2bddc46a..4c92b4db 100644 --- a/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj +++ b/src/SMAPI.Toolkit.CoreInterfaces/SMAPI.Toolkit.CoreInterfaces.csproj @@ -2,9 +2,8 @@ StardewModdingAPI Provides toolkit interfaces which are available to SMAPI mods. - net4.5;netstandard2.0 + net5.0; netstandard2.0 true - x86 diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryModel.cs index 2f58a3f1..4fc4ea54 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryModel.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryModel.cs @@ -1,3 +1,5 @@ +using System; + namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi { /// Metadata about a mod. @@ -7,15 +9,26 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi ** Accessors *********/ /// The mod's unique ID (if known). - public string ID { get; set; } + public string ID { get; } /// The update version recommended by the web API based on its version update and mapping rules. - public ModEntryVersionModel SuggestedUpdate { get; set; } + public ModEntryVersionModel? SuggestedUpdate { get; set; } /// Optional extended data which isn't needed for update checks. - public ModExtendedMetadataModel Metadata { get; set; } + public ModExtendedMetadataModel? Metadata { get; set; } /// The errors that occurred while fetching update data. - public string[] Errors { get; set; } = new string[0]; + public string[] Errors { get; set; } = Array.Empty(); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod's unique ID (if known). + public ModEntryModel(string id) + { + this.ID = id; + } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs index 188db31d..a1e78986 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs @@ -11,18 +11,15 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi *********/ /// The version number. [JsonConverter(typeof(NonStandardSemanticVersionConverter))] - public ISemanticVersion Version { get; set; } + public ISemanticVersion Version { get; } /// The mod page URL. - public string Url { get; set; } + public string Url { get; } /********* ** Public methods *********/ - /// Construct an instance. - public ModEntryVersionModel() { } - /// Construct an instance. /// The version number. /// The mod page URL. diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModExtendedMetadataModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModExtendedMetadataModel.cs index 8c21e4e0..272a2063 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModExtendedMetadataModel.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModExtendedMetadataModel.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; @@ -17,10 +18,10 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi ** Mod info ****/ /// The mod's unique ID. A mod may have multiple current IDs in rare cases (e.g. due to parallel releases or unofficial updates). - public string[] ID { get; set; } = new string[0]; + public string[] ID { get; set; } = Array.Empty(); /// The mod's display name. - public string Name { get; set; } + public string? Name { get; set; } /// The mod ID on Nexus. public int? NexusID { get; set; } @@ -32,31 +33,31 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi public int? CurseForgeID { get; set; } /// The mod key in the CurseForge mod repo (used in mod page URLs). - public string CurseForgeKey { get; set; } + public string? CurseForgeKey { get; set; } /// The mod ID in the ModDrop mod repo. public int? ModDropID { get; set; } /// The GitHub repository in the form 'owner/repo'. - public string GitHubRepo { get; set; } + public string? GitHubRepo { get; set; } /// The URL to a non-GitHub source repo. - public string CustomSourceUrl { get; set; } + public string? CustomSourceUrl { get; set; } /// The custom mod page URL (if applicable). - public string CustomUrl { get; set; } + public string? CustomUrl { get; set; } /// The main version. - public ModEntryVersionModel Main { get; set; } + public ModEntryVersionModel? Main { get; set; } /// The latest optional version, if newer than . - public ModEntryVersionModel Optional { get; set; } + public ModEntryVersionModel? Optional { get; set; } /// The latest unofficial version, if newer than and . - public ModEntryVersionModel Unofficial { get; set; } + public ModEntryVersionModel? Unofficial { get; set; } /// The latest unofficial version for the current Stardew Valley or SMAPI beta, if any. - public ModEntryVersionModel UnofficialForBeta { get; set; } + public ModEntryVersionModel? UnofficialForBeta { get; set; } /**** ** Stable compatibility @@ -66,10 +67,10 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi public WikiCompatibilityStatus? CompatibilityStatus { get; set; } /// The human-readable summary of the compatibility status or workaround, without HTML formatting. - public string CompatibilitySummary { get; set; } + public string? CompatibilitySummary { get; set; } /// The game or SMAPI version which broke this mod, if applicable. - public string BrokeIn { get; set; } + public string? BrokeIn { get; set; } /**** ** Beta compatibility @@ -79,19 +80,22 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi public WikiCompatibilityStatus? BetaCompatibilityStatus { get; set; } /// The human-readable summary of the compatibility status or workaround for the Stardew Valley beta (if any), without HTML formatting. - public string BetaCompatibilitySummary { get; set; } + public string? BetaCompatibilitySummary { get; set; } /// The beta game or SMAPI version which broke this mod, if applicable. - public string BetaBrokeIn { get; set; } + public string? BetaBrokeIn { get; set; } /**** ** Version mappings ****/ - /// Maps local versions to a semantic version for update checks. - public IDictionary MapLocalVersions { get; set; } + /// A serialized change descriptor to apply to the local version during update checks (see ). + public string? ChangeLocalVersions { get; set; } - /// Maps remote versions to a semantic version for update checks. - public IDictionary MapRemoteVersions { get; set; } + /// A serialized change descriptor to apply to the remote version during update checks (see ). + public string? ChangeRemoteVersions { get; set; } + + /// A serialized change descriptor to apply to the update keys during update checks (see ). + public string? ChangeUpdateKeys { get; set; } /********* @@ -107,7 +111,7 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi /// The latest optional version, if newer than . /// The latest unofficial version, if newer than and . /// The latest unofficial version for the current Stardew Valley or SMAPI beta, if any. - public ModExtendedMetadataModel(WikiModEntry wiki, ModDataRecord db, ModEntryVersionModel main, ModEntryVersionModel optional, ModEntryVersionModel unofficial, ModEntryVersionModel unofficialForBeta) + public ModExtendedMetadataModel(WikiModEntry? wiki, ModDataRecord? db, ModEntryVersionModel? main, ModEntryVersionModel? optional, ModEntryVersionModel? unofficial, ModEntryVersionModel? unofficialForBeta) { // versions this.Main = main; @@ -137,8 +141,9 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi this.BetaCompatibilitySummary = wiki.BetaCompatibility?.Summary; this.BetaBrokeIn = wiki.BetaCompatibility?.BrokeIn; - this.MapLocalVersions = wiki.MapLocalVersions; - this.MapRemoteVersions = wiki.MapRemoteVersions; + this.ChangeLocalVersions = wiki.Overrides?.ChangeLocalVersions?.ToString(); + this.ChangeRemoteVersions = wiki.Overrides?.ChangeRemoteVersions?.ToString(); + this.ChangeUpdateKeys = wiki.Overrides?.ChangeUpdateKeys?.ToString(); } // internal DB data diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchEntryModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchEntryModel.cs index bf81e102..9c11e1db 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchEntryModel.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchEntryModel.cs @@ -1,3 +1,6 @@ +using System; +using System.Linq; + namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi { /// Specifies the identifiers for a mod to match. @@ -7,37 +10,39 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi ** Accessors *********/ /// The unique mod ID. - public string ID { get; set; } + public string ID { get; } /// The namespaced mod update keys (if available). - public string[] UpdateKeys { get; set; } + public string[] UpdateKeys { get; private set; } /// The mod version installed by the local player. This is used for version mapping in some cases. - public ISemanticVersion InstalledVersion { get; set; } + public ISemanticVersion? InstalledVersion { get; } /// Whether the installed version is broken or could not be loaded. - public bool IsBroken { get; set; } + public bool IsBroken { get; } /********* ** Public methods *********/ - /// Construct an empty instance. - public ModSearchEntryModel() - { - // needed for JSON deserializing - } - /// Construct an instance. /// The unique mod ID. /// The version installed by the local player. This is used for version mapping in some cases. /// The namespaced mod update keys (if available). /// Whether the installed version is broken or could not be loaded. - public ModSearchEntryModel(string id, ISemanticVersion installedVersion, string[] updateKeys, bool isBroken = false) + public ModSearchEntryModel(string id, ISemanticVersion? installedVersion, string[]? updateKeys, bool isBroken = false) { this.ID = id; this.InstalledVersion = installedVersion; - this.UpdateKeys = updateKeys ?? new string[0]; + this.UpdateKeys = updateKeys ?? Array.Empty(); + this.IsBroken = isBroken; + } + + /// Add update keys for the mod. + /// The update keys to add. + public void AddUpdateKeys(params string[] updateKeys) + { + this.UpdateKeys = this.UpdateKeys.Concat(updateKeys).ToArray(); } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchModel.cs index 73698173..3c74bab0 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchModel.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModSearchModel.cs @@ -1,3 +1,5 @@ +using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Toolkit.Utilities; @@ -22,16 +24,23 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi public ISemanticVersion GameVersion { get; set; } /// The OS on which the player plays. - public Platform? Platform { get; set; } + public Platform Platform { get; set; } /********* ** Public methods *********/ /// Construct an empty instance. + [Obsolete("This constructor only exists to support ASP.NET model binding, and shouldn't be used directly.")] + [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used by ASP.NET model binding.")] public ModSearchModel() { - // needed for JSON deserializing + // ASP.NET Web API needs a public empty constructor for top-level request models, and + // it'll fail if the other constructor is marked with [JsonConstructor]. Apparently + // it's fine with non-empty constructors in nested models like ModSearchEntryModel. + this.Mods = Array.Empty(); + this.ApiVersion = null!; + this.GameVersion = null!; } /// Construct an instance. diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs index 2fb6ed20..ef1904d4 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs @@ -1,27 +1,24 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; -using Newtonsoft.Json; +using System.Threading.Tasks; +using Pathoschild.Http.Client; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi { /// Provides methods for interacting with the SMAPI web API. - public class WebApiClient + public class WebApiClient : IDisposable { /********* ** Fields *********/ - /// The base URL for the web API. - private readonly Uri BaseUrl; - /// The API version number. private readonly ISemanticVersion Version; - /// The JSON serializer settings to use. - private readonly JsonSerializerSettings JsonSettings = new JsonHelper().JsonSettings; + /// The underlying HTTP client. + private readonly IClient Client; /********* @@ -32,8 +29,11 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi /// The web API version. public WebApiClient(string baseUrl, ISemanticVersion version) { - this.BaseUrl = new Uri(baseUrl); this.Version = version; + this.Client = new FluentClient(baseUrl) + .SetUserAgent($"SMAPI/{version}"); + + this.Client.Formatters.JsonFormatter.SerializerSettings = JsonHelper.CreateDefaultSettings(); } /// Get metadata about a set of mods from the web API. @@ -42,35 +42,22 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi /// The Stardew Valley version installed by the player. /// The OS on which the player plays. /// Whether to include extended metadata for each mod. - public IDictionary GetModInfo(ModSearchEntryModel[] mods, ISemanticVersion apiVersion, ISemanticVersion gameVersion, Platform platform, bool includeExtendedMetadata = false) + public async Task> GetModInfoAsync(ModSearchEntryModel[] mods, ISemanticVersion apiVersion, ISemanticVersion gameVersion, Platform platform, bool includeExtendedMetadata = false) { - return this.Post( - $"v{this.Version}/mods", - new ModSearchModel(mods, apiVersion, gameVersion, platform, includeExtendedMetadata) - ).ToDictionary(p => p.ID); + ModEntryModel[] result = await this.Client + .PostAsync( + $"v{this.Version}/mods", + new ModSearchModel(mods, apiVersion, gameVersion, platform, includeExtendedMetadata) + ) + .As(); + + return result.ToDictionary(p => p.ID); } - - /********* - ** Private methods - *********/ - /// Fetch the response from the backend API. - /// The body content type. - /// The expected response type. - /// The request URL, optionally excluding the base URL. - /// The body content to post. - private TResult Post(string url, TBody content) + /// + public void Dispose() { - // note: avoid HttpClient for Mac compatibility - using WebClient client = new WebClient(); - - Uri fullUrl = new Uri(this.BaseUrl, url); - string data = JsonConvert.SerializeObject(content); - - client.Headers["Content-Type"] = "application/json"; - client.Headers["User-Agent"] = $"SMAPI/{this.Version}"; - string response = client.UploadString(fullUrl, data); - return JsonConvert.DeserializeObject(response, this.JsonSettings); + this.Client.Dispose(); } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/ChangeDescriptor.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/ChangeDescriptor.cs new file mode 100644 index 00000000..a2497dea --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/ChangeDescriptor.cs @@ -0,0 +1,202 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; +using System.Linq; + +namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki +{ + /// A set of changes which can be applied to a mod data field. + public class ChangeDescriptor + { + /********* + ** Accessors + *********/ + /// The values to add to the field. + public ISet Add { get; } + + /// The values to remove from the field. + public ISet Remove { get; } + + /// The values to replace in the field, if matched. + public IReadOnlyDictionary Replace { get; } + + /// Whether the change descriptor would make any changes. + public bool HasChanges { get; } + + /// Format a raw value into a normalized form. + public Func FormatValue { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The values to add to the field. + /// The values to remove from the field. + /// The values to replace in the field, if matched. + /// Format a raw value into a normalized form. + public ChangeDescriptor(ISet add, ISet remove, IReadOnlyDictionary replace, Func formatValue) + { + this.Add = add; + this.Remove = remove; + this.Replace = replace; + this.HasChanges = add.Any() || remove.Any() || replace.Any(); + this.FormatValue = formatValue; + } + + /// Apply the change descriptors to a comma-delimited field. + /// The raw field text. + /// Returns the modified field. +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("rawField")] +#endif + public string? ApplyToCopy(string? rawField) + { + // get list + List values = !string.IsNullOrWhiteSpace(rawField) + ? new List( + from field in rawField.Split(',') + let value = field.Trim() + where value.Length > 0 + select value + ) + : new List(); + + // apply changes + this.Apply(values); + + // format + if (rawField == null && !values.Any()) + return null; + return string.Join(", ", values); + } + + /// Apply the change descriptors to the given field values. + /// The field values. + /// Returns the modified field values. + public void Apply(List values) + { + // replace/remove values + if (this.Replace.Any() || this.Remove.Any()) + { + for (int i = values.Count - 1; i >= 0; i--) + { + string value = this.FormatValue(values[i].Trim()); + + if (this.Remove.Contains(value)) + values.RemoveAt(i); + + else if (this.Replace.TryGetValue(value, out string? newValue)) + values[i] = newValue; + } + } + + // add values + if (this.Add.Any()) + { + HashSet curValues = new HashSet(values.Select(p => p.Trim()), StringComparer.OrdinalIgnoreCase); + foreach (string add in this.Add) + { + if (!curValues.Contains(add)) + { + values.Add(add); + curValues.Add(add); + } + } + } + } + + /// + public override string ToString() + { + if (!this.HasChanges) + return string.Empty; + + List descriptors = new List(this.Add.Count + this.Remove.Count + this.Replace.Count); + foreach (string add in this.Add) + descriptors.Add($"+{add}"); + foreach (string remove in this.Remove) + descriptors.Add($"-{remove}"); + foreach (var pair in this.Replace) + descriptors.Add($"{pair.Key} → {pair.Value}"); + + return string.Join(", ", descriptors); + } + + /// Parse a raw change descriptor string into a model. + /// The raw change descriptor. + /// The human-readable error message describing any invalid values that were ignored. + /// Format a raw value into a normalized form if needed. + public static ChangeDescriptor Parse(string? descriptor, out string[] errors, Func? formatValue = null) + { + // init + formatValue ??= p => p; + var add = new HashSet(StringComparer.OrdinalIgnoreCase); + var remove = new HashSet(StringComparer.OrdinalIgnoreCase); + var replace = new Dictionary(StringComparer.OrdinalIgnoreCase); + + // parse each change in the descriptor + if (!string.IsNullOrWhiteSpace(descriptor)) + { + List rawErrors = new List(); + foreach (string rawEntry in descriptor.Split(',')) + { + // normalize entry + string entry = rawEntry.Trim(); + if (entry == string.Empty) + continue; + + // parse as replace (old value → new value) + if (entry.Contains('→')) + { + string[] parts = entry.Split(new[] { '→' }, 2); + string oldValue = formatValue(parts[0].Trim()); + string newValue = formatValue(parts[1].Trim()); + + if (oldValue == string.Empty) + { + rawErrors.Add($"Failed parsing '{rawEntry}': can't map from a blank old value. Use the '+value' format to add a value."); + continue; + } + + if (newValue == string.Empty) + { + rawErrors.Add($"Failed parsing '{rawEntry}': can't map to a blank value. Use the '-value' format to remove a value."); + continue; + } + + replace[oldValue] = newValue; + } + + // else as remove + else if (entry.StartsWith("-")) + { + entry = formatValue(entry.Substring(1).Trim()); + remove.Add(entry); + } + + // else as add + else + { + if (entry.StartsWith("+")) + entry = formatValue(entry.Substring(1).Trim()); + add.Add(entry); + } + } + + errors = rawErrors.ToArray(); + } + else + errors = Array.Empty(); + + // build model + return new ChangeDescriptor( + add: add, + remove: remove, + replace: new ReadOnlyDictionary(replace), + formatValue: formatValue + ); + } + } +} diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiClient.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiClient.cs index 95acedf7..0d0a174e 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiClient.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiClient.cs @@ -7,6 +7,7 @@ using System.Net; using System.Threading.Tasks; using HtmlAgilityPack; using Pathoschild.Http.Client; +using StardewModdingAPI.Toolkit.Framework.UpdateData; namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki { @@ -51,30 +52,49 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki doc.LoadHtml(html); // fetch game versions - string stableVersion = doc.DocumentNode.SelectSingleNode("div[@class='game-stable-version']")?.InnerText; - string betaVersion = doc.DocumentNode.SelectSingleNode("div[@class='game-beta-version']")?.InnerText; + string? stableVersion = doc.DocumentNode.SelectSingleNode("//div[@class='game-stable-version']")?.InnerText; + string? betaVersion = doc.DocumentNode.SelectSingleNode("//div[@class='game-beta-version']")?.InnerText; if (betaVersion == stableVersion) betaVersion = null; - // find mod entries - HtmlNodeCollection modNodes = doc.DocumentNode.SelectNodes("table[@id='mod-list']//tr[@class='mod']"); - if (modNodes == null) - throw new InvalidOperationException("Can't parse wiki compatibility list, no mods found."); - - // parse - WikiModEntry[] mods = this.ParseEntries(modNodes).ToArray(); - return new WikiModList + // parse mod data overrides + Dictionary overrides = new Dictionary(StringComparer.OrdinalIgnoreCase); { - StableVersion = stableVersion, - BetaVersion = betaVersion, - Mods = mods - }; + HtmlNodeCollection modNodes = doc.DocumentNode.SelectNodes("//table[@id='mod-overrides-list']//tr[@class='mod']"); + if (modNodes == null) + throw new InvalidOperationException("Can't parse wiki compatibility list, no mod data overrides section found."); + + foreach (WikiDataOverrideEntry entry in this.ParseOverrideEntries(modNodes)) + { + if (entry.Ids.Any() != true || !entry.HasChanges) + continue; + + foreach (string id in entry.Ids) + overrides[id] = entry; + } + } + + // parse mod entries + WikiModEntry[] mods; + { + HtmlNodeCollection modNodes = doc.DocumentNode.SelectNodes("//table[@id='mod-list']//tr[@class='mod']"); + if (modNodes == null) + throw new InvalidOperationException("Can't parse wiki compatibility list, no mods found."); + mods = this.ParseModEntries(modNodes, overrides).ToArray(); + } + + // build model + return new WikiModList( + stableVersion: stableVersion, + betaVersion: betaVersion, + mods: mods + ); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } @@ -83,7 +103,8 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki *********/ /// Parse valid mod compatibility entries. /// The HTML compatibility entries. - private IEnumerable ParseEntries(IEnumerable nodes) + /// The mod data overrides to apply, if any. + private IEnumerable ParseModEntries(IEnumerable nodes, IDictionary overridesById) { foreach (HtmlNode node in nodes) { @@ -95,70 +116,90 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki int? nexusID = this.GetAttributeAsNullableInt(node, "data-nexus-id"); int? chucklefishID = this.GetAttributeAsNullableInt(node, "data-cf-id"); int? curseForgeID = this.GetAttributeAsNullableInt(node, "data-curseforge-id"); - string curseForgeKey = this.GetAttribute(node, "data-curseforge-key"); + string? curseForgeKey = this.GetAttribute(node, "data-curseforge-key"); int? modDropID = this.GetAttributeAsNullableInt(node, "data-moddrop-id"); - string githubRepo = this.GetAttribute(node, "data-github"); - string customSourceUrl = this.GetAttribute(node, "data-custom-source"); - string customUrl = this.GetAttribute(node, "data-url"); - string anchor = this.GetAttribute(node, "id"); - string contentPackFor = this.GetAttribute(node, "data-content-pack-for"); - string devNote = this.GetAttribute(node, "data-dev-note"); - string pullRequestUrl = this.GetAttribute(node, "data-pr"); - IDictionary mapLocalVersions = this.GetAttributeAsVersionMapping(node, "data-map-local-versions"); - IDictionary mapRemoteVersions = this.GetAttributeAsVersionMapping(node, "data-map-remote-versions"); - string[] changeUpdateKeys = this.GetAttributeAsCsv(node, "data-change-update-keys"); + string? githubRepo = this.GetAttribute(node, "data-github"); + string? customSourceUrl = this.GetAttribute(node, "data-custom-source"); + string? customUrl = this.GetAttribute(node, "data-url"); + string? anchor = this.GetAttribute(node, "id"); + string? contentPackFor = this.GetAttribute(node, "data-content-pack-for"); + string? devNote = this.GetAttribute(node, "data-dev-note"); + string? pullRequestUrl = this.GetAttribute(node, "data-pr"); // parse stable compatibility - WikiCompatibilityInfo compatibility = new WikiCompatibilityInfo - { - Status = this.GetAttributeAsEnum(node, "data-status") ?? WikiCompatibilityStatus.Ok, - BrokeIn = this.GetAttribute(node, "data-broke-in"), - UnofficialVersion = this.GetAttributeAsSemanticVersion(node, "data-unofficial-version"), - UnofficialUrl = this.GetAttribute(node, "data-unofficial-url"), - Summary = this.GetInnerHtml(node, "mod-summary")?.Trim() - }; + WikiCompatibilityInfo compatibility = new( + status: this.GetAttributeAsEnum(node, "data-status") ?? WikiCompatibilityStatus.Ok, + brokeIn: this.GetAttribute(node, "data-broke-in"), + unofficialVersion: this.GetAttributeAsSemanticVersion(node, "data-unofficial-version"), + unofficialUrl: this.GetAttribute(node, "data-unofficial-url"), + summary: this.GetInnerHtml(node, "mod-summary")?.Trim() + ); // parse beta compatibility - WikiCompatibilityInfo betaCompatibility = null; + WikiCompatibilityInfo? betaCompatibility = null; { WikiCompatibilityStatus? betaStatus = this.GetAttributeAsEnum(node, "data-beta-status"); if (betaStatus.HasValue) { - betaCompatibility = new WikiCompatibilityInfo - { - Status = betaStatus.Value, - BrokeIn = this.GetAttribute(node, "data-beta-broke-in"), - UnofficialVersion = this.GetAttributeAsSemanticVersion(node, "data-beta-unofficial-version"), - UnofficialUrl = this.GetAttribute(node, "data-beta-unofficial-url"), - Summary = this.GetInnerHtml(node, "mod-beta-summary") - }; + betaCompatibility = new WikiCompatibilityInfo( + status: betaStatus.Value, + brokeIn: this.GetAttribute(node, "data-beta-broke-in"), + unofficialVersion: this.GetAttributeAsSemanticVersion(node, "data-beta-unofficial-version"), + unofficialUrl: this.GetAttribute(node, "data-beta-unofficial-url"), + summary: this.GetInnerHtml(node, "mod-beta-summary") + ); } } + // find data overrides + WikiDataOverrideEntry? overrides = ids + .Select(id => overridesById.TryGetValue(id, out overrides) ? overrides : null) + .FirstOrDefault(p => p != null); + // yield model - yield return new WikiModEntry + yield return new WikiModEntry( + id: ids, + name: names, + author: authors, + nexusId: nexusID, + chucklefishId: chucklefishID, + curseForgeId: curseForgeID, + curseForgeKey: curseForgeKey, + modDropId: modDropID, + githubRepo: githubRepo, + customSourceUrl: customSourceUrl, + customUrl: customUrl, + contentPackFor: contentPackFor, + compatibility: compatibility, + betaCompatibility: betaCompatibility, + warnings: warnings, + pullRequestUrl: pullRequestUrl, + devNote: devNote, + overrides: overrides, + anchor: anchor + ); + } + } + + /// Parse valid mod data override entries. + /// The HTML mod data override entries. + private IEnumerable ParseOverrideEntries(IEnumerable nodes) + { + foreach (HtmlNode node in nodes) + { + yield return new WikiDataOverrideEntry { - ID = ids, - Name = names, - Author = authors, - NexusID = nexusID, - ChucklefishID = chucklefishID, - CurseForgeID = curseForgeID, - CurseForgeKey = curseForgeKey, - ModDropID = modDropID, - GitHubRepo = githubRepo, - CustomSourceUrl = customSourceUrl, - CustomUrl = customUrl, - ContentPackFor = contentPackFor, - Compatibility = compatibility, - BetaCompatibility = betaCompatibility, - Warnings = warnings, - PullRequestUrl = pullRequestUrl, - DevNote = devNote, - ChangeUpdateKeys = changeUpdateKeys, - MapLocalVersions = mapLocalVersions, - MapRemoteVersions = mapRemoteVersions, - Anchor = anchor + Ids = this.GetAttributeAsCsv(node, "data-id"), + ChangeLocalVersions = this.GetAttributeAsChangeDescriptor(node, "data-local-version", + raw => SemanticVersion.TryParse(raw, out ISemanticVersion? version) ? version.ToString() : raw + ), + ChangeRemoteVersions = this.GetAttributeAsChangeDescriptor(node, "data-remote-version", + raw => SemanticVersion.TryParse(raw, out ISemanticVersion? version) ? version.ToString() : raw + ), + + ChangeUpdateKeys = this.GetAttributeAsChangeDescriptor(node, "data-update-keys", + raw => UpdateKey.TryParse(raw, out UpdateKey key) ? key.ToString() : raw + ) }; } } @@ -166,7 +207,7 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki /// Get an attribute value. /// The element whose attributes to read. /// The attribute name. - private string GetAttribute(HtmlNode element, string name) + private string? GetAttribute(HtmlNode element, string name) { string value = element.GetAttributeValue(name, null); if (string.IsNullOrWhiteSpace(value)) @@ -175,15 +216,27 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki return WebUtility.HtmlDecode(value); } + /// Get an attribute value and parse it as a change descriptor. + /// The element whose attributes to read. + /// The attribute name. + /// Format an raw entry value when applying changes. + private ChangeDescriptor? GetAttributeAsChangeDescriptor(HtmlNode element, string name, Func formatValue) + { + string? raw = this.GetAttribute(element, name); + return raw != null + ? ChangeDescriptor.Parse(raw, out _, formatValue) + : null; + } + /// Get an attribute value and parse it as a comma-delimited list of strings. /// The element whose attributes to read. /// The attribute name. private string[] GetAttributeAsCsv(HtmlNode element, string name) { - string raw = this.GetAttribute(element, name); + string? raw = this.GetAttribute(element, name); return !string.IsNullOrWhiteSpace(raw) ? raw.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray() - : new string[0]; + : Array.Empty(); } /// Get an attribute value and parse it as an enum value. @@ -192,7 +245,7 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki /// The attribute name. private TEnum? GetAttributeAsEnum(HtmlNode element, string name) where TEnum : struct { - string raw = this.GetAttribute(element, name); + string? raw = this.GetAttribute(element, name); if (raw == null) return null; if (!Enum.TryParse(raw, true, out TEnum value) && Enum.IsDefined(typeof(TEnum), value)) @@ -203,10 +256,10 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki /// Get an attribute value and parse it as a semantic version. /// The element whose attributes to read. /// The attribute name. - private ISemanticVersion GetAttributeAsSemanticVersion(HtmlNode element, string name) + private ISemanticVersion? GetAttributeAsSemanticVersion(HtmlNode element, string name) { - string raw = this.GetAttribute(element, name); - return SemanticVersion.TryParse(raw, out ISemanticVersion version) + string? raw = this.GetAttribute(element, name); + return SemanticVersion.TryParse(raw, out ISemanticVersion? version) ? version : null; } @@ -216,59 +269,54 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki /// The attribute name. private int? GetAttributeAsNullableInt(HtmlNode element, string name) { - string raw = this.GetAttribute(element, name); + string? raw = this.GetAttribute(element, name); if (raw != null && int.TryParse(raw, out int value)) return value; return null; } - /// Get an attribute value and parse it as a version mapping. - /// The element whose attributes to read. - /// The attribute name. - private IDictionary GetAttributeAsVersionMapping(HtmlNode element, string name) - { - // get raw value - string raw = this.GetAttribute(element, name); - if (raw?.Contains("→") != true) - return null; - - // parse - // Specified on the wiki in the form "remote version → mapped version; another remote version → mapped version" - IDictionary map = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (string pair in raw.Split(';')) - { - string[] versions = pair.Split('→'); - if (versions.Length == 2 && !string.IsNullOrWhiteSpace(versions[0]) && !string.IsNullOrWhiteSpace(versions[1])) - map[versions[0].Trim()] = versions[1].Trim(); - } - return map; - } - /// Get the text of an element with the given class name. /// The metadata container. /// The field name. - private string GetInnerHtml(HtmlNode container, string className) + private string? GetInnerHtml(HtmlNode container, string className) { return container.Descendants().FirstOrDefault(p => p.HasClass(className))?.InnerHtml; } /// The response model for the MediaWiki parse API. - [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")] - [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")] + [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local", Justification = "Used via JSON deserialization.")] + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local", Justification = "Used via JSON deserialization.")] private class ResponseModel { + /********* + ** Accessors + *********/ /// The parse API results. - public ResponseParseModel Parse { get; set; } + public ResponseParseModel Parse { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The parse API results. + public ResponseModel(ResponseParseModel parse) + { + this.Parse = parse; + } } /// The inner response model for the MediaWiki parse API. - [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")] - [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] - [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")] + [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local", Justification = "Used via JSON deserialization.")] + [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local", Justification = "Used via JSON deserialization.")] + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local", Justification = "Used via JSON deserialization.")] private class ResponseParseModel { + /********* + ** Accessors + *********/ /// The parsed text. - public IDictionary Text { get; set; } + public IDictionary Text { get; } = new Dictionary(); } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiCompatibilityInfo.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiCompatibilityInfo.cs index 204acd2b..71c90d0c 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiCompatibilityInfo.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiCompatibilityInfo.cs @@ -7,18 +7,37 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki ** Accessors *********/ /// The compatibility status. - public WikiCompatibilityStatus Status { get; set; } + public WikiCompatibilityStatus Status { get; } /// The human-readable summary of the compatibility status or workaround, without HTML formatting. - public string Summary { get; set; } + public string? Summary { get; } - /// The game or SMAPI version which broke this mod (if applicable). - public string BrokeIn { get; set; } + /// The game or SMAPI version which broke this mod, if applicable. + public string? BrokeIn { get; } /// The version of the latest unofficial update, if applicable. - public ISemanticVersion UnofficialVersion { get; set; } + public ISemanticVersion? UnofficialVersion { get; } /// The URL to the latest unofficial update, if applicable. - public string UnofficialUrl { get; set; } + public string? UnofficialUrl { get; } + + + /********* + ** Accessors + *********/ + /// Construct an instance. + /// The compatibility status. + /// The human-readable summary of the compatibility status or workaround, without HTML formatting. + /// The game or SMAPI version which broke this mod, if applicable. + /// The version of the latest unofficial update, if applicable. + /// The URL to the latest unofficial update, if applicable. + public WikiCompatibilityInfo(WikiCompatibilityStatus status, string? summary, string? brokeIn, ISemanticVersion? unofficialVersion, string? unofficialUrl) + { + this.Status = status; + this.Summary = summary; + this.BrokeIn = brokeIn; + this.UnofficialVersion = unofficialVersion; + this.UnofficialUrl = unofficialUrl; + } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiDataOverrideEntry.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiDataOverrideEntry.cs new file mode 100644 index 00000000..a6f5a88f --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiDataOverrideEntry.cs @@ -0,0 +1,29 @@ +using System; + +namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki +{ + /// The data overrides to apply to matching mods. + public class WikiDataOverrideEntry + { + /********* + ** Accessors + *********/ + /// The unique mod IDs for the mods to override. + public string[] Ids { get; set; } = Array.Empty(); + + /// Maps local versions to a semantic version for update checks. + public ChangeDescriptor? ChangeLocalVersions { get; set; } + + /// Maps remote versions to a semantic version for update checks. + public ChangeDescriptor? ChangeRemoteVersions { get; set; } + + /// Update keys to add (optionally prefixed by '+'), remove (prefixed by '-'), or replace. + public ChangeDescriptor? ChangeUpdateKeys { get; set; } + + /// Whether the entry has any changes. + public bool HasChanges => + this.ChangeLocalVersions?.HasChanges == true + || this.ChangeRemoteVersions?.HasChanges == true + || this.ChangeUpdateKeys?.HasChanges == true; + } +} diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModEntry.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModEntry.cs index 21466c6a..fc50125f 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModEntry.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModEntry.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki { @@ -9,70 +8,114 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki /********* ** Accessors *********/ - /// The mod's unique ID. If the mod has alternate/old IDs, they're listed in latest to newest order. - public string[] ID { get; set; } + /// The mod's unique ID. If the mod has alternate/old IDs, they're listed in latest to oldest order. + public string[] ID { get; } /// The mod's display name. If the mod has multiple names, the first one is the most canonical name. - public string[] Name { get; set; } + public string[] Name { get; } - /// The mod's author name. If the author has multiple names, the first one is the most canonical name. - public string[] Author { get; set; } + /// The mod's author name. If the author has multiple names, the first one is the most canonical name. + public string[] Author { get; } /// The mod ID on Nexus. - public int? NexusID { get; set; } + public int? NexusID { get; } /// The mod ID in the Chucklefish mod repo. - public int? ChucklefishID { get; set; } + public int? ChucklefishID { get; } /// The mod ID in the CurseForge mod repo. - public int? CurseForgeID { get; set; } + public int? CurseForgeID { get; } /// The mod key in the CurseForge mod repo (used in mod page URLs). - public string CurseForgeKey { get; set; } + public string? CurseForgeKey { get; } /// The mod ID in the ModDrop mod repo. - public int? ModDropID { get; set; } + public int? ModDropID { get; } /// The GitHub repository in the form 'owner/repo'. - public string GitHubRepo { get; set; } + public string? GitHubRepo { get; } /// The URL to a non-GitHub source repo. - public string CustomSourceUrl { get; set; } + public string? CustomSourceUrl { get; } /// The custom mod page URL (if applicable). - public string CustomUrl { get; set; } + public string? CustomUrl { get; } /// The name of the mod which loads this content pack, if applicable. - public string ContentPackFor { get; set; } + public string? ContentPackFor { get; } /// The mod's compatibility with the latest stable version of the game. - public WikiCompatibilityInfo Compatibility { get; set; } + public WikiCompatibilityInfo Compatibility { get; } /// The mod's compatibility with the latest beta version of the game (if any). - public WikiCompatibilityInfo BetaCompatibility { get; set; } + public WikiCompatibilityInfo? BetaCompatibility { get; } /// Whether a Stardew Valley or SMAPI beta which affects mod compatibility is in progress. If this is true, should be used for beta versions of SMAPI instead of . +#if NET5_0_OR_GREATER + [MemberNotNullWhen(true, nameof(WikiModEntry.BetaCompatibility))] +#endif public bool HasBetaInfo => this.BetaCompatibility != null; /// The human-readable warnings for players about this mod. - public string[] Warnings { get; set; } + public string[] Warnings { get; } /// The URL of the pull request which submits changes for an unofficial update to the author, if any. - public string PullRequestUrl { get; set; } + public string? PullRequestUrl { get; } - /// Special notes intended for developers who maintain unofficial updates or submit pull requests. - public string DevNote { get; set; } + /// Special notes intended for developers who maintain unofficial updates or submit pull requests. + public string? DevNote { get; } - /// Update keys to add (optionally prefixed by '+') or remove (prefixed by '-'). - public string[] ChangeUpdateKeys { get; set; } - - /// Maps local versions to a semantic version for update checks. - public IDictionary MapLocalVersions { get; set; } - - /// Maps remote versions to a semantic version for update checks. - public IDictionary MapRemoteVersions { get; set; } + /// The data overrides to apply to the mod's manifest or remote mod page data, if any. + public WikiDataOverrideEntry? Overrides { get; } /// The link anchor for the mod entry in the wiki compatibility list. - public string Anchor { get; set; } + public string? Anchor { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod's unique ID. If the mod has alternate/old IDs, they're listed in latest to oldest order. + /// The mod's display name. If the mod has multiple names, the first one is the most canonical name. + /// The mod's author name. If the author has multiple names, the first one is the most canonical name. + /// The mod ID on Nexus. + /// The mod ID in the Chucklefish mod repo. + /// The mod ID in the CurseForge mod repo. + /// The mod ID in the CurseForge mod repo. + /// The mod ID in the ModDrop mod repo. + /// The GitHub repository in the form 'owner/repo'. + /// The URL to a non-GitHub source repo. + /// The custom mod page URL (if applicable). + /// The name of the mod which loads this content pack, if applicable. + /// The mod's compatibility with the latest stable version of the game. + /// The mod's compatibility with the latest beta version of the game (if any). + /// The human-readable warnings for players about this mod. + /// The URL of the pull request which submits changes for an unofficial update to the author, if any. + /// Special notes intended for developers who maintain unofficial updates or submit pull requests. + /// The data overrides to apply to the mod's manifest or remote mod page data, if any. + /// The link anchor for the mod entry in the wiki compatibility list. + public WikiModEntry(string[] id, string[] name, string[] author, int? nexusId, int? chucklefishId, int? curseForgeId, string? curseForgeKey, int? modDropId, string? githubRepo, string? customSourceUrl, string? customUrl, string? contentPackFor, WikiCompatibilityInfo compatibility, WikiCompatibilityInfo? betaCompatibility, string[] warnings, string? pullRequestUrl, string? devNote, WikiDataOverrideEntry? overrides, string? anchor) + { + this.ID = id; + this.Name = name; + this.Author = author; + this.NexusID = nexusId; + this.ChucklefishID = chucklefishId; + this.CurseForgeID = curseForgeId; + this.CurseForgeKey = curseForgeKey; + this.ModDropID = modDropId; + this.GitHubRepo = githubRepo; + this.CustomSourceUrl = customSourceUrl; + this.CustomUrl = customUrl; + this.ContentPackFor = contentPackFor; + this.Compatibility = compatibility; + this.BetaCompatibility = betaCompatibility; + this.Warnings = warnings; + this.PullRequestUrl = pullRequestUrl; + this.DevNote = devNote; + this.Overrides = overrides; + this.Anchor = anchor; + } } } diff --git a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModList.cs b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModList.cs index 0d614f28..24548078 100644 --- a/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModList.cs +++ b/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiModList.cs @@ -7,12 +7,27 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki ** Accessors *********/ /// The stable game version. - public string StableVersion { get; set; } + public string? StableVersion { get; } /// The beta game version (if any). - public string BetaVersion { get; set; } + public string? BetaVersion { get; } /// The mods on the wiki. - public WikiModEntry[] Mods { get; set; } + public WikiModEntry[] Mods { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The stable game version. + /// The beta game version (if any). + /// The mods on the wiki. + public WikiModList(string? stableVersion, string? betaVersion, WikiModEntry[] mods) + { + this.StableVersion = stableVersion; + this.BetaVersion = betaVersion; + this.Mods = mods; + } } } diff --git a/src/SMAPI.Toolkit/Framework/Constants.cs b/src/SMAPI.Toolkit/Framework/Constants.cs new file mode 100644 index 00000000..55f26582 --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/Constants.cs @@ -0,0 +1,9 @@ +namespace StardewModdingAPI.Toolkit.Framework +{ + /// Contains the SMAPI installer's constants and assumptions. + internal static class Constants + { + /// The name of the game's main DLL, used to detect game folders. + public const string GameDllName = "Stardew Valley.dll"; + } +} diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs b/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs new file mode 100644 index 00000000..d18af59b --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/GameScanning/GameFolderType.cs @@ -0,0 +1,21 @@ +namespace StardewModdingAPI.Toolkit.Framework.GameScanning +{ + /// The detected validity for a Stardew Valley game folder based on file structure heuristics. + public enum GameFolderType + { + /// The folder seems to contain a valid Stardew Valley 1.5.5+ install. + Valid, + + /// The folder doesn't contain Stardew Valley. + NoGameFound, + + /// The folder contains Stardew Valley 1.5.4 or earlier. This version uses XNA Framework and 32-bit .NET Framework 4.5.2 on Windows and Mono on Linux/macOS, and isn't compatible with current versions of SMAPI. + Legacy154OrEarlier, + + /// The folder contains Stardew Valley from the game's legacy compatibility branch, which backports newer changes to the format. + LegacyCompatibilityBranch, + + /// The folder seems to contain Stardew Valley files, but they failed to load for unknown reasons (e.g. corrupted executable). + InvalidUnknown + } +} diff --git a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs index 825988a5..88142805 100644 --- a/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs +++ b/src/SMAPI.Toolkit/Framework/GameScanning/GameScanner.cs @@ -1,61 +1,135 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Xml.Linq; using System.Xml.XPath; using StardewModdingAPI.Toolkit.Utilities; +using System.Reflection; #if SMAPI_FOR_WINDOWS using Microsoft.Win32; +using VdfParser; #endif namespace StardewModdingAPI.Toolkit.Framework.GameScanning { /// Finds installed game folders. + [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "These are valid game install paths.")] public class GameScanner { + /********* + ** Fields + *********/ + /// The current OS. + private readonly Platform Platform; + + /// The Steam app ID for Stardew Valley. + private const string SteamAppId = "413150"; + + /********* ** Public methods *********/ + /// Construct an instance. + public GameScanner() + { + this.Platform = EnvironmentUtility.DetectPlatform(); + } + /// Find all valid Stardew Valley install folders. /// This checks default game locations, and on Windows checks the Windows registry for GOG/Steam install data. A folder is considered 'valid' if it contains the Stardew Valley executable for the current OS. public IEnumerable Scan() { - // get OS info - Platform platform = EnvironmentUtility.DetectPlatform(); - string executableFilename = EnvironmentUtility.GetExecutableName(platform); - // get install paths IEnumerable paths = this - .GetCustomInstallPaths(platform) - .Concat(this.GetDefaultInstallPaths(platform)) - .Select(PathUtilities.NormalizePath) + .GetCustomInstallPaths() + .Concat(this.GetDefaultInstallPaths()) + .Select(path => PathUtilities.NormalizePath(path)) .Distinct(StringComparer.OrdinalIgnoreCase); // yield valid folders foreach (string path in paths) { - DirectoryInfo folder = new DirectoryInfo(path); - if (folder.Exists && folder.EnumerateFiles(executableFilename).Any()) + DirectoryInfo folder = new(path); + if (this.LooksLikeGameFolder(folder)) yield return folder; } } + /// Get whether a folder seems to contain the game. + /// The folder to check. + public bool LooksLikeGameFolder(DirectoryInfo dir) + { + return this.GetGameFolderType(dir) == GameFolderType.Valid; + } + + /// Detect the validity of a game folder based on file structure heuristics. + /// The folder to check. + public GameFolderType GetGameFolderType(DirectoryInfo dir) + { + // no such folder + if (!dir.Exists) + return GameFolderType.NoGameFound; + + // apparently valid + if (dir.EnumerateFiles("Stardew Valley.dll").Any()) + return GameFolderType.Valid; + + // doesn't contain any version of Stardew Valley + FileInfo executable = new(Path.Combine(dir.FullName, "Stardew Valley.exe")); + if (!executable.Exists) + executable = new(Path.Combine(dir.FullName, "StardewValley.exe")); // pre-1.5.5 Linux/macOS executable + if (!executable.Exists) + return GameFolderType.NoGameFound; + + // get assembly version + Version? version; + try + { + version = AssemblyName.GetAssemblyName(executable.FullName).Version; + if (version == null) + return GameFolderType.InvalidUnknown; + } + catch + { + // The executable exists but it doesn't seem to be a valid assembly. This would + // happen with Stardew Valley 1.5.5+, but that should have been flagged as a valid + // folder before this point. + return GameFolderType.InvalidUnknown; + } + + // ignore Stardew Valley 1.5.5+ at this point + if (version.Major == 1 && version.Minor == 3 && version.Build == 37) + return GameFolderType.InvalidUnknown; + + // incompatible version + if (version.Major == 1 && version.Minor < 4) + { + // Stardew Valley 1.5.4 and earlier have assembly versions <= 1.3.7853.31734 + if (version.Minor < 3 || version.Build <= 7853) + return GameFolderType.Legacy154OrEarlier; + + // Stardew Valley 1.5.5+ legacy compatibility branch + return GameFolderType.LegacyCompatibilityBranch; + } + + return GameFolderType.InvalidUnknown; + } /********* ** Private methods *********/ /// The default file paths where Stardew Valley can be installed. - /// The target platform. - /// Derived from the crossplatform mod config: https://github.com/Pathoschild/Stardew.ModBuildConfig. - private IEnumerable GetDefaultInstallPaths(Platform platform) + /// Derived from the crossplatform mod config. + private IEnumerable GetDefaultInstallPaths() { - switch (platform) + switch (this.Platform) { case Platform.Linux: case Platform.Mac: { - string home = Environment.GetEnvironmentVariable("HOME"); + string home = Environment.GetEnvironmentVariable("HOME")!; // Linux yield return $"{home}/GOG Games/Stardew Valley/game"; @@ -63,7 +137,7 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning ? $"{home}/.steam/steam/steamapps/common/Stardew Valley" : $"{home}/.local/share/Steam/steamapps/common/Stardew Valley"; - // Mac + // macOS yield return "/Applications/Stardew Valley.app/Contents/MacOS"; yield return $"{home}/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS"; } @@ -71,52 +145,69 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning case Platform.Windows: { - // Windows - foreach (string programFiles in new[] { @"C:\Program Files", @"C:\Program Files (x86)" }) - { - yield return $@"{programFiles}\GalaxyClient\Games\Stardew Valley"; - yield return $@"{programFiles}\GOG Galaxy\Games\Stardew Valley"; - yield return $@"{programFiles}\Steam\steamapps\common\Stardew Valley"; - } - // Windows registry #if SMAPI_FOR_WINDOWS IDictionary registryKeys = new Dictionary { - [@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"] = "InstallLocation", // Steam + [@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App " + GameScanner.SteamAppId] = "InstallLocation", // Steam [@"SOFTWARE\WOW6432Node\GOG.com\Games\1453375253"] = "PATH", // GOG on 64-bit Windows }; foreach (var pair in registryKeys) { - string path = this.GetLocalMachineRegistryValue(pair.Key, pair.Value); + string? path = this.GetLocalMachineRegistryValue(pair.Key, pair.Value); if (!string.IsNullOrWhiteSpace(path)) yield return path; } // via Steam library path - string steampath = this.GetCurrentUserRegistryValue(@"Software\Valve\Steam", "SteamPath"); - if (steampath != null) - yield return Path.Combine(steampath.Replace('/', '\\'), @"steamapps\common\Stardew Valley"); + string? steamPath = this.GetCurrentUserRegistryValue(@"Software\Valve\Steam", "SteamPath"); + if (steamPath != null) + { + // conventional path + yield return Path.Combine(steamPath.Replace('/', '\\'), @"steamapps\common\Stardew Valley"); + + // from Steam's .vdf file + string? path = this.GetPathFromSteamLibrary(steamPath); + if (!string.IsNullOrWhiteSpace(path)) + yield return path; + } #endif + + // default GOG/Steam paths + foreach (string programFiles in new[] { @"C:\Program Files", @"C:\Program Files (x86)" }) + { + yield return $@"{programFiles}\GalaxyClient\Games\Stardew Valley"; + yield return $@"{programFiles}\GOG Galaxy\Games\Stardew Valley"; + yield return $@"{programFiles}\GOG Games\Stardew Valley"; + yield return $@"{programFiles}\Steam\steamapps\common\Stardew Valley"; + } + + // default Xbox app paths + // The Xbox app saves the install path to the registry, but we can't use it + // here since it saves the internal readonly path (like C:\Program Files\WindowsApps\Mutable\) + // instead of the mods-enabled path(like C:\Program Files\ModifiableWindowsApps\Stardew Valley). + // Fortunately we can cheat a bit: players can customize the install drive, but they can't + // change the install path on the drive. + for (char driveLetter = 'C'; driveLetter <= 'H'; driveLetter++) + yield return $@"{driveLetter}:\Program Files\ModifiableWindowsApps\Stardew Valley"; } break; default: - throw new InvalidOperationException($"Unknown platform '{platform}'."); + throw new InvalidOperationException($"Unknown platform '{this.Platform}'."); } } /// Get the custom install path from the stardewvalley.targets file in the home directory, if any. - /// The target platform. - private IEnumerable GetCustomInstallPaths(Platform platform) + private IEnumerable GetCustomInstallPaths() { // get home path - string homePath = Environment.GetEnvironmentVariable(platform == Platform.Windows ? "USERPROFILE" : "HOME"); + string homePath = Environment.GetEnvironmentVariable(this.Platform == Platform.Windows ? "USERPROFILE" : "HOME")!; if (string.IsNullOrWhiteSpace(homePath)) yield break; // get targets file - FileInfo file = new FileInfo(Path.Combine(homePath, "stardewvalley.targets")); + FileInfo file = new(Path.Combine(homePath, "stardewvalley.targets")); if (!file.Exists) yield break; @@ -133,7 +224,7 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning } // get install path - XElement element = root.XPathSelectElement("//*[local-name() = 'GamePath']"); // can't use '//GamePath' due to the default namespace + XElement? element = root.XPathSelectElement("//*[local-name() = 'GamePath']"); // can't use '//GamePath' due to the default namespace if (!string.IsNullOrWhiteSpace(element?.Value)) yield return element.Value.Trim(); } @@ -142,27 +233,78 @@ namespace StardewModdingAPI.Toolkit.Framework.GameScanning /// Get the value of a key in the Windows HKLM registry. /// The full path of the registry key relative to HKLM. /// The name of the value. - private string GetLocalMachineRegistryValue(string key, string name) + private string? GetLocalMachineRegistryValue(string key, string name) { RegistryKey localMachine = Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) : Registry.LocalMachine; - RegistryKey openKey = localMachine.OpenSubKey(key); + RegistryKey? openKey = localMachine.OpenSubKey(key); if (openKey == null) return null; using (openKey) - return (string)openKey.GetValue(name); + return (string?)openKey.GetValue(name); } /// Get the value of a key in the Windows HKCU registry. /// The full path of the registry key relative to HKCU. /// The name of the value. - private string GetCurrentUserRegistryValue(string key, string name) + private string? GetCurrentUserRegistryValue(string key, string name) { - RegistryKey currentuser = Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64) : Registry.CurrentUser; - RegistryKey openKey = currentuser.OpenSubKey(key); + RegistryKey currentUser = Environment.Is64BitOperatingSystem ? RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64) : Registry.CurrentUser; + RegistryKey? openKey = currentUser.OpenSubKey(key); if (openKey == null) return null; using (openKey) - return (string)openKey.GetValue(name); + return (string?)openKey.GetValue(name); + } + + /// Get the game directory path from alternative Steam library locations. + /// The full path to the directory containing steam.exe. + /// The game directory, if found. + private string? GetPathFromSteamLibrary(string? steamPath) + { + try + { + if (steamPath == null) + return null; + + // get .vdf file path + string libraryFoldersPath = Path.Combine(steamPath.Replace('/', '\\'), "steamapps\\libraryfolders.vdf"); + if (!File.Exists(libraryFoldersPath)) + return null; + + // read data + using FileStream fileStream = File.OpenRead(libraryFoldersPath); + VdfDeserializer deserializer = new(); + dynamic libraries = deserializer.Deserialize(fileStream); + if (libraries?.libraryfolders is null) + return null; + + // get path from Stardew Valley app (if any) + foreach (dynamic pair in libraries.libraryfolders) + { + dynamic library = pair.Value; + + foreach (dynamic app in library.apps) + { + string key = app.Key; + if (key == GameScanner.SteamAppId) + { + string path = library.path; + + return Path.Combine(path.Replace("\\\\", "\\"), "steamapps", "common", "Stardew Valley"); + } + } + } + + return null; + } + catch + { + // The file might not be parseable in some cases (e.g. some players have an older Steam version using + // a different format). Ideally we'd log an error to know when it's actually an issue, but the SMAPI + // installer doesn't have a logging mechanism (and third-party code calling the toolkit may not either). + // So for now, just ignore the error and fallback to the other discovery mechanisms. + return null; + } } #endif } diff --git a/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs index b01d8b21..f464f4bb 100644 --- a/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs +++ b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Reflection; #if SMAPI_FOR_WINDOWS using System.Management; #endif @@ -20,7 +21,8 @@ namespace StardewModdingAPI.Toolkit.Framework /// Get the OS name from the system uname command. /// The buffer to fill with the resulting string. [DllImport("libc")] - static extern int uname(IntPtr buffer); + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "This is the actual external command name.")] + private static extern int uname(IntPtr buffer); /********* @@ -48,22 +50,25 @@ namespace StardewModdingAPI.Toolkit.Framework } } - /// Get the human-readable OS name and version. /// The current platform. - [SuppressMessage("ReSharper", "EmptyGeneralCatchClause", Justification = "Error suppressed deliberately to fallback to default behaviour.")] public static string GetFriendlyPlatformName(string platform) { #if SMAPI_FOR_WINDOWS try { - return new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem") + string? result = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem") .Get() .Cast() .Select(entry => entry.GetPropertyValue("Caption").ToString()) .FirstOrDefault(); + + return result ?? "Windows"; + } + catch + { + // fallback to default behavior } - catch { } #endif string name = Environment.OSVersion.ToString(); @@ -74,26 +79,17 @@ namespace StardewModdingAPI.Toolkit.Framework break; case nameof(Platform.Mac): - name = $"MacOS {name}"; + name = $"macOS {name}"; break; } return name; } - /// Get the name of the Stardew Valley executable. - /// The current platform. - public static string GetExecutableName(string platform) + /// Get whether an executable is 64-bit. + /// The absolute path to the assembly file. + public static bool Is64BitAssembly(string path) { - return platform == nameof(Platform.Windows) - ? "Stardew Valley.exe" - : "StardewValley.exe"; - } - - /// Get whether the platform uses Mono. - /// The current platform. - public static bool IsMono(string platform) - { - return platform == nameof(Platform.Linux) || platform == nameof(Platform.Mac); + return AssemblyName.GetAssemblyName(path).ProcessorArchitecture != ProcessorArchitecture.X86; } @@ -107,7 +103,7 @@ namespace StardewModdingAPI.Toolkit.Framework /// private static bool IsRunningAndroid() { - using Process process = new Process + using Process process = new() { StartInfo = { @@ -131,10 +127,10 @@ namespace StardewModdingAPI.Toolkit.Framework } } - /// Detect whether the code is running on Mac. + /// Detect whether the code is running on macOS. /// - /// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects Mac by calling the - /// uname system command and checking the response, which is always 'Darwin' for MacOS. + /// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects macOS by calling the + /// uname system command and checking the response, which is always 'Darwin' for macOS. /// private static bool IsRunningMac() { @@ -144,7 +140,7 @@ namespace StardewModdingAPI.Toolkit.Framework buffer = Marshal.AllocHGlobal(8192); if (LowLevelEnvironmentUtility.uname(buffer) == 0) { - string os = Marshal.PtrToStringAnsi(buffer); + string? os = Marshal.PtrToStringAnsi(buffer); return os == "Darwin"; } return false; diff --git a/src/SMAPI.Toolkit/Framework/ManifestValidator.cs b/src/SMAPI.Toolkit/Framework/ManifestValidator.cs new file mode 100644 index 00000000..461dc325 --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/ManifestValidator.cs @@ -0,0 +1,106 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using StardewModdingAPI.Toolkit.Utilities; + +namespace StardewModdingAPI.Toolkit.Framework +{ + /// Validates manifest fields. + public static class ManifestValidator + { + /// Validate a manifest's fields. + /// The manifest to validate. + /// The error message indicating why validation failed, if applicable. + /// Returns whether all manifest fields validated successfully. + [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "This is the method that ensures those annotations are respected.")] + public static bool TryValidateFields(IManifest manifest, out string error) + { + // + // Note: SMAPI assumes that it can grammatically append the returned sentence in the + // form "failed loading because its ". Any errors returned should be valid + // in that format, unless the SMAPI call is adjusted accordingly. + // + + bool hasDll = !string.IsNullOrWhiteSpace(manifest.EntryDll); + bool isContentPack = manifest.ContentPackFor != null; + + // validate use of EntryDll vs ContentPackFor fields + if (hasDll == isContentPack) + { + error = hasDll + ? $"manifest sets both {nameof(IManifest.EntryDll)} and {nameof(IManifest.ContentPackFor)}, which are mutually exclusive." + : $"manifest has no {nameof(IManifest.EntryDll)} or {nameof(IManifest.ContentPackFor)} field; must specify one."; + return false; + } + + // validate EntryDll/ContentPackFor format + if (hasDll) + { + if (manifest.EntryDll!.Intersect(Path.GetInvalidFileNameChars()).Any()) + { + error = $"manifest has invalid filename '{manifest.EntryDll}' for the {nameof(IManifest.EntryDll)} field."; + return false; + } + } + else + { + if (string.IsNullOrWhiteSpace(manifest.ContentPackFor!.UniqueID)) + { + error = $"manifest declares {nameof(IManifest.ContentPackFor)} without its required {nameof(IManifestContentPackFor.UniqueID)} field."; + return false; + } + } + + // validate required fields + { + List missingFields = new List(3); + + if (string.IsNullOrWhiteSpace(manifest.Name)) + missingFields.Add(nameof(IManifest.Name)); + if (manifest.Version == null || manifest.Version.ToString() == "0.0.0") + missingFields.Add(nameof(IManifest.Version)); + if (string.IsNullOrWhiteSpace(manifest.UniqueID)) + missingFields.Add(nameof(IManifest.UniqueID)); + + if (missingFields.Any()) + { + error = $"manifest is missing required fields ({string.Join(", ", missingFields)})."; + return false; + } + } + + // validate ID format + if (!PathUtilities.IsSlug(manifest.UniqueID)) + { + error = "manifest specifies an invalid ID (IDs must only contain letters, numbers, underscores, periods, or hyphens)."; + return false; + } + + // validate dependency format + foreach (IManifestDependency? dependency in manifest.Dependencies) + { + if (dependency == null) + { + error = $"manifest has a null entry under {nameof(IManifest.Dependencies)}."; + return false; + } + + if (string.IsNullOrWhiteSpace(dependency.UniqueID)) + { + error = $"manifest has a {nameof(IManifest.Dependencies)} entry with no {nameof(IManifestDependency.UniqueID)} field."; + return false; + } + + if (!PathUtilities.IsSlug(dependency.UniqueID)) + { + error = $"manifest has a {nameof(IManifest.Dependencies)} entry with an invalid {nameof(IManifestDependency.UniqueID)} field (IDs must only contain letters, numbers, underscores, periods, or hyphens)."; + return false; + } + } + + error = ""; + return true; + } + } +} diff --git a/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs b/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs index ef6d4dd9..da678ac9 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs @@ -9,6 +9,6 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors ********/ /// Extra metadata about mods. - public IDictionary ModData { get; set; } + public IDictionary ModData { get; } = new Dictionary(); } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs index b3954693..9674d283 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs @@ -18,10 +18,10 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData public bool IsDefault { get; } /// The lowest version in the range, or null for all past versions. - public ISemanticVersion LowerVersion { get; } + public ISemanticVersion? LowerVersion { get; } /// The highest version in the range, or null for all future versions. - public ISemanticVersion UpperVersion { get; } + public ISemanticVersion? UpperVersion { get; } /********* @@ -33,7 +33,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Whether this field should only be applied if it's not already set. /// The lowest version in the range, or null for all past versions. /// The highest version in the range, or null for all future versions. - public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion lowerVersion, ISemanticVersion upperVersion) + public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion? lowerVersion, ISemanticVersion? upperVersion) { this.Key = key; this.Value = value; @@ -44,7 +44,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get whether this data field applies for the given manifest. /// The mod manifest. - public bool IsMatch(IManifest manifest) + public bool IsMatch(IManifest? manifest) { return manifest?.Version != null // ignore invalid manifest @@ -66,11 +66,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData { // update key case ModDataFieldKey.UpdateKey: - return manifest.UpdateKeys != null && manifest.UpdateKeys.Any(p => !string.IsNullOrWhiteSpace(p)); + return manifest.UpdateKeys.Any(p => !string.IsNullOrWhiteSpace(p)); // non-manifest fields - case ModDataFieldKey.AlternativeUrl: case ModDataFieldKey.StatusReasonPhrase: + case ModDataFieldKey.StatusReasonDetails: case ModDataFieldKey.Status: return false; diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataFieldKey.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataFieldKey.cs index 09dd0cc5..2b59096d 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataFieldKey.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataFieldKey.cs @@ -6,13 +6,13 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// A manifest update key. UpdateKey, - /// An alternative URL the player can check for an updated version. - AlternativeUrl, - /// The mod's predefined compatibility status. Status, /// A reason phrase for the , or null to use the default reason. - StatusReasonPhrase + StatusReasonPhrase, + + /// Technical details shown in TRACE logs for the , or null to omit it. + StatusReasonDetails } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs index 2167d3e5..5912fb87 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs @@ -14,7 +14,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors *********/ /// The mod's current unique ID. - public string ID { get; set; } + public string ID { get; } /// The former mod IDs (if any). /// @@ -23,14 +23,14 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// ID, if any. If the mod's ID changed over time, multiple variants can be separated by the /// | character. /// - public string FormerIDs { get; set; } + public string? FormerIDs { get; } /// The mod warnings to suppress, even if they'd normally be shown. - public ModWarning SuppressWarnings { get; set; } + public ModWarning SuppressWarnings { get; } /// This field stores properties that aren't mapped to another field before they're parsed into . [JsonExtensionData] - public IDictionary ExtensionData { get; set; } + public IDictionary ExtensionData { get; } = new Dictionary(); /// The versioned field data. /// @@ -50,6 +50,17 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /********* ** Public methods *********/ + /// Construct an instance. + /// The mod's current unique ID. + /// The former mod IDs (if any). + /// The mod warnings to suppress, even if they'd normally be shown. + public ModDataModel(string id, string? formerIds, ModWarning suppressWarnings) + { + this.ID = id; + this.FormerIDs = formerIds; + this.SuppressWarnings = suppressWarnings; + } + /// Get a parsed representation of the . public IEnumerable GetFields() { @@ -59,8 +70,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData string packedKey = pair.Key; string value = pair.Value; bool isDefault = false; - ISemanticVersion lowerVersion = null; - ISemanticVersion upperVersion = null; + ISemanticVersion? lowerVersion = null; + ISemanticVersion? upperVersion = null; // parse string[] parts = packedKey.Split('|').Select(p => p.Trim()).ToArray(); @@ -111,11 +122,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData [OnDeserialized] private void OnDeserialized(StreamingContext context) { - if (this.ExtensionData != null) - { - this.Fields = this.ExtensionData.ToDictionary(p => p.Key, p => p.Value.ToString()); - this.ExtensionData = null; - } + this.Fields = this.ExtensionData.ToDictionary(p => p.Key, p => p.Value.ToString()); + this.ExtensionData.Clear(); } } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs index 3201c421..ab0e4377 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs @@ -20,7 +20,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData public string[] FormerIDs { get; } /// The mod warnings to suppress, even if they'd normally be shown. - public ModWarning SuppressWarnings { get; set; } + public ModWarning SuppressWarnings { get; } /// The versioned field data. public ModDataField[] Fields { get; } @@ -70,9 +70,9 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData } /// Get the default update key for this mod, if any. - public string GetDefaultUpdateKey() + public string? GetDefaultUpdateKey() { - string updateKey = this.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey && p.IsDefault)?.Value; + string? updateKey = this.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey && p.IsDefault)?.Value; return !string.IsNullOrWhiteSpace(updateKey) ? updateKey : null; @@ -80,9 +80,9 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get a parsed representation of the which match a given manifest. /// The manifest to match. - public ModDataRecordVersionedFields GetVersionedFields(IManifest manifest) + public ModDataRecordVersionedFields GetVersionedFields(IManifest? manifest) { - ModDataRecordVersionedFields parsed = new ModDataRecordVersionedFields { DisplayName = this.DisplayName, DataRecord = this }; + ModDataRecordVersionedFields parsed = new(this); foreach (ModDataField field in this.Fields.Where(field => field.IsMatch(manifest))) { switch (field.Key) @@ -92,11 +92,6 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData parsed.UpdateKey = field.Value; break; - // alternative URL - case ModDataFieldKey.AlternativeUrl: - parsed.AlternativeUrl = field.Value; - break; - // status case ModDataFieldKey.Status: parsed.Status = (ModStatus)Enum.Parse(typeof(ModStatus), field.Value, ignoreCase: true); @@ -107,6 +102,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData case ModDataFieldKey.StatusReasonPhrase: parsed.StatusReasonPhrase = field.Value; break; + + // status technical reason + case ModDataFieldKey.StatusReasonDetails: + parsed.StatusReasonDetails = field.Value; + break; } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs index 598da66a..65fa424e 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs @@ -7,24 +7,32 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors *********/ /// The underlying data record. - public ModDataRecord DataRecord { get; set; } + public ModDataRecord DataRecord { get; } - /// The default mod name to display when the name isn't available (e.g. during dependency checks). - public string DisplayName { get; set; } - - /// The update key to apply. - public string UpdateKey { get; set; } - - /// The alternative URL the player can check for an updated version. - public string AlternativeUrl { get; set; } + /// The update key to apply (if any). + public string? UpdateKey { get; set; } /// The predefined compatibility status. public ModStatus Status { get; set; } = ModStatus.None; /// A reason phrase for the , or null to use the default reason. - public string StatusReasonPhrase { get; set; } + public string? StatusReasonPhrase { get; set; } + + /// Technical details shown in TRACE logs for the , or null to omit it. + public string? StatusReasonDetails { get; set; } /// The upper version for which the applies (if any). - public ISemanticVersion StatusUpperVersion { get; set; } + public ISemanticVersion? StatusUpperVersion { get; set; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The underlying data record. + public ModDataRecordVersionedFields(ModDataRecord dataRecord) + { + this.DataRecord = dataRecord; + } } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs index a9da884a..168b8aac 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs @@ -14,7 +14,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData private readonly ModDataRecord[] Records; /// Get an update URL for an update key (if valid). - private readonly Func GetUpdateUrl; + private readonly Func GetUpdateUrl; /********* @@ -22,12 +22,12 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData *********/ /// Construct an empty instance. public ModDatabase() - : this(new ModDataRecord[0], key => null) { } + : this(Array.Empty(), _ => null) { } /// Construct an instance. /// The underlying mod data records indexed by default display name. /// Get an update URL for an update key (if valid). - public ModDatabase(IEnumerable records, Func getUpdateUrl) + public ModDatabase(IEnumerable records, Func getUpdateUrl) { this.Records = records.ToArray(); this.GetUpdateUrl = getUpdateUrl; @@ -41,7 +41,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get a mod data record. /// The unique mod ID. - public ModDataRecord Get(string modID) + public ModDataRecord? Get(string? modID) { return !string.IsNullOrWhiteSpace(modID) ? this.Records.FirstOrDefault(p => p.HasID(modID)) @@ -50,11 +50,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get the mod page URL for a mod (if available). /// The unique mod ID. - public string GetModPageUrlFor(string id) + public string? GetModPageUrlFor(string? id) { // get update key - ModDataRecord record = this.Get(id); - ModDataField updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); + ModDataRecord? record = this.Get(id); + ModDataField? updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); if (updateKeyField == null) return null; diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModWarning.cs b/src/SMAPI.Toolkit/Framework/ModData/ModWarning.cs index 925e0b5c..4c76f417 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModWarning.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModWarning.cs @@ -18,8 +18,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// The mod patches the game in a way that may impact stability. PatchesGame = 4, - /// The mod uses the dynamic keyword which won't work on Linux/Mac. +#if SMAPI_DEPRECATED + /// The mod uses the dynamic keyword which won't work on Linux/macOS. + [Obsolete("This value is no longer used by SMAPI and will be removed in the upcoming SMAPI 4.0.0.")] UsesDynamic = 8, +#endif /// The mod references specialized 'unvalidated update tick' events which may impact stability. UsesUnvalidatedUpdateTick = 16, @@ -34,6 +37,17 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData AccessesFilesystem = 128, /// Uses .NET APIs for shell or process access. - AccessesShell = 256 + AccessesShell = 256, + +#if SMAPI_DEPRECATED + /// References the legacy System.Configuration.ConfigurationManager assembly and doesn't include a copy in the mod folder, so it'll break in SMAPI 4.0.0. + DetectedLegacyConfigurationDll = 512, + + /// References the legacy System.Runtime.Caching assembly and doesn't include a copy in the mod folder, so it'll break in SMAPI 4.0.0. + DetectedLegacyCachingDll = 1024, + + /// References the legacy System.Security.Permissions assembly and doesn't include a copy in the mod folder, so it'll break in SMAPI 4.0.0. + DetectedLegacyPermissionsDll = 2048 +#endif } } diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModFolder.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModFolder.cs index d0df09a1..da2a3c85 100644 --- a/src/SMAPI.Toolkit/Framework/ModScanning/ModFolder.cs +++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModFolder.cs @@ -22,13 +22,13 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning public ModType Type { get; } /// The mod manifest. - public Manifest Manifest { get; } + public Manifest? Manifest { get; } /// The error which occurred parsing the manifest, if any. public ModParseError ManifestParseError { get; set; } /// A human-readable message for the , if any. - public string ManifestParseErrorText { get; set; } + public string? ManifestParseErrorText { get; set; } /********* @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// The mod manifest. /// The error which occurred parsing the manifest, if any. /// A human-readable message for the , if any. - public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest manifest, ModParseError manifestParseError, string manifestParseErrorText) + public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest? manifest, ModParseError manifestParseError, string? manifestParseErrorText) { // save info this.Directory = directory; @@ -59,9 +59,9 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning this.ManifestParseErrorText = manifestParseErrorText; // set display name - this.DisplayName = manifest?.Name; - if (string.IsNullOrWhiteSpace(this.DisplayName)) - this.DisplayName = PathUtilities.GetRelativePath(root.FullName, directory.FullName); + this.DisplayName = !string.IsNullOrWhiteSpace(manifest?.Name) + ? manifest.Name + : PathUtilities.GetRelativePath(root.FullName, directory.FullName); } /// Get the update keys for a mod. @@ -69,7 +69,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning public IEnumerable GetUpdateKeys(Manifest manifest) { return - (manifest.UpdateKeys ?? new string[0]) + manifest.UpdateKeys .Where(p => !string.IsNullOrWhiteSpace(p)) .ToArray(); } diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModParseError.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModParseError.cs index b10510ff..f1e782b6 100644 --- a/src/SMAPI.Toolkit/Framework/ModScanning/ModParseError.cs +++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModParseError.cs @@ -9,6 +9,9 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// The folder is empty or contains only ignored files. EmptyFolder, + /// The folder is an empty folder managed by Vortex. + EmptyVortexFolder, + /// The folder is ignored by convention. IgnoredFolder, diff --git a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs index 6d6b6417..5e9e3c35 100644 --- a/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs +++ b/src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text.RegularExpressions; using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Serialization.Models; +using StardewModdingAPI.Toolkit.Utilities.PathLookups; namespace StardewModdingAPI.Toolkit.Framework.ModScanning { @@ -18,15 +19,15 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning private readonly JsonHelper JsonHelper; /// A list of filesystem entry names to ignore when checking whether a folder should be treated as a mod. - private readonly HashSet IgnoreFilesystemNames = new HashSet + private readonly HashSet IgnoreFilesystemNames = new() { new Regex(@"^__folder_managed_by_vortex$", RegexOptions.Compiled | RegexOptions.IgnoreCase), // Vortex mod manager - new Regex(@"(?:^\._|^\.DS_Store$|^__MACOSX$|^mcs$)", RegexOptions.Compiled | RegexOptions.IgnoreCase), // MacOS + new Regex(@"(?:^\._|^\.DS_Store$|^__MACOSX$|^mcs$)", RegexOptions.Compiled | RegexOptions.IgnoreCase), // macOS new Regex(@"^(?:desktop\.ini|Thumbs\.db)$", RegexOptions.Compiled | RegexOptions.IgnoreCase) // Windows }; /// A list of file extensions to ignore when searching for mod files. - private readonly HashSet IgnoreFileExtensions = new HashSet(StringComparer.OrdinalIgnoreCase) + private readonly HashSet IgnoreFileExtensions = new(StringComparer.OrdinalIgnoreCase) { // text ".doc", @@ -38,15 +39,20 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning // images ".bmp", ".gif", + ".ico", ".jpeg", ".jpg", ".png", ".psd", ".tif", + ".xcf", // gimp files // archives ".rar", ".zip", + ".7z", + ".tar", + ".tar.gz", // backup files ".backup", @@ -58,20 +64,28 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning ".lnk" }; - /// The extensions for files which an XNB mod may contain. If a mod doesn't have a manifest.json and contains *only* these file extensions, it should be considered an XNB mod. - private readonly HashSet PotentialXnbModExtensions = new HashSet(StringComparer.OrdinalIgnoreCase) + /// The extensions for packed content files. + private readonly HashSet StrictXnbModExtensions = new(StringComparer.OrdinalIgnoreCase) { - // XNB files ".xgs", ".xnb", ".xsb", - ".xwb", + ".xwb" + }; - // unpacking artifacts + /// The extensions for files which an XNB mod may contain, in addition to . + private readonly HashSet PotentialXnbModExtensions = new(StringComparer.OrdinalIgnoreCase) + { ".json", ".yaml" }; + /// The name of the marker file added by Vortex to indicate it's managing the folder. + private readonly string VortexMarkerFileName = "__folder_managed_by_vortex"; + + /// The name for a mod's configuration JSON file. + private readonly string ConfigFileName = "config.json"; + /********* ** Public methods @@ -85,48 +99,66 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// Extract information about all mods in the given folder. /// The root folder containing mods. - public IEnumerable GetModFolders(string rootPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, bool useCaseInsensitiveFilePaths) { - DirectoryInfo root = new DirectoryInfo(rootPath); - return this.GetModFolders(root, root); + DirectoryInfo root = new(rootPath); + return this.GetModFolders(root, root, useCaseInsensitiveFilePaths); } /// Extract information about all mods in the given folder. /// The root folder containing mods. Only the will be searched, but this field allows it to be treated as a potential mod folder of its own. /// The mod path to search. - // /// If the folder contains multiple XNB mods, treat them as subfolders of a single mod. This is useful when reading a single mod archive, as opposed to a mods folder. - public IEnumerable GetModFolders(string rootPath, string modPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, string modPath, bool useCaseInsensitiveFilePaths) { - return this.GetModFolders(root: new DirectoryInfo(rootPath), folder: new DirectoryInfo(modPath)); + return this.GetModFolders(root: new DirectoryInfo(rootPath), folder: new DirectoryInfo(modPath), useCaseInsensitiveFilePaths: useCaseInsensitiveFilePaths); } /// Extract information from a mod folder. /// The root folder containing mods. /// The folder to search for a mod. - public ModFolder ReadFolder(DirectoryInfo root, DirectoryInfo searchFolder) + /// Whether to match file paths case-insensitively, even on Linux. + public ModFolder ReadFolder(DirectoryInfo root, DirectoryInfo searchFolder, bool useCaseInsensitiveFilePaths) { // find manifest.json - FileInfo manifestFile = this.FindManifest(searchFolder); + FileInfo? manifestFile = this.FindManifest(searchFolder, useCaseInsensitiveFilePaths); // set appropriate invalid-mod error if (manifestFile == null) { - FileInfo[] files = this.RecursivelyGetRelevantFiles(searchFolder).ToArray(); - if (!files.Any()) + FileInfo[] files = this.RecursivelyGetFiles(searchFolder).ToArray(); + FileInfo[] relevantFiles = files.Where(this.IsRelevant).ToArray(); + + // empty Vortex folder + // (this filters relevant files internally so it can check for the normally-ignored Vortex marker file) + if (this.IsEmptyVortexFolder(files)) + return new ModFolder(root, searchFolder, ModType.Invalid, null, ModParseError.EmptyVortexFolder, "it's an empty Vortex folder (is the mod disabled in Vortex?)."); + + // empty folder + if (!relevantFiles.Any()) return new ModFolder(root, searchFolder, ModType.Invalid, null, ModParseError.EmptyFolder, "it's an empty folder."); - if (files.All(this.IsPotentialXnbFile)) + + // XNB mod + if (this.IsXnbMod(relevantFiles)) return new ModFolder(root, searchFolder, ModType.Xnb, null, ModParseError.XnbMod, "it's not a SMAPI mod (see https://smapi.io/xnb for info)."); + + // SMAPI installer + if (relevantFiles.Any(p => p.Name is "install on Linux.sh" or "install on macOS.command" or "install on Windows.bat")) + return new ModFolder(root, searchFolder, ModType.Invalid, null, ModParseError.ManifestMissing, "the SMAPI installer isn't a mod (you can delete this folder after running the installer file)."); + + // not a mod? return new ModFolder(root, searchFolder, ModType.Invalid, null, ModParseError.ManifestMissing, "it contains files, but none of them are manifest.json."); } // read mod info - Manifest manifest = null; + Manifest? manifest = null; ModParseError error = ModParseError.None; - string errorText = null; + string? errorText = null; { try { - if (!this.JsonHelper.ReadJsonFileIfExists(manifestFile.FullName, out manifest) || manifest == null) + if (!this.JsonHelper.ReadJsonFileIfExists(manifestFile.FullName, out manifest)) { error = ModParseError.ManifestInvalid; errorText = "its manifest is invalid."; @@ -144,25 +176,22 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning } } - // normalize display fields - if (manifest != null) - { - manifest.Name = this.StripNewlines(manifest.Name); - manifest.Description = this.StripNewlines(manifest.Description); - manifest.Author = this.StripNewlines(manifest.Author); - } - // get mod type - ModType type = ModType.Invalid; - if (manifest != null) + ModType type; { - type = !string.IsNullOrWhiteSpace(manifest.ContentPackFor?.UniqueID) - ? ModType.ContentPack - : ModType.Smapi; + bool isContentPack = !string.IsNullOrWhiteSpace(manifest?.ContentPackFor?.UniqueID); + bool isSmapi = !string.IsNullOrWhiteSpace(manifest?.EntryDll); + + if (isContentPack == isSmapi) + type = ModType.Invalid; + else if (isContentPack) + type = ModType.ContentPack; + else + type = ModType.Smapi; } // build result - return new ModFolder(root, manifestFile.Directory, type, manifest, error, errorText); + return new ModFolder(root, manifestFile.Directory!, type, manifest, error, errorText); } @@ -172,7 +201,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// Recursively extract information about all mods in the given folder. /// The root mod folder. /// The folder to search for mods. - private IEnumerable GetModFolders(DirectoryInfo root, DirectoryInfo folder) + /// Whether to match file paths case-insensitively, even on Linux. + private IEnumerable GetModFolders(DirectoryInfo root, DirectoryInfo folder, bool useCaseInsensitiveFilePaths) { bool isRoot = folder.FullName == root.FullName; @@ -191,7 +221,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning // find mods in subfolders if (this.IsModSearchFolder(root, folder)) { - IEnumerable subfolders = folder.EnumerateDirectories().SelectMany(sub => this.GetModFolders(root, sub)); + IEnumerable subfolders = folder.EnumerateDirectories().SelectMany(sub => this.GetModFolders(root, sub, useCaseInsensitiveFilePaths)); if (!isRoot) subfolders = this.TryConsolidate(root, folder, subfolders.ToArray()); foreach (ModFolder subfolder in subfolders) @@ -200,7 +230,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning // treat as mod folder else - yield return this.ReadFolder(root, folder); + yield return this.ReadFolder(root, folder, useCaseInsensitiveFilePaths); } /// Consolidate adjacent folders into one mod folder, if possible. @@ -225,26 +255,27 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// Find the manifest for a mod folder. /// The folder to search. - private FileInfo FindManifest(DirectoryInfo folder) + /// Whether to match file paths case-insensitively, even on Linux. + private FileInfo? FindManifest(DirectoryInfo folder, bool useCaseInsensitiveFilePaths) { - while (true) + // check for conventional manifest in current folder + const string defaultName = "manifest.json"; + FileInfo file = new(Path.Combine(folder.FullName, defaultName)); + if (file.Exists) + return file; + + // check for manifest with incorrect capitalization + if (useCaseInsensitiveFilePaths) { - // check for manifest in current folder - FileInfo file = new FileInfo(Path.Combine(folder.FullName, "manifest.json")); - if (file.Exists) - return file; - - // check for single subfolder - FileSystemInfo[] entries = folder.EnumerateFileSystemInfos().Take(2).ToArray(); - if (entries.Length == 1 && entries[0] is DirectoryInfo subfolder) - { - folder = subfolder; - continue; - } - - // not found - return null; + CaseInsensitiveFileLookup fileLookup = new(folder.FullName, SearchOption.TopDirectoryOnly); // don't use GetCachedFor, since we only need it temporarily + file = fileLookup.GetFile(defaultName); + return file.Exists + ? file + : null; } + + // not found + return null; } /// Get whether a given folder should be treated as a search folder (i.e. look for subfolders containing mods). @@ -260,13 +291,13 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning return subfolders.Any() && !files.Any(); } - /// Recursively get all relevant files in a folder based on the result of . + /// Recursively get all files in a folder. /// The root folder to search. - private IEnumerable RecursivelyGetRelevantFiles(DirectoryInfo folder) + private IEnumerable RecursivelyGetFiles(DirectoryInfo folder) { foreach (FileSystemInfo entry in folder.GetFileSystemInfos()) { - if (!this.IsRelevant(entry)) + if (entry is DirectoryInfo && !this.IsRelevant(entry)) continue; if (entry is FileInfo file) @@ -274,7 +305,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning if (entry is DirectoryInfo subfolder) { - foreach (FileInfo subfolderFile in this.RecursivelyGetRelevantFiles(subfolder)) + foreach (FileInfo subfolderFile in this.RecursivelyGetFiles(subfolder)) yield return subfolderFile; } } @@ -284,29 +315,54 @@ namespace StardewModdingAPI.Toolkit.Framework.ModScanning /// The file or folder. private bool IsRelevant(FileSystemInfo entry) { - // ignored file extension - if (entry is FileInfo file && this.IgnoreFileExtensions.Contains(file.Extension)) + // ignored file extensions and any files starting with "." + if ((entry is FileInfo file) && (this.IgnoreFileExtensions.Contains(file.Extension) || file.Name.StartsWith("."))) return false; // ignored entry name return !this.IgnoreFilesystemNames.Any(p => p.IsMatch(entry.Name)); } - /// Get whether a file is potentially part of an XNB mod. - /// The file. - private bool IsPotentialXnbFile(FileInfo entry) + /// Get whether a set of files looks like an XNB mod. + /// The files in the mod. + private bool IsXnbMod(IEnumerable files) { - if (!this.IsRelevant(entry)) - return true; + bool hasXnbFile = false; - return this.PotentialXnbModExtensions.Contains(entry.Extension); // use EndsWith to handle cases like image..png + foreach (FileInfo file in files.Where(this.IsRelevant)) + { + if (this.StrictXnbModExtensions.Contains(file.Extension)) + { + hasXnbFile = true; + continue; + } + + if (!this.PotentialXnbModExtensions.Contains(file.Extension)) + return false; + } + + return hasXnbFile; } - /// Strip newlines from a string. - /// The input to strip. - private string StripNewlines(string input) + /// Get whether a set of files looks like an XNB mod. + /// The files in the mod. + private bool IsEmptyVortexFolder(IEnumerable files) { - return input?.Replace("\r", "").Replace("\n", ""); + bool hasVortexMarker = false; + + foreach (FileInfo file in files) + { + if (file.Name == this.VortexMarkerFileName) + { + hasVortexMarker = true; + continue; + } + + if (this.IsRelevant(file) && file.Name != this.ConfigFileName) + return false; + } + + return hasVortexMarker; } } } diff --git a/src/SMAPI.Toolkit/Framework/SemanticVersionReader.cs b/src/SMAPI.Toolkit/Framework/SemanticVersionReader.cs index 489e1c4d..939be771 100644 --- a/src/SMAPI.Toolkit/Framework/SemanticVersionReader.cs +++ b/src/SMAPI.Toolkit/Framework/SemanticVersionReader.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + namespace StardewModdingAPI.Toolkit.Framework { /// Reads strings into a semantic version. @@ -16,7 +18,7 @@ namespace StardewModdingAPI.Toolkit.Framework /// An optional prerelease tag. /// Optional build metadata. This is ignored when determining version precedence. /// Returns whether the version was successfully parsed. - public static bool TryParse(string versionStr, bool allowNonStandard, out int major, out int minor, out int patch, out int platformRelease, out string prereleaseTag, out string buildMetadata) + public static bool TryParse(string? versionStr, bool allowNonStandard, out int major, out int minor, out int patch, out int platformRelease, out string? prereleaseTag, out string? buildMetadata) { // init major = 0; @@ -103,7 +105,12 @@ namespace StardewModdingAPI.Toolkit.Framework /// The raw characters to parse. /// The index of the next character to read. /// The parsed tag. - private static bool TryParseTag(char[] raw, ref int index, out string tag) + private static bool TryParseTag(char[] raw, ref int index, +#if NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out string? tag + ) { // read tag length int length = 0; diff --git a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs index 7e4d0220..960caf96 100644 --- a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs +++ b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Toolkit.Framework.UpdateData { @@ -15,12 +16,15 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData public ModSiteKey Site { get; } /// The mod ID within the repository. - public string ID { get; } + public string? ID { get; } /// If specified, a substring in download names/descriptions to match. - public string Subkey { get; } + public string? Subkey { get; } /// Whether the update key seems to be valid. +#if NET5_0_OR_GREATER + [MemberNotNullWhen(true, nameof(UpdateKey.ID))] +#endif public bool LooksValid { get; } @@ -32,9 +36,9 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the site. /// If specified, a substring in download names/descriptions to match. - public UpdateKey(string rawText, ModSiteKey site, string id, string subkey) + public UpdateKey(string? rawText, ModSiteKey site, string? id, string? subkey) { - this.RawText = rawText?.Trim(); + this.RawText = rawText?.Trim() ?? string.Empty; this.Site = site; this.ID = id?.Trim(); this.Subkey = subkey?.Trim(); @@ -47,19 +51,19 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the site. /// If specified, a substring in download names/descriptions to match. - public UpdateKey(ModSiteKey site, string id, string subkey) + public UpdateKey(ModSiteKey site, string? id, string? subkey) : this(UpdateKey.GetString(site, id, subkey), site, id, subkey) { } /// Parse a raw update key. /// The raw update key to parse. - public static UpdateKey Parse(string raw) + public static UpdateKey Parse(string? raw) { // extract site + ID - string rawSite; - string id; + string? rawSite; + string? id; { - string[] parts = raw?.Trim().Split(':'); - if (parts == null || parts.Length != 2) + string[]? parts = raw?.Trim().Split(':'); + if (parts?.Length != 2) return new UpdateKey(raw, ModSiteKey.Unknown, null, null); rawSite = parts[0].Trim(); @@ -69,7 +73,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData id = null; // extract subkey - string subkey = null; + string? subkey = null; if (id != null) { string[] parts = id.Split('@'); @@ -89,6 +93,16 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData return new UpdateKey(raw, site, id, subkey); } + /// Parse a raw update key if it's valid. + /// The raw update key to parse. + /// The parsed update key, if valid. + /// Returns whether the update key was successfully parsed. + public static bool TryParse(string raw, out UpdateKey parsed) + { + parsed = UpdateKey.Parse(raw); + return parsed.LooksValid; + } + /// Get a string that represents the current object. public override string ToString() { @@ -99,7 +113,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// Indicates whether the current object is equal to another object of the same type. /// An object to compare with this object. - public bool Equals(UpdateKey other) + public bool Equals(UpdateKey? other) { if (!this.LooksValid) { @@ -117,7 +131,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// Determines whether the specified object is equal to the current object. /// The object to compare with the current object. - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is UpdateKey other && this.Equals(other); } @@ -133,7 +147,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the repository. /// If specified, a substring in download names/descriptions to match. - public static string GetString(ModSiteKey site, string id, string subkey = null) + public static string GetString(ModSiteKey site, string? id, string? subkey = null) { return $"{site}:{id}{subkey}".Trim(); } diff --git a/src/SMAPI.Toolkit/ModToolkit.cs b/src/SMAPI.Toolkit/ModToolkit.cs index 7467a099..67fbb5e2 100644 --- a/src/SMAPI.Toolkit/ModToolkit.cs +++ b/src/SMAPI.Toolkit/ModToolkit.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -8,6 +7,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; using StardewModdingAPI.Toolkit.Framework.GameScanning; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModScanning; +using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization; namespace StardewModdingAPI.Toolkit @@ -22,11 +22,11 @@ namespace StardewModdingAPI.Toolkit private readonly string UserAgent; /// Maps vendor keys (like Nexus) to their mod URL template (where {0} is the mod ID). This doesn't affect update checks, which defer to the remote web API. - private readonly IDictionary VendorModUrls = new Dictionary(StringComparer.OrdinalIgnoreCase) + private readonly Dictionary VendorModUrls = new() { - ["Chucklefish"] = "https://community.playstarbound.com/resources/{0}", - ["GitHub"] = "https://github.com/{0}/releases", - ["Nexus"] = "https://www.nexusmods.com/stardewvalley/mods/{0}" + [ModSiteKey.Chucklefish] = "https://community.playstarbound.com/resources/{0}", + [ModSiteKey.GitHub] = "https://github.com/{0}/releases", + [ModSiteKey.Nexus] = "https://www.nexusmods.com/stardewvalley/mods/{0}" }; @@ -34,7 +34,7 @@ namespace StardewModdingAPI.Toolkit ** Accessors *********/ /// Encapsulates SMAPI's JSON parsing. - public JsonHelper JsonHelper { get; } = new JsonHelper(); + public JsonHelper JsonHelper { get; } = new(); /********* @@ -43,7 +43,7 @@ namespace StardewModdingAPI.Toolkit /// Construct an instance. public ModToolkit() { - ISemanticVersion version = new SemanticVersion(this.GetType().Assembly.GetName().Version); + ISemanticVersion version = new SemanticVersion(this.GetType().Assembly.GetName().Version!); this.UserAgent = $"SMAPI Mod Handler Toolkit/{version}"; } @@ -60,7 +60,7 @@ namespace StardewModdingAPI.Toolkit #if SMAPI_FOR_MOBILE return null; #else - var client = new WikiClient(this.UserAgent); + using WikiClient client = new(this.UserAgent); return await client.FetchModsAsync(); #endif } @@ -69,39 +69,38 @@ namespace StardewModdingAPI.Toolkit /// The file path for the SMAPI metadata file. public ModDatabase GetModDatabase(string metadataPath) { - MetadataModel metadata = JsonConvert.DeserializeObject(File.ReadAllText(metadataPath)); + MetadataModel metadata = JsonConvert.DeserializeObject(File.ReadAllText(metadataPath)) ?? new MetadataModel(); ModDataRecord[] records = metadata.ModData.Select(pair => new ModDataRecord(pair.Key, pair.Value)).ToArray(); return new ModDatabase(records, this.GetUpdateUrl); } /// Extract information about all mods in the given folder. /// The root folder containing mods. - public IEnumerable GetModFolders(string rootPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, bool useCaseInsensitiveFilePaths) { - return new ModScanner(this.JsonHelper).GetModFolders(rootPath); + return new ModScanner(this.JsonHelper).GetModFolders(rootPath, useCaseInsensitiveFilePaths); } /// Extract information about all mods in the given folder. /// The root folder containing mods. Only the will be searched, but this field allows it to be treated as a potential mod folder of its own. /// The mod path to search. - public IEnumerable GetModFolders(string rootPath, string modPath) + /// Whether to match file paths case-insensitively, even on Linux. + public IEnumerable GetModFolders(string rootPath, string modPath, bool useCaseInsensitiveFilePaths) { - return new ModScanner(this.JsonHelper).GetModFolders(rootPath, modPath); + return new ModScanner(this.JsonHelper).GetModFolders(rootPath, modPath, useCaseInsensitiveFilePaths); } /// Get an update URL for an update key (if valid). /// The update key. - public string GetUpdateUrl(string updateKey) + public string? GetUpdateUrl(string updateKey) { - string[] parts = updateKey.Split(new[] { ':' }, 2); - if (parts.Length != 2) + UpdateKey parsed = UpdateKey.Parse(updateKey); + if (!parsed.LooksValid) return null; - string vendorKey = parts[0].Trim(); - string modID = parts[1].Trim(); - - if (this.VendorModUrls.TryGetValue(vendorKey, out string urlTemplate)) - return string.Format(urlTemplate, modID); + if (this.VendorModUrls.TryGetValue(parsed.Site, out string? urlTemplate)) + return string.Format(urlTemplate, parsed.ID); return null; } diff --git a/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs index 233e680b..6f5dffbe 100644 --- a/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs +++ b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs @@ -1,4 +1,6 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("StardewModdingAPI")] +[assembly: InternalsVisibleTo("SMAPI.Installer")] +[assembly: InternalsVisibleTo("SMAPI.Tests")] [assembly: InternalsVisibleTo("SMAPI.Web")] diff --git a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj index 6db64cf3..033009c7 100644 --- a/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj +++ b/src/SMAPI.Toolkit/SMAPI.Toolkit.csproj @@ -3,25 +3,25 @@ StardewModdingAPI.Toolkit A library which encapsulates mod-handling logic for mod managers and tools. Not intended for use by mods. - net4.5;netstandard2.0 + net5.0; netstandard2.0 true - x86 + + - - - + + + + - - diff --git a/src/SMAPI.Toolkit/SemanticVersion.cs b/src/SMAPI.Toolkit/SemanticVersion.cs index 0f341665..3713758f 100644 --- a/src/SMAPI.Toolkit/SemanticVersion.cs +++ b/src/SMAPI.Toolkit/SemanticVersion.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; using StardewModdingAPI.Toolkit.Framework; @@ -38,10 +39,10 @@ namespace StardewModdingAPI.Toolkit public int PlatformRelease { get; } /// - public string PrereleaseTag { get; } + public string? PrereleaseTag { get; } /// - public string BuildMetadata { get; } + public string? BuildMetadata { get; } /********* @@ -54,7 +55,7 @@ namespace StardewModdingAPI.Toolkit /// The platform-specific version (if applicable). /// An optional prerelease tag. /// Optional build metadata. This is ignored when determining version precedence. - public SemanticVersion(int major, int minor, int patch, int platformRelease = 0, string prereleaseTag = null, string buildMetadata = null) + public SemanticVersion(int major, int minor, int patch, int platformRelease = 0, string? prereleaseTag = null, string? buildMetadata = null) { this.MajorVersion = major; this.MinorVersion = minor; @@ -90,7 +91,7 @@ namespace StardewModdingAPI.Toolkit { if (version == null) throw new ArgumentNullException(nameof(version), "The input version string can't be null."); - if (!SemanticVersionReader.TryParse(version, allowNonStandard, out int major, out int minor, out int patch, out int platformRelease, out string prereleaseTag, out string buildMetadata) || (!allowNonStandard && platformRelease != 0)) + if (!SemanticVersionReader.TryParse(version, allowNonStandard, out int major, out int minor, out int patch, out int platformRelease, out string? prereleaseTag, out string? buildMetadata) || (!allowNonStandard && platformRelease != 0)) throw new FormatException($"The input '{version}' isn't a valid semantic version."); this.MajorVersion = major; @@ -104,59 +105,77 @@ namespace StardewModdingAPI.Toolkit } /// - public int CompareTo(ISemanticVersion other) + public int CompareTo(ISemanticVersion? other) { - if (other == null) - throw new ArgumentNullException(nameof(other)); - return this.CompareTo(other.MajorVersion, other.MinorVersion, other.PatchVersion, (other as SemanticVersion)?.PlatformRelease ?? 0, other.PrereleaseTag); + return other == null + ? 1 + : this.CompareTo(other.MajorVersion, other.MinorVersion, other.PatchVersion, (other as SemanticVersion)?.PlatformRelease ?? 0, other.PrereleaseTag); } /// - public bool Equals(ISemanticVersion other) + public bool Equals(ISemanticVersion? other) { return other != null && this.CompareTo(other) == 0; } /// +#if NET5_0_OR_GREATER + [MemberNotNullWhen(true, nameof(SemanticVersion.PrereleaseTag))] +#endif public bool IsPrerelease() { return !string.IsNullOrWhiteSpace(this.PrereleaseTag); } /// - public bool IsOlderThan(ISemanticVersion other) + public bool IsOlderThan(ISemanticVersion? other) { return this.CompareTo(other) < 0; } /// - public bool IsOlderThan(string other) + public bool IsOlderThan(string? other) { - return this.IsOlderThan(new SemanticVersion(other, allowNonStandard: true)); + ISemanticVersion? otherVersion = other != null + ? new SemanticVersion(other, allowNonStandard: true) + : null; + + return this.IsOlderThan(otherVersion); } /// - public bool IsNewerThan(ISemanticVersion other) + public bool IsNewerThan(ISemanticVersion? other) { return this.CompareTo(other) > 0; } /// - public bool IsNewerThan(string other) + public bool IsNewerThan(string? other) { - return this.IsNewerThan(new SemanticVersion(other, allowNonStandard: true)); + ISemanticVersion? otherVersion = other != null + ? new SemanticVersion(other, allowNonStandard: true) + : null; + + return this.IsNewerThan(otherVersion); } /// - public bool IsBetween(ISemanticVersion min, ISemanticVersion max) + public bool IsBetween(ISemanticVersion? min, ISemanticVersion? max) { return this.CompareTo(min) >= 0 && this.CompareTo(max) <= 0; } /// - public bool IsBetween(string min, string max) + public bool IsBetween(string? min, string? max) { - return this.IsBetween(new SemanticVersion(min, allowNonStandard: true), new SemanticVersion(max, allowNonStandard: true)); + ISemanticVersion? minVersion = min != null + ? new SemanticVersion(min, allowNonStandard: true) + : null; + ISemanticVersion? maxVersion = max != null + ? new SemanticVersion(max, allowNonStandard: true) + : null; + + return this.IsBetween(minVersion, maxVersion); } /// @@ -182,7 +201,12 @@ namespace StardewModdingAPI.Toolkit /// The version string. /// The parsed representation. /// Returns whether parsing the version succeeded. - public static bool TryParse(string version, out ISemanticVersion parsed) + public static bool TryParse(string? version, +#if NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out ISemanticVersion? parsed + ) { return SemanticVersion.TryParse(version, allowNonStandard: false, out parsed); } @@ -192,8 +216,19 @@ namespace StardewModdingAPI.Toolkit /// Whether to allow non-standard extensions to semantic versioning. /// The parsed representation. /// Returns whether parsing the version succeeded. - public static bool TryParse(string version, bool allowNonStandard, out ISemanticVersion parsed) + public static bool TryParse(string? version, bool allowNonStandard, +#if NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out ISemanticVersion? parsed + ) { + if (version == null) + { + parsed = null; + return false; + } + try { parsed = new SemanticVersion(version, allowNonStandard); @@ -212,7 +247,7 @@ namespace StardewModdingAPI.Toolkit *********/ /// Get a normalized prerelease or build tag. /// The tag to normalize. - private string GetNormalizedTag(string tag) + private string? GetNormalizedTag(string? tag) { tag = tag?.Trim(); return !string.IsNullOrWhiteSpace(tag) ? tag : null; @@ -224,44 +259,55 @@ namespace StardewModdingAPI.Toolkit /// The patch version to compare with this instance. /// The non-standard platform release to compare with this instance. /// The prerelease tag to compare with this instance. - private int CompareTo(int otherMajor, int otherMinor, int otherPatch, int otherPlatformRelease, string otherTag) + private int CompareTo(int otherMajor, int otherMinor, int otherPatch, int otherPlatformRelease, string? otherTag) { const int same = 0; const int curNewer = 1; const int curOlder = -1; - // compare stable versions - if (this.MajorVersion != otherMajor) - return this.MajorVersion.CompareTo(otherMajor); - if (this.MinorVersion != otherMinor) - return this.MinorVersion.CompareTo(otherMinor); - if (this.PatchVersion != otherPatch) - return this.PatchVersion.CompareTo(otherPatch); - if (this.PlatformRelease != otherPlatformRelease) - return this.PlatformRelease.CompareTo(otherPlatformRelease); - if (this.PrereleaseTag == otherTag) - return same; - - // stable supersedes prerelease - bool curIsStable = string.IsNullOrWhiteSpace(this.PrereleaseTag); - bool otherIsStable = string.IsNullOrWhiteSpace(otherTag); - if (curIsStable) - return curNewer; - if (otherIsStable) - return curOlder; - - // compare two prerelease tag values - string[] curParts = this.PrereleaseTag.Split('.', '-'); - string[] otherParts = otherTag.Split('.', '-'); - for (int i = 0; i < curParts.Length; i++) + int CompareToRaw() { - // longer prerelease tag supersedes if otherwise equal - if (otherParts.Length <= i) - return curNewer; + // compare stable versions + if (this.MajorVersion != otherMajor) + return this.MajorVersion.CompareTo(otherMajor); + if (this.MinorVersion != otherMinor) + return this.MinorVersion.CompareTo(otherMinor); + if (this.PatchVersion != otherPatch) + return this.PatchVersion.CompareTo(otherPatch); + if (this.PlatformRelease != otherPlatformRelease) + return this.PlatformRelease.CompareTo(otherPlatformRelease); + if (this.PrereleaseTag == otherTag) + return same; - // compare if different - if (curParts[i] != otherParts[i]) + // stable supersedes prerelease + bool curIsStable = string.IsNullOrWhiteSpace(this.PrereleaseTag); + bool otherIsStable = string.IsNullOrWhiteSpace(otherTag); + if (curIsStable) + return curNewer; + if (otherIsStable) + return curOlder; + + // compare two prerelease tag values + string[] curParts = this.PrereleaseTag?.Split('.', '-') ?? Array.Empty(); + string[] otherParts = otherTag?.Split('.', '-') ?? Array.Empty(); + int length = Math.Max(curParts.Length, otherParts.Length); + for (int i = 0; i < length; i++) { + // longer prerelease tag supersedes if otherwise equal + if (curParts.Length <= i) + return curOlder; + if (otherParts.Length <= i) + return curNewer; + + // skip if same value, unless we've reached the end + if (curParts[i] == otherParts[i]) + { + if (i == length - 1) + return same; + + continue; + } + // unofficial is always lower-precedence if (otherParts[i].Equals("unofficial", StringComparison.OrdinalIgnoreCase)) return curNewer; @@ -277,10 +323,17 @@ namespace StardewModdingAPI.Toolkit // else compare lexically return string.Compare(curParts[i], otherParts[i], StringComparison.OrdinalIgnoreCase); } + + // fallback (this should never happen) + return string.Compare(this.ToString(), new SemanticVersion(otherMajor, otherMinor, otherPatch, otherPlatformRelease, otherTag).ToString(), StringComparison.OrdinalIgnoreCase); } - // fallback (this should never happen) - return string.Compare(this.ToString(), new SemanticVersion(otherMajor, otherMinor, otherPatch, otherPlatformRelease, otherTag).ToString(), StringComparison.OrdinalIgnoreCase); + int result = CompareToRaw(); + if (result < 0) + return curOlder; + if (result > 0) + return curNewer; + return same; } /// Assert that the current version is valid. diff --git a/src/SMAPI.Toolkit/SemanticVersionComparer.cs b/src/SMAPI.Toolkit/SemanticVersionComparer.cs new file mode 100644 index 00000000..2eca30df --- /dev/null +++ b/src/SMAPI.Toolkit/SemanticVersionComparer.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; + +namespace StardewModdingAPI.Toolkit +{ + /// A comparer for semantic versions based on the field. + public class SemanticVersionComparer : IComparer + { + /********* + ** Accessors + *********/ + /// A singleton instance of the comparer. + public static SemanticVersionComparer Instance { get; } = new(); + + + /********* + ** Public methods + *********/ + /// + public int Compare(ISemanticVersion? x, ISemanticVersion? y) + { + if (object.ReferenceEquals(x, y)) + return 0; + + if (x is null) + return -1; + if (y is null) + return 1; + + return x.CompareTo(y); + } + } +} diff --git a/src/SMAPI.Toolkit/Serialization/Converters/ManifestContentPackForConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/ManifestContentPackForConverter.cs index 5cabe9d8..faaeedea 100644 --- a/src/SMAPI.Toolkit/Serialization/Converters/ManifestContentPackForConverter.cs +++ b/src/SMAPI.Toolkit/Serialization/Converters/ManifestContentPackForConverter.cs @@ -33,7 +33,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The object type. /// The object being read. /// The calling serializer. - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { return serializer.Deserialize(reader); } @@ -42,7 +42,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The JSON writer. /// The value. /// The calling serializer. - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { throw new InvalidOperationException("This converter does not write JSON."); } diff --git a/src/SMAPI.Toolkit/Serialization/Converters/ManifestDependencyArrayConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/ManifestDependencyArrayConverter.cs index 7b88d6b7..c499a2c6 100644 --- a/src/SMAPI.Toolkit/Serialization/Converters/ManifestDependencyArrayConverter.cs +++ b/src/SMAPI.Toolkit/Serialization/Converters/ManifestDependencyArrayConverter.cs @@ -35,13 +35,13 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The object type. /// The object being read. /// The calling serializer. - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { List result = new List(); foreach (JObject obj in JArray.Load(reader).Children()) { - string uniqueID = obj.ValueIgnoreCase(nameof(ManifestDependency.UniqueID)); - string minVersion = obj.ValueIgnoreCase(nameof(ManifestDependency.MinimumVersion)); + string uniqueID = obj.ValueIgnoreCase(nameof(ManifestDependency.UniqueID))!; // will be validated separately if null + string? minVersion = obj.ValueIgnoreCase(nameof(ManifestDependency.MinimumVersion)); bool required = obj.ValueIgnoreCase(nameof(ManifestDependency.IsRequired)) ?? true; result.Add(new ManifestDependency(uniqueID, minVersion, required)); } @@ -52,7 +52,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The JSON writer. /// The value. /// The calling serializer. - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { throw new InvalidOperationException("This converter does not write JSON."); } diff --git a/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs index 3604956b..913d54e0 100644 --- a/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs +++ b/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs @@ -18,10 +18,10 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters ** Accessors *********/ /// Get whether this converter can read JSON. - public override bool CanRead => true; + public override bool CanRead { get; } = true; /// Get whether this converter can write JSON. - public override bool CanWrite => true; + public override bool CanWrite { get; } = true; /********* @@ -39,15 +39,22 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The object type. /// The object being read. /// The calling serializer. - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { string path = reader.Path; switch (reader.TokenType) { case JsonToken.StartObject: return this.ReadObject(JObject.Load(reader)); + case JsonToken.String: - return this.ReadString(JToken.Load(reader).Value(), path); + { + string? value = JToken.Load(reader).Value(); + return value is not null + ? this.ReadString(value, path) + : null; + } + default: throw new SParseException($"Can't parse {nameof(ISemanticVersion)} from {reader.TokenType} node (path: {reader.Path})."); } @@ -57,7 +64,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The JSON writer. /// The value. /// The calling serializer. - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { writer.WriteValue(value?.ToString()); } @@ -73,7 +80,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters int major = obj.ValueIgnoreCase(nameof(ISemanticVersion.MajorVersion)); int minor = obj.ValueIgnoreCase(nameof(ISemanticVersion.MinorVersion)); int patch = obj.ValueIgnoreCase(nameof(ISemanticVersion.PatchVersion)); - string prereleaseTag = obj.ValueIgnoreCase(nameof(ISemanticVersion.PrereleaseTag)); + string? prereleaseTag = obj.ValueIgnoreCase(nameof(ISemanticVersion.PrereleaseTag)); return new SemanticVersion(major, minor, patch, prereleaseTag: prereleaseTag); } @@ -81,11 +88,11 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// Read a JSON string. /// The JSON string value. /// The path to the current JSON node. - private ISemanticVersion ReadString(string str, string path) + private ISemanticVersion? ReadString(string str, string path) { if (string.IsNullOrWhiteSpace(str)) return null; - if (!SemanticVersion.TryParse(str, allowNonStandard: this.AllowNonStandard, out ISemanticVersion version)) + if (!SemanticVersion.TryParse(str, allowNonStandard: this.AllowNonStandard, out ISemanticVersion? version)) throw new SParseException($"Can't parse semantic version from invalid value '{str}', should be formatted like 1.2, 1.2.30, or 1.2.30-beta (path: {path})."); return version; } diff --git a/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs index 549f0c18..cdf2ed77 100644 --- a/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs +++ b/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs @@ -22,16 +22,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The object type. public override bool CanConvert(Type objectType) { - return objectType == typeof(T); - } - - /// Writes the JSON representation of the object. - /// The JSON writer. - /// The value. - /// The calling serializer. - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - throw new InvalidOperationException("This converter does not write JSON."); + return objectType == typeof(T) || Nullable.GetUnderlyingType(objectType) == typeof(T); } /// Reads the JSON representation of the object. @@ -39,20 +30,39 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// The object type. /// The object being read. /// The calling serializer. - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { string path = reader.Path; switch (reader.TokenType) { + case JsonToken.Null when Nullable.GetUnderlyingType(objectType) != null: + return null; + case JsonToken.StartObject: return this.ReadObject(JObject.Load(reader), path); + case JsonToken.String: - return this.ReadString(JToken.Load(reader).Value(), path); + { + string? value = JToken.Load(reader).Value(); + return value is not null + ? this.ReadString(value, path) + : null; + } + default: throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} node (path: {reader.Path})."); } } + /// Writes the JSON representation of the object. + /// The JSON writer. + /// The value. + /// The calling serializer. + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + throw new InvalidOperationException("This converter does not write JSON."); + } + /********* ** Protected methods diff --git a/src/SMAPI.Toolkit/Serialization/InternalExtensions.cs b/src/SMAPI.Toolkit/Serialization/InternalExtensions.cs index 10f88dde..78297035 100644 --- a/src/SMAPI.Toolkit/Serialization/InternalExtensions.cs +++ b/src/SMAPI.Toolkit/Serialization/InternalExtensions.cs @@ -10,12 +10,12 @@ namespace StardewModdingAPI.Toolkit.Serialization /// The value type. /// The JSON object to search. /// The field name. - public static T ValueIgnoreCase(this JObject obj, string fieldName) + public static T? ValueIgnoreCase(this JObject obj, string fieldName) { - JToken token = obj.GetValue(fieldName, StringComparison.OrdinalIgnoreCase); + JToken? token = obj.GetValue(fieldName, StringComparison.OrdinalIgnoreCase); return token != null ? token.Value() - : default(T); + : default; } } } diff --git a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs index 031afbb0..a5d7e2e8 100644 --- a/src/SMAPI.Toolkit/Serialization/JsonHelper.cs +++ b/src/SMAPI.Toolkit/Serialization/JsonHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -14,21 +15,27 @@ namespace StardewModdingAPI.Toolkit.Serialization ** Accessors *********/ /// The JSON settings to use when serializing and deserializing files. - public JsonSerializerSettings JsonSettings { get; } = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection values are duplicated each time the config is loaded - Converters = new List - { - new SemanticVersionConverter(), - new StringEnumConverter() - } - }; + public JsonSerializerSettings JsonSettings { get; } = JsonHelper.CreateDefaultSettings(); /********* ** Public methods *********/ + /// Create an instance of the default JSON serializer settings. + public static JsonSerializerSettings CreateDefaultSettings() + { + return new() + { + Formatting = Formatting.Indented, + ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection values are duplicated each time the config is loaded + Converters = new List + { + new SemanticVersionConverter(), + new StringEnumConverter() + } + }; + } + /// Read a JSON file. /// The model type. /// The absolute file path. @@ -36,7 +43,12 @@ namespace StardewModdingAPI.Toolkit.Serialization /// Returns false if the file doesn't exist, else true. /// The given is empty or invalid. /// The file contains invalid JSON. - public bool ReadJsonFileIfExists(string fullPath, out TModel result) + public bool ReadJsonFileIfExists(string fullPath, +#if NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out TModel? result + ) { // validate if (string.IsNullOrWhiteSpace(fullPath)) @@ -48,9 +60,9 @@ namespace StardewModdingAPI.Toolkit.Serialization { json = File.ReadAllText(fullPath); } - catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException) + catch (Exception ex) when (ex is DirectoryNotFoundException or FileNotFoundException) { - result = default(TModel); + result = default; return false; } @@ -58,7 +70,7 @@ namespace StardewModdingAPI.Toolkit.Serialization try { result = this.Deserialize(json); - return true; + return result != null; } catch (Exception ex) { @@ -88,7 +100,7 @@ namespace StardewModdingAPI.Toolkit.Serialization throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath)); // create directory if needed - string dir = Path.GetDirectoryName(fullPath); + string dir = Path.GetDirectoryName(fullPath)!; if (dir == null) throw new ArgumentException("The file path is invalid.", nameof(fullPath)); if (!Directory.Exists(dir)) @@ -102,7 +114,7 @@ namespace StardewModdingAPI.Toolkit.Serialization /// Deserialize JSON text if possible. /// The model type. /// The raw JSON text. - public TModel Deserialize(string json) + public TModel? Deserialize(string json) { try { @@ -115,7 +127,8 @@ namespace StardewModdingAPI.Toolkit.Serialization { try { - return JsonConvert.DeserializeObject(json.Replace('“', '"').Replace('”', '"'), this.JsonSettings); + return JsonConvert.DeserializeObject(json.Replace('“', '"').Replace('”', '"'), this.JsonSettings) + ?? throw new InvalidOperationException($"Couldn't deserialize model type '{typeof(TModel)}' from empty or null JSON."); } catch { /* rethrow original error */ } } @@ -132,5 +145,11 @@ namespace StardewModdingAPI.Toolkit.Serialization { return JsonConvert.SerializeObject(model, formatting, this.JsonSettings); } + + /// Get a low-level JSON serializer matching the . + public JsonSerializer GetSerializer() + { + return JsonSerializer.CreateDefault(this.JsonSettings); + } } } diff --git a/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs b/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs index 99e85cbd..8a449f0a 100644 --- a/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs +++ b/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs @@ -1,4 +1,7 @@ +using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Text; using Newtonsoft.Json; using StardewModdingAPI.Toolkit.Serialization.Converters; @@ -11,48 +14,45 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models ** Accessors *********/ /// The mod name. - public string Name { get; set; } + public string Name { get; } /// A brief description of the mod. - public string Description { get; set; } + public string Description { get; } /// The mod author's name. - public string Author { get; set; } + public string Author { get; } /// The mod version. - public ISemanticVersion Version { get; set; } + public ISemanticVersion Version { get; } /// The minimum SMAPI version required by this mod, if any. - public ISemanticVersion MinimumApiVersion { get; set; } + public ISemanticVersion? MinimumApiVersion { get; } /// The name of the DLL in the directory that has the Entry method. Mutually exclusive with . - public string EntryDll { get; set; } + public string? EntryDll { get; } /// The mod which will read this as a content pack. Mutually exclusive with . [JsonConverter(typeof(ManifestContentPackForConverter))] - public IManifestContentPackFor ContentPackFor { get; set; } + public IManifestContentPackFor? ContentPackFor { get; } /// The other mods that must be loaded before this mod. [JsonConverter(typeof(ManifestDependencyArrayConverter))] - public IManifestDependency[] Dependencies { get; set; } + public IManifestDependency[] Dependencies { get; } /// The namespaced mod IDs to query for updates (like Nexus:541). - public string[] UpdateKeys { get; set; } + public string[] UpdateKeys { get; private set; } /// The unique mod ID. - public string UniqueID { get; set; } + public string UniqueID { get; } /// Any manifest fields which didn't match a valid field. [JsonExtensionData] - public IDictionary ExtraFields { get; set; } + public IDictionary ExtraFields { get; } = new Dictionary(); /********* ** Public methods *********/ - /// Construct an instance. - public Manifest() { } - /// Construct an instance for a transitional content pack. /// The unique mod ID. /// The mod name. @@ -60,15 +60,101 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models /// A brief description of the mod. /// The mod version. /// The modID which will read this as a content pack. - public Manifest(string uniqueID, string name, string author, string description, ISemanticVersion version, string contentPackFor = null) + public Manifest(string uniqueID, string name, string author, string description, ISemanticVersion version, string? contentPackFor = null) + : this( + uniqueId: uniqueID, + name: name, + author: author, + description: description, + version: version, + minimumApiVersion: null, + entryDll: null, + contentPackFor: contentPackFor != null + ? new ManifestContentPackFor(contentPackFor, null) + : null, + dependencies: null, + updateKeys: null + ) + { } + + /// Construct an instance for a transitional content pack. + /// The unique mod ID. + /// The mod name. + /// The mod author's name. + /// A brief description of the mod. + /// The mod version. + /// The minimum SMAPI version required by this mod, if any. + /// The name of the DLL in the directory that has the Entry method. Mutually exclusive with . + /// The modID which will read this as a content pack. + /// The other mods that must be loaded before this mod. + /// The namespaced mod IDs to query for updates (like Nexus:541). + [JsonConstructor] + public Manifest(string uniqueId, string name, string author, string description, ISemanticVersion version, ISemanticVersion? minimumApiVersion, string? entryDll, IManifestContentPackFor? contentPackFor, IManifestDependency[]? dependencies, string[]? updateKeys) { - this.Name = name; - this.Author = author; - this.Description = description; + this.UniqueID = this.NormalizeField(uniqueId); + this.Name = this.NormalizeField(name, replaceSquareBrackets: true); + this.Author = this.NormalizeField(author); + this.Description = this.NormalizeField(description); this.Version = version; - this.UniqueID = uniqueID; - this.UpdateKeys = new string[0]; - this.ContentPackFor = new ManifestContentPackFor { UniqueID = contentPackFor }; + this.MinimumApiVersion = minimumApiVersion; + this.EntryDll = this.NormalizeField(entryDll); + this.ContentPackFor = contentPackFor; + this.Dependencies = dependencies ?? Array.Empty(); + this.UpdateKeys = updateKeys ?? Array.Empty(); + } + + /// Override the update keys loaded from the mod info. + /// The new update keys to set. + internal void OverrideUpdateKeys(params string[] updateKeys) + { + this.UpdateKeys = updateKeys; + } + + + /********* + ** Private methods + *********/ + /// Normalize a manifest field to strip newlines, trim whitespace, and optionally strip square brackets. + /// The input to strip. + /// Whether to replace square brackets with round ones. This is used in the mod name to avoid breaking the log format. +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("input")] +#endif + private string? NormalizeField(string? input, bool replaceSquareBrackets = false) + { + input = input?.Trim(); + + if (!string.IsNullOrEmpty(input)) + { + StringBuilder? builder = null; + + for (int i = 0; i < input.Length; i++) + { + switch (input[i]) + { + case '\r': + case '\n': + builder ??= new StringBuilder(input); + builder[i] = ' '; + break; + + case '[' when replaceSquareBrackets: + builder ??= new StringBuilder(input); + builder[i] = '('; + break; + + case ']' when replaceSquareBrackets: + builder ??= new StringBuilder(input); + builder[i] = ')'; + break; + } + } + + if (builder != null) + input = builder.ToString(); + } + + return input; } } } diff --git a/src/SMAPI.Toolkit/Serialization/Models/ManifestContentPackFor.cs b/src/SMAPI.Toolkit/Serialization/Models/ManifestContentPackFor.cs index 1eb80889..f7dc8aa8 100644 --- a/src/SMAPI.Toolkit/Serialization/Models/ManifestContentPackFor.cs +++ b/src/SMAPI.Toolkit/Serialization/Models/ManifestContentPackFor.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + namespace StardewModdingAPI.Toolkit.Serialization.Models { /// Indicates which mod can read the content pack represented by the containing manifest. @@ -7,9 +9,36 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models ** Accessors *********/ /// The unique ID of the mod which can read this content pack. - public string UniqueID { get; set; } + public string UniqueID { get; } /// The minimum required version (if any). - public ISemanticVersion MinimumVersion { get; set; } + public ISemanticVersion? MinimumVersion { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The unique ID of the mod which can read this content pack. + /// The minimum required version (if any). + public ManifestContentPackFor(string uniqueId, ISemanticVersion? minimumVersion) + { + this.UniqueID = this.NormalizeWhitespace(uniqueId); + this.MinimumVersion = minimumVersion; + } + + + /********* + ** Private methods + *********/ + /// Normalize whitespace in a raw string. + /// The input to strip. +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("input")] +#endif + private string? NormalizeWhitespace(string? input) + { + return input?.Trim(); + } } } diff --git a/src/SMAPI.Toolkit/Serialization/Models/ManifestDependency.cs b/src/SMAPI.Toolkit/Serialization/Models/ManifestDependency.cs index 00f168f4..fa254ea7 100644 --- a/src/SMAPI.Toolkit/Serialization/Models/ManifestDependency.cs +++ b/src/SMAPI.Toolkit/Serialization/Models/ManifestDependency.cs @@ -1,3 +1,6 @@ +using System.Diagnostics.CodeAnalysis; +using Newtonsoft.Json; + namespace StardewModdingAPI.Toolkit.Serialization.Models { /// A mod dependency listed in a mod manifest. @@ -7,13 +10,13 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models ** Accessors *********/ /// The unique mod ID to require. - public string UniqueID { get; set; } + public string UniqueID { get; } /// The minimum required version (if any). - public ISemanticVersion MinimumVersion { get; set; } + public ISemanticVersion? MinimumVersion { get; } /// Whether the dependency must be installed to use the mod. - public bool IsRequired { get; set; } + public bool IsRequired { get; } /********* @@ -23,13 +26,40 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models /// The unique mod ID to require. /// The minimum required version (if any). /// Whether the dependency must be installed to use the mod. - public ManifestDependency(string uniqueID, string minimumVersion, bool required = true) + public ManifestDependency(string uniqueID, string? minimumVersion, bool required = true) + : this( + uniqueID: uniqueID, + minimumVersion: !string.IsNullOrWhiteSpace(minimumVersion) + ? new SemanticVersion(minimumVersion) + : null, + required: required + ) + { } + + /// Construct an instance. + /// The unique mod ID to require. + /// The minimum required version (if any). + /// Whether the dependency must be installed to use the mod. + [JsonConstructor] + public ManifestDependency(string uniqueID, ISemanticVersion? minimumVersion, bool required = true) { - this.UniqueID = uniqueID; - this.MinimumVersion = !string.IsNullOrWhiteSpace(minimumVersion) - ? new SemanticVersion(minimumVersion) - : null; + this.UniqueID = this.NormalizeWhitespace(uniqueID); + this.MinimumVersion = minimumVersion; this.IsRequired = required; } + + + /********* + ** Private methods + *********/ + /// Normalize whitespace in a raw string. + /// The input to strip. +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("input")] +#endif + private string? NormalizeWhitespace(string? input) + { + return input?.Trim(); + } } } diff --git a/src/SMAPI.Toolkit/Serialization/SParseException.cs b/src/SMAPI.Toolkit/Serialization/SParseException.cs index 5f58b5b8..c2b3f68e 100644 --- a/src/SMAPI.Toolkit/Serialization/SParseException.cs +++ b/src/SMAPI.Toolkit/Serialization/SParseException.cs @@ -11,7 +11,7 @@ namespace StardewModdingAPI.Toolkit.Serialization /// Construct an instance. /// The error message. /// The underlying exception, if any. - public SParseException(string message, Exception ex = null) + public SParseException(string message, Exception? ex = null) : base(message, ex) { } } } diff --git a/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs b/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs index 4ef578f7..1791c5b3 100644 --- a/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs +++ b/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Framework; namespace StardewModdingAPI.Toolkit.Utilities @@ -34,24 +33,16 @@ namespace StardewModdingAPI.Toolkit.Utilities /// Get the human-readable OS name and version. /// The current platform. - [SuppressMessage("ReSharper", "EmptyGeneralCatchClause", Justification = "Error suppressed deliberately to fallback to default behaviour.")] public static string GetFriendlyPlatformName(Platform platform) { return LowLevelEnvironmentUtility.GetFriendlyPlatformName(platform.ToString()); } - /// Get the name of the Stardew Valley executable. - /// The current platform. - public static string GetExecutableName(Platform platform) + /// Get whether an executable is 64-bit. + /// The absolute path to the assembly file. + public static bool Is64BitAssembly(string path) { - return LowLevelEnvironmentUtility.GetExecutableName(platform.ToString()); - } - - /// Get whether the platform uses Mono. - /// The current platform. - public static bool IsMono(this Platform platform) - { - return LowLevelEnvironmentUtility.IsMono(platform.ToString()); + return LowLevelEnvironmentUtility.Is64BitAssembly(path); } } } diff --git a/src/SMAPI.Toolkit/Utilities/FileUtilities.cs b/src/SMAPI.Toolkit/Utilities/FileUtilities.cs index 7856fdb1..a6bf5929 100644 --- a/src/SMAPI.Toolkit/Utilities/FileUtilities.cs +++ b/src/SMAPI.Toolkit/Utilities/FileUtilities.cs @@ -1,4 +1,6 @@ +using System; using System.IO; +using System.Security.Cryptography; using System.Threading; namespace StardewModdingAPI.Toolkit.Utilities @@ -42,5 +44,16 @@ namespace StardewModdingAPI.Toolkit.Utilities if (entry.Exists) throw new IOException($"Timed out trying to delete {entry.FullName}"); } + + /// Get the MD5 hash for a file. + /// The absolute file path. + public static string GetFileHash(string absolutePath) + { + using FileStream stream = File.OpenRead(absolutePath); + using MD5 md5 = MD5.Create(); + + byte[] hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } } } diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs new file mode 100644 index 00000000..496d54c3 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/CaseInsensitiveFileLookup.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for case-insensitive file lookups within a root directory. + internal class CaseInsensitiveFileLookup : IFileLookup + { + /********* + ** Fields + *********/ + /// The root directory path for relative paths. + private readonly string RootPath; + + /// A case-insensitive lookup of file paths within the . Each path is listed in both file path and asset name format, so it's usable in both contexts without needing to re-parse paths. + private readonly Lazy> RelativePathCache; + + /// The case-insensitive file lookups by root path. + private static readonly Dictionary CachedRoots = new(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + /// Which directories to scan from the root. + public CaseInsensitiveFileLookup(string rootPath, SearchOption searchOption = SearchOption.AllDirectories) + { + this.RootPath = PathUtilities.NormalizePath(rootPath); + this.RelativePathCache = new(() => this.GetRelativePathCache(searchOption)); + } + + /// + public FileInfo GetFile(string relativePath) + { + // invalid path + if (string.IsNullOrWhiteSpace(relativePath)) + throw new InvalidOperationException("Can't get a file from an empty relative path."); + + // already cached + if (this.RelativePathCache.Value.TryGetValue(relativePath, out string? resolved)) + return new(Path.Combine(this.RootPath, resolved)); + + // keep capitalization as-is + FileInfo file = new(Path.Combine(this.RootPath, relativePath)); + if (file.Exists) + this.RelativePathCache.Value[relativePath] = relativePath; + return file; + } + + /// + public void Add(string relativePath) + { + // skip if cache isn't created yet (no need to add files manually in that case) + if (!this.RelativePathCache.IsValueCreated) + return; + + // skip if already cached + if (this.RelativePathCache.Value.ContainsKey(relativePath)) + return; + + // make sure path exists + relativePath = PathUtilities.NormalizePath(relativePath); + if (!File.Exists(Path.Combine(this.RootPath, relativePath))) + throw new InvalidOperationException($"Can't add relative path '{relativePath}' to the case-insensitive cache for '{this.RootPath}' because that file doesn't exist."); + + // cache path + this.RelativePathCache.Value[relativePath] = relativePath; + } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + public static CaseInsensitiveFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!CaseInsensitiveFileLookup.CachedRoots.TryGetValue(rootPath, out CaseInsensitiveFileLookup? cache)) + CaseInsensitiveFileLookup.CachedRoots[rootPath] = cache = new CaseInsensitiveFileLookup(rootPath); + + return cache; + } + + + /********* + ** Private methods + *********/ + /// Get a case-insensitive lookup of file paths (see ). + /// Which directories to scan from the root. + private Dictionary GetRelativePathCache(SearchOption searchOption) + { + Dictionary cache = new(StringComparer.OrdinalIgnoreCase); + + foreach (string path in Directory.EnumerateFiles(this.RootPath, "*", searchOption)) + { + string relativePath = path.Substring(this.RootPath.Length + 1); + cache[relativePath] = relativePath; + } + + return cache; + } + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs new file mode 100644 index 00000000..d43b5141 --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/IFileLookup.cs @@ -0,0 +1,16 @@ +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for file lookups within a root directory. + internal interface IFileLookup + { + /// Get the file for a given relative file path, if it exists. + /// The relative path. + FileInfo GetFile(string relativePath); + + /// Add a relative path that was just created by a SMAPI API. + /// The relative path. + void Add(string relativePath); + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs new file mode 100644 index 00000000..414b569b --- /dev/null +++ b/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.IO; + +namespace StardewModdingAPI.Toolkit.Utilities.PathLookups +{ + /// An API for file lookups within a root directory with minimal preprocessing. + internal class MinimalFileLookup : IFileLookup + { + /********* + ** Accessors + *********/ + /// The file lookups by root path. + private static readonly Dictionary CachedRoots = new(); + + /// The root directory path for relative paths. + private readonly string RootPath; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The root directory path for relative paths. + public MinimalFileLookup(string rootPath) + { + this.RootPath = rootPath; + } + + /// + public FileInfo GetFile(string relativePath) + { + return new( + Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath)) + ); + } + + /// + public void Add(string relativePath) { } + + /// Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups. + /// The root path to scan. + public static MinimalFileLookup GetCachedFor(string rootPath) + { + rootPath = PathUtilities.NormalizePath(rootPath); + + if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup)) + MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath); + + return lookup; + } + } +} diff --git a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs index c9fb6213..136279f2 100644 --- a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs +++ b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.IO; using System.Linq; @@ -22,9 +23,12 @@ namespace StardewModdingAPI.Toolkit.Utilities /// The possible directory separator characters in a file path. public static readonly char[] PossiblePathSeparators = new[] { '/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Distinct().ToArray(); - /// The preferred directory separator character in an asset key. + /// The preferred directory separator character in a file path. public static readonly char PreferredPathSeparator = Path.DirectorySeparatorChar; + /// The preferred directory separator character in an asset key. + public static readonly char PreferredAssetSeparator = '/'; + /********* ** Public methods @@ -33,17 +37,39 @@ namespace StardewModdingAPI.Toolkit.Utilities /// The path to split. /// The number of segments to match. Any additional segments will be merged into the last returned part. [Pure] - public static string[] GetSegments(string path, int? limit = null) + public static string[] GetSegments(string? path, int? limit = null) { + if (path == null) + return Array.Empty(); + return limit.HasValue ? path.Split(PathUtilities.PossiblePathSeparators, limit.Value, StringSplitOptions.RemoveEmptyEntries) : path.Split(PathUtilities.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries); } - /// Normalize separators in a file path. - /// The file path to normalize. + /// Normalize an asset name to match how MonoGame's content APIs would normalize and cache it. + /// The asset name to normalize. [Pure] - public static string NormalizePath(string path) +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("assetName")] +#endif + public static string? NormalizeAssetName(string? assetName) + { + assetName = assetName?.Trim(); + if (string.IsNullOrEmpty(assetName)) + return assetName; + + return string.Join(PathUtilities.PreferredAssetSeparator.ToString(), PathUtilities.GetSegments(assetName)); // based on MonoGame's ContentManager.Load logic + } + + /// Normalize separators in a file path for the current platform. + /// The file path to normalize. + /// This should only be used for file paths. For asset names, use instead. + [Pure] +#if NET5_0_OR_GREATER + [return: NotNullIfNotNull("path")] +#endif + public static string? NormalizePath(string? path) { path = path?.Trim(); if (string.IsNullOrEmpty(path)) @@ -76,17 +102,21 @@ namespace StardewModdingAPI.Toolkit.Utilities /// Get a directory or file path relative to a given source path. If no relative path is possible (e.g. the paths are on different drives), an absolute path is returned. /// The source folder path. /// The target folder or file path. - /// - /// - /// NOTE: this is a heuristic implementation that works in the cases SMAPI needs it for, but it doesn't handle all edge cases (e.g. case-sensitivity on Linux, or traversing between UNC paths on Windows). This should be replaced with the more comprehensive Path.GetRelativePath if the game ever migrates to .NET Core. - /// - /// [Pure] public static string GetRelativePath(string sourceDir, string targetPath) { +#if NET5_0 + return Path.GetRelativePath(sourceDir, targetPath); +#else + // NOTE: + // this is a heuristic implementation that works in the cases SMAPI needs it for, but it + // doesn't handle all edge cases (e.g. case-sensitivity on Linux, or traversing between + // UNC paths on Windows). SMAPI and mods will use the more robust .NET 5 version anyway + // though, this is only for compatibility with the mod build package. + // convert to URIs - Uri from = new Uri(sourceDir.TrimEnd(PathUtilities.PossiblePathSeparators) + "/"); - Uri to = new Uri(targetPath.TrimEnd(PathUtilities.PossiblePathSeparators) + "/"); + Uri from = new(sourceDir.TrimEnd(PathUtilities.PossiblePathSeparators) + "/"); + Uri to = new(targetPath.TrimEnd(PathUtilities.PossiblePathSeparators) + "/"); if (from.Scheme != to.Scheme) throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{sourceDir}'."); @@ -111,12 +141,13 @@ namespace StardewModdingAPI.Toolkit.Utilities } return relative; +#endif } /// Get whether a path is relative and doesn't try to climb out of its containing folder (e.g. doesn't contain ../). /// The path to check. [Pure] - public static bool IsSafeRelativePath(string path) + public static bool IsSafeRelativePath(string? path) { if (string.IsNullOrWhiteSpace(path)) return true; @@ -129,9 +160,11 @@ namespace StardewModdingAPI.Toolkit.Utilities /// Get whether a string is a valid 'slug', containing only basic characters that are safe in all contexts (e.g. filenames, URLs, etc). /// The string to check. [Pure] - public static bool IsSlug(string str) + public static bool IsSlug(string? str) { - return !Regex.IsMatch(str, "[^a-z0-9_.-]", RegexOptions.IgnoreCase); + return + string.IsNullOrWhiteSpace(str) + || !Regex.IsMatch(str, "[^a-z0-9_.-]", RegexOptions.IgnoreCase); } } } diff --git a/src/SMAPI.Toolkit/Utilities/Platform.cs b/src/SMAPI.Toolkit/Utilities/Platform.cs index f780e812..563d3250 100644 --- a/src/SMAPI.Toolkit/Utilities/Platform.cs +++ b/src/SMAPI.Toolkit/Utilities/Platform.cs @@ -9,7 +9,7 @@ namespace StardewModdingAPI.Toolkit.Utilities /// The Linux version of the game. Linux, - /// The Mac version of the game. + /// The macOS version of the game. Mac, /// The Windows version of the game. diff --git a/src/SMAPI.Web/BackgroundService.cs b/src/SMAPI.Web/BackgroundService.cs index 64bd5ca5..49356f76 100644 --- a/src/SMAPI.Web/BackgroundService.cs +++ b/src/SMAPI.Web/BackgroundService.cs @@ -19,13 +19,17 @@ namespace StardewModdingAPI.Web ** Fields *********/ /// The background task server. - private static BackgroundJobServer JobServer; + private static BackgroundJobServer? JobServer; /// The cache in which to store wiki metadata. - private static IWikiCacheRepository WikiCache; + private static IWikiCacheRepository? WikiCache; /// The cache in which to store mod data. - private static IModCacheRepository ModCache; + private static IModCacheRepository? ModCache; + + /// Whether the service has been started. + [MemberNotNullWhen(true, nameof(BackgroundService.JobServer), nameof(BackgroundService.WikiCache), nameof(BackgroundService.ModCache))] + private static bool IsStarted { get; set; } /********* @@ -59,6 +63,8 @@ namespace StardewModdingAPI.Web RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *"); // every 10 minutes RecurringJob.AddOrUpdate(() => BackgroundService.RemoveStaleModsAsync(), "0 * * * *"); // hourly + BackgroundService.IsStarted = true; + return Task.CompletedTask; } @@ -66,6 +72,8 @@ namespace StardewModdingAPI.Web /// Tracks whether the shutdown process should no longer be graceful. public async Task StopAsync(CancellationToken cancellationToken) { + BackgroundService.IsStarted = false; + if (BackgroundService.JobServer != null) await BackgroundService.JobServer.WaitForShutdownAsync(cancellationToken); } @@ -73,6 +81,8 @@ namespace StardewModdingAPI.Web /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { + BackgroundService.IsStarted = false; + BackgroundService.JobServer?.Dispose(); } @@ -83,6 +93,9 @@ namespace StardewModdingAPI.Web [AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 30, 60, 120 })] public static async Task UpdateWikiAsync() { + if (!BackgroundService.IsStarted) + throw new InvalidOperationException($"Must call {nameof(BackgroundService.StartAsync)} before scheduling tasks."); + WikiModList wikiCompatList = await new ModToolkit().GetWikiCompatibilityListAsync(); BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods); } @@ -90,6 +103,9 @@ namespace StardewModdingAPI.Web /// Remove mods which haven't been requested in over 48 hours. public static Task RemoveStaleModsAsync() { + if (!BackgroundService.IsStarted) + throw new InvalidOperationException($"Must call {nameof(BackgroundService.StartAsync)} before scheduling tasks."); + BackgroundService.ModCache.RemoveStaleMods(TimeSpan.FromHours(48)); return Task.CompletedTask; } diff --git a/src/SMAPI.Web/Controllers/IndexController.cs b/src/SMAPI.Web/Controllers/IndexController.cs index 080285ab..522d77cd 100644 --- a/src/SMAPI.Web/Controllers/IndexController.cs +++ b/src/SMAPI.Web/Controllers/IndexController.cs @@ -57,21 +57,16 @@ namespace StardewModdingAPI.Web.Controllers { // choose versions ReleaseVersion[] versions = await this.GetReleaseVersionsAsync(); - ReleaseVersion stableVersion = versions.LastOrDefault(version => !version.IsBeta && !version.IsForDevs); - ReleaseVersion stableVersionForDevs = versions.LastOrDefault(version => !version.IsBeta && version.IsForDevs); - ReleaseVersion betaVersion = versions.LastOrDefault(version => version.IsBeta && !version.IsForDevs); - ReleaseVersion betaVersionForDevs = versions.LastOrDefault(version => version.IsBeta && version.IsForDevs); + ReleaseVersion? stableVersion = versions.LastOrDefault(version => !version.IsForDevs); + ReleaseVersion? stableVersionForDevs = versions.LastOrDefault(version => version.IsForDevs); // render view IndexVersionModel stableVersionModel = stableVersion != null ? new IndexVersionModel(stableVersion.Version.ToString(), stableVersion.Release.Body, stableVersion.Asset.DownloadUrl, stableVersionForDevs?.Asset.DownloadUrl) - : new IndexVersionModel("unknown", "", "https://github.com/Pathoschild/SMAPI/releases", null); // just in case something goes wrong) - IndexVersionModel betaVersionModel = betaVersion != null && this.SiteConfig.BetaEnabled - ? new IndexVersionModel(betaVersion.Version.ToString(), betaVersion.Release.Body, betaVersion.Asset.DownloadUrl, betaVersionForDevs?.Asset.DownloadUrl) - : null; + : new IndexVersionModel("unknown", "", "https://github.com/Pathoschild/SMAPI/releases", null); // just in case something goes wrong // render view - var model = new IndexModel(stableVersionModel, betaVersionModel, this.SiteConfig.BetaBlurb, this.SiteConfig.SupporterList); + var model = new IndexModel(stableVersionModel, this.SiteConfig.OtherBlurb, this.SiteConfig.SupporterList); return this.View(model); } @@ -93,39 +88,22 @@ namespace StardewModdingAPI.Web.Controllers { entry.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(this.CacheTime); - // get latest release (whether preview or stable) - GitRelease stableRelease = await this.GitHub.GetLatestReleaseAsync(this.RepositoryName, includePrerelease: true); + // get latest stable release + GitRelease? release = await this.GitHub.GetLatestReleaseAsync(this.RepositoryName, includePrerelease: false); - // split stable/prerelease if applicable - GitRelease betaRelease = null; - if (stableRelease.IsPrerelease) + // strip 'noinclude' blocks from release description + if (release != null) { - GitRelease result = await this.GitHub.GetLatestReleaseAsync(this.RepositoryName, includePrerelease: false); - if (result != null) - { - betaRelease = stableRelease; - stableRelease = result; - } - } - - // strip 'noinclude' blocks from release descriptions - foreach (GitRelease release in new[] { stableRelease, betaRelease }) - { - if (release == null) - continue; - - HtmlDocument doc = new HtmlDocument(); + HtmlDocument doc = new(); doc.LoadHtml(release.Body); - foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//*[@class='noinclude']")?.ToArray() ?? new HtmlNode[0]) + foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//*[@class='noinclude']")?.ToArray() ?? Array.Empty()) node.Remove(); release.Body = doc.DocumentNode.InnerHtml.Trim(); } // get versions - ReleaseVersion[] stableVersions = this.ParseReleaseVersions(stableRelease).ToArray(); - ReleaseVersion[] betaVersions = this.ParseReleaseVersions(betaRelease).ToArray(); - return stableVersions - .Concat(betaVersions) + return this + .ParseReleaseVersions(release) .OrderBy(p => p.Version) .ToArray(); }); @@ -133,7 +111,7 @@ namespace StardewModdingAPI.Web.Controllers /// Get a parsed list of SMAPI downloads for a release. /// The GitHub release. - private IEnumerable ParseReleaseVersions(GitRelease release) + private IEnumerable ParseReleaseVersions(GitRelease? release) { if (release?.Assets == null) yield break; @@ -144,12 +122,11 @@ namespace StardewModdingAPI.Web.Controllers continue; Match match = Regex.Match(asset.FileName, @"SMAPI-(?[\d\.]+(?:-.+)?)-installer(?-for-developers)?.zip"); - if (!match.Success || !SemanticVersion.TryParse(match.Groups["version"].Value, out ISemanticVersion version)) + if (!match.Success || !SemanticVersion.TryParse(match.Groups["version"].Value, out ISemanticVersion? version)) continue; - bool isBeta = version.IsPrerelease(); bool isForDevs = match.Groups["forDevs"].Success; - yield return new ReleaseVersion(release, asset, version, isBeta, isForDevs); + yield return new ReleaseVersion(release, asset, version, isForDevs); } } @@ -168,9 +145,6 @@ namespace StardewModdingAPI.Web.Controllers /// The SMAPI version. public ISemanticVersion Version { get; } - /// Whether this is a beta download. - public bool IsBeta { get; } - /// Whether this is a 'for developers' download. public bool IsForDevs { get; } @@ -182,14 +156,12 @@ namespace StardewModdingAPI.Web.Controllers /// The underlying GitHub release. /// The underlying download asset. /// The SMAPI version. - /// Whether this is a beta download. /// Whether this is a 'for developers' download. - public ReleaseVersion(GitRelease release, GitAsset asset, ISemanticVersion version, bool isBeta, bool isForDevs) + public ReleaseVersion(GitRelease release, GitAsset asset, ISemanticVersion version, bool isForDevs) { this.Release = release; this.Asset = asset; this.Version = version; - this.IsBeta = isBeta; this.IsForDevs = isForDevs; } } diff --git a/src/SMAPI.Web/Controllers/JsonValidatorController.cs b/src/SMAPI.Web/Controllers/JsonValidatorController.cs index c77a3036..c551a805 100644 --- a/src/SMAPI.Web/Controllers/JsonValidatorController.cs +++ b/src/SMAPI.Web/Controllers/JsonValidatorController.cs @@ -64,7 +64,7 @@ namespace StardewModdingAPI.Web.Controllers [Route("json/{schemaName}")] [Route("json/{schemaName}/{id}")] [Route("json/{schemaName}/{id}/{operation}")] - public async Task Index(string schemaName = null, string id = null, string operation = null) + public async Task Index(string? schemaName = null, string? id = null, string? operation = null) { // parse arguments schemaName = this.NormalizeSchemaName(schemaName); @@ -79,7 +79,7 @@ namespace StardewModdingAPI.Web.Controllers return this.View("Index", result); // fetch raw JSON - StoredFileInfo file = await this.Storage.GetAsync(id, renew); + StoredFileInfo file = await this.Storage.GetAsync(id!, renew); if (string.IsNullOrWhiteSpace(file.Content)) return this.View("Index", result.SetUploadError("The JSON file seems to be empty.")); result.SetContent(file.Content, expiry: file.Expiry, uploadWarning: file.Warning); @@ -90,21 +90,27 @@ namespace StardewModdingAPI.Web.Controllers // parse JSON JToken parsed; - try { - parsed = JToken.Parse(file.Content, new JsonLoadSettings + // load raw JSON + var settings = new JsonLoadSettings { DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error, CommentHandling = CommentHandling.Load - }); - } - catch (JsonReaderException ex) - { - return this.View("Index", result.AddErrors(new JsonValidatorErrorModel(ex.LineNumber, ex.Path, ex.Message, ErrorType.None))); - } + }; + try + { + parsed = JToken.Parse(file.Content, settings); + } + catch (JsonReaderException ex) + { + return this.View("Index", result.AddErrors(new JsonValidatorErrorModel(ex.LineNumber, ex.Path!, ex.Message, ErrorType.None))); + } - // format JSON - result.SetContent(parsed.ToString(Formatting.Indented), expiry: file.Expiry, uploadWarning: file.Warning); + // format JSON + string formatted = parsed.ToString(Formatting.Indented); + result.SetContent(formatted, expiry: file.Expiry, uploadWarning: file.Warning); + parsed = JToken.Parse(formatted); // update line number references + } // skip if no schema selected if (schemaName == "none") @@ -113,10 +119,10 @@ namespace StardewModdingAPI.Web.Controllers // load schema JSchema schema; { - FileInfo schemaFile = this.FindSchemaFile(schemaName); + FileInfo? schemaFile = this.FindSchemaFile(schemaName); if (schemaFile == null) return this.View("Index", result.SetParseError($"Invalid schema '{schemaName}'.")); - schema = JSchema.Parse(System.IO.File.ReadAllText(schemaFile.FullName)); + schema = JSchema.Parse(await System.IO.File.ReadAllTextAsync(schemaFile.FullName)); } // get format doc URL @@ -136,7 +142,7 @@ namespace StardewModdingAPI.Web.Controllers /// Save raw JSON data. [HttpPost, AllowLargePosts] [Route("json")] - public async Task PostAsync(JsonValidatorRequestModel request) + public async Task PostAsync(JsonValidatorRequestModel? request) { if (request == null) return this.View("Index", this.GetModel(null, null, isEditView: true).SetUploadError("The request seems to be invalid.")); @@ -145,7 +151,7 @@ namespace StardewModdingAPI.Web.Controllers string schemaName = this.NormalizeSchemaName(request.SchemaName); // get raw text - string input = request.Content; + string? input = request.Content; if (string.IsNullOrWhiteSpace(input)) return this.View("Index", this.GetModel(null, schemaName, isEditView: true).SetUploadError("The JSON file seems to be empty.")); @@ -155,7 +161,7 @@ namespace StardewModdingAPI.Web.Controllers return this.View("Index", this.GetModel(result.ID, schemaName, isEditView: true).SetContent(input, null).SetUploadError(result.UploadError)); // redirect to view - return this.Redirect(this.Url.PlainAction("Index", "JsonValidator", new { schemaName = schemaName, id = result.ID })); + return this.Redirect(this.Url.PlainAction("Index", "JsonValidator", new { schemaName, id = result.ID })!); } @@ -166,14 +172,14 @@ namespace StardewModdingAPI.Web.Controllers /// The stored file ID. /// The schema name with which the JSON was validated. /// Whether to show the edit view. - private JsonValidatorModel GetModel(string pasteID, string schemaName, bool isEditView) + private JsonValidatorModel GetModel(string? pasteID, string? schemaName, bool isEditView) { return new JsonValidatorModel(pasteID, schemaName, this.SchemaFormats, isEditView); } /// Get a normalized schema name, or the if blank. /// The raw schema name to normalize. - private string NormalizeSchemaName(string schemaName) + private string NormalizeSchemaName(string? schemaName) { schemaName = schemaName?.Trim().ToLower(); return !string.IsNullOrWhiteSpace(schemaName) @@ -183,7 +189,7 @@ namespace StardewModdingAPI.Web.Controllers /// Get the schema file given its unique ID. /// The schema ID. - private FileInfo FindSchemaFile(string id) + private FileInfo? FindSchemaFile(string? id) { // normalize ID id = id?.Trim().ToLower(); @@ -191,7 +197,7 @@ namespace StardewModdingAPI.Web.Controllers return null; // get matching file - DirectoryInfo schemaDir = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schemas")); + DirectoryInfo schemaDir = new(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schemas")); foreach (FileInfo file in schemaDir.EnumerateFiles("*.json")) { if (file.Name.Equals($"{id}.json")) @@ -208,13 +214,13 @@ namespace StardewModdingAPI.Web.Controllers // skip through transparent errors if (this.IsTransparentError(error)) { - foreach (var model in error.ChildErrors.SelectMany(this.GetErrorModels)) + foreach (JsonValidatorErrorModel model in error.ChildErrors.SelectMany(this.GetErrorModels)) yield return model; yield break; } // get message - string message = this.GetOverrideError(error); + string? message = this.GetOverrideError(error); if (message == null || message == this.TransparentToken) message = this.FlattenErrorMessage(error); @@ -228,7 +234,7 @@ namespace StardewModdingAPI.Web.Controllers private string FlattenErrorMessage(ValidationError error, int indent = 0) { // get override - string message = this.GetOverrideError(error); + string? message = this.GetOverrideError(error); if (message != null && message != this.TransparentToken) return message; @@ -249,7 +255,7 @@ namespace StardewModdingAPI.Web.Controllers break; case ErrorType.Required: - message = $"Missing required fields: {string.Join(", ", (List)error.Value)}."; + message = $"Missing required fields: {string.Join(", ", (List)error.Value!)}."; break; } @@ -266,7 +272,7 @@ namespace StardewModdingAPI.Web.Controllers if (!error.ChildErrors.Any()) return false; - string @override = this.GetOverrideError(error); + string? @override = this.GetOverrideError(error); return @override == this.TransparentToken || (error.ErrorType == ErrorType.Then && @override == null); @@ -274,18 +280,18 @@ namespace StardewModdingAPI.Web.Controllers /// Get an override error from the JSON schema, if any. /// The schema validation error. - private string GetOverrideError(ValidationError error) + private string? GetOverrideError(ValidationError error) { - string GetRawOverrideError() + string? GetRawOverrideError() { // get override errors - IDictionary errors = this.GetExtensionField>(error.Schema, "@errorMessages"); + IDictionary? errors = this.GetExtensionField>(error.Schema, "@errorMessages"); if (errors == null) return null; - errors = new Dictionary(errors, StringComparer.OrdinalIgnoreCase); + errors = new Dictionary(errors, StringComparer.OrdinalIgnoreCase); // match error by type and message - foreach ((string target, string errorMessage) in errors) + foreach ((string target, string? errorMessage) in errors) { if (!target.Contains(":")) continue; @@ -296,7 +302,7 @@ namespace StardewModdingAPI.Web.Controllers } // match by type - return errors.TryGetValue(error.ErrorType.ToString(), out string message) + return errors.TryGetValue(error.ErrorType.ToString(), out string? message) ? message?.Trim() : null; } @@ -309,15 +315,12 @@ namespace StardewModdingAPI.Web.Controllers /// The field type. /// The schema whose extension fields to search. /// The case-insensitive field key. - private T GetExtensionField(JSchema schema, string key) + private T? GetExtensionField(JSchema schema, string key) { - if (schema.ExtensionData != null) + foreach ((string curKey, JToken value) in schema.ExtensionData) { - foreach ((string curKey, JToken value) in schema.ExtensionData) - { - if (curKey.Equals(key, StringComparison.OrdinalIgnoreCase)) - return value.ToObject(); - } + if (curKey.Equals(key, StringComparison.OrdinalIgnoreCase)) + return value.ToObject(); } return default; @@ -325,7 +328,7 @@ namespace StardewModdingAPI.Web.Controllers /// Format a schema value for display. /// The value to format. - private string FormatValue(object value) + private string FormatValue(object? value) { return value switch { diff --git a/src/SMAPI.Web/Controllers/LogParserController.cs b/src/SMAPI.Web/Controllers/LogParserController.cs index 39de4b5d..a3bcf4c3 100644 --- a/src/SMAPI.Web/Controllers/LogParserController.cs +++ b/src/SMAPI.Web/Controllers/LogParserController.cs @@ -1,6 +1,9 @@ using System; -using System.Linq; +using System.Collections.Specialized; +using System.IO; +using System.Text; using System.Threading.Tasks; +using System.Web; using Microsoft.AspNetCore.Mvc; using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework; @@ -39,24 +42,42 @@ namespace StardewModdingAPI.Web.Controllers ***/ /// Render the log parser UI. /// The stored file ID. - /// Whether to display the raw unparsed log. + /// How to render the log view. /// Whether to reset the log expiry. [HttpGet] [Route("log")] [Route("log/{id}")] - public async Task Index(string id = null, bool raw = false, bool renew = false) + public async Task Index(string? id = null, LogViewFormat format = LogViewFormat.Default, bool renew = false) { // fresh page if (string.IsNullOrWhiteSpace(id)) return this.View("Index", this.GetModel(id)); - // log page + // fetch log StoredFileInfo file = await this.Storage.GetAsync(id, renew); - ParsedLog log = file.Success - ? new LogParser().Parse(file.Content) - : new ParsedLog { IsValid = false, Error = file.Error }; - return this.View("Index", this.GetModel(id, uploadWarning: file.Warning, expiry: file.Expiry).SetResult(log, raw)); + // render view + switch (format) + { + case LogViewFormat.Default: + case LogViewFormat.RawView: + { + ParsedLog log = file.Success + ? new LogParser().Parse(file.Content) + : new ParsedLog { IsValid = false, Error = file.Error }; + + return this.View("Index", this.GetModel(id, uploadWarning: file.Warning, expiry: file.Expiry).SetResult(log, showRaw: format == LogViewFormat.RawView)); + } + + case LogViewFormat.RawDownload: + { + string content = file.Error ?? file.Content ?? string.Empty; + return this.File(Encoding.UTF8.GetBytes(content), "plain/text", $"SMAPI log ({id}).txt"); + } + + default: + throw new InvalidOperationException($"Unknown log view format '{format}'."); + } } /*** @@ -68,9 +89,15 @@ namespace StardewModdingAPI.Web.Controllers public async Task PostAsync() { // get raw log text - string input = this.Request.Form["input"].FirstOrDefault(); - if (string.IsNullOrWhiteSpace(input)) - return this.View("Index", this.GetModel(null, uploadError: "The log file seems to be empty.")); + // note: avoid this.Request.Form, which fails if any mod logged a null character. + string? input; + { + using StreamReader reader = new StreamReader(this.Request.Body); + NameValueCollection parsed = HttpUtility.ParseQueryString(await reader.ReadToEndAsync()); + input = parsed["input"]; + if (string.IsNullOrWhiteSpace(input)) + return this.View("Index", this.GetModel(null, uploadError: "The log file seems to be empty.")); + } // upload log UploadResult uploadResult = await this.Storage.SaveAsync(input); @@ -78,7 +105,7 @@ namespace StardewModdingAPI.Web.Controllers return this.View("Index", this.GetModel(null, uploadError: uploadResult.UploadError)); // redirect to view - return this.Redirect(this.Url.PlainAction("Index", "LogParser", new { id = uploadResult.ID })); + return this.Redirect(this.Url.PlainAction("Index", "LogParser", new { id = uploadResult.ID })!); } @@ -90,7 +117,7 @@ namespace StardewModdingAPI.Web.Controllers /// When the uploaded file will no longer be available. /// A non-blocking warning while uploading the log. /// An error which occurred while uploading the log. - private LogParserModel GetModel(string pasteID, DateTime? expiry = null, string uploadWarning = null, string uploadError = null) + private LogParserModel GetModel(string? pasteID, DateTimeOffset? expiry = null, string? uploadWarning = null, string? uploadError = null) { Platform? platform = this.DetectClientPlatform(); diff --git a/src/SMAPI.Web/Controllers/ModsApiController.cs b/src/SMAPI.Web/Controllers/ModsApiController.cs index 1956bf29..71fb42c2 100644 --- a/src/SMAPI.Web/Controllers/ModsApiController.cs +++ b/src/SMAPI.Web/Controllers/ModsApiController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -76,10 +77,12 @@ namespace StardewModdingAPI.Web.Controllers /// The mod search criteria. /// The requested API version. [HttpPost] - public async Task> PostAsync([FromBody] ModSearchModel model, [FromRoute] string version) + public async Task> PostAsync([FromBody] ModSearchModel? model, [FromRoute] string version) { if (model?.Mods == null) - return new ModEntryModel[0]; + return Array.Empty(); + + ModUpdateCheckConfig config = this.Config.Value; // fetch wiki data WikiModEntry[] wikiData = this.WikiCache.GetWikiMods().Select(p => p.Data).ToArray(); @@ -89,12 +92,17 @@ namespace StardewModdingAPI.Web.Controllers if (string.IsNullOrWhiteSpace(mod.ID)) continue; + // special case: if this is an update check for the official SMAPI repo, check the Nexus mod page for beta versions + if (mod.ID == config.SmapiInfo.ID && mod.UpdateKeys.Any(key => key == config.SmapiInfo.DefaultUpdateKey) && mod.InstalledVersion?.IsPrerelease() == true) + mod.AddUpdateKeys(config.SmapiInfo.AddBetaUpdateKeys); + + // fetch result ModEntryModel result = await this.GetModData(mod, wikiData, model.IncludeExtendedMetadata, model.ApiVersion); if (!model.IncludeExtendedMetadata && (model.ApiVersion == null || mod.InstalledVersion == null)) { - var errors = new List(result.Errors); - errors.Add($"This API can't suggest an update because {nameof(model.ApiVersion)} or {nameof(mod.InstalledVersion)} are null, and you didn't specify {nameof(model.IncludeExtendedMetadata)} to get other info. See the SMAPI technical docs for usage."); - result.Errors = errors.ToArray(); + result.Errors = result.Errors + .Concat(new[] { $"This API can't suggest an update because {nameof(model.ApiVersion)} or {nameof(mod.InstalledVersion)} are null, and you didn't specify {nameof(model.IncludeExtendedMetadata)} to get other info. See the SMAPI technical docs for usage." }) + .ToArray(); } mods[mod.ID] = result; @@ -114,22 +122,26 @@ namespace StardewModdingAPI.Web.Controllers /// Whether to include extended metadata for each mod. /// The SMAPI version installed by the player. /// Returns the mod data if found, else null. - private async Task GetModData(ModSearchEntryModel search, WikiModEntry[] wikiData, bool includeExtendedMetadata, ISemanticVersion apiVersion) + private async Task GetModData(ModSearchEntryModel search, WikiModEntry[] wikiData, bool includeExtendedMetadata, ISemanticVersion? apiVersion) { // cross-reference data - ModDataRecord record = this.ModDatabase.Get(search.ID); - WikiModEntry wikiEntry = wikiData.FirstOrDefault(entry => entry.ID.Contains(search.ID.Trim(), StringComparer.OrdinalIgnoreCase)); + ModDataRecord? record = this.ModDatabase.Get(search.ID); + WikiModEntry? wikiEntry = wikiData.FirstOrDefault(entry => entry.ID.Contains(search.ID.Trim(), StringComparer.OrdinalIgnoreCase)); UpdateKey[] updateKeys = this.GetUpdateKeys(search.UpdateKeys, record, wikiEntry).ToArray(); - ModOverrideConfig overrides = this.Config.Value.ModOverrides.FirstOrDefault(p => p.ID.Equals(search.ID?.Trim(), StringComparison.OrdinalIgnoreCase)); + ModOverrideConfig? overrides = this.Config.Value.ModOverrides.FirstOrDefault(p => p.ID.Equals(search.ID.Trim(), StringComparison.OrdinalIgnoreCase)); bool allowNonStandardVersions = overrides?.AllowNonStandardVersions ?? false; + // SMAPI versions with a '-beta' tag indicate major changes that may need beta mod versions. + // This doesn't apply to normal prerelease versions which have an '-alpha' tag. + bool isSmapiBeta = apiVersion != null && apiVersion.IsPrerelease() && apiVersion.PrereleaseTag.StartsWith("beta"); + // get latest versions - ModEntryModel result = new ModEntryModel { ID = search.ID }; + ModEntryModel result = new(search.ID); IList errors = new List(); - ModEntryVersionModel main = null; - ModEntryVersionModel optional = null; - ModEntryVersionModel unofficial = null; - ModEntryVersionModel unofficialForBeta = null; + ModEntryVersionModel? main = null; + ModEntryVersionModel? optional = null; + ModEntryVersionModel? unofficial = null; + ModEntryVersionModel? unofficialForBeta = null; foreach (UpdateKey updateKey in updateKeys) { // validate update key @@ -140,18 +152,27 @@ namespace StardewModdingAPI.Web.Controllers } // fetch data - ModInfoModel data = await this.GetInfoForUpdateKeyAsync(updateKey, allowNonStandardVersions, wikiEntry?.MapRemoteVersions); + ModInfoModel data = await this.GetInfoForUpdateKeyAsync(updateKey, allowNonStandardVersions, wikiEntry?.Overrides?.ChangeRemoteVersions); if (data.Status != RemoteModStatus.Ok) { errors.Add(data.Error ?? data.Status.ToString()); continue; } + // if there's only a prerelease version (e.g. from GitHub), don't override the main version + ISemanticVersion? curMain = data.Version; + ISemanticVersion? curPreview = data.PreviewVersion; + if (curPreview == null && curMain?.IsPrerelease() == true) + { + curPreview = curMain; + curMain = null; + } + // handle versions - if (this.IsNewer(data.Version, main?.Version)) - main = new ModEntryVersionModel(data.Version, data.Url); - if (this.IsNewer(data.PreviewVersion, optional?.Version)) - optional = new ModEntryVersionModel(data.PreviewVersion, data.Url); + if (this.IsNewer(curMain, main?.Version)) + main = new ModEntryVersionModel(curMain, data.Url!); + if (this.IsNewer(curPreview, optional?.Version)) + optional = new ModEntryVersionModel(curPreview, data.Url!); } // get unofficial version @@ -159,7 +180,7 @@ namespace StardewModdingAPI.Web.Controllers unofficial = new ModEntryVersionModel(wikiEntry.Compatibility.UnofficialVersion, $"{this.Url.PlainAction("Index", "Mods", absoluteUrl: true)}#{wikiEntry.Anchor}"); // get unofficial version for beta - if (wikiEntry?.HasBetaInfo == true) + if (wikiEntry is { HasBetaInfo: true }) { if (wikiEntry.BetaCompatibility.Status == WikiCompatibilityStatus.Unofficial) { @@ -185,20 +206,20 @@ namespace StardewModdingAPI.Web.Controllers if (overrides?.SetUrl != null) { if (main != null) - main.Url = overrides.SetUrl; + main = new(main.Version, overrides.SetUrl); if (optional != null) - optional.Url = overrides.SetUrl; + optional = new(optional.Version, overrides.SetUrl); } // get recommended update (if any) - ISemanticVersion installedVersion = this.ModSites.GetMappedVersion(search.InstalledVersion?.ToString(), wikiEntry?.MapLocalVersions, allowNonStandard: allowNonStandardVersions); + ISemanticVersion? installedVersion = this.ModSites.GetMappedVersion(search.InstalledVersion?.ToString(), wikiEntry?.Overrides?.ChangeLocalVersions, allowNonStandard: allowNonStandardVersions); if (apiVersion != null && installedVersion != null) { // get newer versions List updates = new List(); if (this.IsRecommendedUpdate(installedVersion, main?.Version, useBetaChannel: true)) updates.Add(main); - if (this.IsRecommendedUpdate(installedVersion, optional?.Version, useBetaChannel: installedVersion.IsPrerelease() || search.IsBroken)) + if (this.IsRecommendedUpdate(installedVersion, optional?.Version, useBetaChannel: isSmapiBeta || installedVersion.IsPrerelease() || search.IsBroken)) updates.Add(optional); if (this.IsRecommendedUpdate(installedVersion, unofficial?.Version, useBetaChannel: true)) updates.Add(unofficial); @@ -206,7 +227,7 @@ namespace StardewModdingAPI.Web.Controllers updates.Add(unofficialForBeta); // get newest version - ModEntryVersionModel newest = null; + ModEntryVersionModel? newest = null; foreach (ModEntryVersionModel update in updates) { if (newest == null || update.Version.IsNewerThan(newest.Version)) @@ -232,7 +253,7 @@ namespace StardewModdingAPI.Web.Controllers /// The current semantic version. /// The target semantic version. /// Whether the user enabled the beta channel and should be offered prerelease updates. - private bool IsRecommendedUpdate(ISemanticVersion currentVersion, ISemanticVersion newVersion, bool useBetaChannel) + private bool IsRecommendedUpdate(ISemanticVersion currentVersion, [NotNullWhen(true)] ISemanticVersion? newVersion, bool useBetaChannel) { return newVersion != null @@ -243,7 +264,7 @@ namespace StardewModdingAPI.Web.Controllers /// Get whether a version is newer than an version. /// The current version. /// The other version. - private bool IsNewer(ISemanticVersion current, ISemanticVersion other) + private bool IsNewer([NotNullWhen(true)] ISemanticVersion? current, ISemanticVersion? other) { return current != null && (other == null || other.IsOlderThan(current)); } @@ -251,18 +272,21 @@ namespace StardewModdingAPI.Web.Controllers /// Get the mod info for an update key. /// The namespaced update key. /// Whether to allow non-standard versions. - /// Maps remote versions to a semantic version for update checks. - private async Task GetInfoForUpdateKeyAsync(UpdateKey updateKey, bool allowNonStandardVersions, IDictionary mapRemoteVersions) + /// The changes to apply to remote versions for update checks. + private async Task GetInfoForUpdateKeyAsync(UpdateKey updateKey, bool allowNonStandardVersions, ChangeDescriptor? mapRemoteVersions) { + if (!updateKey.LooksValid) + return new ModInfoModel().SetError(RemoteModStatus.DoesNotExist, $"Invalid update key '{updateKey}'."); + // get mod page IModPage page; { bool isCached = - this.ModCache.TryGetMod(updateKey.Site, updateKey.ID, out Cached cachedMod) + this.ModCache.TryGetMod(updateKey.Site, updateKey.ID, out Cached? cachedMod) && !this.ModCache.IsStale(cachedMod.LastUpdated, cachedMod.Data.Status == RemoteModStatus.TemporaryError ? this.Config.Value.ErrorCacheMinutes : this.Config.Value.SuccessCacheMinutes); if (isCached) - page = cachedMod.Data; + page = cachedMod!.Data; else { page = await this.ModSites.GetModPageAsync(updateKey); @@ -278,7 +302,7 @@ namespace StardewModdingAPI.Web.Controllers /// The specified update keys. /// The mod's entry in SMAPI's internal database. /// The mod's entry in the wiki list. - private IEnumerable GetUpdateKeys(string[] specifiedKeys, ModDataRecord record, WikiModEntry entry) + private IEnumerable GetUpdateKeys(string[]? specifiedKeys, ModDataRecord? record, WikiModEntry? entry) { // get unique update keys List updateKeys = this.GetUnfilteredUpdateKeys(specifiedKeys, record, entry) @@ -286,21 +310,18 @@ namespace StardewModdingAPI.Web.Controllers .Distinct() .ToList(); - // apply remove overrides from wiki + // apply overrides from wiki + if (entry?.Overrides?.ChangeUpdateKeys?.HasChanges == true) { - var removeKeys = new HashSet( - from key in entry?.ChangeUpdateKeys ?? new string[0] - where key.StartsWith('-') - select UpdateKey.Parse(key.Substring(1)) - ); - if (removeKeys.Any()) - updateKeys.RemoveAll(removeKeys.Contains); + List newKeys = updateKeys.Select(p => p.ToString()).ToList(); + entry.Overrides.ChangeUpdateKeys.Apply(newKeys); + updateKeys = newKeys.Select(UpdateKey.Parse).ToList(); } // if the list has both an update key (like "Nexus:2400") and subkey (like "Nexus:2400@subkey") for the same page, the subkey takes priority { var removeKeys = new HashSet(); - foreach (var key in updateKeys) + foreach (UpdateKey key in updateKeys) { if (key.Subkey != null) removeKeys.Add(new UpdateKey(key.Site, key.ID, null)); @@ -316,7 +337,7 @@ namespace StardewModdingAPI.Web.Controllers /// The specified update keys. /// The mod's entry in SMAPI's internal database. /// The mod's entry in the wiki list. - private IEnumerable GetUnfilteredUpdateKeys(string[] specifiedKeys, ModDataRecord record, WikiModEntry entry) + private IEnumerable GetUnfilteredUpdateKeys(string[]? specifiedKeys, ModDataRecord? record, WikiModEntry? entry) { // specified update keys foreach (string key in specifiedKeys ?? Array.Empty()) @@ -327,7 +348,7 @@ namespace StardewModdingAPI.Web.Controllers // default update key { - string defaultKey = record?.GetDefaultUpdateKey(); + string? defaultKey = record?.GetDefaultUpdateKey(); if (!string.IsNullOrWhiteSpace(defaultKey)) yield return defaultKey; } @@ -344,15 +365,6 @@ namespace StardewModdingAPI.Web.Controllers if (entry.ChucklefishID.HasValue) yield return UpdateKey.GetString(ModSiteKey.Chucklefish, entry.ChucklefishID.ToString()); } - - // overrides from wiki - foreach (string key in entry?.ChangeUpdateKeys ?? Array.Empty()) - { - if (key.StartsWith('+')) - yield return key.Substring(1); - else if (!key.StartsWith("-")) - yield return key; - } } } } diff --git a/src/SMAPI.Web/Controllers/ModsController.cs b/src/SMAPI.Web/Controllers/ModsController.cs index 24e36709..919afa5b 100644 --- a/src/SMAPI.Web/Controllers/ModsController.cs +++ b/src/SMAPI.Web/Controllers/ModsController.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Mvc; @@ -52,8 +53,8 @@ namespace StardewModdingAPI.Web.Controllers public ModListModel FetchData() { // fetch cached data - if (!this.Cache.TryGetWikiMetadata(out Cached metadata)) - return new ModListModel(); + if (!this.Cache.TryGetWikiMetadata(out Cached? metadata)) + return new ModListModel(null, null, Array.Empty(), lastUpdated: DateTimeOffset.UtcNow, isStale: true); // build model return new ModListModel( @@ -62,7 +63,7 @@ namespace StardewModdingAPI.Web.Controllers mods: this.Cache .GetWikiMods() .Select(mod => new ModModel(mod.Data)) - .OrderBy(p => Regex.Replace(p.Name.ToLower(), "[^a-z0-9]", "")), // ignore case, spaces, and special characters when sorting + .OrderBy(p => Regex.Replace((p.Name ?? "").ToLower(), "[^a-z0-9]", "")), // ignore case, spaces, and special characters when sorting lastUpdated: metadata.LastUpdated, isStale: this.Cache.IsStale(metadata.LastUpdated, this.StaleMinutes) ); diff --git a/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs index 864aa215..bd414ea2 100644 --- a/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs +++ b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs @@ -40,7 +40,7 @@ namespace StardewModdingAPI.Web.Framework public void OnAuthorization(AuthorizationFilterContext context) { IFeatureCollection features = context.HttpContext.Features; - IFormFeature formFeature = features.Get(); + IFormFeature? formFeature = features.Get(); if (formFeature?.Form == null) { diff --git a/src/SMAPI.Web/Framework/Caching/Cached.cs b/src/SMAPI.Web/Framework/Caching/Cached.cs index 52041a16..b393e1e1 100644 --- a/src/SMAPI.Web/Framework/Caching/Cached.cs +++ b/src/SMAPI.Web/Framework/Caching/Cached.cs @@ -10,21 +10,18 @@ namespace StardewModdingAPI.Web.Framework.Caching ** Accessors *********/ /// The cached data. - public T Data { get; set; } + public T Data { get; } /// When the data was last updated. - public DateTimeOffset LastUpdated { get; set; } + public DateTimeOffset LastUpdated { get; } /// When the data was last requested through the mod API. - public DateTimeOffset LastRequested { get; set; } + public DateTimeOffset LastRequested { get; internal set; } /********* ** Public methods *********/ - /// Construct an empty instance. - public Cached() { } - /// Construct an instance. /// The cached data. public Cached(T data) diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs index 0d912c7b..fb74e9da 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs @@ -1,6 +1,6 @@ using System; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Framework.UpdateData; -using StardewModdingAPI.Web.Framework.Clients; namespace StardewModdingAPI.Web.Framework.Caching.Mods { @@ -15,7 +15,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods /// The mod's unique ID within the . /// The fetched mod. /// Whether to update the mod's 'last requested' date. - bool TryGetMod(ModSiteKey site, string id, out Cached mod, bool markRequested = true); + bool TryGetMod(ModSiteKey site, string id, [NotNullWhen(true)] out Cached? mod, bool markRequested = true); /// Save data fetched for a mod. /// The mod site on which the mod is found. diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs index 9769793c..4ba0bd20 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Toolkit.Framework.UpdateData; -using StardewModdingAPI.Web.Framework.Clients; namespace StardewModdingAPI.Web.Framework.Caching.Mods { @@ -24,7 +24,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods /// The mod's unique ID within the . /// The fetched mod. /// Whether to update the mod's 'last requested' date. - public bool TryGetMod(ModSiteKey site, string id, out Cached mod, bool markRequested = true) + public bool TryGetMod(ModSiteKey site, string id, [NotNullWhen(true)] out Cached? mod, bool markRequested = true) { // get mod if (!this.Mods.TryGetValue(this.GetKey(site, id), out var cachedMod)) diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs index 2ab7ea5a..b8a0df34 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; namespace StardewModdingAPI.Web.Framework.Caching.Wiki @@ -12,16 +13,16 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki *********/ /// Get the cached wiki metadata. /// The fetched metadata. - bool TryGetWikiMetadata(out Cached metadata); + bool TryGetWikiMetadata([NotNullWhen(true)] out Cached? metadata); /// Get the cached wiki mods. /// A filter to apply, if any. - IEnumerable> GetWikiMods(Func filter = null); + IEnumerable> GetWikiMods(Func? filter = null); /// Save data fetched from the wiki compatibility list. /// The current stable Stardew Valley version. /// The current beta Stardew Valley version. /// The mod data. - void SaveWikiData(string stableVersion, string betaVersion, IEnumerable mods); + void SaveWikiData(string? stableVersion, string? betaVersion, IEnumerable mods); } } diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs index 064a7c3c..8b4338e2 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; @@ -12,10 +13,10 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki ** Fields *********/ /// The saved wiki metadata. - private Cached Metadata; + private Cached? Metadata; /// The cached wiki data. - private Cached[] Mods = new Cached[0]; + private Cached[] Mods = Array.Empty>(); /********* @@ -23,7 +24,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki *********/ /// Get the cached wiki metadata. /// The fetched metadata. - public bool TryGetWikiMetadata(out Cached metadata) + public bool TryGetWikiMetadata([NotNullWhen(true)] out Cached? metadata) { metadata = this.Metadata; return metadata != null; @@ -31,7 +32,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki /// Get the cached wiki mods. /// A filter to apply, if any. - public IEnumerable> GetWikiMods(Func filter = null) + public IEnumerable> GetWikiMods(Func? filter = null) { foreach (var mod in this.Mods) { @@ -44,7 +45,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki /// The current stable Stardew Valley version. /// The current beta Stardew Valley version. /// The mod data. - public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable mods) + public void SaveWikiData(string? stableVersion, string? betaVersion, IEnumerable mods) { this.Metadata = new Cached(new WikiMetadata(stableVersion, betaVersion)); this.Mods = mods.Select(mod => new Cached(mod)).ToArray(); diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs index c04de4a5..f53ea201 100644 --- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs +++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs @@ -7,22 +7,19 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki ** Accessors *********/ /// The current stable Stardew Valley version. - public string StableVersion { get; set; } + public string? StableVersion { get; } /// The current beta Stardew Valley version. - public string BetaVersion { get; set; } + public string? BetaVersion { get; } /********* ** Public methods *********/ - /// Construct an instance. - public WikiMetadata() { } - /// Construct an instance. /// The current stable Stardew Valley version. /// The current beta Stardew Valley version. - public WikiMetadata(string stableVersion, string betaVersion) + public WikiMetadata(string? stableVersion, string? betaVersion) { this.StableVersion = stableVersion; this.BetaVersion = betaVersion; diff --git a/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs b/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs index b8b05878..ce0f1122 100644 --- a/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs @@ -42,7 +42,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish /// Get update check info about a mod. /// The mod ID. - public async Task GetModData(string id) + public async Task GetModData(string id) { IModPage page = new GenericModPage(this.SiteKey, id); @@ -51,14 +51,14 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID."); // fetch HTML - string html; + string? html; try { html = await this.Client .GetAsync(string.Format(this.ModPageUrlFormat, parsedId)) .AsString(); } - catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound || ex.Status == HttpStatusCode.Forbidden) + catch (ApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.Forbidden) { return page.SetError(RemoteModStatus.DoesNotExist, "Found no Chucklefish mod with this ID."); } @@ -67,7 +67,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish // extract mod info string url = this.GetModUrl(parsedId); - string version = doc.DocumentNode.SelectSingleNode("//h1/span")?.InnerText; + string? version = doc.DocumentNode.SelectSingleNode("//h1/span")?.InnerText; string name = doc.DocumentNode.SelectSingleNode("//h1").ChildNodes[0].InnerText.Trim(); if (name.StartsWith("[SMAPI]")) name = name.Substring("[SMAPI]".Length).TrimStart(); @@ -79,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } @@ -90,7 +90,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish /// The mod ID. private string GetModUrl(uint id) { - UriBuilder builder = new UriBuilder(this.Client.BaseClient.BaseAddress); + UriBuilder builder = new(this.Client.BaseClient.BaseAddress!); builder.Path += string.Format(this.ModPageUrlFormat, id); return builder.Uri.ToString(); } diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs index d8008721..9b4f2580 100644 --- a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs +++ b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Net; using System.Text.RegularExpressions; using System.Threading.Tasks; using Pathoschild.Http.Client; @@ -17,7 +18,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge private readonly IClient Client; /// A regex pattern which matches a version number in a CurseForge mod file name. - private readonly Regex VersionInNamePattern = new Regex(@"^(?:.+? | *)v?(\d+\.\d+(?:\.\d+)?(?:-.+?)?) *(?:\.(?:zip|rar|7z))?$", RegexOptions.Compiled); + private readonly Regex VersionInNamePattern = new(@"^(?:.+? | *)v?(\d+\.\d+(?:\.\d+)?(?:-.+?)?) *(?:\.(?:zip|rar|7z))?$", RegexOptions.Compiled); /********* @@ -33,14 +34,17 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge /// Construct an instance. /// The user agent for the API client. /// The base URL for the CurseForge API. - public CurseForgeClient(string userAgent, string apiUrl) + /// The API authentication key. + public CurseForgeClient(string userAgent, string apiUrl, string apiKey) { - this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent); + this.Client = new FluentClient(apiUrl) + .SetUserAgent(userAgent) + .AddDefault(request => request.WithHeader("x-api-key", apiKey)); } /// Get update check info about a mod. /// The mod ID. - public async Task GetModData(string id) + public async Task GetModData(string id) { IModPage page = new GenericModPage(this.SiteKey, id); @@ -49,11 +53,18 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid CurseForge mod ID, must be an integer ID."); // get raw data - ModModel mod = await this.Client - .GetAsync($"addon/{parsedId}") - .As(); - if (mod == null) + ModModel? mod; + try + { + ResponseModel response = await this.Client + .GetAsync($"mods/{parsedId}") + .As>(); + mod = response.Data; + } + catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) + { return page.SetError(RemoteModStatus.DoesNotExist, "Found no CurseForge mod with this ID."); + } // get downloads List downloads = new List(); @@ -65,13 +76,13 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge } // return info - return page.SetInfo(name: mod.Name, version: null, url: mod.WebsiteUrl, downloads: downloads); + return page.SetInfo(name: mod.Name, version: null, url: mod.Links.WebsiteUrl, downloads: downloads); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } @@ -80,9 +91,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge *********/ /// Get a raw version string for a mod file, if available. /// The file whose version to get. - private string GetRawVersion(ModFileModel file) + private string? GetRawVersion(ModFileModel file) { - Match match = this.VersionInNamePattern.Match(file.DisplayName); + Match match = this.VersionInNamePattern.Match(file.DisplayName ?? ""); if (!match.Success) match = this.VersionInNamePattern.Match(file.FileName); diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs index 9de74847..e9adcf20 100644 --- a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs +++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs @@ -3,10 +3,26 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels /// Metadata from the CurseForge API about a mod file. public class ModFileModel { + /********* + ** Accessors + *********/ /// The file name as downloaded. - public string FileName { get; set; } + public string FileName { get; } /// The file display name. - public string DisplayName { get; set; } + public string? DisplayName { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The file name as downloaded. + /// The file display name. + public ModFileModel(string fileName, string? displayName) + { + this.FileName = fileName; + this.DisplayName = displayName; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs new file mode 100644 index 00000000..2f9abe4f --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModLinksModel.cs @@ -0,0 +1,7 @@ +namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels +{ + /// A list of links for a mod. + /// The URL for the CurseForge mod page. + /// The URL for the mod's source code, if any. + public record ModLinksModel(string WebsiteUrl, string? SourceUrl); +} diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs index 48cd185b..7018be54 100644 --- a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs +++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs @@ -1,18 +1,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels { - /// An mod from the CurseForge API. - public class ModModel - { - /// The mod's unique ID on CurseForge. - public int ID { get; set; } - - /// The mod name. - public string Name { get; set; } - - /// The web URL for the mod page. - public string WebsiteUrl { get; set; } - - /// The available file downloads. - public ModFileModel[] LatestFiles { get; set; } - } + /// A mod from the CurseForge API. + /// The mod's unique ID on CurseForge. + /// The mod name. + /// The available file downloads. + /// The URLs for this mod. + public record ModModel(int Id, string Name, ModFileModel[] LatestFiles, ModLinksModel Links); } diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs new file mode 100644 index 00000000..4d538a93 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ResponseModel.cs @@ -0,0 +1,8 @@ +using Newtonsoft.Json; + +namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels +{ + /// A response from the CurseForge API. + /// The data returned by the API. + public record ResponseModel(TData Data); +} diff --git a/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs b/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs index f08b471c..548f17c3 100644 --- a/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs +++ b/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs @@ -7,26 +7,23 @@ namespace StardewModdingAPI.Web.Framework.Clients ** Accessors *********/ /// The download's display name. - public string Name { get; set; } + public string Name { get; } /// The download's description. - public string Description { get; set; } + public string? Description { get; } /// The download's file version. - public string Version { get; set; } + public string? Version { get; } /********* ** Public methods *********/ - /// Construct an empty instance. - public GenericModDownload() { } - /// Construct an instance. /// The download's display name. /// The download's description. /// The download's file version. - public GenericModDownload(string name, string description, string version) + public GenericModDownload(string name, string? description, string? version) { this.Name = name; this.Description = description; diff --git a/src/SMAPI.Web/Framework/Clients/GenericModPage.cs b/src/SMAPI.Web/Framework/Clients/GenericModPage.cs index 622e6c56..5353c7e1 100644 --- a/src/SMAPI.Web/Framework/Clients/GenericModPage.cs +++ b/src/SMAPI.Web/Framework/Clients/GenericModPage.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Toolkit.Framework.UpdateData; @@ -17,30 +19,31 @@ namespace StardewModdingAPI.Web.Framework.Clients public string Id { get; set; } /// The mod name. - public string Name { get; set; } + public string? Name { get; set; } /// The mod's semantic version number. - public string Version { get; set; } + public string? Version { get; set; } /// The mod's web URL. - public string Url { get; set; } + public string? Url { get; set; } /// The mod downloads. - public IModDownload[] Downloads { get; set; } = new IModDownload[0]; + public IModDownload[] Downloads { get; set; } = Array.Empty(); /// The mod availability status on the remote site. - public RemoteModStatus Status { get; set; } = RemoteModStatus.Ok; + public RemoteModStatus Status { get; set; } = RemoteModStatus.InvalidData; /// A user-friendly error which indicates why fetching the mod info failed (if applicable). - public string Error { get; set; } + public string? Error { get; set; } + + /// Whether the mod data is valid. + [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))] + public bool IsValid => this.Status == RemoteModStatus.Ok; /********* ** Public methods *********/ - /// Construct an empty instance. - public GenericModPage() { } - /// Construct an instance. /// The mod site containing the mod. /// The mod's unique ID within the site. @@ -55,12 +58,13 @@ namespace StardewModdingAPI.Web.Framework.Clients /// The mod's semantic version number. /// The mod's web URL. /// The mod downloads. - public IModPage SetInfo(string name, string version, string url, IEnumerable downloads) + public IModPage SetInfo(string name, string? version, string url, IEnumerable downloads) { this.Name = name; this.Version = version; this.Url = url; this.Downloads = downloads.ToArray(); + this.Status = RemoteModStatus.Ok; return this; } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs index 73ce4025..dbce9368 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs @@ -5,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// A GitHub download attached to a release. internal class GitAsset { + /********* + ** Accessors + *********/ /// The file name. [JsonProperty("name")] - public string FileName { get; set; } + public string FileName { get; } /// The file content type. [JsonProperty("content_type")] - public string ContentType { get; set; } + public string ContentType { get; } /// The download URL. [JsonProperty("browser_download_url")] - public string DownloadUrl { get; set; } + public string DownloadUrl { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The file name. + /// The file content type. + /// The download URL. + public GitAsset(string fileName, string contentType, string downloadUrl) + { + this.FileName = fileName; + this.ContentType = contentType; + this.DownloadUrl = downloadUrl; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs index 671f077c..785979a5 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs @@ -33,26 +33,26 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// The Accept header value expected by the GitHub API. /// The username with which to authenticate to the GitHub API. /// The password with which to authenticate to the GitHub API. - public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string username, string password) + public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string? username, string? password) { this.Client = new FluentClient(baseUrl) .SetUserAgent(userAgent) .AddDefault(req => req.WithHeader("Accept", acceptHeader)); if (!string.IsNullOrWhiteSpace(username)) - this.Client = this.Client.SetBasicAuthentication(username, password); + this.Client = this.Client.SetBasicAuthentication(username, password!); } /// Get basic metadata for a GitHub repository, if available. /// The repository key (like Pathoschild/SMAPI). /// Returns the repository info if it exists, else null. - public async Task GetRepositoryAsync(string repo) + public async Task GetRepositoryAsync(string repo) { this.AssertKeyFormat(repo); try { return await this.Client .GetAsync($"repos/{repo}") - .As(); + .As(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { @@ -64,7 +64,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// The repository key (like Pathoschild/SMAPI). /// Whether to return a prerelease version if it's latest. /// Returns the release if found, else null. - public async Task GetLatestReleaseAsync(string repo, bool includePrerelease = false) + public async Task GetLatestReleaseAsync(string repo, bool includePrerelease = false) { this.AssertKeyFormat(repo); try @@ -79,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub return await this.Client .GetAsync($"repos/{repo}/releases/latest") - .As(); + .As(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { @@ -89,7 +89,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// Get update check info about a mod. /// The mod ID. - public async Task GetModData(string id) + public async Task GetModData(string id) { IModPage page = new GenericModPage(this.SiteKey, id); @@ -97,15 +97,15 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/SMAPI'."); // fetch repo info - GitRepo repository = await this.GetRepositoryAsync(id); + GitRepo? repository = await this.GetRepositoryAsync(id); if (repository == null) return page.SetError(RemoteModStatus.DoesNotExist, "Found no GitHub repository for this ID."); string name = repository.FullName; string url = $"{repository.WebUrl}/releases"; // get releases - GitRelease latest; - GitRelease preview; + GitRelease? latest; + GitRelease? preview; { // get latest release (whether preview or stable) latest = await this.GetLatestReleaseAsync(id, includePrerelease: true); @@ -116,7 +116,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub preview = null; if (latest.IsPrerelease) { - GitRelease release = await this.GetLatestReleaseAsync(id, includePrerelease: false); + GitRelease? release = await this.GetLatestReleaseAsync(id, includePrerelease: false); if (release != null) { preview = latest; @@ -127,8 +127,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub // get downloads IModDownload[] downloads = new[] { latest, preview } - .Where(release => release != null) - .Select(release => (IModDownload)new GenericModDownload(release.Name, release.Body, release.Tag)) + .Where(release => release is not null) + .Select(release => (IModDownload)new GenericModDownload(release!.Name, release.Body, release.Tag)) .ToArray(); // return info @@ -138,7 +138,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs index 736efbe6..24d6c3c5 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs @@ -5,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// The license info for a GitHub project. internal class GitLicense { + /********* + ** Accessors + *********/ /// The license display name. [JsonProperty("name")] - public string Name { get; set; } + public string Name { get; } /// The SPDX ID for the license. [JsonProperty("spdx_id")] - public string SpdxId { get; set; } + public string SpdxId { get; } /// The URL for the license info. [JsonProperty("url")] - public string Url { get; set; } + public string Url { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The license display name. + /// The SPDX ID for the license. + /// The URL for the license info. + public GitLicense(string name, string spdxId, string url) + { + this.Name = name; + this.SpdxId = spdxId; + this.Url = url; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs index d0db5297..9de6f020 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs @@ -1,3 +1,4 @@ +using System; using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.GitHub @@ -10,24 +11,45 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub *********/ /// The display name. [JsonProperty("name")] - public string Name { get; set; } + public string Name { get; } /// The semantic version string. [JsonProperty("tag_name")] - public string Tag { get; set; } + public string Tag { get; } /// The Markdown description for the release. - public string Body { get; set; } + public string Body { get; internal set; } /// Whether this is a draft version. [JsonProperty("draft")] - public bool IsDraft { get; set; } + public bool IsDraft { get; } /// Whether this is a prerelease version. [JsonProperty("prerelease")] - public bool IsPrerelease { get; set; } + public bool IsPrerelease { get; } /// The attached files. - public GitAsset[] Assets { get; set; } + public GitAsset[] Assets { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The display name. + /// The semantic version string. + /// The Markdown description for the release. + /// Whether this is a draft version. + /// Whether this is a prerelease version. + /// The attached files. + public GitRelease(string name, string tag, string? body, bool isDraft, bool isPrerelease, GitAsset[]? assets) + { + this.Name = name; + this.Tag = tag; + this.Body = body ?? string.Empty; + this.IsDraft = isDraft; + this.IsPrerelease = isPrerelease; + this.Assets = assets ?? Array.Empty(); + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs index 7d80576e..879b5e49 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs @@ -5,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// Basic metadata about a GitHub project. internal class GitRepo { + /********* + ** Accessors + *********/ /// The full repository name, including the owner. [JsonProperty("full_name")] - public string FullName { get; set; } + public string FullName { get; } /// The URL to the repository web page, if any. [JsonProperty("html_url")] - public string WebUrl { get; set; } + public string? WebUrl { get; } /// The code license, if any. [JsonProperty("license")] - public GitLicense License { get; set; } + public GitLicense? License { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full repository name, including the owner. + /// The URL to the repository web page, if any. + /// The code license, if any. + public GitRepo(string fullName, string? webUrl, GitLicense? license) + { + this.FullName = fullName; + this.WebUrl = webUrl; + this.License = license; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs index 0d6f4643..886e32d3 100644 --- a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs +++ b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs @@ -12,12 +12,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub /// Get basic metadata for a GitHub repository, if available. /// The repository key (like Pathoschild/SMAPI). /// Returns the repository info if it exists, else null. - Task GetRepositoryAsync(string repo); + Task GetRepositoryAsync(string repo); /// Get the latest release for a GitHub repository. /// The repository key (like Pathoschild/SMAPI). /// Whether to return a prerelease version if it's latest. /// Returns the release if found, else null. - Task GetLatestReleaseAsync(string repo, bool includePrerelease = false); + Task GetLatestReleaseAsync(string repo, bool includePrerelease = false); } } diff --git a/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs b/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs index 33277711..3697ffae 100644 --- a/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs +++ b/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs @@ -18,6 +18,6 @@ namespace StardewModdingAPI.Web.Framework.Clients *********/ /// Get update check info about a mod. /// The mod ID. - Task GetModData(string id); + Task GetModData(string id); } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs index 3a1c5b9d..f5a5f930 100644 --- a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Pathoschild.Http.Client; using StardewModdingAPI.Toolkit.Framework.UpdateData; @@ -41,9 +42,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop /// Get update check info about a mod. /// The mod ID. - public async Task GetModData(string id) + [SuppressMessage("ReSharper", "ConditionalAccessQualifierIsNonNullableAccordingToAPIContract", Justification = "The nullability is validated in this method.")] + public async Task GetModData(string id) { - var page = new GenericModPage(this.SiteKey, id); + IModPage page = new GenericModPage(this.SiteKey, id); if (!long.TryParse(id, out long parsedId)) return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID."); @@ -58,9 +60,11 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop Mods = true }) .As(); - ModModel mod = response.Mods[parsedId]; - if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue) - return null; + + if (!response.Mods.TryGetValue(parsedId, out ModModel? mod) || mod?.Mod is null) + return page.SetError(RemoteModStatus.DoesNotExist, "Found no ModDrop page with this ID."); + if (mod.Mod.ErrorCode is not null) + return page.SetError(RemoteModStatus.InvalidData, $"ModDrop returned error code {mod.Mod.ErrorCode} for mod ID '{id}'."); // get files var downloads = new List(); @@ -75,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop } // return info - string name = mod.Mod?.Title; + string name = mod.Mod.Title; string url = string.Format(this.ModUrlFormat, id); return page.SetInfo(name: name, version: null, url: url, downloads: downloads); } @@ -83,7 +87,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.Client?.Dispose(); + this.Client.Dispose(); } } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs index b01196f4..31905338 100644 --- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs @@ -5,27 +5,53 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels /// Metadata from the ModDrop API about a mod file. public class FileDataModel { + /********* + ** Accessors + *********/ /// The file title. [JsonProperty("title")] - public string Name { get; set; } + public string Name { get; } /// The file description. [JsonProperty("desc")] - public string Description { get; set; } + public string Description { get; } /// The file version. - public string Version { get; set; } + public string Version { get; } /// Whether the file is deleted. - public bool IsDeleted { get; set; } + public bool IsDeleted { get; } /// Whether the file is hidden from users. - public bool IsHidden { get; set; } + public bool IsHidden { get; } /// Whether this is the default file for the mod. - public bool IsDefault { get; set; } + public bool IsDefault { get; } /// Whether this is an archived file. - public bool IsOld { get; set; } + public bool IsOld { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The file title. + /// The file description. + /// The file version. + /// Whether the file is deleted. + /// Whether the file is hidden from users. + /// Whether this is the default file for the mod. + /// Whether this is an archived file. + public FileDataModel(string name, string description, string version, bool isDeleted, bool isHidden, bool isDefault, bool isOld) + { + this.Name = name; + this.Description = description; + this.Version = version; + this.IsDeleted = isDeleted; + this.IsHidden = isHidden; + this.IsDefault = isDefault; + this.IsOld = isOld; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs index cfdd6a4e..0654b576 100644 --- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs @@ -3,13 +3,31 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels /// Metadata about a mod from the ModDrop API. public class ModDataModel { + /********* + ** Accessors + *********/ /// The mod's unique ID on ModDrop. public int ID { get; set; } + /// The mod name. + public string Title { get; set; } + /// The error code, if any. public int? ErrorCode { get; set; } - /// The mod name. - public string Title { get; set; } + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod's unique ID on ModDrop. + /// The mod name. + /// The error code, if any. + public ModDataModel(int id, string title, int? errorCode) + { + this.ID = id; + this.Title = title; + this.ErrorCode = errorCode; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs index 7f692ca1..cb4be35c 100644 --- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs @@ -5,7 +5,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels /// A list of mods from the ModDrop API. public class ModListModel { + /********* + ** Accessors + *********/ /// The mod data. - public IDictionary Mods { get; set; } + public IDictionary Mods { get; } = new Dictionary(); } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs index 9f4b2c6f..60b818d6 100644 --- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs @@ -3,10 +3,26 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels /// An entry in a mod list from the ModDrop API. public class ModModel { + /********* + ** Accessors + *********/ /// The available file downloads. - public FileDataModel[] Files { get; set; } + public FileDataModel[] Files { get; } /// The mod metadata. - public ModDataModel Mod { get; set; } + public ModDataModel Mod { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The available file downloads. + /// The mod metadata. + public ModModel(FileDataModel[] files, ModDataModel mod) + { + this.Files = files; + this.Mod = mod; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/DisabledNexusClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/DisabledNexusClient.cs new file mode 100644 index 00000000..6edd5f64 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/Nexus/DisabledNexusClient.cs @@ -0,0 +1,31 @@ +using System.Threading.Tasks; +using StardewModdingAPI.Toolkit.Framework.UpdateData; + +namespace StardewModdingAPI.Web.Framework.Clients.Nexus +{ + /// A client for the Nexus website which does nothing, used for local development. + internal class DisabledNexusClient : INexusClient + { + /********* + ** Accessors + *********/ + /// + public ModSiteKey SiteKey => ModSiteKey.Nexus; + + + /********* + ** Public methods + *********/ + /// Get update check info about a mod. + /// The mod ID. + public Task GetModData(string id) + { + return Task.FromResult( + new GenericModPage(ModSiteKey.Nexus, id).SetError(RemoteModStatus.TemporaryError, "The Nexus client is currently disabled due to the configuration.") + ); + } + + /// + public void Dispose() { } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs index ef3ef22e..46c3092c 100644 --- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs @@ -59,7 +59,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus /// Get update check info about a mod. /// The mod ID. - public async Task GetModData(string id) + public async Task GetModData(string id) { IModPage page = new GenericModPage(this.SiteKey, id); @@ -70,25 +70,25 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus // adult content are hidden for anonymous users, so fall back to the API in that case. // Note that the API has very restrictive rate limits which means we can't just use it // for all cases. - NexusMod mod = await this.GetModFromWebsiteAsync(parsedId); + NexusMod? mod = await this.GetModFromWebsiteAsync(parsedId); if (mod?.Status == NexusModStatus.AdultContentForbidden) mod = await this.GetModFromApiAsync(parsedId); // page doesn't exist - if (mod == null || mod.Status == NexusModStatus.Hidden || mod.Status == NexusModStatus.NotPublished) + if (mod == null || mod.Status is NexusModStatus.Hidden or NexusModStatus.NotPublished) return page.SetError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID."); // return info - page.SetInfo(name: mod.Name, url: mod.Url, version: mod.Version, downloads: mod.Downloads); + page.SetInfo(name: mod.Name ?? parsedId.ToString(), url: mod.Url ?? this.GetModUrl(parsedId), version: mod.Version, downloads: mod.Downloads); if (mod.Status != NexusModStatus.Ok) - page.SetError(RemoteModStatus.TemporaryError, mod.Error); + page.SetError(RemoteModStatus.TemporaryError, mod.Error!); return page; } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { - this.WebClient?.Dispose(); + this.WebClient.Dispose(); } @@ -98,7 +98,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus /// Get metadata about a mod by scraping the Nexus website. /// The Nexus mod ID. /// Returns the mod info if found, else null. - private async Task GetModFromWebsiteAsync(uint id) + private async Task GetModFromWebsiteAsync(uint id) { // fetch HTML string html; @@ -114,35 +114,38 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus } // parse HTML - var doc = new HtmlDocument(); + HtmlDocument doc = new(); doc.LoadHtml(html); // handle Nexus error message - HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]"); + HtmlNode? node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]"); if (node != null) { - string[] errorParts = node.InnerText.Trim().Split(new[] { '\n' }, 2, System.StringSplitOptions.RemoveEmptyEntries); + string[] errorParts = node.InnerText.Trim().Split('\n', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); string errorCode = errorParts[0]; - string errorText = errorParts.Length > 1 ? errorParts[1] : null; - switch (errorCode.Trim().ToLower()) + string? errorText = errorParts.Length > 1 ? errorParts[1] : null; + switch (errorCode.ToLower()) { case "not found": return null; default: - return new NexusMod { Error = $"Nexus error: {errorCode} ({errorText}).", Status = this.GetWebStatus(errorCode) }; + return new NexusMod( + status: this.GetWebStatus(errorCode), + error: $"Nexus error: {errorCode} ({errorText})." + ); } } // extract mod info string url = this.GetModUrl(id); - string name = doc.DocumentNode.SelectSingleNode("//div[@id='pagetitle']//h1")?.InnerText.Trim(); - string version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim(); - SemanticVersion.TryParse(version, out ISemanticVersion parsedVersion); + string? name = doc.DocumentNode.SelectSingleNode("//div[@id='pagetitle']//h1")?.InnerText.Trim(); + string? version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim(); + SemanticVersion.TryParse(version, out ISemanticVersion? parsedVersion); // extract files var downloads = new List(); - foreach (var fileSection in doc.DocumentNode.SelectNodes("//div[contains(@class, 'files-tabs')]")) + foreach (HtmlNode fileSection in doc.DocumentNode.SelectNodes("//div[contains(@class, 'files-tabs')]")) { string sectionName = fileSection.Descendants("h2").First().InnerText; if (sectionName != "Main files" && sectionName != "Optional files") @@ -152,7 +155,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus { string fileName = container.GetDataAttribute("name").Value; string fileVersion = container.GetDataAttribute("version").Value; - string description = container.SelectSingleNode("following-sibling::*[1][self::dd]//div").InnerText?.Trim(); // get text of next
tag; derived from https://stackoverflow.com/a/25535623/262123 + string? description = container.SelectSingleNode("following-sibling::*[1][self::dd]//div").InnerText?.Trim(); // get text of next
tag; derived from https://stackoverflow.com/a/25535623/262123 downloads.Add( new GenericModDownload(fileName, description, fileVersion) @@ -161,13 +164,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus } // yield info - return new NexusMod - { - Name = name, - Version = parsedVersion?.ToString() ?? version, - Url = url, - Downloads = downloads.ToArray() - }; + return new NexusMod( + name: name ?? id.ToString(), + version: parsedVersion?.ToString() ?? version, + url: url, + downloads: downloads.ToArray() + ); } /// Get metadata about a mod from the Nexus API. @@ -180,22 +182,21 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus ModFileList files = await this.ApiClient.ModFiles.GetModFiles("stardewvalley", (int)id, FileCategory.Main, FileCategory.Optional); // yield info - return new NexusMod - { - Name = mod.Name, - Version = SemanticVersion.TryParse(mod.Version, out ISemanticVersion version) ? version?.ToString() : mod.Version, - Url = this.GetModUrl(id), - Downloads = files.Files - .Select(file => (IModDownload)new GenericModDownload(file.Name, null, file.FileVersion)) + return new NexusMod( + name: mod.Name, + version: SemanticVersion.TryParse(mod.Version, out ISemanticVersion? version) ? version.ToString() : mod.Version, + url: this.GetModUrl(id), + downloads: files.Files + .Select(file => (IModDownload)new GenericModDownload(file.Name, file.Description, file.FileVersion)) .ToArray() - }; + ); } /// Get the full mod page URL for a given ID. /// The mod ID. private string GetModUrl(uint id) { - UriBuilder builder = new UriBuilder(this.WebClient.BaseClient.BaseAddress); + UriBuilder builder = new(this.WebClient.BaseClient.BaseAddress!); builder.Path += string.Format(this.WebModUrlFormat, id); return builder.Uri.ToString(); } diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs b/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs index aef90ede..3155cfda 100644 --- a/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs +++ b/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs @@ -1,3 +1,4 @@ +using System; using Newtonsoft.Json; namespace StardewModdingAPI.Web.Framework.Clients.Nexus.ResponseModels @@ -9,25 +10,53 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus.ResponseModels ** Accessors *********/ /// The mod name. - public string Name { get; set; } + public string? Name { get; } /// The mod's semantic version number. - public string Version { get; set; } + public string? Version { get; } /// The mod's web URL. [JsonProperty("mod_page_uri")] - public string Url { get; set; } + public string? Url { get; } /// The mod's publication status. [JsonIgnore] - public NexusModStatus Status { get; set; } = NexusModStatus.Ok; + public NexusModStatus Status { get; } /// The files available to download. [JsonIgnore] - public IModDownload[] Downloads { get; set; } + public IModDownload[] Downloads { get; } /// A custom user-friendly error which indicates why fetching the mod info failed (if applicable). [JsonIgnore] - public string Error { get; set; } + public string? Error { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod name + /// The mod's semantic version number. + /// The mod's web URL. + /// The files available to download. + public NexusMod(string name, string? version, string url, IModDownload[] downloads) + { + this.Name = name; + this.Version = version; + this.Url = url; + this.Status = NexusModStatus.Ok; + this.Downloads = downloads; + } + + /// Construct an instance. + /// The mod's publication status. + /// A custom user-friendly error which indicates why fetching the mod info failed (if applicable). + public NexusMod(NexusModStatus status, string error) + { + this.Status = status; + this.Error = error; + this.Downloads = Array.Empty(); + } } } diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs index 813ea115..7f40e713 100644 --- a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs @@ -1,15 +1,35 @@ +using System.Diagnostics.CodeAnalysis; + namespace StardewModdingAPI.Web.Framework.Clients.Pastebin { /// The response for a get-paste request. internal class PasteInfo { + /********* + ** Accessors + *********/ /// Whether the log was successfully fetched. - public bool Success { get; set; } + [MemberNotNullWhen(true, nameof(PasteInfo.Content))] + [MemberNotNullWhen(false, nameof(PasteInfo.Error))] + public bool Success => this.Error == null || this.Content != null; /// The fetched paste content (if is true). - public string Content { get; set; } + public string? Content { get; internal set; } - /// The error message if saving failed. - public string Error { get; set; } + /// The error message (if is false). + public string? Error { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The fetched paste content. + /// The error message, if it failed. + public PasteInfo(string? content, string? error) + { + this.Content = content; + this.Error = error; + } } } diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs index 1be00be7..0e00f071 100644 --- a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs @@ -33,24 +33,24 @@ namespace StardewModdingAPI.Web.Framework.Clients.Pastebin try { // get from API - string content = await this.Client + string? content = await this.Client .GetAsync($"raw/{id}") .AsString(); // handle Pastebin errors if (string.IsNullOrWhiteSpace(content)) - return new PasteInfo { Error = "Received an empty response from Pastebin." }; + return new PasteInfo(null, "Received an empty response from Pastebin."); if (content.StartsWith("Decompress a string. /// The compressed text. /// Derived from . - public string DecompressString(string rawText) + [return: NotNullIfNotNull("rawText")] + public string? DecompressString(string? rawText) { + if (rawText is null) + return rawText; + // get raw bytes byte[] zipBuffer; try @@ -69,7 +74,7 @@ namespace StardewModdingAPI.Web.Framework.Compression return rawText; // decompress - using MemoryStream memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); { // read length prefix int dataLength = BitConverter.ToInt32(zipBuffer, 0); @@ -78,7 +83,7 @@ namespace StardewModdingAPI.Web.Framework.Compression // read data byte[] buffer = new byte[dataLength]; memoryStream.Position = 0; - using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) + using (GZipStream gZipStream = new(memoryStream, CompressionMode.Decompress)) gZipStream.Read(buffer, 0, buffer.Length); // return original string diff --git a/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs index a000865e..ef2d5696 100644 --- a/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs +++ b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + namespace StardewModdingAPI.Web.Framework.Compression { /// Handles GZip compression logic. @@ -12,6 +14,7 @@ namespace StardewModdingAPI.Web.Framework.Compression /// Decompress a string. /// The compressed text. - string DecompressString(string rawText); + [return: NotNullIfNotNull("rawText")] + string? DecompressString(string? rawText); } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs index 878130bf..ebb3618a 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs @@ -10,17 +10,17 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels ** Generic ****/ /// The user agent for API clients, where {0} is the SMAPI version. - public string UserAgent { get; set; } + public string UserAgent { get; set; } = null!; /**** ** Azure ****/ /// The connection string for the Azure Blob storage account. - public string AzureBlobConnectionString { get; set; } + public string? AzureBlobConnectionString { get; set; } /// The Azure Blob container in which to store temporary uploaded logs. - public string AzureBlobTempContainer { get; set; } + public string AzureBlobTempContainer { get; set; } = null!; /// The number of days since the blob's last-modified date when it will be deleted. public int AzureBlobTempExpiryDays { get; set; } @@ -30,65 +30,68 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels ** Chucklefish ****/ /// The base URL for the Chucklefish mod site. - public string ChucklefishBaseUrl { get; set; } + public string ChucklefishBaseUrl { get; set; } = null!; /// The URL for a mod page on the Chucklefish mod site excluding the , where {0} is the mod ID. - public string ChucklefishModPageUrlFormat { get; set; } + public string ChucklefishModPageUrlFormat { get; set; } = null!; /**** ** CurseForge ****/ /// The base URL for the CurseForge API. - public string CurseForgeBaseUrl { get; set; } + public string CurseForgeBaseUrl { get; set; } = null!; + + /// The API authentication key for the CurseForge API. + public string CurseForgeApiKey { get; set; } = null!; /**** ** GitHub ****/ /// The base URL for the GitHub API. - public string GitHubBaseUrl { get; set; } + public string GitHubBaseUrl { get; set; } = null!; /// The Accept header value expected by the GitHub API. - public string GitHubAcceptHeader { get; set; } + public string GitHubAcceptHeader { get; set; } = null!; /// The username with which to authenticate to the GitHub API (if any). - public string GitHubUsername { get; set; } + public string? GitHubUsername { get; set; } /// The password with which to authenticate to the GitHub API (if any). - public string GitHubPassword { get; set; } + public string? GitHubPassword { get; set; } /**** ** ModDrop ****/ /// The base URL for the ModDrop API. - public string ModDropApiUrl { get; set; } + public string ModDropApiUrl { get; set; } = null!; /// The URL for a ModDrop mod page for the user, where {0} is the mod ID. - public string ModDropModPageUrl { get; set; } + public string ModDropModPageUrl { get; set; } = null!; /**** ** Nexus Mods ****/ /// The base URL for the Nexus Mods API. - public string NexusBaseUrl { get; set; } + public string NexusBaseUrl { get; set; } = null!; /// The URL for a Nexus mod page for the user, excluding the , where {0} is the mod ID. - public string NexusModUrlFormat { get; set; } + public string NexusModUrlFormat { get; set; } = null!; /// The URL for a Nexus mod page to scrape for versions, excluding the , where {0} is the mod ID. - public string NexusModScrapeUrlFormat { get; set; } + public string NexusModScrapeUrlFormat { get; set; } = null!; /// The Nexus API authentication key. - public string NexusApiKey { get; set; } + public string? NexusApiKey { get; set; } /**** ** Pastebin ****/ /// The base URL for the Pastebin API. - public string PastebinBaseUrl { get; set; } + public string PastebinBaseUrl { get; set; } = null!; } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs index f382d7b5..e46ecf2b 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs @@ -4,12 +4,12 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels internal class ModOverrideConfig { /// The unique ID from the mod's manifest. - public string ID { get; set; } + public string ID { get; set; } = null!; /// Whether to allow non-standard versions. public bool AllowNonStandardVersions { get; set; } /// The mod page URL to use regardless of which site has the update, or null to use the site URL. - public string SetUrl { get; set; } + public string? SetUrl { get; set; } } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs index bd58dba0..c3b136e8 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs @@ -1,3 +1,5 @@ +using System; + namespace StardewModdingAPI.Web.Framework.ConfigModels { /// The config settings for mod update checks. @@ -6,13 +8,16 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /********* ** Accessors *********/ - /// The number of minutes successful update checks should be cached before refetching them. + /// The number of minutes successful update checks should be cached before re-fetching them. public int SuccessCacheMinutes { get; set; } - /// The number of minutes failed update checks should be cached before refetching them. + /// The number of minutes failed update checks should be cached before re-fetching them. public int ErrorCacheMinutes { get; set; } /// Update-check metadata to override. - public ModOverrideConfig[] ModOverrides { get; set; } + public ModOverrideConfig[] ModOverrides { get; set; } = Array.Empty(); + + /// The update-check config for SMAPI's own update checks. + public SmapiInfoConfig SmapiInfo { get; set; } = null!; } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs index 43969f51..62685e47 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs @@ -6,13 +6,10 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /********* ** Accessors *********/ - /// Whether to show SMAPI beta versions on the main page, if any. - public bool BetaEnabled { get; set; } - - /// A short sentence shown under the beta download button, if any. - public string BetaBlurb { get; set; } + /// A message to show below the download button (e.g. for details on downloading a beta version), in Markdown format. + public string? OtherBlurb { get; set; } /// A list of supports to credit on the main page, in Markdown format. - public string SupporterList { get; set; } + public string? SupporterList { get; set; } } } diff --git a/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs new file mode 100644 index 00000000..a95e0048 --- /dev/null +++ b/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs @@ -0,0 +1,17 @@ +using System; + +namespace StardewModdingAPI.Web.Framework.ConfigModels +{ + /// The update-check config for SMAPI's own update checks. + internal class SmapiInfoConfig + { + /// The mod ID used for SMAPI update checks. + public string ID { get; set; } = null!; + + /// The default update key used for SMAPI update checks. + public string DefaultUpdateKey { get; set; } = null!; + + /// The update keys to add for SMAPI update checks when the player has a beta version installed. + public string[] AddBetaUpdateKeys { get; set; } = Array.Empty(); + } +} diff --git a/src/SMAPI.Web/Framework/Extensions.cs b/src/SMAPI.Web/Framework/Extensions.cs index 5305b142..62a23155 100644 --- a/src/SMAPI.Web/Framework/Extensions.cs +++ b/src/SMAPI.Web/Framework/Extensions.cs @@ -26,10 +26,10 @@ namespace StardewModdingAPI.Web.Framework /// An object that contains route values. /// Get an absolute URL instead of a server-relative path/ /// The generated URL. - public static string PlainAction(this IUrlHelper helper, [AspMvcAction] string action, [AspMvcController] string controller, object values = null, bool absoluteUrl = false) + public static string? PlainAction(this IUrlHelper helper, [AspMvcAction] string action, [AspMvcController] string controller, object? values = null, bool absoluteUrl = false) { // get route values - RouteValueDictionary valuesDict = new RouteValueDictionary(values); + RouteValueDictionary valuesDict = new(values); foreach (var value in helper.ActionContext.RouteData.Values) { if (!valuesDict.ContainsKey(value.Key)) @@ -37,7 +37,7 @@ namespace StardewModdingAPI.Web.Framework } // get relative URL - string url = helper.Action(action, controller, valuesDict); + string? url = helper.Action(action, controller, valuesDict); if (url == null && action.EndsWith("Async")) url = helper.Action(action[..^"Async".Length], controller, valuesDict); @@ -45,7 +45,7 @@ namespace StardewModdingAPI.Web.Framework if (absoluteUrl) { HttpRequest request = helper.ActionContext.HttpContext.Request; - Uri baseUri = new Uri($"{request.Scheme}://{request.Host}"); + Uri baseUri = new($"{request.Scheme}://{request.Host}"); url = new Uri(baseUri, url).ToString(); } @@ -57,7 +57,7 @@ namespace StardewModdingAPI.Web.Framework /// The value to serialize. /// The serialized JSON. /// This bypasses unnecessary validation (e.g. not allowing null values) in . - public static IHtmlContent ForJson(this RazorPageBase page, object value) + public static IHtmlContent ForJson(this RazorPageBase page, object? value) { string json = JsonConvert.SerializeObject(value); return new HtmlString(json); diff --git a/src/SMAPI.Web/Framework/IModDownload.cs b/src/SMAPI.Web/Framework/IModDownload.cs index dc058bcb..fe171785 100644 --- a/src/SMAPI.Web/Framework/IModDownload.cs +++ b/src/SMAPI.Web/Framework/IModDownload.cs @@ -3,13 +3,16 @@ namespace StardewModdingAPI.Web.Framework /// Generic metadata about a file download on a mod page. internal interface IModDownload { + /********* + ** Accessors + *********/ /// The download's display name. string Name { get; } /// The download's description. - string Description { get; } + string? Description { get; } /// The download's file version. - string Version { get; } + string? Version { get; } } } diff --git a/src/SMAPI.Web/Framework/IModPage.cs b/src/SMAPI.Web/Framework/IModPage.cs index e66d401f..4d0a8d61 100644 --- a/src/SMAPI.Web/Framework/IModPage.cs +++ b/src/SMAPI.Web/Framework/IModPage.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Framework.UpdateData; namespace StardewModdingAPI.Web.Framework @@ -16,13 +17,13 @@ namespace StardewModdingAPI.Web.Framework string Id { get; } /// The mod name. - string Name { get; } + string? Name { get; } /// The mod's semantic version number. - string Version { get; } + string? Version { get; } /// The mod's web URL. - string Url { get; } + string? Url { get; } /// The mod downloads. IModDownload[] Downloads { get; } @@ -31,7 +32,12 @@ namespace StardewModdingAPI.Web.Framework RemoteModStatus Status { get; } /// A user-friendly error which indicates why fetching the mod info failed (if applicable). - string Error { get; } + string? Error { get; } + + /// Whether the mod data is valid. + [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))] + [MemberNotNullWhen(false, nameof(IModPage.Error))] + bool IsValid { get; } /********* @@ -42,7 +48,7 @@ namespace StardewModdingAPI.Web.Framework /// The mod's semantic version number. /// The mod's web URL. /// The mod downloads. - IModPage SetInfo(string name, string version, string url, IEnumerable downloads); + IModPage SetInfo(string name, string? version, string url, IEnumerable downloads); /// Set a mod fetch error. /// The mod availability status on the remote site. diff --git a/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs b/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs index 385c0c91..3c1405eb 100644 --- a/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs +++ b/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs @@ -9,7 +9,7 @@ namespace StardewModdingAPI.Web.Framework ** Fields *********/ /// An authorization filter that allows local requests. - private static readonly LocalRequestsOnlyAuthorizationFilter LocalRequestsOnlyFilter = new LocalRequestsOnlyAuthorizationFilter(); + private static readonly LocalRequestsOnlyAuthorizationFilter LocalRequestsOnlyFilter = new(); /********* diff --git a/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs b/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs index 42e283a9..a1384b8f 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Text; using StardewModdingAPI.Web.Framework.LogParsing.Models; @@ -11,22 +12,26 @@ namespace StardewModdingAPI.Web.Framework.LogParsing ** Fields *********/ /// The local time when the next log was posted. - public string Time { get; set; } + public string? Time { get; set; } /// The log level for the next log message. public LogLevel Level { get; set; } + /// The screen ID in split-screen mode. + public int ScreenId { get; set; } + /// The mod name for the next log message. - public string Mod { get; set; } + public string? Mod { get; set; } /// The text for the next log message. - private readonly StringBuilder Text = new StringBuilder(); + private readonly StringBuilder Text = new(); /********* ** Accessors *********/ /// Whether the next log message has been started. + [MemberNotNullWhen(true, nameof(LogMessageBuilder.Time), nameof(LogMessageBuilder.Mod))] public bool Started { get; private set; } @@ -36,10 +41,11 @@ namespace StardewModdingAPI.Web.Framework.LogParsing /// Start accumulating values for a new log message. /// The local time when the log was posted. /// The log level. + /// The screen ID in split-screen mode. /// The mod name. /// The initial log text. /// A log message is already started; call before starting a new message. - public void Start(string time, LogLevel level, string mod, string text) + public void Start(string time, LogLevel level, int screenId, string mod, string text) { if (this.Started) throw new InvalidOperationException("Can't start new message, previous log message isn't done yet."); @@ -48,6 +54,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing this.Time = time; this.Level = level; + this.ScreenId = screenId; this.Mod = mod; this.Text.Append(text); } @@ -65,18 +72,18 @@ namespace StardewModdingAPI.Web.Framework.LogParsing } /// Get a log message for the accumulated values. - public LogMessage Build() + public LogMessage? Build() { if (!this.Started) return null; - return new LogMessage - { - Time = this.Time, - Level = this.Level, - Mod = this.Mod, - Text = this.Text.ToString() - }; + return new LogMessage( + time: this.Time, + level: this.Level, + screenId: this.ScreenId, + mod: this.Mod, + text: this.Text.ToString() + ); } /// Reset to start a new log message. diff --git a/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs b/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs index 5d4c8c08..3f815e3e 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs @@ -10,6 +10,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing *********/ /// Construct an instance. /// The user-friendly error message. - public LogParseException(string message) : base(message) { } + public LogParseException(string message) + : base(message) { } } } diff --git a/src/SMAPI.Web/Framework/LogParsing/LogParser.cs b/src/SMAPI.Web/Framework/LogParsing/LogParser.cs index 227dcd89..c39e612b 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogParser.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogParser.cs @@ -14,38 +14,38 @@ namespace StardewModdingAPI.Web.Framework.LogParsing ** Fields *********/ /// A regex pattern matching the start of a SMAPI message. - private readonly Regex MessageHeaderPattern = new Regex(@"^\[(? + @if (Model.OtherBlurb != null) + { +
@Html.Raw(Markdig.Markdown.ToHtml(Model.OtherBlurb))
+ }
@@ -61,29 +48,11 @@
- @if (Model.BetaVersion == null) - { -

What's new

-
- @Html.Raw(Markdig.Markdown.ToHtml(Model.StableVersion.Description)) -
-

See the release notes and mod compatibility list for more info.

- } - else - { -

What's new in...

-

SMAPI @Model.StableVersion.Version?

-
- @Html.Raw(Markdig.Markdown.ToHtml(Model.StableVersion.Description)) -
-

See the release notes and mod compatibility list for more info.

- -

SMAPI @Model.BetaVersion.Version?

-
- @Html.Raw(Markdig.Markdown.ToHtml(Model.BetaVersion.Description)) -
-

See the release notes and mod compatibility list for more info.

- } +

What's new

+
+ @Html.Raw(Markdig.Markdown.ToHtml(Model.StableVersion.Description)) +
+

See the release notes and mod compatibility list for more info.

@@ -122,10 +91,6 @@

For mod creators

diff --git a/src/SMAPI.Web/Views/JsonValidator/Index.cshtml b/src/SMAPI.Web/Views/JsonValidator/Index.cshtml index 1db79857..f5ec0f7a 100644 --- a/src/SMAPI.Web/Views/JsonValidator/Index.cshtml +++ b/src/SMAPI.Web/Views/JsonValidator/Index.cshtml @@ -5,10 +5,10 @@ @{ // get view data - string curPageUrl = this.Url.PlainAction("Index", "JsonValidator", new { schemaName = Model.SchemaName, id = Model.PasteID }, absoluteUrl: true); - string newUploadUrl = this.Url.PlainAction("Index", "JsonValidator", new { schemaName = Model.SchemaName }); - string schemaDisplayName = null; - bool isValidSchema = Model.SchemaName != null && Model.SchemaFormats.TryGetValue(Model.SchemaName, out schemaDisplayName) && schemaDisplayName?.ToLower() != "none"; + string curPageUrl = this.Url.PlainAction("Index", "JsonValidator", new { schemaName = Model!.SchemaName, id = Model.PasteID }, absoluteUrl: true)!; + string newUploadUrl = this.Url.PlainAction("Index", "JsonValidator", new { schemaName = Model.SchemaName })!; + string? schemaDisplayName = null; + bool isValidSchema = Model.SchemaName != null && Model.SchemaFormats.TryGetValue(Model.SchemaName, out schemaDisplayName) && schemaDisplayName.ToLower() != "none"; // build title ViewData["Title"] = "JSON validator"; diff --git a/src/SMAPI.Web/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index d4ff4f10..b989417e 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -2,17 +2,36 @@ @using StardewModdingAPI.Toolkit.Utilities @using StardewModdingAPI.Web.Framework @using StardewModdingAPI.Web.Framework.LogParsing.Models +@using StardewModdingAPI.Web.ViewModels @model StardewModdingAPI.Web.ViewModels.LogParserModel @{ ViewData["Title"] = "SMAPI log parser"; - IDictionary contentPacks = Model.GetContentPacksByMod(); - IDictionary defaultFilters = Enum - .GetValues(typeof(LogLevel)) - .Cast() - .ToDictionary(level => level.ToString().ToLower(), level => level != LogLevel.Trace); - string curPageUrl = this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID }, absoluteUrl: true); + // get log info + ParsedLog? log = Model!.ParsedLog; + IDictionary contentPacks = Model.GetContentPacksByMod(); + ISet screenIds = new HashSet(log?.Messages.Select(p => p.ScreenId) ?? Array.Empty()); + + // detect suggested fixes + LogModInfo[] outdatedMods = log?.Mods.Where(mod => mod.HasUpdate).ToArray() ?? Array.Empty(); + LogModInfo? errorHandler = log?.Mods.FirstOrDefault(p => p.IsCodeMod && p.Name == "Error Handler"); + bool hasOlderErrorHandler = errorHandler?.GetParsedVersion() is not null && log?.ApiVersionParsed is not null && log.ApiVersionParsed.IsNewerThan(errorHandler.GetParsedVersion()); + bool isPyTkCompatibilityMode = log?.ApiVersionParsed?.IsOlderThan("3.15.0") is false && log.Mods.Any(p => p.IsCodeMod && p.Name == "PyTK" && p.GetParsedVersion()?.IsOlderThan("1.24.0") is true); + + // get filters + IDictionary defaultFilters = Enum + .GetValues() + .ToDictionary(level => level.ToString().ToLower(), level => level != LogLevel.Trace); + IDictionary logLevels = Enum + .GetValues() + .ToDictionary(level => (int)level, level => level.ToString().ToLower()); + IDictionary logSections = Enum + .GetValues() + .ToDictionary(section => (int)section, section => section.ToString()); + + // get form + string curPageUrl = this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID }, absoluteUrl: true)!; } @section Head { @@ -20,27 +39,83 @@ { } - - + + + - + + - - + + + + + } +@* quick navigation links *@ +@section SidebarExtra { + @if (log != null) + { + + } +} + @* upload result banner *@ @if (Model.UploadError != null) { @@ -59,7 +134,7 @@ else if (Model.ParseError != null) Error details: @Model.ParseError
} -else if (Model.ParsedLog?.IsValid == true) +else if (log?.IsValid == true) {