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 );
}
}
}
}
}
}
}