Custom Upgrade Providers

This commit is contained in:
Michael 2017-04-29 08:43:00 -04:00
parent f762756dbc
commit fc815c4869
3 changed files with 30 additions and 19 deletions

View file

@ -53,7 +53,6 @@ namespace ShiftOS.WinForms
SkinEngine.SetIconProber(new ShiftOSIconProvider());
ShiftOS.Engine.AudioManager.Init(new ShiftOSAudioProvider());
Localization.RegisterProvider(new WFLanguageProvider());
Shiftorium.RegisterProvider(new WinformsShiftoriumProvider());
AppearanceManager.OnExit += () =>
{
Environment.Exit(0);
@ -95,6 +94,7 @@ namespace ShiftOS.WinForms
}
}
[ShiftoriumProvider]
internal class WinformsShiftoriumProvider : IShiftoriumProvider
{
public List<ShiftoriumUpgrade> GetDefaults()

View file

@ -41,8 +41,7 @@ namespace ShiftOS.Engine
private static WaveOut _out = null;
private static AudioFileReader _reader = null;
private static IAudioProvider _provider = null;
private static bool _running = true;
/// <summary>
/// Stops the current sound if one is playing and disposes of the sound.
/// </summary>

View file

@ -280,6 +280,7 @@ namespace ShiftOS.Engine
private static IShiftoriumProvider _provider = null;
[Obsolete("Please annotate your provider with a [ShiftoriumProvider] attribute instead. This function doesn't do anything.")]
public static void RegisterProvider(IShiftoriumProvider p)
{
_provider = p;
@ -289,18 +290,6 @@ namespace ShiftOS.Engine
public static List<ShiftoriumUpgrade> GetDefaults()
{
List<ShiftoriumUpgrade> list = new List<ShiftoriumUpgrade>();
try
{
list = _provider.GetDefaults();
}
catch (Exception ex)
{
Console.WriteLine("Couldn't get the upgrade definition list from the provider.");
Console.WriteLine("This might be able to help:");
Console.WriteLine(ex);
list = JsonConvert.DeserializeObject<List<ShiftoriumUpgrade>>(Properties.Resources.Shiftorium);
}
//Now we probe for ShiftoriumUpgradeAttributes for mods.
foreach(var file in System.IO.Directory.GetFiles(Environment.CurrentDirectory))
{
@ -311,10 +300,20 @@ namespace ShiftOS.Engine
var asm = Assembly.LoadFile(file);
foreach (var type in asm.GetTypes())
{
if (type.GetInterfaces().Contains(typeof(IShiftoriumProvider)))
{
if(type.GetCustomAttributes().FirstOrDefault(x=> x is ShiftoriumProviderAttribute) != null)
{
var _p = Activator.CreateInstance(type, null) as IShiftoriumProvider;
list.AddRange(_p.GetDefaults());
}
}
ShiftoriumUpgradeAttribute attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
if (attrib != null)
{
if (list.FirstOrDefault(x => x.Id == attrib.Upgrade) != null)
if (list.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
throw new ShiftoriumConflictException(attrib.Upgrade);
list.Add(new ShiftoriumUpgrade
{
@ -332,7 +331,7 @@ namespace ShiftOS.Engine
attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
if (attrib != null)
{
if (list.FirstOrDefault(x => x.Id == attrib.Upgrade) != null)
if (list.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
throw new ShiftoriumConflictException(attrib.Upgrade);
list.Add(new ShiftoriumUpgrade
{
@ -352,7 +351,7 @@ namespace ShiftOS.Engine
attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
if (attrib != null)
{
if (list.FirstOrDefault(x => x.Id == attrib.Upgrade) != null)
if (list.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
throw new ShiftoriumConflictException(attrib.Upgrade);
list.Add(new ShiftoriumUpgrade
{
@ -372,7 +371,7 @@ namespace ShiftOS.Engine
attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
if (attrib != null)
{
if (list.FirstOrDefault(x => x.Id == attrib.Upgrade) != null)
if (list.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
throw new ShiftoriumConflictException(attrib.Upgrade);
list.Add(new ShiftoriumUpgrade
{
@ -392,6 +391,14 @@ namespace ShiftOS.Engine
catch { }
}
}
foreach(var item in list)
{
if (list.Where(x => x.ID == item.ID).Count() > 1)
throw new ShiftoriumConflictException(item.Id);
}
return list;
}
}
@ -460,4 +467,9 @@ namespace ShiftOS.Engine
}
}
public class ShiftoriumProviderAttribute : Attribute
{
}
}