Example #1
0
        public static void SaveShip(ShipDNA ship)
        {
            // Make sure the folder exists
            string foldername = System.IO.Path.Combine(UtilityCore.GetOptionsFolder(), SHIPFOLDER);

            if (!Directory.Exists(foldername))
            {
                Directory.CreateDirectory(foldername);
            }

            int infiniteLoopDetector = 0;

            while (true)
            {
                char[] illegalChars = System.IO.Path.GetInvalidFileNameChars();
                string filename     = new string(ship.ShipName.Select(o => illegalChars.Contains(o) ? '_' : o).ToArray());
                filename = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.fff") + " - " + filename + ".xml";
                filename = System.IO.Path.Combine(foldername, filename);

                try
                {
                    UtilityCore.SerializeToFile(filename, ship);
                    break;
                }
                catch (IOException ex)
                {
                    infiniteLoopDetector++;
                    if (infiniteLoopDetector > 100)
                    {
                        throw new ApplicationException("Couldn't create the file\r\n" + filename, ex);
                    }
                }
            }
        }
Example #2
0
        private void btnSaveSession_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EncogOCR_Session session = new EncogOCR_Session()
                {
                    ImageSize = _pixels,
                    Sketches  = _prevSketches.ToArray(),
                };

                //C:\Users\<username>\AppData\Roaming\Asteroid Miner\EncogOCR\
                string foldername = UtilityCore.GetOptionsFolder();
                foldername = System.IO.Path.Combine(foldername, FOLDER);
                Directory.CreateDirectory(foldername);

                string filename = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.fff - ");
                filename += cboSession.Text + ".xml";       //TODO: convert illegal chars
                filename  = System.IO.Path.Combine(foldername, filename);

                UtilityCore.SerializeToFile(filename, session);

                LoadSessionsCombobox();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #3
0
        private void SaveBell3Map_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Point[] points = GetBellPoints(txtBell3.Text);
                if (points == null)
                {
                    MessageBox.Show("Invalid control points", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                //C:\Users\<username>\AppData\Roaming\Asteroid Miner\NonlinearRandom\
                string foldername = UtilityCore.GetOptionsFolder();
                foldername = System.IO.Path.Combine(foldername, FOLDER);
                Directory.CreateDirectory(foldername);

                string filename = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.fff - ");
                filename += BELL3;
                filename  = System.IO.Path.Combine(foldername, filename);

                UtilityCore.SerializeToFile(filename, points);

                RebuildBell3Combo();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #4
0
        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;
            }
        }
Example #5
0
 private void SettingsFolder_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         // Open the settings folder in windows explorer
         System.Diagnostics.Process.Start(UtilityCore.GetOptionsFolder());
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Example #6
0
        private void Load(string saveFolder)
        {
            string baseFolder = UtilityCore.GetOptionsFolder();

            FlyingBeanSession session;
            FlyingBeanOptions options;
            ItemOptions       itemOptions;

            ShipDNA[] winningBeans;
            FlyingBeanSession.Load(out session, out options, out itemOptions, out winningBeans, baseFolder, saveFolder);

            FinishLoad(session, options, itemOptions, winningBeans, saveFolder, false);
        }
Example #7
0
        private void LoadSessionsCombobox()
        {
            const string DEFAULT = "session";

            string foldername = UtilityCore.GetOptionsFolder();

            foldername = System.IO.Path.Combine(foldername, FOLDER);

            string current = cboSession.Text;

            cboSession.Items.Clear();

            if (string.IsNullOrEmpty(current))
            {
                current = DEFAULT;
            }

            if (!Directory.Exists(foldername))
            {
                cboSession.Items.Add(DEFAULT);
                cboSession.Text = current;
                return;
            }

            string[] names = Directory.GetFiles(foldername).
                             Select(o => GetSessionNameFromFilename(o)).
                             Where(o => !o.Equals(DEFAULT, StringComparison.OrdinalIgnoreCase)).
                             Distinct((o, n) => o.Equals(n, StringComparison.OrdinalIgnoreCase)).
                             OrderBy(o => o).
                             ToArray();

            if (names.Length == 0)
            {
                cboSession.Items.Add(DEFAULT);
            }
            else
            {
                foreach (string name in names)
                {
                    cboSession.Items.Add(name);
                }

                // Just let it stay DEFAULT.  That's no better than picking a random valid name
                //if(current == DEFAULT && !names.Any(o => o == DEFAULT))
                //{
                //    current = names[0];
                //}
            }

            cboSession.Text = current;
        }
Example #8
0
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //C:\Users\<username>\AppData\Roaming\Asteroid Miner\Miner2D\
                string foldername = UtilityCore.GetOptionsFolder();
                foldername = System.IO.Path.Combine(foldername, FOLDER);
                Directory.CreateDirectory(foldername);

                string filename = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.fff - ");

                if (_object_LinearVelocity != null)
                {
                    filename += "LinearVelocity.xml";
                    filename  = System.IO.Path.Combine(foldername, filename);

                    UtilityCore.SerializeToFile(filename, _object_LinearVelocity);
                }
                else if (_object_LinearForce != null)
                {
                    filename += "LinearForce.xml";
                    filename  = System.IO.Path.Combine(foldername, filename);

                    UtilityCore.SerializeToFile(filename, _object_LinearForce);
                }
                else if (_object_OrientationVelocity != null)
                {
                    filename += "OrientationVelocity.xml";
                    filename  = System.IO.Path.Combine(foldername, filename);

                    UtilityCore.SerializeToFile(filename, _object_OrientationVelocity);
                }
                else if (_object_OrientationForce != null)
                {
                    filename += "OrientationForce.xml";
                    filename  = System.IO.Path.Combine(foldername, filename);

                    UtilityCore.SerializeToFile(filename, _object_OrientationForce);
                }
                else
                {
                    MessageBox.Show("There were no settings to export", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #9
0
        private void OpenFolder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //C:\Users\<username>\AppData\Roaming\Asteroid Miner\Miner2D\
                string foldername = UtilityCore.GetOptionsFolder();
                foldername = System.IO.Path.Combine(foldername, FOLDER);
                Directory.CreateDirectory(foldername);

                System.Diagnostics.Process.Start(foldername);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #10
0
        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);
        }
Example #11
0
        public static string EnsureShipFolderExists(string subfolder)
        {
            // Start with the main ship window
            string retVal = System.IO.Path.Combine(UtilityCore.GetOptionsFolder(), ShipEditorWindow.SHIPFOLDER);

            // If requested, go to a subfolder
            if (!string.IsNullOrEmpty(subfolder))
            {
                retVal = System.IO.Path.Combine(retVal, subfolder);
            }

            // Make sure it exists
            if (!Directory.Exists(retVal))
            {
                Directory.CreateDirectory(retVal);
            }

            // Exit Function
            return(retVal);
        }
Example #12
0
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();

                if (this.SessionFolder != null)
                {
                    dialog.SelectedPath = this.SessionFolder;
                }
                else
                {
                    string baseFolder = UtilityCore.GetOptionsFolder();

                    baseFolder = System.IO.Path.Combine(baseFolder, FlyingBeansWindow.OPTIONSFOLDER);
                    if (Directory.Exists(baseFolder))
                    {
                        dialog.SelectedPath = baseFolder;
                    }
                }

                dialog.Description = "Select save folder";
                if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                try
                {
                    Load(dialog.SelectedPath);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message, MSGBOXCAPTION, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), MSGBOXCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #13
