FindFiles() public static method

Find files that match the name in a case-insensitive way.
public static FindFiles ( [ fullFileName ) : System.IO.FileInfo[]
fullFileName [ Case-insensitive filename to look for.
return System.IO.FileInfo[]
Example #1
0
        // Makes sure that the map file exists, is properly named, and is loadable.
        static void CheckMapFile([NotNull] string worldName)
        {
            if (worldName == null)
            {
                throw new ArgumentNullException("worldName");
            }

            // Check the world's map file
            string fullMapFileName = Path.Combine(Paths.MapPath, worldName + ".fcm");
            string fileName        = Path.GetFileName(fullMapFileName);

            if (Paths.FileExists(fullMapFileName, false))
            {
                if (!Paths.FileExists(fullMapFileName, true))
                {
                    // Map file has wrong capitalization
                    FileInfo[] matches = Paths.FindFiles(fullMapFileName);
                    if (matches.Length == 1)
                    {
                        // Try to rename the map file to match world's capitalization
                        Paths.ForceRename(matches[0].FullName, fileName);
                        if (Paths.FileExists(fullMapFileName, true))
                        {
                            Logger.Log(LogType.Warning,
                                       "WorldManager.CheckMapFile: Map file for world \"{0}\" was renamed from \"{1}\" to \"{2}\"",
                                       worldName, matches[0].Name, fileName);
                        }
                        else
                        {
                            Logger.Log(LogType.Error,
                                       "WorldManager.CheckMapFile: Failed to rename map file of \"{0}\" from \"{1}\" to \"{2}\"",
                                       worldName, matches[0].Name, fileName);
                            return;
                        }
                    }
                    else
                    {
                        Logger.Log(LogType.Warning,
                                   "WorldManager.CheckMapFile: More than one map file exists matching the world name \"{0}\". " +
                                   "Please check the map directory and use /WLoad to load the correct file.",
                                   worldName);
                        return;
                    }
                }
                // Try loading the map header
                try {
                    MapUtility.LoadHeader(fullMapFileName);
                } catch (Exception ex) {
                    Logger.Log(LogType.Warning,
                               "WorldManager.CheckMapFile: Could not load map file for world \"{0}\": {1}",
                               worldName, ex);
                }
            }
            else
            {
                Logger.Log(LogType.Warning,
                           "WorldManager.CheckMapFile: Map file for world \"{0}\" was not found.",
                           worldName);
            }
        }
Example #2
0
        public static string FindMapFile([NotNull] Player player, [NotNull] string fileName)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            // Check if path contains missing drives or invalid characters
            if (!Paths.IsValidPath(fileName))
            {
                player.Message("Invalid filename or path.");
                return(null);
            }

            // Look for the file
            string sourceFullFileName = Path.Combine(Paths.MapPath, fileName);

            if (!File.Exists(sourceFullFileName) && !Directory.Exists(sourceFullFileName))
            {
                if (File.Exists(sourceFullFileName + ".fcm"))
                {
                    // Try with extension added
                    sourceFullFileName += ".fcm";
                }
                else if (MonoCompat.IsCaseSensitive)
                {
                    try {
                        // If we're on a case-sensitive OS, try case-insensitive search
                        FileInfo[] candidates = Paths.FindFiles(sourceFullFileName + ".fcm");
                        if (candidates.Length == 0)
                        {
                            candidates = Paths.FindFiles(sourceFullFileName);
                        }

                        if (candidates.Length == 0)
                        {
                            player.Message("File/directory not found: {0}", fileName);
                        }
                        else if (candidates.Length == 1)
                        {
                            player.Message("Filenames are case-sensitive! Did you mean to load \"{0}\"?", candidates[0].Name);
                        }
                        else
                        {
                            player.Message("Filenames are case-sensitive! Did you mean to load one of these: {0}",
                                           String.Join(", ", candidates.Select(c => c.Name).ToArray()));
                        }
                    } catch (DirectoryNotFoundException ex) {
                        player.Message(ex.Message);
                    }
                    return(null);
                }
                else
                {
                    // Nothing found!
                    player.Message("File/directory not found: {0}", fileName);
                    return(null);
                }
            }

            // Make sure that the given file is within the map directory
            if (!Paths.Contains(Paths.MapPath, sourceFullFileName))
            {
                player.MessageUnsafePath();
                return(null);
            }

            return(sourceFullFileName);
        }
Example #3
0
        static void LoadWorldListXml()
        {
            XDocument  doc        = XDocument.Load(Paths.WorldListFileName);
            XElement   root       = doc.Root;
            World      firstWorld = null;
            XAttribute temp;

            foreach (XElement el in root.Elements("World"))
            {
                try {
                    if ((temp = el.Attribute("name")) == null)
                    {
                        Logger.Log("Server.ParseWorldListXML: World tag with no name skipped.", LogType.Error);
                        continue;
                    }
                    string worldName = temp.Value;
                    if (!World.IsValidName(worldName))
                    {
                        Logger.Log("Server.ParseWorldListXML: Invalid world name skipped: \"{0}\"", LogType.Error, worldName);
                        continue;
                    }

                    if (Worlds.ContainsKey(worldName.ToLower()))
                    {
                        Logger.Log("Server.ParseWorldListXML: Duplicate world name ignored: \"{0}\"", LogType.Error, worldName);
                        continue;
                    }

                    World world;
                    try {
                        world = AddWorld(null, worldName, null, (el.Attribute("noUnload") != null));
                    } catch (WorldOpException ex) {
                        Logger.Log("Server.ParseWorldListXML: Error loading world \"{0}\": {1}", LogType.Error, worldName, ex.Message);
                        continue;
                    }

                    if ((temp = el.Attribute("hidden")) != null)
                    {
                        if (!Boolean.TryParse(temp.Value, out world.IsHidden))
                        {
                            Logger.Log("Server.ParseWorldListXML: Could not parse \"hidden\" attribute of world \"{0}\", assuming NOT hidden.",
                                       LogType.Warning, worldName);
                            world.IsHidden = false;
                        }
                    }
                    if (firstWorld == null)
                    {
                        firstWorld = world;
                    }

                    if (el.Element("accessSecurity") != null)
                    {
                        world.AccessSecurity = new SecurityController(el.Element("accessSecurity"));
                    }
                    else
                    {
                        world.AccessSecurity.MinRank = LoadWorldRankRestriction(world, "access", el);   // LEGACY
                    }

                    if (el.Element("buildSecurity") != null)
                    {
                        world.BuildSecurity = new SecurityController(el.Element("buildSecurity"));
                    }
                    else
                    {
                        world.BuildSecurity.MinRank = LoadWorldRankRestriction(world, "build", el);   // LEGACY
                    }

                    // Check the world's map file
                    string mapFullName = world.GetMapName();
                    string mapName     = Path.GetFileName(mapFullName);

                    if (Paths.FileExists(mapFullName, false))
                    {
                        if (!Paths.FileExists(mapFullName, true))
                        {
                            // Map file has wrong capitalization
                            FileInfo[] matches = Paths.FindFiles(mapFullName);
                            if (matches.Length == 1)
                            {
                                // Try to rename the map file to match world's capitalization
                                Paths.ForceRename(matches[0].FullName, mapName);
                                if (Paths.FileExists(mapFullName, true))
                                {
                                    Logger.Log("Server.LoadWorldListXML: Map file for world \"{0}\" was renamed from \"{1}\" to \"{2}\"", LogType.Warning,
                                               world.Name, matches[0].Name, mapName);
                                }
                                else
                                {
                                    Logger.Log("Server.LoadWorldListXML: Failed to rename map file of \"{0}\" from \"{1}\" to \"{2}\"", LogType.Error,
                                               world.Name, matches[0].Name, mapName);
                                    continue;
                                }
                            }
                            else
                            {
                                Logger.Log("Server.LoadWorldListXML: More than one map file exists matching the world name \"{0}\". " +
                                           "Please check the map directory and use /wload to load the correct file.", LogType.Warning,
                                           world.Name);
                                continue;
                            }
                        }
                        // Try loading the map header
                        try {
                            MapUtility.LoadHeader(world.GetMapName());
                        } catch (Exception ex) {
                            Logger.Log("Server.LoadWorldListXML: Could not load map file for world \"{0}\": {1}", LogType.Warning,
                                       world.Name, ex);
                        }
                    }
                    else
                    {
                        Logger.Log("Server.LoadWorldListXML: Map file for world \"{0}\" was not found.", LogType.Warning,
                                   world.Name);
                    }
                } catch (Exception ex) {
                    Logger.LogAndReportCrash("An error occured while trying to parse one of the entries on the world list",
                                             "fCraft", ex, false);
                }
            }

            if ((temp = root.Attribute("main")) != null)
            {
                MainWorld = FindWorldExact(temp.Value);
                // if specified main world does not exist, use first-defined world
                if (MainWorld == null && firstWorld != null)
                {
                    Logger.Log("The specified main world \"{0}\" does not exist. " +
                               "\"{1}\" was designated main instead. You can use /wmain to change it.",
                               LogType.Warning, temp.Value, firstWorld.Name);
                    MainWorld = firstWorld;
                }
                // if firstWorld was also null, LoadWorldList() should try creating a new mainWorld
            }
            else
            {
                MainWorld = firstWorld;
            }
        }
        // Makes sure that the map file exists, is properly named, and is loadable.
        static void CheckMapFile([NotNull] World world, FileInfo[] allMapFiles)
        {
            if (world == null)
            {
                throw new ArgumentNullException("world");
            }
            // Check the world's map file
            string fullMapFileName = world.MapFileName;
            string fileName        = Path.GetFileName(fullMapFileName);

            if (Paths.FileExists(fullMapFileName, allMapFiles, false))
            {
                if (!Paths.FileExists(fullMapFileName, allMapFiles, true))
                {
                    // Map file has wrong capitalization
                    FileInfo[] matches = Paths.FindFiles(fullMapFileName);
                    if (matches.Length == 1)
                    {
                        // Try to rename the map file to match world's capitalization
                        // ReSharper disable AssignNullToNotNullAttribute
                        Paths.ForceRename(matches[0].FullName, fileName);
                        // ReSharper restore AssignNullToNotNullAttribute

                        // Don't check with allMapFiles, because the array will have the old incorrectly cased filename.
                        if (Paths.FileExists(fullMapFileName, true))
                        {
                            Logger.Log(LogType.Warning,
                                       "WorldManager.CheckMapFile: Map file for world \"{0}\" was renamed from \"{1}\" to \"{2}\"",
                                       world.Name, matches[0].Name, fileName);
                        }
                        else
                        {
                            Logger.Log(LogType.Error,
                                       "WorldManager.CheckMapFile: Failed to rename map file of \"{0}\" from \"{1}\" to \"{2}\"",
                                       world.Name, matches[0].Name, fileName);
                            return;
                        }
                    }
                    else
                    {
                        Logger.Log(LogType.Warning,
                                   "WorldManager.CheckMapFile: More than one map file exists matching the world name \"{0}\". " +
                                   "Please check the map directory and use /WLoad to load the correct file.",
                                   world.Name);
                        return;
                    }
                }
                // Try loading the map header
                try {
                    MapUtility.LoadHeader(world.MapFileName);
                } catch (Exception ex) {
                    Logger.Log(LogType.Warning,
                               "WorldManager.CheckMapFile: Could not load map file for world \"{0}\": {1}",
                               world.Name, ex);
                }
            }
            else
            {
                Logger.Log(LogType.Warning,
                           "WorldManager.CheckMapFile: Map file for world \"{0}\" was not found.",
                           world.Name);
            }
        }