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 ;
2024-03-12 22:52:36 -04:00
using SabreTools.Core.Filter ;
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>
2024-03-05 17:17:40 -05:00
public List < ExtraIniItem > Items { get ; set ; } = [ ] ;
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-09 22:25:43 -08:00
// If there are no inputs, just skip
if ( inputs = = null | | ! inputs . Any ( ) )
return ;
2023-04-19 16:39:58 -04:00
InternalStopwatch watch = new ( "Populating extras from list" ) ;
2021-02-02 14:09:49 -08:00
2020-08-21 10:38:42 -07:00
foreach ( string input in inputs )
{
2023-04-19 16:39:58 -04:00
ExtraIniItem item = new ( ) ;
2020-08-21 10:54:51 -07:00
// If we don't even have a possible field and file combination
2023-04-19 16:39:58 -04:00
if ( ! input . Contains ( ':' ) )
2020-08-21 10:54:51 -07:00
{
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' ) ;
2024-02-28 23:09:31 -05:00
string fileString = inputTrimmed . Substring ( fieldString . Length + 1 ) . Trim ( '"' , ' ' , '\t' ) ;
2020-08-21 10:54:51 -07:00
2024-03-12 22:52:36 -04:00
item . FieldName = FilterParser . ParseFilterId ( fieldString ) ;
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
2024-03-05 17:33:02 -05:00
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 ;
2024-03-05 17:17:40 -05:00
var watch = new InternalStopwatch ( "Applying extra mappings to DAT" ) ;
2021-02-02 14:09:49 -08:00
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
2024-03-05 17:33:02 -05:00
var combinedMaps = CombineExtras ( ) ;
var machines = combinedMaps . Keys ;
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
2024-02-28 19:19:50 -05:00
var datItems = datFile . Items [ machine ] ;
if ( datItems = = null )
continue ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Try to get the map values, if possible
2024-03-05 17:33:02 -05:00
combinedMaps . TryGetValue ( machine , out var mappings ) ;
2021-02-01 11:43:38 -08:00
2021-02-01 14:57:01 -08:00
// Create a setter with the new mappings
2024-03-05 17:17:40 -05:00
var setter = new Setter ( ) ;
2024-03-05 17:33:02 -05:00
setter . PopulateSettersFromDictionary ( mappings ) ;
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 )
{
2024-03-10 16:49:07 -04:00
setter . SetFields ( datItem . GetFieldValue < Machine > ( DatItem . MachineKey ) ) ;
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 ;
}
2024-03-05 17:17:40 -05:00
/// <summary>
/// Combine ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
2024-03-05 17:33:02 -05:00
private Dictionary < string , Dictionary < ( string , string ) , string > > CombineExtras ( )
2024-03-05 17:17:40 -05:00
{
2024-03-05 17:33:02 -05:00
var machineMap = new Dictionary < string , Dictionary < ( string , string ) , string > > ( ) ;
2024-03-05 17:17:40 -05:00
// Loop through each of the extras
foreach ( ExtraIniItem item in Items )
{
foreach ( var mapping in item . Mappings )
{
string machineName = mapping . Key ;
string value = mapping . Value ;
2024-03-05 17:33:02 -05:00
if ( ! machineMap . ContainsKey ( machineName ) )
machineMap [ machineName ] = [ ] ;
2024-03-05 17:17:40 -05:00
2024-03-05 17:33:02 -05:00
machineMap [ machineName ] [ item . FieldName ! ] = value ;
2021-02-01 14:57:01 -08:00
}
}
return machineMap ;
}
2021-02-01 11:43:38 -08:00
#endregion
2020-08-21 10:15:38 -07:00
}
}