Skip to content

Commit

Permalink
feat: Parse mission giver and types
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed May 1, 2024
1 parent 39d2de5 commit 4cfc577
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 50 deletions.
35 changes: 8 additions & 27 deletions Loader/FactionLoader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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
{
Expand All @@ -13,48 +10,32 @@ public class FactionLoader
public string OutputFolder { get; set; }
public string DataRoot { get; set; }

public LocalisationService locService { 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"));
var parser = new ClassParser<Faction>();

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml"))
{
var faction = Parse<Faction>(entityFilename);
var faction = parser.Parse(entityFilename);
AddTranslations(faction);
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
private void AddTranslations(Faction faction)
{
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;
}
faction.displayName = locService.GetText(faction.displayName, faction.displayName);
faction.description = locService.GetText(faction.description, faction.description);
}
}
}
6 changes: 4 additions & 2 deletions Loader/LocalisationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public string GetText(string label, string fallback = null)
if (label == null) return fallback;

var key = label.StartsWith("@") ? label.Substring(1) : label;
var keyP = key+",P";

if (key == "LOC_EMPTY") return fallback;
if (key == "LOC_UNINITIALIZED") return fallback;

if (!labels.ContainsKey(key)) return fallback;
if (!labels.ContainsKey(key) && !labels.ContainsKey(keyP)) return fallback;

var text = labels[key].Replace("\\n", "\n");
var text = labels.ContainsKey(key) ? labels[key] : labels[keyP];
text = text.Replace("\\n", "\n");

if (text == "<= PLACEHOLDER =>") return fallback ?? label;
if (String.IsNullOrWhiteSpace(text)) return fallback ?? label;
Expand Down
68 changes: 48 additions & 20 deletions Loader/MissionLoader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Loader.scdb.Xml.Missiongiver;
using Loader.scdb.Xml.Missiontype;
using Newtonsoft.Json;
using scdb.Xml.Entities;
using scdb.Xml.Missionbroker;

namespace Loader
Expand All @@ -19,13 +18,13 @@ public Dictionary<string, MissionBrokerEntry> LoadMissions()
Directory.CreateDirectory(Path.Combine(OutputFolder, "missions"));

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

var parser = new ClassParser<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);
var mission = parser.Parse(entityFilename);
AddTranslations(mission);
output.Add(mission.__ref, mission);
File.WriteAllText(Path.Combine(OutputFolder, "missions", $"{mission.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(mission));
Expand All @@ -34,29 +33,58 @@ public Dictionary<string, MissionBrokerEntry> LoadMissions()
return output;
}

T Parse<T>(string xmlFilename) where T : ClassBase
public Dictionary<string, MissionType> LoadMissionTypes()
{
string rootNodeName;
using (var reader = XmlReader.Create(new StreamReader(xmlFilename)))
Directory.CreateDirectory(Path.Combine(OutputFolder, "missions", "types"));

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

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

var parser = new ClassParser<MissionType>();

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories))
{
reader.MoveToContent();
rootNodeName = reader.Name;
var missionType = parser.Parse(entityFilename);
AddTypeTranslations(missionType);
output.Add(missionType.__ref, missionType);
File.WriteAllText(Path.Combine(OutputFolder, "missions", "types", $"{missionType.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(missionType));
}

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

public Dictionary<string, MissionGiver> LoadMissionGiver()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "missions", "missiongiver"));

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

var xml = File.ReadAllText(xmlFilename);
var doc = new XmlDocument();
doc.LoadXml(xml);
var path = Path.Combine(DataRoot, Path.Join("Data", "Libs", "Foundry", "Records", "missiongiver"));

var serialiser = new XmlSerializer(typeof(T), new XmlRootAttribute { ElementName = rootNodeName });
using (var stream = new XmlNodeReader(doc))
var parser = new ClassParser<MissionGiver>();

foreach (var entityFilename in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories))
{
var entity = (T)serialiser.Deserialize(stream);
entity.ClassName = className;
return entity;
var giver = parser.Parse(entityFilename);
AddGiverTranslations(giver);
output.Add(giver.__ref, giver);
File.WriteAllText(Path.Combine(OutputFolder, "missions", "missiongiver", $"{giver.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(giver));
}

return output;
}

private void AddTypeTranslations(MissionType entity)
{
entity.LocalisedTypeName = locService.GetText(entity.LocalisedTypeName, entity.LocalisedTypeName);
}

private void AddGiverTranslations(MissionGiver entity)
{
entity.description = locService.GetText(entity.description, entity.description);
entity.headquarters = locService.GetText(entity.headquarters, entity.headquarters);
entity.displayName = locService.GetText(entity.displayName, entity.displayName);
}

private void AddTranslations(MissionBrokerEntry entity)
Expand Down
5 changes: 4 additions & 1 deletion Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ static void Main(string[] args)
var factionLoader = new FactionLoader
{
OutputFolder = outputRoot,
DataRoot = scDataRoot
DataRoot = scDataRoot,
locService = localisationSvc
};
var factions = factionLoader.LoadFactions();

Expand Down Expand Up @@ -147,6 +148,8 @@ static void Main(string[] args)
locService = localisationSvc
};
var missions = missionLoader.LoadMissions();
missionLoader.LoadMissionTypes();
missionLoader.LoadMissionGiver();

var rewardsLoader = new RewardLoader
{
Expand Down
1 change: 1 addition & 0 deletions Loader/scdb.Xml/Missionbroker/Missionbroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class MissionBrokerEntry : ClassBase
public Reference[] associatedMissions;
public MissionReward missionReward;
public SReputationAmountListParams[] missionResultReputationRewards;
public MissionDeadline missionDeadline;
public CompletionTags completionTags;
public Modifiers modifiers;
public Reference[] missionTags;
Expand Down
17 changes: 17 additions & 0 deletions Loader/scdb.Xml/Missiongiver/MissionGiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Xml.Serialization;
using scdb.Xml.Entities;

namespace Loader.scdb.Xml.Missiongiver;

public class MissionGiver : ClassBase
{
[XmlAttribute] public string entityClass;
[XmlAttribute] public string displayName;
[XmlAttribute] public string description;
[XmlAttribute] public string headquarters;
[XmlAttribute] public double invitationTimeout;
[XmlAttribute] public double visitTimeout;
[XmlAttribute] public double shortCooldown;
[XmlAttribute] public double mediumCooldown;
[XmlAttribute] public double longCooldown;
}
9 changes: 9 additions & 0 deletions Loader/scdb.Xml/Missiontype/MissionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Xml.Serialization;
using scdb.Xml.Entities;

namespace Loader.scdb.Xml.Missiontype;

public class MissionType: ClassBase
{
[XmlAttribute] public string LocalisedTypeName;
}

0 comments on commit 4cfc577

Please sign in to comment.