namespace StockSharp.Configuration;
using System.Text;
using Newtonsoft.Json;
using NuGet.Configuration;
///
/// System paths.
///
public static class Paths
{
static Paths()
{
var companyPath = PathsHolder.CompanyPath ?? ConfigManager.TryGet("companyPath");
CompanyPath = companyPath.IsEmpty() ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "StockSharp") : companyPath.ToFullPathIfNeed();
CredentialsFile = Path.Combine(CompanyPath, $"credentials{DefaultSettingsExt}");
AppName = ConfigManager.TryGet("appName", TypeHelper.ApplicationName);
AppDataPath = GetAppDataPath(PathsHolder.AppDataPath ?? ConfigManager.TryGet("settingsPath"), AppName2);
PlatformConfigurationFile = Path.Combine(AppDataPath, $"platform_config{DefaultSettingsExt}");
ProxyConfigurationFile = Path.Combine(CompanyPath, $"proxy_config{DefaultSettingsExt}");
SecurityNativeIdDir = Path.Combine(AppDataPath, "NativeId");
SecurityMappingDir = Path.Combine(AppDataPath, "Symbol mapping");
SecurityExtendedInfo = Path.Combine(AppDataPath, "Extended info");
StorageDir = Path.Combine(AppDataPath, "Storage");
SnapshotsDir = Path.Combine(AppDataPath, "Snapshots");
CandlePatternsFile = Path.Combine(AppDataPath, $"candle_patterns{DefaultSettingsExt}");
CompilerCacheDir = Path.Combine(AppDataPath, "compiler_cache");
LogsDir = Path.Combine(AppDataPath, "Logs");
ReportLogsPath = Path.Combine(AppDataPath, "BugReports");
PythonUtilsPath = Path.Combine(CompanyPath, "Python");
InstallerDir = Path.Combine(CompanyPath, "Installer");
InstallerInstallationsConfigPath = Path.Combine(InstallerDir, $"installer_apps_installed{DefaultSettingsExt}");
try
{
var settings = Settings.LoadDefaultSettings(null);
HistoryDataPath = GetHistoryDataPath(SettingsUtility.GetGlobalPackagesFolder(settings));
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex);
}
}
///
/// Get .
///
/// Relative .
///
///
public static string GetAppDataPath(string appDataPath, string appName)
=> appDataPath.IsEmpty()
? Path.Combine(CompanyPath, appName)
: appDataPath.ToFullPathIfNeed();
///
/// Get history data path.
///
/// Directory.
/// History data path.
public static string GetHistoryDataPath(string startDir)
{
static DirectoryInfo FindHistoryDataSubfolder(DirectoryInfo packageRoot)
{
if (!packageRoot.Exists)
return null;
foreach (var di in packageRoot.GetDirectories().OrderByDescending(di => di.Name))
{
var d = new DirectoryInfo(Path.Combine(di.FullName, "HistoryData"));
if (d.Exists)
return d;
}
return null;
}
var dir = new DirectoryInfo(Path.GetDirectoryName(startDir));
while (dir != null)
{
var hdRoot = FindHistoryDataSubfolder(new DirectoryInfo(Path.Combine(dir.FullName, "packages", "stocksharp.samples.historydata")));
if (hdRoot != null)
return hdRoot.FullName;
dir = dir.Parent;
}
return null;
}
///
/// App title.
///
public static readonly string AppName;
///
///
///
public static string AppName2 => AppName.Remove("S#.", true);
///
/// App title with version.
///
public static string AppNameWithVersion => $"{AppName} v{InstalledVersion}";
///
/// The path to directory with all applications.
///
public static readonly string CompanyPath;
///
/// The path to the file with credentials.
///
public static readonly string CredentialsFile;
///
/// The path to the settings directory.
///
public static readonly string AppDataPath;
///
/// The path to the Python utils directory.
///
public static readonly string PythonUtilsPath;
///
/// The path to the settings directory.
///
public static readonly string ReportLogsPath;
///
/// The path to the configuration file of platform definition.
///
public static readonly string PlatformConfigurationFile;
///
/// The path to the configuration file of proxy settings.
///
public static readonly string ProxyConfigurationFile;
///
/// The path to the directory with native security identifiers.
///
public static readonly string SecurityNativeIdDir;
///
/// The path to the directory with securities id mapping.
///
public static readonly string SecurityMappingDir;
///
/// The path to the directory with securities extended info.
///
public static readonly string SecurityExtendedInfo;
///
/// The path to the directory with market data.
///
public static readonly string StorageDir;
///
/// The path to the directory with snapshots of market data.
///
public static readonly string SnapshotsDir;
///
/// The path to the file with candle patterns.
///
public static readonly string CandlePatternsFile;
///
/// The path to the compiler cache directory.
///
public static readonly string CompilerCacheDir;
///
/// The path to the logs directory.
///
public static readonly string LogsDir;
///
/// The path to the installer directory.
///
public static readonly string InstallerDir;
///
/// The path to the installer directory.
///
public static readonly string InstallerInstallationsConfigPath;
///
/// Installer UI exe name.
///
public const string InstallerUIName = "StockSharp.Installer.UI";
///
/// Installer console exe name.
///
public const string InstallerConsoleName = "StockSharp.Installer.Console";
///
/// Setup name.
///
public const string SetupName = "stocksharp_setup";
///
/// Web site domain.
///
public static string Domain => LocalizedStrings.StockSharpDomain;
///
/// Get website url.
///
/// Localized url.
public static string GetWebSiteUrl() => $"https://stocksharp.{Domain}";
///
/// Get logo url.
///
/// Logo url.
public static string GetLogoUrl() => $"{GetWebSiteUrl()}/images/logo.png";
///
/// Chat in Telegram.
///
public static string Chat => LocalizedStrings.StockSharpChatUrl;
///
/// Bot in Telegram.
///
public static string Bot => $"https://t.me/stocksharpbot";
///
///
public static class Pages
{
///
///
public const long Eula = 274;
///
///
public const long Pricing = 157;
///
///
public const long NugetManual = 241;
///
///
public const long Message = 278;
///
///
public const long Topic = 275;
///
///
public const long File = 276;
///
///
public const long Users = 246;
///
///
public const long Register = 252;
///
///
public const long Forgot = 253;
///
///
public const long Faq = 239;
///
///
public const long Store = 164;
///
///
public const long Login = 251;
///
///
public const long Profile = 243;
}
///
/// Get page url.
///
/// Page id.
/// Url part (topic id, file name etc.).
/// Localized url.
public static string GetPageUrl(long id, object urlPart = default)
{
var url = GetWebSiteUrl() + "/";
url += id switch
{
Pages.Eula => "products/eula",
Pages.Pricing => "pricing",
Pages.NugetManual => "products/nuget_manual",
Pages.Message => "posts/m",
Pages.Topic => "topic",
Pages.File => "file",
Pages.Users => "users",
Pages.Register => "register",
Pages.Forgot => "forgot",
Pages.Faq => "store/faq",
Pages.Store => "store",
Pages.Login => "login",
Pages.Profile => "profile",
_ => throw new ArgumentOutOfRangeException(nameof(id), id, LocalizedStrings.InvalidValue),
};
url += "/";
if (urlPart is not null)
url += $"{urlPart}/";
return url;
}
///
/// Try to build documentation URL.
///
/// URL part.
/// Absolute URL.
public static string TryBuildDocUrl(this string urlPart)
{
if (!urlPart.StartsWithIgnoreCase("http"))
urlPart = GetDocUrl(urlPart);
return urlPart;
}
///
/// To create localized url.
///
/// Help topic.
/// Localized url.
public static string GetDocUrl(string docUrl) => $"https://doc.stocksharp.{Domain}/{docUrl}";
///
/// Entry assembly.
///
public static Assembly EntryAssembly => Assembly.GetEntryAssembly();
private static string _installedVersion;
///
/// Installed version of the product.
///
public static string InstalledVersion
{
get
{
if (_installedVersion != null)
return _installedVersion;
static string GetAssemblyVersion() => EntryAssembly?.GetName().Version.To();
try
{
_installedVersion = TryGetInstalledVersion(Directory.GetCurrentDirectory()).IsEmpty(ConfigManager.TryGet("actualVersion", "5.0.0").IsEmpty(GetAssemblyVersion()));
}
catch
{
_installedVersion = GetAssemblyVersion();
}
return _installedVersion;
}
}
///
/// Reset installed version cache so it would be re-generated next time when it's requested.
///
public static void ResetInstalledVersionCache() => _installedVersion = null;
///
/// Build info version.
///
public static string BuildVersion
=> EntryAssembly?.GetAttribute()?.InformationalVersion;
private static SettingsStorage[] GetInstallations()
{
if (!InstallerInstallationsConfigPath.IsConfigExists())
return null;
SettingsStorage storage;
lock (InstallerInstallationsConfigPath)
storage = InstallerInstallationsConfigPath.DeserializeInvariant();
if (storage is null)
return null;
var installations = storage?.GetValue("Installations");
if (!(installations?.Length > 0))
return null;
return installations;
}
///
/// Try get installed path by product id.
///
/// Identifier.
/// Installed path.
public static string TryGetInstalledPath(long productId)
{
var installations = GetInstallations();
if (installations == null)
return null;
var installation = installations.FirstOrDefault(ss => productId == ss.TryGet("ProductKey"));
if (installation == null)
return null;
return installation.TryGet("InstallDirectory");
}
///
/// Get currently installed version of the product.
///
/// File system path to product installation.
/// Installed version of the product.
public static string TryGetInstalledVersion(string productInstallPath)
{
if (productInstallPath.IsEmpty())
throw new ArgumentException(nameof(productInstallPath));
var installations = GetInstallations();
if (installations == null)
return null;
var installation = installations.FirstOrDefault(ss => productInstallPath.ComparePaths(ss.TryGet("InstallDirectory")));
if (installation == null)
return null;
var version = installation.GetValue