(519124ad1) Fixed items with no sprite crashing the game. Closes #1153

This commit is contained in:
Joonas Rikkonen
2019-03-28 12:40:08 +02:00
parent a8222e429f
commit bcd779fe78
3 changed files with 48 additions and 7 deletions
@@ -11,9 +11,14 @@ namespace Barotrauma
/// </summary> /// </summary>
public static class TextureLoader public static class TextureLoader
{ {
public static Texture2D PlaceHolderTexture
{
get;
private set;
}
static TextureLoader() static TextureLoader()
{ {
BlendColorBlendState = new BlendState BlendColorBlendState = new BlendState
{ {
ColorDestinationBlend = Blend.Zero, ColorDestinationBlend = Blend.Zero,
@@ -38,6 +43,15 @@ namespace Barotrauma
_graphicsDevice = graphicsDevice; _graphicsDevice = graphicsDevice;
_needsBmp = needsBmp; _needsBmp = needsBmp;
_spriteBatch = new SpriteBatch(_graphicsDevice); _spriteBatch = new SpriteBatch(_graphicsDevice);
PlaceHolderTexture = new Texture2D(graphicsDevice, 32, 32);
Color[] data = new Color[32 * 32];
for (int i = 0; i < 32 * 32; i++)
{
data[i] = Color.Magenta;
}
PlaceHolderTexture.SetData(data);
} }
public static Texture2D FromFile(string path, bool preMultiplyAlpha = true) public static Texture2D FromFile(string path, bool preMultiplyAlpha = true)
@@ -209,12 +209,6 @@ namespace Barotrauma.Items.Components
private set; private set;
} }
public AITarget AITarget
{
get;
private set;
}
public ItemComponent(Item item, XElement element) public ItemComponent(Item item, XElement element)
{ {
this.item = item; this.item = item;
@@ -521,6 +521,39 @@ namespace Barotrauma
AllowedLinks = element.GetAttributeStringArray("allowedlinks", new string[0], convertToLowerInvariant: true).ToList(); AllowedLinks = element.GetAttributeStringArray("allowedlinks", new string[0], convertToLowerInvariant: true).ToList();
if (sprite == null)
{
DebugConsole.ThrowError("Item \"" + Name + "\" has no sprite!");
#if SERVER
sprite = new Sprite("", Vector2.Zero);
sprite.SourceRect = new Rectangle(0, 0, 32, 32);
#else
sprite = new Sprite(TextureLoader.PlaceHolderTexture, null, null)
{
Origin = TextureLoader.PlaceHolderTexture.Bounds.Size.ToVector2() / 2
};
#endif
size = sprite.size;
sprite.EntityID = identifier;
}
if (!category.HasFlag(MapEntityCategory.Legacy) && string.IsNullOrEmpty(identifier))
{
DebugConsole.ThrowError(
"Item prefab \"" + name + "\" has no identifier. All item prefabs have a unique identifier string that's used to differentiate between items during saving and loading.");
}
if (!string.IsNullOrEmpty(identifier))
{
MapEntityPrefab existingPrefab = List.Find(e => e.Identifier == identifier);
if (existingPrefab != null)
{
DebugConsole.ThrowError(
"Map entity prefabs \"" + name + "\" and \"" + existingPrefab.Name + "\" have the same identifier!");
}
}
AllowedLinks = element.GetAttributeStringArray("allowedlinks", new string[0], convertToLowerInvariant: true).ToList();
List.Add(this); List.Add(this);
} }