Skip to content

Commit

Permalink
feat: Load missions and factions
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Apr 28, 2024
1 parent 4987a24 commit 39d2de5
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Loader/Damage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using scdb.Xml.Entities;

namespace Loader
{
public class Damage
Expand All @@ -9,7 +11,7 @@ public class Damage
public double biochemical;
public double stun;

public static Damage FromDamageInfo(scdb.Xml.Entities.DamageInfo info)
public static Damage FromDamageInfo(DamageInfo info)
{
if (info == null) return null;

Expand Down
60 changes: 60 additions & 0 deletions Loader/FactionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Loader.scdb.Xml.Factions;
using Newtonsoft.Json;
using scdb.Xml.Entities;

namespace Loader
{
public class FactionLoader
{
public string OutputFolder { get; set; }
public string DataRoot { get; set; }

public Dictionary<string, Faction> LoadFactions()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "factions"));

var output = new Dictionary<string, Faction>();


var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "factions"));

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml"))
{
var faction = Parse<Faction>(entityFilename);
output.Add(faction.__ref, faction);
File.WriteAllText(Path.Combine(OutputFolder, "factions", $"{faction.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(faction));
}

return output;
}

T Parse<T>(string xmlFilename) where T : ClassBase
{
string rootNodeName;
using (var reader = XmlReader.Create(new StreamReader(xmlFilename)))
{
reader.MoveToContent();
rootNodeName = reader.Name;
}

var split = rootNodeName.Split('.');
string className = split[split.Length - 1];

var xml = File.ReadAllText(xmlFilename);
var doc = new XmlDocument();
doc.LoadXml(xml);

var serialiser = new XmlSerializer(typeof(T), new XmlRootAttribute { ElementName = rootNodeName });
using (var stream = new XmlNodeReader(doc))
{
var entity = (T)serialiser.Deserialize(stream);
entity.ClassName = className;
return entity;
}
}
}
}
71 changes: 71 additions & 0 deletions Loader/MissionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using scdb.Xml.Entities;
using scdb.Xml.Missionbroker;

namespace Loader
{
public class MissionLoader
{
public string OutputFolder { get; set; }
public string DataRoot { get; set; }
public LocalisationService locService { get; set; }

public Dictionary<string, MissionBrokerEntry> LoadMissions()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "missions"));

var output = new Dictionary<string, MissionBrokerEntry>();


var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "missionbroker"));

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories))
{
var mission = Parse<MissionBrokerEntry>(entityFilename);
AddTranslations(mission);
output.Add(mission.__ref, mission);
File.WriteAllText(Path.Combine(OutputFolder, "missions", $"{mission.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(mission));
}

return output;
}

T Parse<T>(string xmlFilename) where T : ClassBase
{
string rootNodeName;
using (var reader = XmlReader.Create(new StreamReader(xmlFilename)))
{
reader.MoveToContent();
rootNodeName = reader.Name;
}

var split = rootNodeName.Split('.');
string className = split[split.Length - 1];

var xml = File.ReadAllText(xmlFilename);
var doc = new XmlDocument();
doc.LoadXml(xml);

var serialiser = new XmlSerializer(typeof(T), new XmlRootAttribute { ElementName = rootNodeName });
using (var stream = new XmlNodeReader(doc))
{
var entity = (T)serialiser.Deserialize(stream);
entity.ClassName = className;
return entity;
}
}

private void AddTranslations(MissionBrokerEntry entity)
{
entity.title = locService.GetText(entity.title, entity.title);
entity.titleHUD = locService.GetText(entity.titleHUD, entity.titleHUD);
entity.description = locService.GetText(entity.description, entity.description);
entity.commsChannelName = locService.GetText(entity.commsChannelName, entity.commsChannelName);
entity.missionGiver = locService.GetText(entity.missionGiver, entity.missionGiver);
}
}
}
35 changes: 35 additions & 0 deletions Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static void Main(string[] args)
bool doItems = true;
bool doShops = true;
bool doStarmap = true;
bool doMissions = true;
bool noCache = false;
string typeFilter = null;
string shipFilter = null;
Expand All @@ -32,6 +33,7 @@ static void Main(string[] args)
{ "noitems", v => doItems = false },
{ "noshops", v => doShops = false },
{ "nomap", v => doStarmap = false },
{ "nomissions", v => doMissions = false },
{ "nocache", v => noCache = true },
{ "types=", v => typeFilter = v },
{ "ships=", v=> shipFilter = v }
Expand Down Expand Up @@ -104,6 +106,15 @@ static void Main(string[] args)
var lootTables = lootLoader.LoadTables();
var lootSvc = new LootService(lootArchetypes, lootTables);

// Factions
Console.WriteLine("Load Factions");
var factionLoader = new FactionLoader
{
OutputFolder = outputRoot,
DataRoot = scDataRoot
};
var factions = factionLoader.LoadFactions();

// Ammunition
Console.WriteLine("Load Ammunition");
var ammoLoader = new AmmoLoader
Expand All @@ -124,6 +135,30 @@ static void Main(string[] args)
// //var insuranceSvc = new InsuranceService(insurancePrices);
// var insuranceSvc = new InsuranceService(null);


// Missions
if (doMissions)
{
Console.WriteLine("Load Missions");
var missionLoader = new MissionLoader
{
OutputFolder = outputRoot,
DataRoot = scDataRoot,
locService = localisationSvc
};
var missions = missionLoader.LoadMissions();

var rewardsLoader = new RewardLoader
{
OutputFolder = outputRoot,
DataRoot = scDataRoot,
locService = localisationSvc
};
rewardsLoader.LoadRewards();
rewardsLoader.LoadStandings();
rewardsLoader.LoadScopes();
}

// PersonalInventories
Console.WriteLine("Load PersonalInventories");
var inventoryLoader = new InventoryContainerLoader()
Expand Down
113 changes: 113 additions & 0 deletions Loader/RewadLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Loader.scdb.Xml.Reputation;
using Newtonsoft.Json;
using scdb.Xml.Entities;

namespace Loader
{
public class RewardLoader
{
public string OutputFolder { get; set; }
public string DataRoot { get; set; }
public LocalisationService locService { get; set; }

public Dictionary<string, Scope> LoadScopes()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "reputation", "scopes"));

var output = new Dictionary<string, Scope>();


var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "reputation", "scopes"));

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories))
{
var scope = Parse<Scope>(entityFilename);
AddScopeTranslations(scope);
output.Add(scope.__ref, scope);
File.WriteAllText(Path.Combine(OutputFolder, "reputation", "scopes", $"{scope.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(scope));
}

return output;
}

public Dictionary<string, Standing> LoadStandings()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "reputation", "standings"));

var output = new Dictionary<string, Standing>();


var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "reputation", "standings"));

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories))
{
var standing = Parse<Standing>(entityFilename);
AddStandingTranslations(standing);
output.Add(standing.__ref, standing);
File.WriteAllText(Path.Combine(OutputFolder, "reputation", "standings", $"{standing.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(standing));
}

return output;
}

public void LoadRewards()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "reputation", "rewards"));

var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "reputation", "rewards"));

foreach (var entityFilename in Directory.EnumerateFiles(Path.Join(path, "missionrewards_reputation"), "*.xml", SearchOption.AllDirectories))
{
var reward = Parse<SReputationRewardAmount>(entityFilename);
File.WriteAllText(Path.Combine(OutputFolder, "reputation", "rewards", $"{reward.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(reward));
}

foreach (var entityFilename in Directory.EnumerateFiles(Path.Join(path, "missionrewards_bonusuec"), "*.xml", SearchOption.AllDirectories))
{
var reward = Parse<SReputationMissionRewardBonusParams>(entityFilename);
File.WriteAllText(Path.Combine(OutputFolder, "reputation", "rewards", $"{reward.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(reward));
}
}

T Parse<T>(string xmlFilename) where T : ClassBase
{
string rootNodeName;
using (var reader = XmlReader.Create(new StreamReader(xmlFilename)))
{
reader.MoveToContent();
rootNodeName = reader.Name;
}

var split = rootNodeName.Split('.');
string className = split[split.Length - 1];

var xml = File.ReadAllText(xmlFilename);
var doc = new XmlDocument();
doc.LoadXml(xml);

var serialiser = new XmlSerializer(typeof(T), new XmlRootAttribute { ElementName = rootNodeName });
using (var stream = new XmlNodeReader(doc))
{
var entity = (T)serialiser.Deserialize(stream);
entity.ClassName = className;
return entity;
}
}

private void AddScopeTranslations(Scope entity)
{
entity.displayName = locService.GetText(entity.displayName, entity.displayName);
entity.description = locService.GetText(entity.description, entity.description);
}

private void AddStandingTranslations(Standing entity)
{
entity.displayName = locService.GetText(entity.displayName, entity.displayName);
entity.perkDescription = locService.GetText(entity.perkDescription, entity.perkDescription);
}
}
}
27 changes: 27 additions & 0 deletions Loader/scdb.Xml/Factions/Faction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Xml.Serialization;
using scdb.Xml.Entities;

namespace Loader.scdb.Xml.Factions;

public class Faction : ClassBase
{
[XmlAttribute] public string displayName;
[XmlAttribute] public string description;
[XmlAttribute] public string gameToken;
[XmlAttribute] public string defaultReaction;

public FriendlyFireReactionOverride[] friendlyFireReactionOverrides;
public FactionRelationship[] factionRelationships;
}

public class FriendlyFireReactionOverride
{
[XmlAttribute] public string reactionType;
[XmlAttribute] public bool shouldAllowFriendlyFire;
}

public class FactionRelationship
{
[XmlAttribute] public string faction;
[XmlAttribute] public string reactionType;
}
Loading

0 comments on commit 39d2de5

Please sign in to comment.