Esempio n. 1
0
        private static Resource LoadContent(string path)
        {
            if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(path) || Resource.IsDefaultContentPath(path) || !FileOp.Exists(path))
            {
                return(null);
            }

            Logs.Core.Write("Loading Resource '{0}'", path);
            Logs.Core.PushIndent();

            // Load the Resource and register it
            Resource res = Resource.Load <Resource>(path, r =>
            {
                // Registering takes place in the callback - before initializing the Resource, because
                // that may result in requesting more Resources and an infinite loop if two Resources request
                // each other in their initialization code.
                if (r != null)
                {
                    AddContent(path, r);
                }
            });

            Logs.Core.PopIndent();
            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Changes the path key under which a set of Resource can be found, i.e.
        /// renames all path keys located inside the specified directory.
        /// </summary>
        /// <param name="dir">The Resources current directory</param>
        /// <param name="newDir">The Resources new directory</param>
        public static void RenameContentTree(string dir, string newDir)
        {
            if (string.IsNullOrEmpty(dir))
            {
                return;
            }

            // Normalize directory names
            dir    = dir.Replace(PathOp.AltDirectorySeparatorChar, PathOp.DirectorySeparatorChar);
            newDir = newDir.Replace(PathOp.AltDirectorySeparatorChar, PathOp.DirectorySeparatorChar);

            // Ensure we're ending with directory separator chars
            if (dir[dir.Length - 1] != PathOp.DirectorySeparatorChar)
            {
                dir += PathOp.DirectorySeparatorChar;
            }
            if (newDir[newDir.Length - 1] != PathOp.DirectorySeparatorChar)
            {
                newDir += PathOp.DirectorySeparatorChar;
            }

            List <string> renameList = new List <string>(
                from p in resLibrary.Keys
                where !Resource.IsDefaultContentPath(p) && PathOp.IsPathLocatedIn(p, dir)
                select p);

            foreach (string path in renameList)
            {
                string newPath = newDir + path.Remove(0, dir.Length);
                RenameContent(path, newPath);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the Resource Type that is associated with the specified file, based on its extension.
        /// </summary>
        /// <param name="filePath">Path to the file of whichs Resource Type will be returned</param>
        /// <returns>The Resource Type of the specified file</returns>
        public static Type GetTypeByFileName(string filePath)
        {
            // Early-out if we don't have a valid resource path
            if (string.IsNullOrEmpty(filePath) || Resource.IsDefaultContentPath(filePath))
            {
                return(null);
            }

            // Determine the (double) extension of the resource path
            filePath = PathOp.GetFileNameWithoutExtension(filePath);
            string[] token = filePath.Split('.');
            if (token.Length < 2)
            {
                return(null);
            }

            // Extract the type extension and match it with the available resource types
            string   typeName     = token[token.Length - 1];
            TypeInfo matchingInfo =
                DualityApp.GetAvailDualityTypes(typeof(Resource))
                .FirstOrDefault(t => t.Name == typeName);

            if (matchingInfo == null)
            {
                return(null);
            }

            // Return the result
            return(matchingInfo.AsType());
        }
Esempio n. 4
0
        /// <summary>
        /// Unregisters all content that has been registered using paths contained within
        /// the specified directory.
        /// </summary>
        /// <param name="dir">The directory to unregister</param>
        /// <param name="dispose">If true, unregistered content is also disposed.</param>
        public static void RemoveContentTree(string dir, bool dispose = true)
        {
            if (string.IsNullOrEmpty(dir))
            {
                return;
            }

            List <string> unregisterList = new List <string>(
                from p in resLibrary.Keys
                where !Resource.IsDefaultContentPath(p) && PathOp.IsPathLocatedIn(p, dir)
                select p);

            foreach (string p in unregisterList)
            {
                RemoveContent(p, dispose);
            }
        }