File size: 86,068 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
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
if FirstLoad then
	Mods = {}
	ModsList = false -- in list form sorted by title, used for editing in Ged
	ModsLoaded = false -- an array of the loaded mods in the order of loading
	ModsPackFileName = "ModContent.hpk"
	ModContentPath = "Mod/"
	ModMountNextLabelIdx = 0
	--screenshots are copied to another folder and renamed
	--this way we can distinguish them from shots uploaded from outside the mod editor
	ModsScreenshotPrefix = "ModScreenshot_"
	ModMessageLog = {}
	
	--Bumped whenever a major change to mods has been made.
	--One that breaks backward compatibility.
	ModMinLuaRevision = 233360
	
	--Min game version required to load mods created using the current game version.
	--Bumped whenever forward compatibility is broken for older game versions.
	ModRequiredLuaRevision = 233360
	
	--Mod blacklist support
	--These mods will not show in the UI and will not be loaded
	--Two types of blacklisting:
	--deprecate -> mod is present in the base game and no longer needed, saves will work without it, no warnings for missing in saves, warning in ui that is deprecated, can be loaded again
	--ban -> mod is banned from the game, saves with this mod missing are not guaranteed to work, doesn't show in the ui at all
	ModIdBlacklist = {} -- { [id] = "deprecate", [id] = "ban" }
	AutoDisabledModsAlertText = {}

	if Platform.goldmaster and io.exists("ModTools") then
		function OnMsg.Autorun()
			CreateRealTimeThread(function()
				local out_path = ConvertToOSPath("ModTools/Src/ModTools.code-workspace")
				if io.exists(out_path) then return end
				local template_path = ConvertToOSPath("ModTools/Src/ModTools.code-workspace.template")
				local err, contents = AsyncFileToString(template_path)
				if err then return end
				local mods_path = ConvertToOSPath("AppData/Mods/")
				contents = string.gsub(contents, "Mods", string.gsub(mods_path, "\\", "\\\\"))
				local err = AsyncStringToFile(out_path, contents)
			end)
		end
	end
end

function IsUserCreatedContentAllowed()
	return true
end

mod_print = CreatePrint{
	"mod",
	format = "printf",
	output = DebugPrint,
}

-- Usage:
-- ModLog(T(...)) to print to Mod Manager dialog
-- ModLog(true, T(...)) to print to Mod Manager and store in log file
function ModLog(log_or_msg, msg)
	local log
	if IsT(log_or_msg) then
		msg = log_or_msg
	else
		log = log_or_msg
	end
	msg = _InternalTranslate(msg)
	ModMessageLog[#ModMessageLog + 1] = msg
	if log then
		mod_print(msg)
	end
	ObjModifiedDelayed(ModMessageLog)
end

-- Usage:
-- ModLogF("asd", ...) to print to Mod Manager dialog
-- ModLogF(true, "asd", ...) to print to Mod Manager and store in log file
function ModLogF(log_or_fmt, fmt, ...)
	local log, msg
	if type(log_or_fmt) == "string" then
		local arg1 = fmt
		fmt = log_or_fmt
		msg = string.format(fmt, arg1, ...)
	else
		log = log_or_fmt
		msg = string.format(fmt, ...)
	end
	return ModLog(log, Untranslated(msg))
end

function OnMsg.Autorun()
	ObjModified(ModMessageLog)
end

if not config.Mods then 
	AreModdingToolsActive = empty_func
	DefineClass("ModLinkDef", "PropertyObject")
	return
end

function AreModdingToolsActive()
	return IsModEditorOpened() or IsModManagerOpened() or IsModEditorMap() or (Game and Game.testModGame)
end

DocsRoot = "ModTools/Docs/"
if Platform.developer then
	DocsRoot = "svnProject/Docs/ModTools/"
end


----- ModElement

DefineClass.ModElement = { 
}

function ModElement:OnLoad(mod)
	self:AddPathPrefix()
end

function ModElement:OnUnload(mod)
end

function ModElement:IsMounted()
end

function ModElement:IsPacked()
end

--root path is where the mod exists on the OS
function ModElement:GetModRootPath()
end

--content path is the path where content can be accessed
function ModElement:GetModContentPath()
end

function ModConvertSlashes(path)
	--convert all '\' into '/'
	return string.gsub(path, "\\", "/")
end

local function EscapeMagicSymbols(path)
	--convert all gsub 'magic symbols' into escaped symbols
	return string.gsub(ModConvertSlashes(path), "[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1")
end

local function GetChildren(item)
	if IsKindOf(item, "ModDef") then
		return item.items
	else
		return item
	end
end

function ModElement:PostSave()
	self:AddPathPrefix()
end

local function ModResourceExists(path, item)
	if io.exists(path) then
		return true
	end
	
	if item and IsKindOf(item, "SoundFile") then
		local parent = GetParentTable(item)
		local files = parent and parent:GetSoundFiles()
		if files and files[path .. "." .. item:GetFileExt()] then
			return true
		end
	end

	local res_id = ResourceManager.GetResourceID(path)
	return res_id ~= const.InvalidResourceID
end

local function RecursiveAddPathPrefix(item, mod_path, mod_os_path, mod_content_path, is_packed)
	for i, prop in ipairs(item:GetProperties()) do
		if (prop.editor == "browse" or prop.editor == "ui_image") and not prop.os_path then
			local prop_id = prop.id
			local path = item:GetProperty(prop_id)
			if (path or "") ~= "" and not path:starts_with(ModContentPath) and not ModResourceExists(path, item) then
				if is_packed then
					item:SetProperty(prop_id, mod_content_path .. path)
				else
					if not string.find(path, EscapeMagicSymbols(mod_os_path)) then
						item:SetProperty(prop_id, ModConvertSlashes(mod_os_path .. path))
					end
				end
			end
		end
	end
	
	for _, child in ipairs(GetChildren(item)) do
		RecursiveAddPathPrefix(child, mod_path, mod_os_path, mod_content_path, is_packed)
	end
end

function ModElement:AddPathPrefix()
	local mod_path = ModConvertSlashes(self:GetModRootPath())
	local mod_os_path = ConvertToOSPath(mod_path)
	local mod_content_path = self:GetModContentPath()
	local is_packed = self:IsPacked()
	RecursiveAddPathPrefix(self, mod_path, mod_os_path, mod_content_path, is_packed)
end

function ModElement:PreSave()
	self:RemovePathPrefix()
end

local function RecursiveRemovePathPrefix(item, mod_path, mod_os_path, mod_content_path, is_packed)
	for i, prop in ipairs(item:GetProperties()) do
		if (prop.editor == "browse" or prop.editor == "ui_image") and not prop.os_path then
			local prop_id = prop.id
			local path = item:GetProperty(prop_id)
			if (path or "") ~= "" and not path:starts_with(ModContentPath) then
				path = ModConvertSlashes(path)
				local prefix = is_packed and mod_content_path or mod_os_path
				local new_path = string.gsub(path, EscapeMagicSymbols(prefix), "")
				item:SetProperty(prop_id, new_path)
			end
		end
	end

	for _, child in ipairs(GetChildren(item)) do
		RecursiveRemovePathPrefix(child, mod_path, mod_os_path, mod_content_path, is_packed)
	end
end

function ModElement:RemovePathPrefix()
	local mod_path = ModConvertSlashes(self:GetModRootPath())
	local mod_os_path = ModConvertSlashes(ConvertToOSPath(mod_path))
	local mod_content_path = self:GetModContentPath()
	local is_packed = self:IsPacked()
	RecursiveRemovePathPrefix(self, mod_path, mod_os_path, mod_content_path, is_packed)
end

function ModElement:NeedsResave()
end


----- ModLinkDef

DefineClass.ModLinkDef = {
	__parents = { "DisplayPreset", },
	__generated_by_class = "PresetDef",

	properties = {
		{ id = "Patterns", 
			editor = "string_list", default = {}, item_default = "", items = false, arbitrary_value = true, help = "A link should match any of these patterns. The pattern capture (in brackets) defines how the link will be displayed. Add a capture to remove the typical 'https://www.' at the start." },
		{ id = "Icon", 
			editor = "ui_image", default = false, },
	},
	GlobalMap = "ModLinkDefs",
	EditorMenubarName = "Mod links",
	EditorMenubar = "Editors.Engine",
	StoreAsTable = true,
}

function NormalizeLink(link)
	if not link or link == "" then return "" end
	return link:starts_with("https://") and link or "https://" .. link
end

function GetLinkDef(link)
	link = NormalizeLink(link)
	local link_def, link_short
	ForEachPreset("ModLinkDef", function(def)
		for _, pattern in ipairs(def.Patterns) do
			link_short = link:match(pattern)
			if link_short then
				link_def = def
				return "break"
			end
		end
	end)
	return link_def, link_short
end

function GetLinkShort(link)
	local def, short = GetLinkDef(link)
	return short
end


----- ModDef

DefineClass.ModDef = {
	__parents = { "GedEditedObject", "ModElement", "Container", "InitDone" },
	properties =
	{
		{ category = "Mod",  id = "title",         name = "Title",        editor = "text",   default = "" },
		{ category = "Mod",  id = "description",   name = "Description",  editor = "text",   default = "", lines = 5, max_lines = 15, max_len = 8000, },
		{ category = "Mod",  id = "tags",          name = "Tags",         editor = false,    default = "" },
		{ category = "Mod",  id = "image",         name = "Preview image",editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		{ category = "Mod",  id = "external_links",name = "Links",        editor = "string_list", default = {}, arbitrary_value = true, help = function(obj, prop_meta)
			local sites = {}
			ForEachPreset("ModLinkDef", function(def)
				sites[#sites + 1] = " • " .. (def.display_name == "" and def.id or TTranslate(def.display_name, def))
			end)
			return "Allows including links in the mod description to the following sites:\n\n" .. table.concat(sites, "\n")
		end },
		{ category = "Mod",  id = "last_changes",  name = "Last changes",  editor = "text",   default = "", lines = 3, },
		{ category = "Mod",  id = "ignore_files",  name = "Ignore files",  editor = "string_list", default = { "*.git/*", "*.svn/*" },
			help = "Files in the mod folder that must not be included in the packaged mod."
		},
		{ category = "Mod",  id = "dependencies",  name = "Dependencies",  editor = "nested_list", default = false, base_class = "ModDependency", inclusive = true,
			help = "Allows specifying a list of mods required for this mod to work,\nor mods that must be loaded before it, if present."
		},
		{ category = "Mod",  id = "id",            name = "ID",            editor = "text",   default = "", read_only = true },
		{ category = "Mod",  id = "content_path",  name = "Content path",  editor = "text",   default = false, read_only = true, help = "Folder to access the mod files.", buttons = {{name = "Copy", func = "CopyContentPath"}}, dont_save = true },
		{ category = "Mod",  id = "author",        name = "Author",        editor = "text",   default = "", read_only = true }, -- platform specific author name
		{ category = "Mod",  id = "version_major", name = "Major version", editor = "number", default =  0 },
		{ category = "Mod",  id = "version_minor", name = "Minor version", editor = "number", default =  0 },
		{ category = "Mod",  id = "version",       name = "Revision",      editor = "number", default =  0, read_only = true },
		{ category = "Mod",  id = "lua_revision",      name = "Required game version", editor = "number", default =  0, read_only = true },
		{ category = "Mod",  id = "saved_with_revision", name = "Saved with game version",      editor = "number", default =  0, read_only = true },
		
		-- not displayed, used for saving only
		{ category = "Mod",  id = "entities",      editor = "prop_table",  default = false, no_edit = true },
		{ category = "Mod",  id = "code",          editor = "prop_table",  default = false, no_edit = true },
		{ category = "Mod",  id = "loctables",     editor = "prop_table",  default = false, no_edit = true },
		{ category = "Mod",  id = "default_options",   editor = "prop_table",        default = false, no_edit = true },
		{ category = "Mod",  id = "has_data",      editor = "bool",        default = false, no_edit = true },
		{ category = "Mod",  id = "saved",         editor = "number", default = false, no_edit = true },
		{ category = "Mod",  id = "code_hash",     editor = "number", default = false, no_edit = true },
		
		{ category = "Screenshot", id = "screenshot1", name = "Screenshot", editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		{ category = "Screenshot", id = "screenshot2", name = "Screenshot", editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		{ category = "Screenshot", id = "screenshot3", name = "Screenshot", editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		{ category = "Screenshot", id = "screenshot4", name = "Screenshot", editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		{ category = "Screenshot", id = "screenshot5", name = "Screenshot", editor = "ui_image", default = "", filter = "Image files|*.png;*.jpg" },
		
		{ category = "Conflict Detection",  id = "affected_resources", name = "Affected resources", editor = "nested_list", default = false, base_class = "ModResourceDescriptor", inclusive = true, read_only = true,
			help = "Lists the game content affects by the mod, used to detect potential conflicts as mods are loaded.",
			sort_order = 1000000,
		},
	},
	path = "", -- source folder, this is where the metadata and ModContent.hpk is located
	source = "appdata",
	env = false,
	packed = false,
	mounted = false,
	mount_label = false,
	status = "alive",
	items = false,
	items_file_timestamp = 0,
	options = false,
	dev_message = "",
	ContainerClass = "ModItem",
	force_reload = false,
	mod_opening = false,
	mod_ged_id = false,
	-- Mods Editor
	GedTreeChildren = function (self) return self.items end,
}

function ModDef:OnEditorSetProperty(prop_id, old_value, ged)
	if prop_id == "title" then
		local new_title = "Mod Editor - " .. self[prop_id]
		ged:Send("rfnApp", "SetTitle", new_title)
	end
end

function ModDef:CopyContentPath()
	CopyToClipboard(self.content_path)
end

function ModDef:Init()
	self.options = ModOptionsObject:new({ __mod = self })
end

function ModDef:delete()
	if self.status == "deleted" then
		return
	end

	self:UnloadItems()
	self:UnmountContent()
	self.status = "deleted"
end

function ModDef:IsOpenInGed()
	return not not GedObjects[ParentTableCache[self]]
end

function ModDef:CalculatePersistHash()
	local hash = PropertyObject.CalculatePersistHash(self)
	self:ForEachModItem(function(mod_item)
		mod_item:TrackDirty() -- calculate the current hash, if not already calculated
		hash = xxhash(hash, mod_item:EditorData().current_hash) -- consider the mod changed if any mod item is changed
	end)
	return hash
end

function ModDef:__eq(rhs)
	return
		self.id == rhs.id and
		self.source == rhs.source and
		self.version == rhs.version
end

function ModDef:GetTags()
	assert(false, "override this method in game-specific code")
	return { }
end

function ModDef:GetEditorView()
	if self:ItemsLoaded() then
		return Untranslated("<u(literal(title))> (loaded)\nid <u(id)>, version <VersionString>")
	else
		return Untranslated("<color 128 128 128><u(literal(title))>\nid <u(id)>, version <VersionString></color>")
	end
end

local ModIdCharacters = "ACDEFGHJKLMNPQRSTUVWXYabcdefghijkmnopqrstuvwxyz345679"
function ModDef:GenerateId()
	local id = ""
	for i = 1, 7 do
		local rand = AsyncRand(i > 1 and #ModIdCharacters or #ModIdCharacters - 10)
		id = id .. ModIdCharacters:sub(rand, rand)
	end
	return id
end

function ModDef:GetExternalLinkError()
	local seen = {}
	for i, link in ipairs(self.external_links) do
		if link ~= "" then
			local def = GetLinkDef(link)
			if not def then return "Unrecognized link: " .. link, i end
			if seen[def] then
				return "Duplicate link to: " .. def.id
			end
			seen[def] = true
		end
	end
end

function ModDef:GetError()
	return self:GetExternalLinkError()
end

function ModDef:GetValidatedExternalLinks()
	local seen, validated = {}, {}
	for _, link in ipairs(self.external_links) do
		local def = GetLinkDef(link)
		if def and not seen[def] then
			validated[#validated + 1] = link
			seen[def] = true
		end
	end
	return validated
end

function ModDef:GenerateMountLabel()
	ModMountNextLabelIdx = ModMountNextLabelIdx + 1
	return "mod_" .. ModMountNextLabelIdx
end

function ModDef:ChangePaths(mod_path)
	self:RemovePathPrefix()
	self.path = mod_path
	self:AddPathPrefix()
end

function ModDef:NeedsResave()
	return self:ForEachModItem(function(item)
		if item:NeedsResave() then
			return true
		end
	end)
end

function ModDef:GetWarning()
	if self:NeedsResave() then
		return "Please resave this mod for optimization reasons."
	end
end

function ModDef:SortItems()
	table.stable_sort(self.items, function(a, b)
		if a.class == b.class then
			return a.name < b.name
		end
		return a.class < b.class
	end)
end

function ModDef:ItemsLoaded()
	return not not self.items
end

function ModDef:GenerateModItemId(mod_item)
	return string.format("autoid_%s_%s", self.id, self:GenerateId())
end

function ModDef:GetDefaultOptions()
	local options_items = { }
	if not self.items then return options_items end
	
	self:ForEachModItem("ModItemOption", function(item)
		if item.name and item.name ~= "" then
			options_items[item.name] = item.DefaultValue
		end
	end)
	return options_items
end

function ModDef:HasOptions()
	return self.has_options or next(self.default_options) -- has_options is used for backwards comp
end

function ModDef:GetOptionItems(test)
	local options_items = not test and { }
	if not self.items then return options_items end
	self:ForEachModItem("ModItemOption", function(item)
		if item.name and item.name ~= "" then
			if test then return true end
			table.insert(options_items, item)
		end
	end)
	
	return options_items
end

function ModDef:LoadCode()
	if not LuaLoadedForMods[self.id] then
		rawset(self.env, "FirstLoad", true)
		LuaLoadedForMods[self.id] = true
	end
	
	-- load classes generated from Presets (e.g. composite classes) first
	local errs = {}
	for _, filename in ipairs(self.code) do
		if not filename:starts_with("Code/") then
			local ok, err = pdofile(self.content_path .. filename, self.env, "t")
			if not ok then
				err = err and err:gsub(self.content_path, ""):gsub("cannot read ", ""):gsub(": ", " - ")
				table.insert(errs, err or string.format("%s: Unknown error", filename))
			end
		end
	end
	
	-- load Code mod items last, to allow them define methods of classes loaded above, etc.
	for _, filename in ipairs(self.code) do
		if filename:starts_with("Code/") then
			local ok, err = pdofile(self.content_path .. filename, self.env, "t")
			if not ok then
				err = err and err:gsub(self.content_path, ""):gsub("cannot read ", ""):gsub(": ", " - ")
				table.insert(errs, err or string.format("%s: Unknown error", filename))
			end
		end
	end
	
	rawset(self.env, "FirstLoad", false)
	return next(errs) and errs
end

function ModDef:StoreItemsFileModifiedTime()
	self.items_file_timestamp = io.getmetadata(self.content_path .. "items.lua", "modification_time")
end

function ModDef:IsItemsFileModified()
	return self.items_file_timestamp ~= io.getmetadata(self.content_path .. "items.lua", "modification_time")
end

function ModDef:LoadItems()
	if self:ItemsLoaded() then return end
	
	assert(self.content_path)
	local path = self.content_path .. "items.lua"
	local ok, items = pdofile(path, self.env)
	self.items = ok and items or { }
	if not ok then
		local err = items
		ModLogF(true, "Failed to load mod items for %s. Error: %s", self:GetModLabel("plainText"), err)
		return
	end
	
	-- cache the source file to make sure it matches with the debug info of loaded functions
	-- (and we can get their source code even if items.lua is edited externally)
	local err, source = AsyncFileToString(path, nil, nil, "lines")
	if not err then CacheLuaSourceFile(path, source) end
	
	PopulateParentTableCache(self)
	self:StoreItemsFileModifiedTime()
	
	local has_data
	self:ForEachModItem(function(item)
		item.mod = self
		sprocall(item.OnModLoad, item, self)
		has_data = has_data or item.is_data
	end)
	self.has_data = has_data
end

function ModDef:LoadOptions()
	if not self:HasOptions() then
		self:UnloadOptions()
		return
	end
	
	if AccountStorage then
		local options_in_storage = AccountStorage.ModOptions and AccountStorage.ModOptions[self.id]
		self:UnloadOptions()
		table.overwrite(self.options, options_in_storage)
		-- initialize option defaults
		-- so mod code can use it without GetProperty
		for id, default_value in pairs(self.default_options) do
			if rawget(self.options, id) == nil then
				rawset(self.options, id, default_value)
			end
		end
		Msg("ApplyModOptions", self.id)
	end
end

function ModDef:UnloadOptions()
	local options_obj = self.options
	options_obj.properties = nil
	options_obj.__defaults = nil
	table.clear(options_obj)
	options_obj.__mod = self
end

function ModDef:UnloadItems()
	if not self:ItemsLoaded() then
		return
	end
	
	self:ForEachModItem(function(item)
		item:OnModUnload(self)
		item:delete()
	end)
	
	self.items = false
	self.has_data = false
end

function ModDef:ForEachModItem(classname, fn)
	if not self.items then return end
	for _, item in ipairs(self.items) do
		local ret = item:ForEachModItem(classname, fn)
		if ret == "break" then
			break
		elseif ret ~= nil then
			return ret
		end
	end
end

function ModDef:FindModItem(mod_item)
	return table.find(self.items, mod_item)
end

function ApplyModOptions(host)
	CreateRealTimeThread(function(host)
		local mod = GetDialogModeParam(host)
		local options = mod.options
		if not options then return end
		--@@@msg ApplyModOptions,mod_id- fired when loading the mod options and when the user applies changes to their mod options.
		Msg("ApplyModOptions", mod.id)
		
		AccountStorage.ModOptions = AccountStorage.ModOptions or { }
		local storage_table = AccountStorage.ModOptions[mod.id] or { }
		for _, prop in ipairs(options:GetProperties()) do
			local value = options:GetProperty(prop.id)
			value = type(value) == "table" and table.copy(value) or value
			storage_table[prop.id] = value
		end
		AccountStorage.ModOptions[mod.id] = storage_table
		SaveAccountStorage(1000)
		
		SetBackDialogMode(host)
	end, host)
end

function CancelModOptions(host)
	CreateRealTimeThread(function(host)
		local mod = GetDialogModeParam(host)
		local original_obj = ResolvePropObj(host:ResolveId("idOriginalModOptions").context)
		if not mod or not original_obj then return end
		local properties = mod.options:GetProperties()
		for i = 1, #properties do
			local prop = properties[i]
			local original_value = original_obj:GetProperty(prop.id)
			mod.options:SetProperty(prop.id, original_value)
		end
		SetBackDialogMode(host)
	end, host)
end

function ResetModOptions(host)
	CreateRealTimeThread(function(host)
		local mod = GetDialogModeParam(host)
		local options = mod and mod.options
		if not options then return end
		local properties = mod.options:GetProperties()
		for i = 1, #properties do
			local prop = properties[i]
			local default_value = mod.options:GetDefaultPropertyValue(prop.id, prop)
			mod.options:SetProperty(prop.id, default_value)
		end
		ObjModified(mod.options)
	end, host)
end

function HasModsWithOptions()
	local mods_to_load = AccountStorage and AccountStorage.LoadMods
	if not mods_to_load then return end
	for i,id in ipairs(mods_to_load) do
		local mod_def = Mods[id]
		if mod_def and mod_def:HasOptions() then
			return true
		end
	end
end

function ModDef:UpdateEntities()
	--backwards compatibility for r342666
	self.bin_assets = nil

	local dirty
	if self.items then
		local entities = false
		self:ForEachModItem("ModItemEntity", function(item)
			if item.entity_name ~= "" then
				entities = entities or {}
				table.insert(entities, item.entity_name)
				dirty = dirty or item:IsDirty()
			end
		end)
		self.entities = entities
	end
	return dirty
end

function ModDef:UpdateCode()
	local dirty
	if self.items then
		local code = false
		local code_hash
		self:ForEachModItem(function(item)
			local name = item:GetCodeFileName() or ""
			if name ~= "" then
				code = code or {}
				code[#code + 1] = name
				local err, hash
				if name ~= "" then
					err, hash = AsyncFileToString(item:GetCodeFilePath(), nil, nil, "hash")
				end
				code_hash = xxhash(code_hash, name, hash)
				dirty = dirty or err or item:IsDirty()
			end
		end)
		dirty = dirty or code_hash ~= self.code_hash
		self.code = code
		self.code_hash = code_hash
	end
	return dirty
end

function ModDef:UpdateLocTables()
	if self.items then
		local loctables
		self:ForEachModItem("ModItemLocTable", function(item)
			loctables = loctables or {}
			item:RemovePathPrefix()
			loctables[#loctables + 1] = { filename = item.filename, language = item.language }
			item:AddPathPrefix()
		end)
		self.loctables = loctables
	end
end

function ModDef:MountContent()
	if not self.mounted then
		self.mounted = true
		self.mount_label = self.mount_label or self:GenerateMountLabel()
		if self.packed then
			MountPack(self.content_path, self.path .. ModsPackFileName, "label:" .. self.mount_label)
		else
			MountFolder(self.content_path, self.path, "label:" .. self.mount_label)
		end
	end

	local binAssetsLabel = self.mount_label .. "BinAssets"

	if self.packed and MountsByLabel(binAssetsLabel) == 0 then
		MountFolder("BinAssets/Materials", self.content_path .. "BinAssets", "seethrough label:" .. binAssetsLabel)
	end
end

function ModDef:UnmountContent()
	if not self.mounted then return end
	local binAssetsLabel = self.mount_label .. "BinAssets"
	if self.packed and MountsByLabel(binAssetsLabel) > 0 then 
		UnmountByLabel(binAssetsLabel)
	end
	UnmountByLabel(self.mount_label)
	self.mounted = false
end

function ModDef:IsMounted()
	return self.mounted
end

function ModDef:IsPacked()
	return self.packed
end

function ModDef:GetModRootPath()
	return ConvertToOSPath(SlashTerminate(self.path))
end

function ModDef:GetModContentPath()
	return self.content_path
end

function ModDef:IsTooOld()
	return self.lua_revision < ModMinLuaRevision
end

function ModDef:IsTooNew()
	return self.lua_revision > LuaRevision
end

function ModDef:RefreshAffectedResources()
	if not self.items then return end

	local all_affected_res = {}
	self:ForEachModItem(function(item)
		table.iappend(all_affected_res, item:GetAffectedResources() or empty_table)
	end)

	-- If there are changes to the affected resources, clear the cache
	if not self.affected_resources or not table.iequal(self.affected_resources, all_affected_res) then
		ClearModsAffectedResourcesCache()
	end
	
	self.affected_resources = all_affected_res
end


----- Saving

function ModDef:CanSaveMod(ged, ask_for_save_question)
	assert(self.items)
	if not self.items then return end
	
	local title = "Mod "..self.title
	if ask_for_save_question and ged:WaitQuestion(title, ask_for_save_question, "Yes", "No") ~= "ok" then
		return
	end
	if self:IsItemsFileModified() and ged:WaitQuestion(title,
	   "The items.lua file was modified externally.\n\nSaving will overwrite the external changes. Continue?", "Yes", "No") ~= "ok" then
		return
	end
	if PreloadFunctionsSourceCodes(self) == "error" and ged:WaitQuestion(title,
	   "Unable to get the code of all Lua functions.\n\nThe code of some functions will be lost. Continue?", "Yes", "No") ~= "ok" then
		return
	end
	return true
end

function ModDef:PreSave()
	ModElement.PreSave(self)
	self:RefreshAffectedResources()
end

function ModDef:SaveDef(serialize_only)
	self.external_links = self:GetValidatedExternalLinks()
	if self.content_path then
		local code_dirty
		if not serialize_only then
			Msg("ModDefUpdate", self)
			self.lua_revision = ModRequiredLuaRevision
			self.saved_with_revision = LuaRevision
			self.version = self.version + 1
			self.saved = os.time()
			self.default_options = self:GetDefaultOptions()
			self:UpdateEntities()
			self:UpdateLocTables()
			code_dirty = self:UpdateCode()
		end
		
		local data = pstr("return ", 32768)
		self:PreSave()
		data:appendv(self, "")
		self:PostSave()
		local err = AsyncStringToFile(self.content_path .. "metadata.lua", data)
		
		if not serialize_only then
			CreateRealTimeThread(function()
				Sleep(200)
				self:ForEachModItem(function(item)
					item:MarkClean()
				end)
				self:MarkClean()
				ObjModified(self)
			end)
			DelayedCall(50, SortModsList)
		end
		return err, code_dirty
	end
end

function ModDef:SaveItems()
	if not self:ItemsLoaded() then return "not loaded" end
	
	-- generate code
	local data = pstr("return ", 65536)
	local saved_preset_classes = {}
	self:ForEachModItem(function(item) item:PreSave() end)
	ValueToLuaCode(self.items, "", data)
	self:ForEachModItem(function(item) item:PostSave(saved_preset_classes) end)
	
	-- save to file
	local path = self.content_path .. "items.lua"
	local err = AsyncStringToFile(path, data)
	data:free()
	self:StoreItemsFileModifiedTime()
	
	-- trigger PresetSave, usually used for postprocessing data or applying changes
	for class in sorted_pairs(saved_preset_classes) do
		Msg("PresetSave", class)
	end
	
	CacheModDependencyGraph()
	return err
end

function ModDef:SaveOptions()
	if self.options then
		self.options.properties = nil
		self.options.__defaults = nil
	end
	if self.has_options then
		self.has_options = nil
	end
end

function ModDef:SaveWholeMod()
	GedSetUiStatus("mod_save", "Saving...")
	DiagnosticMessageSuspended = true
	PauseInfiniteLoopDetection("SaveMod")
	
	local err, code_dirty = self:SaveDef()
	self:SaveItems()
	self:SaveOptions()
	if code_dirty then
		ReloadLua()
	end
	
	ResumeInfiniteLoopDetection("SaveMod")
	DiagnosticMessageSuspended = false
	GedSetUiStatus("mod_save")
end

function ModDef:CompareVersion(other_mod, ignore_revision)
	assert(other_mod)
	local version_diffs = {
		self.version_major - (other_mod.version_major or self.version_major),
		self.version_minor - (other_mod.version_minor or self.version_minor),
		ignore_revision and 0 or (self.version - (other_mod.version or 0)),
	}
	
	for i = 1, #version_diffs do
		local diff = version_diffs[i]
		if diff ~= 0 then
			return diff
		end
	end
	return 0
end

function ModDef:GetVersionString()
	return string.format("%d.%02d-%03d", self.version_major, self.version_minor, self.version)
end

function ModDef:GetModLabel(plainText)
	local text = string.format("%s (id %s, v%s)", self.title, self.id, self:GetVersionString())
	return plainText and text or Untranslated(text)
end

function ModDefPersist(mod_info)
	setmetatable(mod_info, ModDef)
	local mod_def = Mods[mod_info.id]
	if not mod_def then
		ModLogF(true, "Savegame references Mod %s which is not present", mod_info:GetModLabel("plainText"))
		return mod_info
	end
	if not mod_def.items then
		ModLogF(true, "This savegame tries to load Mod %s, which is present, but not loaded", mod_def:GetModLabel("plainText"))
	elseif mod_def:CompareVersion(mod_info) ~= 0 then
		ModLogF(true, "This savegame tries to load Mod %s, which is loaded with a different version %s", mod_info:GetModLabel("plainText"), mod_def:GetVersionString())
	else
		ModLogF("This savegame loads Mod %s", mod_def:GetModLabel("plainText"))
	end
	return mod_def
end

function ModDef:__persist()
	local mod_info = {
		title = self.title,
		id = self.id,
		version_major = self.version_major,
		version_minor = self.version_minor,
		version = self.version,
		lua_revision = self.lua_revision,
	}
	return function()
		return ModDefPersist(mod_info)
	end
end

function OnMsg.PersistSave(data)
	data["ModsLoaded"] = ModsLoaded
end

function OnMsg.BugReportStart(print_func)
	local active_mods = {}
	for i,mod in ipairs(ModsLoaded) do
		table.insert(active_mods, string.format("%s (id %s, v%s, source %s, required Lua: %d, saved with Lua: %d",
			mod.title, mod.id, mod:GetVersionString(),  mod.source,
			mod.lua_revision, mod.saved_with_revision))
	end
	table.sort(active_mods)
	print_func("Active Mods:" .. (next(active_mods) and ("\n\t" .. table.concat(active_mods, "\n\t")) or "None") .. "\n")
	local ignored_mods = {}
	if SavegameMeta then
		for _, mod in ipairs(SavegameMeta.ignored_mods) do
			table.insert(ignored_mods, string.format("%s (id %s, v%s, source %s, required Lua: %d, saved with Lua: %d",
				mod.title or "no info", mod.id or "no info", mod.version or "no info",  mod.source or "no info",
				mod.lua_revision or 0, mod.saved_with_revision or 0))
		end
	end
	print_func("Ignored Mods:" .. (next(ignored_mods) and ("\n\t" .. table.concat(ignored_mods, "\n\t")) or "None") .. "\n")
	local affected_resources = GetAllLoadedModsAffectedResources()
	if next(affected_resources) then
		print_func("Affected resources from mods (doesn't include ignored mods and custom code):\n\t" .. table.concat(affected_resources, "\n\t"))
	end
	
	local codes = { }
	Msg("GatherModDownloadCode", codes)
	if next(codes) then
		print_func("Paste in the console to download active mods:")
		for source,code in pairs(codes) do
			print_func("\t", code)
		end
		print_func("\n")
	end
end


----- loading

ModEnvBlacklist = {
	--HG values
	IsDlcOwned = true,
	AccountStorage = true,
	async = true,
	AsyncOpWait = true,
	FirstLoad = true,
	InitDefaultAccountStorage = true,
	ReloadLua = true,
	SetAccountStorage = true,
	SaveAccountStorage = true,
	XPlayerActivate = true,
	XPlayersReset = true,
	WaitLoadAccountStorage = true,
	WaitSaveAccountStorage = true,
	_DoSaveAccountStorage = true,
	ConsoleExec = true,
	Crash = true,
	Stomp = true,
	Msg = true,
	OnMsg = true,
	ModMsgBlacklist = true,
	GetAutoCompletionList = true,
	GedOpInspectorSetGlobal = true,
	getfileline = true,
	CompileExpression = true,
	CompileFunc = true,
	GetFuncSourceString = true,
	GetFuncSource = true,
	FuncSource = true,
	LoadConfig = true,
	SVNDeleteFile = true,
	SVNAddFile = true,
	SVNMoveFile = true,
	SVNLocalInfo = true,
	SVNShowLog = true,
	SVNShowBlame = true,
	SVNShowDiff = true,
	SaveSVNFile = true,
	GetSvnInfo = true,
	GetCallLine = true,
	SaveLuaTableToDisk = true,
	LoadLuaTableFromDisk = true,
	insideHG = true,
	SaveLanguageOption = true,
	GetMachineID = true,
	SaveDLCOwnershipDataToDisk = true,
	LoadDLCOwnershipDataFromDisk = true,
	GetLuaSaveGameData = true,
	GetLuaLoadGamePermanents = true,
	--file operations
	DbgPackMod = true,
	AsyncAchievementUnlock = true,
	AsyncCopyFile = true,
	AsyncCreatePath = true,
	AsyncDeletePath = true,
	AsyncExec = true,
	AsyncFileClose = true,
	AsyncFileDelete = true,
	AsyncFileFlush = true,
	AsyncFileOpen = true,
	AsyncFileRead = true,
	AsyncFileRename = true,
	AsyncFileToString = true,
	AsyncFileWrite = true,
	AsyncGetFileAttribute = true,
	AsyncGetSourceInfo = true,
	AsyncListFiles = true,
	AsyncMountPack = true,
	AsyncPack = true,
	AsyncStringToFile = true,
	AsyncSetFileAttribute = true,
	AsyncUnmount = true,
	AsyncUnpack = true,	
	CheatPlatformUnlockAllAchievements = true,
	CheatPlatformResetAllAchievements = true,
	CopyFile = true,
	DeleteFolderTree = true,
	EV_OpenFile = true,
	FileToLuaValue = true,
	LoadFilesForSearch = true,
	MountFolder = true,
	MountPack = true,
	OS_OpenFile = true,
	PreloadFiles = true,
	StringToFileIfDifferent = true,
	Unmount = true,
	DeleteMod = true,
	--web operations
	AsyncWebRequest = true,
	AsyncWebSocket = true,
	hasRfnPrefix = true,
	LocalIPs = true,
	sockAdvanceDeadline = true,
	sockConnect = true,
	sockDelete = true,
	sockDisconnect = true,
	sockEncryptionKey = true,
	sockGenRSAEncryptedKey = true,
	sockGetGroup = true,
	sockGetHostName = true,
	sockGroupStats = true,
	sockListen = true,
	sockNew = true,
	sockProcess = true,
	sockResolveName = true,
	sockSend = true,
	sockSetGroup = true,
	sockSetOption = true,
	sockSetRSAEncryptedKey = true,
	sockStr = true,
	sockStructs = true,
	--mod loading
	ModEnvBlacklist = true,
	LuaModEnv = true,
	ModsReloadDefs = true,
	ModsPackFileName = true,
	ModsScreenshotPrefix = true,
	CanUnlockAchievement = true,
	ModIdBlacklist = true,
	ModsReloadItems = true,
	ProtectedModsReloadItems = true,
	ContinueModsReloadItems = true,
	
	--built-ins
	_G = true,
	getfenv = true,
	setfenv = true,
	getmetatable = true,
	--setmetatable = true,
	rawget = true,
	collectgarbage = true,
	load = true,
	loadfile = true,
	loadstring = true,
	dofile = true,
	pdofile = true,
	dofolder = true,
	dofolder_files = true,
	dofolder_folders = true,
	dostring = true,
	module = true,
	require = true,
	--libraries
	debug = true,
	io = true,
	os = true,
	package = true,
	lfs = true,
}

ModMsgBlacklist = {
	PersistGatherPermanents = true,
	PersistLoad = true,
	PersistSave = true,
	ModBlacklistPrefixes = true,
	DebugDownloadMods = true,
	PasswordChanged = true,
	UnableToUnlockAchievementReasons = true,
}

function OnMsg.Autorun()
	local string_starts_with = string.starts_with
	local prefixes = {"Debug"}
	Msg("ModBlacklistPrefixes", prefixes)
	for key, value in pairs(_G) do
		if type(key) == "string" then
			for _, prefix in ipairs(prefixes) do
				if string_starts_with(key, prefix, true) then
					ModEnvBlacklist[key] = true
					break
				end
			end
		end
	end
	ModEnvBlacklist.DebugPrint = nil -- this is just a log without screen output
end

const.MaxModDataSize = 32 * 1024

--[[@@@
Writes data into a persistent storage, that can be accessed between different game sessions.
The data must be a string, no longer than *const.MaxModDataSize* - make sure to always check if you're exceeding this size.
This storage is not shared, but is per mod. Anything stored here can only be read by the same mod using [ReadModPersistentData](#ReadModPersistentData).
@function err WriteModPersistentData(data)
@param data - the data to be stored (as a string).
@result err - error message or nil, if successful.
See also: [TupleToLuaCode](#TupleToLuaCode), [Compress](#Compress), [AsyncCompress](#AsyncCompress);
]]
local max_data_length = const.MaxModDataSize
local function WriteModPersistentData(mod, data)
	if type(data) ~= "string" then
		return "data must be a string"
	end

	if #data > max_data_length then
		return string.format("data longer than const.MaxModDataSize (%d bytes)", max_data_length)
	end
	
	if not AccountStorage.ModPersistentData then
		AccountStorage.ModPersistentData = { }
	end
	
	if AccountStorage.ModPersistentData[mod.id] == data then return end
	AccountStorage.ModPersistentData[mod.id] = data
	SaveAccountStorage(5000)
end

--[[@@@
Reads data from a persistent storage, that can be accessed between different game sessions.
This storage is not shared, but is per mod. Anything read here has been previously stored only by the same mod using [WriteModPersistentData](#WriteModPersistentData).
@function err, data ReadModPersistentData()
@result err - error message or nil, if successful.
@result data - data previously stored or nil.
See also: [LuaCodeToTuple](#LuaCodeToTuple), [Decompress](#Decompress), [AsyncDecompress](#AsyncDecompress);
]]
local function ReadModPersistentData(mod)
	return nil, AccountStorage.ModPersistentData and AccountStorage.ModPersistentData[mod.id]
end

--[[@@@
Writes `CurrentModStorageTable` into a persistent storage, that can be accessed between different game sessions.
This is an ease-of-use function for the most common use case of persistent storage - when storing data in a table.
It uses [WriteModPersistentData](#WriteModPersistentData) internally, thus the *const.MaxModDataSize* limit applies.
@function err WriteModPersistentStorageTable()
@result err - error message or nil, if successful.
]]
local function WriteModPersistentStorageTable(mod)
	local storage = rawget(mod.env, "CurrentModStorageTable")
	if type(storage) ~= "table" then
		storage = {}
	end
	local data = TupleToLuaCode(storage)
	return WriteModPersistentData(mod, data)
end

local function CreateModPersistentStorageTable(mod)
	if not AccountStorage then
		WaitLoadAccountStorage()
	end
	local storage
	local err, data = ReadModPersistentData(mod)
	if not err then
		err, storage = LuaCodeToTuple(data, mod.env)
	end
	if type(storage) ~= "table" then
		storage = {}
	end
	return storage
end

function LuaModEnv(env)
	assert(ModEnvBlacklist[1] == nil, "All entries in 'ModEnvBlacklist' must be keys")

	env = env or { }
	local env_meta = { __name = "ModEnv" }
	local original_G = _G
	
	--setup black/white lists
	local value_whitelist = { } --for faster access
	local env_blacklist = ModEnvBlacklist
	local meta_blacklist = { }
	meta_blacklist[env_meta] = true
	meta_blacklist[original_G] = true
	
	--setup environment
	for k in pairs(value_whitelist) do
		if env[k] == nil then
			env[k] = original_G[k]
		end
	end
	
	--setup environment metatable
	env_meta.__index = function(env, key)
		if env_blacklist[key] then return end
		local value = rawget(original_G, key)
		if value ~= nil then
			return value
		end
		if key == "class" then return "" end
		if key == "__ancestors" then return empty_table end
		error("Attempt to use an undefined global '" .. tostring(key) .. "'", 1)
	end
	
	env_meta.__newindex = function(env, key, value)
		if env_blacklist[key] then return end
		if not Loading and PersistableGlobals[key] == nil then
			error("Attempt to create a new global '" .. tostring(key) .. "'", 1)
		end
		rawset(original_G, key, value)
	end
	
	--setup exposed power functions
	local function safe_getmetatable(t)
		local meta = getmetatable(t)
		if meta_blacklist[meta] then return end
		return meta
	end
	local function safe_setmetatable(t, new_meta)
		local meta = getmetatable(t)
		if meta_blacklist[meta] then return end
		return setmetatable(t, new_meta)
	end
	
	local function safe_rawget(t, key)
		local t_value = rawget(t, key)
		if rawequal(t, env) and t_value == nil and not env_blacklist[key] then
			return rawget(original_G, key)
		end
		return t_value
	end
	
	local function safe_Msg(name, ...)
		if ModMsgBlacklist[name] then return end
		local raw_Msg = original_G.Msg
		return raw_Msg(name, ...)
	end
	local safe_OnMsg = { }
	setmetatable(safe_OnMsg, { __newindex = 
		function(_, name, func)
			if ModMsgBlacklist[name] then return end
			local raw_OnMsg = original_G.OnMsg
			raw_OnMsg[name] = func
		end
	})
	
	--finilize setting up the environment and fill in some tables
	env._G = env
	env.getmetatable = safe_getmetatable
	--env.setmetatable = safe_setmetatable
	env.rawget = safe_rawget
	env.os = { time = os.time }
	env.Msg = safe_Msg
	env.OnMsg = safe_OnMsg
	
	setmetatable(env, env_meta)
	return env
end

if FirstLoad then
	SharedModEnv = { }
end

function ModDef:SetupEnv()
	local env = self.env
	rawset(env, "CurrentModPath", self.content_path)
	rawset(env, "CurrentModId", self.id)
	rawset(env, "CurrentModDef", self)
	rawset(env, "CurrentModStorageTable", CreateModPersistentStorageTable(self))
	rawset(env, "CurrentModOptions", self.options)
	
	rawset(env, "WriteModPersistentData", function(...)
		return WriteModPersistentData(self, ...)
	end)
	rawset(env, "ReadModPersistentData", function(...)
		return ReadModPersistentData(self, ...)
	end)
	rawset(env, "WriteModPersistentStorageTable", function(...)
		return WriteModPersistentStorageTable(self, ...)
	end)
end

function OnMsg.PersistGatherPermanents(permanents)
	permanents["func:getmetatable"] = getmetatable
	permanents["func:setmetatable"] = setmetatable
	permanents["func:os.time"] = os.time
	permanents["func:Msg"] = Msg
end

function CanLoadUnpackedMods()
	return not Platform.console --or Platform.developer
end

function ListModFolders(path, source)
	path = SlashTerminate(path)
	local folders = io.listfiles(path, "*", "folders")
	table.sort(folders, CmpLower)
	
	if next(folders) then
		local folder_names = table.imap(folders, string.sub, #path + 1)
	end
	
	for i=1,#folders do
		folders[i] = { path = folders[i], source = source }
	end
	
	return folders
end

function SortModsList()
	if #(ModsList or "") <= 1 then return end
	table.sort(ModsList, function(a, b)
		if b:ItemsLoaded() then
			return a:ItemsLoaded() and a.title < b.title
		end
		return a:ItemsLoaded() or a.title < b.title
	end)
	ObjModified(ModsList)
end

if FirstLoad then
	g_ModDefSourceNotified = {}
end

function ModsReloadDefs()
	--load all places where a mod can be found
	local folders = { }
	if Platform.desktop then
		local f = ListModFolders("AppData/Mods/", "appdata")
		table.iappend(folders, f)
	end
	if config.AdditionalModFolder then
		local f = ListModFolders(config.AdditionalModFolder, "additional")
		table.iappend(folders, f)
	end
	Msg("GatherModDefFolders", folders)
	
	--to avoid issues, when loading metadata, mods are allowed to access only this function
	--nothing else exists in their environment
	local descriptor_classes = ClassDescendantsList("ModResourceDescriptor")
	local metadata_env = {
		PlaceObj = function(class, ...)
			if class ~= "ModDef" and class ~= "ModDependency" and not table.find(descriptor_classes, class) then return end
			return PlaceObj(class, ...)
		end,
		box = box,
	}

	local new_mods = { }
	
	local multiple_sources
	for i,folder in ipairs(folders) do
		--execute the mod metadata.lua file
		local env, ok, def
		local source = folder.source
		local pack_path = folder.path .. "/" .. ModsPackFileName
		--Get last subfolder from a path or complete path if it has no slash in it
		--SplitPath does not work for paths containing a dot
		local folder_name = string.sub(folder.path, (string.match(folder.path, "^.*()/") or 0) + 1)
		if io.exists(pack_path) then
			local prev_id = LocalStorage.ModIdCache and LocalStorage.ModIdCache[folder.path]
			local hpk_mounted_path = ModContentPath .. (prev_id or folder_name) .. "/"
			local mount_label = ModDef:GenerateMountLabel()
			local err = MountPack(hpk_mounted_path, pack_path, "label:" .. mount_label)
			if not err then
				env = LuaModEnv()

				ok, def = pdofile(hpk_mounted_path .. "metadata.lua", metadata_env, "t")
				if ok and IsKindOf(def, "ModDef") then
					def.packed = true
					def.mount_label = mount_label
					Msg("PackedModDefLoaded", pack_path, def)
					if prev_id ~= def.id then
						UnmountByLabel(mount_label)
						LocalStorage.ModIdCache = LocalStorage.ModIdCache or {}
						LocalStorage.ModIdCache[folder.path] = def.id
						SaveLocalStorage()
					else
						def.mounted = true
					end
				else
					UnmountByLabel(mount_label)
				end
			end
		elseif (folder.source == "appdata" or folder.source == "additional") or CanLoadUnpackedMods() then
			env = LuaModEnv()
			ok, def = pdofile(folder.path .. "/metadata.lua", metadata_env, "t")
		end
		
		--finilize loading the mod definition and replace old definitions, if needed
		if env and IsKindOf(def, "ModDef") then
			def.env = env
			def.path = folder.path .. "/" --don't need to use 'ChangePaths()' here
			def.content_path = ModContentPath .. def.id .. "/"
			def.source = source
			
			local mod_used
			if def:IsTooOld() then
				ModLogF("Outdated definition for %s loaded from %s. (Unsupported game version)", def:GetModLabel("plainText"), def.source)
			end
			
			local old = new_mods[def.id]
			if old then
				multiple_sources = table.create_set(multiple_sources, def.id, true)
				local cmp = old:CompareVersion(def)
				if cmp < 0 or (cmp == 0 and old.packed and not def.packed) then
					mod_used = true
				end
			else
				mod_used = true
			end
			
			if mod_used then
				if old then
					old:delete()
				end
				
				new_mods[def.id] = def
				def:SetupEnv()
				def:MountContent()
				def:OnLoad()
			else
				def:delete()
			end
		else
			local err = def
			if not err:ends_with("File Not Found") then
				ModLogF(true, "Failed to load mod metadata from %s. Error: %s", folder.path, err)
			end
		end
	end

	for id in pairs(multiple_sources) do
		local def = new_mods[id]
		if g_ModDefSourceNotified[id] ~= def.path then
			g_ModDefSourceNotified[id] = def.path
			local packed_str = def.packed and "packed" or "unpacked"
			ModLogF("Mod %s loaded from %s (%s)", def:GetModLabel("plainText"), def.source, packed_str)
		end
	end

	local old_mods = Mods
	local new_ids, old_ids = table.keys(new_mods), table.keys(old_mods)
	local any_changes = not (table.is_subset(new_ids, old_ids) and table.is_subset(old_ids, new_ids))
	if not any_changes then
		for id, new_mod in pairs(new_mods) do
			local old_mod = old_mods[id]
			if not old_mod or new_mod ~= old_mod then
				any_changes = true
				break
			end
		end
	end
	if not any_changes then
		for id, new_mod in pairs(new_mods) do
			new_mod:delete()
		end
	
		ModsList = ModsList or {}
		return
	end

	--delete all old mods
	local any_loaded = not not next(ModsLoaded)
	for id,mod in pairs(Mods or empty_table) do
		mod:delete()
	end

	Mods = new_mods
	
	-- add a reference to the mod in affected resources
	for id, mod in pairs(Mods) do
		if mod.affected_resources then
			for _, res in ipairs(mod.affected_resources) do
				res.mod = mod
			end
		end
	end
	
	-- if new mod defs are loaded/unloaded, clear the affected resources cache
	ClearModsAffectedResourcesCache()
	
	ModsList = {}
	for id, mod in pairs(Mods) do
		ModsList[#ModsList+1] = mod
		mod_print("once", "Loaded mod def %s (id %s, v%s) %s from %s", mod.title, mod.id, mod:GetVersionString(), mod.packed and "packed" or "unpacked", mod.source)
	end
	SortModsList()
	CacheModDependencyGraph()
	Msg("ModDefsLoaded")

	if any_loaded then
		ModsReloadItems()
	end
end

local function GetModAllDependencies(mod)
	local result = { }
	
	--local dependencies
	for i,dep in ipairs(mod.dependencies or empty_table) do
		if dep.id and dep.id ~= "" and not table.find(result, "id", dep.id) then
			table.insert(result, dep)
		end
	end
	return result
end

local function GetModDependenciesList(mod, result)
	result = result or { }
	if mod then
		local dependencies = GetModAllDependencies(mod)
		for i,dep in ipairs(dependencies) do
			local dep_id = dep.id
			local dep_mod = Mods[dep_id]
			if dep_mod then
				if table.find(result, dep_mod) then
					return "cycle"
				end
				table.insert(result, dep_mod)
				local err = GetModDependenciesList(dep_mod, result)
				if err then return err end
			else
				table.insert(result, dep_id)
			end
		end
	end
	return false, result
end

local function DetectDependencyError(dep, all, dep_mod, stack)
	if stack[dep.id] then --cycling dependencies
		return "cycle"
	elseif not table.find(all, dep.id) then --not in 'mods to load' list
		return "not loaded"
	elseif not dep:ModFits(dep_mod) then --incompatible
		return "incompatible"
	end
end

local function EnqueueMod(mod, all, queue, stack)
	if not mod then return "no mod" end
	if queue[mod.id] then return end
	
	local mod_queue = table.copy(queue)
	table.insert_unique(mod_queue, mod.id)
	mod_queue[mod.id] = mod

	local dependencies = GetModAllDependencies(mod)
	if next(dependencies) then
		stack[mod.id] = true
		for i,dep in ipairs(dependencies) do
			local dep_mod = Mods[dep.id]
			--forced mods don't go through dependency checks
			if not mod.force_reload then
				if dep_mod then --mod is preset
					local err = DetectDependencyError(dep, all, dep_mod, stack)
					if err then
						if err == "cycle" then
							local other_mods = { }
							for id in pairs(stack) do
								table.insert(other_mods, Untranslated(Mods[id].title))
							end
							ModLogF("Mod %s creates circular dependency cycle with %s.", mod:GetModLabel("plainText"), other_mods)
						elseif err == "not loaded" then
							ModLogF("Cannot load %s because required mod %s is not active.", mod:GetModLabel("plainText"), dep_mod:GetModLabel("plainText"))
						elseif err == "incompatible" then
							ModLogF("Cannot load %s because required mod %s is not compatible.", mod:GetModLabel("plainText"), dep_mod:GetModLabel("plainText"))
						end
						
						stack[mod.id] = nil
						if dep.required then
							return err
						end
					end
				else --mod is not present
					ModLogF("Cannot load %s because required mod %s is not found.", mod:GetModLabel("plainText"), dep.title)
					stack[mod.id] = nil
					return "not found"
				end
			end
			
			local err = EnqueueMod(dep_mod, all, mod_queue, stack)
			if err then
				--forced mods must be loaded no matter what
				if not mod.force_reload then
					stack[mod.id] = nil
					return err
				end
			end
		end
		stack[mod.id] = nil
	end
	
	for i, mod_id in ipairs(mod_queue) do
		if table.find(all, mod_id) then
			table.insert_unique(queue, mod_id)
			queue[mod_id] = mod_queue[mod_id]
		end
	end
end

local function GetLoadingQueue(list)
	local queue = { }
	for i, mod_id in ipairs(list) do
		EnqueueMod(Mods[mod_id], list, queue, { })
	end
	return queue
end


function GetModsToLoad()
	if not IsUserCreatedContentAllowed() then
		return empty_table
	end

	local list
	if (AccountStorage and AccountStorage.LoadAllMods) or config.LoadAllMods then
		list = table.keys(Mods)
		table.sort(list)
	end
	list = list or AccountStorage and table.icopy(AccountStorage.LoadMods) or {}

	-- Autodisable blacklisted mods
	AutoDisabledModsAlertText = { ["ban"] = {}, ["deprecate"] = {}}
	for idx, modId in ripairs(list) do
		local blacklistReason = GetModBlacklistedReason(modId)
		LocalStorage.AutoDisableDeprecated = LocalStorage.AutoDisableDeprecated or {} 
		local disableMod = (not LocalStorage.AutoDisableDeprecated[modId] and blacklistReason and blacklistReason == "deprecate") or (blacklistReason and blacklistReason == "ban")
		if disableMod and blacklistReason and Mods[modId] then
			table.insert(AutoDisabledModsAlertText[blacklistReason], Mods[modId]:GetModLabel("plainText"))
			TurnModOff(modId)
			LocalStorage.AutoDisableDeprecated = LocalStorage.AutoDisableDeprecated or {} 
			LocalStorage.AutoDisableDeprecated[modId] = true
			for _, presetData in pairs(LocalStorage.ModPresets) do
				if presetData.mod_ids[modId] then
					RemoveModFromModPreset(presetId, modId)
				end
			end
			table.remove(list, idx)
		end
	end
	
	SaveLocalStorage()
	
	list = table.ifilter(list, function(i, id)
		local mod = Mods[id]
		if not mod then
			ModLogF("Couldn't find mod %s from your account storage.", id)
			return
		end
		if not mod:IsTooOld() or mod.force_reload then
			return true
		else
			ModLogF("Outdated mod %s cannot be loaded. (Unsupported game version)", mod:GetModLabel("plainText"))
		end
	end)

	return GetLoadingQueue(list)
end

if FirstLoad then
g_CantLoadMods = {}
end

ErrorLoadingModsT = T(164802745041, "The following mods couldn't be loaded. Open the mod manager for more information:\n")
function WaitErrorLoadingMods(customErrorMsg)
	local errorMsg = customErrorMsg or ErrorLoadingModsT
	local modsToLoad = GetModsToLoad()
	local modsEnabledByUser
	if (AccountStorage and AccountStorage.LoadAllMods) or config.LoadAllMods then
		modsEnabledByUser = table.keys(Mods)
		table.sort(modsEnabledByUser)
	end
	modsEnabledByUser = modsEnabledByUser or AccountStorage and table.icopy(AccountStorage.LoadMods) or {}
	
	local modsFailedToLoad
	for idx, mod_id in ipairs(modsEnabledByUser) do
		if not table.find(modsToLoad, mod_id) then
			modsFailedToLoad = table.create_add(modsFailedToLoad, "<space>" .. (Mods[mod_id] and Mods[mod_id].title or mod_id))
			TurnModOff(mod_id, "updatePreset")
			g_CantLoadMods[mod_id] = true
		end
	end
	
	if g_ModsUIContextObj then
		g_ModsUIContextObj:GetInstalledMods()
	end
	
	if modsFailedToLoad then
		SaveAccountStorage(5000)
		modsFailedToLoad = table.concat(modsFailedToLoad, "\n")
		WaitMessage(
			terminal.dekstop,
			T(824112417429, "Warning"),
			T{675270242132, "<errorT><em><mods_list></em>", errorT = errorMsg, mods_list = Untranslated(modsFailedToLoad)},
			T(325411474155, "OK")
		)
	end
end

function ModsReloadItems(map_folder, force_reload, first_load)
	if not config.Mods then return end
	assert(IsRealTimeThread())
	
	local queue = GetModsToLoad()
	local list = table.copy(queue)
	table.sort(list)

	if not force_reload then
		--no changes to mods list?
		local loaded_ids = ModsLoaded and table.map(ModsLoaded, "id") or {}
		table.sort(loaded_ids)
		if table.iequal(loaded_ids, list) then
			return
		end
	end
	
	local reload_assets
	local reload_lua
	local has_data
	
	--unload old mods
	if ModsLoaded then
		for _,mod in ipairs(ModsLoaded) do
			if mod:ItemsLoaded() or mod.status == "deleted" then
				has_data = has_data or mod.has_data
				mod:UnloadItems()
				mod:UnloadOptions()
				--print("Unload mod", mod.id)
				if next(mod.code) then reload_lua = true end
				if next(mod.entities) then reload_assets = true end
			end
		end
	end
	
	--fill ModsLoaded with the new mods
	local old_loaded = ModsLoaded
	ModsLoaded = {}

	for i, id in ipairs(queue) do
		local mod = Mods[id]
		mod.force_reload = false
		assert(not table.find(ModsLoaded, mod))
		ModsLoaded[#ModsLoaded + 1] = mod
		if next(mod.code) then reload_lua = true end
		if next(mod.entities) then reload_assets = true end
	end

	--reload bin assets, if needed
	if reload_assets then
		RegisterModDelayedLoadEntities(ModsLoaded)
		if not first_load then
			ModsLoadAssets(map_folder)
			WaitDelayedLoadEntities()
		end
	end
	
	--loading options happens before the items and code to allow for access of CurrentModOptions
	for _, mod in ipairs(ModsLoaded) do
		mod:LoadOptions()
	end
	
	--reload Lua, if needed
	if first_load then
		return --will be continued during ReloadForDlc
	end
	
	if reload_lua then
		for i,mod in ipairs(old_loaded) do
			if not queue[mod.id] then
				--@@@msg ModUnloadLua, mod_id - fired just before unloading a mod with Lua code.
				Msg("ModUnloadLua", mod.id)
			end
		end
		ReloadLua()
	end

	return ContinueModsReloadItems(map_folder, reload_assets, has_data)
end

function ContinueModsReloadItems(map_folder, reload_assets, has_data)
	--load the items of the new mods
	for _, mod in ipairs(ModsLoaded) do
		if not mod:ItemsLoaded() then
			mod:LoadItems()
		end
		has_data = has_data or mod.has_data
	end
	mod_print("Loaded mod items for: %s", table.concat(table.map(ModsLoaded, "id"), ", "))

	--backwards compatibility for r342666
	local any_old_entity_mods
	for i, mod in ipairs(ModsLoaded) do
		if mod.bin_assets then
			mod:ForEachModItem("ModItemEntity", function(item)
				any_old_entity_mods = true
				DelayedLoadEntity(mod, item.entity_name)
			end)
		end
	end
	if any_old_entity_mods then
		RegisterModDelayedLoadEntities(ModsLoaded)
		ModsLoadAssets(map_folder)
		WaitDelayedLoadEntities()
		ReloadLua()
	end

	if reload_assets then
		ReloadClassEntities()
	end
	
	for _, mod in ipairs(ModsLoaded) do
		if mod:HasOptions() then
			Msg("ApplyModOptions", mod.id) --throw the msg here to make sure it is called after the lua is reloaded and everything for the mod is loaded
		end
	end

	PopulateParentTableCache(Mods)
	ObjModified(ModsList)
	--@@@msg ModsReloaded - fired right after mods are loaded, unloaded or changed.
	Msg("ModsReloaded")
	-- TODO: use has_data to raise a msg about changed data
end

function ProtectedModsReloadItems(map_folder, force_reload)
	LoadingScreenOpen("idLoadingScreen", "reload mod items")
	local old_render_mode = GetRenderMode()
	WaitRenderMode("ui")
	ModsReloadItems(map_folder, force_reload)
	WaitRenderMode(old_render_mode)
	LoadingScreenClose("idLoadingScreen", "reload mod items")
end

function ModsLoadAssets(map_folder)
	LoadingScreenOpen("idModEntitesReload", "ModEntitesReload")
	local old_render_mode = GetRenderMode()
	WaitRenderMode("ui")
	local lastMap = AreModdingToolsActive() and ModEditorMapName or CurrentMap --force reload the map to prevent issues with spawned objects from the mod on the current map
	ResetGameSession()
	ForceReloadBinAssets()
	DlcReloadAssets(DlcDefinitions)
	--actually reload the assets
	LoadBinAssets(map_folder or CurrentMapFolder)
	--wait & unmount
	while AreBinAssetsLoading() do
		Sleep(1)
	end

	ChangeMap(lastMap)
	WaitRenderMode(old_render_mode)
	hr.TR_ForceReloadNoTextures = 1
	LoadingScreenClose("idModEntitesReload", "ModEntitesReload")
end

if FirstLoad then
	LuaLoadedForMods = {}
	ModsPreGameMenuOpen = false
	ModsLoadCodeErrorsMessage = false
	ModsDisplayingMessage = false
end

local loadedWithErrorsT = T(306573510595, "Mod Loaded with Errors")

function OnMsg.PreGameMenuOpen()
	CreateRealTimeThread(function()
		ModsDisplayingMessage = true
		-- display mods that were loaded with errors
		if ModsLoadCodeErrorsMessage then
			WaitMessage(nil, loadedWithErrorsT, ModsLoadCodeErrorsMessage, T(1000136, "OK"))
			ModsLoadCodeErrorsMessage = false
		end
		-- display mods that were rejected (because of dependencies, blacklisting, etc.)
		WaitErrorLoadingMods()
		ModsDisplayingMessage = false
	end)
end

function DisplayModsLoadCodeErrorsMessage()
	CreateRealTimeThread(function()
		ModsDisplayingMessage = true
		WaitMessage(nil, loadedWithErrorsT, ModsLoadCodeErrorsMessage, T(1000136, "OK"))
		ModsLoadCodeErrorsMessage = false
		ModsDisplayingMessage = false
	end)
end

function ModsLoadCode() -- called while reloading Lua (in autorun.lua)
	local collected_errors = {}
	for _, mod in ipairs(ModsLoaded or empty_table) do
		local loading_errors = mod:LoadCode()
		if loading_errors then
			local errs = table.concat(loading_errors, "\n")
			ModLogF(true, string.format("Errors while loading mod %s:\n%s", mod.title, errs)) -- log in the mods log
			
			-- collect all mod loading errors to display to the user in a single message box
			table.insert(collected_errors,
				T{560233458867, "Mod <em><u(literal(title))></em>:\n<u(literal(errs))>", title = mod.title, errs = errs })
		end
	end
	
	if next(collected_errors) then
		ModsLoadCodeErrorsMessage = table.concat(collected_errors, "\n\n")
		--- if the pre-game menu hasn't been opened yet, the message will be displayed when it is
		if (config.MainMenu == 0 or ModsPreGameMenuOpen) and not ModsDisplayingMessage then
			DisplayModsLoadCodeErrorsMessage()
		end
	end
end

function ModsLoadLocTables()
	local list
	if not config.Mods then return end
	if AccountStorage and AccountStorage.LoadAllMods or config.LoadAllMods then
		list = table.keys(Mods)
		table.sort(list)
	end
	list = list or AccountStorage and AccountStorage.LoadMods or {}
	
	local loctables_loaded
	for i, id in ipairs(list) do
		local mod = Mods[id]
		if mod then
			if mod:IsTooOld() then
				ModLogF("Outdated mod %s cannot be loaded. (Unsupported game version)", mod:GetModLabel("plainText"))
			else
				for _, loctable in ipairs(mod.loctables or empty_table) do
					if loctable.language == GetLanguage() or loctable.language == "Any" then
						local file_path = mod.content_path .. loctable.filename
						if io.exists(file_path) then
							LoadTranslationTableFile(file_path)
							loctables_loaded = true
						end
					end
				end
			end
		end
	end
	if loctables_loaded then
		Msg("TranslationChanged")
	end
end

local function DebugWaitThreads(msg, ...)
	local threads = {}
	Msg(msg, threads, ...)
	while next(threads) do
		for i = #threads, 1, -1 do
			if not IsValidThread(threads[i]) then
				table.remove(threads, i)
			end
		end
		Sleep(100)
	end
end

function DebugWaitDownloadExternalMods(mods)
	DebugWaitThreads("DebugDownloadExternalMods", mods)
end

function DebugWaitCopyExternalMods(mods)
	DebugWaitThreads("DebugCopyExternalMods", mods)
end

function DebugDownloadSavegameMods(missings_mods)
	if not IsRealTimeThread() then
		CreateRealTimeThread(DebugDownloadSavegameMods)
		return
	end
	local mods = {}
	for _, mod in ipairs(missings_mods or SavegameMeta.active_mods) do
		if not Mods[mod.id] then
			mods[#mods + 1] = mod
		else
			printf("Mod with ID %s is already present in your local files.", mod.id)
		end
	end
	DebugWaitDownloadExternalMods(mods)
	DebugWaitCopyExternalMods(mods)
	for _, mod in ipairs(missings_mods or SavegameMeta.active_mods) do
		TurnModOn(mod.id)
	end
	SaveAccountStorage()
	ModsReloadDefs()
	ModsReloadItems()
end


----- ModItem

DefineClass.ModItem = {
	__parents = { "GedEditedObject", "InitDone", "ModElement", "Container" },
	properties = {
		{ category = "Mod", id = "name", name = "Name", default = "", editor = "text", },
		{ category = "Mod", id = "comment", name = "Comment", default = "", editor = "text", },
		{ category = "Mod", id = "Documentation", editor = "documentation", dont_save = true, sort_order = 9999999, }, -- display collapsible Documentation at this position
	},
	mod = false,
	EditorName = false,
	EditorView = Untranslated("<color 128 128 128><u(EditorName)></color><opt(ModItemDescription,' ','')><opt(u(comment),' <color 75 105 198>','</color>')>"),
	ModItemDescription = T(674857971939, "<u(name)>"),
	ContainerAddNewButtonMode = "children", -- add a + button to add child item in the tree view (if the ModItem can have children)
	GedTreeCollapsedByDefault = true,
}

function ModItem:IsOpenInGed()
	return not not GedObjects[ParentTableCache[self.mod]]
end

function ModItem:OnEditorNew(parent, ged, is_paste, duplicate_id, mod_id)
	-- Mod item presets can also be added through Preset Editors (see GedOpClonePresetInMod)
	-- In those cases the reference to the mod will be set from the mod_id parameter

	self.mod = (IsKindOf(parent, "ModDef") and parent or parent.mod) or (mod_id and Mods and Mods[mod_id])
	assert(self.mod, "Mod item has no reference to a mod")
end

function ModItem:OnAfterEditorNew(parent, ged, is_paste, old_id, mod_id)
	-- Mod item presets can also be added through Preset Editors (see GedOpClonePresetInMod)
	-- In those cases the reference to the mod will be set from the mod_id parameter

	self.mod = (IsKindOf(parent, "ModDef") and parent or parent.mod) or (mod_id and Mods and Mods[mod_id])
	assert(self.mod, "Mod item has no reference to a mod")
end

-- only used for ModItemPreset, which is not a ModItemUsingFiles
function ModItem:OnEditorDelete(mod, ged)
	local path = self:GetCodeFilePath()
	if path and path ~= "" then
		AsyncFileDelete(path)
	end
end

function ModItem:GetPropOSPathKey(prop_id)
	return string.format("%s_%s_%s", self.class, self.name, prop_id)
end

function ModItem:StoreOSPath(prop_id, value)
	local prop_meta = self:GetPropertyMetadata(prop_id)
	if prop_meta and prop_meta.os_path and prop_meta.dont_save then
		local key = self:GetPropOSPathKey(prop_id)
		table.set(LocalStorage, "ModItemOSPaths", self.mod.id, key, value)
		SaveLocalStorageDelayed()
	end
end

function ModItem:StoreOSPaths(prop_id, value)
	for i, prop_meta in ipairs(self:GetProperties()) do
		if prop_meta.os_path and prop_meta.dont_save then
			local prop_id = prop_meta.id
			local value = self:GetProperty(prop_id)
			self:StoreOSPath(prop_id, value)
		end
	end
end

function ModItem:RestoreOSPaths()
	for i, prop_meta in ipairs(self:GetProperties()) do
		if prop_meta.os_path and prop_meta.dont_save then
			local prop_id = prop_meta.id
			local key = self:GetPropOSPathKey(prop_id)
			local value = table.get(LocalStorage, "ModItemOSPaths", self.mod.id, key)
			if value ~= nil and not self:IsDefaultPropertyValue(prop_id, prop_meta, value) then
				self:SetProperty(prop_id, value)
			end
		end
	end
end

function ModItem:OnEditorSetProperty(prop_id, old_value, ged)
	self:StoreOSPath(prop_id, self:GetProperty(prop_id))
end

function ModItem:OnEditorSelect(selected, ged)
end

function ModItem:IsMounted()
	return self.mod and self.mod:IsMounted()
end

function ModItem:IsPacked()
	return self.mod and self.mod:IsPacked()
end

function ModItem:GetModRootPath()
	return self.mod and self.mod:GetModRootPath()
end

function ModItem:GetModContentPath()
	return self.mod and self.mod:GetModContentPath()
end

function ModItem:OnModLoad(mod)
	self:RestoreOSPaths()
	return ModElement.OnLoad(self, mod)
end

function ModItem:OnModUnload(mod)
	return ModElement.OnUnload(self, mod)
end

function ModItem:TestModItem(ged)
end

function ModItem:GetCodeFileName(name)
end

function ModItem:GetCodeFilePath(name)
	name = self:GetCodeFileName(name)
	if not name or name == "" then return "" end
	return self.mod and self.mod.content_path .. name
end

function ModItem:FindFreeFilename(name)
	local n = 1
	local file_name = name
	while io.exists(self:GetCodeFilePath(file_name)) do
		n = n + 1
		file_name = name .. tostring(n)
	end
	return file_name
end

function ModItem:CleanupForSave(injected_props, restore_data)
	restore_data = PropertyObject.CleanupForSave(self, injected_props, restore_data)
	restore_data[#restore_data + 1] = { obj = self, key = "mod", value = self.mod }
	self.mod = nil
	return restore_data
end

function ModItem:PreSave()
	self:StoreOSPaths()
	return ModElement.PreSave(self)
end

function ModItem:GetAffectedResources()
	return empty_table
end

function ModItem:GetMapName()
	-- return the map name if the mod item contains an editor map
end

function ModItem:ForEachModItem(classname, fn)
	if not fn then
		fn = classname
		classname = nil
	end
	if classname and not IsKindOf(self, classname) then return end
	return fn(self)
end

----- ModOptions

function ModOptionEditorContext(context, prop_meta)
	local value_fn = function() return context:GetProperty(prop_meta.id) end
	local prop_meta_subcontext = SubContext(prop_meta, {
		context_override = context,
	})
	local new_context = SubContext(context, {
		prop_meta = prop_meta_subcontext,
		value = value_fn,
	})
	
	if prop_meta.help and prop_meta.help ~= "" then
		new_context.RolloverTitle = Untranslated(prop_meta.name)
		new_context.RolloverText = Untranslated(prop_meta.help)
	end
	
	return new_context
end

DefineClass.ModOptionsObject = {
	__parents = { "PropertyObject" },
	
	__defaults = false,
	__mod = false,
}

function ModOptionsObject:Clone(class, parent)
	class = class or self.class
	local obj = g_Classes[class]:new(parent)
	obj.__mod = self.__mod
	obj:CopyProperties(self)
	return obj
end

function ModOptionsObject:GetProperties()
	local properties = rawget(self, "properties")
	if properties then return properties end
	
	local properties = {}
	self.properties = properties
	self.__defaults = {}

	local option_items = self.__mod:GetOptionItems()
	for i,option in ipairs(option_items) do
		local option_prop_meta = option:GetOptionMeta()
		table.insert(properties, option_prop_meta)
		self.__defaults[option.name] = option.DefaultValue
	end

	return properties
end

function ModOptionsObject:GetProperty(id)
	self:GetProperties()
	local value = rawget(self, id)
	if value ~= nil then return value end
	return self.__defaults[id]
end

function ModOptionsObject:SetProperty(id, value)
	rawset(self, id, value)
end

DefineClass.ModItemOption = {
	__parents = { "ModItem" },
	properties = {
		{ id = "name", name = "Id", editor = "text", default = "", translate = false, validate = ValidateIdentifier },
		{ id = "DisplayName", name = "Display Name", editor = "text", default = "", translate = false },
		{ id = "Help",        name = "Tooltip",      editor = "text", default = "", translate = false },
	},
	
	mod_option = false,
	ValueEditor = false,
	EditorSubmenu = "Mod options",
}

function ModItemOption:GetModItemDescription()
	if not self:IsDefaultPropertyValue("name", self:GetPropertyMetadata("name"), self:GetProperty("name")) then
		return Untranslated("<name> = <DefaultValue>")
	else
		return Untranslated("NewOption")
	end
end

function ModItemOption:OnEditorNew(parent, ged, is_paste)
	self.mod:LoadOptions()
end

function ModItemOption:OnModLoad()
	ModItem.OnModLoad(self)
	self.mod_option = self.class
end

function ModItemOption:GetOptionMeta()
	local display_name = self.DisplayName
	if not display_name or display_name == "" then
		display_name = self.name
	end
	
	return {
		id = self.name,
		name = T(display_name),
		editor = self.ValueEditor,
		default = self.DefaultValue,
		help = self.Help,
	}
end

function ModItemOption:NeedsResave()
	return self.mod.has_options --deprecated prop
end

DefineClass.ModItemOptionToggle = {
	__parents = { "ModItemOption" },
	properties = {
		{ id = "DefaultValue", name = "Default Value", editor = "bool", default = false },
	},
	
	ValueEditor = "bool",
	EditorName = "Option Toggle",
	Documentation = "Creates a UI entry which toggles between the 2 defined values when pressed.",
}

function ModItemOptionToggle:GetModItemDescription()
	return string.format("%s = %s", self.name, self.DefaultValue and "On" or "Off")
end

DefineClass.ModItemOptionNumber = {
	__parents = { "ModItemOption" },
	properties = {
		{ id = "DefaultValue", name = "Default Value", editor = "number", default = 0 },
		{ id = "MinValue",     name = "Min",           editor = "number", default = 0 },
		{ id = "MaxValue",     name = "Max",           editor = "number", default = 100 },
		{ id = "StepSize",     name = "Step Size",     editor = "number", default = 1 },
	},
	
	ValueEditor = "number",
	EditorName = "Option Number",
	Documentation = "Creates a UI entry with a slider.",
}

function ModItemOptionNumber:GetOptionMeta()
	local meta = ModItemOption.GetOptionMeta(self)
	meta.min = self.MinValue
	meta.max = self.MaxValue
	meta.step = self.StepSize
	meta.slider = true
	meta.show_value_text = true
	return meta
end

DefineClass.ModItemOptionChoice = {
	__parents = { "ModItemOption" },
	properties = {
		{ id = "DefaultValue", name = "Default Value", editor = "choice", default = "", items = function(self) return self.ChoiceList end },
		{ id = "ChoiceList",   name = "Choice List",   editor = "string_list", default = false }
	},
	
	ValueEditor = "choice",
	EditorName = "Option Choice",
	Documentation = "Creates a UI entry with a dropdown that contains all listed options.",
}

function ModItemOptionChoice:GetOptionMeta()
	local meta = ModItemOption.GetOptionMeta(self)
	meta.items = { }
	for i,item in ipairs(self.ChoiceList or empty_table) do
		table.insert(meta.items, { text = T(item), value = item })
	end
	return meta
end


----- ModDependency

local function GetModDependencyDescription(mod)
	return string.format("%s - %s - v %d.%d", mod.title, mod.id, mod.version_major, mod.version_minor)
end

function ModDependencyCombo()
	local result = { }
	for id,mod in pairs(Mods) do
		local text = GetModDependencyDescription(mod)
		local entry = { id = id, text = Untranslated(text) }
		table.insert(result, entry)
	end
	
	return result
end

DefineClass.ModDependency = {
	__parents = { "PropertyObject" },
	properties = {
		{ id = "id",             name = "Mod",            editor = "combo",  default = "", items = ModDependencyCombo },
		{ id = "title",          name = "Title",          editor = "text",   default = "", translate = false,
			read_only = function(dep) return dep.id ~= "" end,
			no_edit = function(dep) return dep.title == "" or dep.id == "" or Mods[dep.id] end }, --editor visible when mod is selected but is missing
		{ id = "version_major",  name = "Major Version",  editor = "number", default = 0 },
		{ id = "version_minor",  name = "Minor Version",  editor = "number", default = 0 },
		{ id = "required",       name = "Required",       editor = "bool",   default = true, help = "A non-required dependency mod will be loaded before your mod, if it is present." },
	},
	own_mod = false, --used for display purposes, assigned in CacheModDependencyGraph
}

function ModDependency:ModFits(mod_def)
	if not mod_def then return false, "no mod" end
	if self.id ~= mod_def.id then return false, "different mod" end
	if mod_def:CompareVersion(self, "ignore_revision") < 0 then return false, "incompatible" end
	return true
end

function ModDependency:GetEditorView()
	local mod = Mods[self.id]
	if mod then
		return GetModDependencyDescription(self) --needs to be self, so the correct version is displayed
	end
	
	return self.class
end

function ModDependency:OnEditorSetProperty(prop_id, old_value, ged)
	if prop_id == "id" then
		local mod = Mods[self.id]
		if mod then
			local err, list = GetModDependenciesList(mod)
			if err == "cycle" then
				ged:ShowMessage("Warning: Cycle", "This mod dependency creates a cycle (or refers to an already existing cycle)")
			end
			self.title = mod.title
			self.version_major = mod.version_major
			self.version_minor = mod.version_minor
		else
			self.title = nil
			self.version_major = nil
			self.version_minor = nil
		end
	end
end

-----

if FirstLoad then
	ModDependencyGraph = false
end

local function CollapseDependencyGraph(node, direction, root_id, all_nodes, visited, list, list_failed)
	list = list or { }
	list_failed = list_failed or { }
	visited = visited or { }
	
	if not visited[node] then
		visited[node] = true
		for i,dep in ipairs(node[direction]) do
			local dep_mod = Mods[dep.id]
			local successful = dep:ModFits(dep_mod)
			local target_list = successful and list or list_failed
			
			--avoid having two entries that have the same mod member
			local idx 
			if direction == "incoming" then
				idx = table.find(target_list, "own_mod", dep.own_mod)
			else
				idx = table.find(target_list, "id", dep.id)
			end
			if idx then
				--strive to have a required entry, instead of an optional one
				if not target_list[idx].required then
					target_list[idx] = dep
				end
			else
				--issues between other mods are not reported only if the direction is 'outgoing'
				--others do not interfere with the workings of this mod
				if direction == "outgoing" or successful or dep.id == root_id then
					table.insert(target_list, dep)
				end
			end
			
			if successful then
				local next_id = (direction == "outgoing") and dep.id or dep.own_mod.id
				CollapseDependencyGraph(all_nodes[next_id], direction, root_id, all_nodes, visited, list, list_failed)
			end
		end
	end
	
	return list, list_failed
end

function CacheModDependencyGraph()
	--'incoming' are mods that depend on this one
	--'outgoing' are mods that this one depends on
	
	local nodes = { }
	for id,mod in pairs(Mods) do
		local entry = nodes[id] or { incoming = { }, outgoing = { } }
		nodes[id] = entry
		entry.outgoing = GetModAllDependencies(mod)
		for i,dep in ipairs(entry.outgoing) do
			dep.own_mod = mod
			local dep_entry = nodes[dep.id] or { incoming = { }, outgoing = { } }
			nodes[dep.id] = dep_entry
			table.insert(dep_entry.incoming, dep)
		end
	end
	
	ModDependencyGraph = { }
	for id,mod in pairs(Mods) do
		local root_id = mod.id
		local outgoing, outgoing_failed = CollapseDependencyGraph(nodes[root_id], "outgoing", root_id, nodes)
		local incoming, incoming_failed = CollapseDependencyGraph(nodes[root_id], "incoming", root_id, nodes)
		ModDependencyGraph[id] = {
			outgoing = outgoing,
			incoming = incoming,
			outgoing_failed = outgoing_failed,
			incoming_failed = incoming_failed,
		}
	end
end

function WaitWarnAboutSkippedMods()
	local all_mods = AccountStorage and AccountStorage.LoadMods
	local skipped_mods = {}
	for _, id in ipairs(all_mods or empty_table) do
		local dependency_data = ModDependencyGraph and ModDependencyGraph[id]
		if dependency_data then
			for _, dep in ipairs(dependency_data.outgoing or empty_table) do
				if dep.required and not table.find(AccountStorage.LoadMods, dep.id) then
					table.insert_unique(skipped_mods, dep.own_mod.title)
				end
			end
			for _, dep in ipairs(dependency_data.outgoing_failed or empty_table) do
				if dep.required then
					table.insert_unique(skipped_mods, dep.own_mod.title)
				end
			end
		end
	end
	if #(skipped_mods or "") > 0 then
		local skipped = table.concat(skipped_mods, "\n")
		WaitMessage(terminal.desktop, 
			T(824112417429, "Warning"),
			T{949870544095, "The following mods will not be loaded because of missing or incompatible mods that they require:\n\n<skipped>", skipped = Untranslated(skipped)},
			T(325411474155, "OK")
		)
	end
end

----

if FirstLoad then
	ReportedMods = false
end
	
function ReportModLuaError(mod, err, stack)
	ReportedMods = ReportedMods or {}
	if ReportedMods[mod.id] then
		return
	end
	ReportedMods[mod.id] = true
	local v_major = mod.version_major or ModDef.version_major
	local v_minor = mod.version_minor or ModDef.version_minor
	local v = mod.version or ModDef.version
	local ver_str = string.format("%d.%02d-%03d", v_major or 0, v_minor or 0, v or 0)
	ModLogF(true, "Lua error in mod %s (id %s, v%s) from %s", mod.title, mod.id, ver_str, mod.source)
	Msg("OnModLuaError", mod, err, stack)
end

function OnMsg.ModsReloaded()
	SetSpecialLuaErrorHandling("Mods", #ModsLoaded > 0)
end

function OnMsg.OnLuaError(err, stack)
	for _, mod in ipairs(ModsLoaded) do
		if mod.content_path then
			if string.find_lower(err, mod.content_path) or string.find_lower(stack, mod.content_path) then
				ReportModLuaError(mod, err, stack)
			end
		end
	end
end

---

if not Platform.developer then
	if Platform.asserts then
		OnMsg.EngineStarted = function()
			ConsoleSetEnabled(true)
		end
	else
		OnMsg.ChangeMap = function(map)
			local dev_tools_visible = IsModEditorMap(map) or IsEditorActive()
			ConsoleSetEnabled(dev_tools_visible)
			local dev_interface = GetDialog(GetDevUIViewport())
			if dev_interface then
				dev_interface:SetUIVisible(dev_tools_visible)
			end
		end
	end
end

--Gossip loaded mods info
function GossipMods()
	local loadedMods = {}
	for _, mod in ipairs(ModsLoaded or empty_table) do
		table.insert(loadedMods, {id = mod.id, name = mod.title, version = mod.version})
	end
	NetGossip("Mods", loadedMods)
end

OnMsg.ModsReloaded = GossipMods
OnMsg.NetConnect = GossipMods

function GetModBlacklistedReason(modId)
	return ModIdBlacklist[modId]
end

function TFormat.BlacklistedMods(deprecatedMods, bannedMods)
	local deprecatedT = T{711775436656, "The following mods are now deprecated. They have been integrated into the base game and will be automatically disabled:\n<em><u(mods_list)></em>", mods_list = deprecatedMods}
	local bannedT = T{872385252314, "The following mods have been blacklisted and automatically blocked:\n<em><u(mods_list)></em>", mods_list = bannedMods}
	if deprecatedMods and bannedMods then
		return T{942032529432, "<deprecated>\n\n<banned>", deprecated = deprecatedT, banned = bannedT}
	elseif deprecatedMods then
		return deprecatedT
	elseif bannedMods then
		return bannedT
	end
end

function CheckBlacklistedMods()
	local deprecatedMods = next(AutoDisabledModsAlertText["deprecate"]) and table.concat(AutoDisabledModsAlertText["deprecate"], "\n") or false
	local bannedMods = next(AutoDisabledModsAlertText["ban"]) and table.concat(AutoDisabledModsAlertText["ban"], "\n") or false
	if not deprecatedMods and not bannedMods then return end
	
	CreateRealTimeThread(function()
		local textT = TFormat.BlacklistedMods(deprecatedMods, bannedMods)
		WaitMessage(
			terminal.dekstop,
			T(824112417429, "Warning"),
			textT,
			T(784547514723, "Ok")
		)
		AutoDisabledModsAlertText = {}
	end)
end

OnMsg.PreGameMenuOpen = CheckBlacklistedMods


----- ModResourceDescriptor

DefineClass.ModResourceDescriptor = {
	__parents = { "PropertyObject" },
	properties = {
	},
	mod = false, -- reference to the mod which affected the described resource
}

function ModResourceDescriptor:CheckForConflict(other)
	return false
end

function ModResourceDescriptor:GetResourceTextDescription(conflict_reason)
	return ""
end


----- Mod Conflicts

function ClearModsAffectedResourcesCache()
	ModsAffectedResourcesCache = { valid = false }
end

if FirstLoad then
	ModsAffectedResourcesCache = false
	ClearModsAffectedResourcesCache()
end

-- Populate the affected resources cache using the loaded mods
function FillModsAffectedResourcesCache()
	ClearModsAffectedResourcesCache()
	
	for idx, mod in ipairs(ModsLoaded) do
		if mod.affected_resources then
			for _, res in ipairs(mod.affected_resources) do
				if not ModsAffectedResourcesCache[res.class] then
					ModsAffectedResourcesCache[res.class] = {}
				end
				table.insert(ModsAffectedResourcesCache[res.class], res)
			end
		end
	end
	
	ModsAffectedResourcesCache.valid = true
end

-- Add the given to-be-loaded mod's affected resources to the affected resources cache if they aren't already in it
function AddToModsAffectedResourcesCache(mod)
	if not mod.affected_resources then return end
	
	for _, res in ipairs(mod.affected_resources) do
		if not ModsAffectedResourcesCache[res.class] then
			ModsAffectedResourcesCache[res.class] = {}
		end
		table.insert_unique(ModsAffectedResourcesCache[res.class], res)
	end
end

-- Remove the given to-be-loaded mod's affected resources from the affected resources cache if they are in it
function RemoveFromModsAffectedResourcesCache(mod)
	if not mod.affected_resources then return end
	
	for _, res in ipairs(mod.affected_resources) do
		if ModsAffectedResourcesCache[res.class] then
			local idx = table.find(ModsAffectedResourcesCache[res.class], res)
			
			if idx then
				table.remove(ModsAffectedResourcesCache[res.class], idx)
			end
		end
	end
end

-- Get the conflicts between all loaded (and to-be-loaded) mods.
-- There's a conflict when the two mods affect the same game resource described by ModResourceDescriptors.
function GetAllLoadedModsConflicts()
	if not ModsAffectedResourcesCache or not ModsAffectedResourcesCache.valid then
		FillModsAffectedResourcesCache()
	end
	
	local conflicts = {}
	for idx, mod in ipairs(ModsLoaded) do
		local mod_conflicts = GetSingleModConflicts(mod)
		table.iappend(conflicts, mod_conflicts)
	end

	return conflicts
end

-- Get the conflicts the given mod has with other loaded (and to-be-loaded) mods.
-- There's a conflict when the two mods affect the same game resource described by ModResourceDescriptors.
function GetSingleModConflicts(mod)
	if not ModsAffectedResourcesCache or not ModsAffectedResourcesCache.valid then
		FillModsAffectedResourcesCache()
	end
	
	local conflicts = {}
	for _, res in ipairs(mod.affected_resources or empty_table) do
		if ModsAffectedResourcesCache[res.class] then
		
			local resources_to_check = ModsAffectedResourcesCache[res.class] or empty_table
			for _, other_res in ipairs(resources_to_check) do
				if res ~= other_res and mod.id ~= other_res.mod.id then
					local conflict, reason = res:CheckForConflict(other_res)
					if conflict then
						local msg =  res:GetResourceTextDescription(reason)
						
						-- Check if this msg and mod pair has already been recorded
						-- This is for mods that conflict on the same resource multiple times
						local duplicate_msg = false
						for _, conf in ipairs(conflicts) do
							if conf.msg == msg and conf.mod1 == mod.id and conf.mod2 == other_res.mod.id then
								duplicate_msg = true
								break
							end
						end
						
						if not duplicate_msg then
							table.insert(conflicts, { mod1 = mod.id, mod2 = other_res.mod.id, msg = msg })
						end
					end
				end
			end

		end
	end
	
	table.sortby_field(conflicts, "mod2")
	return conflicts
end

-- Creates a formatted message about the given mod's conflicts based on the result of GetSingleModConflicts()
function GetModConflictsMessage(mod, conflicts)
	local msg = ""
	local prev_mod2 
	for _, conf in ipairs(conflicts) do
		local title2 = Mods[conf.mod2] and Mods[conf.mod2].title
		if not prev_mod2 then
			msg = string.format("<em>%s</em>:\n", title2)
			prev_mod2 = conf.mod2
		end
		
		if prev_mod2 == conf.mod2 then
			msg = string.format("%s\n - %s", msg, conf.msg)
		else
			msg = string.format("%s\n\n<em>%s</em>:\n - %s", msg, title2, conf.msg)
		end
		
		prev_mod2 = conf.mod2
	end
	
	return msg
end