Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OffsetManager memory leak fix and use current source file to find dat… #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ExBuddy/Data/DataLocation.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion ExBuddy/Data/SqlData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
1 change: 1 addition & 0 deletions ExBuddy/ExBuddy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\OffsetAttribute.cs" />
<Compile Include="Data\DataLocation.cs" />
<Compile Include="Data\MasterpieceSupplyDutyResult.cs" />
<Compile Include="Data\RequiredItemResult.cs" />
<Compile Include="Data\SqlData.cs" />
Expand Down
3 changes: 2 additions & 1 deletion ExBuddy/Plugins/Skywatcher/Providers/LocationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.IO;
using System.Linq;
using Data;

public class LocationProvider
{
Expand All @@ -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))
{
Expand Down
3 changes: 2 additions & 1 deletion ExBuddy/Plugins/Skywatcher/Providers/WeatherRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.IO;
using System.Linq;
using Data;

internal class WeatherRateProvider
{
Expand All @@ -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))
{
Expand Down
9 changes: 6 additions & 3 deletions ExBuddy/Providers/MasterPieceSupplyDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Data;

public class MasterPieceSupplyDataProvider
{
Expand All @@ -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))
{
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
118 changes: 60 additions & 58 deletions ExBuddy/SecondaryOffsetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down