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
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
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
using System.Collections.Generic;
using Photon.Deterministic;
using System;
using Quantum.Core;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Quantum.Inspector;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Quantum;
using Quantum.Profiling;
using Quantum.Allocator;
using System.Collections.Specialized;
using System.Threading;
using Quantum.Prototypes;
using System.Reflection;
using Quantum.Task;
using System.Runtime.CompilerServices;

// Core/CommandSetup.cs

namespace Quantum {
  public static partial class DeterministicCommandSetup {
    public static IDeterministicCommandFactory[] GetCommandFactories(RuntimeConfig gameConfig, SimulationConfig simulationConfig) {
      var factories = new List<IDeterministicCommandFactory>() {
        // pre-defined core commands
        Core.DebugCommand.CreateCommand(),
        new DeterministicCommandPool<Core.CompoundCommand>(),
      };

      AddCommandFactoriesUser(factories, gameConfig, simulationConfig);
      
#pragma warning disable 618 // Use of obsolete members
      var obsoleteCommandsCreated = CommandSetup.CreateCommands(gameConfig, simulationConfig);
      if (obsoleteCommandsCreated != null && obsoleteCommandsCreated.Length > 0) {
        Log.Warn("'CommandSetup.CreateCommands' is now deprecated. " +
                 "Implement a partial declaration of '" + nameof(DeterministicCommandSetup) + "." + nameof(AddCommandFactoriesUser) + "' instead, as shown on 'CommandSetup.User.cs' available on the SDK package." +
                 "Command instances can be used as factories of their own type.");
        factories.AddRange(obsoleteCommandsCreated);
      }
#pragma warning restore 618

      return factories.ToArray();
    }

    static partial void AddCommandFactoriesUser(ICollection<IDeterministicCommandFactory> factories, RuntimeConfig gameConfig, SimulationConfig simulationConfig);
  }
}

// Core/Collision.cs

namespace Quantum {
  
