-
Notifications
You must be signed in to change notification settings - Fork 25
Resolve saves from the platform mapping, not only the play action #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca9d41f
Resolve saves from the platform mapping, not only the play action
gantoine 10b911d
Simplify the save-sync resolution and its supporting helpers
gantoine 35ad2be
Merge remote-tracking branch 'origin/main' into saves/mapping-fallbac…
gantoine 0e1246c
Tighten play-action refresh and share mapping checks
gantoine 5d5c8ef
Keep save sync on the launched emulator and guard action ownership
gantoine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using Newtonsoft.Json; | ||
| using Playnite.SDK; | ||
| using RomM.Models.RomM.Rom; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace RomM.Games | ||
| { | ||
| /// <summary> | ||
| /// The per-ROM sidecar ("{sha1}.json" under the plugin's data folder): the download | ||
| /// descriptors for every revision, the emulator mapping the ROM was imported under, and what | ||
| /// the importer last wrote onto the game's play action. | ||
| /// | ||
| /// Install, uninstall, the version menu and save sync all need it, and each used to carry its | ||
| /// own copy of the path building, the id parsing and the try/catch around a corrupt file. | ||
| /// </summary> | ||
| internal static class RomMGameData | ||
| { | ||
| public static string PathFor(string romDataPath, string sha1) => | ||
| Path.Combine(romDataPath ?? string.Empty, $"{sha1}.json"); | ||
|
|
||
| /// <summary> | ||
| /// The sidecar for a game id, or null when the id is malformed, the file is missing or its | ||
| /// contents cannot be read. What a missing sidecar means is the caller's business. | ||
| /// </summary> | ||
| public static RomMRomLocal Load(string romDataPath, string gameId, ILogger logger, string gameName = null) | ||
| { | ||
| if (!RomMGameId.TryParse(gameId, out int _, out string sha1)) | ||
| { | ||
| logger?.Error($"{gameName ?? gameId} GameID is malformed!"); | ||
| return null; | ||
| } | ||
|
|
||
| return LoadBySha1(romDataPath, sha1, logger, out string _, gameName); | ||
| } | ||
|
|
||
| /// <param name="rawJson"> | ||
| /// The file's text exactly as read, or null when there was none. A caller about to rewrite | ||
| /// the sidecar compares against this to decide whether anything changed, rather than | ||
| /// re-serialising what it has just parsed once per ROM. | ||
| /// </param> | ||
| public static RomMRomLocal LoadBySha1(string romDataPath, string sha1, ILogger logger, out string rawJson, string gameName = null) | ||
| { | ||
| rawJson = null; | ||
|
|
||
| var path = PathFor(romDataPath, sha1); | ||
| if (!File.Exists(path)) | ||
| return null; | ||
|
|
||
| try | ||
| { | ||
| var text = File.ReadAllText(path); | ||
| var data = JsonConvert.DeserializeObject<RomMRomLocal>(text); | ||
| rawJson = text; | ||
| return data; | ||
| } | ||
| catch (IOException ex) | ||
| { | ||
| // Save sync reads the sidecar off the UI thread while an import may be rewriting it; | ||
| // that is a transient sharing violation, not a corrupt file. | ||
| logger?.Warn(ex, $"{gameName ?? sha1} ROM data file could not be read."); | ||
| return null; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger?.Error(ex, $"{gameName ?? sha1} ROM data file is corrupted!"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static void Save(string romDataPath, string sha1, RomMRomLocal data) => | ||
| SaveJson(romDataPath, sha1, JsonConvert.SerializeObject(data)); | ||
|
|
||
| /// <summary> | ||
| /// For a caller that has already serialised the sidecar to compare it with what was on | ||
| /// disk, so it is not serialised a second time just to be written. | ||
| /// </summary> | ||
| public static void SaveJson(string romDataPath, string sha1, string json) => | ||
| File.WriteAllText(PathFor(romDataPath, sha1), json); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.