45ac9240a1fe091bb17ae7ac553780d765e08b9c
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / LoadI18nAssemblies.cs
1 //
2 // LoadI18nAssemblies.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30
31 using Mono.Cecil;
32
33 namespace Mono.Linker.Steps {
34
35         public class LoadI18nAssemblies : BaseStep {
36
37                 static readonly byte [] _pktoken = new byte [] {0x07, 0x38, 0xeb, 0x9f, 0x13, 0x2e, 0xd7, 0x56};
38
39                 I18nAssemblies _assemblies;
40
41                 public LoadI18nAssemblies (I18nAssemblies assemblies)
42                 {
43                         _assemblies = assemblies;
44                 }
45
46                 protected override bool ConditionToProcess ()
47                 {
48                         return _assemblies != I18nAssemblies.None &&
49                                 Type.GetType ("System.MonoType") != null;
50                 }
51
52                 protected override void Process()
53                 {
54                         LoadAssembly (GetAssemblyName (I18nAssemblies.Base));
55
56                         LoadI18nAssembly (I18nAssemblies.CJK);
57                         LoadI18nAssembly (I18nAssemblies.MidEast);
58                         LoadI18nAssembly (I18nAssemblies.Other);
59                         LoadI18nAssembly (I18nAssemblies.Rare);
60                         LoadI18nAssembly (I18nAssemblies.West);
61                 }
62
63                 bool ShouldCopyAssembly (I18nAssemblies current)
64                 {
65                         return (current & _assemblies) != 0;
66                 }
67
68                 void LoadI18nAssembly (I18nAssemblies asm)
69                 {
70                         if (!ShouldCopyAssembly (asm))
71                                 return;
72
73                         AssemblyNameReference name = GetAssemblyName (asm);
74                         LoadAssembly (name);
75                 }
76
77                 void LoadAssembly (AssemblyNameReference name)
78                 {
79                         AssemblyDefinition assembly = Context.Resolve (name);
80                         ResolveFromAssemblyStep.ProcessLibrary (Context, assembly);
81                 }
82
83                 AssemblyNameReference GetAssemblyName (I18nAssemblies assembly)
84                 {
85                         AssemblyNameReference name = new AssemblyNameReference ("I18N", GetCorlibVersion ());
86                         if (assembly != I18nAssemblies.Base)
87                                 name.Name += "." + assembly;
88
89                         name.PublicKeyToken = _pktoken;
90                         return name;
91                 }
92
93                 Version GetCorlibVersion ()
94                 {
95                         foreach (AssemblyDefinition assembly in Context.GetAssemblies ())
96                                 if (assembly.Name.Name == "mscorlib")
97                                         return assembly.Name.Version;
98
99                         return new Version ();
100                 }
101         }
102 }