Skip to content

Commit

Permalink
feat: Parse lootgeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Apr 21, 2024
1 parent 10060a1 commit 4987a24
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 1 deletion.
83 changes: 83 additions & 0 deletions Loader/LootLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Scdb.Xml;
using scdb.Xml.Entities;
using scdb.Xml.Lootgeneration;

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

public Dictionary<string, LootArchetype> LoadArchetypes()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "loot"));
Directory.CreateDirectory(Path.Combine(OutputFolder, "loot", "archetypes"));

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


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

foreach (var entityFilename in Directory.EnumerateFiles(Path.Join(path, "lootarchetypes"), "*.xml"))
{
var archetype = Parse<LootArchetype>(entityFilename);
output.Add(archetype.__ref, archetype);
File.WriteAllText(Path.Combine(OutputFolder, "loot", "archetypes", $"{archetype.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(archetype));
}

return output;
}

public Dictionary<string, LootTable> LoadTables()
{
Directory.CreateDirectory(Path.Combine(OutputFolder, "loot"));
Directory.CreateDirectory(Path.Combine(OutputFolder, "loot", "tables"));

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

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

foreach (var entityFilename in Directory.EnumerateFiles(Path.Join(path, "loottables"), "*.xml", SearchOption.AllDirectories))
{
var lootTable = Parse<LootTable>(entityFilename);
output.Add(lootTable.__ref, lootTable);
File.WriteAllText(Path.Combine(OutputFolder, "loot", "tables", $"{lootTable.ClassName.ToLower()}.json"), JsonConvert.SerializeObject(lootTable));
}

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;
}
}
}
}
16 changes: 16 additions & 0 deletions Loader/LootService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using scdb.Xml.Lootgeneration;

namespace Loader;

public class LootService
{
public Dictionary<string, LootArchetype> archetypes;
public Dictionary<string, LootTable> tables;

public LootService(Dictionary<string, LootArchetype> archetypes, Dictionary<string, LootTable> tables)
{
this.archetypes = archetypes;
this.tables = tables;
}
}
18 changes: 17 additions & 1 deletion Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ static void Main(string[] args)
var manufacturerIndex = manufacturerLoader.Load();
var manufacturerSvc = new ManufacturerService(manufacturerIndex);

// Loot
Console.WriteLine("Load Loot Archetypes");
var lootLoader = new LootLoader
{
OutputFolder = outputRoot,
DataRoot = scDataRoot
};
var lootArchetypes = lootLoader.LoadArchetypes();
var lootTables = lootLoader.LoadTables();
var lootSvc = new LootService(lootArchetypes, lootTables);

// Ammunition
Console.WriteLine("Load Ammunition");
var ammoLoader = new AmmoLoader
Expand Down Expand Up @@ -127,12 +138,17 @@ static void Main(string[] args)
var loadoutLoader = new LoadoutLoader(xmlLoadoutLoader, manualLoadoutLoader);
var itemBuilder = new ItemBuilder(localisationSvc, manufacturerSvc, ammoSvc, entitySvc, inventorySvc);
var itemInstaller = new ItemInstaller(entitySvc, loadoutLoader, itemBuilder);
var meleeLoader = new MeleeCombatLoader{
OutputFolder = outputRoot,
DataRoot = scDataRoot
};;
var meleeConfigSvc = new MeleeCombatService(meleeLoader.Load());

// Items
if (doItems)
{
Console.WriteLine("Load Items");
var itemLoader = new ItemLoader(itemBuilder, manufacturerSvc, entitySvc, ammoSvc, itemInstaller, loadoutLoader, inventorySvc)
var itemLoader = new ItemLoader(itemBuilder, manufacturerSvc, entitySvc, ammoSvc, itemInstaller, loadoutLoader, inventorySvc, meleeConfigSvc)
{
OutputFolder = outputRoot,
DataRoot = scDataRoot,
Expand Down
88 changes: 88 additions & 0 deletions Loader/scdb.Xml/Lootgeneration/LootArchetype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Xml;
using System.Xml.Serialization;
using scdb.Xml.Entities;

namespace scdb.Xml.Lootgeneration
{
public class LootArchetype: ClassBase
{
public ExcludedTags excludedTags;

public PrimaryOrGroup primaryOrGroup;
public SecondaryOrGroups secondaryOrGroups;
}


public class ExcludedTags
{
public Reference[] tags;
}

public class PrimaryOrGroup
{
[XmlAttribute] public string __type;
public LootArchetypeEntry_Primary[] entries;
}


public class SecondaryOrGroups
{
public LootArchetypeOrGroup_Secondary LootArchetypeOrGroup_Secondary;
}

public class LootArchetypeEntry
{
[XmlAttribute] public string name;
[XmlAttribute] public string groupName;
[XmlAttribute] public string tag;
[XmlAttribute] public double weight;
[XmlAttribute] public string __type;
public AdditionalTags additionalTags;
public OptionalData optionalData;
}

public class LootArchetypeEntry_Primary : LootArchetypeEntry;

public class LootArchetypeEntry_Secondary
{
[XmlAttribute] public string tag;
[XmlAttribute] public double weight;
[XmlAttribute] public string __type;
};

public class LootArchetypeOrGroup_Secondary
{

public LootArchetypeEntry_Secondary[] entries;
};

public class OptionalData
{
public EntryOptionalData_StackSize EntryOptionalData_StackSize;
public EntryOptionalData_SpawnWith EntryOptionalData_SpawnWith;
}

public class EntryOptionalData_StackSize
{
[XmlAttribute] public double min;
[XmlAttribute] public double max;
}

public class EntryOptionalData_SpawnWith: EntryOptionalData_StackSize
{
[XmlAttribute] public string name;
[XmlAttribute] public string mode;

public TagsToMatch tagsToMatch;
}

public class PnTags
{
public Reference[] positiveTags;
public Reference[] negativeTags;
}

public class AdditionalTags : PnTags;

public class TagsToMatch : PnTags;
}
25 changes: 25 additions & 0 deletions Loader/scdb.Xml/Lootgeneration/LootTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Xml;
using System.Xml.Serialization;
using scdb.Xml.Entities;

namespace scdb.Xml.Lootgeneration
{
public class LootTable: ClassBase
{
public WeightedLootArchetype[] lootArchetypes;
}

public class WeightedLootArchetype
{
[XmlAttribute] public string archetype;
[XmlAttribute] public double weight;
public NumberOfResultsConstraints numberOfResultsConstraints;
}

public class NumberOfResultsConstraints
{
[XmlAttribute] public double minResults;
[XmlAttribute] public double maxResults;

}
}

0 comments on commit 4987a24

Please sign in to comment.