diff --git a/ExBuddy/Data/DataLocation.cs b/ExBuddy/Data/DataLocation.cs new file mode 100644 index 00000000..c2e3976d --- /dev/null +++ b/ExBuddy/Data/DataLocation.cs @@ -0,0 +1,23 @@ +namespace ExBuddy.Data +{ + using System.Diagnostics; + using System.IO; + using System.Runtime.CompilerServices; + + public static class DataLocation + { + [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.NoInlining)] + public static DirectoryInfo SourceDirectory() + { + var frame = new StackFrame(0, true); + var file = frame.GetFileName(); + + if (!string.IsNullOrEmpty(file) && File.Exists(file)) + { + return new DirectoryInfo(Path.GetDirectoryName(file)); + } + + return null; + } + } +} \ No newline at end of file diff --git a/ExBuddy/Data/SqlData.cs b/ExBuddy/Data/SqlData.cs index 85b809ff..7c431e0f 100644 --- a/ExBuddy/Data/SqlData.cs +++ b/ExBuddy/Data/SqlData.cs @@ -25,7 +25,7 @@ public class SqlData : SQLiteConnection static SqlData() { - var path = Path.Combine(Environment.CurrentDirectory, "Plugins\\ExBuddy\\Data\\" + DbFileName); + var path = Path.Combine(DataLocation.SourceDirectory().FullName, DbFileName); if (File.Exists(path)) { diff --git a/ExBuddy/ExBuddy.csproj b/ExBuddy/ExBuddy.csproj index 196611b2..adf28aad 100644 --- a/ExBuddy/ExBuddy.csproj +++ b/ExBuddy/ExBuddy.csproj @@ -63,6 +63,7 @@ + diff --git a/ExBuddy/Plugins/Skywatcher/Providers/LocationProvider.cs b/ExBuddy/Plugins/Skywatcher/Providers/LocationProvider.cs index 93b5ecf5..2f08c322 100644 --- a/ExBuddy/Plugins/Skywatcher/Providers/LocationProvider.cs +++ b/ExBuddy/Plugins/Skywatcher/Providers/LocationProvider.cs @@ -6,6 +6,7 @@ using System; using System.IO; using System.Linq; + using Data; public class LocationProvider { @@ -19,7 +20,7 @@ public class LocationProvider static LocationProvider() { - var path = Path.Combine(Environment.CurrentDirectory, "Plugins\\ExBuddy\\Data\\" + LocationIndexFileName); + var path = Path.Combine(DataLocation.SourceDirectory().FullName, LocationIndexFileName); if (File.Exists(path)) { diff --git a/ExBuddy/Plugins/Skywatcher/Providers/WeatherRateProvider.cs b/ExBuddy/Plugins/Skywatcher/Providers/WeatherRateProvider.cs index afabcf00..a64eb300 100644 --- a/ExBuddy/Plugins/Skywatcher/Providers/WeatherRateProvider.cs +++ b/ExBuddy/Plugins/Skywatcher/Providers/WeatherRateProvider.cs @@ -6,6 +6,7 @@ using System; using System.IO; using System.Linq; + using Data; internal class WeatherRateProvider { @@ -19,7 +20,7 @@ internal class WeatherRateProvider static WeatherRateProvider() { - var path = Path.Combine(Environment.CurrentDirectory, "Plugins\\ExBuddy\\Data\\" + WeatherRateIndexFileName); + var path = Path.Combine(DataLocation.SourceDirectory().FullName, WeatherRateIndexFileName); if (File.Exists(path)) { diff --git a/ExBuddy/Providers/MasterPieceSupplyDataProvider.cs b/ExBuddy/Providers/MasterPieceSupplyDataProvider.cs index 211b4e9e..6093868b 100644 --- a/ExBuddy/Providers/MasterPieceSupplyDataProvider.cs +++ b/ExBuddy/Providers/MasterPieceSupplyDataProvider.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Xml.Linq; + using Data; public class MasterPieceSupplyDataProvider { @@ -22,7 +23,7 @@ public class MasterPieceSupplyDataProvider static MasterPieceSupplyDataProvider() { - var path = Path.Combine(Environment.CurrentDirectory, "Plugins\\ExBuddy\\Data\\" + MsdFileName); + var path = Path.Combine(DataLocation.SourceDirectory().FullName, MsdFileName); if (File.Exists(path)) { @@ -31,7 +32,8 @@ static MasterPieceSupplyDataProvider() else { DataFilePath = - Directory.GetFiles(PluginManager.PluginDirectory, "*" + MsdFileName, SearchOption.AllDirectories).FirstOrDefault(); + Directory.GetFiles(PluginManager.PluginDirectory, "*" + MsdFileName, SearchOption.AllDirectories) + .FirstOrDefault(); } Instance = new MasterPieceSupplyDataProvider(DataFilePath); @@ -62,7 +64,8 @@ public bool IsValid var result = data.Root.Descendants("MS") .FirstOrDefault( - e => e.Elements().Any(c => string.Equals(c.Value, itemName, StringComparison.InvariantCultureIgnoreCase))); + e => e.Elements().Any(c => + string.Equals(c.Value, itemName, StringComparison.InvariantCultureIgnoreCase))); if (result == null) { diff --git a/ExBuddy/SecondaryOffsetManager.cs b/ExBuddy/SecondaryOffsetManager.cs index 1dc49582..eb557b9d 100644 --- a/ExBuddy/SecondaryOffsetManager.cs +++ b/ExBuddy/SecondaryOffsetManager.cs @@ -32,88 +32,90 @@ public static void IntalizeOffsets() .OrderBy(t => t.Name) .ToArray(); - Parallel.ForEach( - types, - type => - { - var pf = new PatternFinder(Core.Memory); - - foreach (var info in type.GetFields()) + using (var pf = new PatternFinder(Core.Memory)) + { + Parallel.ForEach( + types, + type => { - var offset = (Offset64)Attribute.GetCustomAttributes(info, typeof(Offset64)).FirstOrDefault(); - - if (offset == null) + foreach (var info in type.GetFields()) { - continue; - } - - try - { - var markedDontRebase = false; - - var pattern = offset.Pattern; - if (!pattern.Trim().EndsWith("DontRebase")) - { - pattern = pattern + " DontRebase"; - } + var offset = (Offset64) Attribute.GetCustomAttributes(info, typeof(Offset64)) + .FirstOrDefault(); - var results = pf.FindMany(pattern, ref markedDontRebase); - if (results == null) + if (offset == null) { - //Failed to find a pattern match. - logr.Write("No match for {0} some functionality may not work correctly", info.Name); continue; } - if (results.Length > 1) + try { - lock (Core.Memory) + var markedDontRebase = false; + + var pattern = offset.Pattern; + if (!pattern.Trim().EndsWith("DontRebase")) + { + pattern = pattern + " DontRebase"; + } + + var results = pf.FindMany(pattern, ref markedDontRebase); + if (results == null) { - if (offset.MultipleResults) + //Failed to find a pattern match. + logr.Write("No match for {0} some functionality may not work correctly", info.Name); + continue; + } + + if (results.Length > 1) + { + lock (Core.Memory) { - if (results.Distinct().Count() == 1) + if (offset.MultipleResults) { - //Multiple matches were expected but there was only one result, double check that our pattern is still finding what we wanted + if (results.Distinct().Count() == 1) + { + //Multiple matches were expected but there was only one result, double check that our pattern is still finding what we wanted + logr.Write( + "Multiple matches for {0} were expected, but only one result was found, some functionality may not work correctly", + info.Name); + } + } + else + { + //Multiple matches to the provided pattern were found and we were not expecting this logr.Write( - "Multiple matches for {0} were expected, but only one result was found, some functionality may not work correctly", + "Multiple matches for {0} which was not expected, some functionality may not work correctly", info.Name); } } - else - { - //Multiple matches to the provided pattern were found and we were not expecting this - logr.Write( - "Multiple matches for {0} which was not expected, some functionality may not work correctly", - info.Name); - } } - } - var addrz = (long)results[0]; + var addrz = (long) results[0]; - if (offset.Modifier != 0) - { - addrz = (long)(addrz + offset.Modifier); - } + if (offset.Modifier != 0) + { + addrz = (long) (addrz + offset.Modifier); + } - logr.Write("[SecondaryOffsetManager] Found 0x{0:X} for {1}", addrz, info.Name); + logr.Write("[SecondaryOffsetManager] Found 0x{0:X} for {1}", addrz, info.Name); - if (info.FieldType == typeof(IntPtr)) - { - info.SetValue(null, (IntPtr)addrz); + if (info.FieldType == typeof(IntPtr)) + { + info.SetValue(null, (IntPtr) addrz); + } + else + { + info.SetValue(null, (int) addrz); + } } - else + catch (Exception e) { - info.SetValue(null, (int)addrz); + //Something went wrong + logr.WriteException(e); } } - catch (Exception e) - { - //Something went wrong - logr.WriteException(e); - } - } - }); + }); + } SecondaryOffsetManager.Initalized = true; }