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

wm_files_link.c « intern « windowmanager « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 69858f755ec6e1de9f5b482f9371fabcb5826714 (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2007 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup wm
 *
 * Functions for dealing with append/link operators and helpers.
 */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_ID.h"
#include "DNA_material_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_windowmanager_types.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "BLI_bitmap.h"
#include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLI_linklist.h"
#include "BLI_math.h"
#include "BLI_memarena.h"
#include "BLI_utildefines.h"

#include "PIL_time.h"

#include "BLO_readfile.h"

#include "BKE_asset_engine.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_layer.h"
#include "BKE_lib_id.h"
#include "BKE_lib_override.h"
#include "BKE_lib_remap.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_screen.h" /* BKE_ST_MAXNAME */

#include "BKE_idtype.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"

#include "IMB_colormanagement.h"

#include "ED_datafiles.h"
#include "ED_fileselect.h"
#include "ED_screen.h"
#include "ED_view3d.h"

#include "WM_api.h"
#include "WM_types.h"

#include "wm_files.h"

/* -------------------------------------------------------------------- */
/** \name Link/Append Operator
 * \{ */

static bool wm_link_append_poll(bContext *C)
{
  if (WM_operator_winactive(C)) {
    /* linking changes active object which is pretty useful in general,
     * but which totally confuses edit mode (i.e. it becoming not so obvious
     * to leave from edit mode and invalid tools in toolbar might be displayed)
     * so disable link/append when in edit mode (sergey) */
    if (CTX_data_edit_object(C)) {
      return 0;
    }

    return 1;
  }

  return 0;
}

static int wm_link_append_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
  if (RNA_struct_property_is_set(op->ptr, "filepath")) {
    if (ED_operator_region_view3d_active(C)) {
      RNA_int_set_array(op->ptr, "mouse_coordinates", event->mval);
    }
    return WM_operator_call_notest(C, op);
  }
  else {
    /* XXX TODO solve where to get last linked library from */
    if (G.lib[0] != '\0') {
      RNA_string_set(op->ptr, "filepath", G.lib);
    }
    else if (G.relbase_valid) {
      char path[FILE_MAX];
      BLI_strncpy(path, BKE_main_blendfile_path_from_global(), sizeof(path));
      BLI_path_parent_dir(path);
      RNA_string_set(op->ptr, "filepath", path);
    }
    WM_event_add_fileselect(C, op);
    return OPERATOR_RUNNING_MODAL;
  }
}

static short wm_link_append_flag(wmOperator *op)
{
  PropertyRNA *prop;
  short flag = 0;

  if (RNA_boolean_get(op->ptr, "autoselect")) {
    flag |= FILE_AUTOSELECT;
  }
  if (RNA_boolean_get(op->ptr, "active_collection")) {
    flag |= FILE_ACTIVE_COLLECTION;
  }
  if ((prop = RNA_struct_find_property(op->ptr, "relative_path")) &&
      RNA_property_boolean_get(op->ptr, prop)) {
    flag |= FILE_RELPATH;
  }
  if (RNA_boolean_get(op->ptr, "link")) {
    flag |= FILE_LINK;
  }
  if (RNA_boolean_get(op->ptr, "instance_collections")) {
    flag |= FILE_GROUP_INSTANCE;
  }

  return flag;
}

typedef struct WMLinkAppendDataItem {
  AssetUUID *uuid;
  char *name;
  BLI_bitmap
      *libraries; /* All libs (from WMLinkAppendData.libraries) to try to load this ID from. */
  short idcode;

  ID *new_id;
  void *customdata;
} WMLinkAppendDataItem;

typedef struct WMLinkAppendData {
  const char *root;
  LinkNodePair libraries;
  LinkNodePair items;
  int num_libraries;
  int num_items;
  /** Combines #eFileSel_Params_Flag from DNA_space_types.h and
   * BLO_LibLinkFlags from BLO_readfile.h */
  int flag;

  /* Internal 'private' data */
  MemArena *memarena;
} WMLinkAppendData;

static WMLinkAppendData *wm_link_append_data_new(const int flag)
{
  MemArena *ma = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
  WMLinkAppendData *lapp_data = BLI_memarena_calloc(ma, sizeof(*lapp_data));

  lapp_data->flag = flag;
  lapp_data->memarena = ma;

  return lapp_data;
}

static void wm_link_append_data_free(WMLinkAppendData *lapp_data)
{
  BLI_memarena_free(lapp_data->memarena);
}

/* WARNING! *Never* call wm_link_append_data_library_add() after having added some items! */

static void wm_link_append_data_library_add(WMLinkAppendData *lapp_data, const char *libname)
{
  size_t len = strlen(libname) + 1;
  char *libpath = BLI_memarena_alloc(lapp_data->memarena, len);

  BLI_strncpy(libpath, libname, len);
  BLI_linklist_append_arena(&lapp_data->libraries, libpath, lapp_data->memarena);
  lapp_data->num_libraries++;
}

static WMLinkAppendDataItem *wm_link_append_data_item_add(WMLinkAppendData *lapp_data,
                                                          const char *idname,
                                                          const short idcode,
                                                          const AssetUUID *uuid,
                                                          void *customdata)
{
  WMLinkAppendDataItem *item = BLI_memarena_alloc(lapp_data->memarena, sizeof(*item));
  const size_t len = strlen(idname) + 1;

  if (uuid) {
    item->uuid = BLI_memarena_alloc(lapp_data->memarena, sizeof(*item->uuid));
    *item->uuid = *uuid;
  }
  else {
    item->uuid = NULL;
  }
  item->name = BLI_memarena_alloc(lapp_data->memarena, len);
  BLI_strncpy(item->name, idname, len);
  item->idcode = idcode;
  item->libraries = BLI_BITMAP_NEW_MEMARENA(lapp_data->memarena, lapp_data->num_libraries);

  item->new_id = NULL;
  item->customdata = customdata;

  BLI_linklist_append_arena(&lapp_data->items, item, lapp_data->memarena);
  lapp_data->num_items++;

  return item;
}

static bool wm_asset_engine_load_post_from_append_data(bContext *C,
                                                       AssetEngine *ae,
                                                       WMLinkAppendData *lapp_data)
{
  if (lapp_data->num_items > 0 && ae->type->load_post != NULL) {
    LinkNode *itemlink;
    AssetUUID *uuid;
    AssetUUIDList uuids = {
        .uuids = MEM_callocN(sizeof(*uuids.uuids) * lapp_data->num_items, __func__),
        .nbr_uuids = lapp_data->num_items,
        .asset_engine_version = ae->type->version};

    for (itemlink = lapp_data->items.list, uuid = uuids.uuids; itemlink;
         itemlink = itemlink->next, uuid++) {
      WMLinkAppendDataItem *lapp_item = itemlink->link;
      *uuid = *lapp_item->uuid;
      uuid->id = lapp_item->new_id;
    }

    const bool ret_value = ae->type->load_post(C, ae, &uuids);

    MEM_freeN(uuids.uuids);

    return ret_value;
  }
  return true;
}

static int path_to_idcode(const char *path)
{
  const int filetype = ED_path_extension_type(path);
  switch (filetype) {
    case FILE_TYPE_IMAGE:
    case FILE_TYPE_MOVIE:
      return ID_IM;
    case FILE_TYPE_FTFONT:
      return ID_VF;
    case FILE_TYPE_SOUND:
      return ID_SO;
    case FILE_TYPE_PYSCRIPT:
    case FILE_TYPE_TEXT:
      return ID_TXT;
    default:
      return 0;
  }
}

