示例#1
0
        public static bool Save(ObservableCollection <Villager> villagersCollection)
        {
            try
            {
                if (!File.Exists(DATA_FILENAME))
                {
                    StardewValleyVillagersData stardewValleyVillagersData = new StardewValleyVillagersData
                    {
                        Villagers = new List <Villager_Save>()
                    };

                    foreach (var villager in villagersCollection)
                    {
                        stardewValleyVillagersData.Villagers.Add(new Villager_Save
                        {
                            Address   = villager.Address,
                            AvatarUrl = villager.AvatarUrl,
                            BestGifts = villager.BestGifts,
                            Birthday  = villager.Birthday,
                            Gender    = villager.Gender,
                            LivesIn   = villager.LivesIn,
                            Marriage  = villager.Marriage,
                            Name      = villager.Name
                        });
                    }

                    using (FileStream writerFileStream = new FileStream(DATA_FILENAME, FileMode.Create, FileAccess.Write))
                    {
                        BinaryFormatter.Serialize(writerFileStream, stardewValleyVillagersData);

                        writerFileStream.Close();
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to write to file:\n\n" + ex.Message);
                return(false);
            }
        }
示例#2
0
        public static ObservableCollection <Villager> Load()
        {
            ObservableCollection <Villager> villagers = null;
            StardewValleyVillagersData      dataStore = null;

            if (File.Exists(DATA_FILENAME) && Security.HasAccess(new FileInfo(DATA_FILENAME), System.Security.AccessControl.FileSystemRights.Write))
            {
                try
                {
                    using (FileStream readerFileStream = new FileStream(DATA_FILENAME, FileMode.Open, FileAccess.Read))
                    {
                        dataStore = (StardewValleyVillagersData)BinaryFormatter.Deserialize(readerFileStream);
                        readerFileStream.Close();
                    }

                    villagers = new ObservableCollection <Villager>();

                    foreach (var villager in dataStore.Villagers)
                    {
                        villagers.Add(new Villager(villager.Name)
                        {
                            Address   = villager.Address,
                            AvatarUrl = villager.AvatarUrl,
                            BestGifts = villager.BestGifts,
                            Birthday  = villager.Birthday,
                            Gender    = villager.Gender,
                            LivesIn   = villager.LivesIn,
                            Marriage  = villager.Marriage
                        });
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to load data:\n\n" + ex.Message);
                }
            }

            return(villagers);
        }