public override bool Creation(NewObjectCell.ObjectCreationContext context) { string code = @"using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using NeoAxis; using NeoAxis.Editor; namespace Project { public class {Name}{BasedOnComponentClass} { {Body} } }"; //{Name} var className = Path.GetFileNameWithoutExtension(context.fileCreationRealFileName); code = code.Replace("{Name}", className); //{BasedOnComponentClass} if (!string.IsNullOrEmpty(BaseClass)) { code = code.Replace("{BasedOnComponentClass}", " : " + BaseClass); } else { code = code.Replace("{BasedOnComponentClass}", ""); } //{Body} { var body = new List <string>(); if (AddExampleProperties) { body.Add("[DefaultValue( 1 )]"); body.Add("[Range( 0, 2 )]"); body.Add("public double Power { get; set; } = 1;"); body.Add(""); body.Add("[DefaultValue( \"1 1 1\" )]"); body.Add("public ColorValue Color { get; set; } = new ColorValue( 1, 1, 1 );"); //!!!!пример ссылки на компоненту //!!!!пример ссылки на файл } var isComponent = false; if (!string.IsNullOrEmpty(BaseClass)) { if (BaseClass == "NeoAxis.Component") { isComponent = true; } var type = MetadataManager.GetType(BaseClass); if (type != null && typeof(Component).IsAssignableFrom(type.GetNetType())) { isComponent = true; } var type2 = MetadataManager.GetType("NeoAxis." + BaseClass); if (type2 != null && typeof(Component).IsAssignableFrom(type2.GetNetType())) { isComponent = true; } } if (isComponent) { if (body.Count != 0) { body.Add(""); } body.Add("protected override void OnEnabledInSimulation()"); body.Add("{"); body.Add("}"); body.Add(""); body.Add("protected override void OnUpdate( float delta )"); body.Add("{"); body.Add("}"); body.Add(""); body.Add("protected override void OnSimulationStep()"); body.Add("{"); body.Add("}"); } var bodyStr = ""; foreach (var line in body) { if (bodyStr != "") { bodyStr += "\r\n"; } bodyStr += "\t\t" + line; } code = code.Replace("{Body}", bodyStr); } //write file try { File.WriteAllText(context.fileCreationRealFileName, code); } catch (Exception e) { Log.Warning(e.Message); return(false); } //add to Project.csproj if (AddToProjectCsproj) { var toAdd = new List <string>(); var fileName = Path.Combine("Assets", VirtualPathUtility.GetVirtualPathByReal(context.fileCreationRealFileName)); toAdd.Add(fileName); if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error)) { if (toAdd.Count > 1) { Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj.")); } else { Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj.")); } } else { Log.Warning(error); } } return(true); }
private void kryptonButtonInstall_Click(object sender, EventArgs e) { var info = PackageManager.ReadPackageArchiveInfo(selectedPackage.FullFilePath, out var error); if (info == null) { return; } var filesToCopy = new List <string>(); foreach (var file in info.Files) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, file); filesToCopy.Add(fullName); } var text = string.Format(Translate("Install {0}?\n\n{1} files will created."), selectedPackage.GetDisplayName(), filesToCopy.Count); //var text = string.Format( Translate( "Install {0}?\r\n\r\n{1} files will created." ), selectedPackage.Name, filesToCopy.Count ); //var text = $"Install {selectedPackage.Name}?\r\n\r\n{filesToCopy.Count} files will created."; if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) != EDialogResult.Yes) { return; } var notification = ScreenNotifications.ShowSticky("Installing the package..."); try { using (var archive = ZipFile.OpenRead(selectedPackage.FullFilePath)) { foreach (var entry in archive.Entries) { var fileName = entry.FullName; bool directory = fileName[fileName.Length - 1] == '/'; if (fileName != "Package.info" && !directory) { var fullPath = Path.Combine(VirtualFileSystem.Directories.Project, fileName); var directoryName = Path.GetDirectoryName(fullPath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } entry.ExtractToFile(fullPath, true); } } } PackageManager.ChangeInstalledState(selectedPackage.Name, true); } catch (Exception e2) { EditorMessageBox.ShowWarning(e2.Message); return; } finally { notification.Close(); } if (!string.IsNullOrEmpty(info.AddCSharpFilesToProject)) { var toAdd = new ESet <string>(); var path = Path.Combine(VirtualFileSystem.Directories.Assets, info.AddCSharpFilesToProject); if (Directory.Exists(path)) { var fullPaths = CSharpProjectFileUtility.GetProjectFileCSFiles(false, true); var files = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories); foreach (var file in files) { if (!fullPaths.Contains(file)) { toAdd.AddWithCheckAlreadyContained(file); } } } // if( !fileItem.IsDirectory && Path.GetExtension( fileItem.FullPath ).ToLower() == ".cs" ) // { // bool added = CSharpProjectFileUtility.GetProjectFileCSFiles( false, true ).Contains( fileItem.FullPath ); // if( !added ) // toAdd.Add( fileItem.FullPath ); // } if (toAdd.Count != 0) { if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error2)) { if (toAdd.Count > 1) { Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj.")); } else { Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj.")); } } else { Log.Warning(error2); } } } needUpdateList = true; if (info.MustRestart) { ShowRestartLabel(); } if (!string.IsNullOrEmpty(info.OpenAfterInstall)) { var realFileName = VirtualPathUtility.GetRealPathByVirtual(info.OpenAfterInstall); if (info.MustRestart) { EditorSettingsSerialization.OpenFileAtStartup = realFileName; } else { EditorAPI.SelectFilesOrDirectoriesInMainResourcesWindow(new string[] { realFileName }, Directory.Exists(realFileName)); EditorAPI.OpenFileAsDocument(realFileName, true, true); } } ScreenNotifications.Show(EditorLocalization.Translate("General", "The package has been successfully installed.")); //restart application if (info.MustRestart) { var text2 = EditorLocalization.Translate("General", "To apply changes need restart the editor. Restart?"); if (EditorMessageBox.ShowQuestion(text2, EMessageBoxButtons.YesNo) == EDialogResult.Yes) { EditorAPI.BeginRestartApplication(); } } }
private void kryptonButtonUninstall_Click(object sender, EventArgs e) { var filesToDelete = new List <string>(); bool mustRestart = false; //get list of files to delete if (File.Exists(selectedPackage.FullFilePath)) { //get list of files from the package archive var info = PackageManager.ReadPackageArchiveInfo(selectedPackage.FullFilePath, out var error); if (info == null) { ScreenNotifications.Show("Could not read the package info.", true); Log.Warning(error); return; } foreach (var file in info.Files) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, file); if (File.Exists(fullName)) { filesToDelete.Add(file); } } mustRestart = info.MustRestart; } else { //get list of files from selectedPackage.Files in case when the archive file is not exists var str = selectedPackage.Files.Trim(new char[] { ' ', '\r', '\n' }); var files = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var file in files) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, file); if (File.Exists(fullName)) { filesToDelete.Add(file); } } //!!!!mustRestart mustRestart = true; } if (filesToDelete.Count == 0) { return; } var text = string.Format(Translate("Uninstall {0}?\n\n{1} files will deleted."), selectedPackage.GetDisplayName(), filesToDelete.Count); if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) != EDialogResult.Yes) { return; } var filesToDeletionAtStartup = new List <string>(); try { //remove cs files from Project.csproj try { var toRemove = new List <string>(); foreach (var file in filesToDelete) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, file); if (Path.GetExtension(fullName).ToLower() == ".cs") { toRemove.Add(VirtualPathUtility.NormalizePath(fullName)); } } if (toRemove.Count != 0) { CSharpProjectFileUtility.UpdateProjectFile(null, toRemove, out _); } } catch { } //delete files foreach (var file in filesToDelete) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, file); try { File.Delete(fullName); } catch (UnauthorizedAccessException) { filesToDeletionAtStartup.Add(file); } catch (IOException) { filesToDeletionAtStartup.Add(file); } } //delete empty folders { var allFolders = new ESet <string>(); foreach (var file in filesToDelete) { var f = Path.GetDirectoryName(file); while (!string.IsNullOrEmpty(f)) { allFolders.AddWithCheckAlreadyContained(f); f = Path.GetDirectoryName(f); } } var list = allFolders.ToArray(); CollectionUtility.MergeSort(list, delegate(string f1, string f2) { var levels1 = f1.Split(new char[] { '\\' }).Length; var levels2 = f2.Split(new char[] { '\\' }).Length; return(levels2 - levels1); }); foreach (var folder in list) { var fullName = Path.Combine(VirtualFileSystem.Directories.Project, folder); if (Directory.Exists(fullName) && IsDirectoryEmpty(fullName)) { Directory.Delete(fullName); } } } PackageManager.ChangeInstalledState(selectedPackage.Name, false); } catch (Exception e2) { EditorMessageBox.ShowWarning(e2.Message); return; } if (filesToDeletionAtStartup.Count != 0) { PackageManager.AddFilesToDeletionAtStartup(filesToDeletionAtStartup); } needUpdateList = true; if (mustRestart) { ShowRestartLabel(); } ScreenNotifications.Show(EditorLocalization.Translate("General", "The package has been successfully uninstalled.")); }
public virtual bool Creation(NewObjectCell.ObjectCreationContext context) { if (Window.IsFileCreation() && CreateCSharpClass) { context.disableFileCreation = true; GetCreateCSharpClassInfo(out var csharpRealFileName, out var csharpClassName, out var csharpClassNameWithoutNamespace); try { //main file { string className = csharpClassName; //string className = CreateCSharpClass ? csharpClassName : Window.SelectedType.Name; var text = ".component " + className + "\r\n{\r\n}"; File.WriteAllText(context.fileCreationRealFileName, text); } //cs file //if( CreateCSharpClass ) { string code = @"using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using NeoAxis; namespace Project { public class {Name} : {Base} { } }"; code = code.Replace("{Name}", csharpClassNameWithoutNamespace); code = code.Replace("{Base}", Window.SelectedType.Name); File.WriteAllText(csharpRealFileName, code); } } catch (Exception e) { EditorMessageBox.ShowWarning(e.Message); //Log.Warning( e.Message ); return(false); } //if( CreateCSharpClass ) { //add to Project.csproj { var toAdd = new List <string>(); var fileName = Path.Combine("Assets", VirtualPathUtility.GetVirtualPathByReal(csharpRealFileName)); toAdd.Add(fileName); if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error)) { if (toAdd.Count > 1) { Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj.")); } else { Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj.")); } } else { EditorMessageBox.ShowWarning(error); //Log.Warning( error ); return(false); } } Window.DisableUnableToCreateReason = true; //restart application var text = EditorLocalization.Translate("General", "To apply changes need restart the editor. Restart?\r\n\r\nThe editor must be restarted to compile and enable a new created C# class."); if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) == EDialogResult.Yes) { EditorAPI.BeginRestartApplication(); } Window.DisableUnableToCreateReason = false; } } return(true); }