// Copyright 2026 sjackson0109 — Apache License 2.0 using System.Reflection; namespace RDPWrap.Common; /// /// Helpers for reading and extracting managed embedded resources. /// Mirrors the Delphi ExtractRes / ExtractResText procedures from RDPWInst.dpr /// and RDPConf MainUnit.pas, translated to the .NET manifest-resource model. /// public static class ResourceHelper { /// /// Reads the contents of the embedded resource named /// from /// and returns it as a UTF-8 string. Returns null if not found. /// public static string? ReadText(string resourceName, Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); using var stream = assembly.GetManifestResourceStream(resourceName); if (stream is null) return null; using var reader = new StreamReader(stream, System.Text.Encoding.UTF8); return reader.ReadToEnd(); } /// /// Reads the contents of the embedded resource named /// and returns the raw bytes. /// Returns null if not found. /// public static byte[]? ReadBytes(string resourceName, Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); using var stream = assembly.GetManifestResourceStream(resourceName); if (stream is null) return null; using var ms = new MemoryStream(); stream.CopyTo(ms); return ms.ToArray(); } /// /// Extracts the embedded resource to disk /// at , creating parent directories as needed. /// Returns true on success. Mirrors the Delphi ExtractRes procedure. /// public static bool ExtractToDisk(string resourceName, string destPath, Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); using var stream = assembly.GetManifestResourceStream(resourceName); if (stream is null) { Console.Error.WriteLine($"[-] Resource not found: {resourceName}"); return false; } var dir = Path.GetDirectoryName(destPath); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir); try { using var file = File.Create(destPath); stream.CopyTo(file); Console.WriteLine($"[+] Extracted {resourceName} -> {destPath}"); return true; } catch (Exception ex) { Console.Error.WriteLine( $"[-] Failed to extract resource {resourceName} to {destPath}: {ex.Message}"); return false; } } /// /// Lists all manifest resource names in /// — useful for debugging resource name mismatches. /// public static IEnumerable ListResources(Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); return assembly.GetManifestResourceNames(); } }