  /// <summary>
  /// Interface for receiving callbacks once per frame while two non-trigger 2D colliders are touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnCollision2D : ISignal {
    /// <summary>
    /// Called once per frame while two non-trigger 2D colliders are touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="CollisionInfo2D"/> with data about the collision.</param>
    /// \ingroup Physics2dApi
    void OnCollision2D(Frame f, CollisionInfo2D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once two non-trigger 2D colliders start touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnCollisionEnter2D : ISignal {
    /// <summary>
    /// Called once two non-trigger 2D colliders start touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="CollisionInfo2D"/> with data about the collision.</param>
    /// \ingroup Physics2dApi
    void OnCollisionEnter2D(Frame f, CollisionInfo2D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once two non-trigger 2D colliders stop touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnCollisionExit2D : ISignal {
    /// <summary>
    /// Called once two non-trigger 2D colliders stop touching.
    /// </summary>
    /// <param name="f">The frame in which the entities stopped touching.</param>
    /// <param name="info">The <see cref="ExitInfo2D"/> with the entities that were touching.</param>
    /// \ingroup Physics2dApi
    void OnCollisionExit2D(Frame f, ExitInfo2D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once per frame while a non-trigger and a trigger 2D colliders are touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnTrigger2D : ISignal {
    /// <summary>
    /// Called once per frame while a non-trigger and a trigger 2D colliders are touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="TriggerInfo2D"/> with data about the trigger collision.</param>
    /// \ingroup Physics2dApi
    void OnTrigger2D(Frame f, TriggerInfo2D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once a non-trigger and a trigger 2D colliders start touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnTriggerEnter2D : ISignal {
    /// <summary>
    /// Called once a non-trigger and a trigger 2D colliders start touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="TriggerInfo2D"/> with data about the trigger collision.</param>
    /// \ingroup Physics2dApi
    void OnTriggerEnter2D(Frame f, TriggerInfo2D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once a non-trigger and a trigger 2D colliders stop touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine2D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics2dApi
  public interface ISignalOnTriggerExit2D : ISignal {
    /// <summary>
    /// Called once a non-trigger and a trigger 2D colliders stop touching.
    /// </summary>
    /// <param name="f">The frame in which the entities stopped touching.</param>
    /// <param name="info">The <see cref="ExitInfo2D"/> with the entities that were touching.</param>
    /// \ingroup Physics2dApi
    void OnTriggerExit2D(Frame f, ExitInfo2D info);
  }
  
  /// <summary>
  /// Interface for receiving callbacks once per frame while two non-trigger 3D colliders are touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnCollision3D : ISignal {
    /// <summary>
    /// Called once per frame while two non-trigger 3D colliders are touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="CollisionInfo3D"/> with data about the collision.</param>
    /// \ingroup Physics3dApi
    void OnCollision3D(Frame f, CollisionInfo3D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once two non-trigger 3D colliders start touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnCollisionEnter3D : ISignal {
    /// <summary>
    /// Called once two non-trigger 3D colliders start touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="CollisionInfo3D"/> with data about the collision.</param>
    /// \ingroup Physics3dApi
    void OnCollisionEnter3D(Frame f, CollisionInfo3D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once two non-trigger 3D colliders stop touching.
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnCollisionExit3D : ISignal {
    /// <summary>
    /// Called once two non-trigger 3D colliders stop touching.
    /// </summary>
    /// <param name="f">The frame in which the entities stopped touching.</param>
    /// <param name="info">The <see cref="ExitInfo3D"/> with the entities that were touching.</param>
    /// \ingroup Physics3dApi
    void OnCollisionExit3D(Frame f, ExitInfo3D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once per frame while a non-trigger and a trigger 3D colliders are touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnTrigger3D : ISignal {
    /// <summary>
    /// Called once per frame while a non-trigger and a trigger 3D colliders are touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="TriggerInfo3D"/> with data about the trigger collision.</param>
    /// \ingroup Physics3dApi
    void OnTrigger3D(Frame f, TriggerInfo3D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once a non-trigger and a trigger 3D colliders start touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnTriggerEnter3D : ISignal {
    /// <summary>
    /// Called once a non-trigger and a trigger 3D colliders start touching.
    /// </summary>
    /// <param name="f">The frame in which the collision happened.</param>
    /// <param name="info">The <see cref="TriggerInfo3D"/> with data about the trigger collision.</param>
    /// \ingroup Physics3dApi
    void OnTriggerEnter3D(Frame f, TriggerInfo3D info);
  }

  /// <summary>
  /// Interface for receiving callbacks once a non-trigger and a trigger 3D colliders stop touching.
  /// <remarks>No collision is checked between two kinematic colliders that are both trigger or both non-trigger.</remarks>
  /// <remarks>At least one of the entities involved in a collision must have the respective <see cref="CallbackFlags"/> set for the callback to be called.</remarks>
  /// <remarks>See <see cref="PhysicsEngine3D.SetCallbacks"/> for setting the callbacks flags to an entity.</remarks>
  /// </summary>
  /// \ingroup Physics3dApi
  public interface ISignalOnTriggerExit3D : ISignal {
    /// <summary>
    /// Called once a non-trigger and a trigger 3D colliders stop touching.
    /// </summary>
    /// <param name="f">The frame in which the entities stopped touching.</param>
    /// <param name="info">The <see cref="ExitInfo3D"/> with the entities that were touching.</param>
    /// \ingroup Physics3dApi
    void OnTriggerExit3D(Frame f, ExitInfo3D info);
  }
}

// Core/Frame.cs

namespace Quantum {
  /// <summary>
  /// The user implementation of <see cref="FrameBase"/> that resides in the project quantum_state and has access to all user relevant classes.
  /// </summary>
  /// \ingroup FrameClass
  public unsafe partial class Frame : Core.FrameBase {

    public const int DumpFlag_NoSimulationConfig           = 1 << 1;
    public const int DumpFlag_NoRuntimeConfig              = 1 << 3;
    public const int DumpFlag_NoDeterministicSessionConfig = 1 << 4;
    public const int DumpFlag_NoRuntimePlayers             = 1 << 5;
    public const int DumpFlag_NoDynamicDB                  = 1 << 6;
    public const int DumpFlag_ReadableDynamicDB            = 1 << 7;
    public const int DumpFlag_PrintRawValues               = 1 << 8;
    public const int DumpFlag_ComponentChecksums           = 1 << 9;
    public const int DumpFlag_AssetDBCheckums              = 1 << 10;
    public const int DumpFlag_NoIsVerified                 = 1 << 11;

    [Obsolete("Use DumpFlag_ComponentChecksums")]
    public const int DumpFlag_PrintComponentChecksums     = DumpFlag_ComponentChecksums;
    [Obsolete("Use DumpFlag_ReadableDynamicDB")]
    public const int DumpFlag_PrintReadableDynamicDB      = DumpFlag_ReadableDynamicDB;

    struct RuntimePlayerData {
      public Int32         ActorId;
      public Byte[]        Data;
      public RuntimePlayer Player;
    }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    _globals_* _globals;

    // configs
    RuntimeConfig              _runtimeConfig;
    SimulationConfig           _simulationConfig;
    DeterministicSessionConfig _sessionConfig;

    // systems
    SystemBase[]            _systemsAll;
    SystemBase[]            _systemsRoots;
    Dictionary<Type, Int32> _systemIndexByType;

    // player data
    PersistentMap<Int32, RuntimePlayerData> _playerData;

    ISignalOnPlayerDataSet[] _ISignalOnPlayerDataSet;

    // 2D Physics collision signals
    ISignalOnCollision2D[]      _ISignalOnCollision2DSystems;
    ISignalOnCollisionEnter2D[] _ISignalOnCollisionEnter2DSystems;
    ISignalOnCollisionExit2D[]  _ISignalOnCollisionExit2DSystems;

    // 2D Physics trigger signals
    ISignalOnTrigger2D[]      _ISignalOnTrigger2DSystems;
    ISignalOnTriggerEnter2D[] _ISignalOnTriggerEnter2DSystems;
    ISignalOnTriggerExit2D[]  _ISignalOnTriggerExit2DSystems;

    // 3D Physics collision signals
    ISignalOnCollision3D[]      _ISignalOnCollision3DSystems;
    ISignalOnCollisionEnter3D[] _ISignalOnCollisionEnter3DSystems;
    ISignalOnCollisionExit3D[]  _ISignalOnCollisionExit3DSystems;

    // 3D Physics trigger signals
    ISignalOnTrigger3D[]      _ISignalOnTrigger3DSystems;
    ISignalOnTriggerEnter3D[] _ISignalOnTriggerEnter3DSystems;
    ISignalOnTriggerExit3D[]  _ISignalOnTriggerExit3DSystems;

    ISignalOnNavMeshWaypointReached[] _ISignalOnNavMeshWaypointReachedSystems;
    ISignalOnNavMeshSearchFailed[]    _ISignalOnNavMeshSearchFailedSystems;
    ISignalOnNavMeshMoveAgent[]       _ISignalOnNavMeshMoveAgentSystems;

    ISignalOnMapChanged[]                   _ISignalOnMapChangedSystems;
    ISignalOnEntityPrototypeMaterialized[]  _ISignalOnEntityPrototypeMaterializedSystems;

    ISignalOnPlayerConnected[]    _ISignalOnPlayerConnectedSystems;
    ISignalOnPlayerDisconnected[] _ISignalOnPlayerDisconnectedSystems;

    /// <summary>
    /// Access the global struct with generated values from the DSL.
    /// </summary>
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public _globals_* Global { get { return _globals; } }

    /// <summary>
    /// The randomization session started with the seed from the <see cref="RuntimeConfig"/> used to start the simulation with.
    /// </summary>
    /// <para>Supports determinism under roll-backs.</para>
    /// <para>If random is used in conjunction with the prediction area feature the session needs to be stored on the entities themselves.</para>
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public RNGSession* RNG { get { return &_globals->RngSession; } }

    /// <summary>
    /// Defines the amount of player in this Quantum session.
    /// </summary>
    /// The value is takes from the Deterministic session config.
    public Int32 PlayerCount { get { return _sessionConfig.PlayerCount; } }

    public override NavMeshRegionMask* NavMeshRegionMask => &_globals->NavMeshRegions;

    public override FrameMetaData* FrameMetaData => &_globals->FrameMetaData;

    public override CommitCommandsModes CommitCommandsMode => SimulationConfig.Entities.CommitCommandsMode;

    /// <summary>
    /// Access the signal API.\n
    /// Signals are function signatures used as a decoupled inter-system communication API (a bit like a publisher/subscriber API or observer pattern).
    /// </summary>
    /// Custom signals are defined in the DSL.
    public FrameSignals Signals;

    /// <summary>
    /// Access the event API.\n
    /// Events are a fine-grained solution to communicate things that happen inside the simulation to the rendering engine (they should never be used to modify/update part of the game state).
    /// </summary>
    /// Custom events are defined in the DSL.
    public FrameEvents Events;

    /// <summary>
    /// Access to the assets API
    /// </summary>
    public FrameAssets Assets;

    /// <summary>
    /// The frame user context
    /// </summary>
    public new FrameContextUser Context {
      get { return (FrameContextUser)base.Context; }
    }

    /// <summary>
    /// The <see cref="RuntimeConfig"/> used for this session.
    /// </summary>
    public RuntimeConfig RuntimeConfig { get { return _runtimeConfig; } internal set { _runtimeConfig = value; } }

    /// <summary>
    /// The <see cref="SimulationConfig"/> used for this session.
    /// </summary>
    public SimulationConfig SimulationConfig { get { return _simulationConfig; } internal set { _simulationConfig = value; } }

    /// <summary>
    /// The <see cref="DeterministicSessionConfig"/> used for this session.
    /// </summary>
    public DeterministicSessionConfig SessionConfig { get { return _sessionConfig; } internal set { _sessionConfig = value; } }

    /// <summary>
    /// All systems running in the session.
    /// </summary>
    public SystemBase[] SystemsAll { get { return _systemsAll; } }

    /// <summary>
    /// See <see cref="SimulationRate"/>. This getter acquires the value from the <see cref="SessionConfig"/> though.
    /// </summary>
    public override int UpdateRate { get { return _sessionConfig.UpdateFPS; } }

    /// <summary>
    /// Globally access the physics settings which are taken from the <see cref="SimulationConfig"/> during the Frame constructor.
    /// </summary>
    public sealed override PhysicsSceneSettings* PhysicsSceneSettings { get { return &_globals->PhysicsSettings; } }

    /// <summary>
    /// Delta time in seconds. Can be set during run-time.
    /// </summary>
    public override FP DeltaTime {
      get { return _globals->DeltaTime; }
      set { _globals->DeltaTime = value; }
    }

    /// <summary>
    /// Retrieves the Quantum map asset. Can be set during run-time.
    /// </summary>
    /// If assigned value is different than the current one, signal <see cref="ISignalOnMapChanged"/> is raised.
    public sealed override Map Map {
      get { return FindAsset<Map>(_globals->Map.Id); }
      set {
        AssetRefMap newValue      = value;
        var         previousValue = _globals->Map;
        if (previousValue.Id != newValue.Id) {
          _globals->Map = newValue;
          Signals.OnMapChanged(previousValue);
        }
      }
    }

    public Frame(FrameContext context, SystemBase[] systemsAll, SystemBase[] systemsRoots, DeterministicSessionConfig sessionConfig, RuntimeConfig runtimeConfig, SimulationConfig simulationConfig, FP deltaTime) : base(context) {
      Assert.Check(context != null);

      _systemsAll   = systemsAll;
      _systemsRoots = systemsRoots;

      _runtimeConfig    = runtimeConfig;
      _simulationConfig = simulationConfig;
      _sessionConfig    = sessionConfig;

      _playerData = new PersistentMap<Int32, RuntimePlayerData>();

      AllocGen();
      InitStatic();
      InitGen();

      Assets     = new FrameAssets(this);
      Events     = new FrameEvents(this);
      Signals    = new FrameSignals(this);
      Unsafe     = new FrameBaseUnsafe(this);
      
      Physics2D  = new Physics2D.PhysicsEngine2D.Api(this, context.TaskContext.ThreadCount);
      Physics3D  = new Physics3D.PhysicsEngine3D.Api(this, context.TaskContext.ThreadCount);

      // player data set signal
      _ISignalOnPlayerDataSet = BuildSignalsArray<ISignalOnPlayerDataSet>();

      // 2D Physics collision signals
      _ISignalOnCollision2DSystems      = BuildSignalsArray<ISignalOnCollision2D>();
      _ISignalOnCollisionEnter2DSystems = BuildSignalsArray<ISignalOnCollisionEnter2D>();
      _ISignalOnCollisionExit2DSystems  = BuildSignalsArray<ISignalOnCollisionExit2D>();

      // 2D Physics trigger signals
      _ISignalOnTrigger2DSystems      = BuildSignalsArray<ISignalOnTrigger2D>();
      _ISignalOnTriggerEnter2DSystems = BuildSignalsArray<ISignalOnTriggerEnter2D>();
      _ISignalOnTriggerExit2DSystems  = BuildSignalsArray<ISignalOnTriggerExit2D>();

      // 3D Physics collision signals
      _ISignalOnCollision3DSystems      = BuildSignalsArray<ISignalOnCollision3D>();
      _ISignalOnCollisionEnter3DSystems = BuildSignalsArray<ISignalOnCollisionEnter3D>();
      _ISignalOnCollisionExit3DSystems  = BuildSignalsArray<ISignalOnCollisionExit3D>();

      // 3D Physics trigger signals
      _ISignalOnTrigger3DSystems      = BuildSignalsArray<ISignalOnTrigger3D>();
      _ISignalOnTriggerEnter3DSystems = BuildSignalsArray<ISignalOnTriggerEnter3D>();
      _ISignalOnTriggerExit3DSystems  = BuildSignalsArray<ISignalOnTriggerExit3D>();

      _ISignalOnNavMeshWaypointReachedSystems = BuildSignalsArray<ISignalOnNavMeshWaypointReached>();
      _ISignalOnNavMeshSearchFailedSystems    = BuildSignalsArray<ISignalOnNavMeshSearchFailed>();
      _ISignalOnNavMeshMoveAgentSystems       = BuildSignalsArray<ISignalOnNavMeshMoveAgent>();

      // map changed signal
      _ISignalOnMapChangedSystems = BuildSignalsArray<ISignalOnMapChanged>();

      // prototype materialized signal
      _ISignalOnEntityPrototypeMaterializedSystems = BuildSignalsArray<ISignalOnEntityPrototypeMaterialized>();
      if ( _ISignalOnEntityPrototypeMaterializedSystems.Length > 0 ) {
        base._SignalOnEntityPrototypeMaterialized = (entity, prototype) => Signals.OnEntityPrototypeMaterialized(entity, prototype);
      }

      _ISignalOnPlayerConnectedSystems = BuildSignalsArray<ISignalOnPlayerConnected>();
      _ISignalOnPlayerDisconnectedSystems = BuildSignalsArray<ISignalOnPlayerDisconnected>();

      // assign map, rng session, etc.
      _globals->Map        = FindAsset<Map>(runtimeConfig.Map.Id);
      _globals->RngSession = new RNGSession(runtimeConfig.Seed);
      _globals->DeltaTime  = deltaTime;

      _systemIndexByType = new Dictionary<Type, Int32>(_systemsAll.Length);

      for (Int32 i = 0; i < _systemsAll.Length; ++i) {
        var systemType = _systemsAll[i].GetType();

        if (_systemIndexByType.ContainsKey(systemType) == false) {
          _systemIndexByType.Add(systemType, i);
        }

        // set default enabled systems
        if (_systemsAll[i].StartEnabled) {
          _globals->Systems.Set(_systemsAll[i].RuntimeIndex);
        }
      }

      // init physics settings
      Quantum.PhysicsSceneSettings.Init(&_globals->PhysicsSettings, simulationConfig.Physics);

      // Init navmesh regions to all bit fields to be set
      ClearAllNavMeshRegions();

      // user callbacks
      AllocUser();
      InitUser();
    }

    /// <summary>
    /// Set the prediction area.
    /// </summary>
    /// <param name="position">Center of the prediction area</param>
    /// <param name="radius">Radius of the prediction area</param>
    /// <para>The Prediction Culling feature must be explicitly enabled in <see cref="SimulationConfig.UsePredictionArea"/>.</para>
    /// <para>This can be safely called from the main-thread.</para>
    /// <para>Prediction Culling allows developers to save CPU time in games where the player has only a partial view of the game scene.
    /// Quantum prediction and rollbacks, which are time consuming, will only run for important entities that are visible to the local player(s). Leaving anything outside that area to be simulated only once per tick with no rollbacks as soon as the inputs are confirmed from server.
    /// It is safe and simple to activate and, depending on the game, the performance difference can be quite large.Imagine a 30Hz game to constantly rollback ten ticks for every confirmed input (with more players, the predictor eventually misses at least for one of them). This requires the game simulation to be lightweight to be able to run at almost 300Hz(because of the rollbacks). With Prediction Culling enabled the full frames will be simulated at the expected 30Hz all the time while the much smaller prediction area is the only one running within the prediction buffer.</para>
    public void SetPredictionArea(FPVector3 position, FP radius) {
      Context.SetPredictionArea(position, radius);
    }

    /// <summary>
    /// See <see cref="SetPredictionArea(FPVector3, FP)"/>.
    /// </summary>
    /// <param name="position"></param>
    /// <param name="radius"></param>
    public void SetPredictionArea(FPVector2 position, FP radius) {
      Context.SetPredictionArea(position.XOY, radius);
    }

    /// <summary>
    /// Test is a position is inside the prediction area.
    /// </summary>
    /// <param name="position">Position</param>
    /// <returns>True if the position is inside the prediction area.</returns>
    public Boolean InPredictionArea(FPVector3 position) {
      return Context.InPredictionArea(this, position);
    }

    /// <summary>
    /// See <see cref="InPredictionArea(FPVector3)"/>.
    /// </summary>
    /// <param name="position"></param>
    /// <returns></returns>
    public Boolean InPredictionArea(FPVector2 position) {
      return Context.InPredictionArea(this, position);
    }


    /// <summary>
    /// Serializes the frame using a temporary buffer (20MB).
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    public override Byte[] Serialize(DeterministicFrameSerializeMode mode) {
      return Serialize(mode, new byte[1024 * 1024 * 20], allocOutput: true).Array;
    }

    /// <summary>
    /// Serializes the frame using <paramref name="buffer"/> as a buffer for temporary data. 
    /// 
    /// If <paramref name="allocOutput"/> is set to false, then <paramref name="buffer"/> is also used for the final data - use offset and count from the result to access
    /// the part of <paramref name="buffer"/> where serialized frame is stored.
    /// 
    /// If <paramref name="allocOutput"/> is set to true then a new array is allocated for the result.
    /// 
    /// Despite accepting a buffer, this method still allocates a few small temporary objects. 
    /// <see cref="IAssetSerializer.SerializeAssets(System.Collections.Generic.IEnumerable{AssetObject})"/> is also going
    /// to allocate when serializing DynamicAssetDB, but how much depends on the serializer itself and the number of dynamic assets.
    /// </summary>
    /// <param name="mode"></param>
    /// <param name="buffer"></param>
    /// <param name="offset"></param>
    /// <param name="allocOutput"></param>
    /// <returns>Segment of <paramref name="buffer"/> where the serialized frame is stored</returns>
    /// <remarks>Do not serialize during GameStart callback because systems have not been initialized, yet. Rather use CallbackSimulateFinished to wait for the first update.</remarks>
    public ArraySegment<byte> Serialize(DeterministicFrameSerializeMode mode, byte[] buffer, int offset = 0, bool allocOutput = false) {
      offset = ByteUtils.AddValueBlock((int)mode, buffer, offset);
      offset = ByteUtils.AddValueBlock(Number, buffer, offset);
      offset = ByteUtils.AddValueBlock(CalculateChecksum(false), buffer, offset);

      BitStream stream;

      {
        offset = ByteUtils.BeginByteBlockHeader(buffer, offset, out var blockOffset);

        stream = new BitStream(buffer, buffer.Length - offset, offset) {
          Writing = true
        };

        SerializeRuntimePlayers(stream);

        offset = ByteUtils.EndByteBlockHeader(buffer, blockOffset, stream.BytesRequired);
      }

      {
        offset = ByteUtils.BeginByteBlockHeader(buffer, offset, out var blockOffset);

        stream.SetBuffer(buffer, buffer.Length - offset, offset);
        var serializer = new FrameSerializer(mode, this, stream) {
          Writing = true
        };

        SerializeState(serializer);

        offset = ByteUtils.EndByteBlockHeader(buffer, blockOffset, stream.BytesRequired);
      }

      _dynamicAssetDB.Serialize(Context.AssetSerializer, out var assetDBHeader, out var assetDBData);

      {
        // write the header for the byte block but don't actually copy into the buffer
        // - we'll do that later during the compression stage
        offset = ByteUtils.BeginByteBlockHeader(buffer, offset, out var blockOffset);
        ByteUtils.EndByteBlockHeader(buffer, blockOffset, assetDBHeader.Length + assetDBData.Length);
      }

      using (var outputStream = allocOutput ? new MemoryStream() : new MemoryStream(buffer, offset, buffer.Length - offset)) {
        using (var compressedOutput = ByteUtils.CreateGZipCompressStream(outputStream)) {
          compressedOutput.Write(buffer, 0, offset);
          compressedOutput.Write(assetDBHeader, 0, assetDBHeader.Length);
          compressedOutput.Write(assetDBData, 0, assetDBData.Length);
        }

        if (allocOutput) {
          return new ArraySegment<byte>(outputStream.ToArray());
        } else {
          return new ArraySegment<byte>(buffer, offset, (int)outputStream.Position);
        }
        
      }
    }

    public override void Deserialize(Byte[] data) {
      var blocks = ByteUtils.ReadByteBlocks(ByteUtils.GZipDecompressBytes(data)).ToArray();
      
      var mode   = (DeterministicFrameSerializeMode)BitConverter.ToInt32(blocks[0], 0);
      
      Number = BitConverter.ToInt32(blocks[1], 0);
      
      var checksum = BitConverter.ToUInt64(blocks[2], 0);

      DeserializeRuntimePlayers(blocks[3]);
      DeserializeDynamicAssetDB(blocks[5]);

      FrameSerializer serializer;
      serializer         = new FrameSerializer(mode, this, blocks[4]);
      serializer.Reading = true;

      SerializeState(serializer);

      serializer.VerifyNoUnresolvedPointers();

      if (CalculateChecksum(false) != checksum) {
        throw new Exception($"Checksum of deserialized frame does not match checksum in the source data");
      }
    }

    void SerializeState(FrameSerializer serializer) {
      FrameBase.Serialize(this, serializer);
      _globals_.Serialize(_globals, serializer);
      SerializeEntitiesGen(serializer);
      SerializeUser(serializer);
    }

    Byte[] SerializeRuntimePlayers() {
      BitStream stream;
      stream         = new BitStream(1024 * 10);
      stream.Writing = true;
      SerializeRuntimePlayers(stream);
      return stream.ToArray();
    }

    void SerializeRuntimePlayers(BitStream stream) {
      stream.WriteInt(_playerData.Count);

      foreach (var player in _playerData.Iterator()) {
        stream.WriteInt(player.Key);
        stream.WriteInt(player.Value.ActorId);
        stream.WriteByteArrayLengthPrefixed(player.Value.Data);
        player.Value.Player.Serialize(stream);
      }
    }

    void DeserializeRuntimePlayers(Byte[] bytes) {
      BitStream stream;
      stream         = new BitStream(bytes);
      stream.Reading = true;

      var count = stream.ReadInt();
      _playerData = new PersistentMap<int, RuntimePlayerData>();
      for (Int32 i = 0; i < count; ++i) {
        var player = stream.ReadInt();

        RuntimePlayerData data;
        data.ActorId = stream.ReadInt();
        data.Data    = stream.ReadByteArrayLengthPrefixed();
        data.Player  = new RuntimePlayer();
        data.Player.Serialize(stream);

        _playerData = _playerData.Add(player, data);
      }
    }

    void DeserializeDynamicAssetDB(Byte[] bytes) {
      var assets = _dynamicAssetDB.Deserialize(bytes, Context.AssetSerializer);

      foreach (var asset in assets) {
        asset.Loaded(_dynamicAssetDB, Context.Allocator);
      }
    }

    /// <summary>
    /// Dump the frame in human readable form into a string.
    /// </summary>
    /// <returns>Frame representation</returns>
    public sealed override String DumpFrame(int dumpFlags = 0) {
      var printer = new FramePrinter();
      printer.Reset(this);

      printer.IsRawPrintEnabled = ((dumpFlags & DumpFlag_PrintRawValues) == DumpFlag_PrintRawValues);

      // frame info
      if ((dumpFlags & DumpFlag_NoIsVerified) == DumpFlag_NoIsVerified) {
        printer.AddLine($"#### FRAME DUMP FOR {Number} ####");
      } else {
        printer.AddLine($"#### FRAME DUMP FOR {Number} IsVerified={IsVerified} ####");
      }
      
      if ((dumpFlags & DumpFlag_NoSimulationConfig) != DumpFlag_NoSimulationConfig) {
        printer.AddLine();
        printer.AddObject("# " + nameof(SimulationConfig), SimulationConfig);
      }

      if ((dumpFlags & DumpFlag_NoRuntimeConfig) != DumpFlag_NoRuntimeConfig) {
        printer.AddLine();
        printer.AddObject("# " + nameof(RuntimeConfig), RuntimeConfig);
      }

      if ((dumpFlags & DumpFlag_NoDeterministicSessionConfig) != DumpFlag_NoDeterministicSessionConfig) {
        printer.AddLine();
        printer.AddObject("# " + nameof(SessionConfig), SessionConfig);
      }

      if ((dumpFlags & DumpFlag_NoRuntimePlayers) != DumpFlag_NoRuntimePlayers) {
        printer.AddLine();
        printer.AddLine("# PLAYERS");
        {
          printer.ScopeBegin();
          foreach (var kv in _playerData.Iterator()) {
            printer.AddObject($"[{kv.Key}]", kv.Value);
          }
          printer.ScopeEnd();
        }
      }

      // globals state
      printer.AddLine();
      printer.AddPointer("# GLOBALS", _globals);

      // print entities
      printer.AddLine();
      printer.AddLine("# ENTITIES");
      Print(this, printer, (dumpFlags & DumpFlag_ComponentChecksums) == DumpFlag_ComponentChecksums);

      if ((dumpFlags & DumpFlag_AssetDBCheckums) == DumpFlag_AssetDBCheckums) {
        printer.AddLine();
        printer.AddLine("# ASSETDB CHECKSUMS");
        {
          printer.ScopeBegin();
          AssetObject[] assets = new AssetObject[1];

          var orderedAssets = this.Context.AssetDB.FindAllAssets(true).OrderBy(x => x.Guid);

          foreach (var asset in orderedAssets) {
            assets[0] = asset;
            var bytes = this.Context.AssetSerializer.SerializeAssets(assets);
            fixed (byte* p = bytes) {
              var hash = CRC64.Calculate(0, p, bytes.Length);
              printer.AddLine($"{asset.Identifier}: {hash}");
            }
          }
          printer.ScopeEnd();
        }
      }

      if ((dumpFlags & DumpFlag_NoDynamicDB) != DumpFlag_NoDynamicDB) {
        printer.AddLine();
        printer.AddLine("# DYNAMICDB");
        {
          printer.ScopeBegin();

          var assetSerializer = Context.AssetSerializer;
          if ((dumpFlags & DumpFlag_ReadableDynamicDB) == DumpFlag_ReadableDynamicDB) {
            printer.AddLine($"NextGuid: {_dynamicAssetDB.NextGuid}");
            foreach (var asset in _dynamicAssetDB.Assets) {
              printer.AddLine($"{asset.GetType().FullName}:");
              printer.ScopeBegin();
              printer.AddLine($"{assetSerializer.PrintAsset(asset)}");
              printer.ScopeEnd();
            }
          } else {
            printer.AddLine("Dump: ");
            printer.ScopeBegin();
            var data = _dynamicAssetDB.Serialize(assetSerializer);
            fixed (byte* p = data) {
              UnmanagedUtils.PrintBytesHex(p, data.Length, 32, printer);
            }
            printer.ScopeEnd();
          }

          printer.ScopeEnd();
        }
      }

      // physics states
      if (Physics2D != null) {
        printer.AddLine();
        Physics2D.Print(printer);
      }

      if (Physics3D != null) {
        printer.AddLine();
        Physics3D.Print(printer);
      }

      // heap state
      if ((dumpFlags & DumpFlag_NoHeap) != DumpFlag_NoHeap) {
        printer.AddLine();
        printer.AddLine("# HEAP");
        Allocator.Heap.Print(_heap, printer);
      }

      // dump user data
      var dump = printer.ToString();
      DumpFrameUser(ref dump);

      return dump;
    }


    /// <summary>
    /// Calculates a checksum for the current game state. If the game is not started with <see cref="QuantumGameFlags.DisableSharedChecksumSerializer"/> 
    /// flag, this method is not thread-safe, i.e. calling it from multiple threads for frames from the same simulation is going to break.
    /// </summary>
    public sealed override UInt64 CalculateChecksum() {
      return CalculateChecksum(Context.UseSharedChecksumSerializer);
    }

    /// <summary>
    /// Calculates a checksum for the current game state.
    /// </summary>
    /// <param name="useSharedSerializer">True - use shared checksum serializer to avoid allocs (not thread-safe).</param>
    /// <returns></returns>
    public UInt64 CalculateChecksum(bool useSharedSerializer) {
      FrameSerializer frameSerializer;
      if (useSharedSerializer) {
        frameSerializer = Context.SharedChecksumSerializer;
        Assert.Check(frameSerializer != null); 
      } else {
        frameSerializer = new FrameSerializer(DeterministicFrameSerializeMode.Serialize, this, new FrameChecksumerBitStream());
      }
      return CalculateChecksumInternal(frameSerializer);
    }

    internal UInt64 CalculateChecksumInternal(FrameSerializer serializer) {

      if (serializer == null) {
        throw new ArgumentNullException(nameof(serializer));
      }

      if (serializer.Mode != DeterministicFrameSerializeMode.Serialize) {
        throw new ArgumentException($"Serializer needs to be in {nameof(DeterministicFrameSerializeMode.Serialize)} mode", nameof(serializer));
      }
      
      if (serializer.Stream is FrameChecksumerBitStream checksumStream) {

        Profiling.HostProfiler.Start("CalculateChecksumInternal");

        try {
          serializer.Reset();
          serializer.Writing = true;
          serializer.Frame = this;

          checksumStream.Checksum = (ulong)Number;

          // checksum globals
          _globals_.Serialize(_globals, serializer);

          // checksum entity registry
          FrameBase.Serialize(this, serializer);

          // checksum heap
          return checksumStream.Checksum;
        } finally {
          serializer.Frame = null;
          Profiling.HostProfiler.End();
        }
      } else {
        throw new InvalidOperationException($"Serializer's stream needs to be of {nameof(FrameChecksumerBitStream)} type (is: {serializer.Stream?.GetType().FullName}");
      }
    }

    /// <summary>
    /// Copies the complete frame memory.
    /// </summary>
    /// <param name="frame">Input frame object</param>
    protected sealed override void Copy(DeterministicFrame frame) {
      var f = (Frame)frame;

      // copy player data
      _playerData = f._playerData;

      // copy heap from frame
      Allocator.Heap.Copy(Context.Allocator, _heap, f._heap);

      // copy entity registry
      FrameBase.Copy(this, f);

      // dynamic DB
      _dynamicAssetDB.CopyFrom(f._dynamicAssetDB);

      // perform native copy
      CopyFromGen(f);
      CopyFromUser(f);
    }

    public sealed override void Free() {
      FreeUser();
      FreeGen();
      base.Free();
    }

    [Obsolete("Use SystemIsEnabledSelf instead.")]
    public Boolean SystemIsEnabled<T>() where T : SystemBase {
      return SystemIsEnabledSelf<T>();
    }
    
    [Obsolete("Use SystemIsEnabledSelf instead.")]
    public Boolean SystemIsEnabled(Type t) {
      return SystemIsEnabledSelf(t);
    }

    /// <summary>
    /// Test if a system is enabled.
    /// </summary>
    /// <typeparam name="T">System type</typeparam>
    /// <returns>True if the system is enabled</returns>
    /// Logs an error if the system type is not found.
    public Boolean SystemIsEnabledSelf<T>() where T : SystemBase {
      var system = FindSystem<T>();
      if (system.Item0 == null) {
        return false;
      }

      return _globals->Systems.IsSet(system.Item1);
    }

    public Boolean SystemIsEnabledSelf(Type t) {
      var system = FindSystem(t);
      if (system.Item0 == null) {
        return false;
      }

      return _globals->Systems.IsSet(system.Item1);
    }

    public Boolean SystemIsEnabledSelf(SystemBase s) {
      if (s == null) {
        return false;
      }

      return _globals->Systems.IsSet(s.RuntimeIndex);
    }

    public Boolean SystemIsEnabledInHierarchy<T>() where T : SystemBase {
      var system = FindSystem<T>();
      return SystemIsEnabledInHierarchy(system.Item0);
    }

    public Boolean SystemIsEnabledInHierarchy(Type t) {
      var system = FindSystem(t);
      return SystemIsEnabledInHierarchy(system.Item0);
    }

    public Boolean SystemIsEnabledInHierarchy(SystemBase system) {
      if (system == null)
        return false;

      if (_globals->Systems.IsSet(system.RuntimeIndex) == false)
        return false;
      if (system.ParentSystem == null)
        return true;

      return SystemIsEnabledInHierarchy(system.ParentSystem);
    }

    /// <summary>
    /// Enable a system.
    /// </summary>
    /// <typeparam name="T">System type</typeparam>
    /// Logs an error if the system type is not found.
    public void SystemEnable<T>() where T : SystemBase {
      SystemEnable(typeof(T));
    }
    
    public void SystemEnable(Type t)  {
      var system = FindSystem(t);
      if (system.Item0 == null) {
        return;
      }

      if (_globals->Systems.IsSet(system.Item1) == false) {
        // set flag
        _globals->Systems.Set(system.Item1);

        // Fire callback only if it becomes enabled in hierarchy
        if (system.Item0.ParentSystem == null || SystemIsEnabledInHierarchy(system.Item0.ParentSystem)) {
          try {
            system.Item0.OnEnabled(this);
          } catch (Exception exn) {
            Log.Exception(exn);
          }
        }
      }
    }

    /// <summary>
    /// Disables a system.
    /// </summary>
    /// <typeparam name="T">System type</typeparam>
    /// Logs an error if the system type is not found.
    /// <example><code>
    /// // test for a certain asset and disable the system during its OnInit method
    /// public override void OnInit(Frame f) {
    ///   var testSettings = f.FindAsset<NavMeshAgentsSettings>(f.Map.UserAsset.Id);
    ///   if (testSettings == null) {
    ///     f.SystemDisable<NavMeshAgentTestSystem>();
    ///     return;
    ///    }
    ///    //..
    ///  }
    /// </code></example>
    public void SystemDisable<T>() where T : SystemBase {
      SystemDisable(typeof(T));
    }

    public void SystemDisable<T>(T system) where T : SystemBase {
      SystemDisable(system.GetType());
    }

    public void SystemDisable(Type t) {
      var system = FindSystem(t);
      if (system.Item0 == null) {
        return;
      }

      if (_globals->Systems.IsSet(system.Item1)) {
        // clear flag
        _globals->Systems.Clear(system.Item1);

        // Fire callback only if it was previously enabled in hierarchy
        if (system.Item0.ParentSystem == null || SystemIsEnabledInHierarchy(system.Item0.ParentSystem)) {
          try {
            system.Item0.OnDisabled(this);
          } catch (Exception exn) {
            Log.Exception(exn);
          }
        }
      }
    }
    
    QTuple<SystemBase, Int32> FindSystem<T>() {
      return FindSystem(typeof(T));
    }
    
    QTuple<SystemBase, Int32> FindSystem(Type t) {
      if (_systemIndexByType.TryGetValue(t, out var i)) {
        return QTuple.Create(_systemsAll[i], i);
      }

      Log.Error("System '{0}' not found, did you forget to add it to SystemSetup.CreateSystems ?", t.Name);
      return new QTuple<SystemBase, Int32>(null, -1);
    }


    T[] BuildSignalsArray<T>() {
      return _systemsAll.Where(x => x is T).Cast<T>().ToArray();
    }

    void BuildSignalsArrayOnComponentAdded<T>() where T : unmanaged, IComponent {
      Assert.Check(ComponentTypeId<T>.Id > 0);

      var array = _systemsAll.Where(x => x is ISignalOnComponentAdded<T>).Cast<ISignalOnComponentAdded<T>>().ToArray();
      if (array.Length > 0) {
        _ComponentSignalsOnAdded[ComponentTypeId<T>.Id] = (entity, componentData) => {
          var component = (T*)componentData;
          var systems   = &(_globals->Systems);
          for (Int32 i = 0; i < array.Length; ++i) {
            if (SystemIsEnabledInHierarchy((SystemBase)array[i])) {
              array[i].OnAdded(this, entity, component);
            }
          }
        };
      } else {
        _ComponentSignalsOnAdded[ComponentTypeId<T>.Id] = null;
      }
    }

    void BuildSignalsArrayOnComponentRemoved<T>() where T : unmanaged, IComponent {
      Assert.Check(ComponentTypeId<T>.Id > 0);

      var array = _systemsAll.Where(x => x is ISignalOnComponentRemoved<T>).Cast<ISignalOnComponentRemoved<T>>().ToArray();
      if (array.Length > 0) {
        _ComponentSignalsOnRemoved[ComponentTypeId<T>.Id] = (entity, componentData) => {
          var component = (T*)componentData;
          var systems   = &(_globals->Systems);
          for (Int32 i = 0; i < array.Length; ++i) {
            if (SystemIsEnabledInHierarchy((SystemBase)array[i])) {
              array[i].OnRemoved(this, entity, component);
            }
          }
        };
      } else {
        _ComponentSignalsOnRemoved[ComponentTypeId<T>.Id] = null;
      }
    }

    void AddEvent(EventBase evnt) {
      // set evnt.Tick
      evnt.Tick = Number;

      // add ast last
      Context.Events.AddLast(evnt);
    }

    public static void InitStatic() {
      StaticDelegates.Init();
      InitStaticGen();
    }

    // partial declarations populated from code generator
    static partial void InitStaticGen();
    partial void InitGen();
    partial void FreeGen();
    partial void AllocGen();
    partial void CopyFromGen(Frame                    frame);
    partial void SerializeEntitiesGen(FrameSerializer serializer);

    partial void InitUser();
    partial void FreeUser();
    partial void AllocUser();
    partial void CopyFromUser(Frame frame);

    partial void SerializeUser(FrameSerializer serializer);
    partial void DumpFrameUser(ref String      dump);


    /// <summary>
    /// Gets the runtime player configuration data for a certain player.
    /// </summary>
    /// <param name="player">Player ref</param>
    /// <returns>Player config or null if player was not found</returns>
    public RuntimePlayer GetPlayerData(PlayerRef player) {
      RuntimePlayerData data;

      if (_playerData.TryFind(player, out data)) {
        return data.Player;
      }

      return null;
    }

    /// <summary>
    /// Converts a Quantum PlayerRef to an ActorId (Photon client id).
    /// </summary>
    /// <param name="player">Player reference</param>
    /// <returns>ActorId or null if payer was not found</returns>
    public Int32? PlayerToActorId(PlayerRef player) {
      RuntimePlayerData data;

      if (_playerData.TryFind(player, out data)) {
        return data.ActorId;
      }

      return null;
    }

    /// <summary>
    /// Returns the first player that is using a certain ActorId (Photon client id).
    /// </summary>
    /// <param name="actorId">Actor id</param>
    /// <returns>Player reference or null if actor id was not found</returns>
    /// The first player because multiple players from the same Photon client can join.
    public PlayerRef? ActorIdToFirstPlayer(Int32 actorId) {
      foreach (var kvp in _playerData.Iterator()) {
        if (kvp.Value.ActorId == actorId) {
          return kvp.Key;
        }
      }

      return null;
    }

    /// <summary>
    /// Returns all players with a certain ActorId (Photon client id).
    /// </summary>
    /// <param name="actorId">Actor id</param>
    /// <returns>Array of player references</returns>
    public PlayerRef[] ActorIdToAllPlayers(Int32 actorId) {
      return _playerData.Iterator().Where(x => x.Value.ActorId == actorId).Select(x => (PlayerRef)x.Key).ToArray();
    }

    public void UpdatePlayerData() {
      UInt64 set = 0;

      for (Int32 i = 0; i < PlayerCount; ++i) {
        var rpc = GetRawRpc(i);
        if (rpc != null && rpc.Length > 0) {
          var flags = GetPlayerInputFlags(i);
          if ((flags & DeterministicInputFlags.Command) != DeterministicInputFlags.Command) {
            var playerDataOriginal = _playerData;

            try {
              // create player data
              RuntimePlayerData data;
              data.Data    = rpc;
              data.ActorId = BitConverter.ToInt32(rpc, rpc.Length - 4);
              data.Player  = Quantum.RuntimePlayer.FromByteArray(rpc);

              // set data
              _playerData = _playerData.AddOrSet(i, data);

              // set mask
              set |= 1UL << FPMath.Clamp(i, 0, 63);
#if DEBUG
            } catch (Exception e) {
              Log.Error("## RuntimePlayer Deserialization Threw Exception ##");
              Log.Exception(e);
#else
            } catch {
#endif
              _playerData = playerDataOriginal;
            }
          }
        }
      }

      if (set != 0UL) {
        for (Int32 i = 0; i < PlayerCount; ++i) {
          var b = 1UL << i;
          if ((set & b) == b) {
            try {
              Signals.OnPlayerDataSet(i);
            } catch (Exception exn) {
              Log.Exception(exn);
            }
          }
        }
      }
    }
  }
}


// Core/FrameAssets.cs
namespace Quantum {
  partial class Frame {
    public partial struct FrameAssets {
      Frame _f;

      public FrameAssets(Frame f) {
        _f = f;
      }

      public EntityView View(string view, DatabaseType dbType = DatabaseType.Default) {
        return _f.FindAsset<EntityView>(view, dbType);
      }

      public EntityPrototype Prototype(string prototype, DatabaseType dbType = DatabaseType.Default) {
        return _f.FindAsset<EntityPrototype>(prototype, dbType);
      }
      
      public EntityView View(AssetRefEntityView view) {
        return _f.FindAsset<EntityView>(view.Id);
      }
      
      public EntityPrototype Prototype(AssetRefEntityPrototype prototype) {
        return _f.FindAsset<EntityPrototype>(prototype.Id);
      }
      
      public Map Map(AssetRefMap assetRef) {
        return _f.FindAsset<Map>(assetRef.Id);
      }

      public PhysicsMaterial PhysicsMaterial(AssetRefPhysicsMaterial assetRef) {
        return _f.FindAsset<PhysicsMaterial>(assetRef.Id);
      }

      public PolygonCollider PolygonCollider(AssetRefPolygonCollider assetRef) {
        return _f.FindAsset<PolygonCollider>(assetRef.Id);
      }

      public CharacterController3DConfig CharacterController3DConfig(AssetRefCharacterController3DConfig assetRef) {
        return _f.FindAsset<CharacterController3DConfig>(assetRef.Id);
      }

      public CharacterController2DConfig CharacterController2DConfig(AssetRefCharacterController2DConfig assetRef) {
        return _f.FindAsset<CharacterController2DConfig>(assetRef.Id);
      }

      public NavMesh NavMesh(AssetRefNavMesh assetRef) {
        return _f.FindAsset<NavMesh>(assetRef.Id);
      }

      public NavMeshAgentConfig NavMeshAgentConfig(AssetRefNavMeshAgentConfig assetRef) {
        return _f.FindAsset<NavMeshAgentConfig>(assetRef.Id);
      }

      public SimulationConfig SimulationConfig(AssetRefSimulationConfig assetRef) {
        return _f.FindAsset<SimulationConfig>(assetRef.Id);
      }

      public TerrainCollider TerrainCollider(AssetRefTerrainCollider assetRef) {
        return _f.FindAsset<TerrainCollider>(assetRef.Id);
      }
    }
  }
}

// Core/FrameContextUser.cs
namespace Quantum {
  public partial class FrameContextUser : Core.FrameContext {
    public FrameContextUser(Args args) 
      : base(args) {
      ConstructUser(args);
    }

    public override sealed void Dispose() {
      DisposeUser();
      base.Dispose();
    }

    partial void ConstructUser(Args args);
    partial void DisposeUser();
  }
}

// Core/FrameEvents.cs

namespace Quantum {
  partial class Frame {
    public partial struct FrameEvents {
      Frame _f;

      public FrameEvents(Frame f) {
        _f = f;
      }
    }
  }
}


// Core/FrameSignals.cs

namespace Quantum {
  public unsafe interface ISignalOnComponentAdded<T> : ISignal where T : unmanaged, IComponent {
    void OnAdded(Frame f, EntityRef entity, T* component);
  }

  public unsafe interface ISignalOnComponentRemoved<T> : ISignal where T : unmanaged, IComponent {
    void OnRemoved(Frame f, EntityRef entity, T* component);
  } 
  
  public unsafe interface ISignalOnMapChanged : ISignal {
    void OnMapChanged(Frame f, AssetRefMap previousMap);
  }

  public unsafe interface ISignalOnEntityPrototypeMaterialized : ISignal {
    void OnEntityPrototypeMaterialized(Frame f, EntityRef entity, EntityPrototypeRef prototypeRef);
  }

  public unsafe interface ISignalOnPlayerConnected : ISignal {
    void OnPlayerConnected(Frame f, PlayerRef player);
  }

  public unsafe interface ISignalOnPlayerDisconnected : ISignal {
    void OnPlayerDisconnected(Frame f, PlayerRef player);
  }

  partial class Frame {
    public unsafe partial struct FrameSignals {
      Frame _f;

      public FrameSignals(Frame f) {
        _f = f;
      }

      public void OnPlayerDataSet(PlayerRef player) {
        var array   = _f._ISignalOnPlayerDataSet;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnPlayerDataSet(_f, player);
          }
        }
      }

      public void OnMapChanged(AssetRefMap previousMap) {
        var array = _f._ISignalOnMapChangedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnMapChanged(_f, previousMap);
          }
        }
      }

      public void OnEntityPrototypeMaterialized(EntityRef entity, EntityPrototypeRef prototypeRef) {
        var array = _f._ISignalOnEntityPrototypeMaterializedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnEntityPrototypeMaterialized(_f, entity, prototypeRef);
          }
        }
      }

      public void OnPlayerConnected(PlayerRef player) {
        var array = _f._ISignalOnPlayerConnectedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnPlayerConnected(_f, player);
          }
        }
      }

      public void OnPlayerDisconnected(PlayerRef player) {
        var array = _f._ISignalOnPlayerDisconnectedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnPlayerDisconnected(_f, player);
          }
        }
      }

      public void OnNavMeshWaypointReached(EntityRef entity, FPVector3 waypoint, Navigation.WaypointFlag waypointFlags, ref bool resetAgent) {
        var array   = _f._ISignalOnNavMeshWaypointReachedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnNavMeshWaypointReached(_f, entity, waypoint, waypointFlags, ref resetAgent);
          }
        }
      }

      public void OnNavMeshSearchFailed(EntityRef entity, ref bool resetAgent) {
        var array   = _f._ISignalOnNavMeshSearchFailedSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnNavMeshSearchFailed(_f, entity, ref resetAgent);
          }
        }
      }

      public void OnNavMeshMoveAgent(EntityRef entity, FPVector2 desiredDirection) {
        var array = _f._ISignalOnNavMeshMoveAgentSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnNavMeshMoveAgent(_f, entity, desiredDirection);
          }
        }
      }

      public void OnCollision2D(CollisionInfo2D info) {
        var array   = _f._ISignalOnCollision2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollision2D(_f, info);
          }
        }
      }

