Files
SabreTools/SabreTools.Filtering/ExtraIniItem.cs

108 lines
3.7 KiB
C#
Raw Normal View History

2020-08-21 10:15:38 -07:00
using System;
using System.Collections.Generic;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-09 23:11:10 -08:00
using SabreTools.IO.Readers;
2020-12-07 14:29:45 -08:00
using SabreTools.Logging;
2020-08-21 10:15:38 -07:00
2020-12-08 13:48:57 -08:00
namespace SabreTools.Filtering
2020-08-21 10:15:38 -07:00
{
public class ExtraIniItem
{
#region Fields
/// <summary>
2020-12-13 13:22:06 -08:00
/// MachineField to update with INI information
2020-08-21 10:15:38 -07:00
/// </summary>
2020-12-13 13:22:06 -08:00
public MachineField MachineField { get; set; } = MachineField.NULL;
/// <summary>
/// DatItemField to update with INI information
/// </summary>
public DatItemField DatItemField { get; set; } = DatItemField.NULL;
2020-08-21 10:15:38 -07:00
/// <summary>
2021-02-01 14:57:01 -08:00
/// Mappings from machine names to value
2020-08-21 10:15:38 -07:00
/// </summary>
2021-02-01 14:57:01 -08:00
public Dictionary<string, string> Mappings { get; set; } = new Dictionary<string, string>();
2020-08-21 10:15:38 -07:00
#endregion
#region Extras Population
/// <summary>
/// Populate the dictionary from an INI file
/// </summary>
/// <param name="ini">Path to INI file to populate from</param>
/// <remarks>
/// The INI file format that is supported here is not exactly the same
/// as a traditional one. This expects a MAME extras format, which usually
/// doesn't contain key value pairs and always at least contains one section
/// called `ROOT_FOLDER`. If that's the name of a section, then we assume
/// the value is boolean. If there's another section name, then that is set
/// as the value instead.
/// </remarks>
2020-08-21 10:54:51 -07:00
public bool PopulateFromFile(string ini)
2020-08-21 10:15:38 -07:00
{
// Prepare all intenral variables
IniReader ir = new(ini) { ValidateRows = false };
2020-08-21 10:15:38 -07:00
bool foundRootFolder = false;
// If we got a null reader, just return
if (ir == null)
2020-08-21 10:54:51 -07:00
return false;
2020-08-21 10:15:38 -07:00
// Otherwise, read the file to the end
try
{
while (!ir.EndOfStream)
{
2020-09-21 10:23:57 -07:00
// Read in the next line and process
ir.ReadNextLine();
2020-08-21 10:15:38 -07:00
// We don't care about whitespace or comments
if (ir.RowType == IniRowType.None || ir.RowType == IniRowType.Comment)
continue;
// If we have a section, just read it in
if (ir.RowType == IniRowType.SectionHeader)
{
// If we've found the start of the extras, set the flag
if (string.Equals(ir.Section, "ROOT_FOLDER", StringComparison.OrdinalIgnoreCase))
foundRootFolder = true;
continue;
}
// If we have a value, then we start populating the dictionary
else if (foundRootFolder)
{
2021-02-01 14:57:01 -08:00
// Get the value and machine name
string value = ir.Section;
string machineName = ir.CurrentLine.Trim();
2020-08-21 10:15:38 -07:00
// If the section is "ROOT_FOLDER", then we use the value "true" instead.
// This is done because some INI files use the name of the file as the
// category to be assigned to the items included.
2021-02-01 14:57:01 -08:00
if (value == "ROOT_FOLDER")
value = "true";
2020-08-21 10:15:38 -07:00
// Add the new mapping
2021-02-01 14:57:01 -08:00
Mappings[machineName] = value;
2020-08-21 10:15:38 -07:00
}
}
}
catch (Exception ex)
{
LoggerImpl.Warning(ex, $"Exception found while parsing '{ini}'");
2020-08-21 10:54:51 -07:00
return false;
2020-08-21 10:15:38 -07:00
}
ir.Dispose();
2020-08-21 10:54:51 -07:00
return true;
2020-08-21 10:15:38 -07:00
}
#endregion
}
}