Basics of the virus system

This commit is contained in:
Michael 2017-06-28 16:33:07 -04:00
parent 6cdb8cb025
commit 27264a559d
9 changed files with 252 additions and 1 deletions

View file

@ -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; }
}
}

View file

@ -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" />

View file

@ -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
}

View file

@ -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;
}
}
}

View file

@ -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()));

View file

@ -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; }
}
}

View file

@ -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);

View file

@ -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>

View file

@ -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.");
}
}
}