SMAPI icon indicating copy to clipboard operation
SMAPI copied to clipboard

Add config schema generation

Open erri120 opened this issue 1 year ago • 1 comments

Resolves #928.

Changes

  • Adds a new dependency to SMAPI.Toolkit: Newtonsoft.Json.Schema for schema generation.
  • Adds GenerateConfigSchema option to SConfig that's set to false by default.
  • Adds IDataHelper.WriteJsonSchemaFile and JsonHelper.WriteJsonSchemaFile to generate and write the schema to file.
  • Updates ModHelper.ReadConfig to generate the file config.schema.json.

Notes

I think develop references DLLs from Stardew Valley 1.6 that I don't have access with, so I had to create the code on stable and port it to develop.

I'm not sure how to test this PR, there aren't any unit tests that I can run. I thought about installing latest SMAPI normally and just overwriting the DLLs in the game folder with the ones I build using the PR. However, I don't even know if that works with the .NET DLL resolution process...

erri120 avatar Feb 26 '24 12:02 erri120

You'd be correct in saying that develop references Stardew Valley 1.6. Getting access is as simple as joining the Stardew Valley Discord server and asking in the #making-mods channel to get added (not even pinging anyone - anyone already in the alpha can add you).

Shockah avatar Feb 26 '24 12:02 Shockah

Thanks! Unfortunately Json.NET Schema isn't free, and the AGPL version is limited to 1000 validations per hour. That could easily be reached in our case (since players can have hundreds or thousands of mods, they may launch the game more than once per hour, and mods often read/write their config files more than once).

Pathoschild avatar Jul 06 '24 21:07 Pathoschild

Updated PR to use NJsonSchema.

erri120 avatar Jul 07 '24 10:07 erri120

Unfortunately NJsonSchema fails for many mods (see log). A few sample cases:

mod config result
Automate config crashes with InvalidOperationException.
Chests Anywhere config ❌ ditto Automate.
CJB Cheats Menu config ❌ ditto Automate.
CJB Item Spawner config ❌ ditto Automate.
CJB Show Item Sell Price config ❌ creates a schema for an empty object: { "$schema": "...", title: "ModConfig", type: "object", "additionalProperties": false }.
Content Patcher config ❌ ditto Automate.
Crops Anytime Anywhere config ✅ Seems correct.
Data Layers config ❌ ditto Automate.
Debug Mode config ❌ ditto Automate.
Fast Animations config ✅ Seems correct.
Generic Mod Config Menu config ❌ only includes ScrollSpeed property.
Horse Flute Anywhere config ❌ ditto Automate.
NPC Location Compass config ✅ Seems correct.
Skip Intro config ✅ Seems correct.

It seems to only work with properties (not fields), and fails with some common types like KeybindList (maybe due to the get-only properties?).

I rebased the PR branch onto the latest develop, so you can try it like this:

  1. Reset to the latest PR branch.
  2. Open SMAPI in Visual Studio.
  3. In SMAPI/Constants.cs, change the MinimumGameVersion back to 1.6.8.
  4. Click Build > Rebuild Solution to deploy the SMAPI files.
  5. Click Debug > Start Without Debugging to launch SMAPI.

Pathoschild avatar Jul 07 '24 17:07 Pathoschild