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

Fixes dictionary uses #7

Merged
merged 2 commits into from Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions Scripts/Accounting/AccountHandler.cs
Expand Up @@ -79,11 +79,7 @@ public class AccountHandler
if (a.LoginIPs.Length > 0)
{
IPAddress ip = a.LoginIPs[0];

if (m_IPTable.ContainsKey(ip))
m_IPTable[ip]++;
else
m_IPTable[ip] = 1;
m_IPTable[ip] = (m_IPTable.TryGetValue(ip, out int value) ? value : 0) + 1;
}
}

Expand Down Expand Up @@ -424,4 +420,4 @@ public static bool CheckAccount(Mobile mobCheck, Mobile accCheck)
return false;
}
}
}
}
5 changes: 1 addition & 4 deletions Scripts/Commands/Batch.cs
Expand Up @@ -91,12 +91,9 @@ public override void ExecuteList(CommandEventArgs e, List<object> list)
continue;

Type type = obj.GetType();

PropertyInfo[] chain = propertyChains[type];

string failReason = "";

if (chain == null)
if (!propertyChains.TryGetValue(type, out PropertyInfo[] chain))
propertyChains[type] = chain = Properties.GetPropertyInfoChain(e.Mobile, type, bc.Object,
PropertyAccess.Read, ref failReason);

Expand Down
23 changes: 8 additions & 15 deletions Scripts/Commands/Docs.cs
Expand Up @@ -69,9 +69,7 @@ private static void LoadTypes(Assembly a, Assembly[] asms)
TypeInfo info = new TypeInfo(type);
m_Types[type] = info;

m_Namespaces.TryGetValue(nspace, out List<TypeInfo> nspaces);

if (nspaces == null)
if (!m_Namespaces.TryGetValue(nspace, out List<TypeInfo> nspaces))
m_Namespaces[nspace] = nspaces = new List<TypeInfo>();

nspaces.Add(info);
Expand Down Expand Up @@ -682,9 +680,8 @@ private static bool Document()
m_Types = new Dictionary<Type, TypeInfo>();
m_Namespaces = new Dictionary<string, List<TypeInfo>>();

List<Assembly> assemblies = new List<Assembly>();
List<Assembly> assemblies = new List<Assembly> { Core.Assembly };

assemblies.Add(Core.Assembly);

foreach (Assembly asm in ScriptCompiler.Assemblies)
assemblies.Add(asm);
Expand Down Expand Up @@ -1882,9 +1879,7 @@ public int Compare(SpeechEntry x, SpeechEntry y)

lastIndex = index;

table.TryGetValue(index, out SpeechEntry entry);

if (entry == null)
if (!table.TryGetValue(index, out SpeechEntry entry))
table[index] = entry = new SpeechEntry(index);

entry.Strings.Add(text);
Expand Down Expand Up @@ -1925,12 +1920,12 @@ public class CommandEntrySorter : IComparer<DocCommandEntry>
public int Compare(DocCommandEntry a, DocCommandEntry b)
{
if (a == null && b == null) return 0;

int v = b?.AccessLevel.CompareTo(a?.AccessLevel) ?? 1;

if (v != 0)
return v;

return a?.Name.CompareTo(b?.Name) ?? 1;
}
}
Expand Down Expand Up @@ -2290,9 +2285,7 @@ private static void DocumentConstructibleObject(StreamWriter html, Type t, Const
{
html.Write(" <a ");

m_Types.TryGetValue(parms[j].ParameterType, out TypeInfo typeInfo);

if (typeInfo != null)
if (m_Types.TryGetValue(parms[j].ParameterType, out TypeInfo typeInfo))
html.Write("href=\"types/{0}\" ", typeInfo.FileName);

html.Write("title=\"{0}\">{1}</a>", GetTooltipFor(parms[j]), parms[j].Name);
Expand Down Expand Up @@ -2732,10 +2725,10 @@ public int Compare(BodyEntry a, BodyEntry b)

if (v != 0)
return v;

return a?.Name.CompareTo(b?.Name) ?? 1;
}
}

#endregion
}
}
Expand Up @@ -251,9 +251,7 @@ public virtual void Execute(CommandEventArgs e)
{
if (e.Length >= 1)
{
Commands.TryGetValue(e.GetString(0), out BaseCommand command);

if (command == null)
if (!Commands.TryGetValue(e.GetString(0), out BaseCommand command))
{
e.Mobile.SendMessage(
"That is either an invalid command name or one that does not support this modifier.");
Expand Down Expand Up @@ -294,4 +292,4 @@ public static void Register(BaseCommandImplementor impl)
impl.Register();
}
}
}
}
46 changes: 14 additions & 32 deletions Scripts/Commands/Profiling.cs
Expand Up @@ -99,10 +99,7 @@ public static void CountObjects_OnCommand(CommandEventArgs e)
{
Type type = item.GetType();

if (table.ContainsKey(type))
table[type] = 1 + table[type];
else
table[type] = 1;
table[type] = (table.TryGetValue(type, out int value) ? value : 0) + 1;
}

