2021-02-01 11:43:38 -08:00
using System ;
using System.Collections.Generic ;
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 ;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging ;
2020-08-21 10:54:51 -07:00
2024-10-30 11:26:56 -04:00
namespace SabreTools.DatTools
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-10-24 02:47:30 -04:00
public readonly List < ExtraIniItem > Items = [ ] ;
2020-08-21 10:38:42 -07:00
#endregion
2020-10-07 15:42:30 -07:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
2025-01-08 16:59:44 -05:00
private readonly Logger _logger ;
2020-10-07 16:37:10 -07:00
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public ExtraIni ( )
{
2025-01-08 16:59:44 -05:00
_logger = new Logger ( this ) ;
2020-10-07 16:37:10 -07:00
}
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
2024-10-19 21:41:08 -04:00
if ( inputs = = null | | inputs . Count = = 0 )
2021-02-09 22:25:43 -08:00
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 )
{
2020-08-21 10:54:51 -07:00
// If we don't even have a possible field and file combination
2024-10-19 21:41:08 -04:00
if ( ! input . Contains ( ":" ) )
2020-08-21 10:54:51 -07:00
{
2025-01-08 16:59:44 -05: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-10-24 04:01:45 -04:00
var item = new ExtraIniItem ( fieldString , fileString ) ;
2024-10-24 03:32:53 -04:00
if ( item . Mappings . Count > 0 )
2020-08-21 10:54:51 -07:00
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
2025-01-10 22:03:50 -05:00
if ( Items . Count = = 0 )
2021-02-02 15:15:07 -08:00
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
2025-01-14 20:21:54 -05:00
datFile . BucketBy ( ItemKey . Machine ) ;
2021-02-01 11:43:38 -08:00
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
{
2021-02-01 14:57:01 -08:00
// Get the list of DatItems for the machine
2025-01-14 20:26:57 -05:00
List < DatItem > datItems = datFile . GetItemsForBucket ( machine ) ;
if ( datItems . Count = = 0 )
2024-02-28 19:19:50 -05:00
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 )
{
2025-01-08 16:59:44 -05:00
_logger . Error ( ex ) ;
2021-02-01 11:43:38 -08:00
return false ;
}
2021-02-02 14:09:49 -08:00
finally
{
2024-03-19 21:55:55 -04:00
watch . Stop ( ) ;
}
return true ;
}
/// <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 ApplyExtrasDB ( DatFile datFile , bool throwOnError = false )
{
// If we have no extras, don't attempt to apply and just return true
2025-01-10 22:03:50 -05:00
if ( Items . Count = = 0 )
2024-03-19 21:55:55 -04:00
return true ;
var watch = new InternalStopwatch ( "Applying extra mappings to DAT" ) ;
try
{
// Bucket by game first
2025-01-14 20:21:54 -05:00
datFile . BucketBy ( ItemKey . Machine ) ;
2024-03-19 21:55:55 -04:00
// Create mappings based on the extra items
var combinedMaps = CombineExtras ( ) ;
var games = combinedMaps . Keys ;
// Apply the mappings
foreach ( string game in games )
{
// Get the list of DatItems for the machine
2025-01-12 23:15:30 -05:00
var datItems = datFile . GetItemsForBucketDB ( game ) ;
2024-03-19 21:55:55 -04:00
if ( datItems = = null )
continue ;
// Try to get the map values, if possible
combinedMaps . TryGetValue ( game , out var mappings ) ;
// Create a setter with the new mappings
var setter = new Setter ( ) ;
setter . PopulateSettersFromDictionary ( mappings ) ;
// Loop through and set the fields accordingly
foreach ( var datItem in datItems )
{
2024-12-06 23:16:09 -05:00
var machine = datFile . ItemsDB . GetMachineForItem ( datItem . Key ) ;
if ( machine . Value ! = null )
setter . SetFields ( machine . Value ) ;
2024-03-19 21:55:55 -04:00
2024-12-06 23:16:09 -05:00
setter . SetFields ( datItem . Value ) ;
2024-03-19 21:55:55 -04:00
}
}
}
catch ( Exception ex ) when ( ! throwOnError )
{
2025-01-08 16:59:44 -05:00
_logger . Error ( ex ) ;
2024-03-19 21:55:55 -04:00
return false ;
}
finally
{
2021-02-02 14:09:49 -08:00
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-10-24 03:16:45 -04:00
private Dictionary < string , Dictionary < FilterKey , string > > CombineExtras ( )
2024-03-05 17:17:40 -05:00
{
2024-10-24 03:16:45 -04:00
var machineMap = new Dictionary < string , Dictionary < FilterKey , 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-10-24 02:47:30 -04:00
machineMap [ machineName ] [ item . Key ] = 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
}
}