Exemple #1
0
        public static bool Load(string filename)
        {
            try
            {
                var configFile = new ConfigFile();

                if (!configFile.Load(filename))
                {
                    Log.Info.Write(ErrorSystemType.Config, "Fallback to default settings.");
                    SetDefaults();

                    try
                    {
                        if (Save(filename))
                        {
                            Log.Info.Write(ErrorSystemType.Config, $"Saved default settings to config file '{filename}'.");
                            return(false);
                        }
                    }
                    catch
                    {
                        // ignore
                    }

                    Log.Info.Write(ErrorSystemType.Config, $"Failed to save settings to config file '{filename}'.");
                    return(false);
                }


                // Game
                const string game = "game";
                if (int.TryParse(configFile.Value(game, "options", DefaultOptions.ToString()), out int options))
                {
                    Game.Options = options | 0x01; // bit 0 must be always set
                }
                Game.GraphicDataUsage = configFile.Value(game, "graphic_data_usage", DefaultGraphicDataUsage);
                Game.SoundDataUsage   = configFile.Value(game, "sound_data_usage", DefaultSoundDataUsage);
                Game.MusicDataUsage   = configFile.Value(game, "music_data_usage", DefaultMusicDataUsage);


                // Audio
                const string audio = "audio";
                Audio.Music = configFile.Value(audio, "music", DefaultMusic);
                Audio.Sound = configFile.Value(audio, "sound", DefaultSound);

                if (float.TryParse(configFile.Value(audio, "volume", DefaultVolume.ToString()), NumberStyles.Float, CultureInfo.InvariantCulture, out float volume))
                {
                    Audio.Volume = volume;
                }


                // Video
                const string video = "video";
                if (int.TryParse(configFile.Value(video, "resolution_width", DefaultResolutionX.ToString()), out int resolutionWidth))
                {
                    Video.ResolutionWidth = resolutionWidth;
                }
                if (int.TryParse(configFile.Value(video, "resolution_height", DefaultResolutionY.ToString()), out int resolutionHeight))
                {
                    Video.ResolutionHeight = resolutionHeight;
                }
                Video.Fullscreen = configFile.Value(video, "fullscreen", DefaultFullscreen);

                // Logging
                const string logging = "logging";
                Logging.LogLevel = configFile.Value(logging, "level", DefaultLogLevel);
                if (long.TryParse(configFile.Value(logging, "max_log_size", DefaultMaxLogSize.ToString()), out long maxLogSize))
                {
                    Logging.MaxLogSize = maxLogSize;
                }
                Logging.LogFileName  = configFile.Value(logging, "log_file", DefaultLogFile);
                Logging.LogToConsole = configFile.Value(logging, "log_to_console", DefaultLogToConsole);

                if (Logging.MaxLogSize < MinLogSize)
                {
                    Logging.MaxLogSize = MinLogSize;
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }