Welcome to mirror list, hosted at ThFree Co, Russian Federation.

proton - github.com/ValveSoftware/Proton.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/proton
blob: df4efd4d5a3f287fdecdbc337ced1c25f4959b40 (plain)
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
#!/usr/bin/env python3

#script to launch Wine with the correct environment

import fcntl
import array
import filecmp
import fnmatch
import json
import os
import shutil
import errno
import platform
import stat
import subprocess
import sys
import tarfile
import shlex

from ctypes import CDLL
from ctypes import CFUNCTYPE
from ctypes import POINTER
from ctypes import Structure
from ctypes import addressof
from ctypes import cast
from ctypes import get_errno
from ctypes import sizeof
from ctypes import c_int
from ctypes import c_int64
from ctypes import c_uint
from ctypes import c_long
from ctypes import c_char_p
from ctypes import c_void_p
from ctypes import c_size_t
from ctypes import c_ssize_t

from filelock import FileLock
from random import randrange

#To enable debug logging, copy "user_settings.sample.py" to "user_settings.py"
#and edit it if needed.

CURRENT_PREFIX_VERSION="7.0-100"

PFX="Proton: "
ld_path_var = "LD_LIBRARY_PATH"

def file_exists(s, *, follow_symlinks):
    if follow_symlinks:
        #'exists' returns False on broken symlinks
        return os.path.exists(s)
    #'lexists' returns True on broken symlinks
    return os.path.lexists(s)

def nonzero(s):
    return len(s) > 0 and s != "0"

def prepend_to_env_str(env, variable, prepend_str, separator):
    if not variable in env:
        env[variable] = prepend_str
    else:
        env[variable] = prepend_str + separator + env[variable]

def append_to_env_str(env, variable, append_str, separator):
    if not variable in env:
        env[variable] = append_str
    else:
        env[variable] = env[variable] + separator + append_str

def log(msg):
    sys.stderr.write(PFX + msg + os.linesep)
    sys.stderr.flush()

def file_is_wine_builtin_dll(path):
    if os.path.islink(path):
        contents = os.readlink(path)
        if os.path.dirname(contents).endswith((
                    '/lib/wine',
                    '/lib64/wine',
                    '/lib/wine/fakedlls',
                    '/lib64/wine/fakedlls',
                    '/lib/wine/i386-unix',
                    '/lib/wine/i386-windows',
                    '/lib64/wine/x86_64-unix',
                    '/lib64/wine/x86_64-windows'
                )):
            # This may be a broken link to a dll in a removed Proton install
            return True
    if not file_exists(path, follow_symlinks=True):
        return False
    try:
        sfile = open(path, "rb")
        sfile.seek(0x40)
        tag = sfile.read(20)
        return tag.startswith((b"Wine placeholder DLL", b"Wine builtin DLL"))
    except IOError:
        return False

def makedirs(path):
    try:
        #replace broken symlinks with a new directory
        if os.path.islink(path) and not file_exists(path, follow_symlinks=True):
            os.remove(path)
        os.makedirs(path)
    except OSError:
        #already exists
        pass

def merge_user_dir(src, dst):
    extant_dirs = []
    for src_dir, dirs, files in os.walk(src):
        dst_dir = src_dir.replace(src, dst, 1)

        #as described below, avoid merging game save subdirs, too
        child_of_extant_dir = False
        for dir_ in extant_dirs:
            if dir_ in dst_dir:
                child_of_extant_dir = True
                break
        if child_of_extant_dir:
            continue

        #we only want to copy into directories which don't already exist. games
        #may not react well to two save directory instances being merged.
        if not file_exists(dst_dir, follow_symlinks=True) or os.path.samefile(dst_dir, dst):
            makedirs(dst_dir)
            for dir_ in dirs:
                src_file = os.path.join(src_dir, dir_)
                dst_file = os.path.join(dst_dir, dir_)
                if os.path.islink(src_file) and not file_exists(dst_file, follow_symlinks=True):
                    try_copy(src_file, dst_file, copy_metadata=True, follow_symlinks=False)
            for file_ in files:
                src_file = os.path.join(src_dir, file_)
                dst_file = os.path.join(dst_dir, file_)
                if not file_exists(dst_file, follow_symlinks=True):
                    try_copy(src_file, dst_file, copy_metadata=True, follow_symlinks=False)
        else:
            extant_dirs += dst_dir

def try_copy(src, dst, prefix=None, add_write_perm=True, copy_metadata=False, optional=False,
             follow_symlinks=True, track_file=False, link_debug=False):
    try:
        if prefix is not None:
            dst = os.path.join(prefix, dst)

        if os.path.isdir(dst):
            dst = os.path.join(dst, os.path.basename(src))

        if file_exists(dst, follow_symlinks=False):
            os.remove(dst)
        elif track_file and prefix is not None:
            track_file.write(os.path.relpath(dst, prefix) + '\n')

        if os.path.islink(src) and not follow_symlinks:
            shutil.copyfile(src, dst, follow_symlinks=False)
        else:
            copyfile(src, dst)

        if copy_metadata:
            shutil.copystat(src, dst, follow_symlinks=follow_symlinks)
        else:
            shutil.copymode(src, dst, follow_symlinks=follow_symlinks)

        if add_write_perm:
            new_mode = os.lstat(dst).st_mode | stat.S_IWUSR | stat.S_IWGRP
            os.chmod(dst, new_mode)

        if not file_exists(src + '.debug', follow_symlinks=True):
            link_debug = False

        if file_exists(dst + '.debug', follow_symlinks=False):
            os.remove(dst + '.debug')
        elif link_debug:
            track_file.write(os.path.relpath(dst + '.debug', prefix) + '\n')

        if link_debug:
            os.symlink(src + '.debug', dst + '.debug')

    except FileNotFoundError as e:
        if optional:
            log('Error while copying to \"' + dst + '\": ' + e.strerror)
        else:
            raise

    except PermissionError as e:
        if e.errno == errno.EPERM:
            #be forgiving about permissions errors; if it's a real problem, things will explode later anyway
            log('Error while copying to \"' + dst + '\": ' + e.strerror)
        else:
            raise

# copy_file_range implementation for old Python versions
__syscall__copy_file_range = None

def copy_file_range_ctypes(fd_in, fd_out, count):
    "Copy data using the copy_file_range syscall through ctypes, assuming x86_64 Linux"
    global __syscall__copy_file_range
    __NR_copy_file_range = 326

    if __syscall__copy_file_range is None:
        c_int64_p = POINTER(c_int64)
        prototype = CFUNCTYPE(c_ssize_t, c_long, c_int, c_int64_p,
            c_int, c_int64_p, c_size_t, c_uint, use_errno=True)
        __syscall__copy_file_range = prototype(('syscall', CDLL(None, use_errno=True)))

    while True:
        ret = __syscall__copy_file_range(__NR_copy_file_range, fd_in, None, fd_out, None, count, 0)
        if ret >= 0 or get_errno() != errno.EINTR:
            break

    if ret < 0:
        raise OSError(get_errno(), errno.errorcode.get(get_errno(), 'unknown'))

    return ret

def copyfile_reflink(srcname, dstname):
    "Copy srcname to dstname, making reflink if possible"
    global copyfile
    with open(srcname, 'rb', buffering=0) as src:
        bytes_to_copy = os.fstat(src.fileno()).st_size
        try:
            with open(dstname, 'wb', buffering=0) as dst:
                while bytes_to_copy > 0:
                    bytes_to_copy -= copy_file_range(src.fileno(), dst.fileno(), bytes_to_copy)
        except OSError as e:
            if e.errno not in (errno.EXDEV, errno.ENOSYS, errno.EINVAL):
                raise e
            if e.errno == errno.ENOSYS:
                copyfile = shutil.copyfile
            shutil.copyfile(srcname, dstname)

if hasattr(os, 'copy_file_range'):
    copyfile = copyfile_reflink
    copy_file_range = os.copy_file_range
elif sys.platform == 'linux' and platform.machine() == 'x86_64' and sizeof(c_void_p) == 8:
    copyfile = copyfile_reflink
    copy_file_range = copy_file_range_ctypes
else:
    copyfile = shutil.copyfile

def try_copyfile(src, dst):
    try:
        if os.path.isdir(dst):
            dst = dst + "/" + os.path.basename(src)
        if file_exists(dst, follow_symlinks=False):
            os.remove(dst)
        copyfile(src, dst)
    except PermissionError as e:
        if e.errno == errno.EPERM:
            #be forgiving about permissions errors; if it's a real problem, things will explode later anyway
            log('Error while copying to \"' + dst + '\": ' + e.strerror)
        else:
            raise

def getmtimestr(*path_fragments):
    path = os.path.join(*path_fragments)
    try:
        return str(os.path.getmtime(path))
    except IOError:
        return "0"

def try_get_game_library_dir():
    if not "STEAM_COMPAT_INSTALL_PATH" in g_session.env or \
            not "STEAM_COMPAT_LIBRARY_PATHS" in g_session.env:
        return None

    #find library path which is a subset of the game path
    library_paths = g_session.env["STEAM_COMPAT_LIBRARY_PATHS"].split(":")
    for l in library_paths:
        if l in g_session.env["STEAM_COMPAT_INSTALL_PATH"]:
            return l

    return None

def try_get_steam_dir():
    if not "STEAM_COMPAT_CLIENT_INSTALL_PATH" in g_session.env:
        return None

    return g_session.env["STEAM_COMPAT_CLIENT_INSTALL_PATH"]

def setup_dir_drive(compat_option, drive_name, dest_dir):
        drive_path = g_compatdata.prefix_dir + "dosdevices/" + drive_name
        if compat_option in g_session.compat_config:
            if not dest_dir:
                if file_exists(drive_path, follow_symlinks=False):
                    os.remove(drive_path)
            else:
                if file_exists(drive_path, follow_symlinks=False):
                    cur_tgt = os.readlink(drive_path)
                    if cur_tgt != dest_dir:
                        os.remove(drive_path)
                        os.symlink(dest_dir, drive_path)
                else:
                    os.symlink(dest_dir, drive_path)
        elif file_exists(drive_path, follow_symlinks=False):
            os.remove(drive_path)

def setup_game_dir_drive():
        setup_dir_drive("gamedrive", "s:", try_get_game_library_dir())

def setup_steam_dir_drive():
        setup_dir_drive("steamdrive", "t:", try_get_steam_dir())

# Function to find the installed location of DLL files for use by Wine/Proton
# from the NVIDIA Linux driver
#
# See https://gitlab.steamos.cloud/steamrt/steam-runtime-tools/-/issues/71 for
# background on the chosen method of DLL discovery.
#
# On success, returns a str() of the absolute-path to the directory at which DLL
# files are stored
#
# On failure, returns None
def find_nvidia_wine_dll_dir():
    try:
        libdl = CDLL("libdl.so.2")
    except (OSError):
        return None

    try:
        libglx_nvidia = CDLL("libGLX_nvidia.so.0")
    except OSError:
        return None

    # from dlinfo(3)
    #
    # struct link_map {
    #     ElfW(Addr) l_addr;  /* Difference between the
    #                            address in the ELF file and
    #                            the address in memory */
    #     char      *l_name;  /* Absolute pathname where
    #                            object was found */
    #     ElfW(Dyn) *l_ld;    /* Dynamic section of the
    #                            shared object */
    #     struct link_map *l_next, *l_prev;
    #                         /* Chain of loaded objects */
    #
    #     /* Plus additional fields private to the
    #        implementation */
    # };
    RTLD_DI_LINKMAP = 2
    class link_map(Structure):
        _fields_ = [("l_addr", c_void_p), ("l_name", c_char_p), ("l_ld", c_void_p)]

    # from dlinfo(3)
    #
    # int dlinfo (void *restrict handle, int request, void *restrict info)
    dlinfo_func = libdl.dlinfo
    dlinfo_func.argtypes = c_void_p, c_int, c_void_p
    dlinfo_func.restype = c_int

    # Allocate a link_map object
    glx_nvidia_info_ptr = POINTER(link_map)()

    # Run dlinfo(3) on the handle to libGLX_nvidia.so.0, storing results at the
    # address represented by glx_nvidia_info_ptr
    if dlinfo_func(libglx_nvidia._handle,
                   RTLD_DI_LINKMAP,
                   addressof(glx_nvidia_info_ptr)) != 0:
        return None

    # Grab the contents our of our pointer
    glx_nvidia_info = cast(glx_nvidia_info_ptr, POINTER(link_map)).contents

    # Decode the path to our library to a str()
    if glx_nvidia_info.l_name is None:
        return None
    try:
        libglx_nvidia_path = os.fsdecode(glx_nvidia_info.l_name)
    except UnicodeDecodeError:
        return None

    # Follow any symlinks to the actual file
    libglx_nvidia_realpath = os.path.realpath(libglx_nvidia_path)

    # Go to the relative path ./nvidia/wine from our library
    nvidia_wine_dir = os.path.join(os.path.dirname(libglx_nvidia_realpath), "nvidia", "wine")

    # Check that nvngx.dll exists here, or fail
    if file_exists(os.path.join(nvidia_wine_dir, "nvngx.dll"), follow_symlinks=True):
        return nvidia_wine_dir

    return None

EXT2_IOC_GETFLAGS = 0x80086601
EXT2_IOC_SETFLAGS = 0x40086602

EXT4_CASEFOLD_FL = 0x40000000

def set_dir_casefold_bit(dir_path):
    dr = os.open(dir_path, 0o644)
    if dr < 0:
        return
    try:
        dat = array.array('I', [0])
        if fcntl.ioctl(dr, EXT2_IOC_GETFLAGS, dat, True) >= 0:
            dat[0] = dat[0] | EXT4_CASEFOLD_FL
            fcntl.ioctl(dr, EXT2_IOC_SETFLAGS, dat, False)
    except (OSError, IOError):
        #no problem
        pass
    os.close(dr)

class Proton:
    def __init__(self, base_dir):
        self.base_dir = base_dir + "/"
        self.dist_dir = self.path("dist/")
        self.bin_dir = self.path("dist/bin/")
        self.lib_dir = self.path("dist/lib/")
        self.lib64_dir = self.path("dist/lib64/")
        self.fonts_dir = self.path("dist/share/fonts/")
        self.wine_fonts_dir = self.path("dist/share/wine/fonts/")
        self.wine_inf = self.path("dist/share/wine/wine.inf")
        self.version_file = self.path("version")
        self.default_pfx_dir = self.path("dist/share/default_pfx/")
        self.user_settings_file = self.path("user_settings.py")
        self.wine_bin = self.bin_dir + "wine"
        self.wine64_bin = self.bin_dir + "wine64"
        self.wineserver_bin = self.bin_dir + "wineserver"
        self.dist_lock = FileLock(self.path("dist.lock"), timeout=-1)

    def path(self, d):
        return self.base_dir + d

    def need_tarball_extraction(self):
        '''Checks if the proton_dist tarball archive must be extracted. Returns true if extraction is needed, false otherwise'''
        return not file_exists(self.dist_dir, follow_symlinks=True) or \
            not file_exists(self.path("dist/version"), follow_symlinks=True) or \
            not filecmp.cmp(self.version_file, self.path("dist/version"))

    def extract_tarball(self):
        with self.dist_lock:
            if self.need_tarball_extraction():
                if file_exists(self.dist_dir, follow_symlinks=True):
                    shutil.rmtree(self.dist_dir)
                tar = None
                for sf in ["", ".xz", ".bz2", ".gz"]:
                    if file_exists(self.path("proton_dist.tar" + sf), follow_symlinks=True):
                        tar = tarfile.open(self.path("proton_dist.tar" + sf), mode="r:*")
                        break
                if not tar:
                    log("No proton_dist tarball??")
                    sys.exit(1)
                tar.extractall(path=self.dist_dir)
                tar.close()
                try_copy(self.version_file, self.dist_dir)

    def missing_default_prefix(self):
        '''Check if the default prefix dir is missing. Returns true if missing, false if present'''
        return not os.path.isdir(self.default_pfx_dir)

    def make_default_prefix(self):
        with self.dist_lock:
            local_env = dict(g_session.env)
            if self.missing_default_prefix():
                #make default prefix
                local_env["WINEPREFIX"] = self.default_pfx_dir
                local_env["WINEDEBUG"] = "-all"
                g_session.run_proc([self.wine_bin, "wineboot"], local_env)
                g_session.run_proc([self.wineserver_bin, "-w"], local_env)

class CompatData:
    def __init__(self, compatdata):
        self.base_dir = compatdata + "/"
        self.prefix_dir = self.path("pfx/")
        self.version_file = self.path("version")
        self.config_info_file = self.path("config_info")
        self.tracked_files_file = self.path("tracked_files")
        self.prefix_lock = FileLock(self.path("pfx.lock"), timeout=-1)

    def path(self, d):
        return self.base_dir + d

    def remove_tracked_files(self):
        if not file_exists(self.tracked_files_file, follow_symlinks=True):
            log("Prefix has no tracked_files??")
            return

        with open(self.tracked_files_file, "r") as tracked_files:
            dirs = []
            for f in tracked_files:
                path = self.prefix_dir + f.strip()
                if file_exists(path, follow_symlinks=False):
                    if os.path.isfile(path) or os.path.islink(path):
                        os.remove(path)
                    else:
                        dirs.append(path)
            for d in dirs:
                try:
                    os.rmdir(d)
                except OSError:
                    #not empty
                    pass

        os.remove(self.tracked_files_file)
        os.remove(self.version_file)

    def upgrade_pfx(self, old_ver):
        if old_ver == CURRENT_PREFIX_VERSION:
            return

        log("Upgrading prefix from " + str(old_ver) + " to " + CURRENT_PREFIX_VERSION + " (" + self.base_dir + ")")

        if old_ver is None:
            return

        if not '-' in old_ver:
            #How can this happen??
            log("Prefix has an invalid version?! You may want to back up user files and delete this prefix.")
            #If it does, just let the Wine upgrade happen and hope it works...
            return

        try:
            old_proton_ver, old_prefix_ver = old_ver.split('-')
            old_proton_maj, old_proton_min = old_proton_ver.split('.')
            new_proton_ver, new_prefix_ver = CURRENT_PREFIX_VERSION.split('-')
            new_proton_maj, new_proton_min = new_proton_ver.split('.')

            if int(new_proton_maj) < int(old_proton_maj) or \
                    (int(new_proton_maj) == int(old_proton_maj) and \
                     int(new_proton_min) < int(old_proton_min)):
                log("Removing newer prefix")
                if old_proton_ver == "3.7" and not file_exists(self.tracked_files_file, follow_symlinks=True):
                    #proton 3.7 did not generate tracked_files, so copy it into place first
                    try_copy(g_proton.path("proton_3.7_tracked_files"), self.tracked_files_file)
                self.remove_tracked_files()
                return

            if old_proton_ver == "3.7" and old_prefix_ver == "1":
                if not file_exists(self.prefix_dir + "/drive_c/windows/syswow64/kernel32.dll", follow_symlinks=True):
                    #shipped a busted 64-bit-only installation on 20180822. detect and wipe clean
                    log("Detected broken 64-bit-only installation, re-creating prefix.")
                    shutil.rmtree(self.prefix_dir)
                    return

            #replace broken .NET installations with wine-mono support
            if file_exists(self.prefix_dir + "/drive_c/windows/Microsoft.NET/NETFXRepair.exe", follow_symlinks=True) and \
                    file_is_wine_builtin_dll(self.prefix_dir + "/drive_c/windows/system32/mscoree.dll"):
                log("Broken .NET installation detected, switching to wine-mono.")
                #deleting this directory allows wine-mono to work
                shutil.rmtree(self.prefix_dir + "/drive_c/windows/Microsoft.NET")

            #prior to prefix version 4.11-2, all controllers were xbox controllers. wipe out the old registry entries.
            if (int(old_proton_maj) < 4 or (int(old_proton_maj) == 4 and int(old_proton_min) == 11)) and \
                    int(old_prefix_ver) < 2:
                log("Removing old xinput registry entries.")
                with open(self.prefix_dir + "system.reg", "r") as reg_in:
                    with open(self.prefix_dir + "system.reg.new", "w") as reg_out:
                        for line in reg_in:
                            if line[0] == '[' and "CurrentControlSet" in line and "IG_" in line:
                                if "DeviceClasses" in line:
                                    reg_out.write(line.replace("DeviceClasses", "DeviceClasses_old"))
                                elif "Enum" in line:
                                    reg_out.write(line.replace("Enum", "Enum_old"))
                            else:
                                reg_out.write(line)
                try:
                    os.rename(self.prefix_dir + "system.reg", self.prefix_dir + "system.reg.old")
                except OSError:
                    os.remove(self.prefix_dir + "system.reg")
                    pass

                try:
                    os.rename(self.prefix_dir + "system.reg.new", self.prefix_dir + "system.reg")
                except OSError:
                    log("Unable to write new registry file to " + self.prefix_dir + "system.reg")
                    pass

            # Prior to prefix version 6.3-3, ShellExecute* APIs used DDE.
            # Wipe out old registry entries.
            if int(old_proton_maj) < 6 or (int(old_proton_maj) == 6 and int(old_proton_min) < 3) or \
                    (int(old_proton_maj) == 6 and int(old_proton_min) == 3 and int(old_prefix_ver) < 3):
                delete_keys = {
                    "[Software\\\\Classes\\\\htmlfile\\\\shell\\\\open\\\\ddeexec",
                    "[Software\\\\Classes\\\\pdffile\\\\shell\\\\open\\\\ddeexec",
                    "[Software\\\\Classes\\\\xmlfile\\\\shell\\\\open\\\\ddeexec",
                    "[Software\\\\Classes\\\\ftp\\\\shell\\\\open\\\\ddeexec",
                    "[Software\\\\Classes\\\\http\\\\shell\\\\open\\\\ddeexec",
                    "[Software\\\\Classes\\\\https\\\\shell\\\\open\\\\ddeexec",
                }
                dde_wb = '@="\\"C:\\\\windows\\\\system32\\\\winebrowser.exe\\" -nohome"'

                sysreg_fp = self.prefix_dir + "system.reg"
                new_sysreg_fp = self.prefix_dir + "system.reg.new"

                log("Removing ShellExecute DDE registry entries.")

                with open(sysreg_fp, "r") as reg_in:
                    with open(new_sysreg_fp, "w") as reg_out:
                        for line in reg_in:
                            if line[:line.find("ddeexec")+len("ddeexec")] in delete_keys:
                                reg_out.write(line.replace("ddeexec", "ddeexec_old", 1))
                            elif line.rstrip() == dde_wb:
                                reg_out.write(line.replace("-nohome", "%1"))
                            else:
                                reg_out.write(line)

                # Slightly randomize backup file name to avoid colliding with
                # other backups.
                backup_sysreg_fp = "{}system.reg.{:x}.old".format(self.prefix_dir, randrange(16 ** 8))

                try:
                    os.rename(sysreg_fp, backup_sysreg_fp)
                except OSError:
                    log("Failed to back up old system.reg. Simply deleting it.")
                    os.remove(sysreg_fp)
                    pass

                try:
                    os.rename(new_sysreg_fp, sysreg_fp)
                except OSError:
                    log("Unable to write new registry file to " + sysreg_fp)
                    pass

            stale_builtins = [self.prefix_dir + "/drive_c/windows/system32/amd_ags_x64.dll",
                              self.prefix_dir + "/drive_c/windows/syswow64/amd_ags_x64.dll" ]
            for builtin in stale_builtins:
                if file_exists(builtin, follow_symlinks=False) and file_is_wine_builtin_dll(builtin):
                    log("Removing stale builtin " + builtin)
                    os.remove(builtin)

        except ValueError:
            log("Prefix has an invalid version?! You may want to back up user files and delete this prefix.")
            #Just let the Wine upgrade happen and hope it works...
            return

    def pfx_copy(self, src, dst, dll_copy=False):
        if os.path.islink(src):
            contents = os.readlink(src)
            if os.path.dirname(contents).endswith(('/lib/wine/i386-unix', '/lib/wine/i386-windows', '/lib64/wine/x86_64-unix', '/lib64/wine/x86_64-windows')):
                # wine builtin dll
                # make the destination an absolute symlink
                contents = os.path.normpath(os.path.join(os.path.dirname(src), contents))
            if dll_copy:
                try_copyfile(src, dst)
            else:
                os.symlink(contents, dst)
        else:
            try_copyfile(src, dst)

    def copy_pfx(self):
        with open(self.tracked_files_file, "w") as tracked_files:
            for src_dir, dirs, files in os.walk(g_proton.default_pfx_dir):
                rel_dir = src_dir.replace(g_proton.default_pfx_dir, "", 1).lstrip('/')
                if len(rel_dir) > 0:
                    rel_dir = rel_dir + "/"
                dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
                if not file_exists(dst_dir, follow_symlinks=True):
                    makedirs(dst_dir)
                    tracked_files.write(rel_dir + "\n")
                for dir_ in dirs:
                    src_file = os.path.join(src_dir, dir_)
                    dst_file = os.path.join(dst_dir, dir_)
                    if os.path.islink(src_file) and not file_exists(dst_file, follow_symlinks=True):
                        self.pfx_copy(src_file, dst_file)
                for file_ in files:
                    src_file = os.path.join(src_dir, file_)
                    dst_file = os.path.join(dst_dir, file_)
                    if not file_exists(dst_file, follow_symlinks=True):
                        self.pfx_copy(src_file, dst_file)
                        tracked_files.write(rel_dir + file_ + "\n")
        # Set .update-timestamp so Wine doesn't try to update the prefix.
        # This is needed in case the mtime of wine.inf has changed in distribution.
        with open(os.path.join(self.prefix_dir, '.update-timestamp'), 'w') as update_timestamp:
            mtime = int(os.stat(g_proton.wine_inf).st_mtime)
            update_timestamp.write(str(mtime))

    def update_builtin_libs(self, dll_copy_patterns):
        dll_copy_patterns = dll_copy_patterns.split(',')
        prev_tracked_files = set()
        with open(self.tracked_files_file, "r") as tracked_files:
            for line in tracked_files:
                prev_tracked_files.add(line.strip())
        with open(self.tracked_files_file, "a") as tracked_files:
            for src_dir, dirs, files in os.walk(g_proton.default_pfx_dir):
                rel_dir = src_dir.replace(g_proton.default_pfx_dir, "", 1).lstrip('/')
                if len(rel_dir) > 0:
                    rel_dir = rel_dir + "/"
                dst_dir = src_dir.replace(g_proton.default_pfx_dir, self.prefix_dir, 1)
                if not file_exists(dst_dir, follow_symlinks=True):
                    makedirs(dst_dir)
                    tracked_files.write(rel_dir + "\n")
                for file_ in files:
                    src_file = os.path.join(src_dir, file_)
                    dst_file = os.path.join(dst_dir, file_)
                    if not file_is_wine_builtin_dll(src_file):
                        # Not a builtin library
                        continue
                    if file_is_wine_builtin_dll(dst_file):
                        os.unlink(dst_file)
                    elif file_exists(dst_file, follow_symlinks=False):
                        # builtin library was replaced
                        continue
                    else:
                        os.makedirs(dst_dir, exist_ok=True)
                    dll_copy = any(fnmatch.fnmatch(file_, pattern) for pattern in dll_copy_patterns)
                    self.pfx_copy(src_file, dst_file, dll_copy)
                    tracked_name = rel_dir + file_
                    if tracked_name not in prev_tracked_files:
                        tracked_files.write(tracked_name + "\n")

    def create_fonts_symlinks(self):
        fontsmap = [
            ( g_proton.fonts_dir, "LiberationSans-Regular.ttf", "arial.ttf" ),
            ( g_proton.fonts_dir, "LiberationSans-Bold.ttf", "arialbd.ttf" ),
            ( g_proton.fonts_dir, "LiberationSerif-Regular.ttf", "times.ttf" ),
            ( g_proton.fonts_dir, "LiberationMono-Regular.ttf", "cour.ttf" ),
            ( g_proton.fonts_dir, "LiberationMono-Bold.ttf", "courbd.ttf" ),
            ( g_proton.fonts_dir, "msyh.ttf", "msyh.ttf" ),
            ( g_proton.fonts_dir, "simsun.ttc", "simsun.ttc" ),
            ( g_proton.fonts_dir, "msgothic.ttc", "msgothic.ttc" ),
            ( g_proton.fonts_dir, "malgun.ttf", "malgun.ttf" ),
            ( g_proton.fonts_dir, "micross.ttf", "micross.ttf" ),
            ( g_proton.fonts_dir, "nirmala.ttf", "nirmala.ttf" ),

            ( g_proton.wine_fonts_dir, "tahoma.ttf", "tahoma.ttf" ),
        ]

        windowsfonts = self.prefix_dir + "/drive_c/windows/Fonts"
        makedirs(windowsfonts)
        for p in fontsmap:
            lname = os.path.join(windowsfonts, p[2])
            fname = os.path.join(p[0], p[1])
            if file_exists(lname, follow_symlinks=False):
                if os.path.islink(lname):
                    os.remove(lname)
                    os.symlink(fname, lname)
            else:
                os.symlink(fname, lname)

    def migrate_user_paths(self):
        #move winxp-style paths to vista+ paths. we can't do this in
        #upgrade_pfx because Steam may drop cloud files here at any time.
        for (old, new, link) in \
                [
                    ("drive_c/users/steamuser/Local Settings/Application Data",
                        self.prefix_dir + "drive_c/users/steamuser/AppData/Local",
                        "../AppData/Local"),
                    ("drive_c/users/steamuser/Application Data",
                        self.prefix_dir + "drive_c/users/steamuser/AppData/Roaming",
                        "./AppData/Roaming"),
                    ("drive_c/users/steamuser/My Documents",
                        self.prefix_dir + "drive_c/users/steamuser/Documents",
                        "./Documents"),
                ]:

            #running unofficial Proton/Wine builds against a Proton prefix could
            #create an infinite symlink loop. detect this and clean it up.
            if file_exists(new, follow_symlinks=False) and os.path.islink(new) and os.readlink(new).endswith(old):
                os.remove(new)

            old = self.prefix_dir + old

            if file_exists(old, follow_symlinks=False) and not os.path.islink(old):
                merge_user_dir(src=old, dst=new)
                os.rename(old, old + " BACKUP")
            if not file_exists(old, follow_symlinks=False):
                makedirs(os.path.dirname(old))
                os.symlink(src=link, dst=old)
            elif os.path.islink(old) and not (os.readlink(old) == link):
                os.remove(old)
                os.symlink(src=link, dst=old)

    def setup_prefix(self):
        with self.prefix_lock:
            if file_exists(self.version_file, follow_symlinks=True):
                with open(self.version_file, "r") as f:
                    old_ver = f.readline().strip()
            else:
                old_ver = None

            self.upgrade_pfx(old_ver)

            if not file_exists(self.prefix_dir, follow_symlinks=True):
                makedirs(self.prefix_dir + "/drive_c")
                set_dir_casefold_bit(self.prefix_dir + "/drive_c")

            if not file_exists(self.prefix_dir + "/user.reg", follow_symlinks=True):
                self.copy_pfx()

            self.migrate_user_paths()

            # collect configuration info
            steamdir = os.environ["STEAM_COMPAT_CLIENT_INSTALL_PATH"]

            use_wined3d = "wined3d" in g_session.compat_config
            use_dxvk_dxgi = not use_wined3d and \
                    not ("WINEDLLOVERRIDES" in g_session.env and "dxgi=b" in g_session.env["WINEDLLOVERRIDES"])
            use_nvapi = 'enablenvapi' in g_session.compat_config

            builtin_dll_copy = os.environ.get("PROTON_DLL_COPY",
                    #dxsetup redist
                    "d3dcompiler_*.dll," +
                    "d3dcsx*.dll," +
                    "d3dx*.dll," +
                    "x3daudio*.dll," +
                    "xactengine*.dll," +
                    "xapofx*.dll," +
                    "xaudio*.dll," +
                    "xinput*.dll," +

                    #vcruntime redist
                    "atl1*.dll," +
                    "concrt1*.dll," +
                    "msvcp1*.dll," +
                    "msvcr1*.dll," +
                    "vcamp1*.dll," +
                    "vcomp1*.dll," +
                    "vccorlib1*.dll," +
                    "vcruntime1*.dll," +

                    #some games balk at ntdll symlink(?)
                    "ntdll.dll," +

                    #some games require official vulkan loader
                    "vulkan-1.dll"
                    )

            # If any of this info changes, we must rerun the tasks below
            prefix_info = '\n'.join((
                CURRENT_PREFIX_VERSION,
                g_proton.fonts_dir,
                g_proton.lib_dir,
                g_proton.lib64_dir,
                steamdir,
                getmtimestr(steamdir, 'legacycompat', 'steamclient.dll'),
                getmtimestr(steamdir, 'legacycompat', 'steamclient64.dll'),
                getmtimestr(steamdir, 'legacycompat', 'Steam.dll'),
                g_proton.default_pfx_dir,
                getmtimestr(g_proton.default_pfx_dir, 'system.reg'),
                str(use_wined3d),
                str(use_dxvk_dxgi),
                builtin_dll_copy,
                str(use_nvapi),
            ))

            # check whether any prefix config has changed
            try:
                with open(self.config_info_file, "r") as f:
                    old_prefix_info = f.read()
            except IOError:
                old_prefix_info = ""

            if old_ver != CURRENT_PREFIX_VERSION or old_prefix_info != prefix_info:
                # update builtin dll symlinks or copies
                self.update_builtin_libs(builtin_dll_copy)

                with open(self.config_info_file, "w") as f:
                    f.write(prefix_info)

            with open(self.version_file, "w") as f:
                f.write(CURRENT_PREFIX_VERSION + "\n")

            #create font files symlinks
            self.create_fonts_symlinks()

            with open(self.tracked_files_file, "a") as tracked_files:
                #copy steam files into place
                steam_dir = "drive_c/Program Files (x86)/Steam/"
                makedirs(self.prefix_dir + steam_dir)
                filestocopy = [("steamclient.dll", "steamclient.dll"),
                               ("steamclient64.dll", "steamclient64.dll"),
                               ("GameOverlayRenderer64.dll", "GameOverlayRenderer64.dll"),
                               ("SteamService.exe", "steam.exe"),
                               ("Steam.dll", "Steam.dll")]
                for (src,tgt) in filestocopy:
                    srcfile = steamdir + '/legacycompat/' + src
                    if os.path.isfile(srcfile):
                        try_copy(srcfile, steam_dir + tgt, prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                filestocopy = [("steamclient64.dll", "steamclient64.dll"),
                               ("GameOverlayRenderer.dll", "GameOverlayRenderer.dll"),
                               ("GameOverlayRenderer64.dll", "GameOverlayRenderer64.dll")]
                for (src,tgt) in filestocopy:
                    srcfile = g_proton.path(src)
                    if os.path.isfile(srcfile):
                        try_copy(srcfile, steam_dir + tgt, prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                #copy openvr files into place
                makedirs(self.prefix_dir + "/drive_c/vrclient/bin")
                try_copy(g_proton.lib_dir + "wine/i386-windows/vrclient.dll", "drive_c/vrclient/bin",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                try_copy(g_proton.lib64_dir + "wine/x86_64-windows/vrclient_x64.dll", "drive_c/vrclient/bin",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                try_copy(g_proton.lib_dir + "wine/dxvk/openvr_api_dxvk.dll", "drive_c/windows/syswow64",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                try_copy(g_proton.lib64_dir + "wine/dxvk/openvr_api_dxvk.dll", "drive_c/windows/system32",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                makedirs(self.prefix_dir + "/drive_c/openxr")
                try_copy(g_proton.default_pfx_dir + "drive_c/openxr/wineopenxr64.json", "drive_c/openxr",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                #copy vkd3d files into place
                try_copy(g_proton.lib64_dir + "vkd3d/libvkd3d-shader-1.dll", "drive_c/windows/system32",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                try_copy(g_proton.lib_dir + "vkd3d/libvkd3d-shader-1.dll", "drive_c/windows/syswow64",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                if use_wined3d:
                    dxvkfiles = []
                    wined3dfiles = ["d3d11", "d3d10", "d3d10core", "d3d10_1", "d3d9"]
                else:
                    dxvkfiles = ["d3d11", "d3d10core", "d3d9"]
                    wined3dfiles = []

                if use_dxvk_dxgi:
                    dxvkfiles.append("dxgi")
                else:
                    wined3dfiles.append("dxgi")

                for f in wined3dfiles:
                    try_copy(g_proton.default_pfx_dir + "drive_c/windows/system32/" + f + ".dll", "drive_c/windows/system32",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                    try_copy(g_proton.default_pfx_dir + "drive_c/windows/syswow64/" + f + ".dll", "drive_c/windows/syswow64",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                for f in dxvkfiles:
                    try_copy(g_proton.lib64_dir + "wine/dxvk/" + f + ".dll", "drive_c/windows/system32",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                    try_copy(g_proton.lib_dir + "wine/dxvk/" + f + ".dll", "drive_c/windows/syswow64",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                    g_session.dlloverrides[f] = "n"

                # If the user requested the NVAPI be available, copy it into place.
                # If they didn't, clean up any stray nvapi DLLs.
                if use_nvapi:
                    try_copy(g_proton.lib64_dir + "wine/nvapi/nvapi64.dll", "drive_c/windows/system32",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                    try_copy(g_proton.lib_dir + "wine/nvapi/nvapi.dll", "drive_c/windows/syswow64",
                            prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                    g_session.dlloverrides["nvapi64"] = "n"
                    g_session.dlloverrides["nvapi"] = "n"
                    g_session.dlloverrides["nvcuda"] = "b"
                else:
                    nvapi64_dll = self.prefix_dir + "drive_c/windows/system32/nvapi64.dll"
                    nvapi32_dll = self.prefix_dir + "drive_c/windows/syswow64/nvapi.dll"
                    if file_exists(nvapi64_dll, follow_symlinks=False):
                        os.unlink(nvapi64_dll)
                    if file_exists(nvapi64_dll + '.debug', follow_symlinks=False):
                        os.unlink(nvapi64_dll + '.debug')
                    if file_exists(nvapi32_dll, follow_symlinks=False):
                        os.unlink(nvapi32_dll)
                    if file_exists(nvapi32_dll + '.debug', follow_symlinks=False):
                        os.unlink(nvapi32_dll + '.debug')

                # Try to detect known DLLs that ship with the NVIDIA Linux Driver
                # and add them into the prefix
                nvidia_wine_dll_dir = find_nvidia_wine_dll_dir()
                if nvidia_wine_dll_dir:
                    for dll in ["_nvngx.dll", "nvngx.dll"]:
                        try_copy(nvidia_wine_dll_dir + "/" + dll, "drive_c/windows/system32", optional=True,
                                 prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

                try_copy(g_proton.lib64_dir + "wine/vkd3d-proton/d3d12.dll", "drive_c/windows/system32",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)
                try_copy(g_proton.lib_dir + "wine/vkd3d-proton/d3d12.dll", "drive_c/windows/syswow64",
                        prefix=self.prefix_dir, track_file=tracked_files, link_debug=True)

            setup_game_dir_drive()
            setup_steam_dir_drive()

            # add Steam ffmpeg libraries to path
            use_ffmpeg = "PROTON_NO_STEAM_FFMPEG" not in os.environ or not nonzero(os.environ["PROTON_NO_STEAM_FFMPEG"])
            if use_ffmpeg and 'nosteamffmpeg' not in g_session.compat_config:
                prepend_to_env_str(g_session.env, ld_path_var, steamdir + "/ubuntu12_64/video/:" + steamdir + "/ubuntu12_32/video/", ":")

def comma_escaped(s):
    escaped = False
    idx = -1
    while s[idx] == '\\':
        escaped = not escaped
        idx = idx - 1
    return escaped

#hopefully short-lived, app-specific workarounds for Proton bugs
def default_compat_config():
    ret = set()
    if "SteamAppId" in os.environ:
        appid = os.environ["SteamAppId"]
        if appid in [
                #affected by CW bug 19126
                "536280", #Disintegration
                "563840", #LOST EMBER
                "707030", #POSTAL 4: No Regerts
                "1102190", #Monster Train
                "1110100", #Power Rangers: Battle for the Grid
                "1311070", #Greak: Memories of Azur
                "1331440", #FUSER
                "1359980", #POSTAL: Brain Damaged
                "1361320", #The Room 4: Old Sins
                "1569550", #Dread X Collection: The Hunt
                "1642370", #Terra Nil Demo
                "1766430", #POSTAL Brain Damaged Demo
                "1913910", #Nine Sols Demo

                #affected by CW bug 19741
                "1017900", #Age of Empires: Definitive Edition
                ]:
            ret.add("nomfdxgiman")

        if appid in [
                # OPWR may be causing text input delays in login windows in these games on Wayland due to
                # blit happening before presentation
                "1172620", #Sea of Thieves
                "962130", #Grounded
                "495420", #State of Decay 2: Juggernaut Edition
                "976730", #Halo: The Master Chief Collection
                "1017900", #Age of Empires: Definitive Edition
                "1056090", #Ori and the Will of the Wisps
                "1293830", #Forza Horizon 4
                "1551360", #Forza Horizon 5
                "271590", #Grand Theft Auto V
                "5699", #Grand Theft Auto V Premium Edition
                "1174180", #Red Dead Redemption 2
                "1404210", #Red Dead Online
                "12210", #Grand Theft Auto IV: Complete Edition
                "204100", #Max Payne 3
                "110800", #L.A. Noire
                "12200", #Bully: Scholarship Edition
                "12120", #Grand Theft Auto: San Andreas
                "12110", #Grand Theft Auto: Vice City
                "12100", #Grand Theft Auto III
                "722230", #L.A. Noire: The VR Case Files
                "813780", #Age of Empires II: Definitive Edition
                "933110", #Age of Empires III: Definitive Edition
                "1466860", #Age of Empires IV
                "1097840", #Gears 5
                "1244950", #Battletoads
                "1189800", #Bleeding Edge
                "1184050", #Gears Tactics
                "1240440", #Halo Infinite
                "1250410", #Microsoft Flight Simulator
                "1672970", #Minecraft Dungeons
                "1180660", #Tell Me Why
                "1238430", #Tell Me Why Chapter 2
                "1266670", #Tell Me Why Chapter 3
                # Other issues arising from OWPR code path in apps, e. g., hitting unimplemented bits in
                # d3dcompiler.
                "230410", #Warframe
                ]:
            ret.add("noopwr")

    return ret

class Session:
    def __init__(self):
        self.log_file = None
        self.env = dict(os.environ)
        self.dlloverrides = {
                "steam.exe": "b", #always use our special built-in steam.exe
                "dotnetfx35.exe": "b", #replace the broken installer, as does Windows
                "dotnetfx35setup.exe": "b",
                "beclient.dll": "b,n",
                "beclient_x64.dll": "b,n",
        }

        self.compat_config = default_compat_config()
        self.cmdlineappend = []

        if "STEAM_COMPAT_CONFIG" in os.environ:
            config = os.environ["STEAM_COMPAT_CONFIG"]

            while config:
                (cur, sep, config) = config.partition(',')
                if cur.startswith("cmdlineappend:"):
                    while comma_escaped(cur):
                        (a, b, c) = config.partition(',')
                        cur = cur[:-1] + ',' + a
                        config = c
                    self.cmdlineappend.append(cur[14:].replace('\\\\','\\'))
                else:
                    self.compat_config.add(cur)

        #turn forcelgadd on by default unless it is disabled in compat config
        if not "noforcelgadd" in self.compat_config:
            self.compat_config.add("forcelgadd")

    def init_wine(self):
        if "HOST_LC_ALL" in self.env and len(self.env["HOST_LC_ALL"]) > 0:
            #steam sets LC_ALL=C to help some games, but Wine requires the real value
            #in order to do path conversion between win32 and host. steam sets
            #HOST_LC_ALL to allow us to use the real value.
            self.env["LC_ALL"] = self.env["HOST_LC_ALL"]
        else:
            self.env.pop("LC_ALL", "")

        self.env.pop("WINEARCH", "")

        if 'ORIG_'+ld_path_var not in os.environ:
            # Allow wine to restore this when calling an external app.
            self.env['ORIG_'+ld_path_var] = os.environ.get(ld_path_var, '')

        prepend_to_env_str(self.env, ld_path_var, g_proton.lib64_dir + ":" + g_proton.lib_dir, ":")

        self.env["WINEDLLPATH"] = g_proton.lib64_dir + "/wine:" + g_proton.lib_dir + "/wine"

        self.env["GST_PLUGIN_SYSTEM_PATH_1_0"] = g_proton.lib64_dir + "gstreamer-1.0" + ":" + g_proton.lib_dir + "gstreamer-1.0"
        self.env["WINE_GST_REGISTRY_DIR"] = g_compatdata.path("gstreamer-1.0/")

        if "STEAM_COMPAT_MEDIA_PATH" in os.environ:
            old_audiofoz_path = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/audio.foz"
            if file_exists(old_audiofoz_path, follow_symlinks=False):
                os.remove(old_audiofoz_path)
            self.env["MEDIACONV_AUDIO_DUMP_FILE"] = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/audiov2.foz"
            self.env["MEDIACONV_VIDEO_DUMP_FILE"] = os.environ["STEAM_COMPAT_MEDIA_PATH"] + "/video.foz"

        if "STEAM_COMPAT_TRANSCODED_MEDIA_PATH" in os.environ:
            self.env["MEDIACONV_AUDIO_TRANSCODED_FILE"] = os.environ["STEAM_COMPAT_TRANSCODED_MEDIA_PATH"] + "/transcoded_audio.foz"
            self.env["MEDIACONV_VIDEO_TRANSCODED_FILE"] = os.environ["STEAM_COMPAT_TRANSCODED_MEDIA_PATH"] + "/transcoded_video.foz"

        prepend_to_env_str(self.env, "PATH", g_proton.bin_dir, ":")

    def check_environment(self, env_name, config_name):
        if not env_name in self.env:
            return False
        if nonzero(self.env[env_name]):
            self.compat_config.add(config_name)
        else:
            self.compat_config.discard(config_name)
        return True

    def try_log_slr_versions(self):
        try:
            if "PRESSURE_VESSEL_RUNTIME_BASE" in self.env:
                with open(self.env["PRESSURE_VESSEL_RUNTIME_BASE"] + "/VERSIONS.txt", "r") as f:
                    for l in f:
                        l = l.strip()
                        if len(l) > 0 and not l.startswith("#"):
                            cleaned = l.split("#")[0].strip().replace("\t", " ")
                            split = cleaned.split(" ", maxsplit=1)
                            self.log_file.write(split[0] + ": " + split[1] + "\n")
        except (OSError, IOError, TypeError, KeyError):
            pass

    def setup_logging(self, *, append_forever):
        basedir = self.env.get("PROTON_LOG_DIR", os.environ["HOME"])

        if append_forever:
            #SteamGameId is not always available
            lfile_path = basedir + "/steam-proton.log"
        else:
            if not "SteamGameId" in os.environ:
                return False

            lfile_path = basedir + "/steam-" + os.environ["SteamGameId"] + ".log"

            if file_exists(lfile_path, follow_symlinks=False):
                os.remove(lfile_path)

        makedirs(basedir)
        self.log_file = open(lfile_path, "a")
        return True

    def init_session(self, update_prefix_files):
        self.env["WINEPREFIX"] = g_compatdata.prefix_dir

        #load environment overrides
        used_user_settings = {}
        if file_exists(g_proton.user_settings_file, follow_symlinks=True):
            try:
                import user_settings
                for key, value in user_settings.user_settings.items():
                    if not key in self.env:
                        self.env[key] = value
                        used_user_settings[key] = value
            except:
                log("************************************************")
                log("THERE IS AN ERROR IN YOUR user_settings.py FILE:")
                log("%s" % sys.exc_info()[1])
                log("************************************************")

        if "PROTON_LOG" in self.env and nonzero(self.env["PROTON_LOG"]):
            self.env.setdefault("WINEDEBUG", "+timestamp,+pid,+tid,+seh,+unwind,+debugstr,+loaddll,+mscoree")
            self.env.setdefault("DXVK_LOG_LEVEL", "info")
            self.env.setdefault("VKD3D_DEBUG", "warn")
            self.env.setdefault("VKD3D_SHADER_DEBUG", "fixme")
            self.env.setdefault("WINE_MONO_TRACE", "E:System.NotImplementedException")

            if self.env["PROTON_LOG"] != "1":
                append_to_env_str(self.env, "WINEDEBUG", self.env["PROTON_LOG"], ",")

        #for performance, logging is disabled by default; override with user_settings.py
        self.env.setdefault("WINEDEBUG", "-all")
        self.env.setdefault("DXVK_LOG_LEVEL", "none")
        self.env.setdefault("VKD3D_DEBUG", "none")
        self.env.setdefault("VKD3D_SHADER_DEBUG", "none")

        #disable XIM support until libx11 >= 1.7 is widespread
        self.env.setdefault("WINE_ALLOW_XIM", "0")

        if "wined3d11" in self.compat_config:
            self.compat_config.add("wined3d")

        if not self.check_environment("PROTON_USE_WINED3D", "wined3d"):
            self.check_environment("PROTON_USE_WINED3D11", "wined3d")
        self.check_environment("PROTON_NO_D3D11", "nod3d11")
        self.check_environment("PROTON_NO_D3D10", "nod3d10")
        self.check_environment("PROTON_NO_ESYNC", "noesync")
        self.check_environment("PROTON_NO_FSYNC", "nofsync")
        self.check_environment("PROTON_FORCE_LARGE_ADDRESS_AWARE", "forcelgadd")
        self.check_environment("PROTON_OLD_GL_STRING", "oldglstr")
        self.check_environment("PROTON_NO_WRITE_WATCH", "nowritewatch")
        self.check_environment("PROTON_HIDE_NVIDIA_GPU", "hidenvgpu")
        self.check_environment("PROTON_SET_GAME_DRIVE", "gamedrive")
        self.check_environment("PROTON_SET_STEAM_DRIVE", "steamdrive")
        self.check_environment("PROTON_NO_XIM", "noxim")
        self.check_environment("PROTON_HEAP_DELAY_FREE", "heapdelayfree")
        self.check_environment("PROTON_ENABLE_NVAPI", "enablenvapi")

        if "noesync" in self.compat_config:
            self.env.pop("WINEESYNC", "")
        else:
            self.env["WINEESYNC"] = "1"

        if not "noxim" in self.compat_config:
            self.env.pop("WINE_ALLOW_XIM")

        if "nofsync" in self.compat_config:
            self.env.pop("WINEFSYNC", "")
        else:
            self.env["WINEFSYNC"] = "1"

        if "nowritewatch" in self.compat_config:
            self.env["WINE_DISABLE_WRITE_WATCH"] = "1"

        if "oldglstr" in self.compat_config:
            #mesa override
            self.env["MESA_EXTENSION_MAX_YEAR"] = "2003"
            #nvidia override
            self.env["__GL_ExtensionStringVersion"] = "17700"

        if "forcelgadd" in self.compat_config:
            self.env["WINE_LARGE_ADDRESS_AWARE"] = "1"

        if "heapdelayfree" in self.compat_config:
            self.env["WINE_HEAP_DELAY_FREE"] = "1"

        if "vkd3dbindlesstb" in self.compat_config:
            append_to_env_str(self.env, "VKD3D_CONFIG", "force_bindless_texel_buffer", ",")

        if "vkd3dfl12" in self.compat_config:
            if not "VKD3D_FEATURE_LEVEL" in self.env:
                self.env["VKD3D_FEATURE_LEVEL"] = "12_0"

        if "hidenvgpu" in self.compat_config:
            self.env["WINE_HIDE_NVIDIA_GPU"] = "1"

        if "usenativexinput13" in self.compat_config:
            self.dlloverrides["xinput1_3"] = "n"

        if "disablelibglesv2" in self.compat_config:
            self.dlloverrides["libglesv2"] = "d"

        if "nomfdxgiman" in self.compat_config:
            self.env["WINE_DO_NOT_CREATE_DXGI_DEVICE_MANAGER"] = "1"

        if "noopwr" in self.compat_config:
            self.env["WINE_DISABLE_VULKAN_OPWR"] = "1"

        if "PROTON_CRASH_REPORT_DIR" in self.env:
            self.env["WINE_CRASH_REPORT_DIR"] = self.env["PROTON_CRASH_REPORT_DIR"]

        if self.env["WINEDEBUG"] != "-all":
            if self.setup_logging(append_forever=False):
                self.log_file.write("======================\n")
                with open(g_proton.version_file, "r") as f:
                    self.log_file.write("Proton: " + f.readline().strip() + "\n")
                if "SteamGameId" in self.env:
                    self.log_file.write("SteamGameId: " + self.env["SteamGameId"] + "\n")
                self.log_file.write("Command: " + str(sys.argv[2:] + self.cmdlineappend) + "\n")
                self.log_file.write("Options: " + str(self.compat_config) + "\n")

                self.try_log_slr_versions()

                try:
                    uname = os.uname()
                    kernel_version = f"{uname.sysname} {uname.release} {uname.version} {uname.machine}"
                except OSError:
                    kernel_version = "unknown"

                self.log_file.write(f"Kernel: {kernel_version}\n")

                #dump some important variables into the log header
                for var in ["WINEDLLOVERRIDES", "WINEDEBUG"]:
                    if var in os.environ:
                        self.log_file.write("System " + var + ": " + os.environ[var] + "\n")
                    if var in used_user_settings:
                        self.log_file.write("User settings " + var + ": " + used_user_settings[var] + "\n")

                self.log_file.write("======================\n")
                self.log_file.flush()
            else:
                self.env["WINEDEBUG"] = "-all"

        if "PROTON_REMOTE_DEBUG_CMD" in self.env:
            self.remote_debug_cmd = shlex.split(self.env.get("PROTON_REMOTE_DEBUG_CMD"))
        else:
            self.remote_debug_cmd = None

        if update_prefix_files:
            g_compatdata.setup_prefix()

        if "nod3d11" in self.compat_config:
            self.dlloverrides["d3d11"] = ""
            if "dxgi" in self.dlloverrides:
                del self.dlloverrides["dxgi"]

        if "nod3d10" in self.compat_config:
            self.dlloverrides["d3d10_1"] = ""
            self.dlloverrides["d3d10"] = ""
            self.dlloverrides["dxgi"] = ""

        if "nativevulkanloader" in self.compat_config:
            self.dlloverrides["vulkan-1"] = "n"

        if "enablenvapi" in self.compat_config:
            self.env["DXVK_ENABLE_NVAPI"] = "1"

        s = ""
        for dll in self.dlloverrides:
            setting = self.dlloverrides[dll]
            if len(s) > 0:
                s = s + ";" + dll + "=" + setting
            else:
                s = dll + "=" + setting
        append_to_env_str(self.env, "WINEDLLOVERRIDES", s, ";")

    def dump_dbg_env(self, f):
        f.write("PATH=\"" + self.env["PATH"] + "\" \\\n")
        f.write("\tTERM=\"xterm\" \\\n") #XXX
        f.write("\tWINEDEBUG=\"-all\" \\\n")
        f.write("\tWINEDLLPATH=\"" + self.env["WINEDLLPATH"] + "\" \\\n")
        f.write("\t" + ld_path_var + "=\"" + self.env[ld_path_var] + "\" \\\n")
        f.write("\tWINEPREFIX=\"" + self.env["WINEPREFIX"] + "\" \\\n")
        if "WINEESYNC" in self.env:
            f.write("\tWINEESYNC=\"" + self.env["WINEESYNC"] + "\" \\\n")
        if "WINEFSYNC" in self.env:
            f.write("\tWINEFSYNC=\"" + self.env["WINEFSYNC"] + "\" \\\n")
        if "SteamGameId" in self.env:
            f.write("\tSteamGameId=\"" + self.env["SteamGameId"] + "\" \\\n")
        if "SteamAppId" in self.env:
            f.write("\tSteamAppId=\"" + self.env["SteamAppId"] + "\" \\\n")
        if "WINEDLLOVERRIDES" in self.env:
            f.write("\tWINEDLLOVERRIDES=\"" + self.env["WINEDLLOVERRIDES"] + "\" \\\n")
        if "STEAM_COMPAT_CLIENT_INSTALL_PATH" in self.env:
            f.write("\tSTEAM_COMPAT_CLIENT_INSTALL_PATH=\"" + self.env["STEAM_COMPAT_CLIENT_INSTALL_PATH"] + "\" \\\n")
        if "WINE_LARGE_ADDRESS_AWARE" in self.env:
            f.write("\tWINE_LARGE_ADDRESS_AWARE=\"" + self.env["WINE_LARGE_ADDRESS_AWARE"] + "\" \\\n")
        if "GST_PLUGIN_SYSTEM_PATH_1_0" in self.env:
            f.write("\tGST_PLUGIN_SYSTEM_PATH_1_0=\"" + self.env["GST_PLUGIN_SYSTEM_PATH_1_0"] + "\" \\\n")
        if "WINE_GST_REGISTRY_DIR" in self.env:
            f.write("\tWINE_GST_REGISTRY_DIR=\"" + self.env["WINE_GST_REGISTRY_DIR"] + "\" \\\n")
        if "MEDIACONV_AUDIO_DUMP_FILE" in self.env:
            f.write("\tMEDIACONV_AUDIO_DUMP_FILE=\"" + self.env["MEDIACONV_AUDIO_DUMP_FILE"] + "\" \\\n")
        if "MEDIACONV_AUDIO_TRANSCODED_FILE" in self.env:
            f.write("\tMEDIACONV_AUDIO_TRANSCODED_FILE=\"" + self.env["MEDIACONV_AUDIO_TRANSCODED_FILE"] + "\" \\\n")
        if "MEDIACONV_VIDEO_DUMP_FILE" in self.env:
            f.write("\tMEDIACONV_VIDEO_DUMP_FILE=\"" + self.env["MEDIACONV_VIDEO_DUMP_FILE"] + "\" \\\n")
        if "MEDIACONV_VIDEO_TRANSCODED_FILE" in self.env:
            f.write("\tMEDIACONV_VIDEO_TRANSCODED_FILE=\"" + self.env["MEDIACONV_VIDEO_TRANSCODED_FILE"] + "\" \\\n")

    def dump_dbg_scripts(self):
        exe_name = os.path.basename(sys.argv[2])

        tmpdir = self.env.get("PROTON_DEBUG_DIR", "/tmp") + "/proton_" + os.environ["USER"] + "/"
        makedirs(tmpdir)

        with open(tmpdir + "winedbg", "w") as f:
            f.write("#!/bin/bash\n")
            f.write("#Run winedbg with args\n\n")
            f.write("cd \"" + os.getcwd() + "\"\n")
            self.dump_dbg_env(f)
            f.write("\t\"" + g_proton.wine_bin + "\" winedbg \"$@\"\n")
        os.chmod(tmpdir + "winedbg", 0o755)

        with open(tmpdir + "winedbg_run", "w") as f:
            f.write("#!/bin/bash\n")
            f.write("#Run winedbg and prepare to run game or given program\n\n")
            f.write("cd \"" + os.getcwd() + "\"\n")
            f.write("DEF_CMD=(")
            first = True
            for arg in sys.argv[2:]:
                if first:
                    f.write("\"" + arg + "\"")
                    first = False
                else:
                    f.write(" \"" + arg + "\"")
            f.write(")\n")
            self.dump_dbg_env(f)
            f.write("\t\"" + g_proton.wine_bin + "\" winedbg \"${@:-${DEF_CMD[@]}}\"\n")
        os.chmod(tmpdir + "winedbg_run", 0o755)

        with open(tmpdir + "gdb_attach", "w") as f:
            f.write("#!/bin/bash\n")
            f.write("#Run winedbg in gdb mode and auto-attach to already-running program\n\n")
            f.write("cd \"" + os.getcwd() + "\"\n")
            f.write("EXE_NAME=${1:-\"" + exe_name + "\"}\n")
            f.write("WPID_HEX=$(\"" + tmpdir + "winedbg\" --command 'info process' | grep -i \"$EXE_NAME\" | cut -f2 -d' ' | sed -e 's/^0*//')\n")
            f.write("if [ -z \"$WPID_HEX\" ]; then \n")
            f.write("    echo \"Program does not appear to be running: \\\"$EXE_NAME\\\"\"\n")
            f.write("    exit 1\n")
            f.write("fi\n")
            f.write("WPID_DEC=$(printf %d 0x$WPID_HEX)\n")
            self.dump_dbg_env(f)
            f.write("\t\"" + g_proton.wine_bin + "\" winedbg --gdb $WPID_DEC\n")
        os.chmod(tmpdir + "gdb_attach", 0o755)

        with open(tmpdir + "gdb_run", "w") as f:
            f.write("#!/bin/bash\n")
            f.write("#Run winedbg in gdb mode and prepare to run game or given program\n\n")
            f.write("cd \"" + os.getcwd() + "\"\n")
            f.write("DEF_CMD=(")
            first = True
            for arg in sys.argv[2:]:
                if first:
                    f.write("\"" + arg + "\"")
                    first = False
                else:
                    f.write(" \"" + arg + "\"")
            f.write(")\n")
            self.dump_dbg_env(f)
            f.write("\t\"" + g_proton.wine_bin + "\" winedbg --gdb \"${@:-${DEF_CMD[@]}}\"\n")
        os.chmod(tmpdir + "gdb_run", 0o755)

        with open(tmpdir + "run", "w") as f:
            f.write("#!/bin/bash\n")
            f.write("#Run game or given command in environment\n\n")
            f.write("cd \"" + os.getcwd() + "\"\n")
            f.write("DEF_CMD=(")
            first = True
            for arg in sys.argv[2:]:
                if first:
                    f.write("\"" + arg + "\"")
                    first = False
                else:
                    f.write(" \"" + arg + "\"")
            f.write(")\n")
            self.dump_dbg_env(f)
            f.write("\t\"" + g_proton.wine64_bin + "\" c:\\\\windows\\\\system32\\\\steam.exe \"${@:-${DEF_CMD[@]}}\"\n")
        os.chmod(tmpdir + "run", 0o755)

    def run_proc(self, args, local_env=None):
        if local_env is None:
            local_env = self.env
        return subprocess.call(args, env=local_env, stderr=self.log_file, stdout=self.log_file)

    def run(self):
        if shutil.which('steam-runtime-launcher-interface-0') is not None:
            adverb = ['steam-runtime-launcher-interface-0', 'proton']
        else:
            adverb = []

        if "PROTON_DUMP_DEBUG_COMMANDS" in self.env and nonzero(self.env["PROTON_DUMP_DEBUG_COMMANDS"]):
            try:
                self.dump_dbg_scripts()
            except OSError:
                log("Unable to write debug scripts! " + str(sys.exc_info()[1]))

        if self.remote_debug_cmd:
            remote_debug_cmd = self.remote_debug_cmd
            if not os.path.isabs(remote_debug_cmd[0]):
                remote_debug_cmd[0] = g_proton.path(remote_debug_cmd[0])
            remote_debug_proc = subprocess.Popen([g_proton.wine_bin] + self.remote_debug_cmd,
                                                 env=self.env, stderr=self.log_file, stdout=self.log_file)
        else:
            remote_debug_proc = None

        # CoD: Black Ops 3 workaround
        if os.environ.get("SteamGameId", 0) == "311210":
            argv = [g_proton.wine_bin, "c:\\Program Files (x86)\\Steam\\steam.exe"]
        else:
            argv = [g_proton.wine64_bin, "c:\\windows\\system32\\steam.exe"]

        rc = self.run_proc(adverb + argv + sys.argv[2:] + self.cmdlineappend)

        if remote_debug_proc:
            remote_debug_proc.kill()
            try:
                remote_debug_proc.communicate(2)
            except subprocess.TimeoutExpired as e:
                log("terminate remote debugger")
                remote_debug_proc.terminate()
                remote_debug_proc.communicate()

        return rc

if __name__ == "__main__":
    if not "STEAM_COMPAT_DATA_PATH" in os.environ:
        log("No compat data path?")
        sys.exit(1)

    g_proton = Proton(os.path.dirname(sys.argv[0]))

    if g_proton.need_tarball_extraction():
        g_proton.extract_tarball()

    g_compatdata = CompatData(os.environ["STEAM_COMPAT_DATA_PATH"])

    g_session = Session()

    g_session.init_wine()

    if g_proton.missing_default_prefix():
        g_proton.make_default_prefix()

    g_session.init_session(sys.argv[1] != "runinprefix")

    #determine mode
    rc = 0
    if sys.argv[1] == "run":
        #start target app
        setup_game_dir_drive()
        setup_steam_dir_drive()
        rc = g_session.run()
    elif sys.argv[1] == "waitforexitandrun":
        #wait for wineserver to shut down
        g_session.run_proc([g_proton.wineserver_bin, "-w"])
        #then run
        rc = g_session.run()
    elif sys.argv[1] == "runinprefix":
        rc = g_session.run_proc([g_proton.wine_bin] + sys.argv[2:])
    elif sys.argv[1] == "destroyprefix":
        g_compatdata.remove_tracked_files()
    elif sys.argv[1] == "getcompatpath":
        #linux -> windows path
        path = subprocess.check_output([g_proton.wine_bin, "winepath", "-w", sys.argv[2]], env=g_session.env, stderr=g_session.log_file)
        sys.stdout.buffer.write(path)
    elif sys.argv[1] == "getnativepath":
        #windows -> linux path
        path = subprocess.check_output([g_proton.wine_bin, "winepath", sys.argv[2]], env=g_session.env, stderr=g_session.log_file)
        sys.stdout.buffer.write(path)
    else:
        log("Need a verb.")
        sys.exit(1)

    sys.exit(rc)

#pylint --disable=C0301,C0326,C0330,C0111,C0103,R0902,C1801,R0914,R0912,R0915
# vim: set syntax=python: