-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
296 lines (257 loc) · 9.83 KB
/
Program.cs
File metadata and controls
296 lines (257 loc) · 9.83 KB
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
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
namespace ResourceCompiler
{
static class Program
{
private static string programFilesx86
{
get
{
if (Environment.Is64BitOperatingSystem)
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
return Environment.GetEnvironmentVariable("ProgramFiles");
}
}
private static readonly string sdkPath = Path.Combine(new string[] { programFilesx86, "Microsoft SDKs", "Windows", "v8.0A", "bin", "NETFX 4.0 Tools" });
private static readonly string resgenPath = Path.Combine(sdkPath, "ResGen.exe");
private static readonly string alPath = Path.Combine(sdkPath, "al.exe");
private static readonly string cscPath = Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "csc.exe");
private static bool ignoreEmptyDefault = false;
private static bool resourceMainAtRoot = true;
private static string resourceNamespace = "MyResources";
private static string resourcePath = "Resources";
private static string resourceSourcePath = null;
static void Main(string[] args)
{
// check paths and files.
if (!checkPaths())
return;
// get settings from the config file.
try
{
var config = System.Configuration.ConfigurationManager.AppSettings;
configTryGet(config, "ResourceNamespace", ref resourceNamespace);
configTryGet(config, "ResourcePath", ref resourcePath);
configTryGet(config, "ResourceSourcePath", ref resourceSourcePath);
configTryGet(config, "ResourceMainAtRoot", ref resourceMainAtRoot);
configTryGet(config, "IgnoreEmptyDefault", ref ignoreEmptyDefault);
}
catch (System.Configuration.ConfigurationErrorsException)
{
}
// if no resource source path spesified default to the resource path.
if (resourceSourcePath == null || resourceSourcePath.Length == 0)
resourceSourcePath = resourcePath;
Directory.CreateDirectory(resourcePath);
// Generate default resources lib.
if (!generateDefaultResourceLib() && !ignoreEmptyDefault)
{
writeFail("No default resource sources found!");
return;
}
// Generate resources for soecific cultures.
var cultureDirectories = Directory.GetDirectories(resourceSourcePath, "*", SearchOption.TopDirectoryOnly);
foreach (var cultureDirectory in cultureDirectories)
{
string name = Path.GetFileName(cultureDirectory);
try
{
// get culture
var culture = CultureInfo.GetCultureInfo(name);
var outPath = Path.Combine(resourcePath, culture.Name);
Directory.CreateDirectory(outPath);
bool empty = true;
if (generateResourceLib(cultureDirectory, outPath, resourceNamespace, culture))
empty = false;
// use culture sub directories as other assemblies.
var subDir = Directory.GetDirectories(cultureDirectory, "*", SearchOption.TopDirectoryOnly);
foreach (var dir in subDir)
if (generateResourceLib(dir, outPath, Path.GetFileName(dir), culture))
empty = false;
if (empty)
Console.WriteLine("Culture \"" + culture.Name + "\" had no resources!");
}
catch (CultureNotFoundException)
{
Console.WriteLine("Error: \"" + name + "\" is not a valid culture name!");
}
}
// end
Console.WriteLine("\nFinished.");
waitAnyKey();
}
/// <summary> Make sure we can find needed files: ResGen.exe al.exe csc.exe </summary>
private static bool checkPaths()
{
if (!Directory.Exists(sdkPath))
writeFail("Windows SDK for .Net Framework 4 was not found!\n" + sdkPath + "\nDownload from:\n" + "https://www.microsoft.com/en-us/download/details.aspx?id=8279");
else if (!File.Exists(resgenPath))
writeFail("ResGen.exe was not found in: " + resgenPath);
else if (!File.Exists(cscPath))
writeFail("ssc.exe was not found in: " + cscPath);
else if (!File.Exists(alPath))
writeFail("al.exe was not found in: " + alPath);
else
return true;
return false;
}
/// <summary> Generates default resrouces and compiles them with helper classes. </summary>
private static bool generateDefaultResourceLib()
{
// Generate the resource and script files.
Console.WriteLine("\nGenerating default resources...\n");
var files = getRawResourcesFiles(resourceSourcePath, SearchOption.TopDirectoryOnly);
if (files.Length == 0)
return false;
foreach (var file in files)
{
var name = Path.GetFileNameWithoutExtension(file);
var fileNamespace = resourceNamespace;
var names = name.Split('.');
if (names.Length > 1)
{
fileNamespace = resourceNamespace + "." + string.Join(".", names.Take(names.Length - 1));
name = names[names.Length - 1];
}
var outfile = Path.Combine(resourcePath, fileNamespace + "." + name + ".resources");
var classPath = Path.Combine(resourcePath, name + ".cs");
var classinfo = "/str:cs," + string.Join(",", fileNamespace.ReplaceSpace(), name.ReplaceSpace(), classPath.Quote());
exec(resgenPath, string.Join(" ", file.Quote(), outfile.Quote(), classinfo, "/publicClass"));
}
// Compile scripts and embed default resources in to lib.
Console.WriteLine("\nCompiling default resources...\n");
{
files = Directory.GetFiles(resourcePath, "*.resources", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
return false;
var arguments = new StringBuilder();
arguments.Append("/target:library");
if (resourceMainAtRoot)
arguments.Append(" \"/out:" + resourceNamespace + ".dll\"");
else
arguments.Append(" \"/out:" + Path.Combine(resourcePath, resourceNamespace + ".dll\""));
foreach (var file in files)
arguments.Append(" /res:" + file.Quote());
arguments.Append(" " + Path.Combine(resourcePath, "*.cs"));
exec(cscPath, arguments.ToString());
Console.WriteLine("Deleting temp files...");
// delete .resources files
foreach (var file in files)
File.Delete(file);
// delete .cs files
files = Directory.GetFiles(resourcePath, "*.cs");
foreach (var file in files)
File.Delete(file);
}
return true;
}
/// <summary> Generate resource lib from resources found in path. </summary>
private static bool generateResourceLib(string path, string outPath, string libNamespace, CultureInfo culture)
{
// generate resources
var files = getRawResourcesFiles(path, SearchOption.TopDirectoryOnly);
if (files.Length == 0)
return false;
else
Console.WriteLine("\nGenerating resources for culture \"" + culture.Name + "\"...");
foreach (var file in files)
{
var outfile = Path.Combine(outPath, libNamespace + "." + Path.GetFileNameWithoutExtension(file) + "." + culture.Name + ".resources");
exec(resgenPath, file.Quote() + " " + outfile.Quote());
}
// link resources to lib
files = Directory.GetFiles(outPath, "*.resources", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
return false;
var arguments = new StringBuilder();
arguments.Append("/target:lib /culture:");
arguments.Append(culture.Name);
arguments.Append(" /out:");
arguments.Append(Path.Combine(outPath, libNamespace + ".resources.dll").Quote());
foreach (var file in files)
{
arguments.Append(" /embed:");
arguments.Append(file.Quote());
arguments.Append(',');
arguments.Append(Path.GetFileName(file).ReplaceSpace());
}
exec(alPath, arguments.ToString());
Console.WriteLine("Delete temp resource files..");
foreach (var file in files)
File.Delete(file);
return true;
}
/// <summary> Gets all files with .txt .restext .resx extensions. </summary>
private static string[] getRawResourcesFiles(string path, SearchOption searchOption)
{
return Directory.GetFiles(path, "*.*", searchOption).Where(file =>
file.EndsWith(".txt", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".restext", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".resx", StringComparison.InvariantCultureIgnoreCase)
).ToArray();
}
/// <summary> Executes process without window and dumps output to console. </summary>
private static void exec(string fileName, string arguments)
{
var process = new System.Diagnostics.Process();
var startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = fileName;
startinfo.Arguments = arguments;
startinfo.CreateNoWindow = true;
startinfo.UseShellExecute = false;
startinfo.RedirectStandardOutput = true;
process.StartInfo = startinfo;
process.Start();
while (!process.HasExited)
{
Console.Out.Write(process.StandardOutput.ReadToEnd());
System.Threading.Thread.Sleep(50);
}
Console.Out.Write(process.StandardOutput.ReadToEnd());
}
private static void writeFail(string str)
{
Console.WriteLine();
Console.WriteLine("Error:");
Console.WriteLine(str);
waitAnyKey();
}
private static void waitAnyKey(string str = null)
{
if (str != null)
Console.WriteLine("\n" + str);
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
/// <summary> Only sets value if key is found and not empty. </summary>
private static void configTryGet(System.Collections.Specialized.NameValueCollection config, string key, ref string value)
{
string str = config[key];
if (str != null && str.Length > 0)
value = str;
}
/// <summary> Only sets value if key is found and not empty, and can parse as bool. </summary>
private static void configTryGet(System.Collections.Specialized.NameValueCollection config, string key, ref bool value)
{
string str = config[key];
bool v;
if (str != null && str.Length > 0 && bool.TryParse(str, out v))
value = v;
}
/// <summary> Wraps string in quotes. </summary>
public static string Quote(this string str)
{
return '\"' + str + '\"';
}
/// <summary> Replaces space with _ </summary>
public static string ReplaceSpace(this string str)
{
return str.Replace(' ', '_');
}
}
}