2004-01-15 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
1 //
2 // System.Reflection.Emit/AssemblyBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Resources;
13 using System.IO;
14 using System.Security.Policy;
15 using System.Runtime.Serialization;
16 using System.Globalization;
17 using System.Runtime.CompilerServices;
18 using System.Collections;
19 using System.Runtime.InteropServices;
20 using System.Security.Cryptography;
21 using System.Security.Permissions;
22
23 using Mono.Security;
24 using Mono.Security.Cryptography;
25
26 namespace System.Reflection.Emit {
27
28         internal struct RefEmitPermissionSet {
29                 public SecurityAction action;
30                 public string pset;
31
32                 public RefEmitPermissionSet (SecurityAction action, string pset) {
33                         this.action = action;
34                         this.pset = pset;
35                 }
36         }
37
38         internal struct MonoResource {
39                 public byte[] data;
40                 public string name;
41                 public string filename;
42                 public ResourceAttributes attrs;
43                 public int offset;
44         }
45
46         internal struct MonoWin32Resource {
47                 public int res_type;
48                 public int res_id;
49                 public int lang_id;
50                 public byte[] data;
51
52                 public MonoWin32Resource (int res_type, int res_id, int lang_id, byte[] data) {
53                         this.res_type = res_type;
54                         this.res_id = res_id;
55                         this.lang_id = lang_id;
56                         this.data = data;
57                 }
58         }
59
60         public sealed class AssemblyBuilder : Assembly {
61                 #region Sync with reflection.h
62                 private IntPtr dynamic_assembly;
63                 private MethodInfo entry_point;
64                 private ModuleBuilder[] modules;
65                 private string name;
66                 private string dir;
67                 private CustomAttributeBuilder[] cattrs;
68                 private MonoResource[] resources;
69                 byte[] public_key;
70                 string version;
71                 string culture;
72                 uint algid;
73                 uint flags;
74                 PEFileKinds pekind = PEFileKinds.Dll;
75                 bool delay_sign;
76                 uint access;
77                 Module[] loaded_modules;
78                 MonoWin32Resource[] win32_resources;
79                 #endregion
80                 internal Type corlib_object_type = typeof (System.Object);
81                 internal Type corlib_value_type = typeof (System.ValueType);
82                 internal Type corlib_enum_type = typeof (System.Enum);
83                 internal Type corlib_void_type = typeof (void);
84                 ArrayList resource_writers = null;
85                 Win32VersionResource version_res;
86                 bool created;
87                 bool is_module_only;
88
89                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90                 private static extern void basic_init (AssemblyBuilder ab);
91                 
92                 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
93                         name = n.Name;
94                         if (directory == null || directory == String.Empty)
95                                 dir = Directory.GetCurrentDirectory ();
96                         else
97                                 dir = directory;
98                         this.access = (uint)access;
99
100                         /* Set defaults from n */
101                         if (n.CultureInfo != null) {
102                                 culture = n.CultureInfo.Name;
103                         }
104                         Version v = n.Version;
105                         if (v != null) {
106                                 version = v.ToString ();
107                         }
108
109                         basic_init (this);
110                 }
111
112                 public override string CodeBase {
113                         get {
114                                 throw not_supported ();
115                         }
116                 }
117                 
118                 public override MethodInfo EntryPoint {
119                         get {
120                                 return entry_point;
121                         }
122                 }
123
124                 public override string Location {
125                         get {
126                                 throw not_supported ();
127                         }
128                 }
129
130 #if NET_1_1
131                 /* This is to keep signature compatibility with MS.NET */
132                 public override string ImageRuntimeVersion {
133                         get {
134                                 return base.ImageRuntimeVersion;
135                         }
136                 }
137 #endif
138
139                 public void AddResourceFile (string name, string fileName)
140                 {
141                         AddResourceFile (name, fileName, ResourceAttributes.Public);
142                 }
143
144                 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
145                 {
146                         AddResourceFile (name, fileName, attribute, true);
147                 }
148
149                 private void AddResourceFile (string name, string fileName, ResourceAttributes attribute, bool fileNeedsToExists)
150                 {
151                         check_name_and_filename (name, fileName, fileNeedsToExists);
152
153                         // Resource files are created/searched under the assembly storage
154                         // directory
155                         if (dir != null)
156                                 fileName = Path.Combine (dir, fileName);
157
158                         if (resources != null) {
159                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
160                                 System.Array.Copy(resources, new_r, resources.Length);
161                                 resources = new_r;
162                         } else {
163                                 resources = new MonoResource [1];
164                         }
165                         int p = resources.Length - 1;
166                         resources [p].name = name;
167                         resources [p].filename = fileName;
168                         resources [p].attrs = attribute;
169                 }
170
171                 public void EmbedResourceFile (string name, string fileName)
172                 {
173                         EmbedResourceFile (name, fileName, ResourceAttributes.Public);
174                 }
175
176                 public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
177                 {
178                         if (resources != null) {
179                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
180                                 System.Array.Copy(resources, new_r, resources.Length);
181                                 resources = new_r;
182                         } else {
183                                 resources = new MonoResource [1];
184                         }
185                         int p = resources.Length - 1;
186                         resources [p].name = name;
187                         resources [p].attrs = attribute;
188                         try {
189                                 FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
190                                 long len = s.Length;
191                                 resources [p].data = new byte [len];
192                                 s.Read (resources [p].data, 0, (int)len);
193                                 s.Close ();
194                         } catch {
195                                 /* do something */
196                         }
197                 }
198
199                 internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute)
200                 {
201                         if (resources != null) {
202                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
203                                 System.Array.Copy(resources, new_r, resources.Length);
204                                 resources = new_r;
205                         } else {
206                                 resources = new MonoResource [1];
207                         }
208                         int p = resources.Length - 1;
209                         resources [p].name = name;
210                         resources [p].attrs = attribute;
211                         resources [p].data = blob;
212                 }
213
214                 public ModuleBuilder DefineDynamicModule (string name)
215                 {
216                         return DefineDynamicModule (name, name, false, true);
217                 }
218
219                 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
220                 {
221                         return DefineDynamicModule (name, name, emitSymbolInfo, true);
222                 }
223
224                 public ModuleBuilder DefineDynamicModule(string name, string fileName)
225                 {
226                         return DefineDynamicModule (name, fileName, false, false);
227                 }
228
229                 public ModuleBuilder DefineDynamicModule (string name, string fileName,
230                                                           bool emitSymbolInfo)
231                 {
232                         return DefineDynamicModule (name, fileName, emitSymbolInfo, false);
233                 }
234
235                 private ModuleBuilder DefineDynamicModule (string name, string fileName,
236                                                                                                    bool emitSymbolInfo, bool transient)
237                 {
238                         check_name_and_filename (name, fileName, false);
239
240                         if (!transient) {
241                                 if (Path.GetExtension (fileName) == String.Empty)
242                                         throw new ArgumentException ("Module file name '" + fileName + "' must have file extension.");
243                                 if (!IsSave)
244                                         throw new NotSupportedException ("Persistable modules are not supported in a dynamic assembly created with AssemblyBuilderAccess.Run");
245                                 if (created)
246                                         throw new InvalidOperationException ("Assembly was already saved.");
247                         }
248
249                         ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, transient);
250
251                         if ((modules != null) && is_module_only)
252                                 throw new InvalidOperationException ("A module-only assembly can only contain one module.");
253
254                         if (modules != null) {
255                                 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
256                                 System.Array.Copy(modules, new_modules, modules.Length);
257                                 modules = new_modules;
258                         } else {
259                                 modules = new ModuleBuilder [1];
260                         }
261                         modules [modules.Length - 1] = r;
262                         return r;
263                 }
264
265                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
266                 private extern Module InternalAddModule (string fileName);
267
268                 /*
269                  * Mono extension to support /addmodule in mcs.
270                  */
271                 internal Module AddModule (string fileName)
272                 {
273                         if (fileName == null)
274                                 throw new ArgumentNullException (fileName);
275
276                         Module m = InternalAddModule (fileName);
277
278                         if (loaded_modules != null) {
279                                 Module[] new_modules = new Module [loaded_modules.Length + 1];
280                                 System.Array.Copy (loaded_modules, new_modules, loaded_modules.Length);
281                                 loaded_modules = new_modules;
282                         } else {
283                                 loaded_modules = new Module [1];
284                         }
285                         loaded_modules [loaded_modules.Length - 1] = m;
286
287                         return m;
288                 }
289
290                 public IResourceWriter DefineResource (string name, string description, string fileName)
291                 {
292                         return DefineResource (name, description, fileName, ResourceAttributes.Public);
293                 }
294
295                 public IResourceWriter DefineResource (string name, string description,
296                                                        string fileName, ResourceAttributes attribute)
297                 {
298                         IResourceWriter writer;
299
300                         // description seems to be ignored
301                         AddResourceFile (name, fileName, attribute, false);
302                         writer = new ResourceWriter (fileName);
303                         if (resource_writers == null)
304                                 resource_writers = new ArrayList ();
305                         resource_writers.Add (writer);
306                         return writer;
307                 }
308
309                 private void AddUnmanagedResource (Win32Resource res) {
310                         MemoryStream ms = new MemoryStream ();
311                         res.WriteTo (ms);
312
313                         if (win32_resources != null) {
314                                 MonoWin32Resource[] new_res = new MonoWin32Resource [win32_resources.Length + 1];
315                                 System.Array.Copy (win32_resources, new_res, win32_resources.Length);
316                                 win32_resources = new_res;
317                         }
318                         else
319                                 win32_resources = new MonoWin32Resource [1];
320
321                         win32_resources [win32_resources.Length - 1] = new MonoWin32Resource (res.Type.Id, res.Name.Id, res.Language, ms.ToArray ());
322                 }
323
324                 [MonoTODO]
325                 public void DefineUnmanagedResource (byte[] resource)
326                 {
327                         if (resource == null)
328                                 throw new ArgumentNullException ("resource");
329
330                         /*
331                          * The format of the argument byte array is not documented
332                          * so this method is impossible to implement.
333                          */
334
335                         throw new NotImplementedException ();
336                 }
337
338                 public void DefineUnmanagedResource (string resourceFileName)
339                 {
340                         if (resourceFileName == null)
341                                 throw new ArgumentNullException ("resourceFileName");
342                         if (resourceFileName == String.Empty)
343                                 throw new ArgumentException ("resourceFileName");
344                         if (!File.Exists (resourceFileName) || Directory.Exists (resourceFileName))
345                                 throw new FileNotFoundException ("File '" + resourceFileName + "' does not exists or is a directory.");
346
347                         using (FileStream fs = new FileStream (resourceFileName, FileMode.Open)) {
348                                 Win32ResFileReader reader = new Win32ResFileReader (fs);
349
350                                 foreach (Win32EncodedResource res in reader.ReadResources ()) {
351                                         if (res.Name.IsName || res.Type.IsName)
352                                                 throw new InvalidOperationException ("resource files with named resources or non-default resource types are not supported.");
353
354                                         AddUnmanagedResource (res);
355                                 }
356                         }
357                 }
358
359                 public void DefineVersionInfoResource ()
360                 {
361                         if (version_res != null)
362                                 throw new ArgumentException ("Native resource has already been defined.");                      
363
364                         version_res = new Win32VersionResource (1, 0);
365
366                         if (cattrs != null) {
367                                 foreach (CustomAttributeBuilder cb in cattrs) {
368                                         string attrname = cb.Ctor.ReflectedType.FullName;
369
370                                         if (attrname == "System.Reflection.AssemblyProductAttribute")
371                                                 version_res.ProductName = cb.string_arg ();
372                                         else if (attrname == "System.Reflection.AssemblyCompanyAttribute")
373                                                 version_res.CompanyName = cb.string_arg ();
374                                         else if (attrname == "System.Reflection.AssemblyCopyrightAttribute")
375                                                 version_res.LegalCopyright = cb.string_arg ();
376                                         else if (attrname == "System.Reflection.AssemblyTrademarkAttribute")
377                                                 version_res.LegalTrademarks = cb.string_arg ();
378                                         else if (attrname == "System.Reflection.AssemblyCultureAttribute")
379                                                 version_res.FileLanguage = new CultureInfo (cb.string_arg ()).LCID;
380                                         else if (attrname == "System.Reflection.AssemblyFileVersionAttribute")
381                                                 version_res.FileVersion = cb.string_arg ();
382                                         else if (attrname == "System.Reflection.AssemblyInformationalVersionAttribute")
383                                                 version_res.ProductVersion = cb.string_arg ();
384                                         else if (attrname == "System.Reflection.AssemblyTitleAttribute")
385                                                 version_res.FileDescription = cb.string_arg ();
386                                         else if (attrname == "System.Reflection.AssemblyDescriptionAttribute")
387                                                 version_res.Comments = cb.string_arg ();
388                                 }
389                         }
390                 }
391
392                 public void DefineVersionInfoResource (string product, string productVersion,
393                                                        string company, string copyright, string trademark)
394                 {
395                         if (version_res != null)
396                                 throw new ArgumentException ("Native resource has already been defined.");
397
398                         /*
399                          * We can only create the resource later, when the file name and
400                          * the binary version is known.
401                          */
402
403                         version_res = new Win32VersionResource (1, 0);
404                         version_res.ProductName = product;
405                         version_res.ProductVersion = productVersion;
406                         version_res.CompanyName = company;
407                         version_res.LegalCopyright = copyright;
408                         version_res.LegalTrademarks = trademark;
409                 }
410
411                 /* 
412                  * Mono extension to support /win32icon in mcs
413                  */
414                 internal void DefineIconResource (string iconFileName)
415                 {
416                         if (iconFileName == null)
417                                 throw new ArgumentNullException ("iconFileName");
418                         if (iconFileName == String.Empty)
419                                 throw new ArgumentException ("iconFileName");
420                         if (!File.Exists (iconFileName) || Directory.Exists (iconFileName))
421                                 throw new FileNotFoundException ("File '" + iconFileName + "' does not exists or is a directory.");
422
423                         using (FileStream fs = new FileStream (iconFileName, FileMode.Open)) {
424                                 Win32IconFileReader reader = new Win32IconFileReader (fs);
425                                 
426                                 ICONDIRENTRY[] entries = reader.ReadIcons ();
427
428                                 Win32IconResource[] icons = new Win32IconResource [entries.Length];
429                                 for (int i = 0; i < entries.Length; ++i) {
430                                         icons [i] = new Win32IconResource (i + 1, 0, entries [i]);
431                                         AddUnmanagedResource (icons [i]);
432                                 }
433
434                                 Win32GroupIconResource group = new Win32GroupIconResource (1, 0, icons);
435                                 AddUnmanagedResource (group);
436                         }
437                 }
438
439                 private void DefineVersionInfoResourceImpl (string fileName) {
440                         // Add missing info
441                         if (version_res.FileVersion == "0.0.0.0")
442                                 version_res.FileVersion = version;
443                         version_res.InternalName = Path.GetFileNameWithoutExtension (fileName);
444                         version_res.OriginalFilename = fileName;
445
446                         AddUnmanagedResource (version_res);
447                 }
448
449                 public ModuleBuilder GetDynamicModule (string name)
450                 {
451                         if (name == null)
452                                 throw new ArgumentNullException ("name");
453                         if (name == "")
454                                 throw new ArgumentException ("Name can't be null");
455
456                         if (modules != null)
457                                 for (int i = 0; i < modules.Length; ++i)
458                                         if (modules [i].name == name)
459                                                 return modules [i];
460                         return null;
461                 }
462
463                 public override Type[] GetExportedTypes ()
464                 {
465                         throw not_supported ();
466                 }
467
468                 public override FileStream GetFile (string name)
469                 {
470                         throw not_supported ();
471                 }
472
473                 public override FileStream[] GetFiles(bool getResourceModules) {
474                         throw not_supported ();
475                 }
476
477                 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) {
478                         throw not_supported ();
479                 }
480
481                 public override string[] GetManifestResourceNames() {
482                         throw not_supported ();
483                 }
484
485                 public override Stream GetManifestResourceStream(string name) {
486                         throw not_supported ();
487                 }
488                 public override Stream GetManifestResourceStream(Type type, string name) {
489                         throw not_supported ();
490                 }
491
492                 internal bool IsSave {
493                         get {
494                                 return access != (uint)AssemblyBuilderAccess.Run;
495                         }
496                 }
497
498                 internal string AssemblyDir {
499                         get {
500                                 return dir;
501                         }
502                 }
503
504                 /*
505                  * Mono extension. If this is set, the assembly can only contain one
506                  * module, access should be Save, and the saved image will not contain an
507                  * assembly manifest.
508                  */
509                 internal bool IsModuleOnly {
510                         get {
511                                 return is_module_only;
512                         }
513                         set {
514                                 is_module_only = value;
515                         }
516                 }
517
518                 public void Save (string assemblyFileName)
519                 {
520                         if (resource_writers != null) {
521                                 foreach (IResourceWriter writer in resource_writers) {
522                                         writer.Generate ();
523                                         writer.Close ();
524                                 }
525                         }
526
527                         // Create a main module if not already created
528                         ModuleBuilder mainModule = null;
529                         if (modules != null) {
530                                 foreach (ModuleBuilder module in modules)
531                                         if (module.FullyQualifiedName == assemblyFileName)
532                                                 mainModule = module;
533                         }
534                         if (mainModule == null)
535                                 mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
536
537                         if (!is_module_only)
538                                 mainModule.IsMain = true;
539
540                         /* 
541                          * Create a new entry point if the one specified
542                          * by the user is in another module.
543                          */
544                         if ((entry_point != null) && entry_point.DeclaringType.Module != mainModule) {
545                                 Type[] paramTypes;
546                                 if (entry_point.GetParameters ().Length == 1)
547                                         paramTypes = new Type [] { typeof (string) };
548                                 else
549                                         paramTypes = new Type [0];
550
551                                 MethodBuilder mb = mainModule.DefineGlobalMethod ("__EntryPoint$", MethodAttributes.Static|MethodAttributes.PrivateScope, entry_point.ReturnType, paramTypes);
552                                 ILGenerator ilgen = mb.GetILGenerator ();
553                                 if (paramTypes.Length == 1)
554                                         ilgen.Emit (OpCodes.Ldarg_0);
555                                 ilgen.Emit (OpCodes.Tailcall);
556                                 ilgen.Emit (OpCodes.Call, entry_point);
557                                 ilgen.Emit (OpCodes.Ret);
558
559                                 entry_point = mb;
560                         }
561
562                         if (version_res != null)
563                                 DefineVersionInfoResourceImpl (assemblyFileName);
564                         
565                         foreach (ModuleBuilder module in modules)
566                                 if (module != mainModule)
567                                         module.Save ();
568
569                         // Write out the main module at the end, because it needs to
570                         // contain the hash of the other modules
571                         mainModule.Save ();
572
573                         created = true;
574                 }
575
576                 public void SetEntryPoint (MethodInfo entryMethod)
577                 {
578                         SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
579                 }
580
581                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
582                 {
583                         if (entryMethod == null)
584                                 throw new ArgumentNullException ("entryMethod");
585                         if (entryMethod.DeclaringType.Assembly != this)
586                                 throw new InvalidOperationException ("Entry method is not defined in the same assembly.");
587
588                         entry_point = entryMethod;
589                         pekind = fileKind;
590                 }
591
592                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) 
593                 {
594                         if (customBuilder == null)
595                                 throw new ArgumentNullException ("customBuilder");
596
597                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
598                         byte[] data;
599                         int len, pos;
600                         Mono.Security.StrongName sn;
601                         if (attrname == "System.Reflection.AssemblyVersionAttribute") {
602                                 version = create_assembly_version (customBuilder.string_arg ());
603                                 return;
604                         } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
605                                 string keyfile_name = customBuilder.string_arg ();
606                                 if (keyfile_name == String.Empty)
607                                         return;
608                                 using (FileStream fs = new FileStream (keyfile_name, FileMode.Open)) {
609                                         byte[] snkeypair = new byte [fs.Length];
610                                         fs.Read (snkeypair, 0, snkeypair.Length);
611
612                                         // this will import public or private/public keys
613                                         RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
614                                         // and export only the public part
615                                         sn = new Mono.Security.StrongName (rsa);
616                                         public_key = sn.PublicKey;
617                                 }
618                                 return;
619                         } else if (attrname == "System.Reflection.AssemblyKeyNameAttribute") {
620                                 string key_name = customBuilder.string_arg ();
621                                 if (key_name == String.Empty)
622                                         return;
623                                 CspParameters csparam = new CspParameters ();
624                                 csparam.KeyContainerName = key_name;
625                                 RSA rsacsp = new RSACryptoServiceProvider (csparam);
626                                 sn = new Mono.Security.StrongName (rsacsp);
627                                 public_key = sn.PublicKey;
628                                 return;
629                         } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
630                                 culture = customBuilder.string_arg ();
631                         } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
632                                 data = customBuilder.Data;
633                                 pos = 2;
634                                 algid = (uint)data [pos];
635                                 algid |= ((uint)data [pos + 1]) << 8;
636                                 algid |= ((uint)data [pos + 2]) << 16;
637                                 algid |= ((uint)data [pos + 3]) << 24;
638                         } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
639                                 data = customBuilder.Data;
640                                 pos = 2;
641                                 flags = (uint)data [pos];
642                                 flags |= ((uint)data [pos + 1]) << 8;
643                                 flags |= ((uint)data [pos + 2]) << 16;
644                                 flags |= ((uint)data [pos + 3]) << 24;
645                                 return;
646                         } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
647                                 data = customBuilder.Data;
648                                 pos = 2;
649                                 delay_sign = data [2] != 0;
650                         }
651                         if (cattrs != null) {
652                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
653                                 cattrs.CopyTo (new_array, 0);
654                                 new_array [cattrs.Length] = customBuilder;
655                                 cattrs = new_array;
656                         } else {
657                                 cattrs = new CustomAttributeBuilder [1];
658                                 cattrs [0] = customBuilder;
659                         }
660                 }
661                 public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
662                         if (con == null)
663                                 throw new ArgumentNullException ("con");
664                         if (binaryAttribute == null)
665                                 throw new ArgumentNullException ("binaryAttribute");
666
667                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
668                 }
669
670                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
671                         this.corlib_object_type = corlib_object_type;
672                         this.corlib_value_type = corlib_value_type;
673                         this.corlib_enum_type = corlib_enum_type;
674                 }
675
676                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
677                 {
678                         SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
679                         this.corlib_void_type = corlib_void_type;
680                 }
681
682                 private Exception not_supported () {
683                         // Strange message but this is what MS.NET prints...
684                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
685                 }
686
687                 private void check_name_and_filename (string name, string fileName,
688                                                                                           bool fileNeedsToExists) {
689                         if (name == null)
690                                 throw new ArgumentNullException ("name");
691                         if (fileName == null)
692                                 throw new ArgumentNullException ("fileName");
693                         if (name == "")
694                                 throw new ArgumentException ("name cannot be empty", "name");
695                         if (fileName == "")
696                                 throw new ArgumentException ("fileName cannot be empty", "fileName");
697                         if (Path.GetFileName (fileName) != fileName)
698                                 throw new ArgumentException ("fileName '" + fileName + "' must not include a path.");
699
700                         // Resource files are created/searched under the assembly storage
701                         // directory
702                         string fullFileName = fileName;
703                         if (dir != null)
704                                 fullFileName = Path.Combine (dir, fileName);
705
706                         if (fileNeedsToExists && !File.Exists (fullFileName))
707                                 throw new FileNotFoundException ("Could not find file '" + fileName + "'");
708
709                         if (resources != null) {
710                                 for (int i = 0; i < resources.Length; ++i) {
711                                         if (resources [i].filename == fullFileName)
712                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
713                                         if (resources [i].name == name)
714                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
715                                 }
716                         }
717
718                         if (modules != null) {
719                                 for (int i = 0; i < modules.Length; ++i) {
720                                         // Use fileName instead of fullFileName here
721                                         if (!modules [i].IsTransient () && (modules [i].FileName == fileName))
722                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
723                                         if (modules [i].Name == name)
724                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
725                                 }
726                         }
727                 }
728
729                 private String create_assembly_version (String version) {
730                         String[] parts = version.Split ('.');
731                         int[] ver = new int [4] { 0, 0, 0, 0 };
732
733                         if ((parts.Length < 0) || (parts.Length > 4))
734                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
735
736                         for (int i = 0; i < parts.Length; ++i) {
737                                 if (parts [i] == "*") {
738                                         DateTime now = DateTime.Now;
739
740                                         if (i == 2) {
741                                                 ver [2] = (now - new DateTime (2000, 1, 1)).Days;
742                                                 if (parts.Length == 3)
743                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
744                                         }
745                                         else
746                                                 if (i == 3)
747                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
748                                         else
749                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
750                                 }
751                                 else {
752                                         try {
753                                                 ver [i] = Int32.Parse (parts [i]);
754                                         }
755                                         catch (FormatException) {
756                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
757                                         }
758                                 }
759                         }
760
761                         return ver [0] + "." + ver [1] + "." + ver [2] + "." + ver [3];
762                 }
763         }
764 }