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"); }
public override void onWake() { string text = ""; FileObject f = new ObjectCreator("FileObject").Create(); f.openForRead(this["fileName"]); while (!f.isEOF()) text = text + f.readLine() + "\n"; f.delete(); ((GuiMLTextCtrl) FOT("TextBox")).setText(text); }
public void readDtsConfig() { string filename = Util.filePath(this.path) + "/" + Util.fileBase(this.path) + ".cfg"; string filename2 = Util.filePath(this.path) + "/" + "dtsScene.cfg"; FileObject fo = new ObjectCreator("FileObject").Create(); if (fo.openForRead(filename) || fo.openForRead(filename2)) { string alwaysImport = ""; string neverImport = ""; string mode = "none"; while (!fo.isEOF()) { string line = Util.trim(fo.readLine()); if (line == "AlwaysExport:") // Start of the AlwaysExport list mode = "always"; else if (line == "NeverExport:") // Start of the NeverExport list mode = "never"; else if (Util.startsWith(line, "+", false) || Util.startsWith(line, "-", false)) // Boolean parameters (not supported) mode = "none"; else if (Util.startsWith(line, "=", false)) // Float and integer parameters (not supported) mode = "none"; else if (!Util.startsWith(line, "//", false)) // Non-commented lines { switch (mode) { case "always": alwaysImport = alwaysImport + '\t' + line; break; case "never": neverImport = neverImport + '\t' + line; break; } } } fo.close(); alwaysImport = Util.strreplace(Util.trim(alwaysImport), "\t", ";"); neverImport = Util.strreplace(Util.trim(neverImport), "\t", ";"); ((GuiTextEditCtrl) this.FOT(alwaysImport)).setText(alwaysImport); ((GuiTextEditCtrl) this.FOT(neverImport)).setText(neverImport); } else Util._error("Failed to open " + filename + " or " + filename2 + " for reading"); fo.delete(); }