PdnResources.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using PaintDotNet.Base.SettingModel;
  2. using PaintDotNet.SystemLayer;
  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 PaintDotNet
  13. {
  14. public static class PdnResources
  15. {
  16. private static ResourceManager resourceManager;
  17. private const string ourNamespace = "PaintDotNet";
  18. private static Assembly ourAssembly;
  19. private static string[] localeDirs;
  20. private static CultureInfo pdnCulture;
  21. private static string resourcesDir;
  22. public static string ResourcesDir
  23. {
  24. get
  25. {
  26. if (resourcesDir == null)
  27. {
  28. resourcesDir = Path.GetDirectoryName(typeof(PdnResources).Assembly.Location);
  29. }
  30. return resourcesDir;
  31. }
  32. set
  33. {
  34. resourcesDir = value;
  35. //Initialize();
  36. }
  37. }
  38. public static CultureInfo Culture
  39. {
  40. get
  41. {
  42. return pdnCulture;
  43. }
  44. set
  45. {
  46. System.Threading.Thread.CurrentThread.CurrentUICulture = value;
  47. //Initialize();
  48. }
  49. }
  50. public static void Initialize(ConfigModel configModel)
  51. {
  52. resourceManager = CreateResourceManager(configModel);
  53. ourAssembly = Assembly.GetExecutingAssembly();
  54. pdnCulture = CultureInfo.CurrentUICulture;
  55. localeDirs = GetLocaleDirs();
  56. }
  57. /*static PdnResources()
  58. {
  59. Initialize();
  60. }*/
  61. public static void SetNewCulture(string newLocaleName)
  62. {
  63. CultureInfo newCI = new CultureInfo(newLocaleName);
  64. Settings.CurrentUser.SetString("LanguageName", newLocaleName);
  65. Culture = newCI;
  66. }
  67. /*public static void SetNewCulture(string newLocaleName)
  68. {
  69. string oldUserDataPath = PdnInfo.UserDataPath;
  70. string oldPaletteDirName = PdnResources.GetString("ColorPalettes.UserDataSubDirName");
  71. CultureInfo newCI = new CultureInfo(newLocaleName);
  72. Settings.CurrentUser.SetString("LanguageName", newLocaleName);
  73. Culture = newCI;
  74. string newUserDataPath = PdnInfo.UserDataPath;
  75. string newPaletteDirName = PdnResources.GetString("ColorPalettes.UserDataSubDirName");
  76. // 1. rename user data dir from old localized name to new localized name
  77. if (oldUserDataPath != newUserDataPath)
  78. {
  79. try
  80. {
  81. Directory.Move(oldUserDataPath, newUserDataPath);
  82. }
  83. catch (Exception)
  84. {
  85. }
  86. }
  87. // 2. rename palette dir from old localized name (in new localized user data path) to new localized name
  88. string oldPalettePath = Path.Combine(newUserDataPath, oldPaletteDirName);
  89. string newPalettePath = Path.Combine(newUserDataPath, newPaletteDirName);
  90. if (oldPalettePath != newPalettePath)
  91. {
  92. try
  93. {
  94. Directory.Move(oldPalettePath, newPalettePath);
  95. }
  96. catch (Exception)
  97. {
  98. }
  99. }
  100. // END HACK
  101. }*/
  102. public static string[] GetInstalledLocales()
  103. {
  104. const string left = "PaintDotNet.Strings.3.ZH-CN";
  105. const string right = ".resources";
  106. string ourDir = ResourcesDir;
  107. string fileSpec = left + "*" + right;
  108. string[] pathNames = Directory.GetFiles(ourDir, fileSpec);
  109. List<String> locales = new List<string>();
  110. for (int i = 0; i < pathNames.Length; ++i)
  111. {
  112. string pathName = pathNames[i];
  113. string dirName = Path.GetDirectoryName(pathName);
  114. string fileName = Path.GetFileName(pathName);
  115. string sansRight = fileName.Substring(0, fileName.Length - right.Length);
  116. string sansLeft = sansRight.Substring(left.Length);
  117. string locale;
  118. if (sansLeft.Length > 0 && sansLeft[0] == '.')
  119. {
  120. locale = sansLeft.Substring(1);
  121. }
  122. else if (sansLeft.Length == 0)
  123. {
  124. locale = "en-US";
  125. }
  126. else
  127. {
  128. locale = sansLeft;
  129. }
  130. try
  131. {
  132. // Ensure this locale can create a valid CultureInfo object.
  133. CultureInfo ci = new CultureInfo(locale);
  134. }
  135. catch (Exception)
  136. {
  137. // Skip past invalid locales -- don't let them crash us
  138. continue;
  139. }
  140. locales.Add(locale);
  141. }
  142. return locales.ToArray();
  143. }
  144. public static string[] GetLocaleNameChain()
  145. {
  146. List<string> names = new List<string>();
  147. CultureInfo ci = pdnCulture;
  148. while (ci.Name != string.Empty)
  149. {
  150. names.Add(ci.Name);
  151. ci = ci.Parent;
  152. }
  153. return names.ToArray();
  154. }
  155. private static string[] GetLocaleDirs()
  156. {
  157. const string rootDirName = "Resources";
  158. string appDir = ResourcesDir;
  159. string rootDir = Path.Combine(appDir, rootDirName);
  160. List<string> dirs = new List<string>();
  161. CultureInfo ci = pdnCulture;
  162. while (ci.Name != string.Empty)
  163. {
  164. string localeDir = Path.Combine(rootDir, ci.Name);
  165. if (Directory.Exists(localeDir))
  166. {
  167. dirs.Add(localeDir);
  168. }
  169. ci = ci.Parent;
  170. }
  171. return dirs.ToArray();
  172. }
  173. private static ResourceManager CreateResourceManager(ConfigModel configModel)
  174. {
  175. string stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
  176. if (configModel.Language == 1)
  177. {
  178. stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
  179. }
  180. else if (configModel.Language == 2)
  181. {
  182. stringsFileName = "PaintDotNet.Strings.3.EN-US";
  183. }
  184. else if(configModel.Language == 3)
  185. {
  186. stringsFileName = "PaintDotNet.Strings.3.DE-DE";
  187. }
  188. ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(stringsFileName, ResourcesDir, null);
  189. return rm;
  190. }
  191. public static ResourceManager Strings
  192. {
  193. get
  194. {
  195. return resourceManager;
  196. }
  197. }
  198. public static string GetString(string stringName)
  199. {
  200. string theString = resourceManager.GetString(stringName, pdnCulture);// "";//
  201. if (theString == null)
  202. {
  203. Debug.WriteLine(stringName + " not found");
  204. }
  205. return theString;
  206. }
  207. public static Stream GetResourceStream(string fileName)
  208. {
  209. Stream stream = null;
  210. for (int i = 0; i < localeDirs.Length; ++i)
  211. {
  212. string filePath = Path.Combine(localeDirs[i], fileName);
  213. if (File.Exists(filePath))
  214. {
  215. stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  216. break;
  217. }
  218. }
  219. if (stream == null)
  220. {
  221. string fullName = ourNamespace + "." + fileName;
  222. stream = ourAssembly.GetManifestResourceStream(fullName);
  223. }
  224. return stream;
  225. }
  226. public static Image GetImageBmpOrPng(string fileNameNoExt)
  227. {
  228. // using Path.ChangeExtension is not what we want; quite often filenames are "Icons.BlahBlahBlah"
  229. string fileNameBmp = fileNameNoExt + ".bmp";
  230. Image image = GetImage(fileNameBmp);
  231. if (image == null)
  232. {
  233. string fileNamePng = fileNameNoExt + ".png";
  234. image = GetImage(fileNamePng);
  235. }
  236. return image;
  237. }
  238. public static Image GetImage(string fileName)
  239. {
  240. Stream stream = GetResourceStream(fileName);
  241. Image image = null;
  242. if (stream != null)
  243. {
  244. image = LoadImage(stream);
  245. }
  246. return image;
  247. }
  248. private sealed class PdnImageResource
  249. : ImageResource
  250. {
  251. private string name;
  252. private static Dictionary<string, ImageResource> images;
  253. protected override Image Load()
  254. {
  255. return PdnResources.GetImage(this.name);
  256. }
  257. public static ImageResource Get(string name)
  258. {
  259. ImageResource ir;
  260. if (!images.TryGetValue(name, out ir))
  261. {
  262. ir = new PdnImageResource(name);
  263. images.Add(name, ir);
  264. }
  265. return ir;
  266. }
  267. static PdnImageResource()
  268. {
  269. images = new Dictionary<string, ImageResource>();
  270. }
  271. private PdnImageResource(string name)
  272. : base()
  273. {
  274. this.name = name;
  275. }
  276. private PdnImageResource(Image image)
  277. : base(image)
  278. {
  279. this.name = null;
  280. }
  281. }
  282. public static ImageResource GetImageResource(string fileName)
  283. {
  284. return PdnImageResource.Get(fileName);
  285. }
  286. public static Icon GetIcon(string fileName)
  287. {
  288. Stream stream = GetResourceStream(fileName);
  289. Icon icon = null;
  290. if (stream != null)
  291. {
  292. icon = new Icon(stream);
  293. }
  294. return icon;
  295. }
  296. public static Icon GetIconFromImage(string fileName)
  297. {
  298. Stream stream = GetResourceStream(fileName);
  299. Icon icon = null;
  300. if (stream != null)
  301. {
  302. Image image = LoadImage(stream);
  303. icon = Icon.FromHandle(((Bitmap)image).GetHicon());
  304. image.Dispose();
  305. stream.Close();
  306. }
  307. return icon;
  308. }
  309. private static bool CheckForSignature(Stream input, byte[] signature)
  310. {
  311. long oldPos = input.Position;
  312. byte[] inputSig = new byte[signature.Length];
  313. int amountRead = input.Read(inputSig, 0, inputSig.Length);
  314. bool foundSig = false;
  315. if (amountRead == signature.Length)
  316. {
  317. foundSig = true;
  318. for (int i = 0; i < signature.Length; ++i)
  319. {
  320. foundSig &= (signature[i] == inputSig[i]);
  321. }
  322. }
  323. input.Position = oldPos;
  324. return foundSig;
  325. }
  326. public static bool IsGdiPlusImageAllowed(Stream input)
  327. {
  328. byte[] wmfSig = new byte[] { 0xd7, 0xcd, 0xc6, 0x9a };
  329. byte[] emfSig = new byte[] { 0x01, 0x00, 0x00, 0x00 };
  330. // Check for and explicitely block WMF and EMF images
  331. return !(CheckForSignature(input, emfSig) || CheckForSignature(input, wmfSig));
  332. }
  333. public static Image LoadImage(string fileName)
  334. {
  335. using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  336. {
  337. return LoadImage(stream);
  338. }
  339. }
  340. /// <summary>
  341. /// Loads an image from the given stream. The stream must be seekable.
  342. /// </summary>
  343. /// <param name="input">The Stream to load the image from.</param>
  344. public static Image LoadImage(Stream input)
  345. {
  346. /*
  347. if (!IsGdiPlusImageAllowed(input))
  348. {
  349. throw new IOException("File format is not supported");
  350. }
  351. */
  352. Image image = Image.FromStream(input);
  353. if (image.RawFormat == ImageFormat.Wmf || image.RawFormat == ImageFormat.Emf)
  354. {
  355. image.Dispose();
  356. throw new IOException("File format isn't supported");
  357. }
  358. return image;
  359. }
  360. }
  361. }