public static void Auto_Load_Game() { try { string FolderName = Path.Combine(Directory.GetCurrentDirectory(), "Saves"); List <string> filesnames = Directory.GetFiles(FolderName).ToList(); var start = new DateTime(1970, 1, 1, 0, 0, 0); var fileToLoad = ""; foreach (var filename in filesnames) { if (File.GetCreationTime(filename) > start) { fileToLoad = filename; start = File.GetCreationTime(filename); } } if (String.IsNullOrEmpty(fileToLoad)) { throw new Exception(); } StreamReader saveGetting = new StreamReader(fileToLoad); using (saveGetting) { var loadedSave = JsonConvert.DeserializeObject <Save>(saveGetting.ReadToEnd(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); switch (loadedSave.savedStage) { case 0: case 1: new Tutorial(loadedSave.savedCharacter); break; } } } catch { Console.WriteLine("There are no saves in the directory"); MainFunctions.MainMenu(); } }
public static void Load_Game() { List <string> filesnames = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "Saves")).ToList(); if (filesnames.Count > 0) { foreach (string name in filesnames) { StreamReader SaveGetting = new StreamReader(name); using (SaveGetting) { var shortFileName = Regex.Match(name, @"[0-9]{1,}.dns"); shortFileName = Regex.Match(name, @"[0-9]{1,}"); var loadedSave = JsonConvert.DeserializeObject <Save>(SaveGetting.ReadToEnd(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); Console.WriteLine("Number: {0}", shortFileName); Console.WriteLine("Stage: {0}", loadedSave.savedStage); Console.WriteLine("Character: {0}", loadedSave.savedCharacter.name); Console.WriteLine(); Console.WriteLine(); } } Console.WriteLine("Write the number of save you want to load"); string ReadableSave = Console.ReadLine(); if (File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "Saves", ReadableSave + ".dns"))) { StreamReader SaveGetting = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), "Saves", ReadableSave + ".dns")); using (SaveGetting) { var loadedSave = JsonConvert.DeserializeObject <Save>(SaveGetting.ReadToEnd(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); switch (loadedSave.savedStage) { case 0: case 1: new Tutorial(loadedSave.savedCharacter); break; } } } else { while (!(File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "Saves", ReadableSave + ".dns")))) { Console.WriteLine("Wrong file name. Insert once more"); ReadableSave = Console.ReadLine(); } StreamReader SaveGetting = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), "Saves", ReadableSave + ".dns")); using (SaveGetting) { var loadedSave = JsonConvert.DeserializeObject <Save>(SaveGetting.ReadToEnd(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); switch (loadedSave.savedStage) { case 0: case 1: new Tutorial(loadedSave.savedCharacter); break; } } } } else { Console.WriteLine("There are no saves in the directory"); MainFunctions.MainMenu(); } }