Esempio n. 1
0
        internal static void bulk_download(Dictionary <int, Dictionary <string, string> > download_list, string save_dir, Callback callback)
        {
            string save_path = Paths.joined(CraftManager.ksp_root, "saves", save_dir);

            if (Directory.Exists(save_path))
            {
                ensure_ship_folders_exist(save_dir);

                if (download_list.Count > 0)
                {
                    CraftManager.status_info = "Downloading craft from KerbalX...";
                    List <int> keys = new List <int>(download_list.Keys);
                    int        id   = keys[0];


                    Dictionary <string, string> craft_ref = download_list[id];
                    string type = craft_ref["type"];
                    string path = "";
                    if (type == "Subassembly")
                    {
                        path = Paths.joined(save_path, "Subassemblies", craft_ref["name"] + ".craft");
                    }
                    else
                    {
                        path = Paths.joined(save_path, "Ships", type, craft_ref["name"] + ".craft");
                    }
                    bulk_download_log += (String.IsNullOrEmpty(bulk_download_log) ? "" : "\n") + "downloading [" + (type == "Subassembly" ? "Sub" : type) + "]" + craft_ref["name"] + "...";

                    download_list.Remove(id);

                    api.download_craft(id, (craft_file_string, code) => {
                        if (code == 200)
                        {
                            ConfigNode craft = ConfigNode.Parse(craft_file_string);
                            craft.Save(path);
                            Thread.Sleep(1000); //This sleep is just to add a pause to reduce load on the site.
                            bulk_download_log += "Done";
                            log_scroll.y       = 10000;

                            KerbalX.bulk_download(download_list, save_dir, callback);
                        }
                    });
                }
                else
                {
                    CraftManager.status_info = "";
                    callback();
                }
            }
        }
        protected void show_bulk_download_dialog()
        {
            string resp           = "";
            long   time_completed = 0;
            long   close_delay    = 3000;
            long   time_to_close  = close_delay;
            Dictionary <int, Dictionary <string, string> > ids_to_download = new Dictionary <int, Dictionary <string, string> >();

            foreach (CraftData craft in CraftData.selected_group)
            {
                if (!ids_to_download.ContainsKey(craft.remote_id))
                {
                    ids_to_download.Add(craft.remote_id, new Dictionary <string, string> {
                        { "name", craft.name }, { "type", craft.construction_type }
                    });
                }
            }
            KerbalX.bulk_download_log = "";
            KerbalX.bulk_download(ids_to_download, current_save_dir, () => {
                time_completed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            });
            KerbalX.log_scroll = new Vector2();
            show_dialog("Downloading from KerbalX...", "", d => {
                if (!String.IsNullOrEmpty(KerbalX.bulk_download_log))
                {
                    KerbalX.log_scroll = scroll(KerbalX.log_scroll, d.window_pos.width, 80f, (w) => {
                        label(KerbalX.bulk_download_log);
                    });
                }
                if (time_completed != 0)
                {
                    time_to_close = close_delay - ((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - time_completed);
                    section(() => {
                        fspace();
                        button("close (auto closing in " + ((time_to_close / (TimeSpan.TicksPerMillisecond / 10)) + 1).ToString() + ")", close_dialog);
                    });
                    if (time_to_close <= 0)
                    {
                        resp = "200";
                    }
                }
                return(resp);
            });
        }
        protected void populate_new_save_dialog()
        {
            KerbalX.get_craft_ids_by_version((craft_by_version, versions) => {
                string v1                 = versions[0].ToString();
                string v2                 = versions[1].ToString();
                string resp               = "";
                Version ksp_version       = CraftManager.game_version; //new Version(Versioning.GetVersionString());
                KerbalX.log_scroll        = new Vector2();
                KerbalX.bulk_download_log = "";

                show_dialog("Import Craft From KerbalX", "", d => {
                    label("You don't have any craft in this save yet.", "h2");
                    label("Do you want to fetch your craft from KerbalX?", "h2");

                    if (versions[0] == ksp_version)
                    {
                        button("download " + craft_by_version[v1].Count + " craft built in KSP " + ksp_version, () => {
                            KerbalX.bulk_download(craft_by_version[v1], current_save_dir, () => {});
                        });
                    }
                    else
                    {
                        label("You don't have any craft made in this version of KSP");
                        if (v1 != null || v2 != null)
                        {
                            label("get craft from previous versions:");
                            section(() => {
                                if (v1 != null && craft_by_version[v1] != null)
                                {
                                    button("download " + craft_by_version[v1].Count + " craft built in KSP " + v1, () => {
                                        KerbalX.bulk_download(craft_by_version[v1], current_save_dir, () => {});
                                    });
                                }
                                if (v2 != null && craft_by_version[v2] != null)
                                {
                                    button("download " + craft_by_version[v2].Count + " craft built in KSP " + v2, () => {
                                        KerbalX.bulk_download(craft_by_version[v2], current_save_dir, () => {});
                                    });
                                }
                            });
                        }
                    }


                    if (!String.IsNullOrEmpty(KerbalX.bulk_download_log))
                    {
                        KerbalX.log_scroll = scroll(KerbalX.log_scroll, d.window_pos.width, 80f, (w) => {
                            label(KerbalX.bulk_download_log);
                        });
                    }

                    button("OR cherry pick the ones you want", () => {
                        close_dialog();
                        exit_kerbalx_mode_after_close = true;
                        CraftManager.main_ui.show();
                        KerbalX.load_remote_craft();
                    });
                    button("Close", close_dialog);

                    return(resp);
                });
            });
        }