refactoring to support c# 7.3

This commit is contained in:
Mikhail 2023-06-12 21:59:27 +08:00
parent b0c0014faf
commit 4b63353965
6 changed files with 219 additions and 18 deletions

View File

@ -35,8 +35,9 @@ namespace DCFApixels.DragonECS
#if DEBUG && !DISABLE_DEBUG #if DEBUG && !DISABLE_DEBUG
for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++) for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++)
{ {
using (_markers[i].Auto()) _markers[i].Begin();
targets[i].PreInit(pipeline); targets[i].PreInit(pipeline);
_markers[i].End();
} }
#else #else
foreach (var item in targets) item.PreInit(pipeline); foreach (var item in targets) item.PreInit(pipeline);
@ -64,8 +65,9 @@ namespace DCFApixels.DragonECS
#if DEBUG && !DISABLE_DEBUG #if DEBUG && !DISABLE_DEBUG
for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++) for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++)
{ {
using (_markers[i].Auto()) _markers[i].Begin();
targets[i].Init(pipeline); targets[i].Init(pipeline);
_markers[i].End();
} }
#else #else
foreach (var item in targets) item.Init(pipeline); foreach (var item in targets) item.Init(pipeline);
@ -93,9 +95,9 @@ namespace DCFApixels.DragonECS
#if DEBUG && !DISABLE_DEBUG #if DEBUG && !DISABLE_DEBUG
for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++) for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++)
{ {
using (_markers[i].Auto()) _markers[i].Begin();
targets[i].Run(pipeline); targets[i].Run(pipeline);
_markers[i].End();
} }
#else #else
foreach (var item in targets) item.Run(pipeline); foreach (var item in targets) item.Run(pipeline);
@ -123,8 +125,9 @@ namespace DCFApixels.DragonECS
#if DEBUG && !DISABLE_DEBUG #if DEBUG && !DISABLE_DEBUG
for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++) for (int i = 0; i < targets.Length && targets.Length <= _markers.Length; i++)
{ {
using (_markers[i].Auto()) _markers[i].Begin();
targets[i].Destroy(pipeline); targets[i].Destroy(pipeline);
_markers[i].End();
} }
#else #else
foreach (var item in targets) item.Destroy(pipeline); foreach (var item in targets) item.Destroy(pipeline);

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -468,7 +469,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Object #region Object
public override string ToString() => string.Join(", ", _dense.AsSpan(1, _count).ToArray()); public override string ToString() => string.Join(", ", _dense.Cast<string>(), 0, _count);
public override bool Equals(object obj) => obj is EcsGroup group && Equals(group); public override bool Equals(object obj) => obj is EcsGroup group && Equals(group);
public bool Equals(EcsReadonlyGroup other) => Equals(other.GetGroupInternal()); public bool Equals(EcsReadonlyGroup other) => Equals(other.GetGroupInternal());
public bool Equals(EcsGroup other) public bool Equals(EcsGroup other)

View File

@ -45,8 +45,8 @@ namespace DCFApixels.DragonECS
{ {
_world = world; _world = world;
_subjects = new List<CombinedSubject>(); _subjects = new List<CombinedSubject>();
_inc = new HashSet<int>(8); _inc = new HashSet<int>();
_exc = new HashSet<int>(4); _exc = new HashSet<int>();
} }
internal static TSubject Build<TSubject>(EcsWorld world) where TSubject : EcsSubject internal static TSubject Build<TSubject>(EcsWorld world) where TSubject : EcsSubject
{ {

View File

@ -33,15 +33,14 @@
public EcsReadonlyGroup ExecuteFor(EcsReadonlyGroup sourceGroup) public EcsReadonlyGroup ExecuteFor(EcsReadonlyGroup sourceGroup)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
using (_executeWhere.Auto()) _executeWhere.Begin();
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
#endif #endif
{ _subject.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup);
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения. _executeWhere.End();
#endif #endif
_subject.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup); return _filteredGroup.Readonly;
return _filteredGroup.Readonly;
}
} }
#endregion #endregion
} }

