コード例 #1
0
        public override bool ExecuteAction(ActionProgramRun ap)
        {
            string res;

            if (ap.Functions.ExpandString(UserData, out res) != BaseUtils.Functions.ExpandResult.Failed)
            {
                StringParser sp = new StringParser(res);

                string prefix  = "B_";
                string cmdname = sp.NextWord();

                if (cmdname != null && cmdname.Equals("PREFIX", StringComparison.InvariantCultureIgnoreCase))
                {
                    prefix = sp.NextWord();

                    if (prefix == null)
                    {
                        ap.ReportError("Missing name after Prefix in Bookmarks");
                        return(true);
                    }

                    cmdname = sp.NextWord();
                }

                if (cmdname != null)
                {
                    if (cmdname.Equals("LIST", StringComparison.InvariantCultureIgnoreCase))
                    {
                        string wildcard = sp.NextQuotedWord() ?? "*";

                        int bcount = 1;
                        foreach (BookmarkClass b in GlobalBookMarkList.Instance.Bookmarks)
                        {
                            if (b.Name.WildCardMatch(wildcard, true))
                            {
                                string nprefix = prefix + bcount++.ToStringInvariant() + "_";

                                ap[nprefix + "isstar"] = b.isStar.ToStringIntValue();
                                ap[nprefix + "name"]   = b.Name;
                                ap[nprefix + "x"]      = b.x.ToStringInvariant();
                                ap[nprefix + "y"]      = b.y.ToStringInvariant();
                                ap[nprefix + "z"]      = b.z.ToStringInvariant();
                                ap[nprefix + "time"]   = b.TimeUTC.ToStringUS(); // US Date format
                                ap[nprefix + "note"]   = b.Note;

                                if (b.PlanetaryMarks != null)
                                {
                                    string pprefix = nprefix + "Planet_";
                                    ap[pprefix + "Count"] = b.PlanetaryMarks.Planets.Count().ToStringInvariant();

                                    int pcount = 1;
                                    foreach (PlanetMarks.Planet p in b.PlanetaryMarks.Planets)
                                    {
                                        string plname = pprefix + pcount++.ToStringInvariant() + "_";
                                        ap[plname + "name"]  = p.Name;
                                        ap[plname + "Count"] = p.Locations.Count.ToStringInvariant();

                                        int lcount = 1;
                                        foreach (PlanetMarks.Location l in p.Locations)
                                        {
                                            string locname = plname + lcount++.ToStringInvariant() + "_";
                                            ap[locname + "name"]      = l.Name;
                                            ap[locname + "comment"]   = l.Comment;
                                            ap[locname + "latitude"]  = l.Latitude.ToStringInvariant("0.#");
                                            ap[locname + "longitude"] = l.Longitude.ToStringInvariant("0.#");
                                        }
                                    }
                                }
                            }
                        }

                        ap[prefix + "MatchCount"] = (bcount - 1).ToStringInvariant();
                        ap[prefix + "TotalCount"] = GlobalBookMarkList.Instance.Bookmarks.Count.ToStringInvariant();
                    }
                    else
                    {
                        string name   = sp.NextQuotedWord();
                        bool   region = name.Equals("REGION", StringComparison.InvariantCultureIgnoreCase);
                        if (region)
                        {
                            name = sp.NextQuotedWord();
                        }

                        if (name == null)
                        {
                            ap.ReportError("Missing name in command");
                        }
                        else if (cmdname.Equals("EXIST", StringComparison.InvariantCultureIgnoreCase))
                        {
                            BookmarkClass bk = GlobalBookMarkList.Instance.FindBookmark(name, region);
                            ap[prefix + "Exists"] = (bk != null).ToStringIntValue();
                        }
                        else if (cmdname.Equals("ADD", StringComparison.InvariantCultureIgnoreCase))
                        {
                            double?x     = sp.NextDouble();
                            double?y     = sp.NextDouble();
                            double?z     = sp.NextDouble();
                            string notes = sp.NextQuotedWord(); // valid for it to be null.  Means don't update notes

                            if (x != null && y != null && z != null)
                            {
                                BookmarkClass bk = GlobalBookMarkList.Instance.FindBookmark(name, region);
                                GlobalBookMarkList.Instance.AddOrUpdateBookmark(bk, !region, name, x.Value, y.Value, z.Value, DateTime.Now, notes);
                            }
                            else
                            {
                                ap.ReportError("Missing parameters in Add");
                            }
                        }
                        else if (cmdname.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase))
                        {
                            BookmarkClass bk = GlobalBookMarkList.Instance.FindBookmark(name, region);
                            if (bk != null)
                            {
                                GlobalBookMarkList.Instance.Delete(bk);
                            }
                            else
                            {
                                ap.ReportError("Delete cannot find star or region " + name);
                            }
                        }
                        else if (cmdname.Equals("UPDATENOTE", StringComparison.InvariantCultureIgnoreCase))
                        {
                            string notes = sp.NextQuotedWord();

                            if (notes != null)
                            {
                                BookmarkClass bk = GlobalBookMarkList.Instance.FindBookmark(name, region);
                                if (bk != null)
                                {
                                    bk.UpdateNotes(notes);
                                    GlobalBookMarkList.Instance.TriggerChange(bk);
                                }
                                else
                                {
                                    ap.ReportError("UpdateNote cannot find star or region " + name);
                                }
                            }
                            else
                            {
                                ap.ReportError("UpdateNote notes not present");
                            }
                        }
                        else
                        {
                            bool addstar            = cmdname.Equals("ADDSTAR", StringComparison.InvariantCultureIgnoreCase);
                            bool addplanet          = cmdname.Equals("ADDPLANET", StringComparison.InvariantCultureIgnoreCase);
                            bool deleteplanet       = cmdname.Equals("DELETEPLANET", StringComparison.InvariantCultureIgnoreCase);
                            bool updatenoteonplanet = cmdname.Equals("UPDATEPLANETNOTE", StringComparison.InvariantCultureIgnoreCase);
                            bool planetmarkexists   = cmdname.Equals("PLANETMARKEXISTS", StringComparison.InvariantCultureIgnoreCase);

                            if (!addstar && !addplanet && !deleteplanet && !updatenoteonplanet && !planetmarkexists)
                            {
                                ap.ReportError("Unknown command");
                            }
                            else if (region)
                            {
                                ap.ReportError("Command and REGION are incompatible");
                            }
                            else if (addstar)
                            {
                                var     df  = (ap.ActionController as ActionController).DiscoveryForm;
                                ISystem sys = df.history.FindSystem(name, df.galacticMapping, true);

                                if (sys != null)
                                {
                                    string        notes = sp.NextQuotedWord(); // valid for it to be null, means don't override or set to empty
                                    BookmarkClass bk    = GlobalBookMarkList.Instance.FindBookmarkOnSystem(name);
                                    GlobalBookMarkList.Instance.AddOrUpdateBookmark(bk, true, name, sys.X, sys.Y, sys.Z, DateTime.UtcNow, notes);
                                }
                                else
                                {
                                    ap.ReportError("AddStar cannot find star " + name + " in database");
                                }
                            }
                            else
                            {
                                BookmarkClass bk = GlobalBookMarkList.Instance.FindBookmarkOnSystem(name);

                                if (bk != null)
                                {
                                    string planet    = sp.NextQuotedWord();
                                    string placename = sp.NextQuotedWord();

                                    if (planet != null && placename != null)
                                    {
                                        if (addplanet)
                                        {
                                            double?latp    = sp.NextDouble();
                                            double?longp   = sp.NextDouble();
                                            string comment = sp.NextQuotedWord();       // can be null

                                            if (planet != null && latp != null && longp != null)
                                            {
                                                bk.AddOrUpdateLocation(planet, placename, comment ?? "", latp.Value, longp.Value);
                                                GlobalBookMarkList.Instance.TriggerChange(bk);
                                            }
                                            else
                                            {
                                                ap.ReportError("AddPlanet missing parameters");
                                            }
                                        }
                                        else if (updatenoteonplanet)
                                        {
                                            string comment = sp.NextQuotedWord();
                                            if (comment != null)
                                            {
                                                if (bk.UpdateLocationComment(planet, placename, comment))
                                                {
                                                    GlobalBookMarkList.Instance.TriggerChange(bk);
                                                }
                                                else
                                                {
                                                    ap.ReportError("UpdatePlanetNote no such placename");
                                                }
                                            }
                                            else
                                            {
                                                ap.ReportError("UpdatePlanetNote no comment");
                                            }
                                        }
                                        else if (deleteplanet)
                                        {
                                            if (bk.DeleteLocation(planet, placename))
                                            {
                                                GlobalBookMarkList.Instance.TriggerChange(bk);
                                            }
                                            else
                                            {
                                                ap.ReportError("DeletePlanet no such placename");
                                            }
                                        }
                                        else if (planetmarkexists)
                                        {
                                            ap[prefix + "Exists"] = bk.HasLocation(planet, placename).ToStringIntValue();
                                        }
                                    }
                                    else
                                    {
                                        ap.ReportError("Missing planet and/or placename");
                                    }
                                }
                                else
                                {
                                    ap.ReportError("Cannot find bookmark for " + name);
                                }
                            }
                        }
                    }
                }
                else
                {
                    ap.ReportError("Missing Bookmarks command");
                }
            }
            else
            {
                ap.ReportError(res);
            }

            return(true);
        }
コード例 #2
0
        private void buttonExtImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.InitialDirectory = SQLiteConnectionUser.GetSettingString("BookmarkFormImportExcelFolder", "c:\\");

            if (!System.IO.Directory.Exists(dlg.InitialDirectory))
            {
                System.IO.Directory.CreateDirectory(dlg.InitialDirectory);
            }

            dlg.DefaultExt   = "csv";
            dlg.AddExtension = true;
            dlg.Filter       = "CVS Files (*.csv)|*.csv|All files (*.*)|*.*";

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                string path = dlg.FileName;

                BaseUtils.CSVFile csv = new BaseUtils.CSVFile();

                if (csv.Read(path, System.IO.FileShare.ReadWrite))
                {
                    List <BaseUtils.CSVFile.Row> rows = csv.RowsExcludingHeaderRow;

                    BookmarkClass currentbk = null;

                    var Regexyyyyddmm = new System.Text.RegularExpressions.Regex(@"\d\d\d\d-\d\d-\d\d", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.Singleline);

                    foreach (var r in rows)
                    {
                        string type = r[0];
                        string date = r[1];

                        if (type.HasChars() && date.HasChars())
                        {
                            bool region = type?.Equals("Region", StringComparison.InvariantCultureIgnoreCase) ?? false;

                            DateTime time = DateTime.MinValue;

                            bool isyyyy = Regexyyyyddmm.IsMatch(date);      // excel, after getting our output in, converts the damn thing to local dates.. this distinguishes it.

                            bool success = isyyyy ? DateTime.TryParse(date, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out time) :
                                           DateTime.TryParse(date, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AssumeLocal, out time);

                            if (success)
                            {
                                string name = r[2];
                                string note = r[3];
                                double?x    = r[4].InvariantParseDoubleNull();
                                double?y    = r[5].InvariantParseDoubleNull();
                                double?z    = r[6].InvariantParseDoubleNull();

                                if (x != null && y != null && z != null)
                                {
                                    System.Diagnostics.Debug.WriteLine("Bookmark {0} {1} {2} {3} ({4},{5},{6}", type, time.ToStringZulu(), name, note, x, y, z);

                                    currentbk = GlobalBookMarkList.Instance.FindBookmark(name, region);

                                    if (currentbk != null)
                                    {
                                        GlobalBookMarkList.Instance.AddOrUpdateBookmark(currentbk, !region, name, x.Value, y.Value, z.Value, time, note, currentbk.PlanetaryMarks);
                                    }
                                    else
                                    {
                                        currentbk = GlobalBookMarkList.Instance.AddOrUpdateBookmark(null, !region, name, x.Value, y.Value, z.Value, time, note, null);
                                    }
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Not a system with valid coords {0} {1}", r[0], r[1]);
                                }
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("Rejected due to date {0} {1}", r[0], r[1]);
                            }
                        }

                        string planet = r[7];

                        if (planet.HasChars() && currentbk != null)
                        {
                            string locname   = r[8];
                            string comment   = r[9];
                            double?latitude  = r[10].InvariantParseDoubleNull();
                            double?longitude = r[11].InvariantParseDoubleNull();

                            if (!locname.HasChars() && latitude == null && longitude == null) // whole planet bookmark
                            {
                                currentbk.AddOrUpdatePlanetBookmark(planet, comment);
                            }
                            else if (locname.HasChars() && latitude.HasValue && longitude.HasValue)
                            {
                                currentbk.AddOrUpdateLocation(planet, locname, comment, latitude.Value, longitude.Value);
                            }
                        }
                    }

                    SQLiteConnectionUser.PutSettingString("BookmarkFormImportExcelFolder", System.IO.Path.GetDirectoryName(path));
                }
                else
                {
                    ExtendedControls.MessageBoxTheme.Show(FindForm(), "Failed to read " + path, "Import Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }