1 /**
2  * download dxlib lib
3  *
4  * Author: dokutoku
5  * License: CC0 Universal
6  */
7 module hello;
8 
9 
10 private static import std.algorithm;
11 private static import std.ascii;
12 private static import std.digest;
13 private static import std.digest.sha;
14 private static import std.file;
15 private static import std.getopt;
16 private static import std.net.curl;
17 private static import std.path;
18 private static import std.zip;
19 
20 struct download_info
21 {
22 	string uri;
23 	string sha512_hash;
24 
25 	private bool verify(string file_path) const
26 
27 		do
28 		{
29 			return std.file.exists(file_path) && std.file.isFile(file_path) && std.digest.secureEqual(this.sha512_hash, std.digest.toHexString!(std.ascii.LetterCase.lower)(std.digest.sha.sha512Of(std.file.read(file_path))).idup);
30 		}
31 
32 	private void download(string dir_path) const
33 
34 		in
35 		{
36 			assert(dir_path.length != 0);
37 			assert(std.path.isValidPath(dir_path));
38 		}
39 
40 		do
41 		{
42 			string file_path = std.path.buildNormalizedPath(dir_path, std.path.baseName(this.uri));
43 
44 			if (this.verify(file_path)) {
45 				return;
46 			}
47 
48 			std.net.curl.download(this.uri, file_path);
49 
50 			if (!this.verify(file_path)) {
51 				throw new Exception(`The hash value of the downloaded file is invalid.`);
52 			}
53 		}
54 
55 	void extract_lib(string dir_path, string base_dir) const
56 
57 		in
58 		{
59 			assert(std.path.extension(this.uri) == `.zip`);
60 		}
61 
62 		do
63 		{
64 			this.download(dir_path);
65 			string zip_file = std.path.baseName(this.uri);
66 			//string base_dir = std.path.buildNormalizedPath(output_dir, `lib`);
67 			std.file.mkdirRecurse(base_dir);
68 			auto zip = new std.zip.ZipArchive(std.file.read(std.path.buildNormalizedPath(dir_path, zip_file)));
69 
70 			foreach (name, am; zip.directory) {
71 				if (!std.algorithm.endsWith(name, `.a`, `.lib`)) {
72 					continue;
73 				}
74 
75 				string out_filename = std.path.buildPath(base_dir, std.path.baseName(name));
76 				std.file.write(out_filename, zip.expand(am));
77 			}
78 	}
79 }
80 
81 static immutable download_info windows_vc_info =
82 {
83 	uri: `https://dxlib.xsrv.jp/DxLib/DxLib_VC3_22a.zip`,
84 	sha512_hash: `ecf6e02163c7f40c0a7661453f2c068fc420422a658a319b96f0ca72d3c87615bb64daa85f1623aa826266cbaa2318d4b3ea46e21a5708839c688bf6a71ef8f5`,
85 };
86 
87 static immutable download_info windows_bcc_info =
88 {
89 	uri: `https://dxlib.xsrv.jp/DxLib/DxLib_BCC3_22a.zip`,
90 	sha512_hash: `2ecb3169410ed698ce3f987ff6dc0afb0eddcf5b7e41d1f43fa737bb3a1bb3b3355c76c6f1ea92cedf18bbe8bb537b506208247030c27e060e33319efb3501eb`,
91 };
92 
93 static immutable download_info windows_bcc2_info =
94 {
95 	uri: `https://dxlib.xsrv.jp/DxLib/DxLib_BCC23_22a.zip`,
96 	sha512_hash: `b682344aa8b88b95daafea96282444b36f0861ca56fe0b96d1fcb9a10aa80f0e8ae10a3c097016fcfcee321196b3b61c5c2aea6a8d0d8a276db2231789aa2930`,
97 };
98 
99 static immutable download_info windows_gcc_info =
100 {
101 	uri: `https://dxlib.xsrv.jp/DxLib/DxLib_GCC3_22a.zip`,
102 	sha512_hash: `6b625a23b6e723bf5a26d6c71ae2d48cbd79e820adeccd26096fd56e2dbaaee5e08095968d68155b62e27756b5ff43d43385740fa79d072ef0488bc901fd66dc`,
103 };
104 
105 static immutable download_info android_info =
106 {
107 	uri: `https://dxlib.xsrv.jp/DxLib_Android3_22a.zip`,
108 	sha512_hash: `cbc8c444cd43aba385ce2705a29d0f01ab1637ee01514ab2b9b4112f896b6cf069049724f653de63eb922aa8466bc4afcab1e323b9aae5492332446c5096edd8`,
109 };
110 
111 static immutable download_info ios_info =
112 {
113 	uri: `https://dxlib.xsrv.jp/DxLib/DxLib_iOS3_22a.zip`,
114 	sha512_hash: `4cc6e46e592e38ec91bcd981e0f8fa5235e6297a905281cd5f6984496f4830aa6dd4b15b8b44b0b2a4d10a12d83b05c9c9705debc64c30726139d32ea1c0892a`,
115 };
116 
117 void main(string[] argv)
118 
119 	do
120 	{
121 		string download_path = null;
122 		string output_path = null;
123 		bool is_windows = false;
124 		bool is_windows_vc = false;
125 		bool is_windows_bcc = false;
126 		bool is_windows_bcc2 = false;
127 		bool is_windows_gcc = false;
128 		bool is_android = false;
129 		bool is_ios = false;
130 		bool is_all = false;
131 
132 		auto helpInformation = std.getopt.getopt
133 		(
134 			argv,
135 			`download-path`, `download zip path.`, &download_path,
136 			`out-path`, `Output lib path.`, &output_path,
137 			`Windows`, `Download Visual Studio lib.`, &is_windows,
138 			`Windows-vc`, `Download Visual Studio lib.`, &is_windows_vc,
139 			`Windows-bcc`, `Download C++ Builder lib.`, &is_windows_bcc,
140 			`Windows-bcc2`, `Download Borland C++ Compiler lib.`, &is_windows_bcc2,
141 			`Windows-gcc`, `Download Gnu C++ lib.`, &is_windows_gcc,
142 			`Android`, `Download Android lib.`, &is_android,
143 			`iOS`, `Download iOS lib.`, &is_ios,
144 			`All`, `All download lib.`, &is_all,
145 		);
146 
147 		if (helpInformation.helpWanted) {
148 			std.getopt.defaultGetoptPrinter(`Some information about the program.`, helpInformation.options);
149 
150 			return;
151 		}
152 
153 		if ((!std.path.isValidPath(download_path)) || (!std.file.isDir(download_path))) {
154 			download_path = `./`;
155 		}
156 
157 		if ((!std.path.isValidPath(output_path)) || (!std.file.isDir(output_path))) {
158 			output_path = `./`;
159 		}
160 
161 		if ((is_all) || (is_windows) || (is_windows_vc)) {
162 			windows_vc_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `windows-vc`));
163 		}
164 
165 		if ((is_all) || (is_windows_bcc)) {
166 			windows_bcc_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `windows-bcc`));
167 		}
168 
169 		if ((is_all) || (is_windows_bcc2)) {
170 			windows_bcc2_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `windows-bcc2`));
171 		}
172 
173 		if ((is_all) || (is_windows_gcc)) {
174 			windows_gcc_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `windows-gcc`));
175 		}
176 
177 		if ((is_all) || (is_android)) {
178 			android_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `android`));
179 		}
180 
181 		if ((is_all) || (is_ios)) {
182 			ios_info.extract_lib(download_path, std.path.buildNormalizedPath(output_path, `lib`, `ios`));
183 		}
184 	}