Fix settings file not being able to be found when launching BetterJoyForCemu from Search/PowerToys Run (#673)

* Explicitly assign settings file path

* Set settings path in Config constructor

* Make settings path readonly
This commit is contained in:
ASleepyCat 2021-01-22 16:35:44 +10:00 committed by GitHub
parent 53a0a272b8
commit 8c591fd2f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,11 +4,15 @@ using System.IO;
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
public static class Config { // stores dynamic configuration, including public static class Config { // stores dynamic configuration, including
const string PATH = "settings"; static readonly string path;
static Dictionary<string, string> variables = new Dictionary<string, string>(); static Dictionary<string, string> variables = new Dictionary<string, string>();
const int settingsNum = 11; // currently - ProgressiveScan, StartInTray + special buttons const int settingsNum = 11; // currently - ProgressiveScan, StartInTray + special buttons
static Config() {
path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\settings";
}
public static string GetDefaultValue(string s) { public static string GetDefaultValue(string s) {
switch (s) { switch (s) {
case "ProgressiveScan": case "ProgressiveScan":
@ -39,16 +43,16 @@ namespace BetterJoyForCemu {
foreach (string s in new string[] { "ProgressiveScan", "StartInTray", "capture", "home", "sl_l", "sl_r", "sr_l", "sr_r", "shake", "reset_mouse", "active_gyro" }) foreach (string s in new string[] { "ProgressiveScan", "StartInTray", "capture", "home", "sl_l", "sl_r", "sr_l", "sr_r", "shake", "reset_mouse", "active_gyro" })
variables[s] = GetDefaultValue(s); variables[s] = GetDefaultValue(s);
if (File.Exists(PATH)) { if (File.Exists(path)) {
// Reset settings file if old settings // Reset settings file if old settings
if (CountLinesInFile(PATH) < settingsNum) { if (CountLinesInFile(path) < settingsNum) {
File.Delete(PATH); File.Delete(path);
Init(caliData); Init(caliData);
return; return;
} }
using (StreamReader file = new StreamReader(PATH)) { using (StreamReader file = new StreamReader(path)) {
string line = String.Empty; string line = String.Empty;
int lineNO = 0; int lineNO = 0;
while ((line = file.ReadLine()) != null) { while ((line = file.ReadLine()) != null) {
@ -75,7 +79,7 @@ namespace BetterJoyForCemu {
} }
} }
} else { } else {
using (StreamWriter file = new StreamWriter(PATH)) { using (StreamWriter file = new StreamWriter(path)) {
foreach (string k in variables.Keys) foreach (string k in variables.Keys)
file.WriteLine(String.Format("{0} {1}", k, variables[k])); file.WriteLine(String.Format("{0} {1}", k, variables[k]));
string caliStr = ""; string caliStr = "";
@ -111,7 +115,7 @@ namespace BetterJoyForCemu {
} }
public static void SaveCaliData(List<KeyValuePair<string, float[]>> caliData) { public static void SaveCaliData(List<KeyValuePair<string, float[]>> caliData) {
string[] txt = File.ReadAllLines(PATH); string[] txt = File.ReadAllLines(path);
if (txt.Length < settingsNum + 1) // no custom calibrations yet if (txt.Length < settingsNum + 1) // no custom calibrations yet
Array.Resize(ref txt, txt.Length + 1); Array.Resize(ref txt, txt.Length + 1);
@ -122,17 +126,17 @@ namespace BetterJoyForCemu {
caliStr += space + caliData[i].Key + "," + String.Join(",", caliData[i].Value); caliStr += space + caliData[i].Key + "," + String.Join(",", caliData[i].Value);
} }
txt[settingsNum] = caliStr; txt[settingsNum] = caliStr;
File.WriteAllLines(PATH, txt); File.WriteAllLines(path, txt);
} }
public static void Save() { public static void Save() {
string[] txt = File.ReadAllLines(PATH); string[] txt = File.ReadAllLines(path);
int NO = 0; int NO = 0;
foreach (string k in variables.Keys) { foreach (string k in variables.Keys) {
txt[NO] = String.Format("{0} {1}", k, variables[k]); txt[NO] = String.Format("{0} {1}", k, variables[k]);
NO++; NO++;
} }
File.WriteAllLines(PATH, txt); File.WriteAllLines(path, txt);
} }
} }
} }