static void wm_link_virtual_lib(WMLinkAppendData *lapp_data,
                                Main *bmain,
                                AssetEngineType *aet,
                                const int lib_idx)
{
  const bool generate_overrides = (lapp_data->flag & BLO_LIBLINK_GENERATE_OVERRIDE) != 0;
  LinkNode *itemlink;
  int item_idx;

  BLI_assert(aet);

  /* Find or add virtual library matching current asset engine. */
  Library *virtlib = BKE_library_asset_virtual_ensure(bmain, aet);

  for (item_idx = 0, itemlink = lapp_data->items.list; itemlink;
       item_idx++, itemlink = itemlink->next) {
    WMLinkAppendDataItem *item = itemlink->link;
    ID *new_id = NULL;
    bool id_exists = false;

    if (!BLI_BITMAP_TEST(item->libraries, lib_idx)) {
      continue;
    }

    switch (item->idcode) {
      case ID_IM:
        new_id = (ID *)BKE_image_load_exists_ex(bmain, item->name, &id_exists);
        if (id_exists) {
          if (!new_id->uuid || !ASSETUUID_EQUAL(new_id->uuid, item->uuid)) {
            /* Fake 'same ID' (same path, but different uuid or whatever), force loading into new
             * ID. */
            BLI_assert(new_id->lib != virtlib);
            new_id = (ID *)BKE_image_load(bmain, item->name);
            id_exists = false;
          }
        }
        break;
      default:
        break;
    }

    if (new_id) {
      new_id->lib = virtlib;
      new_id->tag |= LIB_TAG_EXTERN | LIB_ASSET;

      if (!id_exists) {
        new_id->uuid = MEM_mallocN(sizeof(*new_id->uuid), __func__);
        *new_id->uuid = *item->uuid;
      }

      if (generate_overrides) {
        /* Create local override of virtually linked datablock, since we nearly always want to be
         * able to edit pretty much everything about it. */
        new_id = BKE_lib_override_library_create_from_id(bmain, new_id, true);
        /* TODO: will need to protect some fields on type-by-type case (path field). */
      }

      /* If the link is sucessful, clear item's libs 'todo' flags.
       * This avoids trying to link same item with other libraries to come. */
      BLI_bitmap_set_all(item->libraries, false, lapp_data->num_libraries);
      item->new_id = new_id;
    }
  }
  BKE_libraries_asset_repositories_rebuild(bmain);
}

static void wm_link_do(WMLinkAppendData *lapp_data,
                       ReportList *reports,
                       Main *bmain,
                       AssetEngineType *aet,
                       Scene *scene,
                       ViewLayer *view_layer,
                       const View3D *v3d)
{
  Main *mainl;
  BlendHandle *bh;
  Library *lib;

  const int flag = lapp_data->flag;

  LinkNode *liblink, *itemlink;
  int lib_idx, item_idx;

  BLI_assert(lapp_data->num_items && lapp_data->num_libraries);

  for (lib_idx = 0, liblink = lapp_data->libraries.list; liblink;
       lib_idx++, liblink = liblink->next) {
    char *libname = liblink->link;

    if (libname[0] == '\0') {
      /* Special 'virtual lib' cases. */
      wm_link_virtual_lib(lapp_data, bmain, aet, lib_idx);
      continue;
    }

    if (STREQ(libname, BLO_EMBEDDED_STARTUP_BLEND)) {
      bh = BLO_blendhandle_from_memory(datatoc_startup_blend, datatoc_startup_blend_size);
    }
    else {
      bh = BLO_blendhandle_from_file(libname, reports);
    }

    if (bh == NULL) {
      /* Unlikely since we just browsed it, but possible
       * Error reports will have been made by BLO_blendhandle_from_file() */
      continue;
    }

    /* here appending/linking starts */
    mainl = BLO_library_link_begin(bmain, &bh, libname);
    lib = mainl->curlib;
    BLI_assert(lib);
    UNUSED_VARS_NDEBUG(lib);

    if (mainl->versionfile < 250) {
      BKE_reportf(reports,
                  RPT_WARNING,
                  "Linking or appending from a very old .blend file format (%d.%d), no animation "
                  "conversion will "
                  "be done! You may want to re-save your lib file with current Blender",
                  mainl->versionfile,
                  mainl->subversionfile);
    }

    /* For each lib file, we try to link all items belonging to that lib,
     * and tag those successful to not try to load them again with the other libs. */
    for (item_idx = 0, itemlink = lapp_data->items.list; itemlink;
         item_idx++, itemlink = itemlink->next) {
      WMLinkAppendDataItem *item = itemlink->link;
      ID *new_id;

      if (!BLI_BITMAP_TEST(item->libraries, lib_idx)) {
        continue;
      }

      new_id = BLO_library_link_named_part_asset(
          mainl, &bh, aet, lapp_data->root, item->idcode, item->name, item->uuid, flag);

      if (new_id) {
        /* If the link is successful, clear item's libs 'todo' flags.
         * This avoids trying to link same item with other libraries to come. */
        BLI_bitmap_set_all(item->libraries, false, lapp_data->num_libraries);
        item->new_id = new_id;
      }
    }

    BLO_library_link_end(mainl, &bh, flag, bmain, scene, view_layer, v3d);
    BLO_blendhandle_close(bh);
  }
}

/**
 * Check if an item defined by \a name and \a group can be appended/linked.
 *
 * \param reports: Optionally report an error when an item can't be appended/linked.
 */
static bool wm_link_append_item_poll(ReportList *reports,
                                     const char *path,
                                     const char *group,
                                     const char *name,
                                     const bool do_append)
{
  short idcode;

  if (!group || !name) {
    printf("skipping %s\n", path);
    return false;
  }

  idcode = BKE_idtype_idcode_from_name(group);

  /* XXX For now, we do a nasty exception for workspace, forbid linking them.
   *     Not nice, ultimately should be solved! */
  if (!BKE_idtype_idcode_is_linkable(idcode) && (do_append || idcode != ID_WS)) {
    if (reports) {
      if (do_append) {
        BKE_reportf(reports,
                    RPT_ERROR_INVALID_INPUT,
                    "Can't append data-block '%s' of type '%s'",
                    name,
                    group);
      }
      else {
        BKE_reportf(reports,
                    RPT_ERROR_INVALID_INPUT,
                    "Can't link data-block '%s' of type '%s'",
                    name,
                    group);
      }
    }
    return false;
  }

  return true;
}

static int wm_link_append_exec(bContext *C, wmOperator *op)
{
  Main *bmain = CTX_data_main(C);
  Scene *scene = CTX_data_scene(C);
  ViewLayer *view_layer = CTX_data_view_layer(C);
  PropertyRNA *prop;
  WMLinkAppendData *lapp_data;
  char path[FILE_MAX_LIBEXTRA], root[FILE_MAXDIR], libname[FILE_MAX_LIBEXTRA], relname[FILE_MAX];
  char *group, *name;
  int totfiles = 0;

  char asset_engine[BKE_ST_MAXNAME];
  AssetEngineType *aet = NULL;
  AssetUUID uuid = {0};

  RNA_string_get(op->ptr, "filename", relname);
  RNA_string_get(op->ptr, "directory", root);

  BLI_join_dirfile(path, sizeof(path), root, relname);

  RNA_string_get(op->ptr, "asset_engine", asset_engine);
  if (asset_engine[0] != '\0') {
    aet = BKE_asset_engines_find(asset_engine);
  }

  /* test if we have a valid data */
  if (!BLO_library_path_explode(path, libname, &group, &name) && (!aet || !path_to_idcode(path))) {
    BKE_reportf(op->reports, RPT_ERROR, "'%s': not a library", path);
    return OPERATOR_CANCELLED;
  }
  else if (!group && !aet) {
    BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
    return OPERATOR_CANCELLED;
  }
  else if (libname[0] && BLI_path_cmp(BKE_main_blendfile_path(bmain), libname) == 0) {
    BKE_reportf(op->reports, RPT_ERROR, "'%s': cannot use current file as library", path);
    return OPERATOR_CANCELLED;
  }

  /* check if something is indicated for append/link */
  prop = RNA_struct_find_property(op->ptr, "files");
  if (prop) {
    totfiles = RNA_property_collection_length(op->ptr, prop);
    if (totfiles == 0) {
      if (!name) {
        BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
        return OPERATOR_CANCELLED;
      }
    }
  }
  else if (!name) {
    BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", path);
    return OPERATOR_CANCELLED;
  }

  short flag = wm_link_append_flag(op);
  const bool do_append = (flag & FILE_LINK) == 0;

  /* sanity checks for flag */
  if (scene && scene->id.lib) {
    BKE_reportf(op->reports,
                RPT_WARNING,
                "Scene '%s' is linked, instantiation of objects & groups is disabled",
                scene->id.name + 2);
    flag &= ~FILE_GROUP_INSTANCE;
    scene = NULL;
  }

  /* We need to add nothing from BLO_LibLinkFlags to flag here. */

  /* from here down, no error returns */

  if (view_layer && RNA_boolean_get(op->ptr, "autoselect")) {
    BKE_view_layer_base_deselect_all(view_layer);
  }

  /* tag everything, all untagged data can be made local
   * its also generally useful to know what is new
   *
   * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */
  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);

  /* We define our working data...
   * Note that here, each item 'uses' one library, and only one. */
  lapp_data = wm_link_append_data_new(flag);
  lapp_data->root = root;
  if (totfiles != 0) {
    GHash *libraries = BLI_ghash_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, __func__);
    int lib_idx = 0;

    RNA_BEGIN (op->ptr, itemptr, "files") {
      RNA_string_get(&itemptr, "name", relname);

      BLI_join_dirfile(path, sizeof(path), root, relname);

      if (BLO_library_path_explode(path, libname, &group, &name)) {
        if (!wm_link_append_item_poll(NULL, path, group, name, do_append)) {
          continue;
        }

        if (!BLI_ghash_haskey(libraries, libname)) {
          BLI_ghash_insert(libraries, BLI_strdup(libname), POINTER_FROM_INT(lib_idx));
          lib_idx++;
          wm_link_append_data_library_add(lapp_data, libname);
        }
      }
      /* Non-blend paths are only valid in asset engine context (virtual libraries). */
      else if (aet && path_to_idcode(path)) {
        if (!BLI_ghash_haskey(libraries, "")) {
          BLI_ghash_insert(libraries, BLI_strdup(""), POINTER_FROM_INT(lib_idx));
          lib_idx++;
          wm_link_append_data_library_add(lapp_data, "");
        }
      }
    }
    RNA_END;

    RNA_BEGIN (op->ptr, itemptr, "files") {
      WMLinkAppendDataItem *item;

      RNA_string_get(&itemptr, "name", relname);

      BLI_join_dirfile(path, sizeof(path), root, relname);

      if (aet) {
        RNA_int_get_array(&itemptr, "uuid_repository", uuid.uuid_repository);
        RNA_int_get_array(&itemptr, "uuid_asset", uuid.uuid_asset);
        RNA_int_get_array(&itemptr, "uuid_variant", uuid.uuid_variant);
        RNA_int_get_array(&itemptr, "uuid_revision", uuid.uuid_revision);
        RNA_int_get_array(&itemptr, "uuid_view", uuid.uuid_view);
      }

      if (BLO_library_path_explode(path, libname, &group, &name)) {
        if (!wm_link_append_item_poll(op->reports, path, group, name, do_append)) {
#ifdef DEBUG_LIBRARY
          printf("skipping %s\n", path);
#endif
          continue;
        }

        lib_idx = POINTER_AS_INT(BLI_ghash_lookup(libraries, libname));
        item = wm_link_append_data_item_add(
            lapp_data, name, BKE_idtype_idcode_from_name(group), &uuid, NULL);
        BLI_BITMAP_ENABLE(item->libraries, lib_idx);
      }
      else if (aet) { /* Non-blend paths are only valid in asset engine context (virtual
                         libraries). */
        const int idcode = path_to_idcode(path);

        if (idcode != 0) {
          lib_idx = POINTER_AS_INT(BLI_ghash_lookup(libraries, ""));
          item = wm_link_append_data_item_add(lapp_data, path, idcode, &uuid, NULL);
          BLI_BITMAP_ENABLE(item->libraries, lib_idx);
        }
      }
    }
    RNA_END;

    BLI_ghash_free(libraries, MEM_freeN, NULL);
  }
  else if ((group || aet) && path[0]) {
    WMLinkAppendDataItem *item;

    if (aet) {
      RNA_int_get_array(op->ptr, "uuid_repository", uuid.uuid_repository);
      RNA_int_get_array(op->ptr, "uuid_asset", uuid.uuid_asset);
      RNA_int_get_array(op->ptr, "uuid_variant", uuid.uuid_variant);
      RNA_int_get_array(op->ptr, "uuid_revision", uuid.uuid_revision);
      RNA_int_get_array(op->ptr, "uuid_view", uuid.uuid_view);
    }

    if (group) {
      wm_link_append_data_library_add(lapp_data, libname);
      item = wm_link_append_data_item_add(
          lapp_data, name, BKE_idtype_idcode_from_name(group), &uuid, NULL);
      BLI_BITMAP_ENABLE(item->libraries, 0);
    }
    else if (aet) { /* Non-blend paths are only valid in asset engine context (virtual libraries).
                     */
      const int idcode = path_to_idcode(path);

      if (idcode != 0) {
        wm_link_append_data_library_add(lapp_data, "");
        item = wm_link_append_data_item_add(lapp_data, path, idcode, &uuid, NULL);
        BLI_BITMAP_ENABLE(item->libraries, 0);
      }
    }
  }

  if (lapp_data->num_items == 0) {
    /* Early out in case there is nothing to link. */
    wm_link_append_data_free(lapp_data);
    /* Clear pre existing tag. */
    BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
    return OPERATOR_CANCELLED;
  }

  if (!do_append) {
    /* XXX Currently this only applies to virtual libs ('linking' mere image files...).
     *     However, we may want to make this a general option at link time when importing assets...
     */
    lapp_data->flag |= BLO_LIBLINK_GENERATE_OVERRIDE;
  }

  /* XXX We'd need re-entrant locking on Main for this to work... */
  /* BKE_main_lock(bmain); */

  wm_link_do(lapp_data, op->reports, bmain, aet, scene, view_layer, CTX_wm_view3d(C));

  /* BKE_main_unlock(bmain); */

  /* Try to do smart things from context in some cases (like drag'n'drop of material over
   * object...) */
  prop = RNA_struct_find_property(op->ptr, "mouse_coordinates");
  if (prop && RNA_property_is_set(op->ptr, prop)) {
    int mval[2];
    RNA_property_int_get_array(op->ptr, prop, mval);
    Base *obbase = ED_view3d_give_base_under_cursor(C, mval);
    if (obbase) {
      LinkNode *itemlink;
      for (itemlink = lapp_data->items.list; itemlink; itemlink = itemlink->next) {
        ID *new_id = ((WMLinkAppendDataItem *)(itemlink->link))->new_id;

        if (new_id && GS(new_id->name) == ID_MA) {
          BKE_object_material_assign(bmain,
                                     obbase->object,
                                     (Material *)new_id,
                                     obbase->object->actcol,
                                     BKE_MAT_ASSIGN_USERPREF);
        }
      }
    }
#ifdef DEBUG_LIBRARY
    printf("%s\n", obbase ? obbase->object->id.name : "<NULL>");
#endif
  }

  /* mark all library linked objects to be updated */
  BKE_main_lib_objects_recalc_all(bmain);
  IMB_colormanagement_check_file_config(bmain);

  /* append, rather than linking */
  if (do_append) {
    const bool set_fake = RNA_boolean_get(op->ptr, "set_fake");
    const bool use_recursive = RNA_boolean_get(op->ptr, "use_recursive");

    if (use_recursive) {
      BKE_library_make_local(bmain, NULL, NULL, true, set_fake);
    }
    else {
      LinkNode *itemlink;
      GSet *done_libraries = BLI_gset_new_ex(
          BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__, lapp_data->num_libraries);

      for (itemlink = lapp_data->items.list; itemlink; itemlink = itemlink->next) {
        ID *new_id = ((WMLinkAppendDataItem *)(itemlink->link))->new_id;

        if (new_id && !BLI_gset_haskey(done_libraries, new_id->lib)) {
          BKE_library_make_local(bmain, new_id->lib, NULL, true, set_fake);
          BLI_gset_insert(done_libraries, new_id->lib);
        }
      }

      BLI_gset_free(done_libraries, NULL);
    }
  }

  if (aet != NULL && aet->load_post != NULL) {
    AssetEngine *ae = BKE_asset_engine_create(aet, NULL);
    wm_asset_engine_load_post_from_append_data(C, ae, lapp_data);
    BKE_asset_engine_free(ae);
  }

  wm_link_append_data_free(lapp_data);

  /* important we unset, otherwise these object wont
   * link into other scenes from this blend file */
  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);

  /* TODO(sergey): Use proper flag for tagging here. */

  /* TODO (dalai): Temporary solution!
   * Ideally we only need to tag the new objects themselves, not the scene.
   * This way we'll avoid flush of collection properties
   * to all objects and limit update to the particular object only.
   * But afraid first we need to change collection evaluation in DEG
   * according to depsgraph manifesto. */
  DEG_id_tag_update(&scene->id, 0);

  /* recreate dependency graph to include new objects */
  DEG_relations_tag_update(bmain);

  /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
  BLI_strncpy(G.lib, root, FILE_MAX);

  WM_event_add_notifier(C, NC_WINDOW, NULL);

  return OPERATOR_FINISHED;
}

