0

I have two tables which both contain an Environment table

table Environment {
windDirection:int = 0;
windMagnitude:int = 0;
windVariation:int = 0;
rain:ushort = 0;
snow:ushort = 0;
dust:ushort = 0;
fog:ushort = 0;
}

table REQUEST_SCENARIO_CHANGE_ENVIRONMENT_M {
   scenarioId:string;
   environment:Environment;
}

enum SCENARIO_CHANGE_ENVIRONMENT_RESULT_E : byte {
  SUCCESS,
  CLIENT_DOESNT_CONTROL_SCENARIO,
  SCENARIO_NOT_IN_FREE_MODE, // scenario was not setup-free'd
  SCENARIO_NOT_FOUND,
  UNKNOWN_FAILURE
}

table RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M {
   result:SCENARIO_CHANGE_ENVIRONMENT_RESULT_E;
   environment:Environment;
}

As the name implies, the first table is a request made by one of many controlling clients and sent to a server. The server responds with an error code (result). If the result is SUCESS, the RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M is sent to all clients. This way if the environment is successfully changed by one each is alerted to that fact.

My question is, since I already have a properly populated Environment object in the code where I'm creating the RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M, is it possible to take

     var message = Message.GetRootAsMessage(Utilities.ByteBuffer(data.Data));
     var request = REQUEST_SCENARIO_CHANGE_ENVIRONMENT_M.GetRootAsREQUEST_SCENARIO_CHANGE_ENVIRONMENT_M(Utilities.ByteBuffer(message.GetPayloadBytes()));
     SCENARIO_CHANGE_ENVIRONMENT_RESULT_E result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.UNKNOWN_FAILURE;
     Scenario scenario = GetScenarioFromId(request.ScenarioId);
     if (scenario == null)
     {
        result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SCENARIO_NOT_FOUND;
     }
     else
     {
        if (!scenario.IsController(message.SessionToken))
        {
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.CLIENT_DOESNT_CONTROL_SCENARIO;
        }
        else if (scenario.EndScenario() == STS_ERRORS.ERR_NO_ERROR)
        {
           // TODO: Unit Test
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SUCCESS;
        }
        else
        {
           // TODO: Unit Test
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.UNKNOWN_FAILURE;
        }
     }
     FlatBuffers.FlatBufferBuilder fbb = new FlatBuffers.FlatBufferBuilder(1024);
     IOSServer.IOSCore.Messages.Environment.StartEnvironment(fbb);

     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.StartRESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M(fbb);
     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddResult(fbb, result);
     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddEnvironment(fbb, request.Environment);
        var response = RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.EndRESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M(fbb);
     fbb.Finish(response.Value);
     // If the scenario is not able to be ended. Only respond to the originator
     if (result != SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SUCCESS)
     {
        EventData responseData = new EventData(Utilities.GetMessageBytes(fbb, DataType.RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M, Utilities.UniqueId(), 0, message.RequestId));// TODO: Timestamp
        Mediator.GetInstance().RaiseEvent(responseData);
     }
     else // Otherwise, send to all scenario members
     {
        EventData responseData = new EventData(Utilities.GetMessageBytes(fbb, DataType.RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M, Utilities.UniqueId()));// TODO: Timestamp
        Mediator.GetInstance().RaiseEvent(responseData, sendToAllExternalClients: true);
        _scenarios.Remove(request.ScenarioId);
     }

The problem is

RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddEnvironment(fbb, request.Environment);

Is it possible to get an FlatBuffers.Offset<Environment> from Environment?

JohnGalt131
  • 5
  • 1
  • 5

1 Answers1

0

Copying a table from one FlatBuffer to another is generally not possible, except with reflection::CopyTable in C++ (or using the object API).

It needs to be copied (cannot simply be referred to) since all tables and their dependencies need to reside in the same buffer, and are accessed thru offsets.

For the moment in C#, your only solution is to write your own CopyEnvironment function.

Aardappel
  • 5,559
  • 1
  • 19
  • 22