aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS_TheReturn/VirusManager.cs
blob: 56767e61db9be15500331984140dc90a70ff490b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()
        {
            Desktop.InvokeOnWorkerThread(() =>
            {
                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.");
        }
    }
}