using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using SabreTools.Library.DatFiles; using SabreTools.Library.DatItems; using SabreTools.Library.FileTypes; using SabreTools.Library.Reports; namespace SabreTools.Library.Tools { public static class Converters { #region Enum to Enum /// /// Get the field associated with each hash type /// public static Field AsField(this Hash hash) { switch (hash) { case Hash.CRC: return Field.DatItem_CRC; case Hash.MD5: return Field.DatItem_MD5; #if NET_FRAMEWORK case Hash.RIPEMD160: return Field.DatItem_RIPEMD160; #endif case Hash.SHA1: return Field.DatItem_SHA1; case Hash.SHA256: return Field.DatItem_SHA256; case Hash.SHA384: return Field.DatItem_SHA384; case Hash.SHA512: return Field.DatItem_SHA512; default: return Field.NULL; } } /// /// Get the default OutputFormat associated with each PackingFlag /// /// /// public static OutputFormat AsOutputFormat(this PackingFlag packing) { switch (packing) { case PackingFlag.Zip: return OutputFormat.TorrentZip; case PackingFlag.Unzip: case PackingFlag.Partial: return OutputFormat.Folder; case PackingFlag.Flat: return OutputFormat.ParentFolder; case PackingFlag.None: default: return OutputFormat.Folder; } } #endregion #region String to Enum /// /// Get DatFormat value from input string /// /// String to get value from /// DatFormat value corresponding to the string public static DatFormat AsDatFormat(this string input) { switch (input?.Trim().ToLowerInvariant()) { case "all": return DatFormat.ALL; case "am": case "attractmode": return DatFormat.AttractMode; case "cmp": case "clrmamepro": return DatFormat.ClrMamePro; case "csv": return DatFormat.CSV; case "dc": case "doscenter": return DatFormat.DOSCenter; case "json": return DatFormat.Json; case "lr": case "listrom": return DatFormat.Listrom; case "lx": case "listxml": return DatFormat.Listxml; case "md5": return DatFormat.RedumpMD5; case "miss": case "missfile": return DatFormat.MissFile; case "msx": case "openmsx": return DatFormat.OpenMSX; case "ol": case "offlinelist": return DatFormat.OfflineList; case "rc": case "romcenter": return DatFormat.RomCenter; #if NET_FRAMEWORK case "ripemd160": return DatFormat.RedumpRIPEMD160; #endif case "sd": case "sabredat": return DatFormat.SabreDat; case "sfv": return DatFormat.RedumpSFV; case "sha1": return DatFormat.RedumpSHA1; case "sha256": return DatFormat.RedumpSHA256; case "sha384": return DatFormat.RedumpSHA384; case "sha512": return DatFormat.RedumpSHA512; case "sl": case "softwarelist": return DatFormat.SoftwareList; case "smdb": case "everdrive": return DatFormat.EverdriveSMDB; case "ssv": return DatFormat.SSV; case "tsv": return DatFormat.TSV; case "xml": case "logiqx": return DatFormat.Logiqx; default: return 0x0; } } /// /// Get Field value from input string /// /// String to get value from /// Field value corresponding to the string public static Field AsField(this string input) { // If the input is null, we return null if (input == null) return Field.NULL; // Normalize the input input = input.ToLowerInvariant(); // Create regex strings string headerRegex = @"^(dat|header|datheader)[.-_\s]"; string machineRegex = @"^(game|machine)[.-_\s]"; string datItemRegex = @"^(item|datitem|archive|biosset|blank|disk|release|rom|sample)[.-_\s]"; // If we have a header field if (Regex.IsMatch(input, headerRegex)) { // Replace the match and re-normalize string headerInput = Regex.Replace(input, headerRegex, string.Empty) .Replace(' ', '_') .Replace('-', '_') .Replace('.', '_'); switch (headerInput) { #region Common case "file": case "filename": case "file_name": return Field.DatHeader_FileName; case "dat": case "datname": case "dat_name": case "internalname": case "internal_name": return Field.DatHeader_Name; case "desc": case "description": return Field.DatHeader_Description; case "root": case "rootdir": case "root_dir": case "rootdirectory": case "root_directory": return Field.DatHeader_RootDir; case "category": return Field.DatHeader_Category; case "version": return Field.DatHeader_Version; case "date": case "timestamp": case "time_stamp": return Field.DatHeader_Date; case "author": return Field.DatHeader_Author; case "email": case "e_mail": return Field.DatHeader_Email; case "homepage": case "home_page": return Field.DatHeader_Homepage; case "url": return Field.DatHeader_Url; case "comment": return Field.DatHeader_Comment; case "header": case "headerskipper": case "header_skipper": case "skipper": return Field.DatHeader_HeaderSkipper; case "dattype": case "type": case "superdat": return Field.DatHeader_Type; case "forcemerging": case "force_merging": return Field.DatHeader_ForceMerging; case "forcenodump": case "force_nodump": return Field.DatHeader_ForceNodump; case "forcepacking": case "force_packing": return Field.DatHeader_ForcePacking; #endregion #region ListXML case "debug": return Field.DatHeader_Debug; case "mameconfig": case "mame_config": return Field.DatHeader_MameConfig; #endregion #region Logiqx case "build": return Field.DatHeader_Build; case "rommode": case "rom_mode": return Field.DatHeader_RomMode; case "biosmode": case "bios_mode": return Field.DatHeader_BiosMode; case "samplemode": case "sample_mode": return Field.DatHeader_SampleMode; case "lockrommode": case "lockrom_mode": case "lock_rommode": case "lock_rom_mode": return Field.DatHeader_LockRomMode; case "lockbiosmode": case "lockbios_mode": case "lock_biosmode": case "lock_bios_mode": return Field.DatHeader_LockBiosMode; case "locksamplemode": case "locksample_mode": case "lock_samplemode": case "lock_sample_mode": return Field.DatHeader_LockSampleMode; #endregion #region OfflineList case "system": case "plugin": // Used with RomCenter return Field.DatHeader_System; case "screenshotwidth": case "screenshotswidth": case "screenshot_width": case "screenshots_width": return Field.DatHeader_ScreenshotsWidth; case "screenshotheight": case "screenshotsheight": case "screenshot_height": case "screenshots_height": return Field.DatHeader_ScreenshotsHeight; case "info": case "infos": return Field.DatHeader_Infos; case "info_name": case "infos_name": return Field.DatHeader_Info_Name; case "info_visible": case "infos_visible": return Field.DatHeader_Info_Visible; case "info_isnamingoption": case "info_is_naming_option": case "infos_isnamingoption": case "infos_is_naming_option": return Field.DatHeader_Info_IsNamingOption; case "info_default": case "infos_default": return Field.DatHeader_Info_Default; case "canopen": case "can_open": return Field.DatHeader_CanOpen; case "romtitle": case "rom_title": return Field.DatHeader_RomTitle; #endregion #region RomCenter case "rcversion": case "rc_version": case "romcenterversion": case "romcenter_version": case "rom_center_version": return Field.DatHeader_RomCenterVersion; #endregion } } // If we have a machine field else if (Regex.IsMatch(input, machineRegex)) { // Replace the match and re-normalize string machineInput = Regex.Replace(input, machineRegex, string.Empty) .Replace(' ', '_') .Replace('-', '_') .Replace('.', '_'); switch (machineInput) { #region Common case "name": return Field.Machine_Name; case "comment": case "extra": // Used with AttractMode return Field.Machine_Comment; case "desc": case "description": return Field.Machine_Description; case "year": return Field.Machine_Year; case "manufacturer": return Field.Machine_Manufacturer; case "publisher": return Field.Machine_Publisher; case "category": return Field.Machine_Category; case "romof": case "rom_of": return Field.Machine_RomOf; case "cloneof": case "clone_of": return Field.Machine_CloneOf; case "sampleof": case "sample_of": return Field.Machine_SampleOf; case "type": return Field.Machine_Type; #endregion #region AttractMode case "players": return Field.Machine_Players; case "rotation": return Field.Machine_Rotation; case "control": return Field.Machine_Control; case "amstatus": case "am_status": case "gamestatus": case "supportstatus": case "support_status": return Field.Machine_Status; case "displaycount": return Field.Machine_DisplayCount; case "displaytype": return Field.Machine_DisplayType; case "buttons": return Field.Machine_Buttons; #endregion #region ListXML case "sourcefile": case "source_file": return Field.Machine_SourceFile; case "runnable": return Field.Machine_Runnable; case "displays": return Field.Machine_Displays; case "display_tag": return Field.Machine_Display_Tag; case "display_type": return Field.Machine_Display_Type; case "display_rotate": return Field.Machine_Display_Rotate; case "display_flipx": return Field.Machine_Display_FlipX; case "display_width": return Field.Machine_Display_Width; case "display_height": return Field.Machine_Display_Height; case "display_refresh": return Field.Machine_Display_Refresh; case "display_pixclock": return Field.Machine_Display_PixClock; case "display_htotal": return Field.Machine_Display_HTotal; case "display_hbend": return Field.Machine_Display_HBEnd; case "display_hbstart": return Field.Machine_Display_HBStart; case "display_vtotal": return Field.Machine_Display_VTotal; case "display_vbend": return Field.Machine_Display_VBEnd; case "display_vbstart": return Field.Machine_Display_VBStart; case "sounds": return Field.Machine_Sounds; case "sound_channels": return Field.Machine_Sound_Channels; case "conditions": return Field.Machine_Conditions; case "condition_tag": return Field.Machine_Condition_Tag; case "condition_mask": return Field.Machine_Condition_Mask; case "condition_relation": return Field.Machine_Condition_Relation; case "condition_value": return Field.Machine_Condition_Value; case "inputs": return Field.Machine_Inputs; case "input_service": return Field.Machine_Input_Service; case "input_tilt": return Field.Machine_Input_Tilt; case "input_players": return Field.Machine_Input_Players; case "input_coins": return Field.Machine_Input_Coins; case "input_controls": return Field.Machine_Input_Controls; case "input_control_type": return Field.Machine_Input_Control_Type; case "input_control_player": return Field.Machine_Input_Control_Player; case "input_control_buttons": return Field.Machine_Input_Control_Buttons; case "input_control_regbuttons": return Field.Machine_Input_Control_RegButtons; case "input_control_minimum": return Field.Machine_Input_Control_Minimum; case "input_control_maximum": return Field.Machine_Input_Control_Maximum; case "input_control_sensitivity": return Field.Machine_Input_Control_Sensitivity; case "input_control_keydelta": return Field.Machine_Input_Control_KeyDelta; case "input_control_reverse": return Field.Machine_Input_Control_Reverse; case "input_control_ways": return Field.Machine_Input_Control_Ways; case "input_control_ways2": return Field.Machine_Input_Control_Ways2; case "input_control_ways3": return Field.Machine_Input_Control_Ways3; case "dipswitches": return Field.Machine_DipSwitches; case "dipswitch_name": return Field.Machine_DipSwitch_Name; case "dipswitch_tag": return Field.Machine_DipSwitch_Tag; case "dipswitch_mask": return Field.Machine_DipSwitch_Mask; case "dipswitch_locations": return Field.Machine_DipSwitch_Locations; case "dipswitch_location_name": return Field.Machine_DipSwitch_Location_Name; case "dipswitch_location_number": return Field.Machine_DipSwitch_Location_Number; case "dipswitch_location_inverted": return Field.Machine_DipSwitch_Location_Inverted; case "dipswitch_values": return Field.Machine_DipSwitch_Values; case "dipswitch_value_name": return Field.Machine_DipSwitch_Value_Name; case "dipswitch_value_value": return Field.Machine_DipSwitch_Value_Value; case "dipswitch_value_default": return Field.Machine_DipSwitch_Value_Default; case "configurations": return Field.Machine_Configurations; case "configuration_name": return Field.Machine_Configuration_Name; case "configuration_tag": return Field.Machine_Configuration_Tag; case "configuration_mask": return Field.Machine_Configuration_Mask; case "configuration_locations": return Field.Machine_Configuration_Locations; case "configuration_location_name": return Field.Machine_Configuration_Location_Name; case "configuration_location_number": return Field.Machine_Configuration_Location_Number; case "configuration_location_inverted": return Field.Machine_Configuration_Location_Inverted; case "configuration_settings": return Field.Machine_Configuration_Settings; case "configuration_setting_name": return Field.Machine_Configuration_Setting_Name; case "configuration_setting_value": return Field.Machine_Configuration_Setting_Value; case "configuration_setting_default": return Field.Machine_Configuration_Setting_Default; case "ports": return Field.Machine_Ports; case "port_tag": return Field.Machine_Port_Tag; case "port_analogs": return Field.Machine_Port_Analogs; case "port_analog_mask": return Field.Machine_Port_Analog_Mask; case "drivers": return Field.Machine_Drivers; case "driver_status": return Field.Machine_Driver_Status; case "driver_emulation": return Field.Machine_Driver_Emulation; case "driver_cocktail": return Field.Machine_Driver_Cocktail; case "driver_savestate": return Field.Machine_Driver_SaveState; case "features": return Field.Machine_Features; case "feature_type": return Field.Machine_Feature_Type; case "feature_status": return Field.Machine_Feature_Status; case "feature_overall": return Field.Machine_Feature_Overall; case "devices": return Field.Machine_Devices; case "device_type": return Field.Machine_Device_Type; case "device_tag": return Field.Machine_Device_Tag; case "device_fixedimage": return Field.Machine_Device_FixedImage; case "device_mandatory": return Field.Machine_Device_Mandatory; case "device_interface": return Field.Machine_Device_Interface; case "device_instances": return Field.Machine_Device_Instances; case "device_instance_name": return Field.Machine_Device_Instance_Name; case "device_instance_briefname": return Field.Machine_Device_Instance_BriefName; case "device_extensions": return Field.Machine_Device_Extensions; case "device_extension_name": return Field.Machine_Device_Extension_Name; case "slots": return Field.Machine_Slots; case "slot_name": return Field.Machine_Slot_Name; case "slot_slotoptions": return Field.Machine_Slot_SlotOptions; case "slot_slotoption_name": return Field.Machine_Slot_SlotOption_Name; case "slot_slotoption_devicename": return Field.Machine_Slot_SlotOption_DeviceName; case "slot_slotoption_default": return Field.Machine_Slot_SlotOption_Default; #endregion #region Logiqx case "board": return Field.Machine_Board; case "rebuildto": case "rebuild_to": return Field.Machine_RebuildTo; #endregion #region Logiqx EmuArc case "titleid": case "title_id": return Field.Machine_TitleID; case "developer": return Field.Machine_Developer; case "genre": return Field.Machine_Genre; case "subgenre": case "sub_genre": return Field.Machine_Subgenre; case "ratings": return Field.Machine_Ratings; case "score": return Field.Machine_Score; case "enabled": return Field.Machine_Enabled; case "crc": case "hascrc": case "has_crc": return Field.Machine_CRC; case "relatedto": case "related_to": return Field.Machine_RelatedTo; #endregion #region OpenMSX case "genmsxid": case "genmsx_id": case "gen_msxid": case "gen_msx_id": return Field.Machine_GenMSXID; case "system": case "msxsystem": case "msx_system": return Field.Machine_System; case "country": return Field.Machine_Country; #endregion #region SoftwareList case "supported": return Field.Machine_Supported; case "infos": return Field.Machine_Infos; case "info_name": return Field.Machine_Info_Name; case "info_value": return Field.Machine_Info_Value; case "sharedfeatures": return Field.Machine_SharedFeatures; case "sharedfeature_name": return Field.Machine_SharedFeature_Name; case "sharedfeature_value": return Field.Machine_SharedFeature_Value; #endregion } } // If we have a datitem field else if (Regex.IsMatch(input, datItemRegex)) { // Replace the match and re-normalize string itemInput = Regex.Replace(input, datItemRegex, string.Empty) .Replace(' ', '_') .Replace('-', '_') .Replace('.', '_'); switch (itemInput) { #region Common case "name": return Field.DatItem_Name; case "type": return Field.DatItem_Type; #endregion #region AttractMode case "altname": case "alt name": case "alt-name": case "altromname": case "alt romname": case "alt-romname": return Field.DatItem_AltName; case "alttitle": case "alt title": case "alt-title": case "altromtitle": case "alt romtitle": case "alt-romtitle": return Field.DatItem_AltTitle; #endregion #region OpenMSX case "original": return Field.DatItem_Original; case "subtype": case "sub_type": case "openmsxsubtype": case "openmsx_subtype": return Field.DatItem_OpenMSXSubType; case "openmsxtype": case "openmsx_type": return Field.DatItem_OpenMSXType; case "remark": return Field.DatItem_Remark; case "boot": return Field.DatItem_Boot; #endregion #region SoftwareList case "part": return Field.DatItem_Part; case "partname": case "part_name": return Field.DatItem_Part_Name; case "partinterface": case "part_interface": return Field.DatItem_Part_Interface; case "features": return Field.DatItem_Features; case "feature_name": return Field.DatItem_Feature_Name; case "feature_value": return Field.DatItem_Feature_Value; case "areaname": case "area_name": return Field.DatItem_AreaName; case "areasize": case "area_size": return Field.DatItem_AreaSize; case "areawidth": case "area_width": return Field.DatItem_AreaWidth; case "areaendinanness": case "area_endianness": return Field.DatItem_AreaEndianness; case "value": return Field.DatItem_Value; case "loadflag": case "load_flag": return Field.DatItem_LoadFlag; #endregion #region Item-Specific #region Actionable // Rom case "bios": return Field.DatItem_Bios; case "size": return Field.DatItem_Size; case "crc": case "crc32": return Field.DatItem_CRC; case "md5": case "md5_hash": return Field.DatItem_MD5; #if NET_FRAMEWORK case "ripemd160": case "ripemd160_hash": return Field.DatItem_RIPEMD160; #endif case "sha1": case "sha_1": case "sha1hash": case "sha1_hash": case "sha_1hash": case "sha_1_hash": return Field.DatItem_SHA1; case "sha256": case "sha_256": case "sha256hash": case "sha256_hash": case "sha_256hash": case "sha_256_hash": return Field.DatItem_SHA256; case "sha384": case "sha_384": case "sha384hash": case "sha384_hash": case "sha_384hash": case "sha_384_hash": return Field.DatItem_SHA384; case "sha512": case "sha_512": case "sha512hash": case "sha512_hash": case "sha_512hash": case "sha_512_hash": return Field.DatItem_SHA512; case "merge": case "mergetag": case "merge_tag": return Field.DatItem_Merge; case "region": return Field.DatItem_Region; case "offset": return Field.DatItem_Offset; case "date": return Field.DatItem_Date; case "status": return Field.DatItem_Status; case "optional": return Field.DatItem_Optional; case "inverted": return Field.DatItem_Inverted; // Disk case "index": return Field.DatItem_Index; case "writable": return Field.DatItem_Writable; #endregion #region Auxiliary // Adjuster case "default": return Field.DatItem_Default; case "conditions": return Field.DatItem_Conditions; case "condition_tag": return Field.DatItem_Condition_Tag; case "condition_mask": return Field.DatItem_Condition_Mask; case "condition_relation": return Field.DatItem_Condition_Relation; case "condition_value": return Field.DatItem_Condition_Value; // BiosSet case "description": case "biosdescription": case "bios_description": return Field.DatItem_Description; // Chip case "tag": return Field.DatItem_Tag; case "chiptype": case "chip_type": return Field.DatItem_ChipType; case "clock": return Field.DatItem_Clock; // Ram Option case "content": return Field.DatItem_Content; // Release case "language": return Field.DatItem_Language; // Software List case "softwareliststatus": case "softwarelist_status": return Field.DatItem_SoftwareListStatus; case "filter": return Field.DatItem_Filter; #endregion #endregion // Item-Specific } } // Else, we fall back on the old matching // TODO: Remove this entirely switch (input.Replace(' ', '_').Replace('-', '_').Replace('.', '_')) { #region Machine #region Common case "game": case "gamename": case "game-name": case "machine": case "machinename": case "machine-name": return Field.Machine_Name; case "comment": case "extra": return Field.Machine_Comment; case "desc": case "description": case "gamedesc": case "gamedescription": case "game-description": case "game description": case "machinedesc": case "machinedescription": case "machine-description": case "machine description": return Field.Machine_Description; case "year": return Field.Machine_Year; case "manufacturer": return Field.Machine_Manufacturer; case "publisher": return Field.Machine_Publisher; case "category": case "gamecategory": case "game-category": case "machinecategory": case "machine-category": return Field.Machine_Category; case "romof": return Field.Machine_RomOf; case "cloneof": return Field.Machine_CloneOf; case "sampleof": return Field.Machine_SampleOf; case "gametype": case "game type": case "game-type": case "machinetype": case "machine type": case "machine-type": return Field.Machine_Type; #endregion #region AttractMode case "players": return Field.Machine_Players; case "rotation": return Field.Machine_Rotation; case "control": return Field.Machine_Control; case "amstatus": case "am-status": case "gamestatus": case "game-status": case "machinestatus": case "machine-status": case "supportstatus": case "support-status": return Field.Machine_Status; case "displaycount": case "display-count": case "displays": return Field.Machine_DisplayCount; case "displaytype": case "display-type": return Field.Machine_DisplayType; case "buttons": return Field.Machine_Buttons; #endregion #region ListXML case "sourcefile": case "source file": case "source-file": return Field.Machine_SourceFile; case "runnable": return Field.Machine_Runnable; case "slotoptions": case "slot options": case "slot-options": return Field.Machine_Slots; case "infos": return Field.Machine_Infos; #endregion #region Logiqx case "board": return Field.Machine_Board; case "rebuildto": case "rebuild to": case "rebuild-to": return Field.Machine_RebuildTo; #endregion #region Logiqx EmuArc case "titleid": case "title id": case "title-id": return Field.Machine_TitleID; case "developer": return Field.Machine_Developer; case "genre": return Field.Machine_Genre; case "subgenre": return Field.Machine_Subgenre; case "ratings": return Field.Machine_Ratings; case "score": return Field.Machine_Score; case "enabled": return Field.Machine_Enabled; case "hascrc": case "has crc": case "has-crc": return Field.Machine_CRC; case "relatedto": case "related to": case "related-to": return Field.Machine_RelatedTo; #endregion #region OpenMSX case "genmsxid": case "genmsx id": case "genmsx-id": case "gen msx id": case "gen-msx-id": return Field.Machine_GenMSXID; case "system": case "msxsystem": case "msx system": case "msx-system": return Field.Machine_System; case "country": return Field.Machine_Country; #endregion #region SoftwareList case "supported": return Field.Machine_Supported; case "sharedfeat": case "shared feat": case "shared-feat": case "sharedfeature": case "shared feature": case "shared-feature": case "sharedfeatures": case "shared features": case "shared-features": return Field.Machine_SharedFeatures; case "dipswitch": case "dip switch": case "dip-switch": case "dipswitches": case "dip switches": case "dip-switches": return Field.Machine_DipSwitches; #endregion #endregion // Machine #region DatItem #region Common case "itemname": case "item-name": case "name": return Field.DatItem_Name; case "itemtype": case "item-type": case "type": return Field.DatItem_Type; #endregion #region AttractMode case "altname": case "alt name": case "alt-name": case "altromname": case "alt romname": case "alt-romname": return Field.DatItem_AltName; case "alttitle": case "alt title": case "alt-title": case "altromtitle": case "alt romtitle": case "alt-romtitle": return Field.DatItem_AltTitle; #endregion #region OpenMSX case "original": return Field.DatItem_Original; case "subtype": case "sub type": case "sub-type": case "openmsx_subtype": return Field.DatItem_OpenMSXSubType; case "openmsx_type": return Field.DatItem_OpenMSXType; case "remark": return Field.DatItem_Remark; case "boot": return Field.DatItem_Boot; #endregion #region SoftwareList case "partname": case "part name": case "part-name": return Field.DatItem_Part_Name; case "partinterface": case "part interface": case "part-interface": return Field.DatItem_Part_Interface; case "features": return Field.DatItem_Features; case "areaname": case "area name": case "area-name": return Field.DatItem_AreaName; case "areasize": case "area size": case "area-size": return Field.DatItem_AreaSize; case "areawidth": case "area width": case "area-width": return Field.DatItem_AreaWidth; case "areaendinanness": case "area endianness": case "area-endianness": return Field.DatItem_AreaEndianness; case "value": return Field.DatItem_Value; case "loadflag": case "load flag": case "load-flag": return Field.DatItem_LoadFlag; #endregion #region Item-Specific #region Actionable // Rom case "bios": return Field.DatItem_Bios; case "equal": case "greater": case "less": case "size": return Field.DatItem_Size; case "crc": case "crc32": return Field.DatItem_CRC; case "md5": case "md5_hash": return Field.DatItem_MD5; #if NET_FRAMEWORK case "ripemd160": case "ripemd160_hash": return Field.DatItem_RIPEMD160; #endif case "sha1": case "sha_1": case "sha1hash": case "sha1_hash": case "sha_1hash": case "sha_1_hash": return Field.DatItem_SHA1; case "sha256": case "sha_256": case "sha256hash": case "sha256_hash": case "sha_256hash": case "sha_256_hash": return Field.DatItem_SHA256; case "sha384": case "sha_384": case "sha384hash": case "sha384_hash": case "sha_384hash": case "sha_384_hash": return Field.DatItem_SHA384; case "sha512": case "sha_512": case "sha512hash": case "sha512_hash": case "sha_512hash": case "sha_512_hash": return Field.DatItem_SHA512; case "merge": case "mergetag": case "merge_tag": return Field.DatItem_Merge; case "region": return Field.DatItem_Region; case "offset": return Field.DatItem_Offset; case "date": return Field.DatItem_Date; case "itemtatus": case "item-status": case "status": return Field.DatItem_Status; case "optional": return Field.DatItem_Optional; case "inverted": return Field.DatItem_Inverted; // Disk case "index": return Field.DatItem_Index; case "writable": return Field.DatItem_Writable; #endregion #region Auxiliary // Adjuster case "default": return Field.DatItem_Default; case "conditions": return Field.DatItem_Conditions; case "condition_tag": return Field.DatItem_Condition_Tag; case "condition_mask": return Field.DatItem_Condition_Mask; case "condition_relation": return Field.DatItem_Condition_Relation; case "condition_value": return Field.DatItem_Condition_Value; // BiosSet case "biosdescription": case "bios-description": case "biossetdescription": case "biosset-description": case "bios-set-description": return Field.DatItem_Description; // Chip case "tag": return Field.DatItem_Tag; case "chiptype": case "chip_type": return Field.DatItem_ChipType; case "clock": return Field.DatItem_Clock; // Ram Option case "content": return Field.DatItem_Content; // Release case "language": return Field.DatItem_Language; #endregion #endregion // Item-Specific #endregion default: return Field.NULL; } } /// /// Get ItemStatus value from input string /// /// String to get value from /// ItemStatus value corresponding to the string public static ItemStatus AsItemStatus(this string status) { #if NET_FRAMEWORK switch (status?.ToLowerInvariant()) { case "good": return ItemStatus.Good; case "baddump": return ItemStatus.BadDump; case "nodump": case "yes": return ItemStatus.Nodump; case "verified": return ItemStatus.Verified; case "none": case "no": default: return ItemStatus.None; } #else return status?.ToLowerInvariant() switch { "good" => ItemStatus.Good, "baddump" => ItemStatus.BadDump, "nodump" => ItemStatus.Nodump, "yes" => ItemStatus.Nodump, "verified" => ItemStatus.Verified, "none" => ItemStatus.None, "no" => ItemStatus.None, _ => ItemStatus.None, }; #endif } /// /// Get ItemType? value from input string /// /// String to get value from /// ItemType? value corresponding to the string public static ItemType? AsItemType(this string itemType) { #if NET_FRAMEWORK switch (itemType?.ToLowerInvariant()) { case "adjuster": return ItemType.Adjuster; case "archive": return ItemType.Archive; case "biosset": return ItemType.BiosSet; case "blank": return ItemType.Blank; case "chip": return ItemType.Chip; case "device_ref": return ItemType.DeviceReference; case "disk": return ItemType.Disk; case "media": return ItemType.Media; case "ramoption": return ItemType.RamOption; case "release": return ItemType.Release; case "rom": return ItemType.Rom; case "sample": return ItemType.Sample; case "softwarelist": return ItemType.SoftwareList; default: return null; } #else return itemType?.ToLowerInvariant() switch { "adjuster" => ItemType.Adjuster, "archive" => ItemType.Archive, "biosset" => ItemType.BiosSet, "blank" => ItemType.Blank, "chip" => ItemType.Chip, "device_ref" => ItemType.DeviceReference, "disk" => ItemType.Disk, "media" => ItemType.Media, "ramoption" => ItemType.RamOption, "release" => ItemType.Release, "rom" => ItemType.Rom, "sample" => ItemType.Sample, "softwarelist" => ItemType.SoftwareList, _ => null, }; #endif } /// /// Get MachineType value from input string /// /// String to get value from /// MachineType value corresponding to the string public static MachineType AsMachineType(this string gametype) { #if NET_FRAMEWORK switch (gametype?.ToLowerInvariant()) { case "bios": return MachineType.Bios; case "dev": case "device": return MachineType.Device; case "mech": case "mechanical": return MachineType.Mechanical; case "none": default: return MachineType.NULL; } #else return gametype?.ToLowerInvariant() switch { "bios" => MachineType.Bios, "dev" => MachineType.Device, "device" => MachineType.Device, "mech" => MachineType.Mechanical, "mechanical" => MachineType.Mechanical, "none" => MachineType.NULL, _ => MachineType.NULL, }; #endif } /// /// Get MergingFlag value from input string /// /// String to get value from /// MergingFlag value corresponding to the string public static MergingFlag AsMergingFlag(this string merging) { #if NET_FRAMEWORK switch (merging?.ToLowerInvariant()) { case "split": return MergingFlag.Split; case "merged": return MergingFlag.Merged; case "nonmerged": case "unmerged": return MergingFlag.NonMerged; case "full": return MergingFlag.Full; case "device": return MergingFlag.Device; case "none": default: return MergingFlag.None; } #else return merging?.ToLowerInvariant() switch { "split" => MergingFlag.Split, "merged" => MergingFlag.Merged, "nonmerged" => MergingFlag.NonMerged, "unmerged" => MergingFlag.NonMerged, "full" => MergingFlag.Full, "none" => MergingFlag.None, _ => MergingFlag.None, }; #endif } /// /// Get NodumpFlag value from input string /// /// String to get value from /// NodumpFlag value corresponding to the string public static NodumpFlag AsNodumpFlag(this string nodump) { #if NET_FRAMEWORK switch (nodump?.ToLowerInvariant()) { case "obsolete": return NodumpFlag.Obsolete; case "required": return NodumpFlag.Required; case "ignore": return NodumpFlag.Ignore; case "none": default: return NodumpFlag.None; } #else return nodump?.ToLowerInvariant() switch { "obsolete" => NodumpFlag.Obsolete, "required" => NodumpFlag.Required, "ignore" => NodumpFlag.Ignore, "none" => NodumpFlag.None, _ => NodumpFlag.None, }; #endif } /// /// Get OpenMSXSubType value from input string /// /// String to get value from /// OpenMSXSubType value corresponding to the string public static OpenMSXSubType AsOpenMSXSubType(this string itemType) { #if NET_FRAMEWORK switch (itemType?.ToLowerInvariant()) { case "rom": return OpenMSXSubType.Rom; case "megarom": return OpenMSXSubType.MegaRom; case "sccpluscart": return OpenMSXSubType.SCCPlusCart; default: return OpenMSXSubType.NULL; } #else return itemType?.ToLowerInvariant() switch { "rom" => OpenMSXSubType.Rom, "megarom" => OpenMSXSubType.MegaRom, "sccpluscart" => OpenMSXSubType.SCCPlusCart, _ => OpenMSXSubType.NULL, }; #endif } /// /// Get PackingFlag value from input string /// /// String to get value from /// PackingFlag value corresponding to the string public static PackingFlag AsPackingFlag(this string packing) { #if NET_FRAMEWORK switch (packing?.ToLowerInvariant()) { case "yes": case "zip": return PackingFlag.Zip; case "no": case "unzip": return PackingFlag.Unzip; case "partial": return PackingFlag.Partial; case "flat": return PackingFlag.Flat; case "none": default: return PackingFlag.None; } #else return packing?.ToLowerInvariant() switch { "yes" => PackingFlag.Zip, "zip" => PackingFlag.Zip, "no" => PackingFlag.Unzip, "unzip" => PackingFlag.Unzip, "partial" => PackingFlag.Partial, "flat" => PackingFlag.Flat, "none" => PackingFlag.None, _ => PackingFlag.None, }; #endif } /// /// Get Runnable value from input string /// /// String to get value from /// Runnable value corresponding to the string public static Runnable AsRunnable(this string runnable) { #if NET_FRAMEWORK switch (runnable?.ToLowerInvariant()) { case "no": return Runnable.No; case "partial": return Runnable.Partial; case "yes": return Runnable.Yes; default: return Runnable.NULL; } #else return runnable?.ToLowerInvariant() switch { "no" => Runnable.No, "partial" => Runnable.Partial, "yes" => Runnable.Yes, _ => Runnable.NULL, }; #endif } /// /// Get SoftwareListStatus value from input string /// /// String to get value from /// SoftwareListStatus value corresponding to the string public static SoftwareListStatus AsSoftwareListStatus(this string status) { #if NET_FRAMEWORK switch (status?.ToLowerInvariant()) { case "original": return SoftwareListStatus.Original; case "compatible": return SoftwareListStatus.Compatible; case "none": default: return SoftwareListStatus.NULL; } #else return status?.ToLowerInvariant() switch { "original" => SoftwareListStatus.Original, "compatible" => SoftwareListStatus.Compatible, "none" => SoftwareListStatus.NULL, _ => SoftwareListStatus.NULL, }; #endif } /// /// Get StatReportFormat value from input string /// /// String to get value from /// StatReportFormat value corresponding to the string public static StatReportFormat AsStatReportFormat(this string input) { #if NET_FRAMEWORK switch (input?.Trim().ToLowerInvariant()) { case "all": return StatReportFormat.All; case "csv": return StatReportFormat.CSV; case "html": return StatReportFormat.HTML; case "ssv": return StatReportFormat.SSV; case "text": return StatReportFormat.Textfile; case "tsv": return StatReportFormat.TSV; default: return 0x0; } #else return input?.Trim().ToLowerInvariant() switch { "all" => StatReportFormat.All, "csv" => StatReportFormat.CSV, "html" => StatReportFormat.HTML, "ssv" => StatReportFormat.SSV, "text" => StatReportFormat.Textfile, "tsv" => StatReportFormat.TSV, _ => 0x0, }; #endif } /// /// Get Supported value from input string /// /// String to get value from /// Supported value corresponding to the string public static Supported AsSupported(this string supported) { #if NET_FRAMEWORK switch (supported?.ToLowerInvariant()) { case "no": return Supported.No; case "partial": return Supported.Partial; case "yes": return Supported.Yes; default: return Supported.NULL; } #else return supported?.ToLowerInvariant() switch { "no" => Supported.No, "partial" => Supported.Partial, "yes" => Supported.Yes, _ => Supported.NULL, }; #endif } /// /// Get bool? value from input string /// /// String to get value from /// bool? corresponding to the string public static bool? AsYesNo(this string yesno) { #if NET_FRAMEWORK switch (yesno?.ToLowerInvariant()) { case "yes": case "true": return true; case "no": case "false": return false; default: return null; } #else return yesno?.ToLowerInvariant() switch { "yes" => true, "true" => true, "no" => false, "false" => false, _ => null, }; #endif } #endregion #region Enum to String // TODO: DatFormat -> string // TODO: Field -> string /// /// Get string value from input ItemStatus /// /// ItemStatus to get value from /// True to use Yes/No format instead /// String value corresponding to the ItemStatus public static string FromItemStatus(this ItemStatus status, bool yesno) { #if NET_FRAMEWORK switch (status) { case ItemStatus.Good: return "good"; case ItemStatus.BadDump: return "baddump"; case ItemStatus.Nodump: return yesno ? "yes" : "nodump"; case ItemStatus.Verified: return "verified"; default: return null; } #else return status switch { ItemStatus.Good => "good", ItemStatus.BadDump => "baddump", ItemStatus.Nodump => yesno ? "yes" : "nodump", ItemStatus.Verified => "verified", _ => null, }; #endif } /// /// Get string value from input ItemType? /// /// ItemType? to get value from /// String value corresponding to the ItemType? public static string FromItemType(this ItemType? itemType) { #if NET_FRAMEWORK switch (itemType) { case ItemType.Adjuster: return "adjuster"; case ItemType.Archive: return "archive"; case ItemType.BiosSet: return "biosset"; case ItemType.Blank: return "blank"; case ItemType.Chip: return "chip"; case ItemType.DeviceReference: return "device_ref"; case ItemType.Disk: return "disk"; case ItemType.Media: return "media"; case ItemType.RamOption: return "ramoption"; case ItemType.Release: return "release"; case ItemType.Rom: return "rom"; case ItemType.Sample: return "sample"; case ItemType.SoftwareList: return "softwarelist"; default: return null; } #else return itemType switch { ItemType.Adjuster => "adjuster", ItemType.Archive => "archive", ItemType.BiosSet => "biosset", ItemType.Blank => "blank", ItemType.Chip => "chip", ItemType.DeviceReference => "device_ref", ItemType.Disk => "disk", ItemType.Media => "media", ItemType.RamOption => "ramoption", ItemType.Release => "release", ItemType.Rom => "rom", ItemType.Sample => "sample", ItemType.SoftwareList => "softwarelist", _ => null, }; #endif } /// /// Get string value from input MachineType /// /// MachineType to get value from /// True to use old naming instead /// String value corresponding to the MachineType public static string FromMachineType(this MachineType gametype, bool old) { #if NET_FRAMEWORK switch (gametype) { case MachineType.Bios: return "bios"; case MachineType.Device: return old ? "dev" : "device"; case MachineType.Mechanical: return old ? "mech" : "mechanical"; default: return null; } #else return gametype switch { MachineType.Bios => "bios", MachineType.Device => old ? "dev" : "device", MachineType.Mechanical => old ? "mech" : "mechanical", _ => null, }; #endif } /// /// Get string value from input MergingFlag /// /// MergingFlag to get value from /// True to use RomCenter naming instead /// String value corresponding to the MergingFlag public static string FromMergingFlag(this MergingFlag merging, bool romCenter) { #if NET_FRAMEWORK switch (merging) { case MergingFlag.Split: return "split"; case MergingFlag.Merged: return "merged"; case MergingFlag.NonMerged: return romCenter ? "unmerged" : "nonmerged"; case MergingFlag.Full: return "full"; case MergingFlag.Device: return "device"; default: return null; } #else return merging switch { MergingFlag.Split => "split", MergingFlag.Merged => "merged", MergingFlag.NonMerged => romCenter ? "unmerged" : "nonmerged", MergingFlag.Full => "full", MergingFlag.Device => "device", _ => null, }; #endif } /// /// Get string value from input NodumpFlag /// /// NodumpFlag to get value from /// String value corresponding to the NodumpFlag public static string FromNodumpFlag(this NodumpFlag nodump) { #if NET_FRAMEWORK switch (nodump) { case NodumpFlag.Obsolete: return "obsolete"; case NodumpFlag.Required: return "required"; case NodumpFlag.Ignore: return "ignore"; default: return null; } #else return nodump switch { NodumpFlag.Obsolete => "obsolete", NodumpFlag.Required => "required", NodumpFlag.Ignore => "ignore", _ => null, }; #endif } /// /// Get string value from input OpenMSXSubType /// /// OpenMSXSubType to get value from /// String value corresponding to the OpenMSXSubType public static string FromOpenMSXSubType(this OpenMSXSubType itemType) { #if NET_FRAMEWORK switch (itemType) { case OpenMSXSubType.Rom: return "rom"; case OpenMSXSubType.MegaRom: return "megarom"; case OpenMSXSubType.SCCPlusCart: return "sccpluscart"; default: return null; } #else return itemType switch { OpenMSXSubType.Rom => "rom", OpenMSXSubType.MegaRom => "megarom", OpenMSXSubType.SCCPlusCart => "sccpluscart", _ => null, }; #endif } /// /// Get string value from input OutputFormat /// /// OutputFormat to get value from /// String value corresponding to the OutputFormat public static string FromOutputFormat(this OutputFormat itemType) { #if NET_FRAMEWORK switch (itemType) { case OutputFormat.Folder: case OutputFormat.ParentFolder: return "directory"; case OutputFormat.TapeArchive: return "TAR"; case OutputFormat.Torrent7Zip: return "Torrent7Z"; case OutputFormat.TorrentGzip: case OutputFormat.TorrentGzipRomba: return "TorrentGZ"; case OutputFormat.TorrentLRZip: return "TorrentLRZ"; case OutputFormat.TorrentRar: return "TorrentRAR"; case OutputFormat.TorrentXZ: case OutputFormat.TorrentXZRomba: return "TorrentXZ"; case OutputFormat.TorrentZip: return "TorrentZip"; default: return null; } #else return itemType switch { OutputFormat.Folder => "directory", OutputFormat.ParentFolder => "directory", OutputFormat.TapeArchive => "TAR", OutputFormat.Torrent7Zip => "Torrent7Z", OutputFormat.TorrentGzip => "TorrentGZ", OutputFormat.TorrentGzipRomba => "TorrentGZ", OutputFormat.TorrentLRZip => "TorrentLRZ", OutputFormat.TorrentRar => "TorrentRAR", OutputFormat.TorrentXZ => "TorrentXZ", OutputFormat.TorrentXZRomba => "TorrentXZ", OutputFormat.TorrentZip => "TorrentZip", _ => null, }; #endif } /// /// Get string value from input PackingFlag /// /// PackingFlag to get value from /// True to use Yes/No format instead /// String value corresponding to the PackingFlag public static string FromPackingFlag(this PackingFlag packing, bool yesno) { #if NET_FRAMEWORK switch (packing) { case PackingFlag.Zip: return yesno ? "yes" : "zip"; case PackingFlag.Unzip: return yesno ? "no" : "unzip"; case PackingFlag.Partial: return "partial"; case PackingFlag.Flat: return "flat"; default: return null; } #else return packing switch { PackingFlag.Zip => yesno ? "yes" : "zip", PackingFlag.Unzip => yesno ? "yes" : "zip", PackingFlag.Partial => "partial", PackingFlag.Flat => "flat", _ => null, }; #endif } /// /// Get string value from input Runnable /// /// Runnable to get value from /// String value corresponding to the Runnable public static string FromRunnable(this Runnable runnable) { #if NET_FRAMEWORK switch (runnable) { case Runnable.No: return "no"; case Runnable.Partial: return "partial"; case Runnable.Yes: return "yes"; default: return null; } #else return runnable switch { Runnable.No => "no", Runnable.Partial => "partial", Runnable.Yes => "yes", _ => null, }; #endif } /// /// Get string value from input SoftwareListStatus /// /// SoftwareListStatus to get value from /// String value corresponding to the SoftwareListStatus public static string FromSoftwareListStatus(this SoftwareListStatus status) { #if NET_FRAMEWORK switch (status) { case SoftwareListStatus.Original: return "original"; case SoftwareListStatus.Compatible: return "compatible"; default: return null; } #else return status switch { SoftwareListStatus.Original => "original", SoftwareListStatus.Compatible => "compatible", _ => null, }; #endif } /// /// Get string value from input StatReportFormat /// /// StatReportFormat to get value from /// String value corresponding to the StatReportFormat public static string FromStatReportFormat(this StatReportFormat input) { #if NET_FRAMEWORK switch (input) { case StatReportFormat.All: return "all"; case StatReportFormat.CSV: return "csv"; case StatReportFormat.HTML: return "html"; case StatReportFormat.SSV: return "ssv"; case StatReportFormat.Textfile: return "text"; case StatReportFormat.TSV: return "tsv"; default: return null; } #else return input switch { StatReportFormat.All => "all", StatReportFormat.CSV => "csv", StatReportFormat.HTML => "html", StatReportFormat.SSV => "ssv", StatReportFormat.Textfile => "text", StatReportFormat.TSV => "tsv", _ => null, }; #endif } /// /// Get string value from input Supported /// /// Supported to get value from /// String value corresponding to the Supported public static string FromSupported(this Supported supported) { #if NET_FRAMEWORK switch (supported) { case Supported.No: return "no"; case Supported.Partial: return "partial"; case Supported.Yes: return "yes"; default: return null; } #else return supported switch { Supported.No => "no", Supported.Partial => "partial", Supported.Yes => "yes", _ => null, }; #endif } /// /// Get string value from input bool? /// /// bool? to get value from /// String corresponding to the bool? public static string FromYesNo(this bool? yesno) { #if NET_FRAMEWORK switch (yesno) { case true: return "yes"; case false: return "no"; default: return null; } #else return yesno switch { true => "yes", false => "no", _ => null, }; #endif } #endregion } }