Example #1
0
 private void ReadConfiguration()
 {
     l.WriteLine(l.VERBOSE, "Configuration file detected. Reading contents...");
     Configuration = JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(ConfigurationFile));
     if (Configuration == null)
     {
         l.WriteLine(l.VERBOSE, "Erred configuration file! Regenerating from default...");
         File.Delete(ConfigurationFile);
         InitializeConfigurationFile();
     }
     else
     {
         bool ConfigOK = false;
         if (Configuration.ConfigurationVersion != Configuration.GetDefaultConfiguration().ConfigurationVersion)
         {
             l.WriteLine(l.ERROR, "Configuration version mismatch! Reinitializing configuration...");
             File.Move(ConfigurationFile, Path.Combine(ConfigurationFile, ".bak"));
             l.WriteLine(l.ERROR, "Old configuration is now labeled as " + Path.Combine(ConfigurationFile, ".bak") + ".");
             InitializeConfiguration();
             ConfigOK = true;
         }
         else if (String.IsNullOrWhiteSpace(Configuration.ServerName))
         {
             throw new InvalidConfigurationException("Server name is invalid!");
         }
         else if (String.IsNullOrWhiteSpace(Configuration.ServerDesc))
         {
             throw new InvalidConfigurationException("Server description is invalid!");
         }
         else if (Configuration.ID == null)
         {
             throw new InvalidConfigurationException("Server GUID is invalid!");
         }
         else if (Configuration.Port < 1)
         {
             throw new InvalidConfigurationException("Port must be an integer above zero (and below 65535)! (this may happen if your number of max connections is too high!)");
         }
         else if (!NetworkTools.IsPortOpen(Configuration.Port))
         {
             throw new UnavailablePortException("Port is already being used! (NOTE: 1 to 1024 are usually reserved for the operating system.)");
         }
         else if (Configuration.MaxConnections < 1)
         {
             throw new InvalidConfigurationException("Maximum connections must be an integer above zero (and below 2147483647)! (this may happen if your number of max connections is too high!)");
         }
         else if (String.IsNullOrWhiteSpace(Configuration.ConnectionString))
         {
             throw new InvalidConfigurationException("Server connection string is invalid!");
         }
         else if (String.IsNullOrWhiteSpace(Configuration.ColorHex))
         {
             throw new InvalidConfigurationException("Server theme color hex is invalid!");
         }
         else
         {
             if (Configuration.Port < 1024)
             {
                 l.WriteLine(l.WARN, "The port to be occupied looks unsafe! 1 to 1024 are usually reserved for the operating system.");
             }
             if (Configuration.MaxConnections > short.MaxValue)
             {
                 l.WriteLine(l.WARN, "Your maximum connections seem to be high. Beware of CPU burnout!");
             }
             ConfigOK = true;
         }
         l.WriteLine(l.VERBOSE, "Configuration file read. Result: " + (ConfigOK ? "complete and usable." : "erred. Check if your values are correct."));
         if (!ConfigOK)
         {
             throw new InvalidConfigurationException();
         }
     }
 }