From bcd779fe78f0bffa13dd4858913414308c6ba2dc Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Thu, 28 Mar 2019 12:40:08 +0200 Subject: [PATCH] (519124ad1) Fixed items with no sprite crashing the game. Closes #1153 --- .../Source/Utils/TextureLoader.cs | 16 ++++++++- .../Source/Items/Components/ItemComponent.cs | 6 ---- .../Source/Items/ItemPrefab.cs | 33 +++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Utils/TextureLoader.cs b/Barotrauma/BarotraumaClient/Source/Utils/TextureLoader.cs index 484ef9285..30b9805c7 100644 --- a/Barotrauma/BarotraumaClient/Source/Utils/TextureLoader.cs +++ b/Barotrauma/BarotraumaClient/Source/Utils/TextureLoader.cs @@ -11,9 +11,14 @@ namespace Barotrauma /// public static class TextureLoader { + public static Texture2D PlaceHolderTexture + { + get; + private set; + } + static TextureLoader() { - BlendColorBlendState = new BlendState { ColorDestinationBlend = Blend.Zero, @@ -38,6 +43,15 @@ namespace Barotrauma _graphicsDevice = graphicsDevice; _needsBmp = needsBmp; _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) diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs index 5c0fb03b4..e0050c17c 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs @@ -203,12 +203,6 @@ namespace Barotrauma.Items.Components set; } - public AITarget AITarget - { - get; - private set; - } - public AITarget AITarget { get; diff --git a/Barotrauma/BarotraumaShared/Source/Items/ItemPrefab.cs b/Barotrauma/BarotraumaShared/Source/Items/ItemPrefab.cs index 0bc5c0d78..105a87746 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/ItemPrefab.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/ItemPrefab.cs @@ -521,6 +521,39 @@ namespace Barotrauma 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); }