refactor: Update Unity to 2022.3.62

This commit is contained in:
2025-06-21 01:22:02 +08:00
parent d71bf7d4a5
commit 283783954f
112 changed files with 778 additions and 907 deletions

View File

@@ -20,13 +20,13 @@ namespace Cryville.Common {
/// <param name="succeeded">Whether the task has succeeded.</param> /// <param name="succeeded">Whether the task has succeeded.</param>
/// <param name="result">The result.</param> /// <param name="result">The result.</param>
public void Deliver(bool succeeded, T result) { public void Deliver(bool succeeded, T result) {
if (Destination != null) Destination(succeeded, result); Destination?.Invoke(succeeded, result);
} }
/// <summary> /// <summary>
/// Cancels the task. /// Cancels the task.
/// </summary> /// </summary>
public void Cancel() { public void Cancel() {
if (CancelSource != null) CancelSource(); CancelSource?.Invoke();
} }
} }
} }

View File

@@ -35,8 +35,8 @@ namespace Cryville.Common {
return null; return null;
else if (type.IsAssignableFrom(value.GetType())) else if (type.IsAssignableFrom(value.GetType()))
return value; return value;
else if (type.IsEnum && value is string) { else if (type.IsEnum && value is string strValue) {
return Enum.Parse(type, (string)value); return Enum.Parse(type, strValue);
} }
throw new InvalidCastException(string.Format("Cannot cast {0} to {1}", value.GetType(), type)); throw new InvalidCastException(string.Format("Cannot cast {0} to {1}", value.GetType(), type));
} }

View File

@@ -4,7 +4,7 @@ using System.Diagnostics;
namespace Cryville.Common { namespace Cryville.Common {
public class Coroutine { public class Coroutine {
readonly IEnumerator<float> _enumerator; readonly IEnumerator<float> _enumerator;
readonly Stopwatch _stopwatch = new Stopwatch(); readonly Stopwatch _stopwatch = new();
public float Progress { get; private set; } public float Progress { get; private set; }
public Coroutine(IEnumerator<float> enumerator) { public Coroutine(IEnumerator<float> enumerator) {
_enumerator = enumerator; _enumerator = enumerator;

View File

@@ -16,13 +16,11 @@ namespace Cryville.Common.Font {
} }
public void Close() { Reader.Close(); } public void Close() { Reader.Close(); }
public static FontFile Create(FileInfo file) { public static FontFile Create(FileInfo file) => file.Extension switch {
switch (file.Extension) { ".ttf" or ".otf" => new FontFileTTF(file),
case ".ttf": case ".otf": return new FontFileTTF(file); ".ttc" or ".otc" => new FontFileTTC(file),
case ".ttc": case ".otc": return new FontFileTTC(file); _ => null,
default: return null; };
}
}
public Enumerator GetEnumerator() { public Enumerator GetEnumerator() {
return new Enumerator(this); return new Enumerator(this);
@@ -42,7 +40,7 @@ namespace Cryville.Common.Font {
_index = -1; _index = -1;
} }
public Typeface Current { public readonly Typeface Current {
get { get {
if (_index < 0) if (_index < 0)
throw new InvalidOperationException(_index == -1 ? "Enum not started" : "Enum ended"); throw new InvalidOperationException(_index == -1 ? "Enum not started" : "Enum ended");
@@ -50,7 +48,7 @@ namespace Cryville.Common.Font {
} }
} }
object IEnumerator.Current { get { return Current; } } readonly object IEnumerator.Current => Current;
public void Dispose() { public void Dispose() {
_index = -2; _index = -2;

View File

@@ -17,8 +17,7 @@ namespace Cryville.Common.Font {
Shared.Logger.Log(3, "UI", "Discarding a font with a duplicate full name {0}", f.FullName); Shared.Logger.Log(3, "UI", "Discarding a font with a duplicate full name {0}", f.FullName);
continue; continue;
} }
List<Typeface> set2; if (!map2.TryGetValue(f.FamilyName, out List<Typeface> set2)) {
if (!map2.TryGetValue(f.FamilyName, out set2)) {
map2.Add(f.FamilyName, set2 = new List<Typeface>()); map2.Add(f.FamilyName, set2 = new List<Typeface>());
} }
set2.Add(f); set2.Add(f);

View File

@@ -12,7 +12,7 @@ namespace Cryville.Common.Font {
public class FallbackListFontMatcher : FontMatcher { public class FallbackListFontMatcher : FontMatcher {
readonly LanguageMatching _matcher; readonly LanguageMatching _matcher;
static readonly string UltimateFallbackScript = "zzzz"; static readonly string UltimateFallbackScript = "zzzz";
public Dictionary<string, List<string>> MapScriptToTypefaces = new Dictionary<string, List<string>>(); public Dictionary<string, List<string>> MapScriptToTypefaces = new();
public static Dictionary<string, List<string>> GetDefaultWindowsFallbackMap() { public static Dictionary<string, List<string>> GetDefaultWindowsFallbackMap() {
var map = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); var map = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
FillKeysWithScripts(map, () => new List<string>()); FillKeysWithScripts(map, () => new List<string>());
@@ -333,8 +333,7 @@ namespace Cryville.Common.Font {
yield return typeface1; yield return typeface1;
} }
if (distinctFamily) continue; if (distinctFamily) continue;
IReadOnlyCollection<Typeface> typefaces2; if (Manager.MapNameToTypefaces.TryGetValue(candidate, out IReadOnlyCollection<Typeface> typefaces2)) {
if (Manager.MapNameToTypefaces.TryGetValue(candidate, out typefaces2)) {
foreach (var typeface in typefaces2) { foreach (var typeface in typefaces2) {
if (typeface1 == typeface) continue; if (typeface1 == typeface) continue;
yield return typeface; yield return typeface;

View File

@@ -25,7 +25,7 @@ namespace Cryville.Common.Font {
readonly UInt16 majorVersion; readonly UInt16 majorVersion;
readonly UInt16 minorVersion; readonly UInt16 minorVersion;
readonly UInt32 numFonts; readonly UInt32 numFonts;
readonly List<UInt32> tableDirectoryOffsets = new List<UInt32>(); readonly List<UInt32> tableDirectoryOffsets = new();
#pragma warning disable IDE0052 // Reserved #pragma warning disable IDE0052 // Reserved
readonly String dsigTag; readonly String dsigTag;
readonly UInt32 dsigLength; readonly UInt32 dsigLength;
@@ -61,7 +61,7 @@ namespace Cryville.Common.Font {
readonly UInt16 entrySelector; readonly UInt16 entrySelector;
readonly UInt16 rangeShift; readonly UInt16 rangeShift;
#pragma warning restore IDE0052 // Reserved #pragma warning restore IDE0052 // Reserved
readonly List<TableRecord> tableRecords = new List<TableRecord>(); readonly List<TableRecord> tableRecords = new();
public TableDirectory(BinaryReader reader, UInt32 offset) : base(reader, offset) { public TableDirectory(BinaryReader reader, UInt32 offset) : base(reader, offset) {
sfntVersion = reader.ReadUInt32(); sfntVersion = reader.ReadUInt32();
if (sfntVersion != 0x00010000 && sfntVersion != 0x4F54544F && if (sfntVersion != 0x00010000 && sfntVersion != 0x4F54544F &&
@@ -81,13 +81,11 @@ namespace Cryville.Common.Font {
public override IReadOnlyList<TableRecord> GetItems() { public override IReadOnlyList<TableRecord> GetItems() {
return tableRecords; return tableRecords;
} }
public override object GetSubTable(TableRecord item) { public override object GetSubTable(TableRecord item) => item.tableTag switch {
switch (item.tableTag) { "name" => new NameTable(Reader, item.offset),
case "name": return new NameTable(Reader, item.offset); "meta" => new MetaTable(Reader, item.offset),
case "meta": return new MetaTable(Reader, item.offset); _ => throw new NotImplementedException(),
default: throw new NotImplementedException(); };
}
}
} }
public struct TableRecord { public struct TableRecord {
public string tableTag; public string tableTag;
@@ -99,9 +97,9 @@ namespace Cryville.Common.Font {
readonly UInt16 version; readonly UInt16 version;
readonly UInt16 count; readonly UInt16 count;
readonly UInt16 storageOffset; readonly UInt16 storageOffset;
readonly List<NameRecord> nameRecord = new List<NameRecord>(); readonly List<NameRecord> nameRecord = new();
readonly UInt16 langTagCount; readonly UInt16 langTagCount;
readonly List<LangTagRecord> langTagRecord = new List<LangTagRecord>(); readonly List<LangTagRecord> langTagRecord = new();
public NameTable(BinaryReader reader, UInt32 offset) : base(reader, offset) { public NameTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
version = reader.ReadUInt16(); version = reader.ReadUInt16();
count = reader.ReadUInt16(); count = reader.ReadUInt16();
@@ -213,7 +211,7 @@ namespace Cryville.Common.Font {
readonly UInt32 flags; readonly UInt32 flags;
#pragma warning restore IDE0052 // Reserved #pragma warning restore IDE0052 // Reserved
readonly UInt32 dataMapCount; readonly UInt32 dataMapCount;
readonly List<DataMap> dataMaps = new List<DataMap>(); readonly List<DataMap> dataMaps = new();
public MetaTable(BinaryReader reader, UInt32 offset) : base(reader, offset) { public MetaTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
version = reader.ReadUInt32(); version = reader.ReadUInt32();
if (version != 1) throw new NotSupportedException(); if (version != 1) throw new NotSupportedException();

View File

@@ -25,7 +25,7 @@ namespace Cryville.Common {
/// <param name="encoding">The encoding of the string.</param> /// <param name="encoding">The encoding of the string.</param>
/// <returns>The string read from the reader.</returns> /// <returns>The string read from the reader.</returns>
public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) { public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) {
if (encoding == null) encoding = Encoding.UTF8; encoding ??= Encoding.UTF8;
var len = reader.ReadUInt16(); var len = reader.ReadUInt16();
byte[] buffer = reader.ReadBytes(len); byte[] buffer = reader.ReadBytes(len);
return encoding.GetString(buffer); return encoding.GetString(buffer);
@@ -38,7 +38,7 @@ namespace Cryville.Common {
/// <param name="value">The string to write by the writer.</param> /// <param name="value">The string to write by the writer.</param>
/// <param name="encoding">The encoding of the string.</param> /// <param name="encoding">The encoding of the string.</param>
public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) { public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) {
if (encoding == null) encoding = Encoding.UTF8; encoding ??= Encoding.UTF8;
byte[] buffer = encoding.GetBytes(value); byte[] buffer = encoding.GetBytes(value);
writer.Write((ushort)buffer.Length); writer.Write((ushort)buffer.Length);
writer.Write(buffer); writer.Write(buffer);

View File

@@ -2,26 +2,26 @@ using System;
namespace Cryville.Common { namespace Cryville.Common {
public struct Identifier : IEquatable<Identifier> { public struct Identifier : IEquatable<Identifier> {
public static Identifier Empty = new Identifier(0); public static Identifier Empty = new(0);
public int Key { get; private set; } public int Key { get; private set; }
public object Name { get { return IdentifierManager.Shared.Retrieve(Key); } } public readonly object Name => IdentifierManager.Shared.Retrieve(Key);
public Identifier(int key) { public Identifier(int key) {
Key = key; Key = key;
} }
public Identifier(object name) { public Identifier(object name) {
Key = IdentifierManager.Shared.Request(name); Key = IdentifierManager.Shared.Request(name);
} }
public override bool Equals(object obj) { public override readonly bool Equals(object obj) {
if (obj == null || !(obj is Identifier)) return false; if (obj == null || obj is not Identifier other) return false;
return Equals((Identifier)obj); return Equals(other);
} }
public bool Equals(Identifier other) { public readonly bool Equals(Identifier other) {
return Key == other.Key; return Key == other.Key;
} }
public override int GetHashCode() { public override readonly int GetHashCode() {
return Key; return Key;
} }
public override string ToString() { public override readonly string ToString() {
if (Key == 0) return ""; if (Key == 0) return "";
return Name.ToString(); return Name.ToString();
} }

View File

@@ -42,13 +42,13 @@ namespace Cryville.Common.Math {
} }
} }
/// <summary> /// <summary>
/// Performs dot operation with a <see cref="System.Single" /> column vector. /// Performs dot operation with a <see cref="float" /> column vector.
/// </summary> /// </summary>
/// <param name="lhs">The lefthand column vector.</param> /// <param name="lhs">The lefthand column vector.</param>
/// <param name="o">The vector operator.</param> /// <param name="o">The vector operator.</param>
/// <returns>The result of the dot operation.</returns> /// <returns>The result of the dot operation.</returns>
public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) { public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) {
T res = default(T); T res = default;
for (var i = 0; i < Size; i++) for (var i = 0; i < Size; i++)
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i])); res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
return res; return res;

View File

@@ -71,8 +71,7 @@ namespace Cryville.Common.Network.Http11 {
headers["Host"] = _baseUri.Host; headers["Host"] = _baseUri.Host;
byte[] payload = null; byte[] payload = null;
if (body != null) { if (body != null) {
if (encoding == null) encoding ??= Encoding.UTF8;
encoding = Encoding.UTF8;
payload = encoding.GetBytes(body); payload = encoding.GetBytes(body);
headers.Add("Content-Encoding", encoding.EncodingName); headers.Add("Content-Encoding", encoding.EncodingName);
headers.Add("Content-Length", payload.Length.ToString(CultureInfo.InvariantCulture)); headers.Add("Content-Length", payload.Length.ToString(CultureInfo.InvariantCulture));

View File

@@ -56,7 +56,7 @@ namespace Cryville.Common.Network.Http11 {
} }
internal static string ReadLine(BinaryReader reader) { internal static string ReadLine(BinaryReader reader) {
StringBuilder result = new StringBuilder(); StringBuilder result = new();
char c; char c;
while (true) { while (true) {
c = reader.ReadChar(); c = reader.ReadChar();

View File

@@ -72,8 +72,7 @@ namespace Cryville.Common.Network.Http11 {
public void ReadChunk() { public void ReadChunk() {
if (_chunk != null && _chunk.Length == 0) return; if (_chunk != null && _chunk.Length == 0) return;
string[] chunkHeader = Http11Response.ReadLine(_reader).Split(';'); string[] chunkHeader = Http11Response.ReadLine(_reader).Split(';');
int chunkSize; if (!int.TryParse(chunkHeader[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int chunkSize))
if (!int.TryParse(chunkHeader[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out chunkSize))
throw new IOException("Corrupted chunk received"); throw new IOException("Corrupted chunk received");
if (chunkSize == 0) { if (chunkSize == 0) {
_chunk = new byte[0]; _chunk = new byte[0];
@@ -106,7 +105,7 @@ namespace Cryville.Common.Network.Http11 {
} }
public override byte[] ReadToEnd() { public override byte[] ReadToEnd() {
if (_chunk.Length == 0) return new byte[0]; if (_chunk.Length == 0) return new byte[0];
List<byte[]> segs = new List<byte[]>(); List<byte[]> segs = new();
while (true) { while (true) {
if (_pos != 0) { if (_pos != 0) {
var buffer = new byte[_chunk.Length - _pos]; var buffer = new byte[_chunk.Length - _pos];

View File

@@ -56,9 +56,9 @@ namespace Cryville.Common.Pdt {
while (ip != null) { while (ip != null) {
bool nextFlag = false; bool nextFlag = false;
var i = ip.Value; var i = ip.Value;
if (i is PdtInstruction.Operate) { if (i is PdtInstruction.Operate iop) {
int fc0 = _framecount; int fc0 = _framecount;
int fc1 = ((PdtInstruction.Operate)i).Signature.ParamCount; int fc1 = iop.Signature.ParamCount;
try { i.Execute(this, ref ip); } catch (Exception) { } try { i.Execute(this, ref ip); } catch (Exception) { }
if (fc0 - _framecount == fc1) { if (fc0 - _framecount == fc1) {
unsafe { unsafe {
@@ -77,8 +77,7 @@ namespace Cryville.Common.Pdt {
} }
} }
} }
else if (i is PdtInstruction.Collapse) { else if (i is PdtInstruction.Collapse t) {
var t = (PdtInstruction.Collapse)i;
try { try {
var pins = ip; var pins = ip;
i.Execute(this, ref ip); i.Execute(this, ref ip);
@@ -133,15 +132,14 @@ namespace Cryville.Common.Pdt {
exp.IsConstant = true; exp.IsConstant = true;
exp.IsPotentialConstant = true; exp.IsPotentialConstant = true;
for (var ins = il.First; ins != null; ins = ins.Next) { for (var ins = il.First; ins != null; ins = ins.Next) {
if (!(ins.Value is PdtInstruction.PushConstant)) { if (ins.Value is not PdtInstruction.PushConstant) {
exp.IsConstant = false; exp.IsConstant = false;
break; break;
} }
} }
} }
void ReplaceIP(LinkedList<PdtInstruction> il, ref LinkedListNode<PdtInstruction> ip, PdtInstruction ins, Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>> cols) { void ReplaceIP(LinkedList<PdtInstruction> il, ref LinkedListNode<PdtInstruction> ip, PdtInstruction ins, Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>> cols) {
List<PdtInstruction.Collapse> cins; if (cols.TryGetValue(ip, out List<PdtInstruction.Collapse> cins)) cols.Remove(ip);
if (cols.TryGetValue(ip, out cins)) cols.Remove(ip);
ip = il.AddAfter(ip, ins); ip = il.AddAfter(ip, ins);
il.Remove(ip.Previous); il.Remove(ip.Previous);
if (cins != null) cols.Add(ip, cins); if (cins != null) cols.Add(ip, cins);
@@ -163,8 +161,7 @@ namespace Cryville.Common.Pdt {
} }
internal unsafe void PushVariable(int name, bool forced) { internal unsafe void PushVariable(int name, bool forced) {
fixed (StackFrame* frame = &_stack[_framecount++]) { fixed (StackFrame* frame = &_stack[_framecount++]) {
byte[] value; GetVariable(name, forced, out frame->Type, out byte[] value);
GetVariable(name, forced, out frame->Type, out value);
frame->Offset = _goffset; frame->Offset = _goffset;
frame->Length = value.Length; frame->Length = value.Length;
Array.Copy(value, 0, _mem, _goffset, value.Length); Array.Copy(value, 0, _mem, _goffset, value.Length);

View File

@@ -114,7 +114,7 @@ namespace Cryville.Common.Pdt {
} }
} }
public partial class PdtInterpreter { public partial class PdtInterpreter {
static readonly Dictionary<char, int> OP_PRIORITY = new Dictionary<char, int> { static readonly Dictionary<char, int> OP_PRIORITY = new() {
{ '@', 7 }, { '@', 7 },
{ '*', 6 }, { '/', 6 }, { '%', 6 }, { '*', 6 }, { '/', 6 }, { '%', 6 },
{ '+', 5 }, { '-', 5 }, { '+', 5 }, { '-', 5 },
@@ -125,7 +125,7 @@ namespace Cryville.Common.Pdt {
{ ',', 0 }, { ',', 0 },
{ '$', -1 }, { '$', -1 },
}; };
static readonly Dictionary<char, int> OP_TYPE = new Dictionary<char, int> { static readonly Dictionary<char, int> OP_TYPE = new() {
{ '@', 0 }, { '@', 0 },
{ '*', 0 }, { '/', 0 }, { '%', 0 }, { '*', 0 }, { '/', 0 }, { '%', 0 },
{ '+', 0 }, { '-', 0 }, { '+', 0 }, { '-', 0 },
@@ -153,10 +153,10 @@ namespace Cryville.Common.Pdt {
private struct PdtExpToken { private struct PdtExpToken {
public CharCategory Type { get; set; } public CharCategory Type { get; set; }
public string Value { get; set; } public string Value { get; set; }
public override string ToString() { public override readonly string ToString() {
return string.Format("0x{0:x4}: {1}", Type, Value); return string.Format("0x{0:x4}: {1}", Type, Value);
} }
public static readonly PdtExpToken EmptyOperator = new PdtExpToken { public static readonly PdtExpToken EmptyOperator = new() {
Type = CharCategory.Operator, Type = CharCategory.Operator,
Value = "$", Value = "$",
}; };
@@ -249,13 +249,12 @@ namespace Cryville.Common.Pdt {
PdtExpToken? buf = null; PdtExpToken? buf = null;
while (true) { while (true) {
if (buf != null && t.Type != CharCategory.OpeningBracket) { if (buf != null && t.Type != CharCategory.OpeningBracket) {
PdtExpression def; if (defs.TryGetValue(buf.Value.Value, out PdtExpression def)) {
if (defs.TryGetValue(buf.Value.Value, out def)) {
foreach (var i in def.Instructions) ins.AddLast(i); foreach (var i in def.Instructions) ins.AddLast(i);
} }
else { else {
var name = buf.Value.Value; var name = buf.Value.Value;
if (name[0] == '?') ins.AddLast(new PdtInstruction.PushVariable(name.Substring(1), true)); if (name[0] == '?') ins.AddLast(new PdtInstruction.PushVariable(name[1..], true));
else ins.AddLast(new PdtInstruction.PushVariable(name)); else ins.AddLast(new PdtInstruction.PushVariable(name));
} }
buf = null; buf = null;

View File

@@ -65,7 +65,7 @@ namespace Cryville.Common.Pdt {
/// </summary> /// </summary>
public int Position { get; protected set; } public int Position { get; protected set; }
readonly StringBuilder _sb = new StringBuilder(); readonly StringBuilder _sb = new();
#pragma warning disable IDE1006 #pragma warning disable IDE1006
/// <summary> /// <summary>
/// The character at the current position. /// The character at the current position.
@@ -86,7 +86,7 @@ namespace Cryville.Common.Pdt {
protected string tokenb(CharCategory flag) { protected string tokenb(CharCategory flag) {
int sp = Position; int sp = Position;
while ((ct & flag) == 0) Position++; while ((ct & flag) == 0) Position++;
return Source.Substring(sp, Position - sp); return Source[sp..Position];
} }
/// <summary> /// <summary>
/// Reads a token until a character that is not of type <paramref name="flag" /> is met. /// Reads a token until a character that is not of type <paramref name="flag" /> is met.
@@ -97,7 +97,7 @@ namespace Cryville.Common.Pdt {
protected string tokenw(CharCategory flag) { protected string tokenw(CharCategory flag) {
int sp = Position; int sp = Position;
while ((ct & flag) != 0) Position++; while ((ct & flag) != 0) Position++;
return Source.Substring(sp, Position - sp); return Source[sp..Position];
} }
/// <summary> /// <summary>
/// Skips over whitespaces. /// Skips over whitespaces.
@@ -163,7 +163,7 @@ namespace Cryville.Common.Pdt {
return new PdtExpression(ins); return new PdtExpression(ins);
} }
readonly Dictionary<string, PdtExpression> defs = new Dictionary<string, PdtExpression>(); readonly Dictionary<string, PdtExpression> defs = new();
/// <summary> /// <summary>
/// Creates an instance of the <see cref="PdtInterpreter" /> class. /// Creates an instance of the <see cref="PdtInterpreter" /> class.
/// </summary> /// </summary>
@@ -186,8 +186,7 @@ namespace Cryville.Common.Pdt {
public object Interpret(Type type) { public object Interpret(Type type) {
try { try {
if (m_formatVersion == null) InterpretDirectives(); if (m_formatVersion == null) InterpretDirectives();
if (_binder == null) _binder ??= BinderAttribute.CreateBinderOfType(type);
_binder = BinderAttribute.CreateBinderOfType(type);
return InterpretObject(type); return InterpretObject(type);
} }
catch (Exception ex) { catch (Exception ex) {
@@ -256,18 +255,17 @@ namespace Cryville.Common.Pdt {
} }
void InterpretObjectInternal<T>(bool pcflag, Type type, object pkey, object result, Func<Type, object> vfunc) where T : Attribute { void InterpretObjectInternal<T>(bool pcflag, Type type, object pkey, object result, Func<Type, object> vfunc) where T : Attribute {
if (pcflag) { if (pcflag) {
using (var collection = new PairCollection(result)) { using var collection = new PairCollection(result);
var ktype = type.GetGenericArguments()[0]; var ktype = type.GetGenericArguments()[0];
var ptype = type.GetGenericArguments()[1]; var ptype = type.GetGenericArguments()[1];
object key = _binder.ChangeType(pkey, ktype, null); object key = _binder.ChangeType(pkey, ktype, null);
object value = vfunc(ptype); object value = vfunc(ptype);
collection.Add(key, value); collection.Add(key, value);
} }
}
else { else {
MemberInfo prop = null; MemberInfo prop = null;
bool flag = false; bool flag = false;
if (pkey is string) prop = FieldLikeHelper.GetMember(type, (string)pkey); if (pkey is string pname) prop = FieldLikeHelper.GetMember(type, pname);
if (prop == null) { if (prop == null) {
prop = FieldLikeHelper.FindMemberWithAttribute<T>(type); prop = FieldLikeHelper.FindMemberWithAttribute<T>(type);
flag = true; flag = true;
@@ -279,14 +277,13 @@ namespace Cryville.Common.Pdt {
if (origCollection == null) { if (origCollection == null) {
FieldLikeHelper.SetValue(prop, result, origCollection = Activator.CreateInstance(ptype)); FieldLikeHelper.SetValue(prop, result, origCollection = Activator.CreateInstance(ptype));
} }
using (var collection = new PairCollection(origCollection)) { using var collection = new PairCollection(origCollection);
var ktype = ptype.GetGenericArguments()[0]; var ktype = ptype.GetGenericArguments()[0];
var vtype = ptype.GetGenericArguments()[1]; var vtype = ptype.GetGenericArguments()[1];
object key = _binder.ChangeType(pkey, ktype, null); object key = _binder.ChangeType(pkey, ktype, null);
object value = vfunc(vtype); object value = vfunc(vtype);
collection.Add(key, value); collection.Add(key, value);
} }
}
else FieldLikeHelper.SetValue(prop, result, vfunc(ptype), _binder); else FieldLikeHelper.SetValue(prop, result, vfunc(ptype), _binder);
} }
} }
@@ -326,7 +323,7 @@ namespace Cryville.Common.Pdt {
src.Take(interpreter.Position).Count(c => c == '\n') + 1, src.Take(interpreter.Position).Count(c => c == '\n') + 1,
pos - lineStartPos + 1, pos - lineStartPos + 1,
innerException == null ? "Unknown error" : innerException.Message, innerException == null ? "Unknown error" : innerException.Message,
src.Substring(previewStartPos, previewEndPos - previewStartPos) src[previewStartPos..previewEndPos]
); );
} }
} }

View File

@@ -102,17 +102,17 @@ namespace Cryville.Common.Pdt {
ParamCount = paramCount; ParamCount = paramCount;
_hash = Name ^ ((ParamCount << 16) | (ParamCount >> 16)); _hash = Name ^ ((ParamCount << 16) | (ParamCount >> 16));
} }
public override bool Equals(object obj) { public override readonly bool Equals(object obj) {
if (!(obj is PdtOperatorSignature)) return false; if (obj is not PdtOperatorSignature other) return false;
return Equals((PdtOperatorSignature)obj); return Equals(other);
} }
public bool Equals(PdtOperatorSignature other) { public readonly bool Equals(PdtOperatorSignature other) {
return Name == other.Name && ParamCount == other.ParamCount; return Name == other.Name && ParamCount == other.ParamCount;
} }
public override int GetHashCode() { public override readonly int GetHashCode() {
return _hash; return _hash;
} }
public override string ToString() { public override readonly string ToString() {
return string.Format("{0}({1})", IdentifierManager.Shared.Retrieve(Name), ParamCount); return string.Format("{0}({1})", IdentifierManager.Shared.Retrieve(Name), ParamCount);
} }
} }

View File

@@ -24,7 +24,7 @@ namespace Cryville.Common.Pdt {
/// Copies the memory in the span to another span. /// Copies the memory in the span to another span.
/// </summary> /// </summary>
/// <param name="dest">The destination span.</param> /// <param name="dest">The destination span.</param>
public void CopyTo(PdtVariableMemory dest) { public readonly void CopyTo(PdtVariableMemory dest) {
CopyTo(dest._ptr, 0, Length); CopyTo(dest._ptr, 0, Length);
} }
/// <summary> /// <summary>
@@ -32,7 +32,7 @@ namespace Cryville.Common.Pdt {
/// </summary> /// </summary>
/// <param name="dest">The destination buffer.</param> /// <param name="dest">The destination buffer.</param>
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param> /// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
public void CopyTo(byte[] dest, int destOffset) { public readonly void CopyTo(byte[] dest, int destOffset) {
fixed (byte* ptr = dest) { fixed (byte* ptr = dest) {
CopyTo(ptr, destOffset, Length); CopyTo(ptr, destOffset, Length);
} }
@@ -44,13 +44,13 @@ namespace Cryville.Common.Pdt {
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param> /// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
/// <param name="length">The length to copy.</param> /// <param name="length">The length to copy.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is greater than the length of the span.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is greater than the length of the span.</exception>
public void CopyTo(byte* dest, int destOffset, int length) { public readonly void CopyTo(byte* dest, int destOffset, int length) {
if (length > Length) throw new ArgumentOutOfRangeException("length"); if (length > Length) throw new ArgumentOutOfRangeException("length");
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
dest[destOffset + i] = _ptr[i]; dest[destOffset + i] = _ptr[i];
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(PdtVariableMemory obj) { public readonly bool Equals(PdtVariableMemory obj) {
if (Type != obj.Type || Length != obj.Length) return false; if (Type != obj.Type || Length != obj.Length) return false;
for (int i = 0; i < Length; i++) { for (int i = 0; i < Length; i++) {
if (*(_ptr + i) != *(obj._ptr + i)) return false; if (*(_ptr + i) != *(obj._ptr + i)) return false;
@@ -63,7 +63,7 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset on the span to start reading from.</param> /// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>A number.</returns> /// <returns>A number.</returns>
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception> /// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
public float AsNumber(int offset = 0) { public readonly float AsNumber(int offset = 0) {
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector) if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
throw new InvalidCastException("Not a number"); throw new InvalidCastException("Not a number");
float value; float value;
@@ -79,7 +79,7 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset from the start of the span.</param> /// <param name="offset">The offset from the start of the span.</param>
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception> /// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception> /// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
public void SetNumber(float value, int offset = 0) { public readonly void SetNumber(float value, int offset = 0) {
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector) if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
throw new InvalidCastException("Not a number"); throw new InvalidCastException("Not a number");
if (Length < sizeof(float) + offset) if (Length < sizeof(float) + offset)
@@ -94,7 +94,7 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset on the span to start reading from.</param> /// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>A string.</returns> /// <returns>A string.</returns>
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception> /// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
public string AsString(int offset = 0) { public readonly string AsString(int offset = 0) {
if (Type != PdtInternalType.String && Type != PdtInternalType.Array) if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
throw new InvalidCastException("Not a string"); throw new InvalidCastException("Not a string");
var len = *(int*)(_ptr + offset); var len = *(int*)(_ptr + offset);
@@ -107,7 +107,7 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset from the start of the span.</param> /// <param name="offset">The offset from the start of the span.</param>
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception> /// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception> /// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
public void SetString(string value, int offset = 0) { public readonly void SetString(string value, int offset = 0) {
if (Type != PdtInternalType.String && Type != PdtInternalType.Array) if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
throw new InvalidCastException("Not a string"); throw new InvalidCastException("Not a string");
int strlen = value.Length; int strlen = value.Length;
@@ -124,7 +124,7 @@ namespace Cryville.Common.Pdt {
/// <param name="offset">The offset on the span to start reading from.</param> /// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>The name of an undefined identifier.</returns> /// <returns>The name of an undefined identifier.</returns>
/// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception> /// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception>
public int AsIdentifier(int offset = 0) { public readonly int AsIdentifier(int offset = 0) {
if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array) if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an identifier"); throw new InvalidCastException("Not an identifier");
return *(int*)(_ptr + offset); return *(int*)(_ptr + offset);
@@ -140,7 +140,7 @@ namespace Cryville.Common.Pdt {
/// <remarks> /// <remarks>
/// <para>Use <see cref="AsNumber(int)" /> instead while reading an unaligned number.</para> /// <para>Use <see cref="AsNumber(int)" /> instead while reading an unaligned number.</para>
/// </remarks> /// </remarks>
public T As<T>(int offset = 0) { public readonly T As<T>(int offset = 0) {
var len = Unsafe.SizeOf<T>(); var len = Unsafe.SizeOf<T>();
if (offset >= Length) if (offset >= Length)
throw new ArgumentOutOfRangeException("offset"); throw new ArgumentOutOfRangeException("offset");
@@ -159,7 +159,7 @@ namespace Cryville.Common.Pdt {
/// <remarks> /// <remarks>
/// <para>Use <see cref="SetNumber(float, int)" /> instead while writing an unaligned number.</para> /// <para>Use <see cref="SetNumber(float, int)" /> instead while writing an unaligned number.</para>
/// </remarks> /// </remarks>
public void Set<T>(T value, int offset = 0) { public readonly void Set<T>(T value, int offset = 0) {
var len = Unsafe.SizeOf<T>(); var len = Unsafe.SizeOf<T>();
if (offset >= Length) if (offset >= Length)
throw new ArgumentOutOfRangeException("offset"); throw new ArgumentOutOfRangeException("offset");
@@ -173,7 +173,7 @@ namespace Cryville.Common.Pdt {
/// <param name="arrtype">The type of the array.</param> /// <param name="arrtype">The type of the array.</param>
/// <param name="pc">The item count of the array.</param> /// <param name="pc">The item count of the array.</param>
/// <exception cref="InvalidCastException">The span does not represent an array.</exception> /// <exception cref="InvalidCastException">The span does not represent an array.</exception>
public void GetArraySuffix(out int arrtype, out int pc) { public readonly void GetArraySuffix(out int arrtype, out int pc) {
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array) if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an array or vector"); throw new InvalidCastException("Not an array or vector");
arrtype = *(int*)(_ptr + Length - sizeof(int)); arrtype = *(int*)(_ptr + Length - sizeof(int));
@@ -186,7 +186,7 @@ namespace Cryville.Common.Pdt {
/// <param name="arrtype">The type of the array.</param> /// <param name="arrtype">The type of the array.</param>
/// <param name="pc">The item count of the array.</param> /// <param name="pc">The item count of the array.</param>
/// <exception cref="InvalidCastException">The span does not represent an array.</exception> /// <exception cref="InvalidCastException">The span does not represent an array.</exception>
public void SetArraySuffix(int arrtype, int pc = 0) { public readonly void SetArraySuffix(int arrtype, int pc = 0) {
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array) if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an array or vector"); throw new InvalidCastException("Not an array or vector");
*(int*)(_ptr + Length - sizeof(int)) = arrtype; *(int*)(_ptr + Length - sizeof(int)) = arrtype;

View File

@@ -6,15 +6,15 @@ namespace Cryville.Common {
public T Value { get; set; } public T Value { get; set; }
public string Unit { get; set; } public string Unit { get; set; }
public Qualified(string unit) : this(default(T), unit) { } public Qualified(string unit) : this(default, unit) { }
public Qualified(T value, string unit) { public Qualified(T value, string unit) {
Value = value; Value = value;
Unit = unit; Unit = unit;
} }
public override string ToString() { return ToString("G3"); } public override readonly string ToString() { return ToString("G3"); }
public string ToString(string format) { return ToString(format, null); } public readonly string ToString(string format) { return ToString(format, null); }
public string ToString(string format, IFormatProvider formatProvider) { public readonly string ToString(string format, IFormatProvider formatProvider) {
double value = Value.ToDouble(formatProvider); double value = Value.ToDouble(formatProvider);
int expIndex = (int)System.Math.Log10(value) / 3; int expIndex = (int)System.Math.Log10(value) / 3;
if (expIndex == 0) { if (expIndex == 0) {

View File

@@ -2,6 +2,6 @@ using Cryville.Common.Logging;
namespace Cryville.Common { namespace Cryville.Common {
public static class Shared { public static class Shared {
public static readonly Logger Logger = new Logger(); public static readonly Logger Logger = new();
} }
} }

View File

@@ -13,7 +13,7 @@ namespace Cryville.Common {
/// <param name="s">The file name or file path.</param> /// <param name="s">The file name or file path.</param>
/// <returns>The file name or file path with the extension removed.</returns> /// <returns>The file name or file path with the extension removed.</returns>
public static string TrimExt(string s) { public static string TrimExt(string s) {
return s.Substring(0, s.LastIndexOf(".")); return s[..s.LastIndexOf(".")];
} }
/// <summary> /// <summary>
/// Converts the value of a <see cref="TimeSpan" /> to a human-readable string. /// Converts the value of a <see cref="TimeSpan" /> to a human-readable string.
@@ -52,12 +52,12 @@ namespace Cryville.Common {
public static string GetProcessPathFromCommand(string command) { public static string GetProcessPathFromCommand(string command) {
command = command.Trim(); command = command.Trim();
if (command[0] == '"') { if (command[0] == '"') {
return command.Substring(1, command.IndexOf('"', 1) - 1); return command[1..command.IndexOf('"', 1)];
} }
else { else {
int e = command.IndexOf(' '); int e = command.IndexOf(' ');
if (e == -1) return command; if (e == -1) return command;
else return command.Substring(0, e); else return command[..e];
} }
} }
} }

View File

@@ -16,7 +16,7 @@ namespace Cryville.Common.Unity {
public class NetworkTaskWorker { public class NetworkTaskWorker {
bool suspended; bool suspended;
NetworkTask currentNetworkTask; NetworkTask currentNetworkTask;
readonly Queue<NetworkTask> networkTasks = new Queue<NetworkTask>(); readonly Queue<NetworkTask> networkTasks = new();
/// <summary> /// <summary>
/// Current queued task count. /// Current queued task count.

View File

@@ -30,13 +30,13 @@ namespace Cryville.Common.Unity {
[SerializeField] [SerializeField]
public string Attribute; public string Attribute;
public bool Equals(AttributeBinding other) { public readonly bool Equals(AttributeBinding other) {
return Component.Equals(other.Component) && Attribute.Equals(other.Attribute); return Component.Equals(other.Component) && Attribute.Equals(other.Attribute);
} }
public override bool Equals(object obj) { public override readonly bool Equals(object obj) {
return obj is AttributeBinding && Equals((AttributeBinding)obj); return obj is AttributeBinding other && Equals(other);
} }
public override int GetHashCode() { public override readonly int GetHashCode() {
return Component.GetHashCode() ^ Attribute.GetHashCode(); return Component.GetHashCode() ^ Attribute.GetHashCode();
} }
} }
@@ -44,10 +44,10 @@ namespace Cryville.Common.Unity {
[SerializeField] [SerializeField]
StateTweener[] m_children; StateTweener[] m_children;
readonly List<string> _statePriority = new List<string>(); readonly List<string> _statePriority = new();
readonly Dictionary<AttributeBinding, object> _defaults = new Dictionary<AttributeBinding, object>(); readonly Dictionary<AttributeBinding, object> _defaults = new();
readonly Dictionary<AttributeBinding, PropertyTweener<object>> _tweeners = new Dictionary<AttributeBinding, PropertyTweener<object>>(); readonly Dictionary<AttributeBinding, PropertyTweener<object>> _tweeners = new();
readonly Dictionary<string, Dictionary<AttributeBinding, object>> _runtimeStates = new Dictionary<string, Dictionary<AttributeBinding, object>>(); readonly Dictionary<string, Dictionary<AttributeBinding, object>> _runtimeStates = new();
void Awake() { void Awake() {
var types = new Dictionary<AttributeBinding, Type>(); var types = new Dictionary<AttributeBinding, Type>();
@@ -130,7 +130,7 @@ namespace Cryville.Common.Unity {
foreach (var tweener in _tweeners) tweener.Value.Advance(Time.deltaTime); foreach (var tweener in _tweeners) tweener.Value.Advance(Time.deltaTime);
} }
readonly List<string> m_cState = new List<string>(); readonly List<string> m_cState = new();
public IReadOnlyList<string> CurrentState => m_cState; public IReadOnlyList<string> CurrentState => m_cState;
public void ClearState(float transitionDuration = float.Epsilon) { public void ClearState(float transitionDuration = float.Epsilon) {
foreach (var child in m_children) child.ClearState(transitionDuration); foreach (var child in m_children) child.ClearState(transitionDuration);
@@ -161,7 +161,7 @@ namespace Cryville.Common.Unity {
if (index < 0) return; if (index < 0) return;
m_cState.RemoveAt(index); m_cState.RemoveAt(index);
if (index < m_cState.Count) return; if (index < m_cState.Count) return;
var attrs = m_cState.Count == 0 ? _defaults : _runtimeStates[m_cState[m_cState.Count - 1]]; var attrs = m_cState.Count == 0 ? _defaults : _runtimeStates[m_cState[^1]];
foreach (var tweener in _tweeners) { foreach (var tweener in _tweeners) {
tweener.Value.Start(attrs[tweener.Key], transitionDuration); tweener.Value.Start(attrs[tweener.Key], transitionDuration);
} }

View File

@@ -113,7 +113,7 @@ namespace Cryville.Common.Unity.UI {
private bool initialized; private bool initialized;
private GameObject[][] lines; private GameObject[][] lines;
private int[] refl; private int[] refl;
Vector2 cpos = new Vector2(0, 1); Vector2 cpos = new(0, 1);
Vector2 pprectsize; Vector2 pprectsize;
#pragma warning disable IDE0051 #pragma warning disable IDE0051

View File

@@ -35,8 +35,7 @@ namespace Cryville.Common.Unity.UI {
if (MaxFallbackCount <= 0) break; if (MaxFallbackCount <= 0) break;
} }
else { else {
if (_font.fallbackFontAssetTable == null) _font.fallbackFontAssetTable ??= new List<FontAsset>();
_font.fallbackFontAssetTable = new List<FontAsset>();
_font.fallbackFontAssetTable.Add(ifont); _font.fallbackFontAssetTable.Add(ifont);
Shared.Logger.Log(1, "UI", "Using fallback font #{0}: {1}", _font.fallbackFontAssetTable.Count, typeface.FullName); Shared.Logger.Log(1, "UI", "Using fallback font #{0}: {1}", _font.fallbackFontAssetTable.Count, typeface.FullName);
if (_font.fallbackFontAssetTable.Count >= MaxFallbackCount) break; if (_font.fallbackFontAssetTable.Count >= MaxFallbackCount) break;

View File

@@ -25,7 +25,7 @@ namespace Cryville.Crtr {
public int d; public int d;
[JsonIgnore] [JsonIgnore]
public double Decimal { get { return b + (double)n / d; } } public readonly double Decimal { get { return b + (double)n / d; } }
public int CompareTo(BeatTime other) { public int CompareTo(BeatTime other) {
var c = b.CompareTo(other.b); var c = b.CompareTo(other.b);
@@ -34,15 +34,15 @@ namespace Cryville.Crtr {
} }
public override bool Equals(object obj) { public override bool Equals(object obj) {
if (!(obj is BeatTime)) return false; if (obj is not BeatTime other) return false;
return Equals((BeatTime)obj); return Equals(other);
} }
public bool Equals(BeatTime other) { public bool Equals(BeatTime other) {
return b.Equals(other.b) && ((double)n / d).Equals((double)other.n / other.d); return b.Equals(other.b) && ((double)n / d).Equals((double)other.n / other.d);
} }
public override int GetHashCode() { public override readonly int GetHashCode() {
return Decimal.GetHashCode(); return Decimal.GetHashCode();
} }

View File

@@ -4,28 +4,27 @@ using System.Linq;
namespace Cryville.Crtr.Browsing.Actions { namespace Cryville.Crtr.Browsing.Actions {
public class ActionManager { public class ActionManager {
readonly Dictionary<Type, List<IResourceAction>> _actions = new Dictionary<Type, List<IResourceAction>>(); readonly Dictionary<Type, List<IResourceAction>> _actions = new();
readonly Dictionary<IResourceAction, int> _refCounts = new Dictionary<IResourceAction, int>(); readonly Dictionary<IResourceAction, int> _refCounts = new();
public event Action Changed; public event Action Changed;
class ActionPriorityComparer : IComparer<IResourceAction> { class ActionPriorityComparer : IComparer<IResourceAction> {
public static readonly ActionPriorityComparer Instance = new ActionPriorityComparer(); public static readonly ActionPriorityComparer Instance = new();
public int Compare(IResourceAction x, IResourceAction y) { public int Compare(IResourceAction x, IResourceAction y) {
return x.Priority.CompareTo(y.Priority); return x.Priority.CompareTo(y.Priority);
} }
} }
void Register(Type type, IResourceAction action) { void Register(Type type, IResourceAction action) {
List<IResourceAction> actions; if (!_actions.TryGetValue(type, out List<IResourceAction> actions)) {
if (!_actions.TryGetValue(type, out actions)) {
_actions.Add(type, actions = new List<IResourceAction>()); _actions.Add(type, actions = new List<IResourceAction>());
} }
int index = actions.BinarySearch(action, ActionPriorityComparer.Instance); int index = actions.BinarySearch(action, ActionPriorityComparer.Instance);
if (index < 0) index = ~index; if (index < 0) index = ~index;
actions.Insert(index, action); actions.Insert(index, action);
if (_refCounts.ContainsKey(action)) _refCounts[action]++; if (_refCounts.ContainsKey(action)) _refCounts[action]++;
else _refCounts[action] = 0; else _refCounts[action] = 1;
Changed?.Invoke(); Changed?.Invoke();
} }
public void Register(IResourceAction action) { public void Register(IResourceAction action) {
@@ -36,8 +35,7 @@ namespace Cryville.Crtr.Browsing.Actions {
} }
public void Unregister(Type type, IResourceAction action) { public void Unregister(Type type, IResourceAction action) {
List<IResourceAction> actions; if (!_actions.TryGetValue(type, out List<IResourceAction> actions)) return;
if (!_actions.TryGetValue(type, out actions)) return;
if (--_refCounts[action] > 0) return; if (--_refCounts[action] > 0) return;
actions.Remove(action); actions.Remove(action);
Changed?.Invoke(); Changed?.Invoke();
@@ -54,9 +52,8 @@ namespace Cryville.Crtr.Browsing.Actions {
} }
IEnumerable<IResourceAction> GetActions(Uri uri, IResourceMeta res, Type type) { IEnumerable<IResourceAction> GetActions(Uri uri, IResourceMeta res, Type type) {
if (type == null) return Enumerable.Empty<IResourceAction>(); if (type == null) return Enumerable.Empty<IResourceAction>();
List<IResourceAction> actions;
IEnumerable<IResourceAction> result; IEnumerable<IResourceAction> result;
if (_actions.TryGetValue(type, out actions)) if (_actions.TryGetValue(type, out List<IResourceAction> actions))
result = actions.Where(i => i.CanInvoke(uri, res)); result = actions.Where(i => i.CanInvoke(uri, res));
else else
result = Enumerable.Empty<IResourceAction>(); result = Enumerable.Empty<IResourceAction>();

View File

@@ -10,7 +10,7 @@ namespace Cryville.Crtr.Browsing.Actions {
public override int Priority { get { return -50; } } public override int Priority { get { return -50; } }
static readonly Dictionary<string, int> _rulesetTabs = new Dictionary<string, int>(); static readonly Dictionary<string, int> _rulesetTabs = new();
public override bool CanInvoke(Uri uri, IChartDetail resource) { public override bool CanInvoke(Uri uri, IChartDetail resource) {
return true; return true;
@@ -20,15 +20,13 @@ namespace Cryville.Crtr.Browsing.Actions {
} }
public static bool HasTab(string ruleset) { public static bool HasTab(string ruleset) {
int tabId;
var master = ResourceBrowserMaster.Instance; var master = ResourceBrowserMaster.Instance;
if (master == null) return false; if (master == null) return false;
return _rulesetTabs.TryGetValue(ruleset, out tabId) && master.HasTab(tabId); return _rulesetTabs.TryGetValue(ruleset, out int tabId) && master.HasTab(tabId);
} }
public static void Invoke(string ruleset, Action<RulesetConfig> overrides = null) { public static void Invoke(string ruleset, Action<RulesetConfig> overrides = null) {
var master = ResourceBrowserMaster.Instance; var master = ResourceBrowserMaster.Instance;
int tabId; if (_rulesetTabs.TryGetValue(ruleset, out int tabId) && master.TryOpenTab(tabId))
if (_rulesetTabs.TryGetValue(ruleset, out tabId) && master.TryOpenTab(tabId))
return; return;
var browser = Object.Instantiate(master.m_configBrowserPrefab).GetComponent<RulesetConfigBrowser>(); var browser = Object.Instantiate(master.m_configBrowserPrefab).GetComponent<RulesetConfigBrowser>();
try { try {

View File

@@ -8,15 +8,15 @@ namespace Cryville.Crtr.Browsing.Actions {
public abstract bool CanInvoke(Uri uri, T resource); public abstract bool CanInvoke(Uri uri, T resource);
public bool CanInvoke(Uri uri, IResourceMeta resource) { public bool CanInvoke(Uri uri, IResourceMeta resource) {
if (resource == null) throw new ArgumentNullException("resource"); if (resource == null) throw new ArgumentNullException("resource");
if (!(resource is T)) throw new ArgumentException("Mismatched resource type."); if (resource is not T res) throw new ArgumentException("Mismatched resource type.");
return CanInvoke(uri, (T)resource); return CanInvoke(uri, res);
} }
public abstract void Invoke(Uri uri, T resource); public abstract void Invoke(Uri uri, T resource);
public void Invoke(Uri uri, IResourceMeta resource) { public void Invoke(Uri uri, IResourceMeta resource) {
if (resource == null) throw new ArgumentNullException("resource"); if (resource == null) throw new ArgumentNullException("resource");
if (!(resource is T)) throw new ArgumentException("Mismatched resource type."); if (resource is not T res) throw new ArgumentException("Mismatched resource type.");
Invoke(uri, (T)resource); Invoke(uri, res);
} }
} }
} }

View File

@@ -10,18 +10,17 @@ namespace Cryville.Crtr.Browsing {
internal static class ExtensionManager { internal static class ExtensionManager {
static bool _init; static bool _init;
static readonly Dictionary<string, List<ResourceConverter>> _converters static readonly Dictionary<string, List<ResourceConverter>> _converters
= new Dictionary<string, List<ResourceConverter>>(); = new();
public static ISet<string> GetSupportedFormats() { public static ISet<string> GetSupportedFormats() {
return new HashSet<string>(_converters.Keys); return new HashSet<string>(_converters.Keys);
} }
public static bool TryGetConverters(string extension, out IEnumerable<ResourceConverter> converters) { public static bool TryGetConverters(string extension, out IEnumerable<ResourceConverter> converters) {
List<ResourceConverter> outResult; bool result = _converters.TryGetValue(extension, out List<ResourceConverter> outResult);
bool result = _converters.TryGetValue(extension, out outResult);
converters = outResult; converters = outResult;
return result; return result;
} }
static readonly Dictionary<string, string> _localRes static readonly Dictionary<string, string> _localRes
= new Dictionary<string, string>(); = new();
public static IReadOnlyDictionary<string, string> GetLocalResourcePaths() { public static IReadOnlyDictionary<string, string> GetLocalResourcePaths() {
return _localRes; return _localRes;
} }
@@ -62,8 +61,7 @@ namespace Cryville.Crtr.Browsing {
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
var buf = new byte[stream.Length]; var buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length); stream.Read(buf, 0, buf.Length);
var asm = Assembly.Load(buf); var asm = Assembly.Load(buf) ?? throw new TypeLoadException("Failed to load the module");
if (asm == null) throw new TypeLoadException("Failed to load the module");
asms.Add(asm.GetName().Name); asms.Add(asm.GetName().Name);
foreach (var type in asm.GetTypes()) { foreach (var type in asm.GetTypes()) {
if (typeof(ExtensionInterface).IsAssignableFrom(type)) { if (typeof(ExtensionInterface).IsAssignableFrom(type)) {

Some files were not shown because too many files have changed in this diff Show More