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
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
|
//-----------------------------------------------------------------------
// <copyright file="UnitySerializationUtility.cs" company="Sirenix IVS">
// Copyright (c) 2018 Sirenix IVS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
//#define PREFAB_DEBUG
namespace VRC.Udon.Serialization.OdinSerializer
{
using System.Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Utilities;
using UnityEngine;
using UnityEngine.Events;
using System.Runtime.CompilerServices;
using UnityEngine.Assertions;
using System.Runtime.Serialization;
#pragma warning disable 0618 // Obsolete Methods // VRC
#if PREFAB_DEBUG && !SIRENIX_INTERNAL
#warning "Prefab serialization debugging is enabled outside of Sirenix internal. Are you sure this is right?"
#endif
/// <summary>
/// Provides an array of utility wrapper methods for easy serialization and deserialization of Unity objects of any type.
/// Note that, during serialization, it is always assumed that we are running on Unity's main thread. Deserialization can
/// happen on any thread, and all API's interacting with deserialization are thread-safe.
/// <para />
/// Note that setting the IndexReferenceResolver on contexts passed into methods on this class will have no effect, as it will always
/// be set to a UnityReferenceResolver.
/// </summary>
public static class UnitySerializationUtility
{
public static readonly Type SerializeReferenceAttributeType = typeof(SerializeField).Assembly.GetType("UnityEngine.SerializeReference");
private static readonly Assembly String_Assembly = typeof(string).Assembly;
private static readonly Assembly HashSet_Assembly = typeof(HashSet<>).Assembly;
private static readonly Assembly LinkedList_Assembly = typeof(LinkedList<>).Assembly;
#if UNITY_EDITOR
/// <summary>
/// From the new scriptable build pipeline package
/// </summary>
[NonSerialized]
private static readonly Type SBP_ContentPipelineType = TwoWaySerializationBinder.Default.BindToType("UnityEditor.Build.Pipeline.ContentPipeline");
[NonSerialized]
private static readonly MethodInfo PrefabUtility_IsComponentAddedToPrefabInstance_MethodInfo = typeof(UnityEditor.PrefabUtility).GetMethod("IsComponentAddedToPrefabInstance");
[NonSerialized]
private static readonly HashSet<UnityEngine.Object> UnityObjectsWaitingForDelayedModificationApply = new HashSet<UnityEngine.Object>(ReferenceEqualityComparer<UnityEngine.Object>.Default);
[NonSerialized]
private static readonly Dictionary<UnityEngine.Object, List<PrefabModification>> RegisteredPrefabModifications = new Dictionary<UnityEngine.Object, List<PrefabModification>>(ReferenceEqualityComparer<UnityEngine.Object>.Default);
private static class PrefabDeserializeUtility
{
private static int updateCount = 0;
[NonSerialized]
public static readonly HashSet<UnityEngine.Object> PrefabsWithValuesApplied = new HashSet<UnityEngine.Object>(ReferenceEqualityComparer<UnityEngine.Object>.Default);
[NonSerialized]
private static readonly Dictionary<UnityEngine.Object, HashSet<object>> SceneObjectsToKeepOnApply = new Dictionary<UnityEngine.Object, HashSet<object>>(ReferenceEqualityComparer<UnityEngine.Object>.Default);
[NonSerialized]
public static readonly object DeserializePrefabs_LOCK = new object();
private static readonly List<UnityEngine.Object> toRemove = new List<UnityEngine.Object>();
static PrefabDeserializeUtility()
{
UnityEditor.EditorApplication.update += OnEditorUpdate;
}
/// <summary>
/// Note: it is assumed that code calling this is holding the DeserializePrefabCaches_LOCK lock, and will continue to hold it while the returned hashset is being modified
/// </summary>
public static HashSet<object> GetSceneObjectsToKeepSet(UnityEngine.Object unityObject, bool createIfDoesntExist)
{
HashSet<object> keep;
if (!SceneObjectsToKeepOnApply.TryGetValue(unityObject, out keep))
{
keep = new HashSet<object>(ReferenceEqualityComparer<object>.Default);
SceneObjectsToKeepOnApply.Add(unityObject, keep);
}
return keep;
}
public static void CleanSceneObjectToKeepOnApply()
{
lock (DeserializePrefabs_LOCK)
{
foreach (var obj in SceneObjectsToKeepOnApply.Keys)
{
if (obj == null)
{
toRemove.Add(obj);
}
}
for (int i = 0; i < toRemove.Count; i++)
{
SceneObjectsToKeepOnApply.Remove(toRemove[i]);
}
toRemove.Clear();
}
}
private static void OnEditorUpdate()
{
lock (DeserializePrefabs_LOCK)
{
updateCount++;
if (updateCount >= 1000)
{
SceneObjectsToKeepOnApply.Clear();
updateCount = 0;
}
}
}
}
#endif
// Note: Code that accesses any of these four caches should lock on the cache data structure itself
private static readonly Dictionary<MemberInfo, WeakValueGetter> UnityMemberGetters = new Dictionary<MemberInfo, WeakValueGetter>();
private static readonly Dictionary<MemberInfo, WeakValueSetter> UnityMemberSetters = new Dictionary<MemberInfo, WeakValueSetter>();
private static readonly Dictionary<MemberInfo, bool> UnityWillSerializeMembersCache = new Dictionary<MemberInfo, bool>();
private static readonly Dictionary<Type, bool> UnityWillSerializeTypesCache = new Dictionary<Type, bool>();
private static readonly HashSet<Type> UnityNeverSerializesTypes = new HashSet<Type>()
{
typeof(Coroutine)
};
private static readonly HashSet<string> UnityNeverSerializesTypeNames = new HashSet<string>()
{
"UnityEngine.AnimationState"
};
#if UNITY_EDITOR
/// <summary>
/// Whether to always force editor mode serialization. This member only exists in the editor.
/// </summary>
public static bool ForceEditorModeSerialization { get; set; }
/// <summary>
/// Not yet documented.
/// </summary>
public static List<PrefabModification> GetRegisteredPrefabModifications(UnityEngine.Object obj)
{
List<PrefabModification> result;
RegisteredPrefabModifications.TryGetValue(obj, out result);
return result;
}
public static bool HasModificationsWaitingForDelayedApply(UnityEngine.Object obj)
{
return UnityObjectsWaitingForDelayedModificationApply.Contains(obj);
}
#endif
/// <summary>
/// Checks whether Odin will serialize a given member.
/// </summary>
/// <param name="member">The member to check.</param>
/// <param name="serializeUnityFields">Whether to allow serialization of members that will also be serialized by Unity.</param>
/// <param name="policy">The policy that Odin should be using for serialization of the given member. If this parameter is null, it defaults to <see cref="SerializationPolicies.Unity"/>.</param>
/// <returns>True if Odin will serialize the member, otherwise false.</returns>
public static bool OdinWillSerialize(MemberInfo member, bool serializeUnityFields, ISerializationPolicy policy = null)
{
Dictionary<MemberInfo, CachedSerializationBackendResult> cacheForPolicy;
if (policy == null || object.ReferenceEquals(policy, UnityPolicy))
{
cacheForPolicy = OdinWillSerializeCache_UnityPolicy;
}
else if (object.ReferenceEquals(policy, EverythingPolicy))
{
cacheForPolicy = OdinWillSerializeCache_EverythingPolicy;
}
else if (object.ReferenceEquals(policy, StrictPolicy))
{
cacheForPolicy = OdinWillSerializeCache_StrictPolicy;
}
else
{
lock (OdinWillSerializeCache_CustomPolicies)
{
if (!OdinWillSerializeCache_CustomPolicies.TryGetValue(policy, out cacheForPolicy))
{
cacheForPolicy = new Dictionary<MemberInfo, CachedSerializationBackendResult>(ReferenceEqualityComparer<MemberInfo>.Default);
OdinWillSerializeCache_CustomPolicies.Add(policy, cacheForPolicy);
}
}
}
CachedSerializationBackendResult result;
lock (cacheForPolicy)
{
if (!cacheForPolicy.TryGetValue(member, out result))
{
result = default(CachedSerializationBackendResult);
if (serializeUnityFields)
{
result.SerializeUnityFieldsTrueResult = CalculateOdinWillSerialize(member, serializeUnityFields, policy ?? UnityPolicy);
result.HasCalculatedSerializeUnityFieldsTrueResult = true;
}
else
{
result.SerializeUnityFieldsFalseResult = CalculateOdinWillSerialize(member, serializeUnityFields, policy ?? UnityPolicy);
result.HasCalculatedSerializeUnityFieldsFalseResult = true;
}
cacheForPolicy.Add(member, result);
}
else
{
if (serializeUnityFields && !result.HasCalculatedSerializeUnityFieldsTrueResult)
{
result.SerializeUnityFieldsTrueResult = CalculateOdinWillSerialize(member, serializeUnityFields, policy ?? UnityPolicy);
result.HasCalculatedSerializeUnityFieldsTrueResult = true;
cacheForPolicy[member] = result;
}
else if (!serializeUnityFields && !result.HasCalculatedSerializeUnityFieldsFalseResult)
{
result.SerializeUnityFieldsFalseResult = CalculateOdinWillSerialize(member, serializeUnityFields, policy ?? UnityPolicy);
result.HasCalculatedSerializeUnityFieldsFalseResult = true;
cacheForPolicy[member] = result;
}
}
return serializeUnityFields ? result.SerializeUnityFieldsTrueResult : result.SerializeUnityFieldsFalseResult;
}
}
private static bool CalculateOdinWillSerialize(MemberInfo member, bool serializeUnityFields, ISerializationPolicy policy)
{
if (member.DeclaringType == typeof(UnityEngine.Object)) return false;
if (!policy.ShouldSerializeMember(member)) return false;
// Allow serialization of fields with [OdinSerialize], regardless of whether Unity
// serializes the field or not
if (member is FieldInfo && member.IsDefined(typeof(OdinSerializeAttribute), true))
{
return true;
}
// No need to check whether Unity serializes it or not, our answer will always be the same
if (serializeUnityFields) return true;
try
{
if (SerializeReferenceAttributeType != null && member.IsDefined(SerializeReferenceAttributeType, true))
{
// Unity is serializing it as a polymorphic value
return false;
}
}
catch { }
if (GuessIfUnityWillSerialize(member)) return false;
return true;
}
private struct CachedSerializationBackendResult
{
public bool HasCalculatedSerializeUnityFieldsTrueResult;
public bool HasCalculatedSerializeUnityFieldsFalseResult;
public bool SerializeUnityFieldsTrueResult;
public bool SerializeUnityFieldsFalseResult;
}
private static readonly ISerializationPolicy UnityPolicy = SerializationPolicies.Unity;
private static readonly ISerializationPolicy EverythingPolicy = SerializationPolicies.Everything;
private static readonly ISerializationPolicy StrictPolicy = SerializationPolicies.Strict;
private static readonly Dictionary<MemberInfo, CachedSerializationBackendResult> OdinWillSerializeCache_UnityPolicy = new Dictionary<MemberInfo, CachedSerializationBackendResult>(ReferenceEqualityComparer<MemberInfo>.Default);
private static readonly Dictionary<MemberInfo, CachedSerializationBackendResult> OdinWillSerializeCache_EverythingPolicy = new Dictionary<MemberInfo, CachedSerializationBackendResult>(ReferenceEqualityComparer<MemberInfo>.Default);
private static readonly Dictionary<MemberInfo, CachedSerializationBackendResult> OdinWillSerializeCache_StrictPolicy = new Dictionary<MemberInfo, CachedSerializationBackendResult>(ReferenceEqualityComparer<MemberInfo>.Default);
private static readonly Dictionary<ISerializationPolicy, Dictionary<MemberInfo, CachedSerializationBackendResult>> OdinWillSerializeCache_CustomPolicies = new Dictionary<ISerializationPolicy, Dictionary<MemberInfo, CachedSerializationBackendResult>>(ReferenceEqualityComparer<ISerializationPolicy>.Default);
/// <summary>
/// Guesses whether or not Unity will serialize a given member. This is not completely accurate.
/// </summary>
/// <param name="member">The member to check.</param>
/// <returns>True if it is guessed that Unity will serialize the member, otherwise false.</returns>
/// <exception cref="System.ArgumentNullException">The parameter <paramref name="member"/> is null.</exception>
public static bool GuessIfUnityWillSerialize(MemberInfo member)
{
if (member == null)
{
throw new ArgumentNullException("member");
}
bool result;
lock (UnityWillSerializeMembersCache)
{
if (!UnityWillSerializeMembersCache.TryGetValue(member, out result))
{
result = GuessIfUnityWillSerializePrivate(member);
UnityWillSerializeMembersCache[member] = result;
}
}
return result;
}
private static bool GuessIfUnityWillSerializePrivate(MemberInfo member)
{
FieldInfo fieldInfo = member as FieldInfo;
if (fieldInfo == null || fieldInfo.IsStatic || fieldInfo.IsInitOnly)
{
return false;
}
if (fieldInfo.IsDefined<NonSerializedAttribute>())
{
return false;
}
if (SerializeReferenceAttributeType != null && fieldInfo.IsDefined(SerializeReferenceAttributeType, true))
{
return true;
}
if (!typeof(UnityEngine.Object).IsAssignableFrom(fieldInfo.FieldType) && fieldInfo.FieldType == fieldInfo.DeclaringType)
{
// Unity will not serialize references that are obviously cyclical
return false;
}
if (!(fieldInfo.IsPublic || fieldInfo.IsDefined<SerializeField>()))
{
return false;
}
if (fieldInfo.IsDefined<FixedBufferAttribute>())
{
return UnityVersion.IsVersionOrGreater(2017, 1);
}
return GuessIfUnityWillSerialize(fieldInfo.FieldType);
}
/// <summary>
/// Guesses whether or not Unity will serialize a given type. This is not completely accurate.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True if it is guessed that Unity will serialize the type, otherwise false.</returns>
/// <exception cref="System.ArgumentNullException">The parameter <paramref name="type"/> is null.</exception>
public static bool GuessIfUnityWillSerialize(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
bool result;
lock (UnityWillSerializeTypesCache)
{
if (!UnityWillSerializeTypesCache.TryGetValue(type, out result))
{
result = GuessIfUnityWillSerializePrivate(type);
UnityWillSerializeTypesCache[type] = result;
}
}
return result;
}
private static bool GuessIfUnityWillSerializePrivate(Type type)
{
if (UnityNeverSerializesTypes.Contains(type) || UnityNeverSerializesTypeNames.Contains(type.FullName))
{
return false;
}
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
if (type.IsGenericType)
{
return UnityVersion.IsVersionOrGreater(2020, 1);
}
// Unity will always serialize all of its own special objects
// Except when they have generic type arguments.
return true;
}
if (type.IsAbstract || type.IsInterface || type == typeof(object))
{
return false;
}
if (type.IsEnum)
{
var underlyingType = Enum.GetUnderlyingType(type);
if (UnityVersion.IsVersionOrGreater(5, 6))
{
return underlyingType != typeof(long) && underlyingType != typeof(ulong);
}
else
{
return underlyingType == typeof(int) || underlyingType == typeof(byte);
}
}
if (type.IsPrimitive || type == typeof(string))
{
return true;
}
if (typeof(Delegate).IsAssignableFrom(type))
{
return false;
}
if (typeof(UnityEventBase).IsAssignableFrom(type))
{
if (type.IsGenericType && !UnityVersion.IsVersionOrGreater(2020, 1))
{
return false;
}
return (type == typeof(UnityEvent) || type.IsDefined<SerializableAttribute>(false));
}
if (type.IsArray)
{
// Unity does not support multidim arrays, or arrays of lists or arrays.
var elementType = type.GetElementType();
return type.GetArrayRank() == 1
&& !elementType.IsArray
&& !elementType.ImplementsOpenGenericClass(typeof(List<>))
&& GuessIfUnityWillSerialize(elementType);
}
if (type.IsGenericType && !type.IsGenericTypeDefinition && type.GetGenericTypeDefinition() == typeof(List<>))
{
// Unity does not support lists or arrays in lists.
var elementType = type.GetArgumentsOfInheritedOpenGenericClass(typeof(List<>))[0];
if (elementType.IsArray || elementType.ImplementsOpenGenericClass(typeof(List<>)))
{
return false;
}
return GuessIfUnityWillSerialize(elementType);
}
if (type.Assembly.FullName.StartsWith("UnityEngine", StringComparison.InvariantCulture) || type.Assembly.FullName.StartsWith("UnityEditor", StringComparison.InvariantCulture))
{
// We assume Unity will serialize all of their own structs and classes (many of them are not marked serializable).
// If not, well, too bad - the user can use the [OdinSerialize] attribute on their field/property in that case to trigger custom serialization.
return true;
}
// Unity 2020.1 added support for serializing arbitrary generic types directly
if (type.IsGenericType && !UnityVersion.IsVersionOrGreater(2020, 1))
{
return false;
}
// Unity does not serialize [Serializable] structs and classes if they are defined in mscorlib, System.dll or System.Core.dll if those are present
// Checking against the assemblies that declare System.String, HashSet<T> and LinkedList<T> is a simple way to do this.
if (type.Assembly == String_Assembly || type.Assembly == HashSet_Assembly || type.Assembly == LinkedList_Assembly)
{
return false;
}
if (type.IsDefined<SerializableAttribute>(false))
{
// Before Unity 4.5, Unity did not support serialization of custom structs, only custom classes
if (UnityVersion.IsVersionOrGreater(4, 5))
{
return true;
}
else
{
return type.IsClass;
}
}
// Check for synclists if legacy networking is present
// it was removed in 2018.2
if (!UnityVersion.IsVersionOrGreater(2018, 2))
{
Type current = type.BaseType;
while (current != null && current != typeof(object))
{
if (current.IsGenericType && current.GetGenericTypeDefinition().FullName == "UnityEngine.Networking.SyncListStruct`1")
{
return true;
}
current = current.BaseType;
};
}
return false;
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void SerializeUnityObject(UnityEngine.Object unityObject, ref SerializationData data, bool serializeUnityFields = false, SerializationContext context = null)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
#if UNITY_EDITOR
if (OdinPrefabSerializationEditorUtility.HasNewPrefabWorkflow)
{
ISupportsPrefabSerialization supporter = unityObject as ISupportsPrefabSerialization;
if (supporter != null)
{
var sData = supporter.SerializationData;
//if (!sData.ContainsData)
//{
// return;
//}
sData.Prefab = null;
supporter.SerializationData = sData;
}
}
{
bool pretendIsPlayer = Application.isPlaying && !UnityEditor.AssetDatabase.Contains(unityObject);
//
// Look through a stack trace to determine some things about the current serialization context.
// For example, we check if we are currently building a player, or if we are currently recording prefab instance property modifications.
// This is pretty hacky, but as far as we can tell it's the only way to do it.
//
{
var stackFrames = new System.Diagnostics.StackTrace().GetFrames();
Type buildPipelineType = typeof(UnityEditor.BuildPipeline);
Type prefabUtilityType = typeof(UnityEditor.PrefabUtility);
for (int i = 0; i < stackFrames.Length; i++)
{
var frame = stackFrames[i];
var method = frame.GetMethod();
if (method.DeclaringType == buildPipelineType || method.DeclaringType == SBP_ContentPipelineType)
{
// We are currently building a player
pretendIsPlayer = true;
break;
}
if (method.DeclaringType == prefabUtilityType && method.Name == "RecordPrefabInstancePropertyModifications")
{
// Do nothing whatsoever and return immediately, lest we break Unity's "smart" modification recording
return;
}
}
}
if (ForceEditorModeSerialization)
{
pretendIsPlayer = false;
}
//
// Prefab handling
//
// If we're not building a player and the Unity object is a prefab instance
// that supports special prefab serialization, we enter a special bail-out case.
//
if (!pretendIsPlayer)
{
UnityEngine.Object prefab = null;
SerializationData prefabData = default(SerializationData);
bool prefabDataIsFromSelf = false;
if (OdinPrefabSerializationEditorUtility.ObjectIsPrefabInstance(unityObject))
{
prefab = OdinPrefabSerializationEditorUtility.GetCorrespondingObjectFromSource(unityObject);
if (prefab.SafeIsUnityNull() && !object.ReferenceEquals(data.Prefab, null))
{
// Sometimes, GetPrefabParent does not return the prefab,
// because Unity is just completely unreliable.
//
// In these cases, we sometimes have a reference to the
// prefab in the data. If so, we can use that instead.
//
// Even though that reference is "fake null".
prefab = data.Prefab;
}
if (!object.ReferenceEquals(prefab, null))
{
if (prefab is ISupportsPrefabSerialization)
{
var pData = (prefab as ISupportsPrefabSerialization).SerializationData;
if (pData.ContainsData)
{
prefabData = pData;
}
else
{
prefabData = data;
prefabData.Prefab = null;
prefabDataIsFromSelf = true;
}
}
else if (prefab.GetType() != typeof(UnityEngine.Object))
{
//Debug.LogWarning(unityObject.name + " is a prefab instance, but the prefab reference type " + prefab.GetType().GetNiceName() + " does not implement the interface " + typeof(ISupportsPrefabSerialization).GetNiceName() + "; non-Unity-serialized data will most likely not be updated properly from the prefab any more.");
//prefab = null;
prefabData = data;
prefabData.Prefab = null;
prefabDataIsFromSelf = true;
}
}
}
if (!object.ReferenceEquals(prefab, null))
{
// We will bail out. But first...
if (!prefabDataIsFromSelf && prefabData.PrefabModifications != null && prefabData.PrefabModifications.Count > 0)
{
//
// This is a special case that can happen after changes to a prefab instance
// have been applied to the source prefab using "Apply Changes", thus copying
// the instances' applied changes over to the source prefab.
//
// We re-serialize the prefab, to make sure its data is properly saved.
// Though data saved this way will still work, it is quite inefficient.
//
try
{
(prefab as ISerializationCallbackReceiver).OnBeforeSerialize();
}
catch (Exception ex)
{
// This can sometimes throw null reference exceptions in the new prefab workflow,
// if people are doing nested stuff despite the fac that they really, really shouldn't.
//
// Just ignore it.
if (!OdinPrefabSerializationEditorUtility.HasNewPrefabWorkflow)
{
throw ex;
}
}
EditorApplication_delayCall_Alias += () =>
{
if (prefab)
{
UnityEditor.EditorUtility.SetDirty(prefab);
//UnityEditor.AssetDatabase.SaveAssets(); // Has a tendency to cause infinite serialization loops
}
};
prefabData = (prefab as ISupportsPrefabSerialization).SerializationData;
}
// Now we determine the modifications string to keep
bool newModifications = false;
List<string> modificationsToKeep;
List<PrefabModification> modificationsList;
List<UnityEngine.Object> modificationsReferencedUnityObjects = data.PrefabModificationsReferencedUnityObjects;
if (RegisteredPrefabModifications.TryGetValue(unityObject, out modificationsList))
{
RegisteredPrefabModifications.Remove(unityObject);
// We have to generate a new prefab modification string from the registered changes
modificationsToKeep = SerializePrefabModifications(modificationsList, ref modificationsReferencedUnityObjects);
#if PREFAB_DEBUG
Debug.Log("Setting new modifications: ", unityObject);
foreach (var mod in modificationsToKeep)
{
Debug.Log(" " + mod);
}
#endif
newModifications = true;
}
else
{
// Keep the old ones
modificationsToKeep = data.PrefabModifications;
}
// Make sure we have the same base data as the prefab (except UnityObject references), then change the rest
var unityObjects = data.ReferencedUnityObjects;
data = prefabData;
data.ReferencedUnityObjects = unityObjects;
//if (unityObjects.Count == prefabData.ReferencedUnityObjects.Count)
//{
//}
//else
//{
// var stackTrace = new System.Diagnostics.StackTrace();
// //DefaultLoggers.DefaultLogger.LogError(
// // "Prefab instance serialization error on object'" + unityObject.name + "': Unity object reference count mismatch " +
// // "between prefab and prefab instance in the core umodified data! This should never, ever happen! Prefab instance " +
// // "references have been replaced with the prefab's references! Unity object references may have been changed from " +
// // "expected values! Please report this error and how to reproduce it at 'http://bitbucket.org/sirenix/odin-inspector/issues'.");
//}
data.Prefab = prefab;
data.PrefabModifications = modificationsToKeep;
data.PrefabModificationsReferencedUnityObjects = modificationsReferencedUnityObjects;
if (newModifications)
{
SetUnityObjectModifications(unityObject, ref data, prefab);
}
// Now we determine the Unity object references to keep if this prefab instance is ever applied
if (data.Prefab != null) // It can still be "fake null", in which case, never mind
{
PrefabDeserializeUtility.CleanSceneObjectToKeepOnApply();
lock (PrefabDeserializeUtility.DeserializePrefabs_LOCK)
{
HashSet<object> keep = PrefabDeserializeUtility.GetSceneObjectsToKeepSet(unityObject, true);
keep.Clear();
if (data.PrefabModificationsReferencedUnityObjects != null && data.PrefabModificationsReferencedUnityObjects.Count > 0)
{
//var prefabRoot = UnityEditor.PrefabUtility.FindPrefabRoot(((Component)data.Prefab).gameObject);
var instanceRoot = UnityEditor.PrefabUtility.FindPrefabRoot(((Component)unityObject).gameObject);
foreach (var reference in data.PrefabModificationsReferencedUnityObjects)
{
if (reference == null) continue;
if (!(reference is GameObject || reference is Component)) continue;
if (UnityEditor.AssetDatabase.Contains(reference)) continue;
var referencePrefabType = UnityEditor.PrefabUtility.GetPrefabType(reference);
bool mightBeInPrefab = referencePrefabType == UnityEditor.PrefabType.Prefab
|| referencePrefabType == UnityEditor.PrefabType.PrefabInstance
|| referencePrefabType == UnityEditor.PrefabType.ModelPrefab
|| referencePrefabType == UnityEditor.PrefabType.ModelPrefabInstance;
if (!mightBeInPrefab)
{
if (PrefabUtility_IsComponentAddedToPrefabInstance_MethodInfo != null)
{
if (reference is Component && (bool)PrefabUtility_IsComponentAddedToPrefabInstance_MethodInfo.Invoke(null, new object[] { reference }))
{
mightBeInPrefab = true;
}
}
}
if (!mightBeInPrefab)
{
keep.Add(reference);
continue;
}
var gameObject = (GameObject)(reference is GameObject ? reference : (reference as Component).gameObject);
var referenceRoot = UnityEditor.PrefabUtility.FindPrefabRoot(gameObject);
if (referenceRoot != instanceRoot)
{
keep.Add(reference);
}
}
}
}
}
return; // Buh bye
}
}
//
// We are not dealing with a properly supported prefab instance if we get this far.
// Serialize as if it isn't a prefab instance.
//
// Ensure there is no superfluous data left over after serialization
// (We will reassign all necessary data.)
data.Reset();
DataFormat format;
// Get the format to serialize as
{
IOverridesSerializationFormat formatOverride = unityObject as IOverridesSerializationFormat;
if (formatOverride != null)
{
format = formatOverride.GetFormatToSerializeAs(pretendIsPlayer);
}
else if (GlobalSerializationConfig.HasInstanceLoaded)
{
if (pretendIsPlayer)
{
format = GlobalSerializationConfig.Instance.BuildSerializationFormat;
}
else
{
format = GlobalSerializationConfig.Instance.EditorSerializationFormat;
}
}
else if (pretendIsPlayer)
{
format = DataFormat.Binary;
}
else
{
format = DataFormat.Nodes;
}
}
ISerializationPolicy serializationPolicy = SerializationPolicies.Unity;
// Get the policy to serialize with
{
IOverridesSerializationPolicy policyOverride = unityObject as IOverridesSerializationPolicy;
if (policyOverride != null)
{
serializationPolicy = policyOverride.SerializationPolicy ?? SerializationPolicies.Unity;
if (context != null)
{
context.Config.SerializationPolicy = serializationPolicy;
}
serializeUnityFields = policyOverride.OdinSerializesUnityFields;
}
}
if (pretendIsPlayer)
{
// We pretend as though we're serializing outside of the editor
if (format == DataFormat.Nodes)
{
Debug.LogWarning("The serialization format '" + format.ToString() + "' is disabled in play mode, and when building a player. Defaulting to the format '" + DataFormat.Binary.ToString() + "' instead.");
format = DataFormat.Binary;
}
UnitySerializationUtility.SerializeUnityObject(unityObject, ref data.SerializedBytes, ref data.ReferencedUnityObjects, format, serializeUnityFields, context);
data.SerializedFormat = format;
}
else
{
if (format == DataFormat.Nodes)
{
// Special case for node format
if (context == null)
{
using (var newContext = Cache<SerializationContext>.Claim())
using (var writer = new SerializationNodeDataWriter(newContext))
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
if (data.SerializationNodes != null)
{
// Reuse pre-expanded list to keep GC down
data.SerializationNodes.Clear();
writer.Nodes = data.SerializationNodes;
}
resolver.Value.SetReferencedUnityObjects(data.ReferencedUnityObjects);
newContext.Value.Config.SerializationPolicy = serializationPolicy;
newContext.Value.IndexReferenceResolver = resolver.Value;
writer.Context = newContext;
UnitySerializationUtility.SerializeUnityObject(unityObject, writer, serializeUnityFields);
data.SerializationNodes = writer.Nodes;
data.ReferencedUnityObjects = resolver.Value.GetReferencedUnityObjects();
}
}
else
{
using (var writer = new SerializationNodeDataWriter(context))
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
if (data.SerializationNodes != null)
{
// Reuse pre-expanded list to keep GC down
data.SerializationNodes.Clear();
writer.Nodes = data.SerializationNodes;
}
resolver.Value.SetReferencedUnityObjects(data.ReferencedUnityObjects);
context.IndexReferenceResolver = resolver.Value;
UnitySerializationUtility.SerializeUnityObject(unityObject, writer, serializeUnityFields);
data.SerializationNodes = writer.Nodes;
data.ReferencedUnityObjects = resolver.Value.GetReferencedUnityObjects();
}
}
}
else
{
UnitySerializationUtility.SerializeUnityObject(unityObject, ref data.SerializedBytesString, ref data.ReferencedUnityObjects, format, serializeUnityFields, context);
}
data.SerializedFormat = format;
}
}
#else
{
DataFormat format;
IOverridesSerializationFormat formatOverride = unityObject as IOverridesSerializationFormat;
if (formatOverride != null)
{
format = formatOverride.GetFormatToSerializeAs(true);
}
else if (GlobalSerializationConfig.HasInstanceLoaded)
{
format = GlobalSerializationConfig.Instance.BuildSerializationFormat;
}
else
{
format = DataFormat.Binary;
}
if (format == DataFormat.Nodes)
{
Debug.LogWarning("The serialization format '" + format.ToString() + "' is disabled outside of the editor. Defaulting to the format '" + DataFormat.Binary.ToString() + "' instead.");
format = DataFormat.Binary;
}
UnitySerializationUtility.SerializeUnityObject(unityObject, ref data.SerializedBytes, ref data.ReferencedUnityObjects, format);
data.SerializedFormat = format;
}
#endif
}
#if UNITY_EDITOR
private static void SetUnityObjectModifications(UnityEngine.Object unityObject, ref SerializationData data, UnityEngine.Object prefab)
{
//
// We need to set the modifications to the prefab instance manually,
// to ensure that Unity gets it right and doesn't mess with them.
//
Type unityObjectType = unityObject.GetType();
var serializedDataField = unityObjectType.GetAllMembers<FieldInfo>(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(field => field.FieldType == typeof(SerializationData) && UnitySerializationUtility.GuessIfUnityWillSerialize(field))
.LastOrDefault();
if (serializedDataField == null)
{
Debug.LogError("Could not find a field of type " + typeof(SerializationData).Name + " on the serializing type " + unityObjectType.GetNiceName() + " when trying to manually set prefab modifications. It is possible that prefab instances of this type will be corrupted if changes are ever applied to prefab.", prefab);
}
else
{
string serializedDataPath = serializedDataField.Name + ".";
string referencedUnityObjectsPath = serializedDataPath + SerializationData.PrefabModificationsReferencedUnityObjectsFieldName + ".Array.";
string modificationsPath = serializedDataPath + SerializationData.PrefabModificationsFieldName + ".Array.";
string prefabPath = serializedDataPath + SerializationData.PrefabFieldName;
var mods = UnityEditor.PrefabUtility.GetPropertyModifications(unityObject).ToList();
//
// Clear all old modifications to serialized data out
//
for (int i = 0; i < mods.Count; i++)
{
var mod = mods[i];
if (mod.propertyPath.StartsWith(serializedDataPath, StringComparison.InvariantCulture) && object.ReferenceEquals(mod.target, prefab))
{
mods.RemoveAt(i);
i--;
}
}
//
// Add the new modifications
//
// Array length changes seem to always come first? Let's do that to be sure...
mods.Insert(0, new UnityEditor.PropertyModification()
{
target = prefab,
propertyPath = referencedUnityObjectsPath + "size",
value = data.PrefabModificationsReferencedUnityObjects.Count.ToString("D", CultureInfo.InvariantCulture)
});
mods.Insert(0, new UnityEditor.PropertyModification()
{
target = prefab,
propertyPath = modificationsPath + "size",
value = data.PrefabModifications.Count.ToString("D", CultureInfo.InvariantCulture)
});
// Then the prefab object reference
mods.Add(new UnityEditor.PropertyModification()
{
target = prefab,
propertyPath = prefabPath,
objectReference = prefab
});
// Then the actual array values
for (int i = 0; i < data.PrefabModificationsReferencedUnityObjects.Count; i++)
{
mods.Add(new UnityEditor.PropertyModification()
{
target = prefab,
propertyPath = referencedUnityObjectsPath + "data[" + i.ToString("D", CultureInfo.InvariantCulture) + "]",
objectReference = data.PrefabModificationsReferencedUnityObjects[i]
});
}
for (int i = 0; i < data.PrefabModifications.Count; i++)
{
mods.Add(new UnityEditor.PropertyModification()
{
target = prefab,
propertyPath = modificationsPath + "data[" + i.ToString("D", CultureInfo.InvariantCulture) + "]",
value = data.PrefabModifications[i]
});
}
// Set the Unity property modifications
// This won't always stick; there is code in the PropertyTree class
// that keeps checking if the number of custom modifications is correct
// and, if not, it keeps registering the change until Unity gets it.
// Setting the prefab modifications here directly has a tendency to crash the Unity Editor, so we use a delayed call
// so the modifications are set during a time that's better for Unity.
EditorApplication_delayCall_Alias += () =>
{
#if PREFAB_DEBUG
Debug.Log("DELAYED: Actually setting prefab modifications:");
foreach (var mod in mods)
{
if (!mod.propertyPath.StartsWith("serializationData")) continue;
int index = -1;
if (mod.target is Component)
{
Component com = mod.target as Component;
var coms = com.gameObject.GetComponents(com.GetType());
for (int j = 0; j < coms.Length; j++)
{
if (object.ReferenceEquals(coms[j], mod.target))
{
index = j;
break;
}
}
}
Debug.Log(" " + mod.target.name + " (" + index + ") " + mod.propertyPath + ": " + mod.value);
}
#endif
UnityObjectsWaitingForDelayedModificationApply.Remove(unityObject);
UnityEditor.PrefabUtility.SetPropertyModifications(unityObject, mods.ToArray());
};
UnityObjectsWaitingForDelayedModificationApply.Add(unityObject);
}
}
#endif
/// <summary>
/// Not yet documented.
/// </summary>
public static void SerializeUnityObject(UnityEngine.Object unityObject, ref string base64Bytes, ref List<UnityEngine.Object> referencedUnityObjects, DataFormat format, bool serializeUnityFields = false, SerializationContext context = null)
{
byte[] bytes = null;
SerializeUnityObject(unityObject, ref bytes, ref referencedUnityObjects, format, serializeUnityFields, context);
base64Bytes = Convert.ToBase64String(bytes);
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void SerializeUnityObject(UnityEngine.Object unityObject, ref byte[] bytes, ref List<UnityEngine.Object> referencedUnityObjects, DataFormat format, bool serializeUnityFields = false, SerializationContext context = null)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (format == DataFormat.Nodes)
{
Debug.LogError("The serialization data format '" + format.ToString() + "' is not supported by this method. You must create your own writer.");
return;
}
if (referencedUnityObjects == null)
{
referencedUnityObjects = new List<UnityEngine.Object>();
}
else
{
referencedUnityObjects.Clear();
}
using (var stream = Cache<CachedMemoryStream>.Claim())
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
resolver.Value.SetReferencedUnityObjects(referencedUnityObjects);
if (context != null)
{
context.IndexReferenceResolver = resolver.Value;
using (var writerCache = GetCachedUnityWriter(format, stream.Value.MemoryStream, context))
{
SerializeUnityObject(unityObject, writerCache.Value as IDataWriter, serializeUnityFields);
}
}
else
{
using (var con = Cache<SerializationContext>.Claim())
{
con.Value.Config.SerializationPolicy = SerializationPolicies.Unity;
/* If the config instance is not loaded (it should usually be, but in rare cases
* it's not), we must not ask for it, as we are not allowed to load from resources
* or the asset database during some serialization callbacks.
*
* (Trying to do that causes internal Unity errors and potentially even crashes.)
*
* If it's not loaded, we fall back to default values, since there's no other choice.
*/
if (GlobalSerializationConfig.HasInstanceLoaded)
{
//Debug.Log("Serializing " + unityObject.GetType().Name + " WITH loaded!");
con.Value.Config.DebugContext.ErrorHandlingPolicy = GlobalSerializationConfig.Instance.ErrorHandlingPolicy;
con.Value.Config.DebugContext.LoggingPolicy = GlobalSerializationConfig.Instance.LoggingPolicy;
con.Value.Config.DebugContext.Logger = GlobalSerializationConfig.Instance.Logger;
}
else
{
//Debug.Log("Serializing " + unityObject.GetType().Name + " WITHOUT loaded!");
con.Value.Config.DebugContext.ErrorHandlingPolicy = ErrorHandlingPolicy.Resilient;
con.Value.Config.DebugContext.LoggingPolicy = LoggingPolicy.LogErrors;
con.Value.Config.DebugContext.Logger = DefaultLoggers.UnityLogger;
}
con.Value.IndexReferenceResolver = resolver.Value;
using (var writerCache = GetCachedUnityWriter(format, stream.Value.MemoryStream, con))
{
SerializeUnityObject(unityObject, writerCache.Value as IDataWriter, serializeUnityFields);
}
}
}
bytes = stream.Value.MemoryStream.ToArray();
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void SerializeUnityObject(UnityEngine.Object unityObject, IDataWriter writer, bool serializeUnityFields = false)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (writer == null)
{
throw new ArgumentNullException("writer");
}
//if (unityObject is Component)
//{
// Component com = (Component)unityObject;
// if (com.gameObject.scene.IsValid())
// {
// Debug.Log("Serializing scene " + com.gameObject.scene.name + ": " + com, com);
// }
// else
// {
// var path = UnityEditor.AssetDatabase.GetAssetPath(com);
// Debug.Log("Serializing prefab '" + path + "': " + com, com);
// }
//}
//else
//{
// Debug.Log("Serializing " + unityObject, unityObject);
//}
try
{
writer.PrepareNewSerializationSession();
var members = FormatterUtilities.GetSerializableMembers(unityObject.GetType(), writer.Context.Config.SerializationPolicy);
object unityObjectInstance = unityObject;
for (int i = 0; i < members.Length; i++)
{
var member = members[i];
WeakValueGetter getter = null;
if (!OdinWillSerialize(member, serializeUnityFields, writer.Context.Config.SerializationPolicy) || (getter = GetCachedUnityMemberGetter(member)) == null)
{
continue;
}
var value = getter(ref unityObjectInstance);
bool isNull = object.ReferenceEquals(value, null);
// Never serialize serialization data. That way lies madness.
if (!isNull && value.GetType() == typeof(SerializationData))
{
continue;
}
Serializer serializer = Serializer.Get(FormatterUtilities.GetContainedType(member));
try
{
serializer.WriteValueWeak(member.Name, value, writer);
}
catch (Exception ex)
{
writer.Context.Config.DebugContext.LogException(ex);
}
}
writer.FlushToStream();
}
catch (SerializationAbortException ex)
{
throw new SerializationAbortException("Serialization of type '" + unityObject.GetType().GetNiceFullName() + "' aborted.", ex);
}
catch (Exception ex)
{
Debug.LogException(new Exception("Exception thrown while serializing type '" + unityObject.GetType().GetNiceFullName() + "': " + ex.Message, ex));
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void DeserializeUnityObject(UnityEngine.Object unityObject, ref SerializationData data, DeserializationContext context = null)
{
//#if UNITY_EDITOR
DeserializeUnityObject(unityObject, ref data, context, isPrefabData: false, prefabInstanceUnityObjects: null);
//#else
// UnitySerializationUtility.DeserializeUnityObject(unityObject, ref data.SerializedBytes, ref data.ReferencedUnityObjects, data.SerializedFormat, context);
// data = default(SerializationData); // Free all data for GC
//#endif
}
private static void DeserializeUnityObject(UnityEngine.Object unityObject, ref SerializationData data, DeserializationContext context, bool isPrefabData, List<UnityEngine.Object> prefabInstanceUnityObjects)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (isPrefabData && prefabInstanceUnityObjects == null)
{
prefabInstanceUnityObjects = new List<UnityEngine.Object>(); // There's likely no data at all
}
#if UNITY_EDITOR
if (OdinPrefabSerializationEditorUtility.HasNewPrefabWorkflow)
{
ISupportsPrefabSerialization supporter = unityObject as ISupportsPrefabSerialization;
if (supporter != null)
{
var sData = supporter.SerializationData;
if (!sData.ContainsData)
{
return;
}
sData.Prefab = null;
supporter.SerializationData = sData;
}
}
// TODO: This fix needs to be applied for edge-cases! But we also need a way to only do it while in the Editor! and if UNITY_EDITOR is not enough.
//Debug.Log("Deserializing" + new System.Diagnostics.StackTrace().ToString(), unityObject);
//var prefabDataObject = data.Prefab as ISupportsPrefabSerialization;
//if (prefabDataObject != null && data.PrefabModifications != null && UnityEditor.AssetDatabase.Contains(data.Prefab))
//{
// // In some rare cases, prefab instances can become corrupted if there somehow end up being applied bad prefab modifications
// // for the JSON, Nodes or Binary data fields. This will of course result in a corrupted data-stream, and if the resilient serialization mode fails
// // to be resilient, scenes will also give an error when opnened, and the object won't fix itself.
// // How these bad Unity prefab modifications ends up there in the first place, is a mystery.
// // ----------------
// // But it's easy enought to replicate:
// // 1. Create a game object with a SerializedMonoBehaviour containing a dictionary.
// // 2. Make it a prefab
// // 3. Instantiate the dictionary and add an entry to it in the prefab instance and hit apply (ONCE)
// // 4. Now totally random prefab modifications are applied to the SerializedNodes data member: https://jumpshare.com/v/eiZv39CUJKOuNCVhMu83
// // - These are harmless in many situations, as the values are exactly the same as in the prefab: https://jumpshare.com/v/aMlA0dD7gsA2uvO5ot48
// // - Furthermore, when hitting apply again, all of the bad modifications go away.
// // 5. But if we instead of hitting apply again, save the scene, and remove the dictionary member from the c# class,
// // we're now left with permanent modifications to the data.SerializaitionNodes, which will result
// // in a corrupted data stream, as we continue to modify the prefab it self.
// // 6. Set the serialization mode to log warnings and errors, remove the dictioanry from the c# class and open the scene again.
// // ----------------
// // Here we make sure that we deserialize the object using the data from the prefab, and override any potentially corrupted data.
// // In prefab instances, the only thing we care about are the actual prefab-modifications (data.PrefabModifications and data.PrefabModificationsReferencedUnityObjects)
// // This fix will also remove any corruped data, and trigger Unity to remove all bad prefab modifications.
// var prefabData = prefabDataObject.SerializationData;
// if (prefabData.ContainsData)
// {
// data.SerializedFormat = prefabData.SerializedFormat;
// data.SerializationNodes = prefabData.SerializationNodes ?? new List<SerializationNode>();
// data.ReferencedUnityObjects = prefabData.ReferencedUnityObjects ?? new List<UnityEngine.Object>();
// data.SerializedBytesString = prefabData.SerializedBytesString ?? "";
// data.SerializedBytes = prefabData.SerializedBytes ?? new byte[0];
// }
//}
#endif
if ((data.SerializedBytes != null && data.SerializedBytes.Length > 0) && (data.SerializationNodes == null || data.SerializationNodes.Count == 0))
{
// If it happens that we have bytes in the serialized bytes array
// then we deserialize from that instead.
// This happens often in play mode, when instantiating, since we
// are emulating build behaviour.
if (data.SerializedFormat == DataFormat.Nodes)
{
// The stored format says nodes, but there is no serialized node data.
// Figure out what format the serialized bytes are in, and deserialize that format instead
DataFormat formatGuess = data.SerializedBytes[0] == '{' ? DataFormat.JSON : DataFormat.Binary;
try
{
var bytesStr = ProperBitConverter.BytesToHexString(data.SerializedBytes);
Debug.LogWarning("Serialization data has only bytes stored, but the serialized format is marked as being 'Nodes', which is incompatible with data stored as a byte array. Based on the appearance of the serialized bytes, Odin has guessed that the data format is '" + formatGuess + "', and will attempt to deserialize the bytes using that format. The serialized bytes follow, converted to a hex string: " + bytesStr);
}
catch { }
UnitySerializationUtility.DeserializeUnityObject(unityObject, ref data.SerializedBytes, ref data.ReferencedUnityObjects, formatGuess, context);
}
else
{
UnitySerializationUtility.DeserializeUnityObject(unityObject, ref data.SerializedBytes, ref data.ReferencedUnityObjects, data.SerializedFormat, context);
}
// If there are any prefab modifications, we should *always* apply those
ApplyPrefabModifications(unityObject, data.PrefabModifications, data.PrefabModificationsReferencedUnityObjects);
}
else
{
Cache<DeserializationContext> cachedContext = null;
try
{
if (context == null)
{
cachedContext = Cache<DeserializationContext>.Claim();
context = cachedContext;
context.Config.SerializationPolicy = SerializationPolicies.Unity;
/* If the config instance is not loaded (it should usually be, but in rare cases
* it's not), we must not ask for it, as we are not allowed to load from resources
* or the asset database during some serialization callbacks.
*
* (Trying to do that causes internal Unity errors and potentially even crashes.)
*
* If it's not loaded, we fall back to default values, since there's no other choice.
*/
if (GlobalSerializationConfig.HasInstanceLoaded)
{
//Debug.Log("Deserializing " + unityObject.GetType().Name + " WITH loaded!");
context.Config.DebugContext.ErrorHandlingPolicy = GlobalSerializationConfig.Instance.ErrorHandlingPolicy;
context.Config.DebugContext.LoggingPolicy = GlobalSerializationConfig.Instance.LoggingPolicy;
context.Config.DebugContext.Logger = GlobalSerializationConfig.Instance.Logger;
}
else
{
//Debug.Log("Deserializing " + unityObject.GetType().Name + " WITHOUT loaded!");
context.Config.DebugContext.ErrorHandlingPolicy = ErrorHandlingPolicy.Resilient;
context.Config.DebugContext.LoggingPolicy = LoggingPolicy.LogErrors;
context.Config.DebugContext.Logger = DefaultLoggers.UnityLogger;
}
}
// If we have a policy override, use that
{
IOverridesSerializationPolicy policyOverride = unityObject as IOverridesSerializationPolicy;
if (policyOverride != null)
{
var serializationPolicy = policyOverride.SerializationPolicy;
if (serializationPolicy != null)
{
context.Config.SerializationPolicy = serializationPolicy;
}
}
}
if (!isPrefabData && !data.Prefab.SafeIsUnityNull())
{
if (data.Prefab is ISupportsPrefabSerialization)
{
if (object.ReferenceEquals(data.Prefab, unityObject) && data.PrefabModifications != null && data.PrefabModifications.Count > 0)
{
// We are deserializing a prefab, which has *just* had changes applied
// from an instance of itself.
//
// This is the only place, anywhere, where we can detect this happening
// so we need to register it, so the prefab instance that just applied
// its values knows to wipe all of its modifications clean.
// However, we only do this in the editor. If it happens outside of the
// editor it would be deeply strange, but we shouldn't correct anything
// in that case as it makes no sense.
#if UNITY_EDITOR
lock (PrefabDeserializeUtility.DeserializePrefabs_LOCK)
{
PrefabDeserializeUtility.PrefabsWithValuesApplied.Add(unityObject);
}
#endif
}
else
{
// We are dealing with a prefab instance, which is a special bail-out case
SerializationData prefabData = (data.Prefab as ISupportsPrefabSerialization).SerializationData;
#if UNITY_EDITOR
lock (PrefabDeserializeUtility.DeserializePrefabs_LOCK)
{
// Only perform this check in the editor, as we are never dealing with a prefab
// instance outside of the editor - even if the serialized data is weird
if (PrefabDeserializeUtility.PrefabsWithValuesApplied.Contains(data.Prefab))
{
// Our prefab has had values applied; now to check if the object we're
// deserializing was the one to apply those values. If it is, then we
// have to wipe all of this object's prefab modifications clean.
//
// So far, the only way we know how to do that, is checking whether this
// object is currently selected.
if (PrefabSelectionTracker.IsCurrentlySelectedPrefabRoot(unityObject))
{
PrefabDeserializeUtility.PrefabsWithValuesApplied.Remove(data.Prefab);
List<PrefabModification> newModifications = null;
HashSet<object> keep = PrefabDeserializeUtility.GetSceneObjectsToKeepSet(unityObject, false);
if (data.PrefabModificationsReferencedUnityObjects.Count > 0 && keep != null && keep.Count > 0)
{
newModifications = DeserializePrefabModifications(data.PrefabModifications, data.PrefabModificationsReferencedUnityObjects);
newModifications.RemoveAll(n => object.ReferenceEquals(n.ModifiedValue, null) || !keep.Contains(n.ModifiedValue));
}
else
{
if (data.PrefabModifications != null)
{
data.PrefabModifications.Clear();
}
if (data.PrefabModificationsReferencedUnityObjects != null)
{
data.PrefabModificationsReferencedUnityObjects.Clear();
}
}
newModifications = newModifications ?? new List<PrefabModification>();
PrefabModificationCache.CachePrefabModifications(unityObject, newModifications);
RegisterPrefabModificationsChange(unityObject, newModifications);
}
}
}
#endif
if (!prefabData.ContainsData)
{
// Sometimes, the prefab hasn't actually been deserialized yet, because
// Unity doesn't do anything in a sensible way at all.
//
// In this case, we have to deserialize from our own data, and just
// pretend it's the prefab's data. We can just hope Unity hasn't messed
// with the serialized data; it *should* be the same on this instance as
// it is on the prefab itself.
//
// This case occurs often during editor recompile reloads.
DeserializeUnityObject(unityObject, ref data, context, isPrefabData: true, prefabInstanceUnityObjects: data.ReferencedUnityObjects);
}
else
{
// Deserialize the current object with the prefab's data
DeserializeUnityObject(unityObject, ref prefabData, context, isPrefabData: true, prefabInstanceUnityObjects: data.ReferencedUnityObjects);
}
// Then apply the prefab modifications using the deserialization context
ApplyPrefabModifications(unityObject, data.PrefabModifications, data.PrefabModificationsReferencedUnityObjects);
return; // Buh bye
}
}
// A straight UnityEngine.Object instance means that the type has been lost to Unity due to a deleted or renamed script
// We shouldn't complain in this case, as Unity itself will make it clear to the user that there is something wrong.
else if (data.Prefab.GetType() != typeof(UnityEngine.Object))
{
Debug.LogWarning("The type " + data.Prefab.GetType().GetNiceName() + " no longer supports special prefab serialization (the interface " + typeof(ISupportsPrefabSerialization).GetNiceName() + ") upon deserialization of an instance of a prefab; prefab data may be lost. Has a type been lost?");
}
}
var unityObjects = isPrefabData ? prefabInstanceUnityObjects : data.ReferencedUnityObjects;
if (data.SerializedFormat == DataFormat.Nodes)
{
// Special case for node format
using (var reader = new SerializationNodeDataReader(context))
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
resolver.Value.SetReferencedUnityObjects(unityObjects);
context.IndexReferenceResolver = resolver.Value;
reader.Nodes = data.SerializationNodes;
UnitySerializationUtility.DeserializeUnityObject(unityObject, reader);
}
}
else if (data.SerializedBytes != null && data.SerializedBytes.Length > 0)
{
UnitySerializationUtility.DeserializeUnityObject(unityObject, ref data.SerializedBytes, ref unityObjects, data.SerializedFormat, context);
}
else
{
UnitySerializationUtility.DeserializeUnityObject(unityObject, ref data.SerializedBytesString, ref unityObjects, data.SerializedFormat, context);
}
// We may have a prefab that has had changes applied to it; either way, apply the stored modifications.
ApplyPrefabModifications(unityObject, data.PrefabModifications, data.PrefabModificationsReferencedUnityObjects);
}
finally
{
if (cachedContext != null)
{
Cache<DeserializationContext>.Release(cachedContext);
}
}
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void DeserializeUnityObject(UnityEngine.Object unityObject, ref string base64Bytes, ref List<UnityEngine.Object> referencedUnityObjects, DataFormat format, DeserializationContext context = null)
{
if (string.IsNullOrEmpty(base64Bytes))
{
return;
}
byte[] bytes = null;
try
{
bytes = Convert.FromBase64String(base64Bytes);
}
catch (FormatException)
{
Debug.LogError("Invalid base64 string when deserializing data: " + base64Bytes);
}
if (bytes != null)
{
DeserializeUnityObject(unityObject, ref bytes, ref referencedUnityObjects, format, context);
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void DeserializeUnityObject(UnityEngine.Object unityObject, ref byte[] bytes, ref List<UnityEngine.Object> referencedUnityObjects, DataFormat format, DeserializationContext context = null)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (bytes == null || bytes.Length == 0)
{
return;
}
if (format == DataFormat.Nodes)
{
try
{
Debug.LogError("The serialization data format '" + format.ToString() + "' is not supported by this method. You must create your own reader.");
}
catch { }
return;
}
if (referencedUnityObjects == null)
{
referencedUnityObjects = new List<UnityEngine.Object>();
}
using (var stream = Cache<CachedMemoryStream>.Claim())
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
stream.Value.MemoryStream.Write(bytes, 0, bytes.Length);
stream.Value.MemoryStream.Position = 0;
resolver.Value.SetReferencedUnityObjects(referencedUnityObjects);
if (context != null)
{
context.IndexReferenceResolver = resolver.Value;
using (var readerCache = GetCachedUnityReader(format, stream.Value.MemoryStream, context))
{
DeserializeUnityObject(unityObject, readerCache.Value as IDataReader);
}
}
else
{
using (var con = Cache<DeserializationContext>.Claim())
{
con.Value.Config.SerializationPolicy = SerializationPolicies.Unity;
/* If the config instance is not loaded (it should usually be, but in rare cases
* it's not), we must not ask for it, as we are not allowed to load from resources
* or the asset database during some serialization callbacks.
*
* (Trying to do that causes internal Unity errors and potentially even crashes.)
*
* If it's not loaded, we fall back to default values, since there's no other choice.
*/
if (GlobalSerializationConfig.HasInstanceLoaded)
{
//Debug.Log("Deserializing " + unityObject.GetType().Name + " WITH loaded!");
con.Value.Config.DebugContext.ErrorHandlingPolicy = GlobalSerializationConfig.Instance.ErrorHandlingPolicy;
con.Value.Config.DebugContext.LoggingPolicy = GlobalSerializationConfig.Instance.LoggingPolicy;
con.Value.Config.DebugContext.Logger = GlobalSerializationConfig.Instance.Logger;
}
else
{
//Debug.Log("Deserializing " + unityObject.GetType().Name + " WITHOUT loaded!");
con.Value.Config.DebugContext.ErrorHandlingPolicy = ErrorHandlingPolicy.Resilient;
con.Value.Config.DebugContext.LoggingPolicy = LoggingPolicy.LogErrors;
con.Value.Config.DebugContext.Logger = DefaultLoggers.UnityLogger;
}
con.Value.IndexReferenceResolver = resolver.Value;
using (var readerCache = GetCachedUnityReader(format, stream.Value.MemoryStream, con))
{
DeserializeUnityObject(unityObject, readerCache.Value as IDataReader);
}
}
}
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static void DeserializeUnityObject(UnityEngine.Object unityObject, IDataReader reader)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (reader == null)
{
throw new ArgumentNullException("reader");
}
var policyOverride = unityObject as IOverridesSerializationPolicy;
if (policyOverride != null)
{
var policy = policyOverride.SerializationPolicy;
if (policy != null)
{
reader.Context.Config.SerializationPolicy = policy;
}
}
try
{
reader.PrepareNewSerializationSession();
var members = FormatterUtilities.GetSerializableMembersMap(unityObject.GetType(), reader.Context.Config.SerializationPolicy);
int count = 0;
string name;
EntryType entryType;
object unityObjectInstance = unityObject;
while ((entryType = reader.PeekEntry(out name)) != EntryType.EndOfNode && entryType != EntryType.EndOfArray && entryType != EntryType.EndOfStream)
{
MemberInfo member = null;
WeakValueSetter setter = null;
bool skip = false;
if (entryType == EntryType.Invalid)
{
// Oh boy. We have a lot of logging to do!
var message = "Encountered invalid entry while reading serialization data for Unity object of type '" + unityObject.GetType().GetNiceFullName() + "'. " +
"This likely means that Unity has filled Odin's stored serialization data with garbage, which can randomly happen after upgrading the Unity version of the project, or when otherwise doing things that have a lot of fragile interactions with the asset database. " +
"Locating the asset which causes this error log and causing it to reserialize (IE, modifying it and then causing it to be saved to disk) is likely to 'fix' the issue and make this message go away. " +
"Even so, DATA MAY HAVE BEEN LOST, and you should verify with your version control system (you're using one, right?!) that everything is alright, and if not, use it to rollback the asset to recover your data.\n\n\n";
#if UNITY_EDITOR
// Schedule a delayed log:
try
{
message += "A delayed warning message containing the originating object's name, type and scene/asset path (if applicable) will be scheduled for logging on Unity's main thread. Search for \"DELAYED SERIALIZATION LOG\". " +
"This logging callback will also mark the object dirty if it is an asset, hopefully making the issue 'fix' itself. HOWEVER, THERE MAY STILL BE DATA LOSS.\n\n\n";
EditorApplication_delayCall_Alias += () =>
{
var log = "DELAYED SERIALIZATION LOG: Name = " + unityObject.name + ", Type = " + unityObject.GetType().GetNiceFullName();
UnityEngine.Object toPing = unityObject;
var component = unityObject as Component;
if (component != null && component.gameObject.scene.IsValid())
{
log += ", ScenePath = " + component.gameObject.scene.path;
}
if (UnityEditor.AssetDatabase.Contains(unityObject))
{
var path = UnityEditor.AssetDatabase.GetAssetPath(unityObject);
log += ", AssetPath = " + path;
toPing = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path);
if (toPing == null) toPing = unityObject;
UnityEditor.EditorUtility.SetDirty(unityObject);
UnityEditor.AssetDatabase.SaveAssets();
}
Debug.LogWarning(log, toPing);
};
}
catch
{
Debug.LogWarning("DELAYED SERIALIZATION LOG: Delaying log to main thread failed, likely due to a race condition when subscribing to EditorApplication.delayCall; this cannot be guarded against from our code. Try to provoke the error again and hope to get luckier next time!");
}
#endif
message +=
"IF YOU HAVE CONSISTENT REPRODUCTION STEPS THAT MAKE THIS ISSUE REOCCUR, please report it at this issue at 'https://bitbucket.org/sirenix/odin-inspector/issues/526', and copy paste this debug message into your comment, along with any potential actions or recent changes in the project that might have happened to cause this message to occur. " +
"If the data dump in this message is cut off, please find the editor's log file (see https://docs.unity3d.com/Manual/LogFiles.html) and copy paste the full version of this message from there.\n\n\n" +
"Data dump:\n\n" +
" Reader type: " + reader.GetType().Name + "\n";
try
{
message += " Data dump: " + reader.GetDataDump();
//if (reader is SerializationNodeDataReader)
//{
// var nodes = (reader as SerializationNodeDataReader).Nodes;
// message += " Nodes dump: \n\n" + string.Join("\n", nodes.Select(node => " - Name: " + node.Name + "\n Entry: " + node.Entry + "\n Data: " + node.Data).ToArray());
//}
//else if (reader.Stream is MemoryStream)
//{
// message += " Data stream dump (base64): " + ProperBitConverter.BytesToHexString((reader.Stream as MemoryStream).ToArray());
//}
}
finally
{
reader.Context.Config.DebugContext.LogError(message);
skip = true;
}
}
else if (string.IsNullOrEmpty(name))
{
reader.Context.Config.DebugContext.LogError("Entry of type \"" + entryType + "\" in node \"" + reader.CurrentNodeName + "\" is missing a name.");
skip = true;
}
else if (members.TryGetValue(name, out member) == false || (setter = GetCachedUnityMemberSetter(member)) == null)
{
skip = true;
}
if (skip)
{
reader.SkipEntry();
continue;
}
{
Type expectedType = FormatterUtilities.GetContainedType(member);
Serializer serializer = Serializer.Get(expectedType);
try
{
object value = serializer.ReadValueWeak(reader);
setter(ref unityObjectInstance, value);
}
catch (Exception ex)
{
reader.Context.Config.DebugContext.LogException(ex);
}
}
count++;
if (count > 1000)
{
reader.Context.Config.DebugContext.LogError("Breaking out of infinite reading loop! (Read more than a thousand entries for one type!)");
break;
}
}
}
catch (SerializationAbortException ex)
{
throw new SerializationAbortException("Deserialization of type '" + unityObject.GetType().GetNiceFullName() + "' aborted.", ex);
}
catch (Exception ex)
{
Debug.LogException(new Exception("Exception thrown while deserializing type '" + unityObject.GetType().GetNiceFullName() + "': " + ex.Message, ex));
}
}
/// <summary>
/// Not yet documented.
/// </summary>
public static List<string> SerializePrefabModifications(List<PrefabModification> modifications, ref List<UnityEngine.Object> referencedUnityObjects)
{
if (referencedUnityObjects == null)
{
referencedUnityObjects = new List<UnityEngine.Object>();
}
else if (referencedUnityObjects.Count > 0)
{
referencedUnityObjects.Clear();
}
if (modifications == null || modifications.Count == 0)
{
return new List<string>();
}
// Sort modifications alphabetically by path; this will ensure that modifications
// to child paths are always applied after modifications to the parent paths
modifications.Sort((a, b) =>
{
int compared = a.Path.CompareTo(b.Path);
if (compared == 0)
{
if ((a.ModificationType == PrefabModificationType.ListLength || a.ModificationType == PrefabModificationType.Dictionary) && b.ModificationType == PrefabModificationType.Value)
{
return 1;
}
else if (a.ModificationType == PrefabModificationType.Value && (b.ModificationType == PrefabModificationType.ListLength || b.ModificationType == PrefabModificationType.Dictionary))
{
return -1;
}
}
return compared;
});
List<string> result = new List<string>();
using (var context = Cache<SerializationContext>.Claim())
using (var stream = CachedMemoryStream.Claim())
using (var writerCache = Cache<JsonDataWriter>.Claim())
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
var writer = writerCache.Value;
writer.Context = context;
writer.Stream = stream.Value.MemoryStream;
writer.PrepareNewSerializationSession();
writer.FormatAsReadable = false;
writer.EnableTypeOptimization = false;
resolver.Value.SetReferencedUnityObjects(referencedUnityObjects);
writer.Context.IndexReferenceResolver = resolver.Value;
for (int i = 0; i < modifications.Count; i++)
{
var mod = modifications[i];
if (mod.ModificationType == PrefabModificationType.ListLength)
{
writer.MarkJustStarted();
writer.WriteString("path", mod.Path);
writer.WriteInt32("length", mod.NewLength);
writer.FlushToStream();
result.Add(GetStringFromStreamAndReset(stream.Value.MemoryStream));
}
else if (mod.ModificationType == PrefabModificationType.Value)
{
writer.MarkJustStarted();
writer.WriteString("path", mod.Path);
if (mod.ReferencePaths != null && mod.ReferencePaths.Count > 0)
{
writer.BeginStructNode("references", null);
{
for (int j = 0; j < mod.ReferencePaths.Count; j++)
{
writer.WriteString(null, mod.ReferencePaths[j]);
}
}
writer.EndNode("references");
}
var serializer = Serializer.Get<object>();
serializer.WriteValueWeak("value", mod.ModifiedValue, writer);
writer.FlushToStream();
result.Add(GetStringFromStreamAndReset(stream.Value.MemoryStream));
}
else if (mod.ModificationType == PrefabModificationType.Dictionary)
{
writer.MarkJustStarted();
writer.WriteString("path", mod.Path);
Serializer.Get<object[]>().WriteValue("add_keys", mod.DictionaryKeysAdded, writer);
Serializer.Get<object[]>().WriteValue("remove_keys", mod.DictionaryKeysRemoved, writer);
writer.FlushToStream();
result.Add(GetStringFromStreamAndReset(stream.Value.MemoryStream));
}
// We don't want modifications to be able to reference each other
writer.Context.ResetInternalReferences();
}
}
return result;
}
private static string GetStringFromStreamAndReset(Stream stream)
{
byte[] bytes = new byte[stream.Position];
stream.Position = 0;
stream.Read(bytes, 0, bytes.Length);
stream.Position = 0;
return Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// Not yet documented.
/// </summary>
public static List<PrefabModification> DeserializePrefabModifications(List<string> modifications, List<UnityEngine.Object> referencedUnityObjects)
{
if (modifications == null || modifications.Count == 0)
{
// Nothing to apply
return new List<PrefabModification>();
}
List<PrefabModification> result = new List<PrefabModification>();
int longestByteCount = 0;
for (int i = 0; i < modifications.Count; i++)
{
int count = modifications[i].Length * 2;
if (count > longestByteCount)
{
longestByteCount = count;
}
}
using (var context = Cache<DeserializationContext>.Claim())
using (var streamCache = CachedMemoryStream.Claim(longestByteCount))
using (var readerCache = Cache<JsonDataReader>.Claim())// GetCachedUnityReader(DataFormat.JSON, streamCache.Value.MemoryStream, context))
using (var resolver = Cache<UnityReferenceResolver>.Claim())
{
var stream = streamCache.Value.MemoryStream;
var reader = readerCache.Value;
reader.Context = context;
reader.Stream = stream;
resolver.Value.SetReferencedUnityObjects(referencedUnityObjects);
reader.Context.IndexReferenceResolver = resolver.Value;
for (int i = 0; i < modifications.Count; i++)
{
string modStr = modifications[i];
byte[] bytes = Encoding.UTF8.GetBytes(modStr);
stream.SetLength(bytes.Length);
stream.Position = 0;
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
PrefabModification modification = new PrefabModification();
string entryName;
EntryType entryType;
reader.PrepareNewSerializationSession();
entryType = reader.PeekEntry(out entryName);
if (entryType == EntryType.EndOfStream)
{
// We might have reached the end of stream from a prior modification string
// If we have, force our way into the first entry in the new string
reader.SkipEntry();
}
while ((entryType = reader.PeekEntry(out entryName)) != EntryType.EndOfNode && entryType != EntryType.EndOfArray && entryType != EntryType.EndOfStream)
{
if (entryName == null)
{
Debug.LogError("Unexpected entry of type " + entryType + " without a name.");
reader.SkipEntry();
continue;
}
if (entryName.Equals("path", StringComparison.InvariantCultureIgnoreCase))
{
reader.ReadString(out modification.Path);
}
else if (entryName.Equals("length", StringComparison.InvariantCultureIgnoreCase))
{
reader.ReadInt32(out modification.NewLength);
modification.ModificationType = PrefabModificationType.ListLength;
}
else if (entryName.Equals("references", StringComparison.InvariantCultureIgnoreCase))
{
modification.ReferencePaths = new List<string>();
Type dummy;
reader.EnterNode(out dummy);
{
while (reader.PeekEntry(out entryName) == EntryType.String)
{
string path;
reader.ReadString(out path);
modification.ReferencePaths.Add(path);
}
}
reader.ExitNode();
}
else if (entryName.Equals("value", StringComparison.InvariantCultureIgnoreCase))
{
modification.ModifiedValue = Serializer.Get<object>().ReadValue(reader);
modification.ModificationType = PrefabModificationType.Value;
}
else if (entryName.Equals("add_keys", StringComparison.InvariantCultureIgnoreCase))
{
modification.DictionaryKeysAdded = Serializer.Get<object[]>().ReadValue(reader);
modification.ModificationType = PrefabModificationType.Dictionary;
}
else if (entryName.Equals("remove_keys", StringComparison.InvariantCultureIgnoreCase))
{
modification.DictionaryKeysRemoved = Serializer.Get<object[]>().ReadValue(reader);
modification.ModificationType = PrefabModificationType.Dictionary;
}
else
{
Debug.LogError("Unexpected entry name '" + entryName + "' while deserializing prefab modifications.");
reader.SkipEntry();
}
}
if (modification.Path == null)
{
// This happens quite often if you change the structure of a class which cointains prefab modifications made to it.
// Those invalid prefab modifications should be removed, which they seem to be in most cases, but apparently not some.
//
// And debugging a warning here makes give errors because of the bug in VS Bridge. And prevents people from bulding.
// Debug.LogWarning("Error when deserializing prefab modification; no path found. Modification lost; string was: '" + modStr + "'.");
continue;
}
result.Add(modification);
}
}
return result;
}
#if UNITY_EDITOR
/// <summary>
/// Not yet documented.
/// </summary>
public static void RegisterPrefabModificationsChange(UnityEngine.Object unityObject, List<PrefabModification> modifications)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
#if PREFAB_DEBUG
//Debug.Log((Event.current == null ? "NO EVENT" : Event.current.type.ToString()) + ": Registering " + (modifications == null ? 0 : modifications.Count) + " modifications to " + unityObject.name + ":");
Debug.Log("Registering " + (modifications == null ? 0 : modifications.Count) + " prefab modifications: ");
for (int i = 0; i < modifications.Count; i++)
{
var mod = modifications[i];
if (mod.ModificationType == PrefabModificationType.ListLength)
{
Debug.Log(" LENGTH@" + mod.Path + ": " + mod.NewLength);
}
else if (mod.ModificationType == PrefabModificationType.Dictionary)
{
Debug.Log(" DICT@" + mod.Path + ": (add: " + (mod.DictionaryKeysAdded != null ? mod.DictionaryKeysAdded.Length : 0) + ") -- (remove: " + (mod.DictionaryKeysRemoved != null ? mod.DictionaryKeysRemoved.Length : 0) + ")");
}
else
{
string str;
if (mod.ModifiedValue == null)
{
str = "null";
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(mod.ModificationType.GetType()))
{
str = "Unity object";
}
else
{
str = mod.ModificationType.ToString();
}
Debug.Log(" VALUE@" + mod.Path + ": " + str);
}
}
#endif
PrefabModificationCache.CachePrefabModifications(unityObject, modifications);
RegisteredPrefabModifications[unityObject] = modifications;
}
#endif
/// <summary>
/// Creates an object with default values initialized in the style of Unity; strings will be "", classes will be instantiated recursively with default values, and so on.
/// </summary>
public static object CreateDefaultUnityInitializedObject(Type type)
{
Assert.IsNotNull(type);
Assert.IsFalse(type.IsAbstract);
Assert.IsFalse(type.IsInterface);
return CreateDefaultUnityInitializedObject(type, 0);
}
private static object CreateDefaultUnityInitializedObject(Type type, int depth)
{
if (depth > 5)
{
return null;
}
if (!UnitySerializationUtility.GuessIfUnityWillSerialize(type))
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
if (type == typeof(string))
{
return "";
}
else if (type.IsEnum)
{
var values = Enum.GetValues(type);
return values.Length > 0 ? values.GetValue(0) : Enum.ToObject(type, 0);
}
else if (type.IsPrimitive)
{
return Activator.CreateInstance(type);
}
else if (type.IsArray)
{
Assert.IsTrue(type.GetArrayRank() == 1);
return Array.CreateInstance(type.GetElementType(), 0);
}
else if (type.ImplementsOpenGenericClass(typeof(List<>)) || typeof(UnityEventBase).IsAssignableFrom(type))
{
try
{
return Activator.CreateInstance(type);
}
catch
{
return null;
}
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return null;
}
else if ((type.Assembly.GetName().Name.StartsWith("UnityEngine") || type.Assembly.GetName().Name.StartsWith("UnityEditor")) && type.GetConstructor(Type.EmptyTypes) != null)
{
try
{
return Activator.CreateInstance(type);
}
catch (Exception ex)
{
Debug.LogException(ex);
return null;
}
}
object value;
if (type.GetConstructor(Type.EmptyTypes) != null)
{
return Activator.CreateInstance(type);
}
value = FormatterServices.GetUninitializedObject(type);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
for (int i = 0; i < fields.Length; i++)
{
var field = fields[i];
if (GuessIfUnityWillSerialize(field))
{
field.SetValue(value, CreateDefaultUnityInitializedObject(field.FieldType, depth + 1));
}
}
return value;
}
private static void ApplyPrefabModifications(UnityEngine.Object unityObject, List<string> modificationData, List<UnityEngine.Object> referencedUnityObjects)
{
if (unityObject == null)
{
throw new ArgumentNullException("unityObject");
}
if (modificationData == null || modificationData.Count == 0)
{
#if UNITY_EDITOR
PrefabModificationCache.CachePrefabModifications(unityObject, new List<PrefabModification>());
#endif
// Nothing to apply.
return;
}
var modifications = DeserializePrefabModifications(modificationData, referencedUnityObjects);
#if UNITY_EDITOR
PrefabModificationCache.CachePrefabModifications(unityObject, modifications);
#endif
for (int i = 0; i < modifications.Count; i++)
{
var mod = modifications[i];
try
{
mod.Apply(unityObject);
}
catch (Exception ex)
{
Debug.Log("The following exception was thrown when trying to apply a prefab modification for path '" + mod.Path + "':");
Debug.LogException(ex);
}
}
}
private static WeakValueGetter GetCachedUnityMemberGetter(MemberInfo member)
{
lock (UnityMemberGetters)
{
WeakValueGetter result;
if (UnityMemberGetters.TryGetValue(member, out result) == false)
{
if (member is FieldInfo)
{
result = EmitUtilities.CreateWeakInstanceFieldGetter(member.DeclaringType, member as FieldInfo);
}
else if (member is PropertyInfo)
{
result = EmitUtilities.CreateWeakInstancePropertyGetter(member.DeclaringType, member as PropertyInfo);
}
else
{
result = delegate (ref object instance)
{
return FormatterUtilities.GetMemberValue(member, instance);
};
}
UnityMemberGetters.Add(member, result);
}
return result;
}
}
private static WeakValueSetter GetCachedUnityMemberSetter(MemberInfo member)
{
lock (UnityMemberSetters)
{
WeakValueSetter result;
if (UnityMemberSetters.TryGetValue(member, out result) == false)
{
if (member is FieldInfo)
{
result = EmitUtilities.CreateWeakInstanceFieldSetter(member.DeclaringType, member as FieldInfo);
}
else if (member is PropertyInfo)
{
result = EmitUtilities.CreateWeakInstancePropertySetter(member.DeclaringType, member as PropertyInfo);
}
else
{
result = delegate (ref object instance, object value)
{
FormatterUtilities.SetMemberValue(member, instance, value);
};
}
UnityMemberSetters.Add(member, result);
}
return result;
}
}
private static ICache GetCachedUnityWriter(DataFormat format, Stream stream, SerializationContext context)
{
ICache cache;
switch (format)
{
case DataFormat.Binary:
{
var c = Cache<BinaryDataWriter>.Claim();
c.Value.Stream = stream;
cache = c;
}
break;
case DataFormat.JSON:
{
var c = Cache<JsonDataWriter>.Claim();
c.Value.Stream = stream;
cache = c;
}
break;
case DataFormat.Nodes:
throw new InvalidOperationException("Don't do this for nodes!");
default:
throw new NotImplementedException(format.ToString());
}
(cache.Value as IDataWriter).Context = context;
return cache;
//IDataWriter writer;
//if (UnityWriters.TryGetValue(format, out writer) == false)
//{
// writer = SerializationUtility.CreateWriter(stream, context, format);
// UnityWriters.Add(format, writer);
//}
//else
//{
// writer.Context = context;
// if (writer is BinaryDataWriter)
// {
// (writer as BinaryDataWriter).Stream = stream;
// }
// else if (writer is JsonDataWriter)
// {
// (writer as JsonDataWriter).Stream = stream;
// }
//}
//return writer;
}
private static ICache GetCachedUnityReader(DataFormat format, Stream stream, DeserializationContext context)
{
ICache cache;
switch (format)
{
case DataFormat.Binary:
{
var c = Cache<BinaryDataReader>.Claim();
c.Value.Stream = stream;
cache = c;
}
break;
case DataFormat.JSON:
{
var c = Cache<JsonDataReader>.Claim();
c.Value.Stream = stream;
cache = c;
}
break;
case DataFormat.Nodes:
throw new InvalidOperationException("Don't do this for nodes!");
default:
throw new NotImplementedException(format.ToString());
}
(cache.Value as IDataReader).Context = context;
return cache;
//if (UnityReaders.TryGetValue(format, out reader) == false)
//{
// reader = SerializationUtility.CreateReader(stream, context, format);
// UnityReaders.Add(format, reader);
//}
//else
//{
// reader.Context = context;
// if (reader is BinaryDataReader)
// {
// (reader as BinaryDataReader).Stream = stream;
// }
// else if (reader is JsonDataReader)
// {
// (reader as JsonDataReader).Stream = stream;
// }
//}
//return reader;
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoad]
private static class PrefabSelectionTracker
{
private static readonly object LOCK = new object();
private static readonly HashSet<UnityEngine.Object> selectedPrefabObjects;
static PrefabSelectionTracker()
{
selectedPrefabObjects = new HashSet<UnityEngine.Object>(ReferenceEqualityComparer<UnityEngine.Object>.Default);
UnityEditor.Selection.selectionChanged += OnSelectionChanged;
OnSelectionChanged();
}
public static bool IsCurrentlySelectedPrefabRoot(UnityEngine.Object obj)
{
lock (LOCK)
{
return selectedPrefabObjects.Contains(obj);
}
//var component = obj as Component;
//if (object.ReferenceEquals(component, null))
//{
// Debug.LogError("A non-component type Unity object (type '" + obj.GetType() + "') is acting like a prefab. What?", obj);
// return false;
//}
//var prefabRoot = UnityEditor.PrefabUtility.FindPrefabRoot(component.gameObject);
//return prefabRoot != null && PrefabSelectionTracker.SelectedPrefabRoots.Contains(prefabRoot);
//var selectedObjects = PrefabSelectionTracker.SelectedPrefabRoots;
//for (int i = 0; i < selectedObjects.Count; i++)
//{
// if (object.ReferenceEquals(obj, selectedObjects[i]))
// {
// return true;
// }
//}
//return false;
}
private static void OnSelectionChanged()
{
lock (LOCK)
{
selectedPrefabObjects.Clear();
var rootPrefabs = UnityEditor.Selection.objects
.Where(n =>
{
if (!(n is GameObject)) return false;
var prefabType = UnityEditor.PrefabUtility.GetPrefabType(n);
return prefabType == UnityEditor.PrefabType.Prefab
|| prefabType == UnityEditor.PrefabType.ModelPrefab
|| prefabType == UnityEditor.PrefabType.PrefabInstance
|| prefabType == UnityEditor.PrefabType.ModelPrefabInstance;
})
.Select(n => UnityEditor.PrefabUtility.FindPrefabRoot((GameObject)n))
.Distinct();
foreach (var root in rootPrefabs)
{
RegisterRecursive(root);
}
}
//SelectedPrefabRoots.AddRange(selection);
//for (int i = 0; i < selection.Length; i++)
//{
// var obj = selection[i];
// GameObject gameObject = obj as GameObject;
// if (!gameObject.SafeIsUnityNull())
// {
// SelectedPrefabRoots.AddRange(gameObject.GetComponents(typeof(Component)));
// }
//}
}
private static void RegisterRecursive(GameObject go)
{
selectedPrefabObjects.Add(go);
var components = go.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
selectedPrefabObjects.Add(components[i]);
}
var transform = go.transform;
for (int i = 0; i < transform.childCount; i++)
{
var child = transform.GetChild(i);
RegisterRecursive(child.gameObject);
}
}
}
public static class PrefabModificationCache
{
private static readonly Dictionary<object, List<PrefabModification>> CachedDeserializedModifications = new Dictionary<object, List<PrefabModification>>(ReferenceEqualityComparer<object>.Default);
private static readonly Dictionary<object, int> CachedDeserializedModificationTimes = new Dictionary<object, int>(ReferenceEqualityComparer<object>.Default);
private static readonly object Caches_LOCK = new object();
private static int counter = 0;
public static List<PrefabModification> DeserializePrefabModificationsCached(UnityEngine.Object obj, List<string> modifications, List<UnityEngine.Object> referencedUnityObjects)
{
lock (Caches_LOCK)
{
List<PrefabModification> result;
if (!CachedDeserializedModifications.TryGetValue(obj, out result))
{
result = DeserializePrefabModifications(modifications, referencedUnityObjects);
CachedDeserializedModifications.Add(obj, result);
}
CachedDeserializedModificationTimes[obj] = ++counter;
PrunePrefabModificationsCache();
return result;
}
}
public static void CachePrefabModifications(UnityEngine.Object obj, List<PrefabModification> modifications)
{
lock (Caches_LOCK)
{
CachedDeserializedModifications[obj] = modifications;
CachedDeserializedModificationTimes[obj] = ++counter;
PrunePrefabModificationsCache();
}
}
private static void PrunePrefabModificationsCache()
{
const int CACHE_SIZE = 10;
if (CachedDeserializedModifications.Count != CachedDeserializedModificationTimes.Count)
{
CachedDeserializedModifications.Clear();
CachedDeserializedModificationTimes.Clear();
}
// Once, this was a 'while count > CACHE_SIZE' loop, but in certain cases that can infinite loop
// so now it's a simpler and harder-to-break for loop with extra debugging clauses in the body.
int removeCount = CachedDeserializedModificationTimes.Count - CACHE_SIZE;
for (int i = 0; i < removeCount; i++)
{
object lowestObj = null;
int lowestTime = int.MaxValue;
foreach (var pair in CachedDeserializedModificationTimes)
{
if (pair.Value < lowestTime)
{
lowestObj = pair.Key;
lowestTime = pair.Value;
}
}
CachedDeserializedModifications.Remove(lowestObj);
if (!CachedDeserializedModificationTimes.Remove(lowestObj))
{
Debug.LogError("A Unity object instance of type '" + lowestObj.GetType().GetNiceName() + "' has likely become corrupt or destroyed somehow, yet deserialization has been invoked for it. If you're in the editor, you can click this log message to attempt to highlight the object. (It probably won't work, but there's a chance. If the highlighting doesn't work, the object instance is so broken that Odin cannot give you any more info about it than this message contains. Good luck!)", lowestObj as UnityEngine.Object);
// There are bad keys in the dictionaries; we have to clear them and just rebuild the cache.
// This theory isn't confirmed, but it's probably because UnityEngine.Object.GetHashCode()
// returns inconsistent results/changes for destroyed objects.
//
// If we don't clear the dictionaries, we will never be able to remove the bad keys. In olden
// days, this was the cause of infinite looping.
CachedDeserializedModifications.Clear();
CachedDeserializedModificationTimes.Clear();
}
}
}
}
private static readonly MemberInfo EditorApplication_delayCall_Member = typeof(UnityEditor.EditorApplication).GetMember("delayCall", Flags.StaticAnyVisibility).FirstOrDefault();
/// <summary>
/// In 2020.1, Unity changed EditorApplication.delayCall from a field to an event, meaning
/// we now have to use reflection to access it consistently across all versions of Unity.
/// </summary>
private static event Action EditorApplication_delayCall_Alias
{
add
{
if (EditorApplication_delayCall_Member == null) throw new InvalidOperationException("EditorApplication.delayCall field or event could not be found. Odin will be broken.");
if (EditorApplication_delayCall_Member is FieldInfo)
{
UnityEditor.EditorApplication.CallbackFunction val = (UnityEditor.EditorApplication.CallbackFunction)(EditorApplication_delayCall_Member as FieldInfo).GetValue(null);
val += value.ConvertDelegate<UnityEditor.EditorApplication.CallbackFunction>();
(EditorApplication_delayCall_Member as FieldInfo).SetValue(null, val);
}
else if (EditorApplication_delayCall_Member is EventInfo)
{
(EditorApplication_delayCall_Member as EventInfo).AddEventHandler(null, value);
}
else
{
if (EditorApplication_delayCall_Member == null) throw new InvalidOperationException("EditorApplication.delayCall was not a field or an event. Odin will be broken.");
}
}
remove
{
if (EditorApplication_delayCall_Member == null) throw new InvalidOperationException("EditorApplication.delayCall field or event could not be found. Odin will be broken.");
if (EditorApplication_delayCall_Member is FieldInfo)
{
UnityEditor.EditorApplication.CallbackFunction val = (UnityEditor.EditorApplication.CallbackFunction)(EditorApplication_delayCall_Member as FieldInfo).GetValue(null);
val -= value.ConvertDelegate<UnityEditor.EditorApplication.CallbackFunction>();
(EditorApplication_delayCall_Member as FieldInfo).SetValue(null, val);
}
else if (EditorApplication_delayCall_Member is EventInfo)
{
(EditorApplication_delayCall_Member as EventInfo).RemoveEventHandler(null, value);
}
else
{
if (EditorApplication_delayCall_Member == null) throw new InvalidOperationException("EditorApplication.delayCall was not a field or an event. Odin will be broken.");
}
}
}
private static T ConvertDelegate<T>(this Delegate src)
{
if (src == null || src.GetType() == typeof(T))
return (T)(object)src;
if (src.GetInvocationList().Count() == 1)
{
return (T)(object)Delegate.CreateDelegate(typeof(T), src.Target, src.Method);
}
else
{
return (T)(object)src.GetInvocationList().Aggregate<Delegate, Delegate>(null, (current, d) => Delegate.Combine(current, (Delegate)(object)ConvertDelegate<T>(d)));
}
}
#endif
}
}
|