Exemplo n.º 1
0
 public static void RecoverStarSystemDB()
 {
     lock (editLock)
     {
         File.Delete(Constants.DATA_DIR + @"\EDDI.sqlite");
         CreateDatabase();
     }
     var updateLogs = Task.Run(() => DataProviderService.syncFromStarMapService(true));
 }
Exemplo n.º 2
0
 public static void RecoverStarSystemDB()
 {
     using (var con = SimpleDbConnection())
     {
         try
         {
             con.Close();
             SQLiteConnection.ClearAllPools();
             File.Delete(Constants.DATA_DIR + @"\EDDI.sqlite");
         }
         catch (SQLiteException ex)
         {
             handleSqlLiteException(con, ex);
         }
     }
     CreateDatabase();
     var updateLogs = Task.Run(() => DataProviderService.syncFromStarMapService());
 }
Exemplo n.º 3
0
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            List <StarSystem> results = new List <StarSystem>();

            if (!File.Exists(DbFile))
            {
                return(results);
            }
            if (!names.Any())
            {
                return(results);
            }

            List <DatabaseStarSystem> systemsToUpdate = new List <DatabaseStarSystem>();
            List <DatabaseStarSystem> dataSets        = Instance.ReadStarSystems(names);

            bool needToUpdate = false;

            for (int i = 0; i < dataSets.Count; i++)
            {
                DatabaseStarSystem dbStarSystem = dataSets[i];
                if (dbStarSystem.systemJson != null)
                {
                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    dbStarSystem.systemJson = dbStarSystem.systemJson?.Replace(@"""No volcanism""", "null");

                    // Old versions of the data could have a string "InterstellarFactorsContact" for the facilitator station service.  If so we update it
                    dbStarSystem.systemJson = dbStarSystem.systemJson?.Replace(@"""InterstellarFactorsContact""", @"""Facilitator""");

                    if (refreshIfOutdated)
                    {
                        if (dbStarSystem.lastUpdated < DateTime.UtcNow.AddHours(-1))
                        {
                            // Data is stale or we have no record of ever updating this star system
                            needToUpdate = true;
                        }
                        else if (SCHEMA_VERSION >= 2 && (dbStarSystem.systemAddress is null || dbStarSystem.edsmId is null))
                        {
                            // Obtain data for optimized data searches starting with schema version 2
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        // We want to update this star system (don't deserialize the old result at this time)
                        systemsToUpdate.Add(dbStarSystem);
                    }
                    else
                    {
                        // Deserialize the old result
                        StarSystem result = DeserializeStarSystem(dbStarSystem.systemName, dbStarSystem.systemJson, ref needToUpdate);
                        if (result != null)
                        {
                            results.Add(result);
                        }
                        else
                        {
                            // Something went wrong... retrieve new data.
                            systemsToUpdate.Add(dbStarSystem);
                        }
                    }
                }
            }

            if (systemsToUpdate.Count > 0)
            {
                List <StarSystem> updatedSystems = dataProviderService.GetSystemsData(systemsToUpdate.Select(s => s.systemName).ToArray());
                if (updatedSystems == null)
                {
                    return(results);
                }

                // If the newly fetched star system is an empty object except (for the object name), reject it
                // Return old results when new results have been rejected
                List <string> systemsToRevert = new List <string>();
                foreach (StarSystem starSystem in updatedSystems)
                {
                    if (starSystem.systemAddress == null)
                    {
                        systemsToRevert.Add(starSystem.systemname);
                    }
                }
                updatedSystems.RemoveAll(s => systemsToRevert.Contains(s.systemname));
                foreach (string systemName in systemsToRevert)
                {
                    results.Add(GetStarSystem(systemName, false));
                }

                // Synchronize EDSM visits and comments
                updatedSystems = dataProviderService.syncFromStarMapService(updatedSystems);

                // Update properties that aren't synced from the server and that we want to preserve
                updatedSystems = PreserveUnsyncedProperties(updatedSystems, systemsToUpdate);

                // Update the `lastupdated` timestamps for the systems we have updated
                foreach (StarSystem starSystem in updatedSystems)
                {
                    starSystem.lastupdated = DateTime.UtcNow;
                }

                // Add our updated systems to our results
                results.AddRange(updatedSystems);

                // Save changes to our star systems
                Instance.updateStarSystems(updatedSystems);
            }
            return(results);
        }