View File

@ -0,0 +1,187 @@
#if ENABLE_DUMMY_SPAN
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;
namespace DCFApixels.DragonECS
{
internal static class ThrowHelper
{
public static void ThrowIndexOutOfRangeException() => throw new IndexOutOfRangeException();
public static void ThrowArgumentOutOfRangeException() => throw new ArgumentOutOfRangeException();
public static void ThrowInvalidOperationException() => throw new InvalidOperationException();
}
[DebuggerDisplay("{ToString(),raw}")]
public readonly ref struct ReadOnlySpan<T>
{
public static ReadOnlySpan<T> Empty => new ReadOnlySpan<T>(null);
internal readonly T[] _array;
private readonly int _start;
private readonly int _length;
#region Properties
public int Length => _length;
public bool IsEmpty => _length == 0;
public ref readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)index >= (uint)_length || (uint)index < 0)
ThrowHelper.ThrowIndexOutOfRangeException();
return ref _array[index + _start];
}
}
#endregion
#region Constructors
public ReadOnlySpan(T[] array)
{
_array = array ?? Array.Empty<T>();
_start = 0;
_length = array.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan(T[] array, int start, int length)
{
if (array == null)
{
if (start != 0 || length != 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
_array = Array.Empty<T>();
_start = 0;
_length = 0;
return;
}
if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
_array = array;
_start = start;
_length = length;
}
#endregion
#region Object
[Obsolete("Equals() on ReadOnlySpan will always throw an exception. Use the equality operator instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => throw new NotSupportedException();
[Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => throw new NotSupportedException();
public override string ToString()
{
//if (typeof(T) == typeof(char))
// return new string(new ReadOnlySpan<char>(ref Unsafe.As<T, char>(ref _reference), _length));
return $"System.ReadOnlySpan<{typeof(T).Name}>[{_length}]";
}
#endregion
#region operators
public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right) => !(left == right);
public static implicit operator ReadOnlySpan<T>(T[] array) => new ReadOnlySpan<T>(array);
public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment) => new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count);
public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right) => left._length == right._length && left._array == right._array;
#endregion
#region Enumerator
public Enumerator GetEnumerator() => new Enumerator(this);
public ref struct Enumerator
{
private readonly ReadOnlySpan<T> _span;
private int _index;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Enumerator(ReadOnlySpan<T> span)
{
_span = span;
_index = span._start - 1;
}
public ref readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _span[_index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
int index = _index + 1;
if (index < _span.Length)
{
_index = index;
return true;
}
return false;
}
}
#endregion
#region Other
[EditorBrowsable(EditorBrowsableState.Never)]
public ref readonly T GetPinnableReference()
{
if (_length != 0) ThrowHelper.ThrowInvalidOperationException();
return ref _array[0];
}
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public void CopyTo(Span<T> destination)
//{
// if ((uint)_length <= (uint)destination.Length)
// {
// Buffer.Memmove(ref destination._reference, ref _reference, (uint)_length);
// }
// else
// {
// ThrowHelper.ThrowArgumentException_DestinationTooShort();
// }
//}
//public bool TryCopyTo(Span<T> destination)
//{
// bool retVal = false;
// if ((uint)_length <= (uint)destination.Length)
// {
// Buffer.Memmove(ref destination._reference, ref _reference, (uint)_length);
// retVal = true;
// }
// return retVal;
//}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> Slice(int start)
{
if ((uint)start > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
return new ReadOnlySpan<T>(_array, _start + start, _length - start);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> Slice(int start, int length)
{
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
return new ReadOnlySpan<T>(_array, _start + start, length);
}
public T[] ToArray()
{
if (_length == 0)
return Array.Empty<T>();
var result = new T[_length];
Array.Copy(_array, _start, result, 0, _length);
return result;
}
#endregion
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bfb00ad58c175a84388c316daf9b03ae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: