install.ps1 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. param($installPath, $toolsPath, $package, $project)
  2. Set-StrictMode -version 2.0
  3. ##-------------------------------------------------
  4. ## Globals
  5. ##-------------------------------------------------
  6. [string] $basePath = "Registry::HKEY_CURRENT_USER\Software\Xceed Software"
  7. [string] $licensesPath = $basePath + '\' + 'Licenses'
  8. [byte[]] $NbDaysBits = 2, 7, 12, 17, 22, 26, 31, 37, 42, 47, 51, 55, 59, 62, 0xFF
  9. [byte[]] $ProductCodeBits = 3, 16, 29, 41, 53, 61, 0xFF
  10. [byte[]] $ProductVersionBits = 4, 15, 25, 34, 43, 50, 58, 0xFF
  11. [byte[]] $ChecksumBits = 0, 9, 18, 27, 36, 45, 54, 63, 0xFF
  12. [string] $AlphaNumLookup = "ABJCKTDL4UEMW71FNX52YGP98Z63HRS0"
  13. [string[][]] $PackagesMap =
  14. ('DGP','Xceed.Products.Wpf.DataGrid.Base'),`
  15. ('DGP','Xceed.Products.Wpf.DataGrid.Full'),`
  16. ('DGP','Xceed.Products.Wpf.DataGrid.Themes'),`
  17. ('WTK','Extended.Wpf.Toolkit.Plus'),`
  18. ('WTK','Xceed.Products.Wpf.Toolkit.AvalonDock'),`
  19. ('WTK','Xceed.Products.Wpf.Toolkit.AvalonDock.Themes'),`
  20. ('WTK','Xceed.Products.Wpf.Toolkit.Base'),`
  21. ('WTK','Xceed.Products.Wpf.Toolkit.Base.Themes'),`
  22. ('WTK','Xceed.Products.Wpf.Toolkit.ListBox'),`
  23. ('WTK','Xceed.Products.Wpf.Toolkit.ListBox.Themes'),`
  24. ('WDN','Xceed.Products.Documents.Libraries.Full'),`
  25. ('ZIN','Xceed.Products.Zip.NET'),`
  26. ('FTN','Xceed.Products.Ftp.NET'),`
  27. ('ZRT','Xceed.Products.RTZip.NET'),`
  28. ('SFT','Xceed.Products.SFtp.NET'),`
  29. ('','')
  30. [string[][]] $ProductIds =
  31. ('',''),`
  32. ('ZIP',''),`
  33. ('SFX',''),`
  34. ('BKP',''),`
  35. ('WSL',''),`
  36. ('FTP',''),`
  37. ('SCO',''),`
  38. ('BEN',''),`
  39. ('CRY',''),`
  40. ('FTB',''),`
  41. ('ZIN','https://xceed.com/xceed-zip-for-net/'),`
  42. ('ABZ',''),`
  43. ('GRD',''),`
  44. ('SCN',''),`
  45. ('ZIC',''),`
  46. ('SCC',''),`
  47. ('SUI',''),`
  48. ('SUN',''),`
  49. ('FTN',''),`
  50. ('FTC',''),`
  51. ('CHT',''),`
  52. ('DWN',''),`
  53. ('CHW',''),`
  54. ('IVN',''),`
  55. ('RDY',''),`
  56. ('EDN',''),`
  57. ('ZIL',''),`
  58. ('TAN',''),`
  59. ('DGF',''),`
  60. ('DGP','https://xceed.com/xceed-datagrid-for-wpf/'),`
  61. ('WAN',''),`
  62. ('SYN',''),`
  63. ('ZIX',''),`
  64. ('ZII',''),`
  65. ('SFN',''),`
  66. ('ZRT','https://xceed.com/xceed-real-time-zip-for-net/'),`
  67. ('ZRC',''),`
  68. ('UPS',''),`
  69. ('TDV',''),`
  70. ('ZRS',''),`
  71. ('XPT',''),`
  72. ('OFT',''),`
  73. ('GLT',''),`
  74. ('MET',''),`
  75. ('LET',''),`
  76. ('WST',''),`
  77. ('DGS',''),`
  78. ('LBS',''),`
  79. ('ZRP',''),`
  80. ('UPP',''),`
  81. ('LBW',''),`
  82. ('BLD',''),`
  83. ('SFT','https://xceed.com/xceed-sftp-for-net/'),`
  84. ('WTK','https://xceed.com/xceed-toolkit-plus-for-wpf/'),`
  85. ('WDN','https://xceed.com/xceed-words-for-net/')
  86. function shl{
  87. param([System.UInt32] $value, [byte] $nb = 1)
  88. for([System.Int32] $i=0;$i -lt $nb;$i++)
  89. {
  90. $value = $value -band 0x7FFFFFFF
  91. $value *= 2
  92. }
  93. return $value
  94. }
  95. function shr{
  96. param([System.UInt32] $value, [byte] $nb = 1)
  97. for([System.Int32] $i=0;$i -lt $nb;$i++)
  98. {
  99. $value = (($value-($value%2)) / 2)
  100. }
  101. return $value
  102. }
  103. ##-------------------------------------------------
  104. ## Functions
  105. ##-------------------------------------------------
  106. function MapBits{
  107. param([System.Collections.BitArray] $barray, [System.UInt32] $val, [byte[]] $codeBits)
  108. for( [int] $i = 0; $i -lt ($codeBits.Length - 1); $i++ )
  109. {
  110. [int] $x = shl 1 $i
  111. $ba[ $codeBits[ $i ] ] = ( $val -band $x ) -ne 0
  112. }
  113. }
  114. function GetBytes{
  115. param([System.Collections.BitArray] $ba)
  116. [byte[]] $array = New-Object System.Byte[] (9)
  117. for( [byte] $i = 0; $i -lt $ba.Length; $i++ )
  118. {
  119. if($ba[$i])
  120. {
  121. [int] $mod = ($i % 8)
  122. [int] $index = ( $i - $mod ) / 8
  123. $array[ $index ] = ($array[ $index ]) -bor ([byte]( shr 128 $mod ))
  124. }
  125. }
  126. return $array
  127. }
  128. function CalculateChecksum{
  129. param([System.UInt16[]] $b )
  130. [System.UInt16] $dw1 = 0
  131. [System.UInt16] $dw2 = 0
  132. for([int] $i=0;$i -lt $b.Length;$i++)
  133. {
  134. $dw1 += $b[ $i ]
  135. $dw2 += $dw1
  136. }
  137. ##Reduce to 8 bits
  138. [System.UInt16] $r1 = ($dw2 -bxor $dw1)
  139. [byte] $r2 = (shr $r1 8) -bxor ($r1 -band 0x00FF)
  140. return $r2
  141. }
  142. function GenAlpha {
  143. param([System.Collections.BitArray] $ba)
  144. [string] $suffix = ''
  145. [int] $mask = 0x10
  146. [int] $value = 0
  147. for( [int] $i = 0; $i -lt $ba.Length;$i++)
  148. {
  149. if( $mask -eq 0 )
  150. {
  151. $suffix += $AlphaNumLookup[ $value ]
  152. $value = 0
  153. $mask = 0x10
  154. }
  155. if( $ba[ $i ] )
  156. {
  157. $value = $value -bor $mask
  158. }
  159. $mask = shr $mask
  160. }
  161. $suffix += $AlphaNumLookup[ $value ]
  162. return $suffix + 'A';
  163. }
  164. function FindId {
  165. param([string] $id)
  166. [string] $prodId = ''
  167. for( [int] $i = 0; $i -lt $PackagesMap.Length;$i++)
  168. {
  169. if($PackagesMap[$i][1] -eq $id)
  170. {
  171. $prodId = $PackagesMap[$i][0]
  172. break
  173. }
  174. }
  175. if($prodId -ne '')
  176. {
  177. for( [int] $i = 0; $i -lt $ProductIds.Length;$i++)
  178. {
  179. if($ProductIds[$i][0] -eq $prodId)
  180. {
  181. return $i
  182. }
  183. }
  184. }
  185. return -1
  186. }
  187. function Create {
  188. param([int] $pIndex, [int] $maj, [int] $min)
  189. ## Harcode others values that we dont need to customize.
  190. $ba = New-Object System.Collections.BitArray 65
  191. $ba[6] = $true
  192. $ba[64] = $true
  193. [System.DateTime] $date = New-Object -t DateTime -a 2000,11,17
  194. [int] $days = [DateTime]::Today.Subtract($date).Days
  195. [int] $verNo = ($maj*10) + $min
  196. [string] $pPrefix = $ProductIds[$pIndex][0]
  197. [string] $prodId = "$pPrefix$verNo"
  198. MapBits $ba $pIndex $ProductCodeBits
  199. MapBits $ba $verNo $ProductVersionBits
  200. MapBits $ba $days $NbDaysBits
  201. [char[]] $a1 = $prodId.ToCharArray()
  202. [byte[]] $a2 = GetBytes $ba
  203. [System.UInt16[]] $a = New-Object System.UInt16[] ($a1.Length + $a2.Length)
  204. [System.Array]::Copy($a1,0,$a,0,$a1.Length)
  205. [System.Array]::Copy($a2,0,$a,$a1.Length,$a2.Length)
  206. [byte] $checksum = CalculateChecksum $a
  207. MapBits $ba $checksum $ChecksumBits
  208. return $prodId + (GenAlpha $ba)
  209. }
  210. function TestAndCreate {
  211. param([string] $path)
  212. if(!(Test-Path $path))
  213. {
  214. $dump = New-Item $path
  215. }
  216. }
  217. function Setup {
  218. param([int] $pIndex, [int] $major, [int] $minor)
  219. try
  220. {
  221. if($pIndex -lt 0)
  222. {
  223. Write-Host "Failed to find the product."
  224. return
  225. }
  226. if(($major -lt 0) -or ($major -gt 9) -or ($minor -lt 0) -or ($minor -gt 9))
  227. {
  228. Write-Host "Failed to generate a license key."
  229. return
  230. }
  231. [string] $prodPath = $licensesPath + '\' + $ProductIds[$pIndex][0]
  232. [string] $prodVer = "$major.$minor"
  233. TestAndCreate $basePath
  234. TestAndCreate $licensesPath
  235. TestAndCreate $prodPath
  236. [Microsoft.Win32.RegistryKey] $path = Get-Item $prodPath
  237. if($path.GetValue($prodVer, $null) -eq $null)
  238. {
  239. [string] $k = Create $pIndex $major $minor
  240. Set-ItemProperty -Path $prodPath -Name $prodVer -Value $k
  241. }
  242. }
  243. catch{}
  244. }
  245. function ParseVersion {
  246. param([string] $version)
  247. try
  248. {
  249. return [System.Version]::Parse($version)
  250. }
  251. catch
  252. {
  253. return $null
  254. }
  255. }
  256. function GetVSVersion {
  257. try
  258. {
  259. return ParseVersion $dte.Version
  260. }
  261. catch
  262. {
  263. return $null
  264. }
  265. }
  266. function GetPackageVersion {
  267. [System.Version] $vs = GetVSVersion
  268. [System.Version] $retval = $null
  269. if($vs -ne $null)
  270. {
  271. ## Visual Studio 2015 and later
  272. if($vs.Major -ge 14)
  273. {
  274. $retval = ParseVersion $package.Version
  275. }
  276. ## Visual Studio 2013 and earlier
  277. elseif($vs.Major -gt 0)
  278. {
  279. $retval = $package.Version.Version
  280. }
  281. }
  282. return $retval
  283. }
  284. function ExtractPackageVersion {
  285. [System.Version] $version = GetPackageVersion
  286. if($version -ne $null)
  287. {
  288. return $version.Major, $version.Minor
  289. }
  290. else
  291. {
  292. return -1, -1
  293. }
  294. }
  295. ##-------------------------------------------------
  296. ## Entry Point (Main)
  297. ##-------------------------------------------------
  298. [int] $pIndex = FindId $package.Id
  299. if($pIndex -gt 0)
  300. {
  301. [int] $major = -1
  302. [int] $minor = -1
  303. $major, $minor = ExtractPackageVersion
  304. Setup $pIndex $major $minor
  305. [string] $pUrl = $ProductIds[$pIndex][1]
  306. if($pUrl.Length -gt 0)
  307. {
  308. [void] $project.DTE.ItemOperations.Navigate($pUrl)
  309. }
  310. }