summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/BCRInstaller.cs
blob: 6d88ad4da2e139ab05abb311afef5d0de040f8a7 (plain)
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
using FreeImageAPI;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;


namespace ComicRackWebViewer
{
  /// <summary>
  /// Description of BCRInstaller.
  /// </summary>
  public sealed class BCRInstaller
  {
    private const string INSTALLER_FILE = "BCRPlugin.zip";
    private const string VERSION_FILE = "BCRVersion.txt";
    private const string VERSION = "1.31";

    public string InstallFolder { get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } }

    private static BCRInstaller instance = new BCRInstaller();
    
    public static BCRInstaller Instance {
      get {
        return instance;
      }
    }
    
    private BCRInstaller()
    {
    }


    public bool Install()
    {
      

      ///////////////////////////////////////////////////////////
      //
      /*
      if (!InstallState.IsVC90RedistSP1Installed())
      {
        if (MessageBoxResult.Yes != MessageBox.Show("The required component 'Microsoft Visual C++ 2008 SP1 Redistributable Package' seems to be missing.\nDownload and install the correct version (32/64bit) from the Microsoft website, then restart ComicRack.\n\nDo you want to continue anyway (expect a crash...)?", "Badaap Comic Reader Plugin", MessageBoxButton.YesNo, MessageBoxImage.Error))
        {
          return false;
        }
      }
      */
      
      ///////////////////////////////////////////////////////////
      // Install the correct FreeImage.dll
      if (!FreeImage.IsAvailable())
      {
        System.IO.File.Copy(Path.Combine(InstallFolder, (Environment.Is64BitProcess ? "FreeImage.64bit.dll" : "FreeImage.32bit.dll")), Path.Combine(InstallFolder, "FreeImage.dll"), true);
      }
            
      if (!FreeImage.IsAvailable())
      {
        MessageBox.Show("FreeImage.dll seems to be missing. Aborting.", "Badaap Comic Reader Plugin", MessageBoxButton.OK, MessageBoxImage.Error);
        return false;
      }
      
      ///////////////////////////////////////////////////////////
      // Install the correct SQLite.Interop.dll
      try
      {
        var connection = new SQLiteConnection();
      }
      catch (Exception e)
      {
        Trace.WriteLine(String.Format("Exception: {0}", e));
        System.IO.File.Copy(Path.Combine(InstallFolder, (Environment.Is64BitProcess ? "SQLite.Interop.64bit.dll" : "SQLite.Interop.32bit.dll")), Path.Combine(InstallFolder, "SQLite.Interop.dll"), true);
      }
      
      try 
      {
        var connection = new SQLiteConnection();
      }
      catch (System.DllNotFoundException e)
      {
        Trace.WriteLine(String.Format("Exception: {0}", e));
        MessageBox.Show("SQLite.Interop.dll seems to be missing. Aborting.", "Badaap Comic Reader Plugin", MessageBoxButton.OK, MessageBoxImage.Error);
        return false;
      }
      
      ///////////////////////////////////////////////////////////
      // Check version.txt in order to decide if this is an upgrade.
      var versionFilename = Path.Combine(InstallFolder, VERSION_FILE);

      if (File.Exists(versionFilename))
      {
        StreamReader streamReader = new StreamReader(versionFilename);
        string text = streamReader.ReadToEnd();
        streamReader.Close();
        
        if (text.StartsWith(VERSION))
        {
          return true;
        }
      }
      
      // Create/Update the version file.
      System.IO.File.WriteAllText(versionFilename, VERSION);

      var installerFilename = Path.Combine(InstallFolder, INSTALLER_FILE);
            
      if (!Unzip(installerFilename, InstallFolder))
      {
        MessageBox.Show("Error while installing the web viewer site. Aborting.", "Badaap Comic Reader Plugin", MessageBoxButton.OK, MessageBoxImage.Error);
        return false;
      }
        
      
      return true;      
    }
    
    public bool Unzip(string ZipFile, string DestinationFolder)
    {
  		if ( !File.Exists(ZipFile) ) 
  		{
  			Console.WriteLine("Cannot find file '{0}'", ZipFile);
  			return false;
  		}
  
  		using (ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFile))) 
  		{
  			ZipEntry theEntry;
  			while ((theEntry = s.GetNextEntry()) != null) 
  			{
  				string directoryName = Path.GetDirectoryName(theEntry.Name);
  				string fileName      = Path.GetFileName(theEntry.Name);
  				
  				// create directory
  				if (!String.IsNullOrEmpty(directoryName)) 
  				{
  					Directory.CreateDirectory(Path.Combine(DestinationFolder, directoryName));
  				}
  				
  				if (!String.IsNullOrEmpty(fileName)) 
  				{
  					using (FileStream streamWriter = File.Create(Path.Combine(DestinationFolder, theEntry.Name))) 
  					{
  						int size = 2048;
  						byte[] data = new byte[2048];
  						while (true) 
  						{
  							size = s.Read(data, 0, data.Length);
  							if (size > 0) 
  							{
  								streamWriter.Write(data, 0, size);
  							} 
  							else
  							{
  								break;
  							}
  						}
  					}
  				}
  			}
  		}
  		return true;
  	}
    
  }
}