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 int ARGB(byte red, byte green, byte blue)
{
return (int)((uint)((red << 0x10) | (green << 8) | blue | (0xFF << 0x18)));
}
/// <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));
public static int ARGB(byte red, byte green, byte blue, byte alpha)
{
return (int)((uint)((red << 0x10) | (green << 8) | blue | (alpha << 0x18)));
}
public static int Luminosity(byte lum)
{
return (int)((uint)((lum << 0x10) | (lum << 8) | lum | (0xFF << 0x18)));
}
#if false
public static int Luminosity(byte lum) => ARGB(lum, lum, lum);
#endif
}
}

View File

@ -1,107 +1,58 @@
#nullable disable
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
namespace BizHawk.Common
{
/// <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>
/// <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>
[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
{
V temp;
if (!TryGetValue(key, out temp))
{
temp = this[key] = new V();
}
return temp;
}
set
{
base[key] = value;
}
#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];
#pragma warning restore CS8603
set => dictionary[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>
/// a Dictionary-of-lists with key K and values List&lt;V&gt;
/// </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>
[Serializable]
public class Bag<K, V> : BagBase<K, V, Dictionary<K, List<V>>, List<V>> { }
/// <summary>
/// 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()
public class WorkingDictionary<TKey, TValue> : Dictionary<TKey, TValue>
where TKey : notnull
where TValue : new()
{
readonly D dictionary = new D();
public void Add(K key, V val)
public WorkingDictionary() {}
protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) {}
[property: MaybeNull]
public new TValue this[TKey key]
{
this[key].Add(val);
}
public void Add(K key, L val)
{
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;
}
get => TryGetValue(key, out var temp)
? temp
: (base[key] = new TValue());
set => base[key] = value;
}
}
}

View File

@ -1,74 +1,61 @@
#nullable disable
using System;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace BizHawk.Common
{
public class DescribableEnumConverter : EnumConverter
{
private Type enumType;
private readonly Type enumType;
public DescribableEnumConverter(Type type) : base(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,
object value, Type destType)
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
{
var fi = enumType.GetField(Enum.GetName(enumType, value));
var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute));
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 fieldName = Enum.GetName(enumType, value ?? throw new ArgumentException($"got null {nameof(value)}"));
if (fieldName != null)
{
var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute));
if (attr != null && attr.Name.Equals(value))
return Enum.Parse(enumType, fi.Name);
var fieldInfo = enumType.GetField(fieldName);
if (fieldInfo != null)
{
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)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(
enumType.GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(fi => fi.GetValue(null))
.ToList()
);
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
var ret = new List<object>();
foreach (var fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
{
ret.Add(fi.GetValue(null));
}
return new StandardValuesCollection(ret);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
}
}

View File

@ -1,11 +1,8 @@
#nullable disable
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
namespace BizHawk.Common
{
@ -23,7 +20,7 @@ namespace BizHawk.Common
{
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);
});

View File

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

View File

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