-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for Issue #4 on re-used Entity IDs #5
Open
Gnivom
wants to merge
2
commits into
Byteron:main
Choose a base branch
from
Gnivom:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ public void Despawn(Identity identity) | |
meta.Row = 0; | ||
meta.Identity = Identity.None; | ||
|
||
UnusedIds.Enqueue(identity); | ||
UnusedIds.Enqueue(new Identity(identity.Id, (ushort)(identity.Generation + 1))); | ||
|
||
if (!_typesByRelationTarget.TryGetValue(identity, out var list)) | ||
{ | ||
|
@@ -96,6 +96,10 @@ public void Despawn(Identity identity) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void AddComponent(StorageType type, Identity identity, object data) | ||
{ | ||
if (!IsAlive(identity)) | ||
{ | ||
throw new Exception($"cannot add component {type.Type.Name} to despawned entity {identity}"); | ||
} | ||
ref var meta = ref Meta[identity.Id]; | ||
var oldTable = Tables[meta.TableId]; | ||
|
||
|
@@ -139,6 +143,10 @@ public ref T GetComponent<T>(Identity identity, Identity target) | |
{ | ||
var type = StorageType.Create<T>(target); | ||
var meta = Meta[identity.Id]; | ||
if (meta.Identity != identity) | ||
{ | ||
throw new Exception($"cannot get component on despawned entity {identity}"); | ||
} | ||
var table = Tables[meta.TableId]; | ||
var storage = (T[])table.GetStorage(type); | ||
return ref storage[meta.Row]; | ||
|
@@ -147,13 +155,21 @@ public ref T GetComponent<T>(Identity identity, Identity target) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool HasComponent(StorageType type, Identity identity) | ||
{ | ||
if (identity == Identity.None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Early out feels wierd here, current HEAD already checks for Identity.none - so this can be... [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComponent(StorageType type, Identity identity)
{
var meta = Meta[identity.Id];
return meta.Identity != Identity.None
&& meta.Identity == identity
&& Tables[meta.TableId].Types.Contains(type);
} |
||
{ | ||
return false; | ||
} | ||
var meta = Meta[identity.Id]; | ||
return meta.Identity != Identity.None && Tables[meta.TableId].Types.Contains(type); | ||
return meta.Identity == identity && Tables[meta.TableId].Types.Contains(type); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void RemoveComponent(StorageType type, Identity identity) | ||
{ | ||
if (!IsAlive(identity)) | ||
{ | ||
throw new Exception($"cannot remove component {type.Type.Name} from despawned entity {identity}"); | ||
} | ||
ref var meta = ref Meta[identity.Id]; | ||
var oldTable = Tables[meta.TableId]; | ||
|
||
|
@@ -283,13 +299,18 @@ internal bool IsMaskCompatibleWith(Mask mask, Table table) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal bool IsAlive(Identity identity) | ||
{ | ||
return Meta[identity.Id].Identity != Identity.None; | ||
return identity != Identity.None && Meta[identity.Id].Identity == identity; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal ref EntityMeta GetEntityMeta(Identity identity) | ||
{ | ||
return ref Meta[identity.Id]; | ||
ref var meta = ref Meta[identity.Id]; | ||
if (meta.Identity != identity) | ||
{ | ||
throw new Exception($"cannot get entity meta on despawned entity {identity}"); | ||
} | ||
return ref meta; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
@@ -302,6 +323,10 @@ internal Table GetTable(int tableId) | |
internal Entity GetTarget(StorageType type, Identity identity) | ||
{ | ||
var meta = Meta[identity.Id]; | ||
if (meta.Identity != identity) | ||
{ | ||
return Entity.None; | ||
} | ||
var table = Tables[meta.TableId]; | ||
|
||
foreach (var storageType in table.Types) | ||
|
@@ -316,15 +341,17 @@ internal Entity GetTarget(StorageType type, Identity identity) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal Entity[] GetTargets(StorageType type, Identity identity) | ||
{ | ||
var meta = Meta[identity.Id]; | ||
var table = Tables[meta.TableId]; | ||
|
||
var list = ListPool<Entity>.Get(); | ||
|
||
foreach (var storageType in table.Types) | ||
if (IsAlive(identity)) | ||
{ | ||
if (!storageType.IsRelation || storageType.TypeId != type.TypeId) continue; | ||
list.Add(new Entity(storageType.Identity)); | ||
var meta = Meta[identity.Id]; | ||
var table = Tables[meta.TableId]; | ||
foreach (var storageType in table.Types) | ||
{ | ||
if (!storageType.IsRelation || storageType.TypeId != type.TypeId) continue; | ||
list.Add(new Entity(storageType.Identity)); | ||
} | ||
} | ||
|
||
var targetEntities = list.ToArray(); | ||
|
@@ -336,15 +363,17 @@ internal Entity[] GetTargets(StorageType type, Identity identity) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal (StorageType, object)[] GetComponents(Identity identity) | ||
{ | ||
var meta = Meta[identity.Id]; | ||
var table = Tables[meta.TableId]; | ||
|
||
var list = ListPool<(StorageType, object)>.Get(); | ||
|
||
foreach (var type in table.Types) | ||
if (IsAlive(identity)) | ||
{ | ||
var storage = table.GetStorage(type); | ||
list.Add((type, storage.GetValue(meta.Row)!)); | ||
var meta = Meta[identity.Id]; | ||
var table = Tables[meta.TableId]; | ||
foreach (var type in table.Types) | ||
{ | ||
var storage = table.GetStorage(type); | ||
list.Add((type, storage.GetValue(meta.Row)!)); | ||
} | ||
} | ||
|
||
var array = list.ToArray(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest using an assertion instead.Nevermind, the Library throws runtime exceptions as well.