Esempio n. 1
0
        private void lbAvail_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbAvail.SelectedItem != null)
            {
                CMod selMod = (CMod)lbAvail.SelectedItem;

                if (selMod.getDescription() == null)
                {
                    if (selMod.getDependencies() != null)
                    {
                        lblModInfo.Text = "Requires: " + selMod.getDependencies();
                    }
                    else
                    {
                        lblModInfo.Text = "Double click a module to install / update it.";
                    }
                }
                else
                {
                    lblModInfo.Text = selMod.getDescription();
                    if (selMod.getDependencies() != null)
                    {
                        lblModInfo.Text += " [Requires: " + selMod.getDependencies() + "]";
                    }
                }

                // Select the same module in the local repo if installed
                int oldSel = lbInst.SelectedIndex;
                for (int idx = 0; idx < lbInst.Items.Count; idx++)
                {
                    CMod itm = (CMod)lbInst.Items[idx];
                    if (itm.getId() == selMod.getId())
                    {
                        lbInst.SelectedIndex = idx;
                        break;
                    }
                }
            }
            else
            {
                lblModInfo.Text = "Double click a module to install / update it.";
            }
        }
Esempio n. 2
0
        private CMod InstallMod(CMod mod, bool askToReplace = false)
        {
            if (mod.getDependencies() != null && mod.getDependencies().Length > 0)
            {
                // Test if the dependency is met
                CMod dep = findInstalledModWithId(mod.getDependencies());
                if (dep == null)
                {
                    DialogResult dr = System.Windows.Forms.DialogResult.Cancel;
                    dep = findRepoModWithId(mod.getDependencies());

                    if (dep != null)
                    {
                        dr = MessageBox.Show("The mod '" + mod.getName() + "' requires the mod '" + mod.getDependencies() + "' to be installed.\n\nCurrently I cannot find that module in your game but I have it in my repository.\n\nShall I install it?", mod.getDisplayName() + ": missing dependency!", MessageBoxButtons.YesNoCancel);
                        if (dr == System.Windows.Forms.DialogResult.Yes)
                        {
                            InstallMod(dep, true);
                        }
                        else if (dr == System.Windows.Forms.DialogResult.Cancel)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        dr = MessageBox.Show("The mod '" + mod.getName() + "' requires the mod '" + mod.getDependencies() + "' to be installed.\n\nCurrently I cannot find that module in your game and I don't find it in my repository.\n\nContinue anyways?", mod.getDisplayName() + ": missing dependency!", MessageBoxButtons.YesNo);
                        if (dr == System.Windows.Forms.DialogResult.No)
                        {
                            return(null);
                        }
                    }
                }
            }

            using (WebClient wc = new WebClient())
            {
                try
                {
                    wc.DownloadFile(mod.getFileName(), "lpmgr.tmp");

                    if (Directory.Exists("mmTemp"))
                    {
                        Directory.Delete("mmTemp", true);
                    }

                    Directory.CreateDirectory("mmTemp");

                    System.IO.Compression.ZipFile.ExtractToDirectory("lpmgr.tmp", "mmTemp");

                    string[] subFolders = Directory.GetDirectories("mmTemp");
                    if (subFolders.Length == 1)    // Single mod
                    {
                        string name = Path.GetFileName(subFolders[0]);

                        // Test if already installed
                        bool   install = true;
                        string dest    = m_sPath + MOD_SUBFOLDER + name;
                        if (!mod.isPatch() && Directory.Exists(dest))       // patches always override
                        {
                            DialogResult dr = System.Windows.Forms.DialogResult.Yes;
                            if (askToReplace)
                            {
                                dr = MessageBox.Show("Replace existing mod?", mod.getDisplayName() + ": already installed!", MessageBoxButtons.YesNo);
                            }

                            if (dr == System.Windows.Forms.DialogResult.Yes)
                            {
                                Directory.Delete(dest, true);
                            }
                            else
                            {
                                install = false;
                            }
                        }

                        if (install)
                        {
                            if (mod.getDevMessage() != null)
                            {
                                MessageBox.Show(mod.getDevMessage(), "Message from mod " + mod.getName());
                            }

                            if (mod.isPatch())
                            {
                                DirectoryCopy(subFolders[0], dest, true);
                            }
                            else
                            {
                                Directory.Move(subFolders[0], dest);
                            }

                            // Test if the mod file exists
                            DirectoryInfo folder      = new DirectoryInfo(dest);
                            FileInfo      modFileInfo = null;
                            if (folder.GetFiles("*.lpmod").Length > 0)
                            {
                                modFileInfo = folder.GetFiles("*.lpmod")[0];
                            }
                            else if (folder.GetFiles("*.lpaddon").Length > 0)
                            {
                                modFileInfo = folder.GetFiles("*.lpaddon")[0];
                            }

                            if (modFileInfo != null)
                            {
                                CMod newMod = new CMod(mod.getName(), dest);
                                newMod.setFileName(modFileInfo.FullName);
                                newMod.readModInfo();
                                newMod.setIndex(lbInst.Items.Count);

                                if (Directory.Exists("mmTemp"))
                                {
                                    Directory.Delete("mmTemp", true);
                                }
                                File.Delete("lpmgr.tmp");

                                return(newMod);
                            }
                        }
                    }
                    else if (subFolders.Length > 1)
                    {
                        if (mod.getDevMessage() != null)
                        {
                            MessageBox.Show(mod.getDevMessage(), "Message from mod " + mod.getName());
                        }

                        DialogResult dr = System.Windows.Forms.DialogResult.Yes;
                        dr = MessageBox.Show("The package contains multiple folders.\n\nIt may replace / modify several existing mods (which is OK for patches or bundles).\n\nProceed with installation?\n\n(If unsure check the mod description again...)", mod.getDisplayName() + ": Install?", MessageBoxButtons.YesNo);

                        if (dr == System.Windows.Forms.DialogResult.Yes)
                        {
                            CMod retMod = null;
                            foreach (string sf in subFolders)
                            {
                                string name = Path.GetFileName(sf);
                                string dest = m_sPath + MOD_SUBFOLDER + name;

                                DirectoryCopy(sf, dest, true);
                            }
                        }
                    }

                    if (Directory.Exists("mmTemp"))
                    {
                        Directory.Delete("mmTemp", true);
                    }

                    File.Delete("lpmgr.tmp");

                    RefreshLocalMods();
                    RefreshRepoMods();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem during installation of mod " + mod.ToString() + ":\n" + ex.Message, "Ooops...");
                }

                return(null);
            }
        }