Пример #1
0
        /// <summary>
        /// Load the yaml files.
        /// </summary>
        /// <param name="worker">The worker thread that is calling this function.</param>
        /// <param name="start">The starting percentage of the work.</param>
        /// <param name="portion">The portion of the progress that is for this activity.</param>
        /// <returns>The error message or null on success.</returns>
        public static string LoadYAML(BackgroundWorker worker, int start, int portion)
        {
            bool   solarSystemsCached   = false;
            string solarSystemCacheFile = UserData.cachePath + Path.GetFileName(UserData.sdeZip) + ".SolarSystems.Cache";

            if (File.Exists(solarSystemCacheFile))
            {
                if (worker != null && !worker.CancellationPending)
                {
                    // Updatee the worker status.
                    worker.ReportProgress(start, "Loading cached system files...");
                }
                try
                {
                    using (FileStream file = new FileStream(solarSystemCacheFile, FileMode.Open))
                    {
                        using (BinaryReader load = new BinaryReader(file))
                        {
                            bool success = SolarSystem.LoadAll(load);
                            success &= Stargate.LoadAll(load);
                            success &= OrbitalBody.LoadAll(load);
                            success &= NPCStation.LoadAll(load);
                            if (success)
                            {
                                // Successfully loaded cache.
                                solarSystemsCached = true;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // An error occured, we should delete the file it appears corrupted.
                    File.Delete(solarSystemCacheFile);
                }
            }
            baseComplete = false;
            List <string> solarSystemFiles = new List <string>();

            if (File.Exists(UserData.sdeZip))
            {
                FileStream fileStream = File.OpenRead(UserData.sdeZip);
                ZipArchive zip        = new ZipArchive(fileStream);
                foreach (ZipArchiveEntry zipFile in zip.Entries)
                {
                    string fName    = zipFile.FullName;
                    bool   isSystem = fName.EndsWith("solarsystem.staticdata") && !solarSystemsCached;
                    if (fName.EndsWith(".yaml") || isSystem)
                    {
                        MemoryStream memStream = new MemoryStream();
                        Stream       zStream   = zipFile.Open();
                        zStream.CopyTo(memStream);
                        memStream.Position         = 0;
                        zipFiles[zipFile.FullName] = memStream;
                    }
                    if (isSystem)
                    {
                        if (fName.StartsWith("sde"))
                        {
                            fName = fName.Remove(0, 3);
                        }
                        solarSystemFiles.Add(fName);
                    }
                }
            }
            // Get the yaml files.
            List <LoadYamlItem> yamlFiles = GetYamlFiles();
            int baseFiles = yamlFiles.Count;

            worker.ReportProgress(start, "Searching solar system files...");
            int cnt   = 0;
            int total = yamlFiles.Count + solarSystemFiles.Count;

            while (cnt < total)
            {
                while (yamlFiles.Count < 40 && solarSystemFiles.Count > 0 && cnt >= baseFiles)
                {
                    // Start loading some files.
                    string solarSystemFile = solarSystemFiles[0];
                    yamlFiles.Add(new LoadYamlItem(solarSystemFile, SolarSystem.LoadYAML));
                    solarSystemFiles.Remove(solarSystemFile);
                }
                // We still have files to parse.
                foreach (LoadYamlItem item in yamlFiles)
                {
                    if (item.complete == false)
                    {
                        if (worker != null)
                        {
                            // We are using a worker, check for cancel.
                            if (worker.CancellationPending)
                            {
                                return("Cancled by user request.");
                            }
                        }
                        if (item.err != null)
                        {
                            // There was an error.
                            return(item.err);
                        }
                        // This file still not loaded.
                        continue;
                    }
                    // Increment out count and remove the item.
                    cnt++;
                    if (cnt >= baseFiles)
                    {
                        baseComplete = true;
                    }
                    yamlFiles.Remove(item);
                    if (worker != null && !worker.CancellationPending && ((cnt % 10) == 0 || cnt < 20))
                    {
                        // Updatee the worker status.
                        int pct = (cnt * portion) / total;
                        worker.ReportProgress(start + pct, "Loading YAML files... (" + cnt + " of " + total + " complete)");
                    }
                    break;
                }
            }
            zipFiles.Clear();
            // Save cache
            if (!solarSystemsCached)
            {
                using (FileStream mem = new FileStream(solarSystemCacheFile, FileMode.Create))
                {
                    using (BinaryWriter save = new BinaryWriter(mem))
                    {
                        SolarSystem.SaveAll(save);
                        Stargate.SaveAll(save);
                        OrbitalBody.SaveAll(save);
                        NPCStation.SaveAll(save);
                        //byte[] ary = mem.ToArray();
                    }
                }
            }
            return(null);
        }