List<KeyValuePair<Type, int>> items = table.ToList();
Expand All @@ -112,10 +109,7 @@ public static void CountObjects_OnCommand(CommandEventArgs e)
{
Type type = m.GetType();

if (table.ContainsKey(type))
table[type] = 1 + table[type];
else
table[type] = 1;
table[type] = (table.TryGetValue(type, out int value) ? value : 0) + 1;
}

List<KeyValuePair<Type, int>> mobiles = table.ToList();
Expand Down Expand Up @@ -161,10 +155,8 @@ public static void TraceExpanded_OnCommand(CommandEventArgs e)

do
{
typeTable.TryGetValue(itemType, out int[] countTable);

if (countTable == null)
countTable = new int[9];
if (!typeTable.TryGetValue(itemType, out int[] countTable))
typeTable[itemType] = countTable = new int[9];

if ((flags & ExpandFlag.Name) != 0)
++countTable[0];
Expand Down Expand Up @@ -253,13 +245,13 @@ public static void TraceInternal_OnCommand(CommandEventArgs e)
++totalCount;

Type type = item.GetType();
int[] parms = table[type];

if (parms == null)
table[type] = parms = new[] { 0, 0 };

parms[0]++;
parms[1] += item.Amount;
if (table.TryGetValue(type, out int[] parms))
{
parms[0]++;
parms[1] += item.Amount;
} else
table[type] = new[] { 1, item.Amount };
}

using (StreamWriter op = new StreamWriter("internal.log"))
Expand Down Expand Up @@ -319,13 +311,9 @@ public static void ProfileWorld(string type, string opFile)
int length = bin.ReadInt32();
Type objType = types[typeID];

while (objType != typeof(object))
while (objType != null && objType != typeof(object))
{
if (table.ContainsKey(objType))
table[objType] = length + table[objType];
else
table[objType] = length;

table[objType] = length + (table.TryGetValue(objType, out int value) ? value : 0);
objType = objType.BaseType;
total += length;
}
Expand Down Expand Up @@ -362,10 +350,7 @@ public int Compare(KeyValuePair<Type, int> x, KeyValuePair<Type, int> y)

int v = -aCount.CompareTo(bCount);

if (v != 0)
return v;

return x.Key.FullName.CompareTo(y.Key.FullName);
return v != 0 ? v : x.Key.FullName.CompareTo(y.Key.FullName);
}
}

Expand All @@ -378,10 +363,7 @@ public int Compare(KeyValuePair<Type, int[]> x, KeyValuePair<Type, int[]> y)

int v = -aCount.CompareTo(bCount);

if (v != 0)
return v;

return x.Key.FullName.CompareTo(y.Key.FullName);
return v != 0 ? v : x.Key.FullName.CompareTo(y.Key.FullName);
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions Scripts/Commands/Statics.cs
Expand Up @@ -125,16 +125,12 @@ public static void Freeze(Mobile from, Map targetMap, Point3D start3d, Point3D e
if (itemMap == null || itemMap == Map.Internal)
continue;

Dictionary<Point2D, DeltaState> table = mapTable[itemMap];

if (table == null)
if (!mapTable.TryGetValue(itemMap, out Dictionary<Point2D, DeltaState> table))
mapTable[itemMap] = table = new Dictionary<Point2D, DeltaState>();

Point2D p = new Point2D(item.X >> 3, item.Y >> 3);

DeltaState state = table[p];

if (state == null)
if (!table.TryGetValue(p, out DeltaState state))
table[p] = state = new DeltaState(p);

state.m_List.Add(item);
Expand All @@ -159,16 +155,12 @@ public static void Freeze(Mobile from, Map targetMap, Point3D start3d, Point3D e
if (itemMap == null || itemMap == Map.Internal)
continue;

Dictionary<Point2D, DeltaState> table = mapTable[itemMap];

if (table == null)
if (!mapTable.TryGetValue(itemMap, out Dictionary<Point2D, DeltaState> table))
mapTable[itemMap] = table = new Dictionary<Point2D, DeltaState>();

Point2D p = new Point2D(item.X >> 3, item.Y >> 3);

DeltaState state = table[p];

if (state == null)
if (!table.TryGetValue(p, out DeltaState state))
table[p] = state = new DeltaState(p);

state.m_List.Add(item);
Expand Down
13 changes: 4 additions & 9 deletions Scripts/Engines/CannedEvil/ChampionSpawn.cs
Expand Up @@ -953,10 +953,7 @@ public void RegisterDamage(Mobile from, int amount)
if (from == null || !from.Player)
return;

if (m_DamageEntries.ContainsKey(from))
m_DamageEntries[from] += amount;
else
m_DamageEntries.Add(from, amount);
m_DamageEntries[from] = amount + (m_DamageEntries.TryGetValue(from, out int value) ? value : 0);
}

public void AwardArtifact(Item artifact)
Expand Down Expand Up @@ -1073,12 +1070,10 @@ public override void Deserialize(GenericReader reader)
case 5:
{
int entries = reader.ReadInt();
Mobile m;
int damage;
for (int i = 0; i < entries; ++i)
{
m = reader.ReadMobile();
damage = reader.ReadInt();
Mobile m = reader.ReadMobile();
int damage = reader.ReadInt();

if (m == null)
continue;
Expand Down Expand Up @@ -1243,4 +1238,4 @@ public override void Deserialize(GenericReader reader)
}
}
}
}
}
10 changes: 3 additions & 7 deletions Scripts/Engines/ConPVP/AcceptDuelGump.cs
Expand Up @@ -117,9 +117,7 @@ public void AutoReject()

