2022-08-28 07:04:42 +05:00
|
|
|
|
using CreamInstaller.Resources;
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2022-08-18 03:48:05 +05:00
|
|
|
|
|
|
|
|
|
using System;
|
2022-03-19 04:04:49 +05:00
|
|
|
|
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";
|
|
|
|
|
|
2022-03-07 06:00:45 +05:00
|
|
|
|
internal static readonly Version MinimumAppInfoVersion = Version.Parse("3.2.0.0");
|
2022-03-03 18:56:44 +05:00
|
|
|
|
|
2022-03-12 05:46:18 +05:00
|
|
|
|
internal static readonly string CooldownPath = DirectoryPath + @"\cooldown";
|
|
|
|
|
|
2022-08-18 03:48:05 +05:00
|
|
|
|
internal static readonly string OldChoicesPath = DirectoryPath + @"\choices.txt";
|
|
|
|
|
internal static readonly string ChoicesPath = DirectoryPath + @"\choices.json";
|
2022-03-19 04:04:49 +05:00
|
|
|
|
|
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);
|
|
|
|
|
}
|
2022-08-17 02:15:29 +05:00
|
|
|
|
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);
|
2022-08-17 02:15:29 +05:00
|
|
|
|
_ = Directory.CreateDirectory(AppInfoPath);
|
2022-03-03 18:56:44 +05:00
|
|
|
|
File.WriteAllText(AppInfoVersionPath, Application.ProductVersion, Encoding.UTF8);
|
|
|
|
|
}
|
2022-03-12 05:46:18 +05:00
|
|
|
|
if (!Directory.Exists(CooldownPath))
|
2022-08-17 02:15:29 +05:00
|
|
|
|
_ = Directory.CreateDirectory(CooldownPath);
|
2022-08-18 03:48:05 +05:00
|
|
|
|
if (File.Exists(OldChoicesPath))
|
|
|
|
|
File.Delete(OldChoicesPath);
|
2022-03-03 18:56:44 +05:00
|
|
|
|
});
|
2022-03-12 05:46:18 +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))
|
2022-08-17 02:15:29 +05:00
|
|
|
|
_ = Directory.CreateDirectory(CooldownPath);
|
2022-03-12 05:46:18 +05:00
|
|
|
|
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText(cooldownFile, time.ToString());
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
2022-03-19 04:04:49 +05:00
|
|
|
|
|
2022-08-18 03:48:05 +05:00
|
|
|
|
internal static List<(Platform platform, string id)> ReadChoices()
|
2022-03-19 04:04:49 +05:00
|
|
|
|
{
|
2022-08-18 03:48:05 +05:00
|
|
|
|
if (!File.Exists(ChoicesPath)) return null;
|
2022-03-19 04:04:49 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-08-18 03:48:05 +05:00
|
|
|
|
return JsonConvert.DeserializeObject(File.ReadAllText(ChoicesPath),
|
|
|
|
|
typeof(List<(Platform platform, string id)>)) as List<(Platform platform, string id)>;
|
2022-03-19 04:04:49 +05:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-18 03:48:05 +05:00
|
|
|
|
internal static void WriteChoices(List<(Platform platform, string id)> choices)
|
2022-03-19 04:04:49 +05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-08-18 03:48:05 +05:00
|
|
|
|
File.WriteAllText(ChoicesPath, JsonConvert.SerializeObject(choices));
|
2022-03-19 04:04:49 +05:00
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
{
|
2022-08-28 07:04:42 +05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-08-28 07:04:42 +05:00
|
|
|
|
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)
|
2022-08-28 07:04:42 +05:00
|
|
|
|
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
|
|
|
|
}
|