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

Commit

Permalink
Simplify command codegen (#1263)
Browse files Browse the repository at this point in the history
* De-duplicate command metadata storage classes

* Simplify command diff (de)serializers

* Simplify CommandSendStorage

* Simplify CommandDiffStorage

* Improve interfaces, make classes private

* Simplify SubscriptionManagers

* Add ability to force code generation with --force flag

* Actually use the force parameter

* Changelog

* Simplify Command*SubscriptionManagers
  • Loading branch information
zeroZshadow committed Feb 3, 2020
1 parent 9a5aeeb commit 7905523
Show file tree
Hide file tree
Showing 69 changed files with 642 additions and 1,266 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
- Simplified dirtyBits logic and code generation [#1255](https://github.com/spatialos/gdk-for-unity/pull/1255)
- The DeploymentLauncher now uses Platform SDK v14.4.0. [#1260](https://github.com/spatialos/gdk-for-unity/pull/1260)
- `init.sh` and `init.ps1` now support the `--china` and `-china` flag respectively to download from the `cn-production` environment. [#1261](https://github.com/spatialos/gdk-for-unity/pull/1261)
- Simplified code generation for Command classes and relevant interfaces. [#1263](https://github.com/spatialos/gdk-for-unity/pull/1263)
- Added `--force` flag to the CodeGenerator project to skip the dirty checks and re-generate everything. [#1263](https://github.com/spatialos/gdk-for-unity/pull/1263)

## `0.3.2` - 2019-12-23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Improbable.DependentSchema
public class DependentComponentReaderSubscriptionManager : SubscriptionManager<DependentComponentReader>
{
private readonly EntityManager entityManager;
private readonly World world;
private readonly WorkerSystem workerSystem;

private Dictionary<EntityId, HashSet<Subscription<DependentComponentReader>>> entityIdToReaderSubscriptions;

Expand All @@ -27,14 +25,10 @@ public class DependentComponentReaderSubscriptionManager : SubscriptionManager<D
private HashSet<EntityId> entitiesMatchingRequirements = new HashSet<EntityId>();
private HashSet<EntityId> entitiesNotMatchingRequirements = new HashSet<EntityId>();

public DependentComponentReaderSubscriptionManager(World world)
public DependentComponentReaderSubscriptionManager(World world) : base(world)
{
this.world = world;
entityManager = world.EntityManager;

// todo Check that these are there
workerSystem = world.GetExistingSystem<WorkerSystem>();

var constraintCallbackSystem = world.GetExistingSystem<ComponentConstraintsCallbackSystem>();

constraintCallbackSystem.RegisterComponentAddedCallback(DependentComponent.ComponentId, entityId =>
Expand Down Expand Up @@ -146,21 +140,15 @@ private void OnComponentRemoved(EntityId entityId)
[AutoRegisterSubscriptionManager]
public class DependentComponentWriterSubscriptionManager : SubscriptionManager<DependentComponentWriter>
{
private readonly World world;
private readonly WorkerSystem workerSystem;
private readonly ComponentUpdateSystem componentUpdateSystem;

private Dictionary<EntityId, HashSet<Subscription<DependentComponentWriter>>> entityIdToWriterSubscriptions;

private HashSet<EntityId> entitiesMatchingRequirements = new HashSet<EntityId>();
private HashSet<EntityId> entitiesNotMatchingRequirements = new HashSet<EntityId>();

public DependentComponentWriterSubscriptionManager(World world)
public DependentComponentWriterSubscriptionManager(World world) : base(world)
{
this.world = world;

// todo Check that these are there
workerSystem = world.GetExistingSystem<WorkerSystem>();
componentUpdateSystem = world.GetExistingSystem<ComponentUpdateSystem>();

var constraintCallbackSystem = world.GetExistingSystem<ComponentConstraintsCallbackSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@ namespace Improbable.DependentSchema
{
public partial class DependentDataComponent
{
public class BarCommandDiffCommandDeserializer : ICommandDiffDeserializer
private class BarCommandDiffCommandDeserializer : ICommandDiffDeserializer
{
public uint GetComponentId()
{
return ComponentId;
}

public uint GetCommandId()
{
return 1;
}

public void AddRequestToDiff(CommandRequestOp op, ViewDiff diff)
{
var deserializedRequest = global::Improbable.TestSchema.SomeType.Serialization.Deserialize(op.Request.SchemaData.Value.GetObject());
Expand Down Expand Up @@ -60,18 +50,8 @@ public void AddResponseToDiff(CommandResponseOp op, ViewDiff diff, CommandMetaDa
}
}

public class BarCommandCommandSerializer : ICommandSerializer
private class BarCommandCommandSerializer : ICommandSerializer
{
public uint GetComponentId()
{
return ComponentId;
}

public uint GetCommandId()
{
return 1;
}

public void Serialize(MessagesToSend messages, SerializedMessagesToSend serializedMessages, CommandMetaData commandMetaData)
{
var storage = (BarCommandCommandsToSendStorage) messages.GetCommandSendStorage(ComponentId, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,182 +2,26 @@
// DO NOT EDIT - this file is automatically regenerated.
// =====================================================

using System;
using System.Collections.Generic;
using Improbable.Gdk.Core;
using Unity.Entities;
using Improbable.Gdk.Core.Commands;

namespace Improbable.DependentSchema
{
public partial class DependentDataComponent
{
public class DiffBarCommandCommandStorage : IComponentCommandDiffStorage
, IDiffCommandRequestStorage<BarCommand.ReceivedRequest>
, IDiffCommandResponseStorage<BarCommand.ReceivedResponse>
private class DiffBarCommandCommandStorage
: DiffSpawnCubeCommandStorage<BarCommand.ReceivedRequest, BarCommand.ReceivedResponse>
{
private readonly MessageList<BarCommand.ReceivedRequest> requestStorage =
new MessageList<BarCommand.ReceivedRequest>();

private readonly MessageList<BarCommand.ReceivedResponse> responseStorage =
new MessageList<BarCommand.ReceivedResponse>();

private readonly RequestComparer requestComparer = new RequestComparer();
private readonly ResponseComparer responseComparer = new ResponseComparer();

private bool requestsSorted;
private bool responsesSorted;

public uint GetComponentId()
{
return ComponentId;
}

public uint GetCommandId()
{
return 1;
}

public Type GetRequestType()
{
return typeof(BarCommand.ReceivedRequest);
}

public Type GetResponseType()
{
return typeof(BarCommand.ReceivedResponse);
}

public void Clear()
{
requestStorage.Clear();
responseStorage.Clear();
requestsSorted = false;
responsesSorted = false;
}

public void RemoveRequests(long entityId)
{
requestStorage.RemoveAll(request => request.EntityId.Id == entityId);
}

public void AddRequest(BarCommand.ReceivedRequest request)
{
requestStorage.Add(request);
}

public void AddResponse(BarCommand.ReceivedResponse response)
{
responseStorage.Add(response);
}

public MessagesSpan<BarCommand.ReceivedRequest> GetRequests()
{
return requestStorage.Slice();
}

public MessagesSpan<BarCommand.ReceivedRequest> GetRequests(EntityId targetEntityId)
{
if (!requestsSorted)
{
requestStorage.Sort(requestComparer);
requestsSorted = true;
}

var (firstIndex, count) = requestStorage.GetEntityRange(targetEntityId);
return requestStorage.Slice(firstIndex, count);
}

public MessagesSpan<BarCommand.ReceivedResponse> GetResponses()
{
return responseStorage.Slice();
}

public MessagesSpan<BarCommand.ReceivedResponse> GetResponse(long requestId)
{
if (!responsesSorted)
{
responseStorage.Sort(responseComparer);
responsesSorted = true;
}

var responseIndex = responseStorage.GetResponseIndex(requestId);
return responseIndex.HasValue
? responseStorage.Slice(responseIndex.Value, 1)
: MessagesSpan<BarCommand.ReceivedResponse>.Empty();
}

private class RequestComparer : IComparer<BarCommand.ReceivedRequest>
{
public int Compare(BarCommand.ReceivedRequest x, BarCommand.ReceivedRequest y)
{
return x.EntityId.Id.CompareTo(y.EntityId.Id);
}
}

private class ResponseComparer : IComparer<BarCommand.ReceivedResponse>
{
public int Compare(BarCommand.ReceivedResponse x, BarCommand.ReceivedResponse y)
{
return x.RequestId.CompareTo(y.RequestId);
}
}
public override uint ComponentId => Improbable.DependentSchema.DependentDataComponent.ComponentId;
public override uint CommandId => 1;
}

public class BarCommandCommandsToSendStorage : ICommandSendStorage, IComponentCommandSendStorage
, ICommandRequestSendStorage<BarCommand.Request>
, ICommandResponseSendStorage<BarCommand.Response>
private class BarCommandCommandsToSendStorage :
CommandSendStorage<BarCommand.Request, BarCommand.Response>,
IComponentCommandSendStorage
{
private readonly MessageList<CommandRequestWithMetaData<BarCommand.Request>> requestStorage =
new MessageList<CommandRequestWithMetaData<BarCommand.Request>>();

private readonly MessageList<BarCommand.Response> responseStorage =
new MessageList<BarCommand.Response>();

public uint GetComponentId()
{
return ComponentId;
}

public uint GetCommandId()
{
return 1;
}

public Type GetRequestType()
{
return typeof(BarCommand.Request);
}

public Type GetResponseType()
{
return typeof(BarCommand.Response);
}

public void Clear()
{
requestStorage.Clear();
responseStorage.Clear();
}

public void AddRequest(BarCommand.Request request, Entity entity, long requestId)
{
requestStorage.Add(new CommandRequestWithMetaData<BarCommand.Request>(request, entity, requestId));
}

public void AddResponse(BarCommand.Response response)
{
responseStorage.Add(response);
}

internal MessageList<CommandRequestWithMetaData<BarCommand.Request>> GetRequests()
{
return requestStorage;
}

internal MessageList<BarCommand.Response> GetResponses()
{
return responseStorage;
}
uint IComponentCommandSendStorage.ComponentId => ComponentId;
uint IComponentCommandSendStorage.CommandId => 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,17 @@
// DO NOT EDIT - this file is automatically regenerated.
// =====================================================

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

namespace Improbable.DependentSchema
{
public partial class DependentDataComponent
{
public class BarCommandCommandMetaDataStorage : ICommandMetaDataStorage, ICommandPayloadStorage<global::Improbable.TestSchema.SomeType>
private class BarCommandCommandMetaDataStorage :
CommandPayloadStorage<global::Improbable.TestSchema.SomeType>,
ICommandMetaDataStorage
{
private readonly Dictionary<long, CommandContext<global::Improbable.TestSchema.SomeType>> requestIdToRequest =
new Dictionary<long, CommandContext<global::Improbable.TestSchema.SomeType>>();

private readonly Dictionary<long, long> internalRequestIdToRequestId = new Dictionary<long, long>();

public uint GetComponentId()
{
return ComponentId;
}

public uint GetCommandId()
{
return 1;
}

public void RemoveMetaData(long internalRequestId)
{
var requestId = internalRequestIdToRequestId[internalRequestId];
internalRequestIdToRequestId.Remove(internalRequestId);
requestIdToRequest.Remove(requestId);
}

public void SetInternalRequestId(long internalRequestId, long requestId)
{
internalRequestIdToRequestId.Add(internalRequestId, requestId);
}

public void AddRequest(in CommandContext<global::Improbable.TestSchema.SomeType> context)
{
requestIdToRequest[context.RequestId] = context;
}

public CommandContext<global::Improbable.TestSchema.SomeType> GetPayload(long internalRequestId)
{
var id = internalRequestIdToRequestId[internalRequestId];
return requestIdToRequest[id];
}
public uint CommandId => 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,9 @@ public struct Request : ICommandRequest
Payload = request;
}

long IReceivedCommandRequest.GetRequestId()
{
return RequestId;
}
long IReceivedCommandRequest.RequestId => RequestId;

EntityId IReceivedEntityMessage.GetEntityId()
{
return EntityId;
}
EntityId IReceivedEntityMessage.EntityId => EntityId;
}

/// <summary>
Expand Down Expand Up @@ -126,10 +120,7 @@ public Response(long requestId, string failureMessage)
RequestId = requestId;
}

long IReceivedCommandResponse.GetRequestId()
{
return RequestId;
}
long IReceivedCommandResponse.RequestId => RequestId;
}

public readonly struct RawReceivedResponse : IRawReceivedCommandResponse
Expand Down

0 comments on commit 7905523

Please sign in to comment.