Exemplo n.º 4
0
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }
            if (!names.Any())
            {
                return(null);
            }

            List <StarSystem> results = new List <StarSystem>();
            List <KeyValuePair <string, object> > systemsToUpdate = new List <KeyValuePair <string, object> >();
            List <KeyValuePair <string, string> > dataSets        = Instance.ReadStarSystems(names);

            bool needToUpdate = false;

            foreach (KeyValuePair <string, string> kv in dataSets)
            {
                if (!string.IsNullOrEmpty(kv.Value))
                {
                    string name = kv.Key;

                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    string data = kv.Value?.Replace(@"""No volcanism""", "null");

                    // Old versions of the data could have a string "InterstellarFactorsContact" for the facilitator station service.  If so we update it
                    data = kv.Value?.Replace(@"""InterstellarFactorsContact""", @"""Facilitator""");

                    // Determine whether our data is stale (We won't deserialize the the entire system if it's stale)
                    IDictionary <string, object> system = Deserializtion.DeserializeData(data);
                    system.TryGetValue("lastupdated", out object lastUpdatedVal);
                    system.TryGetValue("systemAddress", out object systemAddressVal);
                    system.TryGetValue("EDSMID", out object edsmIdVal);

                    DateTime?lastupdated   = (DateTime?)lastUpdatedVal;
                    long?    systemAddress = (long?)systemAddressVal;
                    long?    edsmId        = (long?)edsmIdVal;

                    if (refreshIfOutdated)
                    {
                        if (lastupdated < DateTime.UtcNow.AddHours(-1))
                        {
                            // Data is stale
                            needToUpdate = true;
                        }
                        else if (lastupdated is null)
                        {
                            // We have no record of ever updating this star system
                            needToUpdate = true;
                        }
                        else if (SCHEMA_VERSION >= 2 && (systemAddress is null || edsmId is null))
                        {
                            // Obtain data for optimized data searches starting with schema version 2
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        // We want to update this star system (don't deserialize the old result at this time)
                        systemsToUpdate.Add(new KeyValuePair <string, object>(name, system));
                    }
                    else
                    {
                        // Deserialize the old result
                        StarSystem result = DeserializeStarSystem(name, data, ref needToUpdate);
                        if (result != null)
                        {
                            results.Add(result);
                        }
                    }
                }
            }

            if (systemsToUpdate.Count > 0)
            {
                List <StarSystem> updatedSystems = dataProviderService.GetSystemsData(systemsToUpdate.Select(s => s.Key).ToArray());

                // If the newly fetched star system is an empty object except (for the object name), reject it
                // Return old results when new results have been rejected
                List <string> systemsToRevert = new List <string>();
                foreach (StarSystem starSystem in updatedSystems)
                {
                    if (starSystem.systemAddress == null)
                    {
                        systemsToRevert.Add(starSystem.systemname);
                    }
                }
                updatedSystems.RemoveAll(s => systemsToRevert.Contains(s.systemname));
                foreach (string systemName in systemsToRevert)
                {
                    results.Add(GetStarSystem(systemName, false));
                }

                // Synchronize EDSM visits and comments
                updatedSystems = dataProviderService.syncFromStarMapService(updatedSystems);

                // Update properties that aren't synced from the server and that we want to preserve
                foreach (StarSystem updatedSystem in updatedSystems)
                {
                    foreach (KeyValuePair <string, object> systemToUpdate in systemsToUpdate)
                    {
                        if (updatedSystem.systemname == systemToUpdate.Key)
                        {
                            Dictionary <string, object> oldStarSystem = (Dictionary <string, object>)systemToUpdate.Value;

                            if (oldStarSystem != null)
                            {
                                // Carry over StarSystem properties that we want to preserve
                                updatedSystem.totalbodies = JsonParsing.getOptionalInt(oldStarSystem, "discoverableBodies") ?? 0;

                                // Carry over Body properties that we want to preserve (e.g. exploration data)
                                oldStarSystem.TryGetValue("bodies", out object bodiesVal);
                                List <Body> oldBodies = JsonConvert.DeserializeObject <List <Body> >(JsonConvert.SerializeObject(bodiesVal));
                                updatedSystem.PreserveBodyData(oldBodies, updatedSystem.bodies);

                                // Carry over Faction properties that we want to preserve (e.g. reputation data)
                                oldStarSystem.TryGetValue("factions", out object factionsVal);
                                if (factionsVal != null)
                                {
                                    List <Faction> oldFactions = JsonConvert.DeserializeObject <List <Faction> >(JsonConvert.SerializeObject(factionsVal));
                                    if (oldFactions?.Count > 0)
                                    {
                                        foreach (var updatedFaction in updatedSystem.factions)
                                        {
                                            foreach (var oldFaction in oldFactions)
                                            {
                                                if (updatedFaction.name == oldFaction.name)
                                                {
                                                    updatedFaction.myreputation = oldFaction.myreputation;
                                                }
                                            }
                                        }
                                    }
                                }

                                // No station data needs to be carried over at this time.
                            }
                        }
                    }
                }

                // Update the `lastupdated` timestamps for the systems we have updated
                foreach (StarSystem starSystem in updatedSystems)
                {
                    starSystem.lastupdated = DateTime.UtcNow;
                }

                // Add our updated systems to our results
                results.AddRange(updatedSystems);

                // Save changes to our star systems
                Instance.updateStarSystems(updatedSystems);
            }
            return(results);
        }
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }
            if (names.Count() == 0)
            {
                return(null);
            }

            List <StarSystem> results         = new List <StarSystem>();
            List <string>     systemsToUpdate = new List <string>();
            List <KeyValuePair <string, string> > dataSets = Instance.ReadStarSystems(names);

            foreach (KeyValuePair <string, string> kv in dataSets)
            {
                bool       needToUpdate = false;
                StarSystem result       = null;

                if (kv.Value != null && kv.Value != "")
                {
                    string name = kv.Key;

                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    string data = kv.Value?.Replace(@"""No volcanism""", "null");

                    // Determine whether our data is stale (We won't deserialize the the entire system if it's stale)
                    IDictionary <string, object> system = Deserializtion.DeserializeData(data);
                    system.TryGetValue("comment", out object commentVal);
                    system.TryGetValue("lastupdated", out object lastUpdatedVal);
                    system.TryGetValue("systemAddress", out object systemAddressVal);
                    system.TryGetValue("EDSMID", out object edsmIdVal);

                    string   comment       = (string)commentVal ?? "";
                    DateTime?lastupdated   = (DateTime?)lastUpdatedVal;
                    long?    systemAddress = (long?)systemAddressVal;
                    long?    edsmId        = (long?)edsmIdVal;

                    if (refreshIfOutdated)
                    {
                        if (lastupdated < DateTime.UtcNow.AddHours(-1))
                        {
                            // Data is stale
                            needToUpdate = true;
                        }
                        else if (lastupdated is null)
                        {
                            // We have no record of ever updating this star system
                            needToUpdate = true;
                        }
                        else if (SCHEMA_VERSION >= 2 && (systemAddress is null || edsmId is null))
                        {
                            // Obtain data for optimized data searches starting with schema version 2
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        // We want to update this star system
                        systemsToUpdate.Add(name);
                    }
                    else
                    {
                        // Deserialize the old result
                        result = DeserializeStarSystem(name, data, ref needToUpdate);
                        if (result != null)
                        {
                            results.Add(result);
                        }
                    }
                }
            }

            if (systemsToUpdate.Count > 0)
            {
                List <StarSystem> updatedSystems = DataProviderService.GetSystemsData(systemsToUpdate.ToArray());

                // If the newly fetched star system is an empty object except (for the object name), reject it
                List <string> systemsToRevert = new List <string>();
                foreach (StarSystem starSystem in updatedSystems)
                {
                    if (starSystem.systemAddress == null)
                    {
                        systemsToRevert.Add(starSystem.systemname);
                    }
                }
                updatedSystems.RemoveAll(s => systemsToRevert.Contains(s.systemname));

                // Return old results when new results have been rejected
                foreach (string systemName in systemsToRevert)
                {
                    results.Add(GetStarSystem(systemName, false));
                }

                // Synchronize EDSM visits and comments
                updatedSystems = DataProviderService.syncFromStarMapService(updatedSystems);

                // Add our updated systems to our results
                results.AddRange(updatedSystems);

                // Save changes to our star systems
                Instance.updateStarSystems(updatedSystems);
            }

            foreach (StarSystem starSystem in results)
            {
                starSystem.lastupdated = DateTime.UtcNow;
            }
            return(results);
        }