diff options
| author | Michael <[email protected]> | 2017-06-28 16:33:07 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-06-28 16:33:07 -0400 |
| commit | 27264a559d7fd40a0c2bc393a5e150c17da1539d (patch) | |
| tree | ed02811fbd109b1637cd9b6a818af3eb4ce46e59 | |
| parent | 6cdb8cb0253230f6f845afb549517c5c3f12d2d1 (diff) | |
| download | shiftos_thereturn-27264a559d7fd40a0c2bc393a5e150c17da1539d.tar.gz shiftos_thereturn-27264a559d7fd40a0c2bc393a5e150c17da1539d.tar.bz2 shiftos_thereturn-27264a559d7fd40a0c2bc393a5e150c17da1539d.zip | |
Basics of the virus system
| -rw-r--r-- | ShiftOS.Objects/Save.cs | 9 | ||||
| -rw-r--r-- | ShiftOS.WinForms/ShiftOS.WinForms.csproj | 2 | ||||
| -rw-r--r-- | ShiftOS.WinForms/VirusTestCommands.cs | 25 | ||||
| -rw-r--r-- | ShiftOS.WinForms/Viruses/WindowsEverywhere.cs | 83 | ||||
| -rw-r--r-- | ShiftOS.WinForms/WinformsDesktop.cs | 1 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/IVirus.cs | 29 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/SaveSystem.cs | 2 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/ShiftOS.Engine.csproj | 2 | ||||
| -rw-r--r-- | ShiftOS_TheReturn/VirusManager.cs | 100 |
9 files changed, 252 insertions, 1 deletions
diff --git a/ShiftOS.Objects/Save.cs b/ShiftOS.Objects/Save.cs index 7323028..c6a7f61 100644 --- a/ShiftOS.Objects/Save.cs +++ b/ShiftOS.Objects/Save.cs @@ -33,6 +33,8 @@ namespace ShiftOS.Objects //Better to store this stuff server-side so we can do some neat stuff with hacking... public class Save { + public List<ViralInfection> ViralInfections { get; set; } + public bool MusicEnabled = true; public bool SoundEnabled = true; public int MusicVolume = 100; @@ -159,4 +161,11 @@ namespace ShiftOS.Objects return true; } } + + public class ViralInfection + { + public string ID { get; set; } + public int ThreatLevel { get; set; } + } + } diff --git a/ShiftOS.WinForms/ShiftOS.WinForms.csproj b/ShiftOS.WinForms/ShiftOS.WinForms.csproj index 901da41..f1c92da 100644 --- a/ShiftOS.WinForms/ShiftOS.WinForms.csproj +++ b/ShiftOS.WinForms/ShiftOS.WinForms.csproj @@ -463,6 +463,8 @@ <DependentUpon>UniteSignupDialog.cs</DependentUpon> </Compile> <Compile Include="VirtualEnvironments.cs" /> + <Compile Include="Viruses\WindowsEverywhere.cs" /> + <Compile Include="VirusTestCommands.cs" /> <Compile Include="VisualBasicStuff.cs" /> <Compile Include="WFLanguageProvider.cs" /> <Compile Include="WidgetManager.cs" /> diff --git a/ShiftOS.WinForms/VirusTestCommands.cs b/ShiftOS.WinForms/VirusTestCommands.cs new file mode 100644 index 0000000..09f1a36 --- /dev/null +++ b/ShiftOS.WinForms/VirusTestCommands.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Engine; + +namespace ShiftOS.WinForms +{ +#if DEBUG + public static class VirusTestCommands + { + [Command("infect", description = "DEBUG: Infect the system with a virus.")] + [RequiresArgument("id")] + [RequiresArgument("threatlevel")] + public static void Infect(Dictionary<string, object> args) + { + var id = args["id"].ToString(); + var threatlevel = Convert.ToInt32(args["threatlevel"].ToString()); + + VirusManager.Infect(id, threatlevel); + } + } +#endif +}
\ No newline at end of file diff --git a/ShiftOS.WinForms/Viruses/WindowsEverywhere.cs b/ShiftOS.WinForms/Viruses/WindowsEverywhere.cs new file mode 100644 index 0000000..df73d58 --- /dev/null +++ b/ShiftOS.WinForms/Viruses/WindowsEverywhere.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Engine; + +namespace ShiftOS.WinForms.Viruses +{ + [Virus("windows_everywhere", "Windows Everywhere", "Makes the windows dance around the screen if the user has the WM Free Placement upgrade. Speed depends on threatlevel.")] + [RequiresUpgrade("wm_free_placement")] + public class WindowsEverywhere : IVirus + { + private System.Windows.Forms.Timer timer = null; + private int ThreatLevel = 1; + private Dictionary<string, System.Drawing.Point> Velocities = null; + + + public void Infect(int threatlevel) + { + Velocities = new Dictionary<string, System.Drawing.Point>(); + ThreatLevel = threatlevel; + timer = new System.Windows.Forms.Timer(); + timer.Interval = 50; + timer.Tick += (o, a) => + { + foreach (var win in AppearanceManager.OpenForms) + { + var border = (win as WindowBorder); + var loc = border.Location; + var velocity = new System.Drawing.Point(1, 1); + string vKey = border.GetType().Name + "_" + border.GetHashCode(); + if (!Velocities.ContainsKey(vKey)) + { + Velocities.Add(vKey, velocity); + } + else + { + velocity = Velocities[vKey]; + } + + //Calculate proper velocity. + if (border.Top <= 0) + { + velocity.Y = 1; + } + if (border.Top + border.Height >= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) + { + velocity.Y = -1; + } + if (border.Left <= 0) + { + velocity.X = 1; + } + if (border.Left + border.Width >= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) + { + velocity.X = -1; + } + //save velocity in memory. + Velocities[vKey] = velocity; + //convert using threatlevel + + var velocityX = velocity.X * (threatlevel * 4); + var velocityY = velocity.Y * (threatlevel * 4); + + loc.X += velocityX; + loc.Y += velocityY; + + border.Location = loc; + } + }; + timer.Start(); + } + + public void Disinfect() + { + timer.Stop(); + Velocities.Clear(); + ThreatLevel = 0; + timer = null; + } + } +} diff --git a/ShiftOS.WinForms/WinformsDesktop.cs b/ShiftOS.WinForms/WinformsDesktop.cs index 7508262..d60b6f6 100644 --- a/ShiftOS.WinForms/WinformsDesktop.cs +++ b/ShiftOS.WinForms/WinformsDesktop.cs @@ -226,6 +226,7 @@ namespace ShiftOS.WinForms SaveSystem.GameReady += () => { + VirusManager.Init(); this.Invoke(new Action(LoadIcons)); if (this.Visible == true) this.Invoke(new Action(() => SetupDesktop())); 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 @@ <Compile Include="Infobox.cs" /> <Compile Include="IShiftOSWindow.cs" /> <Compile Include="IStatusIcon.cs" /> + <Compile Include="IVirus.cs" /> <Compile Include="KernelWatchdog.cs" /> <Compile Include="Localization.cs" /> <Compile Include="LoginManager.cs" /> @@ -165,6 +166,7 @@ <Compile Include="TerminalTextWriter.cs" /> <Compile Include="TutorialManager.cs" /> <Compile Include="UserManagementCommands.cs" /> + <Compile Include="VirusManager.cs" /> <Compile Include="WinOpenAttribute.cs" /> <EmbeddedResource Include="Infobox.resx"> <DependentUpon>Infobox.cs</DependentUpon> 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<IVirus> ActiveInfections = new List<IVirus>(); + + public static void Init() + { + ActiveInfections = new List<IVirus>(); + if (SaveSystem.CurrentSave.ViralInfections == null) + SaveSystem.CurrentSave.ViralInfections = new List<ViralInfection>(); + 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."); + } + } +} |
