Don't crash when loading fonts from CLR DLLs

This commit is contained in:
Yoshi Askharoun
2025-07-29 00:07:28 -05:00
parent 7cba2bf1fd
commit 303a5f84b0
2 changed files with 28 additions and 10 deletions
+15 -6
View File
@@ -210,14 +210,23 @@ namespace Microsoft.Iris.Markup.UIX
private static object CallLoadFontResourceStringString(object instanceObj, object[] parameters) private static object CallLoadFontResourceStringString(object instanceObj, object[] parameters)
{ {
string parameter1 = (string)parameters[0]; string moduleName = (string)parameters[0];
string parameter2 = (string)parameters[1]; string resourceName = (string)parameters[1];
if (string.IsNullOrEmpty(parameter1)) if (string.IsNullOrEmpty(moduleName))
ErrorManager.ReportError("Script runtime failure: Invalid 'null' value for '{0}'", "moduleName"); ErrorManager.ReportError("Script runtime failure: Invalid 'null' value for '{0}'", "moduleName");
if (string.IsNullOrEmpty(parameter2)) if (string.IsNullOrEmpty(resourceName))
ErrorManager.ReportError("Script runtime failure: Invalid 'null' value for '{0}'", "resourceName"); ErrorManager.ReportError("Script runtime failure: Invalid 'null' value for '{0}'", "resourceName");
if (!NativeApi.SpLoadFontResource(parameter1, parameter2))
ErrorManager.ReportError("Font Resource {1} not found in module {0}", parameter1, parameter2); // Check if DLL is a .NET assembly
var assemblyName = System.IO.Path.GetFileNameWithoutExtension(moduleName);
if (ClrDllResources.Instance.TryGetResource($"{assemblyName}!{resourceName}", $"clr-res://{assemblyName}", true, out var resource))
{
return null;
}
if (!NativeApi.SpLoadFontResource(moduleName, resourceName))
ErrorManager.ReportError("Font Resource {1} not found in module {0}", moduleName, resourceName);
return null; return null;
} }
+13 -4
View File
@@ -23,17 +23,26 @@ namespace Microsoft.Iris.OS
public Resource GetResource(string hierarchicalPart, string uri, bool forceSynchronous) public Resource GetResource(string hierarchicalPart, string uri, bool forceSynchronous)
{ {
Resource resource = null; if (!TryGetResource(hierarchicalPart, uri, forceSynchronous, out var resource))
ErrorManager.ReportError($"Invalid resource uri: '{uri}'");
return resource;
}
public bool TryGetResource(string hierarchicalPart, string uri, bool forceSynchronous, out Resource resource)
{
resource = null;
DllResources.ParseResource(hierarchicalPart, out string host, out string identifier); DllResources.ParseResource(hierarchicalPart, out string host, out string identifier);
if (host != null) if (host != null)
{ {
Assembly assembly = GetAssembly(host); Assembly assembly = GetAssembly(host);
if (assembly != null) if (assembly != null)
resource = new ClrDllResource(uri, assembly, identifier); resource = new ClrDllResource(uri, assembly, identifier);
} }
if (resource == null)
ErrorManager.ReportError($"Invalid resource uri: '{uri}'"); return resource is not null;
return resource;
} }
public Assembly GetAssembly(string moduleName) public Assembly GetAssembly(string moduleName)