Beispiel #1
0
        public static GenericAPICommand Execute(GenericAPICommand command)
        {
            List <GenericAPICommand> tmp;
            var commandList = commandTracker.TryGetValue(command.SequenceNumber, out tmp) ? tmp : new List <GenericAPICommand>();

            log(() => $"adding: {command.responseHandler}");
            commandList.Add(command);
            commandTracker[command.SequenceNumber] = commandList;
            api.Game_Request(command.call.CmdId, command.SequenceNumber, command.argument);
            return(command);
        }
Beispiel #2
0
        public static GenericAPICommand Execute(GenericAPICommand command)
        {
            CheckAndWarnIfBrokerNotLoaded();

            lock (commandTracker)
            {
                command.assignSequenceNumber();
                commandTracker.Add(command.SequenceNumber.ToString(), command, DateTime.Now.AddMilliseconds(cacheTimeInMilliseconds));
            }
            api.Game_Request(command.call.CmdId, command.SequenceNumber, command.argument);
            return(command);
        }
Beispiel #3
0
 private static void setupTimer <T>(Timer timeoutTimer, GenericAPICommand command, TaskCompletionSource <T> completionSource)
 {
     timeoutTimer.Elapsed += (_, __) =>
     {
         if (!completionSource.Task.IsCompleted)
         {
             timeoutTimer.Close();
             lock (commandTracker)
             {
                 commandTracker.Remove(command.SequenceNumber.ToString());
             }
             throw new EmpyrionAPITimeoutException("Encountered timeout while waiting for response to command", command);
         }
     };
     timeoutTimer.Start();
 }
Beispiel #4
0
        public static void Request_AlliancesFaction(AlliancesFaction arg, Action <AlliancesFaction> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((AlliancesFaction)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_AlliancesFaction,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #5
0
        public static void Request_Entity_SetName(IdPlayfieldName arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Entity_SetName,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #6
0
        public static void Request_GlobalStructure_Update(PString arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_GlobalStructure_Update,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #7
0
        public static void Request_Player_GetAndRemoveInventory(Id arg, Action <Inventory> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((Inventory)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Player_GetAndRemoveInventory,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #8
0
        public static void Request_Playfield_Entity_List(PString arg, Action <PlayfieldEntityList> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((PlayfieldEntityList)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Playfield_Entity_List,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #9
0
        public static void Request_InGameMessage_Faction(IdMsgPrio arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_InGameMessage_Faction,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #10
0
        public static void Request_Player_AddItem(IdItemStack arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Player_AddItem,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #11
0
        public static void Request_Blueprint_Resources(BlueprintResources arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Blueprint_Resources,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #12
0
        public static void Request_GetBannedPlayers(Action <IdList> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((IdList)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_GetBannedPlayers,
                null,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #13
0
        public static void Request_Get_Factions(Id arg, Action <FactionInfoList> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((FactionInfoList)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Get_Factions,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #14
0
        public static void Request_Dedi_Stats(Action <DediStats> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((DediStats)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Dedi_Stats,
                null,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #15
0
        public static void Request_Entity_PosAndRot(Id arg, Action <IdPositionRotation> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((IdPositionRotation)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Entity_PosAndRot,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #16
0
        public static void Request_Player_ItemExchange(ItemExchangeInfo arg, Action <ItemExchangeInfo> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((ItemExchangeInfo)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Player_ItemExchange,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #17
0
        public static void Request_Player_ChangePlayerfield(IdPlayfieldPositionRotation arg, Action callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback();
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Player_ChangePlayerfield,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #18
0
        public static void Request_ShowDialog_SinglePlayer(DialogBoxData arg, Action <IdAndIntValue> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((IdAndIntValue)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_ShowDialog_SinglePlayer,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #19
0
        public static void Request_Structure_BlockStatistics(Id arg, Action <IdStructureBlockInfo> callback = null, Action <ErrorInfo> onError = null)
        {
            Action <CmdId, object> wiredCallback = null;

            if (callback != null)
            {
                wiredCallback = (_, val) => callback((IdStructureBlockInfo)val);
            }

            var apiCmd = new GenericAPICommand(
                CmdId.Request_Structure_BlockStatistics,
                arg,
                wiredCallback,
                onError ?? noOpErrorHandler
                );

            Broker.Execute(apiCmd);
        }
Beispiel #20
0
        public static void HandleGameEvent(CmdId eventId, ushort seqNr, object data)
        {
            var apiEvent = new apiEvent(eventId, seqNr, data);

            Delegate handler;

            if (eventTable.TryGetValue(eventId, out handler))
            {
                handler.DynamicInvoke(new object[] { data });
            }

            var seqNrString = seqNr.ToString();

            if (!commandTracker.Contains(seqNrString))
            {
                return;
            }

            GenericAPICommand outstandingCommand = default(GenericAPICommand);

            lock (commandTracker)
            {
                outstandingCommand = (GenericAPICommand)commandTracker.Get(seqNrString);
                if (eventId == CmdId.Event_Error || outstandingCommand.validateResponse(eventId, seqNr, data))
                {
                    commandTracker.Remove(seqNrString);
                }
                else
                {
                    return;
                }
            }
            if (eventId == CmdId.Event_Error && outstandingCommand.errorHandler != null)
            {
                outstandingCommand.errorHandler((ErrorInfo)data);
            }
            else
            {
                outstandingCommand.responseHandler(eventId, data);
            }
        }
Beispiel #21
0
        public static Task <R> CreateCommandWithArgAndReturn <A, R>(CmdId cmdId, A arg, Action <R> successCallback = null, Action <ErrorInfo> onError = null)
        {
            var completionSource = new TaskCompletionSource <R>();
            var timeoutTimer     = new System.Timers.Timer(cacheTimeInMilliseconds);

            timeoutTimer.AutoReset = false;

            var successfulCallback = generateSuccessCallback <R>(completionSource, successCallback, timeoutTimer);
            var errorCallback      = generateErrorCallback(onError, timeoutTimer);

            var apiCmd = new GenericAPICommand(
                cmdId,
                arg,
                successfulCallback,
                errorCallback
                );

            Broker.Execute(apiCmd);
            setupTimer <R>(timeoutTimer, apiCmd, completionSource);
            return(completionSource.Task);
        }
Beispiel #22
0
        internal void Add(string seqNrString, GenericAPICommand command, DateTime dateTime)
        {
            this[seqNrString] = command;
            var timeinMS        = (dateTime - DateTime.Now).TotalMilliseconds;
            var expirationTimer = new Timer(timeinMS)
            {
                AutoReset = false
            };

            expirationTimer.Elapsed += (a, b) =>
            {
                object tmp;
                lock (this)
                {
                    if (this.TryGetValue(seqNrString, out tmp) && tmp == command)
                    {
                        this.Remove(seqNrString);
                    }
                }
                expirationTimer.Close();
            };
            expirationTimer.Start();
        }
Beispiel #23
0
 public EmpyrionAPITimeoutException(string message, GenericAPICommand command) : base(message)
 {
     this.command = command;
 }