forked from ArtifexSoftware/Ghostscript.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhostscriptVersionInfo.cs
More file actions
353 lines (293 loc) · 12.5 KB
/
GhostscriptVersionInfo.cs
File metadata and controls
353 lines (293 loc) · 12.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// GhostscriptVersionInfo.cs
// This file is part of Ghostscript.NET library
//
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan)
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using Microsoft.Win32;
namespace Ghostscript.NET
{
/// <summary>
/// Represents a Ghostscript version information.
/// </summary>
public class GhostscriptVersionInfo
{
#region Private local variables
private Version _version;
private string _dllPath;
private string _libPath;
private GhostscriptLicense _licenseType;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the Ghostscript.NET.GhostscriptVersionInfo class.
/// </summary>
/// <param name="version">Ghostscript version.</param>
/// <param name="dllPath">Ghostscript native library path.</param>
/// <param name="libPath">Ghostscript lib path.</param>
/// <param name="licenseType">Ghostscript license type.</param>
public GhostscriptVersionInfo(Version version, string dllPath, string libPath, GhostscriptLicense licenseType)
{
_version = version;
_dllPath = dllPath;
_libPath = libPath;
_licenseType = licenseType;
}
#endregion
public GhostscriptVersionInfo(string customDllPath)
{
_version = new Version();
_dllPath = customDllPath;
_libPath = string.Empty;
_licenseType = GhostscriptLicense.GPL;
}
#region Version
/// <summary>
/// Gets Ghostscript version.
/// </summary>
public Version Version
{
get { return _version; }
}
#endregion
#region DllPath
/// <summary>
/// Gets Ghostscript native library path.
/// </summary>
public string DllPath
{
get { return _dllPath; }
}
#endregion
#region LibPath
/// <summary>
/// Gets Ghostscrip lib path.
/// </summary>
public string LibPath
{
get { return _libPath; }
}
#endregion
#region LicenseType
/// <summary>
/// Gets Ghostscript license type.
/// </summary>
public GhostscriptLicense LicenseType
{
get { return _licenseType; }
}
#endregion
#region ToString
/// <summary>
/// Returns GhostscriptVersionInfo string.
/// </summary>
public override string ToString()
{
return string.Format("Licence: {0}, Version: {1}, Dll: {2}, Lib: {3}", _licenseType, _version, _dllPath, _libPath);
}
#endregion
#region GetInstalledVersions
/// <summary>
/// Gets installed Ghostscript versions list.
/// </summary>
public static List<GhostscriptVersionInfo> GetInstalledVersions()
{
return GetInstalledVersions(GhostscriptLicense.GPL | GhostscriptLicense.AFPL | GhostscriptLicense.Artifex);
}
#endregion
#region GetInstalledVersions
/// <summary>
/// Gets installed Ghostscript versions list.
/// </summary>
/// <returns>A GhostscriptVersionInfo list of the Ghostscript installations found on the local system.</returns>
public static List<GhostscriptVersionInfo> GetInstalledVersions(GhostscriptLicense licenseType)
{
// create a search list instance
List<GhostscriptLicense> licenses = new List<GhostscriptLicense>();
// check if we need to search for AFPL installations
if ((licenseType & GhostscriptLicense.AFPL) == GhostscriptLicense.AFPL)
{
// yep, add this license in the search list
licenses.Add(GhostscriptLicense.AFPL);
}
// check if we need to search for GPL installations
if ((licenseType & GhostscriptLicense.GPL) == GhostscriptLicense.GPL)
{
// yep, add this license in the search list
licenses.Add(GhostscriptLicense.GPL);
}
// check if we need to search for GPL installations
if ((licenseType & GhostscriptLicense.Artifex) == GhostscriptLicense.Artifex)
{
// yep, add this license in the search list
licenses.Add(GhostscriptLicense.Artifex);
}
// create new return list instance
List<GhostscriptVersionInfo> versions = new List<GhostscriptVersionInfo>();
// loop through the search list
foreach (GhostscriptLicense license in licenses)
{
RegistryKey hklm = null;
RegistryKey rkGs = null;
// check if we are running in 64 bit process
if (Environment.Is64BitProcess)
{
// user 64 bit registry key
hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
// user 32 bit registry key
hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
}
// check the license type
if (license == GhostscriptLicense.AFPL)
{
// get the AFPL registry key
rkGs = hklm.OpenSubKey("SOFTWARE\\AFPL Ghostscript\\");
}
else if (license == GhostscriptLicense.GPL)
{
// get the GPL registry key
rkGs = hklm.OpenSubKey("SOFTWARE\\GPL Ghostscript\\");
}
else if (license == GhostscriptLicense.Artifex)
{
rkGs = hklm.OpenSubKey("SOFTWARE\\Artifex Ghostscript\\");
}
// check if we found the registry key
if (rkGs != null)
{
// get this registry key sub-keys
// each sub-key represents a version of the installed Ghostscript library
string[] subkeys = rkGs.GetSubKeyNames();
// loop through all sub-keys
for (int index = 0; index < subkeys.Length; index++)
{
// get the subkey / Ghostscript library version
string versionKey = subkeys[index];
try
{
// open the sub key
RegistryKey rkVer = rkGs.OpenSubKey(versionKey);
// get the Ghostscript native library path
string gsdll = rkVer.GetValue("GS_DLL", string.Empty) as string;
// get the Ghostscript lib path
string gslib = rkVer.GetValue("GS_LIB", string.Empty) as string;
bool compatibile = false;
// check if we can use this dll in this process
if (System.Environment.Is64BitProcess && gsdll.Contains("gsdll64.dll"))
{
// both process and dll are 64 bit, we can use it
compatibile = true;
}
else if (!System.Environment.Is64BitProcess && gsdll.Contains("gsdll32.dll"))
{
// both process and dll are 32 bit, we can use it
compatibile = true;
}
if (compatibile)
{
// put this version in the return list
versions.Add(new GhostscriptVersionInfo(new Version(versionKey), gsdll, gslib, license));
}
}
catch { }
}
}
}
return versions;
}
#endregion
#region GetLastInstalledVersion
/// <summary>
/// Gets top installed Ghostscript version.
/// </summary>
public static GhostscriptVersionInfo GetLastInstalledVersion()
{
return GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL | GhostscriptLicense.Artifex, GhostscriptLicense.GPL);
}
#endregion
#region GetLastInstalledVersion
/// <summary>
/// Gets top installed Ghostscript version.
/// </summary>
/// <param name="licenseType">Serch for the specific Ghostscript version based on the Ghostscript license.</param>
/// <param name="licensePriority">If there are both license types installed, which one should have the prilorty.</param>
/// <returns>GhostscriptVersionInfo object of the last installed Ghostscript version based on priority license.</returns>
public static GhostscriptVersionInfo GetLastInstalledVersion(GhostscriptLicense licenseType, GhostscriptLicense licensePriority)
{
// gets installed Ghostscript versions list
List<GhostscriptVersionInfo> gsVerList = GetInstalledVersions(licenseType);
// cache the list count
int versionsCount = gsVerList.Count;
// check if there is only 1 version of the Ghostscript installed
// if yes, then we don't need a deeper search
if (versionsCount == 1)
{
// simply return the first one
return gsVerList[0];
}
else if (versionsCount > 1)
{
// get the first one
GhostscriptVersionInfo lastGsVer = gsVerList[0];
// loop through all others
for (int index = 1; index < versionsCount; index++)
{
// get one from the list
GhostscriptVersionInfo gs = gsVerList[index];
// compare if it's a newer version
if (gs.Version > lastGsVer.Version)
{
// check if this version has license with larger priority
if (gs.LicenseType == licensePriority)
{
// set top version
lastGsVer = gsVerList[index];
}
}
}
// return top GhostscriptVersionInfo instance
return lastGsVer;
}
// inform the user that we didn't find Ghostscript installed on this system
throw new GhostscriptLibraryNotInstalledException();
}
#endregion
#region IsGhostscriptInstalled
/// <summary>
/// Gets if the Ghostscript is installed on the local system.
/// </summary>
public static bool IsGhostscriptInstalled
{
get
{
// check for any, GPL or AFPL version
return GetInstalledVersions(GhostscriptLicense.GPL | GhostscriptLicense.AFPL | GhostscriptLicense.Artifex).Count > 0;
}
}
#endregion
}
}