Example #1
0
        // Async task to find results given cond in helist, using only vars specified.

        private System.Threading.Tasks.Task <List <Tuple <ISystem, object[]> > > Find(List <HistoryEntry> helist, BaseUtils.ConditionLists cond, HashSet <string> varsusedincondition, ISystem cursystem)
        {
            return(System.Threading.Tasks.Task.Run(() =>
            {
                List <Tuple <ISystem, object[]> > rows = new List <Tuple <ISystem, object[]> >();
                foreach (var he in helist)
                {
                    BaseUtils.Variables scandata = new BaseUtils.Variables();
                    scandata.AddPropertiesFieldsOfClass(he.journalEntry, "",
                                                        new Type[] { typeof(System.Drawing.Icon), typeof(System.Drawing.Image), typeof(System.Drawing.Bitmap), typeof(QuickJSON.JObject) }, 5,
                                                        varsusedincondition);

                    bool?res = cond.CheckAll(scandata, out string errlist, out BaseUtils.ConditionLists.ErrorClass errclass);   // need function handler..

                    if (res.HasValue && res.Value == true)
                    {
                        ISystem sys = he.System;
                        string sep = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator + " ";

                        JournalScan js = he.journalEntry as JournalScan;
                        JournalFSSBodySignals jb = he.journalEntry as JournalFSSBodySignals;
                        JournalSAASignalsFound jbs = he.journalEntry as JournalSAASignalsFound;

                        string name, info;
                        if (js != null)
                        {
                            name = js.BodyName;
                            info = js.DisplayString(0);
                        }
                        else if (jb != null)
                        {
                            name = jb.BodyName;
                            jb.FillInformation(he.System, "", out info, out string d);
                        }
                        else
                        {
                            name = jbs.BodyName;
                            jbs.FillInformation(he.System, "", out info, out string d);
                        }

                        object[] rowobj = { EDDConfig.Instance.ConvertTimeToSelectedFromUTC(he.EventTimeUTC).ToString(),
                                            name,
                                            info,
                                            (cursystem != null ? cursystem.Distance(sys).ToString("0.#") : ""),
                                            sys.X.ToString("0.#") + sep + sys.Y.ToString("0.#") + sep + sys.Z.ToString("0.#") };
                        rows.Add(new Tuple <ISystem, object[]>(sys, rowobj));
                    }
                }

                return rows;
            }));
        }
        private void Controller_NewUIEvent(UIEvent uievent)
        {
            BaseUtils.Variables cv = new BaseUtils.Variables();

            string prefix = "EventClass_";

            cv.AddPropertiesFieldsOfClass(uievent, prefix, new Type[] { typeof(System.Drawing.Icon), typeof(System.Drawing.Image), typeof(System.Drawing.Bitmap), typeof(BaseUtils.JSON.JObject) }, 5);
            cv[prefix + "UIDisplayed"] = "0";
            actioncontroller.ActionRun(Actions.ActionEventEDList.onUIEvent, cv);
            actioncontroller.ActionRun(Actions.ActionEventEDList.EliteUIEvent(uievent), cv);

            if (!uievent.EventRefresh)      // don't send the refresh events thru the system..  see if profiles need changing
            {
                Actions.ActionVars.TriggerVars(cv, "UI" + uievent.EventTypeStr, "UIEvent");

                int i = EDDProfiles.Instance.ActionOn(cv, out string errlist);
                if (i >= 0)
                {
                    ChangeToProfileId(i, true);
                }

                if (errlist.HasChars())
                {
                    LogLine("Profile reports errors in triggers:".T(EDTx.EDDiscoveryForm_PE1) + errlist);
                }

                try
                {
                    if (DLLManager.Count > 0)       // if worth calling..
                    {
                        BaseUtils.JSON.JToken t = BaseUtils.JSON.JToken.FromObject(uievent, ignoreunserialisable: true,
                                                                                   ignored: new Type[] { typeof(Bitmap), typeof(Image) },
                                                                                   maxrecursiondepth: 3);
                        string output = t?.ToString();
                        if (output != null)
                        {
                            //  System.Diagnostics.Debug.WriteLine("DLL JSON UI String " + output);
                            DLLManager.NewUIEvent(output);
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("JSON convert error from object in DLL");
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Could not serialise " + uievent.EventTypeStr + " " + ex);
                }
            }
        }
Example #3
0
        private void buttonFind_Click(object sender, EventArgs e)
        {
            BaseUtils.ConditionLists cond = Valid();
            if (cond != null)
            {
                this.Cursor = Cursors.WaitCursor;
                dataGridView.Rows.Clear();

                DataGridViewColumn sortcol   = dataGridView.SortedColumn != null ? dataGridView.SortedColumn : dataGridView.Columns[0];
                SortOrder          sortorder = dataGridView.SortedColumn != null ? dataGridView.SortOrder : SortOrder.Descending;

                ISystem cursystem = discoveryform.history.CurrentSystem();        // could be null

                foreach (var he in discoveryform.history.FilterByScan())
                {
                    JournalScan js = he.journalEntry as JournalScan;

                    BaseUtils.Variables scandata = new BaseUtils.Variables();
                    scandata.AddPropertiesFieldsOfClass(js, "", new Type[] { typeof(System.Drawing.Icon), typeof(System.Drawing.Image), typeof(System.Drawing.Bitmap), typeof(BaseUtils.JSON.JObject) }, 5);

                    bool?res = cond.CheckAll(scandata, out string errlist, out BaseUtils.ConditionLists.ErrorClass errclass);    // need function handler..

                    if (res.HasValue && res.Value == true)
                    {
                        ISystem  sys    = he.System;
                        string   sep    = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator + " ";
                        object[] rowobj =
                        {
                            EDDConfig.Instance.ConvertTimeToSelectedFromUTC(he.EventTimeUTC).ToString(),
                            js.BodyName,
                            js.DisplayString(0),
                            (cursystem != null ? cursystem.Distance(sys).ToString("0.#") : ""),
                            sys.X.ToString("0.#") + sep + sys.Y.ToString("0.#") + sep + sys.Z.ToString("0.#")
                        };

                        dataGridView.Rows.Add(rowobj);
                        dataGridView.Rows[dataGridView.Rows.Count - 1].Tag = sys;
                    }

                    if (errclass == BaseUtils.ConditionLists.ErrorClass.LeftSideVarUndefined || errclass == BaseUtils.ConditionLists.ErrorClass.RightSideBadFormat)
                    {
                        ExtendedControls.MessageBoxTheme.Show(errlist, "Warning".T(EDTx.Warning), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    }
                }

                dataGridView.Sort(sortcol, (sortorder == SortOrder.Descending) ? ListSortDirection.Descending : ListSortDirection.Ascending);
                dataGridView.Columns[sortcol.Index].HeaderCell.SortGlyphDirection = sortorder;
                this.Cursor = Cursors.Default;
            }
        }
Example #4
0
        private void buttonFind_Click(object sender, EventArgs e)
        {
            BaseUtils.ConditionLists cond = Valid();
            if (cond != null)
            {
                this.Cursor = Cursors.WaitCursor;
                dataGridView.Rows.Clear();

                DataGridViewColumn sortcol   = dataGridView.SortedColumn != null ? dataGridView.SortedColumn : dataGridView.Columns[0];
                SortOrder          sortorder = dataGridView.SortedColumn != null ? dataGridView.SortOrder : SortOrder.Descending;

                ISystem cursystem = discoveryform.history.CurrentSystem;        // could be null

                foreach (var he in discoveryform.history.FilterByScan)
                {
                    JournalScan js = he.journalEntry as JournalScan;

                    BaseUtils.Variables scandata = new BaseUtils.Variables();
                    scandata.AddPropertiesFieldsOfClass(js, "", new Type[] { typeof(System.Drawing.Icon), typeof(System.Drawing.Image), typeof(System.Drawing.Bitmap), typeof(Newtonsoft.Json.Linq.JObject) }, 5);

                    bool?res = cond.CheckAll(scandata, out string errlist);   // need function handler..

                    if (res.HasValue && res.Value == true)
                    {
                        ISystem  sys    = he.System;
                        string   sep    = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator + " ";
                        object[] rowobj =
                        {
                            (EDDConfig.Instance.DisplayUTC ? he.EventTimeUTC : he.EventTimeLocal).ToString(),
                            js.BodyName,
                            js.DisplayString(0,                                                              true),
                            (cursystem != null ? cursystem.Distance(sys).ToString("0.#") : ""),
                            sys.X.ToString("0.#") + sep + sys.Y.ToString("0.#") + sep + sys.Z.ToString("0.#")
                        };

                        dataGridView.Rows.Add(rowobj);
                        dataGridView.Rows[dataGridView.Rows.Count - 1].Tag = sys;
                    }
                }

                dataGridView.Sort(sortcol, (sortorder == SortOrder.Descending) ? ListSortDirection.Descending : ListSortDirection.Ascending);
                dataGridView.Columns[sortcol.Index].HeaderCell.SortGlyphDirection = sortorder;
                this.Cursor = Cursors.Default;
            }
        }
Example #5
0
        public List <UIEvent> Scan()
        {
            //  System.Diagnostics.Debug.WriteLine(Environment.TickCount % 100000 + "Check " + statusfile);

            if (File.Exists(statusfile))
            {
                JObject jo = null;

                Stream stream = null;
                try
                {
                    stream = File.Open(statusfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    StreamReader reader = new StreamReader(stream);

                    string text = reader.ReadToEnd();

                    stream.Close();

                    if (text != null && (prev_text == null || !text.Equals(prev_text))) // if text not null, and prev text is null OR not equal
                    {
                        jo        = JObject.ParseThrowCommaEOL(text);                   // and of course the json could be crap
                        prev_text = text;                                               // set after successful parse
                    }
                }
                catch
                { }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }

                if (jo != null)
                {
                    DateTime EventTimeUTC = jo["timestamp"].DateTimeUTC();

                    List <UIEvent> events = new List <UIEvent>();

                    if (prev_flags == null)                               // if first run, set prev flags to impossible type.
                    {
                        prev_flags  = (long)StatusFlagsShipType.ShipMask; // set an impossible ship type to start the ball rolling
                        prev_flags2 = 0;
                    }

                    UIEvents.UIShipType.Shiptype shiptype = ShipType(prev_flags.Value, prev_flags2.Value);

                    long curflags  = jo["Flags"].Long();
                    long curflags2 = jo["Flags2"].Long(0);      // 0 is backwards compat with horizons

                    bool fireoverall        = false;
                    bool fireoverallrefresh = prev_guifocus == NotPresent; //meaning its a refresh

                    long flagsdelta2 = 0;                                  // what changed between prev and current

                    if (curflags != prev_flags.Value || curflags2 != prev_flags2.Value)
                    {
                        UIEvents.UIShipType.Shiptype nextshiptype = ShipType(curflags, curflags2);

                        //System.Diagnostics.Debug.WriteLine("UI Flags changed {0} {1} {2} -> {3} {4} {5}", prev_flags.Value, prev_flags2.Value, shiptype, curflags, curflags2, nextshiptype);

                        bool refresh = shiptype == UIEvents.UIShipType.Shiptype.None;   // refresh if prev ship was none..

                        if (shiptype != nextshiptype)
                        {
                            events.Add(new UIEvents.UIShipType(nextshiptype, EventTimeUTC, refresh));        // CHANGE of ship/on foot/taxi etc
                            //prev_flags = ~curflags;       // force re-reporting (don't think its ness)
                            //prev_flags2 = ~curflags2;
                            refresh = true;
                        }

                        if (nextshiptype != UIEvents.UIShipType.Shiptype.None)
                        {
                            events.AddRange(ReportFlagState(typeof(StatusFlags2), curflags2, prev_flags2.Value, EventTimeUTC, refresh));
                            events.AddRange(ReportFlagState(typeof(StatusFlagsShip), curflags, prev_flags.Value, EventTimeUTC, refresh));
                            events.AddRange(ReportFlagState(typeof(StatusFlagsSRV), curflags, prev_flags.Value, EventTimeUTC, refresh));
                            events.AddRange(ReportFlagState(typeof(StatusFlagsAll), curflags, prev_flags.Value, EventTimeUTC, refresh));
                        }

                        flagsdelta2 = curflags2 ^ prev_flags2.Value;        // record the delta here for later processing, some of those flags go into the main reports

                        prev_flags  = curflags;
                        prev_flags2 = curflags2;
                        shiptype    = nextshiptype;
                        fireoverall = true;
                    }

                    int curguifocus = jo["GuiFocus"].Int(NotPresent);
                    if (curguifocus != prev_guifocus)
                    {
                        events.Add(new UIEvents.UIGUIFocus(curguifocus, EventTimeUTC, prev_guifocus == NotPresent));
                        prev_guifocus = curguifocus;
                        fireoverall   = true;
                    }

                    int[] pips = jo["Pips"]?.ToObjectQ <int[]>();

                    if (pips != null)
                    {
                        double sys = pips[0] / 2.0;     // convert to normal, instead of half pips
                        double eng = pips[1] / 2.0;
                        double wep = pips[2] / 2.0;
                        if (sys != prev_pips.Systems || wep != prev_pips.Weapons || eng != prev_pips.Engines)
                        {
                            UIEvents.UIPips.Pips newpips = new UIEvents.UIPips.Pips()
                            {
                                Systems = sys, Engines = eng, Weapons = wep
                            };
                            events.Add(new UIEvents.UIPips(newpips, EventTimeUTC, prev_pips.Engines < 0));
                            prev_pips   = newpips;
                            fireoverall = true;
                        }
                    }
                    else if (prev_pips.Valid)       // missing pips, if we are valid.. need to clear them
                    {
                        UIEvents.UIPips.Pips newpips = new UIEvents.UIPips.Pips();
                        events.Add(new UIEvents.UIPips(newpips, EventTimeUTC, prev_pips.Engines < 0));
                        prev_pips   = newpips;
                        fireoverall = true;
                    }

                    int?curfiregroup = jo["FireGroup"].IntNull();       // may appear/disappear.

                    if (curfiregroup != null && curfiregroup != prev_firegroup)
                    {
                        events.Add(new UIEvents.UIFireGroup(curfiregroup.Value + 1, EventTimeUTC, prev_firegroup == NotPresent));
                        prev_firegroup = curfiregroup.Value;
                        fireoverall    = true;
                    }

                    JToken jfuel = jo["Fuel"];

                    if (jfuel != null && jfuel.IsObject)        // because they changed its type in 3.3.2
                    {
                        double?curfuel = jfuel["FuelMain"].DoubleNull();
                        double?curres  = jfuel["FuelReservoir"].DoubleNull();
                        if (curfuel != null && curres != null)
                        {
                            if (Math.Abs(curfuel.Value - prev_curfuel) >= 0.1 || Math.Abs(curres.Value - prev_curres) >= 0.01)  // don't fire if small changes
                            {
                                //System.Diagnostics.Debug.WriteLine("UIEvent Fuel " + curfuel.Value + " " + prev_curfuel + " Res " + curres.Value + " " + prev_curres);
                                events.Add(new UIEvents.UIFuel(curfuel.Value, curres.Value, shiptype, EventTimeUTC, prev_firegroup == NotPresent));
                                prev_curfuel = curfuel.Value;
                                prev_curres  = curres.Value;
                                fireoverall  = true;
                            }
                        }
                    }

                    int?curcargo = jo["Cargo"].IntNull();       // may appear/disappear and only introduced for 3.3
                    if (curcargo != null && curcargo.Value != prev_cargo)
                    {
                        events.Add(new UIEvents.UICargo(curcargo.Value, shiptype, EventTimeUTC, prev_firegroup == NotPresent));
                        prev_cargo  = curcargo.Value;
                        fireoverall = true;
                    }

                    double jlat     = jo["Latitude"].Double(UIEvents.UIPosition.InvalidValue);   // if not there, min value
                    double jlon     = jo["Longitude"].Double(UIEvents.UIPosition.InvalidValue);
                    double jalt     = jo["Altitude"].Double(UIEvents.UIPosition.InvalidValue);
                    double jheading = jo["Heading"].Double(UIEvents.UIPosition.InvalidValue);
                    double jpradius = jo["PlanetRadius"].Double(UIEvents.UIPosition.InvalidValue);       // 3.4

                    if (jlat != prev_pos.Latitude || jlon != prev_pos.Longitude || jalt != prev_pos.Altitude || jheading != prev_heading || jpradius != prev_jpradius)
                    {
                        UIEvents.UIPosition.Position newpos = new UIEvents.UIPosition.Position()
                        {
                            Latitude = jlat, Longitude = jlon,
                            Altitude = jalt, AltitudeFromAverageRadius = Flags(curflags, StatusFlagsReportedInOtherEvents.AltitudeFromAverageRadius)
                        };

                        events.Add(new UIEvents.UIPosition(newpos, jheading, jpradius, EventTimeUTC, prev_pos.ValidPosition == false));
                        prev_pos      = newpos;
                        prev_heading  = jheading;
                        prev_jpradius = jpradius;
                        fireoverall   = true;
                    }

                    string cur_legalstatus = jo["LegalState"].StrNull();

                    if (cur_legalstatus != prev_legalstatus)
                    {
                        events.Add(new UIEvents.UILegalStatus(cur_legalstatus, EventTimeUTC, prev_legalstatus == null));
                        prev_legalstatus = cur_legalstatus;
                        fireoverall      = true;
                    }

                    string cur_bodyname = jo["BodyName"].StrNull();

                    if (cur_bodyname != prev_bodyname)
                    {
                        events.Add(new UIEvents.UIBodyName(cur_bodyname, EventTimeUTC, prev_bodyname == null));
                        prev_bodyname = cur_bodyname;
                        fireoverall   = true;
                    }

                    string cur_weapon    = jo["SelectedWeapon"].StrNull();              // null if not there
                    string cur_weaponloc = jo["SelectedWeapon_Localised"].Str();        // empty if not there

                    if (cur_weapon != prev_selectedweapon)
                    {
                        events.Add(new UIEvents.UISelectedWeapon(cur_weapon, cur_weaponloc, EventTimeUTC, prev_selectedweapon == null));
                        prev_selectedweapon    = cur_weapon;
                        prev_selectedweaponloc = cur_weaponloc;
                        fireoverall            = true;
                    }

                    double oxygen = jo["Oxygen"].Double(NotPresent);                //-1 is not present
                    oxygen = oxygen < 0 ? oxygen : oxygen * 100;                    // correct to 0-100%
                    bool lowoxygen = Flags(curflags2, StatusFlags2OtherFlags.LowOxygen);

                    if (oxygen != prev_oxygen || Flags(flagsdelta2, StatusFlags2OtherFlags.LowOxygen))
                    {
                        events.Add(new UIEvents.UIOxygen(oxygen, lowoxygen, EventTimeUTC, prev_oxygen < 0));
                        prev_oxygen = oxygen;
                        fireoverall = true;
                    }

                    double health = jo["Health"].Double(NotPresent);                //-1 is not present
                    health = health < 0 ? health : health * 100;                    // correct to 0-100%
                    bool lowhealth = Flags(curflags2, StatusFlags2OtherFlags.LowHealth);

                    if (health != prev_health || Flags(flagsdelta2, StatusFlags2OtherFlags.LowHealth))
                    {
                        events.Add(new UIEvents.UIHealth(health, lowhealth, EventTimeUTC, prev_health < 0));
                        prev_health = health;
                        fireoverall = true;
                    }

                    double gravity = jo["Gravity"].Double(NotPresent);                //-1 is not present

                    if (gravity != prev_gravity)
                    {
                        events.Add(new UIEvents.UIGravity(gravity, EventTimeUTC, prev_gravity < 0));
                        prev_gravity = gravity;
                        fireoverall  = true;
                    }

                    double temperature = jo["Temperature"].Double(NotPresent);       //-1 is not present

                    UIEvents.UITemperature.TempState tempstate =
                        Flags(curflags2, StatusFlags2OtherFlags.VeryCold) ? UIEvents.UITemperature.TempState.VeryCold :       // order important, you can get Cold | VeryCold
                        Flags(curflags2, StatusFlags2OtherFlags.VeryHot) ? UIEvents.UITemperature.TempState.VeryHot :
                        Flags(curflags2, StatusFlags2OtherFlags.Cold) ? UIEvents.UITemperature.TempState.Cold :
                        Flags(curflags2, StatusFlags2OtherFlags.Hot) ? UIEvents.UITemperature.TempState.Hot :
                        UIEvents.UITemperature.TempState.Normal;

                    if (temperature != prev_temperature || (flagsdelta2 & (long)StatusFlags2OtherFlags.TempBits) != 0)
                    {
                        events.Add(new UIEvents.UITemperature(temperature, tempstate, EventTimeUTC, prev_temperature < 0));
                        prev_temperature = temperature;
                        fireoverall      = true;
                    }

                    if (fireoverall)
                    {
                        List <UITypeEnum> flagsset = ReportFlagState(typeof(StatusFlagsShip), curflags);
                        flagsset.AddRange(ReportFlagState(typeof(StatusFlagsSRV), curflags));
                        flagsset.AddRange(ReportFlagState(typeof(StatusFlagsAll), curflags));
                        flagsset.AddRange(ReportFlagState(typeof(StatusFlags2), curflags2));

                        bool glidemode       = Flags(curflags2, StatusFlags2.GlideMode);
                        bool breathableatmos = Flags(curflags2, StatusFlags2.BreathableAtmosphere);

                        UIEvents.UIOverallStatus.FSDStateType fsdstate = UIEvents.UIOverallStatus.FSDStateType.Normal;
                        if (Flags(curflags, StatusFlagsShip.FsdJump))
                        {
                            fsdstate = UIEvents.UIOverallStatus.FSDStateType.Jumping;
                        }
                        else if (Flags(curflags, StatusFlagsShip.FsdCharging))
                        {
                            fsdstate = UIEvents.UIOverallStatus.FSDStateType.Charging;
                        }
                        else if (Flags(curflags2, StatusFlags2.GlideMode))
                        {
                            fsdstate = UIEvents.UIOverallStatus.FSDStateType.Gliding;
                        }
                        else if (Flags(curflags, StatusFlagsShip.FsdCooldown))
                        {
                            fsdstate = UIEvents.UIOverallStatus.FSDStateType.Cooldown;
                        }

                        events.Add(new UIEvents.UIOverallStatus(shiptype, flagsset, prev_guifocus, prev_pips, prev_firegroup,
                                                                prev_curfuel, prev_curres, prev_cargo, prev_pos, prev_heading, prev_jpradius, prev_legalstatus,
                                                                prev_bodyname,
                                                                prev_health, lowhealth, gravity, prev_temperature, tempstate, prev_oxygen, lowoxygen,
                                                                prev_selectedweapon, prev_selectedweaponloc,
                                                                fsdstate, breathableatmos,
                                                                EventTimeUTC, fireoverallrefresh));        // overall list of flags set
                    }

                    //for debugging, keep
#if false
                    foreach (var uient in events)
                    {
                        BaseUtils.Variables v = new BaseUtils.Variables();
                        v.AddPropertiesFieldsOfClass(uient, "", null, 2);
                        System.Diagnostics.Trace.WriteLine(string.Format("New UI entry from journal {0} {1}", uient.EventTimeUTC, uient.EventTypeStr));
                        foreach (var x in v.NameEnumuerable)
                        {
                            System.Diagnostics.Trace.WriteLine(string.Format("  {0} = {1}", x, v[x]));
                        }
                    }
#endif

                    return(events);
                }
            }

            return(new List <UIEvent>());
        }