Ejemplo n.º 1
0
        /// <summary>
        /// Loads the save file with the given file name, or returns an empty save
        /// if the file doens't exist.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private SaveFile LoadFile(string fileName)
        {
            SaveFile file = new SaveFile(fileName);

            StorageContainer container = SmileyUtil.GetStorageContainer();

            if (!container.FileExists(fileName))
            {
                file.IsEmpty = true;
                return(file);
            }

            //Select the specified save file
            using (BitStream input = new BitStream(container, fileName, BitStreamMode.Read))
            {
                file.TimePlayed = TimeSpan.FromTicks(input.ReadBits(64));

                //Load abilties
                foreach (Ability ability in Enum.GetValues(typeof(Ability)))
                {
                    file.HasAbility[ability] = input.ReadBit();
                }

                //Load keys
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        file.NumKeys[i, j] = input.ReadByte();
                    }
                }

                //Load gems
                foreach (Level level in Enum.GetValues(typeof(Level)))
                {
                    foreach (Gem gem in Enum.GetValues(typeof(Gem)))
                    {
                        file.NumGems[level][gem] = input.ReadByte();
                    }
                }

                //Load money
                file.Money = input.ReadByte();

                //Load upgrades
                foreach (Upgrade upgrade in Enum.GetValues(typeof(Upgrade)))
                {
                    file.NumUpgrades[upgrade] = input.ReadByte();
                }

                //Load which bosses have been slain
                foreach (Boss boss in Enum.GetValues(typeof(Boss)))
                {
                    file.HasKilledBoss[boss] = input.ReadBit();
                }

                //Load player zone and location
                file.Level       = (Level)input.ReadByte();
                file.PlayerGridX = input.ReadByte();
                file.PlayerGridY = input.ReadByte();

                //Health and mana
                file.PlayerHealth = (float)(input.ReadByte() / 4);
                file.PlayerMana   = (float)(input.ReadByte());

                //Load changed shit
                int numChanges = input.ReadBits(16);
                for (int i = 0; i < numChanges; i++)
                {
                    file.ChangeTile((Level)input.ReadByte(), input.ReadByte(), input.ReadByte());
                }

                //Load Stats
                file.NumTongueLicks   = input.ReadByte();
                file.NumEnemiesKilled = input.ReadByte();
                file.PixelsTraversed  = input.ReadBits(24);

                //Tutorial Man
                file.AdviceManEncounterCompleted = input.ReadBit();

                for (int i = 0; i < 3; i++)
                {
                    SMH.GUI.SetAbilityInSlot((Ability)input.ReadBits(5), i);
                }

                foreach (Level level in Enum.GetValues(typeof(Level)))
                {
                    file.HasVisitedLevel[level] = input.ReadBit();
                }

                file.Difficulty = (Difficulty)input.ReadByte();

                //Exploration data
                foreach (Level level in Enum.GetValues(typeof(Level)))
                {
                    for (int j = 0; j < 256; j++)
                    {
                        for (int k = 0; k < 256; k++)
                        {
                            file.Explored[level][j, k] = input.ReadBit();
                        }
                    }
                }
            }

            file.TimeFileLoaded = DateTime.Now.TimeOfDay;

            return(file);
        }
