Allow customize data location

This commit is contained in:
qianlifeng 2023-09-06 09:28:48 +08:00
parent 12f9c1da88
commit 2215e05c70
5 changed files with 48 additions and 27 deletions

2
.editorconfig Normal file
View File

@ -0,0 +1,2 @@
[*]
max_line_length = 180

View File

@ -1,17 +0,0 @@
namespace Wox.Core;
public class Const
{
private const string Wox = "Wox";
private const string Plugins = "Plugins";
/// <summary>
/// Places for storing plugins, configs and etc
/// </summary>
public static readonly string DataDirectory = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox);
/// <summary>
/// Places for storing plugins
/// </summary>
public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins);
}

37
Wox.Core/DataLocation.cs Normal file
View File

@ -0,0 +1,37 @@
namespace Wox.Core;
public class DataLocation
{
private const string Wox = "wox";
private const string Plugins = "plugins";
/// <summary>
/// Places for storing plugins
/// </summary>
public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins);
/// <summary>
/// Places for storing plugins, configs and etc
/// We allow user to customize the data directory, so we need to store this customized location in a fixed-location file and read the real data directory from it
/// </summary>
public static string DataDirectory
{
get
{
var defaultDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox);
if (!Directory.Exists(defaultDataDirectory)) Directory.CreateDirectory(defaultDataDirectory);
var dataLocationFile = Path.Join(defaultDataDirectory, "location.txt");
if (!File.Exists(dataLocationFile)) File.WriteAllText(dataLocationFile, defaultDataDirectory);
var dataLocation = File.ReadAllText(dataLocationFile);
return dataLocation == "" ? defaultDataDirectory : dataLocation;
}
}
public static void EnsureDirectoryExist()
{
if (!Directory.Exists(DataDirectory)) Directory.CreateDirectory(DataDirectory);
if (!Directory.Exists(PluginsDirectory)) Directory.CreateDirectory(PluginsDirectory);
}
}

View File

@ -5,16 +5,6 @@
/// </summary>
public static class PluginManager
{
static PluginManager()
{
EnsurePluginDirectory();
}
private static void EnsurePluginDirectory()
{
if (!Directory.Exists(Const.PluginsDirectory)) Directory.CreateDirectory(Const.PluginsDirectory);
}
/// <summary>
/// because InitializePlugins needs API, so LoadPlugins needs to be called first
/// </summary>

View File

@ -1,6 +1,7 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Wox.Core;
using Wox.Core.Plugin;
using Wox.ViewModels;
using Wox.Views;
@ -24,6 +25,14 @@ public class App : Application
base.OnFrameworkInitializationCompleted();
Bootstrap();
}
/// <summary>
/// </summary>
private void Bootstrap()
{
DataLocation.EnsureDirectoryExist();
PluginManager.LoadPlugins();
}
}