Ejemplo n.º 1
0
        //TODO: Rework this class to use SessionSaveLoad.cs
        public static void Save(string baseFolder, string saveFolder, FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions)
        {
            // Session
            //NOTE: This is in the base folder
            UtilityCore.SerializeToFile(Path.Combine(baseFolder, FILENAME_SESSION), session);

            // ItemOptions
            UtilityCore.SerializeToFile(Path.Combine(saveFolder, FILENAME_ITEMOPTIONS), itemOptions);

            // Options
            FlyingBeanOptions optionCloned = UtilityCore.Clone(options);

            optionCloned.DefaultBeanList = null;                // this is programatically generated, no need to save it
            //optionCloned.NewBeanList		//NOTE: These will be serialized with the options file

            SortedList <string, ShipDNA> winningFilenames;
            SortedList <string, double>  maxScores;

            ExtractHistory(out winningFilenames, out maxScores, options.WinnersFinal);          // can't use cloned.winners, that property is skipped when serializing
            optionCloned.WinningScores = maxScores;

            // Main class
            UtilityCore.SerializeToFile(Path.Combine(saveFolder, FILENAME_OPTIONS), optionCloned);

            // Winning beans
            if (winningFilenames != null)
            {
                foreach (string beanFile in winningFilenames.Keys)
                {
                    UtilityCore.SerializeToFile(Path.Combine(saveFolder, beanFile), winningFilenames[beanFile]);
                }
            }
        }
Ejemplo n.º 2
0
        public PanelFile(string sessionFolder, FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions, SortedList <string, ShipDNA> defaultBeanList)
        {
            InitializeComponent();

            _defaultBeanList = defaultBeanList;

            this.SessionFolder = sessionFolder;
            this.Session       = session;
            this.Options       = options;
            this.ItemOptions   = itemOptions;

            _isInitialized = true;
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Get the best of each bean type
                var topBeans = GetTopBeans();
                if (topBeans == null || topBeans.Length == 0)
                {
                    MessageBox.Show("There are no high scoring beans yet", MSGBOXCAPTION, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                //TODO: May want to ask the user to pick one

                // Make sure the folder exists
                string foldername = PanelBeanTypes.EnsureShipFolderExists(BEANSUBFOLDER);

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

                // Write a file for each
                foreach (var bean in topBeans)
                {
                    if (bean.Item2 == null)
                    {
                        throw new ApplicationException("ShipDNA should never be null from the finals list");
                    }

                    // Build up filename
                    string filename = timestamp + " - " + bean.Item1 + " - " + Math.Round(bean.Item3, 1).ToString();
                    if (txtExportName.Text != "")
                    {
                        filename += " (" + FlyingBeanSession.EscapeFilename(txtExportName.Text) + ")";
                    }

                    filename += ".xml";

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

                    // Write it
                    UtilityCore.SerializeToFile(filename, bean.Item2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), MSGBOXCAPTION, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
                }
            }
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        private void FinishLoad(FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions, ShipDNA[] winningBeans, string saveFolder, bool startEmpty)
        {
            // Manually instantiate some of the properties that didn't get serialized
            options.DefaultBeanList = _defaultBeanList;

            if (options.NewBeanList == null)            // if this was called by new, it will still be null
            {
                options.NewBeanList = new SortedList <string, ShipDNA>();

                if (!startEmpty)
                {
                    string[] beanKeys = options.DefaultBeanList.Keys.ToArray();

                    foreach (int keyIndex in UtilityCore.RandomRange(0, beanKeys.Length, Math.Min(3, beanKeys.Length)))         // only do 3 of the defaults
                    {
                        string key = beanKeys[keyIndex];
                        options.NewBeanList.Add(key, options.DefaultBeanList[key]);
                    }
                }
            }

            options.MutateArgs = PanelMutation.BuildMutateArgs(options);

            options.GravityField = new GravityFieldUniform();
            options.Gravity      = options.Gravity;     // the property set modifies the gravity field

            options.WinnersLive = new WinnerList(true, options.TrackingMaxLineagesLive, options.TrackingMaxPerLineageLive);

            if (options.WinnersFinal == null)           // if a previous save had winning ships, this will already be loaded with them
            {
                options.WinnersFinal = new WinnerList(false, options.TrackingMaxLineagesFinal, options.TrackingMaxPerLineageFinal);
            }

            options.WinnerCandidates = new CandidateWinners();
            // These are already in the final list, there's no point in putting them in the candidate list as well
            //if (winningBeans != null)
            //{
            //    foreach (ShipDNA dna in winningBeans)
            //    {
            //        options.WinnerCandidates.Add(dna);
            //    }
            //}

            // Make sure the session folder is up to date
            if (saveFolder == null)
            {
                this.SessionFolder = null;
            }
            else
            {
                string dirChar = Regex.Escape(System.IO.Path.DirectorySeparatorChar.ToString());

                string pattern = dirChar + Regex.Escape(FlyingBeansWindow.SESSIONFOLDERPREFIX) + "[^" + dirChar + "]+(?<upto>" + dirChar + ")" + Regex.Escape(FlyingBeansWindow.SAVEFOLDERPREFIX);
                Match  match   = Regex.Match(saveFolder, pattern, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    this.SessionFolder = saveFolder.Substring(0, match.Groups["upto"].Index);           // the session folder is everything before the save subfolder
                }
                else
                {
                    // They may have chosen a folder that they unziped onto their desktop, or wherever.  Leaving this null so that if they hit save, it will be saved
                    // in the appropriate location
                    this.SessionFolder = null;
                }
            }

            // Swap out the settings
            this.Session     = session;
            this.Options     = options;
            this.ItemOptions = itemOptions;

            // Inform the world
            if (this.SessionChanged != null)
            {
                this.SessionChanged(this, new EventArgs());
            }
        }
Ejemplo n.º 9
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);
        }