AA2Install.formMain.refreshModList C# (CSharp) Method

refreshModList() public method

Refreshes the list from the /mods/ directory.
public refreshModList ( bool skipReload = false, string filter = "" ) : void
skipReload bool
filter string
return void
        public void refreshModList(bool skipReload = false, string filter = "")
        {
            lsvMods.Items.Clear();
            ModDictionary modDict;
            if (!Configuration.loadMods(out modDict))
            {
                currentOwner.InvokeMessageBox("The configuration file has been detected as corrupt. Cache will be cleared.", "Corrupt configuration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                FlushCache();
            }

            if (!skipReload)
            {
                setEnabled(false);
                initializeBench();
                
                string[] paths = getFiles(Paths.MODS, "*.7z|*.zip", SearchOption.TopDirectoryOnly);
                prgMinor.Style = ProgressBarStyle.Marquee;
            
                int i = 0;
            
                foreach (string path in paths)
                {
                    i++;
                    updateStatus("(" + i + "/" + paths.Length + ") Processing " + path.Remove(0, path.LastIndexOf('\\') + 1) + "...");

                    bool flag = false;
                    foreach (Mod m in modDict.Values)
                    {
                        if (path == m.Filename)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        prgMajor.Value = i;
                        continue;
                    }

                    modDict[path] = _7z.Index(path);

                    prgMajor.Value = (100 * i / paths.Length);
                }
            }

            foreach (Mod m in modDict.Values.ToList())
            {
                //bool backup = File.Exists(Paths.BACKUP + "\\" + m.Name.Remove(0, m.Name.LastIndexOf('\\') + 1).Replace(".zip", ".7z"));

                if (!File.Exists(m.Filename) && !m.Installed)
                {
                    modDict.Remove(m.Name);
                    continue;
                }

                Regex r = new Regex(@"^AA2_[A-Z]{4}\\");

                if (!m.Name.Replace(".7z", "").Replace(".zip", "").ToLower().Contains(filter.ToLower()) && filter != "")
                    continue;

                lsvMods.Items.Add(m.Name, m.Name.Replace(".7z", "").Replace(".zip", ""), 0);
                int index = lsvMods.Items.IndexOfKey(m.Name);

                if (m.Installed && !m.Exists)
                {
                    //lsvMods.Items[index].BackColor = Color.DarkBlue;
                }
                else if (!m.SubFilenames.Any(s => r.IsMatch(s)))
                {
                    List<string> sfiles;

                    SevenZipExtractor sz = new SevenZipExtractor(m.Filename);
                    sfiles = sz.Files.Select(s => s.Filename).ToList();

                    if (sfiles.Any(s => s.EndsWith(".7z") || s.EndsWith(".zip")))
                    {
                        lsvMods.Items[index].BackColor = Color.FromArgb(0xF7D088);
                        m.Properties["Status"] = "Actual mod 7z may be inside mod archive.";
                    }
                    else
                    {
                        lsvMods.Items[index].BackColor = Color.FromArgb(0xFFCCCC);
                        m.Properties["Status"] = "Not a valid mod.";
                    }
                }
                else if (m.Installed)
                {
                    lsvMods.Items[index].BackColor = Color.LightGreen;
                    lsvMods.Items[index].Checked = true;
                }

                m.Properties["Maximum Size"] = (m.Size / (1024)).ToString("#,## kB");

                if (m.Installed && m.InstallTime.Year > 2000)
                    m.Properties["Installed on"] = m.InstallTime.ToString();
                else
                    m.Properties.Remove("Installed on");

                lsvMods.Items[index].Tag = m;
                
                //modDict[m.Name] = m;
            }

            lsvMods.Sort();

            if (!skipReload)
            {
                Configuration.saveMods(modDict);

                prgMinor.Style = ProgressBarStyle.Continuous;

                prgMinor.Value = prgMinor.Maximum;
                updateStatus("Ready.", LogIcon.OK, false);


                setEnabled(true);
            }
        }