1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-02 10:43:31 +00:00

Remove invalid filename symbols before save. This fixes #237

This commit is contained in:
SpoinkyNL 2016-12-22 15:22:13 +01:00
parent c73c671f01
commit 876e1d81d7
2 changed files with 14 additions and 6 deletions

View File

@ -67,7 +67,7 @@ namespace Artemis.DAL
lock (prof) lock (prof)
{ {
// Store the file // 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"); throw new ArgumentException("Profile is invalid. Name, GameName and KeyboardSlug are required");
var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}"; var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}";
@ -84,11 +84,11 @@ namespace Artemis.DAL
} }
catch (Exception e) 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; 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); 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) public static void DeleteProfile(ProfileModel prof)
{ {
// Remove the file // 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)) if (File.Exists(path))
File.Delete(path); File.Delete(path);
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
@ -11,6 +12,7 @@ using Artemis.Profiles.Layers.Models;
using Artemis.Profiles.Lua; using Artemis.Profiles.Lua;
using Artemis.Utilities; using Artemis.Utilities;
using Artemis.Utilities.ParentChild; using Artemis.Utilities.ParentChild;
using Newtonsoft.Json;
using Color = System.Windows.Media.Color; using Color = System.Windows.Media.Color;
using Point = System.Windows.Point; using Point = System.Windows.Point;
using Size = System.Windows.Size; using Size = System.Windows.Size;
@ -19,8 +21,11 @@ namespace Artemis.Profiles
{ {
public class ProfileModel public class ProfileModel
{ {
private readonly char[] _invalidFileNameChars;
public ProfileModel() public ProfileModel()
{ {
_invalidFileNameChars = Path.GetInvalidFileNameChars();
Layers = new ChildItemCollection<ProfileModel, LayerModel>(this); Layers = new ChildItemCollection<ProfileModel, LayerModel>(this);
} }
@ -33,6 +38,9 @@ namespace Artemis.Profiles
public int Height { get; set; } public int Height { get; set; }
public string LuaScript { get; set; } public string LuaScript { get; set; }
[JsonIgnore]
public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray());
public void FixOrder() public void FixOrder()
{ {
Layers.Sort(l => l.Order); Layers.Sort(l => l.Order);
@ -195,7 +203,7 @@ namespace Artemis.Profiles
protected bool Equals(ProfileModel other) protected bool Equals(ProfileModel other)
{ {
return string.Equals(Name, other.Name) && return string.Equals(Slug, other.Slug) &&
string.Equals(KeyboardSlug, other.KeyboardSlug) && string.Equals(KeyboardSlug, other.KeyboardSlug) &&
string.Equals(GameName, other.GameName); string.Equals(GameName, other.GameName);
} }
@ -212,7 +220,7 @@ namespace Artemis.Profiles
{ {
unchecked unchecked
{ {
var hashCode = Name?.GetHashCode() ?? 0; var hashCode = Slug?.GetHashCode() ?? 0;
hashCode = (hashCode*397) ^ (KeyboardSlug?.GetHashCode() ?? 0); hashCode = (hashCode*397) ^ (KeyboardSlug?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (GameName?.GetHashCode() ?? 0); hashCode = (hashCode*397) ^ (GameName?.GetHashCode() ?? 0);
return hashCode; return hashCode;