From 52da9c254bfeb3e256aa4f53dd1e4d4bbe41ba92 Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 13 Aug 2021 16:22:10 +0100 Subject: [PATCH] Add code to clean up bsg json so more bot files can be parsed instead of discarded --- Generator/BotParser.cs | 45 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/Generator/BotParser.cs b/Generator/BotParser.cs index f4befe9..7ae53eb 100644 --- a/Generator/BotParser.cs +++ b/Generator/BotParser.cs @@ -2,6 +2,7 @@ using Generator.Models.Input; using Generator.Models.Output; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; @@ -12,22 +13,18 @@ namespace Generator { internal class BotParser { - private readonly string _workingPath; private readonly string _dumpPath; - private readonly string[] _botTypes; - public BotParser(string workingPath, string dumpPath, string[] botTypes) + public BotParser(string dumpPath, string[] botTypes) { - _workingPath = workingPath; _dumpPath = dumpPath; - _botTypes = botTypes; } public List Parse() { var stopwatch = Stopwatch.StartNew(); - var failedFiles = 0; + var failedFilesCount = 0; CreateDirIfDoesntExist(_dumpPath); var botFiles = Directory.GetFiles(_dumpPath, "*.json", SearchOption.TopDirectoryOnly).ToList(); @@ -37,16 +34,18 @@ namespace Generator foreach (var file in botFiles) { var splitFile = file.Split("\\"); - failedFiles++; + var json = File.ReadAllText(file); try { + json = PruneMalformedBsgJson(json, splitFile.Last()); + var bots = ParseJson(json, file); if (bots == null || bots.Count == 0) { - Console.WriteLine($"no bots found, skipping file: {splitFile.Last()}"); + Console.WriteLine($"skipping file: {splitFile.Last()}. no bots found, "); continue; } @@ -54,25 +53,41 @@ namespace Generator foreach (var bot in bots) { parsedBots.Add(bot); - } + } } catch (JsonException jex) { - + failedFilesCount++; Console.WriteLine($"JSON Error message: {jex.Message} || file: {splitFile.Last()}"); - continue; } } stopwatch.Stop(); - - Console.WriteLine($"Parsed: {parsedBots.Count} Failed: {failedFiles}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds"); - - + LoggingHelpers.LogToConsole($"Cleaned and Parsed: {parsedBots.Count} Failed: {failedFilesCount}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds"); return parsedBots; } + private string PruneMalformedBsgJson(string json, string fileName) + { + // Bsg send json where an item has a location of 1 but it should be an object with x/y/z coords + var o = JObject.Parse(json); + var jItemsToReplace = o.SelectTokens("$.data[*].Inventory.items[?(@.location == 1)].location"); + //var jItemsToReplace = o.SelectTokens("$.data[*].Inventory.items[?(@.location == 1 && @.slotId == 'cartridges')].location"); + + if (jItemsToReplace != null && jItemsToReplace.Any()) + { + LoggingHelpers.LogToConsole($"file {fileName} has {jItemsToReplace.Count()} json issues, cleaning up."); + foreach (var item in jItemsToReplace) + { + var obj = new { x = 1, y = 0, r = 0 }; + item.Replace(JToken.FromObject(obj)); + } + } + + return o.ToString(); + } + private void CreateDirIfDoesntExist(string path) { if (!Directory.Exists($"{path}"))