aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS_TheReturn/Hacking.cs
blob: ea2d89b1cb8c57076c16a074d765c0cc84531005 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShiftOS.Engine
{
    public static class Hacking
    {
        private static List<HackableSystem> _activeConnections = new List<HackableSystem>();
        private static List<Objects.Hackable> Hackables = new List<Objects.Hackable>();
        private static List<Objects.Loot> Loot = new List<Objects.Loot>();

        public static HackableSystem CurrentHackable { get; private set; } 

        public static Objects.Hackable[] AvailableToHack
        {
            get
            {
                return Hackables.Where(x => Shiftorium.UpgradeInstalled(x.Dependencies) && !Shiftorium.UpgradeInstalled(x.ID)).ToArray();
            }
        }


        public static HackableSystem[] ActiveConnections
        {
            get
            {
                return _activeConnections.ToArray();
            }
        }

        public static HackableSystem[] PwnedConnections
        {
            get
            {
                return _activeConnections.Where(x => x.IsPwn3d).ToArray();
            }
        }

        public static HackableSystem[] TimedConnections
        {
            get
            {
                return _activeConnections.Where(x => x.Data.ConnectionTimeoutLevel > 0&&!x.IsPwn3d).ToArray();
            }
        }

        public static void InitHack(Objects.Hackable data)
        {
            var hsys = new HackableSystem();
            hsys.Data = data;
            hsys.IsPwn3d = false;
            var fs = new Objects.ShiftFS.Directory();
            fs.Name = data.FriendlyName;
            Objects.ShiftFS.Utils.Mounts.Add(fs);
            var mountid = Objects.ShiftFS.Utils.Mounts.IndexOf(fs);
            Objects.ShiftFS.Utils.Mounts.Remove(fs);
            hsys.Filesystem = fs;
            hsys.FirewallCracked = (data.FirewallStrength == 0);
            hsys.DoConnectionTimeout = (data.ConnectionTimeoutLevel > 0);
            if (hsys.DoConnectionTimeout)
            {
                hsys.MillisecondsCountdown = 1000 * (240 / data.ConnectionTimeoutLevel);
            }
            else
            {
                hsys.MillisecondsCountdown = 0;
            }
            hsys.PortsToUnlock = new List<Port>();
            if (data.SystemType.HasFlag(Objects.SystemType.EmailServer))
                hsys.PortsToUnlock.Add(new Port
                {
                    Value = 25,
                    Name = "SMTP mailserver (unencrypted)",
                });
            if (data.SystemType.HasFlag(Objects.SystemType.FileServer))
                hsys.PortsToUnlock.Add(new Port
                {
                    Value = 22,
                    Name = "File Transfer Protocol",
                });
            if (data.SystemType.HasFlag(Objects.SystemType.SSHServer))
                hsys.PortsToUnlock.Add(new Port
                {
                    Value = 21,
                    Name = "ShiftSSH server",
                });
            if (data.SystemType.HasFlag(Objects.SystemType.Database))
                hsys.PortsToUnlock.Add(new Port
                {
                    Value = 3306,
                    Name = "MySQL database",
                });

            CurrentHackable = hsys;
        }

        public static void FailHack()
        {
            if (CurrentHackable == null)
                throw new NaughtyDeveloperException("Someone tried to fail a non-existent hack.");
            if (CurrentHackable.IsPwn3d)
                throw new NaughtyDeveloperException("A developer tried to un-pwn a pwn3d hackable.");
            if (!string.IsNullOrWhiteSpace(CurrentHackable.Data.OnHackFailedStoryEvent))
                Story.Start(CurrentHackable.Data.OnHackFailedStoryEvent);
            if (Objects.ShiftFS.Utils.Mounts.Contains(CurrentHackable.Filesystem))
                Objects.ShiftFS.Utils.Mounts.Remove(CurrentHackable.Filesystem);
            CurrentHackable = null;
        }

        public static void Initiate()
        {
            foreach(var type in ReflectMan.Types.Where(x => x.GetInterfaces().Contains(typeof(IHackableProvider))))
            {
                var @interface = (IHackableProvider)Activator.CreateInstance(type, null);
                Hackables.AddRange(@interface.GetHackables());
                var lootinfo = @interface.GetLootInfo();
                foreach(var loot in lootinfo)
                {
                    var existing = Loot.FirstOrDefault(x => x.Info.Filename == loot.Filename);
                    if (existing != null)
                        throw new DataConflictException("Data conflict encountered while reading loot data. Two or more loot resources with the filename \"" + loot.Filename + "\" were found. This can cause major bugs and confusion in the game.");
                    var @new = new Objects.Loot(loot, @interface.GetLootFromResource(loot.ResourceId));
                    Loot.Add(@new);
                }
            }

            var hackable = Hackables.FirstOrDefault(x => Hackables.Where(y => x.SystemName == y.SystemName).Count() > 1);
            if(hackable != null)
            {
                throw new DataConflictException("Data conflict encountered while initiating the hacking engine. Two or more hackables were found with the same hostname \"" + hackable.SystemName + "\". This is a direct violation of the ShiftOS save system and Shiftorium backend.");
            }
        }
    }

    /// <summary>
    /// An exception which is thrown when a developer deliberately tries to cause a bug.
    /// </summary>
    public class NaughtyDeveloperException : Exception
    {
        /// <summary>
        /// Create a new instance of the <see cref="NaughtyDeveloperException"/>, with the specified message, which will cause Visual Studio to call the person who caused the exception a scrotem. 
        /// </summary>
        /// <param name="message">The message you want to yell at the user.</param>
        public NaughtyDeveloperException(string message) : base(message + " - FIX IT, YOU SCROTEM")
        {

        }
    }

    public class DataConflictException : Exception
    {
        public DataConflictException(string message) : base(message)
        {

        }
    }

    public interface IHackableProvider
    {
        Objects.Hackable[] GetHackables();
        Objects.LootInfo[] GetLootInfo();
        byte[] GetLootFromResource(string resId);
    }

    public class HackableSystem
    {
        public Objects.Hackable Data { get; set; }
        public List<Port> PortsToUnlock { get; set; }
        public bool FirewallCracked { get; set; }
        public Objects.ShiftFS.Directory Filesystem { get; set; }
        public double MillisecondsCountdown { get; set; }
        public bool DoConnectionTimeout { get; set; }
        public bool IsPwn3d { get; set; }
    }

    public class Port
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
}