public static bool IsImportFileExisting(string filePath) { string srcFilePath, targetName, targetDir; PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir); // Does the source file already exist? if (File.Exists(srcFilePath)) { return(true); } // Find an importer and check if one of its output files already exist IFileImporter importer = CorePluginRegistry.GetFileImporter(i => i.CanImportFile(srcFilePath)); return(importer != null && importer.GetOutputFiles(srcFilePath, targetName, targetDir).Any(File.Exists)); }
public static bool ImportFile(string filePath) { // Determine & check paths string srcFilePath, targetName, targetDir; PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir); // Find an importer to handle the file import IFileImporter importer = CorePluginRegistry.GetFileImporter(i => i.CanImportFile(srcFilePath)); if (importer != null) { try { // Assure the directory exists Directory.CreateDirectory(Path.GetDirectoryName(srcFilePath)); // Move file from data directory to source directory if (File.Exists(srcFilePath)) { File.Copy(filePath, srcFilePath, true); File.Delete(filePath); } else { File.Move(filePath, srcFilePath); } } catch (Exception) { return(false); } // Import it importer.ImportFile(srcFilePath, targetName, targetDir); GC.Collect(); GC.WaitForPendingFinalizers(); return(true); } else { return(false); } }
public static void ReimportFile(string filePath) { // Find an importer to handle the file import IFileImporter importer = CorePluginRegistry.GetFileImporter(i => i.CanImportFile(filePath)); if (importer == null) { return; } // Guess which Resources are affected and check them first string fileBaseName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(filePath)); List <ContentRef <Resource> > checkContent = ContentProvider.GetAvailableContent <Resource>(); for (int i = 0; i < checkContent.Count; ++i) { ContentRef <Resource> resRef = checkContent[i]; if (resRef.Name == fileBaseName) { checkContent.RemoveAt(i); checkContent.Insert(0, resRef); } } // Iterate over all existing Resources to find out which one to ReImport. List <Resource> touchedResources = null; foreach (ContentRef <Resource> resRef in checkContent) { if (resRef.IsDefaultContent) { continue; } if (!importer.IsUsingSrcFile(resRef, filePath)) { continue; } try { importer.ReimportFile(resRef, filePath); if (resRef.IsLoaded) { if (touchedResources == null) { touchedResources = new List <Resource>(); } touchedResources.Add(resRef.Res); } // Multiple Resources referring to a single source file shouldn't happen // in the current implementation of FileImport and Resource system. // Might change later. break; } catch (Exception) { Log.Editor.WriteError("Can't re-import file '{0}'", filePath); } } if (touchedResources != null) { DualityEditorApp.NotifyObjPropChanged(null, new ObjectSelection((IEnumerable <object>)touchedResources)); } }
public static void NotifyFileRenamed(string filePathOld, string filePathNew) { if (string.IsNullOrEmpty(filePathOld)) { return; } // Find an importer to handle the file rename IFileImporter importer = CorePluginRegistry.GetFileImporter(i => i.CanImportFile(filePathOld)); if (importer == null) { return; } // Guess which Resources are affected and check them first string fileBaseName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(filePathOld)); List <ContentRef <Resource> > checkContent = ContentProvider.GetAvailableContent <Resource>(); for (int i = 0; i < checkContent.Count; ++i) { ContentRef <Resource> resRef = checkContent[i]; if (resRef.Name == fileBaseName) { checkContent.RemoveAt(i); checkContent.Insert(0, resRef); } } // Iterate over all existing Resources to find out which one to modify. List <Resource> touchedResources = null; foreach (ContentRef <Resource> resRef in checkContent) { if (resRef.IsDefaultContent) { continue; } if (!importer.IsUsingSrcFile(resRef, filePathOld)) { continue; } try { Resource res = resRef.Res; if (res.SourcePath == filePathOld) { res.SourcePath = filePathNew; if (touchedResources == null) { touchedResources = new List <Resource>(); } touchedResources.Add(res); // Multiple Resources referring to a single source file shouldn't happen // in the current implementation of FileImport and Resource system. // Might change later. break; } } catch (Exception) { Log.Editor.WriteError("There was an error internally renaming a source file '{0}' to '{1}'", filePathOld, filePathNew); } } if (touchedResources != null) { DualityEditorApp.FlagResourceUnsaved(touchedResources); } }