aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS_TheReturn/Shiftorium.cs
diff options
context:
space:
mode:
authorwilliam341 <[email protected]>2017-05-28 12:37:00 -0700
committerGitHub <[email protected]>2017-05-28 12:37:00 -0700
commit771c20cfb3a703e0f1550fdcf9eb07b78298c944 (patch)
tree59cb532e15ebff313fdba2be264d78ec0033f407 /ShiftOS_TheReturn/Shiftorium.cs
parent496b0cbf8659c99203f48210fd39c572400ae623 (diff)
parentc7ba7d733c756d196f98dd4533289a1ef4db715f (diff)
downloadshiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.gz
shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.bz2
shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.zip
Merge pull request #1 from shiftos-game/master
welp, no longer a dev.
Diffstat (limited to 'ShiftOS_TheReturn/Shiftorium.cs')
-rw-r--r--ShiftOS_TheReturn/Shiftorium.cs355
1 files changed, 327 insertions, 28 deletions
diff --git a/ShiftOS_TheReturn/Shiftorium.cs b/ShiftOS_TheReturn/Shiftorium.cs
index e6fc299..bd7105f 100644
--- a/ShiftOS_TheReturn/Shiftorium.cs
+++ b/ShiftOS_TheReturn/Shiftorium.cs
@@ -34,6 +34,9 @@ using System.Diagnostics;
namespace ShiftOS.Engine
{
+ /// <summary>
+ /// Backend class for the Shiftorium.
+ /// </summary>
public static class Shiftorium
{
/// <summary>
@@ -41,15 +44,77 @@ namespace ShiftOS.Engine
/// </summary>
public static bool Silent = false;
+ /// <summary>
+ /// Gets all Shiftorium categories.
+ /// </summary>
+ /// <param name="onlyAvailable">Should we look in the "available" upgrade list (i.e, what the user can buy right now), or the full upgrade list?</param>
+ /// <returns>All Shiftorium categories from the list, in a <see cref="System.String[]"/>. </returns>
+ public static string[] GetCategories(bool onlyAvailable = true)
+ {
+ List<string> cats = new List<string>();
+ IEnumerable<ShiftoriumUpgrade> upgrades = GetDefaults();
+ if (onlyAvailable)
+ upgrades = new List<ShiftoriumUpgrade>(GetAvailable());
+
+ foreach (var upg in upgrades)
+ {
+ if (!cats.Contains(upg.Category))
+ cats.Add(upg.Category);
+ }
+
+ return cats.ToArray();
+ }
+
+ /// <summary>
+ /// Causes the engine to alert the frontend of a new Shiftorium upgrade install.
+ /// </summary>
public static void InvokeUpgradeInstalled()
{
Installed?.Invoke();
}
+ /// <summary>
+ /// Gets the category of an upgrade.
+ /// </summary>
+ /// <param name="id">The upgrade ID to check</param>
+ /// <returns>"Other" if the upgrade is not found, else, the upgrade category.</returns>
+ public static string GetCategory(string id)
+ {
+ var upg = GetDefaults().FirstOrDefault(x => x.ID == id);
+ if (upg == null)
+ return "Other";
+ return (upg.Category == null) ? "Other" : upg.Category;
+ }
+
+ /// <summary>
+ /// Gets all upgrades in a given category.
+ /// </summary>
+ /// <param name="cat">The category name to search</param>
+ /// <returns>The upgrades in the category.</returns>
+ public static IEnumerable<ShiftoriumUpgrade> GetAllInCategory(string cat)
+ {
+ return GetDefaults().Where(x => x.Category == cat);
+ }
- public static bool Buy(string id, int cost)
+ /// <summary>
+ /// Gets whether or not the user has installed all upgrades in a category.
+ /// </summary>
+ /// <param name="cat">The category to search.</param>
+ /// <returns>Boolean value representing whether the user has installed all upgrades in the category.</returns>
+ public static bool IsCategoryEmptied(string cat)
{
- if(SaveSystem.CurrentSave.Codepoints >= cost)
+ return GetDefaults().Where(x => x.Category == cat).FirstOrDefault(x => x.Installed == false) == null;
+ }
+
+ /// <summary>
+ /// Buy an upgrade, deducting the specified amount of Codepoints.
+ /// </summary>
+ /// <param name="id">The upgrade ID to buy</param>
+ /// <param name="cost">The amount of Codepoints to deduct</param>
+ /// <returns>True if the upgrade was installed successfully, false if the user didn't have enough Codepoints or the upgrade wasn' found.</returns>
+ public static bool Buy(string id, long cost)
+ {
+ if (SaveSystem.CurrentSave.Codepoints >= cost)
{
SaveSystem.CurrentSave.Upgrades[id] = true;
TerminalBackend.InvokeCommand("sos.save");
@@ -61,17 +126,22 @@ namespace ShiftOS.Engine
}
else
{
- if(!Silent)
+ if (!Silent)
Console.WriteLine($"{{SHIFTORIUM_NOTENOUGHCP}}: {cost} > {SaveSystem.CurrentSave.Codepoints}");
return false;
}
}
+ /// <summary>
+ /// Determines whether all Shiftorium upgrade attributes for this type have been installed.
+ /// </summary>
+ /// <param name="type">The type to scan</param>
+ /// <returns>Boolean value representing the result of this function.</returns>
public static bool UpgradeAttributesUnlocked(Type type)
{
- foreach(var attr in type.GetCustomAttributes(true))
+ foreach (var attr in type.GetCustomAttributes(true))
{
- if(attr is RequiresUpgradeAttribute)
+ if (attr is RequiresUpgradeAttribute)
{
var rAttr = attr as RequiresUpgradeAttribute;
return rAttr.Installed;
@@ -81,6 +151,11 @@ namespace ShiftOS.Engine
return true;
}
+ /// <summary>
+ /// Determines whether all Shiftorium upgrade attributes for this method have been installed.
+ /// </summary>
+ /// <param name="type">The method to scan</param>
+ /// <returns>Boolean value representing the result of this function.</returns>
public static bool UpgradeAttributesUnlocked(MethodInfo type)
{
foreach (var attr in type.GetCustomAttributes(true))
@@ -95,6 +170,11 @@ namespace ShiftOS.Engine
return true;
}
+ /// <summary>
+ /// Determines whether all Shiftorium upgrade attributes for this property have been installed.
+ /// </summary>
+ /// <param name="type">The property to scan</param>
+ /// <returns>Boolean value representing the result of this function.</returns>
public static bool UpgradeAttributesUnlocked(PropertyInfo type)
{
foreach (var attr in type.GetCustomAttributes(true))
@@ -109,6 +189,11 @@ namespace ShiftOS.Engine
return true;
}
+ /// <summary>
+ /// Determines whether all Shiftorium upgrade attributes for this field have been installed.
+ /// </summary>
+ /// <param name="type">The field to scan</param>
+ /// <returns>Boolean value representing the result of this function.</returns>
public static bool UpgradeAttributesUnlocked(FieldInfo type)
{
foreach (var attr in type.GetCustomAttributes(true))
@@ -123,15 +208,140 @@ namespace ShiftOS.Engine
return true;
}
+ private static List<ShiftoriumUpgrade> upgDb = null;
+
+ public static void CreateUpgradeDatabase()
+ {
+ upgDb = new List<ShiftoriumUpgrade>();
+ //Now we probe for ShiftoriumUpgradeAttributes for mods.
+ foreach (var file in System.IO.Directory.GetFiles(Environment.CurrentDirectory))
+ {
+ if (file.EndsWith(".exe") || file.EndsWith(".dll"))
+ {
+ try
+ {
+ 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;
+ upgDb.AddRange(_p.GetDefaults());
+ }
+ }
+
+
+ ShiftoriumUpgradeAttribute attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
+ if (attrib != null)
+ {
+ if (upgDb.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
+ throw new ShiftoriumConflictException(attrib.Upgrade);
+ upgDb.Add(new ShiftoriumUpgrade
+ {
+ Id = attrib.Upgrade,
+ Name = attrib.Name,
+ Cost = attrib.Cost,
+ Description = attrib.Description,
+ Dependencies = attrib.Dependencies,
+ Category = attrib.Category
+ });
+ }
+
+ foreach (var mth in type.GetMethods())
+ {
+ attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
+ if (attrib != null)
+ {
+ if (upgDb.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
+ throw new ShiftoriumConflictException(attrib.Upgrade);
+ upgDb.Add(new ShiftoriumUpgrade
+ {
+ Id = attrib.Upgrade,
+ Name = attrib.Name,
+ Cost = attrib.Cost,
+ Description = attrib.Description,
+ Dependencies = attrib.Dependencies,
+ Category = attrib.Category
+ });
+
+ }
+ }
+
+ foreach (var mth in type.GetFields())
+ {
+ attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
+ if (attrib != null)
+ {
+ if (upgDb.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
+ throw new ShiftoriumConflictException(attrib.Upgrade);
+ upgDb.Add(new ShiftoriumUpgrade
+ {
+ Id = attrib.Upgrade,
+ Name = attrib.Name,
+ Cost = attrib.Cost,
+ Description = attrib.Description,
+ Dependencies = attrib.Dependencies,
+ Category = attrib.Category
+ });
+
+ }
+ }
+
+ foreach (var mth in type.GetProperties())
+ {
+ attrib = mth.GetCustomAttributes(false).FirstOrDefault(x => x is ShiftoriumUpgradeAttribute) as ShiftoriumUpgradeAttribute;
+ if (attrib != null)
+ {
+ if (upgDb.FirstOrDefault(x => x.ID == attrib.Upgrade) != null)
+ throw new ShiftoriumConflictException(attrib.Upgrade);
+ upgDb.Add(new ShiftoriumUpgrade
+ {
+ Id = attrib.Upgrade,
+ Name = attrib.Name,
+ Cost = attrib.Cost,
+ Description = attrib.Description,
+ Dependencies = attrib.Dependencies,
+ Category = attrib.Category
+ });
+
+ }
+ }
+
+ }
+ }
+ catch { }
+ }
+ }
+
+
+
+ foreach (var item in upgDb)
+ {
+ if (upgDb.Where(x => x.ID == item.ID).Count() > 1)
+ throw new ShiftoriumConflictException(item.Id);
+ }
+ }
+
+
+ /// <summary>
+ /// Gets or sets whether the Shiftorium has been initiated.
+ /// </summary>
public static bool IsInitiated { get; private set; }
+
+ /// <summary>
+ /// Initiates the Shiftorium.
+ /// </summary>
public static void Init()
{
if (IsInitiated == false)
{
IsInitiated = true;
//Let the crash handler deal with this one...
- var dict = GetDefaults();
+ CreateUpgradeDatabase();
+ var dict = upgDb;
foreach (var itm in dict)
{
if (!SaveSystem.CurrentSave.Upgrades.ContainsKey(itm.ID))
@@ -150,9 +360,14 @@ namespace ShiftOS.Engine
}
- public static int GetCPValue(string id)
+ /// <summary>
+ /// Get the codepoint value for an upgrade.
+ /// </summary>
+ /// <param name="id">The upgrade ID to search</param>
+ /// <returns>The codepoint value.</returns>
+ public static long GetCPValue(string id)
{
- foreach(var upg in GetDefaults())
+ foreach (var upg in GetDefaults())
{
if (upg.ID == id)
return upg.Cost;
@@ -160,10 +375,14 @@ namespace ShiftOS.Engine
return 0;
}
+ /// <summary>
+ /// Gets all available upgrades.
+ /// </summary>
+ /// <returns></returns>
public static ShiftoriumUpgrade[] GetAvailable()
{
List<ShiftoriumUpgrade> available = new List<ShiftoriumUpgrade>();
- foreach(var defaultupg in GetDefaults())
+ foreach (var defaultupg in GetDefaults())
{
if (!UpgradeInstalled(defaultupg.ID) && DependenciesInstalled(defaultupg))
available.Add(defaultupg);
@@ -171,6 +390,11 @@ namespace ShiftOS.Engine
return available.ToArray();
}
+ /// <summary>
+ /// Determines whether all dependencies of a given upgrade have been installed.
+ /// </summary>
+ /// <param name="upg">The upgrade to scan</param>
+ /// <returns>Boolean representing the result of this function.</returns>
public static bool DependenciesInstalled(ShiftoriumUpgrade upg)
{
if (string.IsNullOrEmpty(upg.Dependencies))
@@ -180,23 +404,33 @@ namespace ShiftOS.Engine
else if (upg.Dependencies.Contains(";"))
{
string[] dependencies = upg.Dependencies.Split(';');
- foreach(var dependency in dependencies)
+ foreach (var dependency in dependencies)
{
if (!UpgradeInstalled(dependency))
return false;
}
return true;
- }
+ }
else
{
return UpgradeInstalled(upg.Dependencies);
}
}
+ /// <summary>
+ /// Fired when an upgrade is installed.
+ /// </summary>
public static event EmptyEventHandler Installed;
+ /// <summary>
+ /// Determines if an upgrade is installed.
+ /// </summary>
+ /// <param name="id">The upgrade ID to scan.</param>
+ /// <returns>Whether the upgrade is installed.</returns>
public static bool UpgradeInstalled(string id)
{
+ if (string.IsNullOrWhiteSpace(id))
+ return true;
if (SaveSystem.CurrentSave != null)
{
if (!IsInitiated)
@@ -204,13 +438,35 @@ namespace ShiftOS.Engine
}
try
{
- return SaveSystem.CurrentSave.Upgrades[id];
+ if (SaveSystem.CurrentSave == null)
+ return false;
+
+ if (SaveSystem.CurrentSave.StoriesExperienced == null)
+ SaveSystem.CurrentSave.StoriesExperienced = new List<string>();
+
+ if (id.Contains(';'))
+ {
+ foreach (var u in id.Split(';'))
+ {
+ if (UpgradeInstalled(u) == false)
+ return false;
+ }
+ return true;
+ }
+
+ bool upgInstalled = false;
+ if (SaveSystem.CurrentSave.Upgrades.ContainsKey(id))
+ upgInstalled = SaveSystem.CurrentSave.Upgrades[id];
+
+ if (upgInstalled == false)
+ return SaveSystem.CurrentSave.StoriesExperienced.Contains(id);
+ return true;
}
catch
{
- return false;
Console.WriteLine("Upgrade " + id + "DNE.");
Console.WriteLine();
+ return false;
}
}
@@ -220,30 +476,28 @@ 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;
}
- //Bless the newer NEWER engine.
+ /// <summary>
+ /// Gets every upgrade inside the frontend and all mods.
+ /// </summary>
+ /// <returns>Every single found Shiftorium upgrade.</returns>
public static List<ShiftoriumUpgrade> GetDefaults()
{
- try
- {
- return _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);
- return JsonConvert.DeserializeObject<List<ShiftoriumUpgrade>>(Properties.Resources.Shiftorium);
- }
+ return upgDb;
}
}
public interface IShiftoriumProvider
{
+ /// <summary>
+ /// Retrieves all frontend upgrades.
+ /// </summary>
+ /// <returns></returns>
List<ShiftoriumUpgrade> GetDefaults();
}
@@ -260,14 +514,59 @@ namespace ShiftOS.Engine
public string ID { get; private set; }
}
+
+
public class ShiftoriumUpgrade
{
public string Name { get; set; }
public string Description { get; set; }
- public int Cost { get; set; }
+ public long Cost { get; set; }
public string ID { get { return (this.Id != null ? this.Id : (Name.ToLower().Replace(" ", "_"))); } }
- public string Id { get; }
-
+ public string Id { get; set; }
+ public string Category { get; set; }
+ public bool Installed
+ {
+ get
+ {
+ return Shiftorium.UpgradeInstalled(ID);
+ }
+ }
public string Dependencies { get; set; }
}
+
+ public class ShiftoriumUpgradeAttribute : RequiresUpgradeAttribute
+ {
+ public ShiftoriumUpgradeAttribute(string name, long cost, string desc, string dependencies, string category) : base(name.ToLower().Replace(" ", "_"))
+ {
+ Name = name;
+ Description = desc;
+ Dependencies = dependencies;
+ Cost = cost;
+ Category = category;
+ }
+
+ public string Name { get; private set; }
+ public string Description { get; private set; }
+ public long Cost { get; private set; }
+ public string Dependencies { get; private set; }
+ public string Category { get; private set; }
+ }
+
+ public class ShiftoriumConflictException : Exception
+ {
+ public ShiftoriumConflictException() : base("An upgrade conflict has occurred while loading Shiftorium Upgrades from an assembly. Is there a duplicate upgrade ID?")
+ {
+
+ }
+
+ public ShiftoriumConflictException(string id) : base("An upgrade conflict has occurred while loading Shiftorium Upgrades from an assembly. An upgrade with the ID \"" + id + "\" has already been loaded.")
+ {
+
+ }
+ }
+
+ public class ShiftoriumProviderAttribute : Attribute
+ {
+
+ }
}