Replaced instances where MapEntityPrefabs are searched for based on the name with a static Find method that takes the aliases of the prefabs into account.

This commit is contained in:
Joonas Rikkonen
2017-11-14 21:40:43 +02:00
parent d072713936
commit 5f4bf48449
17 changed files with 141 additions and 130 deletions
@@ -101,7 +101,7 @@ namespace Barotrauma
{ }
public Gap(Rectangle newRect, bool isHorizontal, Submarine submarine)
: base (MapEntityPrefab.list.Find(m=> m.Name == "Gap"), submarine)
: base (MapEntityPrefab.Find("Gap"), submarine)
{
rect = newRect;
linkedTo = new ObservableCollection<MapEntity>();
@@ -255,7 +255,7 @@ namespace Barotrauma
public override MapEntity Clone()
{
return new Hull(MapEntityPrefab.list.Find(m => m.Name == "Hull"), rect, Submarine);
return new Hull(MapEntityPrefab.Find("Hull"), rect, Submarine);
}
public static EntityGrid GenerateEntityGrid(Submarine submarine)
@@ -722,7 +722,7 @@ namespace Barotrauma
int.Parse(element.Attribute("height").Value));
}
Hull h = new Hull(MapEntityPrefab.list.Find(m => m.Name == "Hull"), rect, submarine);
Hull h = new Hull(MapEntityPrefab.Find("Hull"), rect, submarine);
h.waterVolume = element.GetAttributeFloat("pressure", 0.0f);
@@ -389,12 +389,11 @@ namespace Barotrauma.RuinGeneration
prop.Prefab as StructurePrefab, null).MoveWithLevel = true;
}
}
//generate doors & sensors that close them -------------------------------------------------------------
var sensorPrefab = ItemPrefab.list.Find(ip => ip.Name == "Alien Motion Sensor") as ItemPrefab;
var wirePrefab = ItemPrefab.list.Find(ip => ip.Name == "Wire") as ItemPrefab;
var sensorPrefab = MapEntityPrefab.Find("Alien Motion Sensor") as ItemPrefab;
var wirePrefab = MapEntityPrefab.Find("Wire") as ItemPrefab;
foreach (Corridor corridor in corridors)
{
@@ -27,26 +27,26 @@ namespace Barotrauma.RuinGeneration
private RuinStructure(XElement element)
{
string prefab = element.GetAttributeString("prefab", "").ToLowerInvariant();
Prefab = MapEntityPrefab.list.Find(s => s.Name.ToLowerInvariant() == prefab);
string name = element.GetAttributeString("prefab", "");
Prefab = MapEntityPrefab.Find(name);
if (Prefab == null)
{
DebugConsole.ThrowError("Loading ruin structure failed - structure prefab \""+prefab+" not found");
DebugConsole.ThrowError("Loading ruin structure failed - structure prefab \"" + name + " not found");
return;
}
string alignmentStr = element.GetAttributeString("alignment","Bottom");
string alignmentStr = element.GetAttributeString("alignment", "Bottom");
if (!Enum.TryParse<Alignment>(alignmentStr, true, out Alignment))
{
DebugConsole.ThrowError("Error in ruin structure \""+prefab+"\" - "+alignmentStr+" is not a valid alignment");
DebugConsole.ThrowError("Error in ruin structure \"" + name + "\" - " + alignmentStr + " is not a valid alignment");
}
string typeStr = element.GetAttributeString("type","");
if (!Enum.TryParse<RuinStructureType>(typeStr,true, out Type))
string typeStr = element.GetAttributeString("type", "");
if (!Enum.TryParse<RuinStructureType>(typeStr, true, out Type))
{
DebugConsole.ThrowError("Error in ruin structure \"" + prefab + "\" - " + typeStr + " is not a valid type");
DebugConsole.ThrowError("Error in ruin structure \"" + name + "\" - " + typeStr + " is not a valid type");
return;
}
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Barotrauma
@@ -191,6 +192,43 @@ namespace Barotrauma
}
}
public static MapEntityPrefab Find(string name, bool caseSensitive = false)
{
if (caseSensitive)
{
foreach (MapEntityPrefab prefab in list)
{
if (prefab.name == name || (prefab.Aliases != null && prefab.Aliases.Contains(name))) return prefab;
}
}
else
{
name = name.ToLowerInvariant();
foreach (MapEntityPrefab prefab in list)
{
if (prefab.name.ToLowerInvariant() == name || (prefab.Aliases != null && prefab.Aliases.Any(a => a.ToLowerInvariant() == name))) return prefab;
}
}
return null;
}
/// <summary>
/// Check if the name or any of the aliases of this prefab match the given name.
/// </summary>
public bool NameMatches(string name, bool caseSensitive = false)
{
if (caseSensitive)
{
return this.name == name || (Aliases != null && Aliases.Any(a => a == name));
}
else
{
name = name.ToLowerInvariant();
return this.name.ToLowerInvariant() == name || (Aliases != null && Aliases.Any(a => a.ToLowerInvariant() == name));
}
}
//a method that allows the GUIListBoxes to check through a delegate if the entityprefab is still selected
public static object GetSelected()
{
@@ -824,28 +824,19 @@ namespace Barotrauma
public static void Load(XElement element, Submarine submarine)
{
Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
string name = element.Attribute("name").Value;
Structure s = null;
foreach (MapEntityPrefab ep in MapEntityPrefab.list)
StructurePrefab prefab = MapEntityPrefab.Find(name) as StructurePrefab;
if (prefab == null)
{
if (ep.Name == name || (ep.Aliases != null && ep.Aliases.Contains(name)))
{
s = new Structure(rect, (StructurePrefab)ep, submarine);
s.Submarine = submarine;
s.ID = (ushort)int.Parse(element.Attribute("ID").Value);
break;
}
}
if (s == null)
{
DebugConsole.ThrowError("Structure prefab " + name + " not found.");
DebugConsole.ThrowError("Error loading structure - structure prefab " + name + " not found.");
return;
}
Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
Structure s = new Structure(rect, prefab, submarine);
s.Submarine = submarine;
s.ID = (ushort)int.Parse(element.Attribute("ID").Value);
foreach (XElement subElement in element.Elements())
{