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
@@ -718,13 +718,8 @@ namespace Barotrauma
if (wiringMode)
{
CreateDummyCharacter();
var screwdriverPrefab = ItemPrefab.list.Find(ip => ip.Name == "Screwdriver") as ItemPrefab;
var item = new Item(screwdriverPrefab, Vector2.Zero, null);
var item = new Item(MapEntityPrefab.Find("Screwdriver") as ItemPrefab, Vector2.Zero, null);
dummyCharacter.Inventory.TryPutItem(item, null, new List<InvSlotType>() { InvSlotType.RightHand });
wiringToolPanel = CreateWiringPanel();
}
else
@@ -760,11 +755,12 @@ namespace Barotrauma
GUIListBox listBox = new GUIListBox(Rectangle.Empty, "", frame);
listBox.OnSelected = SelectWire;
foreach (MapEntityPrefab ep in MapEntityPrefab.list)
{
var itemPrefab = ep as ItemPrefab;
if (itemPrefab == null || itemPrefab.Name == null || !itemPrefab.Name.Contains("Wire")) continue;
if (itemPrefab == null || itemPrefab.Name == null) continue;
if (!itemPrefab.Name.Contains("Wire") && (itemPrefab.Aliases == null || !itemPrefab.Aliases.Any(a => a.Contains("Wire")))) continue;
GUIFrame imgFrame = new GUIFrame(new Rectangle(0, 0, (int)itemPrefab.sprite.size.X, (int)itemPrefab.sprite.size.Y), null, listBox);
imgFrame.UserData = itemPrefab;
@@ -98,7 +98,7 @@ namespace Barotrauma
{
string itemName = itemElement.GetAttributeString("name", "");
ItemPrefab itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
ItemPrefab itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Tried to spawn \"" + Name + "\" with the item \"" + itemName + "\". Matching item prefab not found.");
@@ -124,7 +124,7 @@ namespace Barotrauma
character.Inventory.TryPutItem(item, null, item.AllowedSlots);
}
if (item.Prefab.Name == "ID Card" && spawnPoint != null)
if (item.Prefab.NameMatches("ID Card") && spawnPoint != null)
{
foreach (string s in spawnPoint.IdCardTags)
{
@@ -213,7 +213,7 @@ namespace Barotrauma
string itemName = string.Join(" ", args.Take(args.Length - extraParams)).ToLowerInvariant();
var itemPrefab = MapEntityPrefab.list.Find(ip => ip.Name.ToLowerInvariant() == itemName) as ItemPrefab;
var itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null)
{
ThrowError("Item \"" + itemName + "\" not found!");
@@ -14,19 +14,18 @@ namespace Barotrauma
public override string ToString()
{
return "ScriptedEvent (" + (itemPrefab==null ? "null" : itemPrefab.Name) + ")";
return "ScriptedEvent (" + (itemPrefab == null ? "null" : itemPrefab.Name) + ")";
}
public ArtifactEvent(XElement element)
: base(element)
{
string itemName = element.GetAttributeString("itemname", "");
itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name "+itemName);
DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name " + itemName);
}
}
@@ -43,15 +43,15 @@ namespace Barotrauma
{
string itemName = element.GetAttributeString("name", "");
ItemPrefab itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
if (itemPrefab==null)
ItemPrefab itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Couldn't spawn item for cargo mission: item prefab \""+element.Name.ToString()+"\" not found");
DebugConsole.ThrowError("Couldn't spawn item for cargo mission: item prefab \"" + element.Name.ToString() + "\" not found");
return;
}
WayPoint cargoSpawnPos = WayPoint.GetRandom(SpawnType.Cargo, null, Submarine.MainSub, true);
if (cargoSpawnPos==null)
if (cargoSpawnPos == null)
{
DebugConsole.ThrowError("Couldn't spawn items for cargo mission, cargo spawnpoint not found");
return;
@@ -28,8 +28,13 @@ namespace Barotrauma
{
string itemName = element.GetAttributeString("itemname", "");
itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name " + itemName);
return;
}
string spawnPositionTypeStr = element.GetAttributeString("spawntype", "");
if (string.IsNullOrWhiteSpace(spawnPositionTypeStr) ||
@@ -37,11 +42,6 @@ namespace Barotrauma
{
spawnPositionType = Level.PositionType.Cave | Level.PositionType.Ruin;
}
if (itemPrefab == null)
{
DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name "+itemName);
}
}
public override void Start(Level level)
@@ -390,7 +390,7 @@ namespace Barotrauma.Items.Components
for (int i = 0; i < 2; i++)
{
hullRects[i].Location -= MathUtils.ToPoint((subs[i].WorldPosition - subs[i].HiddenSubPosition));
hulls[i] = new Hull(MapEntityPrefab.list.Find(m => m.Name == "Hull"), hullRects[i], subs[i]);
hulls[i] = new Hull(MapEntityPrefab.Find("Hull"), hullRects[i], subs[i]);
hulls[i].AddToGrid(subs[i]);
for (int j = 0; j < 2; j++)
@@ -420,7 +420,7 @@ namespace Barotrauma.Items.Components
for (int i = 0; i < 2; i++)
{
hullRects[i].Location -= MathUtils.ToPoint((subs[i].WorldPosition - subs[i].HiddenSubPosition));
hulls[i] = new Hull(MapEntityPrefab.list.Find(m => m.Name == "Hull"), hullRects[i], subs[i]);
hulls[i] = new Hull(MapEntityPrefab.Find("Hull"), hullRects[i], subs[i]);
hulls[i].AddToGrid(subs[i]);
if (hullIds[i] != null) hulls[i].ID = (ushort)hullIds[i];
@@ -59,7 +59,7 @@ namespace Barotrauma.Items.Components
{
if (deconstructProduct.RequireFullCondition && targetItem.Condition < targetItem.Prefab.Health) continue;
var itemPrefab = MapEntityPrefab.list.FirstOrDefault(ip => ip.Name.ToLowerInvariant() == deconstructProduct.ItemPrefabName.ToLowerInvariant()) as ItemPrefab;
var itemPrefab = MapEntityPrefab.Find(deconstructProduct.ItemPrefabName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Tried to deconstruct item \"" + targetItem.Name + "\" but couldn't find item prefab \"" + deconstructProduct + "\"!");
@@ -22,7 +22,7 @@ namespace Barotrauma.Items.Components
{
string name = element.GetAttributeString("name", "");
TargetItem = MapEntityPrefab.list.Find(ip => ip.Name.ToLowerInvariant() == name.ToLowerInvariant()) as ItemPrefab;
TargetItem = MapEntityPrefab.Find(name) as ItemPrefab;
if (TargetItem == null)
{
@@ -30,9 +30,7 @@ namespace Barotrauma.Items.Components
}
RequiredSkills = new List<Skill>();
RequiredTime = element.GetAttributeFloat("requiredtime", 1.0f);
RequiredItems = new List<Tuple<ItemPrefab, int>>();
string[] requiredItemNames = element.GetAttributeString("requireditems", "").Split(',');
@@ -40,25 +38,22 @@ namespace Barotrauma.Items.Components
{
if (string.IsNullOrWhiteSpace(requiredItemName)) continue;
ItemPrefab requiredItem = ItemPrefab.list.Find(ip => ip.Name.ToLowerInvariant() == requiredItemName.Trim().ToLowerInvariant()) as ItemPrefab;
ItemPrefab requiredItem = MapEntityPrefab.Find(requiredItemName.Trim()) as ItemPrefab;
if (requiredItem == null)
{
DebugConsole.ThrowError("Error in fabricable item " + name + "! Required item \"" + requiredItemName + "\" not found.");
continue;
}
var existing = RequiredItems.Find(r => r.Item1 == requiredItem);
if (existing == null)
{
RequiredItems.Add(new Tuple<ItemPrefab, int>(requiredItem, 1));
}
else
{
RequiredItems.Remove(existing);
RequiredItems.Add(new Tuple<ItemPrefab, int>(requiredItem, existing.Item2+1));
RequiredItems.Add(new Tuple<ItemPrefab, int>(requiredItem, existing.Item2 + 1));
}
}
@@ -1488,11 +1488,8 @@ namespace Barotrauma
if (!spawn) return null;
//----------------------------------------
var prefab = MapEntityPrefab.list.Find(me => me.Name == itemName);
if (prefab == null) return null;
var itemPrefab = prefab as ItemPrefab;
var itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null) return null;
Inventory inventory = null;
@@ -1592,68 +1589,64 @@ namespace Barotrauma
public static void Load(XElement element, Submarine submarine)
{
Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
string name = element.Attribute("name").Value;
foreach (MapEntityPrefab ep in MapEntityPrefab.list)
ItemPrefab prefab = MapEntityPrefab.Find(name) as ItemPrefab;
if (prefab == null)
{
ItemPrefab ip = ep as ItemPrefab;
if (ip == null) continue;
DebugConsole.ThrowError("Error loading item - item prefab \"" + name + "\" not found.");
return;
}
if (ip.Name != name && (ip.Aliases == null || !ip.Aliases.Contains(name))) continue;
Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
if (rect.Width == 0 && rect.Height == 0)
{
rect.Width = (int)prefab.Size.X;
rect.Height = (int)prefab.Size.Y;
}
if (rect.Width == 0 && rect.Height == 0)
Item item = new Item(rect, prefab, submarine);
item.Submarine = submarine;
item.ID = (ushort)int.Parse(element.Attribute("ID").Value);
item.linkedToID = new List<ushort>();
foreach (XAttribute attribute in element.Attributes())
{
SerializableProperty property = null;
if (!item.properties.TryGetValue(attribute.Name.ToString(), out property)) continue;
bool shouldBeLoaded = false;
foreach (var propertyAttribute in property.Attributes.OfType<Serialize>())
{
rect.Width = (int)ip.Size.X;
rect.Height = (int)ip.Size.Y;
}
Item item = new Item(rect, ip, submarine);
item.Submarine = submarine;
item.ID = (ushort)int.Parse(element.Attribute("ID").Value);
item.linkedToID = new List<ushort>();
foreach (XAttribute attribute in element.Attributes())
{
SerializableProperty property = null;
if (!item.properties.TryGetValue(attribute.Name.ToString(), out property)) continue;
bool shouldBeLoaded = false;
foreach (var propertyAttribute in property.Attributes.OfType<Serialize>())
if (propertyAttribute.isSaveable)
{
if (propertyAttribute.isSaveable)
{
shouldBeLoaded = true;
break;
}
}
if (shouldBeLoaded) property.TrySetValue(attribute.Value);
}
string linkedToString = element.GetAttributeString("linked", "");
if (linkedToString!="")
{
string[] linkedToIds = linkedToString.Split(',');
for (int i = 0; i<linkedToIds.Length;i++)
{
item.linkedToID.Add((ushort)int.Parse(linkedToIds[i]));
shouldBeLoaded = true;
break;
}
}
foreach (XElement subElement in element.Elements())
if (shouldBeLoaded) property.TrySetValue(attribute.Value);
}
string linkedToString = element.GetAttributeString("linked", "");
if (linkedToString != "")
{
string[] linkedToIds = linkedToString.Split(',');
for (int i = 0; i < linkedToIds.Length; i++)
{
ItemComponent component = item.components.Find(x => x.Name == subElement.Name.ToString());
if (component == null) continue;
component.Load(subElement);
item.linkedToID.Add((ushort)int.Parse(linkedToIds[i]));
}
break;
}
foreach (XElement subElement in element.Elements())
{
ItemComponent component = item.components.Find(x => x.Name == subElement.Name.ToString());
if (component == null) continue;
component.Load(subElement);
}
}
@@ -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())
{
@@ -429,10 +429,10 @@ namespace Barotrauma.Networking
//(in order to give them appropriate ID card tags)
var mainSubSpawnPoints = WayPoint.SelectCrewSpawnPoints(characterInfos, Submarine.MainSub);
ItemPrefab divingSuitPrefab = ItemPrefab.list.Find(ip => ip.Name == "Diving Suit") as ItemPrefab;
ItemPrefab oxyPrefab = ItemPrefab.list.Find(ip => ip.Name == "Oxygen Tank") as ItemPrefab;
ItemPrefab scooterPrefab = ItemPrefab.list.Find(ip => ip.Name == "Underwater Scooter") as ItemPrefab;
ItemPrefab batteryPrefab = ItemPrefab.list.Find(ip => ip.Name == "Battery Cell") as ItemPrefab;
ItemPrefab divingSuitPrefab = MapEntityPrefab.Find("Diving Suit") as ItemPrefab;
ItemPrefab oxyPrefab = MapEntityPrefab.Find("Oxygen Tank") as ItemPrefab;
ItemPrefab scooterPrefab = MapEntityPrefab.Find("Underwater Scooter") as ItemPrefab;
ItemPrefab batteryPrefab = MapEntityPrefab.Find("Battery Cell") as ItemPrefab;
var cargoSp = WayPoint.WayPointList.Find(wp => wp.Submarine == respawnShuttle && wp.SpawnType == SpawnType.Cargo);