blob: b5c50b70b428ce096c04d629fdc39a6ec867ea5c (
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ShiftOS.Objects;
namespace ShiftOS.Engine
{
public static class VirusManager
{
public static List<IVirus> ActiveInfections = new List<IVirus>();
public static void Init()
{
Desktop.InvokeOnWorkerThread(() =>
{
ShiftOS.Objects.ShiftFS.Utils.FileRead += (path) =>
{
Desktop.InvokeOnWorkerThread(() =>
{
var headerData = Objects.ShiftFS.Utils.GetHeaderText(path);
if(headerData != null)
{
try
{
var viruses = JsonConvert.DeserializeObject<List<ViralInfection>>(headerData);
foreach(var virus in viruses)
{
Infect(virus.ID, virus.ThreatLevel);
}
}
catch { }
}
});
};
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
{
return;
//no need to reinfect with a lower 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.");
}
public static void Disinfect(string id)
{
foreach(var virus in ActiveInfections.ToArray())
{
var type = virus.GetType();
var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is VirusAttribute) as VirusAttribute;
if(attrib != null)
{
if (attrib.ID == id)
{
ActiveInfections.Remove(virus);
var inf = SaveSystem.CurrentSave.ViralInfections.FirstOrDefault(x => x.ID == id);
if (inf != null)
SaveSystem.CurrentSave.ViralInfections.Remove(inf);
virus.Disinfect();
}
}
}
}
}
}
|