Skip to content

Commit

Permalink
replaced parse which can throw exceptions. with tryparse.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebanisu committed Feb 25, 2020
1 parent a74d303 commit c9007ce
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Core/Saves/Saves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static partial class Saves
{
#region Fields

private const int SteamOffset = 0x184;
private const int GamesPerSlot = 30;
private const int Slots = 2;
private const int SteamOffset = 0x184;

#endregion Fields

Expand Down Expand Up @@ -101,8 +101,16 @@ private static void ProcessFiles(IEnumerable<string> files, string pattern)
//List<Action> actions = new List<Action>();
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
IEnumerable<Action> actions = files.Select(x => new { file = x, match = regex.Match(x) })
.Where(x => x.match.Success && x.match.Groups.Count > 1)
.Select(x => new { x.file, slot = int.Parse(x.match.Groups[1].Value) - 1, game = int.Parse(x.match.Groups[Slots].Value) - 1 })
.Where(x => x.match.Success && x.match.Groups.Count >= 3)
.Select(x =>
{
if (!int.TryParse(x.match.Groups[1].Value, out int slot) || !int.TryParse(x.match.Groups[2].Value, out int game))
{
slot = -1;
game = -1;
}
return new { x.file, slot = slot - 1, game = game - 1 };
})
.Where(x => x.slot < Slots && x.game < GamesPerSlot && x.game >= 0 && x.slot >= 0)
.Select(x => new Action(() => { Read(x.file, out FileList[x.slot, x.game]); }));
Memory.ProcessActions(actions.ToArray());
Expand Down

1 comment on commit c9007ce

@Sebanisu
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

also checked group count to be 3 or greater because i'm referencing 1 and 2 indexes. and 0 is always the entire string match. Was only checking for greater than 1.

Please sign in to comment.