lotrointerface.com
Search Downloads

LoTROInterface SVN Reminders

[/] [trunk/] [Thurallor/] [Reminders/] [MigrateDialog.lua] - Rev 2

Compare with Previous | Blame | View Log

MigrateDialog = class(Turbine.UI.Lotro.Window);

function MigrateDialog:Constructor(window, accountSettings, serverSettings)
    self.window = window;
    self.accountSettings = accountSettings;
    self.serverSettings = serverSettings;
    self.migrating = (self.serverSettings ~= nil);
    
    if (self.serverSettings and self.serverSettings.language) then
        L:SetLanguage(self.serverSettings.language);
    end

    Turbine.UI.Lotro.Window.Constructor(self);
    local version = window.plugin:GetVersion();
    self:SetText(L:GetText("/MigrateDialog/Title"):gsub("<version>", version));
    self:SetVisible(true);
    self.width = L:GetText("/MigrateDialog/Width");
    self:SetSize(self.width, 100);
    self:SetZOrder(2);

    self:Redraw();
end

function MigrateDialog:AddLabel(text, numLines)
    local label = Turbine.UI.Label();
    label:SetSelectable(true);
    label:SetParent(self);
    label:SetPosition(30, self.top);
    label:SetFont(Turbine.UI.Lotro.Font.Verdana14);
    label:SetTextAlignment(Turbine.UI.ContentAlignment.TopLeft);
    label:SetMultiline(true);
    label:SetText(text);
    local height = numLines * 14;
    label:SetSize(self.width - 60, height);
    self.top = self.top + height;
    return label;
end

function MigrateDialog:AddFilename(filename)
    local label = self:AddLabel(filename, 1);
    label:SetFont(Turbine.UI.Lotro.Font.LucidaConsole12);
    label:SetText(filename);
    label:SetHeight(12 * 3);
    label:SetLeft(60);
    label:SetWidth(self.width - 90);
    self.top = self.top - 14 + (12 * 3)
    return label;
end

function MigrateDialog:AddDropDown(items, selectedItem, width)
    local dropDown = Thurallor.UI.DropDown(items, selectedItem);
    dropDown:SetParent(self);
    dropDown:SetAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
    dropDown:SetFont(Turbine.UI.Lotro.Font.Verdana14);
    dropDown:SetPosition((self.width - width) / 2, self.top);
    dropDown:SetSize(width, 22);
    self.top = self.top + 22;
    return dropDown;
end

function MigrateDialog:AddButton(text, width)
    local button = Turbine.UI.Lotro.Button();
    button:SetParent(self);
    button:SetText(text);
    local left = (self.width - width) / 2;
    button:SetPosition(left, self.top);
    self.top = self.top + button:GetHeight();
    button:SetWidth(width);
    return button;
end

function MigrateDialog:SkipLine()
    self.top = self.top + 14;
end