static void wm_link_append_properties_common(wmOperatorType *ot, bool is_link)
{
  PropertyRNA *prop;

  /* better not save _any_ settings for this operator */
  /* properties */
  prop = RNA_def_string(ot->srna,
                        "asset_engine",
                        NULL,
                        sizeof(((AssetEngineType *)NULL)->idname),
                        "Asset Engine",
                        "Asset engine identifier used to append/link the data");
  RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);

  prop = RNA_def_boolean(
      ot->srna, "link", is_link, "Link", "Link the objects or data-blocks rather than appending");
  RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
  prop = RNA_def_boolean(ot->srna, "autoselect", true, "Select", "Select new objects");
  RNA_def_property_flag(prop, PROP_SKIP_SAVE);
  prop = RNA_def_boolean(ot->srna,
                         "active_collection",
                         true,
                         "Active Collection",
                         "Put new objects on the active collection");
  RNA_def_property_flag(prop, PROP_SKIP_SAVE);
  prop = RNA_def_boolean(
      ot->srna,
      "instance_collections",
      is_link,
      "Instance Collections",
      "Create instances for collections, rather than adding them directly to the scene");
  RNA_def_property_flag(prop, PROP_SKIP_SAVE);

  prop = RNA_def_int_vector(
      ot->srna,
      "mouse_coordinates",
      2,
      (const int[2]){0, 0},
      INT_MIN,
      INT_MAX,
      "Mouse Coordinates",
      "Store sompe mouse coordinates (e.g. to get object on which linked material was dropped...)",
      INT_MIN,
      INT_MAX);
  RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
}

void WM_OT_link(wmOperatorType *ot)
{
  ot->name = "Link";
  ot->idname = "WM_OT_link";
  ot->description = "Link from a Library .blend file";

  ot->invoke = wm_link_append_invoke;
  ot->exec = wm_link_append_exec;
  ot->poll = wm_link_append_poll;

  ot->flag |= OPTYPE_UNDO;

  WM_operator_properties_filesel(ot,
                                 FILE_TYPE_FOLDER | FILE_TYPE_BLENDER | FILE_TYPE_BLENDERLIB,
                                 FILE_LOADLIB,
                                 FILE_OPENFILE,
                                 WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME |
                                     WM_FILESEL_RELPATH | WM_FILESEL_FILES | WM_FILESEL_SHOW_PROPS,
                                 FILE_DEFAULTDISPLAY,
                                 FILE_SORT_ALPHA);

  wm_link_append_properties_common(ot, true);
}

void WM_OT_append(wmOperatorType *ot)
{
  ot->name = "Append";
  ot->idname = "WM_OT_append";
  ot->description = "Append from a Library .blend file";

  ot->invoke = wm_link_append_invoke;
  ot->exec = wm_link_append_exec;
  ot->poll = wm_link_append_poll;

  ot->flag |= OPTYPE_UNDO;

  WM_operator_properties_filesel(ot,
                                 FILE_TYPE_FOLDER | FILE_TYPE_BLENDER | FILE_TYPE_BLENDERLIB,
                                 FILE_LOADLIB,
                                 FILE_OPENFILE,
                                 WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME |
                                     WM_FILESEL_FILES | WM_FILESEL_SHOW_PROPS,
                                 FILE_DEFAULTDISPLAY,
                                 FILE_SORT_ALPHA);

  wm_link_append_properties_common(ot, false);
  RNA_def_boolean(ot->srna,
                  "set_fake",
                  false,
                  "Fake User",
                  "Set Fake User for appended items (except Objects and Groups)");
  RNA_def_boolean(
      ot->srna,
      "use_recursive",
      true,
      "Localize All",
      "Localize all appended data, including those indirectly linked from other libraries");
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Append Single Data-Block & Return it
 *
 * Used for appending workspace from startup files.
 * \{ */

ID *WM_file_append_datablock(Main *bmain,
                             Scene *scene,
                             ViewLayer *view_layer,
                             View3D *v3d,
                             const char *filepath,
                             const short id_code,
                             const char *id_name)
{
  /* Tag everything so we can make local only the new datablock. */
  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);

  /* Define working data, with just the one item we want to append. */
  WMLinkAppendData *lapp_data = wm_link_append_data_new(0);

  wm_link_append_data_library_add(lapp_data, filepath);
  WMLinkAppendDataItem *item = wm_link_append_data_item_add(
      lapp_data, id_name, id_code, NULL, NULL);
  BLI_BITMAP_ENABLE(item->libraries, 0);

  /* Link datablock. */
  wm_link_do(lapp_data, NULL, bmain, NULL, scene, view_layer, v3d);

  /* Get linked datablock and free working data. */
  ID *id = item->new_id;
  wm_link_append_data_free(lapp_data);

  /* Make datablock local. */
  BKE_library_make_local(bmain, NULL, NULL, true, false);

  /* Clear pre existing tag. */
  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);

  return id;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Library Relocate Operator & Library Reload API
 * \{ */

static int wm_lib_relocate_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
  Library *lib;
  char lib_name[MAX_NAME];

  RNA_string_get(op->ptr, "library", lib_name);
  lib = (Library *)BKE_libblock_find_name(CTX_data_main(C), ID_LI, lib_name);

  if (lib) {
    if (lib->parent) {
      BKE_reportf(op->reports,
                  RPT_ERROR_INVALID_INPUT,
                  "Cannot relocate indirectly linked library '%s'",
                  lib->filepath);
      return OPERATOR_CANCELLED;
    }
    if (lib->flag & LIBRARY_FLAG_VIRTUAL) {
      BKE_reportf(op->reports,
                  RPT_ERROR_INVALID_INPUT,
                  "Cannot relocate virtual library '%s'",
                  lib->id.name + 2);
      return OPERATOR_CANCELLED;
    }
    RNA_string_set(op->ptr, "filepath", lib->filepath);

    WM_event_add_fileselect(C, op);

    return OPERATOR_RUNNING_MODAL;
  }

  return OPERATOR_CANCELLED;
}

/**
 * \param library if given, all IDs from that library will be removed and reloaded. Otherwise, IDs
 * must have already been removed from \a bmain, and added to \a lapp_data.
 */
static void lib_relocate_do(Main *bmain,
                            Library *library,
                            WMLinkAppendData *lapp_data,
                            ReportList *reports,
                            AssetEngineType *aet,
                            const bool do_reload)
{
  ListBase *lbarray[MAX_LIBARRAY];
  int lba_idx;

  LinkNode *itemlink;
  int item_idx;

  /* Remove all IDs to be reloaded from Main. */
  if (library) {
    lba_idx = set_listbasepointers(bmain, lbarray);
    while (lba_idx--) {
      ID *id = lbarray[lba_idx]->first;
      const short idcode = id ? GS(id->name) : 0;

      if (!id || !BKE_idtype_idcode_is_linkable(idcode)) {
        /* No need to reload non-linkable datatypes,
         * those will get relinked with their 'users ID'. */
        continue;
      }

      for (; id; id = id->next) {
        if (id->lib == library) {
          WMLinkAppendDataItem *item;

          /* We remove it from current Main, and add it to items to link... */
          /* Note that non-linkable IDs (like e.g. shapekeys) are also explicitely
           * linked here... */
          BLI_remlink(lbarray[lba_idx], id);
          item = wm_link_append_data_item_add(lapp_data, id->name + 2, idcode, NULL, id);
          BLI_bitmap_set_all(item->libraries, true, lapp_data->num_libraries);

#ifdef DEBUG_LIBRARY
          printf("\tdatablock to seek for: %s\n", id->name);
#endif
        }
      }
    }
  }

  if (lapp_data->num_items == 0) {
    /* Early out in case there is nothing to do. */
    return;
  }

  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);

  /* We do not want any instantiation here! */
  wm_link_do(lapp_data, reports, bmain, aet, NULL, NULL, NULL);

  BKE_main_lock(bmain);

  /* We add back old id to bmain.
   * We need to do this in a first, separated loop, otherwise some of those may not be handled by
   * ID remapping, which means they would still reference old data to be deleted... */
  for (item_idx = 0, itemlink = lapp_data->items.list; itemlink;
       item_idx++, itemlink = itemlink->next) {
    WMLinkAppendDataItem *item = itemlink->link;
    ID *old_id = item->customdata;

    BLI_assert(old_id);
    BLI_addtail(which_libbase(bmain, GS(old_id->name)), old_id);
  }

  /* Since our (old) reloaded IDs were removed from main, the user count done for them in linking
   * code is wrong, we need to redo it here after adding them back to main. */
  BKE_main_id_refcount_recompute(bmain, false);

  /* Note that in reload case, we also want to replace indirect usages. */
  const short remap_flags = ID_REMAP_SKIP_NEVER_NULL_USAGE |
                            ID_REMAP_NO_INDIRECT_PROXY_DATA_USAGE |
                            (do_reload ? 0 : ID_REMAP_SKIP_INDIRECT_USAGE);
  for (item_idx = 0, itemlink = lapp_data->items.list; itemlink;
       item_idx++, itemlink = itemlink->next) {
    WMLinkAppendDataItem *item = itemlink->link;
    ID *old_id = item->customdata;
    ID *new_id = item->new_id;

    BLI_assert(old_id);
    if (do_reload) {
      /* Since we asked for placeholders in case of missing IDs,
       * we expect to always get a valid one. */
      BLI_assert(new_id);
    }
    if (new_id) {
#ifdef PRINT_DEBUG
      printf("before remap of %s, old_id users: %d, new_id users: %d\n",
             old_id->name,
             old_id->us,
             new_id->us);
#endif
      BKE_libblock_remap_locked(bmain, old_id, new_id, remap_flags);

      if (old_id->flag & LIB_FAKEUSER) {
        id_fake_user_clear(old_id);
        id_fake_user_set(new_id);
      }

#ifdef PRINT_DEBUG
      printf("after remap of %s, old_id users: %d, new_id users: %d\n",
             old_id->name,
             old_id->us,
             new_id->us);
#endif

      /* In some cases, new_id might become direct link, remove parent of library in this case. */
      if (new_id->lib && new_id->lib->parent && (new_id->tag & LIB_TAG_INDIRECT) == 0) {
        if (do_reload) {
          BLI_assert(0); /* Should not happen in 'pure' reload case... */
        }
        new_id->lib->parent = NULL;
      }
    }

    if (old_id->us > 0 && new_id && old_id->lib == new_id->lib) {
      /* Note that this *should* not happen - but better be safe than sorry in this area,
       * at least until we are 100% sure this cannot ever happen.
       * Also, we can safely assume names were unique so far,
       * so just replacing '.' by '~' should work,
       * but this does not totally rules out the possibility of name collision. */
      size_t len = strlen(old_id->name);
      size_t dot_pos;
      bool has_num = false;

      for (dot_pos = len; dot_pos--;) {
        char c = old_id->name[dot_pos];
        if (c == '.') {
          break;
        }
        else if (c < '0' || c > '9') {
          has_num = false;
          break;
        }
        has_num = true;
      }

      if (has_num) {
        old_id->name[dot_pos] = '~';
      }
      else {
        len = MIN2(len, MAX_ID_NAME - 7);
        BLI_strncpy(&old_id->name[len], "~000", 7);
      }

      id_sort_by_name(which_libbase(bmain, GS(old_id->name)), old_id, NULL);

      BKE_reportf(
          reports,
          RPT_WARNING,
          "Lib Reload: Replacing all references to old data-block '%s' by reloaded one failed, "
          "old one (%d remaining users) had to be kept and was renamed to '%s'",
          new_id->name,
          old_id->us,
          old_id->name);
    }
  }

  BKE_main_unlock(bmain);

  for (item_idx = 0, itemlink = lapp_data->items.list; itemlink;
       item_idx++, itemlink = itemlink->next) {
    WMLinkAppendDataItem *item = itemlink->link;
    ID *old_id = item->customdata;

    if (old_id->us == 0) {
      BKE_id_free(bmain, old_id);
    }
  }

  /* Some datablocks can get reloaded/replaced 'silently' because they are not linkable
   * (shape keys e.g.), so we need another loop here to clear old ones if possible. */
  lba_idx = set_listbasepointers(bmain, lbarray);
  while (lba_idx--) {
    ID *id, *id_next;
    for (id = lbarray[lba_idx]->first; id; id = id_next) {
      id_next = id->next;
      /* XXX That check may be a bit to generic/permissive? */
      if (id->lib && (id->flag & LIB_TAG_PRE_EXISTING) && id->us == 0) {
        BKE_id_free(bmain, id);
      }
    }
  }

  /* Get rid of no more used libraries... */
  BKE_main_id_tag_idcode(bmain, ID_LI, LIB_TAG_DOIT, true);
  lba_idx = set_listbasepointers(bmain, lbarray);
  while (lba_idx--) {
    ID *id;
    for (id = lbarray[lba_idx]->first; id; id = id->next) {
      if (id->lib) {
        id->lib->id.tag &= ~LIB_TAG_DOIT;
      }
    }
  }
  Library *lib, *lib_next;
  for (lib = which_libbase(bmain, ID_LI)->first; lib; lib = lib_next) {
    lib_next = lib->id.next;
    if (lib->id.tag & LIB_TAG_DOIT) {
      id_us_clear_real(&lib->id);
      if (lib->id.us == 0) {
        BKE_id_free(bmain, (ID *)lib);
      }
    }
  }

  BKE_main_lib_objects_recalc_all(bmain);
  IMB_colormanagement_check_file_config(bmain);

  /* important we unset, otherwise these object wont
   * link into other scenes from this blend file */
  BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);

  /* recreate dependency graph to include new objects */
  DEG_relations_tag_update(bmain);
}

void WM_lib_reload(Library *lib, bContext *C, ReportList *reports)
{
  if (!BLO_has_bfile_extension(lib->filepath)) {
    BKE_reportf(reports, RPT_ERROR, "'%s' is not a valid library filepath", lib->filepath);
    return;
  }

  if (!BLI_exists(lib->filepath)) {
    BKE_reportf(reports,
                RPT_ERROR,
                "Trying to reload library '%s' from invalid path '%s'",
                lib->id.name,
                lib->filepath);
    return;
  }

  WMLinkAppendData *lapp_data = wm_link_append_data_new(BLO_LIBLINK_USE_PLACEHOLDERS |
                                                        BLO_LIBLINK_FORCE_INDIRECT);

  wm_link_append_data_library_add(lapp_data, lib->filepath);

  lib_relocate_do(CTX_data_main(C), lib, lapp_data, reports, NULL, true);

  wm_link_append_data_free(lapp_data);

  WM_event_add_notifier(C, NC_WINDOW, NULL);
}

