public static void RenameReferencedFile(string oldName, string newName, ReferencedFileSave rfs, IElement container) { string oldDirectory = FileManager.GetDirectory(oldName); string newDirectory = FileManager.GetDirectory(newName); string newFileNameAbsolute = ProjectManager.MakeAbsolute(newName); string instanceName = FileManager.RemovePath(FileManager.RemoveExtension(newName)); string whyIsntValid; if (oldDirectory != newDirectory) { MessageBox.Show("The old file was located in \n" + oldDirectory + "\n" + "The new file is located in \n" + newDirectory + "\n" + "Currently Glue does not support changing directories.", "Warning"); rfs.SetNameNoCall(oldName); } else if (NameVerifier.IsReferencedFileNameValid(instanceName, rfs.GetAssetTypeInfo(), rfs, container, out whyIsntValid) == false) { MessageBox.Show(whyIsntValid); rfs.SetNameNoCall(oldName); } else { bool shouldMove = true; bool shouldContinue = true; CheckForExistingFileOfSameName(oldName, rfs, newFileNameAbsolute, ref shouldMove, ref shouldContinue); if (shouldContinue) { MoveFileIfNecessary(oldName, newName, shouldMove); string rfsType = rfs.RuntimeType; if (rfsType != null && rfsType.Contains(".")) { // We dont want the fully qualified type. rfsType = FileManager.GetExtension(rfsType); } UpdateObjectsUsingFile(container, oldName, rfs, rfsType); RegenerateCodeAndUpdateUiAccordingToRfsRename(oldName, newName, rfs); UpdateBuildItemsForRenamedRfs(oldName, newName); AdjustDataFilesIfIsCsv(oldName, rfs); GluxCommands.Self.SaveGlux(); ProjectManager.SaveProjects(); } } }
public void TestReferencedFileSave() { EntitySave entitySave = new EntitySave(); ReferencedFileSave rfs = new ReferencedFileSave(); rfs.DestroyOnUnload = false; rfs.Name = "File.png"; entitySave.ReferencedFiles.Add(rfs); string whyNot; NameVerifier.IsReferencedFileNameValid("File", null, null, entitySave, out whyNot); if (string.IsNullOrEmpty(whyNot)) { throw new Exception("Same-named files shouldn't be allowed, but the NameVerifier allows it."); } NameVerifier.IsReferencedFileNameValid("File.wav", null, null, entitySave, out whyNot); if (string.IsNullOrEmpty(whyNot)) { throw new Exception("Same-named files shouldn't be allowed, but the NameVerifier allows it."); } NameVerifier.IsReferencedFileNameValid("Folder/File.wav", null, null, entitySave, out whyNot); if (string.IsNullOrEmpty(whyNot)) { throw new Exception("Same-named files shouldn't be allowed, but the NameVerifier allows it."); } NameVerifier.IsReferencedFileNameValid("Folder/File", null, null, entitySave, out whyNot); if (string.IsNullOrEmpty(whyNot)) { throw new Exception("Same-named files shouldn't be allowed, but the NameVerifier allows it."); } NameVerifier.IsReferencedFileNameValid("if", null, null, entitySave, out whyNot); if (string.IsNullOrEmpty(whyNot)) { throw new Exception("'if' is a reserved keyword and should not be allowed as a file name"); } }
public ReferencedFileSave CreateReferencedFileSaveForExistingFile(IElement containerForFile, string directoryInsideContainer, string absoluteFileName, PromptHandleEnum unknownTypeHandle, AssetTypeInfo ati, out string creationReport, out string errorMessage) { creationReport = ""; errorMessage = null; ReferencedFileSave referencedFileSaveToReturn = null; string whyItIsntValid; // Let's see if there is already an Entity with the same name string fileWithoutPath = FileManager.RemovePath(FileManager.RemoveExtension(absoluteFileName)); bool isValid = NameVerifier.IsReferencedFileNameValid(fileWithoutPath, ati, referencedFileSaveToReturn, containerForFile, out whyItIsntValid); if (!isValid) { errorMessage = "Invalid file name:\n" + fileWithoutPath + "\n" + whyItIsntValid; } else { Zipper.UnzipAndModifyFileIfZip(ref absoluteFileName); string extension = FileManager.GetExtension(absoluteFileName); bool isValidExtensionOrIsConfirmedByUser; bool isUnknownType; FlatRedBall.Glue.Plugins.ExportedImplementations.CommandInterfaces.ElementCommands.CheckAndWarnAboutUnknownFileTypes(unknownTypeHandle, extension, out isValidExtensionOrIsConfirmedByUser, out isUnknownType); string fileToAdd = null; if (isValidExtensionOrIsConfirmedByUser) { string directoryThatFileShouldBeRelativeTo = FlatRedBall.Glue.Plugins.ExportedImplementations.CommandInterfaces.ElementCommands.GetFullPathContentDirectory(containerForFile, directoryInsideContainer); string projectDirectory = ProjectManager.ContentProject.GetAbsoluteContentFolder(); bool needsToCopy = !FileManager.IsRelativeTo(absoluteFileName, projectDirectory); if (needsToCopy) { fileToAdd = directoryThatFileShouldBeRelativeTo + FileManager.RemovePath(absoluteFileName); fileToAdd = FileManager.MakeRelative(fileToAdd, ProjectManager.ContentProject.GetAbsoluteContentFolder()); try { FileHelper.RecursivelyCopyContentTo(absoluteFileName, FileManager.GetDirectory(absoluteFileName), directoryThatFileShouldBeRelativeTo); } catch (System.IO.FileNotFoundException fnfe) { errorMessage = "Could not copy the files because of a missing file: " + fnfe.Message; } } else { fileToAdd = FlatRedBall.Glue.Plugins.ExportedImplementations.CommandInterfaces.ElementCommands.GetNameOfFileRelativeToContentFolder(absoluteFileName, directoryThatFileShouldBeRelativeTo, projectDirectory); } } if (string.IsNullOrEmpty(errorMessage)) { BuildToolAssociation bta = null; if (ati != null && !string.IsNullOrEmpty(ati.CustomBuildToolName)) { bta = BuildToolAssociationManager.Self.GetBuilderToolAssociationByName(ati.CustomBuildToolName); } if (containerForFile != null) { referencedFileSaveToReturn = containerForFile.AddReferencedFile(fileToAdd, ati, bta); } else { bool useFullPathAsName = false; // todo - support built files here referencedFileSaveToReturn = AddReferencedFileToGlobalContent(fileToAdd, useFullPathAsName); } // This will be null if there was an error above in creating this file if (referencedFileSaveToReturn != null) { if (containerForFile != null) { containerForFile.HasChanged = true; } if (fileToAdd.EndsWith(".csv")) { string fileToAddAbsolute = ProjectManager.MakeAbsolute(fileToAdd); CsvCodeGenerator.GenerateAndSaveDataClass(referencedFileSaveToReturn, referencedFileSaveToReturn.CsvDelimiter); } if (isUnknownType) { referencedFileSaveToReturn.LoadedAtRuntime = false; } string error; referencedFileSaveToReturn.RefreshSourceFileCache(false, out error); if (!string.IsNullOrEmpty(error)) { ErrorReporter.ReportError(referencedFileSaveToReturn.Name, error, false); } } } } return(referencedFileSaveToReturn); }
public ReferencedFileSave CreateReferencedFileSaveForExistingFile(IElement containerForFile, string directoryInsideContainer, string absoluteFileName, PromptHandleEnum unknownTypeHandle, AssetTypeInfo ati, out string creationReport, out string errorMessage) { creationReport = ""; errorMessage = null; ReferencedFileSave referencedFileSaveToReturn = null; string whyItIsntValid; // Let's see if there is already an Entity with the same name string fileWithoutPath = FileManager.RemovePath(FileManager.RemoveExtension(absoluteFileName)); bool isValid = NameVerifier.IsReferencedFileNameValid(fileWithoutPath, ati, referencedFileSaveToReturn, containerForFile, out whyItIsntValid); if (!isValid) { errorMessage = "Invalid file name:\n" + fileWithoutPath + "\n" + whyItIsntValid; } else { Zipper.UnzipAndModifyFileIfZip(ref absoluteFileName); string extension = FileManager.GetExtension(absoluteFileName); bool isValidExtensionOrIsConfirmedByUser; bool isUnknownType; CheckAndWarnAboutUnknownFileTypes(unknownTypeHandle, extension, out isValidExtensionOrIsConfirmedByUser, out isUnknownType); if (isValidExtensionOrIsConfirmedByUser) { string directoryThatFileShouldBeRelativeTo = GetFullPathContentDirectory(containerForFile, directoryInsideContainer); string projectDirectory = ProjectManager.ContentProject.GetAbsoluteContentFolder(); string fileToAdd = GetNameOfFileRelativeToContentFolder(absoluteFileName, directoryThatFileShouldBeRelativeTo, projectDirectory); BuildToolAssociation bta = null; if (ati != null && !string.IsNullOrEmpty(ati.CustomBuildToolName)) { bta = BuildToolAssociationManager.Self.GetBuilderToolAssociationByName(ati.CustomBuildToolName); } if (containerForFile != null) { referencedFileSaveToReturn = containerForFile.AddReferencedFile(fileToAdd, ati, bta); } else { bool useFullPathAsName = false; // todo - support built files here referencedFileSaveToReturn = AddReferencedFileToGlobalContent(fileToAdd, useFullPathAsName); } // This will be null if there was an error above in creating this file if (referencedFileSaveToReturn != null) { if (containerForFile != null) { containerForFile.HasChanged = true; } if (fileToAdd.EndsWith(".csv")) { string fileToAddAbsolute = ProjectManager.MakeAbsolute(fileToAdd); CsvCodeGenerator.GenerateAndSaveDataClass(referencedFileSaveToReturn, referencedFileSaveToReturn.CsvDelimiter); } if (isUnknownType) { referencedFileSaveToReturn.LoadedAtRuntime = false; } ProjectManager.UpdateFileMembershipInProject(referencedFileSaveToReturn); PluginManager.ReactToNewFile(referencedFileSaveToReturn); GluxCommands.Self.SaveGlux(); ProjectManager.SaveProjects(); UnreferencedFilesManager.Self.RefreshUnreferencedFiles(false); string error; referencedFileSaveToReturn.RefreshSourceFileCache(false, out error); if (!string.IsNullOrEmpty(error)) { ErrorReporter.ReportError(referencedFileSaveToReturn.Name, error, false); } } } } return(referencedFileSaveToReturn); }