From 27264a559d7fd40a0c2bc393a5e150c17da1539d Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 28 Jun 2017 16:33:07 -0400 Subject: Basics of the virus system --- ShiftOS_TheReturn/IVirus.cs | 29 +++++++++ ShiftOS_TheReturn/SaveSystem.cs | 2 +- ShiftOS_TheReturn/ShiftOS.Engine.csproj | 2 + ShiftOS_TheReturn/VirusManager.cs | 100 ++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 ShiftOS_TheReturn/IVirus.cs create mode 100644 ShiftOS_TheReturn/VirusManager.cs (limited to 'ShiftOS_TheReturn') diff --git a/ShiftOS_TheReturn/IVirus.cs b/ShiftOS_TheReturn/IVirus.cs new file mode 100644 index 0000000..0c06aea --- /dev/null +++ b/ShiftOS_TheReturn/IVirus.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Engine +{ + public interface IVirus + { + void Infect(int threatlevel); + void Disinfect(); + } + + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class VirusAttribute : Attribute + { + public VirusAttribute(string id, string name, string desc) + { + Name = name; + ID = id; + Description = desc; + } + + public string Name { get; set; } + public string Description { get; set; } + public string ID { get; set; } + } +} diff --git a/ShiftOS_TheReturn/SaveSystem.cs b/ShiftOS_TheReturn/SaveSystem.cs index b767605..e20bc08 100644 --- a/ShiftOS_TheReturn/SaveSystem.cs +++ b/ShiftOS_TheReturn/SaveSystem.cs @@ -191,7 +191,7 @@ namespace ShiftOS.Engine { // "No errors, this never gets called." Console.WriteLine("[inetd] SEVERE: " + ex.Message); - string dest = "Startup Exception " + DateTime.Now.ToString() + ".txt"; + string dest = "Startup Exception " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt"; System.IO.File.WriteAllText(dest, ex.ToString()); Console.WriteLine("[inetd] Full exception details have been saved to: " + dest); Thread.Sleep(3000); diff --git a/ShiftOS_TheReturn/ShiftOS.Engine.csproj b/ShiftOS_TheReturn/ShiftOS.Engine.csproj index 9bfa5f9..f1946ad 100644 --- a/ShiftOS_TheReturn/ShiftOS.Engine.csproj +++ b/ShiftOS_TheReturn/ShiftOS.Engine.csproj @@ -136,6 +136,7 @@ + @@ -165,6 +166,7 @@ + Infobox.cs diff --git a/ShiftOS_TheReturn/VirusManager.cs b/ShiftOS_TheReturn/VirusManager.cs new file mode 100644 index 0000000..74d0e80 --- /dev/null +++ b/ShiftOS_TheReturn/VirusManager.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; + +namespace ShiftOS.Engine +{ + public static class VirusManager + { + public static List ActiveInfections = new List(); + + public static void Init() + { + ActiveInfections = new List(); + if (SaveSystem.CurrentSave.ViralInfections == null) + SaveSystem.CurrentSave.ViralInfections = new List(); + foreach(var virusdata in SaveSystem.CurrentSave.ViralInfections) + { + var virus = CreateVirus(virusdata.ID, virusdata.ThreatLevel); + var existing = ActiveInfections.FirstOrDefault(x => x.GetType() == virus.GetType()); + if(existing != null) + { + var eIndex = ActiveInfections.IndexOf(existing); + ActiveInfections[eIndex] = virus; + existing.Disinfect(); + } + else + { + ActiveInfections.Add(virus); + } + } + } + + public static void Infect(string id, int threatlevel) + { + if (threatlevel < 1) + throw new Exception("Threat level can't be below 1."); + if (threatlevel > 4) + throw new Exception("Threat level can't be above 4."); + + var infection = SaveSystem.CurrentSave.ViralInfections.FirstOrDefault(x => x.ID == id); + if (infection != null) + { + if(infection.ThreatLevel < threatlevel) + { + infection.ThreatLevel = threatlevel; + } + } + else + { + SaveSystem.CurrentSave.ViralInfections.Add(new ViralInfection + { + ID = id, + ThreatLevel = threatlevel + }); + } + var virus = CreateVirus(id, threatlevel); + var existing = ActiveInfections.FirstOrDefault(x => x.GetType() == virus.GetType()); + if(existing != null) + { + var eIndex = ActiveInfections.IndexOf(existing); + ActiveInfections[eIndex] = virus; + existing.Disinfect(); + } + else + { + ActiveInfections.Add(virus); + } + } + + internal static IVirus CreateVirus(string id, int threatlevel) + { + if (threatlevel < 1) + throw new Exception("Threat level can't be below 1."); + if (threatlevel > 4) + throw new Exception("Threat level can't be above 4."); + + foreach(var type in ReflectMan.Types.Where(x => x.GetInterfaces().Contains(typeof(IVirus)) && Shiftorium.UpgradeAttributesUnlocked(x))) + { + var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is VirusAttribute) as VirusAttribute; + if(attrib != null) + { + if(attrib.ID == id) + { + IVirus virus = (IVirus)Activator.CreateInstance(type); + virus.Infect(threatlevel); + return virus; + } + } + } + { + + } + + throw new Exception("Cannot create virus."); + } + } +} -- cgit v1.2.3