123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using SmartCoalApplication.Base;
- using SmartCoalApplication.Base.SettingModel;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Resources;
- namespace Resources
- {
- public class PdnResources
- {
- private static ResourceManager resourceManager;
- private const string ourNamespace = "SmartCoalApplication.Resources";
- private static Assembly ourAssembly;
- private static string[] localeDirs;
- private static CultureInfo pdnCulture;
- private static string resourcesDir;
- public static void Initialize(ConfigModel configModel)
- {
- resourceManager = CreateResourceManager(configModel);
- ourAssembly = Assembly.GetExecutingAssembly();
- pdnCulture = CultureInfo.CurrentUICulture;
- localeDirs = GetLocaleDirs();
- }
- private static ResourceManager CreateResourceManager(ConfigModel configModel)
- {
- string stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
- if (configModel.Language == 1)
- {
- stringsFileName = "PaintDotNet.Strings.3.ZH-CN";
- }
- else if (configModel.Language == 2)
- {
- stringsFileName = "PaintDotNet.Strings.3.EN-US";
- }
- ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(stringsFileName, ResourcesDir, null);
- return rm;
- }
- public static string ResourcesDir
- {
- get
- {
- if (resourcesDir == null)
- {
- resourcesDir = Path.GetDirectoryName(typeof(PdnResources).Assembly.Location);
- }
- return resourcesDir;
- }
- set
- {
- resourcesDir = value;
- //Initialize();
- }
- }
- public static CultureInfo Culture
- {
- get
- {
- return pdnCulture;
- }
- set
- {
- System.Threading.Thread.CurrentThread.CurrentUICulture = value;
- //Initialize();
- }
- }
- public static string[] GetInstalledLocales()
- {
- const string left = "PaintDotNet.Strings.3.ZH-CN";
- const string right = ".resources";
- string ourDir = ResourcesDir;
- string fileSpec = left + "*" + right;
- string[] pathNames = Directory.GetFiles(ourDir, fileSpec);
- List<String> locales = new List<string>();
- for (int i = 0; i < pathNames.Length; ++i)
- {
- string pathName = pathNames[i];
- string dirName = Path.GetDirectoryName(pathName);
- string fileName = Path.GetFileName(pathName);
- string sansRight = fileName.Substring(0, fileName.Length - right.Length);
- string sansLeft = sansRight.Substring(left.Length);
- string locale;
- if (sansLeft.Length > 0 && sansLeft[0] == '.')
- {
- locale = sansLeft.Substring(1);
- }
- else if (sansLeft.Length == 0)
- {
- locale = "en-US";
- }
- else
- {
- locale = sansLeft;
- }
- try
- {
- CultureInfo ci = new CultureInfo(locale);
- }
- catch (Exception)
- {
- continue;
- }
- locales.Add(locale);
- }
- return locales.ToArray();
- }
- private static string[] GetLocaleDirs()
- {
- const string rootDirName = "";
- string appDir = ResourcesDir;
- string rootDir = Path.Combine(appDir, rootDirName);
- List<string> dirs = new List<string>();
- CultureInfo ci = pdnCulture;
- while (ci.Name != string.Empty)
- {
- string localeDir = Path.Combine(rootDir, ci.Name);
- if (Directory.Exists(localeDir))
- {
- dirs.Add(localeDir);
- }
- ci = ci.Parent;
- }
- return dirs.ToArray();
- }
- public static ResourceManager Strings
- {
- get
- {
- return resourceManager;
- }
- }
- public static string GetString(string stringName)
- {
- string theString = resourceManager.GetString(stringName, pdnCulture);
- if (theString == null)
- {
- Debug.WriteLine(stringName + " not found");
- }
- return theString;
- }
- public static Stream GetResourceStream(string fileName)
- {
- Stream stream = null;
- for (int i = 0; i < localeDirs.Length; ++i)
- {
- string filePath = Path.Combine(localeDirs[i], fileName);
- if (File.Exists(filePath))
- {
- stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
- break;
- }
- }
- if (stream == null)
- {
- string fullName = ourNamespace + "." + fileName;
- stream = ourAssembly.GetManifestResourceStream(fullName);
- }
- return stream;
- }
- public static Image GetImage(string fileName)
- {
- Stream stream = GetResourceStream(fileName);
- Image image = null;
- if (stream != null)
- {
- image = LoadImage(stream);
- }
- return image;
- }
- private sealed class PdnImageResource : ImageResource
- {
- private string name;
- private static Dictionary<string, ImageResource> images;
- protected override Image Load()
- {
- return PdnResources.GetImage(this.name);
- }
- public static ImageResource Get(string name)
- {
- ImageResource ir;
- if (!images.TryGetValue(name, out ir))
- {
- ir = new PdnImageResource(name);
- images.Add(name, ir);
- }
- return ir;
- }
- static PdnImageResource()
- {
- images = new Dictionary<string, ImageResource>();
- }
- private PdnImageResource(string name) : base()
- {
- this.name = name;
- }
-
- private PdnImageResource(Image image) : base(image)
- {
- this.name = null;
- }
- }
- public static ImageResource GetImageResource(string fileName)
- {
- return PdnImageResource.Get(fileName);
- }
- private static bool CheckForSignature(Stream input, byte[] signature)
- {
- long oldPos = input.Position;
- byte[] inputSig = new byte[signature.Length];
- int amountRead = input.Read(inputSig, 0, inputSig.Length);
- bool foundSig = false;
- if (amountRead == signature.Length)
- {
- foundSig = true;
- for (int i = 0; i < signature.Length; ++i)
- {
- foundSig &= (signature[i] == inputSig[i]);
- }
- }
- input.Position = oldPos;
- return foundSig;
- }
- public static Image LoadImage(Stream input)
- {
- Image image = Image.FromStream(input);
- if (image.RawFormat == ImageFormat.Wmf || image.RawFormat == ImageFormat.Emf)
- {
- image.Dispose();
- throw new IOException("File format isn't supported");
- }
- return image;
- }
- }
- }
|