Пример #1
0
        public override void OnLoad(ConfigNode config)
        {
            var oldPath = KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/kethane.cfg";
            var oldConfig = ConfigNode.Load(oldPath);
            if (oldConfig != null)
            {
                config = oldConfig;
                System.IO.File.Delete(oldPath);
            }

            if (!config.HasValue("Version") && (config.CountNodes > 0 || config.CountValues > 2))
            {
                try
                {
                    config = upgradeConfig(config);
                }
                catch (Exception e)
                {
                    Debug.LogError("[Kethane] Error upgrading legacy data: " + e);
                    config = new ConfigNode();
                }
            }

            var timer = System.Diagnostics.Stopwatch.StartNew();

            PlanetDeposits.Clear();
            Scans.Clear();

            var resourceNodes = config.GetNodes("Resource");

            foreach (var resource in KethaneController.ResourceDefinitions)
            {
                var resourceName = resource.Resource;
                var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode();

                PlanetDeposits[resourceName] = new Dictionary<string, IBodyResources>();
                Scans[resourceName] = new Dictionary<string, Cell.Set>();

                generatorNodes[resourceName] = resourceNode.GetNode("Generator") ?? resource.Generator;
                var generator = createGenerator(generatorNodes[resourceName].CreateCopy());
                if (generator == null)
                {
                    Debug.LogWarning("[Kethane] Defaulting to empty generator for " + resourceName);
                    generator = new EmptyResourceGenerator();
                }
                generators[resourceName] = generator;

                var bodyNodes = resourceNode.GetNodes("Body");

                foreach (var body in FlightGlobals.Bodies)
                {
                    var bodyNode = bodyNodes.SingleOrDefault(n => n.GetValue("Name") == body.name) ?? new ConfigNode();

                    PlanetDeposits[resourceName][body.name] = generator.Load(body, bodyNode.GetNode("GeneratorData"));
                    Scans[resourceName][body.name] = new Cell.Set(MapOverlay.GridLevel);

                    var scanMask = bodyNode.GetValue("ScanMask");
                    if (scanMask != null)
                    {
                        try
                        {
                            Scans[resourceName][body.name] = new Cell.Set(MapOverlay.GridLevel, Convert.FromBase64String(scanMask.Replace('.', '/').Replace('%', '=')));
                        }
                        catch (FormatException e)
                        {
                            Debug.LogError(String.Format("[Kethane] Failed to parse {0}/{1} scan string, resetting ({2})", body.name, resourceName, e.Message));
                        }
                    }
                }
            }

            if (!config.HasValue("Version") || config.GetValue("Version") == "0.8")
            {
                var str = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Kethane.Resources.GridIndices.txt")).ReadToEnd();
                var map = str.Split(',').Select(s => new Cell(uint.Parse(s))).ToArray();

                foreach (var resource in KethaneController.ResourceDefinitions)
                {
                    foreach (var body in FlightGlobals.Bodies)
                    {
                        var old = Scans[resource.Resource][body.name];
                        var set = new Cell.Set(MapOverlay.GridLevel);

                        foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel))
                        {
                            set[cell] = old[map[cell.Index]];
                        }

                        Scans[resource.Resource][body.name] = set;
                    }
                }
            }

            timer.Stop();
            Debug.LogWarning(String.Format("Kethane deposits loaded ({0}ms)", timer.ElapsedMilliseconds));
        }
Пример #2
0
        public override void OnLoad(ConfigNode config)
        {
            var oldPath   = KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/kethane.cfg";
            var oldConfig = ConfigNode.Load(oldPath);

            if (oldConfig != null)
            {
                config = oldConfig;
                System.IO.File.Delete(oldPath);
            }

            if (!config.HasValue("Version") && (config.CountNodes > 0 || config.CountValues > 2))
            {
                try
                {
                    config = upgradeConfig(config);
                }
                catch (Exception e)
                {
                    Debug.LogError("[Kethane] Error upgrading legacy data: " + e);
                    config = new ConfigNode();
                }
            }

            var timer = System.Diagnostics.Stopwatch.StartNew();

            PlanetDeposits.Clear();
            Scans.Clear();

            var resourceNodes = config.GetNodes("Resource");

            foreach (var resource in KethaneController.ResourceDefinitions)
            {
                var resourceName = resource.Resource;
                var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode();

                PlanetDeposits[resourceName] = new Dictionary <string, IBodyResources>();
                Scans[resourceName]          = new Dictionary <string, Cell.Set>();

                generatorNodes[resourceName] = resourceNode.GetNode("Generator") ?? resource.Generator;
                var generator = createGenerator(generatorNodes[resourceName].CreateCopy());
                if (generator == null)
                {
                    Debug.LogWarning("[Kethane] Defaulting to empty generator for " + resourceName);
                    generator = new EmptyResourceGenerator();
                }
                generators[resourceName] = generator;

                var bodyNodes = resourceNode.GetNodes("Body");

                foreach (var body in FlightGlobals.Bodies)
                {
                    var bodyNode = bodyNodes.SingleOrDefault(n => n.GetValue("Name") == body.name) ?? new ConfigNode();

                    PlanetDeposits[resourceName][body.name] = generator.Load(body, bodyNode.GetNode("GeneratorData"));
                    Scans[resourceName][body.name]          = new Cell.Set(MapOverlay.GridLevel);

                    var scanMask = bodyNode.GetValue("ScanMask");
                    if (scanMask != null)
                    {
                        try
                        {
                            Scans[resourceName][body.name] = new Cell.Set(MapOverlay.GridLevel, Convert.FromBase64String(scanMask.Replace('.', '/').Replace('%', '=')));
                        }
                        catch (FormatException e)
                        {
                            Debug.LogError(String.Format("[Kethane] Failed to parse {0}/{1} scan string, resetting ({2})", body.name, resourceName, e.Message));
                        }
                    }
                }
            }

            if (!config.HasValue("Version") || config.GetValue("Version") == "0.8")
            {
                var str = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Kethane.Resources.GridIndices.txt")).ReadToEnd();
                var map = str.Split(',').Select(s => new Cell(uint.Parse(s))).ToArray();

                foreach (var resource in KethaneController.ResourceDefinitions)
                {
                    foreach (var body in FlightGlobals.Bodies)
                    {
                        var old = Scans[resource.Resource][body.name];
                        var set = new Cell.Set(MapOverlay.GridLevel);

                        foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel))
                        {
                            set[cell] = old[map[cell.Index]];
                        }

                        Scans[resource.Resource][body.name] = set;
                    }
                }
            }

            timer.Stop();
            Debug.LogWarning(String.Format("Kethane deposits loaded ({0}ms)", timer.ElapsedMilliseconds));
        }