Skip to content

Commit

Permalink
GameNPC.LoadFromDatabase: Clear cached values
Browse files Browse the repository at this point in the history
* Fixes `/mob reload` not reloading properly.
  • Loading branch information
bm01 committed Jul 11, 2024
1 parent b63492b commit 12d695b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
4 changes: 2 additions & 2 deletions GameServer/gameobjects/GameLiving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3739,9 +3739,9 @@ protected virtual GameEffectList CreateEffectsList()
/// <summary>
/// Holds all abilities of the living (KeyName -> Ability)
/// </summary>
protected readonly Dictionary<string, Ability> m_abilities = new Dictionary<string, Ability>();
protected Dictionary<string, Ability> m_abilities = [];

protected readonly Object m_lockAbilities = new Object();
protected readonly object m_lockAbilities = new();

/// <summary>
/// Asks for existence of specific ability
Expand Down
39 changes: 26 additions & 13 deletions GameServer/gameobjects/GameNPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,18 @@ public override void LoadFromDatabase(DataObject obj)
if (obj is not DbMob)
return;

// Clear cached values in case this is a reload.
NPCTemplate = null;
EquipmentTemplateID = null;
Inventory = null;
_templateEquipmentIds = null;
_templateLevels = null;
_templateModels = null;
_templateSizes = null;
Spells = [];
Styles = [];
Abilities = [];

base.LoadFromDatabase(obj);

m_loadedFromScript = false;
Expand Down Expand Up @@ -1246,7 +1258,7 @@ public virtual void LoadTemplate(INpcTemplate template)

// Some properties don't have to be reloaded if we're reusing the same template.
// Some properties are also randomized and will be changed even if the template doesn't change.
bool isNewTemplate = NPCTemplate != template;
bool isNewTemplate = NPCTemplate != template;

// We need the level to be set before assigning spells to scale pet spells.
if (isNewTemplate)
Expand Down Expand Up @@ -3522,7 +3534,7 @@ public override long LastAttackedByEnemyTickPvP

#region Spell

private List<Spell> m_spells = new(0);
private List<Spell> m_spells = [];
private ConcurrentDictionary<GameObject, List<SpellWaitingForLosCheck>> _spellsWaitingForLosCheck = new();

public class SpellWaitingForLosCheck
Expand All @@ -3542,9 +3554,9 @@ public SpellWaitingForLosCheck(Spell spell, SpellLine spellLine, long requestTim
/// <summary>
/// property of spell array of NPC
/// </summary>
public virtual IList Spells
public virtual List<Spell> Spells
{
get { return m_spells; }
get => m_spells;
set
{
if (value == null || value.Count < 1)
Expand All @@ -3559,9 +3571,9 @@ public virtual IList Spells
}
else
{
m_spells = value.Cast<Spell>().ToList();
//if(!SortedSpells)
SortSpells();
// Voluntary copy. This isn't ideal and needs to be changed eventually.
m_spells = value.ToList();
SortSpells();
}
}
}
Expand Down Expand Up @@ -3882,14 +3894,14 @@ public virtual void OnCastSpellLosCheckFail(GameObject target)
/// <summary>
/// Styles for this NPC
/// </summary>
private IList m_styles = new List<Style>(0);
public IList Styles
private List<Style> m_styles = [];
public List<Style> Styles
{
get { return m_styles; }
get => m_styles;
set
{
m_styles = value;
this.SortStyles();
SortStyles();
}
}

Expand Down Expand Up @@ -4106,6 +4118,7 @@ public Dictionary<string, Ability> Abilities

return tmp;
}
protected set => m_abilities = value;
}

#endregion
Expand Down Expand Up @@ -4453,10 +4466,10 @@ public GameNPC Copy(GameNPC copyTarget)
}

if (Spells != null && Spells.Count > 0)
copyTarget.Spells = new List<Spell>(Spells.Cast<Spell>());
copyTarget.Spells = new List<Spell>(Spells);

if (Styles != null && Styles.Count > 0)
copyTarget.Styles = new ArrayList(Styles);
copyTarget.Styles = new List<Style>(Styles);

if (copyTarget.Inventory != null)
copyTarget.SwitchWeapon(ActiveWeaponSlot);
Expand Down
19 changes: 10 additions & 9 deletions GameServer/gameutils/INpcTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using DOL.GS.Styles;

namespace DOL.GS
{
/// <summary>
/// Interface for all NPC templates
/// </summary>
public interface INpcTemplate
/// <summary>
/// Interface for all NPC templates
/// </summary>
public interface INpcTemplate
{
/// <summary>
/// Gets the npc template ID
Expand Down Expand Up @@ -63,10 +64,10 @@ public interface INpcTemplate
/// <summary>
/// Gets the template npc abilities
/// </summary>
IList Spells { get; }
IList Styles { get; }
IList SpellLines { get; }
IList Abilities { get; }
List<Spell> Spells { get; }
List<Style> Styles { get; }
List<SpellLine> SpellLines { get; }
List<Ability> Abilities { get; }

/// <summary>
/// Gets the template npc stats
Expand Down
40 changes: 20 additions & 20 deletions GameServer/gameutils/NpcTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DOL.Database;
Expand All @@ -8,10 +8,10 @@

namespace DOL.GS
{
/// <summary>
/// A npc template
/// </summary>
public class NpcTemplate : INpcTemplate
/// <summary>
/// A npc template
/// </summary>
public class NpcTemplate : INpcTemplate
{
/// <summary>
/// Defines a logger for this class.
Expand Down Expand Up @@ -48,10 +48,10 @@ public class NpcTemplate : INpcTemplate
protected short m_intelligence;
protected short m_empathy;
protected short m_charisma;
protected IList m_styles;
protected IList m_spells;
protected IList m_spelllines;
protected IList m_abilities;
protected List<Style> m_styles;
protected List<Spell> m_spells;
protected List<SpellLine> m_spelllines;
protected List<Ability> m_abilities;
protected byte m_aggroLevel;
protected int m_aggroRange;
protected ushort m_race;
Expand Down Expand Up @@ -105,9 +105,9 @@ public NpcTemplate(DbNpcTemplate data)
m_empathy = data.Empathy;

//Time to add Spells/Styles and Abilties to the templates
m_abilities = new ArrayList();
m_spelllines = new ArrayList();
m_spells = new ArrayList();
m_abilities = [];
m_spelllines = [];
m_spells = [];
//Adding the spells to an Arraylist here
if (data.Spells != null && data.Spells.Length > 0)
{
Expand All @@ -123,7 +123,7 @@ public NpcTemplate(DbNpcTemplate data)
}

// Adding Style list to Template NPC
m_styles = new ArrayList();
m_styles = [];
if (data.Styles != null && data.Styles.Length > 0)
{
string[] styles = data.Styles.Split(';');
Expand Down Expand Up @@ -220,7 +220,7 @@ public NpcTemplate( GameNPC mob )
try
{
if (m_abilities == null)
m_abilities = new ArrayList(mob.Abilities.Count);
m_abilities = new(mob.Abilities.Count);

foreach (Ability mobAbility in mob.Abilities.Values)
{
Expand All @@ -238,7 +238,7 @@ public NpcTemplate( GameNPC mob )
try
{
if (m_spells == null)
m_spells = new ArrayList(mob.Spells.Count);
m_spells = new(mob.Spells.Count);

foreach (Spell mobSpell in mob.Spells)
{
Expand All @@ -256,7 +256,7 @@ public NpcTemplate( GameNPC mob )
try
{
if (m_styles == null)
m_styles = new ArrayList(mob.Styles.Count);
m_styles = new(mob.Styles.Count);

foreach (Style mobStyle in mob.Styles)
{
Expand Down Expand Up @@ -506,31 +506,31 @@ public byte LeftHandSwingChance
/// <summary>
/// Gets the template npc spells name array
/// </summary>
public IList Spells
public List<Spell> Spells
{
get { return m_spells; }
set { m_spells = value; }
}
/// <summary>
/// Gets the template npc styles name array
/// </summary>
public IList Styles
public List<Style> Styles
{
get { return m_styles; }
set { m_styles = value; }
}
/// <summary>
/// Gets the template npc spellLines
/// </summary>
public IList SpellLines
public List<SpellLine> SpellLines
{
get { return m_spelllines; }
set { m_spelllines = value; }
}
///<summary>
///Gets the template npc Abilities
///</summary>
public IList Abilities
public List<Ability> Abilities
{
get { return m_abilities; }
set { m_abilities = value; }
Expand Down

0 comments on commit 12d695b

Please sign in to comment.