private T SyncRequest <T>(CmdId reqCmdId, CmdId resCmdId, object data)
        {
            Handle handle = new Handle();

            lock (_handles)
            {
                bool passedZero = false;
                do
                {
                    if (_sequenceNumber == 0)
                    {
                        // Sequence Number 0 is reserved for Game Events
                        _sequenceNumber = 1;
                        // This avoids the rare scenario that we have somehow
                        // simultainiously used up all 65534 available sequence
                        // numbers. If we pass Zero twice insize a lock.. fsck it.
                        if (passedZero)
                        {
                            throw new Exception("Ran out of Available Sequence Numbers");
                        }
                        else
                        {
                            passedZero = true;
                        }
                    }
                    else
                    {
                        // Increment until we find an unused SequenceNumber
                        _sequenceNumber += 1;
                    }
                } while (_handles.ContainsKey(_sequenceNumber));
                handle._sequenceNumber = _sequenceNumber;
                _handles.Add(_sequenceNumber, handle);
            }

            lock (handle)
            {
                _modGameApi.Game_Request(reqCmdId, handle._sequenceNumber, data);
                Monitor.Wait(handle);
            }

            lock (_handles)
            {
                _handles.Remove(handle._sequenceNumber);
            }

            if (handle._resCmdId == CmdId.Event_Error)
            {
                ErrorInfo errorInfo = (ErrorInfo)handle._responseData;
                throw new Exception("Error Invoking Game Request: " + Enum.GetName(typeof(ErrorType), errorInfo.errorType));
            }

            if (handle._resCmdId != resCmdId)
            {
                throw new Exception("Expected CmdId: " + Enum.GetName(typeof(CmdId), resCmdId)
                                    + " Found CmdId: " + Enum.GetName(typeof(CmdId), handle._resCmdId));
            }
            return((T)handle._responseData);
        }
Beispiel #2
0
 private void Messenger(String msgType, int Priority, int player, String msg, int Duration)
 {
     if (msgType == "ChatAsServer")
     {
         string command = "SAY '" + msg + "'";
         GameAPI.Game_Request(CmdId.Request_ConsoleCommand, (ushort)CmdId.Request_InGameMessage_AllPlayers, new Eleon.Modding.PString(command));
     }
     if (msgType == "Alert")
     {
         GameAPI.Game_Request(CmdId.Request_InGameMessage_AllPlayers, (ushort)CmdId.Request_InGameMessage_AllPlayers, new IdMsgPrio(player, msg, Convert.ToByte(Priority), Duration));
     }
 }
    private void trackHandler(APICmd cmd, Action <CmdId, object> handler)
    {
        var seqNr = unusedSequenceNumbers.Dequeue();

        actionTracker[seqNr] = handler;
        GameAPI.Game_Request(cmd.cmd, seqNr, cmd.data);
    }
    public void Game_Update()
    {
        // Exchange lists
        List <ModProtocol.Package> temp;

        lock (receivedPackages) {
            temp                 = receivedPackages;
            receivedPackages     = receivedPackagesTemp;
            receivedPackagesTemp = temp;
            receivedPackages.Clear();
        }

        // Send request to the game
        ModProtocol.Package tmpPackage;
        if (temp.Count > 0)
        {
            GameAPI.Console_Write("Updating Game State");
        }

        for (int i = 0; i < temp.Count; i++)
        {
            tmpPackage = temp[i];
            GameAPI.Console_Write(string.Format("updating game, id: {0}, type: {1}", tmpPackage.cmd, Enum.GetName(cmdType, tmpPackage.cmd)));
            GameAPI.Game_Request(tmpPackage.cmd, tmpPackage.seqNr, tmpPackage.data);
        }
        temp.Clear();
    }
Beispiel #5
0
        private void trackHandler(APICmd cmd, Action <CmdId, object> handler)
        {
            var seqNr = (ushort)((handledMessageCount++ % 30000) + offset);

            actionTracker[seqNr] = handler;
            GameAPI.Game_Request(cmd.cmd, seqNr, cmd.data);
        }
    public void Game_Event(CmdId eventId, ushort seqNr, object data)
    {
        broker.HandleMessage(eventId, seqNr, data);

        if (settlementOperations.ContainsKey(seqNr))
        {
            var op = settlementOperations[seqNr];
            HandleSettlementWokflowEvent(GameAPI, op, eventId, data);
        }

        GameAPI.Console_Write($"ID:EVENT! {eventId} - {seqNr}");
        try
        {
            switch (eventId)
            {
            case CmdId.Event_Playfield_Stats:
                var playfieldData = (PlayfieldStats)data;
                var pstatstring   = $"id: {Serializer.Serialize(playfieldData)}";
                GameAPI.Console_Write(pstatstring);
                break;

            case CmdId.Event_Player_Connected:
                GameAPI.Game_Request(CmdId.Request_Player_Info, (ushort)CmdId.Request_Player_Info, (Id)data);
                break;

            case CmdId.Event_Statistics:

                StatisticsParam stats = (StatisticsParam)data;
                Handle_event_statistics(stats);
                break;

            case CmdId.Event_ChatMessage:
                ChatInfo ci = (ChatInfo)data;
                Handle_chat_message(ci);
                break;

            case CmdId.Event_GlobalStructure_List:
                GlobalStructureList info = (GlobalStructureList)data;
                writeGlobalStructureList(info);
                break;

            default:
                GameAPI.Console_Write($"event: {eventId}");
                var outmessage = "NO DATA";
                if (data != null)
                {
                    outmessage = "data: " + data.ToString();
                }
                GameAPI.Console_Write(outmessage);

                break;
            }
        }
        catch (Exception ex)
        {
            GameAPI.Console_Write(ex.Message);
            GameAPI.Console_Write(ex.ToString());
        }
    }