static int wm_lib_relocate_exec_do(bContext *C, wmOperator *op, bool do_reload)
{
  Library *lib;
  char lib_name[MAX_NAME];

  RNA_string_get(op->ptr, "library", lib_name);
  lib = (Library *)BKE_libblock_find_name(CTX_data_main(C), ID_LI, lib_name);

  if (lib) {
    Main *bmain = CTX_data_main(C);
    PropertyRNA *prop;
    WMLinkAppendData *lapp_data;

    char path[FILE_MAX], root[FILE_MAXDIR], libname[FILE_MAX], relname[FILE_MAX];
    short flag = 0;

    if (RNA_boolean_get(op->ptr, "relative_path")) {
      flag |= FILE_RELPATH;
    }

    if (lib->parent && !do_reload) {
      BKE_reportf(op->reports,
                  RPT_ERROR_INVALID_INPUT,
                  "Cannot relocate indirectly linked library '%s'",
                  lib->filepath);
      return OPERATOR_CANCELLED;
    }
    if (lib->flag & LIBRARY_FLAG_VIRTUAL) {
      BKE_reportf(op->reports,
                  RPT_ERROR_INVALID_INPUT,
                  "Cannot relocate or reload virtual library '%s'",
                  lib->id.name + 2);
      return OPERATOR_CANCELLED;
    }

    RNA_string_get(op->ptr, "directory", root);
    RNA_string_get(op->ptr, "filename", libname);

    if (!BLO_has_bfile_extension(libname)) {
      BKE_report(op->reports, RPT_ERROR, "Not a library");
      return OPERATOR_CANCELLED;
    }

    BLI_join_dirfile(path, sizeof(path), root, libname);

    if (!BLI_exists(path)) {
      BKE_reportf(op->reports,
                  RPT_ERROR_INVALID_INPUT,
                  "Trying to reload or relocate library '%s' to invalid path '%s'",
                  lib->id.name,
                  path);
      return OPERATOR_CANCELLED;
    }

    if (BLI_path_cmp(lib->filepath, path) == 0) {
#ifdef PRINT_DEBUG
      printf("We are supposed to reload '%s' lib (%d)...\n", lib->filepath, lib->id.us);
#endif

      do_reload = true;

      lapp_data = wm_link_append_data_new(flag);
      wm_link_append_data_library_add(lapp_data, path);
    }
    else {
      int totfiles = 0;

#ifdef PRINT_DEBUG
      printf("We are supposed to relocate '%s' lib to new '%s' one...\n", lib->filepath, libname);
#endif

      /* Check if something is indicated for relocate. */
      prop = RNA_struct_find_property(op->ptr, "files");
      if (prop) {
        totfiles = RNA_property_collection_length(op->ptr, prop);
        if (totfiles == 0) {
          if (!libname[0]) {
            BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
            return OPERATOR_CANCELLED;
          }
        }
      }

      lapp_data = wm_link_append_data_new(flag);

      if (totfiles) {
        RNA_BEGIN (op->ptr, itemptr, "files") {
          RNA_string_get(&itemptr, "name", relname);

          BLI_join_dirfile(path, sizeof(path), root, relname);

          if (BLI_path_cmp(path, lib->filepath) == 0 || !BLO_has_bfile_extension(relname)) {
            continue;
          }

#ifdef PRINT_DEBUG
          printf("\t candidate new lib to reload datablocks from: %s\n", path);
#endif
          wm_link_append_data_library_add(lapp_data, path);
        }
        RNA_END;
      }
      else {
#ifdef PRINT_DEBUG
        printf("\t candidate new lib to reload datablocks from: %s\n", path);
#endif
        wm_link_append_data_library_add(lapp_data, path);
      }
    }

    if (do_reload) {
      lapp_data->flag |= BLO_LIBLINK_USE_PLACEHOLDERS | BLO_LIBLINK_FORCE_INDIRECT;
    }

    lib_relocate_do(bmain, lib, lapp_data, op->reports, NULL, do_reload);

    wm_link_append_data_free(lapp_data);

    /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
    BLI_strncpy(G.lib, root, FILE_MAX);

    WM_event_add_notifier(C, NC_WINDOW, NULL);

    return OPERATOR_FINISHED;
  }

  return OPERATOR_CANCELLED;
}

static int wm_lib_relocate_exec(bContext *C, wmOperator *op)
{
  return wm_lib_relocate_exec_do(C, op, false);
}

void WM_OT_lib_relocate(wmOperatorType *ot)
{
  PropertyRNA *prop;

  ot->name = "Relocate Library";
  ot->idname = "WM_OT_lib_relocate";
  ot->description = "Relocate the given library to one or several others";

  ot->invoke = wm_lib_relocate_invoke;
  ot->exec = wm_lib_relocate_exec;

  ot->flag |= OPTYPE_UNDO;

  prop = RNA_def_string(ot->srna, "library", NULL, MAX_NAME, "Library", "Library to relocate");
  RNA_def_property_flag(prop, PROP_HIDDEN);

  WM_operator_properties_filesel(ot,
                                 FILE_TYPE_FOLDER | FILE_TYPE_BLENDER,
                                 FILE_BLENDER,
                                 FILE_OPENFILE,
                                 WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME |
                                     WM_FILESEL_FILES | WM_FILESEL_RELPATH,
                                 FILE_DEFAULTDISPLAY,
                                 FILE_SORT_ALPHA);
}

static int wm_lib_reload_exec(bContext *C, wmOperator *op)
{
  return wm_lib_relocate_exec_do(C, op, true);
}

void WM_OT_lib_reload(wmOperatorType *ot)
{
  PropertyRNA *prop;

  ot->name = "Reload Library";
  ot->idname = "WM_OT_lib_reload";
  ot->description = "Reload the given library";

  ot->exec = wm_lib_reload_exec;

  ot->flag |= OPTYPE_UNDO;

  prop = RNA_def_string(ot->srna, "library", NULL, MAX_NAME, "Library", "Library to reload");
  RNA_def_property_flag(prop, PROP_HIDDEN);

  WM_operator_properties_filesel(ot,
                                 FILE_TYPE_FOLDER | FILE_TYPE_BLENDER,
                                 FILE_BLENDER,
                                 FILE_OPENFILE,
                                 WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME |
                                     WM_FILESEL_RELPATH,
                                 FILE_DEFAULTDISPLAY,
                                 FILE_SORT_ALPHA);
}

/** \name Asset-related operators.
 *
 * \{ */

typedef struct AssetUpdateCheckEngine {
  struct AssetUpdateCheckEngine *next, *prev;
  AssetEngine *ae;

  /* Note: We cannot store IDs themselves in non-locking async task... so we'll have to check again
   * for UUID/IDs mapping on each update call... Not ideal, but don't think it will be that big of
   * a bottleneck in practice. */
  AssetUUIDList uuids;
  int allocated_uuids;
  int ae_job_id;
  short status;
} AssetUpdateCheckEngine;

typedef struct AssetUpdateCheckJob {
  ListBase engines;
  short flag;

  float *progress;
  short *stop;
} AssetUpdateCheckJob;

/* AssetUpdateCheckEngine.status */
enum {
  AUCE_UPDATE_CHECK_DONE = 1 << 0,  /* Update check is finished for this engine. */
  AUCE_ENSURE_ASSETS_DONE = 1 << 1, /* Asset ensure is finished for this engine (if applicable). */
};

/* AssetUpdateCheckJob.flag */
enum {
  AUCJ_ENSURE_ASSETS = 1 << 0, /* Try to perform the 'ensure' task too. */
};

