ForceRename() public static method

Allows making changes to filename capitalization on case-insensitive filesystems.
public static ForceRename ( [ originalFullFileName, [ newFileName ) : void
originalFullFileName [ Full path to the original filename
newFileName [ New file name (do not include the full path)
return void
Example #1
0
        // Makes sure that the map file exists, is properly named, and is loadable.
        static void CheckMapFile([NotNull] World world)
        {
            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, 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}\"",
                                       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, true);
                } 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);
            }
        }
Example #2
0
        /// <summary> Changes the name of the given world. </summary>
        public static void RenameWorld(World world, string newName, bool moveMapFile)
        {
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }

            if (!World.IsValidName(newName))
            {
                throw new WorldOpException(newName, WorldOpExceptionCode.InvalidWorldName);
            }

            if (world == null)
            {
                throw new WorldOpException(null, WorldOpExceptionCode.WorldNotFound);
            }

            lock (world.WorldLock) {
                string oldName = world.Name;
                if (oldName == newName)
                {
                    throw new WorldOpException(world.Name, WorldOpExceptionCode.NoChangeNeeded);
                }

                lock ( WorldListLock ) {
                    World newWorld = FindWorldExact(newName);
                    if (newWorld != null && newWorld != world)
                    {
                        throw new WorldOpException(newName, WorldOpExceptionCode.DuplicateWorldName);
                    }

                    Worlds.Remove(world.Name.ToLower());
                    world.Name = newName;
                    Worlds.Add(newName.ToLower(), world);
                    UpdateWorldList();

                    if (moveMapFile)
                    {
                        string oldFullFileName = Path.Combine(Paths.MapPath, oldName + ".fcm");
                        string newFileName     = newName + ".fcm";
                        if (File.Exists(oldFullFileName))
                        {
                            try {
                                Paths.ForceRename(oldFullFileName, newFileName);
                            } catch (Exception ex) {
                                throw new WorldOpException(world.Name,
                                                           WorldOpExceptionCode.MapMoveError,
                                                           ex);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        /// <summary> Changes the name of the given world. </summary>
        public static void RenameWorld([NotNull] World world, [NotNull] string newName, bool moveMapFile, bool overwrite)
        {
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            if (world == null)
            {
                throw new ArgumentNullException("world");
            }

            if (!World.IsValidName(newName))
            {
                throw new WorldOpException(newName, WorldOpExceptionCode.InvalidWorldName);
            }

            lock (world.SyncRoot) {
                string oldName = world.Name;
                if (oldName == newName)
                {
                    throw new WorldOpException(world.Name, WorldOpExceptionCode.NoChangeNeeded);
                }

                lock ( SyncRoot ) {
                    World newWorld = FindWorldExact(newName);
                    if (newWorld != null && newWorld != world)
                    {
                        if (overwrite)
                        {
                            RemoveWorld(newWorld);
                        }
                        else
                        {
                            throw new WorldOpException(newName, WorldOpExceptionCode.DuplicateWorldName);
                        }
                    }

                    WorldIndex.Remove(world.Name.ToLower());
                    world.Name = newName;
                    WorldIndex.Add(newName.ToLower(), world);
                    UpdateWorldList();

                    if (moveMapFile)
                    {
                        string oldMapFile = Path.Combine(Paths.MapPath, oldName + ".fcm");
                        string newMapFile = newName + ".fcm";
                        if (File.Exists(oldMapFile))
                        {
                            try {
                                Paths.ForceRename(oldMapFile, newMapFile);
                            } catch (Exception ex) {
                                throw new WorldOpException(world.Name,
                                                           WorldOpExceptionCode.MapMoveError,
                                                           ex);
                            }
                        }

                        using (world.BlockDB.GetWriteLock()) {
                            string oldBlockDBFile = Path.Combine(Paths.BlockDBDirectory, oldName + ".fbdb");
                            string newBockDBFile  = newName + ".fbdb";
                            if (File.Exists(oldBlockDBFile))
                            {
                                try {
                                    Paths.ForceRename(oldBlockDBFile, newBockDBFile);
                                } catch (Exception ex) {
                                    throw new WorldOpException(world.Name,
                                                               WorldOpExceptionCode.MapMoveError,
                                                               ex);
                                }
                            }
                        }
                    }
                }
            }
        }
Example #4
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;
            }
        }