Пример #1
0
        public override object ProcessAPIString(string apistring)
        {
            var data = new DataSources()
            {
                vessel = getVessel()
            };
            // Extract any arguments/parameters in this API string
            var name = apistring;

            parseParams(ref name, ref data);

            // Are we in flight mode, with a vessel?
            var cleanFlightMode = HighLogic.LoadedSceneIsFlight && data.vessel != null;

            try {
                // Get the API entry
                APIEntry apiEntry = null;
                process(name, out apiEntry);
                if (apiEntry == null)
                {
                    return(null);
                }

                // Can we run this variable at the moment?
                if (!apiEntry.alwaysEvaluable && !cleanFlightMode)
                {
                    if (data.vessel == null)
                    {
                        throw new VariableNotEvaluable(apistring, "No vessel!");
                    }
                    throw new VariableNotEvaluable(apistring, "Not in flight mode");
                }

                // run the API entry
                var result = apiEntry.function(data);
                // And return the serialization-ready value
                return(apiEntry.formatter.prepareForSerialization(result));
            } catch (UnknownAPIException)
            {
                if (!cleanFlightMode)
                {
                    throw new VariableNotEvaluable(apistring, "Plugin variables not evaluable outside flight scene with vessel");
                }

                // Try looking in the pluginManager
                var pluginAPI = _manager.GetAPIDelegate(name);
                // If no entry, just continue the throwing of the exception
                if (pluginAPI == null)
                {
                    throw;
                }

                // We found an API entry! Let's use that.
                return(pluginAPI(data.vessel, data.args.ToArray()));
            }
        }
Пример #2
0
        private APIEntry argumentParse(String args, DataSources dataSources)
        {
            String[] argsSplit = args.Split(ARGUMENTS_ASSIGN);
            APIEntry result    = null;

            kspAPI.process(argsSplit[1], out result);

            dataSources.setVarName(argsSplit[0]);
            return(result);
        }
Пример #3
0
        public void getAPIEntry(string APIString, ref List <APIEntry> APIList)
        {
            APIEntry result = null;

            foreach (DataLinkHandler APIHandler in APIHandlers)
            {
                if (APIHandler.process(APIString, out result))
                {
                    break;
                }
            }

            APIList.Add(result);
        }
Пример #4
0
        public void process(String API, out APIEntry apiEntry)
        {
            APIEntry result = null;

            foreach (DataLinkHandler APIHandler in APIHandlers)
            {
                if (APIHandler.process(API, out result))
                {
                    break;
                }
            }

            apiEntry = result;
        }
Пример #5
0
        public void process(String API, out APIEntry apiEntry)
        {
            APIEntry result = null;

            foreach (DataLinkHandler APIHandler in APIHandlers)
            {
                if (APIHandler.process(API, out result))
                {
                    break;
                }
            }
            if (result == null)
            {
                throw new UnknownAPIException("Could not find API entry named " + API, API);
            }
            apiEntry = result;
        }
Пример #6
0
        private String argumentsParse(String args, DataSources dataSources)
        {
            APIEntry currentEntry = null;
            var      APIResults   = new Dictionary <string, object>();

            String[] argsSplit = args.Split(ARGUMENTS_DELIMETER);

            foreach (String arg in argsSplit)
            {
                string refArg = arg;
                PluginLogger.fine(refArg);
                kspAPI.parseParams(ref refArg, ref dataSources);
                currentEntry = argumentParse(refArg, dataSources);
                APIResults[dataSources.getVarName()] = currentEntry.formatter.prepareForSerialization(currentEntry.function(dataSources));

                //Only parse the paused argument if the active vessel is null
                if (dataSources.vessel == null)
                {
                    break;
                }
            }

            return(SimpleJson.SimpleJson.SerializeObject(APIResults));
        }
Пример #7
0
        private String argumentsParse(String args, DataSources dataSources)
        {
            APIEntry      currentEntry = null;
            List <string> APIResults   = new List <string>();

            String[] argsSplit = args.Split(ARGUMENTS_DELIMETER);

            foreach (String arg in argsSplit)
            {
                string refArg = arg;
                PluginLogger.fine(refArg);
                kspAPI.parseParams(ref refArg, ref dataSources);
                currentEntry = argumentParse(refArg, dataSources);
                APIResults.Add(currentEntry.formatter.format(currentEntry.function(dataSources)));

                //Only parse the paused argument if the active vessel is null
                if (dataSources.vessel == null)
                {
                    break;
                }
            }

            return(currentEntry.formatter.pack(APIResults));
        }
Пример #8
0
        public void process(String API, out APIEntry apiEntry)
        {
            APIEntry result = null;
            foreach (DataLinkHandler APIHandler in APIHandlers)
            {
                if (APIHandler.process(API, out result))
                {
                    break;
                }
            }

            apiEntry = result;
        }
Пример #9
0
        private void streamData(object sender, ElapsedEventArgs e)
        {
            streamTimer.Interval = streamRate;

            DataSources dataSources = new DataSources();

            if (toRun.Count + subscriptions.Count > 0)
            {
                try
                {
                    List <string> entries = new List <string>();

                    APIEntry entry = null;

                    lock (subscriptionLock)
                    {
                        dataSources.vessel = kspAPI.getVessel();

                        //Only parse the paused argument if the active vessel is null
                        if (dataSources.vessel != null)
                        {
                            toRun.UnionWith(subscriptions);

                            foreach (string s in toRun)
                            {
                                DataSources dataSourcesClone = dataSources.Clone();
                                string      trimedQuotes     = s.Trim();
                                string      refArg           = trimedQuotes;
                                kspAPI.parseParams(ref refArg, ref dataSourcesClone);

                                kspAPI.process(refArg, out entry);

                                if (entry != null)
                                {
                                    dataSourcesClone.setVarName(trimedQuotes);
                                    entries.Add(entry.formatter.format(entry.function(dataSourcesClone), dataSourcesClone.getVarName()));
                                }
                            }

                            toRun.Clear();

                            if (entry != null)
                            {
                                WebSocketFrame frame  = new WebSocketFrame(ASCIIEncoding.UTF8.GetBytes(entry.formatter.pack(entries)));
                                byte[]         bFrame = frame.AsBytes();
                                dataRates.addDownLinkPoint(System.DateTime.Now, bFrame.Length * UpLinkDownLinkRate.BITS_PER_BYTE);
                                clientConnection.Send(bFrame);
                            }
                        }
                        else
                        {
                            sendNullMessage();
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    PluginLogger.debug("Swallowing null reference exception, potentially due to async game state change.");
                    sendNullMessage();
                }
                catch (Exception ex)
                {
                    PluginLogger.debug("Closing socket due to potential client disconnect:" + ex.GetType().ToString());
                    close();
                }
            }
            else
            {
                sendNullMessage();
            }
        }