modde-games 0.2.1

Game plugin implementations for modde
Documentation
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
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
//! `OptiScaler` — DLSS/FSR/XeSS upscaling and frame generation replacement.
//!
//! `OptiScaler` hooks into a game via proxy DLLs (typically `dxgi.dll` or
//! `winmm.dll`). Some games need additional DLLs like `nvngx.dll`.
//!
//! This implementation also subsumes the old fgmod DLL restoration logic:
//! when fgmod deletes certain DLLs at launch time, the launch wrapper
//! restores them.

use std::collections::{BTreeMap, BTreeSet};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use anyhow::{Context, Result};
use futures::StreamExt;
use reqwest::Client;
use smallvec::{SmallVec, smallvec};
use tokio::io::AsyncWriteExt;
use tracing::info;
use xxhash_rust::xxh64::Xxh64;

use crate::optiscaler::{
    OptiScalerProfile, default_optiscaler_profile, resolve_optiscaler_profiles,
};

use super::{
    AppliedFiles, GameTool, ToolApplyPreview, ToolAvailability, ToolCategory, ToolConfig,
    ToolGameContext, ToolReleaseAsset, ToolReleaseInstallFuture, ToolReleaseListFuture,
    ToolReleaseSummary,
};

const CUSTOM_OPTISCALER_PROFILE: &str = "custom";
pub const OPTISCALER_SOURCE_OFFICIAL: &str = "github_release";
pub const OPTISCALER_SOURCE_GOVERLAY_BUILDS: &str = "goverlay_builds";
pub const OPTISCALER_SOURCE_GOVERLAY_FGMOD: &str = "goverlay_fgmod";
pub const FSR4_VARIANT_LATEST_FP8: &str = "latest_fp8";
pub const FSR4_VARIANT_INT8_402: &str = "int8_402";

const FSR4_DLL_NAME: &str = "amd_fidelityfx_upscaler_dx12.dll";
const FSR4_LATEST_DIR: &str = "FSR4_LATEST";
const FSR4_INT8_DIR: &str = "FSR4_INT8";
const OPTIPATCHER_REPO: &str = "optiscaler/OptiPatcher";
const OPTIPATCHER_ASSET: &str = "OptiPatcher.asi";
const FP8_EMULATION_ENV_KEY: &str = "DXIL_SPIRV_CONFIG";
const FP8_EMULATION_ENV_VALUE: &str = "wmma_rdna3_workaround";

pub static OPTISCALER: OptiScaler = OptiScaler;

pub struct OptiScaler;

pub const OPTISCALER_PROXY_DLLS: &[&str] = &[
    "dxgi.dll",
    "winmm.dll",
    "d3d12.dll",
    "dbghelp.dll",
    "version.dll",
    "wininet.dll",
    "winhttp.dll",
    "OptiScaler.asi",
];

pub const OPTISCALER_COMPANION_FILES: &[&str] = &[
    "fakenvapi.dll",
    "nvngx-wrapper.dll",
    "nvngx.dll",
    "_nvngx.dll",
    "nvngx_dlss.dll",
];

pub const OPTISCALER_COMPANION_DIRS: &[&str] = &["D3D12_OptiScaler", "plugins"];

/// DLLs that fgmod deletes at launch time. If any of these are deployed by
/// mods or by `OptiScaler` itself, the launch wrapper must restore them.
pub const FGMOD_DELETED_DLLS: &[&str] = &[
    "dxgi.dll",
    "winmm.dll",
    "nvngx.dll",
    "_nvngx.dll",
    "nvngx-wrapper.dll",
    "dlss-enabler.dll",
    "OptiScaler.dll",
];

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptiScalerInstallStatus {
    Absent,
    Managed,
    Unmanaged,
    PartiallyManaged,
    Conflicted,
}

impl std::fmt::Display for OptiScalerInstallStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Absent => write!(f, "absent"),
            Self::Managed => write!(f, "managed"),
            Self::Unmanaged => write!(f, "unmanaged"),
            Self::PartiallyManaged => write!(f, "partially managed"),
            Self::Conflicted => write!(f, "conflicted"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptiScalerVersionIdentity {
    CachedRelease(String),
    FileMetadata(String),
    ContentHash(String),
    Unknown,
}

impl std::fmt::Display for OptiScalerVersionIdentity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CachedRelease(tag) => write!(f, "{tag}"),
            Self::FileMetadata(version) => write!(f, "{version}"),
            Self::ContentHash(hash) => write!(f, "hash:{hash}"),
            Self::Unknown => write!(f, "unknown"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OptiScalerDetectedFile {
    pub rel_path: PathBuf,
    pub hash: Option<String>,
    pub managed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OptiScalerInstallState {
    pub status: OptiScalerInstallStatus,
    pub executable_dir: PathBuf,
    pub proxy_dlls: Vec<String>,
    pub wine_dll_overrides: Vec<String>,
    pub config_path: Option<PathBuf>,
    pub ini_settings: BTreeMap<String, String>,
    pub companion_files: Vec<PathBuf>,
    pub recognized_files: Vec<OptiScalerDetectedFile>,
    pub version: OptiScalerVersionIdentity,
    pub latest_backup: Option<PathBuf>,
}

impl OptiScalerInstallState {
    #[must_use]
    pub fn summary(&self) -> String {
        let proxy = if self.proxy_dlls.is_empty() {
            "none".to_string()
        } else {
            self.proxy_dlls.join(", ")
        };
        format!("{}; version {}; proxy {}", self.status, self.version, proxy)
    }
}

impl GameTool for OptiScaler {
    fn tool_id(&self) -> &'static str {
        "optiscaler"
    }

    fn display_name(&self) -> &'static str {
        "OptiScaler"
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::Upscaler
    }

    fn description(&self) -> &'static str {
        "OptiScaler / fgmod deployment for upscaling and frame-generation proxy DLLs."
    }

    fn settings_schema(&self) -> Vec<super::ToolSettingSpec> {
        let config = self.default_config();
        self.settings_schema_for(None, &config)
    }

    fn settings_schema_for(
        &self,
        context: Option<&ToolGameContext>,
        config: &ToolConfig,
    ) -> Vec<super::ToolSettingSpec> {
        let mut specs = vec![
            super::ToolSettingSpec::labeled_select(
                "source_mode",
                "Source",
                "Where modde should get OptiScaler files from.",
                &[
                    (OPTISCALER_SOURCE_OFFICIAL, "Official GitHub releases"),
                    (OPTISCALER_SOURCE_GOVERLAY_BUILDS, "GOverlay builds"),
                    (OPTISCALER_SOURCE_GOVERLAY_FGMOD, "GOverlay fgmod directory"),
                    ("local_dir", "Local OptiScaler directory"),
                ],
            )
            .section("Source"),
            super::ToolSettingSpec::labeled_select(
                "goverlay_channel",
                "GOverlay channel",
                "GOverlay OptiScaler builds channel.",
                &[
                    ("edge", "Bleeding-edge"),
                    ("stable", "Stable"),
                    ("master", "Master"),
                    ("any", "Any release branch"),
                ],
            )
            .section("Source"),
            super::ToolSettingSpec::select(
                "release_tag",
                "Release tag",
                "OptiScaler release tag selected in the UI.",
                std::slice::from_ref(&config.get_str("release_tag").unwrap_or("latest")),
            )
            .section("Source"),
            super::ToolSettingSpec::select(
                "release_asset",
                "Release asset",
                "Release asset selected from GitHub.",
                std::slice::from_ref(&config.get_str("release_asset").unwrap_or("")),
            )
            .section("Source"),
            super::ToolSettingSpec::labeled_select(
                "proxy_dll",
                "Proxy DLL",
                "DLL name used to load OptiScaler for this game.",
                &[
                    ("dxgi.dll", "dxgi.dll - DirectX graphics proxy"),
                    ("version.dll", "version.dll - Windows version proxy"),
                    ("dbghelp.dll", "dbghelp.dll - Debug helper proxy"),
                    ("d3d12.dll", "d3d12.dll - Direct3D 12 proxy"),
                    ("wininet.dll", "wininet.dll - WinINet proxy"),
                    ("winhttp.dll", "winhttp.dll - WinHTTP proxy"),
                    ("winmm.dll", "winmm.dll - Multimedia proxy"),
                    ("nvngx.dll", "nvngx.dll - NVIDIA NGX proxy"),
                    ("OptiScaler.asi", "OptiScaler.asi - ASI plugin"),
                ],
            )
            .section("Basic"),
            super::ToolSettingSpec::text(
                "dll_overrides",
                "DLL overrides",
                "Comma or whitespace separated Wine DLL override base names.",
            )
            .section("Basic"),
            super::ToolSettingSpec::bool(
                "copy_companion_files",
                "Copy companion files",
                "Copy fakenvapi, nvngx wrapper, and other DLLs found next to OptiScaler.",
            )
            .section("Basic"),
            super::ToolSettingSpec::labeled_select(
                "fsr4_variant",
                "FSR4 variant",
                "FSR4 payload copied as amd_fidelityfx_upscaler_dx12.dll.",
                &[
                    (FSR4_VARIANT_LATEST_FP8, "Latest (FP8)"),
                    (FSR4_VARIANT_INT8_402, "4.0.2c (INT8)"),
                ],
            )
            .section("Basic"),
            super::ToolSettingSpec::bool(
                "enable_optipatcher",
                "OptiPatcher",
                "Use OptiPatcher to unlock DLSS and DLSS frame generation inputs without whole-game spoofing in supported games.",
            )
            .section("Basic"),
            super::ToolSettingSpec::bool(
                "spoof_dlss",
                "Spoof DLSS fallback",
                "Fallback DXGI spoofing path for games that still need whole-game spoofing.",
            )
            .section("Basic"),
            super::ToolSettingSpec::read_only(
                "derived_executable_dir",
                "Executable directory",
                "Derived from the selected game's metadata.",
            )
            .section("Detected Game"),
        ];

        if let Some(context) = context {
            let profiles = resolve_optiscaler_profiles(&context.game_id);
            if !profiles.is_empty() {
                let profile_options = std::iter::once(super::ToolSelectOption::new(
                    CUSTOM_OPTISCALER_PROFILE,
                    "Custom / no community profile",
                ))
                .chain(
                    profiles
                        .iter()
                        .map(|profile| super::ToolSelectOption::new(profile.id, profile.name)),
                )
                .collect();
                specs.insert(
                    0,
                    super::ToolSettingSpec {
                        key: "optiscaler_profile",
                        label: "Profile",
                        description: "Community-tested OptiScaler profile to apply, or custom settings.",
                        section: "Profile",
                        advanced: false,
                        kind: super::ToolSettingKind::Select { options: profile_options },
                    },
                );
                specs.push(
                    super::ToolSettingSpec::read_only(
                        "optiscaler_profile_source_url",
                        "Profile source",
                        "Community compatibility source for the selected profile.",
                    )
                    .section("Profile"),
                );
                specs.push(
                    super::ToolSettingSpec::read_only(
                        "tested_optiscaler_version",
                        "Tested version",
                        "OptiScaler version reported by the selected profile.",
                    )
                    .section("Profile"),
                );
                specs.push(
                    super::ToolSettingSpec::read_only(
                        "optiscaler_profile_notes",
                        "Profile notes",
                        "Community notes for the selected profile.",
                    )
                    .section("Profile"),
                );
            }
        }

        if config.get_str("source_mode") == Some("local_dir") {
            specs.insert(
                1,
                super::ToolSettingSpec::path(
                    "local_source_dir",
                    "Local source directory",
                    "Directory containing OptiScaler.dll and companion files.",
                )
                .section("Source"),
            );
        }
        if config.get_str("source_mode") != Some(OPTISCALER_SOURCE_GOVERLAY_BUILDS) {
            specs.retain(|spec| spec.key != "goverlay_channel");
        }

        if config.get_str("fsr4_variant") == Some(FSR4_VARIANT_INT8_402) {
            specs.push(
                super::ToolSettingSpec::read_only(
                    "emulate_fp8",
                    "Emulate FP8",
                    "Only applies to the Latest (FP8) FSR4 variant.",
                )
                .section("Basic"),
            );
        } else {
            specs.push(
                super::ToolSettingSpec::bool(
                    "emulate_fp8",
                    "Emulate FP8",
                    "Set DXIL_SPIRV_CONFIG=wmma_rdna3_workaround for the Latest (FP8) FSR4 variant.",
                )
                .section("Basic"),
            );
        }

        specs.extend(goverlay_optiscaler_ini_specs());
        specs.extend(optiscaler_ini_specs(config));
        specs
    }

    fn detect_available(&self) -> ToolAvailability {
        // Check common locations for OptiScaler
        let candidates = [
            dirs::data_dir()
                .unwrap_or_default()
                .join("goverlay/fgmod/OptiScaler.dll"),
            dirs::home_dir()
                .unwrap_or_default()
                .join(".local/share/goverlay/fgmod/OptiScaler.dll"),
        ];

        for path in &candidates {
            if path.exists() {
                return ToolAvailability::Available {
                    version: Some("fgmod".into()),
                };
            }
        }

        // User can provide a custom path via settings
        ToolAvailability::Available {
            version: Some("user-provided".into()),
        }
    }

    fn env_vars(&self, config: &ToolConfig) -> SmallVec<[(String, String); 4]> {
        if config.get_bool("emulate_fp8")
            && config.get_str("fsr4_variant") == Some(FSR4_VARIANT_LATEST_FP8)
        {
            smallvec![(
                FP8_EMULATION_ENV_KEY.to_string(),
                FP8_EMULATION_ENV_VALUE.to_string()
            )]
        } else {
            SmallVec::new()
        }
    }

    fn wine_dll_overrides(&self, config: &ToolConfig) -> SmallVec<[String; 4]> {
        let primary = config
            .get_str("proxy_dll")
            .or_else(|| config.get_str("dll_name"))
            .unwrap_or("dxgi.dll")
            .trim_end_matches(".dll")
            .to_string();
        let mut overrides: SmallVec<[String; 4]> = smallvec![primary];

        if config.get_bool("needs_winmm") && !overrides.iter().any(|value| value == "winmm") {
            overrides.push("winmm".into());
        }
        if let Some(raw) = config.get_str("dll_overrides") {
            for part in raw.split([',', ';', ' ', '\n', '\t']) {
                let value = part.trim().trim_end_matches(".dll");
                if !value.is_empty() && !overrides.iter().any(|existing| existing == value) {
                    overrides.push(value.to_string());
                }
            }
        }

        overrides
    }

    fn apply(&self, game_dir: &Path, config: &ToolConfig) -> Result<AppliedFiles> {
        self.apply_for(game_dir, None, config)
    }

    fn apply_for(
        &self,
        game_dir: &Path,
        context: Option<&ToolGameContext>,
        config: &ToolConfig,
    ) -> Result<AppliedFiles> {
        let source_dir = resolve_source_dir(config).context(
            "optiscaler: choose a GitHub release, local directory, or install fgmod/goverlay",
        )?;

        let dll_name = config
            .get_str("proxy_dll")
            .or_else(|| config.get_str("dll_name"))
            .unwrap_or("dxgi.dll");
        let target_dir = context
            .and_then(|context| context.executable_dir.clone())
            .unwrap_or_else(|| legacy_target_dir(game_dir, config));

        std::fs::create_dir_all(&target_dir)
            .with_context(|| format!("failed to create {}", target_dir.display()))?;
        let applied_paths = managed_paths_from_config(config);
        let existing = scan_optiscaler_install_in_dir(&target_dir, &applied_paths)?;
        if matches!(
            existing.status,
            OptiScalerInstallStatus::Unmanaged
                | OptiScalerInstallStatus::PartiallyManaged
                | OptiScalerInstallStatus::Conflicted
        ) {
            backup_optiscaler_install(context.map(|context| context.game_id.as_str()), &existing)?;
        }

        let mut applied = AppliedFiles::default();

        // Copy OptiScaler as the requested DLL name
        let optiscaler_dll = source_dir.join("OptiScaler.dll");
        if optiscaler_dll.exists() {
            let dest = target_dir.join(dll_name);
            std::fs::copy(&optiscaler_dll, &dest)
                .with_context(|| format!("failed to copy OptiScaler to {}", dest.display()))?;
            let rel = relative_to_game(game_dir, &dest)?;
            applied.files.push(rel);
            info!(as_dll = %dll_name, "applied OptiScaler DLL");
        }

        // Copy OptiScaler.ini if present (or generate default)
        let ini_src = source_dir.join("OptiScaler.ini");
        let ini_dest = target_dir.join("OptiScaler.ini");
        if ini_src.exists() {
            let reset_reason = optiscaler_config_reset_reason(&existing, &ini_src, config);
            if reset_reason.is_some() {
                apply_ini_overrides_with_existing(&ini_src, None, &ini_dest, config)?;
            } else {
                apply_ini_overrides_with_existing(
                    &ini_src,
                    ini_dest.exists().then_some(&ini_dest),
                    &ini_dest,
                    config,
                )?;
            }
            let rel = relative_to_game(game_dir, &ini_dest)?;
            applied.files.push(rel);
        }

        // Copy additional DLLs from source (fakenvapi, nvngx-wrapper, etc.)
        if config.get_bool("copy_companion_files") {
            for entry in std::fs::read_dir(&source_dir)
                .with_context(|| format!("failed to read directory: {}", source_dir.display()))?
                .flatten()
            {
                let src = entry.path();
                if !src.is_file() {
                    continue;
                }
                let Some(name) = src.file_name().and_then(|name| name.to_str()) else {
                    continue;
                };
                if !name.eq_ignore_ascii_case("OptiScaler.dll")
                    && !name.eq_ignore_ascii_case("OptiScaler.ini")
                    && !name.eq_ignore_ascii_case(FSR4_DLL_NAME)
                    && name.to_ascii_lowercase().ends_with(".dll")
                {
                    let dest = target_dir.join(name);
                    std::fs::copy(&src, &dest)
                        .with_context(|| format!("failed to copy {}", dest.display()))?;
                    let rel = relative_to_game(game_dir, &dest)?;
                    applied.files.push(rel);
                }
            }
        }

        if let Some(fsr4_src) = selected_fsr4_variant_source(&source_dir, config) {
            if !fsr4_src.is_file() {
                anyhow::bail!(
                    "optiscaler: selected FSR4 variant '{}' was not found at {}",
                    fsr4_variant(config),
                    fsr4_src.display()
                );
            }
            let dest = target_dir.join(FSR4_DLL_NAME);
            std::fs::copy(&fsr4_src, &dest)
                .with_context(|| format!("failed to copy FSR4 variant to {}", dest.display()))?;
            let rel = relative_to_game(game_dir, &dest)?;
            applied.files.push(rel);
        }

        if config.get_bool("enable_optipatcher") {
            let optipatcher_src = optipatcher_asi_source(&source_dir);
            if !optipatcher_src.is_file() {
                anyhow::bail!(
                    "optiscaler: OptiPatcher.asi is required but not cached; install or update the selected OptiScaler release first"
                );
            }
            let dest = target_dir.join("plugins").join(OPTIPATCHER_ASSET);
            if let Some(parent) = dest.parent() {
                std::fs::create_dir_all(parent)
                    .with_context(|| format!("failed to create {}", parent.display()))?;
            }
            std::fs::copy(&optipatcher_src, &dest)
                .with_context(|| format!("failed to copy OptiPatcher.asi to {}", dest.display()))?;
            let rel = relative_to_game(game_dir, &dest)?;
            applied.files.push(rel);
        }

        if source_dir.join("D3D12_OptiScaler").is_dir() {
            let dest = target_dir.join("D3D12_OptiScaler");
            copy_dir_recursive(&source_dir.join("D3D12_OptiScaler"), &dest)?;
            collect_relative_files(game_dir, &dest, &mut applied.files)?;
        }

        Ok(applied)
    }

    fn preview_apply_for(
        &self,
        game_dir: &Path,
        context: Option<&ToolGameContext>,
        config: &ToolConfig,
    ) -> Result<ToolApplyPreview> {
        let Some(source_dir) = resolve_source_dir(config) else {
            return Ok(optiscaler_missing_preview(
                "optiscaler: choose a GitHub release, local directory, or install fgmod/goverlay",
            ));
        };

        let dll_name = config
            .get_str("proxy_dll")
            .or_else(|| config.get_str("dll_name"))
            .unwrap_or("dxgi.dll");
        let target_dir = context
            .and_then(|context| context.executable_dir.clone())
            .unwrap_or_else(|| legacy_target_dir(game_dir, config));
        let applied_paths = managed_paths_from_config(config);
        let existing = scan_optiscaler_install_in_dir(&target_dir, &applied_paths)?;
        let mut preview = ToolApplyPreview::default();

        let optiscaler_dll = source_dir.join("OptiScaler.dll");
        if optiscaler_dll.is_file() {
            preview_source_file(
                game_dir,
                &optiscaler_dll,
                &target_dir.join(dll_name),
                &mut preview,
            )?;
        } else {
            preview.missing_inputs.push(format!(
                "optiscaler: OptiScaler.dll not found in {}",
                source_dir.display()
            ));
        }

        let ini_src = source_dir.join("OptiScaler.ini");
        let ini_dest = target_dir.join("OptiScaler.ini");
        if ini_src.is_file() {
            let reset_reason = optiscaler_config_reset_reason(&existing, &ini_src, config);
            let content = if reset_reason.is_some() {
                build_ini_with_overrides(&ini_src, None, config)?.into_bytes()
            } else {
                build_ini_with_overrides(
                    &ini_src,
                    ini_dest.exists().then_some(ini_dest.as_path()),
                    config,
                )?
                .into_bytes()
            };
            preview_bytes(game_dir, &ini_dest, &content, &mut preview);
        } else {
            preview.missing_inputs.push(format!(
                "optiscaler: OptiScaler.ini not found in {}",
                source_dir.display()
            ));
        }

        if config.get_bool("copy_companion_files") {
            for entry in std::fs::read_dir(&source_dir)
                .with_context(|| format!("failed to read directory: {}", source_dir.display()))?
                .flatten()
            {
                let src = entry.path();
                if !src.is_file() {
                    continue;
                }
                let Some(name) = src.file_name().and_then(|name| name.to_str()) else {
                    continue;
                };
                if !name.eq_ignore_ascii_case("OptiScaler.dll")
                    && !name.eq_ignore_ascii_case("OptiScaler.ini")
                    && !name.eq_ignore_ascii_case(FSR4_DLL_NAME)
                    && name.to_ascii_lowercase().ends_with(".dll")
                {
                    preview_source_file(game_dir, &src, &target_dir.join(name), &mut preview)?;
                }
            }
        }

        if let Some(fsr4_src) = selected_fsr4_variant_source(&source_dir, config) {
            if fsr4_src.is_file() {
                preview_source_file(
                    game_dir,
                    &fsr4_src,
                    &target_dir.join(FSR4_DLL_NAME),
                    &mut preview,
                )?;
            } else {
                preview.missing_inputs.push(format!(
                    "optiscaler: selected FSR4 variant '{}' not found at {}",
                    fsr4_variant(config),
                    fsr4_src.display()
                ));
            }
        }

        if config.get_bool("enable_optipatcher") {
            let optipatcher_src = optipatcher_asi_source(&source_dir);
            if optipatcher_src.is_file() {
                preview_source_file(
                    game_dir,
                    &optipatcher_src,
                    &target_dir.join("plugins").join(OPTIPATCHER_ASSET),
                    &mut preview,
                )?;
            } else {
                preview.missing_inputs.push(
                    "optiscaler: OptiPatcher.asi is required but not cached; install or update the selected OptiScaler release first"
                        .to_string(),
                );
            }
        }

        let d3d12_src = source_dir.join("D3D12_OptiScaler");
        if d3d12_src.is_dir() {
            preview_dir_recursive(
                game_dir,
                &d3d12_src,
                &target_dir.join("D3D12_OptiScaler"),
                &mut preview,
            )?;
        }

        Ok(preview)
    }

    fn default_config(&self) -> ToolConfig {
        let mut config = ToolConfig::new("optiscaler");
        config.set(
            "source_mode",
            serde_json::json!(OPTISCALER_SOURCE_GOVERLAY_FGMOD),
        );
        config.set("goverlay_channel", serde_json::json!("edge"));
        config.set("release_tag", serde_json::json!("latest"));
        config.set("release_asset", serde_json::json!(""));
        config.set("local_source_dir", serde_json::json!(""));
        config.set("proxy_dll", serde_json::json!("dxgi.dll"));
        config.set("dll_overrides", serde_json::json!(""));
        config.set("copy_companion_files", serde_json::json!(true));
        config.set("enable_optipatcher", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_LATEST_FP8));
        config.set("emulate_fp8", serde_json::json!(false));
        config.set("spoof_dlss", serde_json::json!(false));
        config.set("ini_overrides", serde_json::json!({}));
        config
    }

    fn default_config_for(&self, context: Option<&ToolGameContext>) -> ToolConfig {
        let mut config = self.default_config();
        apply_game_defaults(&mut config, context);
        config
    }

    fn supports_releases(&self) -> bool {
        true
    }

    fn list_releases(&self) -> ToolReleaseListFuture<'_> {
        Box::pin(async { list_optiscaler_releases().await })
    }

    fn installable_release_assets(&self, release: &ToolReleaseSummary) -> Vec<String> {
        release
            .assets
            .iter()
            .filter(|asset| is_installable_release_asset(&asset.name))
            .map(|asset| asset.name.clone())
            .collect()
    }

    fn install_release<'a>(
        &'a self,
        _game_id: &'a str,
        mut config: ToolConfig,
        tag: &'a str,
        asset: &'a str,
    ) -> ToolReleaseInstallFuture<'a> {
        Box::pin(async move {
            install_optiscaler_release_asset(tag, asset).await?;
            let normalized_tag = normalize_optiscaler_release_tag(tag);
            apply_optiscaler_release_selection(&mut config, &normalized_tag, asset);
            if config.get_bool("enable_optipatcher") {
                install_latest_optipatcher().await?;
            }
            Ok(config)
        })
    }

    fn install_release_from_path<'a>(
        &'a self,
        _game_id: &'a str,
        mut config: ToolConfig,
        tag: &'a str,
        asset: &'a str,
        path: PathBuf,
    ) -> ToolReleaseInstallFuture<'a> {
        Box::pin(async move {
            install_optiscaler_release_asset_from_path(tag, asset, &path)?;
            let normalized_tag = normalize_optiscaler_release_tag(tag);
            apply_optiscaler_release_selection(&mut config, &normalized_tag, asset);
            if config.get_bool("enable_optipatcher") {
                install_latest_optipatcher().await?;
            }
            Ok(config)
        })
    }
}

pub async fn list_optiscaler_releases() -> Result<Vec<ToolReleaseSummary>> {
    let mut releases = official_optiscaler_releases().await?;
    releases.extend(goverlay_optiscaler_releases().await?);
    Ok(releases)
}

#[must_use]
pub fn normalize_optiscaler_release_config(config: &mut ToolConfig) -> bool {
    let mut changed = false;
    if let Some(tag) = config.get_str("release_tag").map(str::to_string)
        && !tag.trim().is_empty()
    {
        let normalized = normalize_optiscaler_release_tag(&tag);
        if normalized != tag {
            config.set("release_tag", serde_json::json!(normalized));
            changed = true;
        }
    }
    if let Some(tag) = config.get_str("release_tag").map(str::to_string) {
        if let Some(channel) = optiscaler_goverlay_channel_for_tag(&tag) {
            if config.get_str("source_mode") != Some(OPTISCALER_SOURCE_GOVERLAY_BUILDS) {
                config.set(
                    "source_mode",
                    serde_json::json!(OPTISCALER_SOURCE_GOVERLAY_BUILDS),
                );
                changed = true;
            }
            if config.get_str("goverlay_channel") != Some(channel) {
                config.set("goverlay_channel", serde_json::json!(channel));
                changed = true;
            }
        } else if !tag.trim().is_empty()
            && optiscaler_release_is_official(&tag)
            && config.get_str("source_mode").is_none()
        {
            config.set("source_mode", serde_json::json!(OPTISCALER_SOURCE_OFFICIAL));
            changed = true;
        }
    }
    changed
}

#[must_use]
pub fn optiscaler_release_matches_config(
    release: &ToolReleaseSummary,
    config: &ToolConfig,
) -> bool {
    match config
        .get_str("source_mode")
        .unwrap_or(OPTISCALER_SOURCE_GOVERLAY_FGMOD)
    {
        OPTISCALER_SOURCE_OFFICIAL => optiscaler_release_is_official(&release.tag),
        OPTISCALER_SOURCE_GOVERLAY_BUILDS => {
            optiscaler_goverlay_channel_for_tag(&release.tag)
                == Some(config.get_str("goverlay_channel").unwrap_or("edge"))
        }
        _ => false,
    }
}

#[must_use]
pub fn optiscaler_release_is_official(tag: &str) -> bool {
    normalize_optiscaler_release_tag(tag).starts_with("official:")
}

#[must_use]
pub fn optiscaler_goverlay_channel_for_tag(tag: &str) -> Option<&'static str> {
    let encoded = normalize_optiscaler_release_tag(tag);
    let channel = encoded
        .strip_prefix("goverlay-")
        .and_then(|rest| rest.split_once(':'))
        .map(|(channel, _)| channel)?;
    match channel {
        "edge" => Some("edge"),
        "stable" => Some("stable"),
        "master" => Some("master"),
        "any" => Some("any"),
        _ => None,
    }
}

pub async fn install_optiscaler_release_asset(tag: &str, asset_name: &str) -> Result<PathBuf> {
    let releases = list_optiscaler_releases().await?;
    let (normalized_tag, asset) = select_optiscaler_release_asset(&releases, tag, asset_name)?;
    if !is_installable_release_asset(&asset.name) {
        anyhow::bail!(
            "selected asset '{}' is not a supported archive (.zip or .7z)",
            asset.name
        );
    }

    let cache_dir = cached_release_dir(&normalized_tag);
    std::fs::create_dir_all(&cache_dir)
        .with_context(|| format!("failed to create {}", cache_dir.display()))?;
    let archive_path = cache_dir.join(&asset.name);
    download_release_asset(asset, &archive_path).await?;
    extract_optiscaler_archive_flat(&archive_path, &cache_dir)?;
    Ok(cache_dir)
}

pub fn install_optiscaler_release_asset_from_path(
    tag: &str,
    asset_name: &str,
    path: &Path,
) -> Result<PathBuf> {
    let normalized_tag = normalize_optiscaler_release_tag(tag);
    if !is_installable_release_asset(asset_name) {
        anyhow::bail!("selected asset '{asset_name}' is not a supported archive (.zip or .7z)");
    }

    let cache_dir = cached_release_dir(&normalized_tag);
    std::fs::create_dir_all(&cache_dir)
        .with_context(|| format!("failed to create {}", cache_dir.display()))?;
    let archive_path = cache_dir.join(asset_name);
    std::fs::copy(path, &archive_path).with_context(|| {
        format!(
            "failed to copy release asset {} to {}",
            path.display(),
            archive_path.display()
        )
    })?;
    extract_optiscaler_archive_flat(&archive_path, &cache_dir)?;
    Ok(cache_dir)
}

pub async fn install_latest_optipatcher() -> Result<PathBuf> {
    let release = super::release::list_github_releases(OPTIPATCHER_REPO)
        .await?
        .into_iter()
        .next()
        .ok_or_else(|| anyhow::anyhow!("OptiPatcher release not found"))?;
    let asset = release
        .assets
        .iter()
        .find(|asset| asset.name.eq_ignore_ascii_case(OPTIPATCHER_ASSET))
        .ok_or_else(|| {
            anyhow::anyhow!(
                "OptiPatcher release {} did not contain {}",
                release.tag,
                OPTIPATCHER_ASSET
            )
        })?;
    let dest = cached_optipatcher_asi();
    download_release_asset(asset, &dest).await?;
    Ok(dest)
}

fn normalize_optiscaler_release_tag(tag: &str) -> String {
    if tag.contains(':') {
        tag.to_string()
    } else {
        encode_optiscaler_release_tag("official", tag)
    }
}

fn select_optiscaler_release_asset<'a>(
    releases: &'a [ToolReleaseSummary],
    tag: &str,
    asset_name: &str,
) -> Result<(String, &'a ToolReleaseAsset)> {
    let normalized_tag = normalize_optiscaler_release_tag(tag);
    let release = releases
        .iter()
        .find(|release| release.tag == normalized_tag)
        .ok_or_else(|| anyhow::anyhow!("OptiScaler release tag not found: {tag}"))?;
    let asset = release
        .assets
        .iter()
        .find(|asset| asset.name == asset_name)
        .ok_or_else(|| anyhow::anyhow!("asset '{asset_name}' not found in release {tag}"))?;
    Ok((normalized_tag, asset))
}

async fn official_optiscaler_releases() -> Result<Vec<ToolReleaseSummary>> {
    Ok(
        super::release::list_github_releases("optiscaler/OptiScaler")
            .await?
            .into_iter()
            .map(|mut release| {
                release.tag = encode_optiscaler_release_tag("official", &release.tag);
                release
            })
            .collect(),
    )
}

async fn goverlay_optiscaler_releases() -> Result<Vec<ToolReleaseSummary>> {
    let mut releases: Vec<_> =
        super::release::list_github_releases("benjamimgois/OptiScaler-builds")
            .await?
            .into_iter()
            .filter_map(goverlay_release_summary)
            .collect();
    releases.sort_by(|left, right| right.published_at.cmp(&left.published_at));
    Ok(releases)
}

fn goverlay_release_summary(mut release: ToolReleaseSummary) -> Option<ToolReleaseSummary> {
    let channel = goverlay_release_channel(&release.tag)?;
    let assets = goverlay_installable_assets(channel, &release);
    if assets.is_empty() {
        return None;
    }
    release.tag = encode_optiscaler_release_tag(channel, &release.tag);
    release.assets = assets;
    Some(release)
}

fn encode_optiscaler_release_tag(source: &str, tag: &str) -> String {
    if tag.contains(':') {
        tag.to_string()
    } else {
        format!("{source}:{tag}")
    }
}

fn goverlay_release_channel(tag: &str) -> Option<&'static str> {
    if tag.starts_with("edge-") {
        Some("goverlay-edge")
    } else if tag.starts_with("master-") {
        Some("goverlay-master")
    } else if tag.starts_with("any-release-") {
        Some("goverlay-any")
    } else if is_goverlay_stable_tag(tag) {
        Some("goverlay-stable")
    } else {
        None
    }
}

fn is_goverlay_stable_tag(tag: &str) -> bool {
    let (version, patch) = tag
        .split_once('-')
        .map_or((tag, None), |(version, patch)| (version, Some(patch)));
    if let Some(patch) = patch
        && (patch.is_empty() || !patch.chars().all(|ch| ch.is_ascii_digit()))
    {
        return false;
    }
    let mut parts = version.split('.');
    let Some(major) = parts.next() else {
        return false;
    };
    let Some(minor) = parts.next() else {
        return false;
    };
    let Some(patch) = parts.next() else {
        return false;
    };
    parts.next().is_none()
        && [major, minor, patch]
            .into_iter()
            .all(|part| !part.is_empty() && part.chars().all(|ch| ch.is_ascii_digit()))
}

fn goverlay_installable_assets(
    channel: &str,
    release: &ToolReleaseSummary,
) -> Vec<ToolReleaseAsset> {
    match channel {
        "goverlay-stable" => release_assets_named(release, "optiScaler-stable.7z"),
        "goverlay-edge" => release_assets_named(release, "optiscaler-edge.7z"),
        "goverlay-master" | "goverlay-any" => release
            .assets
            .iter()
            .filter(|asset| {
                let lower = asset.name.to_ascii_lowercase();
                lower.ends_with(".7z") && !lower.ends_with(".json")
            })
            .cloned()
            .collect(),
        _ => Vec::new(),
    }
}

fn release_assets_named(
    release: &ToolReleaseSummary,
    expected_name: &str,
) -> Vec<ToolReleaseAsset> {
    release
        .assets
        .iter()
        .filter(|asset| asset.name.eq_ignore_ascii_case(expected_name))
        .cloned()
        .collect()
}

async fn download_release_asset(asset: &ToolReleaseAsset, dest: &Path) -> Result<()> {
    if let Some(parent) = dest.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    let client = Client::new();
    let response = client
        .get(&asset.download_url)
        .header("User-Agent", "modde")
        .send()
        .await?
        .error_for_status()?;
    let mut file = tokio::fs::File::create(dest)
        .await
        .with_context(|| format!("failed to create {}", dest.display()))?;
    let mut stream = response.bytes_stream();
    while let Some(chunk) = stream.next().await {
        file.write_all(&chunk?).await?;
    }
    file.flush().await?;
    Ok(())
}

#[must_use]
pub fn is_installable_release_asset(name: &str) -> bool {
    let lower = name.to_ascii_lowercase();
    lower.ends_with(".zip") || lower.ends_with(".7z")
}

fn legacy_target_dir(game_dir: &Path, config: &ToolConfig) -> PathBuf {
    let exe_subdir = config.get_str("exe_subdir").unwrap_or("");
    if exe_subdir.is_empty() {
        game_dir.to_path_buf()
    } else {
        game_dir.join(exe_subdir)
    }
}

fn relative_to_game(game_dir: &Path, dest: &Path) -> Result<PathBuf> {
    dest.strip_prefix(game_dir)
        .map(Path::to_path_buf)
        .with_context(|| {
            format!(
                "optiscaler: destination {} is not under game dir {}",
                dest.display(),
                game_dir.display()
            )
        })
}

#[must_use]
pub fn cached_release_dir(tag: &str) -> PathBuf {
    modde_core::paths::modde_data_dir()
        .join("tools")
        .join("optiscaler")
        .join(sanitize_tag(tag))
}

#[must_use]
pub fn cached_optipatcher_dir() -> PathBuf {
    modde_core::paths::modde_data_dir()
        .join("tools")
        .join("optipatcher")
        .join("rolling")
}

#[must_use]
pub fn cached_optipatcher_asi() -> PathBuf {
    cached_optipatcher_dir().join(OPTIPATCHER_ASSET)
}

fn sanitize_tag(tag: &str) -> String {
    tag.chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_') {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

fn resolve_source_dir(config: &ToolConfig) -> Option<PathBuf> {
    match config.get_str("source_mode").unwrap_or("goverlay_fgmod") {
        OPTISCALER_SOURCE_OFFICIAL | OPTISCALER_SOURCE_GOVERLAY_BUILDS => {
            let tag = config.get_str("release_tag")?;
            cached_release_source_dir(tag)
        }
        "local_dir" => config
            .get_str("local_source_dir")
            .or_else(|| config.get_str("source_dir"))
            .filter(|value| !value.trim().is_empty())
            .map(PathBuf::from),
        _ => {
            let fgmod = dirs::home_dir()?.join(".local/share/goverlay/fgmod");
            fgmod.is_dir().then_some(fgmod)
        }
    }
}

fn cached_release_source_dir(tag: &str) -> Option<PathBuf> {
    let candidates = if let Some(official_tag) = tag.strip_prefix("official:") {
        vec![cached_release_dir(tag), cached_release_dir(official_tag)]
    } else {
        let normalized = normalize_optiscaler_release_tag(tag);
        if normalized == tag {
            vec![cached_release_dir(tag)]
        } else {
            vec![cached_release_dir(tag), cached_release_dir(&normalized)]
        }
    };
    candidates
        .into_iter()
        .find(|dir| dir.join("OptiScaler.dll").exists())
}

fn optiscaler_missing_preview(message: impl Into<String>) -> ToolApplyPreview {
    ToolApplyPreview {
        missing_inputs: vec![message.into()],
        ..ToolApplyPreview::default()
    }
}

fn fsr4_variant(config: &ToolConfig) -> &str {
    match config.get_str("fsr4_variant") {
        Some(FSR4_VARIANT_INT8_402) => FSR4_VARIANT_INT8_402,
        _ => FSR4_VARIANT_LATEST_FP8,
    }
}

fn selected_fsr4_variant_source(source_dir: &Path, config: &ToolConfig) -> Option<PathBuf> {
    let dir = match fsr4_variant(config) {
        FSR4_VARIANT_INT8_402 => FSR4_INT8_DIR,
        FSR4_VARIANT_LATEST_FP8 => FSR4_LATEST_DIR,
        _ => return None,
    };
    let selected = source_dir.join(dir).join(FSR4_DLL_NAME);
    let has_variant_payloads =
        source_dir.join(FSR4_LATEST_DIR).is_dir() || source_dir.join(FSR4_INT8_DIR).is_dir();
    let expects_goverlay_payloads = matches!(
        config.get_str("source_mode"),
        Some(OPTISCALER_SOURCE_GOVERLAY_BUILDS | OPTISCALER_SOURCE_GOVERLAY_FGMOD)
    );
    (selected.is_file() || has_variant_payloads || expects_goverlay_payloads).then_some(selected)
}

fn optipatcher_asi_source(source_dir: &Path) -> PathBuf {
    [
        source_dir.join("plugins").join(OPTIPATCHER_ASSET),
        source_dir.join(OPTIPATCHER_ASSET),
        cached_optipatcher_asi(),
    ]
    .into_iter()
    .find(|path| path.is_file())
    .unwrap_or_else(cached_optipatcher_asi)
}

fn preview_source_file(
    game_dir: &Path,
    src: &Path,
    dest: &Path,
    preview: &mut ToolApplyPreview,
) -> Result<()> {
    let expected =
        std::fs::read(src).with_context(|| format!("failed to read {}", src.display()))?;
    preview_bytes(game_dir, dest, &expected, preview);
    Ok(())
}

fn preview_bytes(game_dir: &Path, dest: &Path, expected: &[u8], preview: &mut ToolApplyPreview) {
    let changed = std::fs::read(dest).map_or(true, |current| current != expected);
    let rel = dest.strip_prefix(game_dir).unwrap_or(dest).to_path_buf();
    preview.record_file(rel, changed);
}

fn preview_dir_recursive(
    game_dir: &Path,
    src: &Path,
    dest: &Path,
    preview: &mut ToolApplyPreview,
) -> Result<()> {
    for entry in std::fs::read_dir(src)
        .with_context(|| format!("failed to read directory: {}", src.display()))?
        .flatten()
    {
        let ty = entry.file_type()?;
        let src_path = entry.path();
        let dest_path = dest.join(entry.file_name());
        if ty.is_dir() {
            preview_dir_recursive(game_dir, &src_path, &dest_path, preview)?;
        } else {
            preview_source_file(game_dir, &src_path, &dest_path, preview)?;
        }
    }
    Ok(())
}

fn goverlay_optiscaler_ini_specs() -> Vec<super::ToolSettingSpec> {
    vec![
        super::ToolSettingSpec::labeled_select(
            "ini_overrides.Menu.ShortcutKey",
            "Menu shortcut",
            "OptiScaler [Menu] ShortcutKey override.",
            &[
                ("auto", "Auto"),
                ("INSERT", "Insert"),
                ("HOME", "Home"),
                ("END", "End"),
                ("DELETE", "Delete"),
                ("BACKQUOTE", "Backquote"),
                ("F1", "F1"),
                ("F2", "F2"),
                ("F3", "F3"),
                ("F4", "F4"),
                ("F5", "F5"),
                ("F6", "F6"),
                ("F7", "F7"),
                ("F8", "F8"),
                ("F9", "F9"),
                ("F10", "F10"),
                ("F11", "F11"),
                ("F12", "F12"),
            ],
        )
        .section("Menu"),
        super::ToolSettingSpec::number(
            "ini_overrides.Menu.Scale",
            "Menu scale",
            "OptiScaler [Menu] Scale override.",
            0.5,
            2.0,
            0.1,
        )
        .section("Menu"),
        super::ToolSettingSpec::tri_state_bool(
            "ini_overrides.NvApi.OverrideNvapiDll",
            "Override NVAPI DLL",
            "OptiScaler OverrideNvapiDll override.",
        )
        .section("Fakenvapi"),
        super::ToolSettingSpec::labeled_select(
            "ini_overrides.FSR.UpscalerIndex",
            "FSR upscaler backend",
            "OptiScaler [FSR] UpscalerIndex override.",
            &[
                ("auto", "Auto"),
                ("0", "0 - FSR 4.0.2"),
                ("1", "1 - FSR 3.1.5"),
                ("2", "2 - FSR 2.3.4"),
            ],
        )
        .section("Basic"),
        super::ToolSettingSpec::labeled_select(
            "ini_overrides.FSR.FGIndex",
            "FSR frame generation backend",
            "OptiScaler [FSR] FGIndex override.",
            &[
                ("auto", "Auto"),
                ("0", "0 - FSR 4.0.0"),
                ("1", "1 - FSR 3.1.6"),
            ],
        )
        .section("Basic"),
        super::ToolSettingSpec::labeled_select(
            "ini_overrides.fakenvapi.force_reflex",
            "Force Reflex",
            "fakenvapi force_reflex override.",
            &[
                ("0", "0 - Follow in-game setting"),
                ("1", "1 - Force disable"),
                ("2", "2 - Force enable"),
            ],
        )
        .section("Fakenvapi"),
        super::ToolSettingSpec::tri_state_bool(
            "ini_overrides.fakenvapi.force_latencyflex",
            "Force LatencyFlex",
            "fakenvapi force_latencyflex override.",
        )
        .section("Fakenvapi"),
        super::ToolSettingSpec::labeled_select(
            "ini_overrides.fakenvapi.latencyflex_mode",
            "LatencyFlex mode",
            "fakenvapi latencyflex_mode override.",
            &[
                ("0", "0 - Conservative"),
                ("1", "1 - Aggressive"),
                ("2", "2 - Use Reflex frame IDs"),
            ],
        )
        .section("Fakenvapi"),
        super::ToolSettingSpec::tri_state_bool(
            "ini_overrides.fakenvapi.enable_trace_logs",
            "Trace logs",
            "fakenvapi enable_trace_logs override.",
        )
        .section("Fakenvapi"),
    ]
}

fn optiscaler_ini_specs(config: &ToolConfig) -> Vec<super::ToolSettingSpec> {
    let Some(source_dir) = resolve_source_dir(config) else {
        return Vec::new();
    };
    let ini = source_dir.join("OptiScaler.ini");
    let Ok(content) = std::fs::read_to_string(ini) else {
        return Vec::new();
    };
    parse_ini_keys(&content)
        .into_iter()
        .filter(|key| {
            !matches!(
                key.as_str(),
                "FSR.Fsr4Update" | "Spoofing.Dxgi" | "Plugins.LoadAsiPlugins"
            )
        })
        .take(24)
        .map(|key| {
            let leaked: &'static str = Box::leak(format!("ini_overrides.{key}").into_boxed_str());
            let label: &'static str = Box::leak(key.into_boxed_str());
            infer_optiscaler_ini_spec(leaked, label)
                .section("Advanced")
                .advanced()
        })
        .collect()
}

fn infer_optiscaler_ini_spec(key: &'static str, label: &'static str) -> super::ToolSettingSpec {
    let lower = label.to_ascii_lowercase();
    if matches!(
        lower.as_str(),
        "fsr4update"
            | "dxgi"
            | "loadasiplugins"
            | "overridenvapidll"
            | "force_latencyflex"
            | "enable_trace_logs"
    ) {
        return super::ToolSettingSpec::tri_state_bool(key, label, "OptiScaler.ini override.");
    }
    if lower.ends_with("scale")
        || lower.ends_with("alpha")
        || lower.contains("sharpness")
        || lower.contains("bias")
    {
        return super::ToolSettingSpec::number(
            key,
            label,
            "OptiScaler.ini override.",
            0.0,
            10.0,
            0.05,
        );
    }
    super::ToolSettingSpec::text(key, label, "OptiScaler.ini override.")
}

pub fn extract_optiscaler_archive_flat(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let lower = archive_path.to_string_lossy().to_ascii_lowercase();
    if lower.ends_with(".zip") {
        return extract_zip_flat(archive_path, dest_dir);
    }
    if lower.ends_with(".7z") {
        return extract_7z_flat(archive_path, dest_dir);
    }
    anyhow::bail!(
        "unsupported OptiScaler archive type: {}",
        archive_path.display()
    )
}

fn extract_zip_flat(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let file = std::fs::File::open(archive_path)
        .with_context(|| format!("failed to open {}", archive_path.display()))?;
    let mut archive = zip::ZipArchive::new(file)?;
    let mut copied = 0usize;
    let mut copied_optiscaler = false;
    for index in 0..archive.len() {
        let mut entry = archive.by_index(index)?;
        if entry.is_dir() {
            continue;
        }
        let Some(enclosed) = entry.enclosed_name() else {
            continue;
        };
        let Some(name) = enclosed.file_name().map(ToOwned::to_owned) else {
            continue;
        };
        let Some(name_str) = name.to_str() else {
            continue;
        };
        let lower = name_str.to_ascii_lowercase();
        if !is_optiscaler_payload_file(&lower) {
            continue;
        }
        let out = optiscaler_payload_dest(dest_dir, &enclosed, &name);
        if let Some(parent) = out.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        let mut output = std::fs::File::create(&out)
            .with_context(|| format!("failed to create {}", out.display()))?;
        std::io::copy(&mut entry, &mut output)?;
        copied += 1;
        copied_optiscaler |= lower == "optiscaler.dll";
    }
    if copied == 0 || !copied_optiscaler {
        anyhow::bail!("archive did not contain OptiScaler.dll");
    }
    Ok(())
}

fn extract_7z_flat(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let tmp_base = modde_core::paths::modde_data_dir().join("tmp");
    std::fs::create_dir_all(&tmp_base)
        .with_context(|| format!("failed to create {}", tmp_base.display()))?;
    let extract_dir = tmp_base.join(format!(
        "modde-optiscaler-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_nanos()
    ));
    std::fs::create_dir_all(&extract_dir)
        .with_context(|| format!("failed to create {}", extract_dir.display()))?;
    let out_arg = format!("-o{}", extract_dir.display());
    let archive_arg = archive_path.to_string_lossy().to_string();
    let mut extracted = false;
    for bin in ["7zz", "7z"] {
        let status = Command::new(bin)
            .args(["x", "-y", &out_arg, &archive_arg])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
        if status.is_ok_and(|status| status.success()) {
            extracted = true;
            break;
        }
    }
    if !extracted {
        anyhow::bail!(
            "failed to extract {} (tried 7zz and 7z)",
            archive_path.display()
        );
    }
    let result = copy_optiscaler_payload_flat(&extract_dir, dest_dir);
    let _ = std::fs::remove_dir_all(&extract_dir);
    result
}

fn copy_optiscaler_payload_flat(source_root: &Path, dest_dir: &Path) -> Result<()> {
    let mut stack = vec![source_root.to_path_buf()];
    let mut copied = 0usize;
    let mut copied_optiscaler = false;
    while let Some(dir) = stack.pop() {
        for entry in std::fs::read_dir(&dir)
            .with_context(|| format!("failed to read directory: {}", dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            let metadata = path.symlink_metadata()?;
            if metadata.file_type().is_symlink() {
                continue;
            }
            if metadata.is_dir() {
                stack.push(path);
                continue;
            }
            let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
                continue;
            };
            let lower = name.to_ascii_lowercase();
            if is_optiscaler_payload_file(&lower) {
                let relative = path.strip_prefix(source_root).unwrap_or(&path);
                let dest = optiscaler_payload_dest(dest_dir, relative, std::ffi::OsStr::new(name));
                if let Some(parent) = dest.parent() {
                    std::fs::create_dir_all(parent)
                        .with_context(|| format!("failed to create {}", parent.display()))?;
                }
                std::fs::copy(&path, &dest)
                    .with_context(|| format!("failed to copy {}", dest.display()))?;
                copied += 1;
                copied_optiscaler |= lower == "optiscaler.dll";
            }
        }
    }
    if copied == 0 || !copied_optiscaler {
        anyhow::bail!("archive did not contain OptiScaler.dll");
    }
    Ok(())
}

fn is_optiscaler_payload_file(lower_name: &str) -> bool {
    matches!(
        lower_name,
        "optiscaler.dll"
            | "optiscaler.ini"
            | "fakenvapi.dll"
            | "nvngx-wrapper.dll"
            | "optipatcher.asi"
    ) || lower_name.ends_with(".dll")
}

fn optiscaler_payload_dest(
    dest_dir: &Path,
    relative_path: &Path,
    file_name: &std::ffi::OsStr,
) -> PathBuf {
    if file_name
        .to_str()
        .is_some_and(|name| name.eq_ignore_ascii_case(FSR4_DLL_NAME))
    {
        for component in relative_path.components() {
            let value = component.as_os_str().to_string_lossy();
            if value.eq_ignore_ascii_case(FSR4_LATEST_DIR) {
                return dest_dir.join(FSR4_LATEST_DIR).join(FSR4_DLL_NAME);
            }
            if value.eq_ignore_ascii_case(FSR4_INT8_DIR) {
                return dest_dir.join(FSR4_INT8_DIR).join(FSR4_DLL_NAME);
            }
        }
    }
    if file_name
        .to_str()
        .is_some_and(|name| name.eq_ignore_ascii_case(OPTIPATCHER_ASSET))
    {
        return dest_dir.join("plugins").join(OPTIPATCHER_ASSET);
    }
    dest_dir.join(file_name)
}

#[must_use]
pub fn parse_optiscaler_ini(content: &str) -> BTreeMap<String, String> {
    let mut section = String::new();
    let mut values = BTreeMap::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with(';') {
            continue;
        }
        if trimmed.starts_with('[') && trimmed.ends_with(']') {
            section = trimmed[1..trimmed.len() - 1].trim().to_string();
            continue;
        }
        if let Some((key, value)) = trimmed.split_once('=') {
            let key = key.trim();
            if !key.is_empty() {
                let path = if section.is_empty() {
                    key.to_string()
                } else {
                    format!("{section}.{key}")
                };
                values.insert(path, value.trim().to_string());
            }
        }
    }
    values
}

fn parse_ini_keys(content: &str) -> Vec<String> {
    let mut section = String::new();
    let mut keys = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with(';') {
            continue;
        }
        if trimmed.starts_with('[') && trimmed.ends_with(']') {
            section = trimmed[1..trimmed.len() - 1].trim().to_string();
            continue;
        }
        if let Some((key, _)) = trimmed.split_once('=') {
            let key = key.trim();
            if !key.is_empty() {
                if section.is_empty() {
                    keys.push(key.to_string());
                } else {
                    keys.push(format!("{section}.{key}"));
                }
            }
        }
    }
    keys
}

fn apply_ini_overrides_with_existing(
    src: &Path,
    existing: Option<&Path>,
    dest: &Path,
    config: &ToolConfig,
) -> Result<()> {
    let content = build_ini_with_overrides(src, existing, config)?;
    std::fs::write(dest, content).with_context(|| format!("failed to write {}", dest.display()))?;
    Ok(())
}

fn build_ini_with_overrides(
    src: &Path,
    existing: Option<&Path>,
    config: &ToolConfig,
) -> Result<String> {
    let mut content = std::fs::read_to_string(src)
        .with_context(|| format!("failed to read {}", src.display()))?;
    if let Some(existing) = existing
        && let Ok(existing_content) = std::fs::read_to_string(existing)
    {
        let source_keys: BTreeSet<String> = parse_ini_keys(&content).into_iter().collect();
        for (key, value) in parse_optiscaler_ini(&existing_content) {
            if source_keys.contains(&key) {
                content = set_ini_value(&content, &key, &value);
            }
        }
    }
    if let Some(overrides) = config
        .settings
        .get("ini_overrides")
        .and_then(serde_json::Value::as_object)
    {
        for (path, value) in flatten_ini_overrides(overrides) {
            let value = match value {
                serde_json::Value::String(value) => value,
                other => other.to_string(),
            };
            content = set_ini_value(&content, path.as_str(), value.as_str());
        }
    }
    for (path, value) in effective_optiscaler_ini_overrides(config) {
        content = set_ini_value(&content, path, value);
    }
    Ok(content)
}

fn effective_optiscaler_ini_overrides(config: &ToolConfig) -> Vec<(&'static str, &'static str)> {
    let mut overrides = Vec::new();
    overrides.push((
        "FSR.Fsr4Update",
        match fsr4_variant(config) {
            FSR4_VARIANT_INT8_402 => "auto",
            _ => "True",
        },
    ));
    overrides.push((
        "Spoofing.Dxgi",
        if config.get_bool("spoof_dlss") {
            "auto"
        } else {
            "false"
        },
    ));
    overrides.push((
        "Plugins.LoadAsiPlugins",
        if config.get_bool("enable_optipatcher") {
            "true"
        } else {
            "auto"
        },
    ));
    overrides
}

#[must_use]
pub fn managed_manifest_json(game_dir: &Path, applied: &AppliedFiles) -> serde_json::Value {
    let files = applied
        .files
        .iter()
        .map(|rel| {
            let abs = game_dir.join(rel);
            serde_json::json!({
                "path": rel.to_string_lossy().replace('\\', "/"),
                "hash": file_hash_hex(&abs).ok(),
                "size": abs.metadata().ok().map(|metadata| metadata.len()),
            })
        })
        .collect::<Vec<_>>();
    serde_json::json!(files)
}

#[must_use]
pub fn managed_paths_from_config(config: &ToolConfig) -> BTreeSet<String> {
    config
        .settings
        .get("managed_manifest")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|entry| entry.get("path").and_then(serde_json::Value::as_str))
        .map(normalize_rel_path)
        .collect()
}

/// Apply game-specific `OptiScaler` defaults from community compatibility data.
pub fn apply_game_defaults(config: &mut ToolConfig, context: Option<&ToolGameContext>) {
    let Some(context) = context else {
        return;
    };
    let Some(profile) = selected_or_default_profile(&context.game_id, config) else {
        return;
    };
    apply_optiscaler_profile_metadata(config, profile);
}

/// Apply a community-tested `OptiScaler` profile to a config.
pub fn apply_profile_by_id(config: &mut ToolConfig, game_id: &str, profile_id: &str) -> bool {
    if profile_id == CUSTOM_OPTISCALER_PROFILE {
        apply_custom_profile(config);
        return true;
    }
    let Some(profile) = resolve_optiscaler_profiles(game_id)
        .iter()
        .find(|profile| profile.id == profile_id)
    else {
        return false;
    };
    apply_optiscaler_profile_metadata(config, profile);
    true
}

fn selected_or_default_profile(
    game_id: &str,
    config: &ToolConfig,
) -> Option<&'static OptiScalerProfile> {
    if config.get_str("optiscaler_profile") == Some(CUSTOM_OPTISCALER_PROFILE) {
        return None;
    }
    config
        .get_str("optiscaler_profile")
        .and_then(|selected| {
            resolve_optiscaler_profiles(game_id)
                .iter()
                .find(|profile| profile.id == selected)
        })
        .or_else(|| default_optiscaler_profile(game_id))
}

fn apply_custom_profile(config: &mut ToolConfig) {
    config.set(
        "optiscaler_profile",
        serde_json::json!(CUSTOM_OPTISCALER_PROFILE),
    );
    config.set(
        "optiscaler_profile_name",
        serde_json::json!("Custom / no community profile"),
    );
    config.set("optiscaler_profile_source_url", serde_json::json!(""));
    config.set("tested_optiscaler_version", serde_json::json!(""));
    config.set(
        "optiscaler_profile_notes",
        serde_json::json!("Community profile guidance is not applied."),
    );
}

fn apply_optiscaler_profile_metadata(config: &mut ToolConfig, profile: &OptiScalerProfile) {
    config.set("optiscaler_profile", serde_json::json!(profile.id));
    config.set("optiscaler_profile_name", serde_json::json!(profile.name));
    config.set(
        "optiscaler_profile_source_url",
        serde_json::json!(profile.source_url),
    );
    config.set(
        "tested_optiscaler_version",
        serde_json::json!(profile.tested_optiscaler_version),
    );
    config.set("optiscaler_profile_notes", serde_json::json!(profile.notes));
}

fn apply_optiscaler_release_selection(config: &mut ToolConfig, normalized_tag: &str, asset: &str) {
    if let Some(channel) = optiscaler_goverlay_channel_for_tag(normalized_tag) {
        config.set(
            "source_mode",
            serde_json::json!(OPTISCALER_SOURCE_GOVERLAY_BUILDS),
        );
        config.set("goverlay_channel", serde_json::json!(channel));
    } else {
        config.set("source_mode", serde_json::json!(OPTISCALER_SOURCE_OFFICIAL));
    }
    config.set("release_tag", serde_json::json!(normalized_tag));
    config.set("release_asset", serde_json::json!(asset));
}

pub fn scan_optiscaler_install(
    game_id: &str,
    game_dir: &Path,
    managed_paths: &BTreeSet<String>,
) -> Result<OptiScalerInstallState> {
    let executable_dir = crate::resolve_game_plugin(game_id)
        .map(|plugin| plugin.executable_dir(game_dir))
        .unwrap_or_else(|| game_dir.to_path_buf());
    let executable_managed_paths =
        managed_paths_for_executable_dir(game_dir, &executable_dir, managed_paths);
    let mut state = scan_optiscaler_install_in_dir(&executable_dir, &executable_managed_paths)?;
    state.latest_backup = latest_optiscaler_backup(Some(game_id));
    Ok(state)
}

fn managed_paths_for_executable_dir(
    game_dir: &Path,
    executable_dir: &Path,
    managed_paths: &BTreeSet<String>,
) -> BTreeSet<String> {
    let mut out = managed_paths.clone();
    let Ok(executable_rel) = executable_dir.strip_prefix(game_dir) else {
        return out;
    };
    let executable_prefix = normalize_rel_path(executable_rel.to_string_lossy());
    let executable_prefix = executable_prefix.trim_end_matches('/');
    if executable_prefix.is_empty() {
        return out;
    }
    let prefix = format!("{executable_prefix}/");
    for path in managed_paths {
        if let Some(stripped) = path.strip_prefix(&prefix) {
            out.insert(stripped.to_string());
        }
    }
    out
}

pub fn scan_optiscaler_install_in_dir(
    executable_dir: &Path,
    managed_paths: &BTreeSet<String>,
) -> Result<OptiScalerInstallState> {
    let mut recognized_files = Vec::new();
    let mut proxy_dlls = Vec::new();
    let mut companion_files = Vec::new();
    let config_path = executable_dir
        .join("OptiScaler.ini")
        .exists()
        .then(|| executable_dir.join("OptiScaler.ini"));

    for &name in OPTISCALER_PROXY_DLLS {
        let path = executable_dir.join(name);
        if path.is_file() && is_likely_optiscaler_proxy(&path, name) {
            proxy_dlls.push(name.to_string());
            push_detected_file(executable_dir, &path, managed_paths, &mut recognized_files);
        }
    }
    if let Some(path) = &config_path {
        push_detected_file(executable_dir, path, managed_paths, &mut recognized_files);
    }
    for entry in std::fs::read_dir(executable_dir)
        .into_iter()
        .flatten()
        .flatten()
    {
        let path = entry.path();
        let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
            continue;
        };
        let lower = name.to_ascii_lowercase();
        let is_companion = OPTISCALER_COMPANION_FILES
            .iter()
            .any(|known| known.eq_ignore_ascii_case(name))
            || lower.starts_with("libxess")
            || lower.starts_with("amd_fidelityfx");
        if path.is_file() && is_companion {
            companion_files.push(path.clone());
            push_detected_file(executable_dir, &path, managed_paths, &mut recognized_files);
        }
        if path.is_dir()
            && OPTISCALER_COMPANION_DIRS
                .iter()
                .any(|known| known.eq_ignore_ascii_case(name))
        {
            companion_files.push(path.clone());
            collect_detected_dir(executable_dir, &path, managed_paths, &mut recognized_files)?;
        }
    }

    recognized_files.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
    recognized_files.dedup_by(|a, b| a.rel_path == b.rel_path);
    proxy_dlls.sort();
    proxy_dlls.dedup();
    companion_files.sort();
    companion_files.dedup();

    let managed_count = recognized_files.iter().filter(|file| file.managed).count();
    let status = if proxy_dlls.len() > 1 {
        OptiScalerInstallStatus::Conflicted
    } else if recognized_files.is_empty() {
        OptiScalerInstallStatus::Absent
    } else if managed_count == recognized_files.len() {
        OptiScalerInstallStatus::Managed
    } else if managed_count == 0 {
        OptiScalerInstallStatus::Unmanaged
    } else {
        OptiScalerInstallStatus::PartiallyManaged
    };

    let ini_settings = config_path
        .as_ref()
        .and_then(|path| std::fs::read_to_string(path).ok())
        .map(|content| parse_optiscaler_ini(&content))
        .unwrap_or_default();
    let version = identify_optiscaler_version(executable_dir, &recognized_files);
    let wine_dll_overrides = proxy_dlls
        .iter()
        .filter_map(|name| name.strip_suffix(".dll").map(ToOwned::to_owned))
        .collect();
    let latest_backup = latest_optiscaler_backup(None);

    Ok(OptiScalerInstallState {
        status,
        executable_dir: executable_dir.to_path_buf(),
        proxy_dlls,
        wine_dll_overrides,
        config_path,
        ini_settings,
        companion_files,
        recognized_files,
        version,
        latest_backup,
    })
}

pub fn backup_optiscaler_install(
    game_id: Option<&str>,
    state: &OptiScalerInstallState,
) -> Result<Option<PathBuf>> {
    if state.recognized_files.is_empty() {
        return Ok(None);
    }
    let backup_dir = optiscaler_backup_root(game_id).join(timestamp_slug());
    for file in &state.recognized_files {
        let src = state.executable_dir.join(&file.rel_path);
        let dst = backup_dir.join(&file.rel_path);
        if src.is_file() {
            if let Some(parent) = dst.parent() {
                std::fs::create_dir_all(parent)
                    .with_context(|| format!("failed to create {}", parent.display()))?;
            }
            std::fs::copy(&src, &dst)
                .with_context(|| format!("failed to copy {}", dst.display()))?;
        }
    }
    let manifest = serde_json::json!({
        "status": state.status.to_string(),
        "version": state.version.to_string(),
        "executable_dir": state.executable_dir.display().to_string(),
        "files": state.recognized_files.iter().map(|file| {
            serde_json::json!({
                "path": file.rel_path.to_string_lossy().replace('\\', "/"),
                "hash": file.hash,
                "managed": file.managed,
            })
        }).collect::<Vec<_>>(),
    });
    std::fs::create_dir_all(&backup_dir)
        .with_context(|| format!("failed to create {}", backup_dir.display()))?;
    let manifest_path = backup_dir.join("modde-optiscaler-backup.json");
    std::fs::write(&manifest_path, serde_json::to_string_pretty(&manifest)?)
        .with_context(|| format!("failed to write {}", manifest_path.display()))?;
    Ok(Some(backup_dir))
}

pub fn latest_optiscaler_backup(game_id: Option<&str>) -> Option<PathBuf> {
    let root = optiscaler_backup_root(game_id);
    let mut entries = std::fs::read_dir(root)
        .ok()?
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            entry.file_type().ok()?.is_dir().then_some(path)
        })
        .collect::<Vec<_>>();
    entries.sort();
    entries.pop()
}

pub fn restore_latest_optiscaler_backup(game_id: &str, game_dir: &Path) -> Result<PathBuf> {
    let backup = latest_optiscaler_backup(Some(game_id))
        .ok_or_else(|| anyhow::anyhow!("no OptiScaler backup found for {game_id}"))?;
    let executable_dir = crate::resolve_game_plugin(game_id)
        .map(|plugin| plugin.executable_dir(game_dir))
        .unwrap_or_else(|| game_dir.to_path_buf());
    restore_dir_contents(&backup, &executable_dir)?;
    Ok(backup)
}

fn optiscaler_backup_root(game_id: Option<&str>) -> PathBuf {
    modde_core::paths::modde_data_dir()
        .join("tool-backups")
        .join("optiscaler")
        .join(game_id.unwrap_or("_unknown"))
}

fn timestamp_slug() -> String {
    let secs = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |duration| duration.as_secs());
    format!("{secs}-{}", std::process::id())
}

fn is_likely_optiscaler_proxy(path: &Path, name: &str) -> bool {
    if name.eq_ignore_ascii_case("OptiScaler.asi") {
        return true;
    }
    let Ok(hash) = file_hash_hex(path) else {
        return true;
    };
    cached_release_dirs().into_iter().any(|dir| {
        file_hash_hex(&dir.join("OptiScaler.dll")).ok().as_deref() == Some(hash.as_str())
    }) || path.metadata().is_ok_and(|metadata| metadata.len() > 0)
}

fn identify_optiscaler_version(
    executable_dir: &Path,
    recognized_files: &[OptiScalerDetectedFile],
) -> OptiScalerVersionIdentity {
    let proxy_hashes = recognized_files
        .iter()
        .filter(|file| {
            file.rel_path
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|file_name| {
                    OPTISCALER_PROXY_DLLS
                        .iter()
                        .any(|name| file_name.eq_ignore_ascii_case(name))
                })
        })
        .filter_map(|file| file.hash.as_deref())
        .collect::<Vec<_>>();
    for dir in cached_release_dirs() {
        let Ok(hash) = file_hash_hex(&dir.join("OptiScaler.dll")) else {
            continue;
        };
        if proxy_hashes.iter().any(|candidate| *candidate == hash)
            && let Some(tag) = dir.file_name().and_then(|name| name.to_str())
        {
            return OptiScalerVersionIdentity::CachedRelease(tag.to_string());
        }
    }
    let version_file = executable_dir.join("version.txt");
    if let Ok(version) = std::fs::read_to_string(version_file) {
        let trimmed = version.trim();
        if !trimmed.is_empty() {
            return OptiScalerVersionIdentity::FileMetadata(trimmed.to_string());
        }
    }
    if let Some(hash) = proxy_hashes.first() {
        return OptiScalerVersionIdentity::ContentHash((*hash).to_string());
    }
    OptiScalerVersionIdentity::Unknown
}

fn cached_release_dirs() -> Vec<PathBuf> {
    let root = modde_core::paths::modde_data_dir()
        .join("tools")
        .join("optiscaler");
    std::fs::read_dir(root)
        .into_iter()
        .flatten()
        .flatten()
        .filter_map(|entry| entry.file_type().ok()?.is_dir().then_some(entry.path()))
        .collect()
}

fn push_detected_file(
    root: &Path,
    path: &Path,
    managed_paths: &BTreeSet<String>,
    out: &mut Vec<OptiScalerDetectedFile>,
) {
    let rel = path.strip_prefix(root).unwrap_or(path).to_path_buf();
    let normalized = normalize_rel_path(rel.to_string_lossy());
    out.push(OptiScalerDetectedFile {
        rel_path: rel,
        hash: file_hash_hex(path).ok(),
        managed: managed_paths.contains(&normalized),
    });
}

fn collect_detected_dir(
    root: &Path,
    dir: &Path,
    managed_paths: &BTreeSet<String>,
    out: &mut Vec<OptiScalerDetectedFile>,
) -> Result<()> {
    for entry in std::fs::read_dir(dir)
        .with_context(|| format!("failed to read directory: {}", dir.display()))?
    {
        let entry = entry?;
        let path = entry.path();
        let metadata = path.symlink_metadata()?;
        if metadata.file_type().is_symlink() {
            continue;
        }
        if metadata.is_dir() {
            collect_detected_dir(root, &path, managed_paths, out)?;
        } else if metadata.is_file() {
            push_detected_file(root, &path, managed_paths, out);
        }
    }
    Ok(())
}

fn collect_relative_files(game_dir: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    for entry in std::fs::read_dir(dir)
        .with_context(|| format!("failed to read directory: {}", dir.display()))?
    {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            collect_relative_files(game_dir, &path, out)?;
        } else if path.is_file() {
            out.push(relative_to_game(game_dir, &path)?);
        }
    }
    Ok(())
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
    std::fs::create_dir_all(dst).with_context(|| format!("failed to create {}", dst.display()))?;
    for entry in std::fs::read_dir(src)
        .with_context(|| format!("failed to read directory: {}", src.display()))?
    {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if src_path.is_dir() {
            copy_dir_recursive(&src_path, &dst_path)?;
        } else if src_path.is_file() {
            std::fs::copy(&src_path, &dst_path)
                .with_context(|| format!("failed to copy {}", dst_path.display()))?;
        }
    }
    Ok(())
}

fn restore_dir_contents(src: &Path, dst: &Path) -> Result<()> {
    for entry in std::fs::read_dir(src)
        .with_context(|| format!("failed to read directory: {}", src.display()))?
    {
        let entry = entry?;
        if entry.file_name() == "modde-optiscaler-backup.json" {
            continue;
        }
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if src_path.is_dir() {
            restore_dir_contents(&src_path, &dst_path)?;
        } else if src_path.is_file() {
            if let Some(parent) = dst_path.parent() {
                std::fs::create_dir_all(parent)
                    .with_context(|| format!("failed to create {}", parent.display()))?;
            }
            std::fs::copy(&src_path, &dst_path)
                .with_context(|| format!("failed to copy {}", dst_path.display()))?;
        }
    }
    Ok(())
}

fn normalize_rel_path(path: impl AsRef<str>) -> String {
    path.as_ref().replace('\\', "/").to_ascii_lowercase()
}

fn file_hash_hex(path: &Path) -> Result<String> {
    let mut file =
        std::fs::File::open(path).with_context(|| format!("failed to open {}", path.display()))?;
    let mut hasher = Xxh64::new(0);
    let mut buf = [0_u8; 8192];
    loop {
        let read = file.read(&mut buf)?;
        if read == 0 {
            break;
        }
        hasher.update(&buf[..read]);
    }
    Ok(format!("{:016x}", hasher.digest()))
}

fn optiscaler_config_reset_reason(
    existing: &OptiScalerInstallState,
    new_ini: &Path,
    config: &ToolConfig,
) -> Option<String> {
    if config.get_bool("force_config_reset") {
        return Some("forced by setting".to_string());
    }
    let Ok(new_content) = std::fs::read_to_string(new_ini) else {
        return None;
    };
    let new_keys: BTreeSet<String> = parse_ini_keys(&new_content).into_iter().collect();
    let old_unknown = existing
        .ini_settings
        .keys()
        .any(|key| !new_keys.contains(key));
    old_unknown.then_some("schema mismatch".to_string())
}

fn flatten_ini_overrides(
    overrides: &serde_json::Map<String, serde_json::Value>,
) -> Vec<(String, serde_json::Value)> {
    fn walk(prefix: &str, value: &serde_json::Value, out: &mut Vec<(String, serde_json::Value)>) {
        if let serde_json::Value::Object(map) = value {
            for (key, child) in map {
                let path = if prefix.is_empty() {
                    key.clone()
                } else {
                    format!("{prefix}.{key}")
                };
                walk(&path, child, out);
            }
        } else {
            out.push((prefix.to_string(), value.clone()));
        }
    }

    let mut out = Vec::new();
    for (key, value) in overrides {
        walk(key, value, &mut out);
    }
    out
}

fn set_ini_value(content: &str, path: &str, value: &str) -> String {
    let Some((target_section, target_key)) = path.rsplit_once('.') else {
        tracing::warn!(
            ini_key = path,
            "optiscaler: ini override key has no section prefix; expected 'section.key'"
        );
        return content.to_string();
    };
    let mut current_section = "";
    let mut updated = false;
    let mut lines = Vec::new();

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('[') && trimmed.ends_with(']') {
            if !updated && !target_section.is_empty() && current_section == target_section {
                lines.push(format!("{target_key}={value}"));
                updated = true;
            }
            current_section = trimmed[1..trimmed.len() - 1].trim();
        }

        if current_section == target_section
            && let Some((key, _)) = trimmed.split_once('=')
            && key.trim() == target_key
        {
            lines.push(format!("{target_key}={value}"));
            updated = true;
            continue;
        }
        lines.push(line.to_string());
    }

    if !updated && !target_section.is_empty() && current_section == target_section {
        lines.push(format!("{target_key}={value}"));
        updated = true;
    }

    if !updated {
        if !target_section.is_empty() {
            lines.push(format!("[{target_section}]"));
        }
        lines.push(format!("{target_key}={value}"));
    }

    lines.join("\n") + "\n"
}

/// Build fgmod DLL restore commands for the launch wrapper.
///
/// Scans the staging mods directory for DLLs that fgmod will delete at launch,
/// and returns `(source, destination)` pairs for the wrapper to restore them.
#[must_use]
pub fn fgmod_restore_commands(game_dir: &Path, staging_dir: &Path) -> Vec<(String, String)> {
    let exe_dir = crate::resolve_game_plugin("cyberpunk2077")
        .map(|plugin| plugin.executable_dir(game_dir))
        .unwrap_or_else(|| game_dir.join("bin/x64"));
    fgmod_restore_commands_for_executable_dir(game_dir, staging_dir, &exe_dir)
}

/// Build fgmod DLL restore commands for a known executable directory.
#[must_use]
pub fn fgmod_restore_commands_for_executable_dir(
    _game_dir: &Path,
    staging_dir: &Path,
    exe_dir: &Path,
) -> Vec<(String, String)> {
    let mut restore = Vec::new();

    let mods_dir = staging_dir.join("mods");
    if !mods_dir.exists() {
        return restore;
    }

    for entry in std::fs::read_dir(&mods_dir).into_iter().flatten().flatten() {
        if !entry.file_type().is_ok_and(|t| t.is_dir()) {
            continue;
        }

        let mod_bin_x64 = entry.path().join("bin/x64");
        if !mod_bin_x64.exists() {
            continue;
        }

        for dll_entry in std::fs::read_dir(&mod_bin_x64)
            .into_iter()
            .flatten()
            .flatten()
        {
            let dll_name = dll_entry.file_name().to_string_lossy().to_lowercase();
            if FGMOD_DELETED_DLLS
                .iter()
                .any(|d| d.to_lowercase() == dll_name)
            {
                let src = dll_entry.path();
                let dest = exe_dir.join(&*dll_name);
                restore.push((
                    src.to_string_lossy().to_string(),
                    dest.to_string_lossy().to_string(),
                ));
            }
        }
    }

    restore
}

/// Shim for `dirs::data_dir()` / `dirs::home_dir()` — we use a minimal
/// vendored version to avoid adding the full `dirs` crate.
mod dirs {
    use std::path::PathBuf;

    pub fn data_dir() -> Option<PathBuf> {
        std::env::var_os("XDG_DATA_HOME")
            .map(PathBuf::from)
            .or_else(|| home_dir().map(|h| h.join(".local/share")))
    }

    pub fn home_dir() -> Option<PathBuf> {
        std::env::var_os("HOME").map(PathBuf::from)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, BTreeSet};
    use std::path::PathBuf;

    use crate::tools::ToolSettingKind;

    use super::*;

    #[test]
    fn parse_optiscaler_ini_preserves_section_paths() {
        let parsed = parse_optiscaler_ini(
            r"
            ; comment
            [OptiScaler]
            Dxgi=auto
            LoadAsiPlugins=true
            [Menu]
            Scale=1.25
            ",
        );
        assert_eq!(parsed.get("OptiScaler.Dxgi"), Some(&"auto".to_string()));
        assert_eq!(
            parsed.get("OptiScaler.LoadAsiPlugins"),
            Some(&"true".to_string())
        );
        assert_eq!(parsed.get("Menu.Scale"), Some(&"1.25".to_string()));
    }

    #[test]
    fn preview_reports_changed_when_proxy_or_ini_differ() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"new dll").expect("source dll");
        std::fs::write(
            source.path().join("OptiScaler.ini"),
            "[FSR]\nFGIndex=auto\n",
        )
        .expect("source ini");
        std::fs::write(game.path().join("dxgi.dll"), b"old dll").expect("dest dll");
        std::fs::write(game.path().join("OptiScaler.ini"), "[FSR]\nFGIndex=1\n").expect("dest ini");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set(
            "ini_overrides",
            serde_json::json!({ "FSR": { "FGIndex": "2" } }),
        );

        let preview = OptiScaler
            .preview_apply_for(game.path(), None, &config)
            .expect("preview");

        assert!(preview.changed_files.contains(&PathBuf::from("dxgi.dll")));
        assert!(
            preview
                .changed_files
                .contains(&PathBuf::from("OptiScaler.ini"))
        );
        assert!(preview.missing_inputs.is_empty());
    }

    #[test]
    fn preview_reports_unchanged_and_does_not_create_target_dirs() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"same dll").expect("source dll");
        std::fs::write(
            source.path().join("OptiScaler.ini"),
            "[FSR]\nFGIndex=auto\n",
        )
        .expect("source ini");
        let target = game.path().join("Bin");
        std::fs::create_dir(&target).expect("target");
        std::fs::write(target.join("dxgi.dll"), b"same dll").expect("dest dll");
        std::fs::write(
            target.join("OptiScaler.ini"),
            "[FSR]\nFGIndex=auto\nFsr4Update=True\n[Spoofing]\nDxgi=false\n[Plugins]\nLoadAsiPlugins=auto\n",
        )
        .expect("dest ini");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("exe_subdir", serde_json::json!("Bin"));

        let preview = OptiScaler
            .preview_apply_for(game.path(), None, &config)
            .expect("preview");

        assert!(preview.changed_files.is_empty());
        assert!(
            preview
                .unchanged_files
                .contains(&PathBuf::from("Bin/dxgi.dll"))
        );
        assert!(
            preview
                .unchanged_files
                .contains(&PathBuf::from("Bin/OptiScaler.ini"))
        );
        assert!(!game.path().join("Other").exists());
    }

    #[test]
    fn preview_does_not_create_missing_target_directory() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"dll").expect("source dll");
        std::fs::write(source.path().join("OptiScaler.ini"), "[FSR]\n").expect("source ini");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("exe_subdir", serde_json::json!("MissingBin"));

        let preview = OptiScaler
            .preview_apply_for(game.path(), None, &config)
            .expect("preview");

        assert!(preview.has_changes());
        assert!(!game.path().join("MissingBin").exists());
    }

    #[test]
    fn goverlay_inspired_settings_expose_friendly_raw_value_selects() {
        let specs = OptiScaler.settings_schema();
        let shortcut = specs
            .iter()
            .find(|spec| spec.key == "ini_overrides.Menu.ShortcutKey")
            .expect("shortcut spec");
        assert_eq!(shortcut.section, "Menu");
        assert!(!shortcut.advanced);
        let ToolSettingKind::Select { options } = &shortcut.kind else {
            panic!("shortcut should be a select");
        };
        assert!(
            options
                .iter()
                .any(|option| option.value == "INSERT" && option.label == "Insert")
        );

        let upscaler = specs
            .iter()
            .find(|spec| spec.key == "ini_overrides.FSR.UpscalerIndex")
            .expect("FSR upscaler spec");
        assert_eq!(upscaler.section, "Basic");
        let ToolSettingKind::Select { options } = &upscaler.kind else {
            panic!("FSR upscaler should be a select");
        };
        assert_eq!(options[0].value, "auto");
        assert_eq!(options[0].label, "Auto");
        assert!(
            options
                .iter()
                .any(|option| option.value == "0" && option.label == "0 - FSR 4.0.2")
        );
        assert!(
            options
                .iter()
                .any(|option| option.value == "2" && option.label == "2 - FSR 2.3.4")
        );

        let fg = specs
            .iter()
            .find(|spec| spec.key == "ini_overrides.FSR.FGIndex")
            .expect("FSR FG spec");
        let ToolSettingKind::Select { options } = &fg.kind else {
            panic!("FSR FG should be a select");
        };
        assert!(
            options
                .iter()
                .any(|option| option.value == "1" && option.label == "1 - FSR 3.1.6")
        );
    }

    #[test]
    fn goverlay_inspired_settings_mark_only_risky_entries_advanced() {
        let specs = OptiScaler.settings_schema();
        let spoof = specs
            .iter()
            .find(|spec| spec.key == "spoof_dlss")
            .expect("spoof fallback spec");
        assert_eq!(spoof.label, "Spoof DLSS fallback");
        assert_eq!(spoof.section, "Basic");
        assert!(!spoof.advanced);

        let optipatcher = specs
            .iter()
            .find(|spec| spec.key == "enable_optipatcher")
            .expect("OptiPatcher spec");
        assert_eq!(optipatcher.section, "Basic");
        assert!(!optipatcher.advanced);

        assert!(
            !specs
                .iter()
                .any(|spec| spec.key == "ini_overrides.Plugins.LoadAsiPlugins")
        );
    }

    #[test]
    fn emulate_fp8_schema_is_read_only_for_int8_variant() {
        let mut config = OptiScaler.default_config();
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));

        let specs = OptiScaler.settings_schema_for(None, &config);
        let emulate = specs
            .iter()
            .find(|spec| spec.key == "emulate_fp8")
            .expect("emulate fp8 spec");

        assert_eq!(emulate.section, "Basic");
        assert!(matches!(emulate.kind, ToolSettingKind::ReadOnly));
    }

    #[test]
    fn goverlay_channel_is_only_exposed_for_goverlay_build_source() {
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("github_release"));
        let specs = OptiScaler.settings_schema_for(None, &config);
        assert!(!specs.iter().any(|spec| spec.key == "goverlay_channel"));

        config.set("source_mode", serde_json::json!("goverlay_builds"));
        let specs = OptiScaler.settings_schema_for(None, &config);
        assert!(specs.iter().any(|spec| spec.key == "goverlay_channel"));
    }

    #[test]
    fn fsr_backend_overrides_write_raw_ini_values() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src = tmp.path().join("OptiScaler.ini");
        let dest = tmp.path().join("applied.ini");
        std::fs::write(
            &src,
            "[FSR]\nUpscalerIndex=auto\nFGIndex=auto\n[Menu]\nShortcutKey=auto\n",
        )
        .expect("source ini");
        let mut config = OptiScaler.default_config();
        config.set(
            "ini_overrides",
            serde_json::json!({
                "FSR": {
                    "UpscalerIndex": "0",
                    "FGIndex": "1"
                },
                "Menu": {
                    "ShortcutKey": "INSERT"
                }
            }),
        );

        apply_ini_overrides_with_existing(&src, None, &dest, &config).expect("apply overrides");

        let parsed = parse_optiscaler_ini(&std::fs::read_to_string(dest).expect("dest ini"));
        assert_eq!(parsed.get("FSR.UpscalerIndex"), Some(&"0".to_string()));
        assert_eq!(parsed.get("FSR.FGIndex"), Some(&"1".to_string()));
        assert_eq!(parsed.get("Menu.ShortcutKey"), Some(&"INSERT".to_string()));
    }

    #[test]
    fn optiscaler_high_level_controls_write_ini_values() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let src = tmp.path().join("OptiScaler.ini");
        let dest = tmp.path().join("applied.ini");
        std::fs::write(
            &src,
            "[FSR]\nFsr4Update=auto\n[Spoofing]\nDxgi=auto\n[Plugins]\nLoadAsiPlugins=auto\n",
        )
        .expect("source ini");
        let mut config = OptiScaler.default_config();
        config.set("enable_optipatcher", serde_json::json!(true));
        config.set("spoof_dlss", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_LATEST_FP8));

        apply_ini_overrides_with_existing(&src, None, &dest, &config).expect("apply overrides");

        let parsed = parse_optiscaler_ini(&std::fs::read_to_string(dest).expect("dest ini"));
        assert_eq!(parsed.get("FSR.Fsr4Update"), Some(&"True".to_string()));
        assert_eq!(parsed.get("Spoofing.Dxgi"), Some(&"false".to_string()));
        assert_eq!(
            parsed.get("Plugins.LoadAsiPlugins"),
            Some(&"true".to_string())
        );
    }

    #[test]
    fn optiscaler_fp8_variant_copies_selected_fsr4_dll() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"dll").expect("source dll");
        std::fs::write(source.path().join("OptiScaler.ini"), "[FSR]\n").expect("source ini");
        std::fs::create_dir_all(source.path().join(FSR4_LATEST_DIR)).expect("latest dir");
        std::fs::write(
            source.path().join(FSR4_LATEST_DIR).join(FSR4_DLL_NAME),
            b"fp8",
        )
        .expect("fp8 dll");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_LATEST_FP8));

        let applied = OptiScaler.apply(game.path(), &config).expect("apply");

        assert_eq!(
            std::fs::read(game.path().join(FSR4_DLL_NAME)).expect("deployed FSR4"),
            b"fp8"
        );
        assert!(applied.files.contains(&PathBuf::from(FSR4_DLL_NAME)));
    }

    #[test]
    fn optiscaler_int8_variant_copies_int8_and_ignores_fp8_env() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"dll").expect("source dll");
        std::fs::write(source.path().join("OptiScaler.ini"), "[FSR]\n").expect("source ini");
        std::fs::create_dir_all(source.path().join(FSR4_INT8_DIR)).expect("int8 dir");
        std::fs::write(
            source.path().join(FSR4_INT8_DIR).join(FSR4_DLL_NAME),
            b"int8",
        )
        .expect("int8 dll");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));
        config.set("emulate_fp8", serde_json::json!(true));

        OptiScaler.apply(game.path(), &config).expect("apply");

        assert_eq!(
            std::fs::read(game.path().join(FSR4_DLL_NAME)).expect("deployed FSR4"),
            b"int8"
        );
        assert!(OptiScaler.env_vars(&config).is_empty());
    }

    #[test]
    fn optiscaler_missing_optipatcher_is_reported_in_preview() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"dll").expect("source dll");
        std::fs::write(source.path().join("OptiScaler.ini"), "[Plugins]\n").expect("source ini");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("enable_optipatcher", serde_json::json!(true));

        let preview = OptiScaler
            .preview_apply_for(game.path(), None, &config)
            .expect("preview");

        let optipatcher_rel = PathBuf::from("plugins").join(OPTIPATCHER_ASSET);
        assert!(
            preview
                .missing_inputs
                .iter()
                .any(|input| input.contains("OptiPatcher.asi"))
                || preview.changed_files.contains(&optipatcher_rel)
                || preview.unchanged_files.contains(&optipatcher_rel)
        );
    }

    #[test]
    fn optiscaler_uses_bundled_optipatcher_from_source_plugins() {
        let source = tempfile::tempdir().expect("source");
        let game = tempfile::tempdir().expect("game");
        std::fs::write(source.path().join("OptiScaler.dll"), b"dll").expect("source dll");
        std::fs::write(source.path().join("OptiScaler.ini"), "[Plugins]\n").expect("source ini");
        std::fs::create_dir_all(source.path().join("plugins")).expect("plugins dir");
        std::fs::write(
            source.path().join("plugins").join(OPTIPATCHER_ASSET),
            b"bundled asi",
        )
        .expect("source optipatcher");
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set(
            "local_source_dir",
            serde_json::json!(source.path().display().to_string()),
        );
        config.set("enable_optipatcher", serde_json::json!(true));

        OptiScaler.apply(game.path(), &config).expect("apply");

        assert_eq!(
            std::fs::read(game.path().join("plugins").join(OPTIPATCHER_ASSET))
                .expect("deployed optipatcher"),
            b"bundled asi"
        );
    }

    #[test]
    fn optiscaler_archive_payload_keeps_optipatcher_in_plugins() {
        assert!(is_optiscaler_payload_file("optipatcher.asi"));
        assert_eq!(
            optiscaler_payload_dest(
                Path::new("/cache"),
                Path::new("plugins/OptiPatcher.asi"),
                std::ffi::OsStr::new("OptiPatcher.asi"),
            ),
            PathBuf::from("/cache/plugins/OptiPatcher.asi")
        );
    }

    #[test]
    fn optiscaler_fp8_env_only_for_latest_fp8_emulation() {
        let mut config = OptiScaler.default_config();
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_LATEST_FP8));
        config.set("emulate_fp8", serde_json::json!(true));

        assert_eq!(
            OptiScaler.env_vars(&config).as_slice(),
            [(
                FP8_EMULATION_ENV_KEY.to_string(),
                FP8_EMULATION_ENV_VALUE.to_string()
            )]
        );

        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));
        assert!(OptiScaler.env_vars(&config).is_empty());
    }

    #[test]
    fn goverlay_release_classification_maps_supported_channels() {
        assert_eq!(
            goverlay_release_channel("edge-0.9.12.0323"),
            Some("goverlay-edge")
        );
        assert_eq!(
            goverlay_release_channel("master-3ce61922"),
            Some("goverlay-master")
        );
        assert_eq!(
            goverlay_release_channel("any-release-0.9-2083b274"),
            Some("goverlay-any")
        );
        assert_eq!(goverlay_release_channel("0.9.1-0"), Some("goverlay-stable"));
        assert_eq!(goverlay_release_channel("fsr-int8"), None);
    }

    #[test]
    fn goverlay_release_summary_requires_full_install_archive() {
        let edge = release_fixture("edge-0.9.12.0323", &["notes.json", "optiscaler-edge.7z"]);
        let edge = goverlay_release_summary(edge).expect("edge release");
        assert_eq!(edge.tag, "goverlay-edge:edge-0.9.12.0323");
        assert_eq!(edge.assets.len(), 1);
        assert_eq!(edge.assets[0].name, "optiscaler-edge.7z");

        let master = release_fixture(
            "master-3ce61922",
            &["master-3ce61922.json", "OptiScaler_master_3ce61922.7z"],
        );
        let master = goverlay_release_summary(master).expect("master release");
        assert_eq!(master.tag, "goverlay-master:master-3ce61922");
        assert_eq!(master.assets[0].name, "OptiScaler_master_3ce61922.7z");

        let fsr_int8 = release_fixture("fsr-int8", &["amd_fidelityfx_upscaler_dx12.dll"]);
        assert!(goverlay_release_summary(fsr_int8).is_none());
    }

    #[test]
    fn optiscaler_release_tags_are_encoded_by_source() {
        assert_eq!(
            encode_optiscaler_release_tag("official", "v0.9.1"),
            "official:v0.9.1"
        );
        assert_eq!(
            normalize_optiscaler_release_tag("v0.9.1"),
            "official:v0.9.1"
        );
        assert_eq!(
            normalize_optiscaler_release_tag("goverlay-edge:edge-0.9.12.0323"),
            "goverlay-edge:edge-0.9.12.0323"
        );
    }

    #[test]
    fn optiscaler_release_asset_selection_uses_encoded_source_keys() {
        let releases = vec![
            release_fixture("official:v0.9.1", &["Optiscaler_0.9.1-final.7z"]),
            release_fixture("goverlay-edge:edge-0.9.12.0323", &["optiscaler-edge.7z"]),
        ];

        let (tag, asset) =
            select_optiscaler_release_asset(&releases, "v0.9.1", "Optiscaler_0.9.1-final.7z")
                .expect("legacy official tag resolves");
        assert_eq!(tag, "official:v0.9.1");
        assert_eq!(asset.name, "Optiscaler_0.9.1-final.7z");

        let (tag, asset) = select_optiscaler_release_asset(
            &releases,
            "goverlay-edge:edge-0.9.12.0323",
            "optiscaler-edge.7z",
        )
        .expect("goverlay edge tag resolves");
        assert_eq!(tag, "goverlay-edge:edge-0.9.12.0323");
        assert_eq!(asset.name, "optiscaler-edge.7z");

        assert!(
            select_optiscaler_release_asset(&releases, "edge-0.9.12.0323", "optiscaler-edge.7z")
                .is_err()
        );
    }

    #[test]
    fn optiscaler_release_config_moves_legacy_goverlay_tag_to_goverlay_source() {
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("github_release"));
        config.set(
            "release_tag",
            serde_json::json!("goverlay-edge:edge-0.9.12.0323"),
        );

        assert!(normalize_optiscaler_release_config(&mut config));

        assert_eq!(config.get_str("source_mode"), Some("goverlay_builds"));
        assert_eq!(config.get_str("goverlay_channel"), Some("edge"));
        assert_eq!(
            config.get_str("release_tag"),
            Some("goverlay-edge:edge-0.9.12.0323")
        );
    }

    #[test]
    fn optiscaler_release_matching_respects_source_and_channel() {
        let official = release_fixture("official:v0.9.1", &["Optiscaler.7z"]);
        let edge = release_fixture("goverlay-edge:edge-0.9.12.0323", &["optiscaler-edge.7z"]);
        let master = release_fixture(
            "goverlay-master:master-3ce61922",
            &["OptiScaler_master_3ce61922.7z"],
        );
        let mut config = OptiScaler.default_config();

        config.set("source_mode", serde_json::json!("github_release"));
        assert!(optiscaler_release_matches_config(&official, &config));
        assert!(!optiscaler_release_matches_config(&edge, &config));

        config.set("source_mode", serde_json::json!("goverlay_builds"));
        config.set("goverlay_channel", serde_json::json!("edge"));
        assert!(optiscaler_release_matches_config(&edge, &config));
        assert!(!optiscaler_release_matches_config(&master, &config));

        config.set("goverlay_channel", serde_json::json!("master"));
        assert!(!optiscaler_release_matches_config(&edge, &config));
        assert!(optiscaler_release_matches_config(&master, &config));
    }

    #[test]
    fn goverlay_release_sorts_newest_first_by_publish_date() {
        let mut releases = [
            release_fixture_with_date(
                "goverlay-edge:edge-old",
                &["optiscaler-edge.7z"],
                "2026-03-20T01:08:18Z",
            ),
            release_fixture_with_date(
                "goverlay-edge:edge-new",
                &["optiscaler-edge.7z"],
                "2026-03-24T00:18:25Z",
            ),
        ];

        releases.sort_by(|left, right| right.published_at.cmp(&left.published_at));

        assert_eq!(releases[0].tag, "goverlay-edge:edge-new");
        assert_eq!(releases[1].tag, "goverlay-edge:edge-old");
    }

    #[test]
    fn scanner_reports_absent_install() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let state = scan_optiscaler_install_in_dir(tmp.path(), &BTreeSet::new()).expect("scan");
        assert_eq!(state.status, OptiScalerInstallStatus::Absent);
        assert!(state.recognized_files.is_empty());
    }

    #[test]
    fn scanner_reports_unmanaged_install_with_config_and_companions() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(tmp.path().join("dxgi.dll"), b"optiscaler").expect("proxy");
        std::fs::write(
            tmp.path().join("OptiScaler.ini"),
            "[OptiScaler]\nDxgi=false\n",
        )
        .expect("ini");
        std::fs::write(tmp.path().join("fakenvapi.dll"), b"fake").expect("companion");

        let state = scan_optiscaler_install_in_dir(tmp.path(), &BTreeSet::new()).expect("scan");
        assert_eq!(state.status, OptiScalerInstallStatus::Unmanaged);
        assert_eq!(state.proxy_dlls, vec!["dxgi.dll".to_string()]);
        assert_eq!(state.wine_dll_overrides, vec!["dxgi".to_string()]);
        assert_eq!(
            state.ini_settings.get("OptiScaler.Dxgi"),
            Some(&"false".to_string())
        );
        assert_eq!(state.recognized_files.len(), 3);
    }

    #[test]
    fn scanner_distinguishes_managed_and_conflicted_installs() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(tmp.path().join("dxgi.dll"), b"optiscaler").expect("proxy");
        let mut managed = BTreeSet::new();
        managed.insert("dxgi.dll".to_string());
        let state = scan_optiscaler_install_in_dir(tmp.path(), &managed).expect("scan");
        assert_eq!(state.status, OptiScalerInstallStatus::Managed);

        std::fs::write(tmp.path().join("winmm.dll"), b"optiscaler").expect("proxy");
        let state = scan_optiscaler_install_in_dir(tmp.path(), &managed).expect("scan");
        assert_eq!(state.status, OptiScalerInstallStatus::Conflicted);
    }

    #[test]
    fn scanner_matches_stellar_blade_root_relative_managed_manifest() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let exe_dir = tmp.path().join("SB/Binaries/Win64");
        std::fs::create_dir_all(&exe_dir).expect("exe dir");
        std::fs::write(exe_dir.join("dxgi.dll"), b"optiscaler").expect("proxy");
        std::fs::write(exe_dir.join("OptiScaler.ini"), "[OptiScaler]\nDxgi=auto\n").expect("ini");
        std::fs::write(exe_dir.join("version.txt"), "v0.9.1\n").expect("version");

        let unmanaged =
            scan_optiscaler_install("stellar-blade", tmp.path(), &BTreeSet::new()).expect("scan");
        assert_eq!(unmanaged.status, OptiScalerInstallStatus::Unmanaged);
        assert_eq!(
            unmanaged.summary(),
            "unmanaged; version v0.9.1; proxy dxgi.dll"
        );
        assert_eq!(unmanaged.wine_dll_overrides, vec!["dxgi".to_string()]);

        let mut managed = BTreeSet::new();
        managed.insert("sb/binaries/win64/dxgi.dll".to_string());
        managed.insert("sb/binaries/win64/optiscaler.ini".to_string());
        let managed_state =
            scan_optiscaler_install("stellar-blade", tmp.path(), &managed).expect("scan");
        assert_eq!(managed_state.status, OptiScalerInstallStatus::Managed);
        assert_eq!(
            managed_state.summary(),
            "managed; version v0.9.1; proxy dxgi.dll"
        );
    }

    #[test]
    fn stellar_blade_default_config_adds_community_metadata_only() {
        let context = ToolGameContext::from_parts(
            "stellar-blade",
            "Stellar Blade",
            Some(PathBuf::from("/fake/StellarBlade")),
            None,
        );
        let config = OptiScaler.default_config_for(Some(&context));

        assert_eq!(
            config.get_str("source_mode"),
            Some(OPTISCALER_SOURCE_GOVERLAY_FGMOD)
        );
        assert_eq!(config.get_str("goverlay_channel"), Some("edge"));
        assert_eq!(config.get_str("release_tag"), Some("latest"));
        assert_eq!(config.get_str("release_asset"), Some(""));
        assert_eq!(config.get_str("proxy_dll"), Some("dxgi.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some(""));
        assert!(config.get_bool("copy_companion_files"));
        assert!(!config.get_bool("enable_optipatcher"));
        assert_eq!(
            config.get_str("fsr4_variant"),
            Some(FSR4_VARIANT_LATEST_FP8)
        );
        assert!(!config.get_bool("emulate_fp8"));
        assert!(!config.get_bool("spoof_dlss"));
        assert_eq!(config.get_str("optiscaler_profile"), Some("community-dxgi"));
        assert_eq!(config.get_str("tested_optiscaler_version"), Some("0.9"));
        assert_eq!(
            config.get_str("optiscaler_profile_source_url"),
            Some("https://github.com/optiscaler/OptiScaler/wiki/Stellar-Blade")
        );
        assert_eq!(
            config.settings.get("ini_overrides"),
            Some(&serde_json::json!({}))
        );
    }

    #[test]
    fn games_without_optiscaler_profiles_keep_generic_defaults() {
        let context =
            ToolGameContext::from_parts("skyrim-se", "Skyrim Special Edition", None, None);
        let config = OptiScaler.default_config_for(Some(&context));

        assert_eq!(config.get_str("source_mode"), Some("goverlay_fgmod"));
        assert_eq!(config.get_str("release_tag"), Some("latest"));
        assert_eq!(config.get_str("proxy_dll"), Some("dxgi.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some(""));
        assert_eq!(config.get_str("optiscaler_profile"), None);
    }

    #[test]
    fn custom_profile_opt_out_prevents_community_defaults() {
        let context = ToolGameContext::from_parts(
            "stellar-blade",
            "Stellar Blade",
            Some(PathBuf::from("/fake/StellarBlade")),
            None,
        );
        let mut config = OptiScaler.default_config();
        config.set("optiscaler_profile", serde_json::json!("custom"));
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set("release_tag", serde_json::json!("latest"));
        config.set("proxy_dll", serde_json::json!("winmm.dll"));

        apply_game_defaults(&mut config, Some(&context));

        assert_eq!(config.get_str("optiscaler_profile"), Some("custom"));
        assert_eq!(config.get_str("source_mode"), Some("local_dir"));
        assert_eq!(config.get_str("release_tag"), Some("latest"));
        assert_eq!(config.get_str("proxy_dll"), Some("winmm.dll"));
        assert_eq!(config.get_str("tested_optiscaler_version"), None);
    }

    #[test]
    fn stellar_blade_game_defaults_preserve_custom_settings_for_profile() {
        let context = ToolGameContext::from_parts(
            "stellar-blade",
            "Stellar Blade",
            Some(PathBuf::from("/fake/StellarBlade")),
            None,
        );
        let mut config = OptiScaler.default_config();
        config.set("optiscaler_profile", serde_json::json!("community-dxgi"));
        config.set("source_mode", serde_json::json!("goverlay_builds"));
        config.set("goverlay_channel", serde_json::json!("edge"));
        config.set(
            "release_tag",
            serde_json::json!("goverlay-edge:edge-0.9.12.0323"),
        );
        config.set("release_asset", serde_json::json!("optiscaler-edge.7z"));
        config.set("proxy_dll", serde_json::json!("winmm.dll"));
        config.set("dll_overrides", serde_json::json!("winmm,nvngx"));
        config.set("copy_companion_files", serde_json::json!(false));
        config.set("enable_optipatcher", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));
        config.set("emulate_fp8", serde_json::json!(true));
        config.set("spoof_dlss", serde_json::json!(true));
        config.set(
            "ini_overrides",
            serde_json::json!({"Spoofing": {"Dxgi": "false"}}),
        );

        apply_game_defaults(&mut config, Some(&context));

        assert_eq!(config.get_str("optiscaler_profile"), Some("community-dxgi"));
        assert_eq!(config.get_str("source_mode"), Some("goverlay_builds"));
        assert_eq!(config.get_str("goverlay_channel"), Some("edge"));
        assert_eq!(
            config.get_str("release_tag"),
            Some("goverlay-edge:edge-0.9.12.0323")
        );
        assert_eq!(config.get_str("release_asset"), Some("optiscaler-edge.7z"));
        assert_eq!(config.get_str("proxy_dll"), Some("winmm.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some("winmm,nvngx"));
        assert!(!config.get_bool("copy_companion_files"));
        assert!(!config.get_bool("enable_optipatcher"));
        assert_eq!(config.get_str("fsr4_variant"), Some(FSR4_VARIANT_INT8_402));
        assert!(config.get_bool("emulate_fp8"));
        assert!(config.get_bool("spoof_dlss"));
        assert_eq!(
            config
                .settings
                .pointer("/ini_overrides/Spoofing/Dxgi")
                .and_then(serde_json::Value::as_str),
            Some("false")
        );
        assert_eq!(config.get_str("tested_optiscaler_version"), Some("0.9"));
    }

    #[test]
    fn stellar_blade_game_defaults_preserve_existing_release_without_profile_marker() {
        let context = ToolGameContext::from_parts(
            "stellar-blade",
            "Stellar Blade",
            Some(PathBuf::from("/fake/StellarBlade")),
            None,
        );
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("github_release"));
        config.set("release_tag", serde_json::json!("official:v0.9.11"));
        config.set("release_asset", serde_json::json!("OptiScaler_0.9.11.7z"));

        apply_game_defaults(&mut config, Some(&context));

        assert_eq!(config.get_str("optiscaler_profile"), Some("community-dxgi"));
        assert_eq!(config.get_str("source_mode"), Some("github_release"));
        assert_eq!(config.get_str("release_tag"), Some("official:v0.9.11"));
        assert_eq!(
            config.get_str("release_asset"),
            Some("OptiScaler_0.9.11.7z")
        );
        assert_eq!(config.get_str("proxy_dll"), Some("dxgi.dll"));
        assert!(!config.get_bool("enable_optipatcher"));
    }

    #[test]
    fn selecting_profile_after_custom_applies_metadata_only() {
        let mut config = OptiScaler.default_config();
        assert!(apply_profile_by_id(&mut config, "stellar-blade", "custom"));
        assert_eq!(config.get_str("optiscaler_profile"), Some("custom"));
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set("release_tag", serde_json::json!("custom-build"));
        config.set("release_asset", serde_json::json!("CustomOptiScaler.7z"));
        config.set("proxy_dll", serde_json::json!("winmm.dll"));
        config.set("dll_overrides", serde_json::json!("winmm,nvngx"));
        config.set("copy_companion_files", serde_json::json!(false));
        config.set("enable_optipatcher", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));
        config.set("emulate_fp8", serde_json::json!(true));
        config.set("spoof_dlss", serde_json::json!(true));

        assert!(apply_profile_by_id(
            &mut config,
            "stellar-blade",
            "community-dxgi"
        ));

        assert_eq!(config.get_str("optiscaler_profile"), Some("community-dxgi"));
        assert_eq!(config.get_str("source_mode"), Some("local_dir"));
        assert_eq!(config.get_str("goverlay_channel"), Some("edge"));
        assert_eq!(config.get_str("release_tag"), Some("custom-build"));
        assert_eq!(config.get_str("release_asset"), Some("CustomOptiScaler.7z"));
        assert_eq!(config.get_str("proxy_dll"), Some("winmm.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some("winmm,nvngx"));
        assert!(!config.get_bool("copy_companion_files"));
        assert!(!config.get_bool("enable_optipatcher"));
        assert_eq!(config.get_str("fsr4_variant"), Some(FSR4_VARIANT_INT8_402));
        assert!(config.get_bool("emulate_fp8"));
        assert!(config.get_bool("spoof_dlss"));
        assert_eq!(config.get_str("tested_optiscaler_version"), Some("0.9"));
    }

    #[test]
    fn optiscaler_profile_metadata_does_not_change_settings() {
        let profile = crate::optiscaler::OptiScalerProfile {
            id: "test-profile",
            name: "Test Profile",
            source_url: "https://example.test/profile",
            tested_optiscaler_version: "1.2.3",
            source_mode: None,
            goverlay_channel: None,
            proxy_dll: "winmm.dll",
            release_tag: Some("v1.2.3"),
            release_asset: Some("OptiScaler.7z"),
            wine_dll_overrides: &["winmm", "nvngx"],
            copy_companion_files: false,
            enable_optipatcher: true,
            fsr4_variant: Some(FSR4_VARIANT_INT8_402),
            emulate_fp8: true,
            spoof_dlss: true,
            ini_overrides: &[crate::optiscaler::OptiScalerIniOverride {
                key: "Spoofing.Dxgi",
                value: "false",
            }],
            notes: "Test notes",
        };
        let mut config = OptiScaler.default_config();
        config.set("source_mode", serde_json::json!("local_dir"));
        config.set("release_tag", serde_json::json!("custom-build"));
        config.set("release_asset", serde_json::json!("CustomOptiScaler.7z"));
        config.set("proxy_dll", serde_json::json!("dxgi.dll"));
        config.set("dll_overrides", serde_json::json!("dxgi"));
        config.set("copy_companion_files", serde_json::json!(true));
        config.set("enable_optipatcher", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_LATEST_FP8));
        config.set("emulate_fp8", serde_json::json!(false));
        config.set("spoof_dlss", serde_json::json!(false));
        config.set(
            "ini_overrides",
            serde_json::json!({"OptiScaler": {"Dxgi": "auto"}}),
        );

        apply_optiscaler_profile_metadata(&mut config, &profile);

        assert_eq!(config.get_str("optiscaler_profile"), Some("test-profile"));
        assert_eq!(config.get_str("source_mode"), Some("local_dir"));
        assert_eq!(config.get_str("release_tag"), Some("custom-build"));
        assert_eq!(config.get_str("release_asset"), Some("CustomOptiScaler.7z"));
        assert_eq!(config.get_str("proxy_dll"), Some("dxgi.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some("dxgi"));
        assert!(config.get_bool("copy_companion_files"));
        assert!(!config.get_bool("enable_optipatcher"));
        assert_eq!(
            config.get_str("fsr4_variant"),
            Some(FSR4_VARIANT_LATEST_FP8)
        );
        assert!(!config.get_bool("emulate_fp8"));
        assert!(!config.get_bool("spoof_dlss"));
        assert_eq!(
            config
                .settings
                .pointer("/ini_overrides/OptiScaler/Dxgi")
                .and_then(serde_json::Value::as_str),
            Some("auto")
        );
        assert_eq!(config.get_str("tested_optiscaler_version"), Some("1.2.3"));
        assert_eq!(
            config.get_str("optiscaler_profile_source_url"),
            Some("https://example.test/profile")
        );
    }

    #[test]
    fn optiscaler_release_selection_preserves_custom_profile_deployment() {
        let mut config = OptiScaler.default_config();
        config.set("optiscaler_profile", serde_json::json!("community-dxgi"));
        config.set("proxy_dll", serde_json::json!("winmm.dll"));
        config.set("dll_overrides", serde_json::json!("winmm,nvngx"));
        config.set("copy_companion_files", serde_json::json!(false));
        config.set("enable_optipatcher", serde_json::json!(false));
        config.set("fsr4_variant", serde_json::json!(FSR4_VARIANT_INT8_402));
        config.set("emulate_fp8", serde_json::json!(true));
        config.set("spoof_dlss", serde_json::json!(true));
        config.set(
            "ini_overrides",
            serde_json::json!({"Spoofing": {"Dxgi": "false"}}),
        );

        apply_optiscaler_release_selection(&mut config, "official:v0.9.11", "OptiScaler_0.9.11.7z");

        assert_eq!(config.get_str("optiscaler_profile"), Some("community-dxgi"));
        assert_eq!(config.get_str("source_mode"), Some("github_release"));
        assert_eq!(config.get_str("release_tag"), Some("official:v0.9.11"));
        assert_eq!(
            config.get_str("release_asset"),
            Some("OptiScaler_0.9.11.7z")
        );
        assert_eq!(config.get_str("proxy_dll"), Some("winmm.dll"));
        assert_eq!(config.get_str("dll_overrides"), Some("winmm,nvngx"));
        assert!(!config.get_bool("copy_companion_files"));
        assert!(!config.get_bool("enable_optipatcher"));
        assert_eq!(config.get_str("fsr4_variant"), Some(FSR4_VARIANT_INT8_402));
        assert!(config.get_bool("emulate_fp8"));
        assert!(config.get_bool("spoof_dlss"));
        assert_eq!(
            config
                .settings
                .pointer("/ini_overrides/Spoofing/Dxgi")
                .and_then(serde_json::Value::as_str),
            Some("false")
        );
    }

    #[test]
    fn incompatible_existing_ini_triggers_reset_reason() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let new_ini = tmp.path().join("new.ini");
        std::fs::write(&new_ini, "[OptiScaler]\nDxgi=auto\n").expect("new ini");
        let state = OptiScalerInstallState {
            status: OptiScalerInstallStatus::Unmanaged,
            executable_dir: tmp.path().to_path_buf(),
            proxy_dlls: vec![],
            wine_dll_overrides: vec![],
            config_path: None,
            ini_settings: BTreeMap::from([("Removed.SectionKey".to_string(), "true".to_string())]),
            companion_files: vec![],
            recognized_files: vec![],
            version: OptiScalerVersionIdentity::Unknown,
            latest_backup: None,
        };
        let config = OptiScaler.default_config();
        assert_eq!(
            optiscaler_config_reset_reason(&state, &new_ini, &config).as_deref(),
            Some("schema mismatch")
        );
    }

    fn release_fixture(tag: &str, asset_names: &[&str]) -> ToolReleaseSummary {
        ToolReleaseSummary {
            tag: tag.to_string(),
            name: None,
            published_at: None,
            assets: asset_names
                .iter()
                .map(|name| ToolReleaseAsset {
                    name: (*name).to_string(),
                    download_url: format!("https://example.test/{name}"),
                    size: 1,
                })
                .collect(),
        }
    }

    fn release_fixture_with_date(
        tag: &str,
        asset_names: &[&str],
        published_at: &str,
    ) -> ToolReleaseSummary {
        let mut release = release_fixture(tag, asset_names);
        release.published_at = Some(published_at.to_string());
        release
    }

    // ── set_ini_value ─────────────────────────────────────────────────

    #[test]
    fn set_ini_value_updates_existing_key_in_section() {
        let content = "[OptiScaler]\nDxgi=auto\nLoadAsiPlugins=false\n";
        let updated = set_ini_value(content, "OptiScaler.Dxgi", "manual");
        assert!(updated.contains("Dxgi=manual"));
        assert!(updated.contains("LoadAsiPlugins=false"));
    }

    #[test]
    fn set_ini_value_appends_section_when_missing() {
        let updated = set_ini_value("", "Menu.Scale", "1.25");
        assert!(updated.contains("[Menu]"));
        assert!(updated.contains("Scale=1.25"));
    }

    #[test]
    fn set_ini_value_returns_content_unchanged_when_key_has_no_section() {
        let content = "[OptiScaler]\nDxgi=auto\n";
        let updated = set_ini_value(content, "no_section_prefix", "x");
        assert_eq!(updated, content, "malformed key should not mutate content");
    }

    // ── relative_to_game ──────────────────────────────────────────────

    #[test]
    fn relative_to_game_strips_game_prefix() {
        let game = PathBuf::from("/games/skyrim");
        let dest = PathBuf::from("/games/skyrim/Data/SKSE/plugins/foo.dll");
        let rel = relative_to_game(&game, &dest).expect("strips prefix");
        assert_eq!(rel, PathBuf::from("Data/SKSE/plugins/foo.dll"));
    }

    #[test]
    fn relative_to_game_errors_when_dest_outside_game_dir() {
        let game = PathBuf::from("/games/skyrim");
        let dest = PathBuf::from("/elsewhere/foo.dll");
        let err = relative_to_game(&game, &dest).expect_err("must fail");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("/elsewhere/foo.dll") && msg.contains("/games/skyrim"),
            "error must mention both paths: {msg}"
        );
    }
}