Beispiel #7
0
        void HandleGlobalStructureList(GlobalStructureList gsl)
        {
            if (gsl == null)
            {
                return;
            }

            foreach (KeyValuePair <string, List <GlobalStructureInfo> > pfstructs in gsl.globalStructures)
            {
                mGameAPI.Console_Write("GSL for " + pfstructs.Key);
                foreach (GlobalStructureInfo gsi in pfstructs.Value)
                {
                    if (gsi.coreType == 1)                      //player core
                    {
                        mGameAPI.Console_Write("Player Structure: " + gsi.name + ", ID: " + gsi.id);
                        mGameAPI.Console_Write("Powered: " + gsi.powered);
                        mGameAPI.Console_Write("Fuel Remaining: " + gsi.fuel);

                        mGameAPI.Game_Request(CmdId.Request_Structure_BlockStatistics, (ushort)0, new Id(gsi.id));
                    }
                }
            }
        }
Beispiel #8
0
    private void HandleSettlementWokflowEvent(ModGameAPI GameAPI, SettlementOperation operation, CmdId eventType, object data)
    {
        var operationPayload = Serializer.Serialize(operation);

        var message = $"*** processing operation {operation.seqNr}\n *** cmdid:{eventType} \n *** " +
                      $"last operation: {operation.stage} \n***  payload: {operationPayload}";

        GameAPI.Console_Write(message);


        switch (eventType)
        {
        case CmdId.Event_GlobalStructure_List:
            var structureList = (GlobalStructureList)data;
            operation = updateOperationFromStructureList(operation, structureList);
            if (operation.stage != SettlementStage.IdentifiedReplacement)
            {
                deprovisionOperation(operation.seqNr);
                return;
            }
            settlementOperations[operation.seqNr] = operation;
            GameAPI.Game_Request(CmdId.Request_NewEntityId, operation.seqNr, null);
            break;

        case CmdId.Event_NewEntityId:
            var newId = (Id)data;

            GameAPI.Console_Write($"*** new id: {Serializer.Serialize(newId)} ***");

            operation.newStructureId = newId;
            EntitySpawnInfo newInfo = new EntitySpawnInfo()
            {
                forceEntityId = newId.id,
                playfield     = operation.playfieldName,
                pos           = operation.originalStructureInfo.pos,
                rot           = operation.originalStructureInfo.rot,
                name          = operation.newStructureName,
                prefabName    = "Test-Bed (Settled)",
                type          = operation.originalStructureInfo.type,
            };

            GameAPI.Console_Write($"*** requesting spawn: {Serializer.Serialize(newInfo)} ***");
            operation.newStructureInfo            = newInfo;
            operation.stage                       = SettlementStage.ProvisionedReplacement;
            operation.stage                       = SettlementStage.RequestedDemolition;
            settlementOperations[operation.seqNr] = operation;
            Id outId = new Id(operation.originalStructureInfo.id);
            GameAPI.Game_Request(CmdId.Request_Entity_Destroy, operation.seqNr, outId);

            break;

        case CmdId.Event_Ok:
            if (operation.stage == SettlementStage.RequestedDemolition)
            {
                operation.stage = SettlementStage.EmplacedNewSettlement;
                settlementOperations[operation.seqNr] = operation;
                GameAPI.Console_Write($"*** new settlement info:{Serializer.Serialize(operation.newStructureInfo)}");
                GameAPI.Game_Request(CmdId.Request_Entity_Spawn, operation.seqNr, operation.newStructureInfo);
            }
            else if (operation.stage == SettlementStage.EmplacedNewSettlement)
            {
                operation.stage = SettlementStage.SettlementComplete;
                settlementOperations[operation.seqNr] = operation;
                deprovisionOperation(operation.seqNr);
                GameAPI.Console_Write("*** settlement complete!!! ***");
            }
            break;

        case CmdId.Event_Error:
            var error = (ErrorInfo)data;
            GameAPI.Console_Write($"*** error: {Serializer.Serialize(error)} ***");

            deprovisionOperation(operation.seqNr);
            break;
        }
    }
Beispiel #9
0
        private void ChatMessage(String msg)
        {
            String command = "SAY '" + msg + "'";

            GameAPI.Game_Request(CmdId.Request_ConsoleCommand, (ushort)CmdId.Request_InGameMessage_AllPlayers, new Eleon.Modding.PString(command));
        }
Beispiel #10
0
 public void Get_PlayerList()
 {
     GameAPI.Game_Request(CmdId.Request_Player_List, (ushort)CmdId.Request_Player_List, null);
 }