Cleanup some more files

This commit is contained in:
YoshiRulz 2020-01-24 09:37:15 +10:00
parent 3ae58fd9d6
commit 4ed402fc59
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
6 changed files with 84 additions and 165 deletions

View File

@ -1,22 +1,12 @@
#nullable disable namespace BizHawk.Common
namespace BizHawk.Common
{ {
public static class Colors public static class Colors
{ {
public static int ARGB(byte red, byte green, byte blue) /// <remarks>This is just <c>Color.FromArgb(alpha, red, green, blue).ToArgb()</c> with extra steps.</remarks>
{ public static int ARGB(byte red, byte green, byte blue, byte alpha = 0xFF) => unchecked((int) ((uint) (alpha << 24) | (uint) (red << 16) | (uint) (green << 8) | blue));
return (int)((uint)((red << 0x10) | (green << 8) | blue | (0xFF << 0x18)));
}
public static int ARGB(byte red, byte green, byte blue, byte alpha) #if false
{ public static int Luminosity(byte lum) => ARGB(lum, lum, lum);
return (int)((uint)((red << 0x10) | (green << 8) | blue | (alpha << 0x18))); #endif
}
public static int Luminosity(byte lum)
{
return (int)((uint)((lum << 0x10) | (lum << 8) | lum | (0xFF << 0x18)));
}
} }
} }

View File

@ -1,107 +1,58 @@
#nullable disable using System;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace BizHawk.Common namespace BizHawk.Common
{ {
/// <summary> /// <summary>Wrapper over <see cref="WorkingDictionary{TKey, TValue}">WorkingDictionary</see>&lt;<paramref name="TKey"/>, <see cref="List{T}">List</see>&lt;<paramref name="TValue"/>>>.</summary>
/// A dictionary that creates new values on the fly as necessary so that any key you need will be defined.
/// </summary>
/// <typeparam name="K">dictionary keys</typeparam>
/// <typeparam name="V">dictionary values</typeparam>
[Serializable] [Serializable]
public class WorkingDictionary<K, V> : Dictionary<K, V> where V : new() public class Bag<TKey, TValue> : IEnumerable<TValue> where TKey : notnull
{ {
public new V this[K key] private readonly WorkingDictionary<TKey, List<TValue>> dictionary = new WorkingDictionary<TKey, List<TValue>>();
public IList<TKey> Keys => dictionary.Keys.ToList();
public List<TValue> this[TKey key]
{ {
get #pragma warning disable CS8603 // the only call to the index setter of `dictionary` is this index setter, which only takes non-null `List<TValue>`s
{ get => dictionary[key];
V temp; #pragma warning restore CS8603
if (!TryGetValue(key, out temp)) set => dictionary[key] = value;
{
temp = this[key] = new V();
}
return temp;
}
set
{
base[key] = value;
}
} }
public WorkingDictionary() { } public void Add(TKey key, IEnumerable<TValue> val) => this[key].AddRange(val);
protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } public void Add(TKey key, TValue val) => this[key].Add(val);
public bool ContainsKey(TKey key) => dictionary.ContainsKey(key);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<TValue> GetEnumerator() => dictionary.Values.SelectMany(lv => lv).GetEnumerator();
public IEnumerator<KeyValuePair<TKey, List<TValue>>> GetKVPEnumerator() => dictionary.GetEnumerator();
} }
/// <summary> /// <summary>A dictionary whose index getter creates an entry if the requested key isn't part of the collection, making it always safe to use the returned value. The new entry's value will be the result of the default constructor of <typeparamref name="TValue"/>.</summary>
/// a Dictionary-of-lists with key K and values List&lt;V&gt;
/// </summary>
[Serializable] [Serializable]
public class Bag<K, V> : BagBase<K, V, Dictionary<K, List<V>>, List<V>> { } public class WorkingDictionary<TKey, TValue> : Dictionary<TKey, TValue>
where TKey : notnull
/// <summary> where TValue : new()
/// base class for Bag and SortedBag
/// </summary>
/// <typeparam name="K">dictionary keys</typeparam>
/// <typeparam name="V">list values</typeparam>
/// <typeparam name="D">dictionary type</typeparam>
/// <typeparam name="L">list type</typeparam>
[Serializable]
public class BagBase<K, V, D, L> : IEnumerable<V>
where D : IDictionary<K, L>, new()
where L : IList<V>, IEnumerable<V>, new()
{ {
readonly D dictionary = new D(); public WorkingDictionary() {}
public void Add(K key, V val)
protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) {}
[property: MaybeNull]
public new TValue this[TKey key]
{ {
this[key].Add(val); get => TryGetValue(key, out var temp)
} ? temp
: (base[key] = new TValue());
public void Add(K key, L val) set => base[key] = value;
{
foreach (var v in val)
this[key].Add(v);
}
public bool ContainsKey(K key) { return dictionary.ContainsKey(key); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public IEnumerator<V> GetEnumerator()
{
return dictionary.Values.SelectMany(lv => lv).GetEnumerator();
}
public IEnumerable KeyValuePairEnumerator { get { return dictionary; } }
/// <summary>
/// Gets the list of keys contained herein
/// </summary>
public IList<K> Keys { get { return new List<K>(dictionary.Keys); } }
public L this[K key]
{
get
{
L slot;
if (!dictionary.TryGetValue(key, out slot))
{
dictionary[key] = slot = new L();
}
return slot;
}
set
{
dictionary[key] = value;
}
} }
} }
} }

