GameMaker / FoundryPLANT

1 stars 0 forks source link

Write a feature to fix broken image / token / avatar / item / etc. links #10

Open GameMaker opened 3 years ago

GameMaker commented 3 years ago

Load the collection. Iterate over them all Find broken links Copy the filename Look in the Forge Asset Manager:

cachedAssets = await ForgeAPI.call('assets'); for the Forge (returns array)

Try to find the file. If you find only one, fix it in memory. If you find more than one file with the same filename, but they're the same size and hash, you can deconflict and keep just one (user prompt) Then Actor.update([{_id: actor1_id, img: img1}, {_id: actor2_id, img: img2}, ....]) instead of multiple Actor.update({_id: single_actor_id, img: img}) basically game.actors.entities.forEach(actor => checkImgExists(actor.img)) where checkImgExists uses something like cachedAssets.find(a => a.url === actor.img)

More suggestions: filename = actor.img.split("/").pop() will get you the filename can do the same on name field and you can compare make sure you only check paths that start with https://assets.forge-vtt.com/ of course otherwise, if you have a url to somewhere else, that would break but if you have a relative path (i.e: doesn't start with https://), then you may want to look for the whole path in the assets library, cause that would also be a missing file... unless it points to a core icons/ field probably worth checking that the url also includes your own user id, because things could be referring to images from the bazaar (the dnd5e compendium has its own images pointing to the bazaar for spell icons and things like that for example) so there's slightly more logic that needs to go into it if you don't want it to start coughing at the first unexpected url

GameMaker commented 3 years ago

Check second comment on https://www.reddit.com/r/ForgeVTT/comments/jlx1dx/way_to_fix_all_broken_asset_links/

GameMaker commented 3 years ago

Another possibly useful gist: let badTokens = game.actors.filter(a => a.data.token.img.includes("AppData")); let updates = badTokens.map(a => {return {_id: a.data._id, "token.img": a.data.img}}); Actor.update(updates);