Skip to content

Commit

Permalink
Entity.Get is now unsafe: exception is more cryptic if it does not ha…
Browse files Browse the repository at this point in the history
…ve a component but it's faster so eh

updated System.Memory reference
  • Loading branch information
Doraku committed Jun 1, 2018
1 parent cf1576a commit 8ae3b07
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 196 deletions.
25 changes: 10 additions & 15 deletions source/DefaultEcs/DefaultEcs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
<TargetFrameworks>
netstandard1.1;
netstandard2.0;
monoandroid81;
<!--monoandroid81;-->
</TargetFrameworks>
<Optimize>true</Optimize>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Configurations>Debug;Release</Configurations>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\..\ds.snk</AssemblyOriginatorKeyFile>

<DocumentationFile Label="Documentation" Condition="'$(TargetFramework)'=='netstandard1.1'">bin\DefaultEcs.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Label="Debug" Condition="'$(Configuration)'=='Debug'">
Expand All @@ -18,9 +21,12 @@
</PropertyGroup>

<ItemGroup Label="Reference">
<PackageReference Include="System.Memory" Version="4.5.0-rc1" />
<PackageReference Condition="'$(TargetFramework)'=='netstandard1.1'" Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.0" />

<PackageReference Label="Documentation" Condition="'$(TargetFramework)'=='netstandard1.1'" Include="Vsxmd" Version="1.2.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup Label="Package">
<Authors>Paillat Laszlo</Authors>
<PackageId>DefaultEcs</PackageId>
Expand All @@ -32,19 +38,8 @@
gamedev game-development game-engine ecs entity-component-system
</PackageTags>

<Version>0.3.3-alpha</Version>
<Version>0.4.0</Version>
<PackageReleaseNotes>
</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Label="Documentation" Condition="'$(TargetFramework)'=='netstandard1.1'">
<DocumentationFile>bin\DefaultEcs.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup Label="Documentation" Condition="'$(TargetFramework)'=='netstandard1.1'">
<PackageReference Include="Vsxmd" Version="1.2.0" PrivateAssets="All" />
</ItemGroup>

<!--<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.1'">
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>-->
</Project>
37 changes: 24 additions & 13 deletions source/DefaultEcs/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using DefaultEcs.Technical;
using DefaultEcs.Technical.Message;

Expand All @@ -9,11 +10,14 @@ namespace DefaultEcs
/// Represents an item in the <see cref="World"/>.
/// Only use <see cref="Entity"/> generated from the <see cref="World.CreateEntity"/> method.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public readonly struct Entity : IDisposable, IEquatable<Entity>
{
#region Fields

[FieldOffset(0)]
internal readonly int WorldId;
[FieldOffset(4)]
internal readonly int EntityId;

#endregion
Expand Down Expand Up @@ -133,21 +137,28 @@ public void Remove<T>()
/// </summary>
/// <typeparam name="T">The type of the component.</typeparam>
/// <returns>A reference to the component.</returns>
/// <exception cref="InvalidOperationException"><see cref="Entity"/> was not created from a <see cref="WorldId"/>.</exception>
/// <exception cref="InvalidOperationException"><see cref="Entity"/> does not have a component of type <typeparamref name="T"/>.</exception>
/// <exception cref="Exception"><see cref="Entity"/> was not created from a <see cref="WorldId"/> or does not have a component of type <typeparamref name="T"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T Get<T>()
{
ThrowIfNull();
public ref T Get<T>() => ref ComponentManager<T>.Pools[WorldId].Get(EntityId);
//{
// ThrowIfNull();

if (WorldId >= ComponentManager<T>.Pools.Length
|| ComponentManager<T>.Pools[WorldId] == null)
{
throw new InvalidOperationException($"Entity does not have a component of type {nameof(T)}");
}
// if (WorldId >= ComponentManager<T>.Pools.Length
// || ComponentManager<T>.Pools[WorldId] == null)
// {
// throw new InvalidOperationException($"Entity does not have a component of type {nameof(T)}");
// }

return ref ComponentManager<T>.Pools[WorldId].Get(EntityId);
}
// return ref ComponentManager<T>.Pools[WorldId].Get(EntityId);
//}

///// <summary>
/////
///// </summary>
///// <typeparam name="T"></typeparam>
///// <returns></returns>
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public ref T FuckItGet<T>() => ref ComponentManager<T>.Pools[WorldId].FuckItGet(EntityId);

//public void SetChildren(params Entity[] children)
//{
Expand Down Expand Up @@ -217,7 +228,7 @@ public void Dispose()
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
public bool Equals(Entity other) => EntityId == other.EntityId;
public bool Equals(Entity other) => EntityId == other.EntityId && WorldId == other.WorldId;

#endregion

Expand Down
Loading

0 comments on commit 8ae3b07

Please sign in to comment.