We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Resources
- {
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Reflection;
- using System.Runtime.Versioning;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- internal sealed class RuntimeResourceSet : ResourceSet, IEnumerable
- {
- internal const int Version = 2;
-
-
-
-
-
- private Dictionary<string, ResourceLocator> _resCache;
-
-
-
-
- private ResourceReader _defaultReader;
-
- private Dictionary<string, ResourceLocator> _caseInsensitiveTable;
-
-
-
- private bool _haveReadFromReader;
-
- [ResourceExposure(ResourceScope.Machine)]
- [ResourceConsumption(ResourceScope.Machine)]
- internal RuntimeResourceSet(string fileName) : base(false)
- {
- BCLDebug.Log("RESMGRFILEFORMAT", "RuntimeResourceSet .ctor(String)");
- _resCache = new Dictionary<string, ResourceLocator>(FastResourceComparer.Default);
- Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
- _defaultReader = new ResourceReader(stream, _resCache);
- Reader = _defaultReader;
- }
-
- internal RuntimeResourceSet(Stream stream) : base(false)
- {
- BCLDebug.Log("RESMGRFILEFORMAT", "RuntimeResourceSet .ctor(Stream)");
- _resCache = new Dictionary<string, ResourceLocator>(FastResourceComparer.Default);
- _defaultReader = new ResourceReader(stream, _resCache);
- Reader = _defaultReader;
- }
-
- protected override void Dispose(bool disposing)
- {
- if (Reader == null)
- return;
-
- if (disposing) {
- lock (Reader) {
- _resCache = null;
- if (_defaultReader != null) {
- _defaultReader.Close();
- _defaultReader = null;
- }
- _caseInsensitiveTable = null;
-
- base.Dispose(disposing);
- }
- }
- else {
-
- _resCache = null;
- _caseInsensitiveTable = null;
- _defaultReader = null;
- base.Dispose(disposing);
- }
- }
-
- public override IDictionaryEnumerator GetEnumerator()
- {
- return GetEnumeratorHelper();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumeratorHelper();
- }
-
- private IDictionaryEnumerator GetEnumeratorHelper()
- {
- IResourceReader copyOfReader = Reader;
- if (copyOfReader == null || _resCache == null)
- throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
-
- return copyOfReader.GetEnumerator();
- }
-
-
- public override string GetString(string key)
- {
- object o = GetObject(key, false, true);
- return (string)o;
- }
-
- public override string GetString(string key, bool ignoreCase)
- {
- object o = GetObject(key, ignoreCase, true);
- return (string)o;
- }
-
- public override object GetObject(string key)
- {
- return GetObject(key, false, false);
- }
-
- public override object GetObject(string key, bool ignoreCase)
- {
- return GetObject(key, ignoreCase, false);
- }
-
- private object GetObject(string key, bool ignoreCase, bool isString)
- {
- if (key == null)
- throw new ArgumentNullException("key");
- Dictionary<string, ResourceLocator> copyOfCache = _resCache;
- if (Reader == null || copyOfCache == null)
- throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
-
- object value = null;
- ResourceLocator resLocation;
- if (copyOfCache.TryGetValue(key, out resLocation)) {
- value = ResolveResourceLocator(resLocation, key, copyOfCache, false);
- return value;
- }
-
- lock (Reader) {
- if (Reader == null)
- throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
-
- if (_defaultReader != null) {
- BCLDebug.Log("RESMGRFILEFORMAT", "Going down fast path in RuntimeResourceSet::GetObject");
-
-
- int dataPos = -1;
- if (_resCache.TryGetValue(key, out resLocation)) {
- value = resLocation.Value;
- dataPos = resLocation.DataPosition;
- }
-
- if (dataPos == -1 && value == null) {
- dataPos = _defaultReader.FindPosForResource(key);
- }
-
- if (dataPos != -1 && value == null) {
- BCLDebug.Assert(dataPos >= 0, "data section offset cannot be negative!");
-
-
-
-
-
- ResourceTypeCode typeCode;
- if (isString) {
- value = _defaultReader.LoadString(dataPos);
- typeCode = ResourceTypeCode.String;
- }
- else {
- value = _defaultReader.LoadObject(dataPos, out typeCode);
- }
-
- resLocation = new ResourceLocator(dataPos, (ResourceLocator.CanCache(typeCode)) ? value : null);
- lock (_resCache) {
- _resCache[key] = resLocation;
- }
- }
-
- if (value != null || !ignoreCase) {
-
- return value;
-
- }
- }
-
-
-
-
- if (!_haveReadFromReader) {
-
- if (ignoreCase && _caseInsensitiveTable == null) {
- _caseInsensitiveTable = new Dictionary<string, ResourceLocator>(StringComparer.OrdinalIgnoreCase);
- }
- #if _DEBUG
- BCLDebug.Perf(!ignoreCase, "Using case-insensitive lookups is bad perf-wise. Consider capitalizing " + key + " correctly in your source");
- #endif
-
- if (_defaultReader == null) {
- IDictionaryEnumerator en = Reader.GetEnumerator();
- while (en.MoveNext()) {
- DictionaryEntry entry = en.Entry;
- string readKey = (string)entry.Key;
- ResourceLocator resLoc = new ResourceLocator(-1, entry.Value);
- _resCache.Add(readKey, resLoc);
- if (ignoreCase)
- _caseInsensitiveTable.Add(readKey, resLoc);
- }
-
-
- if (!ignoreCase)
- Reader.Close();
- }
- else {
- BCLDebug.Assert(ignoreCase, "This should only happen for case-insensitive lookups");
- ResourceReader.ResourceEnumerator en = _defaultReader.GetEnumeratorInternal();
- while (en.MoveNext()) {
-
- string currentKey = (string)en.Key;
- int dataPos = en.DataPosition;
- ResourceLocator resLoc = new ResourceLocator(dataPos, null);
- _caseInsensitiveTable.Add(currentKey, resLoc);
- }
- }
- _haveReadFromReader = true;
- }
- object obj = null;
- bool found = false;
- bool keyInWrongCase = false;
- if (_defaultReader != null) {
- if (_resCache.TryGetValue(key, out resLocation))
- found = true;
- }
- if (!found && ignoreCase) {
- if (_caseInsensitiveTable.TryGetValue(key, out resLocation)) {
- found = true;
- keyInWrongCase = true;
- }
- }
- if (found) {
- obj = ResolveResourceLocator(resLocation, key, copyOfCache, keyInWrongCase);
- }
- return obj;
- }
-
- }
-
-
-
-
- private object ResolveResourceLocator(ResourceLocator resLocation, string key, Dictionary<string, ResourceLocator> copyOfCache, bool keyInWrongCase)
- {
-
-
- object value = resLocation.Value;
- if (value == null) {
- ResourceTypeCode typeCode;
- lock (Reader) {
- value = _defaultReader.LoadObject(resLocation.DataPosition, out typeCode);
- }
- if (!keyInWrongCase && ResourceLocator.CanCache(typeCode)) {
- resLocation.Value = value;
- copyOfCache[key] = resLocation;
- }
- }
- return value;
- }
- }
- }