public bool SessionMatches(GameSessionInfo other)
 {
     return(other.SessionType == SessionType && other.UniqueId == UniqueId);
 }
        /// <summary>
        /// Clears the in-game saved games directory by moving saves
        /// to sub-directories.
        /// </summary>
        public static void CheckForSavesInMainSaveDirectory()
        {
            if (!Directory.Exists(ProgramConstants.GamePath + SavedGamesDirectory))
            {
                return;
            }

            string[] saveFiles = Directory.GetFiles(ProgramConstants.GamePath + SavedGamesDirectory, "*.SAV");

            string unknownSaveDirPath = ProgramConstants.GamePath + SavedGamesDirectory + "/Unknown/";

            Directory.CreateDirectory(unknownSaveDirPath);
            foreach (string save in saveFiles)
            {
                string metaFilePath = Path.ChangeExtension(save, SavedGameMetaExtension);

                string destination = unknownSaveDirPath + Path.GetFileName(save);
                bool   isUnknown   = true;

                if (File.Exists(metaFilePath))
                {
                    GameSessionInfo meta = GameSessionInfo.ParseFromFile(metaFilePath);

                    if (meta != null && meta.SessionType != GameSessionType.UNKNOWN)
                    {
                        isUnknown = false;
                        string uniqueIdString = meta.UniqueId.ToString(CultureInfo.InvariantCulture);
                        if (Path.GetFileName(save).StartsWith("AUTOSAVE"))
                        {
                            destination = ProgramConstants.GamePath + SavedGamesDirectory + "/" + AutoSavesDirectoryName + "/" + Path.GetFileName(save);
                            Logger.Log($"Moving saved game {Path.GetFileName(save)} to auto-saves directory.");
                        }
                        else
                        {
                            destination = ProgramConstants.GamePath + SavedGamesDirectory + "/" + uniqueIdString + "/" + Path.GetFileName(save);
                            Logger.Log($"Moving saved game {Path.GetFileName(save)} to {uniqueIdString} saves directory.");
                        }

                        string metaFileDestination = Path.ChangeExtension(destination, SavedGameMetaExtension);
                        Directory.CreateDirectory(Path.GetDirectoryName(metaFileDestination));
                        File.Delete(metaFileDestination);
                        File.Move(metaFilePath, metaFileDestination);
                    }
                }

                if (isUnknown)
                {
                    Logger.Log("Moving saved game " + Path.GetFileName(save) + " to UNKNOWN saves directory.");
                }

                var random = new Random();
                while (File.Exists(destination))
                {
                    // Add random numbers to file name instead of letting the client crash
                    destination = Path.GetDirectoryName(destination) + "/" + Path.GetFileNameWithoutExtension(destination) + random.Next(0, 10).ToString(CultureInfo.InvariantCulture) + ".SAV";
                }

                File.Move(save, destination);
            }

            string[] mpSaveFiles   = Directory.GetFiles(ProgramConstants.GamePath + SavedGamesDirectory, "*.NET");
            string   mpSaveDirPath = ProgramConstants.GamePath + MultiplayerSaveGameManager.SAVED_GAMES_MP_DIRECTORY;

            Directory.CreateDirectory(mpSaveDirPath);
            foreach (string save in mpSaveFiles)
            {
                Logger.Log("Moving saved game " + Path.GetFileName(save) + " to MULTIPLAYER saves directory.");
                File.Move(save, mpSaveDirPath + "/" + Path.GetFileName(save));
            }

            string spawnSGIniPath = ProgramConstants.GamePath + SavedGamesDirectory + "/" + MultiplayerSaveGameManager.SPAWN_INI_NAME;

            if (File.Exists(spawnSGIniPath))
            {
                Logger.Log($"Moving { MultiplayerSaveGameManager.SPAWN_INI_NAME } to MULTIPLAYER saves directory.");
                File.Move(spawnSGIniPath, ProgramConstants.GamePath + MultiplayerSaveGameManager.SAVED_GAMES_MP_DIRECTORY + "/" + MultiplayerSaveGameManager.SPAWN_INI_NAME);
            }
        }
        public void StartSession()
        {
            Logger.Log("Starting game session.");

            gameSaved = false;

            // Move possible saved games of this session to the main saved games directory

            // Build a list of save files to move
            List <string> saveFiles = null;

            if (SessionType == GameSessionType.MULTIPLAYER)
            {
                if (Directory.Exists(ProgramConstants.GamePath + MultiplayerSaveGameManager.SAVED_GAMES_MP_DIRECTORY))
                {
                    saveFiles = Directory.GetFiles(ProgramConstants.GamePath + MultiplayerSaveGameManager.SAVED_GAMES_MP_DIRECTORY, "*.NET").ToList();
                }
            }
            else if (SessionType == GameSessionType.UNKNOWN)
            {
                string saveDirPath = ProgramConstants.GamePath + SavedGamesDirectory + "/Unknown";
                if (Directory.Exists(saveDirPath))
                {
                    saveFiles = Directory.GetFiles(saveDirPath, "*.SAV").ToList();
                }
            }
            else
            {
                string saveDirPath = ProgramConstants.GamePath + SavedGamesDirectory + "/" + UniqueId.ToString(CultureInfo.InvariantCulture);
                if (Directory.Exists(saveDirPath))
                {
                    saveFiles = Directory.GetFiles(saveDirPath, "*.SAV").ToList();
                }
            }

            if (SessionType != GameSessionType.MULTIPLAYER)
            {
                // Check for auto-saves

                string autoSaveDirectoryPath = ProgramConstants.GamePath + SavedGamesDirectory + "/" + AutoSavesDirectoryName;
                if (Directory.Exists(autoSaveDirectoryPath))
                {
                    string[] autoSaveFiles = Directory.GetFiles(autoSaveDirectoryPath, "*.SAV");

                    if (saveFiles == null)
                    {
                        saveFiles = new List <string>();
                    }

                    saveFiles.AddRange(autoSaveFiles);
                }
            }

            // Move the files and any potential meta files
            if (saveFiles != null && saveFiles.Count > 0)
            {
                Logger.Log("Moving up to " + saveFiles.Count + " save files from sub-directories to main saved games directory.");

                try
                {
                    foreach (string savePath in saveFiles)
                    {
                        string metaFilePath = Path.ChangeExtension(savePath, SavedGameMetaExtension);
                        if (File.Exists(metaFilePath))
                        {
                            var meta = GameSessionInfo.ParseFromFile(metaFilePath);

                            if (meta == null || !meta.SessionMatches(SessionInfo))
                            {
                                continue;
                            }

                            File.Move(metaFilePath, ProgramConstants.GamePath + SavedGamesDirectory + "/" + Path.GetFileName(metaFilePath));
                        }

                        Logger.Log("Moving save " + savePath.Substring(ProgramConstants.GamePath.Length));

                        File.Move(savePath, ProgramConstants.GamePath + SavedGamesDirectory + "/" + Path.GetFileName(savePath));
                    }
                }
                catch (IOException ex)
                {
                    Logger.Log("FAILED to move saved games of session to in-game saved game directory: " + ex.Message);
                }
            }
            else
            {
                Logger.Log("Previous saved games not detected for current session.");
            }

            // Only set up the file system watcher in multiplayer games
            if (SessionType == GameSessionType.MULTIPLAYER)
            {
                if (fileSystemWatcher != null)
                {
                    fileSystemWatcher.Dispose();
                }

                string filter = SessionType == GameSessionType.MULTIPLAYER ? "*.NET" : "*.SAV";

                fileSystemWatcher                     = new FileSystemWatcher(ProgramConstants.GamePath + "Saved Games", filter);
                fileSystemWatcher.Created            += FileSystemWatcher_Event;
                fileSystemWatcher.Changed            += FileSystemWatcher_Event;
                fileSystemWatcher.EnableRaisingEvents = true;
            }
        }
 public GameSessionManager(GameSessionInfo sessionInfo, Action <Delegate, object[]> callbackAction)
 {
     SessionInfo         = sessionInfo;
     this.callbackAction = callbackAction;
 }