OpenHacknet/MissionGenerator.cs
2015-10-27 23:04:18 -04:00

153 lines
No EOL
6.5 KiB
C#

using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content;
namespace Hacknet
{
public static class MissionGenerator
{
private const int FILE_DELETION = 0;
private const int WEBSITE_CHANGE = 1;
private const int TYPES_OF_GENERATED_MISSIONS = 2;
public static int generationCount;
private static bool customKeysWereSet;
private static string customFileData;
private static ContentManager content;
private static List<List<string>> MissionLists;
public static void init(ContentManager contentManager)
{
content = contentManager;
MissionLists = new List<List<string>>();
for (var index = 0; index < 2; ++index)
{
string str;
switch (index)
{
case 0:
str = "\\Missions\\Generated\\FileDeletion\\";
break;
case 1:
str = "\\Missions\\Generated\\WebsiteChange\\";
break;
default:
str = null;
break;
}
MissionLists.Add(new List<string>());
foreach (FileSystemInfo fileSystemInfo in new DirectoryInfo(content.RootDirectory + str).GetFiles("*.*")
)
MissionLists[index].Add("Content\\" + str + Path.GetFileName(fileSystemInfo.Name));
}
}
public static object generate(int secutiryLevel)
{
var type = 0;
if (secutiryLevel <= 3)
{
type = 0;
}
else
{
type = 0;
if (Utils.flipCoin())
type = 1;
}
var index = (int) ((MissionLists[type].Count - 1)*Utils.random.NextDouble() + 0.5);
var filename = MissionLists[type][index];
customKeysWereSet = false;
MissionGenerationParser.Client = NameGenerator.generateName();
MissionGenerationParser.Other = "Unknown";
MissionGenerationParser.Target = NameGenerator.generateName();
ComputerLoader.MissionPreLoadComplete += () =>
{
var c = (Computer) generateComputer(secutiryLevel, MissionGenerationParser.Target);
Computer computer;
switch (type)
{
case 1:
computer = addWebsiteChangeRequirements(c, OS.currentInstance);
break;
default:
computer = addFileDeletionRequirements(c, OS.currentInstance);
break;
}
computer.idName = "Gen" + generationCount;
MissionGenerationParser.Comp = computer.idName;
OS.currentInstance.netMap.nodes.Add(computer);
};
var activeMission = (ActiveMission) ComputerLoader.readMission(filename);
ComputerLoader.MissionPreLoadComplete = null;
activeMission.wasAutoGenerated = true;
activeMission.genTarget = MissionGenerationParser.Comp;
activeMission.genPath = MissionGenerationParser.Path;
activeMission.genFile = MissionGenerationParser.File;
activeMission.client = MissionGenerationParser.Client;
activeMission.genTargetName = MissionGenerationParser.Target;
activeMission.genOther = MissionGenerationParser.Other;
++generationCount;
return activeMission;
}
public static object generateComputer(int secLevel, string name = null)
{
if (name == null)
name = NameGenerator.generateName();
var computer = new Computer(name, NetworkMap.generateRandomIP(),
OS.currentInstance.netMap.getRandomPosition(), secLevel, 2, OS.currentInstance);
if (computer.traceTime <= 0.0)
computer.traceTime = 10f*Computer.BASE_TRACE_TIME;
return computer;
}
public static void setMissionGenerationKeys(Dictionary<string, string> keys)
{
MissionGenerationParser.File = !keys.ContainsKey("File")
? null
: MissionGenerationParser.parse(keys["File"]);
customFileData = !keys.ContainsKey("Data") ? null : MissionGenerationParser.parse(keys["Data"]);
MissionGenerationParser.Path = !keys.ContainsKey("Path")
? null
: MissionGenerationParser.parse(keys["Path"]);
if (keys.ContainsKey("Client"))
MissionGenerationParser.Client = keys["Client"];
if (keys.ContainsKey("Comp"))
MissionGenerationParser.Comp = keys["Comp"];
customKeysWereSet = true;
}
private static Computer addFileDeletionRequirements(Computer c, OS os)
{
var nameEntry = "Archive_Data.zip";
if (customKeysWereSet && MissionGenerationParser.File != null)
nameEntry = MissionGenerationParser.File;
var fileEntry = new FileEntry(
customFileData != null ? customFileData : Computer.generateBinaryString(2000), nameEntry);
var folder = c.files.root.folders[0];
if (customKeysWereSet && MissionGenerationParser.Path != null)
folder = c.getFolderFromPath(MissionGenerationParser.Path, true);
folder.files.Add(fileEntry);
MissionGenerationParser.File = nameEntry;
if (!customKeysWereSet && MissionGenerationParser.Path != null)
MissionGenerationParser.Path = "home";
return c;
}
private static Computer addWebsiteChangeRequirements(Computer c, OS os)
{
var webServerDaemon = new WebServerDaemon(c, MissionGenerationParser.Target + " Web Server", os,
"Content/Web/BaseImageWebPage.html");
c.daemons.Add(webServerDaemon);
c.initDaemons();
if (customKeysWereSet && customFileData != null)
webServerDaemon.generateBaseCorporateSite(MissionGenerationParser.Target, customFileData);
else
webServerDaemon.generateBaseCorporateSite(MissionGenerationParser.Target,
"Content/Web/BaseCorporatePage.html");
MissionGenerationParser.File = "index.html";
MissionGenerationParser.Path = "web";
return c;
}
}
}