Пример #1
0
        public static void Add(MySim mySim)
        {
            CreateMySimFileIfNotExists();
            var bytes = mySim.Encode();
            var img   = SaveImage(mySim.Bitmap, mySim.ImageFileName);

            try
            {
                using (var stream = new FileStream(MySimFilePath, FileMode.Append, FileAccess.Write))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            catch (IOException)
            {
                try
                {
                    File.Delete(img);
                }
                finally
                {
                    throw new IOException("Can't write to MySims.dat file.");
                }
            }
        }
Пример #2
0
 private static void AddMySim(string[] args)
 {
     if (args.Length == 4)
     {
         var name       = args[0];
         var gender     = ParseGender(args[1]);
         var zodiacSign = ParseZodiacSign(args[2]);
         var path       = args[3];
         var sim        = new MySim(name, gender, zodiacSign, path);
         MySimFile.Add(sim);
     }
     else
     {
         throw new ArgumentException("'add' command takes 4 arguments.");
     }
 }
Пример #3
0
        public static void UpdateImage(int index, string imageFileName)
        {
            CreateMySimFileIfNotExists();
            var    bitmap   = MySim.ImportImage(imageFileName);
            string filename = null;

            try
            {
                using (var stream = new FileStream(MySimFilePath, FileMode.Open, FileAccess.Read))
                {
                    var bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, (int)stream.Length);
                    var b = bytes.Skip(4);
                    for (var i = 0; b.Count() > 0; i++)
                    {
                        var nameLength = (int)b.ElementAt(0);
                        b = b.Skip(1);
                        b = b.Skip(nameLength);
                        b = b.Skip(2);
                        var filenameLength = (int)b.ElementAt(0);
                        b = b.Skip(1);
                        if (i == index)
                        {
                            filename = DecodeUTF8(b.Take(filenameLength).ToArray());
                            break;
                        }
                        b = b.Skip(filenameLength);
                    }
                }
            }
            catch (IOException)
            {
                throw new IOException("Can't read MySims.dat file.");
            }
            if (filename == null)
            {
                throw new InvalidOperationException("The index is out of range.");
            }
            SaveImage(bitmap, filename, true);
        }