diff --git a/BizHawk.Client.Common/ControllerBinding.cs b/BizHawk.Client.Common/ControllerBinding.cs index 3338263a4a..8f957847cb 100644 --- a/BizHawk.Client.Common/ControllerBinding.cs +++ b/BizHawk.Client.Common/ControllerBinding.cs @@ -11,15 +11,15 @@ namespace BizHawk.Client.Common { public Controller(ControllerDefinition definition) { - _type = definition; - for (int i = 0; i < _type.FloatControls.Count; i++) + Definition = definition; + for (int i = 0; i < Definition.FloatControls.Count; i++) { - _floatButtons[_type.FloatControls[i]] = _type.FloatRanges[i].Mid; - _floatRanges[_type.FloatControls[i]] = _type.FloatRanges[i]; + _floatButtons[Definition.FloatControls[i]] = Definition.FloatRanges[i].Mid; + _floatRanges[Definition.FloatControls[i]] = Definition.FloatRanges[i]; } } - public ControllerDefinition Definition => _type; + public ControllerDefinition Definition { get; private set; } public bool IsPressed(string button) { @@ -37,10 +37,8 @@ namespace BizHawk.Client.Common private readonly Dictionary _floatRanges = new WorkingDictionary(); private readonly Dictionary _floatBinds = new Dictionary(); - private ControllerDefinition _type; - /// don't do this - public void ForceType(ControllerDefinition newType) { _type = newType; } + public void ForceType(ControllerDefinition newType) => Definition = newType; public bool this[string button] => IsPressed(button); @@ -154,9 +152,7 @@ namespace BizHawk.Client.Common } public void ApplyAxisConstraints(string constraintClass) - { - _type.ApplyAxisConstraints(constraintClass, _floatButtons); - } + => Definition.ApplyAxisConstraints(constraintClass, _floatButtons); /// /// merges pressed logical buttons from the supplied controller, effectively ORing it with the current state diff --git a/BizHawk.Client.Common/IPS.cs b/BizHawk.Client.Common/IPS.cs index 6014d55ed5..b1a8e76491 100644 --- a/BizHawk.Client.Common/IPS.cs +++ b/BizHawk.Client.Common/IPS.cs @@ -10,7 +10,7 @@ namespace BizHawk.Client.Common var ipsHeader = new byte[5]; patch.Read(ipsHeader, 0, 5); - string header = "PATCH"; + const string header = "PATCH"; for (int i = 0; i < 5; i++) { if (ipsHeader[i] != header[i]) @@ -21,7 +21,7 @@ namespace BizHawk.Client.Common } // header verified, loop over patch entries - uint eof = ('E' * 0x10000) + ('O' * 0x100) + 'F'; + const uint eof = ('E' * 0x10000) + ('O' * 0x100) + 'F'; var ret = new MemoryStream(rom.Length); ret.Write(rom, 0, rom.Length); diff --git a/BizHawk.Client.Common/OpenAdvanced.cs b/BizHawk.Client.Common/OpenAdvanced.cs index f826b08a5f..e062c60604 100644 --- a/BizHawk.Client.Common/OpenAdvanced.cs +++ b/BizHawk.Client.Common/OpenAdvanced.cs @@ -49,28 +49,15 @@ namespace BizHawk.Client.Common int idx = text.IndexOf('*'); string type = text.Substring(0, idx); string token = text.Substring(idx + 1); - IOpenAdvanced ioa; - if (type == OpenAdvancedTypes.OpenRom) + var ioa = type switch { - ioa = new OpenAdvanced_OpenRom(); - } - else if (type == OpenAdvancedTypes.Libretro) - { - ioa = new OpenAdvanced_Libretro(); - } - else if (type == OpenAdvancedTypes.LibretroNoGame) - { - ioa = new OpenAdvanced_LibretroNoGame(); - } - else if (type == OpenAdvancedTypes.MAME) - { - ioa = new OpenAdvanced_MAME(); - } - else - { - ioa = null; - } + OpenAdvancedTypes.OpenRom => (IOpenAdvanced)new OpenAdvanced_OpenRom(), + OpenAdvancedTypes.Libretro => new OpenAdvanced_Libretro(), + OpenAdvancedTypes.LibretroNoGame => new OpenAdvanced_LibretroNoGame(), + OpenAdvancedTypes.MAME => new OpenAdvanced_MAME(), + _ => null + }; if (ioa == null) { @@ -83,7 +70,7 @@ namespace BizHawk.Client.Common public static string Serialize(IOpenAdvanced ioa) { - StringWriter sw = new StringWriter(); + var sw = new StringWriter(); sw.Write("{0}*", ioa.TypeName); ioa.Serialize(sw); return sw.ToString(); diff --git a/BizHawk.Client.Common/PathManager.cs b/BizHawk.Client.Common/PathManager.cs index ef9248ab8a..645092ac38 100644 --- a/BizHawk.Client.Common/PathManager.cs +++ b/BizHawk.Client.Common/PathManager.cs @@ -171,18 +171,10 @@ namespace BizHawk.Client.Common { // determine the number of parent directories in path and return result int x = path.HowMany('\\'); - if (x > 0) - { - return path.HowMany("..\\"); - } - - return 0; + return x > 0 ? path.HowMany("..\\") : 0; } - public static bool IsRecent(string path) - { - return path == "%recent%"; - } + public static bool IsRecent(string path) => path == "%recent%"; public static string GetLuaPath() { diff --git a/BizHawk.Client.Common/RecentFiles.cs b/BizHawk.Client.Common/RecentFiles.cs index 19ca7df854..12efeb75dd 100644 --- a/BizHawk.Client.Common/RecentFiles.cs +++ b/BizHawk.Client.Common/RecentFiles.cs @@ -40,28 +40,10 @@ namespace BizHawk.Client.Common [JsonIgnore] public string MostRecent => recentlist.Any() ? recentlist[0] : ""; - public string this[int index] - { - get - { - if (recentlist.Any()) - { - return recentlist[index]; - } + public string this[int index] => recentlist.Any() ? recentlist[index] : ""; - return ""; - } - } - - public IEnumerator GetEnumerator() - { - return recentlist.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + public IEnumerator GetEnumerator() => recentlist.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public void Clear() { diff --git a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs index 5d37ee2d65..14750d9b17 100644 --- a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs +++ b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs @@ -36,7 +36,7 @@ namespace BizHawk.Client.Common.cheats code = code.Remove(0, 2); var addrStr = code.Remove(0, 2); - addrStr = addrStr + code.Remove(2, 2); + addrStr += code.Remove(2, 2); result.Value = int.Parse(valueStr, NumberStyles.HexNumber); result.Address = int.Parse(addrStr, NumberStyles.HexNumber); diff --git a/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs b/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs index 1afbb25aaf..af97cfd93a 100644 --- a/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs +++ b/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs @@ -19,35 +19,35 @@ namespace BizHawk.Client.Common.cheats } var parseString = code.Remove(0, 2); - switch (code.Length) + return code.Length switch { - case 9: - // Sample Code of 1-Byte: - // FFF761:64 - // Becomes: - // Address: F761 - // Value: 64 - return new DecodeResult - { - Address = int.Parse(parseString.Remove(4, 3), NumberStyles.HexNumber), - Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber), - Size = WatchSize.Byte - }; - case 11: - // Sample Code of 2-Byte: - // FFF761:6411 - // Becomes: - // Address: F761 - // Value: 6411 - return new DecodeResult - { - Address = int.Parse(parseString.Remove(4, 5), NumberStyles.HexNumber), - Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber), - Size = WatchSize.Word - }; - default: - return new InvalidCheatCode("Action Replay/Pro Action Replay Codes need to be either 9 or 11 characters."); - } + 9 => + // Sample Code of 1-Byte: + // FFF761:64 + // Becomes: + // Address: F761 + // Value: 64 + new DecodeResult + { + Address = int.Parse(parseString.Remove(4, 3), NumberStyles.HexNumber) + , Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber) + , Size = WatchSize.Byte + }, + 11 => + // Sample Code of 2-Byte: + // FFF761:6411 + // Becomes: + // Address: F761 + // Value: 6411 + new DecodeResult + { + Address = int.Parse(parseString.Remove(4, 5), NumberStyles.HexNumber) + , Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber) + , Size = WatchSize.Word + }, + _ => new InvalidCheatCode( + "Action Replay/Pro Action Replay Codes need to be either 9 or 11 characters.") + }; } } } diff --git a/BizHawk.Client.Common/config/ConfigService.cs b/BizHawk.Client.Common/config/ConfigService.cs index b47b0fecd9..5b81e54d08 100644 --- a/BizHawk.Client.Common/config/ConfigService.cs +++ b/BizHawk.Client.Common/config/ConfigService.cs @@ -50,12 +50,7 @@ namespace BizHawk.Client.Common throw new InvalidOperationException("Config Error", ex); } - if (config == null) - { - return new T(); - } - - return config; + return config ?? new T(); } public static void Save(string filepath, object config) diff --git a/BizHawk.Client.Common/inputAdapters/OverrideAdapter.cs b/BizHawk.Client.Common/inputAdapters/OverrideAdapter.cs index 40efb6b4bc..892dbfff82 100644 --- a/BizHawk.Client.Common/inputAdapters/OverrideAdapter.cs +++ b/BizHawk.Client.Common/inputAdapters/OverrideAdapter.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; - +using System.Linq; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common @@ -29,47 +29,16 @@ namespace BizHawk.Client.Common } public float GetFloat(string name) - { - if (_floatOverrides.ContainsKey(name)) - { - return _floatOverrides[name]; - } + => _floatOverrides.ContainsKey(name) + ? _floatOverrides[name] + : 0.0F; + - return 0.0F; - } + public IEnumerable Overrides => _overrides.Select(kvp => kvp.Key); - public IEnumerable Overrides - { - get - { - foreach (var kvp in _overrides) - { - yield return kvp.Key; - } - } - } + public IEnumerable FloatOverrides => _floatOverrides.Select(kvp => kvp.Key); - public IEnumerable FloatOverrides - { - get - { - foreach (var kvp in _floatOverrides) - { - yield return kvp.Key; - } - } - } - - public IEnumerable InversedButtons - { - get - { - foreach (var name in _inverses) - { - yield return name; - } - } - } + public IEnumerable InversedButtons => _inverses; public void SetFloat(string name, float value) { diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs index 225d3c7b6b..df1b6ccb01 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs @@ -56,12 +56,7 @@ namespace BizHawk.Client.Common [LuaMethod("trim", "returns a string that trims whitespace on the left and right ends of the string")] public static string Trim(string str) { - if (string.IsNullOrEmpty(str)) - { - return null; - } - - return str.Trim(); + return string.IsNullOrEmpty(str) ? null : str.Trim(); } [LuaMethodExample("local stbizrep = bizstring.replace( \"Some string\", \"Some\", \"Replaced\" );")] diff --git a/BizHawk.Client.Common/lua/LuaDocumentation.cs b/BizHawk.Client.Common/lua/LuaDocumentation.cs index dc81b3e5d4..59ee62163d 100644 --- a/BizHawk.Client.Common/lua/LuaDocumentation.cs +++ b/BizHawk.Client.Common/lua/LuaDocumentation.cs @@ -220,7 +220,7 @@ __Types and notation__ } } - private string TypeCleanup(string str) + private static string TypeCleanup(string str) { return str .Replace("System", "") diff --git a/BizHawk.Client.Common/lua/LuaFile.cs b/BizHawk.Client.Common/lua/LuaFile.cs index dddb59a85d..080f9328d7 100644 --- a/BizHawk.Client.Common/lua/LuaFile.cs +++ b/BizHawk.Client.Common/lua/LuaFile.cs @@ -61,18 +61,18 @@ public void Toggle() { - if (State == RunState.Paused) + switch (State) { - State = RunState.Running; - } - else if (State == RunState.Disabled) - { - State = RunState.Running; - FrameWaiting = false; - } - else - { - State = RunState.Disabled; + case RunState.Paused: + State = RunState.Running; + break; + case RunState.Disabled: + State = RunState.Running; + FrameWaiting = false; + break; + default: + State = RunState.Disabled; + break; } } diff --git a/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/BizHawk.Client.Common/lua/LuaLibraryBase.cs index fde7af97e7..70c1b44cc8 100644 --- a/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -60,22 +60,13 @@ namespace BizHawk.Client.Common protected static Color? ToColor(object o) { - if (o == null) + return o switch { - return null; - } - - if (o is double d) - { - return Color.FromArgb((int)(long)d); - } - - if (o is string s) - { - return Color.FromName(s); - } - - return null; + double d => Color.FromArgb((int) (long) d), + string s => Color.FromName(s), + null => null, + _ => null + }; } protected void Log(object message) diff --git a/BizHawk.Client.Common/movie/PlatformFrameRates.cs b/BizHawk.Client.Common/movie/PlatformFrameRates.cs index b82f85eefa..41fbaa2b9e 100644 --- a/BizHawk.Client.Common/movie/PlatformFrameRates.cs +++ b/BizHawk.Client.Common/movie/PlatformFrameRates.cs @@ -80,12 +80,7 @@ namespace BizHawk.Client.Common get { var key = systemId + (pal ? "_PAL" : ""); - if (Rates.ContainsKey(key)) - { - return Rates[key]; - } - - return 60.0; + return Rates.ContainsKey(key) ? Rates[key] : 60.0; } } diff --git a/BizHawk.Client.Common/movie/SubtitleList.cs b/BizHawk.Client.Common/movie/SubtitleList.cs index 2dd085746f..0c3d1f6d3b 100644 --- a/BizHawk.Client.Common/movie/SubtitleList.cs +++ b/BizHawk.Client.Common/movie/SubtitleList.cs @@ -77,7 +77,7 @@ namespace BizHawk.Client.Common { int index = 1; var sb = new StringBuilder(); - List subs = new List(); + var subs = new List(); foreach (var subtitle in this) { subs.Add(new Subtitle(subtitle)); diff --git a/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs b/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs index 9b0f05cb6d..4eaa9acb71 100644 --- a/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs +++ b/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs @@ -50,16 +50,7 @@ namespace BizHawk.Client.Common public bool StartsFromSavestate { - get - { - if (Header.ContainsKey(HeaderKeys.STARTSFROMSAVESTATE)) - { - return bool.Parse(Header[HeaderKeys.STARTSFROMSAVESTATE]); - } - - return false; - } - + get => Header.ContainsKey(HeaderKeys.STARTSFROMSAVESTATE) && bool.Parse(Header[HeaderKeys.STARTSFROMSAVESTATE]); set { if (value) @@ -75,16 +66,7 @@ namespace BizHawk.Client.Common public bool StartsFromSaveRam { - get - { - if (Header.ContainsKey(HeaderKeys.STARTSFROMSAVERAM)) - { - return bool.Parse(Header[HeaderKeys.STARTSFROMSAVERAM]); - } - - return false; - } - + get => Header.ContainsKey(HeaderKeys.STARTSFROMSAVERAM) && bool.Parse(Header[HeaderKeys.STARTSFROMSAVERAM]); set { if (value) @@ -106,16 +88,7 @@ namespace BizHawk.Client.Common public string GameName { - get - { - if (Header.ContainsKey(HeaderKeys.GAMENAME)) - { - return Header[HeaderKeys.GAMENAME]; - } - - return ""; - } - + get => Header.ContainsKey(HeaderKeys.GAMENAME) ? Header[HeaderKeys.GAMENAME] : ""; set { if (Header[HeaderKeys.GAMENAME] != value) @@ -128,16 +101,7 @@ namespace BizHawk.Client.Common public string SystemID { - get - { - if (Header.ContainsKey(HeaderKeys.PLATFORM)) - { - return Header[HeaderKeys.PLATFORM]; - } - - return ""; - } - + get => Header.ContainsKey(HeaderKeys.PLATFORM) ? Header[HeaderKeys.PLATFORM] : ""; set { if (Header[HeaderKeys.PLATFORM] != value) diff --git a/BizHawk.Client.Common/movie/import/Fm2Import.cs b/BizHawk.Client.Common/movie/import/Fm2Import.cs index 8151af5f0e..731797f5bc 100644 --- a/BizHawk.Client.Common/movie/import/Fm2Import.cs +++ b/BizHawk.Client.Common/movie/import/Fm2Import.cs @@ -16,7 +16,7 @@ namespace BizHawk.Client.Common { var neshawkName = ((CoreAttribute)Attribute.GetCustomAttribute(typeof(NES), typeof(CoreAttribute))).CoreName; Result.Movie.HeaderEntries[HeaderKeys.CORE] = neshawkName; - var emulator = "FCEUX"; + const string emulator = "FCEUX"; var platform = "NES"; // TODO: FDS? var syncSettings = new NES.NESSyncSettings(); diff --git a/BizHawk.Client.Common/movie/import/LsmvImport.cs b/BizHawk.Client.Common/movie/import/LsmvImport.cs index 9b174570e4..807d9150e1 100644 --- a/BizHawk.Client.Common/movie/import/LsmvImport.cs +++ b/BizHawk.Client.Common/movie/import/LsmvImport.cs @@ -342,14 +342,10 @@ namespace BizHawk.Client.Common.movie.import } // LSNES frames don't start or end with a |. - int start = 1; int end = sections.Length; - int playerOffset = 0; - for (int section = start; section < end; section++) + for (int player = 1; player < end; player++) { - // The player number is one less than the section number for the reasons explained above. - int player = section + playerOffset; string prefix = $"P{player} "; // Gameboy doesn't currently have a prefix saying which player the input is for. @@ -360,12 +356,12 @@ namespace BizHawk.Client.Common.movie.import // Only count lines with that have the right number of buttons and are for valid players. if ( - sections[section].Length == buttons.Length) + sections[player].Length == buttons.Length) { for (int button = 0; button < buttons.Length; button++) { // Consider the button pressed so long as its spot is not occupied by a ".". - controllers[prefix + buttons[button]] = sections[section][button] != '.'; + controllers[prefix + buttons[button]] = sections[player][button] != '.'; } } } diff --git a/BizHawk.Client.Common/movie/import/Mc2Import.cs b/BizHawk.Client.Common/movie/import/Mc2Import.cs index 5e92324aa8..2866b97118 100644 --- a/BizHawk.Client.Common/movie/import/Mc2Import.cs +++ b/BizHawk.Client.Common/movie/import/Mc2Import.cs @@ -155,14 +155,12 @@ namespace BizHawk.Client.Common.movie.import Skip the first two sections of the split, which consist of everything before the starting | and the command. Do not use the section after the last |. In other words, get the sections for the players. */ - int start = 2; int end = sections.Length - 1; - int playerOffset = -1; - for (int section = start; section < end; section++) + for (int section = 2; section < end; section++) { // The player number is one less than the section number for the reasons explained above. - int player = section + playerOffset; + int player = section - 1; string prefix = $"P{player} "; // Only count lines with that have the right number of buttons and are for valid players. diff --git a/BizHawk.Client.Common/movie/import/MovieImport.cs b/BizHawk.Client.Common/movie/import/MovieImport.cs index cdb17eece2..9e5a1674f5 100644 --- a/BizHawk.Client.Common/movie/import/MovieImport.cs +++ b/BizHawk.Client.Common/movie/import/MovieImport.cs @@ -45,17 +45,14 @@ namespace BizHawk.Client.Common .GetConstructor(new Type[] { }) ?.Invoke(new object[] { }); - if (importer == null) - { - return ImportResult.Error($"No importer found for file type {ext}"); - } - - return importer.Import(path); + return importer == null + ? ImportResult.Error($"No importer found for file type {ext}") + : importer.Import(path); } private static Type ImporterForExtension(string ext) { - return Importers.FirstOrDefault(i => string.Equals(i.Value.Extension, ext, StringComparison.OrdinalIgnoreCase)).Key; + return Importers.First(i => string.Equals(i.Value.Extension, ext, StringComparison.OrdinalIgnoreCase)).Key; } } } diff --git a/BizHawk.Client.Common/movie/import/SmvImport.cs b/BizHawk.Client.Common/movie/import/SmvImport.cs index bf2eb1de48..ae74eb5011 100644 --- a/BizHawk.Client.Common/movie/import/SmvImport.cs +++ b/BizHawk.Client.Common/movie/import/SmvImport.cs @@ -34,22 +34,13 @@ namespace BizHawk.Client.Common.movie.import // 004 4-byte little-endian unsigned int: version number uint versionNumber = r.ReadUInt32(); - string version; - switch (versionNumber) + var version = versionNumber switch { - case 1: - version = "1.43"; - break; - case 4: - version = "1.51"; - break; - case 5: - version = "1.52"; - break; - default: - version = "Unknown"; - break; - } + 1 => "1.43", + 4 => "1.51", + 5 => "1.52", + _ => "Unknown" + }; Result.Movie.Comments.Add($"{EmulationOrigin} Snes9x version {version}"); Result.Movie.Comments.Add($"{MovieOrigin} .SMV"); diff --git a/BizHawk.Client.Common/movie/import/bkm/BkmControllerAdapter.cs b/BizHawk.Client.Common/movie/import/bkm/BkmControllerAdapter.cs index ee9050aa9f..15f981394b 100644 --- a/BizHawk.Client.Common/movie/import/bkm/BkmControllerAdapter.cs +++ b/BizHawk.Client.Common/movie/import/bkm/BkmControllerAdapter.cs @@ -28,83 +28,53 @@ namespace BizHawk.Client.Common /// public void SetControllersAsMnemonic(string mnemonic) { - if (ControlType == "Null Controller") + switch (ControlType) { - return; - } - - if (ControlType == "Lynx Controller") - { - SetLynxControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "SNES Controller") - { - SetSNESControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "Commodore 64 Controller") - { - SetC64ControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "GBA Controller") - { - SetGBAControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "Atari 7800 ProLine Joystick Controller") - { - SetAtari7800AsMnemonic(mnemonic); - return; - } - - if (ControlType == "Dual Gameboy Controller") - { - SetDualGameBoyControllerAsMnemonic(mnemonic); - return; - } - - if (ControlType == "WonderSwan Controller") - { - SetWonderSwanControllerAsMnemonic(mnemonic); - return; - } - - if (ControlType == "Nintendo 64 Controller") - { - SetN64ControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "Saturn Controller") - { - SetSaturnControllersAsMnemonic(mnemonic); - return; - } - - if (ControlType == "PSP Controller") - { - // TODO - return; - } - - if (ControlType == "GPGX Genesis Controller") - { - if (IsGenesis6Button()) + case "Null Controller": + return; + case "Lynx Controller": + SetLynxControllersAsMnemonic(mnemonic); + return; + case "SNES Controller": + SetSNESControllersAsMnemonic(mnemonic); + return; + case "Commodore 64 Controller": + SetC64ControllersAsMnemonic(mnemonic); + return; + case "GBA Controller": + SetGBAControllersAsMnemonic(mnemonic); + return; + case "Atari 7800 ProLine Joystick Controller": + SetAtari7800AsMnemonic(mnemonic); + return; + case "Dual Gameboy Controller": + SetDualGameBoyControllerAsMnemonic(mnemonic); + return; + case "WonderSwan Controller": + SetWonderSwanControllerAsMnemonic(mnemonic); + return; + case "Nintendo 64 Controller": + SetN64ControllersAsMnemonic(mnemonic); + return; + case "Saturn Controller": + SetSaturnControllersAsMnemonic(mnemonic); + return; + case "PSP Controller": + // TODO + return; + case "GPGX Genesis Controller": { - SetGenesis6ControllersAsMnemonic(mnemonic); - } - else - { - SetGenesis3ControllersAsMnemonic(mnemonic); - } + if (IsGenesis6Button()) + { + SetGenesis6ControllersAsMnemonic(mnemonic); + } + else + { + SetGenesis3ControllersAsMnemonic(mnemonic); + } - return; + return; + } } var c = new MnemonicChecker(mnemonic); @@ -118,41 +88,42 @@ namespace BizHawk.Client.Common { return; } - else if (mnemonic[1] == 'P') + + switch (mnemonic[1]) { - Force("Power", true); - } - else if (mnemonic[1] == 'E') - { - Force("FDS Eject", true); - } - else if (mnemonic[1] == '0') - { - Force("FDS Insert 0", true); - } - else if (mnemonic[1] == '1') - { - Force("FDS Insert 1", true); - } - else if (mnemonic[1] == '2') - { - Force("FDS Insert 2", true); - } - else if (mnemonic[1] == '3') - { - Force("FDS Insert 3", true); - } - else if (mnemonic[1] == 'c') - { - Force("VS Coin 1", true); - } - else if (mnemonic[1] == 'C') - { - Force("VS Coin 2", true); - } - else if (mnemonic[1] != '.') - { - Force("Reset", true); + case 'P': + Force("Power", true); + break; + case 'E': + Force("FDS Eject", true); + break; + case '0': + Force("FDS Insert 0", true); + break; + case '1': + Force("FDS Insert 1", true); + break; + case '2': + Force("FDS Insert 2", true); + break; + case '3': + Force("FDS Insert 3", true); + break; + case 'c': + Force("VS Coin 1", true); + break; + case 'C': + Force("VS Coin 2", true); + break; + default: + { + if (mnemonic[1] != '.') + { + Force("Reset", true); + } + + break; + } } } @@ -195,9 +166,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); int ctr = start; - if (mnemonic.Length < srcindex + ctr + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + ctr + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -210,17 +181,17 @@ namespace BizHawk.Client.Common foreach (string button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force(prefix + button, c[srcindex + ctr++]); + Force(prefix + button, c[srcIndex + ctr++]); } } if (ControlType == "SMS Controller") { - int srcindex = BkmMnemonicConstants.Players[ControlType] * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = BkmMnemonicConstants.Players[ControlType] * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); int ctr = start; foreach (var command in BkmMnemonicConstants.Commands[ControlType].Keys) { - Force(command, c[srcindex + ctr++]); + Force(command, c[srcIndex + ctr++]); } } } @@ -228,10 +199,7 @@ namespace BizHawk.Client.Common private readonly WorkingDictionary _myBoolButtons = new WorkingDictionary(); private readonly WorkingDictionary _myFloatControls = new WorkingDictionary(); - private bool IsGenesis6Button() - { - return Definition.BoolButtons.Contains("P1 X"); - } + private bool IsGenesis6Button() => Definition.BoolButtons.Contains("P1 X"); private void Force(string button, bool state) { @@ -247,7 +215,7 @@ namespace BizHawk.Client.Common private void SetGBAControllersAsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 2) { @@ -268,7 +236,7 @@ namespace BizHawk.Client.Common private void SetGenesis6ControllersAsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 2) @@ -292,9 +260,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); - if (mnemonic.Length < srcindex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -302,14 +270,14 @@ namespace BizHawk.Client.Common int start = 3; foreach (string button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } } private void SetGenesis3ControllersAsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 2) @@ -333,9 +301,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons["GPGX 3-Button Controller"].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons["GPGX 3-Button Controller"].Count + 1); - if (mnemonic.Length < srcindex + 3 + BkmMnemonicConstants.Buttons["GPGX 3-Button Controller"].Count - 1) + if (mnemonic.Length < srcIndex + 3 + BkmMnemonicConstants.Buttons["GPGX 3-Button Controller"].Count - 1) { return; } @@ -343,7 +311,7 @@ namespace BizHawk.Client.Common int start = 3; foreach (string button in BkmMnemonicConstants.Buttons["GPGX 3-Button Controller"].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } } @@ -369,9 +337,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); - if (mnemonic.Length < srcindex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -379,7 +347,7 @@ namespace BizHawk.Client.Common int start = 3; foreach (var button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } } @@ -401,9 +369,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); - if (mnemonic.Length < srcindex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -411,14 +379,14 @@ namespace BizHawk.Client.Common int start = 3; foreach (var button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force(button, c[srcindex + start++]); + Force(button, c[srcIndex + start++]); } } } private void SetN64ControllersAsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 2) @@ -460,7 +428,7 @@ namespace BizHawk.Client.Common private void SetSaturnControllersAsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 2) @@ -479,9 +447,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); - if (mnemonic.Length < srcindex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + 3 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -489,14 +457,14 @@ namespace BizHawk.Client.Common int start = 3; foreach (string button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } } private void SetAtari7800AsMnemonic(string mnemonic) { - MnemonicChecker c = new MnemonicChecker(mnemonic); + var c = new MnemonicChecker(mnemonic); _myBoolButtons.Clear(); if (mnemonic.Length < 5) @@ -526,16 +494,16 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); int start = 6; - if (mnemonic.Length < srcindex + start + BkmMnemonicConstants.Buttons[ControlType].Count) + if (mnemonic.Length < srcIndex + start + BkmMnemonicConstants.Buttons[ControlType].Count) { return; } foreach (string button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } } @@ -575,9 +543,9 @@ namespace BizHawk.Client.Common for (int player = 1; player <= BkmMnemonicConstants.Players[ControlType]; player++) { - int srcindex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); + int srcIndex = (player - 1) * (BkmMnemonicConstants.Buttons[ControlType].Count + 1); - if (mnemonic.Length < srcindex + 1 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) + if (mnemonic.Length < srcIndex + 1 + BkmMnemonicConstants.Buttons[ControlType].Count - 1) { return; } @@ -585,14 +553,14 @@ namespace BizHawk.Client.Common int start = 1; foreach (var button in BkmMnemonicConstants.Buttons[ControlType].Keys) { - Force($"P{player} {button}", c[srcindex + start++]); + Force($"P{player} {button}", c[srcIndex + start++]); } } - int startk = 13; + int startKey = 13; foreach (string button in BkmMnemonicConstants.Buttons["Commodore 64 Keyboard"].Keys) { - Force(button, c[startk++]); + Force(button, c[startKey++]); } } diff --git a/BizHawk.Client.Common/movie/import/bkm/BkmHeader.cs b/BizHawk.Client.Common/movie/import/bkm/BkmHeader.cs index 001fdf65bb..5a25b2da4d 100644 --- a/BizHawk.Client.Common/movie/import/bkm/BkmHeader.cs +++ b/BizHawk.Client.Common/movie/import/bkm/BkmHeader.cs @@ -22,16 +22,9 @@ namespace BizHawk.Client.Common public string SavestateBinaryBase64Blob { - get - { - if (ContainsKey(HeaderKeys.SAVESTATEBINARYBASE64BLOB)) - { - return this[HeaderKeys.SAVESTATEBINARYBASE64BLOB]; - } - - return null; - } - + get => ContainsKey(HeaderKeys.SAVESTATEBINARYBASE64BLOB) + ? this[HeaderKeys.SAVESTATEBINARYBASE64BLOB] + : null; set { if (value == null) @@ -48,7 +41,6 @@ namespace BizHawk.Client.Common public new string this[string key] { get => ContainsKey(key) ? base[key] : ""; - set { if (ContainsKey(key)) diff --git a/BizHawk.Client.Common/movie/import/bkm/BkmMovie.cs b/BizHawk.Client.Common/movie/import/bkm/BkmMovie.cs index 185d403053..5489ee4112 100644 --- a/BizHawk.Client.Common/movie/import/bkm/BkmMovie.cs +++ b/BizHawk.Client.Common/movie/import/bkm/BkmMovie.cs @@ -31,12 +31,7 @@ namespace BizHawk.Client.Common return double.PositiveInfinity; } - if (Loaded) - { - return _log.Count; - } - - return 0; + return Loaded ? _log.Count : 0; } } diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs index 1f33d8634f..806ec62e2c 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovie.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovie.cs @@ -395,12 +395,9 @@ namespace BizHawk.Client.Common public Guid BranchGuidByIndex(int index) { - if (index >= Branches.Count) - { - return Guid.Empty; - } - - return Branches[index].UniqueIdentifier; + return index >= Branches.Count + ? Guid.Empty + : Branches[index].UniqueIdentifier; } public int BranchIndexByHash(Guid uuid) diff --git a/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs b/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs index 39ea2089ab..74a7d74be6 100644 --- a/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs +++ b/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs @@ -44,17 +44,12 @@ namespace BizHawk.Client.Common public override bool Equals(object obj) { - if (obj == null) + return obj switch { - return false; - } - - if (obj is TasMovieMarker marker) - { - return Frame == marker.Frame; - } - - return false; + null => false, + TasMovieMarker marker => Frame == marker.Frame, + _ => false + }; } public static bool operator ==(TasMovieMarker marker, int frame) diff --git a/BizHawk.Client.Common/tools/Cheat.cs b/BizHawk.Client.Common/tools/Cheat.cs index 9eec69594a..ec1901038d 100644 --- a/BizHawk.Client.Common/tools/Cheat.cs +++ b/BizHawk.Client.Common/tools/Cheat.cs @@ -87,24 +87,15 @@ namespace BizHawk.Client.Common public string AddressStr => _watch.AddressString; - public string ValueStr - { - get - { - switch (_watch.Size) + public string ValueStr => + _watch.Size switch { - default: - case WatchSize.Separator: - return ""; - case WatchSize.Byte: - return (_watch as ByteWatch).FormatValue((byte)_val); - case WatchSize.Word: - return (_watch as WordWatch).FormatValue((ushort)_val); - case WatchSize.DWord: - return (_watch as DWordWatch).FormatValue((uint)_val); - } - } - } + WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_val), + WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_val), + WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_val), + WatchSize.Separator => "", + _ => "" + }; public string CompareStr { @@ -112,25 +103,21 @@ namespace BizHawk.Client.Common { if (_compare.HasValue) { - switch (_watch.Size) + return _watch.Size switch { - default: - case WatchSize.Separator: - return ""; - case WatchSize.Byte: - return (_watch as ByteWatch).FormatValue((byte)_compare.Value); - case WatchSize.Word: - return (_watch as WordWatch).FormatValue((ushort)_compare.Value); - case WatchSize.DWord: - return (_watch as DWordWatch).FormatValue((uint)_compare.Value); - } + WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_compare.Value), + WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_compare.Value), + WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_compare.Value), + WatchSize.Separator => "", + _ => "" + }; } return ""; } } - public CompareType ComparisonType { get; private set; } + public CompareType ComparisonType { get; } public void Enable(bool handleChange = true) { @@ -170,15 +157,10 @@ namespace BizHawk.Client.Common } } - private string GetStringForPulse(int val) - { - if (_watch.Type == DisplayType.Hex) - { - return val.ToString("X8"); - } - - return val.ToString(); - } + private string GetStringForPulse(int val) => + _watch.Type == DisplayType.Hex + ? val.ToString("X8") + : val.ToString(); public void Pulse() { @@ -239,13 +221,13 @@ namespace BizHawk.Client.Common switch (_watch.Size) { case WatchSize.Byte: - _watch.Poke((_watch as ByteWatch).FormatValue((byte)_val)); + _watch.Poke(((ByteWatch)_watch).FormatValue((byte)_val)); break; case WatchSize.Word: - _watch.Poke((_watch as WordWatch).FormatValue((ushort)_val)); + _watch.Poke(((WordWatch)_watch).FormatValue((ushort)_val)); break; case WatchSize.DWord: - _watch.Poke((_watch as DWordWatch).FormatValue((uint)_val)); + _watch.Poke(((DWordWatch)_watch).FormatValue((uint)_val)); break; } } @@ -381,7 +363,7 @@ namespace BizHawk.Client.Common public void SetType(DisplayType type) { - if (_watch.IsDiplayTypeAvailable(type)) + if (_watch.IsDisplayTypeAvailable(type)) { _watch.Type = type; Changes(); diff --git a/BizHawk.Client.Common/tools/CheatList.cs b/BizHawk.Client.Common/tools/CheatList.cs index 8700accafd..45d013e23b 100644 --- a/BizHawk.Client.Common/tools/CheatList.cs +++ b/BizHawk.Client.Common/tools/CheatList.cs @@ -80,13 +80,7 @@ namespace BizHawk.Client.Common public bool AttemptToLoadCheatFile() { var file = new FileInfo(_defaultFileName); - - if (file.Exists) - { - return Load(file.FullName, false); - } - - return false; + return file.Exists && Load(file.FullName, false); } public void NewList(string defaultFileName, bool autosave = false) @@ -297,12 +291,9 @@ namespace BizHawk.Client.Common case WatchSize.Byte: return activeCheat.Value; case WatchSize.Word: - if (size == WatchSize.Byte) - { - return GetByteValue(domain, addr); - } - - return activeCheat.Value; + return size == WatchSize.Byte + ? GetByteValue(domain, addr) + : activeCheat.Value; case WatchSize.DWord: if (size == WatchSize.Byte) { diff --git a/BizHawk.Client.Common/tools/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine.cs index 19e50c930e..0bc8882e40 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine.cs @@ -162,25 +162,15 @@ namespace BizHawk.Client.Common { int before = _watchList.Count; - switch (_compareTo) + _watchList = _compareTo switch { - default: - case Compare.Previous: - _watchList = ComparePrevious(_watchList).ToList(); - break; - case Compare.SpecificValue: - _watchList = CompareSpecificValue(_watchList).ToList(); - break; - case Compare.SpecificAddress: - _watchList = CompareSpecificAddress(_watchList).ToList(); - break; - case Compare.Changes: - _watchList = CompareChanges(_watchList).ToList(); - break; - case Compare.Difference: - _watchList = CompareDifference(_watchList).ToList(); - break; - } + Compare.Previous => ComparePrevious(_watchList).ToList(), + Compare.SpecificValue => CompareSpecificValue(_watchList).ToList(), + Compare.SpecificAddress => CompareSpecificAddress(_watchList).ToList(), + Compare.Changes => CompareChanges(_watchList).ToList(), + Compare.Difference => CompareDifference(_watchList).ToList(), + _ => ComparePrevious(_watchList).ToList() + }; if (_settings.PreviousType == PreviousType.LastSearch) { @@ -201,20 +191,15 @@ namespace BizHawk.Client.Common ? _watchList.BinarySearch(w => w.Address, address) : _watchList.FirstOrDefault(w => w.Address == address), 1); - switch (_compareTo) + return _compareTo switch { - default: - case Compare.Previous: - return !ComparePrevious(listOfOne).Any(); - case Compare.SpecificValue: - return !CompareSpecificValue(listOfOne).Any(); - case Compare.SpecificAddress: - return !CompareSpecificAddress(listOfOne).Any(); - case Compare.Changes: - return !CompareChanges(listOfOne).Any(); - case Compare.Difference: - return !CompareDifference(listOfOne).Any(); - } + Compare.Previous => !ComparePrevious(listOfOne).Any(), + Compare.SpecificValue => !CompareSpecificValue(listOfOne).Any(), + Compare.SpecificAddress => !CompareSpecificAddress(listOfOne).Any(), + Compare.Changes => !CompareChanges(listOfOne).Any(), + Compare.Difference => !CompareDifference(listOfOne).Any(), + _ => !ComparePrevious(listOfOne).Any() + }; } public int Count => _watchList.Count; @@ -497,48 +482,28 @@ namespace BizHawk.Client.Common { default: case ComparisonOperator.Equal: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(w.Previous)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(w.Previous)); - + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(w.Previous)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(w.Previous)); case ComparisonOperator.NotEqual: return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) != SignExtendAsNeeded(w.Previous)); - case ComparisonOperator.GreaterThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(w.Previous)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(w.Previous)); - + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(w.Previous)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(w.Previous)); case ComparisonOperator.GreaterThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(w.Previous)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(w.Previous)); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(w.Previous)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(w.Previous)); case ComparisonOperator.LessThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(w.Previous)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(w.Previous)); - + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(w.Previous)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(w.Previous)); case ComparisonOperator.LessThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(w.Previous)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(w.Previous)); - + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(w.Previous)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(w.Previous)); case ComparisonOperator.DifferentBy: if (DifferentBy.HasValue) { @@ -573,48 +538,31 @@ namespace BizHawk.Client.Common { default: case ComparisonOperator.Equal: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(compareValue)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(CompareValue.Value)); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) == ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) == SignExtendAsNeeded(CompareValue.Value)); case ComparisonOperator.NotEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) != ToFloat(compareValue)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) != SignExtendAsNeeded(compareValue)); - + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) != ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) != SignExtendAsNeeded(compareValue)); case ComparisonOperator.GreaterThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(compareValue)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(compareValue)); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) > ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) > SignExtendAsNeeded(compareValue)); case ComparisonOperator.GreaterThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(compareValue)); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(compareValue)); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) >= ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) >= SignExtendAsNeeded(compareValue)); case ComparisonOperator.LessThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(compareValue)); - } + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) < ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(compareValue)); - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) < SignExtendAsNeeded(compareValue)); case ComparisonOperator.LessThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(compareValue)); - } + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) <= ToFloat(compareValue)) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(compareValue)); - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) <= SignExtendAsNeeded(compareValue)); case ComparisonOperator.DifferentBy: if (DifferentBy.HasValue) { @@ -735,47 +683,29 @@ namespace BizHawk.Client.Common { default: case ComparisonOperator.Equal: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - ToFloat(w.Previous) == compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) == compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - ToFloat(w.Previous) == compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) == compareValue); case ComparisonOperator.NotEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous != compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) != compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous != compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) != compareValue); case ComparisonOperator.GreaterThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous > compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) > compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous > compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) > compareValue); case ComparisonOperator.GreaterThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous >= compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) >= compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous >= compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) >= compareValue); case ComparisonOperator.LessThan: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous < compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) < compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous < compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) < compareValue); case ComparisonOperator.LessThanEqual: - if (_settings.Type == DisplayType.Float) - { - return watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous <= compareValue); - } - - return watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) <= compareValue); + return _settings.Type == DisplayType.Float + ? watchList.Where(w => ToFloat(GetValue(w.Address)) - w.Previous <= compareValue) + : watchList.Where(w => SignExtendAsNeeded(GetValue(w.Address)) - SignExtendAsNeeded(w.Previous) <= compareValue); case ComparisonOperator.DifferentBy: if (DifferentBy.HasValue) { @@ -802,7 +732,7 @@ namespace BizHawk.Client.Common #region Private parts - private float ToFloat(long val) + private static float ToFloat(long val) { var bytes = BitConverter.GetBytes((int)val); return BitConverter.ToSingle(bytes, 0); @@ -815,48 +745,35 @@ namespace BizHawk.Client.Common return val; } - switch (_settings.Size) + return _settings.Size switch { - default: - case WatchSize.Byte: - return (sbyte)val; - case WatchSize.Word: - return (short)val; - case WatchSize.DWord: - return (int)val; - } + WatchSize.Byte => (sbyte) val, + WatchSize.Word => (short) val, + WatchSize.DWord => (int) val, + _ => (sbyte) val + }; } private long GetValue(long addr) { // do not return sign extended variables from here. - switch (_settings.Size) + return _settings.Size switch { - default: - case WatchSize.Byte: - var theByte = _settings.Domain.PeekByte(addr % Domain.Size); - return theByte; - - case WatchSize.Word: - var theWord = _settings.Domain.PeekUshort(addr % Domain.Size, _settings.BigEndian); - return theWord; - - case WatchSize.DWord: - var theDWord = _settings.Domain.PeekUint(addr % Domain.Size, _settings.BigEndian); - return theDWord; - } + WatchSize.Byte => _settings.Domain.PeekByte(addr % Domain.Size), + WatchSize.Word => _settings.Domain.PeekUshort(addr % Domain.Size, _settings.BigEndian), + WatchSize.DWord => _settings.Domain.PeekUint(addr % Domain.Size, _settings.BigEndian), + _ => _settings.Domain.PeekByte(addr % Domain.Size) + }; } private bool CanDoCompareType(Compare compareType) { - switch (_settings.Mode) + return _settings.Mode switch { - default: - case Settings.SearchMode.Detailed: - return true; - case Settings.SearchMode.Fast: - return compareType != Compare.Changes; - } + Settings.SearchMode.Detailed => true, + Settings.SearchMode.Fast => (compareType != Compare.Changes), + _ => true + }; } #endregion diff --git a/BizHawk.Client.Common/tools/Watch/ByteWatch.cs b/BizHawk.Client.Common/tools/Watch/ByteWatch.cs index 7cf747df62..ae16eaf750 100644 --- a/BizHawk.Client.Common/tools/Watch/ByteWatch.cs +++ b/BizHawk.Client.Common/tools/Watch/ByteWatch.cs @@ -181,18 +181,14 @@ namespace BizHawk.Client.Common // TODO: Implements IFormattable public string FormatValue(byte val) { - switch (Type) + return Type switch { - default: - case DisplayType.Unsigned: - return val.ToString(); - case DisplayType.Signed: - return ((sbyte)val).ToString(); - case DisplayType.Hex: - return $"{val:X2}"; - case DisplayType.Binary: - return Convert.ToString(val, 2).PadLeft(8, '0').Insert(4, " "); - } + DisplayType.Unsigned => val.ToString(), + DisplayType.Signed => ((sbyte) val).ToString(), + DisplayType.Hex => $"{val:X2}", + DisplayType.Binary => Convert.ToString(val, 2).PadLeft(8, '0').Insert(4, " "), + _ => val.ToString() + }; } /// diff --git a/BizHawk.Client.Common/tools/Watch/Watch.cs b/BizHawk.Client.Common/tools/Watch/Watch.cs index 44ab8eea5a..0753f32bb1 100644 --- a/BizHawk.Client.Common/tools/Watch/Watch.cs +++ b/BizHawk.Client.Common/tools/Watch/Watch.cs @@ -35,7 +35,7 @@ namespace BizHawk.Client.Common /// Occurs when a is incompatible with the protected Watch(MemoryDomain domain, long address, WatchSize size, DisplayType type, bool bigEndian, string note) { - if (IsDiplayTypeAvailable(type)) + if (IsDisplayTypeAvailable(type)) { _domain = domain; Address = address; @@ -136,18 +136,14 @@ namespace BizHawk.Client.Common /// New instance. True type is depending of size parameter public static Watch GenerateWatch(MemoryDomain domain, long address, WatchSize size, DisplayType type, bool bigEndian, string note = "", long value = 0, long prev = 0, int changeCount = 0) { - switch (size) + return size switch { - default: - case WatchSize.Separator: - return SeparatorWatch.NewSeparatorWatch(note); - case WatchSize.Byte: - return new ByteWatch(domain, address, type, bigEndian, note, (byte)value, (byte)prev, changeCount); - case WatchSize.Word: - return new WordWatch(domain, address, type, bigEndian, note, (ushort)value, (ushort)prev, changeCount); - case WatchSize.DWord: - return new DWordWatch(domain, address, type, bigEndian, note, (uint)value, (uint)prev, changeCount); - } + WatchSize.Separator => SeparatorWatch.NewSeparatorWatch(note), + WatchSize.Byte => new ByteWatch(domain, address, type, bigEndian, note, (byte) value, (byte) prev, changeCount), + WatchSize.Word => new WordWatch(domain, address, type, bigEndian, note, (ushort) value, (ushort) prev, changeCount), + WatchSize.DWord => new DWordWatch(domain, address, type, bigEndian, note, (uint) value, (uint) prev, changeCount), + _ => SeparatorWatch.NewSeparatorWatch(note) + }; } #region Operators @@ -455,14 +451,14 @@ namespace BizHawk.Client.Common /// True if both object are equals; otherwise, false public override bool Equals(object obj) { - if (obj is Watch) + if (obj is Watch watch) { - return Equals((Watch)obj); + return Equals(watch); } - if (obj is Cheat) + if (obj is Cheat cheat) { - return Equals((Cheat)obj); + return Equals(cheat); } return base.Equals(obj); @@ -482,7 +478,7 @@ namespace BizHawk.Client.Common /// used for the current /// /// you want to check - public bool IsDiplayTypeAvailable(DisplayType type) + public bool IsDisplayTypeAvailable(DisplayType type) { return AvailableTypes().Any(d => d == type); } @@ -501,10 +497,7 @@ namespace BizHawk.Client.Common /// It's used by the "Display on screen" option in the RamWatch window /// /// A well formatted string representation - public virtual string ToDisplayString() - { - return $"{Notes}: {ValueString}"; - } + public virtual string ToDisplayString() => $"{Notes}: {ValueString}"; #endregion @@ -565,18 +558,9 @@ namespace BizHawk.Client.Common /// public long Address { get; } - private string AddressFormatStr - { - get - { - if (_domain != null) - { - return $"X{(_domain.Size - 1).NumHexDigits()}"; - } - - return ""; - } - } + private string AddressFormatStr => _domain != null + ? $"X{(_domain.Size - 1).NumHexDigits()}" + : ""; /// /// Gets the address in the formatted as string @@ -603,7 +587,7 @@ namespace BizHawk.Client.Common get => _type; set { - if (IsDiplayTypeAvailable(value)) + if (IsDisplayTypeAvailable(value)) { _type = value; } @@ -658,122 +642,88 @@ namespace BizHawk.Client.Common // TODO: Replace all the following stuff by implementing ISerializable public static string DisplayTypeToString(DisplayType type) { - switch (type) + return type switch { - default: - return type.ToString(); - case DisplayType.FixedPoint_12_4: - return "Fixed Point 12.4"; - case DisplayType.FixedPoint_20_12: - return "Fixed Point 20.12"; - case DisplayType.FixedPoint_16_16: - return "Fixed Point 16.16"; - } + DisplayType.FixedPoint_12_4 => "Fixed Point 12.4", + DisplayType.FixedPoint_20_12 => "Fixed Point 20.12", + DisplayType.FixedPoint_16_16 => "Fixed Point 16.16", + _ => type.ToString() + }; } public static DisplayType StringToDisplayType(string name) { - switch (name) + return name switch { - default: - return (DisplayType)Enum.Parse(typeof(DisplayType), name); - case "Fixed Point 12.4": - return DisplayType.FixedPoint_12_4; - case "Fixed Point 20.12": - return DisplayType.FixedPoint_20_12; - case "Fixed Point 16.16": - return DisplayType.FixedPoint_16_16; - } + "Fixed Point 12.4" => DisplayType.FixedPoint_12_4, + "Fixed Point 20.12" => DisplayType.FixedPoint_20_12, + "Fixed Point 16.16" => DisplayType.FixedPoint_16_16, + _ => (DisplayType) Enum.Parse(typeof(DisplayType), name) + }; } public char SizeAsChar { get { - switch (Size) + return Size switch { - default: - case WatchSize.Separator: - return 'S'; - case WatchSize.Byte: - return 'b'; - case WatchSize.Word: - return 'w'; - case WatchSize.DWord: - return 'd'; - } + WatchSize.Separator => 'S', + WatchSize.Byte => 'b', + WatchSize.Word => 'w', + WatchSize.DWord => 'd', + _ => 'S' + }; } } public static WatchSize SizeFromChar(char c) { - switch (c) + return c switch { - default: - case 'S': - return WatchSize.Separator; - case 'b': - return WatchSize.Byte; - case 'w': - return WatchSize.Word; - case 'd': - return WatchSize.DWord; - } + 'S' => WatchSize.Separator, + 'b' => WatchSize.Byte, + 'w' => WatchSize.Word, + 'd' => WatchSize.DWord, + _ => WatchSize.Separator + }; } public char TypeAsChar { get { - switch (Type) + return Type switch { - default: - case DisplayType.Separator: - return '_'; - case DisplayType.Unsigned: - return 'u'; - case DisplayType.Signed: - return 's'; - case DisplayType.Hex: - return 'h'; - case DisplayType.Binary: - return 'b'; - case DisplayType.FixedPoint_12_4: - return '1'; - case DisplayType.FixedPoint_20_12: - return '2'; - case DisplayType.FixedPoint_16_16: - return '3'; - case DisplayType.Float: - return 'f'; - } + DisplayType.Separator => '_', + DisplayType.Unsigned => 'u', + DisplayType.Signed => 's', + DisplayType.Hex => 'h', + DisplayType.Binary => 'b', + DisplayType.FixedPoint_12_4 => '1', + DisplayType.FixedPoint_20_12 => '2', + DisplayType.FixedPoint_16_16 => '3', + DisplayType.Float => 'f', + _ => '_' + }; } } public static DisplayType DisplayTypeFromChar(char c) { - switch (c) + return c switch { - default: - case '_': - return DisplayType.Separator; - case 'u': - return DisplayType.Unsigned; - case 's': - return DisplayType.Signed; - case 'h': - return DisplayType.Hex; - case 'b': - return DisplayType.Binary; - case '1': - return DisplayType.FixedPoint_12_4; - case '2': - return DisplayType.FixedPoint_20_12; - case '3': - return DisplayType.FixedPoint_16_16; - case 'f': - return DisplayType.Float; - } + '_' => DisplayType.Separator, + 'u' => DisplayType.Unsigned, + 's' => DisplayType.Signed, + 'h' => DisplayType.Hex, + 'b' => DisplayType.Binary, + '1' => DisplayType.FixedPoint_12_4, + '2' => DisplayType.FixedPoint_20_12, + '3' => DisplayType.FixedPoint_16_16, + 'f' => DisplayType.Float, + _ => DisplayType.Separator + }; } } } diff --git a/BizHawk.Client.Common/tools/Watch/WatchList/WatchEqualityComparer.cs b/BizHawk.Client.Common/tools/Watch/WatchList/WatchEqualityComparer.cs index 6e6ea8af05..4b3140f47a 100644 --- a/BizHawk.Client.Common/tools/Watch/WatchList/WatchEqualityComparer.cs +++ b/BizHawk.Client.Common/tools/Watch/WatchList/WatchEqualityComparer.cs @@ -20,12 +20,7 @@ namespace BizHawk.Client.Common { if (ReferenceEquals(x, null)) { - if (ReferenceEquals(y, null)) - { - return true; - } - - return false; + return ReferenceEquals(y, null); } if (ReferenceEquals(y, null)) @@ -33,12 +28,7 @@ namespace BizHawk.Client.Common return false; } - if (ReferenceEquals(x, y)) - { - return true; - } - - return false; + return ReferenceEquals(x, y); } /// @@ -46,10 +36,7 @@ namespace BizHawk.Client.Common /// /// Watch to get hash /// int that can serves as a unique representation of current Watch - public int GetHashCode(Watch obj) - { - return obj.GetHashCode(); - } + public int GetHashCode(Watch obj) => obj.GetHashCode(); } } } diff --git a/BizHawk.Client.Common/tools/Watch/WordWatch.cs b/BizHawk.Client.Common/tools/Watch/WordWatch.cs index 76122a4749..b3ecda30e4 100644 --- a/BizHawk.Client.Common/tools/Watch/WordWatch.cs +++ b/BizHawk.Client.Common/tools/Watch/WordWatch.cs @@ -55,10 +55,7 @@ namespace BizHawk.Client.Common /// Get a list a that can be used for this /// /// An enumeration that contains all valid - public override IEnumerable AvailableTypes() - { - return ValidTypes; - } + public override IEnumerable AvailableTypes() => ValidTypes; /// /// Reset the previous value; set it to the current one @@ -193,20 +190,19 @@ namespace BizHawk.Client.Common // TODO: Implements IFormattable public string FormatValue(ushort val) { - switch (Type) + return Type switch { - default: - case DisplayType.Unsigned: - return val.ToString(); - case DisplayType.Signed: - return ((short)val).ToString(); - case DisplayType.Hex: - return $"{val:X4}"; - case DisplayType.FixedPoint_12_4: - return $"{val / 16.0:F4}"; - case DisplayType.Binary: - return Convert.ToString(val, 2).PadLeft(16, '0').Insert(8, " ").Insert(4, " ").Insert(14, " "); - } + DisplayType.Unsigned => val.ToString(), + DisplayType.Signed => ((short) val).ToString(), DisplayType.Hex => $"{val:X4}", + DisplayType.FixedPoint_12_4 => $"{val / 16.0:F4}", + DisplayType.Binary => Convert + .ToString(val, 2) + .PadLeft(16, '0') + .Insert(8, " ") + .Insert(4, " ") + .Insert(14, " "), + _ => val.ToString() + }; } /// diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index a42094aa21..f3d2e74ffc 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -341,6 +341,7 @@ True True True + True True True True