using System;
using System.IO;
using PCL.Core.Utils.OS;
using Special = System.Environment.SpecialFolder;
namespace PCL.Core.App;
///
/// Application global paths.
/// NOTE: The behaviors of all path strings depends on API provided by
/// .NET standard library. You should use and other APIs relative to it to process
/// any path string from this service, rather than concat paths manually.
///
public static class Paths
{
///
/// The default directory used for relative path combining.
///
public static string DefaultDirectory => Basics.ExecutableDirectory;
private static string _data;
private static string _sharedData;
private static string _sharedLocalData;
private static string _temp;
///
/// Per-instance data directory.
///
public static string Data { get => _data; set => _data = value; }
///
/// Shared synchronized data directory.
///
public static string SharedData { get => _sharedData; set => _sharedData = value; }
///
/// Shared synchronized data directory of old versions.
/// Keep the value just for migration, DO NOT USE IT.
///
public static string OldSharedData { get; set; }
///
/// Shared local data directory, used to put some large files that can be released or downloaded back anytime.
///
public static string SharedLocalData { get => _sharedLocalData; set => _sharedLocalData = value; }
///
/// Temporary files directory (can be deleted anytime, except when the program is running).
///
public static string Temp { get => _temp; set => _temp = value; }
///
/// Get path string relative to a special folder.
///
/// the special folder
/// the relative path
/// the path string relative to the special folder
public static string GetSpecialPath(Special folder, string relative)
{
var folderPath = Environment.GetFolderPath(folder);
return Path.Combine(folderPath, relative);
}
static Paths()
{
#if DEBUG
const string name = "PCLCE_Debug";
const string oldName = ".PCLCEDebug";
#else
const string name = "PCLCE";
const string oldName = ".PCLCE";
#endif
// fill paths
_data = Path.Combine(DefaultDirectory, "PCL");
_sharedData = GetSpecialPath(Special.ApplicationData, name);
_sharedLocalData = GetSpecialPath(Special.LocalApplicationData, name);
_temp = Path.Combine(Path.GetTempPath(), name);
OldSharedData = GetSpecialPath(Special.ApplicationData, oldName);
#if DEBUG
// read environment variables
EnvironmentInterop.ReadVariable("PCL_PATH", ref _data);
EnvironmentInterop.ReadVariable("PCL_PATH_SHARED", ref _sharedData);
EnvironmentInterop.ReadVariable("PCL_PATH_LOCAL", ref _sharedLocalData);
EnvironmentInterop.ReadVariable("PCL_PATH_TEMP", ref _temp);
#endif
// create directories
Directory.CreateDirectory(_data);
Directory.CreateDirectory(_sharedData);
Directory.CreateDirectory(_sharedLocalData);
Directory.CreateDirectory(_temp);
}
}