      public void OnCollisionEnter2D(CollisionInfo2D info) {
        var array   = _f._ISignalOnCollisionEnter2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollisionEnter2D(_f, info);
          }
        }
      }

      public void OnCollisionExit2D(ExitInfo2D info) {
        var array   = _f._ISignalOnCollisionExit2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollisionExit2D(_f, info);
          }
        }
      }
      
      public void OnTrigger2D(TriggerInfo2D info) {
        var array   = _f._ISignalOnTrigger2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTrigger2D(_f, info);
          }
        }
      }

      public void OnTriggerEnter2D(TriggerInfo2D info) {
        var array   = _f._ISignalOnTriggerEnter2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTriggerEnter2D(_f, info);
          }
        }
      }

      public void OnTriggerExit2D(ExitInfo2D info) {
        var array   = _f._ISignalOnTriggerExit2DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTriggerExit2D(_f, info);
          }
        }
      }
      
      public void OnCollision3D(CollisionInfo3D info) {
        var array   = _f._ISignalOnCollision3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollision3D(_f, info);
          }
        }
      }

      public void OnCollisionEnter3D(CollisionInfo3D info) {
        var array   = _f._ISignalOnCollisionEnter3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollisionEnter3D(_f, info);
          }
        }
      }

      public void OnCollisionExit3D(ExitInfo3D info) {
        var array   = _f._ISignalOnCollisionExit3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnCollisionExit3D(_f, info);
          }
        }
      }
      
      public void OnTrigger3D(TriggerInfo3D info) {
        var array   = _f._ISignalOnTrigger3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTrigger3D(_f, info);
          }
        }
      }

      public void OnTriggerEnter3D(TriggerInfo3D info) {
        var array   = _f._ISignalOnTriggerEnter3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTriggerEnter3D(_f, info);
          }
        }
      }

      public void OnTriggerExit3D(ExitInfo3D info) {
        var array   = _f._ISignalOnTriggerExit3DSystems;
        for (Int32 i = 0; i < array.Length; ++i) {
          var s = array[i];
          if (_f.SystemIsEnabledInHierarchy((SystemBase)s)) {
            s.OnTriggerExit3D(_f, info);
          }
        }
      }
    }
  }
}


// Core/NavMeshSignals.cs

namespace Quantum
{
  /// <summary>
  /// Signal is fired when an agent reaches a waypoint.
  /// </summary>
  /// <remarks>Requires enabled <see cref="Navigation.Config.EnableNavigationCallbacks"/> in <see cref="SimulationConfig.Navigation"/>.</remarks>
  /// \ingroup NavigationApi
  public unsafe interface ISignalOnNavMeshWaypointReached : ISignal {
    /// <param name="f">Current frame object</param>
    /// <param name="entity">The entity the navmesh agent component belongs to</param>
    /// <param name="waypoint">The current waypoint position</param>
    /// <param name="waypointFlags">The current waypoint flags</param>
    /// <param name="resetAgent">If set to true the NavMeshPathfinder component will be cleared and stopped. Set to false if NavMeshPathfinder.SetTarget() was called inside the callback.</param>
    void OnNavMeshWaypointReached(Frame f, EntityRef entity, FPVector3 waypoint, Navigation.WaypointFlag waypointFlags, ref bool resetAgent);
  }

  /// <summary>
  /// Signal is fired when the agent could not find a path in the agent update after using <see cref="NavMeshSteeringAgent.SetTarget(Core.FrameBase, FPVector2, NavMesh, bool)"/>
  /// </summary>
  /// <remarks>Requires enabled <see cref="Navigation.Config.EnableNavigationCallbacks"/> in <see cref="SimulationConfig.Navigation"/>.</remarks>
  /// \ingroup NavigationApi
  public unsafe interface ISignalOnNavMeshSearchFailed: ISignal {
    /// <param name="f">Current frame object</param>
    /// <param name="entity">The entity the navmesh agent component belongs to</param>
    /// <param name="resetAgent">Set this to true if the agent should reset its internal state (default is true).</param>
    void OnNavMeshSearchFailed(Frame f, EntityRef entity, ref bool resetAgent);
  }

  /// <summary>
  /// Signal is called when the agent should move. The desired direction is influence by avoidance.
  /// </summary>
  /// <remarks>The agent velocity should be set in the callback.</remarks>
  /// \ingroup NavigationApi
  public unsafe interface ISignalOnNavMeshMoveAgent: ISignal {
    void OnNavMeshMoveAgent(Frame f, EntityRef entity, FPVector2 desiredDirection);
  }
}


// Core/RecordingFlags.cs

namespace  Quantum {
  [Flags]
  public enum RecordingFlags {
    None      = 0,
    Input     = 1 << 0,
    Checksums = 1 << 1,
    Default   = Input | Checksums,
    All       = 0xFF
  }
}

// Core/RuntimeConfig.cs

namespace Quantum {
  /// <summary>
  /// In contrast to the <see cref="SimulationConfig"/>, which has only static configuration data, the RuntimeConfig holds information that can be different from game to game.
  /// </summary>
  /// By default is defines for example what map to load and the random start seed. It is assembled from scratch each time starting a game.
  /// <para>Developers can add custom data to quantum_code/quantum.state/RuntimeConfig.User.cs (don't forget to fill out the serialization methods).</para>
  /// <para>Like the <see cref="DeterministicSessionConfig"/> this config is distributed to every other client after the first player connected and joined the Quantum plugin.</para>
  [Serializable]
  public partial class RuntimeConfig {
    /// <summary> Seed to initialize the randomization session under <see cref="Frame.RNG"/>. </summary>
    public Int32 Seed;
    /// <summary> Asset reference of the Quantum map used with the upcoming game session. </summary>
    public AssetRefMap Map;
    /// <summary> Asset reference to the SimulationConfig used with the upcoming game session. </summary>
    public AssetRefSimulationConfig SimulationConfig;

    /// <summary>
    /// Serializing the members to be send to the server plugin and other players.
    /// </summary>
    /// <param name="stream">Input output stream</param>
    public void Serialize(BitStream stream) {
      stream.Serialize(ref Seed);
      stream.Serialize(ref Map.Id.Value);
      stream.Serialize(ref SimulationConfig.Id.Value);
      SerializeUserData(stream);
    }

    /// <summary>
    /// Dump the content into a human readable form.
    /// </summary>
    /// <returns>String representation</returns>
    public String Dump() {
      String dump = "";
      DumpUserData(ref dump);

      StringBuilder sb = new StringBuilder();
      sb.Append(dump);
      sb.Append("\n");
      sb.AppendLine("Seed: " + Seed);
      sb.AppendLine($"Map.Guid: {Map.ToString()}");
      sb.AppendLine($"SimulationConfig.Guid: {SimulationConfig.ToString()} ");

      return sb.ToString();
    }

    partial void DumpUserData(ref String dump);
    partial void SerializeUserData(BitStream stream);

    /// <summary>
    /// Serialize the class into a byte array.
    /// </summary>
    /// <param name="config">Config to serialized</param>
    /// <returns>Byte array</returns>
    public static Byte[] ToByteArray(RuntimeConfig config) {
      BitStream stream;
      
      stream = new BitStream(new Byte[8192]);
      stream.Writing = true;

      config.Serialize(stream);

      return stream.ToArray();
    }

    /// <summary>
    /// Deserialize the class from a byte array.
    /// </summary>
    /// <param name="data">Config class in byte array form</param>
    /// <returns>New instance of the deserialized class</returns>
    public static RuntimeConfig FromByteArray(Byte[] data) {
      BitStream stream;
      stream = new BitStream(data);
      stream.Reading = true;

      RuntimeConfig config;
      config = new RuntimeConfig();
      config.Serialize(stream);

      return config;
    }
  }
}


// Core/RuntimePlayer.cs

namespace Quantum {

  public interface ISignalOnPlayerDataSet : ISignal {
    void OnPlayerDataSet(Frame f, PlayerRef player);
  }

  [Serializable]
  public partial class RuntimePlayer {
    public void Serialize(BitStream stream) {
      SerializeUserData(stream);
    }

    public String Dump() {
      String dump = "";
      DumpUserData(ref dump);
      return dump ?? "";
    }

    partial void DumpUserData(ref String dump);
    partial void SerializeUserData(BitStream stream);

    public static Byte[] ToByteArray(RuntimePlayer player) {
      BitStream stream;

      stream = new BitStream(new Byte[8192]);
      stream.Writing = true;

      player.Serialize(stream);

      return stream.ToArray();
    }

    public static RuntimePlayer FromByteArray(Byte[] data) {
      BitStream stream;
      stream = new BitStream(data);
      stream.Reading = true;

      RuntimePlayer player;
      player = new RuntimePlayer();
      player.Serialize(stream);

      return player;
    }
  }
}


// Core/SimulationConfig.cs

namespace Quantum {
  /// <summary>
  /// The SimulationConfig holds parameters used in the ECS layer and inside core systems like physics and navigation.
  /// </summary>
  [Serializable, AssetObjectConfig(GenerateLinkingScripts = true, GenerateAssetCreateMenu = false, GenerateAssetResetMethod = false)]
  public partial class SimulationConfig : AssetObject {
    public const long DEFAULT_ID = (long)DefaultAssetGuids.SimulationConfig;
    
    public enum AutoLoadSceneFromMapMode {
      Disabled,
      Legacy,
      UnloadPreviousSceneThenLoad,
      LoadThenUnloadPreviousScene
    }

    /// <summary>
    /// Global navmesh configurations.
    /// </summary>
    [Space]
    public Navigation.Config Navigation;
    /// <summary>
    /// Global physics configurations.
    /// </summary>
    [Space]
    public PhysicsCommon.Config Physics;
    /// <summary>
    /// Global entities configuration
    /// </summary>
    [Space]
    public FrameBase.EntitiesConfig Entities;
    /// <summary>
    /// This option will trigger a Unity scene load during the Quantum start sequence.\n
    /// This might be convenient to start with but once the starting sequence is customized disable it and implement the scene loading by yourself.
    /// "Previous Scene" refers to a scene name in Quantum Map.
    /// </summary>
    [Tooltip("This option will trigger a Unity scene load during the Quantum start sequence.\nThis might be convenient to start with but once the starting sequence is customized disable it and implement the scene loading by yourself.\n\"Previous Scene\" refers to a scene name in Quantum Map.")]
    public AutoLoadSceneFromMapMode AutoLoadSceneFromMap = AutoLoadSceneFromMapMode.UnloadPreviousSceneThenLoad;
    /// <summary>
    /// Configure how the client tracks the time to progress the Quantum simulation from the QuantumRunner class.
    /// </summary>
    [Tooltip("Configure how the client tracks the time to progress the Quantum simulation from the QuantumRunner class.")]
    public SimulationUpdateTime DeltaTimeType = SimulationUpdateTime.Default;
    /// <summary>
    /// Define the max heap size for one page of memory the frame class uses for custom allocations like QList<> for example.
    /// </summary>
    /// <remarks>2^15 = 32.768 bytes</remarks>
    /// <remarks><code>TotalHeapSizeInBytes = (1 << HeapPageShift) * HeapPageCount</code></remarks>
    [Tooltip("Define the max heap size for one page of memory the frame class uses for custom allocations like QList<> for example.\n\n2^15 = 32.768 bytes\nTotalHeapSizeInBytes = (1 << HeapPageShift) * HeapPageCount\n\nDefault is 15.")]
    public int HeapPageShift = 15;
    /// <summary>
    /// Define the max heap page count for memory the frame class uses for custom allocations like QList<> for example.
    /// </summary>
    /// <remarks><code>TotalHeapSizeInBytes = (1 << HeapPageShift) * HeapPageCount</code></remarks>
    [Tooltip("Define the max heap page count for memory the frame class uses for custom allocations like QList<> for example.\n\nTotalHeapSizeInBytes = (1 << HeapPageShift) * HeapPageCount\n\nDefault is 256.")]
    public int HeapPageCount = 256;
    /// <summary>
    /// Sets extra heaps to allocate for a session in case you need to
    /// create 'auxiliary' frames than actually required for the simulation itself
    /// </summary>
    [Tooltip("Sets extra heaps to allocate for a session in case you need to create 'auxiliary' frames than actually required for the simulation itself.\nDefault is 0.")]
    public int HeapExtraCount = 0;
    /// <summary>
    /// Override the number of threads used internally.
    /// </summary>
    [Tooltip("Override the number of threads used internally.\nDefault is 2.")]
    public int ThreadCount = 2;
    /// <summary>
    /// How long to store checksumed verified frames. The are used to generate a frame dump in case of a checksum error happening. Not used in Replay and Local mode.
    /// </summary>
    [Tooltip("How long to store checksumed verified frames.\nThe are used to generate a frame dump in case of a checksum error happening. Not used in Replay and Local mode.\nDefault is 3.")] 
    public FP ChecksumSnapshotHistoryLengthSeconds = 3;
    /// <summary>
    /// Additional options for checksum dumps, if the default settings don't provide a clear picture. 
    /// </summary>
    [EnumFlags]
    [Tooltip("Additional options for checksum dumps, if the default settings don't provide a clear picture. ")]
    public SimulationConfigChecksumErrorDumpOptions ChecksumErrorDumpOptions;
  }

  [Serializable, StructLayout(LayoutKind.Explicit)]
  public unsafe struct AssetRefSimulationConfig : IEquatable<AssetRefSimulationConfig> {
    public const int SIZE = sizeof(ulong);

    [FieldOffset(0)]
    public AssetGuid Id;

    public static implicit operator AssetRefSimulationConfig(Map value) {
      var r = default(AssetRefSimulationConfig);
      if (value != null) {
        r.Id = value.Guid;
      }
      return r;
    }

    public static void Serialize(void* ptr, FrameSerializer serializer) {
      var p = (AssetRefSimulationConfig*)ptr;
      AssetGuid.Serialize(&p->Id, serializer);
    }

    public override string ToString() {
      return AssetRef.ToString(Id);
    }

    public bool Equals(AssetRefSimulationConfig other) {
      return Id.Equals(other.Id);
    }

    public override bool Equals(object obj) {
      return obj is AssetRefSimulationConfig other && Equals(other);
    }

    public override int GetHashCode() {
      return Id.GetHashCode();
    }
    
    public static bool operator ==(AssetRefSimulationConfig a, AssetRefSimulationConfig b) {
      return a.Id == b.Id;
    }
    
    public static bool operator !=(AssetRefSimulationConfig a, AssetRefSimulationConfig b) {
      return a.Id != b.Id;
    }
  }

  public static class AssetRefSimulationConfigExt {
    public static SimulationConfig FindAsset(this Core.FrameBase f, AssetRefSimulationConfig assetRef) {
      return f.FindAsset<SimulationConfig>(assetRef.Id);
    }
  }

  [Flags]
  public enum SimulationConfigChecksumErrorDumpOptions {
    SendAssetDBChecksums = 1 << 0,
    ReadableDynamicDB    = 1 << 1,
    RawFPValues          = 1 << 2,
    ComponentChecksums   = 1 << 3,
  }

}


// Core/SimulationUpdateTime.cs
namespace Quantum {
  /// <summary>
  /// The type of measuring time progressions to update the local simulation.
  /// </summary>
  /// <para>Caveat: Changing it will make every client use the setting which might be undesirable when only used for debugging.</para>
  public enum SimulationUpdateTime {
    /// <summary>
    /// Internal stopwatch. Recommended for releasing games.
    /// </summary>
    Default,
    /// <summary>
    /// Engine (Unity) delta time. Extremely useful when pausing the Unity simulation during debugging for example.
    /// </summary>
    /// Caveat: the setting can cause issues with time synchronization when initializing online matches: the time tracking can be inaccurate under load (e.g.level loading) and result in a lot of large extra time syncs request and canceled inputs for a client when starting an online game.
    EngineDeltaTime,
    /// <summary>
    /// Engine unscaled delta time.
    /// </summary>
    EngineUnscaledDeltaTime
  }
}

// Core/StaticDelegates.cs

namespace Quantum {
  public static unsafe partial class StaticDelegates {
    internal struct Tag {}

    public static void Init() {
      CallOnce<Tag>.Invoke(() => InitGen());
    }

    static partial void InitGen();
  }
}

// Core/TypeRegistry.cs

namespace Quantum {
  public partial class TypeRegistry {
    readonly Dictionary<Type, int> _types = new Dictionary<Type, int>();

    public ReadOnlyDictionary<Type, int> Types {
      get {
        return new ReadOnlyDictionary<Type, int>(_types);
      }
    }

    public TypeRegistry() {
      AddBuiltIns();
      AddGenerated();
    }

    void Register(Type type, int size) {
      if (_types.ContainsKey(type)) {
        return;
      }
      
      _types.Add(type, size);
    }

    void AddBuiltIns() {
      Register(typeof(EntityRef), EntityRef.SIZE);
      Register(typeof(ComponentReference), ComponentReference.SIZE);
      Register(typeof(AssetRef), AssetRef.SIZE);
      Register(typeof(Shape2DType), 1);
      Register(typeof(Shape3DType), 1);
      Register(typeof(Shape2D), Shape2D.SIZE);
      Register(typeof(Shape2D.BoxShape), Shape2D.BoxShape.SIZE);
      Register(typeof(Shape2D.CircleShape), Shape2D.CircleShape.SIZE);
      Register(typeof(Shape2D.PolygonShape), Shape2D.PolygonShape.SIZE);
      Register(typeof(Shape2D.EdgeShape), Shape2D.EdgeShape.SIZE);
      Register(typeof(Shape2D.CompoundShape2D), Shape2D.CompoundShape2D.SIZE);
      Register(typeof(Shape3D), Shape3D.SIZE);
      Register(typeof(Shape3D.BoxShape), Shape3D.BoxShape.SIZE);
      Register(typeof(Shape3D.SphereShape), Shape3D.SphereShape.SIZE);
      Register(typeof(Shape3D.MeshShape), Shape3D.MeshShape.SIZE);
      Register(typeof(Shape3D.TerrainShape), Shape3D.TerrainShape.SIZE);
      Register(typeof(Shape3D.CompoundShape3D), Shape3D.CompoundShape3D.SIZE);
      Register(typeof(Physics2D.Joint), Physics2D.Joint.SIZE);
      Register(typeof(Physics2D.JointType), sizeof(short));
      Register(typeof(Physics2D.SpringJoint), Physics2D.SpringJoint.SIZE);
      Register(typeof(Physics2D.DistanceJoint), Physics2D.DistanceJoint.SIZE);
      Register(typeof(Physics2D.HingeJoint), Physics2D.HingeJoint.SIZE);
      Register(typeof(Physics3D.Joint3D), Physics3D.Joint3D.SIZE);
      Register(typeof(Physics3D.JointType3D), sizeof(short));
      Register(typeof(Physics3D.SpringJoint3D), Physics3D.SpringJoint3D.SIZE);
      Register(typeof(Physics3D.DistanceJoint3D), Physics3D.DistanceJoint3D.SIZE);
      Register(typeof(Physics3D.HingeJoint3D), Physics3D.HingeJoint3D.SIZE);
      Register(typeof(QBoolean), QBoolean.SIZE);

      // register internal heap types - heap struct itself does not need to be registered
      Allocator.Heap.RegisterInternalTypes(Register);
      
      // register internal physics types
      PhysicsCommon.RegisterInternalTypes(Register);
      Physics2D.PhysicsEngine2D.RegisterInternalTypes(Register);
      Physics3D.PhysicsEngine3D.RegisterInternalTypes(Register);
      
      // register collection memory integrity checks
      Collections.QCollectionsUtils.RegisterTypes(Register);
    }
    
    partial void AddGenerated();
  }
}

// Game/CallbackDispatcher.cs

namespace Quantum {
  public class CallbackDispatcher : DispatcherBase, Quantum.ICallbackDispatcher {

    protected static Dictionary<Type, Int32> GetBuiltInTypes() {
      return new Dictionary<Type, Int32>() {
        { typeof(CallbackChecksumComputed), CallbackChecksumComputed.ID },
        { typeof(CallbackChecksumError), CallbackChecksumError.ID },
        { typeof(CallbackChecksumErrorFrameDump), CallbackChecksumErrorFrameDump.ID },
        { typeof(CallbackEventCanceled), CallbackEventCanceled.ID },
        { typeof(CallbackEventConfirmed), CallbackEventConfirmed.ID },
        { typeof(CallbackGameDestroyed), CallbackGameDestroyed.ID },
        { typeof(CallbackGameStarted), CallbackGameStarted.ID },
        { typeof(CallbackGameResynced), CallbackGameResynced.ID },
        { typeof(CallbackInputConfirmed), CallbackInputConfirmed.ID },
        { typeof(CallbackPollInput), CallbackPollInput.ID },
        { typeof(CallbackSimulateFinished), CallbackSimulateFinished.ID },
        { typeof(CallbackUpdateView), CallbackUpdateView.ID },
        { typeof(CallbackPluginDisconnect), CallbackPluginDisconnect.ID },
      };
    }

    public CallbackDispatcher() : base(GetBuiltInTypes()) { }
    protected CallbackDispatcher(Dictionary<Type, Int32> callbackTypes) : base(callbackTypes) { }

    public bool Publish(CallbackBase e) {
      return base.InvokeMeta(e.ID, e);
    }
  }
}


// Game/ChecksumErrorFrameDumpContext.cs

namespace Quantum {
  public unsafe partial class ChecksumErrorFrameDumpContext {
    public SimulationConfig SimulationConfig;
    public QTuple<AssetGuid, ulong>[] AssetDBChecksums;

    private ChecksumErrorFrameDumpContext() {}

    public ChecksumErrorFrameDumpContext(QuantumGame game, Frame frame) {
      var options = game.Configurations.Simulation.ChecksumErrorDumpOptions;

      SimulationConfig = game.Configurations.Simulation;

      // write checksums
      if ((options & SimulationConfigChecksumErrorDumpOptions.SendAssetDBChecksums) == SimulationConfigChecksumErrorDumpOptions.SendAssetDBChecksums) {
        
        var assets = frame.Context.AssetDB.FindAllAssets(true).ToList();
        AssetDBChecksums = new QTuple<AssetGuid, ulong>[assets.Count];
        assets.Sort((a, b) => a.Guid.CompareTo(b.Guid));

        AssetObject[] temp = new AssetObject[1];
        for (int i = 0; i < assets.Count; ++i) {
          temp[0] = assets[i];
          var bytes = frame.Context.AssetSerializer.SerializeAssets(temp);
          fixed (byte* p = bytes) {
            var crc = CRC64.Calculate(0, p, bytes.Length);
            AssetDBChecksums[i] = QTuple.Create(assets[i].Guid, crc);
          }
        }
      }

      ConstructUser(game, frame);
    }

    partial void ConstructUser(QuantumGame game, Frame frame);
    partial void SerializeUser(QuantumGame game, BinaryWriter writer);
    partial void DeserializeUser(QuantumGame game, BinaryReader reader);

    public void Serialize(QuantumGame game, BinaryWriter writer) {
      writer.Write(AssetDBChecksums?.Length ?? 0);
      if (AssetDBChecksums != null) {
        foreach (var asset in AssetDBChecksums) {
          writer.Write(asset.Item0.Value);
          writer.Write(asset.Item1);
        }
      }

      // write simulation config
      if (SimulationConfig != null) {
        var simConfigBytes = game.AssetSerializer.SerializeAssets(new[] { SimulationConfig });
        writer.Write(simConfigBytes.Length);
        writer.Write(simConfigBytes);
      } else {
        writer.Write(0);
      }

      SerializeUser(game, writer);
    }

    public static ChecksumErrorFrameDumpContext Deserialize(QuantumGame game, BinaryReader reader) {
      var result = new ChecksumErrorFrameDumpContext();

      // read checksums
      {
        int count = reader.ReadInt32();
        if (count > 0) {
          result.AssetDBChecksums = new QTuple<AssetGuid, ulong>[count];

          for (int i = 0; i < count; ++i) {
            var guidRaw = reader.ReadInt64();
            var crc64 = reader.ReadUInt64();
            result.AssetDBChecksums[i] = QTuple.Create(new AssetGuid(guidRaw), crc64);
          }
        }
      }

      // read sim config
      {
        int count = reader.ReadInt32();
        if (count > 0) {
          var configBytes = reader.ReadBytes(count);
          result.SimulationConfig = (SimulationConfig)game.AssetSerializer.DeserializeAssets(configBytes).Single();
        }
      }

      result.DeserializeUser(game, reader);

      return result;
    }
  }
}


// Game/EventDispatcher.cs

namespace Quantum {
  public class EventDispatcher : DispatcherBase, IEventDispatcher {

    private static Dictionary<Type, Int32> GetEventTypes() {
      var result = new Dictionary<Type, Int32> {
        { typeof(EventBase), 0 }
      };

      for (int eventID = 0; eventID < Frame.FrameEvents.EVENT_TYPE_COUNT; ++eventID) {
        result.Add(Frame.FrameEvents.GetEventType(eventID), eventID + 1);
      }

      return result;
    }

    public EventDispatcher() : base(GetEventTypes()) { }

    public unsafe bool Publish(EventBase e) {

      int eventDepth = 0;
      for (int id = e.Id; id >= 0; id = Frame.FrameEvents.GetParentEventID(id)) {
        ++eventDepth;
      }

      int* eventIdStack = stackalloc int[eventDepth];
      for (int id = e.Id, i = 0; id >= 0; id = Frame.FrameEvents.GetParentEventID(id), i++) {
        eventIdStack[i] = id;
      }

      bool hadActiveHandlers = false;

      // start with the EventBase
      int metaIndex = 0;

      for (; ; ) {
        hadActiveHandlers |= base.InvokeMeta(metaIndex, e);

        if (--eventDepth >= 0) {
          // choose next event
          metaIndex = eventIdStack[eventDepth] + 1;
        } else {
          break;
        }
      }

      return hadActiveHandlers;
    }
  }
}


// Game/InstantReplaySettings.cs

namespace Quantum {
  [Serializable]
  public struct InstantReplaySettings {
    public int SnapshotsPerSecond;
    public FP LenghtSeconds;

    public static InstantReplaySettings Default => new InstantReplaySettings() {
      LenghtSeconds = 3,
      SnapshotsPerSecond = 1,
    };

    public static InstantReplaySettings FromLength(FP length, int snapshotsPerSecond) {
      return new InstantReplaySettings() {
        LenghtSeconds = length,
        SnapshotsPerSecond = snapshotsPerSecond
      };
    }

    public override string ToString() {
      return $"({nameof(SnapshotsPerSecond)}: {SnapshotsPerSecond}, {nameof(LenghtSeconds)}: {LenghtSeconds})";
    }
  }
}


// Game/QuantumGame.Snapshots.cs

namespace Quantum {
  public partial class QuantumGame {

    DeterministicFrameRingBuffer _checksumSnapshotBuffer;
    DeterministicFrameRingBuffer _instantReplaySnapshotBuffer;

    bool _instantReplaySnapshotsRecording;
    Int32 _commonSnapshotInterval;
    Int32 _instantReplaySnapshotInterval;

    void SnapshotsOnDestroy() {
      _checksumSnapshotBuffer?.Clear();
      _checksumSnapshotBuffer = null;
      _instantReplaySnapshotBuffer?.Clear();
      _instantReplaySnapshotBuffer = null;
    }

    void SnapshotsOnSimulateFinished(DeterministicFrame state) {

      if (!state.IsVerified) {
        return;
      }

      HostProfiler.Start("QuantumGame.RecordingSnapshots");

      if (_checksumSnapshotBuffer != null) {
        // in case replay interval is less than checksum interval and replay is not being recorded,
        // there's no need to sample at a common rate
        Int32 interval;
        if (_commonSnapshotInterval > 0 && _instantReplaySnapshotsRecording) {
          Assert.Check(_instantReplaySnapshotBuffer == null);
          interval = _commonSnapshotInterval;
        } else {
          interval = Session.SessionConfig.ChecksumInterval;
        }

        if ((state.Number % interval) == 0) {
          _checksumSnapshotBuffer.PushBack(state, this, _context);
        }
      }

      if (_instantReplaySnapshotsRecording && _instantReplaySnapshotBuffer != null) {
        Assert.Check(_commonSnapshotInterval <= 0);
        if (_instantReplaySnapshotBuffer.Count == 0 || (state.Number % _instantReplaySnapshotInterval) == 0) {
          _instantReplaySnapshotBuffer.PushBack(state, this, _context);
        }
      }

      HostProfiler.End();
    }

    Int32 SnapshotsCreateBuffers(Int32 simulationRate, Int32 checksumInterval, FP checksumTimeWindow, Int32 replayInterval, FP replayTimeWindow) {

      var checksumFrameWindow = FPMath.CeilToInt(simulationRate * checksumTimeWindow);
      var replayFrameWindow   = FPMath.CeilToInt(simulationRate * replayTimeWindow);

      var checksumBufferSize = DeterministicFrameRingBuffer.GetSize(FPMath.CeilToInt(simulationRate * checksumTimeWindow), checksumInterval);
      var replayBufferSize   = DeterministicFrameRingBuffer.GetSize(FPMath.CeilToInt(simulationRate * replayTimeWindow), replayInterval);

      if (checksumInterval > 0 && checksumBufferSize > 0 && replayBufferSize > 0 && replayInterval > 0) {
        if (DeterministicFrameRingBuffer.TryGetCommonSamplingPattern(checksumFrameWindow, checksumInterval, replayFrameWindow, replayInterval, out var commonWindow, out var commonInterval)) {
          _commonSnapshotInterval = commonInterval;
          _checksumSnapshotBuffer = new DeterministicFrameRingBuffer(DeterministicFrameRingBuffer.GetSize(commonWindow, commonInterval));
          Log.Trace($"Snapshots: common buffer created with interval: {_commonSnapshotInterval}, window: {commonWindow}, capacity: {_checksumSnapshotBuffer.Capacity}");
          return _checksumSnapshotBuffer.Capacity;
        } else {
          // shared buffer not possible
          Log.Warn($"Unable to create a shared buffer for checksumed frames and replay snapshots. This is not optimal. Check the documentation for details.");
        }
      }

      if (checksumBufferSize > 0) {
        _checksumSnapshotBuffer = new DeterministicFrameRingBuffer(checksumBufferSize);
        Log.Trace($"Snapshots: checksum buffer created with interval {checksumInterval}, capacity: {checksumBufferSize}");
      }

      if (replayBufferSize > 0) {
        _instantReplaySnapshotInterval = replayInterval;
        _instantReplaySnapshotBuffer = new DeterministicFrameRingBuffer(replayBufferSize);
        Log.Trace($"Snapshots: replay buffer created with interval {replayInterval}, capacity: {replayBufferSize}");
      }

      return checksumBufferSize + replayBufferSize;
    }

    static Int32 SnapshotsGetMinBufferSize(Int32 window, Int32 samplingRate) {
      return samplingRate <= 0 ? 0 : (1 + window / samplingRate);
    }
  }
}

// Game/QuantumGame.cs

namespace Quantum {
  /// <summary>
  /// This class contains values for flags that will be accessible with <see cref="QuantumGame.GameFlags"/>.
  /// Built-in flags control some aspects of QuantumGame inner workings, without affecting the simulation
  /// outcome.
  /// </summary>
  public partial class QuantumGameFlags {
    /// <summary>
    /// Starts the game in the server mode. 
    /// When this flag is not set, all the events marked with "server" get culled immediatelly.
    /// If this flag is set, all the events marked with "client" get culled immediatelly.
    /// </summary>
    public const int Server = 1 << 0;
    /// <summary>
    /// By default, QuantumGame uses a single shared checksum serializer to reduce allocations. 
    /// The serializer is *not* static - it is only shared between frames comming from the same QuantumGame.
    /// Set this flag if you want to disable this behaviour, for example if you calculate
    /// checksums for multiple frames using multiple threads.
    /// </summary>
    public const int DisableSharedChecksumSerializer = 1 << 1;
    /// <summary>
    /// Custom user flags start from this value. Flags are accessible with <see cref="QuantumGame.GameFlags"/>.
    /// </summary>
    public const int CustomFlagsStart = 1 << 16;
  }

  /// <summary>
  /// QuantumGame acts as an interface to the simulation from the client code's perspective.
  /// </summary>
  /// Access and method to this class is always safe from the clients point of view.
  public unsafe partial class QuantumGame : IDeterministicGame {
    public event Action<ProfilerContextData> ProfilerSampleGenerated;

    public struct StartParameters {
      public IResourceManager       ResourceManager;
      public IAssetSerializer       AssetSerializer;
      public ICallbackDispatcher    CallbackDispatcher;
      public IEventDispatcher       EventDispatcher;
      public InstantReplaySettings  InstantReplaySettings;
      public int                    HeapExtraCount;
      public DynamicAssetDB         InitialDynamicAssets;
      public int                    GameFlags;
    }


    /// <summary>
    /// Stores the different frames the simulation uses during one tick.
    /// </summary>
    public class FramesContainer {
      public Frame Verified;
      public Frame Predicted;
      public Frame PredictedPrevious;
      public Frame PreviousUpdatePredicted;
    }

    // Caveat: Only set after the first CreateFrame() call
    public class ConfigurationsContainer {
      public RuntimeConfig Runtime;
      public SimulationConfig Simulation;
    }

    /// <summary> Access the frames of various times available during one tick. </summary>
    public FramesContainer Frames { get; }

    /// <summary> Access the configurations that the simulation is running with. </summary>
    public ConfigurationsContainer Configurations { get; }

    /// <summary> Access the Deterministic session object to query more internals. </summary>
    public DeterministicSession Session { get; private set; }

    /// <summary> Used for position interpolation on the client for smoother interpolation results. </summary>
    public Single InterpolationFactor { get; private set; }

    /// <summary> </summary>
    public InstantReplaySettings InstantReplayConfig { get; private set; }

    /// <summary> </summary>
    public IAssetSerializer AssetSerializer { get; }

    /// <summary> Extra heaps to allocate for a session in case you need to create 'auxiliary' frames than actually required for the simulation itself. </summary>
    public int HeapExtraCount { get; }


    Byte[] _inputStreamReadZeroArray;
    IResourceManager _resourceManager;
    ICallbackDispatcher _callbackDispatcher;
    IEventDispatcher _eventDispatcher;

    FrameSerializer _inputSerializerRead;
    FrameSerializer _inputSerializerWrite;

    SystemBase[] _systemsRoot;
    SystemBase[] _systemsAll;

    FrameContext _context;
    TypeRegistry _typeRegistry;
    bool _polledInputInThisSimulation;
    DynamicAssetDB _initialDynamicAssets;
    int _flags;

    public QuantumGame(in StartParameters startParams) {
      _typeRegistry  = new TypeRegistry();
      Frames         = new FramesContainer();
      Configurations = new ConfigurationsContainer();

      _resourceManager      = startParams.ResourceManager;
      AssetSerializer       = startParams.AssetSerializer;
      _callbackDispatcher   = startParams.CallbackDispatcher;
      _eventDispatcher      = startParams.EventDispatcher;
      InstantReplayConfig   = startParams.InstantReplaySettings;
      HeapExtraCount        = startParams.HeapExtraCount;
      _flags                = startParams.GameFlags;

      if (startParams.InitialDynamicAssets != null) {
        _initialDynamicAssets = new DynamicAssetDB();
        _initialDynamicAssets.CopyFrom(startParams.InitialDynamicAssets);
      }

      InitCallbacks();
    }

    [Obsolete]
    public QuantumGame(IResourceManager manager, IAssetSerializer assetSerializer, ICallbackDispatcher callbackDispatcher, IEventDispatcher eventDispatcher)
      : this(new StartParameters() {
        ResourceManager = manager,
        AssetSerializer = assetSerializer,
        CallbackDispatcher = callbackDispatcher,
        EventDispatcher = eventDispatcher,
      }) { }

    /// <summary>
    /// Returns an array that is unique on  every client and represents the indexes for players that your local machine controls in the Quantum simulation.
    /// </summary>
    /// <returns>Array of player indices</returns>
    public Int32[] GetLocalPlayers() {
      return Session.LocalPlayerIndices;
    }

    /// <summary>
    ///  Helps to decide if a PlayerRef is associated with the local player.
    /// </summary>
    /// <param name="playerRef">Player reference</param>
    /// <returns>True if the player is the local player</returns>
    public Boolean PlayerIsLocal(PlayerRef playerRef) {
      if (playerRef == PlayerRef.None) {
        return false;
      }

      for (Int32 i = 0; i < Session.LocalPlayerIndices.Length; i++) {
        if (Session.LocalPlayerIndices[i] == playerRef) {
          return true;
        }
      }

      return false;
    }

    /// <summary>
    /// Sends a command to the server.
    /// </summary>
    /// <param name="command">Command to send</param>
    /// Commands are similar to input, they drive the simulation, but do not have to be sent regularly.
    /// <example><code>
    /// RemoveUnitCommand command = new RemoveUnitCommand();
    /// command.CellIndex = 42;
    /// QuantumRunner.Default.Game.SendCommand(command);
    /// </code></example>
    public void SendCommand(DeterministicCommand command) {
      var players = GetLocalPlayers();
      if (players.Length > 0) {
        Session.SendCommand(players[0], command);
      } else {
        Log.Error("No local player found to send command for");
      }
    }

    /// <summary>
    /// Sends a command to the server.
    /// </summary>
    /// <param name="player">Specify the player index (PlayerRef) when you have multiple players controlled from the same machine.</param>
    /// <param name="command">Command to send</param>
    /// <para>See <see cref="SendCommand(DeterministicCommand)"/></para>
    /// <para>Games that only have one local player can ignore the player index field.</para>
    public void SendCommand(Int32 player, DeterministicCommand command) {
      Session.SendCommand(player, command);
    }

    /// <summary>
    /// Send data for the local player to join the online match.
    /// If the client has multiple local players, the data will be sent for the first of them (smallest player index).
    /// </summary>
    /// <param name="data">Player data</param>
    /// After starting, joining the Quantum Game and after the OnGameStart signal has been fired each player needs to call the SendPlayerData method to be added as a player in every ones simulation.\n
    /// The reason this needs to be called explicitly is that it greatly simplifies late-joining players.
    public void SendPlayerData(RuntimePlayer data) {
      var players = GetLocalPlayers();
      if (players.Length > 0) {
        Session.SetPlayerData(players[0], RuntimePlayer.ToByteArray(data));
      } else {
        Log.Error("No local player found to send player data for.");
      }
    }

    /// <summary>
    /// Send data for one local player to join the online match.
    /// </summary>
    /// <param name="player">Local player index</param>
    /// <param name="data">Player data</param>
    /// After starting, joining the Quantum Game and after the OnGameStart signal has been fired each player needs to call the SendPlayerData method to be added as a player in every ones simulation.\n
    /// The reason this needs to be called explicitly is that it greatly simplifies late-joining players.
    public void SendPlayerData(Int32 player, RuntimePlayer data) {
      Session.SetPlayerData(player, RuntimePlayer.ToByteArray(data));
    }

    /// <summary>
    /// <see cref="QuantumGameFlags"/>
    /// </summary>
    public int GameFlags => _flags;

    public void OnDestroy() {
      SnapshotsOnDestroy();
      InvokeOnDestroy();
    }

    public Frame CreateFrame() {
      return (Frame)((IDeterministicGame)this).CreateFrame(_context);
    }

    DeterministicFrame IDeterministicGame.CreateFrame(IDisposable context) {
      return new Frame((FrameContextUser)context, _systemsAll, _systemsRoot, Session.SessionConfig, Configurations.Runtime, Configurations.Simulation, Session.DeltaTime);
    }

    DeterministicFrame IDeterministicGame.CreateFrame(IDisposable context, Byte[] data) {
      Frame f = CreateFrame();
      f.Deserialize(data);
      return f;
    }

    public DeterministicFrame GetVerifiedFrame(int tick) {
      if (_checksumSnapshotBuffer != null) {
        var result = _checksumSnapshotBuffer.Find(tick, DeterministicFrameSnapshotBufferFindMode.Equal);
        if (result == null) {
          Log.Warn($"Unable to find verified frame for tick {tick}, increase {nameof(DeterministicSessionConfig.ChecksumInterval)} or increase {nameof(SimulationConfig.ChecksumSnapshotHistoryLengthSeconds)}.");
        }
        return result;
      }
      return null;
    }

    public IDisposable CreateFrameContext() {
      if (_context == null) {
        Assert.Check(_systemsAll == null);
        Assert.Check(_systemsRoot == null);

        // create asset database
        var assetDB = _resourceManager.CreateAssetDatabase();

        // de-serialize runtime config, session is the one from the server
        Configurations.Runtime = RuntimeConfig.FromByteArray(Session.RuntimeConfig);
        Configurations.Simulation = assetDB.FindAsset<SimulationConfig>(Configurations.Runtime.SimulationConfig.Id, true);

        // register commands
        Session.CommandSerializer.RegisterFactories(DeterministicCommandSetup.GetCommandFactories(Configurations.Runtime, Configurations.Simulation));

        // initialize systems
        _systemsRoot = SystemSetup.CreateSystems(Configurations.Runtime, Configurations.Simulation).Where(x => x != null).ToArray();
        _systemsAll = _systemsRoot.SelectMany(x => x.Hierarchy).ToArray();

        Int32 heapCount = 4;
        heapCount += Math.Max(0, Configurations.Simulation.HeapExtraCount);
        heapCount += Math.Max(0, HeapExtraCount);
        heapCount += SnapshotsCreateBuffers(Session.SessionConfig.UpdateFPS,
          Session.IsOnline ? Session.SessionConfig.ChecksumInterval : 0, Configurations.Simulation.ChecksumSnapshotHistoryLengthSeconds,
          InstantReplayConfig.SnapshotsPerSecond == 0 ? 0 : Session.SessionConfig.UpdateFPS / InstantReplayConfig.SnapshotsPerSecond, InstantReplayConfig.LenghtSeconds);

        // set system runtime indices
        for (Int32 i = 0; i < _systemsAll.Length; ++i) {
          _systemsAll[i].RuntimeIndex = i;
        }

        // set core count override
        Session.PlatformInfo.CoreCount = Configurations.Simulation.ThreadCount;

        FrameContext.Args args;
        args.AssetDatabase               = assetDB;
        args.PlatformInfo                = Session.PlatformInfo;
        args.IsServer                    = (_flags & QuantumGameFlags.Server) == QuantumGameFlags.Server;
        args.IsLocalPlayer               = Session.IsLocalPlayer;
        args.HeapConfig                  = new Heap.Config(Configurations.Simulation.HeapPageShift, Configurations.Simulation.HeapPageCount, heapCount);
        args.PhysicsConfig               = Configurations.Simulation.Physics;
        args.NavigationConfig            = Configurations.Simulation.Navigation;
        args.CommandSerializer           = Session.CommandSerializer;
        args.AssetSerializer             = AssetSerializer;
        args.InitialDynamicAssets        = _initialDynamicAssets;
        args.UseSharedChecksumSerialized = (_flags & QuantumGameFlags.DisableSharedChecksumSerializer) != QuantumGameFlags.DisableSharedChecksumSerializer;

        // toggle various parts of the context code
        args.UsePhysics2D   = _systemsAll.FirstOrDefault(x => x is PhysicsSystem2D) != null;
        args.UsePhysics3D   = _systemsAll.FirstOrDefault(x => x is PhysicsSystem3D) != null;
        args.UseNavigation  = _systemsAll.FirstOrDefault(x => x is NavigationSystem) != null;
        args.UseCullingArea = _systemsAll.FirstOrDefault(x => x is CullingSystem2D) != null || _systemsAll.FirstOrDefault(x => x is CullingSystem3D) != null;

        // create frame context
        _context = new FrameContextUser(args);
      }

      return _context;
    }

    /// <summary>
    /// Set the prediction area.
    /// </summary>
    /// <param name="position">Center of the prediction area</param>
    /// <param name="radius">Radius of the prediction area</param>
    /// <para>The Prediction Culling feature must be explicitly enabled in <see cref="SimulationConfig.UsePredictionArea"/>.</para>
    /// <para>This can be safely called from the main-thread.</para>
    /// <para>Prediction Culling allows developers to save CPU time in games where the player has only a partial view of the game scene.
    /// Quantum prediction and rollbacks, which are time consuming, will only run for important entities that are visible to the local player(s). Leaving anything outside that area to be simulated only once per tick with no rollbacks as soon as the inputs are confirmed from server.
    /// It is safe and simple to activate and, depending on the game, the performance difference can be quite large.Imagine a 30Hz game to constantly rollback ten ticks for every confirmed input (with more players, the predictor eventually misses at least for one of them). This requires the game simulation to be lightweight to be able to run at almost 300Hz(because of the rollbacks). With Prediction Culling enabled the full frames will be simulated at the expected 30Hz all the time while the much smaller prediction area is the only one running within the prediction buffer.</para>
    public void SetPredictionArea(FPVector3 position, FP radius) {
      _context.SetPredictionArea(position, radius);
    }

    /// <summary>
    /// See <see cref="SetPredictionArea(FPVector3, FP)"/>.
    /// </summary>
    /// <param name="position"></param>
    /// <param name="radius"></param>
    public void SetPredictionArea(FPVector2 position, FP radius) {
      _context.SetPredictionArea(position.XOY, radius);
    }

    public void OnGameEnded() {
      InvokeOnGameEnded();
    }

    public void OnGameStart(DeterministicFrame f) {
      // init event invoker
      InitEventInvoker(Session.RollbackWindow);

      Frames.Predicted = (Frame)f;
      Frames.PredictedPrevious = (Frame)f;
      Frames.Verified = (Frame)f;
      Frames.PreviousUpdatePredicted = (Frame)f;

      InvokeOnGameStart();

      // init systems on latest frame
      InitSystems(f);

      Log.Debug("Local Players: " + string.Join(" ", Session.LocalPlayerIndices));
    }

    public void OnGameResync() {
      _checksumSnapshotBuffer?.Clear();
      ReplayToolsOnGameResync();

      // reset physics engines statics
      Frames.Verified.Physics2D.Init();
      Frames.Verified.Physics3D.Init();

      // events won't get confirmed
      CancelPendingEvents();

      InvokeOnGameResync();
    }

    public DeterministicFrameInputTemp OnLocalInput(Int32 frame, Int32 player) {
      var input = default(QTuple<Input, DeterministicInputFlags>);

      // poll input
      try {
        bool isFirst = _polledInputInThisSimulation == false;
        _polledInputInThisSimulation = true;
        input = InvokeOnPollInput(frame, player, isFirst);
      } catch (Exception exn) {
        Log.Error("## Input Code Threw Exception ##");
        Log.Exception(exn);
      }

      if (_inputSerializerWrite == null) {
        _inputSerializerWrite = new FrameSerializer(DeterministicFrameSerializeMode.Serialize, null, new Byte[1024]);
      }

      // clear old data
      _inputSerializerWrite.Reset();
      _inputSerializerWrite.Writing = true;
      _inputSerializerWrite.InputMode = true;

      // pack into stream
      Input.Write(_inputSerializerWrite, input.Item0);

      // return temp input
      return DeterministicFrameInputTemp.Predicted(frame, player, _inputSerializerWrite.Stream.Data, _inputSerializerWrite.Stream.BytesRequired, input.Item1);
    }

    public void OnSimulate(DeterministicFrame state) {
      HostProfiler.Start("QuantumGame.OnSimulate");

      var f = (Frame)state;

      try {
        // reset profiling
        HostProfiler.Start("Init Profiler");
        f.Context.ProfilerContext.Reset();
        var profiler = f.Context.ProfilerContext.GetProfilerForTaskThread(0);
        HostProfiler.End();

        HostProfiler.Start("ApplyInputs");
        ApplyInputs(f);
        HostProfiler.End();

        HostProfiler.Start("OnSimulateBegin");
        f.Context.OnFrameSimulationBegin(f);
        f.OnFrameSimulateBegin();
        f.Context.TaskContext.BeginFrame(f);
        HostProfiler.End();

        var handle = f.Context.TaskContext.AddRootTask();

        HostProfiler.Start("UpdatePlayerData");
        f.UpdatePlayerData();
        HostProfiler.End();

        profiler.Start("Scheduling Tasks #ff9900");
        HostProfiler.Start("Scheduling Tasks");

        var systems = &f.Global->Systems;

        for (Int32 i = 0; i < _systemsRoot.Length; ++i) {
          if (f.SystemIsEnabledSelf(_systemsRoot[i])) {
            try {
              handle = _systemsRoot[i].OnSchedule(f, handle);
            } catch (Exception exn) {
              LogSimulationException(exn);
            }
          }
        }

        HostProfiler.End();
        profiler.End();

        try {
          f.Context.TaskContext.EndFrame();
          f.OnFrameSimulateEnd();
          f.Context.OnFrameSimulationEnd();
        } catch (Exception exn) {
          Log.Exception(exn);
        }

        if (ProfilerSampleGenerated != null) {
          var data = f.Context.ProfilerContext.CreateReport(f.Number, f.IsVerified);
          ProfilerSampleGenerated(data);
        }

#if PROFILER_FRAME_AVERAGE
      f.Context.ProfilerContext.StoreFrameTime();
      Log.Info("Frame Average: " +  f.Context.ProfilerContext.GetFrameTimeAverage());
#endif
      } catch (Exception exn) {
        LogSimulationException(exn);
      }

      HostProfiler.End();
    }

    public void OnSimulateFinished(DeterministicFrame state) {
      SnapshotsOnSimulateFinished(state);
      InvokeOnSimulateFinished(state);
    }

    public void OnUpdateDone() {
      Frames.Predicted = (Frame)Session.FramePredicted;
      Frames.PredictedPrevious = (Frame)Session.FramePredictedPrevious;
      Frames.Verified = (Frame)Session.FrameVerified;
      Frames.PreviousUpdatePredicted = (Frame)Session.PreviousUpdateFramePredicted;

      if (Session.IsStalling == false) {
        var f = (float)(Session.AccumulatedTime / Frames.Predicted.DeltaTime.AsFloat);
        InterpolationFactor = f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f; // Clamp01
      }

      InvokeOnUpdateView();
      InvokeEvents();
    }

    public void AssignSession(DeterministicSession session) {
      Session = session;

      DeterministicSessionConfig sessionConfig;
      Session.GetLocalConfigs(out sessionConfig, out _);

      // verify player count is in correct range
      if (sessionConfig.PlayerCount < 1 || sessionConfig.PlayerCount > Quantum.Input.MAX_COUNT) {
        throw new Exception(String.Format("Invalid player count {0} (needs to be in 1-{1} range)", sessionConfig.PlayerCount, Quantum.Input.MAX_COUNT));
      }

      // verify all types
      var verifier = new MemoryLayoutVerifier(MemoryLayoutVerifier.Platform ?? new MemoryLayoutVerifier.DefaultPlatform());
      var result = verifier.Verify(_typeRegistry.Types);
      if (result.Count > 0) {
        throw new Exception("MemoryIntegrity Check Failed: " + System.Environment.NewLine + String.Join(System.Environment.NewLine, result.ToArray()));
      } else {
        Log.Info("Memory Integrity Verified");
      }
    }

