public void ArchiveGameStateOfPreviousGame(GameState newState) { if (IsInDebugMode && LastGameStateOfPreviousGame != null) { /* Save the previous game state, as it was the end of the last game: */ Guid gameGuid = Guid.NewGuid(); string fileName = String.Format("GameState.{0}.ext", gameGuid); string historyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string historyFolderName = String.Format("{0}History", Solver.GetType().Name); historyFolder = Path.Combine(historyFolder, historyFolderName); if (!Directory.Exists(historyFolder)) { Directory.CreateDirectory(historyFolder); } string BinaryGameStateFilePath = Path.ChangeExtension(Path.Combine(historyFolder, fileName), ".bin"); string XmlGameStateFilePath = Path.ChangeExtension(Path.Combine(historyFolder, fileName), ".xml"); LastGameStateOfPreviousGame.SaveGameState(BinaryGameStateFilePath, FileType.Binary); LastGameStateOfPreviousGame.SaveGameState(XmlGameStateFilePath, FileType.Xml); } }
/// <summary> /// The StartTime property should be set before calling Run() (unless IgnoreTimer is set to true). /// </summary> public void Run(IEnumerable <RawCellData> cells) { /* Determine file paths: */ string exeFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); BinaryGameStateFilePath = Path.Combine(exeFolder, "GameState.bin"); XmlGameStateFilePath = Path.Combine(exeFolder, "GameState.xml"); /* Determine previous game state (this will be saved when a new game starts): */ if (File.Exists(BinaryGameStateFilePath)) { LastGameStateOfPreviousGame = GameState.LoadGameState(BinaryGameStateFilePath, FileType.Binary); } else if (File.Exists(XmlGameStateFilePath)) { LastGameStateOfPreviousGame = GameState.LoadGameState(XmlGameStateFilePath, FileType.Xml); } else { LastGameStateOfPreviousGame = null; } /* Backup existing files: */ if (IsInDebugMode) { if (File.Exists(BinaryGameStateFilePath)) { string backupFileName = Path.ChangeExtension(BinaryGameStateFilePath, ".bak.bin"); File.Copy(BinaryGameStateFilePath, backupFileName, true /*overWrite*/); } if (File.Exists(XmlGameStateFilePath)) { string backupFileName = Path.ChangeExtension(XmlGameStateFilePath, ".bak.xml"); File.Copy(XmlGameStateFilePath, backupFileName, true /*overWrite*/); } } /* Create the initial game state: */ CurrentGameState = LastGameStateOfPreviousGame == null ? new GameState() : LastGameStateOfPreviousGame.Clone(); /* Run the solver: */ CurrentGameState.NewGameDetected += ArchiveGameStateOfPreviousGame; try { /* Set up the new game state: */ CurrentGameState.LoadRawCellData(cells); Run(); } finally { /* Ensure no memory leaks due to dangling events: */ CurrentGameState.NewGameDetected -= ArchiveGameStateOfPreviousGame; } }