Browse Source

Fixes to PosterEngine for multiple timberline things

Kenric Nugteren 1 year ago
parent
commit
1586c694cf
1 changed files with 18 additions and 3 deletions
  1. 18 3
      InABox.Core/Postable/PosterUtils.cs

+ 18 - 3
InABox.Core/Postable/PosterUtils.cs

@@ -16,6 +16,8 @@ namespace InABox.Core
         {
             public Type Engine { get; set; }
 
+            public Type Entity { get; set; }
+
             public Type Poster { get; set; }
         }
 
@@ -44,6 +46,7 @@ namespace InABox.Core
             ).Select(x => new EngineType
             {
                 Engine = x,
+                Entity = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[0],
                 Poster = x.GetInterfaceDefinition(typeof(IPosterEngine<,,>))!.GenericTypeArguments[1].GetGenericTypeDefinition()
             }).ToArray();
             return _posterEngines;
@@ -129,14 +132,26 @@ namespace InABox.Core
             }
             var poster = GetPosters()?.FirstOrDefault(x => x.EntityName() == settings.PosterType)!;
 
-            return (GetPosterEngines().FirstOrDefault(x => poster.HasInterface(x.Poster))?.Engine
-                ?? throw new Exception("No poster for the given settings.")).MakeGenericType(typeof(T));
+            var engines = GetPosterEngines().Where(x => poster.HasInterface(x.Poster)).ToList();
+            if (!engines.Any())
+            {
+                throw new Exception("No poster for the given settings.");
+            }
+            else if(engines.Count == 1)
+            {
+                return engines[0].Engine.MakeGenericType(typeof(T));
+            }
+            else
+            {
+                return engines.Single(x => x.Entity == typeof(T)).Engine.MakeGenericType(typeof(T));
+            }
         }
 
         public static IPosterEngine<T> CreateEngine<T>()
             where T : Entity, IPostable, IRemotable, IPersistent, new()
         {
-            return (Activator.CreateInstance(GetEngine<T>()) as IPosterEngine<T>)!;
+            var engine = GetEngine<T>();
+            return (Activator.CreateInstance(engine) as IPosterEngine<T>)!;
         }
 
         /// <summary>