View File

@ -1,74 +1,61 @@
#nullable disable using System;
using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Reflection; using System.Reflection;
namespace BizHawk.Common namespace BizHawk.Common
{ {
public class DescribableEnumConverter : EnumConverter public class DescribableEnumConverter : EnumConverter
{ {
private Type enumType; private readonly Type enumType;
public DescribableEnumConverter(Type type) : base(type) public DescribableEnumConverter(Type type) : base(type)
{ {
enumType = type; enumType = type;
} }
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType) => srcType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) => destType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{ {
return destType == typeof(string); var valueStr = value?.ToString() ?? throw new ArgumentException($"got null {nameof(value)}");
return Enum.Parse(
enumType,
enumType.GetFields(BindingFlags.Public | BindingFlags.Static)
.FirstOrDefault(fi => valueStr.Equals((fi.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute)?.Name))?.Name
?? valueStr
);
} }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
object value, Type destType)
{ {
var fi = enumType.GetField(Enum.GetName(enumType, value)); var fieldName = Enum.GetName(enumType, value ?? throw new ArgumentException($"got null {nameof(value)}"));
var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute)); if (fieldName != null)
if (attr != null)
return attr.Name;
else
return value.ToString();
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
{
return srcType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
object value)
{
foreach (var fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
{ {
var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute)); var fieldInfo = enumType.GetField(fieldName);
if (attr != null && attr.Name.Equals(value)) if (fieldInfo != null)
return Enum.Parse(enumType, fi.Name); {
var found = fieldInfo.GetCustomAttribute(typeof(DisplayAttribute));
if (found is DisplayAttribute da) return da.Name;
}
} }
return Enum.Parse(enumType, (string)value); #pragma warning disable CS8603 // value can't be null here, it would've thrown already
return value.ToString();
#pragma warning restore CS8603
} }
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(
{ enumType.GetFields(BindingFlags.Public | BindingFlags.Static)
return true; .Select(fi => fi.GetValue(null))
} .ToList()
);
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
{
var ret = new List<object>();
foreach (var fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
{
ret.Add(fi.GetValue(null));
}
return new StandardValuesCollection(ret);
}
} }
} }

View File

@ -1,11 +1,8 @@
#nullable disable using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security.AccessControl;
namespace BizHawk.Common namespace BizHawk.Common
{ {
@ -23,7 +20,7 @@ namespace BizHawk.Common
{ {
private static readonly Lazy<IEnumerable<string>> asdf = new Lazy<IEnumerable<string>>(() => private static readonly Lazy<IEnumerable<string>> asdf = new Lazy<IEnumerable<string>>(() =>
{ {
var currDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:", ""); var currDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)?.Replace("file:", "") ?? string.Empty;
return new[] { "/usr/lib/", "/usr/lib/bizhawk/", "./", "./dll/" }.Select(dir => dir[0] == '.' ? currDir + dir.Substring(1) : dir); return new[] { "/usr/lib/", "/usr/lib/bizhawk/", "./", "./dll/" }.Select(dir => dir[0] == '.' ? currDir + dir.Substring(1) : dir);
}); });

View File

@ -1,12 +1,11 @@
#nullable disable using System;
using System;
namespace BizHawk.Common namespace BizHawk.Common
{ {
public interface IMonitor public interface IMonitor
{ {
void Enter(); void Enter();
void Exit(); void Exit();
} }
@ -22,7 +21,8 @@ namespace BizHawk.Common
private class EnterExitWrapper : IDisposable private class EnterExitWrapper : IDisposable
{ {
private readonly IMonitor _m; private readonly IMonitor _m;
private bool _disposed = false;
private bool _disposed;
public EnterExitWrapper(IMonitor m) public EnterExitWrapper(IMonitor m)
{ {

View File

@ -722,13 +722,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public List<CartInfo> Identify(string sha1) public List<CartInfo> Identify(string sha1)
{ {
lock (syncroot) lock (syncroot) return sha1_table[sha1];
{
if (!sha1_table.ContainsKey(sha1))
return new List<CartInfo>();
else
return sha1_table[sha1];
}
} }
} }
} }