Exemplo n.º 1
0
        public IOManager()
        {
            #if XBOX360
            initializing = true;

            try
            {
                result = StorageDevice.BeginShowSelector(null, null);
                result.AsyncWaitHandle.WaitOne();
                device = StorageDevice.EndShowSelector(result);
                result.AsyncWaitHandle.Close();
            }
            catch
            { }

            try
            {
                result = device.BeginOpenContainer("Accelerated Delivery", null, null);
                result.AsyncWaitHandle.WaitOne();
                container = device.EndOpenContainer(result);
                result.AsyncWaitHandle.Close();
            }
            catch
            { }
            #elif WINDOWS
            #region Save Downloading
            try
            {
                RegistryKey k1 = null, k2 = null;
                bool uploadFailed = false;
                string password = null;
            #if INDIECITY
                password = "******";
                username = "******";
                filename = Program.SavePath + "IndieCity.sav";
                backupName = Program.SavePath + "IndieCity.bak";
            #else
                using (k1 = Registry.CurrentUser.OpenSubKey("Software", true))
                    using(k2 = k1.CreateSubKey("Accelerated Delivery"))
                    {
                        username = (string)k2.GetValue("username");
                        password = (string)k2.GetValue("password");
            #endif
            #if !INDIECITY
                        filename = Program.SavePath + username + ".sav";
                        backupName = Program.SavePath + username + ".bak";
                        try { uploadFailed = bool.Parse((string)k2.GetValue("hydroxide", false)); }
                        catch { uploadFailed = false; }
                    }
            #endif

                HashAlgorithm a = new SHA256Managed();
                byte[] n = Encoding.UTF8.GetBytes(username);
                byte[] salt = a.ComputeHash(n);
                byte[] ps = Encoding.UTF8.GetBytes(password);
                byte[] pre = new byte[salt.Length + ps.Length];
                ps.CopyTo(pre, 0);
                salt.CopyTo(pre, ps.Length);
                key = a.ComputeHash(pre);

            #if !INDIECITY
                if(!uploadFailed)
                {
                    //SecureString p = new SecureString();
                    //p.AppendChar('p'); p.AppendChar('s'); p.AppendChar('q'); p.AppendChar('1'); p.AppendChar('Z'); p.AppendChar('f');
                    //p.AppendChar('J'); p.AppendChar('i'); p.AppendChar('I');
                    //p.MakeReadOnly();

                    //FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://accelerateddeliverygame.com/assets/saves/" + username + "/ADsave.sav");
                    //request.Method = WebRequestMethods.Ftp.DownloadFile;
                    //request.Credentials = new NetworkCredential("*****@*****.**", p);

                    //FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    WebClient c = new WebClient();
                    byte[] contents = c.UploadValues("http://www.accelerateddeliverygame.com/assets/download_save.php",
                        new System.Collections.Specialized.NameValueCollection() { { "u", username }, { "p", password } });

                    //Stream responseStream = response.GetResponseStream();
                    //StreamReader reader = new StreamReader(responseStream);

                    StreamWriter write;// = new StreamWriter(File.Open(backupName, FileMode.Create, FileAccess.Write));
                    //byte[] contents;
                    //List<byte> bytes = new List<byte>();
                    //while(true)
                    //{
                    //    // read byte-by-byte
                    //    int data = reader.BaseStream.ReadByte();
                    //    if(data == -1)
                    //        break;
                    //    bytes.Add((byte)data);
                    //}
                    //contents = bytes.ToArray(); // convert to array
                    //reader.BaseStream.Read(contents, 0, (int)contents.Length); // reads file into array
                    //write.BaseStream.Write(contents, 0, (int)bytes); // writes file to backup
                    //write.Close();
                    write = new StreamWriter(File.Open(filename, FileMode.Create, FileAccess.Write)); // opens primary
                    write.BaseStream.Write(contents, 0, (int)contents.Length); // writes file to primary
                    write.Close();
                    //reader.Close();
                    //responseStream.Close();
                }
            #endif
            }
            catch(Exception e) { Program.Cutter.WriteExceptionToLog(e, false); }
            #endregion
            #endif
            saveDataSerializer = new XmlSerializer(typeof(SaveFile));
            //saveDataSerializer.UnknownAttribute += new XmlAttributeEventHandler(doPoop);
            //saveDataSerializer.UnknownElement += new t(doPoop);
            //saveDataSerializer.UnknownNode += new XmlNodeEventHandler(doPoop);
            //saveDataSerializer.UnreferencedObject += new UnreferencedObjectEventHandler(doPoop);

            file = new SaveFile();
            OnSaveChanged += delegate { Input.SetOptions(CurrentSaveWindowsOptions, CurrentSaveXboxOptions); };

            try { Load(); SuccessfulLoad = true; }
            catch
            {
                SuccessfulLoad = false;
            #if WINDOWS
                New();
            #endif
            }
            finally { if(CurrentSaveWindowsOptions != null && CurrentSaveXboxOptions != null) Input.SetOptions(CurrentSaveWindowsOptions, CurrentSaveXboxOptions); else throw new Exception("Options are null, crash imminent."); }
            #if XBOX360
            initializing = false;
            #endif
        }
Exemplo n.º 2
0
 private bool tryLoad(Stream s)
 {
 try
 {
     HandleEncryption(ref s);
     file = (SaveFile)saveDataSerializer.Deserialize(s);
     HandleEncryption(ref s);
 }
 catch
 {
     s.Seek(0, SeekOrigin.Begin);
     return true;
 }
 finally
 {
     s.Seek(0, SeekOrigin.Begin);
 }
 return false;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the options from the XML.
        /// </summary>
        public void Load()
        {
            Program.Cutter.WriteToLog(this, "Attempting to load game data.");
            #if XBOX360
            try
            {
                if(container == null)
                {
                    if(initializing)
                        throw new FileNotFoundException("Set Manager to null and continue along.");
                    int? devSelRes;
                    do
                    {
                        devSelRes = SimpleMessageBox.ShowMessageBox("Save", "No storage device is selected. Please select a device.",
                            new string[] { "Select a Device", "Continue Without Saving" }, 0, MessageBoxIcon.Alert);
                        switch(devSelRes)
                        {
                            case -1: // Fall through
                            case 1: returnOptionsToDefault();
                                return;
                            case 0: StorageDevice.BeginShowSelector(storageSelectEnd, null);
                                break;
                        }
                    } while(devSelRes == null);
                }

            try
            {
            FileNotFound:
                if(!container.FileExists(filename))
                {
                    if(!container.FileExists(backupName))
                    {
                        if(initializing)
                            throw new FileNotFoundException("Set Manager to null and continue along.");
                        int? fileResult;
                        do
                        {
                            fileResult = SimpleMessageBox.ShowMessageBox("No File Found", "No options file was found on the storage device. Would you like to create a new file or select a new device?",
                                new string[] { "Create File", "Select New Device", "Exit Without Loading" }, 0, MessageBoxIcon.Alert);
                            switch(fileResult)
                            {
                                case 0: New();
                                    return;
                                case 1: result = StorageDevice.BeginShowSelector(null, null);
                                    result.AsyncWaitHandle.WaitOne();
                                    device = StorageDevice.EndShowSelector(result);
                                    result.AsyncWaitHandle.Close();

                                    result = device.BeginOpenContainer("Accelerated Delivery", null, null);
                                    result.AsyncWaitHandle.WaitOne();
                                    container = device.EndOpenContainer(result);
                                    result.AsyncWaitHandle.Close();
                                    goto FileNotFound;
                                case -1: // Fall through
                                case 2: returnOptionsToDefault();
                                    return;
                            }
                        } while(fileResult == null);
                    }
                }

                currentStream = container.OpenFile(filename, FileMode.Open);
                HandleEncryption(ref currentStream);
                file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                currentStream.Close();
                container.Dispose();
            }
            catch
            {
                try
                {
                    if(!initializing)
                    {
                        int? lalala;
                        do
                        {
                            lalala = SimpleMessageBox.ShowMessageBox("Warning", "The save file was missing or corrupted, but a backup file was found. Loading backup file.", new string[] { "Okay" }, 0, MessageBoxIcon.Alert);
                        } while(lalala == null);
                    }
                    backupStream = container.OpenFile(backupName, FileMode.Open);
                    HandleEncryption(ref backupStream);
                    file = (SaveFile)saveDataSerializer.Deserialize(backupStream);
                    currentStream = container.CreateFile(filename);
                    backupStream.Seek(0, SeekOrigin.Begin);
                    backupStream.CopyTo(currentStream);
                    currentStream.Close();
                    backupStream.Close();
                    container.Dispose();
                }
                catch(Exception ex)
                {
                    if(backupStream != null)
                        backupStream.Close();
                    if(currentStream != null)
                        currentStream.Close();
                    if(container != null)
                        container.Dispose();
                    throw ex;
                }
            }
            #elif WINDOWS
            // Attempt to load primary save
            try
            {
                if(!File.Exists(filename))
                {
                    if(!File.Exists(backupName))
                        New();
                }
                currentStream = File.Open(filename, FileMode.Open);
                HandleEncryption(ref currentStream);
                try
                {
                    file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                }
                catch
                {
                    currentStream.Seek(0, SeekOrigin.Begin);
                    HandleEncryption(ref currentStream);
                    file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                }
                // at this point loading was successful
                // it will always be safe to copy to backup at this point
                currentStream.Seek(0, SeekOrigin.Begin);
                backupStream = File.Open(backupName, FileMode.Create);
                currentStream.CopyTo(backupStream);
                backupStream.Close();
                //HandleEncryption(ref currentStream); // re-encrypts the file
                currentStream.Close();
                SaveGame1 = file.save1;
                SaveGame2 = file.save2;
                SaveGame3 = file.save3;
                currentSave = file.currentSaveGame;
                SuccessfulLoad = true;
                Program.Cutter.WriteToLog(this, "Load successful.");
            }
            catch(Exception ex)
            {
                Program.Cutter.WriteToLog(this, "An error occurred while loading game data. Attempting to load backup save.");
                Program.Cutter.WriteExceptionToLog(ex, false);
                try
                {
                    FileStream dump = File.Open(Program.SavePath + "dump.sav", FileMode.Create);
                    currentStream.Seek(0, SeekOrigin.Begin);
                    currentStream.CopyTo(dump);
                    dump.Close();
                }
                catch(Exception exc)
                {
                    Program.Cutter.WriteToLog(this, "Could not dump save file.");
                    Program.Cutter.WriteExceptionToLog(exc, false);
                }
                finally
                {
                    // Attempt to load backup save and copy data to current save

                    try
                    {
                        if(File.Exists(backupName))
                        {
                            backupStream = File.Open(backupName, FileMode.Open);
                            HandleEncryption(ref backupStream);
                            file = (SaveFile)saveDataSerializer.Deserialize(backupStream);
                            currentStream = File.Open(filename, FileMode.Create);
                            backupStream.Seek(0, SeekOrigin.Begin);
                            backupStream.CopyTo(currentStream);
                            currentStream.Close();
                            backupStream.Close();
                            SaveGame1 = file.save1;
                            SaveGame2 = file.save2;
                            SaveGame3 = file.save3;
                            currentSave = file.currentSaveGame;
                            SuccessfulLoad = true;
                            Program.Cutter.WriteToLog(this, "Load successful.");
                        }
                        else
                        {
                            Program.Cutter.WriteToLog(this, "Backup save was not there. Load failed.");
                            DialogResult d = MessageBox.Show("The save file is corrupt. Is it okay to create a new save file? This will erase your progress.", "Error!", MessageBoxButtons.YesNo);
                            if(d == DialogResult.Yes)
                                New();
                            else
                                Program.Game.Exit();
                        }
                    }
                    catch
                    {
                        // Primary and backups are faulty, throw error
                        if(backupStream != null)
                            backupStream.Close();
                        if(currentStream != null)
                            currentStream.Close();

                        SuccessfulLoad = false;
                        Program.Cutter.WriteToLog(this, "Primary and backup saves are faulty or missing. Load failed. Error is:");
                        Program.Cutter.WriteExceptionToLog(ex, false);

                        DialogResult d = MessageBox.Show("The save file is corrupt. Is it okay to create a new save file? This will erase your progress.", "Error!", MessageBoxButtons.YesNo);
                        if(d == DialogResult.Yes)
                            New();
                        else
                            Program.Game.Exit();
                    }
                }
            }
            #endif
            }