Example #1
0
        public static async Task <bool> SaveConfig(PrismConfig config, string filePath, bool backupOriginalFile = true)
        {
            if (backupOriginalFile && File.Exists(filePath))
            {
                var    extension      = Path.GetExtension(filePath);
                string backupFilePath = Path.ChangeExtension(filePath, $"{extension}.backup");

                if (!File.Exists(backupFilePath))
                {
                    File.Copy(filePath, backupFilePath, false);
                }
            }

            await Task.Run(() =>
            {
                using var streamWriter = File.CreateText(config.FilePath);
                streamWriter.WriteLine("# prism3d variable config data");
                streamWriter.WriteLine();

                foreach (var kv in config.KeyValues)
                {
                    streamWriter.WriteLine($"uset {kv.Key} \"{kv.Value}\"");
                }
            });

            return(false);
        }
 public static (bool, PrismConfig) LoadGameConfig(FileStream fileStream)
 {
     try
     {
         var result = PrismConfig.LoadConfig(fileStream);
         return(true, result);
     }
     catch (FileNotFoundException)
     {
         return(false, null);
     }
     catch (ConfigFormatException)
     {
         return(false, null);
     }
 }
Example #3
0
        public static PrismConfig LoadConfig(FileStream fileStream)
        {
            PrismConfig result = CreateConfig();

            result.FilePath = fileStream.Name;
            string[] fileLines = FileUtility.ReadAllLines(fileStream).ToArray();

            for (var i = 0; i < fileLines.Length; i++)
            {
                string   line        = fileLines[i];
                string   trimmedLine = line.Trim();
                string[] parts       = trimmedLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (trimmedLine.StartsWith("#") || string.IsNullOrEmpty(trimmedLine)) // skip comment and empty lines
                {
                    continue;
                }

                string key;
                string value;

                if (parts[0] != "uset")
                {
                    throw new ConfigFormatException($"Found unexpected instruction '{parts[0]}' on line {i} in config file.")
                          {
                              Line        = i,
                              Instruction = parts[0]
                          };
                }

                key   = parts[1];
                value = string.Join(' ', parts.Skip(2)).Trim('"');

                result.KeyValues[key] = value;
            }

            return(result);
        }