static void WorldLoadHandler( Player player, Command cmd ) {
string fileName = cmd.Next();
string worldName = cmd.Next();
if( worldName == null && player.World == null ) {
player.Message( "When using /WLoad from console, you must specify the world name." );
return;
}
if( fileName == null ) {
// No params given at all
CdWorldLoad.PrintUsage( player );
return;
}
string fullFileName = WorldManager.FindMapFile( player, fileName );
if( fullFileName == null ) return;
// Loading map into current world
if( worldName == null ) {
if( !cmd.IsConfirmed ) {
player.Confirm( cmd, "Replace THIS MAP with \"{0}\"?", fileName );
return;
}
Map map;
try {
map = MapUtility.Load( fullFileName );
} catch( Exception ex ) {
player.MessageNow( "Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message );
return;
}
World world = player.World;
// Loading to current world
world.MapChangedBy = player.Name;
world.ChangeMap( map );
world.Players.Message( player, "{0}&S loaded a new map for this world.",
player.ClassyName );
player.MessageNow( "New map loaded for the world {0}", world.ClassyName );
Logger.Log( LogType.UserActivity,
"{0} loaded new map for world \"{1}\" from {2}",
player.Name, world.Name, fileName );
} else {
// Loading to some other (or new) world
if( !World.IsValidName( worldName ) ) {
player.MessageInvalidWorldName( worldName );
return;
}
string buildRankName = cmd.Next();
string accessRankName = cmd.Next();
Rank buildRank = RankManager.DefaultBuildRank;
Rank accessRank = null;
if( buildRankName != null ) {
buildRank = RankManager.FindRank( buildRankName );
if( buildRank == null ) {
player.MessageNoRank( buildRankName );
return;
}
if( accessRankName != null ) {
accessRank = RankManager.FindRank( accessRankName );
if( accessRank == null ) {
player.MessageNoRank( accessRankName );
return;
}
}
}
// Retype world name, if needed
if( worldName == "-" ) {
if( player.LastUsedWorldName != null ) {
worldName = player.LastUsedWorldName;
} else {
player.Message( "Cannot repeat world name: you haven't used any names yet." );
return;
}
}
lock( WorldManager.SyncRoot ) {
World world = WorldManager.FindWorldExact( worldName );
if( world != null ) {
player.LastUsedWorldName = world.Name;
// Replacing existing world's map
if( !cmd.IsConfirmed ) {
player.Confirm( cmd, "Replace map for {0}&S with \"{1}\"?",
world.ClassyName, fileName );
return;
}
Map map;
try {
map = MapUtility.Load( fullFileName );
} catch( Exception ex ) {
player.MessageNow( "Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message );
return;
}
try {
world.MapChangedBy = player.Name;
world.ChangeMap( map );
} catch( WorldOpException ex ) {
Logger.Log( LogType.Error,
"Could not complete WorldLoad operation: {0}", ex.Message );
player.Message( "&WWLoad: {0}", ex.Message );
return;
}
world.Players.Message( player, "{0}&S loaded a new map for the world {1}",
player.ClassyName, world.ClassyName );
player.MessageNow( "New map for the world {0}&S has been loaded.", world.ClassyName );
Logger.Log( LogType.UserActivity,
"{0} loaded new map for world \"{1}\" from {2}",
player.Name, world.Name, fullFileName );
} else {
// Adding a new world
string targetFullFileName = Path.Combine( Paths.MapPath, worldName + ".fcm" );
if( !cmd.IsConfirmed &&
File.Exists( targetFullFileName ) && // target file already exists
!Paths.Compare( targetFullFileName, fullFileName ) ) {
// and is different from sourceFile
player.Confirm( cmd,
"A map named \"{0}\" already exists, and will be overwritten with \"{1}\".",
Path.GetFileName( targetFullFileName ), Path.GetFileName( fullFileName ) );
return;
}
Map map;
try {
map = MapUtility.Load( fullFileName );
} catch( Exception ex ) {
player.MessageNow( "Could not load \"{0}\": {1}: {2}",
fileName, ex.GetType().Name, ex.Message );
return;
}
World newWorld;
try {
newWorld = WorldManager.AddWorld( player, worldName, map, false );
} catch( WorldOpException ex ) {
player.Message( "WLoad: {0}", ex.Message );
return;
}
if( newWorld == null ) {
player.MessageNow( "Failed to create a new world." );
return;
}
player.LastUsedWorldName = worldName;
newWorld.BuildSecurity.MinRank = buildRank;
if( accessRank == null ) {
newWorld.AccessSecurity.ResetMinRank();
} else {
newWorld.AccessSecurity.MinRank = accessRank;
}
newWorld.BlockDB.AutoToggleIfNeeded();
if( BlockDB.IsEnabledGlobally && newWorld.BlockDB.IsEnabled ) {
player.Message( "BlockDB is now auto-enabled on world {0}", newWorld.ClassyName );
}
newWorld.LoadedBy = player.Name;
newWorld.LoadedOn = DateTime.UtcNow;
Server.Message( "{0}&S created a new world named {1}",
player.ClassyName, newWorld.ClassyName );
Logger.Log( LogType.UserActivity,
"{0} created a new world named \"{1}\" (loaded from \"{2}\")",
player.Name, worldName, fileName );
WorldManager.SaveWorldList();
player.MessageNow( "Access permission is {0}+&S, and build permission is {1}+",
newWorld.AccessSecurity.MinRank.ClassyName,
newWorld.BuildSecurity.MinRank.ClassyName );
}
}
}
Server.RequestGC();
}