markus / MarkusAutoUpdate / src / MarkusUpdateExtract_Net / Program.cs @ 63946dd5
이력 | 보기 | 이력해설 | 다운로드 (6.7 KB)
1 | d8f5045e | taeseongkim | using System; |
---|---|---|---|
2 | using System.IO; |
||
3 | using System.IO.Compression; |
||
4 | using System.Diagnostics; |
||
5 | using System.Linq; |
||
6 | using System.Security.AccessControl; |
||
7 | using System.IO.MemoryMappedFiles; |
||
8 | using System.Text; |
||
9 | using IIpc; |
||
10 | using System.CodeDom; |
||
11 | using System.Runtime.InteropServices; |
||
12 | using log4net; |
||
13 | |||
14 | namespace MarkusUpdateExtract |
||
15 | { |
||
16 | class Program |
||
17 | { |
||
18 | static IIpc.WcfServer wcfServer; |
||
19 | static ILog logger = LogManager.GetLogger(typeof(Program)); |
||
20 | |||
21 | static bool IsUpdate = false; |
||
22 | [DllImport("Kernel32")] |
||
23 | private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlEventHandler handler, bool add); |
||
24 | |||
25 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683242.aspx |
||
26 | private delegate bool SetConsoleCtrlEventHandler(CtrlType sig); |
||
27 | private enum CtrlType |
||
28 | { |
||
29 | CTRL_C_EVENT = 0, |
||
30 | CTRL_BREAK_EVENT = 1, |
||
31 | CTRL_CLOSE_EVENT = 2, |
||
32 | CTRL_LOGOFF_EVENT = 5, |
||
33 | CTRL_SHUTDOWN_EVENT = 6 |
||
34 | } |
||
35 | |||
36 | [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] |
||
37 | static void Main(string[] args) |
||
38 | { |
||
39 | try |
||
40 | { |
||
41 | SetConsoleCtrlHandler(Handler, true); |
||
42 | |||
43 | } |
||
44 | catch (Exception ex) |
||
45 | { |
||
46 | logger.Error("SetConsoleCtrlHandler Error", ex); |
||
47 | } |
||
48 | |||
49 | try |
||
50 | { |
||
51 | |||
52 | logger.Info($"args {string.Join(",",args)}"); |
||
53 | |||
54 | if (args?.Length > 0 && !string.IsNullOrWhiteSpace(args[0])) |
||
55 | { |
||
56 | //args = new string[] { "extract" }; |
||
57 | |||
58 | wcfServer = new WcfServer(args[0] + "E"); |
||
59 | wcfServer.Start(); |
||
60 | |||
61 | var wcfClient = new WcfClient(args[0]+ "M"); |
||
62 | wcfClient.ProcessStart(); |
||
63 | |||
64 | wcfServer.ParamReceived += WcfServer_ParamReceived; |
||
65 | |||
66 | Console.WriteLine(wcfServer.Endpoint); |
||
67 | logger.Info($"Wcf Server Endpoint : {wcfServer.Endpoint}"); |
||
68 | //Console.ReadKey(); |
||
69 | while (!IsUpdate) |
||
70 | { |
||
71 | logger.Info($"Server State : {wcfServer.State().ToString()}"); |
||
72 | Console.WriteLine("Server State : " + wcfServer.State().ToString()); |
||
73 | System.Threading.Thread.Sleep(1000); |
||
74 | } |
||
75 | } |
||
76 | else |
||
77 | { |
||
78 | //Console.ReadKey(); |
||
79 | Environment.Exit(1); |
||
80 | } |
||
81 | } |
||
82 | catch (Exception ex) |
||
83 | { |
||
84 | Console.WriteLine(ex.ToString()); |
||
85 | Console.ReadKey(); |
||
86 | Environment.Exit(1); |
||
87 | } |
||
88 | |||
89 | } |
||
90 | |||
91 | private static bool Handler(CtrlType signal) |
||
92 | { |
||
93 | switch (signal) |
||
94 | { |
||
95 | case CtrlType.CTRL_BREAK_EVENT: |
||
96 | case CtrlType.CTRL_C_EVENT: |
||
97 | case CtrlType.CTRL_LOGOFF_EVENT: |
||
98 | case CtrlType.CTRL_SHUTDOWN_EVENT: |
||
99 | case CtrlType.CTRL_CLOSE_EVENT: |
||
100 | |||
101 | if (wcfServer != null) |
||
102 | { |
||
103 | wcfServer.Stop(); |
||
104 | logger.Info("Wcf Server Closing"); |
||
105 | } |
||
106 | |||
107 | Environment.Exit(0); |
||
108 | return false; |
||
109 | |||
110 | default: |
||
111 | return false; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | private static void WcfServer_ParamReceived(object sender, ParamReceivedEventArgs e) |
||
116 | { |
||
117 | logger.Info($"ParamReceived : {e.InstallPath} , {e.TempStoragePath} , {e.DownloadFile}, {e.KcomPath} , {e.Param}"); |
||
118 | |||
119 | ExtractFile(e.InstallPath, e.TempStoragePath, e.DownloadFile, e.KcomPath, e.Param); |
||
120 | } |
||
121 | |||
122 | static void ExtractFile(string InstallPath, string tempStoragePath, string downloadFile, string KcomPath, string Param) |
||
123 | { |
||
124 | try |
||
125 | { |
||
126 | //args = new string[] |
||
127 | //{ |
||
128 | //@"C:\Program Files\Doftech\MARKUS", |
||
129 | //@"C:\Users\kts\AppData\Local\Temp\MARKUS\fwcp21al.rqk\", |
||
130 | //@"C:\Users\kts\AppData\Local\Temp\MARKUS\fwcp21al.rqk\Markus_3.0.0.zip", |
||
131 | //@"C:\Program Files\Doftech\MARKUS\KCOM.exe", |
||
132 | //"eyJEb2N1bWVudEl0ZW1JRCI6IjMwMDAwMTY1IiwiYlBhcnRuZXIiOmZhbHNlLCJDcmVhdGVGaW5hbFBERlBlcm1pc3Npb24iOnRydWUsIk5ld0NvbW1lbnRQZXJtaXNzaW9uIjp0cnVlLCJQcm9qZWN0Tk8iOiIwMDAwMDAiLCJVc2VySUQiOiJkb2Z0ZWNoIn0=" |
||
133 | //}; |
||
134 | |||
135 | //DirectoryInfo dInfo = new DirectoryInfo(InstallPath); |
||
136 | //DirectorySecurity dSecurity = dInfo.GetAccessControl(); |
||
137 | //dSecurity.AddAccessRule(new FileSystemAccessRule(System.Security.Principal.WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)); |
||
138 | //dInfo.SetAccessControl(dSecurity); |
||
139 | |||
140 | var extractFolder = Path.Combine(tempStoragePath, "Extract"); |
||
141 | |||
142 | ZipFile.ExtractToDirectory(downloadFile, extractFolder); |
||
143 | |||
144 | foreach (var file in Directory.EnumerateFiles(extractFolder, "*.*", SearchOption.AllDirectories)) |
||
145 | { |
||
146 | var target = System.IO.Path.Combine(InstallPath, file.Replace(extractFolder, "").Substring(1)); |
||
147 | |||
148 | var dir = Path.GetDirectoryName(target); |
||
149 | |||
150 | if (!Directory.Exists(dir)) |
||
151 | { |
||
152 | Directory.CreateDirectory(dir); |
||
153 | } |
||
154 | |||
155 | File.Copy(file, target, FileExits(target)); |
||
156 | logger.Info($"Copy - {target}"); |
||
157 | Console.WriteLine($"Copy - {target}"); |
||
158 | } |
||
159 | |||
160 | Process.Start(KcomPath, Param); |
||
161 | } |
||
162 | catch (Exception ex) |
||
163 | { |
||
164 | logger.Error($"ExtractFile Error",ex); |
||
165 | |||
166 | Console.WriteLine(ex.ToString()); |
||
167 | |||
168 | if (ex.InnerException != null) |
||
169 | { |
||
170 | logger.Error($"ExtractFile Error InnerException", ex.InnerException); |
||
171 | Console.WriteLine(ex.InnerException.ToString()); |
||
172 | } |
||
173 | |||
174 | Environment.Exit(0); |
||
175 | } |
||
176 | finally |
||
177 | { |
||
178 | IsUpdate = true; |
||
179 | //Console.ReadKey(); |
||
180 | Environment.Exit(0); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private static bool FileExits(string fileName) |
||
185 | { |
||
186 | try |
||
187 | { |
||
188 | return File.Exists(fileName); |
||
189 | } |
||
190 | catch (Exception) |
||
191 | { |
||
192 | return false; |
||
193 | throw; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | } |
||
198 | } |