public static void BeginIgnore(Mobile source, Mobile toIgnore)
{
List<IgnoreEntry> list = m_IgnoreLists[source];

if (list == null)
if (!m_IgnoreLists.TryGetValue(source, out List<IgnoreEntry> list))
m_IgnoreLists[source] = list = new List<IgnoreEntry>();

for (int i = 0; i < list.Count; ++i)
Expand All @@ -141,9 +139,7 @@ public static void BeginIgnore(Mobile source, Mobile toIgnore)

public static bool IsIgnored(Mobile source, Mobile check)
{
List<IgnoreEntry> list = m_IgnoreLists[source];

if (list == null)
if (!m_IgnoreLists.TryGetValue(source, out List<IgnoreEntry> list))
return false;

for (int i = 0; i < list.Count; ++i)
Expand Down Expand Up @@ -280,4 +276,4 @@ public void Refresh()
}
}
}
}
}
2 changes: 1 addition & 1 deletion Scripts/Engines/ConPVP/Games/BombingRun.cs
Expand Up @@ -1247,7 +1247,7 @@ public BRTeamInfo(int teamID, GenericReader ip)
if (mob == null)
return null;

if (!(Players[mob] is BRPlayerInfo val))
if (!Players.TryGetValue(mob, out BRPlayerInfo val))
Players[mob] = val = new BRPlayerInfo(this, mob);

return val;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Engines/ConPVP/Games/KingOfTheHill.cs
Expand Up @@ -604,7 +604,7 @@ public KHTeamInfo(int teamID, GenericReader ip)
if (mob == null)
return null;

if (!(Players[mob] is KHPlayerInfo val))
if (!Players.TryGetValue(mob, out KHPlayerInfo val))
Players[mob] = val = new KHPlayerInfo(this, mob);

return val;
Expand Down
9 changes: 4 additions & 5 deletions Scripts/Engines/ConPVP/Ladder.cs
Expand Up @@ -269,9 +269,7 @@ public void UpdateEntry(LadderEntry entry)

public LadderEntry Find(Mobile mob)
{
LadderEntry entry = m_Table[mob];

if (entry == null)
if (m_Table.TryGetValue(mob, out LadderEntry entry))
{
m_Table[mob] = entry = new LadderEntry(mob, this);
entry.Index = Entries.Count;
Expand All @@ -283,7 +281,8 @@ public LadderEntry Find(Mobile mob)

public LadderEntry FindNoCreate(Mobile mob)
{
return m_Table[mob];
m_Table.TryGetValue(mob, out LadderEntry entry);
return entry;
}

public void Serialize(GenericWriter writer)
Expand Down Expand Up @@ -364,4 +363,4 @@ public void Serialize(GenericWriter writer)
writer.WriteEncodedInt(Losses);
}
}
}
}
6 changes: 2 additions & 4 deletions Scripts/Engines/ConPVP/Preferences.cs
Expand Up @@ -107,9 +107,7 @@ public Preferences(GenericReader reader)

public PreferencesEntry Find(Mobile mob)
{
PreferencesEntry entry = m_Table[mob];

if (entry == null)
if (m_Table.TryGetValue(mob, out PreferencesEntry entry))
{
m_Table[mob] = entry = new PreferencesEntry(mob);
Entries.Add(entry);
Expand Down Expand Up @@ -276,4 +274,4 @@ private void AddColumnHeader(int width, string name)
m_ColumnX += width;
}
}
}
}