2004-01-08 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                                 }
383                         }
384                 }
385
386                 public void DefineVersionInfoResource (string product, string productVersion,
387                                                        string company, string copyright, string trademark)
388                 {
389                         if (version_res != null)
390                                 throw new ArgumentException ("Native resource has already been defined.");
391
392                         /*
393                          * We can only create the resource later, when the file name and
394                          * the binary version is known.
395                          */
396
397                         version_res = new Win32VersionResource (1, 0);
398                         version_res.ProductName = product;
399                         version_res.ProductVersion = productVersion;
400                         version_res.CompanyName = company;
401                         version_res.LegalCopyright = copyright;
402                         version_res.LegalTrademarks = trademark;
403                 }
404
405                 /* 
406                  * Mono extension to support /win32icon in mcs
407                  */
408                 internal void DefineIconResource (string iconFileName)
409                 {
410                         if (iconFileName == null)
411                                 throw new ArgumentNullException ("iconFileName");
412                         if (iconFileName == String.Empty)
413                                 throw new ArgumentException ("iconFileName");
414                         if (!File.Exists (iconFileName) || Directory.Exists (iconFileName))
415                                 throw new FileNotFoundException ("File '" + iconFileName + "' does not exists or is a directory.");
416
417                         using (FileStream fs = new FileStream (iconFileName, FileMode.Open)) {
418                                 Win32IconFileReader reader = new Win32IconFileReader (fs);
419                                 
420                                 ICONDIRENTRY[] entries = reader.ReadIcons ();
421
422                                 Win32IconResource[] icons = new Win32IconResource [entries.Length];
423                                 for (int i = 0; i < entries.Length; ++i) {
424                                         icons [i] = new Win32IconResource (i + 1, 0, entries [i]);
425                                         AddUnmanagedResource (icons [i]);
426                                 }
427
428                                 Win32GroupIconResource group = new Win32GroupIconResource (1, 0, icons);
429                                 AddUnmanagedResource (group);
430                         }
431                 }
432
433                 private void DefineVersionInfoResourceImpl (string fileName) {
434                         // Add missing info
435                         if (version_res.FileVersion == "0.0.0.0")
436                                 version_res.FileVersion = version;
437                         version_res.OriginalFilename = fileName;
438
439                         AddUnmanagedResource (version_res);
440                 }
441
442                 public ModuleBuilder GetDynamicModule (string name)
443                 {
444                         if (name == null)
445                                 throw new ArgumentNullException ("name");
446                         if (name == "")
447                                 throw new ArgumentException ("Name can't be null");
448
449                         if (modules != null)
450                                 for (int i = 0; i < modules.Length; ++i)
451                                         if (modules [i].name == name)
452                                                 return modules [i];
453                         return null;
454                 }
455
456                 public override Type[] GetExportedTypes ()
457                 {
458                         throw not_supported ();
459                 }
460
461                 public override FileStream GetFile (string name)
462                 {
463                         throw not_supported ();
464                 }
465
466                 public override FileStream[] GetFiles(bool getResourceModules) {
467                         throw not_supported ();
468                 }
469
470                 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) {
471                         throw not_supported ();
472                 }
473
474                 public override string[] GetManifestResourceNames() {
475                         throw not_supported ();
476                 }
477
478                 public override Stream GetManifestResourceStream(string name) {
479                         throw not_supported ();
480                 }
481                 public override Stream GetManifestResourceStream(Type type, string name) {
482                         throw not_supported ();
483                 }
484
485                 internal bool IsSave {
486                         get {
487                                 return access != (uint)AssemblyBuilderAccess.Run;
488                         }
489                 }
490
491                 internal string AssemblyDir {
492                         get {
493                                 return dir;
494                         }
495                 }
496
497                 /*
498                  * Mono extension. If this is set, the assembly can only contain one
499                  * module, access should be Save, and the saved image will not contain an
500                  * assembly manifest.
501                  */
502                 internal bool IsModuleOnly {
503                         get {
504                                 return is_module_only;
505                         }
506                         set {
507                                 is_module_only = value;
508                         }
509                 }
510
511                 public void Save (string assemblyFileName)
512                 {
513                         if (resource_writers != null) {
514                                 foreach (IResourceWriter writer in resource_writers) {
515                                         writer.Generate ();
516                                         writer.Close ();
517                                 }
518                         }
519
520                         // Create a main module if not already created
521                         ModuleBuilder mainModule = null;
522                         if (modules != null) {
523                                 foreach (ModuleBuilder module in modules)
524                                         if (module.FullyQualifiedName == assemblyFileName)
525                                                 mainModule = module;
526                         }
527                         if (mainModule == null)
528                                 mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
529
530                         if (!is_module_only)
531                                 mainModule.IsMain = true;
532
533                         if (version_res != null)
534                                 DefineVersionInfoResourceImpl (assemblyFileName);
535                         
536                         foreach (ModuleBuilder module in modules)
537                                 if (module != mainModule)
538                                         module.Save ();
539
540                         // Write out the main module at the end, because it needs to
541                         // contain the hash of the other modules
542                         mainModule.Save ();
543
544                         created = true;
545                 }
546
547                 public void SetEntryPoint (MethodInfo entryMethod)
548                 {
549                         SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
550                 }
551
552                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
553                 {
554                         if (entryMethod == null)
555                                 throw new ArgumentNullException ("entryMethod");
556                         if (entryMethod.DeclaringType.Assembly != this)
557                                 throw new InvalidOperationException ("Entry method is not defined in the same assembly.");
558
559                         entry_point = entryMethod;
560                         pekind = fileKind;
561                 }
562
563                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) 
564                 {
565                         if (customBuilder == null)
566                                 throw new ArgumentNullException ("customBuilder");
567
568                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
569                         byte[] data;
570                         int len, pos;
571                         Mono.Security.StrongName sn;
572                         if (attrname == "System.Reflection.AssemblyVersionAttribute") {
573                                 version = create_assembly_version (customBuilder.string_arg ());
574                                 return;
575                         } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
576                                 string keyfile_name = customBuilder.string_arg ();
577                                 if (keyfile_name == String.Empty)
578                                         return;
579                                 using (FileStream fs = new FileStream (keyfile_name, FileMode.Open)) {
580                                         byte[] snkeypair = new byte [fs.Length];
581                                         fs.Read (snkeypair, 0, snkeypair.Length);
582
583                                         // this will import public or private/public keys
584                                         RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
585                                         // and export only the public part
586                                         sn = new Mono.Security.StrongName (rsa);
587                                         public_key = sn.PublicKey;
588                                 }
589                                 return;
590                         } else if (attrname == "System.Reflection.AssemblyKeyNameAttribute") {
591                                 string key_name = customBuilder.string_arg ();
592                                 if (key_name == String.Empty)
593                                         return;
594                                 CspParameters csparam = new CspParameters ();
595                                 csparam.KeyContainerName = key_name;
596                                 RSA rsacsp = new RSACryptoServiceProvider (csparam);
597                                 sn = new Mono.Security.StrongName (rsacsp);
598                                 public_key = sn.PublicKey;
599                                 return;
600                         } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
601                                 culture = customBuilder.string_arg ();
602                         } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
603                                 data = customBuilder.Data;
604                                 pos = 2;
605                                 algid = (uint)data [pos];
606                                 algid |= ((uint)data [pos + 1]) << 8;
607                                 algid |= ((uint)data [pos + 2]) << 16;
608                                 algid |= ((uint)data [pos + 3]) << 24;
609                         } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
610                                 data = customBuilder.Data;
611                                 pos = 2;
612                                 flags = (uint)data [pos];
613                                 flags |= ((uint)data [pos + 1]) << 8;
614                                 flags |= ((uint)data [pos + 2]) << 16;
615                                 flags |= ((uint)data [pos + 3]) << 24;
616                                 return;
617                         } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
618                                 data = customBuilder.Data;
619                                 pos = 2;
620                                 delay_sign = data [2] != 0;
621                         }
622                         if (cattrs != null) {
623                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
624                                 cattrs.CopyTo (new_array, 0);
625                                 new_array [cattrs.Length] = customBuilder;
626                                 cattrs = new_array;
627                         } else {
628                                 cattrs = new CustomAttributeBuilder [1];
629                                 cattrs [0] = customBuilder;
630                         }
631                 }
632                 public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
633                         if (con == null)
634                                 throw new ArgumentNullException ("con");
635                         if (binaryAttribute == null)
636                                 throw new ArgumentNullException ("binaryAttribute");
637
638                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
639                 }
640
641                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
642                         this.corlib_object_type = corlib_object_type;
643                         this.corlib_value_type = corlib_value_type;
644                         this.corlib_enum_type = corlib_enum_type;
645                 }
646
647                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
648                 {
649                         SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
650                         this.corlib_void_type = corlib_void_type;
651                 }
652
653                 private Exception not_supported () {
654                         // Strange message but this is what MS.NET prints...
655                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
656                 }
657
658                 private void check_name_and_filename (string name, string fileName,
659                                                                                           bool fileNeedsToExists) {
660                         if (name == null)
661                                 throw new ArgumentNullException ("name");
662                         if (fileName == null)
663                                 throw new ArgumentNullException ("fileName");
664                         if (name == "")
665                                 throw new ArgumentException ("name cannot be empty", "name");
666                         if (fileName == "")
667                                 throw new ArgumentException ("fileName cannot be empty", "fileName");
668                         if (Path.GetFileName (fileName) != fileName)
669                                 throw new ArgumentException ("fileName '" + fileName + "' must not include a path.");
670
671                         // Resource files are created/searched under the assembly storage
672                         // directory
673                         string fullFileName = fileName;
674                         if (dir != null)
675                                 fullFileName = Path.Combine (dir, fileName);
676
677                         if (fileNeedsToExists && !File.Exists (fullFileName))
678                                 throw new FileNotFoundException ("Could not find file '" + fileName + "'");
679
680                         if (resources != null) {
681                                 for (int i = 0; i < resources.Length; ++i) {
682                                         if (resources [i].filename == fullFileName)
683                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
684                                         if (resources [i].name == name)
685                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
686                                 }
687                         }
688
689                         if (modules != null) {
690                                 for (int i = 0; i < modules.Length; ++i) {
691                                         // Use fileName instead of fullFileName here
692                                         if (!modules [i].IsTransient () && (modules [i].FileName == fileName))
693                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
694                                         if (modules [i].Name == name)
695                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
696                                 }
697                         }
698                 }
699
700                 private String create_assembly_version (String version) {
701                         String[] parts = version.Split ('.');
702                         int[] ver = new int [4] { 0, 0, 0, 0 };
703
704                         if ((parts.Length < 0) || (parts.Length > 4))
705                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
706
707                         for (int i = 0; i < parts.Length; ++i) {
708                                 if (parts [i] == "*") {
709                                         DateTime now = DateTime.Now;
710
711                                         if (i == 2) {
712                                                 ver [2] = (now - new DateTime (2000, 1, 1)).Days;
713                                                 if (parts.Length == 3)
714                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
715                                         }
716                                         else
717                                                 if (i == 3)
718                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
719                                         else
720                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
721                                 }
722                                 else {
723                                         try {
724                                                 ver [i] = Int32.Parse (parts [i]);
725                                         }
726                                         catch (FormatException) {
727                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
728                                         }
729                                 }
730                         }
731
732                         return ver [0] + "." + ver [1] + "." + ver [2] + "." + ver [3];
733                 }
734         }
735 }