2021-08-12 16:52:06 +01:00
using Generator.Helpers ;
using Generator.Models.Input ;
using Generator.Models.Output ;
using Newtonsoft.Json ;
2021-08-13 16:22:10 +01:00
using Newtonsoft.Json.Linq ;
2021-08-12 16:52:06 +01:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
using System.Linq ;
namespace Generator
{
internal class BotParser
{
private readonly string _dumpPath ;
2021-08-13 16:22:10 +01:00
public BotParser ( string dumpPath , string [ ] botTypes )
2021-08-12 16:52:06 +01:00
{
_dumpPath = dumpPath ;
}
public List < Datum > Parse ( )
{
var stopwatch = Stopwatch . StartNew ( ) ;
2021-08-13 16:22:10 +01:00
var failedFilesCount = 0 ;
2021-08-12 16:52:06 +01:00
CreateDirIfDoesntExist ( _dumpPath ) ;
var botFiles = Directory . GetFiles ( _dumpPath , "*.json" , SearchOption . TopDirectoryOnly ) . ToList ( ) ;
Console . WriteLine ( $"{botFiles.Count} files found" ) ;
var parsedBots = new List < Datum > ( ) ;
foreach ( var file in botFiles )
{
var splitFile = file . Split ( "\\" ) ;
2021-08-13 16:22:10 +01:00
2021-08-12 16:52:06 +01:00
var json = File . ReadAllText ( file ) ;
try
{
2021-08-13 16:22:10 +01:00
json = PruneMalformedBsgJson ( json , splitFile . Last ( ) ) ;
2021-08-12 16:52:06 +01:00
var bots = ParseJson ( json , file ) ;
2021-08-12 17:19:54 +01:00
if ( bots = = null | | bots . Count = = 0 )
{
2021-08-13 16:22:10 +01:00
Console . WriteLine ( $"skipping file: {splitFile.Last()}. no bots found, " ) ;
2021-08-12 17:19:54 +01:00
continue ;
}
2021-08-12 16:52:06 +01:00
Console . WriteLine ( $"parsing: {bots.Count} bots in file {splitFile.Last()}" ) ;
foreach ( var bot in bots )
{
parsedBots . Add ( bot ) ;
2021-08-13 16:22:10 +01:00
}
2021-08-12 16:52:06 +01:00
}
catch ( JsonException jex )
{
2021-08-13 16:22:10 +01:00
failedFilesCount + + ;
2021-08-12 16:52:06 +01:00
Console . WriteLine ( $"JSON Error message: {jex.Message} || file: {splitFile.Last()}" ) ;
}
}
stopwatch . Stop ( ) ;
2021-08-13 16:22:10 +01:00
LoggingHelpers . LogToConsole ( $"Cleaned and Parsed: {parsedBots.Count} Failed: {failedFilesCount}. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds" ) ;
2021-08-12 16:52:06 +01:00
2021-08-13 16:22:10 +01:00
return parsedBots ;
}
2021-08-12 16:52:06 +01:00
2021-08-13 16:22:10 +01:00
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");
2021-08-12 16:52:06 +01:00
2021-08-13 16:22:10 +01:00
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 ) ) ;
}
}
2021-08-12 16:52:06 +01:00
2021-08-13 16:22:10 +01:00
return o . ToString ( ) ;
2021-08-12 16:52:06 +01:00
}
private void CreateDirIfDoesntExist ( string path )
{
if ( ! Directory . Exists ( $"{path}" ) )
{
//create dump dir
Directory . CreateDirectory ( $"{path}" ) ;
}
}
private static List < Datum > ParseJson ( string json , string file )
{
//Console.WriteLine($"parsing file {file}");
var serialisedObject = JsonConvert . DeserializeObject < Models . Input . Root > ( json ) ;
return serialisedObject . data ;
}
}
}