Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Simplify components, step 1 #1290

Merged
merged 10 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
- Ported all modules to the new `CodegenJob` model. [#1276](https://github.com/spatialos/gdk-for-unity/pull/1276)
- Running forced code generation now deletes the `ImprobableCodegen.marker` file. [#1294](https://github.com/spatialos/gdk-for-unity/pull/1294)
- Added tests coverage for the interaction between unlinking a GameObject and Reader/Writer/CommandSender/CommandReceiver state. [#1295](https://github.com/spatialos/gdk-for-unity/pull/1295)
- Reduce complexity in `ViewDiff` and `MessagesToSend` classes. [#1290](https://github.com/spatialos/gdk-for-unity/pull/1290)
- De-duplicated code for generated `ComponentDiffStorage` instances. [#1290](https://github.com/spatialos/gdk-for-unity/pull/1290)
- `Improbable.Gdk.Core.EntityId` is now a readonly struct. [#1290](https://github.com/spatialos/gdk-for-unity/pull/1290)

## `0.3.3` - 2020-02-14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,155 +2,24 @@
// DO NOT EDIT - this file is automatically regenerated.
// =====================================================

using System.Collections.Generic;
using System;
using Improbable.Gdk.Core;
using Improbable.Worker.CInterop;

namespace Improbable.DependentSchema
{
public partial class DependentComponent
{
public class DiffComponentStorage : IDiffUpdateStorage<Update>, IDiffComponentAddedStorage<Update>, IDiffAuthorityStorage
public class DiffComponentStorage : DiffComponentStorage<Update>
{
private readonly HashSet<EntityId> entitiesUpdated = new HashSet<EntityId>();

private List<EntityId> componentsAdded = new List<EntityId>();
private List<EntityId> componentsRemoved = new List<EntityId>();

private readonly AuthorityComparer authorityComparer = new AuthorityComparer();
private readonly UpdateComparer<Update> updateComparer = new UpdateComparer<Update>();

// 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 MessageList<ComponentUpdateReceived<Update>> updateStorage =
new MessageList<ComponentUpdateReceived<Update>>();

private MessageList<AuthorityChangeReceived> authorityChanges =
new MessageList<AuthorityChangeReceived>();

public Type[] GetEventTypes()
public override Type[] GetEventTypes()
{
return new Type[]
{
};
}

public Type GetUpdateType()
{
return typeof(Update);
}

public uint GetComponentId()
{
return ComponentId;
}

public 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);
}

if (!componentsAdded.Remove(id))
{
componentsRemoved.Add(id);
}
}

public void AddEntityComponent(long entityId, Update component)
{
var id = new EntityId(entityId);
if (!componentsRemoved.Remove(id))
{
componentsAdded.Add(id);
}

AddUpdate(new ComponentUpdateReceived<Update>(component, id, 0));
}

public void AddUpdate(ComponentUpdateReceived<Update> 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<Update>> GetUpdates()
{
return updateStorage.Slice();
}

public MessagesSpan<ComponentUpdateReceived<Update>> GetUpdates(EntityId entityId)
{
var range = updateStorage.GetEntityRange(entityId);
return updateStorage.Slice(range.FirstIndex, range.Count);
}

public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges()
{
return authorityChanges.Slice();
}

public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges(EntityId entityId)
protected override void ClearEventStorage(long entityId)
{
var range = authorityChanges.GetEntityRange(entityId);
return authorityChanges.Slice(range.FirstIndex, range.Count);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,173 +2,46 @@
// DO NOT EDIT - this file is automatically regenerated.
// =====================================================

using System.Collections.Generic;
using System;
using Improbable.Gdk.Core;
using Improbable.Worker.CInterop;

namespace Improbable.DependentSchema
{
public partial class DependentDataComponent
{
public class DiffComponentStorage : IDiffUpdateStorage<Update>, IDiffComponentAddedStorage<Update>, IDiffAuthorityStorage
public class DiffComponentStorage : DiffComponentStorage<Update>
, IDiffEventStorage<FooEvent.Event>
{
private readonly HashSet<EntityId> entitiesUpdated = new HashSet<EntityId>();

private List<EntityId> componentsAdded = new List<EntityId>();
private List<EntityId> componentsRemoved = new List<EntityId>();

private readonly AuthorityComparer authorityComparer = new AuthorityComparer();
private readonly UpdateComparer<Update> updateComparer = new UpdateComparer<Update>();

// 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 MessageList<ComponentUpdateReceived<Update>> updateStorage =
new MessageList<ComponentUpdateReceived<Update>>();

private MessageList<AuthorityChangeReceived> authorityChanges =
new MessageList<AuthorityChangeReceived>();

private MessageList<ComponentEventReceived<FooEvent.Event>> fooEventEventStorage =
new MessageList<ComponentEventReceived<FooEvent.Event>>();

private readonly EventComparer<FooEvent.Event> fooEventComparer =
new EventComparer<FooEvent.Event>();

public Type[] GetEventTypes()
public override Type[] GetEventTypes()
{
return new Type[]
{
typeof(FooEvent.Event)
};
}

public Type GetUpdateType()
public override void Clear()
{
return typeof(Update);
}

public uint GetComponentId()
{
return ComponentId;
}

public void Clear()
{
entitiesUpdated.Clear();
updateStorage.Clear();
authorityChanges.Clear();
componentsAdded.Clear();
componentsRemoved.Clear();
base.Clear();

fooEventEventStorage.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);

fooEventEventStorage.RemoveAll(change => change.EntityId.Id == entityId);
}

if (!componentsAdded.Remove(id))
{
componentsRemoved.Add(id);
}
}

public void AddEntityComponent(long entityId, Update component)
{
var id = new EntityId(entityId);
if (!componentsRemoved.Remove(id))
{
componentsAdded.Add(id);
}

AddUpdate(new ComponentUpdateReceived<Update>(component, id, 0));
}

public void AddUpdate(ComponentUpdateReceived<Update> 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<Update>> GetUpdates()
{
return updateStorage.Slice();
}

public MessagesSpan<ComponentUpdateReceived<Update>> GetUpdates(EntityId entityId)
{
var range = updateStorage.GetEntityRange(entityId);
return updateStorage.Slice(range.FirstIndex, range.Count);
}

public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges()
{
return authorityChanges.Slice();
}

public MessagesSpan<AuthorityChangeReceived> GetAuthorityChanges(EntityId entityId)
protected override void ClearEventStorage(long entityId)
{
var range = authorityChanges.GetEntityRange(entityId);
return authorityChanges.Slice(range.FirstIndex, range.Count);
fooEventEventStorage.RemoveAll(change => change.EntityId.Id == entityId);
}

MessagesSpan<ComponentEventReceived<FooEvent.Event>> IDiffEventStorage<FooEvent.Event>.GetEvents(EntityId entityId)
{
var range = fooEventEventStorage.GetEntityRange(entityId);
return fooEventEventStorage.Slice(range.FirstIndex, range.Count);
var (firstIndex, count) = fooEventEventStorage.GetEntityRange(entityId);
return fooEventEventStorage.Slice(firstIndex, count);
}

MessagesSpan<ComponentEventReceived<FooEvent.Event>> IDiffEventStorage<FooEvent.Event>.GetEvents()
Expand Down
Loading