/// <summary> /// Sets the workspace you want to edit. /// </summary> /// <param name="ws">The workspace which will be edited.</param> internal void EditWorkspace(Workspace ws) { _workspace = ws; // Fill the form. this.nameTextBox.Text = ws.Name; this.XMLTextBox.Text = ws.XMLFolder; this.workspaceTextBox.Text = Path.GetDirectoryName(ws.FileName); this.sourceTextBox.Text = ws.Folder; this.exportTextBox.Text = ws.DefaultExportFolder; this.workspaceTextBox.Enabled = false; this.workspaceButton.Enabled = false; this._filename = ws.FileName; this.Text = Resources.WorkspaceEditTiltle; // Add all xml files into the listbox. if (!string.IsNullOrEmpty(ws.XMLFolder)) { DirectoryInfo dirInfo = new DirectoryInfo(Directory.GetCurrentDirectory()); string xmlFolder = ws.XMLFolder; if (!Path.IsPathRooted(ws.XMLFolder)) { xmlFolder = Path.GetFullPath(xmlFolder); } if (!Directory.Exists(ws.XMLFolder)) { Directory.CreateDirectory(ws.XMLFolder); } string[] files = Directory.GetFiles(xmlFolder, "*.xml", SearchOption.TopDirectoryOnly); foreach(string file in files) { string filename = Path.GetFileName(file); pluginListBox.Items.Add(filename, ws.XMLPlugins.Contains(filename)); } } checkButtons(); }
/// <summary> /// Handles when the done button is pressed. /// </summary> private void doneButton_Click(object sender, EventArgs e) { string wksName = nameTextBox.Text.Trim(); string wksLocation = locationTextBox.Text.Trim(); string xmlfolder = xmlFolderTextBox.Text.Trim(); if (string.IsNullOrEmpty(wksName) || string.IsNullOrEmpty(wksLocation) || string.IsNullOrEmpty(xmlfolder)) { MessageBox.Show(Resources.WorkspaceSettingWarning, Resources.Warning, MessageBoxButtons.OK); return; } string driveStr0 = Path.GetPathRoot(wksLocation); //string driveStr3 = Path.GetPathRoot(xmlfolder); //Debug.Check(driveStr3 == driveStr0); // create the given behavior folder if it does not exist string behaviorFolder = this.sourceTextBox.Text; if (string.IsNullOrEmpty(behaviorFolder)) { behaviorFolder = Path.Combine(wksLocation, "behaviors"); } string driveStr1 = Path.GetPathRoot(behaviorFolder); Debug.Check(driveStr1 == driveStr0); if (!Directory.Exists(behaviorFolder)) Directory.CreateDirectory(behaviorFolder); // create the given export folder if it does not exist string exportFolder = this.exportTextBox.Text; if (string.IsNullOrEmpty(exportFolder)) { exportFolder = Path.Combine(wksLocation, "exported"); } string driveStr2 = Path.GetPathRoot(behaviorFolder); Debug.Check(driveStr2 == driveStr0); if (!Directory.Exists(exportFolder)) Directory.CreateDirectory(exportFolder); // create the updated or new workspace if (_workspace != null) { _workspace = new Workspace(_filename, wksName, xmlfolder, behaviorFolder, exportFolder, _workspace.ExportDatas); } else { _workspace = new Workspace(_filename, wksName, xmlfolder, behaviorFolder, exportFolder); } // update plugins foreach (string plugin in pluginListBox.CheckedItems) { _workspace.AddXMLPlugin(plugin); } Close(); }
/// <summary> /// Handles when the cancel button is pressed. /// </summary> private void cancelButton_Click(object sender, EventArgs e) { // we did not edit or create a workspace _workspace= null; Close(); }
public static bool SaveCustomMeta(Workspace ws) { string bbPath = ws.getBlackboardPath(); XmlDocument bbfile = new XmlDocument(); try { FileManagers.SaveResult result = FileManagers.FileManager.MakeWritable(bbPath, Resources.SaveFileWarning); if (FileManagers.SaveResult.Succeeded != result) { return false; } bbfile.RemoveAll(); XmlDeclaration declaration = bbfile.CreateXmlDeclaration("1.0", "utf-8", null); bbfile.AppendChild(declaration); XmlElement meta = bbfile.CreateElement("meta"); bbfile.AppendChild(meta); SaveCustomTypes(bbfile, meta); SaveCustomMembers(bbfile, meta); bbfile.Save(bbPath); ws.IsBlackboardDirty = false; return true; } catch (Exception ex) { bbfile.RemoveAll(); string msgError = string.Format(Resources.SaveFileError, bbPath, ex.Message); MessageBox.Show(msgError, Resources.SaveError, MessageBoxButtons.OK); } return false; }
public static void PreLoadCustomMeta(Workspace ws) { _agentsXMLNode = null; _typesXMLNode = null; string bbPath = ws.getBlackboardPath(); if (string.IsNullOrEmpty(bbPath) || !File.Exists(bbPath)) { return; } XmlDocument bbfile = new XmlDocument(); try { FileStream fs = new FileStream(bbPath, FileMode.Open, FileAccess.Read, FileShare.Read); bbfile.Load(fs); fs.Close(); XmlNode root = bbfile.ChildNodes[1]; if (root.Name == "meta") { foreach(XmlNode xmlNode in root.ChildNodes) { if (xmlNode.Name == "agents") { _agentsXMLNode = xmlNode; } else if (xmlNode.Name == "types") { _typesXMLNode = xmlNode; } } } else if (root.Name == "agents") { _agentsXMLNode = root; } } catch (Exception e) { MessageBox.Show(e.Message, Resources.LoadError, MessageBoxButtons.OK); bbfile.RemoveAll(); } }
public static void SaveWorkspaceFile(Workspace ws) { // check if we have a valid result if (ws != null && !string.IsNullOrEmpty(ws.FileName)) { try { XmlDocument xml = new XmlDocument(); // Create the xml declaration. XmlDeclaration declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null); xml.AppendChild(declaration); { // Create workspace node. XmlElement workspace = xml.CreateElement("workspace"); workspace.SetAttribute("name", ws.Name); workspace.SetAttribute("xmlmetafolder", ws.RelativeXMLFolder); workspace.SetAttribute("folder", ws.RelativeFolder); workspace.SetAttribute("export", ws.RelativeDefaultExportFolder); xml.AppendChild(workspace); // Create XML plugin nodes. foreach(string xmlPlugin in ws.XMLPlugins) { XmlElement p = xml.CreateElement("xmlmeta"); p.InnerText = xmlPlugin; workspace.AppendChild(p); } // Create export nodes. XmlElement export = xml.CreateElement("export"); workspace.AppendChild(export); foreach(string format in ws._exportDatas.Keys) { ExportData data = ws._exportDatas[format]; // Create exporter nodes. XmlElement exporter = xml.CreateElement(format); export.AppendChild(exporter); // Create isExported node. XmlElement isExported = xml.CreateElement("isexported"); isExported.InnerText = data.IsExported.ToString(); exporter.AppendChild(isExported); // Create exportUnifiedFile node. if (format != "xml" && format != "bson") { XmlElement exportUnifiedFile = xml.CreateElement("exportunifiedfile"); exportUnifiedFile.InnerText = data.ExportUnifiedFile.ToString(); exporter.AppendChild(exportUnifiedFile); } // Create folder node. if (!string.IsNullOrEmpty(data.ExportFolder)) { XmlElement folder = xml.CreateElement("folder"); folder.InnerText = data.ExportFolder; exporter.AppendChild(folder); } // Create includedfilenames nodes. if (data.ExportIncludedFilenames.Count > 0) { XmlElement exportincludedfilenames = xml.CreateElement("includedfilenames"); exporter.AppendChild(exportincludedfilenames); foreach(string includedFilename in data.ExportIncludedFilenames) { XmlElement exportincludedfilename = xml.CreateElement("includedfilename"); exportincludedfilename.InnerText = includedFilename; exportincludedfilenames.AppendChild(exportincludedfilename); } } } } //FileManagers.FileManager.MakeWritable(filename); //string dir = Path.GetDirectoryName(filename); //if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) //{ // Directory.CreateDirectory(dir); //} // save workspaces xml.Save(ws.FileName); } catch (Exception) { string msgError = string.Format(Resources.SaveWorkspaceError, ws.FileName); MessageBox.Show(msgError, Resources.SaveError, MessageBoxButtons.OK); } } }
public static Workspace LoadWorkspaceFile(string filename) { try { if (!File.Exists(filename)) { return null; } XmlDocument xml = new XmlDocument(); xml.Load(filename); XmlNode root = xml.ChildNodes[1]; if (root.Name == "workspace") { string name = GetAttribute(root, "name"); string xmlfolder = GetAttribute(root, "xmlmetafolder"); string folder = GetAttribute(root, "folder"); string defaultExportFolder = GetAttribute(root, "export"); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(xmlfolder) || string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(defaultExportFolder)) { throw new Exception(Resources.LoadWorkspaceError); } Workspace ws = new Workspace(filename, name, xmlfolder, folder, defaultExportFolder); foreach(XmlNode subnode in root) { if (subnode.NodeType == XmlNodeType.Element) { switch (subnode.Name) { // Load all XML plugins. case "xmlmeta": string nodeName = subnode.InnerText.Trim(); if (!string.IsNullOrEmpty(nodeName)) { ws.AddXMLPlugin(nodeName); } break; // Load export nodes. case "export": foreach(XmlNode exportNode in subnode) { if (exportNode.NodeType == XmlNodeType.Element) { if (!ws._exportDatas.ContainsKey(exportNode.Name)) { ws._exportDatas[exportNode.Name] = new ExportData(); } ExportData data = ws._exportDatas[exportNode.Name]; foreach(XmlNode exportInfoNode in exportNode) { switch (exportInfoNode.Name) { case "isexported": data.IsExported = Boolean.Parse(exportInfoNode.InnerText.Trim()); break; case "exportunifiedfile": data.ExportUnifiedFile = Boolean.Parse(exportInfoNode.InnerText.Trim()); break; case "folder": data.ExportFolder = exportInfoNode.InnerText.Trim(); break; case "includedfilenames": foreach(XmlNode sn in exportInfoNode) { if (sn.NodeType == XmlNodeType.Element && sn.Name == "includedfilename") { string includeFilename = sn.InnerText.Trim(); if (!data.ExportIncludedFilenames.Contains(includeFilename)) { data.ExportIncludedFilenames.Add(includeFilename); } } } break; } } } } break; } } } return ws; } } catch (Exception) { string msgError = string.Format(Resources.LoadWorkspaceError, filename); MessageBox.Show(msgError, Resources.LoadError, MessageBoxButtons.OK); } return null; }
private static bool ExportBsonCustomMembers(Workspace ws) { string bbPath = ws.getExportCustomMembersBsonPath(); try { FileManagers.SaveResult result = FileManagers.FileManager.MakeWritable(bbPath, Resources.SaveFileWarning); if (FileManagers.SaveResult.Succeeded != result) { return false; } using(var ms = new MemoryStream()) using(var writer = new BinaryWriter(ms)) { BsonSerializer serializer = BsonSerializer.CreateSerialize(writer); serializer.WriteStartDocument(); serializer.WriteComment("EXPORTED BY TOOL, DON'T MODIFY IT!"); serializer.WriteStartElement("agents"); //serializer.WriteAttributeString("version", "1"); serializer.WriteString("1"); foreach(AgentType agent in Plugin.AgentTypes) { bool hasCustomizedProperty = (agent.GetProperties().Count > 0); //IList<MethodDef> methods = agent.GetMethods(MethodType.Task | MethodType.Event); IList<MethodDef> methods = agent.GetMethods(); bool hasCustomizedMethod = false; foreach(MethodDef method in methods) { if (method.IsCustomized && (method.MemberType == MemberType.Task)) { hasCustomizedMethod = true; break; } } if (agent.IsCustomized || hasCustomizedProperty || hasCustomizedMethod) { serializer.WriteStartElement("agent"); serializer.WriteString(agent.AgentTypeName); //base if (agent.Base != null) { serializer.WriteString(agent.Base.AgentTypeName); } else { serializer.WriteString("none"); } if (hasCustomizedProperty) { serializer.WriteStartElement("properties"); foreach(PropertyDef prop in agent.GetProperties()) { //skip array element if (prop.IsArrayElement || prop.IsPar) { continue; } serializer.WriteStartElement("property"); serializer.WriteString(prop.BasicName); serializer.WriteString(Plugin.GetNativeTypeName(prop.NativeType)); serializer.WriteString(prop.IsMember ? "true" : "false"); serializer.WriteString(prop.IsStatic ? "true" : "false"); //serializer.WriteString(prop.IsPublic ? "true" : "false"); if (prop.IsMember) { serializer.WriteString(prop.ClassName); } else { serializer.WriteString(prop.DefaultValue); } serializer.WriteEndElement(); } serializer.WriteEndElement(); } if (hasCustomizedMethod) { serializer.WriteStartElement("methods"); foreach(MethodDef method in methods) { //skip root and those other custom method if ((method.IsCustomized || method.IsInherited) && (method.MemberType == MemberType.Task)) { Debug.Check(method.IsCustomized); serializer.WriteStartElement("method"); serializer.WriteString(method.BasicName); //serializer.WriteString(Plugin.GetNativeTypeName(method.ReturnType)); //serializer.WriteString(method.IsStatic ? "true" : "false"); //serializer.WriteString(method.IsPublic ? "true" : "false"); //serializer.WriteString((method.MemberType == MemberType.Task) ? "true" : "false"); serializer.WriteString(method.ClassName); foreach(MethodDef.Param param in method.Params) { serializer.WriteStartElement("parameter"); serializer.WriteString(param.Name); serializer.WriteString(param.NativeType); serializer.WriteEndElement(); } serializer.WriteEndElement(); } } //end of methods serializer.WriteEndElement(); } //end of agent serializer.WriteEndElement(); } else { //serializer.WriteTypeNone(); } } //end of 'agents' serializer.WriteEndElement(); serializer.WriteEndDocument(); using(FileStream fs = File.Create(bbPath)) { using(BinaryWriter w = new BinaryWriter(fs)) { byte[] d = ms.ToArray(); w.Write(d); fs.Close(); } } } return true; } catch (Exception ex) { string msgError = string.Format(Resources.SaveFileError, bbPath, ex.Message); MessageBox.Show(msgError, Resources.SaveError, MessageBoxButtons.OK); } return false; }
private static bool ExportXmlCustomMembers(Workspace ws) { string bbPath = ws.getExportCustomMembersXmlPath(); try { FileManagers.SaveResult result = FileManagers.FileManager.MakeWritable(bbPath, Resources.SaveFileWarning); if (FileManagers.SaveResult.Succeeded != result) { return false; } using(StreamWriter file = new StreamWriter(bbPath)) { XmlWriterSettings xmlWs = new XmlWriterSettings(); xmlWs.Indent = true; //xmlWs.OmitXmlDeclaration = true; using(XmlWriter xmlWrtier = XmlWriter.Create(file, xmlWs)) { xmlWrtier.WriteStartDocument(); xmlWrtier.WriteComment("EXPORTED BY TOOL, DON'T MODIFY IT!"); //bbfile.WriteComment("Source File: " + behavior.MakeRelative(behavior.FileManager.Filename)); xmlWrtier.WriteStartElement("agents"); xmlWrtier.WriteAttributeString("version", "1"); foreach(AgentType agent in Plugin.AgentTypes) { bool hasCustomizedProperty = (agent.GetProperties().Count > 0); IList<MethodDef> methods = agent.GetMethods(); bool hasCustomizedMethod = false; foreach(MethodDef method in methods) { if (method.IsCustomized) { hasCustomizedMethod = true; break; } } if (agent.IsCustomized || hasCustomizedProperty || hasCustomizedMethod) { xmlWrtier.WriteStartElement("agent"); xmlWrtier.WriteAttributeString("type", agent.AgentTypeName); if (agent.Base != null) { xmlWrtier.WriteAttributeString("base", agent.Base.AgentTypeName); } if (hasCustomizedProperty) { xmlWrtier.WriteStartElement("properties"); foreach(PropertyDef prop in agent.GetProperties()) { //skip array element if (prop.IsArrayElement || prop.IsPar) { continue; } xmlWrtier.WriteStartElement("property"); xmlWrtier.WriteAttributeString("name", prop.BasicName); xmlWrtier.WriteAttributeString("type", Plugin.GetNativeTypeName(prop.NativeType)); xmlWrtier.WriteAttributeString("member", prop.IsMember ? "true" : "false"); xmlWrtier.WriteAttributeString("static", prop.IsStatic ? "true" : "false"); //xmlWrtier.WriteAttributeString("public", prop.IsPublic ? "true" : "false"); if (prop.IsMember) { xmlWrtier.WriteAttributeString("agent", prop.ClassName); } else { xmlWrtier.WriteAttributeString("defaultvalue", prop.DefaultValue); } xmlWrtier.WriteEndElement(); } xmlWrtier.WriteEndElement(); } if (hasCustomizedMethod) { xmlWrtier.WriteStartElement("methods"); foreach(MethodDef method in methods) { //skip root, but custom method is included if (method.IsCustomized || method.IsInherited) { xmlWrtier.WriteStartElement("method"); xmlWrtier.WriteAttributeString("name", method.BasicName); //xmlWrtier.WriteAttributeString("returntype", Plugin.GetNativeTypeName(method.ReturnType)); //xmlWrtier.WriteAttributeString("static", method.IsStatic ? "true" : "false"); //xmlWrtier.WriteAttributeString("public", method.IsPublic ? "true" : "false"); //xmlWrtier.WriteAttributeString("istask", (method.MemberType == MemberType.Task) ? "true" : "false"); //xmlWrtier.WriteAttributeString("isevent", (method.IsNamedEvent || method.MemberType == MemberType.Task) ? "true" : "false"); xmlWrtier.WriteAttributeString("agent", method.ClassName); foreach(MethodDef.Param param in method.Params) { xmlWrtier.WriteStartElement("parameter"); xmlWrtier.WriteAttributeString("name", param.Name); xmlWrtier.WriteAttributeString("type", param.NativeType); xmlWrtier.WriteEndElement(); } xmlWrtier.WriteEndElement(); } } xmlWrtier.WriteEndElement(); } //end of agent xmlWrtier.WriteEndElement(); } } //end of agents xmlWrtier.WriteEndElement(); xmlWrtier.WriteEndDocument(); } file.Close(); } return true; } catch (Exception ex) { string msgError = string.Format(Resources.SaveFileError, bbPath, ex.Message); MessageBox.Show(msgError, Resources.SaveError, MessageBoxButtons.OK); } return false; }
public static void ExportCustomMembers(Workspace ws) { SaveCustomMeta(ws); ExportXmlCustomMembers(ws); ExportBsonCustomMembers(ws); }
public static void ExportCustomMembers(Workspace ws, bool exportXML, bool exportBson) { SaveCustomMeta(ws); if (exportXML) ExportXmlCustomMembers(ws); if (exportBson) ExportBsonCustomMembers(ws); }
public static bool SaveExtraMeta(Workspace ws) { string extraPath = ws.getExtraMetaPath(); XmlDocument extrafile = new XmlDocument(); try { FileManagers.SaveResult result = FileManagers.FileManager.MakeWritable(extraPath, Resources.SaveFileWarning); if (FileManagers.SaveResult.Succeeded != result) return false; extrafile.RemoveAll(); XmlDeclaration declaration = extrafile.CreateXmlDeclaration("1.0", "utf-8", null); extrafile.AppendChild(declaration); XmlComment comment = extrafile.CreateComment("EXPORTED BY TOOL, DON'T MODIFY IT!"); extrafile.AppendChild(comment); XmlElement meta = extrafile.CreateElement("extrameta"); extrafile.AppendChild(meta); if (SaveExtraMembers(extrafile, meta)) extrafile.Save(extraPath); return true; } catch (Exception ex) { extrafile.RemoveAll(); string msgError = string.Format(Resources.SaveFileError, extraPath, ex.Message); MessageBox.Show(msgError, Resources.SaveError, MessageBoxButtons.OK); } return false; }