2018-05-22 10:11:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Core.Helper;
|
|
|
|
|
using Core.Main.DataObjects.PTMagicData;
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
namespace Core.Main
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public class PTMagicConfiguration
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
private GeneralSettings _generalSettings = null;
|
|
|
|
|
private AnalyzerSettings _analyzerSettings = null;
|
|
|
|
|
private SecureSettings _secureSettings = null;
|
2020-08-19 20:10:49 +02:00
|
|
|
|
private string _basePath;
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public PTMagicConfiguration()
|
|
|
|
|
{
|
2020-08-19 20:10:49 +02:00
|
|
|
|
_basePath = Directory.GetCurrentDirectory();
|
|
|
|
|
LoadSettings(_basePath);
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public PTMagicConfiguration(string basePath)
|
|
|
|
|
{
|
2020-08-19 20:10:49 +02:00
|
|
|
|
_basePath = basePath;
|
|
|
|
|
LoadSettings(_basePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RefreshSettings()
|
|
|
|
|
{
|
|
|
|
|
LoadSettings(_basePath);
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
private void LoadSettings(string basePath)
|
|
|
|
|
{
|
|
|
|
|
if (!basePath.EndsWith(Path.DirectorySeparatorChar))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
basePath += Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneralSettingsWrapper gsw = JsonConvert.DeserializeObject<GeneralSettingsWrapper>(File.ReadAllText(basePath + "settings.general.json"));
|
|
|
|
|
_generalSettings = gsw.GeneralSettings;
|
|
|
|
|
|
|
|
|
|
AnalyzerSettingsWrapper asw = JsonConvert.DeserializeObject<AnalyzerSettingsWrapper>(File.ReadAllText(basePath + "settings.analyzer.json"));
|
|
|
|
|
_analyzerSettings = asw.AnalyzerSettings;
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (!_generalSettings.Application.ProfitTrailerPath.EndsWith(Path.DirectorySeparatorChar))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
_generalSettings.Application.ProfitTrailerPath += Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (!_generalSettings.Application.ProfitTrailerMonitorURL.EndsWith("/"))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
_generalSettings.Application.ProfitTrailerMonitorURL += "/";
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (File.Exists(basePath + "settings.secure.json"))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
SecureSettingsWrapper ssw = JsonConvert.DeserializeObject<SecureSettingsWrapper>(File.ReadAllText(basePath + "settings.secure.json"));
|
|
|
|
|
_secureSettings = ssw.SecureSettings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public string GetProfitTrailerLicenseKeyMasked()
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
string result = "";
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (!this.GeneralSettings.Application.ProfitTrailerLicense.Equals(""))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
result = this.GeneralSettings.Application.ProfitTrailerLicense.Substring(0, 4);
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
for (int i = 1; i < this.GeneralSettings.Application.ProfitTrailerLicense.Length - 8; i++)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
result += "*";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result += this.GeneralSettings.Application.ProfitTrailerLicense.Substring(this.GeneralSettings.Application.ProfitTrailerLicense.Length - 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 16:20:03 +01:00
|
|
|
|
public string GetProfitTrailerServerAPITokenMasked()
|
|
|
|
|
{
|
|
|
|
|
string result = "";
|
|
|
|
|
|
|
|
|
|
if (!this.GeneralSettings.Application.ProfitTrailerServerAPIToken.Equals(""))
|
|
|
|
|
{
|
|
|
|
|
result = this.GeneralSettings.Application.ProfitTrailerServerAPIToken.Substring(0, 4);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < this.GeneralSettings.Application.ProfitTrailerServerAPIToken.Length - 8; i++)
|
|
|
|
|
{
|
|
|
|
|
result += "*";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result += this.GeneralSettings.Application.ProfitTrailerServerAPIToken.Substring(this.GeneralSettings.Application.ProfitTrailerServerAPIToken.Length - 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public GeneralSettings GeneralSettings
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
return _generalSettings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public AnalyzerSettings AnalyzerSettings
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
return _analyzerSettings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public SecureSettings SecureSettings
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
if (_secureSettings == null) _secureSettings = new SecureSettings();
|
|
|
|
|
return _secureSettings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
public void WriteGeneralSettings()
|
2018-12-15 22:07:29 +01:00
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
GeneralSettingsWrapper gsWrapper = new GeneralSettingsWrapper();
|
|
|
|
|
gsWrapper.GeneralSettings = this.GeneralSettings;
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
FileHelper.CreateBackup(_basePath + "settings.general.json", _basePath, "settings.general.json.backup");
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
FileHelper.WriteTextToFile(_basePath, "settings.general.json", JsonConvert.SerializeObject(gsWrapper, Formatting.Indented));
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
public void WriteAnalyzerSettings()
|
2018-12-15 22:07:29 +01:00
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
AnalyzerSettingsWrapper asWrapper = new AnalyzerSettingsWrapper();
|
|
|
|
|
asWrapper.AnalyzerSettings = this.AnalyzerSettings;
|
|
|
|
|
|
|
|
|
|
JsonSerializerSettings settings = new JsonSerializerSettings();
|
|
|
|
|
settings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
|
settings.DefaultValueHandling = DefaultValueHandling.Ignore;
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
FileHelper.CreateBackup(_basePath + "settings.analyzer.json", _basePath, "settings.analyzer.json.backup");
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
FileHelper.WriteTextToFile(_basePath, "settings.analyzer.json", JsonConvert.SerializeObject(asWrapper, Formatting.Indented, settings));
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
public void WriteSecureSettings(string password)
|
2018-12-15 22:07:29 +01:00
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
string passwordEncrypted = EncryptionHelper.Encrypt(password);
|
|
|
|
|
|
|
|
|
|
this.SecureSettings.MonitorPassword = passwordEncrypted;
|
|
|
|
|
|
|
|
|
|
SecureSettingsWrapper ssWrapper = new SecureSettingsWrapper();
|
|
|
|
|
ssWrapper.SecureSettings = this.SecureSettings;
|
|
|
|
|
|
2020-08-19 20:10:49 +02:00
|
|
|
|
FileHelper.WriteTextToFile(_basePath, "settings.secure.json", JsonConvert.SerializeObject(ssWrapper, Formatting.Indented));
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|