-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
70 lines (60 loc) · 2.68 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Mono.Cecil;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace ReplaceEmbeddedAssemblyResource
{
class Program
{
static void Main(string[] args)
{
// TODO: Smarter handling of command line parameters if there ever are additional ones
if (args.Length < 4 || args.Length == 5 || args.Length > 6)
{
Console.Error.WriteLine("Expected arguments: <Assembly-Path> <New-Assembly-Path> <Resource-Name> <Resource-Path> [-snk <Strong-Name-Key-Path>]");
Environment.Exit(1);
}
else if (args.Length == 6 && args[4] != "-snk")
{
Console.Error.WriteLine("Available options are:");
Console.Error.WriteLine("\t-snk: Path to strong name key file (.snk).");
Environment.Exit(1);
}
var assemblyPath = args[0];
var newAssemblyPath = args[1];
var resourceName = args[2];
var resourcePath = args[3];
var snkPath = args.Length == 6 ? args[5] : null;
var assemblyDef = AssemblyDefinition.ReadAssembly(assemblyPath);
Console.WriteLine("Loaded assembly " + assemblyDef);
// TODO: Support other modules than MainModule
var resources = assemblyDef.MainModule.Resources;
var selectedResource = resources.FirstOrDefault(x => x.Name == resourceName);
if (selectedResource != null)
{
using (var filestream = File.OpenRead(resourcePath))
{
var newResource = new EmbeddedResource(resourceName, selectedResource.Attributes, filestream);
resources.Remove(selectedResource);
resources.Add(newResource);
if (snkPath == null)
{
assemblyDef.Write(newAssemblyPath);
}
else
{
Console.WriteLine("Using strong name key file " + snkPath);
assemblyDef.Write(newAssemblyPath, new WriterParameters() { StrongNameKeyPair = new StrongNameKeyPair(File.ReadAllBytes(snkPath)) });
}
}
Console.WriteLine("Replaced embedded resource " + resourceName + " successfully!");
}
else
{
Console.Error.WriteLine("Could not find a resource with name " + resourceName);
Console.Error.WriteLine("Available resources: " + String.Join(", ", resources.Select(x => x.Name).DefaultIfEmpty("<none>")));
}
}
}
}