Timberborn-Modding-Central / TimberAPI

The one and only
https://timberapi.com
GNU Lesser General Public License v2.1
33 stars 17 forks source link

Feature suggestion: Rewrite stock game paths to the resources #76

Closed ihsoft closed 1 year ago

ihsoft commented 1 year ago

Sometimes the stock game logic uses names of the resources instead of the full path or an instance. Then, it constructs the full path to the resource internally and tries to load it. Obviously, such an approach is not compatible with modding due to the modded resources are located at the different paths. When this happens people either reinvent the wheel by reimplementing the existing code or patch the game code with Harmony.

Some common examples:

  1. StatusSpecification.SpriteName . Those, who want to use the status system, have to patch StatusSpriteLoader.
  2. CursorService.SetCursor. Using of custom cursor would require either doing it via Unity or patching a private method.

The idea of this feature request is to come up with a solution to handle resource paths at the TAPI level. By either patching the common game classes or by rewriting the path at the lower level in AssetLoader.Load. E.g. like in this experimental branch.

KYPremco commented 1 year ago

Thanks, we have discussed this already in the discord but to summaire.

In general I'd want to keep TimberAPI as low maintances as possible with some exceptions I believe are a must (like category button). To make it easier to update with less downtime.

The suggestion you made is not bad, the big problem I see with it is that it will get confusing when to use _TAPI_RESOURCE_ for a resource.

So my guess would go to a specification like AssetSpecification.TimberApi.json. If I can do it without name AssetSpecification.json with the following content:

{
    "RemovePrefixes": [
        "Spires/StatusIcons"
    ]
}

Any modder can just add a prefix from the game to the list and the bug will be fixed automaticly.

The RemovePrefixes isn't the best name, suggestions are welcome.

KYPremco commented 1 year ago

@ihsoft The specification has become: AssetSpecification.json. usage:

json:

{
  "IgnoreDirectoryPrefixes": [
    "Sprites/StatusIcons"
  ]
}
ihsoft commented 1 year ago

The prefix gets removed as specified, i.e. Sprites/StatusIcons. However, the actual path that is being resolved is Sprites/StatusIcons\sprite_name. Where \ is Path.PathSeparator and not just a constant symbol. I'd expect it be / on linux-like machines. So, as of now the onle way to make it working is defining all the versions with the separators:

{
  "IgnoreDirectoryPrefixes": [
    "Sprites/StatusIcons/",
    "Sprites/StatusIcons\\",
    "UI/Cursors/",
    "UI/Cursors\\"
  ]
}
KYPremco commented 1 year ago

The prefix gets removed as specified, i.e. Sprites/StatusIcons. However, the actual path that is being resolved is Sprites/StatusIcons\sprite_name. Where \ is Path.PathSeparator and not just a constant symbol. I'd expect it be / on linux-like machines. So, as of now the onle way to make it working is defining all the versions with the separators:

{
  "IgnoreDirectoryPrefixes": [
    "Sprites/StatusIcons/",
    "Sprites/StatusIcons\\",
    "UI/Cursors/",
    "UI/Cursors\\"
  ]
}

The leading slash must be included if they also automaitcly include it in their string. I don't see this as a problem with the feature but good catch.

I don't think linux is required to have the Sprites/StatusIcons\\ since it is not a files based system and is something from unity itself. to be sure you could ask @ginfuyou on discord to test your mod without the back slashes.

Edit: I overread the Path.PathSeparator... try to test it out and I hope it doesn't require it, but I cannot quite fix it either and your fix looks fine to me.

ihsoft commented 1 year ago

Here is a code snippet for the custom cursors:

    private CustomCursor GetCursor(string cursorName)
    {
        return _resourceAssetLoader.Load<CustomCursor>(Path.Combine(CursorDirectory, cursorName));
    }
KYPremco commented 1 year ago

Yeah that will switch between slashes depending on the OS. Though I find that it sets double backslashes a bit weird. It will be a small inconvenience to add multiple whenever this is used. Whenever I'll make a guide for it I will add it as a warning to keep this in mind whereever Timberborn does this.

ihsoft commented 1 year ago

Double slashes are only needed due to it's a JSON file. It's escaping of a specail symbol. Once the JSON is read and decoded, there will be just one back-slash. So, it's one symbol in both cases. Either \ or /.

KYPremco commented 1 year ago

Ah right 👍, well I hope the feature will come in handy. No more need to wait for me until I would fix it.

ihsoft commented 1 year ago

Yeah, it's usable as is :) Thanks!