2021-02-01 11:43:38 -08:00
using System ;
using System.Collections.Generic ;
2021-02-01 14:57:01 -08:00
using System.Linq ;
2021-02-01 15:09:44 -08:00
2021-02-01 11:43:38 -08:00
using SabreTools.Core ;
2020-12-08 16:37:08 -08:00
using SabreTools.Core.Tools ;
2021-02-01 11:43:38 -08:00
using SabreTools.DatFiles ;
using SabreTools.DatItems ;
2020-12-07 14:29:45 -08:00
using SabreTools.Logging ;
2020-08-21 10:54:51 -07:00
2020-12-08 13:48:57 -08:00
namespace SabreTools.Filtering
2020-08-21 10:15:38 -07:00
{
public class ExtraIni
{
2020-08-21 10:38:42 -07:00
#region Fields
2020-08-21 10:15:38 -07:00
/// <summary>
/// List of extras to apply
/// </summary>
public List < ExtraIniItem > Items { get ; set ; } = new List < ExtraIniItem > ( ) ;
2020-08-21 10:38:42 -07:00
#endregion
2020-10-07 15:42:30 -07:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
2020-10-07 16:37:10 -07:00
private readonly Logger logger ;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public ExtraIni ( )
{
logger = new Logger ( this ) ;
}
2020-10-07 15:42:30 -07:00
#endregion
2021-02-01 11:43:38 -08:00
#region Population
2020-08-21 10:38:42 -07:00
/// <summary>
/// Populate item using field:file inputs
/// </summary>
/// <param name="inputs">Field and file combinations</param>
public void PopulateFromList ( List < string > inputs )
{
2021-02-02 14:09:49 -08:00
InternalStopwatch watch = new InternalStopwatch ( "Populating extras from list" ) ;
2020-08-21 10:38:42 -07:00
foreach ( string input in inputs )
{
ExtraIniItem item = new ExtraIniItem ( ) ;
2020-08-21 10:54:51 -07:00
// If we don't even have a possible field and file combination
if ( ! input . Contains ( ":" ) )
{
2020-10-07 15:42:30 -07:00
logger . Warning ( $"'{input}` is not a valid INI extras string. Valid INI extras strings are of the form 'key:value'. Please refer to README.1ST or the help feature for more details." ) ;
2020-08-21 10:54:51 -07:00
return ;
}
string inputTrimmed = input . Trim ( '"' , ' ' , '\t' ) ;
string fieldString = inputTrimmed . Split ( ':' ) [ 0 ] . ToLowerInvariant ( ) . Trim ( '"' , ' ' , '\t' ) ;
2020-12-14 16:01:28 -08:00
string fileString = inputTrimmed [ ( fieldString . Length + 1 ) . . ] . Trim ( '"' , ' ' , '\t' ) ;
2020-08-21 10:54:51 -07:00
2020-12-13 13:22:06 -08:00
item . DatItemField = fieldString . AsDatItemField ( ) ;
item . MachineField = fieldString . AsMachineField ( ) ;
2020-08-21 10:54:51 -07:00
if ( item . PopulateFromFile ( fileString ) )
Items . Add ( item ) ;
2020-08-21 10:38:42 -07:00
}
2021-02-02 14:09:49 -08:00
watch . Stop ( ) ;
2020-08-21 10:38:42 -07:00
}
#endregion
2021-02-01 11:43:38 -08:00
#region Running
/// <summary>
/// Apply a set of Extra INIs on the DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the extras were applied, false on error</returns>
public bool ApplyExtras ( DatFile datFile , bool throwOnError = false )
{
2021-02-02 15:15:07 -08:00
// If we have no extras, don't attempt to apply and just return true
if ( Items = = null | | ! Items . Any ( ) )
return true ;
2021-02-02 14:09:49 -08:00
InternalStopwatch watch = new InternalStopwatch ( "Applying extra mappings to DAT" ) ;
2021-02-01 11:43:38 -08:00
try
{
// Bucket by game first
datFile . Items . BucketBy ( ItemKey . Machine , DedupeType . None ) ;
2021-02-01 14:57:01 -08:00
// Create mappings based on the extra items
var combinedMachineMaps = CombineMachineExtras ( ) ;
var combinedDatItemMaps = CombineDatItemExtras ( ) ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Now get the combined set of keys
var machines = combinedMachineMaps . Keys . Concat ( combinedDatItemMaps . Keys ) . Distinct ( ) ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Apply the mappings
foreach ( string machine in machines )
2021-02-01 11:43:38 -08:00
{
// If the key doesn't exist, continue
2021-02-01 14:57:01 -08:00
if ( ! datFile . Items . ContainsKey ( machine ) )
2021-02-01 11:43:38 -08:00
continue ;
2021-02-01 14:57:01 -08:00
// Get the list of DatItems for the machine
List < DatItem > datItems = datFile . Items [ machine ] ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Try to get the map values, if possible
combinedMachineMaps . TryGetValue ( machine , out Dictionary < MachineField , string > machineMappings ) ;
combinedDatItemMaps . TryGetValue ( machine , out Dictionary < DatItemField , string > datItemMappings ) ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Create a setter with the new mappings
Setter setter = new Setter
{
MachineMappings = machineMappings ,
DatItemMappings = datItemMappings ,
} ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Loop through and set the fields accordingly
2021-02-01 11:43:38 -08:00
foreach ( var datItem in datItems )
{
2021-02-01 14:57:01 -08:00
setter . SetFields ( datItem . Machine ) ;
2021-02-01 14:07:50 -08:00
setter . SetFields ( datItem ) ;
2021-02-01 11:43:38 -08:00
}
}
}
catch ( Exception ex ) when ( ! throwOnError )
{
logger . Error ( ex ) ;
return false ;
}
2021-02-02 14:09:49 -08:00
finally
{
watch . Stop ( ) ;
}
2021-02-01 11:43:38 -08:00
return true ;
}
2021-02-01 14:57:01 -08:00
/// <summary>
/// Combine MachineField-based ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
private Dictionary < string , Dictionary < MachineField , string > > CombineMachineExtras ( )
{
var machineMap = new Dictionary < string , Dictionary < MachineField , string > > ( ) ;
// Loop through each of the extras
foreach ( ExtraIniItem item in Items . Where ( i = > i . MachineField ! = MachineField . NULL ) )
{
foreach ( var mapping in item . Mappings )
{
string machineName = mapping . Key ;
string value = mapping . Value ;
machineMap [ machineName ] = new Dictionary < MachineField , string >
{
[item.MachineField] = value ,
} ;
}
}
return machineMap ;
}
/// <summary>
/// Combine DatItemField-based ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
private Dictionary < string , Dictionary < DatItemField , string > > CombineDatItemExtras ( )
{
var datItemMap = new Dictionary < string , Dictionary < DatItemField , string > > ( ) ;
// Loop through each of the extras
foreach ( ExtraIniItem item in Items . Where ( i = > i . DatItemField ! = DatItemField . NULL ) )
{
foreach ( var mapping in item . Mappings )
{
string machineName = mapping . Key ;
string value = mapping . Value ;
datItemMap [ machineName ] = new Dictionary < DatItemField , string > ( )
{
[item.DatItemField] = value ,
} ;
}
}
return datItemMap ;
}
2021-02-01 11:43:38 -08:00
#endregion
2020-08-21 10:15:38 -07:00
}
}