239 lines
8.6 KiB
C#
Raw Normal View History

using Common;
using Common.Extensions;
using Newtonsoft.Json;
using PMCGenerator.Models;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PMCGenerator
{
class Program
{
static void Main(string[] args)
{
var presetPath = CreateInputFolder();
var presetFiles = GetPresetFileList(presetPath);
// Parse into list of strongly typed objects
2021-08-24 23:04:26 +01:00
var parsedPresets = ParsePresetJsons(presetFiles);
// Create flat lists of weapons + list of mods
2021-08-24 23:04:26 +01:00
var flatPrimaryWeaponsList = GetWeaponsFromRawFile(parsedPresets);
var flatSecondaryWeaponsList = GetSecondaryWeaponsFromRawFile(parsedPresets);
var flatModList = GetModsFromRawFile(parsedPresets);
// Add weapon mods to output
2021-08-24 23:04:26 +01:00
var output = new { FirstPrimaryWeapon = new List<string>(), Holster = new List<string>(), mods = new Dictionary<string, Dictionary<string, List<string>>>() };
output.FirstPrimaryWeapon.AddRange(flatPrimaryWeaponsList.Select(x => x.TemplateId).Distinct());
output.Holster.AddRange(flatSecondaryWeaponsList.Select(x => x.TemplateId).Distinct());
2021-08-24 17:11:19 +01:00
2021-08-24 23:04:26 +01:00
foreach (var weapon in flatPrimaryWeaponsList)
{
// add weapon if its not already here
2021-08-24 17:11:19 +01:00
if (!output.mods.ContainsKey(weapon.TemplateId))
{
// Add weapon to dictionary
2021-08-24 17:11:19 +01:00
output.mods.Add(weapon.TemplateId, new Dictionary<string, List<string>>());
}
// Get mods types for this gun, top level
var uniqueModSlots = flatModList.Where(x => x.ParentId == weapon.Id).Select(x => x.SlotId).Distinct().ToList();
foreach (var modSlotId in uniqueModSlots)
{
2021-08-24 17:11:19 +01:00
Dictionary<string, List<string>> weaponModsToModify = output.mods[weapon.TemplateId];
if (!weaponModsToModify.ContainsKey(modSlotId))
{
weaponModsToModify.Add(modSlotId, new List<string>());
}
}
var modsForWeapon = flatModList.Where(x => x.ParentId == weapon.Id).ToList();
2021-08-24 17:11:19 +01:00
Dictionary<string, List<string>> weaponMods = output.mods[weapon.TemplateId];
foreach (var mod in modsForWeapon)
{
weaponMods[mod.SlotId].AddUnique(mod.TemplateId);
}
}
// Get mods where parent is not weapon and add to output
foreach (var mod in flatModList.Where(x => x.ParentId != null
2021-08-24 23:04:26 +01:00
&& !flatPrimaryWeaponsList.Any(y => y.Id == x.ParentId)).ToList())
{
// No parent tempalte id found, create and add mods details
2021-08-24 17:11:19 +01:00
if (!output.mods.ContainsKey(mod.ParentTemplateId))
{
var templateIdsList = new List<string>{mod.TemplateId};
var subtype = new Dictionary<string, List<string>>{{ mod.SlotId, templateIdsList } };
2021-08-24 17:11:19 +01:00
output.mods.Add(mod.ParentTemplateId, subtype);
}
//Add subtype to item
2021-08-24 17:11:19 +01:00
var subtypeToAddTo = output.mods[mod.ParentTemplateId];
// No subtype, add it
if (!subtypeToAddTo.ContainsKey(mod.SlotId))
{
var valueToAdd = new List<string>(){ mod.TemplateId };
subtypeToAddTo.Add(mod.SlotId, valueToAdd);
}
// subtype exists, add to it
subtypeToAddTo[mod.SlotId].AddUnique(mod.TemplateId);
}
// Create output dir
var outputPath = CreateOutputFolder();
// Turn into json
var outputJson = JsonConvert.SerializeObject(output, Formatting.Indented);
CreateJsonFile(outputPath, outputJson);
}
2021-08-24 23:04:26 +01:00
private static List<Presets> ParsePresetJsons(List<string> presetFiles)
{
var result = new List<Presets>();
foreach (var presetFile in presetFiles)
{
var json = File.ReadAllText(presetFile);
var parsedFile = JsonConvert.DeserializeObject<Presets>(json);
result.Add(parsedFile);
}
int count = 0;
foreach (var file in result)
{
count += file.weaponbuilds.Count;
}
LoggingHelpers.LogToConsole($"{count} presets parsed");
return result;
}
2021-08-24 17:11:19 +01:00
private static List<ModDetails> GetModsFromRawFile(List<Presets> parsedPresets)
{
List <ModDetails> result = new List<ModDetails>();
2021-08-24 17:11:19 +01:00
foreach (var file in parsedPresets)
{
2021-08-24 17:11:19 +01:00
foreach (var item in file.weaponbuilds)
{
2021-08-24 17:11:19 +01:00
Weapon weapon = item.Value;
// Loop over weapons mods
foreach (var mod in weapon.items)
{
2021-08-24 17:11:19 +01:00
// Skip items with no parent (this is the weapon itself, first item in list)
if (mod.parentId == null)
{
continue;
}
Module parentMod = GetModsParent(file, mod.parentId);
2021-08-24 23:04:26 +01:00
if (parentMod != null)
{
result.Add(new ModDetails(mod.slotId, mod._id, mod._tpl, mod.parentId, parentMod._tpl));
}
}
}
}
2021-08-24 17:11:19 +01:00
return result;
}
/// <summary>
/// Find a mod where the supplied parentid equals the items id
/// </summary>
private static Module GetModsParent(Presets parsedPresets, string parentId)
{
foreach (var y in parsedPresets.weaponbuilds.Values)
{
var mod = y.items.Find(x => x._id == parentId);
if (mod != null)
{
return mod;
}
}
return null;
}
2021-08-24 17:11:19 +01:00
private static List<WeaponDetails> GetWeaponsFromRawFile(List<Presets> parsedPresets)
{
var result = new List<WeaponDetails>();
2021-08-24 17:11:19 +01:00
foreach (var file in parsedPresets)
{
2021-08-24 23:04:26 +01:00
foreach (var item in file.weaponbuilds.Where(x=> !x.Key.Contains("pistol")))
2021-08-24 17:11:19 +01:00
{
Weapon weapon = item.Value;
result.Add(new WeaponDetails(item.Key, weapon.items[0]._id, weapon.items[0]._tpl));
}
}
2021-08-24 17:11:19 +01:00
return result;
}
2021-08-24 23:04:26 +01:00
private static List<WeaponDetails> GetSecondaryWeaponsFromRawFile(List<Presets> parsedPresets)
{
var result = new List<WeaponDetails>();
foreach (var file in parsedPresets)
{
foreach (var item in file.weaponbuilds.Where(x => x.Key.Contains("pistol")))
{
Weapon weapon = item.Value;
result.Add(new WeaponDetails(item.Key, weapon.items[0]._id, weapon.items[0]._tpl));
}
}
return result;
}
// Write json to a file
private static void CreateJsonFile(string outputPath, string outputJson)
{
File.WriteAllText($"{outputPath}\\usec.json", outputJson);
}
/// <summary>
/// Read json file names from preset folder
/// </summary>
/// <param name="presetPath">path to check for preset files</param>
/// <returns></returns>
private static List<string> GetPresetFileList(string presetPath)
{
var presetFiles = Directory.GetFiles(presetPath, "*.json", SearchOption.TopDirectoryOnly).ToList();
2021-08-24 23:04:26 +01:00
LoggingHelpers.LogToConsole($"{presetFiles.Count} preset files found");
return presetFiles;
}
/// <summary>
/// Create folder structure to read from
/// </summary>
private static string CreateInputFolder()
{
var workingPath = Directory.GetCurrentDirectory();
var presetPath = $"{workingPath}//input//presets";
DiskHelpers.CreateDirIfDoesntExist(presetPath);
return presetPath;
}
private static string CreateOutputFolder()
{
var workingPath = Directory.GetCurrentDirectory();
var outputPath = $"{workingPath}\\output";
DiskHelpers.CreateDirIfDoesntExist(outputPath);
return outputPath;
}
}
}