Пример #1
0
 private void LoadServersFromDisk()
 {
     if (!Directory.Exists(Constants.ExecutionPath + Constants.ServerFolderLocation))
     {
         Directory.CreateDirectory(Constants.ExecutionPath + Constants.ServerFolderLocation);
     }
     foreach (string folder in Directory.GetDirectories(Constants.ExecutionPath + Constants.ServerFolderLocation))
     {
         Guid serverGuid = Guid.Empty;
         if (Guid.TryParse(Path.GetFileName(folder), out serverGuid))
         {
             if (File.Exists(folder + "/config.xml"))
             {
                 try
                 {
                     ServerConfiguration config = new ServerConfiguration(serverGuid).FromConfiguration();
                     ServerProxy         toAdd  = config.CreateServer();
                     LoadedServers[serverGuid] = toAdd;
                 }
                 catch (Exception e)
                 {
                     Logger.WriteError("Error loading server " + serverGuid + "!\n" + e);
                 }
             }
             else
             {
                 Logger.WriteWarning("Couldn't find server configuration for folder " + folder);
             }
         }
         else
         {
             Logger.WriteWarning("Couldn't load folder " + folder + " as Minecraft server instance!");
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Adds a new server to the list of known servers.
        /// </summary>
        /// <param name="configuration">The configuration of the new server.</param>
        /// <returns>The newly created server.</returns>
        /// <exception cref="InvalidOperationException">
        /// Thrown if <paramref name="configuration"/> does not have a unique identifier or if a server with the same ID already exists.
        /// </exception>
        public ServerProxy AddServer(ServerConfiguration configuration)
        {
            if (configuration.ID == default)
            {
                throw new InvalidOperationException("The server has not been assigned a proper unique identifier.");
            }
            if (LoadedServers.ContainsKey(configuration.ID))
            {
                throw new InvalidOperationException("There is already a server registered with that unique identifier.");
            }
            configuration.SaveConfiguration();
            ServerProxy toLoad = configuration.CreateServer();

            LoadedServers[configuration.ID] = toLoad;
            if (configuration.IsEnabled)
            {
                toLoad.StartAsync();
            }
            return(toLoad);
        }