FileBasedResourceGroveler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. **
  7. ** Purpose: Searches for resources on disk, used for file-
  8. ** based resource lookup.
  9. **
  10. **
  11. ===========================================================*/
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Globalization;
  15. using System.IO;
  16. using Internal.IO;
  17. namespace System.Resources
  18. {
  19. internal class FileBasedResourceGroveler : IResourceGroveler
  20. {
  21. private readonly ResourceManager.ResourceManagerMediator _mediator;
  22. public FileBasedResourceGroveler(ResourceManager.ResourceManagerMediator mediator)
  23. {
  24. Debug.Assert(mediator != null, "mediator shouldn't be null; check caller");
  25. _mediator = mediator;
  26. }
  27. // Consider modifying IResourceGroveler interface (hence this method signature) when we figure out
  28. // serialization compat story for moving ResourceManager members to either file-based or
  29. // manifest-based classes. Want to continue tightening the design to get rid of unused params.
  30. public ResourceSet? GrovelForResourceSet(CultureInfo culture, Dictionary<string, ResourceSet> localResourceSets, bool tryParents, bool createIfNotExists)
  31. {
  32. Debug.Assert(culture != null, "culture shouldn't be null; check caller");
  33. string? fileName = null;
  34. ResourceSet? rs = null;
  35. // Don't use Assembly manifest, but grovel on disk for a file.
  36. // Create new ResourceSet, if a file exists on disk for it.
  37. string tempFileName = _mediator.GetResourceFileName(culture);
  38. fileName = FindResourceFile(culture, tempFileName);
  39. if (fileName == null)
  40. {
  41. if (tryParents)
  42. {
  43. // If we've hit top of the Culture tree, return.
  44. if (culture.HasInvariantCultureName)
  45. {
  46. // We really don't think this should happen - we always
  47. // expect the neutral locale's resources to be present.
  48. throw new MissingManifestResourceException(SR.MissingManifestResource_NoNeutralDisk + Environment.NewLineConst + "baseName: " + _mediator.BaseNameField + " locationInfo: " + (_mediator.LocationInfo == null ? "<null>" : _mediator.LocationInfo.FullName) + " fileName: " + _mediator.GetResourceFileName(culture));
  49. }
  50. }
  51. }
  52. else
  53. {
  54. rs = CreateResourceSet(fileName);
  55. }
  56. return rs;
  57. }
  58. // Given a CultureInfo, it generates the path &; file name for
  59. // the .resources file for that CultureInfo. This method will grovel
  60. // the disk looking for the correct file name & path. Uses CultureInfo's
  61. // Name property. If the module directory was set in the ResourceManager
  62. // constructor, we'll look there first. If it couldn't be found in the module
  63. // diretory or the module dir wasn't provided, look in the current
  64. // directory.
  65. private string? FindResourceFile(CultureInfo culture, string fileName)
  66. {
  67. Debug.Assert(culture != null, "culture shouldn't be null; check caller");
  68. Debug.Assert(fileName != null, "fileName shouldn't be null; check caller");
  69. // If we have a moduleDir, check there first. Get module fully
  70. // qualified name, append path to that.
  71. if (_mediator.ModuleDir != null)
  72. {
  73. string path = Path.Combine(_mediator.ModuleDir, fileName);
  74. if (File.Exists(path))
  75. {
  76. return path;
  77. }
  78. }
  79. // look in .
  80. if (File.Exists(fileName))
  81. return fileName;
  82. return null; // give up.
  83. }
  84. // Constructs a new ResourceSet for a given file name.
  85. private ResourceSet CreateResourceSet(string file)
  86. {
  87. Debug.Assert(file != null, "file shouldn't be null; check caller");
  88. if (_mediator.UserResourceSet == null)
  89. {
  90. return new RuntimeResourceSet(file);
  91. }
  92. else
  93. {
  94. object[] args = new object[1];
  95. args[0] = file;
  96. try
  97. {
  98. return (ResourceSet)Activator.CreateInstance(_mediator.UserResourceSet, args)!;
  99. }
  100. catch (MissingMethodException e)
  101. {
  102. throw new InvalidOperationException(SR.Format(SR.InvalidOperation_ResMgrBadResSet_Type, _mediator.UserResourceSet.AssemblyQualifiedName), e);
  103. }
  104. }
  105. }
  106. }
  107. }