function MigrateDialog:Redraw()
    L:SetContext("/MigrateDialog");

    self:GetControls():Clear();
    self.top = 45;

    if (self.migrating) then
        self:AddLabel(L:GetText("Intro"), L:GetText("IntroLines"));
        self:SkipLine();
    end
    
    self:AddLabel(L:GetText("WhichServer"), L:GetText("WhichServerLines"));
   
    self:SkipLine();
    local serverNames = {};
    for name, _ in pairs(Turbine.ServerName) do
        table.insert(serverNames, name);
    end
    table.sort(serverNames);
    local serverName = self.selectedServer or Turbine.Engine:GetServerName();
    self.serverNameDropDown = self:AddDropDown(serverNames, serverName or L:GetText("/OptionsPanel/GlobalTab/ServerUnknown"), 110);
    self.serverNameDropDown.SelectionChanged = function(_, args)
        self.selectedServer = args.Item;
        Turbine.Engine:SetServerName(self.selectedServer);
        self:Redraw();
    end
    local button = Thurallor.UI.TrickButton(L:GetClientLanguageText("/Chat/ServerNameCommand"), Turbine.ChatType.Standard, L:GetClientLanguageText("/Chat/ServerNameDetect"));
    button:SetParent(self);
    button:SetFont(Turbine.UI.Lotro.Font.Verdana14);
    button:SetText(L:GetText("/OptionsPanel/GlobalTab/DetectServerName"));
    button:SetWidth(L:GetText("/OptionsPanel/GlobalTab/DetectServerNameWidth"));
    button:SetPosition(self.serverNameDropDown:GetLeft() + self.serverNameDropDown:GetWidth() + 4, self.serverNameDropDown:GetTop());
    button.Click = function(ctl, ...)
        self.serverNameDropDown:SetSelectedItem(...);
    end

    if (serverName) then
        if (self.migrating) then
            local serverDir = Turbine.ServerDirectoryName[serverName];
        
            self:SkipLine();
            local text = L:GetText("ExistingSettings"):gsub("<server>", serverName);
            self:AddLabel(text, L:GetText("ExistingSettingsLines"));

            self:SkipLine();
            local path = "Documents\\The Lord of the Rings Online\\\nPluginData\\<account>\\<server>\\AllCharacters\\\nReminders.plugindata";
            path = path:gsub("<server>", serverDir);
            self:AddFilename(path);

            self:SkipLine();
            self:AddLabel(L:GetText("BackupOldSettings"), L:GetText("BackupOldSettingsLines"));

            self:SkipLine();
            local path = "Documents\\The Lord of the Rings Online\\\nPluginData\\<account>\\<server>\\AllCharacters\\\nReminders_backup.plugindata";
            path = path:gsub("<server>", serverDir);
            self:AddFilename(path);

            self:SkipLine();
            self:AddLabel(L:GetText("CreateNewSettings"), L:GetText("CreateNewSettingsLines"));
            
            self:SkipLine();
            path = "Documents\\The Lord of the Rings Online\\\nPluginData\\<account>\\AllServers\\\nReminders.plugindata";
            self:AddFilename(path);
        end

        self:SkipLine();
        local button = self:AddButton(L:GetText("Ok"), 50);
        button.Click = function()
            if (self.migrating) then
                self:DoMigration();
            end
            self:Close();
        end

        if (self.migrating) then
            self:SkipLine();
            self:AddLabel(L:GetText("AfterYouClickOK"), L:GetText("AfterYouClickOKLines"));
        end
    end
    
    self:SetHeight(self.top + 30);
    CenterWindow(self);
end

function MigrateDialog:DoMigration()
    local accountSettings, serverSettings = self.accountSettings, self.serverSettings;

    -- Create backup file for existing server settings
    local serverData = ExportTable(serverSettings);
    Turbine.PluginData.Save(Turbine.DataScope.Server, "Reminders_backup", serverData);

    -- Replace all instances of charName with charID
    local serverID = Turbine.Engine.GetServerID();
    local newCharSettings = {};
    for charName, charSettings in pairs(serverSettings.characters) do
        local charID = serverID .. "." .. charName;
        newCharSettings[charID] = serverSettings.characters[charName];
    end
    serverSettings.characters = newCharSettings;
    for _, tabSettings in pairs(serverSettings.tabs) do
        local events = tabSettings.events or {};
        for eventNum, eventInfo in pairs(events) do
            eventInfo.charID = serverID .. "." .. eventInfo.charName;
            eventInfo.charName = nil;
        end
    end

    -- If the account-wide file doesn't exist, use the server-specific settings as a starting point
    if (not accountSettings) then
        accountSettings = serverSettings;
        
    -- Otherwise we need to merge the server's characters, categories, and reminders into the account-wide file
    else
        -- Merge character settings from server to account
        for charID, charSettings in pairs(serverSettings.characters) do
            accountSettings.characters[charID] = charSettings;
        end
        
        -- Merge categories from server to account
        local accountCategory = {};
        for _, catInfo in ipairs(accountSettings.categories) do
            accountCategory[catInfo.name] = catInfo;
        end
        for _, catInfo in ipairs(serverSettings.categories) do
            local name = catInfo.name;
            if (not accountCategory[name]) then
                table.insert(accountSettings.categories, catInfo);
            end
        end

        -- Merge reminders from server to account
        local accountTab = {};
        for _, tabSettings in pairs(accountSettings.tabs) do
            accountTab[tabSettings.name] = tabSettings;
        end
        for _, tabSettings in pairs(serverSettings.tabs) do
            local name = tabSettings.name;
            local accountTab = accountTab[name];
            if (not accountTab) then
                table.insert(accountSettings.tabs, tabSettings);
            else
                local serverEvents = tabSettings.events or {};
                for _, eventInfo in pairs(serverEvents) do
                    table.insert(accountTab.events, eventInfo);
                end
            end
        end
    end

    -- Save account-wide settings
    local accountData = ExportTable(accountSettings);
    Turbine.PluginData.Save(Turbine.DataScope.Account, "Reminders", accountData);

    -- Delete server-specific settings
    Turbine.PluginData.Save(Turbine.DataScope.Server, "Reminders", nil);
end

Compare with Previous | Blame


All times are GMT -5. The time now is 07:10 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui