private static Tuple <string, ShipDNA>[] ScanFolder(string foldername, CancellationToken cancel) { //NOTE: This method is running in an arbitrary thread try { List <Tuple <string, ShipDNA> > retVal = new List <Tuple <string, ShipDNA> >(); foreach (string filename in Directory.EnumerateFiles(foldername, "*.xml")) { if (cancel.IsCancellationRequested) { return(null); } try { // Try to deserialize this file as shipdna ShipDNA dna = UtilityCore.DeserializeFromFile <ShipDNA>(filename); // Success, add it retVal.Add(Tuple.Create(filename, dna)); } catch (Exception) { } } return(retVal.ToArray()); } catch (Exception) { return(null); } }
private void RebuildBell3Combo() { string foldername = UtilityCore.GetOptionsFolder(); foldername = System.IO.Path.Combine(foldername, FOLDER); string[] filenames = Directory.GetFiles(foldername, "*" + BELL3); try { _isProgramaticallyChanging = true; cboBell3.Items.Clear(); foreach (string filename in filenames) { Point[] points = UtilityCore.DeserializeFromFile <Point[]>(filename); string pointText = string.Join("\r\n", points.Select(o => string.Format("{0} {1}", o.X, o.Y))); TextBlock text = new TextBlock() { Text = string.Format("preset {0}", cboBell3.Items.Count + 1), Tag = pointText, }; cboBell3.Items.Add(text); } } finally { _isProgramaticallyChanging = false; } }
//NOTE: This only looks for the session file. If there is no session file, call AttemptNameFolder with baseFolder\name to see if that works private static ISessionOptions AttemptBaseFolder(string baseFolder, string name) { string sessionFilename = GetRootOptionsFilename(baseFolder, name); if (!File.Exists(sessionFilename)) { return(null); } return(UtilityCore.DeserializeFromFile <ISessionOptions>(sessionFilename)); }
public static ShipDNA LoadShip(out string errMsg) { string foldername = System.IO.Path.Combine(UtilityCore.GetOptionsFolder(), SHIPFOLDER); Directory.CreateDirectory(foldername); // Even if the folder is empty, they may want a folder nearby //if (!Directory.Exists(foldername) || Directory.GetFiles(foldername).Length == 0) //{ // errMsg = "No existing ships were found"; // return null; //} OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = foldername; dialog.Multiselect = false; dialog.Title = "Please select a ship"; bool?result = dialog.ShowDialog(); if (result == null || !result.Value) { errMsg = ""; return(null); } // Load the file object deserialized = UtilityCore.DeserializeFromFile <ShipDNA>(dialog.FileName); ShipDNA retVal = deserialized as ShipDNA; if (retVal == null && deserialized != null) { if (deserialized == null) { errMsg = "Couldn't deserialize file"; } else { errMsg = string.Format("File is not a ShipDNA ({0})", deserialized.GetType().ToString()); } } // Exit Function errMsg = ""; return(retVal); }
/// <summary> /// Returns the names and deserialized objects in the save folder /// </summary> /// <param name="saveFolder">Either a folder or zip file</param> public static Tuple <string, object>[] Load_Specific(string saveFolder) { if (saveFolder.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) { throw new ApplicationException("finish handling zip files"); } List <Tuple <string, object> > retVal = new List <Tuple <string, object> >(); foreach (string xmlFileName in Directory.GetFiles(saveFolder, "*.xml")) { object deserialized = UtilityCore.DeserializeFromFile(xmlFileName); retVal.Add(Tuple.Create(Path.GetFileNameWithoutExtension(xmlFileName), deserialized)); } return(retVal.ToArray()); }
public static void Load(out FlyingBeanSession session, out FlyingBeanOptions options, out ItemOptions itemOptions, out ShipDNA[] winningBeans, string baseFolder, string saveFolder) { #region Session string filename = Path.Combine(baseFolder, FILENAME_SESSION); if (File.Exists(filename)) { session = UtilityCore.DeserializeFromFile <FlyingBeanSession>(filename); } else { session = new FlyingBeanSession(); } #endregion #region FlyingBeanOptions filename = Path.Combine(saveFolder, FILENAME_OPTIONS); if (File.Exists(filename)) { options = UtilityCore.DeserializeFromFile <FlyingBeanOptions>(filename); } else { //options = new FlyingBeanOptions(); throw new ArgumentException("Didn't find " + FILENAME_OPTIONS + "\r\n\r\nThe folder might not be a valid save folder"); } #endregion // This currently isn't stored to file session = new FlyingBeanSession(); #region ItemOptions filename = Path.Combine(saveFolder, FILENAME_ITEMOPTIONS); if (File.Exists(filename)) { itemOptions = UtilityCore.DeserializeFromFile <ItemOptions>(filename); } else { itemOptions = new ItemOptions(); } #endregion #region Winning Beans List <Tuple <string, ShipDNA> > winners = new List <Tuple <string, ShipDNA> >(); foreach (string filename2 in Directory.GetFiles(saveFolder)) { Match match = Regex.Match(Path.GetFileName(filename2), @"^(.+?( - )){2}(?<rank>\d+)\.xml$", RegexOptions.IgnoreCase); if (!match.Success) { continue; } // All other xml files should be winning beans ShipDNA dna = null; try { dna = UtilityCore.DeserializeFromFile <ShipDNA>(filename2); } catch (Exception) { // Must not be a ship continue; } winners.Add(Tuple.Create(Path.GetFileName(filename2), dna)); } winningBeans = winners.Select(o => o.Item2).ToArray(); #endregion // WinnersFinal options.WinnersFinal = MergeHistory(options.WinningScores, winners, options.TrackingMaxLineagesFinal, options.TrackingMaxPerLineageFinal); }
private Tuple <bool, string> TryLoadSession(string name = "") { #region Find file //C:\Users\<username>\AppData\Roaming\Asteroid Miner\EncogOCR\ string foldername = UtilityCore.GetOptionsFolder(); foldername = System.IO.Path.Combine(foldername, FOLDER); if (!Directory.Exists(foldername)) { return(Tuple.Create(false, "Saves folder doesn't exist")); } string[] filenames; if (string.IsNullOrEmpty(name)) { filenames = Directory.GetFiles(foldername); } else { filenames = Directory.GetFiles(foldername, "*" + name + "*"); } string filename = filenames. OrderByDescending(o => o). // the files all start with a date same (ymdhmsf), so the last file alphabetically will be the latest FirstOrDefault(); if (filename == null) { return(Tuple.Create(false, "Couldn't find session")); } #endregion #region Deserialize session EncogOCR_Session session = null; try { session = UtilityCore.DeserializeFromFile <EncogOCR_Session>(filename); } catch (Exception ex) { return(Tuple.Create(false, ex.Message)); } #endregion #region Load session _isLoadingSession = true; ClearEverything(); _pixels = session.ImageSize; txtPixels.Text = _pixels.ToString(); if (session.Sketches != null) { foreach (var sketch in session.Sketches) { sketch.GenerateBitmap(session.ImageSize); sketch.GenerateImageControl(); AddSketch(sketch); } } _isLoadingSession = false; #endregion // The combo should already have this, be just making sure LoadSessionsCombobox(); cboSession.Text = GetSessionNameFromFilename(filename); #region Redraw canvasInk.Strokes.Clear(); RedrawSmallImage(); //TODO: When the NN gets serialized/deserialized, turn this off RedrawPreviousImages(true); #endregion return(Tuple.Create(true, "")); }
public bool TryLoadLastSave(bool useCurrentSessionIfSet) { string saveFolder; #region Find latest save folder if (this.SessionFolder == null || !useCurrentSessionIfSet) { //C:\Users\<username>\AppData\Roaming\Asteroid Miner\Flying Beans\ string baseFolder = UtilityCore.GetOptionsFolder(); string beanFolder = System.IO.Path.Combine(baseFolder, FlyingBeansWindow.OPTIONSFOLDER); if (!Directory.Exists(beanFolder)) { // Not even this folder exists, there won't be any saves return(false); } string sessionFolderName = null; // See if the session file is here string sessionFilename = System.IO.Path.Combine(baseFolder, FlyingBeanSession.FILENAME_SESSION); if (File.Exists(sessionFilename)) { try { FlyingBeanSession session = UtilityCore.DeserializeFromFile <FlyingBeanSession>(sessionFilename); if (Directory.Exists(session.SessionFolder)) // it may have been deleted { sessionFolderName = session.SessionFolder; } } catch (Exception) { } } if (sessionFolderName == null) { string[] sessionFolders = Directory.GetDirectories(beanFolder, FlyingBeansWindow.SESSIONFOLDERPREFIX + " *").OrderByDescending(o => o).ToArray(); if (sessionFolders == null || sessionFolders.Length == 0) { // There are no session folders return(false); } sessionFolderName = sessionFolders[0]; } saveFolder = FindLatestSaveFolder(sessionFolderName); if (saveFolder == null) { // Didn't find a save folder return(false); } // Remember the session folder this.SessionFolder = sessionFolderName; } else { // Use the current session saveFolder = FindLatestSaveFolder(this.SessionFolder); if (saveFolder == null) { return(false); } } #endregion Load(saveFolder); return(true); }