<commit_before>﻿/*
 * Copyright © 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */

// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
// Blamming GITHUB gets you banned for a while during debugging. 
// turn this on only for releasing
#define GITHUBDOWNLOAD
// turn this on only for releasing

using EDDiscovery.HTTP;
using EDDiscovery.Win32Constants;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EDDiscovery.Forms
{
    public partial class DownloadManagerForm : Form
    {
        public Dictionary<string,string> changelist = new Dictionary<string,string>();      //+ enabled/installed, - deleted/disabled

        class Group
        {
            public VersioningManager.DownloadItem di;
            public Panel panel;
            public Button info;
            public Label type;
            public Label name;
            public Label version;
            public Label shortdesc;
            public Label actionlabel;
            public ExtendedControls.ButtonExt actionbutton;
            public ExtendedControls.ButtonExt deletebutton;
            public ExtendedControls.CheckBoxCustom enabled;
        }

        List<Group> groups = new List<Group>();
        VersioningManager mgr;
        EDDiscovery2.EDDTheme theme;

        int panelheightmargin = 1;
        int labelheightmargin = 6;
        int panelleftmargin = 3;
        Font font;

        string downloadactfolder;
        string downloadflightfolder;
#if DEBUG
        string downloadactdebugfolder;
#endif

        public DownloadManagerForm()
        {
            InitializeComponent();
        }

        public void Init(EDDiscovery2.EDDTheme th)
        {
            theme = th;
            font = new Font(theme.FontName, 10);
            bool winborder = theme.ApplyToForm(this, font);
            statusStripCustom.Visible = panelTop.Visible = panelTop.Enabled = !winborder;
            richTextBoxScrollDescription.TextBox.ReadOnly = true;
        }

        public bool DownloadFromGitHub(string downloadfolder, string gitdir)
        {
            DirectoryInfo di = new DirectoryInfo(downloadfolder);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }

            GitHubClass ghc = new GitHubClass();

            List<GitHubFile> files = ghc.GetDataFiles(gitdir);

            return ghc.DownloadFiles(files, downloadfolder);
        }

        private System.Threading.Thread CheckThread;

        private void DownloadManager_Shown(object sender, EventArgs e)
        {
            CheckThread = new System.Threading.Thread(new System.Threading.ThreadStart(CheckState));
            CheckThread.Start();
        }

        private void CheckState()
        {
            downloadactfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\act");
            if (!System.IO.Directory.Exists(downloadactfolder))
                System.IO.Directory.CreateDirectory(downloadactfolder);

            downloadflightfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\flights");
            if (!System.IO.Directory.Exists(downloadflightfolder))
                System.IO.Directory.CreateDirectory(downloadflightfolder);

#if DEBUG
            downloadactdebugfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\Debug");
            if (!System.IO.Directory.Exists(downloadactdebugfolder))
                System.IO.Directory.CreateDirectory(downloadactdebugfolder);
#endif

#if GITHUBDOWNLOAD

            DownloadFromGitHub(downloadactfolder, "ActionFiles/V1");
            DownloadFromGitHub(downloadflightfolder, "VideoFiles/V1");
#if DEBUG
            DownloadFromGitHub(downloadactdebugfolder, "ActionFiles/Debug");
#endif
#endif

            Invoke((MethodInvoker)ReadyToDisplay);
        }

        private void DownloadManagerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CheckThread != null && CheckThread.IsAlive)     // can't close if its alive, it will call back nothing
                CheckThread.Join();

            font?.Dispose();
        }

        void ReadyToDisplay()
        {
            panelVScroll.RemoveAllControls();

            mgr = new VersioningManager();

            int[] edversion = Tools.GetEDVersion();
            System.Diagnostics.Debug.Assert(edversion != null);

            mgr.ReadLocalFiles(Tools.GetAppDataDirectory(), "Actions", "*.act", "Action File");
            mgr.ReadInstallFiles(downloadactfolder, Tools.GetAppDataDirectory(), "*.act", edversion, "Action File");

#if DEBUG
            mgr.ReadInstallFiles(downloadactdebugfolder, Tools.GetAppDataDirectory(), "*.act", edversion, "Action File");
#endif

            mgr.ReadLocalFiles(Tools.GetAppDataDirectory(), "Flights", "*.vid", "Video File");
            mgr.ReadInstallFiles(downloadflightfolder, Tools.GetAppDataDirectory(), "*.vid", edversion, "Video File");

            mgr.Sort();<commit_msg>Fix deadlock in download manager<commit_after>﻿/*
 * Copyright © 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */

// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
// Blamming GITHUB gets you banned for a while during debugging. 
// turn this on only for releasing
#define GITHUBDOWNLOAD
// turn this on only for releasing

using EDDiscovery.HTTP;
using EDDiscovery.Win32Constants;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EDDiscovery.Forms
{
    public partial class DownloadManagerForm : Form
    {
        public Dictionary<string,string> changelist = new Dictionary<string,string>();      //+ enabled/installed, - deleted/disabled

        class Group
        {
            public VersioningManager.DownloadItem di;
            public Panel panel;
            public Button info;
            public Label type;
            public Label name;
            public Label version;
            public Label shortdesc;
            public Label actionlabel;
            public ExtendedControls.ButtonExt actionbutton;
            public ExtendedControls.ButtonExt deletebutton;
            public ExtendedControls.CheckBoxCustom enabled;
        }

        List<Group> groups = new List<Group>();
        VersioningManager mgr;
        EDDiscovery2.EDDTheme theme;

        int panelheightmargin = 1;
        int labelheightmargin = 6;
        int panelleftmargin = 3;
        Font font;

        string downloadactfolder;
        string downloadflightfolder;
#if DEBUG
        string downloadactdebugfolder;
#endif

        public DownloadManagerForm()
        {
            InitializeComponent();
        }

        public void Init(EDDiscovery2.EDDTheme th)
        {
            theme = th;
            font = new Font(theme.FontName, 10);
            bool winborder = theme.ApplyToForm(this, font);
            statusStripCustom.Visible = panelTop.Visible = panelTop.Enabled = !winborder;
            richTextBoxScrollDescription.TextBox.ReadOnly = true;
        }

        public bool DownloadFromGitHub(string downloadfolder, string gitdir)
        {
            DirectoryInfo di = new DirectoryInfo(downloadfolder);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }

            GitHubClass ghc = new GitHubClass();

            List<GitHubFile> files = ghc.GetDataFiles(gitdir);

            return ghc.DownloadFiles(files, downloadfolder);
        }

        private System.Threading.Thread CheckThread;

        private void DownloadManager_Shown(object sender, EventArgs e)
        {
            CheckThread = new System.Threading.Thread(new System.Threading.ThreadStart(CheckState));
            CheckThread.Start();
        }

        private void CheckState()
        {
            downloadactfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\act");
            if (!System.IO.Directory.Exists(downloadactfolder))
                System.IO.Directory.CreateDirectory(downloadactfolder);

            downloadflightfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\flights");
            if (!System.IO.Directory.Exists(downloadflightfolder))
                System.IO.Directory.CreateDirectory(downloadflightfolder);

#if DEBUG
            downloadactdebugfolder = System.IO.Path.Combine(Tools.GetAppDataDirectory(), "temp\\Debug");
            if (!System.IO.Directory.Exists(downloadactdebugfolder))
                System.IO.Directory.CreateDirectory(downloadactdebugfolder);
#endif

#if GITHUBDOWNLOAD

            DownloadFromGitHub(downloadactfolder, "ActionFiles/V1");
            DownloadFromGitHub(downloadflightfolder, "VideoFiles/V1");
#if DEBUG
            DownloadFromGitHub(downloadactdebugfolder, "ActionFiles/Debug");
#endif
#endif

            BeginInvoke((MethodInvoker)ReadyToDisplay);
        }

        private void DownloadManagerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CheckThread != null && CheckThread.IsAlive)     // can't close if its alive, it will call back nothing
                CheckThread.Join();

            font?.Dispose();
        }

        void ReadyToDisplay()
        {
            panelVScroll.RemoveAllControls();

            mgr = new VersioningManager();

            int[] edversion = Tools.GetEDVersion();
            System.Diagnostics.Debug.Assert(edversion != null);

            mgr.ReadLocalFiles(Tools.GetAppDataDirectory(), "Actions", "*.act", "Action File");
            mgr.ReadInstallFiles(downloadactfolder, Tools.GetAppDataDirectory(), "*.act", edversion, "Action File");

#if DEBUG
            mgr.ReadInstallFiles(downloadactdebugfolder, Tools.GetAppDataDirectory(), "*.act", edversion, "Action File");
#endif

            mgr.ReadLocalFiles(Tools.GetAppDataDirectory(), "Flights", "*.vid", "Video File");
            mgr.ReadInstallFiles(downloadflightfolder, Tools.GetAppDataDirectory(), "*.vid", edversion, "Video File");

            mgr.Sort();
{'fraction_non_alphanumeric': 0.08678534748373845, 'fraction_numerical': 0.003252310852447792, 'mean_word_length': 2.650421743205248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 65, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810142', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2888, 'n_words': 801}