PdnResources.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using SmartCoalApplication.Base;
  2. using SmartCoalApplication.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Resources;
  12. namespace Resources
  13. {
  14. public class PdnResources
  15. {
  16. private static ResourceManager resourceManager;
  17. private const string ourNamespace = "SmartCoalApplication.Resources";
  18. private static Assembly ourAssembly;
  19. private static string[] localeDirs;
  20. private static CultureInfo pdnCulture;
  21. private static string resourcesDir;
  22. public static void Initialize(ConfigModel configModel)
  23. {
  24. resourceManager = CreateResourceManager(configModel);
  25. ourAssembly = Assembly.GetExecutingAssembly();
  26. pdnCulture = CultureInfo.CurrentUICulture;
  27. localeDirs = GetLocaleDirs();
  28. }
  29. private static ResourceManager CreateResourceManager(ConfigModel configModel)
  30. {
  31. string stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
  32. if (configModel.Language == 1)
  33. {
  34. stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
  35. }
  36. else if (configModel.Language == 2)
  37. {
  38. stringsFileName = "PaintDotNet.Strings.3.EN-US";
  39. }
  40. ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(stringsFileName, ResourcesDir, null);
  41. return rm;
  42. }
  43. public static string ResourcesDir
  44. {
  45. get
  46. {
  47. if (resourcesDir == null)
  48. {
  49. resourcesDir = Path.GetDirectoryName(typeof(PdnResources).Assembly.Location);
  50. }
  51. return resourcesDir;
  52. }
  53. set
  54. {
  55. resourcesDir = value;
  56. //Initialize();
  57. }
  58. }
  59. public static CultureInfo Culture
  60. {
  61. get
  62. {
  63. return pdnCulture;
  64. }
  65. set
  66. {
  67. System.Threading.Thread.CurrentThread.CurrentUICulture = value;
  68. //Initialize();
  69. }
  70. }
  71. public static string[] GetInstalledLocales()
  72. {
  73. const string left = "PaintDotNet.Strings.3.ZH-CN";
  74. const string right = ".resources";
  75. string ourDir = ResourcesDir;
  76. string fileSpec = left + "*" + right;
  77. string[] pathNames = Directory.GetFiles(ourDir, fileSpec);
  78. List<String> locales = new List<string>();
  79. for (int i = 0; i < pathNames.Length; ++i)
  80. {
  81. string pathName = pathNames[i];
  82. string dirName = Path.GetDirectoryName(pathName);
  83. string fileName = Path.GetFileName(pathName);
  84. string sansRight = fileName.Substring(0, fileName.Length - right.Length);
  85. string sansLeft = sansRight.Substring(left.Length);
  86. string locale;
  87. if (sansLeft.Length > 0 && sansLeft[0] == '.')
  88. {
  89. locale = sansLeft.Substring(1);
  90. }
  91. else if (sansLeft.Length == 0)
  92. {
  93. locale = "en-US";
  94. }
  95. else
  96. {
  97. locale = sansLeft;
  98. }
  99. try
  100. {
  101. CultureInfo ci = new CultureInfo(locale);
  102. }
  103. catch (Exception)
  104. {
  105. continue;
  106. }
  107. locales.Add(locale);
  108. }
  109. return locales.ToArray();
  110. }
  111. private static string[] GetLocaleDirs()
  112. {
  113. const string rootDirName = "";
  114. string appDir = ResourcesDir;
  115. string rootDir = Path.Combine(appDir, rootDirName);
  116. List<string> dirs = new List<string>();
  117. CultureInfo ci = pdnCulture;
  118. while (ci.Name != string.Empty)
  119. {
  120. string localeDir = Path.Combine(rootDir, ci.Name);
  121. if (Directory.Exists(localeDir))
  122. {
  123. dirs.Add(localeDir);
  124. }
  125. ci = ci.Parent;
  126. }
  127. return dirs.ToArray();
  128. }
  129. public static ResourceManager Strings
  130. {
  131. get
  132. {
  133. return resourceManager;
  134. }
  135. }
  136. public static string GetString(string stringName)
  137. {
  138. string theString = resourceManager.GetString(stringName, pdnCulture);
  139. if (theString == null)
  140. {
  141. Debug.WriteLine(stringName + " not found");
  142. }
  143. return theString;
  144. }
  145. public static Stream GetResourceStream(string fileName)
  146. {
  147. Stream stream = null;
  148. for (int i = 0; i < localeDirs.Length; ++i)
  149. {
  150. string filePath = Path.Combine(localeDirs[i], fileName);
  151. if (File.Exists(filePath))
  152. {
  153. stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  154. break;
  155. }
  156. }
  157. if (stream == null)
  158. {
  159. string fullName = ourNamespace + "." + fileName;
  160. stream = ourAssembly.GetManifestResourceStream(fullName);
  161. }
  162. return stream;
  163. }
  164. public static Image GetImage(string fileName)
  165. {
  166. Stream stream = GetResourceStream(fileName);
  167. Image image = null;
  168. if (stream != null)
  169. {
  170. image = LoadImage(stream);
  171. }
  172. return image;
  173. }
  174. private sealed class PdnImageResource : ImageResource
  175. {
  176. private string name;
  177. private static Dictionary<string, ImageResource> images;
  178. protected override Image Load()
  179. {
  180. return PdnResources.GetImage(this.name);
  181. }
  182. public static ImageResource Get(string name)
  183. {
  184. ImageResource ir;
  185. if (!images.TryGetValue(name, out ir))
  186. {
  187. ir = new PdnImageResource(name);
  188. images.Add(name, ir);
  189. }
  190. return ir;
  191. }
  192. static PdnImageResource()
  193. {
  194. images = new Dictionary<string, ImageResource>();
  195. }
  196. private PdnImageResource(string name) : base()
  197. {
  198. this.name = name;
  199. }
  200. private PdnImageResource(Image image) : base(image)
  201. {
  202. this.name = null;
  203. }
  204. }
  205. public static ImageResource GetImageResource(string fileName)
  206. {
  207. return PdnImageResource.Get(fileName);
  208. }
  209. private static bool CheckForSignature(Stream input, byte[] signature)
  210. {
  211. long oldPos = input.Position;
  212. byte[] inputSig = new byte[signature.Length];
  213. int amountRead = input.Read(inputSig, 0, inputSig.Length);
  214. bool foundSig = false;
  215. if (amountRead == signature.Length)
  216. {
  217. foundSig = true;
  218. for (int i = 0; i < signature.Length; ++i)
  219. {
  220. foundSig &= (signature[i] == inputSig[i]);
  221. }
  222. }
  223. input.Position = oldPos;
  224. return foundSig;
  225. }
  226. public static Image LoadImage(Stream input)
  227. {
  228. Image image = Image.FromStream(input);
  229. if (image.RawFormat == ImageFormat.Wmf || image.RawFormat == ImageFormat.Emf)
  230. {
  231. image.Dispose();
  232. throw new IOException("File format isn't supported");
  233. }
  234. return image;
  235. }
  236. }
  237. }