CreamInstaller/CreamInstaller/Utility/ProgramData.cs

167 lines
6.2 KiB
C#
Raw Normal View History

using CreamInstaller.Resources;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
2022-03-03 18:56:44 +05:00
using System.IO;
2022-08-26 07:04:45 +05:00
using System.Linq;
2022-03-03 18:56:44 +05:00
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CreamInstaller.Utility;
internal static class ProgramData
{
internal static readonly string DirectoryPathOld = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CreamInstaller";
internal static readonly string DirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\CreamInstaller";
internal static readonly string AppInfoPath = DirectoryPath + @"\appinfo";
internal static readonly string AppInfoVersionPath = AppInfoPath + @"\version.txt";
internal static readonly Version MinimumAppInfoVersion = Version.Parse("3.2.0.0");
2022-03-03 18:56:44 +05:00
internal static readonly string CooldownPath = DirectoryPath + @"\cooldown";
internal static readonly string OldChoicesPath = DirectoryPath + @"\choices.txt";
internal static readonly string ChoicesPath = DirectoryPath + @"\choices.json";
2022-08-26 07:04:45 +05:00
internal static readonly string KoaloaderProxyChoicesPath = DirectoryPath + @"\proxies.json";
2022-03-03 18:56:44 +05:00
internal static async Task Setup() => await Task.Run(() =>
{
if (Directory.Exists(DirectoryPathOld))
{
if (Directory.Exists(DirectoryPath)) Directory.Delete(DirectoryPath, true);
Directory.Move(DirectoryPathOld, DirectoryPath);
}
if (!Directory.Exists(DirectoryPath)) _ = Directory.CreateDirectory(DirectoryPath);
2022-03-03 18:56:44 +05:00
if (!File.Exists(AppInfoVersionPath) || !Version.TryParse(File.ReadAllText(AppInfoVersionPath, Encoding.UTF8), out Version version) || version < MinimumAppInfoVersion)
{
if (Directory.Exists(AppInfoPath)) Directory.Delete(AppInfoPath, true);
_ = Directory.CreateDirectory(AppInfoPath);
2022-03-03 18:56:44 +05:00
File.WriteAllText(AppInfoVersionPath, Application.ProductVersion, Encoding.UTF8);
}
if (!Directory.Exists(CooldownPath))
_ = Directory.CreateDirectory(CooldownPath);
if (File.Exists(OldChoicesPath))
File.Delete(OldChoicesPath);
2022-03-03 18:56:44 +05:00
});
internal static bool CheckCooldown(string identifier, int cooldown)
{
DateTime now = DateTime.UtcNow;
DateTime lastCheck = GetCooldown(identifier) ?? now;
bool cooldownOver = (now - lastCheck).TotalSeconds > cooldown;
if (cooldownOver || now == lastCheck)
SetCooldown(identifier, now);
return cooldownOver;
}
private static DateTime? GetCooldown(string identifier)
{
if (Directory.Exists(CooldownPath))
{
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
if (File.Exists(cooldownFile))
{
try
{
if (DateTime.TryParse(File.ReadAllText(cooldownFile), out DateTime cooldown))
return cooldown;
}
catch { }
}
}
return null;
}
private static void SetCooldown(string identifier, DateTime time)
{
if (!Directory.Exists(CooldownPath))
_ = Directory.CreateDirectory(CooldownPath);
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
try
{
File.WriteAllText(cooldownFile, time.ToString());
}
catch { }
}
internal static List<(Platform platform, string id)> ReadChoices()
{
if (!File.Exists(ChoicesPath)) return null;
try
{
return JsonConvert.DeserializeObject(File.ReadAllText(ChoicesPath),
typeof(List<(Platform platform, string id)>)) as List<(Platform platform, string id)>;
}
catch
{
return new();
}
}
internal static void WriteChoices(List<(Platform platform, string id)> choices)
{
try
{
File.WriteAllText(ChoicesPath, JsonConvert.SerializeObject(choices));
}
catch { }
}
2022-08-26 07:04:45 +05:00
internal static List<(Platform platform, string id, string proxy)> ReadKoaloaderProxyChoices()
{
if (!File.Exists(KoaloaderProxyChoicesPath)) return null;
try
{
return JsonConvert.DeserializeObject(File.ReadAllText(KoaloaderProxyChoicesPath),
typeof(List<(Platform platform, string id, string proxy)>)) as List<(Platform platform, string id, string proxy)>;
}
catch
{
return new();
}
}
internal static void WriteKoaloaderProxyChoices(List<(Platform platform, string id, string proxy)> choices)
{
try
{
File.WriteAllText(KoaloaderProxyChoicesPath, JsonConvert.SerializeObject(choices));
}
catch { }
}
internal static void UpdateKoaloaderProxyChoices()
{
string defaultProxy = "version";
2022-08-26 07:04:45 +05:00
List<(Platform platform, string id, string proxy)> choices = ReadKoaloaderProxyChoices() ?? new();
foreach ((Platform platform, string id, string proxy) choice in choices.ToList())
if (ProgramSelection.FromPlatformId(choice.platform, choice.id) is ProgramSelection selection)
{
string proxy = choice.proxy;
if (proxy.Contains('.')) // convert pre-v4.1.0.0 choices
proxy.GetProxyInfoFromIdentifier(out proxy, out _);
2022-08-26 07:04:45 +05:00
if (selection.KoaloaderProxy is null)
selection.KoaloaderProxy = proxy;
else if (selection.KoaloaderProxy != proxy && choices.Remove(choice))
2022-08-26 07:04:45 +05:00
choices.Add((selection.Platform, selection.Id, selection.KoaloaderProxy));
}
foreach (ProgramSelection selection in ProgramSelection.AllSafe)
if (selection.KoaloaderProxy is null)
{
selection.KoaloaderProxy = defaultProxy;
choices.Add((selection.Platform, selection.Id, selection.KoaloaderProxy));
}
if (choices.Any())
WriteKoaloaderProxyChoices(choices);
}
2022-08-26 08:18:26 +05:00
internal static void ResetKoaloaderProxyChoices()
{
if (File.Exists(KoaloaderProxyChoicesPath))
File.Delete(KoaloaderProxyChoicesPath);
UpdateKoaloaderProxyChoices();
}
2022-03-03 18:56:44 +05:00
}