public static void untag_craft(List <CraftData> craft, string tag_name) { foreach (CraftData c in craft) { Tags.untag_craft(c, tag_name); } }
protected void respond_to_tag_menu(string resp) { if (resp == "new_tag") { CraftManager.main_ui.create_tag_dialog(false, CraftData.active_craft); } else { if (CraftData.active_craft.Count == 1) { CraftData craft = CraftData.active_craft[0]; if (craft.tag_names().Contains(resp)) { Tags.untag_craft(craft, resp); } else { Tags.tag_craft(craft, resp); } } else { //if all craft in the active selection have the tag, then remove the tag. if (CraftData.active_craft.FindAll(c => c.tag_names().Contains(resp)).Count == CraftData.active_craft.Count) { CraftData.active_craft.ForEach(c => Tags.untag_craft(c, resp)); } else //otherwise add the tag (in the case where some have it and other not, this will result in all having the tag). { CraftData.active_craft.ForEach(c => Tags.tag_craft(c, resp)); } } } }
//Rename the craft (both name in the craft file and the file itself). Does checks before attempting rename to ensure valid name. //the new name should be set on the craft object first; //craft.new_name = "I am Jeff"; //craft.rename(); //returns various strings depending on outcome, a "200" string means all went ok, yes it's an HTTP status code, I'm a web dev, deal with it. public string rename() { string os_safe_name = new_name; if (!String.IsNullOrEmpty(new_name) && new_name != name) { foreach (char c in Path.GetInvalidFileNameChars()) { if (os_safe_name.Contains(c.ToString())) { os_safe_name = os_safe_name.Replace(c, '_'); } } string new_path = path.Replace(file_name + ".craft", os_safe_name + ".craft"); if (File.Exists(new_path)) { return("Another craft already has this name"); } FileInfo file = new FileInfo(path); if (file.Exists) { try{ file.MoveTo(new_path); } catch (Exception e) { return("Unable to rename file\n" + e.Message); } FileInfo thumbnail_file = new FileInfo(thumbnail_path()); List <string> tags = Tags.untag_craft(this); //remove old name from tags (returns any tag names it was in). ConfigNode nodes = ConfigNode.Load(new_path); nodes.SetValue("ship", new_name); nodes.Save(new_path); initialize(new_path, stock_craft); //reprocess the craft file Tags.tag_craft(this, tags); //add updated craft to the tags it was previously in. if (thumbnail_file.Exists) { thumbnail_file.MoveTo(thumbnail_path()); thumbnail = null; } return("200"); } else { return("error 404 - file not found"); } } else { if (String.IsNullOrEmpty(new_name)) { return("name can not be blank"); } else { return("200"); //do nothing if name is unchanged. } } }
//Transfer craft to VAB/SPH/Subassemblies public string transfer_to(EditorFacility facility) { string new_path = ""; if (facility == EditorFacility.SPH) { new_path = Paths.joined(CraftManager.ksp_root, "saves", save_dir, "Ships", "SPH", file_name + ".craft"); } else if (facility == EditorFacility.VAB) { new_path = Paths.joined(CraftManager.ksp_root, "saves", save_dir, "Ships", "VAB", file_name + ".craft"); } else if (facility == EditorFacility.None) { new_path = Paths.joined(CraftManager.ksp_root, "saves", save_dir, "Subassemblies", file_name + ".craft"); } if (String.IsNullOrEmpty(new_path)) { return("Unexpected error"); } if (File.Exists(new_path)) { string msg = "A craft with this name already exists in " + (facility == EditorFacility.None ? "Subassemblies" : "the " + facility.ToString()); return(msg); } else { try{ ConfigNode nodes = ConfigNode.Load(path); nodes.SetValue("type", facility.ToString()); nodes.Save(new_path); } catch (Exception e) { return("Unable to move craft; " + e.Message); } List <string> tags = Tags.untag_craft(this); FileInfo thumbnail_file = new FileInfo(thumbnail_path()); File.Delete(path); initialize(new_path, stock_craft); Tags.tag_craft(this, tags); if (thumbnail_file.Exists) { thumbnail_file.MoveTo(thumbnail_path()); thumbnail = null; } if (CraftManager.main_ui) { CraftManager.main_ui.refresh(); } return("200"); } }
public string delete() { if (File.Exists(path)) { CraftManager.log("Deleting Craft: " + path); File.Delete(path); FileInfo thumbnail_file = new FileInfo(thumbnail_path()); if (thumbnail_file.Exists) { thumbnail_file.Delete(); } Tags.untag_craft(this); if (CraftManager.main_ui) { CraftManager.main_ui.refresh(); } return("200"); } else { return("error 404 - file not found: " + path); } }