// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.IO; using System.Reflection; namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers { public static class Common { /// /// Extracts an embedded file out of a given assembly. /// /// The namespace of you assembly. /// The name of the file to extract. /// A stream containing the file data. public static Stream GetEmbeddedFile(string assemblyName, string fileName) { try { Assembly a = Assembly.Load(assemblyName); Stream str = a.GetManifestResourceStream(assemblyName + "." + fileName); if (str == null) { throw new InvalidDataException("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'"); } return str; } catch (Exception e) { throw new InvalidDataException($"{assemblyName}: {e.Message}"); } } /// /// Extracts an embedded file out of a given assembly. /// /// The name of the resource which normally is the namespace of you assembly and the file. /// A stream containing the file data. public static Stream GetEmbeddedFile(Assembly assembly, string resourceName) { try { Stream str = assembly.GetManifestResourceStream(resourceName); if (str == null) { throw new InvalidDataException( $"Could not locate embedded resource '{resourceName}' in assembly '{assembly.GetName()}'"); } return str; } catch (Exception e) { throw new InvalidDataException($"{assembly.GetName()}: {e.Message}"); } } } }