Ejemplo n.º 2
0
        public void EraseFile(int index)
        {
            try
            {
                IAsyncResult device = StorageDevice.BeginShowSelector(PlayerIndex.One, null, null);
                device.AsyncWaitHandle.WaitOne();
                StorageDevice storageDevice = StorageDevice.EndShowSelector(device);

                if (storageDevice.IsConnected && storageDevice != null)
                {
                    IAsyncResult result = storageDevice.BeginOpenContainer("Element Data", null, null);
                    result.AsyncWaitHandle.WaitOne();
                    StorageContainer container = storageDevice.EndOpenContainer(result);
                    result.AsyncWaitHandle.Close();

                    if (index == 0)
                    {
                        if (container.FileExists(FILE_0_DATA_NAME))
                        {
                            container.DeleteFile(FILE_0_DATA_NAME);
                        }

                        if (container.FileExists(FILE_0_DATA_NAME_OLD))
                        {
                            container.DeleteFile(FILE_0_DATA_NAME_OLD);
                        }

                        if (container.FileExists(FILE_0_DATA_NAME_NEW))
                        {
                            container.DeleteFile(FILE_0_DATA_NAME_NEW);
                        }
                    }
                    else if (index == 1)
                    {
                        if (container.FileExists(FILE_1_DATA_NAME))
                        {
                            container.DeleteFile(FILE_1_DATA_NAME);
                        }

                        if (container.FileExists(FILE_1_DATA_NAME_OLD))
                        {
                            container.DeleteFile(FILE_1_DATA_NAME_OLD);
                        }

                        if (container.FileExists(FILE_1_DATA_NAME_NEW))
                        {
                            container.DeleteFile(FILE_1_DATA_NAME_NEW);
                        }
                    }
                    else if (index == 2)
                    {
                        if (container.FileExists(FILE_2_DATA_NAME))
                        {
                            container.DeleteFile(FILE_2_DATA_NAME);
                        }

                        if (container.FileExists(FILE_2_DATA_NAME_OLD))
                        {
                            container.DeleteFile(FILE_2_DATA_NAME_OLD);
                        }

                        if (container.FileExists(FILE_2_DATA_NAME_NEW))
                        {
                            container.DeleteFile(FILE_2_DATA_NAME_NEW);
                        }
                    }

                    container.Dispose();
                    Console.WriteLine("Erase complete!");
                }
            }
            catch
            {
                Console.WriteLine("ERROR! ERASE FAILED!!!");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method deletes a game save object from
        /// the StorageContainer for this game.
        /// By: Joseph Shaw
        /// </summary>
        /// <param name="device">The device we are deleting from</param>
        /// <param name="gameData">The game data we are deleting</param>
        public static void DoDeleteGame(StorageDevice device, CharacterSaveFile gameData)
        {
            // Open a storage container.
            IAsyncResult result = device.BeginOpenContainer("DungeonCrawler", null, null);

            // Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            StorageContainer container = device.EndOpenContainer(result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            // Create the BinaryFormatter here in-case we have to create a new save
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            Stream          stream;
            // Use this to tell us if this is a new save
            bool fileExists = false;

            // Check to see whether the save exists.
            if (container.FileExists(gameData.fileName))
            {
                // Delete it so that we can create one fresh.
                container.DeleteFile(gameData.fileName);
                fileExists = true;
            }

            // Create/Update the charPreview to reflect the current player's level
            CharacterSaveFilePreview charPreview;
            MasterSaveFile           masterSaveFile = GetMasterSaveFile(device);

            if (fileExists)
            {
                charPreview = masterSaveFile.charFiles.Find(charFile => charFile.CharacterSaveFile == gameData.fileName);
                masterSaveFile.charFiles.Remove(charPreview);
            }

            if (masterSaveFile.charFiles != null)
            {
                // Sort the list by the file name and rename the files to eliminate gaps
                masterSaveFile.charFiles.OrderBy(s1 => s1.CharacterSaveFile);
                masterSaveFile.charFiles = RenameFiles(device, masterSaveFile.charFiles);
            }
            else
            {
                masterSaveFile.charFiles = new List <CharacterSaveFilePreview>();
            }

            SaveMasterFile(device, masterSaveFile);

            // Create the file.
            stream = container.CreateFile(gameData.fileName);

            // Convert the file to binary and save it
            binaryFormatter.Serialize(stream, gameData);

            // Close the file.
            stream.Close();

            // Dispose the container, to commit changes.
            container.Dispose();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create new replay for a specific track number.
        /// </summary>
        /// <param name="setTrackNum">Set track number</param>
        public Replay(int setTrackNum, bool createNew, Track track)
        {
            trackNum = setTrackNum;

            // if creating new, we're done
            if (createNew == true)
            {
                return;
            }

            bool replayFileFound = false;

            FileHelper.StorageContainerMRE.WaitOne();
            FileHelper.StorageContainerMRE.Reset();

            try
            {
                StorageDevice storageDevice = FileHelper.XnaUserDevice;
                if ((storageDevice != null) && storageDevice.IsConnected)
                {
                    IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);

                    async.AsyncWaitHandle.WaitOne();

                    using (StorageContainer container =
                               storageDevice.EndOpenContainer(async))
                    {
                        async.AsyncWaitHandle.Close();
                        if (container.FileExists(ReplayFilenames[trackNum]))
                        {
                            using (Stream stream = container.OpenFile(ReplayFilenames[trackNum],
                                                                      FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                using (BinaryReader reader = new BinaryReader(stream))
                                {
                                    // Load total lap time
                                    lapTime = reader.ReadSingle();

                                    // Load matrix values
                                    int numOfMatrixValues = reader.ReadInt32();
                                    for (int num = 0; num < numOfMatrixValues; num++)
                                    {
                                        trackMatrixValues.Add(
                                            FileHelper.ReadMatrix(reader));
                                    }

                                    // Load checkpoint times
                                    int numOfCheckpointTimes = reader.ReadInt32();
                                    for (int num = 0; num < numOfCheckpointTimes; num++)
                                    {
                                        checkpointTimes.Add(reader.ReadSingle());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                System.Diagnostics.Trace.WriteLine("Settings Load Failure: " + exc.ToString());
            }

            FileHelper.StorageContainerMRE.Set();

            // Load if possible
            if (!replayFileFound && File.Exists(Path.Combine(
                                                    Directories.ContentDirectory, ReplayFilenames[trackNum])))
            {
                using (Stream stream = TitleContainer.OpenStream(
                           "Content\\" + ReplayFilenames[trackNum]))
                {
                    using (BinaryReader reader = new BinaryReader(stream))
                    {
                        // Load total lap time
                        lapTime = reader.ReadSingle();

                        // Load matrix values
                        int numOfMatrixValues = reader.ReadInt32();
                        for (int num = 0; num < numOfMatrixValues; num++)
                        {
                            trackMatrixValues.Add(
                                FileHelper.ReadMatrix(reader));
                        }

                        // Load checkpoint times
                        int numOfCheckpointTimes = reader.ReadInt32();
                        for (int num = 0; num < numOfCheckpointTimes; num++)
                        {
                            checkpointTimes.Add(reader.ReadSingle());
                        }
                    }
                }
            }

            if (!replayFileFound)
            {
                // Create new default replay for this track!
                // Get all track positions based on the current top highscore time!
                lapTime = Highscores.GetTopLapTime(trackNum);
                int numOfMatrixValues = 1 + (int)(lapTime / TrackMatrixIntervals);

                float lastTrackPos          = 0.0f;
                int   oldTrackSegmentNumber = 0;

                // Go twice as long and abort when we reach the finish line!
                for (int num = 0; num < numOfMatrixValues * 2; num++)
                {
                    // See Landscape.TestRenderLandscape for more details.
                    float carTrackPos = 0.00001f +
                                        ((float)num / (float)(numOfMatrixValues - 1));// *
                    //not needed, scaled in GetTrackPositionMatrix: track.Length;
                    float difference = carTrackPos - lastTrackPos;
                    carTrackPos  = lastTrackPos + difference * 0.1f;
                    lastTrackPos = carTrackPos;

                    float  roadWidth, nextRoadWidth;
                    Matrix carMatrix =
                        track.GetTrackPositionMatrix(carTrackPos,
                                                     out roadWidth, out nextRoadWidth);

                    // Store
                    trackMatrixValues.Add(carMatrix);

                    // Also check if we passed a checkpoint
                    int trackSegmentNumber =
                        (int)(carTrackPos * track.NumberOfSegments);
                    // Segment changed
                    if (trackSegmentNumber != oldTrackSegmentNumber)
                    {
                        // Check if we passed a checkpoint.
                        for (int checkpointNum = 0; checkpointNum < track.
                             CheckpointSegmentPositions.Count; checkpointNum++)
                        {
                            // We have to check if we are between the old
                            // and the current track segement numbers, we
                            // might skip one or two in 200ms.
                            if (track.CheckpointSegmentPositions[checkpointNum] >
                                oldTrackSegmentNumber &&
                                track.CheckpointSegmentPositions[checkpointNum] <=
                                trackSegmentNumber)
                            {
                                // We passed that checkpoint, add the simulated time
                                checkpointTimes.Add(
                                    lapTime * (float)num / (float)(numOfMatrixValues - 1));
                                break;
                            }
                        }
                    }
                    oldTrackSegmentNumber = trackSegmentNumber;

                    // Reached finish?
                    if (carTrackPos >= 1.0f)
                    {
                        // Then abort, do not add more.
                        break;
                    }
                }

                // Add the final checkpoint for the laptime
                checkpointTimes.Add(lapTime);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Load
        /// </summary>
        public static void Load()
        {
            bool saveImmediately = false;

            needSave = false;

            FileHelper.StorageContainerMRE.WaitOne();
            FileHelper.StorageContainerMRE.Reset();

            try
            {
                StorageDevice storageDevice = FileHelper.XnaUserDevice;
                if ((storageDevice != null) && storageDevice.IsConnected)
                {
                    using (StorageContainer container =
                               Storage.OpenContainer(storageDevice, "RacingGame"))
                    {
                        if (container.FileExists(SettingsFilename))
                        {
                            using (Stream file = container.OpenFile(SettingsFilename,
                                                                    FileMode.Create, FileAccess.Write))
                            {
                                if (file.Length > 0)
                                {
                                    GameSettings loadedGameSettings =
                                        (GameSettings) new XmlSerializer(
                                            typeof(GameSettings)).Deserialize(file);
                                    if (loadedGameSettings != null)
                                    {
                                        defaultInstance = loadedGameSettings;
                                    }
                                }
                                else
                                {
                                    // If the file is empty, just create a new file with the
                                    // default settings.
                                    needSave        = true;
                                    saveImmediately = true;
                                }
                            }
                        }
                        else
                        {
                            // Create new file after exiting
                            needSave = true;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                System.Diagnostics.Trace.WriteLine("Settings Load Failure: " + exc.ToString());
            }

            FileHelper.StorageContainerMRE.Set();

            if (saveImmediately)
            {
                Save();
            }
        }