Skip to content

Commit

Permalink
Crash on image/panel not found and add TryGet functions for searching
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Oct 19, 2022
1 parent 686f158 commit 7f314c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
29 changes: 22 additions & 7 deletions OpenRA.Game/Graphics/ChromeProvider.cs
Expand Up @@ -144,6 +144,15 @@ static void LoadCollection(string name, MiniYaml yaml)
}

public static Sprite GetImage(string collectionName, string imageName)
{
var image = TryGetImage(collectionName, imageName);
if (image == null)
throw new ArgumentException($"Sprite `{collectionName}/{imageName}` was not found.");

return image;
}

public static Sprite TryGetImage(string collectionName, string imageName)
{
if (string.IsNullOrEmpty(collectionName))
return null;
Expand All @@ -153,10 +162,7 @@ public static Sprite GetImage(string collectionName, string imageName)
return sprite;

if (!collections.TryGetValue(collectionName, out var collection))
{
Log.Write("debug", "Could not find collection '{0}'", collectionName);
return null;
}

if (!collection.Regions.TryGetValue(imageName, out var mi))
return null;
Expand All @@ -176,6 +182,15 @@ public static Sprite GetImage(string collectionName, string imageName)
}

public static Sprite[] GetPanelImages(string collectionName)
{
var panel = TryGetPanelImages(collectionName);
if (panel == null)
throw new ArgumentException($"Panel `{collectionName}` was not found.");

return panel;
}

public static Sprite[] TryGetPanelImages(string collectionName)
{
if (string.IsNullOrEmpty(collectionName))
return null;
Expand All @@ -185,17 +200,14 @@ public static Sprite[] GetPanelImages(string collectionName)
return cachedSprites;

if (!collections.TryGetValue(collectionName, out var collection))
{
Log.Write("debug", "Could not find collection '{0}'", collectionName);
return null;
}

Sprite[] sprites;
if (collection.PanelRegion != null)
{
if (collection.PanelRegion.Length != 8)
{
Log.Write("debug", "Collection '{0}' does not define a valid PanelRegion", collectionName);
Log.Write("debug", $"Collection '{collectionName}' does not define a valid PanelRegion");
return null;
}

Expand All @@ -222,6 +234,9 @@ public static Sprite[] GetPanelImages(string collectionName)
}
else
{
if (!collection.Regions.Any())
return Array.Empty<Sprite>();

// Support manual definitions for unusual dialog layouts
sprites = new[]
{
Expand Down
9 changes: 1 addition & 8 deletions OpenRA.Mods.Common/Widgets/ImageWidget.cs
Expand Up @@ -33,14 +33,7 @@ public class ImageWidget : Widget
public Func<string> GetTooltipText;

readonly CachedTransform<(string, string), Sprite> getImageCache = new CachedTransform<(string, string), Sprite>(
((string collection, string image) args) =>
{
var sprite = ChromeProvider.GetImage(args.collection, args.image);
if (sprite == null)
throw new ArgumentException($"Sprite {args.collection}/{args.image} was not found.");
return sprite;
});
((string collection, string image) args) => ChromeProvider.GetImage(args.collection, args.image));

public ImageWidget()
{
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Widgets/WidgetUtils.cs
Expand Up @@ -38,7 +38,7 @@ public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Foc
{
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
var variantImageName = GetStatefulImageName(imageName, args.Disabled, args.Pressed, args.Hover, args.Focused);
return ChromeProvider.GetImage(collectionName, variantImageName) ?? ChromeProvider.GetImage(collectionName, imageName);
return ChromeProvider.TryGetImage(collectionName, variantImageName) ?? ChromeProvider.GetImage(collectionName, imageName);
});
}

Expand All @@ -50,7 +50,7 @@ public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Foc
{
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
var variantCollectionName = GetStatefulImageName(collectionName, args.Disabled, args.Pressed, args.Hover, args.Focused);
return ChromeProvider.GetPanelImages(variantCollectionName) ?? ChromeProvider.GetPanelImages(collectionName);
return ChromeProvider.TryGetPanelImages(variantCollectionName) ?? ChromeProvider.GetPanelImages(collectionName);
});
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public static void DrawSpriteCentered(Sprite s, PaletteReference p, float2 pos,

public static void DrawPanel(string collection, Rectangle bounds)
{
var sprites = ChromeProvider.GetPanelImages(collection);
var sprites = ChromeProvider.TryGetPanelImages(collection);
if (sprites != null)
DrawPanel(bounds, sprites);
}
Expand Down

0 comments on commit 7f314c6

Please sign in to comment.