Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AddinDatabase.cs « Mono.Addins.Database « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73cb55c4c7a807b8f32512f19eb1b04f9bc27cb6 (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
//
// AddinDatabase.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using Mono.Addins.Description;

namespace Mono.Addins.Database
{
    class AddinDatabase
	{
		public const string GlobalDomain = "global";
		public const string UnknownDomain = "unknown";
		
		public const string VersionTag = "004";

		readonly AddinEngine addinEngine;
		readonly AddinRegistry registry;
		readonly string addinDbDir;
		readonly FileDatabase fileDatabase;
		readonly object localLock = new object ();

		bool allSetupInfosLoaded;
		ImmutableArray<Addin> allSetupInfos;
		ImmutableArray<Addin> addinSetupInfos;
		ImmutableArray<Addin> rootSetupInfos;

		Dictionary<string, Addin> cachedAddinSetupInfos = new Dictionary<string, Addin> ();
		ImmutableAddinHostIndex hostIndex;

		internal static bool RunningSetupProcess;

		bool fatalDatabseError;
		DatabaseConfiguration config = null;
		int lastDomainId;
		AddinFileSystemExtension fileSystemExtension = new AddinFileSystemExtension ();
		List<object> extensions = new List<object> ();
		
		public AddinDatabase (AddinEngine addinEngine, AddinRegistry registry)
		{
			this.addinEngine = addinEngine;
			this.registry = registry;
			addinDbDir = Path.Combine (registry.AddinCachePath, "addin-db-" + VersionTag);
			fileDatabase = new FileDatabase (AddinDbDir);
		}

		public AddinDatabaseTransaction BeginTransaction (ExtensionContextTransaction addinEngineTransaction = null)
		{
			return new AddinDatabaseTransaction (this, localLock, addinEngineTransaction);
		}

		internal AddinEngine AddinEngine => addinEngine;

		string AddinDbDir {
			get { return addinDbDir; }
		}
		
		public AddinFileSystemExtension FileSystem {
			get { return fileSystemExtension; }
		}
		
		public string AddinCachePath {
			get { return Path.Combine (AddinDbDir, "addin-data"); }
		}
		
		public string AddinFolderCachePath {
			get { return Path.Combine (AddinDbDir, "addin-dir-data"); }
		}
		
		public string AddinPrivateDataPath {
			get { return Path.Combine (AddinDbDir, "addin-priv-data"); }
		}
		
		public string HostsPath {
			get { return Path.Combine (AddinDbDir, "hosts"); }
		}
		
		string HostIndexFile {
			get { return Path.Combine (AddinDbDir, "host-index"); }
		}
		
		string ConfigFile {
			get { return Path.Combine (AddinDbDir, "config.xml"); }
		}
		
		internal bool IsGlobalRegistry {
			get {
				return registry.RegistryPath == AddinRegistry.GlobalRegistryPath;
			}
		}
		
		public AddinRegistry Registry {
			get {
				return this.registry;
			}
		}
		
		public void CopyExtensions (AddinDatabase other)
		{
			lock (extensions) {
				foreach (object o in other.extensions)
					RegisterExtension (o);
			}
		}
		
		public void RegisterExtension (object extension)
		{
			lock (extensions) {
				extensions.Add (extension);
				if (extension is AddinFileSystemExtension)
					fileSystemExtension = (AddinFileSystemExtension)extension;
				else
					throw new NotSupportedException ();
			}
		}
		
		public void UnregisterExtension (object extension)
		{
			lock (extensions) {
				extensions.Remove (extension);
				if ((extension as AddinFileSystemExtension) == fileSystemExtension)
					fileSystemExtension = new AddinFileSystemExtension ();
				else
					throw new InvalidOperationException ();
			}
		}
		
		public ExtensionNodeSet FindNodeSet (string domain, string addinId, string id)
		{
			return FindNodeSet (domain, addinId, id, new Hashtable ());
		}
		
		ExtensionNodeSet FindNodeSet (string domain, string addinId, string id, Hashtable visited)
		{
			if (visited.Contains (addinId))
				return null;
			visited.Add (addinId, addinId);
			Addin addin = GetInstalledAddin (domain, addinId, true, false);
			if (addin == null)
				return null;
			AddinDescription desc = addin.Description;
			if (desc == null)
				return null;
			foreach (ExtensionNodeSet nset in desc.ExtensionNodeSets)
				if (nset.Id == id)
					return nset;
			
			// Not found in the add-in. Look on add-ins on which it depends
			
			foreach (Dependency dep in desc.MainModule.Dependencies) {
				AddinDependency adep = dep as AddinDependency;
				if (adep == null) continue;
				
				string aid = Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version);
				ExtensionNodeSet nset = FindNodeSet (domain, aid, id, visited);
				if (nset != null)
					return nset;
			}
			return null;
		}

		public IEnumerable<Addin> GetInstalledAddins (string domain, AddinSearchFlagsInternal flags)
		{
			if (domain == null)
				domain = registry.CurrentDomain;
			
			// Get the cached list if the add-in list has already been loaded.
			// The domain doesn't have to be checked again, since it is always the same
			
			return InternalGetInstalledAddins (domain, null, flags & ~AddinSearchFlagsInternal.LatestVersionsOnly, false);
		}
		
		IEnumerable<Addin> InternalGetInstalledAddins (string domain, AddinSearchFlagsInternal type, bool dbIsLockedForRead)
		{
			return InternalGetInstalledAddins (domain, null, type, dbIsLockedForRead);
		}
		
		IEnumerable<Addin> InternalGetInstalledAddins (string domain, string idFilter, AddinSearchFlagsInternal type, bool dbIsLockedForRead)
		{
			if (!allSetupInfosLoaded) {
				lock (localLock) {
					if (!allSetupInfosLoaded) {
						Dictionary<string, Addin> adict = new Dictionary<string, Addin> ();

						using (!dbIsLockedForRead ? fileDatabase.LockRead() : null) {
							// Global add-ins are valid for any private domain
							if (domain != AddinDatabase.GlobalDomain)
								FindInstalledAddins (adict, AddinDatabase.GlobalDomain);

							FindInstalledAddins (adict, domain);
						}
						List<Addin> alist = new List<Addin> (adict.Values);
						UpdateLastVersionFlags (alist);
						allSetupInfos = alist.ToImmutableArray ();
						addinSetupInfos = alist.Where (addin => !addin.Description.IsRoot).ToImmutableArray ();
						rootSetupInfos = alist.Where (addin => addin.Description.IsRoot).ToImmutableArray ();
						allSetupInfosLoaded = true;
					}
				}
			}
			IEnumerable<Addin> result;

			if ((type & AddinSearchFlagsInternal.IncludeAll) == AddinSearchFlagsInternal.IncludeAll) {
				result = allSetupInfos;
			} else if ((type & AddinSearchFlagsInternal.IncludeAddins) == AddinSearchFlagsInternal.IncludeAddins) {
				result = addinSetupInfos;
			} else {
				result = rootSetupInfos;
			}

			result = FilterById (result, idFilter);

			if ((type & AddinSearchFlagsInternal.LatestVersionsOnly) == AddinSearchFlagsInternal.LatestVersionsOnly)
				result = result.Where (a => a.IsLatestVersion);

			if ((type & AddinSearchFlagsInternal.ExcludePendingUninstall) == AddinSearchFlagsInternal.ExcludePendingUninstall)
				result = result.Where (a => !IsRegisteredForUninstall (a.Description.Domain, a.Id));
			return result;
		}

		IEnumerable<Addin> FilterById (IEnumerable<Addin> addins, string id)
		{
			if (id == null)
				return addins;
			return addins.Where (a => Addin.GetIdName (a.Id) == id);
		}

		void FindInstalledAddins (Dictionary<string,Addin> result, string domain)
		{
			string dir = Path.Combine (AddinCachePath, domain);
			if (Directory.Exists (dir)) {
				foreach (string file in fileDatabase.GetDirectoryFiles (dir, "*,*.maddin")) {
					string id = Path.GetFileNameWithoutExtension (file);
					if (!result.ContainsKey (id)) {
						var adesc = GetInstalledDomainAddin (domain, id, true, false, false);
						if (adesc != null)
							result.Add (id, adesc);
					}
				}
			}
		}
		
		void UpdateLastVersionFlags (List<Addin> addins)
		{
			Dictionary<string,string> versions = new Dictionary<string, string> ();
			foreach (Addin a in addins) {
				string last;
				string id, version;
				Addin.GetIdParts (a.Id, out id, out version);
				if (!versions.TryGetValue (id, out last) || Addin.CompareVersions (last, version) > 0)
					versions [id] = version;
			}
			foreach (Addin a in addins) {
				string id, version;
				Addin.GetIdParts (a.Id, out id, out version);
				a.IsLatestVersion = versions [id] == version;
			}
		}

		public Addin GetInstalledAddin (string domain, string id)
		{
			return GetInstalledAddin (domain, id, false, false);
		}
		
		public Addin GetInstalledAddin (string domain, string id, bool exactVersionMatch)
		{
			return GetInstalledAddin (domain, id, exactVersionMatch, false);
		}
		
		public Addin GetInstalledAddin (string domain, string id, bool exactVersionMatch, bool enabledOnly)
		{
			// Try the given domain, and if not found, try the shared domain
			Addin ad = GetInstalledDomainAddin (domain, id, exactVersionMatch, enabledOnly, true);
			if (ad != null)
				return ad;
			if (domain != AddinDatabase.GlobalDomain)
				return GetInstalledDomainAddin (AddinDatabase.GlobalDomain, id, exactVersionMatch, enabledOnly, true);
			else
				return null;
		}

		Addin GetInstalledDomainAddin (string domain, string id, bool exactVersionMatch, bool enabledOnly, bool dbLockCheck)
		{
			string idd = id + " " + domain;
			Addin sinfo;
			bool found;
			lock (cachedAddinSetupInfos) {
				found = cachedAddinSetupInfos.TryGetValue (idd, out sinfo);
			}

			if (found) {
				if (sinfo != null) {
					if (!enabledOnly || sinfo.Enabled)
						return sinfo;
					if (exactVersionMatch)
						return null;
				} else if (enabledOnly) {
					// Ignore the 'not installed' flag when disabled add-ins are allowed
					return null;
				}
			}
		
			if (dbLockCheck)
				InternalCheck (domain);

			string version, name;
			Addin.GetIdParts (id, out name, out version);

			using ((dbLockCheck ? fileDatabase.LockRead () : null))
			{
				if (sinfo == null && !string.IsNullOrEmpty (version)) {
					// If the same add-in with same version exists in both the global domain and the private domain,
					// take the instance in the global domain. This is an edge case, since in general add-ins will
					// have different versions. Taking the one from global domain in case of colision makes
					// it easier to "replace" an add-in bundled in an app for unit testing purposes.
					// So, look for an exact match in the global domain first:

					string foundDomain = null;

					string path = GetDescriptionPath (GlobalDomain, id);
					if (fileDatabase.Exists (path))
						foundDomain = GlobalDomain;
					else {
						path = GetDescriptionPath (domain, id);
						if (fileDatabase.Exists (path))
							foundDomain = domain;
					}
					if (foundDomain != null) {
						sinfo = new Addin (this, foundDomain, id);
						lock (cachedAddinSetupInfos) {
							cachedAddinSetupInfos [idd] = sinfo;
							if (!enabledOnly || sinfo.Enabled)
								return sinfo;
							if (exactVersionMatch) {
								// Cache lookups with negative result
								cachedAddinSetupInfos [idd] = null;
								return null;
							}
						}
					}
				}

				// Exact version not found. Look for a compatible version
				if (!exactVersionMatch) {
					sinfo = null;
					string bestVersion = null;
					Addin.GetIdParts (id, out name, out version);
					
					foreach (Addin ia in InternalGetInstalledAddins (domain, name, AddinSearchFlagsInternal.IncludeAll, true)) 
					{
						if ((!enabledOnly || ia.Enabled) &&
						    (version.Length == 0 || ia.SupportsVersion (version)) && 
						    (bestVersion == null || Addin.CompareVersions (bestVersion, ia.Version) > 0)) 
						{
							bestVersion = ia.Version;
							sinfo = ia;
						}
					}
					if (sinfo != null) {
						lock (cachedAddinSetupInfos) {
							cachedAddinSetupInfos [idd] = sinfo;
						}
						return sinfo;
					}
				}

				// Cache lookups with negative result
				// Ignore the 'not installed' flag when disabled add-ins are allowed
				if (enabledOnly) {
					lock (cachedAddinSetupInfos) {
						cachedAddinSetupInfos [idd] = null;
					}
				}
				return null;
			}
		}
		
		public void Shutdown ()
		{
			ResetCachedData ();
		}
		
		public Addin GetAddinForHostAssembly (string domain, string assemblyLocation)
		{
			InternalCheck (domain);
			Addin ainfo = null;

			lock (cachedAddinSetupInfos) {
				if (cachedAddinSetupInfos.TryGetValue (assemblyLocation, out var ob))
					return ob; // this can be null, if the add-in is disabled
			}

			var index = GetAddinHostIndex ();
			string addin, addinFile, rdomain;
			if (index.GetAddinForAssembly (assemblyLocation, out addin, out addinFile, out rdomain)) {
				string sid = addin + " " + rdomain;
				lock (cachedAddinSetupInfos) {
					if (!cachedAddinSetupInfos.TryGetValue(sid, out ainfo))
						ainfo = new Addin (this, rdomain, addin);
					cachedAddinSetupInfos [assemblyLocation] = ainfo;
					cachedAddinSetupInfos [sid] = ainfo;
				}
			}
			
			return ainfo;
		}
		
		
		public bool IsAddinEnabled (string domain, string id)
		{
			Addin ainfo = GetInstalledAddin (domain, id);
			if (ainfo != null)
				return ainfo.Enabled;
			else
				return false;
		}
		
		internal bool IsAddinEnabled (string domain, string id, bool exactVersionMatch)
		{
			if (!exactVersionMatch)
				return IsAddinEnabled (domain, id);
			Addin ainfo = GetInstalledAddin (domain, id, exactVersionMatch, false);
			if (ainfo == null)
				return false;
			return Configuration.IsEnabled (id, ainfo.AddinInfo.EnabledByDefault);
		}
		
		public void EnableAddin (string domain, string id)
		{
			EnableAddin (domain, id, true);
		}

		public void EnableAddin (string domain, string id, bool exactVersionMatch)
		{
			using var transaction = BeginTransaction ();
			EnableAddin (transaction, domain, id, exactVersionMatch);
		}

		void EnableAddin (AddinDatabaseTransaction dbTransaction, string domain, string id, bool exactVersionMatch)
		{
			Addin ainfo = GetInstalledAddin (domain, id, exactVersionMatch, false);
			if (ainfo == null)
				// It may be an add-in root
				return;

			if (IsAddinEnabled (domain, id))
				return;
			
			// Enable required add-ins
			
			foreach (Dependency dep in ainfo.AddinInfo.Dependencies) {
				if (dep is AddinDependency) {
					AddinDependency adep = dep as AddinDependency;
					string adepid = Addin.GetFullId (ainfo.AddinInfo.Namespace, adep.AddinId, adep.Version);
					EnableAddin (dbTransaction, domain, adepid, false);
				}
			}

			Configuration.SetEnabled (dbTransaction, id, true, ainfo.AddinInfo.EnabledByDefault, true);
			SaveConfiguration (dbTransaction);

			if (addinEngine != null && addinEngine.IsInitialized) {
				addinEngine.ActivateAddin (dbTransaction.GetAddinEngineTransaction(), id);
			}
		}

		public void DisableAddin (string domain, string id, bool exactVersionMatch = false, bool onlyForCurrentSession = false)
		{
			using var transaction = BeginTransaction ();
			DisableAddin (transaction, domain, id, exactVersionMatch, onlyForCurrentSession);
		}

		void DisableAddin (AddinDatabaseTransaction dbTransaction, string domain, string id, bool exactVersionMatch = false, bool onlyForCurrentSession = false)
		{
			Addin ai = GetInstalledAddin (domain, id, true);
			if (ai == null)
				throw new InvalidOperationException ("Add-in '" + id + "' not installed.");

			if (!IsAddinEnabled (domain, id, exactVersionMatch))
				return;

			Configuration.SetEnabled (dbTransaction, id, false, ai.AddinInfo.EnabledByDefault, exactVersionMatch, onlyForCurrentSession);
			SaveConfiguration (dbTransaction);
			
			// Disable all add-ins which depend on it
			
			try {
				string idName = Addin.GetIdName (id);
				
				foreach (Addin ainfo in GetInstalledAddins (domain, AddinSearchFlagsInternal.IncludeAddins)) {
					foreach (Dependency dep in ainfo.AddinInfo.Dependencies) {
						AddinDependency adep = dep as AddinDependency;
						if (adep == null)
							continue;
						
						string adepid = Addin.GetFullId (ainfo.AddinInfo.Namespace, adep.AddinId, null);
						if (adepid != idName)
							continue;
						
						// The add-in that has been disabled, might be a requirement of this one, or maybe not
						// if there is an older version available. Check it now.
						
						adepid = Addin.GetFullId (ainfo.AddinInfo.Namespace, adep.AddinId, adep.Version);
						Addin adepinfo = GetInstalledAddin (domain, adepid, false, true);
						
						if (adepinfo == null) {
							DisableAddin (dbTransaction, domain, ainfo.Id, onlyForCurrentSession: onlyForCurrentSession);
							break;
						}
					}
				}
			}
			catch {
				// If something goes wrong, enable the add-in again
				Configuration.SetEnabled (dbTransaction, id, true, ai.AddinInfo.EnabledByDefault, false, onlyForCurrentSession);
				SaveConfiguration (dbTransaction);
				throw;
			}

			if (addinEngine != null && addinEngine.IsInitialized) {
				addinEngine.UnloadAddin (dbTransaction.GetAddinEngineTransaction(), id);
			}
		}

		void UpdateEnabledStatus (AddinDatabaseTransaction transaction)
		{
			// Ensure that all enabled addins that have dependencies also have their dependencies enabled.
			HashSet<Addin> updatedAddins = new HashSet<Addin> ();
			var allAddins = GetInstalledAddins (registry.CurrentDomain, AddinSearchFlagsInternal.IncludeAddins | AddinSearchFlagsInternal.LatestVersionsOnly).ToList ();
			foreach (Addin addin in allAddins)
				UpdateEnabledStatus (transaction, registry.CurrentDomain, addin, allAddins, updatedAddins);
		}

		void UpdateEnabledStatus (AddinDatabaseTransaction transaction, string domain, Addin addin, List<Addin> allAddins, HashSet<Addin> updatedAddins)
		{
			if (!updatedAddins.Add (addin))
				return;

			if (!addin.Enabled)
				return;

			// Make sure all dependencies of this add-in have an up to date enabled status

			foreach (Dependency dep in addin.AddinInfo.Dependencies) {
				var adep = dep as AddinDependency;
				if (adep == null)
					continue;

				string adepid = Addin.GetFullId (addin.AddinInfo.Namespace, adep.AddinId, null);
				var dependency = allAddins.FirstOrDefault (a => Addin.GetFullId (a.Namespace, a.LocalId, null) == adepid);
				if (dependency != null) {
					UpdateEnabledStatus (transaction, domain, dependency, allAddins, updatedAddins);
					if (!dependency.Enabled) {
						// One of the dependencies is disabled, so this add-in also needs to be disabled.
						// However, we disabled only for the current configuration, we don't want to change
						// what the user configured.
						DisableAddin (transaction, domain, addin.Id, onlyForCurrentSession: true);
						return;
					}
				}
			}
		}

		public void RegisterForUninstall (string domain, string id, IEnumerable<string> files)
		{
			using var transaction = BeginTransaction ();
			DisableAddin (transaction, domain, id, true);
			Configuration.RegisterForUninstall (transaction, id, files);
			SaveConfiguration (transaction);
		}

		public bool IsRegisteredForUninstall (string domain, string addinId)
		{
			return Configuration.IsRegisteredForUninstall (addinId);
		}
		
		internal bool HasPendingUninstalls (string domain)
		{
			return Configuration.HasPendingUninstalls;
		}
		
		internal string GetDescriptionPath (string domain, string id)
		{
			return Path.Combine (Path.Combine (AddinCachePath, domain), id + ".maddin");
		}
		
		void InternalCheck (string domain)
		{
			// If the database is broken, don't try to regenerate it at every check.
			if (fatalDatabseError)
				return;

			bool update = false;
			using (fileDatabase.LockRead ()) {
				if (!Directory.Exists (AddinCachePath)) {
					update = true;
				}
			}
			if (update)
				Update (null, domain);
		}
		
		void GenerateAddinExtensionMapsInternal (IProgressStatus monitor, string domain, List<string> addinsToUpdate, List<string> addinsToUpdateRelations, List<string> removedAddins)
		{
			AddinUpdateData updateData = new AddinUpdateData (this, monitor);
			
			// Clear cached data
			lock(cachedAddinSetupInfos)
				cachedAddinSetupInfos.Clear ();
			
			// Collect all information
			
			AddinIndex addinHash = new AddinIndex ();
			
			if (monitor.LogLevel > 1)
				monitor.Log ("Generating add-in extension maps");
			
			Hashtable changedAddins = null;
			var descriptionsToSave = new List<AddinDescription> ();
			var files = new List<string> ();
			
			bool partialGeneration = addinsToUpdate != null;
			string[] domains = GetDomains ().Where (d => d == domain || d == GlobalDomain).ToArray ();
			
			// Get the files to be updated
			
			if (partialGeneration) {
				changedAddins = new Hashtable ();
				
				if (monitor.LogLevel > 2)
					monitor.Log ("Doing a partial registry update.\nAdd-ins to be updated:");
				// Get the files and ids of all add-ins that have to be updated
				// Include removed add-ins: if there are several instances of the same add-in, removing one of
				// them will make other instances to show up. If there is a single instance, its files are
				// already removed.
				foreach (string sa in addinsToUpdate.Union (removedAddins)) {
					changedAddins [sa] = sa;
					if (monitor.LogLevel > 2)
						monitor.Log (" - " + sa);
					foreach (string file in GetAddinFiles (sa, domains)) {
						if (!files.Contains (file)) {
							files.Add (file);
							string an = Path.GetFileNameWithoutExtension (file);
							changedAddins [an] = an;
							if (monitor.LogLevel > 2 && an != sa)
								monitor.Log (" - " + an);
						}
					}
				}
				
				if (monitor.LogLevel > 2)
					monitor.Log ("Add-ins whose relations have to be updated:");
				
				// Get the files and ids of all add-ins whose relations have to be updated
				foreach (string sa in addinsToUpdateRelations) {
					foreach (string file in GetAddinFiles (sa, domains)) {
						if (!files.Contains (file)) {
							if (monitor.LogLevel > 2) {
								string an = Path.GetFileNameWithoutExtension (file);
								monitor.Log (" - " + an);
							}
							files.Add (file);
						}
					}
				}
			}
			else {
				foreach (var dom in domains)
					files.AddRange (fileDatabase.GetDirectoryFiles (Path.Combine (AddinCachePath, dom), "*.maddin"));
			}
			
			// Load the descriptions.
			foreach (string file in files) {
			
				AddinDescription conf;
				if (!ReadAddinDescription (monitor, file, out conf)) {
					SafeDelete (monitor, file);
					continue;
				}

				// If the original file does not exist, the description can be deleted
				if (!fileSystemExtension.FileExists (conf.AddinFile)) {
					SafeDelete (monitor, file);
					continue;
				}
				
				// Remove old data from the description. Remove the data of the add-ins that
				// have changed. This data will be re-added later.
				
				conf.UnmergeExternalData (changedAddins);
				descriptionsToSave.Add (conf);
				
				addinHash.Add (conf);
			}

			// Sort the add-ins, to make sure add-ins are processed before
			// all their dependencies
			
			var sorted = addinHash.GetSortedAddins ();
			
			// Register extension points and node sets
			foreach (AddinDescription conf in sorted)
				CollectExtensionPointData (conf, updateData);
			
			if (monitor.LogLevel > 2)
				monitor.Log ("Registering new extensions:");
			
			// Register extensions
			foreach (AddinDescription conf in sorted) {
				if (changedAddins == null || changedAddins.ContainsKey (conf.AddinId)) {
					if (monitor.LogLevel > 2)
						monitor.Log ("- " + conf.AddinId + " (" + conf.Domain + ")");
					CollectExtensionData (monitor, addinHash, conf, updateData);
				}
			}
			
			// Save the maps
			foreach (AddinDescription conf in descriptionsToSave) {
				ConsolidateExtensions (conf);
				conf.SaveBinary (fileDatabase);
			}
			
			if (monitor.LogLevel > 1) {
				monitor.Log ("Addin relation map generated.");
				monitor.Log ("  Addins Updated: " + descriptionsToSave.Count);
				monitor.Log ("  Extension points: " + updateData.RelExtensionPoints);
				monitor.Log ("  Extensions: " + updateData.RelExtensions);
				monitor.Log ("  Extension nodes: " + updateData.RelExtensionNodes);
				monitor.Log ("  Node sets: " + updateData.RelNodeSetTypes);
			}
		}
		
		void ConsolidateExtensions (AddinDescription conf)
		{
			// Merges extensions with the same path
			
			foreach (ModuleDescription module in conf.AllModules) {
				Dictionary<string,Extension> extensions = new Dictionary<string, Extension> ();
				foreach (Extension ext in module.Extensions) {
					Extension mainExt;
					if (extensions.TryGetValue (ext.Path, out mainExt)) {
						var list = new List<ExtensionNodeDescription> ();
						EnsureInsertionsSorted (ext.ExtensionNodes);
						list.AddRange (ext.ExtensionNodes);
						int pos = -1;
						foreach (ExtensionNodeDescription node in list) {
							ext.ExtensionNodes.Remove (node);
							AddNodeSorted (mainExt.ExtensionNodes, node, ref pos);
						}
					} else {
						extensions [ext.Path] = ext;
						EnsureInsertionsSorted (ext.ExtensionNodes);
					}
				}
				
				// Sort the nodes
			}
		}
		
		void EnsureInsertionsSorted (ExtensionNodeDescriptionCollection list)
		{
			// Makes sure that the nodes in the collections are properly sorted wrt insertafter and insertbefore attributes
			Dictionary<string,ExtensionNodeDescription> added = new Dictionary<string, ExtensionNodeDescription> ();
			List<ExtensionNodeDescription> halfSorted = new List<ExtensionNodeDescription> ();
			bool orderChanged = false;
			
			for (int n = list.Count - 1; n >= 0; n--) {
				ExtensionNodeDescription node = list [n];
				if (node.Id.Length > 0)
					added [node.Id] = node;
				if (node.InsertAfter.Length > 0) {
					ExtensionNodeDescription relNode;
					if (added.TryGetValue (node.InsertAfter, out relNode)) {
						// Out of order. Move it before the referenced node
						int i = halfSorted.IndexOf (relNode);
						halfSorted.Insert (i, node);
						orderChanged = true;
					} else {
						halfSorted.Add (node);
					}
				} else
					halfSorted.Add (node);
			}
			halfSorted.Reverse ();
			List<ExtensionNodeDescription> fullSorted = new List<ExtensionNodeDescription> ();
			added.Clear ();
			
			foreach (ExtensionNodeDescription node in halfSorted) {
				if (node.Id.Length > 0)
					added [node.Id] = node;
				if (node.InsertBefore.Length > 0) {
					ExtensionNodeDescription relNode;
					if (added.TryGetValue (node.InsertBefore, out relNode)) {
						// Out of order. Move it before the referenced node
						int i = fullSorted.IndexOf (relNode);
						fullSorted.Insert (i, node);
						orderChanged = true;
					} else {
						fullSorted.Add (node);
					}
				} else
					fullSorted.Add (node);
			}
			if (orderChanged) {
				list.Clear ();
				foreach (ExtensionNodeDescription node in fullSorted)
					list.Add (node);
			}
		}
		
		void AddNodeSorted (ExtensionNodeDescriptionCollection list, ExtensionNodeDescription node, ref int curPos)
		{
			// Adds the node at the correct position, taking into account insertbefore and insertafter
			
			if (node.InsertAfter.Length > 0) {
				string afterId = node.InsertAfter;
				for (int n=0; n<list.Count; n++) {
					if (list[n].Id == afterId) {
						list.Insert (n + 1, node);
						curPos = n + 2;
						return;
					}
				}
			}
			else if (node.InsertBefore.Length > 0) {
				string beforeId = node.InsertBefore;
				for (int n=0; n<list.Count; n++) {
					if (list[n].Id == beforeId) {
						list.Insert (n, node);
						curPos = n + 1;
						return;
					}
				}
			}
			if (curPos == -1)
				list.Add (node);
			else
				list.Insert (curPos++, node);
		}

		
		IEnumerable GetAddinFiles (string fullId, string[] domains)
		{
			// Look for all versions of the add-in, because this id may be the id of a reference,
			// and the exact reference version may not be installed.
			string s = fullId;
			int i = s.LastIndexOf (',');
			if (i != -1)
				s = s.Substring (0, i);
			s += ",*";
			
			// Look for the add-in in any of the existing folders
			foreach (string domain in domains) {
				string mp = GetDescriptionPath (domain, s);
				string dir = Path.GetDirectoryName (mp);
				string pat = Path.GetFileName (mp);
				foreach (string fmp in fileDatabase.GetDirectoryFiles (dir, pat))
					yield return fmp;
			}
		}
		
		// Collects extension data in a hash table. The key is the path, the value is a list
		// of add-ins ids that extend that path
		
		void CollectExtensionPointData (AddinDescription conf, AddinUpdateData updateData)
		{
			foreach (ExtensionNodeSet nset in conf.ExtensionNodeSets) {
				try {
					updateData.RegisterNodeSet (conf, nset);
					updateData.RelNodeSetTypes++;
				} catch (Exception ex) {
					throw new InvalidOperationException ("Error reading node set: " + nset.Id, ex);
				}
			}
			
			foreach (ExtensionPoint ep in conf.ExtensionPoints) {
				try {
					updateData.RegisterExtensionPoint (conf, ep);
					updateData.RelExtensionPoints++;
				} catch (Exception ex) {
					throw new InvalidOperationException ("Error reading extension point: " + ep.Path, ex);
				}
			}
		}
		
		void CollectExtensionData (IProgressStatus monitor, AddinIndex addinHash, AddinDescription conf, AddinUpdateData updateData)
		{
			IEnumerable<string> missingDeps = addinHash.GetMissingDependencies (conf, conf.MainModule);
			if (missingDeps.Any ()) {
				string w = "The add-in '" + conf.AddinId + "' could not be updated because some of its dependencies are missing or not compatible:";
				w += BuildMissingAddinsList (addinHash, conf, missingDeps);
				monitor.ReportWarning (w);
				return;
			}
			
			CollectModuleExtensionData (conf, conf.MainModule, updateData, addinHash);
			
			foreach (ModuleDescription module in conf.OptionalModules) {
				missingDeps = addinHash.GetMissingDependencies (conf, module);
				if (missingDeps.Any ()) {
					if (monitor.LogLevel > 1) {
						string w = "An optional module of the add-in '" + conf.AddinId + "' could not be updated because some of its dependencies are missing or not compatible:";
						w += BuildMissingAddinsList (addinHash, conf, missingDeps);
					}
				}
				else
					CollectModuleExtensionData (conf, module, updateData, addinHash);
			}
		}
		
		string BuildMissingAddinsList (AddinIndex addinHash, AddinDescription conf, IEnumerable<string> missingDeps)
		{
			string w = "";
			foreach (string dep in missingDeps) {
				var found = addinHash.GetSimilarExistingAddin (conf, dep);
				if (found == null)
					w += "\n  missing: " + dep;
				else
					w += "\n  required: " + dep + ", found: " + found.AddinId;
			}
			return w;
		}
		
		void CollectModuleExtensionData (AddinDescription conf, ModuleDescription module, AddinUpdateData updateData, AddinIndex index)
		{
			foreach (Extension ext in module.Extensions) {
				updateData.RelExtensions++;
				updateData.RegisterExtension (conf, module, ext);
				AddChildExtensions (conf, module, updateData, index, ext.Path, ext.ExtensionNodes, false);
			}
		}
		
		void AddChildExtensions (AddinDescription conf, ModuleDescription module, AddinUpdateData updateData, AddinIndex index, string path, ExtensionNodeDescriptionCollection nodes, bool conditionChildren)
		{
			// Don't register conditions as extension nodes.
			if (!conditionChildren)
				updateData.RegisterExtension (conf, module, path);
			
			foreach (ExtensionNodeDescription node in nodes) {
				if (node.NodeName == "ComplexCondition")
					continue;
				updateData.RelExtensionNodes++;
				string id = node.GetAttribute ("id");
				if (id.Length != 0) {
					bool isCondition = node.NodeName == "Condition";
					if (isCondition) {
						// Find the add-in that provides the implementation for this condition.
						// Store that id in the condition. The add-in engine will ensure the add-in
						// is loaded when it tries to evaluate this condition.
						var condAsm = index.FindCondition (conf, module, id);
						if (condAsm != null)
							node.SetAttribute (Condition.SourceAddinAttribute, condAsm);
					}
					AddChildExtensions (conf, module, updateData, index, path + "/" + id, node.ChildNodes, isCondition);
				}
			}
		}
		
		string[] GetDomains ()
		{
			string[] dirs = fileDatabase.GetDirectories (AddinCachePath);
			string[] ids = new string [dirs.Length];
			for (int n=0; n<dirs.Length; n++)
				ids [n] = Path.GetFileName (dirs [n]);
			return ids;
		}

		public string GetUniqueDomainId ()
		{
			if (lastDomainId != 0) {
				lastDomainId++;
				return lastDomainId.ToString ();
			}
			lastDomainId = 1;
			foreach (string s in fileDatabase.GetDirectories (AddinCachePath)) {
				string dn = Path.GetFileName (s);
				if (dn == GlobalDomain)
					continue;
				try {
					int n = int.Parse (dn);
					if (n >= lastDomainId)
						lastDomainId = n + 1;
				} catch {
				}
			}
			return lastDomainId.ToString ();
		}

		internal void ResetBasicCachedData ()
		{
			lock(localLock)
				allSetupInfosLoaded = false;
		}

		internal void ResetCachedData (AddinDatabaseTransaction dbTransaction = null)
		{
			ResetBasicCachedData ();
			hostIndex = null;
			lock(cachedAddinSetupInfos)
				cachedAddinSetupInfos.Clear ();
			dependsOnCache.Clear ();
			if (addinEngine != null)
				addinEngine.ResetCachedData (dbTransaction?.GetAddinEngineTransaction());
		}

		Dictionary<string, HashSet<string>> dependsOnCache = new Dictionary<string, HashSet<string>> ();
		public bool AddinDependsOn (string domain, string id1, string id2)
		{
			var depTree = GetOrCreateAddInDependencyTree (domain, id1);
			return depTree.Contains (id2);
		}

		HashSet<string> GetOrCreateAddInDependencyTree (string domain, string addin)
		{
			HashSet<string> cache;
			if (dependsOnCache.TryGetValue (addin, out cache)) {
				return cache;
			}

			dependsOnCache [addin] = cache = new HashSet<string> ();

			Addin addin1 = GetInstalledAddin (domain, addin, false);

			// We can assume that if the add-in is not returned here, it may be a root addin.
			if (addin1 == null)
				return cache;
			
			foreach (Dependency dep in addin1.AddinInfo.Dependencies) {
				AddinDependency adep = dep as AddinDependency;
				if (adep == null)
					continue;
				
				string depid = Addin.GetFullId (addin1.AddinInfo.Namespace, adep.AddinId, null);
				cache.Add (depid);

				var recursiveDependencies = GetOrCreateAddInDependencyTree (domain, depid);
				cache.UnionWith (recursiveDependencies);
			}
			return cache;
		}

		public void GenerateScanDataFiles (IProgressStatus monitor, string folder, bool recursive)
		{
			ISetupHandler setup = GetSetupHandler ();
			setup.GenerateScanDataFiles (monitor, registry, Path.GetFullPath (folder), recursive);
		}

		public void Repair (IProgressStatus monitor, string domain, ScanOptions context = null)
		{
			using (fileDatabase.LockWrite ()) {
				try {
					if (Directory.Exists (AddinCachePath))
						Directory.Delete (AddinCachePath, true);
					if (Directory.Exists (AddinFolderCachePath))
						Directory.Delete (AddinFolderCachePath, true);
					if (File.Exists (HostIndexFile))
						File.Delete (HostIndexFile);
				}
				catch (Exception ex) {
					monitor.ReportError ("The add-in registry could not be rebuilt. It may be due to lack of write permissions to the directory: " + AddinDbDir, ex);
				}
			}
			ResetBasicCachedData ();
			
			Update (monitor, domain, context);
		}

		public void Update(IProgressStatus monitor, string domain, ScanOptions context = null, ExtensionContextTransaction addinEngineTransaction = null)
		{
			if (monitor == null)
				monitor = new ConsoleProgressStatus(false);

			if (RunningSetupProcess)
				return;

			fatalDatabseError = false;

			DateTime tim = DateTime.Now;

			var dbTransaction = BeginTransaction(addinEngineTransaction);

			RunPendingUninstalls(dbTransaction, monitor);

			Hashtable installed = new Hashtable();
			bool changesFound = CheckFolders(monitor, domain);

			if (monitor.IsCanceled)
				return;

			if (monitor.LogLevel > 1)
				monitor.Log("Folders checked (" + (int)(DateTime.Now - tim).TotalMilliseconds + " ms)");

			if (changesFound)
			{
				// Something has changed, the add-ins need to be re-scanned, but it has
				// to be done in an external process

				if (domain != null)
				{
					foreach (Addin ainfo in InternalGetInstalledAddins(domain, AddinSearchFlagsInternal.IncludeAddins, false))
					{
						installed[ainfo.Id] = ainfo.Id;
					}
				}

				RunScannerProcess(monitor, context);

				ResetCachedData(dbTransaction);

				registry.NotifyDatabaseUpdated();
			}

			if (fatalDatabseError)
				monitor.ReportError("The add-in database could not be updated. It may be due to file corruption. Try running the setup repair utility", null);

			// Update the currently loaded add-ins
			if (changesFound && domain != null && addinEngine != null && addinEngine.IsInitialized)
			{
				Hashtable newInstalled = new Hashtable();
				foreach (Addin ainfo in GetInstalledAddins(domain, AddinSearchFlagsInternal.IncludeAddins))
				{
					newInstalled[ainfo.Id] = ainfo.Id;
				}

				foreach (string aid in installed.Keys)
				{
					// Always try to unload, event if the add-in was not currently loaded.
					// Required since the add-ins has to be marked as 'disabled', to avoid
					// extensions from this add-in to be loaded
					if (!newInstalled.Contains(aid))
						addinEngine.UnloadAddin(dbTransaction.GetAddinEngineTransaction(), aid);
				}

				foreach (string aid in newInstalled.Keys)
				{
					if (!installed.Contains(aid))
					{
						Addin addin = addinEngine.Registry.GetAddin(aid);
						if (addin != null)
							addinEngine.ActivateAddin(dbTransaction.GetAddinEngineTransaction(), aid);
					}
				}
			}
			UpdateEnabledStatus(dbTransaction);
		}

		void RunPendingUninstalls (AddinDatabaseTransaction dbTransaction, IProgressStatus monitor)
		{
			bool changesDone = false;
			
			foreach (var adn in Configuration.GetPendingUninstalls ()) {
				HashSet<string> files = new HashSet<string> (adn.Files);
				if (AddinManager.CheckAssembliesLoaded (files))
					continue;
				
				if (monitor.LogLevel > 1)
					monitor.Log ("Uninstalling " + adn.AddinId);
				
				// Make sure all files can be deleted before doing so
				bool canUninstall = true;
				foreach (string f in adn.Files) {
					if (!File.Exists (f))
						continue;
					try {
						File.OpenWrite (f).Close ();
					} catch {
						canUninstall = false;
						break;
					}
				}
				
				if (!canUninstall)
					continue;
				
				foreach (string f in adn.Files) {
					try {
						if (File.Exists (f))
							File.Delete (f);
					} catch {
						canUninstall = false;
					}
				}
				
				if (canUninstall) {
					Configuration.UnregisterForUninstall (dbTransaction, adn.AddinId);
					changesDone = true;
				}
			}
			if (changesDone)
				SaveConfiguration (dbTransaction);
		}
		
		void RunScannerProcess (IProgressStatus monitor, ScanOptions context)
		{
			ISetupHandler setup = GetSetupHandler ();


			IProgressStatus scanMonitor = monitor;
			context = context ?? new ScanOptions ();

			if (fileSystemExtension.GetType () != typeof (AddinFileSystemExtension))
				context.FileSystemExtension = fileSystemExtension;

			bool retry = false;
			do {
				try {
					if (monitor.LogLevel > 1)
						monitor.Log ("Looking for addins");
					setup.Scan (scanMonitor, registry, null, context);
					retry = false;
				}
				catch (Exception ex) {
					ProcessFailedException pex = ex as ProcessFailedException;
					if (pex != null) {
						// Get the last logged operation.
						if (pex.LastLog.StartsWith ("scan:", StringComparison.Ordinal)) {
							// It crashed while scanning a file. Add the file to the ignore list and try again.
							string file = pex.LastLog.Substring (5);
							context.FilesToIgnore.Add (file);
							monitor.ReportWarning ("Could not scan file: " + file);
							retry = true;
							continue;
						}
					}
					fatalDatabseError = true;
					// If the process has crashed, try to do a new scan, this time using verbose log,
					// to give the user more information about the origin of the crash.
					if (pex != null && !retry) {
						monitor.ReportError ("Add-in scan operation failed. The runtime may have encountered an error while trying to load an assembly.", null);
						if (monitor.LogLevel <= 1) {
							// Re-scan again using verbose log, to make it easy to find the origin of the error.
							retry = true;
							scanMonitor = new ConsoleProgressStatus (true);
						}
					} else
						retry = false;
					
					if (!retry) {
						var pfex = ex as ProcessFailedException;
						monitor.ReportError ("Add-in scan operation failed", pfex != null? pfex.InnerException : ex);
						monitor.Cancel ();
						return;
					}
				}
			}
			while (retry);
		}
		
		bool DatabaseInfrastructureCheck (IProgressStatus monitor)
		{
			// Do some sanity check, to make sure the basic database infrastructure can be created
			
			bool hasChanges = false;
			
			try {
			
				if (!Directory.Exists (AddinCachePath)) {
					Directory.CreateDirectory (AddinCachePath);
					hasChanges = true;
				}
			
				if (!Directory.Exists (AddinFolderCachePath)) {
					Directory.CreateDirectory (AddinFolderCachePath);
					hasChanges = true;
				}
			
				// Make sure we can write in those folders

				Util.CheckWrittableFloder (AddinCachePath);
				Util.CheckWrittableFloder (AddinFolderCachePath);
				
				fatalDatabseError = false;
			}
			catch (Exception ex) {
				monitor.ReportError ("Add-in cache directory could not be created", ex);
				fatalDatabseError = true;
				monitor.Cancel ();
			}
			return hasChanges;
		}
		
		
		internal bool CheckFolders (IProgressStatus monitor, string domain)
		{
			using (fileDatabase.LockRead ()) {
				AddinScanResult scanResult = new AddinScanResult ();
				scanResult.CheckOnly = true;
				scanResult.Domain = domain;
				InternalScanFolders (monitor, scanResult);
				return scanResult.ChangesFound;
			}
		}
		
		internal void ScanFolders (IProgressStatus monitor, string currentDomain, string folderToScan, ScanOptions context)
		{
			AddinScanResult res = new AddinScanResult ();
			res.Domain = currentDomain;
			res.ScanContext.AddPathsToIgnore (context.FilesToIgnore);
			res.CleanGeneratedAddinScanDataFiles = context.CleanGeneratedAddinScanDataFiles;
			ScanFolders (monitor, res);
		}
		
		internal void GenerateScanDataFilesInProcess (IProgressStatus monitor, string folderToScan, bool recursive)
		{
			using (var visitor = new AddinScanDataFileGenerator (this, registry, folderToScan)) {
				visitor.VisitFolder (monitor, folderToScan, null, recursive);
			}
		}
		
		void ScanFolders (IProgressStatus monitor, AddinScanResult scanResult)
		{
			// All changes are done in a transaction, which won't be committed until
			// all files have been updated.
			
			if (!fileDatabase.BeginTransaction ()) {
				// The database is already being updated. Can't do anything for now.
				return;
			}
			
			try
			{
				// Perform the add-in scan
				
				InternalScanFolders (monitor, scanResult);
				
				fileDatabase.CommitTransaction ();
			}
			catch {
				fileDatabase.RollbackTransaction ();
				throw;
			}
		}

		void InternalScanFolders (IProgressStatus monitor, AddinScanResult scanResult)
		{
			try {
				fileSystemExtension.ScanStarted ();
				InternalScanFolders2 (monitor, scanResult);
			} finally {
				fileSystemExtension.ScanFinished ();
			}
		}
		
		void InternalScanFolders2 (IProgressStatus monitor, AddinScanResult scanResult)
		{
			DateTime tim = DateTime.Now;
			
			DatabaseInfrastructureCheck (monitor);
			if (monitor.IsCanceled)
				return;
			
			try {
				scanResult.HostIndex = new AddinHostIndex(GetAddinHostIndex ());
			}
			catch (Exception ex) {
				if (scanResult.CheckOnly) {
					scanResult.ChangesFound = true;
					return;
				}
				monitor.ReportError ("Add-in root index is corrupt. The add-in database will be regenerated.", ex);
				scanResult.RegenerateAllData = true;
			}
			
			var updater = new AddinRegistryUpdater (this, scanResult);

			// Check if any of the previously scanned folders has been deleted

			foreach (string file in Directory.EnumerateFiles (AddinFolderCachePath, "*.data")) {
				AddinScanFolderInfo folderInfo;
				bool res = ReadFolderInfo (monitor, file, out folderInfo);
				bool validForDomain = scanResult.Domain == null || folderInfo.Domain == GlobalDomain || folderInfo.Domain == scanResult.Domain;
				if (!res || (validForDomain && !fileSystemExtension.DirectoryExists (folderInfo.Folder))) {
					if (res) {
						// Folder has been deleted. Remove the add-ins it had.
						updater.UpdateDeletedAddins (monitor, folderInfo);
					} else {
						// Folder info file corrupt. Regenerate all.
						scanResult.ChangesFound = true;
						scanResult.RegenerateRelationData = true;
					}

					if (!scanResult.CheckOnly)
						SafeDelete (monitor, file);
					else if (scanResult.ChangesFound)
						return;
				}
			}

			// Look for changes in the add-in folders

			if (registry.StartupDirectory != null)
				updater.VisitFolder (monitor, registry.StartupDirectory, null, false);

			if (scanResult.CheckOnly && (scanResult.ChangesFound || monitor.IsCanceled))
				return;

			if (scanResult.Domain == null)
				updater.VisitFolder (monitor, HostsPath, GlobalDomain, false);

			if (scanResult.CheckOnly && (scanResult.ChangesFound || monitor.IsCanceled))
				return;

			foreach (string dir in registry.GlobalAddinDirectories) {
				if (scanResult.CheckOnly && (scanResult.ChangesFound || monitor.IsCanceled))
					return;
				updater.VisitFolder (monitor, dir, GlobalDomain, true);
			}

			if (scanResult.CheckOnly || !scanResult.ChangesFound)
				return;

			// Scan the files which have been modified

			// AssemblyIndex will contain all assemblies that were
			// found while looking for add-ins. Use it to resolve assemblies
			// while scanning those add-ins.

			using (var scanner = new AddinScanner (this, scanResult.AssemblyIndex)) {
				foreach (FileToScan file in scanResult.FilesToScan)
					scanner.ScanFile (monitor, file, scanResult, scanResult.CleanGeneratedAddinScanDataFiles);
			}

			// Save folder info
			
			foreach (AddinScanFolderInfo finfo in scanResult.ModifiedFolderInfos)
				SaveFolderInfo (monitor, finfo);

			if (monitor.LogLevel > 1)
				monitor.Log ("Folders scan completed (" + (int) (DateTime.Now - tim).TotalMilliseconds + " ms)");

			SaveAddinHostIndex (scanResult);
			ResetCachedData ();
			
			if (!scanResult.ChangesFound) {
				if (monitor.LogLevel > 1)
					monitor.Log ("No changes found");
				return;
			}
			
			tim = DateTime.Now;
			try {
				if (scanResult.RegenerateRelationData) {
					if (monitor.LogLevel > 1)
						monitor.Log ("Regenerating all add-in relations.");
					scanResult.AddinsToUpdate = null;
					scanResult.AddinsToUpdateRelations = null;
				}
				
				GenerateAddinExtensionMapsInternal (monitor, scanResult.Domain, scanResult.AddinsToUpdate, scanResult.AddinsToUpdateRelations, scanResult.RemovedAddins);
			}
			catch (Exception ex) {
				fatalDatabseError = true;
				monitor.ReportError ("The add-in database could not be updated. It may be due to file corruption. Try running the setup repair utility", ex);
			}
			
			if (monitor.LogLevel > 1)
				monitor.Log ("Add-in relations analyzed (" + (int) (DateTime.Now - tim).TotalMilliseconds + " ms)");
			
			SaveAddinHostIndex (scanResult);

			hostIndex = scanResult.HostIndex.ToImmutableAddinHostIndex ();
		}
		
		public void ParseAddin (IProgressStatus progressStatus, string domain, string file, string outFile, bool inProcess)
		{
			if (!inProcess) {
				ISetupHandler setup = GetSetupHandler ();
				setup.GetAddinDescription (progressStatus, registry, Path.GetFullPath (file), outFile);
				return;
			}
			
			using (fileDatabase.LockRead ())
			{
				// First of all, check if the file belongs to a registered add-in
				AddinScanFolderInfo finfo;
				if (GetFolderInfoForPath (progressStatus, Path.GetDirectoryName (file), out finfo) && finfo != null) {
					AddinFileInfo afi = finfo.GetAddinFileInfo (file);
					if (afi != null && afi.IsAddin) {
						AddinDescription adesc;
						GetAddinDescription (progressStatus, afi.Domain, afi.AddinId, file, out adesc);
						if (adesc != null)
							adesc.Save (outFile);
						return;
					}
				}
				
				AddinScanResult sr = new AddinScanResult ();
				sr.Domain = domain;

				var res = new AssemblyLocatorVisitor (this, registry, true);

				using (var scanner = new AddinScanner (this, res)) {
					AddinDescription desc = scanner.ScanSingleFile (progressStatus, file, sr);
					if (desc != null) {
						// Reset the xml doc so that it is not reused when saving. We want a brand new document
						desc.ResetXmlDoc ();
						desc.Save (outFile);
					}
				}
			}
		}
		
		public string GetFolderDomain (IProgressStatus progressStatus, string path)
		{
			AddinScanFolderInfo folderInfo;

			if (GetFolderInfoForPath (progressStatus, path, out folderInfo) && folderInfo == null) {
				if (path.Length > 0 && path [path.Length - 1] != Path.DirectorySeparatorChar)
					// Try again by appending a directory separator at the end. Some directories are registered like this.
					GetFolderInfoForPath (progressStatus, path + Path.DirectorySeparatorChar, out folderInfo);
				else if (path.Length > 0 && path [path.Length - 1] == Path.DirectorySeparatorChar)
					// Try again by removing the directory separator at the end. Some directories are registered like this.
					GetFolderInfoForPath (progressStatus, path.TrimEnd (Path.DirectorySeparatorChar), out folderInfo);
			}
			if (folderInfo != null && !string.IsNullOrEmpty (folderInfo.Domain))
				return folderInfo.Domain;
			else
				return UnknownDomain;
		}
		
		public string GetFolderConfigFile (string path)
		{
			path = Path.GetFullPath (path);
			
			string s = path.Replace ("_", "__");
			s = s.Replace (Path.DirectorySeparatorChar, '_');
			s = s.Replace (Path.AltDirectorySeparatorChar, '_');
			s = s.Replace (Path.VolumeSeparatorChar, '_');
			
			return Path.Combine (AddinFolderCachePath, s + ".data");
		}
		
		internal void UninstallAddin (IProgressStatus monitor, string domain, string addinId, string addinFile, AddinScanResult scanResult)
		{
			AddinDescription desc;
			
			if (!GetAddinDescription (monitor, domain, addinId, addinFile, out desc)) {
				// If we can't get information about the old assembly, just regenerate all relation data
				scanResult.RegenerateRelationData = true;
				return;
			}
			
			scanResult.AddRemovedAddin (addinId);
			
			// If the add-in didn't exist, there is nothing left to do
			
			if (desc == null)
				return;
			
			// If the add-in already existed, the dependencies of the old add-in need to be re-analyzed
			
			Util.AddDependencies (desc, scanResult);
			if (desc.IsRoot)
				scanResult.HostIndex.RemoveHostData (desc.AddinId, desc.AddinFile);

			RemoveAddinDescriptionFile (monitor, desc.FileName);
		}
		
		public bool GetAddinDescription (IProgressStatus monitor, string domain, string addinId, string addinFile, out AddinDescription description)
		{
			// If the same add-in is installed in different folders (in the same domain) there will be several .maddin files for it,
			// using the suffix "_X" where X is a number > 1 (for example: someAddin,1.0.maddin, someAddin,1.0.maddin_2, someAddin,1.0.maddin_3, ...)
			// We need to return the .maddin whose AddinFile matches the one being requested
			
			addinFile = Path.GetFullPath (addinFile);
			int altNum = 1;
			string baseFile = GetDescriptionPath (domain, addinId);
			string file = baseFile;
			bool failed = false;
			
			do {
				if (!ReadAddinDescription (monitor, file, out description)) {
					// Remove the AddinDescription here since it is corrupted.
					// Avoids creating alternate versions of corrupted files when later calling SaveDescription.
					RemoveAddinDescriptionFile (monitor, file);
					failed = true;
					continue;
				}
				if (description == null)
					break;
				if (Path.GetFullPath (description.AddinFile) == addinFile)
					return true;
				file = baseFile + "_" + (++altNum);
			}
			while (fileDatabase.Exists (file));
			
			// File not found. Return false only if there has been any read error.
			description = null;
			return failed;
		}
		
		bool RemoveAddinDescriptionFile (IProgressStatus monitor, string file)
		{
			// Removes an add-in description and shifts up alternate instances of the description file
			// (so xxx,1.0.maddin_2 will become xxx,1.0.maddin, xxx,1.0.maddin_3 -> xxx,1.0.maddin_2, etc)
			
			if (!SafeDelete (monitor, file))
				return false;
			
			int dversion;
			if (file.EndsWith (".maddin"))
				dversion = 2;
			else {
				int i = file.LastIndexOf ('_');
				dversion = 1 + int.Parse (file.Substring (i + 1));
				file = file.Substring (0, i);
			}

			while (fileDatabase.Exists (file + "_" + dversion)) {
				string newFile = dversion == 2 ? file : file + "_" + (dversion-1);
				try {
					fileDatabase.Rename (file + "_" + dversion, newFile);
				} catch (Exception ex) {
					if (monitor.LogLevel > 1) {
						monitor.Log ("Could not rename file '" + file + "_" + dversion + "' to '" + newFile + "'");
						monitor.Log (ex.ToString ());
					}
				}
				dversion++;
			}
			string dir = Path.GetDirectoryName (file);
			if (fileDatabase.DirectoryIsEmpty (dir))
				SafeDeleteDir (monitor, dir);
			
			if (dversion == 2) {
				// All versions of the add-in removed.
				SafeDeleteDir (monitor, Path.Combine (AddinPrivateDataPath, Path.GetFileNameWithoutExtension (file)));
			}
			
			return true;
		}
		
		public bool ReadAddinDescription (IProgressStatus monitor, string file, out AddinDescription description)
		{
			try {
				description = AddinDescription.ReadBinary (fileDatabase, file);
				if (description != null)
					description.OwnerDatabase = this;
				return true;
			}
			catch (Exception ex) {
				if (monitor == null)
					throw;
				description = null;
				monitor.ReportError ("Could not read folder info file", ex);
				return false;
			}
		}
		
		public bool SaveDescription (IProgressStatus monitor, AddinDescription desc, string replaceFileName)
		{
			try {
				if (replaceFileName != null)
					desc.SaveBinary (fileDatabase, replaceFileName);
				else {
					string file = GetDescriptionPath (desc.Domain, desc.AddinId);
					string dir = Path.GetDirectoryName (file);
					if (!fileDatabase.DirExists (dir))
						fileDatabase.CreateDir (dir);
					if (fileDatabase.Exists (file)) {
						// Another AddinDescription already exists with the same name.
						// Create an alternate AddinDescription file
						int altNum = 2;
						while (fileDatabase.Exists (file + "_" + altNum))
							altNum++;
						file = file + "_" + altNum;
					}
					desc.SaveBinary (fileDatabase, file);
				}
				return true;
			}
			catch (Exception ex) {
				monitor.ReportError ("Add-in info file could not be saved", ex);
				return false;
			}
		}
		
		public bool AddinDescriptionExists (string domain, string addinId)
		{
			string file = GetDescriptionPath (domain, addinId);
			return fileDatabase.Exists (file);
		}
		
		public bool ReadFolderInfo (IProgressStatus monitor, string file, out AddinScanFolderInfo folderInfo)
		{
			try {
				folderInfo = AddinScanFolderInfo.Read (fileDatabase, file);
				return true;
			}
			catch (Exception ex) {
				folderInfo = null;
				monitor.ReportError ("Could not read folder info file", ex);
				return false;
			}
		}
		
		public bool GetFolderInfoForPath (IProgressStatus monitor, string path, out AddinScanFolderInfo folderInfo)
		{
			try {
				folderInfo = AddinScanFolderInfo.Read (fileDatabase, AddinFolderCachePath, path);
				return true;
			}
			catch (Exception ex) {
				folderInfo = null;
				if (monitor != null)
					monitor.ReportError ("Could not read folder info file", ex);
				return false;
			}
		}

		public bool SaveFolderInfo (IProgressStatus monitor, AddinScanFolderInfo folderInfo)
		{
			try {
				folderInfo.Write (fileDatabase, AddinFolderCachePath);
				return true;
			}
			catch (Exception ex) {
				monitor.ReportError ("Could not write folder info file", ex);
				return false;
			}
		}
		
		public bool DeleteFolderInfo (IProgressStatus monitor, AddinScanFolderInfo folderInfo)
		{
			return SafeDelete (monitor, folderInfo.FileName);
		}
		
		public bool SafeDelete (IProgressStatus monitor, string file)
		{
			try {
				fileDatabase.Delete (file);
				return true;
			}
			catch (Exception ex) {
				if (monitor.LogLevel > 1) {
					monitor.Log ("Could not delete file: " + file);
					monitor.Log (ex.ToString ());
				}
				return false;
			}
		}
		
		public bool SafeDeleteDir (IProgressStatus monitor, string dir)
		{
			try {
				fileDatabase.DeleteDir (dir);
				return true;
			}
			catch (Exception ex) {
				if (monitor.LogLevel > 1) {
					monitor.Log ("Could not delete directory: " + dir);
					monitor.Log (ex.ToString ());
				}
				return false;
			}
		}

		ImmutableAddinHostIndex GetAddinHostIndex ()
		{
			if (hostIndex != null)
				return hostIndex;
			
			using (fileDatabase.LockRead ()) {
				if (fileDatabase.Exists (HostIndexFile))
					hostIndex = AddinHostIndex.ReadAsImmutable (fileDatabase, HostIndexFile);
				else
					hostIndex = new ImmutableAddinHostIndex ();
			}
			return hostIndex;
		}
		
		void SaveAddinHostIndex (AddinScanResult scanResult)
		{
			if (scanResult.HostIndex != null)
				scanResult.HostIndex.Write (fileDatabase, HostIndexFile);
		}

		internal string GetUniqueAddinId (string file, string oldId, string ns, string version)
		{
			string baseId = "__" + Path.GetFileNameWithoutExtension (file);

			if (Path.GetExtension (baseId) == ".addin")
				baseId = Path.GetFileNameWithoutExtension (baseId);
			
			string name = baseId;
			string id = Addin.GetFullId (ns, name, version);
			
			// If the old Id is already an automatically generated one, reuse it
			if (oldId != null && oldId.StartsWith (id))
				return name;
			
			int n = 1;
			while (AddinIdExists (id)) {
				name = baseId + "_" + n;
				id = Addin.GetFullId (ns, name, version);
				n++;
			}
			return name;
		}
		
		bool AddinIdExists (string id)
		{
			foreach (string d in fileDatabase.GetDirectories (AddinCachePath)) {
				if (fileDatabase.Exists (Path.Combine (d, id + ".addin")))
				    return true;
			}
			return false;
		}
		
		ISetupHandler GetSetupHandler ()
		{
			// .NET Core doesn't support domains, so it will always use SetupLocal, but it will
			// avoid loading assemblies by forcing the use of the cecil reflector
#if NET461
			if (fs.RequiresIsolation)
				return new SetupDomain ();
			else
#endif
				return new SetupLocal ();
		}
		
		public void ResetConfiguration ()
		{
			if (File.Exists (ConfigFile))
				File.Delete (ConfigFile);
			config = null;
			ResetCachedData ();
		}
		
		DatabaseConfiguration Configuration {
			get {
				if (config == null) {
					lock (localLock) {
						using (fileDatabase.LockRead ()) {
							if (fileDatabase.Exists (ConfigFile))
								config = DatabaseConfiguration.Read (ConfigFile);
							else
								config = DatabaseConfiguration.ReadAppConfig ();
						}
					}
				}
				return config;
			}
		}
		
		void SaveConfiguration (AddinDatabaseTransaction dbTransaction)
		{
			if (config != null) {
				using (fileDatabase.LockWrite ()) {
					config.Write (ConfigFile);
				}
			}
		}
	}
	
	class AddinIndex
	{
		Dictionary<string, List<AddinDescription>> addins = new Dictionary<string, List<AddinDescription>> ();
		
		public void Add (AddinDescription desc)
		{
			string id = Addin.GetFullId (desc.Namespace, desc.LocalId, null);
			List<AddinDescription> list;
			if (!addins.TryGetValue (id, out list))
				addins [id] = list = new List<AddinDescription> ();
			list.Add (desc);
		}
		
		List<AddinDescription> FindDescriptions (string domain, string fullid)
		{
			// Returns all registered add-ins which are compatible with the provided
			// fullid. Compatible means that the id is the same and the version is within
			// the range of compatible versions of the add-in.
			
			var res = new List<AddinDescription> ();
			string id = Addin.GetIdName (fullid);
			List<AddinDescription> list;
			if (!addins.TryGetValue (id, out list))
				return res;
			string version = Addin.GetIdVersion (fullid);
			foreach (AddinDescription desc in list) {
				if ((desc.Domain == domain || domain == AddinDatabase.GlobalDomain) && desc.SupportsVersion (version))
					res.Add (desc);
			}
			return res;
		}
		
		public IEnumerable<string> GetMissingDependencies (AddinDescription desc, ModuleDescription mod)
		{
			foreach (Dependency dep in mod.Dependencies) {
				AddinDependency adep = dep as AddinDependency;
				if (adep == null)
					continue;
				var descs = FindDescriptions (desc.Domain, adep.FullAddinId);
				if (descs.Count == 0)
					yield return adep.FullAddinId;
			}
		}
		
		public AddinDescription GetSimilarExistingAddin (AddinDescription conf, string addinId)
		{
			string domain = conf.Domain;
			List<AddinDescription> list;
			if (!addins.TryGetValue (Addin.GetIdName (addinId), out list))
				return null;
			string version = Addin.GetIdVersion (addinId);
			foreach (AddinDescription desc in list) {
				if ((desc.Domain == domain || domain == AddinDatabase.GlobalDomain) && !desc.SupportsVersion (version))
					return desc;
			}
			return null;
		}
		
		public string FindCondition (AddinDescription desc, ModuleDescription mod, string conditionId)
		{
			foreach (ConditionTypeDescription ctd in desc.ConditionTypes) {
				if (ctd.Id == conditionId)
					return desc.AddinId;
			}

			foreach (Dependency dep in mod.Dependencies) {
				AddinDependency adep = dep as AddinDependency;

				if (adep == null)
					continue;
				var descs = FindDescriptions (desc.Domain, adep.FullAddinId);
				foreach (var d in descs) {
					var c = FindCondition (d, d.MainModule, conditionId);
					if (c != null)
						return c;
				}
			}
			return null;
		}

		public List<AddinDescription> GetSortedAddins ()
		{
			var inserted = new HashSet<string> ();
			var lists = new Dictionary<string,List<AddinDescription>> ();
			
			foreach (List<AddinDescription> dlist in addins.Values) {
				foreach (AddinDescription desc in dlist)
					InsertSortedAddin (inserted, lists, desc);
			}
			
			// Merge all domain lists into a single list.
			// Make sure the global domain is inserted the last
			
			List<AddinDescription> global;
			lists.TryGetValue (AddinDatabase.GlobalDomain, out global);
			lists.Remove (AddinDatabase.GlobalDomain);
			
			List<AddinDescription> sortedAddins = new List<AddinDescription> ();
			foreach (var dl in lists.Values) {
				sortedAddins.AddRange (dl);
			}
			if (global != null)
				sortedAddins.AddRange (global);
			return sortedAddins;
		}

		void InsertSortedAddin (HashSet<string> inserted, Dictionary<string,List<AddinDescription>> lists, AddinDescription desc)
		{
			string sid = desc.AddinId + " " + desc.Domain;
			if (!inserted.Add (sid))
				return;

			foreach (ModuleDescription mod in desc.AllModules) {
				foreach (Dependency dep in mod.Dependencies) {
					AddinDependency adep = dep as AddinDependency;
					if (adep == null)
						continue;
					var descs = FindDescriptions (desc.Domain, adep.FullAddinId);
					if (descs.Count > 0) {
						foreach (AddinDescription sd in descs)
							InsertSortedAddin (inserted, lists, sd);
					}
				}
			}
			List<AddinDescription> list;
			if (!lists.TryGetValue (desc.Domain, out list))
				lists [desc.Domain] = list = new List<AddinDescription> ();
			
			list.Add (desc);
		}
	}

	class AddinDatabaseTransaction : IDisposable
	{
		readonly AddinDatabase addinDatabase;
		readonly object localLock;
		ExtensionContextTransaction addinEngineTransaction;
		bool addinEngineTransactionStarted;

		public AddinDatabaseTransaction (AddinDatabase addinDatabase, object localLock, ExtensionContextTransaction addinEngineTransaction)
		{
			this.addinDatabase = addinDatabase;
			this.localLock = localLock;
			this.addinEngineTransaction = addinEngineTransaction;
			Monitor.Enter (localLock);
		}

		public ExtensionContextTransaction GetAddinEngineTransaction()
		{
			if (addinEngineTransaction != null)
				return addinEngineTransaction;
			addinEngineTransactionStarted = true;
			return addinEngineTransaction = addinDatabase.AddinEngine.BeginTransaction();
		}

		public void Dispose ()
		{
			if (addinEngineTransactionStarted)
				addinEngineTransaction.Dispose();
			Monitor.Exit (localLock);
		}
	}

	// Keep in sync with AddinSearchFlags
	[Flags]
	enum AddinSearchFlagsInternal
	{
		IncludeAddins = 1,
		IncludeRoots = 1 << 1,
		IncludeAll = IncludeAddins | IncludeRoots,
		LatestVersionsOnly = 1 << 3,
		ExcludePendingUninstall = 1 << 4
	}
}