From d2cc397c69c51ba8a16084e906e28489d1be3389 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 20 Sep 2018 11:23:47 +0300 Subject: [PATCH] refactor, saveManeger --- OWTrack/MainForm.cs | 73 +++++++++++++++--------------------------- OWTrack/Program.cs | 2 +- OWTrack/Tracker.cs | 2 +- OWTrack/saveManeger.cs | 62 ++++++++++++----------------------- 4 files changed, 49 insertions(+), 90 deletions(-) diff --git a/OWTrack/MainForm.cs b/OWTrack/MainForm.cs index ce47097..2da19ed 100644 --- a/OWTrack/MainForm.cs +++ b/OWTrack/MainForm.cs @@ -17,7 +17,7 @@ namespace OWTrack public MainForm() { - InitializeComponent(); + InitializeComponent(); loadSave(); checkStatus(); update(); @@ -47,80 +47,59 @@ namespace OWTrack private void loadSave() { Directory.CreateDirectory("saves"); - if (saveExist()) + if (saveManeger.saveExist()) { - tr.wins = savedTracker().wins; - tr.losses = savedTracker().losses; - tr.newSR = savedTracker().newSR; - tr.startSR = savedTracker().startSR; - tr.gamePath = savedTracker().gamePath; - update(); - } - } - - private bool saveExist() - { - try - { - if (File.Exists(savesPath)) - { + try + { using (StreamReader st = new StreamReader(savesPath)) { string line = st.ReadLine(); if (line.Contains("Overwatch.exe")) { - st.Close(); - return true; + tr = saveManeger.GetSavedTracker(); + st.Close(); } else { if (!tr.LoacteOW()) - { - st.Close(); + { tr.gamePath = getGamePath(); } - return true; + st.Close(); } - } - } - else - { - if (!tr.LoacteOW()) - { - tr.gamePath = getGamePath(); - } - return false; - } - } - catch (Exception e) - { - MessageBox.Show(e.Message); - return false; - } - } + } + } + catch (Exception e) + { + MessageBox.Show(e.Message); + } + update(); + } + else if (!tr.LoacteOW()) + { + tr.gamePath = getGamePath(); + } + } + private string getGamePath() { openFileDialog1.Title = "Select Overwatch.exe"; openFileDialog1.DefaultExt = "exe"; - openFileDialog1.Filter = "exe Files (*.exe)|*.exe|All files (*.*)|*.*"; - //openFileDialog1.CheckFileExists = true; - //openFileDialog1.CheckPathExists = true; - + openFileDialog1.Filter = "exe Files (*.exe)|*.exe|All files (*.*)|*.*"; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { //tr.gamePath = openFileDialog1.FileName; + MessageBox.Show("Saved Overwatch.exe location.\nPress Clear to rest\n" + openFileDialog1.FileName ); return openFileDialog1.FileName; } else { + update(); return null; - //Close(); - } - //Show(); - //update(); + } } private Tracker savedTracker() diff --git a/OWTrack/Program.cs b/OWTrack/Program.cs index f37ed81..963e7e6 100644 --- a/OWTrack/Program.cs +++ b/OWTrack/Program.cs @@ -18,7 +18,7 @@ namespace OWTrack Application.Run(new MainForm()); } - public static string Version { get; } = "1.4.0a1"; + public static string Version { get; } = "1.4.0a2"; //public static string Version = Application.ProductVersion; //public static Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; diff --git a/OWTrack/Tracker.cs b/OWTrack/Tracker.cs index ab55b4a..a278053 100644 --- a/OWTrack/Tracker.cs +++ b/OWTrack/Tracker.cs @@ -13,7 +13,7 @@ namespace OWTrack public string gamePath; public void Track() { } - public void reset() { wins = 0; losses = 0; startSR = 0; newSR = 0; } + public void reset() { wins = 0; losses = 0; startSR = 0; newSR = 0; gamePath = null; } public void addWin() { wins++; } public void addLoss() { losses++; } public void reduceWin() { wins--; } diff --git a/OWTrack/saveManeger.cs b/OWTrack/saveManeger.cs index 7349658..b903c23 100644 --- a/OWTrack/saveManeger.cs +++ b/OWTrack/saveManeger.cs @@ -10,11 +10,13 @@ namespace OWTrack { class saveManeger { + private static string savesPath = Directory.GetCurrentDirectory() + "/saves/data.json"; + public static Tracker GetSavedTracker() { try { - return JsonConvert.DeserializeObject(File.ReadAllText(Directory.GetCurrentDirectory() + "/data.json")); + return JsonConvert.DeserializeObject(File.ReadAllText(savesPath)); } catch (Exception e) { @@ -48,45 +50,23 @@ namespace OWTrack } } - //private bool saveExist() - //{ - // try - // { - // if (File.Exists(Directory.GetCurrentDirectory() + "/data.json")) - // { - // using (StreamReader st = new StreamReader(Directory.GetCurrentDirectory() + "/data.json")) - // { - // string line = st.ReadLine(); - // if (line.Contains("Overwatch.exe")) - // { - // st.Close(); - // return true; - // } - // else - // { - // if (!tr.LoacteOW()) - // { - // st.Close(); - // getGamePath(); - // } - // return true; - // } - // } - // } - // else - // { - // if (!tr.LoacteOW()) - // { - // getGamePath(); - // } - // return false; - // } - // } - // catch (Exception e) - // { - // MessageBox.Show(e.Message); - // return false; - // } - //} + public static bool saveExist() + { + try + { + if (File.Exists(savesPath)) + { + return true; + } + else + { + return false; + } + } + catch (Exception e) + { + throw e; + } + } } }