1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Move data directory to ApplicationData instead of Documents

This commit is contained in:
Robert Beekman 2017-11-30 12:06:47 +01:00
parent bc64ac33f8
commit ea2b7f6622
10 changed files with 85 additions and 69 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
@ -24,6 +25,9 @@ namespace Artemis
public ArtemisBootstrapper() public ArtemisBootstrapper()
{ {
// Make sure the data folder exists
GeneralHelpers.SetupDataFolder();
// Start logging before anything else // Start logging before anything else
Logging.SetupLogging(SettingsProvider.Load<GeneralSettings>().LogLevel); Logging.SetupLogging(SettingsProvider.Load<GeneralSettings>().LogLevel);
// Restore DDLs before interacting with any SDKs // Restore DDLs before interacting with any SDKs

View File

@ -19,10 +19,8 @@ namespace Artemis.DAL
{ {
public static class ProfileProvider public static class ProfileProvider
{ {
public static readonly string ProfileFolder = GeneralHelpers.DataFolder + "profiles\\";
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static readonly string ProfileFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\profiles";
private static bool _installedDefaults; private static bool _installedDefaults;
static ProfileProvider() static ProfileProvider()
@ -69,7 +67,7 @@ namespace Artemis.DAL
if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Slug?.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}";
if (!Directory.Exists(path)) if (!Directory.Exists(path))
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
@ -87,7 +85,7 @@ namespace Artemis.DAL
return; return;
} }
File.WriteAllText(path + $@"\{prof.Slug}.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);
} }
} }
@ -103,7 +101,7 @@ namespace Artemis.DAL
return; return;
// Store the profile path before it is renamed // Store the profile path before it is renamed
var oldPath = ProfileFolder + $@"\{profile.KeyboardSlug}\{profile.GameName}\{profile.Slug}.json"; var oldPath = ProfileFolder + $"{profile.KeyboardSlug}\\{profile.GameName}\\{profile.Slug}.json";
// Update the profile, creating a new file // Update the profile, creating a new file
profile.Name = name; profile.Name = name;
@ -117,7 +115,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.Slug}.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);
} }
@ -155,16 +153,15 @@ namespace Artemis.DAL
File.WriteAllText(path, json); File.WriteAllText(path, json);
} }
public static void InsertGif(string effectName, string profileName, string layerName, Bitmap gifFile, public static void InsertGif(string moduleName, string profileName, string layerName, Bitmap gifFile, string fileName)
string fileName)
{ {
var directories = new DirectoryInfo(ProfileFolder).GetDirectories(); var directories = new DirectoryInfo(ProfileFolder).GetDirectories();
var profiles = new List<ProfileModel>(); var profiles = new List<ProfileModel>();
foreach (var directoryInfo in directories) foreach (var directoryInfo in directories)
profiles.AddRange(ReadProfiles(directoryInfo.Name + "/effectName").Where(d => d.Name == profileName)); profiles.AddRange(ReadProfiles(directoryInfo.Name + "\\" + moduleName).Where(d => d.Name == profileName));
// Extract the GIF file // Extract the GIF file
var gifDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\gifs"; var gifDir = GeneralHelpers.DataFolder + "gifs";
Directory.CreateDirectory(gifDir); Directory.CreateDirectory(gifDir);
var gifPath = gifDir + $"\\{fileName}.gif"; var gifPath = gifDir + $"\\{fileName}.gif";
if (!File.Exists(gifPath)) if (!File.Exists(gifPath))
@ -184,7 +181,7 @@ namespace Artemis.DAL
public static List<ProfileModel> ReadProfiles(string subDirectory) public static List<ProfileModel> ReadProfiles(string subDirectory)
{ {
var profiles = new List<ProfileModel>(); var profiles = new List<ProfileModel>();
var directory = ProfileFolder + "/" + subDirectory; var directory = ProfileFolder + subDirectory;
if (!Directory.Exists(directory)) if (!Directory.Exists(directory))
return profiles; return profiles;
@ -193,7 +190,6 @@ namespace Artemis.DAL
// Parse the JSON files into objects and add them if they are valid // Parse the JSON files into objects and add them if they are valid
foreach (var path in profilePaths) foreach (var path in profilePaths)
{
try try
{ {
var prof = LoadProfileIfValid(path); var prof = LoadProfileIfValid(path);
@ -201,21 +197,15 @@ namespace Artemis.DAL
continue; continue;
// Only add unique profiles // Only add unique profiles
if (profiles.Any(p => p.GameName == prof.GameName && p.Name == prof.Name && if (profiles.Any(p => p.GameName == prof.GameName && p.Name == prof.Name && p.KeyboardSlug == prof.KeyboardSlug))
p.KeyboardSlug == prof.KeyboardSlug))
{
Logger.Info("Didn't load duplicate profile: {0}", path); Logger.Info("Didn't load duplicate profile: {0}", path);
}
else else
{
profiles.Add(prof); profiles.Add(prof);
}
} }
catch (Exception e) catch (Exception e)
{ {
Logger.Error("Failed to load profile: {0} - {1}", path, e); Logger.Error("Failed to load profile: {0} - {1}", path, e);
} }
}
return profiles; return profiles;
} }
@ -232,8 +222,7 @@ namespace Artemis.DAL
_installedDefaults = true; _installedDefaults = true;
// Load the ZIP from resources // Load the ZIP from resources
var stream = Assembly.GetExecutingAssembly() var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Artemis.Resources.Keyboards.default-profiles.zip");
.GetManifestResourceStream("Artemis.Resources.Keyboards.default-profiles.zip");
// Extract it over the old defaults in case one was updated // Extract it over the old defaults in case one was updated
if (stream == null) if (stream == null)
@ -241,7 +230,6 @@ namespace Artemis.DAL
var archive = new ZipArchive(stream); var archive = new ZipArchive(stream);
archive.ExtractToDirectory(ProfileFolder, true); archive.ExtractToDirectory(ProfileFolder, true);
InsertGif("GeneralProfile", "Demo (duplicate to keep changes)", "GIF", Resources.demo_gif, "demo-gif"); InsertGif("GeneralProfile", "Demo (duplicate to keep changes)", "GIF", Resources.demo_gif, "demo-gif");
} }
catch (IOException e) catch (IOException e)

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Artemis.Settings; using Artemis.Settings;
using Artemis.Utilities;
using Newtonsoft.Json; using Newtonsoft.Json;
using NLog; using NLog;
@ -11,10 +12,7 @@ namespace Artemis.DAL
public static class SettingsProvider public static class SettingsProvider
{ {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly string SettingsFolder = GeneralHelpers.DataFolder + "settings";
private static readonly string SettingsFolder =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\settings";
private static readonly List<IArtemisSettings> Settings = new List<IArtemisSettings>(); private static readonly List<IArtemisSettings> Settings = new List<IArtemisSettings>();
/// <summary> /// <summary>
@ -35,9 +33,7 @@ namespace Artemis.DAL
try try
{ {
var loadSettings = (IArtemisSettings) JsonConvert var loadSettings = (IArtemisSettings) JsonConvert.DeserializeObject<T>(File.ReadAllText(SettingsFolder + $"{typeof(T)}.json"));
.DeserializeObject<T>(File.ReadAllText(SettingsFolder + $@"\{typeof(T)}.json"));
if (loadSettings == null) if (loadSettings == null)
{ {
loadSettings = (IArtemisSettings) new T(); loadSettings = (IArtemisSettings) new T();
@ -80,7 +76,7 @@ namespace Artemis.DAL
return; return;
} }
File.WriteAllText(SettingsFolder + $@"\{artemisSettings.GetType()}.json", json); File.WriteAllText(SettingsFolder + $"{artemisSettings.GetType()}.json", json);
} }
/// <summary> /// <summary>

View File

@ -40,8 +40,7 @@ namespace Artemis.Modules.Games.Witcher3
public override void Enable() public override void Enable()
{ {
// Ensure the config file is found // Ensure the config file is found
var witcherSettings = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + var witcherSettings = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\The Witcher 3\\user.settings";
@"\The Witcher 3\user.settings";
if (File.Exists(witcherSettings)) if (File.Exists(witcherSettings))
_witcherSettings = witcherSettings; _witcherSettings = witcherSettings;

View File

@ -1,5 +1,4 @@
using System; using System.Diagnostics;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
@ -14,8 +13,7 @@ namespace Artemis.Modules.Games.Witcher3
{ {
public sealed class Witcher3ViewModel : ModuleViewModel public sealed class Witcher3ViewModel : ModuleViewModel
{ {
public Witcher3ViewModel(MainManager mainManager, [Named(nameof(Witcher3Model))] ModuleModel moduleModel, public Witcher3ViewModel(MainManager mainManager, [Named(nameof(Witcher3Model))] ModuleModel moduleModel, IKernel kernel) : base(mainManager, moduleModel, kernel)
IKernel kernel) : base(mainManager, moduleModel, kernel)
{ {
DisplayName = "The Witcher 3"; DisplayName = "The Witcher 3";
} }
@ -54,11 +52,9 @@ namespace Artemis.Modules.Games.Witcher3
using (var archive = new ZipArchive(stream)) using (var archive = new ZipArchive(stream))
{ {
// Look for any conflicting mods // Look for any conflicting mods
if (Directory.Exists(dialog.SelectedPath + @"\mods")) if (Directory.Exists(dialog.SelectedPath + "\\mods"))
{ {
var file = Directory.GetFiles(dialog.SelectedPath + @"\mods", "playerWitcher.ws", var file = Directory.GetFiles(dialog.SelectedPath + "\\mods", "playerWitcher.ws", SearchOption.AllDirectories).FirstOrDefault();
SearchOption.AllDirectories)
.FirstOrDefault();
if (file != null) if (file != null)
if (!file.Contains("modArtemis")) if (!file.Contains("modArtemis"))
{ {
@ -70,15 +66,9 @@ namespace Artemis.Modules.Games.Witcher3
if (!viewHelp.Value) if (!viewHelp.Value)
return; return;
// Put the mod in the documents folder instead // Put the mod in the data folder instead, not user friendly but access to documents is unsure
// Create the directory structure archive.ExtractToDirectory(GeneralHelpers.DataFolder + "witcher3-mod", true);
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + System.Diagnostics.Process.Start(new ProcessStartInfo("https://github.com/SpoinkyNL/Artemis/wiki/The-Witcher-3"));
@"\Artemis";
archive.ExtractToDirectory(folder + @"witcher3-mod", true);
System.Diagnostics.Process.Start(
new ProcessStartInfo("https://github.com/SpoinkyNL/Artemis/wiki/The-Witcher-3"));
return; return;
} }
} }

View File

@ -29,8 +29,8 @@ namespace Artemis.Utilities.DataReaders
#region Logitech #region Logitech
private static readonly string LogitechPath = @"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\"; private static readonly string LogitechPath = "C:\\Program Files\\Logitech Gaming Software\\SDK\\LED\\x64\\";
private static readonly string DllPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\dll"; private static readonly string DllPath = GeneralHelpers.DataFolder + "dll\\";
public static void PlaceLogitechDll() public static void PlaceLogitechDll()
{ {
@ -41,13 +41,13 @@ namespace Artemis.Utilities.DataReaders
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true) ?? var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true) ??
Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true); Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true);
key.SetValue(null, DllPath + @"\LogitechLed.dll"); key.SetValue(null, DllPath + "LogitechLed.dll");
// Make sure the fake DLL is in place // Make sure the fake DLL is in place
if (!Directory.Exists(DllPath)) if (!Directory.Exists(DllPath))
Directory.CreateDirectory(DllPath); Directory.CreateDirectory(DllPath);
if (!File.Exists(DllPath + @"\LogitechLed.dll")) if (!File.Exists(DllPath + "LogitechLed.dll"))
File.WriteAllBytes(DllPath + @"\LogitechLED.dll", Resources.LogitechLED); File.WriteAllBytes(DllPath + "LogitechLED.dll", Resources.LogitechLED);
} }
catch (Exception e) catch (Exception e)
{ {
@ -59,7 +59,7 @@ namespace Artemis.Utilities.DataReaders
{ {
// Change the registry key to point to the real DLL // Change the registry key to point to the real DLL
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true); var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true);
key?.SetValue(null, LogitechPath + @"\LogitechLed.dll"); key?.SetValue(null, LogitechPath + "LogitechLed.dll");
} }
#endregion #endregion

View File

@ -13,6 +13,29 @@ namespace Artemis.Utilities
{ {
public static class GeneralHelpers public static class GeneralHelpers
{ {
public static string DataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Artemis\\";
public static void SetupDataFolder()
{
if (!Directory.Exists(DataFolder))
Directory.CreateDirectory(DataFolder);
var oldDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Artemis\\";
try
{
if (!Directory.Exists(oldDataFolder))
return;
// Migrate the old Artemis folder if it's present and access to it can be gained
MoveDirectory(oldDataFolder, DataFolder);
}
catch (Exception)
{
// ignored, could be blocked, in that case tough luck!
}
}
/// <summary> /// <summary>
/// Perform a deep Copy of the object, using Json as a serialisation method. /// Perform a deep Copy of the object, using Json as a serialisation method.
/// </summary> /// </summary>
@ -66,7 +89,6 @@ namespace Artemis.Utilities
// At this point the loop is in the item type contained in the list // At this point the loop is in the item type contained in the list
PropertyCollection parent; PropertyCollection parent;
if (path.Contains("Item") && inList) if (path.Contains("Item") && inList)
{
parent = new PropertyCollection parent = new PropertyCollection
{ {
Type = propInfo.PropertyType.Name, Type = propInfo.PropertyType.Name,
@ -74,9 +96,7 @@ namespace Artemis.Utilities
Display = $"{path.Replace("Item.", "").Replace(".", " ")}{propInfo.Name}", Display = $"{path.Replace("Item.", "").Replace(".", " ")}{propInfo.Name}",
Path = $"{path.Replace("Item.", "")}{propInfo.Name}" Path = $"{path.Replace("Item.", "")}{propInfo.Name}"
}; };
}
else else
{
parent = new PropertyCollection parent = new PropertyCollection
{ {
Type = propInfo.PropertyType.Name, Type = propInfo.PropertyType.Name,
@ -84,7 +104,6 @@ namespace Artemis.Utilities
Display = $"{path.Replace(".", " ")}{propInfo.Name}", Display = $"{path.Replace(".", " ")}{propInfo.Name}",
Path = $"{path}{propInfo.Name}" Path = $"{path}{propInfo.Name}"
}; };
}
if (propInfo.PropertyType.BaseType?.Name == "Enum") if (propInfo.PropertyType.BaseType?.Name == "Enum")
{ {
@ -154,6 +173,27 @@ namespace Artemis.Utilities
return (T) Enum.Parse(typeof(T), value, true); return (T) Enum.Parse(typeof(T), value, true);
} }
public static void MoveDirectory(string source, string target)
{
var sourcePath = source.TrimEnd('\\', ' ');
var targetPath = target.TrimEnd('\\', ' ');
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories).GroupBy(Path.GetDirectoryName);
foreach (var folder in files)
{
var targetFolder = folder.Key.Replace(sourcePath, targetPath);
Directory.CreateDirectory(targetFolder);
foreach (var file in folder)
{
var fileName = Path.GetFileName(file);
if (fileName == null) continue;
var targetFile = Path.Combine(targetFolder, fileName);
if (File.Exists(targetFile)) File.Delete(targetFile);
File.Move(file, targetFile);
}
}
Directory.Delete(source, true);
}
public struct PropertyCollection public struct PropertyCollection
{ {
public string Display { get; set; } public string Display { get; set; }

View File

@ -119,7 +119,7 @@ namespace Artemis.Utilities
// Ensure the update folder exists // Ensure the update folder exists
var artemisFolder = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1); var artemisFolder = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1);
var updateFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Artemis\\updates"; var updateFolder = GeneralHelpers.DataFolder + "updates";
var updatePath = updateFolder + "\\" + releaseFile["name"].Value<string>(); var updatePath = updateFolder + "\\" + releaseFile["name"].Value<string>();
if (!Directory.Exists(updateFolder)) if (!Directory.Exists(updateFolder))
Directory.CreateDirectory(updateFolder); Directory.CreateDirectory(updateFolder);

View File

@ -96,8 +96,7 @@ namespace Artemis.ViewModels
public void OpenLog() public void OpenLog()
{ {
// Get the logging directory // Get the logging directory
var logDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) var logDir = new DirectoryInfo(GeneralHelpers.DataFolder + "logs");
+ @"\Artemis\logs");
// Get the newest log file // Get the newest log file
var currentLog = logDir.GetFiles().OrderByDescending(f => f.LastWriteTime).FirstOrDefault(); var currentLog = logDir.GetFiles().OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
// Open the file with the user's default program // Open the file with the user's default program

View File

@ -227,7 +227,7 @@ namespace Artemis.ViewModels.Flyouts
public void ShowLogs() public void ShowLogs()
{ {
var logPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\logs"; var logPath = GeneralHelpers.DataFolder + "logs";
System.Diagnostics.Process.Start(logPath); System.Diagnostics.Process.Start(logPath);
} }