Skip to content

Commit

Permalink
Merge pull request #28 from unv-annihilator/develop
Browse files Browse the repository at this point in the history
Prevent Argument Exception, Attempt to fix Access Violation
  • Loading branch information
H4vC committed Feb 7, 2015
2 parents 52d3231 + 615e5d7 commit 2af59b1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
9 changes: 6 additions & 3 deletions src/Hud/DPS/DpsMeterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ private double CalculateDps(TimeSpan elapsedTime)
if (hp > -1000000 && hp < 10000000)
{
int lastHP;
if (lastMonsters.TryGetValue(monster.Id, out lastHP) && lastHP > hp)
if (lastMonsters.TryGetValue(monster.Id, out lastHP))
{
totalDamage += lastHP - hp;
// make this a separte if statement to prevent dictionary already containing item
if (lastHP > hp)
{
totalDamage += lastHP - hp;
}
}
monsters.Add(monster.Id, hp);
}
}
lastMonsters = monsters;
Expand Down
58 changes: 20 additions & 38 deletions src/Hud/Loot/ItemAlertPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using PoeHUD.Controllers;
using PoeHUD.Framework.Helpers;
Expand All @@ -20,27 +18,28 @@

using SharpDX;
using SharpDX.Direct3D9;
using System.Collections.Concurrent;

namespace PoeHUD.Hud.Loot
{
public class ItemAlertPlugin : SizedPluginWithMapIcons<ItemAlertSettings>
{
private readonly HashSet<long> playedSoundsCache;

private readonly Dictionary<EntityWrapper, AlertDrawStyle> currentAlerts;
private readonly ConcurrentDictionary<EntityWrapper, AlertDrawStyle> currentAlerts;

private readonly Dictionary<string, CraftingBase> craftingBases;

private readonly HashSet<string> currencyNames;

private Dictionary<int, ItemsOnGroundLabelElement> currentLabels;
private ConcurrentDictionary<int, ItemsOnGroundLabelElement> currentLabels;

public ItemAlertPlugin(GameController gameController, Graphics graphics, ItemAlertSettings settings)
: base(gameController, graphics, settings)
{
playedSoundsCache = new HashSet<long>();
currentAlerts = new Dictionary<EntityWrapper, AlertDrawStyle>();
currentLabels = new Dictionary<int, ItemsOnGroundLabelElement>();
currentAlerts = new ConcurrentDictionary<EntityWrapper, AlertDrawStyle>();
currentLabels = new ConcurrentDictionary<int, ItemsOnGroundLabelElement>();
currencyNames = LoadCurrency();
craftingBases = LoadCraftingBases();

Expand All @@ -62,18 +61,6 @@ public override void Render()
const int BOTTOM_MARGIN = 2;
bool shouldUpdate = false;

if (Settings.BorderSettings.Enable)
{
Dictionary<EntityWrapper, AlertDrawStyle> tempCopy = new Dictionary<EntityWrapper, AlertDrawStyle>(currentAlerts);
Parallel.ForEach(tempCopy.Where(x => x.Key.IsValid), kv =>
{
if (DrawBorder(kv.Key.Address) && !shouldUpdate)
{
shouldUpdate = true;
}
});
}

foreach (KeyValuePair<EntityWrapper, AlertDrawStyle> kv in currentAlerts.Where(x => x.Key.IsValid))
{
string text = GetItemName(kv);
Expand All @@ -85,11 +72,15 @@ public override void Render()
ItemsOnGroundLabelElement entityLabel;
if (currentLabels.TryGetValue(kv.Key.Address, out entityLabel))
{
// Don't make labels on the right for items we can't pick up UNTIL we can pick it up or invisible
if (Settings.HideOthers && !entityLabel.CanPickUp)
if (Settings.BorderSettings.Enable && DrawBorder(kv.Key.Address) && !shouldUpdate)
{
return;
shouldUpdate = true;
}
// Don't make labels on the right for items we can't pick up UNTIL we can pick it up or invisible
//if (Settings.HideOthers && !entityLabel.CanPickUp)
//{
// return;
//}
}
else
{
Expand All @@ -105,7 +96,8 @@ public override void Render()

if (shouldUpdate)
{
currentLabels = GameController.Game.IngameState.IngameUi.ItemsOnGroundLabels.ToDictionary(y => y.ItemOnGround.Address, y => y);
// Change this in the future to prevent problems (create separate thread to do the updating, thread safe calls)
currentLabels = new ConcurrentDictionary<int, ItemsOnGroundLabelElement> (GameController.Game.IngameState.IngameUi.ItemsOnGroundLabels.ToDictionary(y => y.ItemOnGround.Address, y => y));
}
}
}
Expand All @@ -124,17 +116,6 @@ private Vector2 DrawText(Vector2 playerPos, Vector2 position, int BOTTOM_MARGIN,

protected override void OnEntityAdded(EntityWrapper entity)
{
// Added a check to see if it's null, will need to figure out why it can be null, if crashes still occur one of the atributes of an entity can be null but only in partyplay (have to investigate).
string[] Attributes = new string[] { "Address", "Id", "IsAlive", "IsHostile", "IsValid", "LongId", "Minions", "Path" };
for (int i = 0; i != 8; i++ )
{
string output = "";
if (entity.GetType().GetProperty(Attributes[1]).GetValue(entity, null) == null)
{
output = output + Attributes[i] + "is null \n";
File.AppendAllText("drawlog", output);
}
}
if (Settings.Enable && entity != null && !GameController.Area.CurrentArea.IsTown && !currentAlerts.ContainsKey(entity) && entity.HasComponent<WorldItem>())
{
IEntity item = entity.GetComponent<WorldItem>().ItemEntity;
Expand All @@ -143,7 +124,7 @@ protected override void OnEntityAdded(EntityWrapper entity)
if (props.ShouldAlert(currencyNames, Settings))
{
AlertDrawStyle drawStyle = props.GetDrawStyle();
currentAlerts.Add(entity, drawStyle);
currentAlerts.TryAdd(entity, drawStyle);
CurrentIcons[entity] = new MapIcon(entity, new HudTexture("minimap_default_icon.png", drawStyle.AlertColor), () => Settings.ShowItemOnMap, 8);

if (Settings.PlaySound && !playedSoundsCache.Contains(entity.LongId))
Expand All @@ -158,8 +139,10 @@ protected override void OnEntityAdded(EntityWrapper entity)
protected override void OnEntityRemoved(EntityWrapper entity)
{
base.OnEntityRemoved(entity);
currentAlerts.Remove(entity);
currentLabels.Remove(entity.Address);
AlertDrawStyle ignoredStyle;
currentAlerts.TryRemove(entity, out ignoredStyle);
ItemsOnGroundLabelElement ignoredLabel;
currentLabels.TryRemove(entity.Address, out ignoredLabel);
}

private static Dictionary<string, CraftingBase> LoadCraftingBases()
Expand Down Expand Up @@ -256,8 +239,7 @@ private bool DrawBorder(int entityAddress)
if (Settings.BorderSettings.ShowTimer && timeLeft.TotalMilliseconds > 0)
{
borderColor = Settings.BorderSettings.CantPickUpBorderColor;
Graphics.DrawText(timeLeft.ToString(@"mm\:ss"), Settings.BorderSettings.TimerTextSize,
rect.TopRight.Translate(4, 0));
Graphics.DrawText(timeLeft.ToString(@"mm\:ss"), Settings.BorderSettings.TimerTextSize, rect.TopRight.Translate(4, 0));
}
}
Graphics.DrawFrame(rect, Settings.BorderSettings.BorderWidth, borderColor);
Expand Down
4 changes: 2 additions & 2 deletions src/Hud/Loot/ItemAlertSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ItemAlertSettings()
ShowItemOnMap = true;
Crafting = true;
ShowText = true;
HideOthers = false;
//HideOthers = false;
PlaySound = true;
TextSize = new RangeNode<int>(25, 10, 50);
Rares = true;
Expand All @@ -32,7 +32,7 @@ public ItemAlertSettings()

public ToggleNode ShowText { get; set; }

public ToggleNode HideOthers { get; set; }
//public ToggleNode HideOthers { get; set; }

public ToggleNode PlaySound { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Hud/Menu/MenuPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void CreateMenu()
AddChild(qualitySkillGemMenu, "Min. quality", qualityItemsSettings.SkillGem.MinQuality);
AddChild(itemAlertMenu, "Play sound", settingsHub.ItemAlertSettings.PlaySound);
MenuItem alertTextMenu = AddChild(itemAlertMenu, "Show text", settingsHub.ItemAlertSettings.ShowText);
AddChild(itemAlertMenu, "Hide Others", settingsHub.ItemAlertSettings.HideOthers);
//AddChild(itemAlertMenu, "Hide Others", settingsHub.ItemAlertSettings.HideOthers);
AddChild(alertTextMenu, "Font size", settingsHub.ItemAlertSettings.TextSize);
BorderSettings borderSettings = settingsHub.ItemAlertSettings.BorderSettings;
MenuItem showBorderMenu = AddChild(itemAlertMenu, "Show border", borderSettings.Enable);
Expand Down
17 changes: 11 additions & 6 deletions src/Hud/UI/Renderers/FontRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ public void Begin()

public Size2 DrawText(string text, string fontName, int height, Vector2 position, Color color, FontDrawFlags align)
{
try {
Font font = GetFont(fontName, height);
Font font;
Rectangle fontDimension;
try
{
font = GetFont(fontName, height);
var rectangle = new Rectangle((int)position.X, (int)position.Y, 0, 0);
Rectangle fontDimension = font.MeasureText(null, text, rectangle, align);
font.DrawText(sprite, text, fontDimension, align, color);
fontDimension = font.MeasureText(null, text, rectangle, align);
if (!sprite.IsDisposed)
font.DrawText(sprite, text, fontDimension, align, color);
return new Size2(fontDimension.Width, fontDimension.Height);
}
catch (AccessViolationException exception)
catch (Exception exception)
{
// Console.WriteLine("Access Violation! " + position.X + ", " + position.Y);
//Console.WriteLine("Exception! X: " + position.X + ", Y: " + position.Y + ", Text: " + text);
//Console.WriteLine(exception.StackTrace);
}
return new Size2();
}
Expand Down

0 comments on commit 2af59b1

Please sign in to comment.