Files @ r28731:377f4b908032
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/network_content.cpp - annotation

Peter Nelson
Fix #12052: NewGRFs clearing industry cargo slots could fallback to default instead of empty. (#12053)

Ensure the default label is cleared when NewGRF sets a produced/accepted cargo slot, so that default fallback isn't used.
   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
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r18754:42d72cb83fe7
r10793:5ba2151e71e9
r18617:524f2e0f54dc
r12668:95f5d46a2c14
r14427:e0f0ac35263c
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r24439:3207de2680bf
r24439:3207de2680bf
r24439:3207de2680bf
r24439:3207de2680bf
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r11278:28e3268b0f53
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r27391:048886674223
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28686:6688cf312bd7
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28686:6688cf312bd7
r28686:6688cf312bd7
r28686:6688cf312bd7
r10793:5ba2151e71e9
r28686:6688cf312bd7
r28686:6688cf312bd7
r28686:6688cf312bd7
r28686:6688cf312bd7
r10793:5ba2151e71e9
r28686:6688cf312bd7
r27391:048886674223
r28686:6688cf312bd7
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28686:6688cf312bd7
r25617:39a04ee83963
r25946:208a790a4923
r28686:6688cf312bd7
r25946:208a790a4923
r25946:208a790a4923
r25946:208a790a4923
r10793:5ba2151e71e9
r28686:6688cf312bd7
r25616:f6fb8fecc5a4
r28686:6688cf312bd7
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r25458:3aaccd46b7fa
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r12668:95f5d46a2c14
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r14081:4835a3d5682c
r14081:4835a3d5682c
r14081:4835a3d5682c
r14081:4835a3d5682c
r12674:8254ab83ecad
r12674:8254ab83ecad
r12674:8254ab83ecad
r12674:8254ab83ecad
r10793:5ba2151e71e9
r18508:f893337ab5c7
r18508:f893337ab5c7
r18508:f893337ab5c7
r10793:5ba2151e71e9
r18508:f893337ab5c7
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r18754:42d72cb83fe7
r11278:28e3268b0f53
r11278:28e3268b0f53
r11278:28e3268b0f53
r11278:28e3268b0f53
r11278:28e3268b0f53
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10843:cc632b9b7c4f
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23838:bfeaabaa7b1d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10841:585c775bc8b1
r23536:ce42deb0b32d
r27391:048886674223
r10841:585c775bc8b1
r25618:b5050eb6e9cd
r10843:cc632b9b7c4f
r10841:585c775bc8b1
r15429:a562bd7c12e6
r15429:a562bd7c12e6
r15429:a562bd7c12e6
r15436:42a92f38eb9f
r15429:a562bd7c12e6
r25619:54e015e45b51
r15429:a562bd7c12e6
r10841:585c775bc8b1
r15436:42a92f38eb9f
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23532:dc91fcd293f5
r10841:585c775bc8b1
r10841:585c775bc8b1
r25946:208a790a4923
r25946:208a790a4923
r25946:208a790a4923
r25946:208a790a4923
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10843:cc632b9b7c4f
r10843:cc632b9b7c4f
r14081:4835a3d5682c
r12674:8254ab83ecad
r11278:28e3268b0f53
r11278:28e3268b0f53
r10843:cc632b9b7c4f
r14500:437319d09620
r18747:12d6e8d373e7
r18754:42d72cb83fe7
r10843:cc632b9b7c4f
r10843:cc632b9b7c4f
r10843:cc632b9b7c4f
r10843:cc632b9b7c4f
r10841:585c775bc8b1
r10841:585c775bc8b1
r28682:564e133c9c4f
r10793:5ba2151e71e9
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r25950:50ad36e37113
r28074:fffdbfe84aad
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r27737:728d55b97775
r22375:9674facf16bc
r27737:728d55b97775
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10793:5ba2151e71e9
r11952:e103a659495d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10841:585c775bc8b1
r23520:20bbc807b0eb
r27737:728d55b97775
r27737:728d55b97775
r10793:5ba2151e71e9
r28682:564e133c9c4f
r27737:728d55b97775
r10793:5ba2151e71e9
r23536:ce42deb0b32d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r27391:048886674223
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r27391:048886674223
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23532:dc91fcd293f5
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r14427:e0f0ac35263c
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10841:585c775bc8b1
r14424:d8d9d1f57cf6
r23536:ce42deb0b32d
r12585:1cbbde1f1c7c
r10841:585c775bc8b1
r23532:dc91fcd293f5
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23553:9f01781fc43d
r14424:d8d9d1f57cf6
r14424:d8d9d1f57cf6
r14424:d8d9d1f57cf6
r14424:d8d9d1f57cf6
r26886:071a237e4b4c
r26886:071a237e4b4c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r26884:cdcba673c0a3
r23536:ce42deb0b32d
r26884:cdcba673c0a3
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r26874:065b6bc49f35
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23553:9f01781fc43d
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r11356:ae9277304002
r27737:728d55b97775
r11356:ae9277304002
r27737:728d55b97775
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28682:564e133c9c4f
r10793:5ba2151e71e9
r11952:e103a659495d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r24524:a57f35a240ed
r10793:5ba2151e71e9
r19537:6e0664ef363a
r24524:a57f35a240ed
r10793:5ba2151e71e9
r24524:a57f35a240ed
r24524:a57f35a240ed
r24524:a57f35a240ed
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23284:211b1ebce6cb
r23284:211b1ebce6cb
r24524:a57f35a240ed
r23607:36c15679007d
r23284:211b1ebce6cb
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r23284:211b1ebce6cb
r20973:ac8069eb1768
r24524:a57f35a240ed
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r15053:4993e149302d
r15053:4993e149302d
r17388:4c1f53028687
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r15053:4993e149302d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r25429:9e7f57ebdc95
r23607:36c15679007d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r25245:5872175e0e0c
r28686:6688cf312bd7
r10793:5ba2151e71e9
r23607:36c15679007d
r14425:bcca07eb8aff
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28686:6688cf312bd7
r28686:6688cf312bd7
r28686:6688cf312bd7
r28686:6688cf312bd7
r10793:5ba2151e71e9
r14425:bcca07eb8aff
r25458:3aaccd46b7fa
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r28686:6688cf312bd7
r28686:6688cf312bd7
r25565:6a5de7df7ea1
r16578:cef5370aef8a
r25458:3aaccd46b7fa
r10793:5ba2151e71e9
r23607:36c15679007d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r17444:de3de14c6d35
r10793:5ba2151e71e9
r14425:bcca07eb8aff
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r23607:36c15679007d
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r24524:a57f35a240ed
r24524:a57f35a240ed
r19944:25a78576fb5e
r25565:6a5de7df7ea1
r16578:cef5370aef8a
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r23607:36c15679007d
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r24524:a57f35a240ed
r14425:bcca07eb8aff
r19537:6e0664ef363a
r19537:6e0664ef363a
r18860:340289b48453
r18860:340289b48453
r24524:a57f35a240ed
r24533:a813d0309c92
r18860:340289b48453
r14671:59266b4843ee
r14671:59266b4843ee
r24526:74cfd45960ed
r24524:a57f35a240ed
r14671:59266b4843ee
r14671:59266b4843ee
r24439:3207de2680bf
r24439:3207de2680bf
r24439:3207de2680bf
r24439:3207de2680bf
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14635:a75264d517b7
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r14425:bcca07eb8aff
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23522:1fb564b1efa6
r23522:1fb564b1efa6
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23607:36c15679007d
r26885:8b59e35b5c78
r17444:de3de14c6d35
r14427:e0f0ac35263c
r23607:36c15679007d
r14427:e0f0ac35263c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r26886:071a237e4b4c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r28382:50bd98948184
r14427:e0f0ac35263c
r28382:50bd98948184
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r28382:50bd98948184
r28382:50bd98948184
r28382:50bd98948184
r14427:e0f0ac35263c
r28377:d7ceb84d9d5a
r28377:d7ceb84d9d5a
r14427:e0f0ac35263c
r23607:36c15679007d
r14427:e0f0ac35263c
r28382:50bd98948184
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23532:dc91fcd293f5
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23607:36c15679007d
r14427:e0f0ac35263c
r28382:50bd98948184
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r17444:de3de14c6d35
r14427:e0f0ac35263c
r28382:50bd98948184
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23607:36c15679007d
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23520:20bbc807b0eb
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23607:36c15679007d
r14427:e0f0ac35263c
r14434:ff88a5e07169
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23536:ce42deb0b32d
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14429:d459d2cbce62
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r23520:20bbc807b0eb
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14434:ff88a5e07169
r14979:071183f945de
r14427:e0f0ac35263c
r27666:68994e4ae3c4
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r27666:68994e4ae3c4
r27666:68994e4ae3c4
r27666:68994e4ae3c4
r27666:68994e4ae3c4
r27666:68994e4ae3c4
r27666:68994e4ae3c4
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r27666:68994e4ae3c4
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14427:e0f0ac35263c
r14425:bcca07eb8aff
r17641:a80e43fd2f5e
r10793:5ba2151e71e9
r10841:585c775bc8b1
r11544:5b7269ee2600
r14427:e0f0ac35263c
r23607:36c15679007d
r23607:36c15679007d
r26886:071a237e4b4c
r26886:071a237e4b4c
r10793:5ba2151e71e9
r24952:3017eb59bf1a
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r23607:36c15679007d
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r17641:a80e43fd2f5e
r28531:925d537e515d
r10841:585c775bc8b1
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r17641:a80e43fd2f5e
r25388:32b23efdcf4e
r10841:585c775bc8b1
r23506:bba4d0c6d70d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23506:bba4d0c6d70d
r10841:585c775bc8b1
r10841:585c775bc8b1
r24952:3017eb59bf1a
r10841:585c775bc8b1
r10841:585c775bc8b1
r11589:553d24b1005c
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r26886:071a237e4b4c
r26886:071a237e4b4c
r10841:585c775bc8b1
r26886:071a237e4b4c
r28531:925d537e515d
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10793:5ba2151e71e9
r27942:f7389062d120
r10793:5ba2151e71e9
r26886:071a237e4b4c
r25458:3aaccd46b7fa
r25454:212b1b5e83b5
r25458:3aaccd46b7fa
r25458:3aaccd46b7fa
r25454:212b1b5e83b5
r25458:3aaccd46b7fa
r10843:cc632b9b7c4f
r25458:3aaccd46b7fa
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10793:5ba2151e71e9
r10841:585c775bc8b1
r10841:585c775bc8b1
r24952:3017eb59bf1a
r25458:3aaccd46b7fa
r10841:585c775bc8b1
r10841:585c775bc8b1
r10793:5ba2151e71e9
r16222:694bed5fd015
r20565:90fc925b267e
r23838:bfeaabaa7b1d
r24952:3017eb59bf1a
r20565:90fc925b267e
r11226:cd52d6615d08
r11226:cd52d6615d08
r16618:d89f0c3f42e7
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23527:c5aaabea48de
r10841:585c775bc8b1
r23527:c5aaabea48de
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23607:36c15679007d
r10841:585c775bc8b1
r25946:208a790a4923
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r23607:36c15679007d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23607:36c15679007d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23607:36c15679007d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10843:cc632b9b7c4f
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10843:cc632b9b7c4f
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23536:ce42deb0b32d
r12765:13a11d84969b
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r14992:2648a73e4ab8
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r25946:208a790a4923
r10841:585c775bc8b1
r25946:208a790a4923
r25946:208a790a4923
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23532:dc91fcd293f5
r10841:585c775bc8b1
r17150:a4e0d8c6c5fa
r17150:a4e0d8c6c5fa
r17150:a4e0d8c6c5fa
r17150:a4e0d8c6c5fa
r23520:20bbc807b0eb
r10841:585c775bc8b1
r17150:a4e0d8c6c5fa
r10841:585c775bc8b1
r23536:ce42deb0b32d
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r25617:39a04ee83963
r25617:39a04ee83963
r23607:36c15679007d
r25617:39a04ee83963
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23536:ce42deb0b32d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r25617:39a04ee83963
r25617:39a04ee83963
r23607:36c15679007d
r25617:39a04ee83963
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23517:1a32c3c14728
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r25518:10899ab79aae
r25518:10899ab79aae
r25518:10899ab79aae
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23517:1a32c3c14728
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r25518:10899ab79aae
r25518:10899ab79aae
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r15810:0e843b449a0a
r10841:585c775bc8b1
r26829:f1ebd010c392
r26829:f1ebd010c392
r10841:585c775bc8b1
r26829:f1ebd010c392
r26829:f1ebd010c392
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r17138:5078c9240593
r10886:02a2d01be830
r10886:02a2d01be830
r23536:ce42deb0b32d
r10886:02a2d01be830
r23517:1a32c3c14728
r23517:1a32c3c14728
r25946:208a790a4923
r10886:02a2d01be830
r10886:02a2d01be830
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23604:c5c1cd425760
r23604:c5c1cd425760
r23604:c5c1cd425760
r10860:f5bbffcfa3ae
r23604:c5c1cd425760
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23604:c5c1cd425760
r23604:c5c1cd425760
r10860:f5bbffcfa3ae
r23604:c5c1cd425760
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23604:c5c1cd425760
r23604:c5c1cd425760
r23604:c5c1cd425760
r10860:f5bbffcfa3ae
r23604:c5c1cd425760
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r17444:de3de14c6d35
r10841:585c775bc8b1
r23604:c5c1cd425760
r23604:c5c1cd425760
r10860:f5bbffcfa3ae
r23604:c5c1cd425760
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23607:36c15679007d
r10841:585c775bc8b1
r10841:585c775bc8b1
r10841:585c775bc8b1
r23604:c5c1cd425760
r23604:c5c1cd425760
r23604:c5c1cd425760
r10860:f5bbffcfa3ae
r23604:c5c1cd425760
r10841:585c775bc8b1
r10793:5ba2151e71e9
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file network_content.cpp Content sending/receiving part of the network protocol. */

#include "../stdafx.h"
#include "../rev.h"
#include "../ai/ai.hpp"
#include "../game/game.hpp"
#include "../window_func.h"
#include "../error.h"
#include "../base_media_base.h"
#include "../settings_type.h"
#include "network_content.h"

#include "table/strings.h"

#if defined(WITH_ZLIB)
#include <zlib.h>
#endif

#ifdef __EMSCRIPTEN__
#	include <emscripten.h>
#endif

#include "../safeguards.h"

extern bool HasScenario(const ContentInfo *ci, bool md5sum);

/** The client we use to connect to the server. */
ClientNetworkContentSocketHandler _network_content_client;

/** Wrapper function for the HasProc */
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
{
	return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? &ci->md5sum : nullptr) != nullptr;
}

/**
 * Check whether a function piece of content is locally known.
 * Matches on the unique ID and possibly the MD5 checksum.
 * @param ci     the content info to search for
 * @param md5sum also match the MD5 checksum?
 * @return true iff it's known
 */
typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);

bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet &p)
{
	ContentInfo *ci = new ContentInfo();
	ci->type     = (ContentType)p.Recv_uint8();
	ci->id       = (ContentID)p.Recv_uint32();
	ci->filesize = p.Recv_uint32();

	ci->name        = p.Recv_string(NETWORK_CONTENT_NAME_LENGTH);
	ci->version     = p.Recv_string(NETWORK_CONTENT_VERSION_LENGTH);
	ci->url         = p.Recv_string(NETWORK_CONTENT_URL_LENGTH);
	ci->description = p.Recv_string(NETWORK_CONTENT_DESC_LENGTH, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);

	ci->unique_id = p.Recv_uint32();
	for (size_t j = 0; j < ci->md5sum.size(); j++) {
		ci->md5sum[j] = p.Recv_uint8();
	}

	uint dependency_count = p.Recv_uint8();
	ci->dependencies.reserve(dependency_count);
	for (uint i = 0; i < dependency_count; i++) {
		ContentID dependency_cid = (ContentID)p.Recv_uint32();
		ci->dependencies.push_back(dependency_cid);
		this->reverse_dependency_map.insert({ dependency_cid, ci->id });
	}

	uint tag_count = p.Recv_uint8();
	ci->tags.reserve(tag_count);
	for (uint i = 0; i < tag_count; i++) ci->tags.push_back(p.Recv_string(NETWORK_CONTENT_TAG_LENGTH));

	if (!ci->IsValid()) {
		delete ci;
		this->CloseConnection();
		return false;
	}

	/* Find the appropriate check function */
	HasProc proc = nullptr;
	switch (ci->type) {
		case CONTENT_TYPE_NEWGRF:
			proc = HasGRFConfig;
			break;

		case CONTENT_TYPE_BASE_GRAPHICS:
			proc = BaseGraphics::HasSet;
			break;

		case CONTENT_TYPE_BASE_MUSIC:
			proc = BaseMusic::HasSet;
			break;

		case CONTENT_TYPE_BASE_SOUNDS:
			proc = BaseSounds::HasSet;
			break;

		case CONTENT_TYPE_AI:
			proc = AI::HasAI; break;
			break;

		case CONTENT_TYPE_AI_LIBRARY:
			proc = AI::HasAILibrary; break;
			break;

		case CONTENT_TYPE_GAME:
			proc = Game::HasGame; break;
			break;

		case CONTENT_TYPE_GAME_LIBRARY:
			proc = Game::HasGameLibrary; break;
			break;

		case CONTENT_TYPE_SCENARIO:
		case CONTENT_TYPE_HEIGHTMAP:
			proc = HasScenario;
			break;

		default:
			break;
	}

	if (proc != nullptr) {
		if (proc(ci, true)) {
			ci->state = ContentInfo::ALREADY_HERE;
		} else {
			ci->state = ContentInfo::UNSELECTED;
			if (proc(ci, false)) ci->upgrade = true;
		}
	} else {
		ci->state = ContentInfo::UNSELECTED;
	}

	/* Something we don't have and has filesize 0 does not exist in the system */
	if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;

	/* Do we already have a stub for this? */
	for (ContentInfo *ici : this->infos) {
		if (ici->type == ci->type && ici->unique_id == ci->unique_id && ci->md5sum == ici->md5sum) {
			/* Preserve the name if possible */
			if (ci->name.empty()) ci->name = ici->name;
			if (ici->IsSelected()) ci->state = ici->state;

			/*
			 * As ici might be selected by the content window we cannot delete that.
			 * However, we want to keep most of the values of ci, except the values
			 * we (just) already preserved.
			 */
			*ici = *ci;
			delete ci;

			this->OnReceiveContentInfo(ici);
			return true;
		}
	}

	/* Missing content info? Don't list it */
	if (ci->filesize == 0) {
		delete ci;
		return true;
	}

	this->infos.push_back(ci);

	/* Incoming data means that we might need to reconsider dependencies */
	ConstContentVector parents;
	this->ReverseLookupTreeDependency(parents, ci);
	for (const ContentInfo *ici : parents) {
		this->CheckDependencyState(const_cast<ContentInfo *>(ici));
	}

	this->OnReceiveContentInfo(ci);

	return true;
}

/**
 * Request the content list for the given type.
 * @param type The content type to request the list for.
 */
void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
{
	if (type == CONTENT_TYPE_END) {
		this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
		this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
		this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
		this->RequestContentList(CONTENT_TYPE_SCENARIO);
		this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
		this->RequestContentList(CONTENT_TYPE_AI);
		this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
		this->RequestContentList(CONTENT_TYPE_GAME);
		this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
		this->RequestContentList(CONTENT_TYPE_NEWGRF);
		return;
	}

	this->Connect();

	auto p = std::make_unique<Packet>(PACKET_CONTENT_CLIENT_INFO_LIST);
	p->Send_uint8 ((byte)type);
	p->Send_uint32(0xffffffff);
	p->Send_uint8 (1);
	p->Send_string("vanilla");
	p->Send_string(_openttd_content_version);

	/* Patchpacks can extend the list with one. In BaNaNaS metadata you can
	 * add a branch in the 'compatibility' list, to filter on this. If you want
	 * your patchpack to be mentioned in the BaNaNaS web-interface, create an
	 * issue on https://github.com/OpenTTD/bananas-api asking for this.

	p->Send_string("patchpack"); // Or what-ever the name of your patchpack is.
	p->Send_string(_openttd_content_version_patchpack);

	 */

	this->SendPacket(std::move(p));
}

/**
 * Request the content list for a given number of content IDs.
 * @param count The number of IDs to request.
 * @param content_ids The unique identifiers of the content to request information about.
 */
void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
{
	this->Connect();

	while (count > 0) {
		/* We can "only" send a limited number of IDs in a single packet.
		 * A packet begins with the packet size and a byte for the type.
		 * Then this packet adds a uint16_t for the count in this packet.
		 * The rest of the packet can be used for the IDs. */
		uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16_t)) / sizeof(uint32_t));

		auto p = std::make_unique<Packet>(PACKET_CONTENT_CLIENT_INFO_ID, TCP_MTU);
		p->Send_uint16(p_count);

		for (uint i = 0; i < p_count; i++) {
			p->Send_uint32(content_ids[i]);
		}

		this->SendPacket(std::move(p));
		count -= p_count;
		content_ids += p_count;
	}
}

/**
 * Request the content list for a list of content.
 * @param cv List with unique IDs and MD5 checksums.
 * @param send_md5sum Whether we want a MD5 checksum matched set of files or not.
 */
void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
{
	if (cv == nullptr) return;

	this->Connect();

	assert(cv->size() < 255);
	assert(cv->size() < (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8_t)) /
			(sizeof(uint8_t) + sizeof(uint32_t) + (send_md5sum ? MD5_HASH_BYTES : 0)));

	auto p = std::make_unique<Packet>(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID, TCP_MTU);
	p->Send_uint8((uint8_t)cv->size());

	for (const ContentInfo *ci : *cv) {
		p->Send_uint8((byte)ci->type);
		p->Send_uint32(ci->unique_id);
		if (!send_md5sum) continue;

		for (size_t j = 0; j < ci->md5sum.size(); j++) {
			p->Send_uint8(ci->md5sum[j]);
		}
	}

	this->SendPacket(std::move(p));

	for (ContentInfo *ci : *cv) {
		bool found = false;
		for (ContentInfo *ci2 : this->infos) {
			if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
					(!send_md5sum || ci->md5sum == ci2->md5sum)) {
				found = true;
				break;
			}
		}
		if (!found) {
			this->infos.push_back(ci);
		} else {
			delete ci;
		}
	}
}

/**
 * Actually begin downloading the content we selected.
 * @param[out] files The number of files we are going to download.
 * @param[out] bytes The number of bytes we are going to download.
 * @param fallback Whether to use the fallback or not.
 */
void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
{
	bytes = 0;

	ContentIDList content;
	for (const ContentInfo *ci : this->infos) {
		if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;

		content.push_back(ci->id);
		bytes += ci->filesize;
	}

	files = (uint)content.size();

	/* If there's nothing to download, do nothing. */
	if (files == 0) return;

	this->isCancelled = false;

	if (_settings_client.network.no_http_content_downloads || fallback) {
		this->DownloadSelectedContentFallback(content);
	} else {
		this->DownloadSelectedContentHTTP(content);
	}
}

/**
 * Initiate downloading the content over HTTP.
 * @param content The content to download.
 */
void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
{
	std::string content_request;
	for (const ContentID &id : content) {
		content_request += std::to_string(id) + "\n";
	}

	this->http_response_index = -1;

	NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, content_request);
}

/**
 * Initiate downloading the content over the fallback protocol.
 * @param content The content to download.
 */
void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
{
	uint count = (uint)content.size();
	const ContentID *content_ids = content.data();
	this->Connect();

	while (count > 0) {
		/* We can "only" send a limited number of IDs in a single packet.
		 * A packet begins with the packet size and a byte for the type.
		 * Then this packet adds a uint16_t for the count in this packet.
		 * The rest of the packet can be used for the IDs. */
		uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16_t)) / sizeof(uint32_t));

		auto p = std::make_unique<Packet>(PACKET_CONTENT_CLIENT_CONTENT, TCP_MTU);
		p->Send_uint16(p_count);

		for (uint i = 0; i < p_count; i++) {
			p->Send_uint32(content_ids[i]);
		}

		this->SendPacket(std::move(p));
		count -= p_count;
		content_ids += p_count;
	}
}

/**
 * Determine the full filename of a piece of content information
 * @param ci         the information to get the filename from
 * @param compressed should the filename end with .gz?
 * @return a statically allocated buffer with the filename or
 *         nullptr when no filename could be made.
 */
static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
{
	Subdirectory dir = GetContentInfoSubDir(ci->type);
	if (dir == NO_DIRECTORY) return {};

	std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
	buf += ci->filename;
	buf += compressed ? ".tar.gz" : ".tar";

	return buf;
}

/**
 * Gunzip a given file and remove the .gz if successful.
 * @param ci container with filename
 * @return true if the gunzip completed
 */
static bool GunzipFile(const ContentInfo *ci)
{
#if defined(WITH_ZLIB)
	bool ret = true;

	/* Need to open the file with fopen() to support non-ASCII on Windows. */
	FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb");
	if (ftmp == nullptr) return false;
	/* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
	int fdup = dup(fileno(ftmp));
	gzFile fin = gzdopen(fdup, "rb");
	fclose(ftmp);

	FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb");

	if (fin == nullptr || fout == nullptr) {
		ret = false;
	} else {
		byte buff[8192];
		for (;;) {
			int read = gzread(fin, buff, sizeof(buff));
			if (read == 0) {
				/* If gzread() returns 0, either the end-of-file has been
				 * reached or an underlying read error has occurred.
				 *
				 * gzeof() can't be used, because:
				 * 1.2.5 - it is safe, 1 means 'everything was OK'
				 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
				 * 1.2.3.3 - 1 is returned for truncated archive
				 *
				 * So we use gzerror(). When proper end of archive
				 * has been reached, then:
				 * errnum == Z_STREAM_END in 1.2.3.3,
				 * errnum == 0 in 1.2.4 and 1.2.5 */
				int errnum;
				gzerror(fin, &errnum);
				if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
				break;
			}
			if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
				/* If gzread() returns -1, there was an error in archive */
				ret = false;
				break;
			}
			/* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
			 * if (read < sizeof(buff)) break; */
		}
	}

	if (fin != nullptr) {
		gzclose(fin);
	} else if (fdup != -1) {
		/* Failing gzdopen does not close the passed file descriptor. */
		close(fdup);
	}
	if (fout != nullptr) fclose(fout);

	return ret;
#else
	NOT_REACHED();
#endif /* defined(WITH_ZLIB) */
}

/**
 * Simple wrapper around fwrite to be able to pass it to Packet's TransferOut.
 * @param file   The file to write data to.
 * @param buffer The buffer to write to the file.
 * @param amount The number of bytes to write.
 * @return The number of bytes that were written.
 */
static inline ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
{
	return fwrite(buffer, 1, amount, file);
}

bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &p)
{
	if (this->curFile == nullptr) {
		delete this->curInfo;
		/* When we haven't opened a file this must be our first packet with metadata. */
		this->curInfo = new ContentInfo;
		this->curInfo->type     = (ContentType)p.Recv_uint8();
		this->curInfo->id       = (ContentID)p.Recv_uint32();
		this->curInfo->filesize = p.Recv_uint32();
		this->curInfo->filename = p.Recv_string(NETWORK_CONTENT_FILENAME_LENGTH);

		if (!this->BeforeDownload()) {
			this->CloseConnection();
			return false;
		}
	} else {
		/* We have a file opened, thus are downloading internal content */
		size_t toRead = p.RemainingBytesToTransfer();
		if (toRead != 0 && (size_t)p.TransferOut(TransferOutFWrite, this->curFile) != toRead) {
			CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
			ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
			this->CloseConnection();
			fclose(this->curFile);
			this->curFile = nullptr;

			return false;
		}

		this->OnDownloadProgress(this->curInfo, (int)toRead);

		if (toRead == 0) this->AfterDownload();
	}

	return true;
}

/**
 * Handle the opening of the file before downloading.
 * @return false on any error.
 */
bool ClientNetworkContentSocketHandler::BeforeDownload()
{
	if (!this->curInfo->IsValid()) {
		delete this->curInfo;
		this->curInfo = nullptr;
		return false;
	}

	if (this->curInfo->filesize != 0) {
		/* The filesize is > 0, so we are going to download it */
		std::string filename = GetFullFilename(this->curInfo, true);
		if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) {
			/* Unless that fails of course... */
			CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
			ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
			return false;
		}
	}
	return true;
}

/**
 * Handle the closing and extracting of a file after
 * downloading it has been done.
 */
void ClientNetworkContentSocketHandler::AfterDownload()
{
	/* We read nothing; that's our marker for end-of-stream.
	 * Now gunzip the tar and make it known. */
	fclose(this->curFile);
	this->curFile = nullptr;

	if (GunzipFile(this->curInfo)) {
		unlink(GetFullFilename(this->curInfo, true).c_str());

		Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
		if (sd == NO_DIRECTORY) NOT_REACHED();

		TarScanner ts;
		std::string fname = GetFullFilename(this->curInfo, false);
		ts.AddFile(sd, fname);

		if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
			/* Music can't be in a tar. So extract the tar! */
			ExtractTar(fname, BASESET_DIR);
			unlink(fname.c_str());
		}

#ifdef __EMSCRIPTEN__
		EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
#endif

		this->OnDownloadComplete(this->curInfo->id);
	} else {
		ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
	}
}

bool ClientNetworkContentSocketHandler::IsCancelled() const
{
	return this->isCancelled;
}

/* Also called to just clean up the mess. */
void ClientNetworkContentSocketHandler::OnFailure()
{
	this->http_response.clear();
	this->http_response.shrink_to_fit();
	this->http_response_index = -2;

	if (this->curFile != nullptr) {
		this->OnDownloadProgress(this->curInfo, -1);

		fclose(this->curFile);
		this->curFile = nullptr;
	}

	/* If we fail, download the rest via the 'old' system. */
	if (!this->isCancelled) {
		uint files, bytes;

		this->DownloadSelectedContent(files, bytes, true);
	}
}

void ClientNetworkContentSocketHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t length)
{
	assert(data.get() == nullptr || length != 0);

	/* Ignore any latent data coming from a connection we closed. */
	if (this->http_response_index == -2) {
		return;
	}

	this->lastActivity = std::chrono::steady_clock::now();

	if (this->http_response_index == -1) {
		if (data != nullptr) {
			/* Append the rest of the response. */
			this->http_response.insert(this->http_response.end(), data.get(), data.get() + length);
			return;
		} else {
			/* Make sure the response is properly terminated. */
			this->http_response.push_back('\0');

			/* And prepare for receiving the rest of the data. */
			this->http_response_index = 0;
		}
	}

	if (data != nullptr) {
		/* We have data, so write it to the file. */
		if (fwrite(data.get(), 1, length, this->curFile) != length) {
			/* Writing failed somehow, let try via the old method. */
			this->OnFailure();
		} else {
			/* Just received the data. */
			this->OnDownloadProgress(this->curInfo, (int)length);
		}

		/* Nothing more to do now. */
		return;
	}

	if (this->curFile != nullptr) {
		/* We've finished downloading a file. */
		this->AfterDownload();
	}

	if ((uint)this->http_response_index >= this->http_response.size()) {
		/* It's not a real failure, but if there's
		 * nothing more to download it helps with
		 * cleaning up the stuff we allocated. */
		this->OnFailure();
		return;
	}

	delete this->curInfo;
	/* When we haven't opened a file this must be our first packet with metadata. */
	this->curInfo = new ContentInfo;

/** Check p for not being null and return calling OnFailure if that's not the case. */
#define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
/** Check p for not being null and then terminate, or return calling OnFailure. */
#define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }

	for (;;) {
		char *str = this->http_response.data() + this->http_response_index;
		char *p = strchr(str, '\n');
		check_and_terminate(p);

		/* Update the index for the next one */
		this->http_response_index += (int)strlen(str) + 1;

		/* Read the ID */
		p = strchr(str, ',');
		check_and_terminate(p);
		this->curInfo->id = (ContentID)atoi(str);

		/* Read the type */
		str = p + 1;
		p = strchr(str, ',');
		check_and_terminate(p);
		this->curInfo->type = (ContentType)atoi(str);

		/* Read the file size */
		str = p + 1;
		p = strchr(str, ',');
		check_and_terminate(p);
		this->curInfo->filesize = atoi(str);

		/* Read the URL */
		str = p + 1;
		/* Is it a fallback URL? If so, just continue with the next one. */
		if (strncmp(str, "ottd", 4) == 0) {
			if ((uint)this->http_response_index >= this->http_response.size()) {
				/* Have we gone through all lines? */
				this->OnFailure();
				return;
			}
			continue;
		}

		p = strrchr(str, '/');
		check_not_null(p);
		p++; // Start after the '/'

		std::string filename = p;
		/* Remove the extension from the string. */
		for (uint i = 0; i < 2; i++) {
			auto pos = filename.find_last_of('.');
			if (pos == std::string::npos) {
				this->OnFailure();
				return;
			}
			filename.erase(pos);
		}

		/* Copy the string, without extension, to the filename. */
		this->curInfo->filename = std::move(filename);

		/* Request the next file. */
		if (!this->BeforeDownload()) {
			this->OnFailure();
			return;
		}

		NetworkHTTPSocketHandler::Connect(str, this);
		return;
	}

#undef check
#undef check_and_terminate
}

/**
 * Create a socket handler to handle the connection.
 */
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
	NetworkContentSocketHandler(),
	http_response_index(-2),
	curFile(nullptr),
	curInfo(nullptr),
	isConnecting(false),
	isCancelled(false)
{
	this->lastActivity = std::chrono::steady_clock::now();
}

/** Clear up the mess ;) */
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
{
	delete this->curInfo;
	if (this->curFile != nullptr) fclose(this->curFile);

	for (ContentInfo *ci : this->infos) delete ci;
}

/** Connect to the content server. */
class NetworkContentConnecter : public TCPConnecter {
public:
	/**
	 * Initiate the connecting.
	 * @param address The address of the server.
	 */
	NetworkContentConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {}

	void OnFailure() override
	{
		_network_content_client.isConnecting = false;
		_network_content_client.OnConnect(false);
	}

	void OnConnect(SOCKET s) override
	{
		assert(_network_content_client.sock == INVALID_SOCKET);
		_network_content_client.lastActivity = std::chrono::steady_clock::now();
		_network_content_client.isConnecting = false;
		_network_content_client.sock = s;
		_network_content_client.Reopen();
		_network_content_client.OnConnect(true);
	}
};

/**
 * Connect with the content server.
 */
void ClientNetworkContentSocketHandler::Connect()
{
	if (this->sock != INVALID_SOCKET || this->isConnecting) return;

	this->isCancelled = false;
	this->isConnecting = true;

	TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
}

/**
 * Disconnect from the content server.
 */
NetworkRecvStatus ClientNetworkContentSocketHandler::CloseConnection(bool)
{
	this->isCancelled = true;
	NetworkContentSocketHandler::CloseConnection();

	if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;

	this->CloseSocket();
	this->OnDisconnect();

	return NETWORK_RECV_STATUS_OKAY;
}

/**
 * Check whether we received/can send some data from/to the content server and
 * when that's the case handle it appropriately
 */
void ClientNetworkContentSocketHandler::SendReceive()
{
	if (this->sock == INVALID_SOCKET || this->isConnecting) return;

	if (std::chrono::steady_clock::now() > this->lastActivity + IDLE_TIMEOUT) {
		this->CloseConnection();
		return;
	}

	if (this->CanSendReceive()) {
		if (this->ReceivePackets()) {
			/* Only update activity once a packet is received, instead of every time we try it. */
			this->lastActivity = std::chrono::steady_clock::now();
		}
	}

	this->SendPackets();
}

/**
 * Download information of a given Content ID if not already tried
 * @param cid the ID to try
 */
void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
{
	/* When we tried to download it already, don't try again */
	if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;

	this->requested.push_back(cid);
	this->RequestContentList(1, &cid);
}

/**
 * Get the content info based on a ContentID
 * @param cid the ContentID to search for
 * @return the ContentInfo or nullptr if not found
 */
ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid) const
{
	for (ContentInfo *ci : this->infos) {
		if (ci->id == cid) return ci;
	}
	return nullptr;
}


/**
 * Select a specific content id.
 * @param cid the content ID to select
 */
void ClientNetworkContentSocketHandler::Select(ContentID cid)
{
	ContentInfo *ci = this->GetContent(cid);
	if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;

	ci->state = ContentInfo::SELECTED;
	this->CheckDependencyState(ci);
}

/**
 * Unselect a specific content id.
 * @param cid the content ID to deselect
 */
void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
{
	ContentInfo *ci = this->GetContent(cid);
	if (ci == nullptr || !ci->IsSelected()) return;

	ci->state = ContentInfo::UNSELECTED;
	this->CheckDependencyState(ci);
}

/** Select everything we can select */
void ClientNetworkContentSocketHandler::SelectAll()
{
	for (ContentInfo *ci : this->infos) {
		if (ci->state == ContentInfo::UNSELECTED) {
			ci->state = ContentInfo::SELECTED;
			this->CheckDependencyState(ci);
		}
	}
}

/** Select everything that's an update for something we've got */
void ClientNetworkContentSocketHandler::SelectUpgrade()
{
	for (ContentInfo *ci : this->infos) {
		if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
			ci->state = ContentInfo::SELECTED;
			this->CheckDependencyState(ci);
		}
	}
}

/** Unselect everything that we've not downloaded so far. */
void ClientNetworkContentSocketHandler::UnselectAll()
{
	for (ContentInfo *ci : this->infos) {
		if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
	}
}

/** Toggle the state of a content info and check its dependencies */
void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
{
	switch (ci->state) {
		case ContentInfo::SELECTED:
		case ContentInfo::AUTOSELECTED:
			this->Unselect(ci->id);
			break;

		case ContentInfo::UNSELECTED:
			this->Select(ci->id);
			break;

		default:
			break;
	}
}

/**
 * Reverse lookup the dependencies of (direct) parents over a given child.
 * @param parents list to store all parents in (is not cleared)
 * @param child   the child to search the parents' dependencies for
 */
void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
{
	auto range = this->reverse_dependency_map.equal_range(child->id);

	for (auto iter = range.first; iter != range.second; ++iter) {
		parents.push_back(GetContent(iter->second));
	}
}

/**
 * Reverse lookup the dependencies of all parents over a given child.
 * @param tree  list to store all parents in (is not cleared)
 * @param child the child to search the parents' dependencies for
 */
void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
{
	tree.push_back(child);

	/* First find all direct parents. We can't use the "normal" iterator as
	 * we are including stuff into the vector and as such the vector's data
	 * store can be reallocated (and thus move), which means out iterating
	 * pointer gets invalid. So fall back to the indices. */
	for (uint i = 0; i < tree.size(); i++) {
		ConstContentVector parents;
		this->ReverseLookupDependency(parents, tree[i]);

		for (const ContentInfo *ci : parents) {
			include(tree, ci);
		}
	}
}

/**
 * Check the dependencies (recursively) of this content info
 * @param ci the content info to check the dependencies of
 */
void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
{
	if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
		/* Selection is easy; just walk all children and set the
		 * autoselected state. That way we can see what we automatically
		 * selected and thus can unselect when a dependency is removed. */
		for (auto &dependency : ci->dependencies) {
			ContentInfo *c = this->GetContent(dependency);
			if (c == nullptr) {
				this->DownloadContentInfo(dependency);
			} else if (c->state == ContentInfo::UNSELECTED) {
				c->state = ContentInfo::AUTOSELECTED;
				this->CheckDependencyState(c);
			}
		}
		return;
	}

	if (ci->state != ContentInfo::UNSELECTED) return;

	/* For unselection we need to find the parents of us. We need to
	 * unselect them. After that we unselect all children that we
	 * depend on and are not used as dependency for us, but only when
	 * we automatically selected them. */
	ConstContentVector parents;
	this->ReverseLookupDependency(parents, ci);
	for (const ContentInfo *c : parents) {
		if (!c->IsSelected()) continue;

		this->Unselect(c->id);
	}

	for (auto &dependency : ci->dependencies) {
		const ContentInfo *c = this->GetContent(dependency);
		if (c == nullptr) {
			DownloadContentInfo(dependency);
			continue;
		}
		if (c->state != ContentInfo::AUTOSELECTED) continue;

		/* Only unselect when WE are the only parent. */
		parents.clear();
		this->ReverseLookupDependency(parents, c);

		/* First check whether anything depends on us */
		int sel_count = 0;
		bool force_selection = false;
		for (const ContentInfo *parent_ci : parents) {
			if (parent_ci->IsSelected()) sel_count++;
			if (parent_ci->state == ContentInfo::SELECTED) force_selection = true;
		}
		if (sel_count == 0) {
			/* Nothing depends on us */
			this->Unselect(c->id);
			continue;
		}
		/* Something manually selected depends directly on us */
		if (force_selection) continue;

		/* "Flood" search to find all items in the dependency graph*/
		parents.clear();
		this->ReverseLookupTreeDependency(parents, c);

		/* Is there anything that is "force" selected?, if so... we're done. */
		for (const ContentInfo *parent_ci : parents) {
			if (parent_ci->state != ContentInfo::SELECTED) continue;

			force_selection = true;
			break;
		}

		/* So something depended directly on us */
		if (force_selection) continue;

		/* Nothing depends on us, mark the whole graph as unselected.
		 * After that's done run over them once again to test their children
		 * to unselect. Don't do it immediately because it'll do exactly what
		 * we're doing now. */
		for (const ContentInfo *parent : parents) {
			if (parent->state == ContentInfo::AUTOSELECTED) this->Unselect(parent->id);
		}
		for (const ContentInfo *parent : parents) {
			this->CheckDependencyState(this->GetContent(parent->id));
		}
	}
}

/** Clear all downloaded content information. */
void ClientNetworkContentSocketHandler::Clear()
{
	for (ContentInfo *c : this->infos) delete c;

	this->infos.clear();
	this->requested.clear();
	this->reverse_dependency_map.clear();
}

/*** CALLBACK ***/

void ClientNetworkContentSocketHandler::OnConnect(bool success)
{
	for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
		ContentCallback *cb = this->callbacks[i];
		/* the callback may remove itself from this->callbacks */
		cb->OnConnect(success);
		if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
	}
}

void ClientNetworkContentSocketHandler::OnDisconnect()
{
	for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
		ContentCallback *cb = this->callbacks[i];
		cb->OnDisconnect();
		if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
	}
}

void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
{
	for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
		ContentCallback *cb = this->callbacks[i];
		/* the callback may add items and/or remove itself from this->callbacks */
		cb->OnReceiveContentInfo(ci);
		if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
	}
}

void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
{
	for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
		ContentCallback *cb = this->callbacks[i];
		cb->OnDownloadProgress(ci, bytes);
		if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
	}
}

void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
{
	ContentInfo *ci = this->GetContent(cid);
	if (ci != nullptr) {
		ci->state = ContentInfo::ALREADY_HERE;
	}

	for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
		ContentCallback *cb = this->callbacks[i];
		/* the callback may remove itself from this->callbacks */
		cb->OnDownloadComplete(cid);
		if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
	}
}