Esempio n. 1
0
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] // this usage is perfectly correct
        public static CargoInfoReader FromFile(string filename = null)
        {
            CargoInfoReader info = new CargoInfoReader();

            string data = Files.FromSavedGames("Cargo.json");

            if (data != null)
            {
                info = JsonConvert.DeserializeObject <CargoInfoReader>(data);
            }
            return(info);
        }
Esempio n. 2
0
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] // this usage is perfectly correct
        public static CargoInfoReader FromFile(string filename = null)
        {
            CargoInfoReader info      = new CargoInfoReader();
            Regex           Filter    = new Regex(@"^Cargo\.json$");
            string          directory = GetSavedGamesDir();

            if (directory == null || directory.Trim() == "")
            {
                return(null);
            }

            FileInfo fileInfo = null;

            try
            {
                fileInfo = FindCargoInfoFile(directory, Filter);
            }
            catch (NotSupportedException nsex)
            {
                Logging.Error("Directory " + directory + " not supported: ", nsex);
            }

            if (fileInfo != null)
            {
                int maxTries = 6;
                while (IsFileLocked(fileInfo))
                {
                    Thread.Sleep(100);
                    maxTries--;
                    if (maxTries == 0)
                    {
                        Logging.Info("Unable to open Elite Dangerous Cargo.json file");
                        return(null);
                    }
                }

                using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                    {
                        string data = string.Empty;
                        fs.Seek(0, SeekOrigin.Begin);
                        data = reader.ReadToEnd() ?? string.Empty;
                        info = JsonConvert.DeserializeObject <CargoInfoReader>(data);
                    }
            }
            return(info);
        }