Skip to content

Add GithubTags mod source#416

Merged
TaroEld merged 2 commits into
MSUTeam:developmentfrom
Suor:updates-github-tags
Sep 17, 2024
Merged

Add GithubTags mod source#416
TaroEld merged 2 commits into
MSUTeam:developmentfrom
Suor:updates-github-tags

Conversation

@Suor

@Suor Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor

Usage:

mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.GitHubTags,
    // A link into a subdir here, but repo itself is also allowed
    "https://github.com/Suor/battle-brothers-mods/tree/master/autopilot"
    // tag prefix, for i.e. tag autopilot-new-2.2.0 be 2.2.0 release
    {Prefix = "autopilot-new-"});
mod.Mod.Registry.setUpdateSource(::MSU.System.Registry.ModSourceDomain.GitHubTags);

// Can still add this for icon with a link
mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.NexusMods,
    "https://www.nexusmods.com/battlebrothers/mods/675");

Comment thread msu/systems/registry/mod_sources/mod_source_githubtags.nut Outdated
@Enduriel

Enduriel commented Sep 4, 2024

Copy link
Copy Markdown
Member

Could you also mock up some doc for this, so that we could see how it should be implemented and so that it could be easily copied into the MSU wiki later?

@Suor Suor force-pushed the updates-github-tags branch from 37bcd3d to b9a3280 Compare September 4, 2024 10:07
@Suor

Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

Could you also mock up some doc for this, so that we could see how it should be implemented and so that it could be easily copied into the MSU wiki later?

Not sure what do you mean. I have an example in commit message above, can copy that to Wiki.

Or you mean a way to implement an extra type of source? I don't know a way to document it concise enough so that it will be more useful than reading actual source code :)

@Suor

Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

Maybe change this:

    function addModSource( _domain, _url )
    {
        ::MSU.requireOneFromTypes(["string", "table"], _url);
        this.__ModSources[_domain] <- ::MSU.System.Registry.ModSources[_domain](_url);
    }

to

    function addModSource( _domain, _url, _opts = null )
    {
        ::MSU.requireString(_url);
        if (_opts != null) ::MSU.requireTable(_opts);
        this.__ModSources[_domain] <- ::MSU.System.Registry.ModSources[_domain](_url, _opts);
    }

?

@Enduriel

Enduriel commented Sep 4, 2024

Copy link
Copy Markdown
Member

I mean the method of implementing the additional source, this would have to be documented in the MSU wiki as that is standard practice for MSU, and it's supposed to make it more accessible. We don't really point people at the source code, an explanation + an implementation example is required :/.

@Enduriel

Enduriel commented Sep 4, 2024

Copy link
Copy Markdown
Member

and yes I much prefer your suggested change

@Suor

Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

I mean the method of implementing the additional source, this would have to be documented in the MSU wiki as that is standard practice for MSU, and it's supposed to make it more accessible.

Heh, do you mean implementing inside or outside MSU? And someone adding this simply from docs is just not realistic :). Say you have docs for custom mod settings items, but there is no way I could have done that not reading source throughly. I will try to come up with something though.

@Suor Suor force-pushed the updates-github-tags branch 2 times, most recently from 42aca3a to d1fd215 Compare September 4, 2024 10:41
@Suor

Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

Ok, added the suggested change.

@Enduriel

Enduriel commented Sep 4, 2024

Copy link
Copy Markdown
Member

I think custom mod settings is a fair example, but admittedly that stuff is pretty complex and requires working with a lot of existing systems within MSU. The GitHub tags shouldn't actually be that complicated to implement from what I understand, and doesn't require interfacing with any internal stuff, so I don't think it should be that problematic?

@Suor

Suor commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

Ok, here it is:

To add new mod source one needs to do several things:

  1. Add a new item to ::MSU.System.Registry.ModSourceDomain enum.
  2. Implement ::MSU.Class.ModSource descendant.
  3. Register it with a ::MSU.System.Registry.addNewModSource() call.
  4. (optionally) Add Regex and BadURLMessage to restrict allowed urls.
  5. (optionally) Add Icon to show icon link to a mod on your source.
  6. (optionally) Provide .getUpdateURL() and .extractRelease() methods to support update checks.

Here is a minimal example needed to add a new source, I am providing a way to add links to
a non-existent hacky-stash.game website here:

::MSU.System.Registry.ModSourceDomain.add("HackyStash");
::YourNamespace.ModSourceHackyStash <- class extends ::MSU.Class.ModSource
{
    static ModSourceDomain = ::MSU.Class.RegistrySystem.ModSourceDomain.HackyStash;
}
::MSU.System.Registry.addNewModSource(::HackMSU.ModSourceHackyStash);

Let's add URL validation and some icon:

::YourNamespace.ModSourceHackyStash <- class extends ::MSU.Class.ModSource
{
    ...
    static Regex = regexp(@"https://hacky-stash.game/mods/([-\w]+)");
    static BadURLMessage = "A link must point into directly to a mod page, e.g. 'https://hacky-stash.game/mods/cleaver-axe-overhaul'";
    static Icon = "github";  // Only github or nexusmods images go with MSU
}

Now any typos and other mishaps in the URL will be caught, like people typing in https://stashy-hack.mage/mod/more-zombies or something. Also an icon link to hacky-stash.game will show up as long as a mod author will provide another way of checking for updates.

To enable update checking for our mod source we will need to add two extra methods:

::YourNamespace.ModSourceHackyStash <- class extends ::MSU.Class.ModSource
{
    ...
    function getUpdateURL()
    {
        local capture = this.Regex.capture(this.__URL);
        local slug = ::MSU.regexMatch(capture, this.__URL, 1);
        // Forturnately hacky slash provides an API
        return format("https://api.hacky-stash.game/mods/%s/releases", slug)
    }

    function extractRelease(_data) {
        // say we get a JSON with with all releases and the latest one goes first
        if (_data.len() == 0) // protect against no releases
            return null;      // null means no info, MSU gets it
        local release = _data[0];
        // For MSU to understand us we need to send a table with 2 keys:
        //     Version - a semver string of latest available release
        //     Changes - a description of changes brought by it, pass empty string if absent 
        return {Version = release.version, Changes = release.message || ""}
    }
}

@Suor Suor force-pushed the updates-github-tags branch from d1fd215 to fd757fe Compare September 4, 2024 11:33
@Enduriel

Enduriel commented Sep 4, 2024

Copy link
Copy Markdown
Member

Thank you, this is all pretty great, one final thing that I'd ask that will be pretty annoying, but could you separate the refactor (cleaning up the way mod sources are defined) and the feat (adding the github tags mod source) into 2 separate commits? Once that's done I'd be happy to approve, really like this.

@Suor Suor force-pushed the updates-github-tags branch 2 times, most recently from aa8bbc0 to 39ac5f1 Compare September 5, 2024 02:42
@Suor

Suor commented Sep 5, 2024

Copy link
Copy Markdown
Contributor Author

Ok, done.

@TaroEld TaroEld left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks for the refactor, just needs a few adaptations.

Comment thread msu/systems/registry/mod_sources/mod_source.nut
Comment thread msu/systems/registry/mod_sources/mod_source_githubtags.nut Outdated
Comment thread msu/systems/registry/mod_sources/mod_source_githubtags.nut Outdated
Comment thread msu/systems/registry/mod_sources/mod_source.nut Outdated
Comment thread msu/systems/registry/mod_sources/mod_source_githubtags.nut
- extract URL validation to the base class
- add a way to supply extra options
- add Icon static attr to disentangle icon from enum/class
- add `.extractRelease()` to cleanup data in a source-specific manner
- map fetched data to update info in `.checkIfModVersionsAreNew()`
instead of passing through and patching it
@Suor Suor force-pushed the updates-github-tags branch 2 times, most recently from e611d14 to 0497431 Compare September 6, 2024 11:44
Usage:

```squirrel
mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.GitHubTags,
    "https://github.com/Suor/battle-brothers-mods/tree/master/autopilot"
    // tag prefix, for i.e. tag autopilot-new-2.2.0 be 2.2.0 release
    {Prefix = "autopilot-new-"});
mod.Mod.Registry.setUpdateSource(::MSU.System.Registry.ModSourceDomain.GitHubTags);

// Can still add this for icon with a link
mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.NexusMods,
    "https://www.nexusmods.com/battlebrothers/mods/675");
```
@Suor Suor force-pushed the updates-github-tags branch from 0497431 to 26415de Compare September 6, 2024 13:29
@TaroEld TaroEld self-requested a review September 6, 2024 13:35
@TaroEld TaroEld merged commit c29438f into MSUTeam:development Sep 17, 2024
TaroEld pushed a commit that referenced this pull request Sep 17, 2024
* refactor: mod sources

- extract URL validation to the base class
- add a way to supply extra options
- add Icon static attr to disentangle icon from enum/class
- add `.extractRelease()` to cleanup data in a source-specific manner
- map fetched data to update info in `.checkIfModVersionsAreNew()`
instead of passing through and patching it

* feat: add GithubTags mod source

Usage:

```squirrel
mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.GitHubTags,
    "https://github.com/Suor/battle-brothers-mods/tree/master/autopilot"
    // tag prefix, for i.e. tag autopilot-new-2.2.0 be 2.2.0 release
    {Prefix = "autopilot-new-"});
mod.Mod.Registry.setUpdateSource(::MSU.System.Registry.ModSourceDomain.GitHubTags);

// Can still add this for icon with a link
mod.Mod.Registry.addModSource(
    ::MSU.System.Registry.ModSourceDomain.NexusMods,
    "https://www.nexusmods.com/battlebrothers/mods/675");
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants