ProjectArcade

Форк
0
140 строк · 4.2 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Runtime.InteropServices;
6
using System.Runtime.InteropServices.ComTypes;
7
using System.Management;
8
using System.Diagnostics;
9
using System.Globalization;
10
using System.IO;
11

12
namespace EmulatorLauncher.Common.Compression
13
{
14
    public class MountFile : IDisposable
15
    {
16
        public static MountFile Mount(string filename, string extractionpath, string overlayPath)
17
        {
18
            if (!Zip.IsCompressedFile(filename))
19
                return null;
20

21
            string mountPath = Path.Combine(Path.GetDirectoryName(typeof(MountFile).Assembly.Location), "mount.exe");
22
            if (!File.Exists(mountPath))
23
                return null;
24

25
            string dokan = Environment.GetEnvironmentVariable("DokanLibrary2");
26
            if (!Directory.Exists(dokan))
27
                return null;
28

29
            dokan = Path.Combine(dokan, "dokan2.dll");
30
            if (!File.Exists(dokan))
31
                return null;
32

33
            var drive = FileTools.FindFreeDriveLetter();
34
            if (drive == null)
35
                return null;
36

37
            if (!Zip.IsFreeDiskSpaceAvailableForExtraction(filename, extractionpath))
38
                return null;
39

40
            if (!Directory.Exists(extractionpath))
41
            {
42
                Directory.CreateDirectory(extractionpath);
43
                FileTools.CompressDirectory(extractionpath);
44
            }
45
         
46
            List<string> args = new List<string>();      
47
    
48
            if (Debugger.IsAttached)
49
                args.Add("-debug");
50

51
            args.Add("-drive");
52
            args.Add(drive.Substring(0, 2));
53

54
            if (!string.IsNullOrEmpty(extractionpath))
55
            {
56
             //   extractionpath = Path.Combine(extractionpath, Path.GetFileName(filename));
57

58
                args.Add("-extractionpath");
59
                args.Add("\"" + extractionpath + "\"");
60

61
                Directory.CreateDirectory(extractionpath);
62
            }
63

64
            if (!string.IsNullOrEmpty(overlayPath))
65
            {
66
                args.Add("-overlay");
67
                args.Add("\"" + overlayPath + "\"");
68

69
                Directory.CreateDirectory(overlayPath);
70
            }
71

72
            args.Add("\"" + filename + "\"");
73

74
            var mountProcess = Process.Start(new ProcessStartInfo()
75
            {
76
                FileName = mountPath,
77
                WorkingDirectory = Path.GetDirectoryName(filename),
78
                Arguments = string.Join(" ", args.ToArray()),
79
                UseShellExecute = false,
80
                CreateNoWindow = !Debugger.IsAttached
81
            });
82

83
            int time = Environment.TickCount;
84
            int elapsed = 0;
85

86
            while (elapsed < 5000)
87
            {
88
                if (mountProcess.WaitForExit(10))
89
                    return null;
90

91
                if (Directory.Exists(drive))
92
                {
93
                    Job.Current.AddProcess(mountProcess);
94
                    return new MountFile(mountProcess, filename, drive, extractionpath, overlayPath);
95
                }
96

97
                int newTime = Environment.TickCount;
98
                elapsed = time - newTime;
99
                time = newTime;
100
            }
101

102
            try { mountProcess.Kill(); }
103
            catch { }
104

105
            return null;
106
        }
107

108
        private MountFile(Process process, string filename, string driveLetter, string extractionpath, string overlayPath)
109
        {
110
            Filename = filename;
111
            DriveLetter = driveLetter;
112
            ExtractionPath = extractionpath;
113
            OverlayPath = overlayPath;
114
            Process = process;
115
        }
116

117
        public string Filename { get; private set; }
118
        public string DriveLetter { get; private set; }
119
        public string ExtractionPath { get; private set; }
120
        public string OverlayPath { get; private set; }
121

122
        public Process Process { get; private set; }
123

124
        public void UnMount()
125
        {
126
            try { Process.Kill(); }
127
            catch { }
128
        }
129

130
        public void Dispose()
131
        {
132
            UnMount();
133
        }
134

135
        public static void ShowDownloadDokanPage()
136
        {
137
            Process.Start("https://github.com/dokan-dev/dokany/releases");
138
        }
139
    }
140
}
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.