private static void CreateScriptFile() { bool willWrite = false; string path = Path.Combine(Directory, string.Format("{0}.cs", ExtensionName.RemoveSpecialCharacters())); if (File.Exists(path)) { if (EditorUtility.DisplayDialog( "File existed", "The file name is existed in the current directory, overwrite?", "Yes", "No")) { willWrite = true; } } else { willWrite = true; } if (!willWrite) { return; } string[] text = GenerateCode(); File.WriteAllLines(path, text); AssetDatabase.Refresh(); //import the file Object asset = AssetDatabase.LoadAssetAtPath <Object>(FileUtil.GetProjectRelativePath(path)); Selection.activeObject = asset; EditorGUIUtility.PingObject(asset); }
/// <summary> /// Save document in the server /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { try { string pathServerTemp = Server.MapPath("Portals/0/Images/Temp/"); string pathServerThumbImages = Server.MapPath("Portals/0/ModIma/ThumbImages/"); if (IsChallengeFiles) { if (string.IsNullOrEmpty(ValidateSecurity.ValidateString(txtTitle.Text, false))) { rgvtxtTitle.IsValid = false; return; } //Information of the document challengeFileComponent = new ChallengeFileComponent(Guid.NewGuid()); challengeFileComponent.ChallengeFile.Created = DateTime.Now; challengeFileComponent.ChallengeFile.Updated = challengeFileComponent.ChallengeFile.Created; challengeFileComponent.ChallengeFile.ObjectName = txtFileName.Text; challengeFileComponent.ChallengeFile.ObjectType = ddCategory.SelectedItem.Text; challengeFileComponent.ChallengeFile.Size = FileSize; challengeFileComponent.ChallengeFile.ObjectExtension = ExtensionName; challengeFileComponent.ChallengeFile.Language = Language; challengeFileComponent.ChallengeFile.ChallengeReferenceId = ChallengeReference; try { string pathServer = Server.MapPath(ddCategory.SelectedValue); string sourceFile = System.IO.Path.Combine(pathServerTemp, FileName + ExtensionName); string destFile = System.IO.Path.Combine(pathServer, ValidateSecurity.ValidateString(txtFileName.Text, false) + ValidateSecurity.ValidateString(lblExtension.Text, false)); if (!Directory.Exists(pathServer)) { Directory.CreateDirectory(pathServer); } if (System.IO.File.Exists(sourceFile)) { if (!System.IO.File.Exists(destFile)) { System.IO.File.Move(sourceFile, destFile); } else { System.IO.File.Delete(destFile); System.IO.File.Move(sourceFile, destFile); } } //Save document information in the database challengeFileComponent.ChallengeFile.ObjectLocation = ddCategory.SelectedValue + ValidateSecurity.ValidateString(txtFileName.Text, false) + ValidateSecurity.ValidateString(lblExtension.Text, false); challengeFileComponent.Save(); } catch { } } else { documentComponent = new DocumentComponent(Guid.NewGuid()); UserPropertyComponent user = new UserPropertyComponent(UserId); documentComponent.Document.Created = DateTime.Now; documentComponent.Document.CreatedBy = UserId; documentComponent.Document.Updated = documentComponent.Document.Created; documentComponent.Document.Views = 0; documentComponent.Document.Version = 1; documentComponent.Document.UploadedBy = user.UserProperty.UserId; documentComponent.Document.Author = string.Empty;// user.UserProperty.FirstName + " " + user.UserProperty.LastName; documentComponent.Document.Name = ValidateSecurity.ValidateString(txtFileName.Text, false); documentComponent.Document.Title = ValidateSecurity.ValidateString(txtTitle.Text, false); documentComponent.Document.FileType = ExtensionName; documentComponent.Document.Deleted = false; documentComponent.Document.Description = ValidateSecurity.ValidateString(txtDescription.Text, false); documentComponent.Document.Size = FileSize; documentComponent.Document.Permission = "0"; documentComponent.Document.Scope = rdbScope.SelectedValue; documentComponent.Document.Status = "published"; documentComponent.Document.Category = ddCategory.SelectedValue; documentComponent.Document.DocumentObject = Bytes; documentComponent.Document.ExternalReference = SolutionId; documentComponent.Document.Folder = Folder; //Save information of the document if (documentComponent.Save() < 0) { throw new Exception(); } if (ExtensionName.ToUpper() == ".PDF") { GhostscriptWrapper.GeneratePageThumb(pathServerTemp + FileName + ExtensionName, pathServerThumbImages + "pdf-" + documentComponent.Document.DocumentId.ToString() + ".jpg", 1, 150, 150, 300, 300); } } FillDataRepeater(); WizardFile.ActiveStepIndex = 0; } catch (Exception exc) { Exceptions. ProcessModuleLoadException( this, exc); } }
private static string[] GenerateCode() { //Declare it TypeDeclaration d = new TypeDeclaration.Builder() .SetAccessModifier(AccessModifier.PUBLIC) .SetIsStatic(true) .SetInheritance(Inheritance.DEFAULT) .SetScope(Scope.CLASS) .SetName(ExtensionName.RemoveSpecialCharacters()) .Build(); //Import regularly used namespaces, just call a Using(...) if you need more UsingNode un = new UsingNode() .Using("UnityEngine") .Using("System.Collections") .Using("System.Collections.Generic") .Using("UnityEditor"); //Import the base class namespace to simplify its name in the declaration string if (d.baseClass != null) { un.Using(d.baseClass.Namespace); } //The same for interfaces if (d.implementedInterface.HasElement()) { for (int i = 0; i < d.implementedInterface.Count; ++i) { un.Using(d.implementedInterface[i].Namespace); } } //Give it a hint to determine type names TypeAlias.SetUsingNode(un); List <string> code = new List <string>(); //add basic function for an extension code.Add(GetExtensionNameCode()); code.Add(GetPublisherNameCode()); code.Add(GetDescriptionCode()); code.Add(GetVersionCode()); code.Add(GetOpenUserGuideCode()); code.Add(GetOpenSupportLinkCode()); code.Add(GetButtonMethodCode()); code.Add(GetOnGUICode()); //Implement the type TypeNode tn = new TypeNode() .Declare(d) .Implement(code); //Put the new type into the namespace NamespaceNode nn = new NamespaceNode() .SetName(string.Format("{0}.GriffinExtension", PublisherName.RemoveSpecialCharacters())) .SetTypes(tn); //Put everything into a script ScriptNode sn = new ScriptNode() .SetCompilationDirective("GRIFFIN && UNITY_EDITOR") .SetUsing(un) .SetNamespace(nn) .SetTypes(tn); //also set type in case namespace is empty //Now make the code look awesome CodeFormatter formatter = new CodeFormatter(); formatter.addSpaceAfterUsings = true; formatter.addSpaceBetweenMethods = true; formatter.bracketStyle = CodeFormatter.OpenCurlyBracketStyle.NewLine; string[] script = formatter.Format(sn); return(script); //phew... }