File size: 4,040 Bytes
e26fba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MovieTube.Installer.Properties;
using System.Diagnostics;

namespace MovieTube.Installer
{
    public partial class MainForm : Form
    {
        private const string CodecName = "K-Lite_Codec_Pack_1060_Basic.exe";
        private const string CodecININame = "klcp_basic_unattended.ini";
        private const string MovieTubeSetupName = "MovieTube.Setup.msi";
        private const string InstallFolderName = "MovieTube";
        private const string AppName = "MovieTube.exe";

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(e.CloseReason == CloseReason.UserClosing)
                e.Cancel = true;
        }

        private void Install()
        {
            //install the Codec
            DisplayMessage("Decompressing K-Lite Codec Pack for playback");
            Decompress(CodecName, Resources.K_Lite_Codec_Pack_1060_Basic);
            Decompress(CodecININame, Resources.klcp_basic_unattended);
            DisplayMessage("Installing K-Lite Codec Pack for playback");
            LaunchApp(CodecName, String.Format(" /verysilent /norestart /LoadInf=\".\\{0}\" ", CodecININame));
            DisplayMessage("Decompressing MovieTube setup");
            Decompress(MovieTubeSetupName, Resources.MovieTube_Setup);
            Decompress("setup.exe", Resources.setup);
            DisplayMessage("Installing MovieTube setup");
            //LaunchApp("msiexec.exe", String.Format("  /i \"{0}\"", MovieTubeSetupName));

            LaunchApp("setup.exe", String.Empty);

            var appPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), InstallFolderName), AppName);
            var dbPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), InstallFolderName), "movie.db");

            if (File.Exists(dbPath))
            {
                try
                {
                    File.Delete(dbPath);
                }
                catch { }
            }

            //check if installed
            if (File.Exists(appPath))
            {
                LaunchApp(appPath, String.Empty, false);
            }

            Application.Exit();
        }

        private void LaunchApp(string name, string args, bool wait = true)
        {
            var startInfo = new ProcessStartInfo(name, args);
            startInfo.Verb = "runas";
            var dir = Path.GetDirectoryName(name);
            if (!String.IsNullOrEmpty(dir))
                startInfo.WorkingDirectory = dir;

            var process = Process.Start(startInfo);
            if(wait)
                process.WaitForExit();
        }

        private void Decompress(string name, byte[] data)
        {
            var path = Path.Combine(Application.StartupPath, name);
            if (File.Exists(path))
                File.Delete(path);
            File.WriteAllBytes(path, data);

        }
        private void Decompress(string name, string data)
        {
            var path = Path.Combine(Application.StartupPath, name);
            if (File.Exists(path))
                File.Delete(path);
            File.WriteAllText(path, data);
        }

        private void DisplayMessage(string text)
        {
            this.labelMessage.Text = text + "...";
            Application.DoEvents();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
           
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            this.timer.Dispose();
            Install();
        }

    }
}