0
        private static void LogHit(double mass1, double mass2, double speed, double energy)
        {
            // When you have a bunch of these files, go to command prompt,
            //      type: copy * all.txt
            //      then copy the contents into excel, chart away

            string folder = UtilityCore.GetOptionsFolder();

            folder = System.IO.Path.Combine(folder, "Logs");

            System.IO.Directory.CreateDirectory(folder);

            string filename = "collision " + Guid.NewGuid().ToString() + ".txt";

            filename = System.IO.Path.Combine(folder, filename);

            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(new System.IO.FileStream(filename, System.IO.FileMode.CreateNew)))
            {
                writer.WriteLine(string.Format("mass1, mass2, speed, energy\t{0}\t{1}\t{2}\t{3}", mass1, mass2, speed, energy));
            }
        }
Example #14
0
        private string GetNewSaveFolder(string name)
        {
            //C:\Users\username\AppData\Roaming\Asteroid Miner\Flying Beans\Session 2013-02-16 15.20.00.000\Save 2013-02-16 15.20.000

            string timestamp = DateTime.Now.ToString(TIMESTAMPFORMAT);

            // Session folder
            if (this.SessionFolder == null)
            {
                string baseFolder = UtilityCore.GetOptionsFolder();
                baseFolder = System.IO.Path.Combine(baseFolder, FlyingBeansWindow.OPTIONSFOLDER);
                if (!Directory.Exists(baseFolder))
                {
                    Directory.CreateDirectory(baseFolder);
                }

                this.SessionFolder = System.IO.Path.Combine(baseFolder, FlyingBeansWindow.SESSIONFOLDERPREFIX + " " + timestamp);
            }

            if (!Directory.Exists(this.SessionFolder))
            {
                Directory.CreateDirectory(this.SessionFolder);
            }

            // Save folder
            //NOTE: This class doesn't store the current save folder, just the parent session
            string retVal = System.IO.Path.Combine(this.SessionFolder, FlyingBeansWindow.SAVEFOLDERPREFIX + " " + timestamp);

            if (!string.IsNullOrEmpty(name))
            {
                retVal += " " + FlyingBeanSession.EscapeFilename(name);
            }

            Directory.CreateDirectory(retVal);

            // Exit Function
            return(retVal);
        }
Example #15
0
        public void Save(bool async, bool pruneAutosavesAfter, string name)
        {
            const int MAXAUTOSAVES = 8;

            string baseFolder = UtilityCore.GetOptionsFolder();
            string saveFolder = GetNewSaveFolder(name);

            FlyingBeanSession session       = this.Session ?? new FlyingBeanSession();
            FlyingBeanOptions options       = this.Options;
            ItemOptions       itemOptions   = this.ItemOptions;
            string            sessionFolder = this.SessionFolder;

            // It doesn't matter what folder was stored there before, make sure it holds where the last save is
            session.SessionFolder = sessionFolder;

            if (async)
            {
                Task.Factory.StartNew(() =>
                {
                    FlyingBeanSession.Save(baseFolder, saveFolder, session, options, itemOptions);

                    if (pruneAutosavesAfter)
                    {
                        PruneAutosaves(sessionFolder, MAXAUTOSAVES);
                    }
                });
            }
            else
            {
                FlyingBeanSession.Save(baseFolder, saveFolder, session, options, itemOptions);

                if (pruneAutosavesAfter)
                {
                    PruneAutosaves(sessionFolder, MAXAUTOSAVES);
                }
            }
        }
Example #16
0
        /// <summary>
        /// This overload uses the standard base folder
        /// </summary>
        public static SessionFolderResults Load(string name, Func <ISessionOptions> getSession = null)
        {
            string baseFolder = UtilityCore.GetOptionsFolder();

            return(Load(baseFolder, name));
        }
Example #17
0
        /// <summary>
        /// This overload uses the standard base folder
        /// </summary>
        public static void Save(string name, ISessionOptions session, IEnumerable <Tuple <string, object> > saveFiles, bool zipped)
        {
            string baseFolder = UtilityCore.GetOptionsFolder();

            Save(baseFolder, name, session, saveFiles, zipped);
        }
Example #18
0
        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, ""));
        }
Example #19
0
        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);
        }