public override ProblemCollection Check( Resource resource ) { using( var stream = new MemoryStream( resource.Data ) ) { try { using( ResourceReader reader = new ResourceReader( stream ) ) { AssemblyResourceNameHelper nameHelper = AssemblyResourceNameHelper.Get( resource.DefiningModule.ContainingAssembly ); IEnumerable<string> allTextResources = reader.Cast<DictionaryEntry>().Select( e => e.Key.ToString() ); IEnumerable<string> allNotReferencedResources = allTextResources.Where( resourceName => !nameHelper.Contains( resourceName ) ); foreach( var resourceName in allNotReferencedResources ) { Problems.Add( new Problem( GetResolution( resourceName, resource.Name ), resourceName ) ); } } } catch( Exception ) { } } return Problems; }
/// <summary> /// Load piece sets from resource /// </summary> /// <returns></returns> public static SortedList <string, PieceSet> LoadPieceSetFromResource() { SortedList <string, PieceSet> arrRetVal; Assembly asm; string strResName; string strKeyName; string strPieceSetName; string[] arrPart; Stream streamResource; ResourceReader resReader; PieceSet pieceSet; arrRetVal = new SortedList <string, PieceSet>(64); asm = typeof(App).Assembly; strResName = asm.GetName().Name + ".g.resources"; streamResource = asm.GetManifestResourceStream(strResName); try { resReader = new System.Resources.ResourceReader(streamResource); streamResource = null; using (resReader) { foreach (DictionaryEntry dictEntry in resReader.Cast <DictionaryEntry>()) { strKeyName = Uri.UnescapeDataString(dictEntry.Key as string); if (strKeyName != null) { strKeyName = strKeyName.ToLower(); if (strKeyName.StartsWith("piecesets/") && strKeyName.EndsWith(".baml")) { arrPart = strKeyName.Split('/'); if (arrPart.Length == 3) { strPieceSetName = arrPart[1]; if (!arrRetVal.ContainsKey(strPieceSetName)) { pieceSet = new PieceSetStandard(strPieceSetName, strPieceSetName); arrRetVal.Add(strPieceSetName, pieceSet); } } } } } } } finally { if (streamResource != null) { streamResource.Dispose(); } } return(arrRetVal); }
public static string[] GetResourceNames() { var assembly = Assembly.GetExecutingAssembly(); string resName = assembly.GetName().Name + ".g.resources"; using (var stream = assembly.GetManifestResourceStream(resName)) { using (var reader = new System.Resources.ResourceReader(stream)) { return(reader.Cast <DictionaryEntry>().Select(entry => (string)entry.Key).ToArray()); } } }
protected override void LoadChildren() { EmbeddedResource er = this.Resource as EmbeddedResource; if (er != null) { Stream s = er.GetResourceStream(); s.Position = 0; ResourceReader reader; try { reader = new ResourceReader(s); } catch (ArgumentException) { return; } foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) { ProcessResourceEntry(entry); } } }
private IList <DictionaryEntry> GetAllAssemblyResources(Assembly assembly) { #if debug var resourcesName = assembly.GetName().Name + ".g"; var manager = new ResourceManager(resourcesName, assembly); var resourceSet = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); return(resourceSet.OfType <DictionaryEntry>().ToList()); #else string resName = assembly.GetName().Name + ".g.resources"; using (var stream = assembly.GetManifestResourceStream(resName)) { using (var reader = new System.Resources.ResourceReader(stream)) { return(reader.Cast <DictionaryEntry>().ToList()); } } #endif }
/// <summary> /// Load piece sets from resource /// </summary> /// <returns></returns> public static SortedList<string, PieceSet> LoadPieceSetFromResource() { SortedList<string, PieceSet> arrRetVal; Assembly asm; string strResName; string strKeyName; string strPieceSetName; string[] arrPart; Stream streamResource; ResourceReader resReader; PieceSet pieceSet; arrRetVal = new SortedList<string,PieceSet>(64); asm = typeof(App).Assembly; strResName = asm.GetName().Name + ".g.resources"; streamResource = asm.GetManifestResourceStream(strResName); try { resReader = new System.Resources.ResourceReader(streamResource); streamResource = null; using (resReader) { foreach (DictionaryEntry dictEntry in resReader.Cast<DictionaryEntry>()) { strKeyName = dictEntry.Key as string; if (strKeyName != null) { strKeyName = strKeyName.ToLower(); if (strKeyName.StartsWith("piecesets/") && strKeyName.EndsWith(".baml")) { arrPart = strKeyName.Split('/'); if (arrPart.Length == 3) { strPieceSetName = arrPart[1]; if (!arrRetVal.ContainsKey(strPieceSetName)) { pieceSet = new PieceSetStandard(strPieceSetName, strPieceSetName); arrRetVal.Add(strPieceSetName, pieceSet); } } } } } } } finally { if (streamResource != null) { streamResource.Dispose(); } } return(arrRetVal); }
protected override void LoadChildren() { EmbeddedResource er = this.Resource as EmbeddedResource; if (er != null) { Stream s = er.GetResourceStream(); s.Position = 0; ResourceReader reader; try { reader = new ResourceReader(s); } catch (ArgumentException) { return; } foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) { if (entry.Value is Stream) Children.Add(ResourceEntryNode.Create(entry.Key.ToString(), (Stream)entry.Value)); else if (entry.Value is byte[]) Children.Add(ResourceEntryNode.Create(entry.Key.ToString(), new MemoryStream((byte[])entry.Value))); } } }
//确保程序集的资源元数据信息都已加载 private static void makeSureLoadAssemblyResourceMetaData(Assembly assembly) { String assemblyName = assembly.GetName().Name; lock (typeof(ResourceUtils)) { //嵌入的资源 if (!assemblyEmbedResourceDict.ContainsKey(assembly)) { assemblyEmbedResourceDict[assembly] = assembly.GetManifestResourceNames(); } //Resource的资源 string resBaseName = assemblyName + ".g.resources"; if (!assemblyResourceResourceDict.ContainsKey(assembly)) { assemblyResourceResourceDict[assembly] = new Dictionary <string, string>(); if (assemblyEmbedResourceDict[assembly].Contains(resBaseName)) { String[] resourceList = new String[0]; using (var stream = assembly.GetManifestResourceStream(resBaseName)) { if (stream != null) { using (var reader = new System.Resources.ResourceReader(stream)) { resourceList = reader.Cast <DictionaryEntry>().Select(entry => (string)entry.Key).ToArray(); } } } foreach (var resource in resourceList) { assemblyResourceResourceDict[assembly][resource.Replace('/', '.')] = resource; } } } } }
static void InitResources() { var asm = typeof(CrispImageConverter).Assembly; string resName = asm.GetName().Name + ".g.resources"; using (var stream = asm.GetManifestResourceStream(resName)) { if (stream == null) { return; // design time } using (var reader = new System.Resources.ResourceReader(stream)) { imageResources = reader.Cast <DictionaryEntry>() .Select(x => (string)x.Key) .Where(key => key.StartsWith("images/", StringComparison.OrdinalIgnoreCase)) .Select(uri => new { Source = new ImageSourceInfo() { Uri = "/Swiddler;component/" + FixXamlExtension(uri), Size = GetImageSize(uri, out var nameKey) },
//确保程序集的资源元数据信息都已加载 private static void makeSureLoadAssemblyResourceMetaData(Assembly assembly) { String assemblyName = assembly.GetName().Name; lock (typeof(ResourceUtils)) { //嵌入的资源 if (!assemblyEmbedResourceDict.ContainsKey(assembly)) { assemblyEmbedResourceDict[assembly] = assembly.GetManifestResourceNames(); } //Resource的资源 string resBaseName = assemblyName + ".g.resources"; if (!assemblyResourceResourceDict.ContainsKey(assembly)) { assemblyResourceResourceDict[assembly] = new Dictionary<string, string>(); if (assemblyEmbedResourceDict[assembly].Contains(resBaseName)) { String[] resourceList = new String[0]; using (var stream = assembly.GetManifestResourceStream(resBaseName)) { if (stream != null) { using (var reader = new System.Resources.ResourceReader(stream)) { resourceList = reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray(); } } } foreach (var resource in resourceList) { assemblyResourceResourceDict[assembly][resource.Replace('/', '.')] = resource; } } } } }
Stream LoadBaml(Resource res, string name) { EmbeddedResource er = res as EmbeddedResource; if (er != null) { Stream s = er.GetResourceStream(); s.Position = 0; ResourceReader reader; try { reader = new ResourceReader(s); } catch (ArgumentException) { return null; } foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) { if (entry.Key.ToString() == name) { if (entry.Value is Stream) return (Stream)entry.Value; if (entry.Value is byte[]) return new MemoryStream((byte[])entry.Value); } } } return null; }
protected override void LoadChildren() { EmbeddedResource er = this.Resource as EmbeddedResource; if (er != null) { Stream s = er.GetResourceStream(); s.Position = 0; ResourceReader reader; try { reader = new ResourceReader(s); } catch { // NotSupportedException, IOException, BadImageFormatException, ArgumentException // and any other possible exception return; } try { foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) { ProcessResourceEntry(entry); } } catch { } } }
/// <summary> /// Add text files from assembly resources under specified scope. /// </summary> /// <param name="assembly"></param> /// <param name="scope"></param> public void AddTextFilesFromResources(Assembly assembly, string scope) { Contract.Assert(assembly != null, "assembly is required to have a value."); Contract.Assert(scope != null, "scope is required to have a value."); scope = scope.ToLower(); string resourceName = string.Format("{0}.g.resources", assembly.GetName().Name); Stream resourceStream = assembly.GetManifestResourceStream(resourceName); if (resourceStream == null) { throw new FileNotFoundException(string.Format("Resource file {0} is not found from assembly {1}", resourceName, assembly.GetName().Name)); } using (ResourceReader rr = new ResourceReader(resourceStream)) { var filesToDeploy = rr.Cast<DictionaryEntry>().Where(entry => { string key = entry.Key as string; if (string.IsNullOrEmpty(key)) { return false; } return key.StartsWith(scope) && (entry.Value is Stream); }).Select(entry => { string key = entry.Key as string; key = key.Substring(scope.Length); if (key.StartsWith("/")) { key = key.Substring(1); } return Tuple.Create<string, Stream>(key, entry.Value as Stream); }); if (filesToDeploy.Count() == 0) { throw new FileNotFoundException(string.Format("No file is found under path {0}", scope)); } foreach (var fileEntry in filesToDeploy) { using (StreamReader sr = new StreamReader(fileEntry.Item2)) { if (this.TextFiles.ContainsKey(fileEntry.Item1)) { this.TextFiles[fileEntry.Item1] = sr.ReadToEnd(); } else { this.TextFiles.Add(fileEntry.Item1, sr.ReadToEnd()); } } } } }
protected override void LoadChildren() { EmbeddedResource er = r as EmbeddedResource; if (er != null) { try { Stream s = er.GetResourceStream(); s.Position = 0; if (er.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)) { ResourceReader reader = new ResourceReader(s); foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) { if (entry.Value is Stream) Children.Add(new ResourceEntryNode(entry.Key.ToString(), (Stream)entry.Value)); } } } catch (ArgumentException) { } } }