FinalSolution/FinalSolution/Config.cs

112 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using FinalSolution.Properties;
using Newtonsoft.Json;
namespace FinalSolution
{
public static class Config
{
public static ConfigData Instance => _instance ?? ReadConfig();
private static ConfigData _instance;
private static ConfigData ReadConfig(string path = "config.json")
{
if (File.Exists(path))
{
var data = JsonConvert.DeserializeObject<ConfigData>(File.ReadAllText(path));
// check if the currently running game path is equal to the config-ed one.
// need manually enable in the FinalSolution.exe.config!
// require ForceCreateNewFilter to work as intended!!
VerifyGamePath(data);
return _instance = data;
}
MessageBox.Show(Resources.Config_ReadConfig_Awareness, Resources.Config_ReadConfig_Awareness_Title);
var appPath = FindGameRunning(); // get game path from currently running process
if (appPath == null) SelectGameDialog(out appPath); // if there is no running game, ask the player to select
if (appPath == "") // if both of these are failed, just fuck off!
{
MessageBox.Show(Resources.Config_ReadConfig_Go_To_Read_README);
Application.Exit();
throw new Exception();
}
_instance = new ConfigData
{
Destiny2Path = appPath,
Hotkeys = new Dictionary<string, string>
{
{ "tsf_3074", "Control+G" },
{ "tsf_3074_ul", "Control+H" },
{ "tsf_7500", "Control+T" },
{ "tsf_fg", "Control+J" },
{ "tsf_27k", "Alt+N" },
{ "tsf_30k", "Alt+B" },
}
};
SaveConfig();
return _instance;
}
private static void SaveConfig(string path = "config.json")
{
File.WriteAllText(path, JsonConvert.SerializeObject(_instance, Formatting.Indented));
}
private static string FindGameRunning()
{
var process = Process.GetProcessesByName("destiny2").FirstOrDefault();
var path = process?.GetMainModuleFileName();
if (path != null)
{
Console.WriteLine(Resources.Config_FindGameRunning_Found_Destiny_2_Process, path);
}
return path;
}
private static void SelectGameDialog(out string appPath)
{
var dialog = new OpenFileDialog
{
Multiselect = false,
Title = Resources.Config_FindDestiny2_Select_Destiny_2_Application,
Filter = @"Destiny 2|destiny2.exe"
};
dialog.ShowDialog();
Console.WriteLine(dialog.FileName);
appPath = dialog.FileName;
}
private static void VerifyGamePath(ConfigData data)
{
if (!Settings.Default.AutoUpdateGamePath) return;
// if the game path is not set; this must be something wrong!
if (data.Destiny2Path == null) return;
var curr = FindGameRunning();
if (curr == null || curr == data.Destiny2Path) return;
Console.WriteLine(Resources.Config_VerifyGamePath, data.Destiny2Path, curr);
data.Destiny2Path = curr;
}
}
public class ConfigData
{
public string Destiny2Path;
public Dictionary<string, string> Hotkeys;
}
}