From 876e1d81d74ed72240d2364c32611d19b8e0d036 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Thu, 22 Dec 2016 15:22:13 +0100 Subject: [PATCH] Remove invalid filename symbols before save. This fixes #237 --- Artemis/Artemis/DAL/ProfileProvider.cs | 8 ++++---- Artemis/Artemis/Profiles/ProfileModel.cs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index 1e0a61dca..5b757726e 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -67,7 +67,7 @@ namespace Artemis.DAL lock (prof) { // Store the file - if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Name?.Length > 1)) + if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Slug?.Length > 1)) throw new ArgumentException("Profile is invalid. Name, GameName and KeyboardSlug are required"); var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}"; @@ -84,11 +84,11 @@ namespace Artemis.DAL } catch (Exception e) { - Logger.Error(e, "Couldn't save profile '{0}.json'", prof.Name); + Logger.Error(e, "Couldn't save profile '{0}.json'", prof.Slug); return; } - File.WriteAllText(path + $@"\{prof.Name}.json", json); + File.WriteAllText(path + $@"\{prof.Slug}.json", json); Logger.Debug("Saved profile {0}/{1}/{2}", prof.KeyboardSlug, prof.GameName, prof.Name); } } @@ -114,7 +114,7 @@ namespace Artemis.DAL public static void DeleteProfile(ProfileModel prof) { // Remove the file - var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}\{prof.Name}.json"; + var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}\{prof.Slug}.json"; if (File.Exists(path)) File.Delete(path); } diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index 84ebaac4d..7ec550697 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Linq; using System.Windows; using System.Windows.Media; @@ -11,6 +12,7 @@ using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Lua; using Artemis.Utilities; using Artemis.Utilities.ParentChild; +using Newtonsoft.Json; using Color = System.Windows.Media.Color; using Point = System.Windows.Point; using Size = System.Windows.Size; @@ -19,8 +21,11 @@ namespace Artemis.Profiles { public class ProfileModel { + private readonly char[] _invalidFileNameChars; + public ProfileModel() { + _invalidFileNameChars = Path.GetInvalidFileNameChars(); Layers = new ChildItemCollection(this); } @@ -33,6 +38,9 @@ namespace Artemis.Profiles public int Height { get; set; } public string LuaScript { get; set; } + [JsonIgnore] + public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray()); + public void FixOrder() { Layers.Sort(l => l.Order); @@ -195,7 +203,7 @@ namespace Artemis.Profiles protected bool Equals(ProfileModel other) { - return string.Equals(Name, other.Name) && + return string.Equals(Slug, other.Slug) && string.Equals(KeyboardSlug, other.KeyboardSlug) && string.Equals(GameName, other.GameName); } @@ -212,7 +220,7 @@ namespace Artemis.Profiles { unchecked { - var hashCode = Name?.GetHashCode() ?? 0; + var hashCode = Slug?.GetHashCode() ?? 0; hashCode = (hashCode*397) ^ (KeyboardSlug?.GetHashCode() ?? 0); hashCode = (hashCode*397) ^ (GameName?.GetHashCode() ?? 0); return hashCode;