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