    public void OnChecksumError(DeterministicTickChecksumError error, DeterministicFrame[] frames) {
      InvokeOnChecksumError(error, frames);
    }

    public void OnChecksumComputed(Int32 frame, ulong checksum) {
      InvokeOnChecksumComputed(frame, checksum);
      ReplayToolsOnChecksumComputed(frame, checksum);
    }

    public void OnSimulationEnd() {
      _context.OnSimulationEnd();
    }

    public void OnSimulationBegin() {
      _polledInputInThisSimulation = false;
      _context.OnSimulationBegin();
    }

    public void OnInputConfirmed(DeterministicFrameInputTemp input) {
      InvokeOnInputConfirmed(input);
      ReplayToolsOnInputConfirmed(input);
    }

    public void OnChecksumErrorFrameDump(int actorId, int frameNumber, DeterministicSessionConfig sessionConfig, byte[] runtimeConfig, byte[] frameData, byte[] extraData) {
      InvokeOnChecksumErrorFrameDump(actorId, frameNumber, sessionConfig, runtimeConfig, frameData, extraData);
    }

    public void OnPluginDisconnect(string reason) {
      Log.Error("DISCONNECTED: " + reason);
      InvokeOnPluginDisconnect(reason);
    }

    public int GetInputInMemorySize() {
      return sizeof(Input);
    }

    public Int32 GetInputSerializedFixedSize() {
      var stream = new FrameSerializer(DeterministicFrameSerializeMode.Serialize, null, 1024);
      stream.Writing = true;
      stream.InputMode = true;
      Input.Write(stream, new Input());
      return stream.ToArray().Length;
    }

    void InitSystems(DeterministicFrame df) {
      var f = (Frame)df;

      try {
        f.Context.OnFrameSimulationBegin(f);

        // call init on ALL systems
        for (Int32 i = 0; i < _systemsAll.Length; ++i) {
          try {
            _systemsAll[i].OnInit(f);

            if (f.CommitCommandsMode == CommitCommandsModes.InBetweenSystems) {
              f.Unsafe.CommitAllCommands();
            }
          } catch (Exception exn) {
            LogSimulationException(exn);
          }
        }

        // TODO: this seems like a good place to fire OnMapChanged,
        // if we want to do that for the initial map

        // call OnEnabled on all systems which start enabled
        for (Int32 i = 0; i < _systemsAll.Length; ++i) {
          if (_systemsAll[i].StartEnabled) {
            try {
              _systemsAll[i].OnEnabled(f);
              
              if (f.CommitCommandsMode == CommitCommandsModes.InBetweenSystems) {
                f.Unsafe.CommitAllCommands();
              }
            } catch (Exception exn) {
              LogSimulationException(exn);
            }
          }
        }

        f.Context.OnFrameSimulationEnd();
      } catch (Exception e) {
        LogSimulationException(e);
      }

      // invoke events from OnInit/OnEnabled
      InvokeEvents();
    }

    public void DeserializeInputInto(int player, byte[] data, byte* buffer) {
      if (_inputSerializerRead == null) {
        _inputStreamReadZeroArray = new Byte[1024];
        _inputSerializerRead = new FrameSerializer(DeterministicFrameSerializeMode.Serialize, null, new Byte[1024]);
      }

      _inputSerializerRead.Reset();
      _inputSerializerRead.Frame = null;
      _inputSerializerRead.Reading = true;
      _inputSerializerRead.InputMode = true;

      if (data == null || data.Length == 0) {
        _inputSerializerRead.CopyFromArray(_inputStreamReadZeroArray);
      } else {
        _inputSerializerRead.CopyFromArray(data);
      }

      try {
        *(Input*)buffer = Input.Read(_inputSerializerRead);
      } catch (Exception exn) {
        *(Input*)buffer = default;

        // log exception
        Log.Error("Received invalid input data from player {0}, could not deserialize.", player);
        Log.Exception(exn);
      }
    }

    void ApplyInputs(Frame f) {
      for (Int32 i = 0; i < Session.PlayerCount; i++) {
        var raw = f.GetRawInput(i);
        if (raw == null) {
          Log.Error($"Got null input for player {i}");
        } else {
          f.SetPlayerInput(i, *(Input*)raw);
        }
      }
    }

    Boolean ReadInputFromStream(out Input input) {
      try {
        input = Input.Read(_inputSerializerRead);
        return true;
      } catch {
        input = default(Input);
        return false;
      }
    }

    void LogSimulationException(Exception exn) {
      Log.Error("## Simulation Code Threw Exception ##");
      Log.Exception(exn);
    }

    public byte[] GetExtraErrorFrameDumpData(DeterministicFrame frame) {
      using (var stream = new MemoryStream()) {
        using (var writer = new BinaryWriter(stream)) {
          var data = new ChecksumErrorFrameDumpContext(this, (Frame)frame);
          data.Serialize(this, writer);
        }
        return stream.ToArray();
      }
    }
  }
}

// Game/QuantumGame.ReplayTools.cs

namespace Quantum {
  public partial class QuantumGame {

    public InputProvider RecordedInputs { get; private set; }
    public ChecksumFile RecordedChecksums { get; private set; }

    ChecksumFile _checksumsToVerify;

    [Obsolete("No longer needed. Just use File.WriteAllBytes(path, serializer.SerializeAssets(assets))")]
    public static void ExportDatabase(IEnumerable<AssetObject> assets, IAssetSerializer serializer, string folderPath, int serializationBufferSize, string dbExtension = ".json") {
      var filePath = Path.Combine(folderPath, "db" + dbExtension);
      File.WriteAllBytes(filePath, serializer.SerializeAssets(assets));
    }

    [Obsolete("Use GetInstantReplaySnapshot(int)")]
    public Frame GetRecordedSnapshot(int frame) {
      return GetInstantReplaySnapshot(frame);
    }

    public Frame GetInstantReplaySnapshot(int frame) {
      if (!_instantReplaySnapshotsRecording) {
        Log.Error("Can't find any recorded snapshots. Use StartRecordingSnapshots to start recording.");
        return null;
      }

      var buffer = (_commonSnapshotInterval > 0 ? _checksumSnapshotBuffer : _instantReplaySnapshotBuffer);
      Assert.Check(buffer != null);

      var result = buffer.Find(frame, DeterministicFrameSnapshotBufferFindMode.ClosestLessThanOrEqual);
      if (result == null) {
        result = buffer.Find(frame, DeterministicFrameSnapshotBufferFindMode.Closest);
        if (result == null) {
          Log.Warn("Unable to find a replay snapshot for frame {0}. No snapshots were saved.", frame);
        } else {
          Log.Warn("Unable to find a replay snapshot for frame {0} or earlier. The closest match is {1}." +
                   "Increase the max replay length.", frame, result.Number);
        }
      }

      return (Frame)result;
    }

    public void GetInstantReplaySnapshots(int startFrame, int endFrame, List<Frame> frames) {
      if (!_instantReplaySnapshotsRecording) {
        Log.Error("Can't find any recorded snapshots. Use StartRecordingSnapshots to start recording.");
        return;
      }

      var buffer = (_commonSnapshotInterval > 0 ? _checksumSnapshotBuffer : _instantReplaySnapshotBuffer);
      Assert.Check(buffer != null);

      var firstFrame = buffer.Find(startFrame, DeterministicFrameSnapshotBufferFindMode.ClosestLessThanOrEqual);
      var minFrameNumber = firstFrame?.Number ?? startFrame;

      foreach (Frame frame in buffer.Data) {
        if (frame == null) {
          continue;
        }

        if (frame.Number >= minFrameNumber && frame.Number <= endFrame)
          frames.Add(frame);
      }
    }

    public ReplayFile CreateSavegame() {
      if (Frames.Verified == null) {
        Log.Error("Cannot create a savegame. Frames verified not found.");
        return null;
      }

      return new ReplayFile {
        DeterministicConfig = Frames.Verified.SessionConfig,
        RuntimeConfig = Frames.Verified.RuntimeConfig,
        InputHistory = null,
        Length = Frames.Verified.Number,
        Frame = Frames.Verified.Serialize(DeterministicFrameSerializeMode.Serialize)
      };
    }

    public ReplayFile GetRecordedReplay() {
      if (Frames.Verified == null) {
        Log.Error("Cannot create a replay. Frames current or verified are not valid, yet.");
        return null;
      }

      if (RecordedInputs == null) {
        Log.Error("Cannot create a replay, because no recorded input was found. Use StartRecordingInput to start recording or setup RecordingFlags.");
        return null;
      }

      var verifiedFrame = Frames.Verified.Number;

      return new ReplayFile {
        DeterministicConfig = Frames.Verified.SessionConfig,
        RuntimeConfig = Frames.Verified.RuntimeConfig,
        InputHistory = RecordedInputs.ExportToList(verifiedFrame),
        Length = verifiedFrame,
        InitialFrame = Session.InitialTick,
        InitialFrameData = Session.IntitialFrameData,
      };
    }


    private void ReplayToolsOnInputConfirmed(DeterministicFrameInputTemp input) {
      if (RecordedInputs == null)
        return;
      RecordedInputs.OnInputConfirmed(this, input);
    }

    private void ReplayToolsOnGameResync() {
      _instantReplaySnapshotBuffer?.Clear();
      RecordedInputs?.Clear(Frames.Verified.Number);
      RecordedChecksums?.Clear();
    }

    private void ReplayToolsOnChecksumComputed(Int32 frame, ulong checksum) {
      if (RecordedChecksums != null) {
        RecordedChecksums.RecordChecksum(this, frame, checksum);
      }
      if (_checksumsToVerify != null) {
        _checksumsToVerify.VerifyChecksum(this, frame, checksum);
      }
    }

    public void StartRecordingInput(Int32? startFrame = null) {
      if (Session == null) {
        Log.Error("Can't start input recording, because the session is invalid. Wait for the OnGameStart callback.");
        return;
      }
      if (RecordedInputs == null) {
        if (startFrame.HasValue) {
          RecordedInputs = new InputProvider(Session.SessionConfig.PlayerCount, startFrame.Value, 60 * 60, 0);
        } else {
          // start frame is the session RollbackWindow
          RecordedInputs = new InputProvider(Session.SessionConfig);
        }
        Log.Info("QuantumGame.ReplayTools: Input recording started");
      }
    }

    public void StartRecordingChecksums() {
      if (RecordedChecksums == null) {
        RecordedChecksums = new ChecksumFile();
        Log.Info("QuantumGame.ReplayTools: Checksum recording started");
      }
    }

    public void StartVerifyingChecksums(ChecksumFile checksums) {
      if (_checksumsToVerify == null) {
        _checksumsToVerify = checksums;
        Log.Info("QuantumGame.ReplayTools: Checksum verification started");
      }
    }

    public void StartRecordingInstantReplaySnapshots() {
      if (_instantReplaySnapshotsRecording) {
        return;
      }

      if (InstantReplayConfig.LenghtSeconds <= 0 || InstantReplayConfig.SnapshotsPerSecond <= 0) {
        Assert.Check(_instantReplaySnapshotBuffer == null);
        Assert.Check(_commonSnapshotInterval <= 0);
        Log.Error($"Can't start recording replay snapshots with these settings: {InstantReplayConfig}");
        return;
      }

      _instantReplaySnapshotsRecording = true;
    }

    [Obsolete("Use StartRecordingInstantReplaySnapshots() instead and StartParameters properties instead.")]
    public void StartRecordingSnapshots(float bufferSizeSec, int snapshotFrequencyPerSec) {
    }
  }
}

// Game/QuantumGame.EventDispatcher.cs

namespace Quantum {
  public partial class QuantumGame {

    Dictionary<EventKey, bool> _eventsTriggered; 
    Queue<EventKey> _eventsConfirmationQueue;


    public int EventWaitingForConfirmationCount => _eventsConfirmationQueue.Count;

    void InitEventInvoker(Int32 size) {
      // how many events per frame without a resize
      const int EventsPerTickHeuristic = 50;
      _eventsTriggered = new Dictionary<EventKey, bool>(size * EventsPerTickHeuristic);
      _eventsConfirmationQueue = new Queue<EventKey>(size * EventsPerTickHeuristic);
    }

    void RaiseEvent(EventBase evnt) {
      try {
        evnt.Game = this;
        _eventDispatcher?.Publish(evnt);
      } catch (Exception exn) {
        Log.Error("## Event Callback Threw Exception ##");
        Log.Exception(exn);
      }
    }

    void CancelPendingEvents() {
      while (_eventsConfirmationQueue.Count > 0) {
        var key = _eventsConfirmationQueue.Dequeue();
        _eventsTriggered.Remove(key);
        InvokeOnEvent(key, false);
      }
      _eventsTriggered.Clear();
    }


    void InvokeEvents() {
      while (_context.Events.Count > 0) {
        var head = _context.Events.PopHead();
        try {
          if (head.Synced) {
            if (Session.IsFrameVerified(head.Tick)) {
              RaiseEvent(head);
            }
          } else {
            // calculate hash code
            var key = new EventKey(head.Tick, head.Id, head.GetHashCode());

            // if frame is verified, CONFIRM the event in the temp collection of hashes
            bool confirmed = Session.IsFrameVerified(head.Tick);

            // if this was already raised, do nothing
            if (!_eventsTriggered.TryGetValue(key, out var alreadyConfirmed)) {
              // dont trigger this again
              _eventsTriggered.Add(key, confirmed);
              // trigger event
              RaiseEvent(head);
              // enqueue confirmation
              _eventsConfirmationQueue.Enqueue(key);
            } else if (confirmed && !alreadyConfirmed) {
              // confirm this event is definitive...
              _eventsTriggered[key] = confirmed;
            }
          }
        } finally {
          _context.ReleaseEvent(head);
        }
      }

      // invoke confirmed/canceled event callbacks
      while (_eventsConfirmationQueue.Count > 0) {

        var key = _eventsConfirmationQueue.Peek();

        // need to wait; this will block confirmations from resimulations to maintain order
        if (!Session.IsFrameVerified(key.Tick)) {
          Assert.Check(key.Tick <= Session.FrameVerified.Number + Session.RollbackWindow);
          break;
        }

        var confirmed = _eventsTriggered[key];
        _eventsTriggered.Remove(key);
        _eventsConfirmationQueue.Dequeue();

        InvokeOnEvent(key, confirmed);
      }
    }
  }
}

// Game/QuantumGameCallbacks.cs

namespace Quantum {

  public enum CallbackId {
    PollInput,
    GameStarted,
    GameResynced,
    GameDestroyed,
    UpdateView,
    SimulateFinished,
    EventCanceled,
    EventConfirmed,
    ChecksumError,
    ChecksumErrorFrameDump,
    InputConfirmed,
    ChecksumComputed,
    PluginDisconnect,
    UserCallbackIdStart,
  }

  /// <summary>
  /// Callback called when the simulation queries local input.
  /// </summary>
  public sealed class CallbackPollInput : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.PollInput;
    internal CallbackPollInput(QuantumGame game) : base(ID, game) { }

    public Int32 Frame;
    public Int32 Player;

    public void SetInput(Input input, DeterministicInputFlags flags) {
      IsInputSet = true;
      Input = input;
      Flags = flags;
    }

    public void SetInput(QTuple<Input, DeterministicInputFlags> input) {
      SetInput(input.Item0, input.Item1);
    }

    public bool IsFirstInThisUpdate { get; internal set; }
    public bool IsInputSet { get; internal set; }
    public Input Input { get; private set; }
    public DeterministicInputFlags Flags { get; private set; }
  }

  /// <summary>
  /// Callback called when the game has been started.
  /// </summary>
  public sealed class CallbackGameStarted : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.GameStarted;
    internal CallbackGameStarted(QuantumGame game) : base(ID, game) { }
  }

  /// <summary>
  /// Callback called when the game has been re-synchronized from a snapshot.
  /// </summary>
  public sealed class CallbackGameResynced : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.GameResynced;
    internal CallbackGameResynced(QuantumGame game) : base(ID, game) { }
  }

  /// <summary>
  /// Callback called when the game was destroyed.
  /// </summary>
  public sealed class CallbackGameDestroyed : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.GameDestroyed;
    internal CallbackGameDestroyed(QuantumGame game) : base(ID, game) { }
  }

  /// <summary>
  /// Callback guaranteed to be called every rendered frame.
  /// </summary>
  public sealed class CallbackUpdateView : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.UpdateView;
    internal CallbackUpdateView(QuantumGame game) : base(ID, game) { }
  }

  /// <summary>
  /// Callback called when frame simulation has completed.
  /// </summary>
  public sealed class CallbackSimulateFinished : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.SimulateFinished;
    internal CallbackSimulateFinished(QuantumGame game) : base(ID, game) { }

    public Frame Frame;
  }

  /// <summary>
  /// Callback called when an event raised in a predicted frame was canceled in a verified frame due to a roll-back / missed prediction.
  /// Synchronised events are only raised on verified frames and thus will never be canceled; this is useful to graciously discard non-sync'ed events in the view.
  /// </summary>
  public sealed class CallbackEventCanceled : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.EventCanceled;
    internal CallbackEventCanceled(QuantumGame game) : base(ID, game) { }

    public EventKey EventKey;
  }

  /// <summary>
  /// Callback called when an event was confirmed by a verified frame.
  /// </summary>
  public sealed class CallbackEventConfirmed : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.EventConfirmed;
    internal CallbackEventConfirmed(QuantumGame game) : base(ID, game) { }

    public EventKey EventKey;
  }

  /// <summary>
  /// Callback called on a checksum error.
  /// </summary>
  public sealed class CallbackChecksumError : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.ChecksumError;
    internal CallbackChecksumError(QuantumGame game) : base(ID, game) { }

    public DeterministicTickChecksumError Error;
    internal DeterministicFrame[] _rawFrames;
    internal Frame[] _convertedFrame;

    public int FrameCount => Frames.Length;
    public Frame GetFrame(int index) => (Frame)Frames[index];

    public Frame[] Frames {
      get {
        if (_convertedFrame == null) {
          _convertedFrame = new Frame[_rawFrames.Length];
          for (int i = 0; i < _rawFrames.Length; ++i) {
            _convertedFrame[i] = (Frame)_rawFrames[i];
          }
        }
        return _convertedFrame;
      }
    }
  }

  /// <summary>
  /// Callback called when due to a checksum error a frame is dumped.
  /// </summary>
  public sealed class CallbackChecksumErrorFrameDump : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.ChecksumErrorFrameDump;
    internal CallbackChecksumErrorFrameDump(QuantumGame game) : base(ID, game) { }

    public Int32 ActorId;
    public Int32 FrameNumber;
    public Byte[] FrameData;
    public Byte[] RuntimeConfigBytes;
    public Byte[] ExtraBytes;
    public DeterministicSessionConfig SessionConfig;

    private Frame _frameToOverride;

    private Byte[] _overridenFrameData;
    private SimulationConfig _overridenSimulationConfig;
    private DeterministicSessionConfig _overridenSessionConfig;
    private RuntimeConfig _overridenRuntimeConfig;

    private QTuple<bool, string> _frameDump;
    private QTuple<bool, Frame> _frame;
    private QTuple<bool, RuntimeConfig> _runtimeConfig;
    private QTuple<bool, ChecksumErrorFrameDumpContext> _context;

    internal void Clear() {

      try {
        if (_overridenRuntimeConfig != null) {
          _frameToOverride.RuntimeConfig = _overridenRuntimeConfig;
        }
        if (_overridenSessionConfig != null) {
          _frameToOverride.SessionConfig = _overridenSessionConfig;
        }

        if (_overridenSimulationConfig != null) {
          _frameToOverride.SimulationConfig = _overridenSimulationConfig;
        }

        if (_overridenFrameData != null) {
          _frameToOverride.Deserialize(_overridenFrameData);
        }
      } finally {
        _frameToOverride = null;
        _overridenFrameData = null;
        _overridenSimulationConfig = null;
        _overridenSessionConfig = null;
        _overridenRuntimeConfig = null;

        _runtimeConfig = default;
        _context = default;
        _frame = default;
        _frameDump = default;
        SessionConfig = null;
      }
    }

    public Frame Frame {
      get {
        if (!_frame.Item0) {
          _frame = QTuple.Create(true, (Frame)null);
          if (_frameToOverride != null) {
            var originalFrameData = _frameToOverride.Serialize(DeterministicFrameSerializeMode.Serialize);
            try {
              _frameToOverride.Deserialize(FrameData);
              _frame = QTuple.Create(true, _frameToOverride);
              _overridenFrameData = originalFrameData;
            } catch (System.Exception ex) {
              // revert to the old data
              Log.Warn($"Failed to deserilize dump frame. The snapshot will appear as raw data.\n{ex}");
              _frameToOverride.Deserialize(originalFrameData);
            }

            _overridenRuntimeConfig = _frameToOverride.RuntimeConfig;
            _overridenSessionConfig = _frameToOverride.SessionConfig;
            _overridenSimulationConfig = _frameToOverride.SimulationConfig;
            _frameToOverride.SessionConfig = SessionConfig;

            if (RuntimeConfig != null) {
              _frameToOverride.RuntimeConfig = RuntimeConfig;
            }

            if (SimulationConfig != null) {
              _frameToOverride.SimulationConfig = SimulationConfig;
            }
          }
        }
        return _frame.Item1;
      }
    }

    public string FrameDump {
      get {
        if (!_frameDump.Item0) {
          if (Frame != null) {
            int dumpFlags = Frame.DumpFlag_NoHeap | Frame.DumpFlag_NoIsVerified;
            if (RuntimeConfig == null) {
              dumpFlags |= Frame.DumpFlag_NoRuntimeConfig;
            }
            if (SimulationConfig == null) {
              dumpFlags |= Frame.DumpFlag_NoSimulationConfig;
            }

            var options = Game.Configurations.Simulation.ChecksumErrorDumpOptions;
            if (options.HasFlag(SimulationConfigChecksumErrorDumpOptions.ReadableDynamicDB)) {
              dumpFlags |= Frame.DumpFlag_ReadableDynamicDB;
            }
            if (options.HasFlag(SimulationConfigChecksumErrorDumpOptions.RawFPValues)) {
              dumpFlags |= Frame.DumpFlag_PrintRawValues;
            }
            if (options.HasFlag(SimulationConfigChecksumErrorDumpOptions.ComponentChecksums)) {
              dumpFlags |= Frame.DumpFlag_ComponentChecksums;
            }

            _frameDump = QTuple.Create(true, Frame.DumpFrame(dumpFlags));

            if (Context?.AssetDBChecksums != null) {
              var sb = new StringBuilder();
              sb.Append(_frameDump.Item1);
              sb.AppendLine();
              sb.AppendLine("# RECEIVED ASSETDB CHECKSUMS");
              foreach (var entry in Context.AssetDBChecksums) {
                sb.Append(entry.Item0).Append(": ").Append(entry.Item1).AppendLine();
              }

              _frameDump = QTuple.Create(true, sb.ToString());
            }
          } else {
            unsafe {
              byte[] actualData = FrameData;
              bool wasCompressed = false;
              try {
                actualData = ByteUtils.GZipDecompressBytes(FrameData);
                wasCompressed = true;
              } catch { }

              fixed (byte* p = actualData) {
                var printer = new FramePrinter();
                printer.AddLine($"#### RAW FRAME DUMP (was compressed: {wasCompressed}) ####");
                printer.ScopeBegin();
                UnmanagedUtils.PrintBytesHex(p, FrameData.Length, 32, printer);
                printer.ScopeEnd();
                _frameDump = QTuple.Create(true, printer.ToString());
              }
            }
          }
        }

        return _frameDump.Item1;
      }
    }


    public RuntimeConfig RuntimeConfig {
      get {
        if (!_runtimeConfig.Item0) {
          try {
            _runtimeConfig = QTuple.Create(true, RuntimeConfig.FromByteArray(RuntimeConfigBytes));
          } catch (Exception ex) {
            Log.Exception(ex);
            _runtimeConfig = QTuple.Create(true, (RuntimeConfig)null);
          }
        }
        return _runtimeConfig.Item1;
      }
    }

    public SimulationConfig SimulationConfig => Context?.SimulationConfig;

    internal void Init(Frame frame) {
      _frameToOverride = frame;
    }

    public ChecksumErrorFrameDumpContext Context {
      get {
        if (!_context.Item0) {
          try {
            using (var reader = new BinaryReader(new MemoryStream(ExtraBytes))) {
              _context = QTuple.Create(true, ChecksumErrorFrameDumpContext.Deserialize(Game, reader));
            }
          } catch (Exception ex) {
            Log.Exception(ex);
            _context = QTuple.Create(true, (ChecksumErrorFrameDumpContext)null);
          }
        }
        return _context.Item1;
      }
    }
  }

  /// <summary>
  /// Callback when local input was confirmed.
  /// </summary>
  public sealed class CallbackInputConfirmed : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.InputConfirmed;
    internal CallbackInputConfirmed(QuantumGame game) : base(ID, game) { }
    public DeterministicFrameInputTemp Input;
  }

  /// <summary>
  /// Callback called when a checksum has been computed.
  /// </summary>
  public sealed class CallbackChecksumComputed : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.ChecksumComputed;
    internal CallbackChecksumComputed(QuantumGame game) : base(ID, game) { }

    public Int32 Frame;
    public UInt64 Checksum;
  }

  /// <summary>
  /// Callback called when the local client is disconnected by the plugin.
  /// </summary>
  public sealed class CallbackPluginDisconnect : QuantumGame.CallbackBase {
    public new const Int32 ID = (int)CallbackId.PluginDisconnect;
    internal CallbackPluginDisconnect(QuantumGame game) : base(ID, game) { }

    public string Reason;
  }

  partial class QuantumGame {

    public class CallbackBase : Quantum.CallbackBase {
      public new QuantumGame Game {
        get => (QuantumGame)base.Game;
        set => base.Game = value;
      }

      public CallbackBase(int id, QuantumGame game) : base(id, game) {
      }


      public static Type GetCallbackType(CallbackId id) {
        switch (id) {
          case CallbackId.ChecksumComputed: return typeof(CallbackChecksumComputed);
          case CallbackId.ChecksumError: return typeof(CallbackChecksumError);
          case CallbackId.ChecksumErrorFrameDump: return typeof(CallbackChecksumErrorFrameDump);
          case CallbackId.EventCanceled: return typeof(CallbackEventCanceled);
          case CallbackId.EventConfirmed: return typeof(CallbackEventConfirmed);
          case CallbackId.GameDestroyed: return typeof(CallbackGameDestroyed);
          case CallbackId.GameStarted: return typeof(CallbackGameStarted);
          case CallbackId.InputConfirmed: return typeof(CallbackInputConfirmed);
          case CallbackId.PollInput: return typeof(CallbackPollInput);
          case CallbackId.SimulateFinished: return typeof(CallbackSimulateFinished);
          case CallbackId.UpdateView: return typeof(CallbackUpdateView);
          case CallbackId.PluginDisconnect: return typeof(CallbackPluginDisconnect);
          default: throw new ArgumentOutOfRangeException(nameof(id));
        }
      }
    }

    // callback objects
    private CallbackChecksumComputed _callbackChecksumComputed;
    private CallbackChecksumError _callbackChecksumError;
    private CallbackChecksumErrorFrameDump _callbackChecksumErrorFrameDump;
    private CallbackEventCanceled _callbackEventCanceled;
    private CallbackEventConfirmed _callbackEventConfirmed;
    private CallbackGameDestroyed _callbackGameDestroyed;
    private CallbackGameStarted _callbackGameStarted;
    private CallbackGameResynced _callbackGameResynced;
    private CallbackInputConfirmed _callbackInputConfirmed;
    private CallbackPollInput _callbackPollInput;
    private CallbackSimulateFinished _callbackSimulateFinished;
    private CallbackUpdateView _callbackUpdateView;
    private CallbackPluginDisconnect _callbackPluginDisconnect;

    public void InitCallbacks() {
      _callbackChecksumComputed = new CallbackChecksumComputed(this);
      _callbackChecksumError = new CallbackChecksumError(this);
      _callbackChecksumErrorFrameDump = new CallbackChecksumErrorFrameDump(this);
      _callbackEventCanceled = new CallbackEventCanceled(this);
      _callbackEventConfirmed = new CallbackEventConfirmed(this);
      _callbackGameDestroyed = new CallbackGameDestroyed(this);
      _callbackGameStarted = new CallbackGameStarted(this);
      _callbackGameResynced = new CallbackGameResynced(this);
      _callbackInputConfirmed = new CallbackInputConfirmed(this);
      _callbackPollInput = new CallbackPollInput(this);
      _callbackSimulateFinished = new CallbackSimulateFinished(this);
      _callbackUpdateView = new CallbackUpdateView(this);
      _callbackPluginDisconnect = new CallbackPluginDisconnect(this);
    }

    public void InvokeOnGameEnded() {
      // not implemented 
    }

    public void InvokeOnDestroy() {
      try {
        _callbackDispatcher?.Publish(_callbackGameDestroyed);
      } catch (Exception ex) {
        Log.Exception(ex);

      }
    }

    void InvokeOnGameStart() {
      try {
        _callbackDispatcher?.Publish(_callbackGameStarted);
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }

    void InvokeOnGameResync() {
      try {
        _callbackDispatcher?.Publish(_callbackGameResynced);
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }


    QTuple<Input, DeterministicInputFlags> InvokeOnPollInput(int frame, int player, bool isFirstInThisUpdate) {

      try {
        _callbackPollInput.IsInputSet = false;
        _callbackPollInput.Frame = frame;
        _callbackPollInput.Player = player;
        _callbackPollInput.IsFirstInThisUpdate = isFirstInThisUpdate;
        _callbackDispatcher?.Publish(_callbackPollInput);
        if (_callbackPollInput.IsInputSet) {
          return QTuple.Create(_callbackPollInput.Input, _callbackPollInput.Flags);
        }
        return default;
      } catch (Exception ex) {
        Log.Exception(ex);
        return default;
      }
    }

    void InvokeOnUpdateView() {
      HostProfiler.Start("QuantumGame.InvokeOnUpdateView");
      try {
        _callbackDispatcher?.Publish(_callbackUpdateView);
      } catch (Exception ex) {
        Log.Exception(ex);
      }
      HostProfiler.End();
    }

    public void InvokeOnSimulateFinished(DeterministicFrame state) {
      HostProfiler.Start("QuantumGame.InvokeOnSimulateFinished");
      try {
        _callbackSimulateFinished.Frame = (Frame)state;
        _callbackDispatcher?.Publish(_callbackSimulateFinished);
      } catch (Exception ex) {
        Log.Exception(ex);
      }

      _callbackSimulateFinished.Frame = null;
      HostProfiler.End();
    }

    public void InvokeOnChecksumError(DeterministicTickChecksumError error, DeterministicFrame[] frames) {
      try {
        _callbackChecksumError.Error = error;
        _callbackChecksumError._rawFrames = frames;
        _callbackChecksumError._convertedFrame = null;
        try {
          _callbackDispatcher?.Publish(_callbackChecksumError);
        } finally {
          _callbackChecksumError._rawFrames = null;
          _callbackChecksumError._convertedFrame = null;
        }
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }

    public void InvokeOnChecksumComputed(Int32 frame, ulong checksum) {
      try {
        _callbackChecksumComputed.Frame = frame;
        _callbackChecksumComputed.Checksum = checksum;
        _callbackDispatcher?.Publish(_callbackChecksumComputed);
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }

    public void InvokeOnInputConfirmed(DeterministicFrameInputTemp input) {
      try {
        _callbackInputConfirmed.Input = input;
        try {
          _callbackDispatcher?.Publish(_callbackInputConfirmed);
        } finally {
          _callbackInputConfirmed.Input = default;
        }
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }

    public void InvokeOnChecksumErrorFrameDump(Int32 actorId, Int32 frameNumber, DeterministicSessionConfig sessionConfig, byte[] runtimeConfig, byte[] frameData, byte[] extraData) {
      HostProfiler.Start("QuantumGame.InvokeOnChecksumErrorFrameDump");
      try {

        // find the frame that's going to be overwritten: 
        Frame frameToOverwrite = null;

        if (_checksumSnapshotBuffer?.Capacity > 0) {
          if (_checksumSnapshotBuffer.Count == 0) {
            _checksumSnapshotBuffer.PushBack(Frames.Verified, this, _context);
          }
          frameToOverwrite = (Frame)_checksumSnapshotBuffer.PeekBack();
        } else {
          // TODO: use replay buffer maybe? or one of predicted?
          Log.Warn("Unable to acquire a frame to decode the snapshot. The snapshot will appear as raw binary data. Increase ChecksumFrameBufferSize.");
        }

        try {
          _callbackChecksumErrorFrameDump.Init(frameToOverwrite);
          _callbackChecksumErrorFrameDump.ActorId = actorId;
          _callbackChecksumErrorFrameDump.FrameNumber = frameNumber;
          _callbackChecksumErrorFrameDump.FrameData = frameData;
          _callbackChecksumErrorFrameDump.SessionConfig = sessionConfig;
          _callbackChecksumErrorFrameDump.RuntimeConfigBytes = runtimeConfig;
          _callbackChecksumErrorFrameDump.ExtraBytes = extraData;

          _callbackDispatcher?.Publish(_callbackChecksumErrorFrameDump);

        } finally {
          _callbackChecksumErrorFrameDump.Clear();
        }
      } catch (Exception ex) {
        HostProfiler.End();
        Log.Exception(ex);
      }
    }

    private void InvokeOnEvent(EventKey key, bool confirmed) {
      try {
        if (confirmed) {
          _callbackEventConfirmed.EventKey = key;
          _callbackDispatcher?.Publish(_callbackEventConfirmed);
        } else {
          // call event cancelation, passing: game (this), frame (f), event hash...
          // also pass the index from eventCollection (trhis is the event type ID);
          _callbackEventCanceled.EventKey = key;
          _callbackDispatcher?.Publish(_callbackEventCanceled);
        }
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }

    public void InvokeOnPluginDisconnect(string reason) {
      try {
        _callbackPluginDisconnect.Reason = reason;
        _callbackDispatcher?.Publish(_callbackPluginDisconnect);
      } catch (Exception ex) {
        Log.Exception(ex);
      }
    }
  }
}

// Replay/DotNetTaskRunner.cs

namespace Quantum {
  public class DotNetTaskRunner : IDeterministicPlatformTaskRunner {
    int _length;
    bool[] _done = new bool[128];
    public void Schedule(Action[] delegates) {
      // store how many we're executing
      _length = delegates.Length;
      // clear current state
      Array.Clear(_done, 0, _done.Length);
      // barrier this
      Thread.MemoryBarrier();
      // queue work
      for (int i = 0; i < delegates.Length; ++i) {
        ThreadPool.QueueUserWorkItem(Wrap(i, delegates[i]));
      }
    }
    public void WaitForComplete() {
      throw new NotImplementedException();
    }
    public bool PollForComplete() {
      for (int i = 0; i < _length; ++i) {
        if (Volatile.Read(ref _done[i]) == false) {
          return false;
        }
      }
      return true;
    }
    WaitCallback Wrap(int index, Action callback) {
      return _ => {
        try {
          Photon.Deterministic.Assert.Check(Volatile.Read(ref _done[index]) == false);
          callback();
        } catch (Exception exn) {
          Log.Exception(exn);
        } finally {
          Volatile.Write(ref _done[index], true);
        }
      };
    }
  }
}

// Replay/FlatEntityPrototypeContainer.cs

namespace Quantum.Prototypes {
  [Serializable]
  public partial class FlatEntityPrototypeContainer {
    [ArrayLength(0, 1)] public List<CharacterController2D_Prototype> CharacterController2D;
    [ArrayLength(0, 1)] public List<CharacterController3D_Prototype> CharacterController3D;
    [ArrayLength(0, 1)] public List<NavMeshAvoidanceAgent_Prototype> NavMeshAvoidanceAgent;
    [ArrayLength(0, 1)] public List<NavMeshAvoidanceObstacle_Prototype> NavMeshAvoidanceObstacle;
    [ArrayLength(0, 1)] public List<NavMeshPathfinder_Prototype> NavMeshPathfinder;
    [ArrayLength(0, 1)] public List<NavMeshSteeringAgent_Prototype> NavMeshSteeringAgent;
    [ArrayLength(0, 1)] public List<PhysicsBody2D_Prototype> PhysicsBody2D;
    [ArrayLength(0, 1)] public List<PhysicsBody3D_Prototype> PhysicsBody3D;
    [ArrayLength(0, 1)] public List<PhysicsCollider2D_Prototype> PhysicsCollider2D;
    [ArrayLength(0, 1)] public List<PhysicsCollider3D_Prototype> PhysicsCollider3D;
    [ArrayLength(0, 1)] public List<PhysicsCallbacks2D_Prototype> PhysicsCallbacks2D;
    [ArrayLength(0, 1)] public List<PhysicsCallbacks3D_Prototype> PhysicsCallbacks3D;
    [ArrayLength(0, 1)] public List<Transform2D_Prototype> Transform2D;
    [ArrayLength(0, 1)] public List<Transform2DVertical_Prototype> Transform2DVertical;
    [ArrayLength(0, 1)] public List<Transform3D_Prototype> Transform3D;
    [ArrayLength(0, 1)] public List<View_Prototype> View;
    [ArrayLength(0, 1)] public List<PhysicsJoints2D_Prototype> PhysicsJoints2D;
    [ArrayLength(0, 1)] public List<PhysicsJoints3D_Prototype> PhysicsJoints3D;

    public void Collect(List<ComponentPrototype> target) {
      Collect(CharacterController2D, target);
      Collect(CharacterController3D, target);
      Collect(NavMeshAvoidanceAgent, target);
      Collect(NavMeshAvoidanceObstacle, target);
      Collect(NavMeshPathfinder, target);
      Collect(NavMeshSteeringAgent, target);
      Collect(PhysicsBody2D, target);
      Collect(PhysicsBody3D, target);
      Collect(PhysicsCollider2D, target);
      Collect(PhysicsCollider3D, target);
      Collect(PhysicsCallbacks2D, target);
      Collect(PhysicsCallbacks3D, target);
      Collect(Transform2D, target);
      Collect(Transform2DVertical, target);
      Collect(Transform3D, target);
      Collect(View, target);
      Collect(PhysicsJoints2D, target);
      Collect(PhysicsJoints3D, target);
      CollectGen(target);
    }

    public void Store(IList<ComponentPrototype> prototypes) {
      var visitor = new FlatEntityPrototypeContainer.StoreVisitor() {
        Storage = this
      };

      foreach (var prototype in prototypes) {
        prototype.Dispatch(visitor);
      }
    }

    public unsafe partial class StoreVisitor : ComponentPrototypeVisitor {
      public FlatEntityPrototypeContainer Storage;
      public override void Visit(CharacterController2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.CharacterController2D);
      }
      public override void Visit(CharacterController3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.CharacterController3D);
      }
      public override void Visit(NavMeshAvoidanceAgent_Prototype prototype) {
        Storage.Store(prototype, ref Storage.NavMeshAvoidanceAgent);
      }
      public override void Visit(NavMeshAvoidanceObstacle_Prototype prototype) {
        Storage.Store(prototype, ref Storage.NavMeshAvoidanceObstacle);
      }
      public override void Visit(NavMeshPathfinder_Prototype prototype) {
        Storage.Store(prototype, ref Storage.NavMeshPathfinder);
      }
      public override void Visit(NavMeshSteeringAgent_Prototype prototype) {
        Storage.Store(prototype, ref Storage.NavMeshSteeringAgent);
      }
      public override void Visit(PhysicsBody2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsBody2D);
      }
      public override void Visit(PhysicsBody3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsBody3D);
      }
      public override void Visit(PhysicsCollider2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsCollider2D);
      }
      public override void Visit(PhysicsCollider3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsCollider3D);
      }
      public override void Visit(PhysicsCallbacks2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsCallbacks2D);
      }
      public override void Visit(PhysicsCallbacks3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsCallbacks3D);
      }
      public override void Visit(Transform2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.Transform2D);
      }
      public override void Visit(Transform2DVertical_Prototype prototype) {
        Storage.Store(prototype, ref Storage.Transform2DVertical);
      }
      public override void Visit(Transform3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.Transform3D);
      }
      public override void Visit(View_Prototype prototype) {
        Storage.Store(prototype, ref Storage.View);
      }
      public override void Visit(PhysicsJoints2D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsJoints2D);
      }

      public override void Visit(PhysicsJoints3D_Prototype prototype) {
        Storage.Store(prototype, ref Storage.PhysicsJoints3D);
      }
    }

    partial void CollectGen(List<ComponentPrototype> target);

    private void Collect<TPrototype>(List<TPrototype> source, List<ComponentPrototype> destination) where TPrototype : ComponentPrototype {
      if (source == null)
        return;
      for (int i = 0; i < source.Count; ++i)
        destination.Add(source[i]);
    }

    private void Store<T>(T value, ref List<T> destination) { 
      Assert.Check(value.GetType() == typeof(T));
      if (destination == null)
        destination = new List<T>(1);
      destination.Add(value);
    }
  }
}


// Replay/InactiveTaskRunner.cs

namespace Quantum {
  public class InactiveTaskRunner : IDeterministicPlatformTaskRunner {
    public void Schedule(Action[] delegates) { }

    public void WaitForComplete() { }

    public bool PollForComplete() {
      return true;
    }
  }
}

// Replay/JsonAssetSerializerBase.cs

namespace Quantum {
  public abstract class JsonAssetSerializerBase : IAssetSerializer {
    private List<ComponentPrototype> _prototypeBuffer = new List<ComponentPrototype>();

    public bool IsPrettyPrintEnabled { get; set; } = false;

    /// <summary>
    /// If set to a positive value, all uncompressed BinaryData assets with size over the value will be compressed
    /// during serialization.
    /// </summary>
    public int CompressBinaryDataOnSerializationThreshold { get; set; } = 1024;

    /// <summary>
    /// If true, all compressed BinaryData assets will be decompressed during deserialization.
    /// </summary>
    public bool DecompressBinaryDataOnDeserialization { get; set; }

    public Encoding Encoding => Encoding.UTF8;

    public byte[] SerializeReplay(ReplayFile replay) {
      var json = ToJson(replay);
      return Encoding.GetBytes(json);
    }

    public ReplayFile DeserializeReplay(byte[] data) {
      var json = Encoding.GetString(data);
      return (ReplayFile)FromJson(json, typeof(ReplayFile));
    }

    public byte[] SerializeChecksum(ChecksumFile checksums) {
      var json = ToJson(checksums);
      return Encoding.GetBytes(json);
    }

    public ChecksumFile DeserializeChecksum(byte[] data) {
      var json = Encoding.GetString(data);
      return (ChecksumFile)FromJson(json, typeof(ChecksumFile));
    }

    public byte[] SerializeAssets(IEnumerable<AssetObject> assets) {
      FlatDatabaseFile db = new FlatDatabaseFile();
      List<UserAssetSurrogate> userAssets = new List<UserAssetSurrogate>();

      var visitor = new AssetVisitor() { 
        Storage = db,
        Serializer = this
      };

      Assembly executingAssembly = typeof(JsonAssetSerializerBase).Assembly;

      foreach (var asset in assets) {
        if (asset is IBuiltInAssetObject builtInAsset) {
          builtInAsset.Dispatch(visitor);
        } else {
          var assetType = asset.GetType();
          var surrogate = new UserAssetSurrogate() {
            Type = (assetType.Assembly == executingAssembly) ? assetType.FullName : assetType.AssemblyQualifiedName,
            Json = ToJson(asset),
          };
          userAssets.Add(surrogate);
        }
      }

      db.UserAssets = userAssets;

      var json = ToJson(db);
      return Encoding.GetBytes(json);
    }


    public string PrintAsset(AssetObject asset) {

      object objectToSerialize = asset;
      if ( asset is EntityPrototype ep ) {
        objectToSerialize = CreateSurrogate(ep);
      } else if ( asset is Map map) {
        objectToSerialize = CreateSurrogate(map);
      }

      return ToJson(objectToSerialize);
    }

    public IEnumerable<AssetObject> DeserializeAssets(byte[] data) => IAssetSerializerExtensions.DeserializeAssets(this, data);

    public IEnumerable<AssetObject> DeserializeAssets(byte[] data, int index, int count) {
      string json = Encoding.UTF8.GetString(data, index, count);
      var db = (FlatDatabaseFile)FromJson(json, typeof(FlatDatabaseFile));

      List<AssetObject> result = new List<AssetObject>();
      Collect(db.CharacterController2DConfig, result);
      Collect(db.CharacterController3DConfig, result);
      Collect(db.EntityView, result);
      Collect(db.NavMesh, result);
      Collect(db.NavMeshAgentConfig, result);
      Collect(db.PhysicsMaterial, result);
      Collect(db.PolygonCollider, result);
      Collect(db.TerrainCollider, result);

      if (db.EntityPrototype != null) {
        foreach (var surrogate in db.EntityPrototype) {
          result.Add(CreateFromSurrogate(surrogate));
        }
      }

      if (db.Map != null) {
        foreach (var surrogate in db.Map) {
          result.Add(CreateFromSurrogate(surrogate));
        }
      }

      if (db.BinaryData != null) {
        foreach (var surrogate in db.BinaryData) {
          result.Add(CreateFromSurrogate(surrogate));
        }
      }

      if (db.UserAssets != null) {
        foreach (var surrogate in db.UserAssets) {
          var type = Type.GetType(surrogate.Type, true);
          var asset = (AssetObject)FromJson(surrogate.Json, type);
          result.Add(asset);
        }
      }

      return result;
    }

    protected void Collect<AssetType>(List<AssetType> source, List<AssetObject> destination) where AssetType : AssetObject {
      if (source == null)
        return;
      for (int i = 0; i < source.Count; ++i)
        destination.Add(source[i]);
    }

    protected abstract object FromJson(string json, Type type);

    protected abstract string ToJson(object obj);

    private static EntityPrototypeSurrogate CreateSurrogate(EntityPrototype asset) {
      var visitor = new FlatEntityPrototypeContainer.StoreVisitor() {
        Storage = new FlatEntityPrototypeContainer()
      };

      foreach (var prototype in asset.Container.Components) {
        prototype.Dispatch(visitor);
      }

      return new EntityPrototypeSurrogate() {
        Identifier = asset.Identifier,
        Container = visitor.Storage
      };
    }

    private static MapSurrogate CreateSurrogate(Map asset) {
      var mapEntities = new FlatEntityPrototypeContainer[asset.MapEntities.Length];

      var visitor = new FlatEntityPrototypeContainer.StoreVisitor();

      for (int i = 0; i < mapEntities.Length; ++i) {
        visitor.Storage = mapEntities[i] = new FlatEntityPrototypeContainer();
        foreach (var prototype in asset.MapEntities[i].Components) {
          prototype.Dispatch(visitor);
        }
      }

      return new MapSurrogate() {
        Map = asset,
        MapEntities = mapEntities,
      };
    }

    

    private EntityPrototype CreateFromSurrogate(EntityPrototypeSurrogate surrogate) {
      try {
        Assert.Check(_prototypeBuffer.Count == 0);
        surrogate.Container.Collect(_prototypeBuffer);
        return new EntityPrototype() {
          Identifier = surrogate.Identifier,
          Container = new EntityPrototypeContainer() {
            Components = _prototypeBuffer.ToArray()
          }
        };
      } finally {
        _prototypeBuffer.Clear();
      }
    }

    private Map CreateFromSurrogate(MapSurrogate surrogate) {
      Assert.Check(_prototypeBuffer.Count == 0);

      var entityCount = surrogate.MapEntities.Length;
      var mapEntities = new EntityPrototypeContainer[entityCount];

      for (int i = 0; i < entityCount; ++i) {
        try {
          surrogate.MapEntities[i].Collect(_prototypeBuffer);
          mapEntities[i] = new EntityPrototypeContainer() {
            Components = _prototypeBuffer.ToArray()
          };
        } finally {
          _prototypeBuffer.Clear();
        }
      }

      var map = surrogate.Map;
      map.MapEntities = mapEntities;
      return map;
    }

    private BinaryDataSurrogate CreateSurrogate(BinaryData asset) {

      byte[] data = asset.Data ?? Array.Empty<byte>();
      bool isCompressed = asset.IsCompressed;

      if (!asset.IsCompressed && CompressBinaryDataOnSerializationThreshold > 0 && data.Length >= CompressBinaryDataOnSerializationThreshold) {
        data = ByteUtils.GZipCompressBytes(data);
        isCompressed = true;
      }

      return new BinaryDataSurrogate() {
        Identifier = asset.Identifier,
        Base64Data = ByteUtils.Base64Encode(data),
        IsCompressed = isCompressed,
      };
    }

    private BinaryData CreateFromSurrogate(BinaryDataSurrogate surrogate) {

      var result = new BinaryData() {
        Identifier = surrogate.Identifier,
        Data = ByteUtils.Base64Decode(surrogate.Base64Data),
        IsCompressed = surrogate.IsCompressed,
      };

      if (surrogate.IsCompressed && DecompressBinaryDataOnDeserialization) {
        result.IsCompressed = false;
        result.Data = ByteUtils.GZipDecompressBytes(result.Data);
      }

      return result;
    }

    [Serializable]
    public class EntityPrototypeSurrogate {
      public FlatEntityPrototypeContainer Container;
      public AssetObjectIdentifier Identifier;
    }

    [Serializable]
    public class MapSurrogate {
      public Map Map;
      public FlatEntityPrototypeContainer[] MapEntities;
    }

    [Serializable]
    public class UserAssetSurrogate {
      public string Json;
      public string Type;
    }

    [Serializable]
    public class BinaryDataSurrogate {
      public AssetObjectIdentifier Identifier;
      public bool IsCompressed;
      public string Base64Data;
    }

    private class AssetVisitor : IAssetObjectVisitor {
      public FlatDatabaseFile Storage;
      public JsonAssetSerializerBase Serializer;

      void IAssetObjectVisitor.Visit(BinaryData asset) {
        Storage.BinaryData.Add(Serializer.CreateSurrogate(asset));
      }

      void IAssetObjectVisitor.Visit(CharacterController2DConfig asset) {
        Storage.CharacterController2DConfig.Add(asset);
      }

      void IAssetObjectVisitor.Visit(CharacterController3DConfig asset) {
        Storage.CharacterController3DConfig.Add(asset);
      }

      void IAssetObjectVisitor.Visit(EntityPrototype asset) {
        Storage.EntityPrototype.Add(CreateSurrogate(asset));
      }

      void IAssetObjectVisitor.Visit(EntityView asset) {
        Storage.EntityView.Add(asset);
      }

      void IAssetObjectVisitor.Visit(Map asset) {
        Storage.Map.Add(CreateSurrogate(asset));
      }

      void IAssetObjectVisitor.Visit(NavMesh asset) {
        Storage.NavMesh.Add(asset);
      }

      void IAssetObjectVisitor.Visit(NavMeshAgentConfig asset) {
        Storage.NavMeshAgentConfig.Add(asset);
      }

      void IAssetObjectVisitor.Visit(PhysicsMaterial asset) {
        Storage.PhysicsMaterial.Add(asset);
      }

      void IAssetObjectVisitor.Visit(PolygonCollider asset) {
        Storage.PolygonCollider.Add(asset);
      }

      void IAssetObjectVisitor.Visit(TerrainCollider asset) {
        Storage.TerrainCollider.Add(asset);
      }
    }

    [Serializable]
    private sealed class FlatDatabaseFile {
      public List<CharacterController2DConfig> CharacterController2DConfig = new List<CharacterController2DConfig>();
      public List<CharacterController3DConfig> CharacterController3DConfig = new List<CharacterController3DConfig>();
      public List<EntityPrototypeSurrogate> EntityPrototype = new List<EntityPrototypeSurrogate>();
      public List<EntityView> EntityView = new List<EntityView>();
      public List<MapSurrogate> Map = new List<MapSurrogate>();
      public List<NavMesh> NavMesh = new List<NavMesh>();
      public List<NavMeshAgentConfig> NavMeshAgentConfig = new List<NavMeshAgentConfig>();
      public List<PhysicsMaterial> PhysicsMaterial = new List<PhysicsMaterial>();
      public List<PolygonCollider> PolygonCollider = new List<PolygonCollider>();
      public List<TerrainCollider> TerrainCollider = new List<TerrainCollider>();
      public List<UserAssetSurrogate> UserAssets = new List<UserAssetSurrogate>();
      public List<BinaryDataSurrogate> BinaryData = new List<BinaryDataSurrogate>();
    }
  }
}

// Replay/ChecksumFile.cs

namespace Quantum {

  [Serializable]
  public class ChecksumFile {
    public const int GrowSize = 60 * 60; // one minute of recording at 60 FPS

    [Serializable]
    public struct ChecksumEntry {
      public int Frame;
      // This is super annoying: Unity JSON cannot read the unsigned long data type. 
      // We can convert on this level, keeping the ULong CalculateChecksum() signature and encode the 
      // checksum as a long for serialization. Any other ideas?
      public long ChecksumAsLong;
    }

    public ChecksumEntry[] Checksums;

    private Int32 writeIndex;

    public Dictionary<int, ChecksumEntry> ToDictionary() {
      return Checksums.Where(item => item.Frame != 0).ToDictionary(item => item.Frame, item => item);
    }

    internal void RecordChecksum(QuantumGame game, Int32 frame, ulong checksum) {
      if (Checksums == null) {
        Checksums = new ChecksumEntry[GrowSize];
      }

      if (writeIndex + 1 > Checksums.Length) {
        Array.Resize(ref Checksums, Checksums.Length + GrowSize);
      }

      Checksums[writeIndex].Frame = frame;
      Checksums[writeIndex].ChecksumAsLong = ChecksumFileHelper.UlongToLong(checksum);
      writeIndex++;
    }

    internal void VerifyChecksum(QuantumGame game, Int32 frame, ulong checksum) {
      if (Checksums.Length > 0) {
        var readIndex = (frame - Checksums[0].Frame) / game.Session.SessionConfig.ChecksumInterval;
        Assert.Check(Checksums[readIndex].Frame == frame);
        if (Checksums[readIndex].ChecksumAsLong != ChecksumFileHelper.UlongToLong(checksum)) {
          Log.Error($"Checksum mismatch in frame {frame}: {Checksums[readIndex].ChecksumAsLong} != {ChecksumFileHelper.UlongToLong(checksum)}");
        }
      }
    }

    internal void Clear() {
      writeIndex = 0;
      if ( Checksums != null ) {
        for (int i = 0; i < Checksums.Length; ++i) {
          Checksums[i] = default;
        }
      }
    }
  }

  public static class ChecksumFileHelper {
    public static unsafe long UlongToLong(ulong value) {
      return *((long*)&value);
    }

    public static unsafe ulong LongToULong(long value) {
      return *((ulong*)&value);
    }
  }
}


// Replay/InputProvider.cs

namespace Quantum {
  public class InputProvider : IDeterministicReplayProvider {
    private int                         _playerCount;
    private int                         _growSize;
    private int                         _startFrame;
    private DeterministicTickInputSet[] _inputs;

    private int MaxFrame => _inputs.Length + _startFrame;

    public InputProvider(DeterministicSessionConfig config, int capacity = 60 * 60, int growSize = 0) : this(config.PlayerCount, config.RollbackWindow, capacity, growSize) {
    }

    [Obsolete("Use 'InputProvider(DeterministicTickInputSet[])' instead.")]
    public InputProvider(DeterministicSessionConfig config, DeterministicTickInputSet[] inputList) : this(inputList) {}

    public InputProvider(DeterministicTickInputSet[] inputList) {
      ImportFromList(inputList);
    }

    public InputProvider(int playerCount, int startFrame, int capacity, int growSize) {
      _playerCount = playerCount;
      _startFrame  = startFrame;
      _growSize    = growSize;

      if (capacity > 0) {
        Allocate(capacity);
      }
    }

    public void Clear(int startFrame) {
      _startFrame = startFrame;
      for (int i = 0; i < _inputs.Length; i++) {
        _inputs[i].Tick = i + _startFrame;
        for (int j = 0; j < _playerCount; j++) {
          _inputs[i].Inputs[j].Clear();
        }
      }
    }

    [Obsolete("Use 'ImportFromList(DeterministicTickInputSet[])' instead.")]
    public void ImportFromList(DeterministicTickInputSet[] inputList, int startFrame) {
      // all InputProvider features expect the first input on the list to be the starting frame,
      // setting it to a different number will result in misbehavior
      ImportFromList(inputList);
    }

    public void ImportFromList(DeterministicTickInputSet[] inputList) {
      _startFrame = inputList.Length == 0 ? 0 : inputList[0].Tick;

      // Use external list as our own
      _inputs = inputList;
      for (int i = 0; i < _inputs.Length; i++) {
        for (int j = 0; j < inputList[i].Inputs.Length; j++) {
          inputList[i].Inputs[j].Sent = true;
        }
      }
    }

    public DeterministicTickInputSet[] ExportToList(int verifiedFrame) {
      var size = _inputs.Length;
      while (size > 0 && _inputs[size - 1].Inputs.Any(x => x.Tick == 0 || x.Tick > verifiedFrame)) {
        // Truncate non-verified and incomplete input from the end
        size--;
      }

      if (size <= 0) {
        return new DeterministicTickInputSet[0];
      }

      var result = new DeterministicTickInputSet[size];
      Array.Copy(_inputs, result, size);

      return result;
    }

    public void OnInputConfirmed(QuantumGame game, DeterministicFrameInputTemp input) {
      if (input.Frame < _startFrame) {
        // if starting to record from a frame following a snapshot,
        // confirmed inputs from previous frames can still arrive
        return;
      }

      if (input.Frame >= MaxFrame) {
        var minSize  = Math.Max(input.Frame - _startFrame, _inputs.Length);
        var growSize = _growSize > 0 ? minSize + _growSize : minSize * 2;
        Allocate(growSize);
      }

      _inputs[ToIndex(input.Frame)].Inputs[input.Player].Set(input);
    }

    public void InjectInput(DeterministicTickInput input, bool localReplay) {
      if (input.Tick >= MaxFrame) {
        var minSize  = Math.Max(input.Tick - _startFrame, _inputs.Length);
        var growSize = _growSize > 0 ? minSize + _growSize : minSize * 2;
        Allocate(growSize);
      }

      _inputs[ToIndex(input.Tick)].Inputs[input.PlayerIndex].CopyFrom(input);

      if (localReplay) {
        _inputs[ToIndex(input.Tick)].Inputs[input.PlayerIndex].Sent = true;
      }
    }

    public void AddRpc(int player, byte[] data, bool command) {
    }

    public bool CanSimulate(int frame) {
      var index = ToIndex(frame);

      if (index >= 0 && index < _inputs.Length) {
        return _inputs[index].IsComplete();
      }

      return false;
    }

    public QTuple<byte[], bool> GetRpc(int frame, int player) {
      if (frame < MaxFrame) {
        return QTuple.Create(
                             _inputs[ToIndex(frame)].Inputs[player].Rpc,
                             (_inputs[ToIndex(frame)].Inputs[player].Flags & DeterministicInputFlags.Command) == DeterministicInputFlags.Command);
      }

      return default;
    }

    public DeterministicFrameInputTemp GetInput(int frame, int player) {
      if (frame < MaxFrame) {
        var input = _inputs[ToIndex(frame)].Inputs[player];
        return DeterministicFrameInputTemp.Verified(frame, player, null, input.DataArray, input.DataLength, input.Flags);
      }

      return default;
    }

    private int ToIndex(int frame) {
      return frame - _startFrame;
    }

    private void Allocate(int size) {
      var oldSize = 0;
      if (_inputs == null) {
        _inputs = new DeterministicTickInputSet[size];
      } else {
        oldSize = _inputs.Length;
        Array.Resize(ref _inputs, size);
      }

      for (int i = oldSize; i < _inputs.Length; i++) {
        _inputs[i].Tick = i + _startFrame;
        _inputs[i].Inputs = new DeterministicTickInput[_playerCount];
        for (int j = 0; j < _playerCount; j++) {
          _inputs[i].Inputs[j] = new DeterministicTickInput();
        }
      }
    }
  }

  public static class InputProviderExtensions {
    public static void CopyFrom(this DeterministicTickInput input, DeterministicTickInput otherInput) {
      input.Sent        = otherInput.Sent;
      input.Tick        = otherInput.Tick;
      input.PlayerIndex = otherInput.PlayerIndex;
      input.DataLength  = otherInput.DataLength;
      input.Flags       = otherInput.Flags;

      if (otherInput.DataArray != null) {
        input.DataArray = new byte[otherInput.DataArray.Length];
        Array.Copy(otherInput.DataArray, input.DataArray, otherInput.DataArray.Length);
      }

      if (otherInput.Rpc != null) {
        input.Rpc = new byte[otherInput.Rpc.Length];
        Array.Copy(otherInput.Rpc, input.Rpc, otherInput.Rpc.Length);
      }
    }

    public static void Clear(this DeterministicTickInput input) {
      input.Tick        = default;
      input.PlayerIndex = default;
      input.DataArray   = default;
      input.DataLength  = default;
      input.Flags       = default;
      input.Rpc         = default;
    }



    public static void Set(this DeterministicTickInput input, DeterministicFrameInputTemp temp) {
      input.Tick        = temp.Frame;
      input.PlayerIndex = temp.Player;
      input.DataArray   = temp.CloneData();
      input.DataLength  = temp.DataLength;
      input.Flags       = temp.Flags;
      input.Rpc         = temp.Rpc;
    }

    public static bool IsComplete(this DeterministicTickInputSet set) {
      for (int i = 0; i < set.Inputs.Length; i++) {
        if (set.Inputs[i].Tick == 0) {
          return false;
        }
      }

      return true;
    }

    public static bool IsFinished(this DeterministicTickInputSet set) {
      for (int i = 0; i < set.Inputs.Length; i++) {
        if (set.Inputs[i].Tick == 0 ||
            set.Inputs[i].Sent == false) {
          return false;
        }
      }

      return true;
    }
  }
}

// Replay/ReplayFile.cs

namespace Quantum {

  [Serializable]
  public class ReplayFile {
    public RuntimeConfig RuntimeConfig;
    public DeterministicSessionConfig DeterministicConfig;
    public DeterministicTickInputSet[] InputHistory;
    public Int32 Length;
    public byte[] Frame;
    public Int32 InitialFrame;
    public byte[] InitialFrameData;
  }
}


// Replay/SessionContainer.cs

namespace Quantum {
  public class SessionContainer {
    public static Boolean _loadedAllStatics = false;
    public static readonly Object _lock = new Object();

    DeterministicSessionConfig _sessionConfig;
    RuntimeConfig _runtimeConfig;
    QuantumGame _game;
    DeterministicSession _session;
    long _startGameTimeoutInMiliseconds = -1;
    DateTime _startGameTimestamp;

    public QuantumGame QuantumGame => _game;
    public IDeterministicGame Game => _game;
    public DeterministicSession Session => _session;
    public RuntimeConfig RuntimeConfig => _runtimeConfig;
    public DeterministicSessionConfig DeterministicConfig => _sessionConfig;

    /// <summary>
    /// Check this when the container reconnects into a running game and handle accordingly.
    /// </summary>
    public bool HasGameStartTimedOut => _startGameTimeoutInMiliseconds > 0 && Session != null && Session.IsPaused && DateTime.Now > _startGameTimestamp + TimeSpan.FromMilliseconds(_startGameTimeoutInMiliseconds);
    /// <summary>
    /// Default is infinity (-1). Set this when the you expect to connect to a running game and wait for a snapshot.
    /// </summary>
    public long GameStartTimeoutInMiliseconds {
      get { 
        return _startGameTimeoutInMiliseconds; 
      } 
      set {
        _startGameTimeoutInMiliseconds = value;
      }
    }

    #region Obsolete Members

    [Obsolete("Renamed to Session")]
    public DeterministicSession session;
    [Obsolete("Renamed to SessionConfig")]
    public DeterministicSessionConfig config => _sessionConfig;
    [Obsolete("Renamed to RuntimeConfig")]
    public RuntimeConfig runtimeConfig => _runtimeConfig;
    [Obsolete("Removed allocator access because it being disposed internally and unsafe to use outside")]
    public Native.Allocator allocator => null;
    [Obsolete("Removed property, use StartReplay(.., IDeterministicReplayProvider provider, ..)")]
    public IDeterministicReplayProvider provider;
    [Obsolete("Renamed to Game, changed to getter only, set by Start(QuantumGame.StartParameters startParams)")]
    public IDeterministicGame game => _game;
    [Obsolete("Removed property, set by Start(QuantumGame.StartParameters startParams)")]
    public IResourceManager resourceManager;
    [Obsolete("Removed property, set by using constructor SessionContainer(ReplayFile)")]
    public ReplayFile replayFile;
    [Obsolete("Removed property, set by Start(QuantumGame.StartParameters startParams)")]
    public IAssetSerializer assetSerializer;
    [Obsolete("Removed property, set by Start(QuantumGame.StartParameters startParams)")]
    public IEventDispatcher eventDispatcher;
    [Obsolete("Removed property, set by Start(QuantumGame.StartParameters startParams)")]
    public ICallbackDispatcher callbackDispatcher;
    [Obsolete("Removed property, set by Start(QuantumGame.StartParameters startParams)")]
    public int gameFlags;

    #endregion

    public static Native.Allocator CreateNativeAllocator() {
      switch (Environment.OSVersion.Platform) {
        case PlatformID.Unix:
        case PlatformID.MacOSX:
          return new Native.LIBCAllocator();
        default:
          return new Native.MSVCRTAllocator();
      }
    }

    public static Native.Utility CreateNativeUtils() {
      switch (Environment.OSVersion.Platform) {
        case PlatformID.Unix:
        case PlatformID.MacOSX:
          return new Native.LIBCUtility();

        default:
          return new Native.MSVCRTUtility();
      }
    }

    [Obsolete("Use Start(QuantumGame.StartParameters startParams, IDeterministicReplayProvider provider) instead of setting properties")]
    public void Start(bool logInitForConsole = true) {
      if (provider == null) {
        provider = new InputProvider(_sessionConfig);
      }

      StartReplay(new QuantumGame.StartParameters() {
        ResourceManager = resourceManager,
        AssetSerializer = assetSerializer,
        CallbackDispatcher = callbackDispatcher,
        EventDispatcher = eventDispatcher,
        GameFlags = gameFlags,
      }, provider, "server", logInitForConsole);
    }

    /// <summary>
    /// Start the simulation as a replay by providing an input provider.
    /// </summary>
    /// <param name="startParams">Game start parameters</param>
    /// <param name="provider">Input provider</param>
    /// <param name="clientId">Optional client id</param>
    /// <param name="logInitForConsole">Optionally disable setting up the console as log output (required on the Quantum plugin)</param>
    public void StartReplay(QuantumGame.StartParameters startParams, IDeterministicReplayProvider provider, string clientId = "server", bool logInitForConsole = true, IDeterministicPlatformTaskRunner taskRunner = null) {
      DeterministicSessionArgs sessionArgs;
      sessionArgs.Mode = DeterministicGameMode.Replay;
      sessionArgs.Game = null;
      sessionArgs.Replay = provider;
      sessionArgs.Communicator = null;
      sessionArgs.PlatformInfo = null;
      sessionArgs.InitialTick = 0;
      sessionArgs.FrameData = null;
      sessionArgs.SessionConfig = null;
      sessionArgs.RuntimeConfig = null;
      Start(startParams, sessionArgs, _sessionConfig.PlayerCount, clientId, logInitForConsole, taskRunner);
    }

    /// <summary>
    /// Start the simulation as a spectator.
    /// </summary>
    /// <param name="startParams">Game start parameters</param>
    /// <param name="networkCommunicator">Quantum network comunicator (has to have a peer that is connected to a room</param>
    /// <param name="frameData">Optionally the frame to start from</param>
    /// <param name="initialTick">The tick that the frame data is based on</param>
    /// <param name="clientId">Optional client id</param>
    /// <param name="logInitForConsole">Optionally disable setting up the console as log output (required on the Quantum plugin)</param>
    public void StartSpectator(QuantumGame.StartParameters startParams, ICommunicator networkCommunicator, byte[] frameData = null, int initialTick = 0, string clientId = "observer", bool logInitForConsole = true, IDeterministicPlatformTaskRunner taskRunner = null) {
      DeterministicSessionArgs sessionArgs;
      sessionArgs.Mode = DeterministicGameMode.Spectating;
      sessionArgs.Game = null;
      sessionArgs.Replay = null;
      sessionArgs.Communicator = networkCommunicator;
      sessionArgs.PlatformInfo = null;
      sessionArgs.InitialTick = initialTick;
      sessionArgs.FrameData = frameData;
      sessionArgs.SessionConfig = null;
      sessionArgs.RuntimeConfig = null;
      Start(startParams, sessionArgs, 0, clientId, logInitForConsole, taskRunner);
    }

    /// <summary>
    /// Start the simulation in a custom way.
    /// </summary>
    /// <param name="startParams">Game start parameters</param>
    /// <param name="sessionArgs">Game session args</param>
    /// <param name="playerSlots">Number of player slots</param>
    /// <param name="clientId">Optional client id</param>
    /// <param name="logInitForConsole">Optionally disable setting up the console as log output (required on the Quantum plugin)</param>
    public void Start(QuantumGame.StartParameters startParams, DeterministicSessionArgs sessionArgs, int playerSlots, string clientId = "server", bool logInitForConsole = true, IDeterministicPlatformTaskRunner taskRunner = null) {
      if (!_loadedAllStatics) {
        lock (_lock) {
          if (!_loadedAllStatics) {
            // console first
            if (logInitForConsole) {
              Log.InitForConsole();
            }

            // try to figure out platform if not set
            if (Native.Utils == null) {
              Native.Utils = CreateNativeUtils();
            }

            if (MemoryLayoutVerifier.Platform == null) {
              MemoryLayoutVerifier.Platform = new MemoryLayoutVerifier.DefaultPlatform();
            }
          }

          _loadedAllStatics = true;
        }
      }

      _game = new QuantumGame(startParams);

      DeterministicPlatformInfo info;
      info = new DeterministicPlatformInfo();
      info.Allocator = CreateNativeAllocator();
      info.Architecture = DeterministicPlatformInfo.Architectures.x86;
      info.RuntimeHost = DeterministicPlatformInfo.RuntimeHosts.PhotonServer;
      info.Runtime = DeterministicPlatformInfo.Runtimes.NetFramework;
      info.TaskRunner = taskRunner ?? new DotNetTaskRunner();

      switch (Environment.OSVersion.Platform) {
        case PlatformID.Unix:
          info.Platform = DeterministicPlatformInfo.Platforms.Linux;
          break;

        case PlatformID.MacOSX:
          info.Platform = DeterministicPlatformInfo.Platforms.OSX;
          break;

        default:
          info.Platform = DeterministicPlatformInfo.Platforms.Windows;
          break;
      }

      sessionArgs.Game = _game;
      sessionArgs.PlatformInfo = info;
      sessionArgs.SessionConfig = _sessionConfig;
      sessionArgs.RuntimeConfig = RuntimeConfig.ToByteArray(_runtimeConfig);

      _session = new DeterministicSession(sessionArgs);
      _session.Join(clientId, playerSlots);

      _startGameTimestamp = DateTime.Now;
    }

    /// <summary>
    /// Update the session.
    /// </summary>
    /// <param name="dt">Optionally provide a custom delta time</param>
    public void Service(double? dt = null) {
      _session.Update(dt);
    }

    /// <summary>
    /// Destroy the session.
    /// </summary>
    public void Destroy() {
      _session?.Destroy();
      _session = null;
    }

    /// <summary>
    /// Use other constructors that provide the session and runtime config.
    /// </summary>
    public SessionContainer() {
      _sessionConfig = null;
      _runtimeConfig = null;
    }

    public SessionContainer(ReplayFile replayFile) {
      _sessionConfig = replayFile.DeterministicConfig;
      _runtimeConfig = replayFile.RuntimeConfig;
    }

    public SessionContainer(DeterministicSessionConfig sessionConfig, RuntimeConfig runtimeConfig) {
      _sessionConfig = sessionConfig;
      _runtimeConfig = runtimeConfig;
    }
  }
}


// Replay/SessionContainer.Legacy.cs

namespace Quantum.Legacy {
  [Obsolete("Use Quantum.SessionContainer")]
  public class SessionContainer {
    DeterministicSessionConfig _sessionConfig;
    RuntimeConfig              _runtimeConfig;

    public DeterministicSession         session;
    public DeterministicSessionConfig config  => _sessionConfig ?? replayFile.DeterministicConfig;
    public RuntimeConfig runtimeConfig        => _runtimeConfig ?? replayFile.RuntimeConfig;
    public Native.Allocator allocator         => _allocator.Value;


    public IDeterministicReplayProvider provider;
    public IDeterministicGame           game;
    public IResourceManager             resourceManager;
    public ReplayFile                   replayFile;
    public IAssetSerializer             assetSerializer;
    public IEventDispatcher             eventDispatcher;
    public ICallbackDispatcher          callbackDispatcher;
    public int                          gameFlags;

    public static Boolean          _loadedAllStatics = false;
    public static Object           _lock             = new Object();

    private static Lazy<Native.Allocator> _allocator = new Lazy<Native.Allocator>(() => CreateNativeAllocator());

    public static Native.Allocator CreateNativeAllocator() {
      switch (System.Environment.OSVersion.Platform) {
        case PlatformID.Unix:
        case PlatformID.MacOSX:
          return new Native.LIBCAllocator();
        default:
          return new Native.MSVCRTAllocator();
      }
    }

    public static Native.Utility CreateNativeUtils() {
      switch (System.Environment.OSVersion.Platform) {
        case PlatformID.Unix:
        case PlatformID.MacOSX:
          return new Native.LIBCUtility();

        default:
          return new Native.MSVCRTUtility();
      }
    }

    public void Start(bool logInitForConsole = true) {
      
      if (!_loadedAllStatics) {
        lock (_lock) {
          if (!_loadedAllStatics) {
            // console first
            if (logInitForConsole) {
              Log.InitForConsole();
            }

            // try to figure out platform if not set
            if (Native.Utils == null) {
              Native.Utils = CreateNativeUtils();
            }

            if (MemoryLayoutVerifier.Platform == null) {
              MemoryLayoutVerifier.Platform = new MemoryLayoutVerifier.DefaultPlatform();
            }
          }
          _loadedAllStatics = true;
        }
      }

      game = new QuantumGame(new QuantumGame.StartParameters() {
        ResourceManager = resourceManager, 
        AssetSerializer = assetSerializer, 
        CallbackDispatcher = callbackDispatcher, 
        EventDispatcher = eventDispatcher,
        GameFlags = gameFlags,
      });

      if (provider == null) {
        provider = new InputProvider(config);
      }

      DeterministicPlatformInfo info;
      info              = new DeterministicPlatformInfo();
      info.Allocator    = allocator;
      info.Architecture = DeterministicPlatformInfo.Architectures.x86;
      info.RuntimeHost  = DeterministicPlatformInfo.RuntimeHosts.PhotonServer;
      info.Runtime      = DeterministicPlatformInfo.Runtimes.NetFramework;
      info.TaskRunner   = new DotNetTaskRunner();

      switch (System.Environment.OSVersion.Platform) {
        case PlatformID.Unix:
          info.Platform = DeterministicPlatformInfo.Platforms.Linux;
          break;

        case PlatformID.MacOSX:
          info.Platform = DeterministicPlatformInfo.Platforms.OSX;
          break;

        default:
          info.Platform = DeterministicPlatformInfo.Platforms.Windows;
          break;
      }

      DeterministicSessionArgs args;
      args.Game                  = game;
      args.Mode                  = DeterministicGameMode.Replay;
      args.Replay                = provider;
      args.FrameData             = null;
      args.Communicator          = null;
      args.InitialTick           = 0;
      args.SessionConfig         = config;
      args.PlatformInfo          = info;
      args.RuntimeConfig         = RuntimeConfig.ToByteArray(runtimeConfig);

      session = new DeterministicSession(args);
      session.Join("server", config.PlayerCount);
    }

    public void Service(double? dt = null) {
      session.Update(dt);
    }

    public void Destroy() {
      if (session != null)
        session.Destroy();
      session = null;

      //DB.Dispose();
    }

    public SessionContainer() {
      _sessionConfig = null;
      _runtimeConfig = null;
    }

    public SessionContainer(DeterministicSessionConfig sessionConfig, RuntimeConfig runtimeConfig) {
      _sessionConfig = sessionConfig;
      _runtimeConfig = runtimeConfig;
    }
  }
}


// Systems/Base/SystemArrayComponent.cs

namespace Quantum.Task {
  public abstract unsafe class SystemArrayComponent<T> : SystemBase where T : unmanaged, IComponent {
    private TaskDelegateHandle _arrayTaskDelegateHandle;

    // internal max slices
    private const int MAX_SLICES_COUNT = 32;

    public virtual int SlicesCount => MAX_SLICES_COUNT / 2;

    public sealed override void OnInit(Frame f) {
      f.Context.TaskContext.RegisterDelegate(TaskArrayComponent, GetType().Name + ".Update", ref _arrayTaskDelegateHandle);
      OnInitUser(f);
    }

    protected virtual void OnInitUser(Frame f) {

    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      var slicesCount = Math.Max(1, Math.Min(SlicesCount, MAX_SLICES_COUNT));
      return f.Context.TaskContext.AddArrayTask(_arrayTaskDelegateHandle, null, f.ComponentCount<T>(includePendingRemoval: true), taskHandle, slicesCount);
    }

    private void TaskArrayComponent(FrameThreadSafe f, int start, int count, void* arg) {
      var iterator = f.GetComponentBlockIterator<T>(start, count).GetEnumerator();
      while (iterator.MoveNext()) {
        var (entity, component) = iterator.Current;
        Update(f, entity, component);
      }
    }

    public abstract void Update(FrameThreadSafe f, EntityRef entity, T* component);
  }
}

// Systems/Base/SystemArrayFilter.cs

namespace Quantum.Task {
  public abstract unsafe class SystemArrayFilter<T> : SystemBase where T : unmanaged {
    private TaskDelegateHandle        _arrayTaskDelegateHandle;
    private ComponentFilterStructMeta _filterMeta;

    // internal max slices
    private const int MAX_SLICES_COUNT = 32;

    public virtual int SlicesCount => MAX_SLICES_COUNT / 2;

    public virtual bool UseCulling => true;

    public virtual ComponentSet Without => default;

    public virtual ComponentSet Any => default;

    public sealed override void OnInit(Frame f) {
      _filterMeta = ComponentFilterStructMeta.Create<T>();
      Assert.Check(_filterMeta.ComponentCount > 0, "Filter Struct '{0}' must have at least one component pointer.", typeof(T));
      
      f.Context.TaskContext.RegisterDelegate(TaskArrayFilter, GetType().Name + ".Update", ref _arrayTaskDelegateHandle);
      OnInitUser(f);
    }

    protected virtual void OnInitUser(Frame f) {

    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      // figure out smallest block iterator
      var taskSize = f.ComponentCount(_filterMeta.ComponentTypes[0], includePendingRemoval: true);

      for (var i = 1; i < _filterMeta.ComponentCount; ++i) {
        var otherCount = f.ComponentCount(_filterMeta.ComponentTypes[i], includePendingRemoval: true);
        if (otherCount < taskSize) {
          taskSize = otherCount;
        }
      }

      var slicesCount = Math.Max(1, Math.Min(SlicesCount, MAX_SLICES_COUNT));
      return f.Context.TaskContext.AddArrayTask(_arrayTaskDelegateHandle, null, taskSize, taskHandle, slicesCount);
    }

    private void TaskArrayFilter(FrameThreadSafe f, int start, int count, void* userData) {
      // grab iterator
      var iterator = f.FilterStruct<T>(Without, Any, start, count);

      // set culling flag
      iterator.UseCulling = UseCulling;

      var filter = default(T);

      // execute filter loop
      while (iterator.Next(&filter)) {
        Update(f, ref filter);
      }
    }

    public abstract void Update(FrameThreadSafe f, ref T filter);
  }
}

// Systems/Base/SystemBase.cs

namespace Quantum {
  public abstract partial class SystemBase {
    Int32? _runtimeIndex;
    String _scheduleSample;
    SystemBase _parentSystem;

    public Int32 RuntimeIndex {
      get {
        return (Int32)_runtimeIndex;
      }
      set {
        if (_runtimeIndex.HasValue) {
          Log.Error("Can't change systems runtime index after game has started");
        } else {
          _runtimeIndex = value;
        }
      }
    }

    public SystemBase ParentSystem {
      get {
        return _parentSystem;
      } 
      internal set {
        _parentSystem = value;
      }
    }

    public virtual IEnumerable<SystemBase> ChildSystems {
      get {
        return new SystemBase[0];
      }
    }

    public IEnumerable<SystemBase> Hierarchy {
      get {
        yield return this;

        foreach (var child in ChildSystems) {
          foreach (var childHierarchy in child.Hierarchy) {
            if (childHierarchy != null) {
              yield return childHierarchy;
            }
          }
        }
      }
    }

    public virtual Boolean StartEnabled {
      get { return true; }
    }

    public SystemBase() {
      _scheduleSample = GetType().Name + ".Schedule";
    }

    public SystemBase(string scheduleSample) {
      _scheduleSample = scheduleSample;
    }
    
    public virtual void OnInit(Frame f) {
      
    }

    public virtual void OnEnabled(Frame f) {
      
    }

    public virtual void OnDisabled(Frame f) {
      
    }

    public TaskHandle OnSchedule(Frame f, TaskHandle taskHandle) {
#if DEBUG
      var profiler = f.Context.ProfilerContext.GetProfilerForTaskThread(0);
      try {
        profiler.Start(_scheduleSample);
#endif
        
        return Schedule(f, taskHandle);
        
#if DEBUG
      } finally {
        profiler.End();
      }
#endif
    }

    protected abstract TaskHandle Schedule(Frame f, TaskHandle taskHandle);
  }
}

// Systems/Base/SystemGroup.cs

namespace Quantum {
  public unsafe class SystemGroup : SystemBase {
    SystemBase[] _children;

    public sealed override IEnumerable<SystemBase> ChildSystems {
      get { return _children; }
    }

    public SystemGroup(String name, params SystemBase[] children) : base(name + ".Schedule") {
      _children = children;

      for (int i = 0; i < _children.Length; i++) {
        _children[i].ParentSystem = this;
      }
    }

    protected sealed override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      if (_children != null) {
        for (var i = 0; i < _children.Length; ++i) {
          if (f.SystemIsEnabledSelf(_children[i])) {
            try {
              taskHandle = _children[i].OnSchedule(f, taskHandle);
            } catch (Exception exn) {
              Log.Exception(exn);
            }
          }
        }
      }

      return taskHandle;
    }

    public override void OnEnabled(Frame f) {
      base.OnEnabled(f);

      for (int i = 0; i < _children.Length; ++i) {
        if (f.SystemIsEnabledSelf(_children[i])) {
          _children[i].OnEnabled(f);
        }
      }
    }

    public override void OnDisabled(Frame f) {
      base.OnDisabled(f);

      for (int i = 0; i < _children.Length; ++i) {
        if (f.SystemIsEnabledSelf(_children[i])) {
          _children[i].OnDisabled(f);
        }
      }
    }
  }
}

// Systems/Base/SystemMainThread.cs

namespace Quantum {
  public abstract unsafe class SystemMainThread : SystemBase {
    TaskDelegateHandle _updateHandle;

    String _update;

    public SystemMainThread(string name) {
      _update = name + ".Update";
    }

    public SystemMainThread() {
      _update   = GetType().Name + ".Update";
    }

    protected TaskHandle ScheduleUpdate(Frame f, TaskHandle taskHandle) {
      if (_updateHandle.IsValid == false) {
        f.Context.TaskContext.RegisterDelegate(TaskCallback, _update, ref _updateHandle);
      }

      return f.Context.TaskContext.AddMainThreadTask(_updateHandle, null, taskHandle);
    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return ScheduleUpdate(f, taskHandle);
    }

    void TaskCallback(FrameThreadSafe frame, int start, int count, void* arg) {
      Update((Frame)frame);

      if (((FrameBase)frame).CommitCommandsMode == CommitCommandsModes.InBetweenSystems) {
        ((FrameBase)frame).Unsafe.CommitAllCommands();
      }
    }

    public abstract void Update(Frame f);
  }
}

// Systems/Base/SystemMainThreadFilter.cs

namespace Quantum {
  public abstract unsafe class SystemMainThreadFilter<T> : SystemMainThread where T : unmanaged {
    public virtual bool UseCulling {
      get { return true; }
    }

    public virtual ComponentSet Without {
      [MethodImpl(MethodImplOptions.AggressiveInlining)]
      get => default;
    }
    
    public virtual ComponentSet Any {
      [MethodImpl(MethodImplOptions.AggressiveInlining)]
      get => default;
    }

    public sealed override void Update(Frame f) {
      // grab iterator
      var it = f.Unsafe.FilterStruct<T>(Without, Any);

      // set culling flag
      it.UseCulling = UseCulling;

      // execute filter loop
      var filter = default(T);

      while (it.Next(&filter)) {
        Update(f, ref filter);
      }
    }

    public abstract void Update(Frame f, ref T filter);
  }
}

// Systems/Base/SystemMainThreadGroup.cs

namespace Quantum {
  public unsafe class SystemMainThreadGroup : SystemMainThread {
    SystemMainThread[] _children;

    public SystemMainThreadGroup(string name, params SystemMainThread[] children)
      : base(name + ".Schedule") {
      Assert.Check(name != null);
      Assert.Check(children != null);

      _children = children;

      for (int i = 0; i < _children.Length; i++) {
        _children[i].ParentSystem = this;
      }
    }

    public sealed override IEnumerable<SystemBase> ChildSystems {
      get { return _children; }
    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      if (_children != null) {
        for (var i = 0; i < _children.Length; ++i) {
          if (f.SystemIsEnabledSelf(_children[i])) {
            try {
              taskHandle = _children[i].OnSchedule(f, taskHandle);
            } catch (Exception exn) {
              Log.Exception(exn);
            }
          }
        }
      }

      return taskHandle;
    }

    public override void OnEnabled(Frame f) {
      base.OnEnabled(f);

      for (int i = 0; i < _children.Length; ++i) {
        if (f.SystemIsEnabledSelf(_children[i])) {
          _children[i].OnEnabled(f);
        }
      }
    }

    public override void OnDisabled(Frame f) {
      base.OnDisabled(f);

      for (int i = 0; i < _children.Length; ++i) {
        if (f.SystemIsEnabledSelf(_children[i])) {
          _children[i].OnDisabled(f);
        }
      }
    }

    public sealed override void Update(Frame f) {
    }
  }
}

// Systems/Base/SystemSignalsOnly.cs

namespace Quantum {
  public class SystemSignalsOnly : SystemBase {
    protected sealed override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return taskHandle;
    }
  }
}

// Systems/Base/SystemThreadedComponent.cs

namespace Quantum.Task {
  public abstract unsafe class SystemThreadedComponent<T> : SystemBase where T : unmanaged, IComponent {
    private TaskDelegateHandle _threadedTaskDelegateHandle;
    private int                _sliceIndexer;
    private int                _sliceSize;

    public const int DEFAULT_SLICE_SIZE = 16;

    public virtual int SliceSize => DEFAULT_SLICE_SIZE;

    public sealed override void OnInit(Frame f) {
      f.Context.TaskContext.RegisterDelegate(TaskThreadedComponent, GetType().Name + ".Update", ref _threadedTaskDelegateHandle);
      OnInitUser(f);
    }

    protected virtual void OnInitUser(Frame f) {

    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      // reset indexer
      _sliceIndexer = -1;
      
      // cache slice size safely in main-thread
      _sliceSize = Math.Max(1, SliceSize);
      
      return f.Context.TaskContext.AddThreadedTask(_threadedTaskDelegateHandle, null, taskHandle);
    }

    private void TaskThreadedComponent(FrameThreadSafe f, int start, int count, void* userData) {
      while (true) {
        var sliceIndex = Interlocked.Increment(ref _sliceIndexer);
        var iterator   = f.GetComponentBlockIterator<T>(sliceIndex * _sliceSize, _sliceSize).GetEnumerator();

        if (iterator.MoveNext() == false) {
          // chunk is out of buffer range, we're done
          return;
        }

        do {
          var (entity, component) = iterator.Current;
          Update(f, entity, component);
        } while (iterator.MoveNext());
      }
    }

    public abstract void Update(FrameThreadSafe f, EntityRef entity, T* component);
  }
}

// Systems/Base/SystemThreadedFilter.cs

namespace Quantum.Task {
  public abstract unsafe class SystemThreadedFilter<T> : SystemBase where T : unmanaged {
    private TaskDelegateHandle _threadedTaskDelegateHandle;
    private int                _sliceIndexer;
    private int                _sliceSize;

    public const int DEFAULT_SLICE_SIZE = 16;

    public virtual int SliceSize => DEFAULT_SLICE_SIZE;

    public virtual bool UseCulling => true;

    public virtual ComponentSet Without => default;

    public virtual ComponentSet Any => default;

    public sealed override void OnInit(Frame f) {
      f.Context.TaskContext.RegisterDelegate(TaskThreadedFilter, GetType().Name + ".Update", ref _threadedTaskDelegateHandle);
      OnInitUser(f);
    }

    protected virtual void OnInitUser(Frame f) {

    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      // reset indexer
      _sliceIndexer = -1;

      // cache slice size safely in main-thread
      _sliceSize = Math.Max(1, SliceSize);

      return f.Context.TaskContext.AddThreadedTask(_threadedTaskDelegateHandle, null, taskHandle);
    }

    private void TaskThreadedFilter(FrameThreadSafe f, int start, int count, void* userData) {
      var iterator = f.FilterStruct<T>(Without, Any);

      // set culling flag
      iterator.UseCulling = UseCulling;

      var filter = default(T);

      while (true) {
        var sliceIndex = Interlocked.Increment(ref _sliceIndexer);

        // reset iterator
        iterator.Reset(sliceIndex * _sliceSize, _sliceSize);

        // execute filter loop
        if (iterator.Next(&filter) == false) {
          // chunk is out of buffer range, we're done
          return;
        }

        do {
          Update(f, ref filter);
        } while (iterator.Next(&filter));
      }
    }

    public abstract void Update(FrameThreadSafe f, ref T filter);
  }
}

// Systems/Core/DebugSystem.cs

namespace Quantum.Core {

  public static partial class DebugCommandType {
    public const int Create = 0;
    public const int Destroy = 1;
    public const int UserCommandTypeStart = 1000;
  }

  public static partial class DebugCommand {
    
    public static event Action<Payload, Exception> CommandExecuted
#if DEBUG && !QUANTUM_DEBUG_COMMAND_DISABLED
    {
      add => _commandExecuted += value;
      remove => _commandExecuted -= value;
    }
    private static Action<Payload, Exception> _commandExecuted;
#else
    { add { } remove { } }
#endif

    public static bool IsEnabled =>
#if DEBUG && !QUANTUM_DEBUG_COMMAND_DISABLED
      true;
#else
      false;
#endif

    public static void Send(QuantumGame game, params Payload[] payload) {
#if DEBUG && !QUANTUM_DEBUG_COMMAND_DISABLED
      game.SendCommand(new InternalCommand() {
        Data = payload
      });
#else
      Log.Warn("DebugCommand works only in DEBUG builds without QUANTUM_DEBUG_COMMAND_DISABLED define.");
#endif
    }

    public static void Reset() {
#if DEBUG && !QUANTUM_DEBUG_COMMAND_DISABLED
      _commandExecuted = null;
#endif
    }


    public partial struct Payload {
      public long Id;
      public int Type;
      public EntityRef Entity;
      public ComponentSet Components;
      public byte[] Data;
    }


    public static Payload CreateDestroyPayload(EntityRef entityRef) {
      return new Payload() {
        Type = DebugCommandType.Destroy,
        Entity = entityRef
      };
    }

    public static Payload CreateMaterializePayload(EntityRef entityRef, EntityPrototype prototype, IAssetSerializer serializer) {
      ComponentSet componentSet = default;
      foreach (var component in prototype.Container.Components) {
        componentSet.Add(ComponentTypeId.GetComponentIndex(component.ComponentType));
      }
      return new Payload() {
        Type = DebugCommandType.Create,
        Entity = entityRef,
        Data = serializer.SerializeAssets(new[] { prototype }),
        Components = componentSet
      };
    }

    public static Payload CreateRemoveComponentPayload(EntityRef entityRef, Type componentType) {
      var components = new ComponentSet();
      components.Add(ComponentTypeId.GetComponentIndex(componentType));

      return new Payload() {
        Type = DebugCommandType.Destroy,
        Entity = entityRef,
        Components = components
      };
    }

#if QUANTUM_DEBUG_COMMAND_DISABLED
    internal static DeterministicCommand CreateCommand() => null;
    internal static SystemBase CreateSystem() => null;
#else
    internal static DeterministicCommand CreateCommand() => new InternalCommand();
    internal static SystemBase CreateSystem() => new InternalSystem();

#if DEBUG
    private static void Execute(Frame f, ref Payload payload) {
      Exception error = null;
      try {
        switch (payload.Type) {
          case DebugCommandType.Create:
            payload.Entity = ExecuteCreate(f, payload.Entity, payload.Data);
            break;
          case DebugCommandType.Destroy:
            ExecuteDestroy(f, payload.Entity, payload.Components);
            break;
          default:
            if (payload.Type >= DebugCommandType.UserCommandTypeStart) {
              ExecuteUser(f, ref payload);
            } else {
              throw new InvalidOperationException($"Unknown command type: {payload.Type}");
            }
            break;
        }
      } catch (Exception ex) {
        error = ex;
      }
      _commandExecuted?.Invoke(payload, error);
    }

    private static void ExecuteDestroy(Frame f, EntityRef entity, ComponentSet components) {
      if (!f.Exists(entity)) {
        Log.Error("Entity does not exist: {0}", entity);
      } else if (components.IsEmpty) {
        if (!f.Destroy(entity)) {
          Log.Error("Failed to destroy entity {0}", entity);
        }
      } else {
        for (int i = 1; i < ComponentTypeId.Type.Length; ++i) {
          if (!components.IsSet(i)) {
            continue;
          }
          var type = ComponentTypeId.Type[i];
          if (!f.Remove(entity, type)) {
            Log.Error("Failed to destroy component {0} of entity {1}", type, entity);
          }
        }

      }
    }

    private static EntityRef ExecuteCreate(Frame f, EntityRef entity, byte[] data) {
      EntityPrototype prototype = null;
      if (data?.Length > 0) {
        prototype = f.Context.AssetSerializer.DeserializeAssets(data).OfType<EntityPrototype>().FirstOrDefault();
        if (prototype == null) {
          Log.Error("No prototype found");
        }
      }

      if (!entity.IsValid) {
        if (prototype != null) {
          entity = f.Create(prototype);
        } else {
          entity = f.Create();
        }
      } else if (prototype != null) {
        f.Set(entity, prototype, out _);
      }

      return entity;
    }

    static partial void ExecuteUser(Frame f, ref Payload payload);
    static partial void SerializeUser(BitStream stream, ref Payload payload);

    private class InternalCommand : DeterministicCommand {
      public Payload[] Data = { };

      public override void Serialize(BitStream stream) {
        stream.SerializeArrayLength(ref Data);

        for (int i = 0; i < Data.Length; ++i) {
          stream.Serialize(ref Data[i].Id);
          stream.Serialize(ref Data[i].Type);
          stream.Serialize(ref Data[i].Entity);
          stream.Serialize(ref Data[i].Data);
          unsafe {
            var set = Data[i].Components;
            for (int block = 0; block < ComponentSet.BLOCK_COUNT; ++block) {
              stream.Serialize((&set)->_set + block);
            }
            Data[i].Components = set;
          }
          SerializeUser(stream, ref Data[i]);
        }
      }
    }

    private class InternalSystem : SystemMainThread {
      public override void Update(Frame f) {
        for (int p = 0; p < f.PlayerCount; ++p) {
          if (f.GetPlayerCommand(p) is InternalCommand cmd) {
            for (int i = 0; i < cmd.Data.Length; ++i) {
              Execute(f, ref cmd.Data[i]);
            }
          }
        }
      }
    }
#else
    private class InternalCommand : DeterministicCommand {
      public override void Serialize(BitStream stream) {
        throw new NotSupportedException("DebugCommands only work in DEBUG mode");
      }
    }

    private class InternalSystem : SystemBase {
      protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
        return taskHandle;
      }
    }
#endif
#endif
  }
}


// Systems/Core/NavigationSystem.cs

namespace Quantum.Core {
  public unsafe class NavigationSystem : SystemBase, INavigationCallbacks {
    Frame _f;

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      _f = f;
      return f.Navigation.Update(f, f.DeltaTime, this, taskHandle);
    }

    public void OnWaypointReached(EntityRef entity, FPVector3 waypoint, Navigation.WaypointFlag waypointFlags, ref bool resetAgent) {
      _f.Signals.OnNavMeshWaypointReached(entity, waypoint, waypointFlags, ref resetAgent);
    }

    public void OnSearchFailed(EntityRef entity, ref bool resetAgent) {
      _f.Signals.OnNavMeshSearchFailed(entity, ref resetAgent);
    }

    public void OnMoveAgent(EntityRef entity, FPVector2 desiredDirection) {
      _f.Signals.OnNavMeshMoveAgent(entity, desiredDirection);
    }
  }
}

// Systems/Core/EntityPrototypeSystem.cs
namespace Quantum.Core {
  public unsafe sealed partial class EntityPrototypeSystem : SystemSignalsOnly, ISignalOnMapChanged {
    public override void OnInit(Frame f) {
      OnMapChanged(f, default);
    }

    public void OnMapChanged(Frame f, AssetRefMap previousMap) {
      if (previousMap.Id.IsValid) {
        foreach (var (entity, _) in f.GetComponentIterator<MapEntityLink>()) {
          f.Destroy(entity);
        }
      }

      if (f.Map != null) {
        f.Create(f.Map.MapEntities, f.Map);
      }
    }
  }
}

// Systems/Core/CullingSystem.cs

namespace Quantum.Core {
  public unsafe class CullingSystem2D : SystemBase {
    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return f.Context.Culling.Schedule2D(f, taskHandle);
    }
  }
  
  public unsafe class CullingSystem3D : SystemBase {
    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return f.Context.Culling.Schedule3D(f, taskHandle);
    }
  }
}

// Systems/Core/PlayerConnectedSystem.cs
namespace Quantum.Core {
  unsafe class PlayerConnectedSystem : SystemMainThread {
    public override void Update(Frame f) {
      if (f.IsVerified == false) {
        return;
      }

      for (int p = 0; p < f.PlayerCount; p++) {
        var isPlayerConnected = (f.GetPlayerInputFlags(p) & Photon.Deterministic.DeterministicInputFlags.PlayerNotPresent) == 0;
        if (isPlayerConnected != f.Global->PlayerLastConnectionState.IsSet(p)) {
          if (isPlayerConnected) {
            f.Signals.OnPlayerConnected(p);
          } else {
            f.Signals.OnPlayerDisconnected(p);
          }

          if (isPlayerConnected) {
            f.Global->PlayerLastConnectionState.Set(p);
          }
          else {
            f.Global->PlayerLastConnectionState.Clear(p);
          }
        }
      }
    }
  }
}



// Systems/Core/PhysicsSystem.cs

namespace Quantum.Core {
  public unsafe partial class PhysicsSystem2D : SystemBase, ICollisionCallbacks2D {
    public override void OnInit(Frame f) {
      f.Physics2D.Init();
    }

    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return f.Physics2D.Update(this, f.DeltaTime, taskHandle);
    }
    
    public void OnCollision2D(FrameBase f, CollisionInfo2D info) {
      ((Frame)f).Signals.OnCollision2D(info);
    }

    public void OnCollisionEnter2D(FrameBase f, CollisionInfo2D info) {
      ((Frame)f).Signals.OnCollisionEnter2D(info);
    }

    public void OnCollisionExit2D(FrameBase f, ExitInfo2D info) {
      ((Frame)f).Signals.OnCollisionExit2D(info);
    }

    public void OnTrigger2D(FrameBase f, TriggerInfo2D info) {
      ((Frame)f).Signals.OnTrigger2D(info);
    }

    public void OnTriggerEnter2D(FrameBase f, TriggerInfo2D info) {
      ((Frame)f).Signals.OnTriggerEnter2D(info);
    }

    public void OnTriggerExit2D(FrameBase f, ExitInfo2D info) {
      ((Frame)f).Signals.OnTriggerExit2D(info);
    }
  }
  
  public unsafe partial class PhysicsSystem3D : SystemBase, ICollisionCallbacks3D {
    public override void OnInit(Frame f) {
      f.Physics3D.Init();
    }
    
    protected override TaskHandle Schedule(Frame f, TaskHandle taskHandle) {
      return f.Physics3D.Update(this, f.DeltaTime, taskHandle);
    }

    public void OnCollision3D(FrameBase f, CollisionInfo3D info) {
      ((Frame)f).Signals.OnCollision3D(info);
    }

    public void OnCollisionEnter3D(FrameBase f, CollisionInfo3D info) {
      ((Frame)f).Signals.OnCollisionEnter3D(info);
    }

    public void OnCollisionExit3D(FrameBase f, ExitInfo3D info) {
      ((Frame)f).Signals.OnCollisionExit3D(info);
    }

    public void OnTrigger3D(FrameBase f, TriggerInfo3D info) {
      ((Frame)f).Signals.OnTrigger3D(info);
    }

    public void OnTriggerEnter3D(FrameBase f, TriggerInfo3D info) {
      ((Frame)f).Signals.OnTriggerEnter3D(info);
    }

    public void OnTriggerExit3D(FrameBase f, ExitInfo3D info) {
      ((Frame)f).Signals.OnTriggerExit3D(info);
    }
  }
}