public static async Task <SettingsV2Model> UpgradeSettingsToLatest(string filePath) { int currentVersion = await GetSettingsVersion(filePath); if (currentVersion < 0) { // Settings file is invalid, we can't use this return(null); } else if (currentVersion > SettingsV2Model.LatestVersion) { // Future build, like a preview build, we can't load this return(null); } else if (currentVersion < SettingsV2Model.LatestVersion) { // Perform upgrade of settings if (currentVersion < 41) { await SettingsV2Upgrader.Version41Upgrade(filePath); } if (currentVersion < 42) { await SettingsV2Upgrader.Version42Upgrade(filePath); } if (currentVersion < 43) { await SettingsV2Upgrader.Version43Upgrade(filePath); } if (currentVersion < 44) { await SettingsV2Upgrader.Version44Upgrade(filePath); } } SettingsV2Model settings = await FileSerializerHelper.DeserializeFromFile <SettingsV2Model>(filePath, ignoreErrors : true); settings.Version = SettingsV2Model.LatestVersion; return(settings); }
public static async Task Version42Upgrade(string filePath) { SettingsV2Model settings = await FileSerializerHelper.DeserializeFromFile <SettingsV2Model>(filePath, ignoreErrors : true); if (settings.IsStreamer) { List <UserQuoteModel> quotes = new List <UserQuoteModel>(); await ChannelSession.Services.Database.Read(settings.DatabaseFilePath, "SELECT * FROM Quotes", (Dictionary <string, object> data) => { string json = (string)data["Data"]; json = json.Replace("MixItUp.Base.ViewModel.User.UserQuoteViewModel", "MixItUp.Base.Model.User.UserQuoteModel"); quotes.Add(JSONSerializerHelper.DeserializeFromString <UserQuoteModel>(json)); }); await ChannelSession.Services.Database.BulkWrite(settings.DatabaseFilePath, "REPLACE INTO Quotes(ID, Data) VALUES(@ID, @Data)", quotes.Select(q => new Dictionary <string, object>() { { "@ID", q.ID.ToString() }, { "@Data", JSONSerializerHelper.SerializeToString(q) } })); } await settings.Initialize(); #pragma warning disable CS0612 // Type or member is obsolete if (settings.DiagnosticLogging) { ChannelSession.AppSettings.DiagnosticLogging = true; } #pragma warning restore CS0612 // Type or member is obsolete #pragma warning disable CS0612 // Type or member is obsolete foreach (UserCurrencyModel oldCurrency in settings.Currencies.Values) { CurrencyModel newCurrency = new CurrencyModel(); newCurrency.ID = oldCurrency.ID; newCurrency.Name = oldCurrency.Name; newCurrency.AcquireAmount = oldCurrency.AcquireAmount; newCurrency.AcquireInterval = oldCurrency.AcquireInterval; newCurrency.MinimumActiveRate = oldCurrency.MinimumActiveRate; newCurrency.OfflineAcquireAmount = oldCurrency.OfflineAcquireAmount; newCurrency.OfflineAcquireInterval = oldCurrency.OfflineAcquireInterval; newCurrency.MaxAmount = oldCurrency.MaxAmount; newCurrency.SpecialIdentifier = oldCurrency.SpecialIdentifier; newCurrency.SubscriberBonus = oldCurrency.SubscriberBonus; newCurrency.ModeratorBonus = oldCurrency.ModeratorBonus; newCurrency.OnFollowBonus = oldCurrency.OnFollowBonus; newCurrency.OnHostBonus = oldCurrency.OnHostBonus; newCurrency.OnSubscribeBonus = oldCurrency.OnSubscribeBonus; newCurrency.ResetInterval = (Model.Currency.CurrencyResetRateEnum)((int)oldCurrency.ResetInterval); newCurrency.ResetStartCadence = oldCurrency.ResetStartCadence; newCurrency.LastReset = oldCurrency.LastReset; newCurrency.IsPrimary = oldCurrency.IsPrimary; if (oldCurrency.RankChangedCommand != null) { settings.SetCustomCommand(oldCurrency.RankChangedCommand); newCurrency.RankChangedCommandID = oldCurrency.RankChangedCommand.ID; } foreach (UserRankViewModel rank in oldCurrency.Ranks) { newCurrency.Ranks.Add(new RankModel(rank.Name, rank.MinimumPoints)); } settings.Currency[newCurrency.ID] = newCurrency; } foreach (UserInventoryModel oldInventory in settings.Inventories.Values) { InventoryModel newInventory = new InventoryModel(); newInventory.ID = oldInventory.ID; newInventory.Name = oldInventory.Name; newInventory.DefaultMaxAmount = oldInventory.DefaultMaxAmount; newInventory.SpecialIdentifier = oldInventory.SpecialIdentifier; newInventory.ShopEnabled = oldInventory.ShopEnabled; newInventory.ShopCommand = oldInventory.ShopCommand; newInventory.ShopCurrencyID = oldInventory.ShopCurrencyID; newInventory.TradeEnabled = oldInventory.TradeEnabled; newInventory.TradeCommand = oldInventory.TradeCommand; if (oldInventory.ItemsBoughtCommand != null) { settings.SetCustomCommand(oldInventory.ItemsBoughtCommand); newInventory.ItemsBoughtCommandID = oldInventory.ItemsBoughtCommand.ID; } else { CustomCommand buyCommand = new CustomCommand(InventoryWindowViewModel.ItemsBoughtCommandName); buyCommand.Actions.Add(new ChatAction("You bought $itemtotal $itemname for $itemcost $currencyname", sendAsStreamer: false)); settings.SetCustomCommand(buyCommand); newInventory.ItemsBoughtCommandID = buyCommand.ID; } if (oldInventory.ItemsSoldCommand != null) { settings.SetCustomCommand(oldInventory.ItemsSoldCommand); newInventory.ItemsSoldCommandID = oldInventory.ItemsSoldCommand.ID; } else { CustomCommand sellCommand = new CustomCommand(InventoryWindowViewModel.ItemsSoldCommandName); sellCommand.Actions.Add(new ChatAction("You sold $itemtotal $itemname for $itemcost $currencyname", sendAsStreamer: false)); settings.SetCustomCommand(sellCommand); newInventory.ItemsSoldCommandID = sellCommand.ID; } if (oldInventory.ItemsTradedCommand != null) { settings.SetCustomCommand(oldInventory.ItemsTradedCommand); newInventory.ItemsTradedCommandID = oldInventory.ItemsTradedCommand.ID; } else { CustomCommand tradeCommand = new CustomCommand(InventoryWindowViewModel.ItemsTradedCommandName); tradeCommand.Actions.Add(new ChatAction("@$username traded $itemtotal $itemname to @$targetusername for $targetitemtotal $targetitemname", sendAsStreamer: false)); settings.SetCustomCommand(tradeCommand); newInventory.ItemsTradedCommandID = tradeCommand.ID; } foreach (UserInventoryItemModel oldItem in oldInventory.Items.Values.ToList()) { InventoryItemModel newItem = new InventoryItemModel(oldItem.Name, oldItem.MaxAmount, oldItem.BuyAmount, oldItem.SellAmount); newItem.ID = oldItem.ID; newInventory.Items[newItem.ID] = newItem; } settings.Inventory[newInventory.ID] = newInventory; } #pragma warning restore CS0612 // Type or member is obsolete if (settings.GiveawayRequirements != null && settings.GiveawayRequirements.Inventory != null && settings.Inventory.ContainsKey(settings.GiveawayRequirements.Inventory.InventoryID)) { InventoryModel inventory = settings.Inventory[settings.GiveawayRequirements.Inventory.InventoryID]; #pragma warning disable CS0612 // Type or member is obsolete if (inventory != null && !string.IsNullOrEmpty(settings.GiveawayRequirements.Inventory.ItemName)) { InventoryItemModel item = inventory.GetItem(settings.GiveawayRequirements.Inventory.ItemName); if (item != null) { settings.GiveawayRequirements.Inventory.ItemID = item.ID; } settings.GiveawayRequirements.Inventory.ItemName = null; } #pragma warning restore CS0612 // Type or member is obsolete } foreach (CommandBase command in SettingsV2Upgrader.GetAllCommands(settings)) { if (command is PermissionsCommandBase) { PermissionsCommandBase pCommand = (PermissionsCommandBase)command; if (pCommand.Requirements != null && pCommand.Requirements.Inventory != null && settings.Inventory.ContainsKey(pCommand.Requirements.Inventory.InventoryID)) { InventoryModel inventory = settings.Inventory[pCommand.Requirements.Inventory.InventoryID]; #pragma warning disable CS0612 // Type or member is obsolete if (inventory != null && !string.IsNullOrEmpty(pCommand.Requirements.Inventory.ItemName)) { InventoryItemModel item = inventory.GetItem(pCommand.Requirements.Inventory.ItemName); if (item != null) { pCommand.Requirements.Inventory.ItemID = item.ID; } pCommand.Requirements.Inventory.ItemName = null; } #pragma warning restore CS0612 // Type or member is obsolete } } } List <UserDataModel> usersToRemove = new List <UserDataModel>(); foreach (UserDataModel user in settings.UserData.Values.ToList()) { if (user.MixerID <= 0) { usersToRemove.Add(user); } } foreach (UserDataModel user in usersToRemove) { settings.UserData.Remove(user.ID); } await ChannelSession.Services.Settings.Save(settings); }
private async Task <SettingsV2Model> LoadSettings(string filePath) { return(await SettingsV2Upgrader.UpgradeSettingsToLatest(filePath)); }
public async Task <Result <SettingsV2Model> > RestorePackagedBackup(string filePath) { try { string tempFilePath = ChannelSession.Services.FileService.GetTempFolder(); string tempFolder = Path.GetDirectoryName(tempFilePath); string settingsFile = null; string databaseFile = null; try { using (ZipArchive zipFile = ZipFile.Open(filePath, ZipArchiveMode.Read)) { foreach (ZipArchiveEntry entry in zipFile.Entries) { string extractedFilePath = Path.Combine(tempFolder, entry.Name); if (File.Exists(extractedFilePath)) { File.Delete(extractedFilePath); } if (extractedFilePath.EndsWith(SettingsV2Model.SettingsFileExtension, StringComparison.InvariantCultureIgnoreCase)) { settingsFile = extractedFilePath; } else if (extractedFilePath.EndsWith(SettingsV2Model.DatabaseFileExtension, StringComparison.InvariantCultureIgnoreCase)) { databaseFile = extractedFilePath; } } zipFile.ExtractToDirectory(tempFolder); } } catch (Exception ex) { Logger.Log(ex); } int currentVersion = -1; if (!string.IsNullOrEmpty(settingsFile)) { currentVersion = await SettingsV2Upgrader.GetSettingsVersion(settingsFile); } if (currentVersion == -1) { return(new Result <SettingsV2Model>("The backup file selected does not appear to contain Mix It Up settings.")); } if (currentVersion > SettingsV2Model.LatestVersion) { return(new Result <SettingsV2Model>("The backup file is valid, but is from a newer version of Mix It Up. Be sure to upgrade to the latest version." + Environment.NewLine + Environment.NewLine + "NOTE: This may require you to opt-in to the Preview build from the General tab in Settings if this was made in a Preview build.")); } return(new Result <SettingsV2Model>(await FileSerializerHelper.DeserializeFromFile <SettingsV2Model>(settingsFile))); } catch (Exception ex) { Logger.Log(ex); return(new Result <SettingsV2Model>(ex)); } }