├── .gitignore ├── ReplaceEmbeddedAssemblyResource.csproj ├── .github └── workflows │ └── build.yml ├── README.md └── Program.cs /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | obj 3 | -------------------------------------------------------------------------------- /ReplaceEmbeddedAssemblyResource.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | ReplaceEmbeddedAssemblyResource 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET Core SDK 17 | uses: actions/setup-dotnet@v1 18 | - name: Install dependencies 19 | run: dotnet restore 20 | - name: Build 21 | run: dotnet build --configuration Release --no-restore 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ReplaceEmbeddedAssemblyResource 2 | 3 | A small .NET Core program to replace embedded resources inside .NET assemblies. 4 | 5 | ## How to build 6 | 7 | dotnet build 8 | 9 | ## How to use 10 | 11 | dotnet bin/Debug/netcoreapp3.1/ReplaceEmbeddedAssemblyResource.dll [-snk ] 12 | 13 | Example arguments: 14 | 15 | Assembly.dll NewAssembly.dll Acme.Project.data.bin data.bin 16 | SignedAssembly.dll NewAssembly.dll Acme.Project.data.bin data.bin -snk KeyFile.snk 17 | 18 | ## License 19 | 20 | Copyright (c) 2013, Digifort Sverige AB 21 | Copyright (c) 2015, [dee-see](https://github.com/dee-see/) 22 | Copyright (c) 2018, [Tim Riemann](https://github.com/Octoate/) 23 | Copyright (c) 2020, Christoffer Sawicki 24 | All rights reserved. 25 | 26 | Redistribution and use in source and binary forms, with or without modification, 27 | are permitted provided that the following conditions are met: 28 | 29 | * Redistributions of source code must retain the above copyright notice, this 30 | list of conditions and the following disclaimer. 31 | * Redistributions in binary 32 | form must reproduce the above copyright notice, this list of conditions and the 33 | following disclaimer in the documentation and/or other materials provided with 34 | the distribution. 35 | * Neither the name of the Digifort Sverige AB nor the names of 36 | its contributors may be used to endorse or promote products derived from this 37 | software without specific prior written permission. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 40 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 41 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 43 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 44 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 46 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 47 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 48 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | using System; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace ReplaceEmbeddedAssemblyResource 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | // TODO: Smarter handling of command line parameters if there ever are additional ones 14 | if (args.Length < 4 || args.Length == 5 || args.Length > 6) 15 | { 16 | Console.Error.WriteLine("Expected arguments: [-snk ]"); 17 | Environment.Exit(1); 18 | } 19 | else if (args.Length == 6 && args[4] != "-snk") 20 | { 21 | Console.Error.WriteLine("Available options are:"); 22 | Console.Error.WriteLine("\t-snk: Path to strong name key file (.snk)."); 23 | Environment.Exit(1); 24 | } 25 | 26 | var assemblyPath = args[0]; 27 | var newAssemblyPath = args[1]; 28 | var resourceName = args[2]; 29 | var resourcePath = args[3]; 30 | var snkPath = args.Length == 6 ? args[5] : null; 31 | 32 | var assemblyDef = AssemblyDefinition.ReadAssembly(assemblyPath); 33 | 34 | Console.WriteLine("Loaded assembly " + assemblyDef); 35 | 36 | // TODO: Support other modules than MainModule 37 | 38 | var resources = assemblyDef.MainModule.Resources; 39 | 40 | var selectedResource = resources.FirstOrDefault(x => x.Name == resourceName); 41 | 42 | if (selectedResource != null) 43 | { 44 | using (var filestream = File.OpenRead(resourcePath)) 45 | { 46 | var newResource = new EmbeddedResource(resourceName, selectedResource.Attributes, filestream); 47 | resources.Remove(selectedResource); 48 | resources.Add(newResource); 49 | 50 | if (snkPath == null) 51 | { 52 | assemblyDef.Write(newAssemblyPath); 53 | } 54 | else 55 | { 56 | Console.WriteLine("Using strong name key file " + snkPath); 57 | assemblyDef.Write(newAssemblyPath, new WriterParameters() { StrongNameKeyPair = new StrongNameKeyPair(File.ReadAllBytes(snkPath)) }); 58 | } 59 | } 60 | 61 | Console.WriteLine("Replaced embedded resource " + resourceName + " successfully!"); 62 | } 63 | else 64 | { 65 | Console.Error.WriteLine("Could not find a resource with name " + resourceName); 66 | Console.Error.WriteLine("Available resources: " + String.Join(", ", resources.Select(x => x.Name).DefaultIfEmpty(""))); 67 | } 68 | } 69 | } 70 | } 71 | --------------------------------------------------------------------------------