This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Simplify components, step 1 #1290
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f9815e
De-duplicate DiffComponentStorage
zeroZshadow 33daedc
Simplify component command storage in ViewDiff and MessagesToSend
zeroZshadow 32849f2
Metadata feedback
zeroZshadow 7f6a131
Generated Code
zeroZshadow 5ab60b8
Changelog
zeroZshadow 06464f7
Fixed merge issue
zeroZshadow 56f824d
Feedback Jamie
zeroZshadow df50df8
Update CHANGELOG.md
zeroZshadow 6e666b1
More feedback
zeroZshadow d03cc31
d
zeroZshadow 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
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
143 changes: 143 additions & 0 deletions
143
workers/unity/Packages/io.improbable.gdk.core/View/DiffComponentStorage.cs
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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Improbable.Worker.CInterop; | ||
|
||
namespace Improbable.Gdk.Core | ||
{ | ||
public abstract class DiffComponentStorage<TUpdate> : IDiffUpdateStorage<TUpdate>, IDiffComponentAddedStorage<TUpdate>, IDiffAuthorityStorage | ||
where TUpdate : ISpatialComponentUpdate | ||
{ | ||
private readonly HashSet<EntityId> entitiesUpdated = new HashSet<EntityId>(); | ||
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. Can you make this protected since I'll need that in #1298 ? 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. 👍 |
||
private readonly uint componentId; | ||
|
||
private readonly List<EntityId> componentsAdded = new List<EntityId>(); | ||
private readonly List<EntityId> componentsRemoved = new List<EntityId>(); | ||
|
||
private readonly AuthorityComparer authorityComparer = new AuthorityComparer(); | ||
private readonly UpdateComparer<TUpdate> updateComparer = new UpdateComparer<TUpdate>(); | ||
|
||
// Used to represent a state machine of authority changes. Valid state changes are: | ||
// authority lost -> authority lost temporarily | ||
// authority lost temporarily -> authority lost | ||
// authority gained -> authority gained | ||
// Creating the authority lost temporarily set is the aim as it signifies authority epoch changes | ||
private readonly HashSet<EntityId> authorityLost = new HashSet<EntityId>(); | ||
private readonly HashSet<EntityId> authorityGained = new HashSet<EntityId>(); | ||
private readonly HashSet<EntityId> authorityLostTemporary = new HashSet<EntityId>(); | ||
|
||
private readonly MessageList<ComponentUpdateReceived<TUpdate>> updateStorage = | ||
new MessageList<ComponentUpdateReceived<TUpdate>>(); | ||
|
||
private readonly MessageList<AuthorityChangeReceived> authorityChanges = | ||
new MessageList<AuthorityChangeReceived>(); | ||
|
||
public abstract Type[] GetEventTypes(); | ||
|
||
public Type GetUpdateType() => typeof(TUpdate); | ||
|
||
public virtual void Clear() | ||
{ | ||
entitiesUpdated.Clear(); | ||
updateStorage.Clear(); | ||
authorityChanges.Clear(); | ||
componentsAdded.Clear(); | ||
componentsRemoved.Clear(); | ||
} | ||
|
||
public void RemoveEntityComponent(long entityId) | ||
{ | ||
var id = new EntityId(entityId); | ||
|
||
// Adding a component always updates it, so this will catch the case where the component was just added | ||
if (entitiesUpdated.Remove(id)) | ||
{ | ||
updateStorage.RemoveAll(update => update.EntityId.Id == entityId); | ||
authorityChanges.RemoveAll(change => change.EntityId.Id == entityId); | ||
|
||
//playerCollidedEventStorage.RemoveAll(change => change.EntityId.Id == entityId); | ||
zeroZshadow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ClearEventStorage(entityId); | ||
} | ||
|
||
if (!componentsAdded.Remove(id)) | ||
{ | ||
componentsRemoved.Add(id); | ||
} | ||
} | ||
|
||
protected abstract void ClearEventStorage(long entityId); | ||
|
||
|
||
zeroZshadow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void AddEntityComponent(long entityId, TUpdate component) | ||
{ | ||
var id = new EntityId(entityId); | ||
if (!componentsRemoved.Remove(id)) | ||
{ | ||
componentsAdded.Add(id); | ||
} | ||
|
||
AddUpdate(new ComponentUpdateReceived<TUpdate>(component, id, 0)); | ||
} | ||
|
||
public void AddUpdate(ComponentUpdateReceived<TUpdate> update) | ||
{ | ||
entitiesUpdated.Add(update.EntityId); | ||
updateStorage.InsertSorted(update, updateComparer); | ||
} | ||
|
||
public void AddAuthorityChange(AuthorityChangeReceived authorityChange) | ||
{ | ||
if (authorityChange.Authority == Authority.NotAuthoritative) | ||
{ | ||
if (authorityLostTemporary.Remove(authorityChange.EntityId) || !authorityGained.Contains(authorityChange.EntityId)) | ||
{ | ||
authorityLost.Add(authorityChange.EntityId); | ||
} | ||
} | ||
else if (authorityChange.Authority == Authority.Authoritative) | ||
{ | ||
if (authorityLost.Remove(authorityChange.EntityId)) | ||
{ | ||
authorityLostTemporary.Add(authorityChange.EntityId); | ||
} | ||
else | ||
{ | ||
authorityGained.Add(authorityChange.EntityId); | ||
} | ||
} | ||
|
||
authorityChanges.InsertSorted(authorityChange, authorityComparer); | ||
} | ||
|
||
public List<EntityId> GetComponentsAdded() | ||
{ | ||
return componentsAdded; | ||
} | ||
|
||
public List<EntityId> GetComponentsRemoved() | ||
{ | ||
return componentsRemoved; | ||
} | ||
|
||
public MessagesSpan<ComponentUpdateReceived<TUpdate>> GetUpdates() | ||
{ | ||
return updateStorage.Slice(); | ||
} | ||
|
||
public MessagesSpan<ComponentUpdateReceived<TUpdate>> GetUpdates(EntityId entityId) | ||
{ | ||
var (firstIndex, count) = updateStorage.GetEntityRange(entityId); | ||
return updateStorage.Slice(firstIndex, count); | ||
} | ||
|
||
public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges() | ||
{ | ||
return authorityChanges.Slice(); | ||
} | ||
|
||
public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges(EntityId entityId) | ||
{ | ||
var (firstIndex, count) = authorityChanges.GetEntityRange(entityId); | ||
return authorityChanges.Slice(firstIndex, count); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
workers/unity/Packages/io.improbable.gdk.core/View/DiffComponentStorage.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
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.
Worth noting this in the changelog :)