Merge branch 'Improve-search' of Hesham/OWtrack into release/1.4.3

This commit is contained in:
Hesham 2018-12-10 10:44:02 +03:00 committed by HeshamTB
commit 632325fcd0
3 changed files with 57 additions and 32 deletions

View File

@ -86,7 +86,7 @@ namespace OWTrack
{ {
try try
{ {
using (StreamReader st = new StreamReader(Paths.SAVES)) using (StreamReader st = new StreamReader(Paths.GetSaves()))
{ {
string line = st.ReadLine(); string line = st.ReadLine();
if (line.Contains("Overwatch.exe")) if (line.Contains("Overwatch.exe"))
@ -98,6 +98,7 @@ namespace OWTrack
{ {
if (!tr.LoacteOW()) if (!tr.LoacteOW())
{ {
MessageBox.Show(tr.gamePath);
tr.gamePath = getGamePath(); tr.gamePath = getGamePath();
} }
} }

View File

@ -25,6 +25,7 @@ using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using System.Collections.Generic; using System.Collections.Generic;
namespace OWTrack namespace OWTrack
{ {
class Tracker class Tracker
@ -46,6 +47,14 @@ namespace OWTrack
public bool TrackOW = true; public bool TrackOW = true;
public bool TrackSR = true; public bool TrackSR = true;
struct ProgramFiles
{
public static readonly string C = "C:\\Program Files";
public static readonly string D = "D:\\Program Files";
public static readonly string E = "E:\\Program Files";
public static readonly string F = "F:\\Program Files";
}
public bool owRunning() public bool owRunning()
{ {
if (TrackOW) if (TrackOW)
@ -69,34 +78,15 @@ namespace OWTrack
{ {
try try
{ {
DriveInfo[] driveInfo = DriveInfo.GetDrives();
List<string> paths = new List<string>(); List<string> paths = new List<string>();
string[] filesC = null; //Searches all drives (too long)
string[] filesD = null; foreach (var drive in driveInfo)
if (ProgramFilesExist('c')) { filesC = Directory.GetFiles("C:\\Program Files", "Overwatch.exe", SearchOption.AllDirectories); }
if (ProgramFilesExist('d')) { filesD = Directory.GetFiles("D:\\Program Files", "Overwatch.exe", SearchOption.AllDirectories); }
if (filesC != null)
{ {
for (int i = 0; i < filesC.Length; i++) //paths.AddRange(GetFiles(drive.ToString(),"Overwatch.exe"));
{
if (filesC[i].Contains("Overwatch.exe"))
{
paths.Add(filesC[i]);
}
}
}
if (filesD != null)
{
for (int i = 0; i < filesD.Length - 1; i++)
{
if (filesD[i].Contains("Overwatch.exe"))
{
paths.Add(filesD[i]);
}
}
} }
paths.AddRange(GetFiles(ProgramFiles.C, "Overwatch.exe"));
paths.AddRange(GetFiles(ProgramFiles.D, "Overwatch.exe"));
if (paths.Count > 1) if (paths.Count > 1)
{ {
@ -122,5 +112,36 @@ namespace OWTrack
{ {
return Directory.Exists(drive+":\\Program Files"); return Directory.Exists(drive+":\\Program Files");
} }
public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
Stack<string> pending = new Stack<string>();
pending.Push(root);
while (pending.Count != 0)
{
var path = pending.Pop();
string[] next = null;
try
{
next = Directory.GetFiles(path, searchPattern);
}
catch { }
if (next != null && next.Length != 0)
foreach (var file in next) yield return file;
try
{
next = Directory.GetDirectories(path);
foreach (var subdir in next) pending.Push(subdir);
}
catch { }
}
}
}
struct Settings
{
bool TrackSR, TrackOW;
string OWpath;
} }
} }

View File

@ -28,11 +28,14 @@ using System.Threading.Tasks;
namespace OWTrack namespace OWTrack
{ {
static class Paths public static class Paths
{ {
private static string curDir = Directory.GetCurrentDirectory(); private static string curDir = Directory.GetCurrentDirectory();
public static string SAVES = curDir + "/saves/data.json"; private static string SAVES = curDir + "/saves/data.json";
public static string JSON = curDir + "/data.json"; private static string JSON = curDir + "/data.json";
public static string GetJSON() { return JSON; }
public static string GetSaves() { return SAVES; }
public static string GetCurrentDir() { return curDir; }
} }
class saveManeger class saveManeger
@ -45,7 +48,7 @@ namespace OWTrack
{ {
try try
{ {
return JsonConvert.DeserializeObject<Tracker>(File.ReadAllText(Paths.SAVES)); return JsonConvert.DeserializeObject<Tracker>(File.ReadAllText(Paths.GetSaves()));
} }
catch (Exception e) catch (Exception e)
{ {
@ -74,7 +77,7 @@ namespace OWTrack
{ {
try try
{ {
File.WriteAllText(Paths.SAVES, JsonConvert.SerializeObject(tracker)); File.WriteAllText(Paths.GetSaves(), JsonConvert.SerializeObject(tracker));
return true; return true;
} }
catch (Exception) catch (Exception)
@ -91,7 +94,7 @@ namespace OWTrack
{ {
try try
{ {
if (File.Exists(Paths.SAVES)) if (File.Exists(Paths.GetSaves()))
{ {
return true; return true;
} }