/* Helper to fetch a set of assets to handle, regrouped by asset engine. */
static void asset_update_engines_uuids_fetch(ListBase *engines,
                                             Main *bmain,
                                             AssetUUIDList *uuids,
                                             const short uuid_tags,
                                             const bool do_reset_tags)
{
  for (Library *lib = bmain->libraries.first; lib; lib = lib->id.next) {
    if (lib->asset_repository) {
      printf("Checking lib file '%s' (engine %s, ver. %d)\n",
             lib->filepath,
             lib->asset_repository->asset_engine,
             lib->asset_repository->asset_engine_version);

      AssetUpdateCheckEngine *auce = NULL;
      AssetEngineType *ae_type = BKE_asset_engines_find(lib->asset_repository->asset_engine);
      bool copy_engine = false;

      if (ae_type == NULL) {
        printf("ERROR! Unknown asset engine!\n");
      }

      for (AssetRef *aref = lib->asset_repository->assets.first; aref; aref = aref->next) {
        ID *id = ((LinkData *)aref->id_list.first)->data;
        BLI_assert(id->uuid);

        if (uuid_tags && !(id->uuid->tag & uuid_tags)) {
          continue;
        }

        if (uuids) {
          int i = uuids->nbr_uuids;
          bool skip = true;
          for (AssetUUID *uuid = uuids->uuids; i--; uuid++) {
            if (ASSETUUID_EQUAL(id->uuid, uuid)) {
              skip = false;
              break;
            }
          }
          if (skip) {
            continue;
          }
        }

        if (ae_type == NULL) {
          if (do_reset_tags) {
            id->uuid->tag = UUID_TAG_ENGINE_MISSING;
          }
          else {
            id->uuid->tag |= UUID_TAG_ENGINE_MISSING;
          }
          G.f |= G_ASSETS_FAIL;
          continue;
        }

        if (auce == NULL) {
          for (auce = engines->first; auce; auce = auce->next) {
            if (auce->ae->type == ae_type) {
              /* In case we have several engine versions for the same engine, we create several
               * AssetUpdateCheckEngine structs (since an uuid list can only handle one ae
               * version), using the same (shallow) copy of the actual asset engine. */
              copy_engine = (auce->uuids.asset_engine_version !=
                             lib->asset_repository->asset_engine_version);
              break;
            }
          }
          if (copy_engine || auce == NULL) {
            AssetUpdateCheckEngine *auce_prev = auce;
            auce = MEM_callocN(sizeof(*auce), __func__);
            auce->ae = copy_engine ? BKE_asset_engine_copy(auce_prev->ae) :
                                     BKE_asset_engine_create(ae_type, NULL);
            auce->ae_job_id = AE_JOB_ID_UNSET;
            auce->uuids.asset_engine_version = lib->asset_repository->asset_engine_version;
            BLI_addtail(engines, auce);
          }
        }

        printf("\tWe need to check for updated asset %s...\n", id->name);
        if (do_reset_tags) {
          id->uuid->tag = (id->tag & LIB_TAG_MISSING) ? UUID_TAG_ASSET_MISSING : 0;
        }

        auce->uuids.nbr_uuids++;
        BKE_asset_uuid_print(id->uuid);
        if (auce->uuids.nbr_uuids > auce->allocated_uuids) {
          auce->allocated_uuids += 16;
          BLI_assert(auce->uuids.nbr_uuids < auce->allocated_uuids);

          const size_t allocsize = sizeof(*auce->uuids.uuids) * (size_t)auce->allocated_uuids;
          auce->uuids.uuids = auce->uuids.uuids ?
                                  MEM_reallocN_id(auce->uuids.uuids, allocsize, __func__) :
                                  MEM_mallocN(allocsize, __func__);
        }
        auce->uuids.uuids[auce->uuids.nbr_uuids - 1] = *id->uuid;
      }
    }
  }
}

static void asset_updatecheck_startjob(void *aucjv, short *stop, short *do_update, float *progress)
{
  AssetUpdateCheckJob *aucj = aucjv;

  aucj->progress = progress;
  aucj->stop = stop;
  /* Using AE engine, worker thread here is just sleeping! */
  while (!*stop) {
    *do_update = true;
    PIL_sleep_ms(100);
  }
}

static void asset_updatecheck_update(void *aucjv)
{
  AssetUpdateCheckJob *aucj = aucjv;
  Main *bmain = G.main;

  const bool do_ensure = ((aucj->flag & AUCJ_ENSURE_ASSETS) != 0);
  bool is_finished = true;
  int nbr_engines = 0;

  *aucj->progress = 0.0f;

  /* TODO need to take care of 'broken' engines that error - in this case we probably want to
   * cancel the whole update process over effected libraries' data... */
  for (AssetUpdateCheckEngine *auce = aucj->engines.first; auce;
       auce = auce->next, nbr_engines++) {
    AssetEngine *ae = auce->ae;
    AssetEngineType *ae_type = ae->type;

    /* Step 1: we ask asset engine about status of all asset IDs from it. */
    if (!(auce->status & AUCE_UPDATE_CHECK_DONE)) {
      auce->ae_job_id = ae_type->update_check(ae, auce->ae_job_id, &auce->uuids);
      if (auce->ae_job_id == AE_JOB_ID_INVALID) { /* Immediate execution. */
        *aucj->progress += 1.0f;
        auce->status |= AUCE_UPDATE_CHECK_DONE;
      }
      else {
        *aucj->progress += ae_type->progress(ae, auce->ae_job_id);
        if ((ae_type->status(ae, auce->ae_job_id) & (AE_STATUS_RUNNING | AE_STATUS_VALID)) !=
            (AE_STATUS_RUNNING | AE_STATUS_VALID)) {
          auce->status |= AUCE_UPDATE_CHECK_DONE;
        }
      }

      if (auce->status & AUCE_UPDATE_CHECK_DONE) {
        auce->ae_job_id = AE_JOB_ID_UNSET;

        for (Library *lib = bmain->libraries.first; lib; lib = lib->id.next) {
          if (!lib->asset_repository ||
              (BKE_asset_engines_find(lib->asset_repository->asset_engine) != ae_type)) {
            continue;
          }

          /* UUIDs returned by update_check are assumed to be valid (one way or the other) in
           * current asset engine version. */
          lib->asset_repository->asset_engine_version = ae_type->version;

          int i = auce->uuids.nbr_uuids;
          for (AssetUUID *uuid = auce->uuids.uuids; i--; uuid++) {
            for (AssetRef *aref = lib->asset_repository->assets.first; aref; aref = aref->next) {
              ID *id = ((LinkData *)aref->id_list.first)->data;
              BLI_assert(id->uuid);
              if (ASSETUUID_EQUAL(id->uuid, uuid)) {
                *id->uuid = *uuid;

                if (id->uuid->tag & UUID_TAG_ENGINE_MISSING) {
                  G.f |= G_ASSETS_FAIL;
                  printf("\t%s uses a currently unknown asset engine!\n", id->name);
                }
                else if (id->uuid->tag & UUID_TAG_ASSET_MISSING) {
                  G.f |= G_ASSETS_FAIL;
                  printf("\t%s is currently unknown by asset engine!\n", id->name);
                }
                else if (id->uuid->tag & UUID_TAG_ASSET_RELOAD) {
                  G.f |= G_ASSETS_NEED_RELOAD;
                  printf("\t%s needs to be reloaded/updated!\n", id->name);
                }
                break;
              }
            }
          }
        }
      }
    }

    /* Step 2: If required and supported, we 'ensure' assets tagged as to be reloaded. */
    if (do_ensure && !(auce->status & AUCE_ENSURE_ASSETS_DONE) && ae_type->ensure_uuids != NULL) {
      /* TODO ensure entries! */
      *aucj->progress += 1.0f;
      auce->status |= AUCE_ENSURE_ASSETS_DONE;
      if (auce->status & AUCE_ENSURE_ASSETS_DONE) {
        auce->ae_job_id = AE_JOB_ID_UNSET;
      }
    }

    if ((auce->status & (AUCE_UPDATE_CHECK_DONE | AUCE_ENSURE_ASSETS_DONE)) !=
        (AUCE_UPDATE_CHECK_DONE | AUCE_ENSURE_ASSETS_DONE)) {
      is_finished = false;
    }
  }

  *aucj->progress /= (float)(do_ensure ? nbr_engines * 2 : nbr_engines);
  *aucj->stop = is_finished;
}

static void asset_updatecheck_endjob(void *aucjv)
{
  AssetUpdateCheckJob *aucj = aucjv;

  /* In case there would be some dangling update. */
  asset_updatecheck_update(aucjv);

  for (AssetUpdateCheckEngine *auce = aucj->engines.first; auce; auce = auce->next) {
    AssetEngine *ae = auce->ae;
    if (!ELEM(auce->ae_job_id, AE_JOB_ID_INVALID, AE_JOB_ID_UNSET)) {
      ae->type->kill(ae, auce->ae_job_id);
    }
  }
}

static void asset_updatecheck_free(void *aucjv)
{
  AssetUpdateCheckJob *aucj = aucjv;

  for (AssetUpdateCheckEngine *auce = aucj->engines.first; auce; auce = auce->next) {
    BKE_asset_engine_free(auce->ae);
    MEM_freeN(auce->uuids.uuids);
  }
  BLI_freelistN(&aucj->engines);

  MEM_freeN(aucj);
}

static void asset_updatecheck_start(const bContext *C)
{
  wmJob *wm_job;
  AssetUpdateCheckJob *aucj;

  Main *bmain = CTX_data_main(C);

  /* prepare job data */
  aucj = MEM_callocN(sizeof(*aucj), __func__);

  G.f &= ~(G_ASSETS_FAIL | G_ASSETS_NEED_RELOAD | G_ASSETS_QUIET);

  /* Get all assets' uuids, grouped by asset engine/versions - and with cleared status tags. */
  asset_update_engines_uuids_fetch(&aucj->engines, bmain, NULL, 0, true);

  /* Early out if there is nothing to do! */
  if (BLI_listbase_is_empty(&aucj->engines)) {
    asset_updatecheck_free(aucj);
    return;
  }

  /* setup job */
  wm_job = WM_jobs_get(CTX_wm_manager(C),
                       CTX_wm_window(C),
                       CTX_wm_area(C),
                       "Checking for asset updates...",
                       WM_JOB_PROGRESS,
                       WM_JOB_TYPE_ASSET_UPDATECHECK);
  WM_jobs_customdata_set(wm_job, aucj, asset_updatecheck_free);
  WM_jobs_timer(
      wm_job,
      0.1,
      0,
      0 /*NC_SPACE | ND_SPACE_FILE_LIST, NC_SPACE | ND_SPACE_FILE_LIST*/); /* TODO probably
                                                                              outliner stuff once
                                                                              UI is defined for
                                                                              this! */
  WM_jobs_callbacks(wm_job,
                    asset_updatecheck_startjob,
                    NULL,
                    asset_updatecheck_update,
                    asset_updatecheck_endjob);

  /* start the job */
  WM_jobs_start(CTX_wm_manager(C), wm_job);
}

static int wm_assets_update_check_exec(bContext *C, wmOperator *UNUSED(op))
{
  asset_updatecheck_start(C);

  return OPERATOR_FINISHED;
}

void WM_OT_assets_update_check(wmOperatorType *ot)
{
  ot->name = "Check Assets Update";
  ot->idname = "WM_OT_assets_update_check";
  ot->description = "Check/refresh status of assets (in a background job)";

  ot->exec = wm_assets_update_check_exec;
}

static int wm_assets_reload_exec(bContext *C, wmOperator *op)
{
  /* We need to:
   *   - get list of all asset IDs to reload (either via given uuids, or their tag), and regroup
   * them by asset engine.
   *   - tag somehow all their indirect 'dependencies' IDs.
   *   - call load_pre to get actual filepaths.
   *   - do reload/relocate and remap as in lib_reload.
   *   - cleanup indirect dependencies IDs with zero users.
   */
  Main *bmain = CTX_data_main(C);

  ListBase engines = {NULL};

  /* For now, ignore the uuids list of op. */
  asset_update_engines_uuids_fetch(&engines, bmain, NULL, UUID_TAG_ASSET_RELOAD, false);

  for (AssetUpdateCheckEngine *auce = engines.first; auce; auce = auce->next) {
    FileDirEntryArr *paths = BKE_asset_engine_uuids_load_pre(auce->ae, &auce->uuids);
    FileDirEntry *en;
    AssetUUID *uuid;

    char path[FILE_MAX_LIBEXTRA], libname[FILE_MAX];
    char *group, *name;

    short flag = 0;
    bool do_reload = true;

    WMLinkAppendData *lapp_data = wm_link_append_data_new(flag);
    lapp_data->root = paths->root;

    GHash *libraries = BLI_ghash_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, __func__);
    int lib_idx = 0;

#ifdef DEBUG_LIBRARY
    printf("Engine %s (ver. %d) returned root path '%s'\n",
           auce->ae->type->name,
           auce->ae->type->version,
           paths->root);
#endif
    for (en = paths->entries.first; en; en = en->next) {
#ifdef DEBUG_LIBRARY
      printf("\t-> %s\n", en->relpath);
#endif
      BLI_join_dirfile(path, sizeof(path), paths->root, en->relpath);

      if (BLO_library_path_explode(path, libname, &group, &name)) {
        BLI_assert(group && name);

        if (!BLI_ghash_haskey(libraries, libname)) {
          BLI_ghash_insert(libraries, BLI_strdup(libname), POINTER_FROM_INT(lib_idx));
          lib_idx++;
          wm_link_append_data_library_add(lapp_data, libname);
        }
      }
      /* Non-blend paths are only valid in asset engine context (virtual libraries). */
      else if (path_to_idcode(path)) {
        if (!BLI_ghash_haskey(libraries, "")) {
          BLI_ghash_insert(libraries, BLI_strdup(""), POINTER_FROM_INT(lib_idx));
          lib_idx++;
          wm_link_append_data_library_add(lapp_data, "");
        }
      }
      else {
        BLI_assert(0);
      }
    }

    for (en = paths->entries.first, uuid = auce->uuids.uuids; en; en = en->next, uuid++) {
      int idcode;
      const char *libname_def, *name_def;

      if (BLO_library_path_explode(path, libname, &group, &name)) {
        idcode = BKE_idtype_idcode_from_name(group);
        libname_def = libname;
        name_def = name;
      }
      else {
        idcode = path_to_idcode(path);
        libname_def = "";
        name_def = path;
      }
      if (idcode != 0) {
        WMLinkAppendDataItem *item;

        AssetRef *aref = BKE_libraries_asset_repository_uuid_find(bmain, uuid);
        ID *old_id = aref ? ((LinkData *)aref->id_list.first)->data : NULL;
        BLI_assert(!old_id || (old_id->uuid && ASSETUUID_EQUAL(old_id->uuid, uuid)));

        lib_idx = POINTER_AS_INT(BLI_ghash_lookup(libraries, libname_def));

        BLI_remlink(which_libbase(bmain, GS(old_id->name)), old_id);
        item = wm_link_append_data_item_add(lapp_data, name_def, idcode, uuid, old_id);
        BLI_BITMAP_ENABLE(item->libraries, lib_idx);
      }
    }

    lib_relocate_do(bmain, NULL, lapp_data, op->reports, auce->ae->type, do_reload);

    wm_asset_engine_load_post_from_append_data(C, auce->ae, lapp_data);

    wm_link_append_data_free(lapp_data);
    BLI_ghash_free(libraries, MEM_freeN, NULL);
    BKE_filedir_entryarr_clear(paths);
    MEM_freeN(paths);
  }

  /* Cleanup. */
  for (AssetUpdateCheckEngine *auce = engines.first; auce; auce = auce->next) {
    BKE_asset_engine_free(auce->ae);
    MEM_SAFE_FREE(auce->uuids.uuids);
  }
  BLI_freelistN(&engines);

  WM_event_add_notifier(C, NC_WINDOW, NULL);
  G.f &= ~G_ASSETS_NEED_RELOAD;

  return OPERATOR_FINISHED;
}

void WM_OT_assets_reload(wmOperatorType *ot)
{
  PropertyRNA *prop;

  ot->name = "Reload Assets";
  ot->idname = "WM_OT_assets_reload";
  ot->description =
      "Reload the given assets (either explicitely by their UUIDs, or all curently tagged for "
      "reloading)";

  //  ot->invoke = wm_assets_reload_invoke;
  ot->exec = wm_assets_reload_exec;

  ot->flag |= OPTYPE_UNDO; /* XXX Do we want to keep this? Is it even working? */

  prop = RNA_def_collection_runtime(
      ot->srna, "uuids", &RNA_AssetUUID, "UUIDs", "UUIDs of assets to reload");
  RNA_def_property_flag(prop, PROP_HIDDEN);
}

/** \} */