public void save(bool selectedOnly, bool noPrompt) { GuiEditorGui.GuiEditor GuiEditor = "GuiEditor"; GuiControl GuiEditorContent = "GuiEditorContent"; GuiEditorStatusBar GuiEditorStatusBar = "GuiEditorStatusBar"; SimObject currentObject; // Get the control we should save. if (selectedOnly) { SimSet selected = GuiEditor.getSelection(); if (selected.getCount() == 0) return; else if (selected.getCount() > 1) { Util.messageBox("Invalid selection", "Only a single control hierarchy can be saved to a file. Make sure you have selected only one control in the tree view.", "", ""); return; } currentObject = selected.getObject(0); } else if (GuiEditorContent.getCount() > 0) currentObject = GuiEditorContent.getObject(0); else return; // Store the current guide set on the control. GuiEditor.writeGuides(currentObject); currentObject.canSaveDynamicFields = true; // Make sure the guides get saved out. // Construct a base filename. string name; if (currentObject.getName() != "") name = currentObject.getName() + ".gui"; else name = "Untitled.gui"; // Construct a path. string currentFile; if (selectedOnly && currentObject != GuiEditorContent.getObject(0) && currentObject.getFilename() == ((SimObject) GuiEditorContent.getObject(0)).getFilename()) { // Selected child control that hasn't been yet saved to its own file. currentFile = GuiEditor["LastPath"] + "/" + name; currentFile = Util.makeRelativePath(currentFile, Util.getMainDotCsDir()); } else { currentFile = currentObject.getFilename(); if (currentFile == "") { // No file name set on control. Force a prompt. noPrompt = false; if (GuiEditor["LastPath"] != "") { currentFile = GuiEditor["LastPath"] + "/" + name; currentFile = Util.makeRelativePath(currentFile, Util.getMainDotCsDir()); } else currentFile = Util._expandFilename(name); } else currentFile = Util._expandFilename(currentFile); } // Get the filename. string filename; if (!noPrompt) { filename = GuiEditorFileDialog.GuiBuilder.GetSaveName(currentFile); // console.Call_Classname("GuiBuilder", "getSaveName", new string[] {currentFile} ); if (filename == "") return; } else filename = currentFile; // Save the Gui. if (Util.isWriteableFileName(filename)) { // // Extract any existent TorqueScript before writing out to disk // FileObject fileObject = new ObjectCreator("FileObject").Create(); fileObject.openForRead(filename); bool skipLines = true; bool beforeObject = true; // var++ does not post-increment var, in torquescript, it pre-increments it, // because ++var is illegal. //int lines = -1; //int beforeLines = -1; skipLines = false; //string[] beforeNewFileLines = new string[]{}; //string[] newFileLines = new string[]{}; List<string> beforeNewFileLines = new List<string>(); List<string> newFileLines = new List<string>(); while (!fileObject.isEOF()) { string line = fileObject.readLine(); if (line == "//--- OBJECT WRITE BEGIN ---") skipLines = true; else if (line == "//--- OBJECT WRITE END ---") { skipLines = false; beforeObject = false; } else if (skipLines == false) { if (beforeObject) beforeNewFileLines.Add(line); //beforeNewFileLines[ beforeLines++ ] = line; else //newFileLines[ lines++ ] = line; newFileLines.Add(line); } } fileObject.close(); fileObject.delete(); FileObject fo = new ObjectCreator("FileObject").Create(); fo.openForWrite(filename); // Write out the captured TorqueScript that was before the object before the object foreach (string line in beforeNewFileLines) fo.writeLine(line); //for(int i = 0; i <= beforeLines; i++) // fo.writeLine( beforeNewFileLines[ i ] ); fo.writeLine("//--- OBJECT WRITE BEGIN ---"); fo.writeObject(currentObject, "%guiContent = "); fo.writeLine("//--- OBJECT WRITE END ---"); // Write out captured TorqueScript below Gui object //for( int i = 0; i <= lines; i++ ) // fo.writeLine( newFileLines[ i ] ); foreach (string line in newFileLines) fo.writeLine(line); fo.close(); fo.delete(); currentObject.setFilename(Util.makeRelativePath(filename, Util.getMainDotCsDir())); GuiEditorStatusBar.print("Saved file '" + currentObject.getFilename() + "'"); } else Util.messageBox("Error writing to file", "There was an error writing to file '" + currentFile + "'. The file may be read-only.", "Ok", "Error"); }