예제 #1
0
        public bool CanPerform(Type target, Operation restrictTo = Operation.All)
        {
            Operation oldAllowedOp = this.allowedOp;

            this.allowedOp = oldAllowedOp & restrictTo;

            // Convert ContentRef requests to their respective Resource-requests
            target = ResTypeFromRefType(target);

            if (this.checkedTypes.Contains(target))
            {
                this.allowedOp = oldAllowedOp;
                return(false);
            }
            this.curComplexity++;
            this.checkedTypes.Add(target);

            bool result = false;

            if (!result && this.data.GetDataPresent(target))
            {
                result = true;
            }
            if (!result && this.data.GetDataPresent(target.MakeArrayType()))
            {
                result = true;
            }
            if (!result && this.data.ContainsContentRefs(target))
            {
                result = true;
            }
            if (!result && this.data.ContainsComponentRefs(target))
            {
                result = true;
            }
            if (!result)
            {
                result = CorePluginRegistry.GetDataConverters(target).Any(s => !this.usedConverters.Contains(s) && s.CanConvertFrom(this));
            }

            if (result || this.allowedOp != oldAllowedOp)
            {
                this.checkedTypes.Remove(target);
            }
            this.maxComplexity = Math.Max(this.maxComplexity, this.curComplexity);
            this.curComplexity--;

            this.allowedOp = oldAllowedOp;
            return(result);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        public static void GetPreview(IPreviewQuery query)
        {
            if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated)
            {
                return;
            }
            if (query == null)
            {
                return;
            }

            //System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
            //w.Restart();

            var generators = (
                from g in CorePluginRegistry.GetPreviewGenerators()
                orderby query.SourceFits(g.ObjectType) descending, g.Priority descending
                select g).ToArray();

            foreach (IPreviewGenerator gen in generators)
            {
                if (!query.TransformSource(gen.ObjectType))
                {
                    continue;
                }

                gen.Perform(query);

                if (query.Result != null)
                {
                    break;
                }
            }

            //Log.Editor.Write("Generating preview for {0} / {1} took {2:F} ms", query.OriginalSource, query.GetType().Name, w.Elapsed.TotalMilliseconds);
        }
예제 #5
0
        public IEnumerable <object> Perform(Type target, Operation restrictTo = Operation.All)
        {
            Operation oldAllowedOp = this.allowedOp;

            this.allowedOp = oldAllowedOp & restrictTo;

            // Convert ContentRef requests to their respective Resource-requests
            Type originalType = target;

            target = ResTypeFromRefType(target);

            //Log.Editor.Write("Convert to {0}", target.Name);
            bool fittingDataFound = false;

            // Check if there already is fitting data available
            IEnumerable <object> fittingData = null;

            if (fittingData == null)
            {
                // Single object
                if (this.data.GetDataPresent(target))
                {
                    fittingData = new[] { this.data.GetData(target) }
                }
                ;
            }
            if (fittingData == null)
            {
                // Object array
                Type arrType = target.MakeArrayType();
                if (this.data.GetDataPresent(arrType))
                {
                    fittingData = this.data.GetData(arrType) as IEnumerable <object>;
                }
            }
            if (fittingData == null)
            {
                // ComponentRefs
                if (this.data.ContainsComponentRefs(target))
                {
                    fittingData = this.data.GetComponentRefs(target);
                }
            }
            if (fittingData == null)
            {
                // ContentRefs
                if (this.data.ContainsContentRefs(target))
                {
                    fittingData = this.data.GetContentRefs(target).Res();
                }
            }

            // If something fitting was found, directly add it to the operation results
            if (fittingData != null)
            {
                fittingDataFound = true;
                foreach (object obj in fittingData)
                {
                    this.AddResult(obj);
                }
            }

            // No result yet? Search suitable converters
            if (!fittingDataFound)
            {
                var converterQuery = CorePluginRegistry.GetDataConverters(target);
                List <ConvComplexityEntry> converters = new List <ConvComplexityEntry>();
                foreach (var c in converterQuery)
                {
                    this.maxComplexity = 0;
                    if (this.usedConverters.Contains(c))
                    {
                        continue;
                    }
                    if (!c.CanConvertFrom(this))
                    {
                        continue;
                    }
                    converters.Add(new ConvComplexityEntry(c, this.maxComplexity));
                }

                // Perform conversion
                converters.StableSort((c1, c2) => (c2.Converter.Priority - c1.Converter.Priority) * 10000 + (c1.Complexity - c2.Complexity));
                foreach (var c in converters)
                {
                    //Log.Editor.Write("using {0}", s.GetType().Name);
                    //Log.Editor.PushIndent();
                    //Log.Editor.Write("before: {0}", this.Result.ToString(o => string.Format("{0} {1}", o.GetType().Name, o), ", "));
                    this.usedConverters.Add(c.Converter);
                    bool handled = c.Converter.Convert(this);
                    this.usedConverters.Remove(c.Converter);
                    //Log.Editor.Write("after: {0}", this.Result.ToString(o => string.Format("{0} {1}", o.GetType().Name, o), ", "));
                    //Log.Editor.PopIndent();
                    if (handled)
                    {
                        break;
                    }
                }
            }

            IEnumerable <object> returnValue = this.result;

            // Convert back to Resource requests
            if (typeof(IContentRef).IsAssignableFrom(originalType))
            {
                returnValue = result.OfType <Resource>().Select(r => r.GetContentRef());
            }

            returnValue = returnValue ?? (IEnumerable <object>)Array.CreateInstance(originalType, 0);
            returnValue = returnValue.Where(originalType.IsInstanceOfType);

            this.allowedOp = oldAllowedOp;
            return(returnValue);
        }
예제 #6
0
        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));
            }
        }
예제 #7
0
        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);
            }
        }