/// <summary>
        /// Interrogate an assembly to within a package to get its assembly info
        /// </summary>
        /// <param name="archivePath">the path to the package in the file system</param>
        /// <param name="uri">the uri of the assembly within the package</param>
        /// <param name="info">the info extracted from the assembly</param>
		/// <param name="errorMessage">the user friendly error message</param>
		/// <param name="execptionMessage">the raw exception message</param>
        /// <returns>true if the assembly info could be extracted</returns>
		public bool GetAssemblyInfo(string archivePath, string uri, out AssemblyInfo info, out string errorMessage, out string execptionMessage)
        {
            info = null;
			errorMessage = null; 
			execptionMessage = null; 

			try
			{
				// load the package that contains the assembly to interrogate
				using (Package package = PackageHelperLite.GetPackage(archivePath, false, FileAccess.Read))
				{
					// hook on event to resolve the assembly manualy
					AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

					// try to extract assembly from package
					if (LoadAssembly(package, uri, out m_Assembly, out execptionMessage) == false)
					{
						// could not load the assembly
						errorMessage = string.Format(Strings.Error_AssemblyLoad, uri); 
						return false;
					}

					// get the name of the assembly
					AssemblyName asmName = m_Assembly.GetName();

					info = new AssemblyInfo();

					// check the entry point and see if it takes in arguments
					info.PassArgs = m_Assembly.EntryPoint.GetParameters().Length > 0;

					// iterate through all the attributes on the assembly 
					foreach (object obj in m_Assembly.GetCustomAttributes(false))
					{
						#region Handle each attribute

						if (obj is AssemblyTitleAttribute)
						{
							info.Title = (obj as AssemblyTitleAttribute).Title;
						}
						else if (obj is AssemblyDescriptionAttribute)
						{
							info.Description = (obj as AssemblyDescriptionAttribute).Description;
						}
						else if (obj is AssemblyConfigurationAttribute)
						{
							info.Configuration = (obj as AssemblyConfigurationAttribute).Configuration;
						}
						else if (obj is AssemblyCompanyAttribute)
						{
							info.Company = (obj as AssemblyCompanyAttribute).Company;
						}
						else if (obj is AssemblyProductAttribute)
						{
							info.Product = (obj as AssemblyProductAttribute).Product;
						}
						else if (obj is AssemblyCopyrightAttribute)
						{
							info.Copyright = (obj as AssemblyCopyrightAttribute).Copyright;
						}
						else if (obj is AssemblyTrademarkAttribute)
						{
							info.Trademark = (obj as AssemblyTrademarkAttribute).Trademark;
						}
						else if (obj is AssemblyCultureAttribute)
						{
							info.Culture = (obj as AssemblyCultureAttribute).Culture;
						}
						else if (obj is AssemblyVersionAttribute)
						{
							info.Version = (obj as AssemblyVersionAttribute).Version;
						}
						else if (obj is AssemblyFileVersionAttribute)
						{
							info.FileVersion = (obj as AssemblyFileVersionAttribute).Version;
						}

						#endregion
					}

					// if we have a version then copy it accross
					if (IsNullOrEmpty(info.Version))
					{
						info.Version = asmName.Version.ToString();
					}

					// we got the info ok
					return true;
				}
			}
			catch (TypeLoadException ex)
			{
				// the assembly info could not be obtained
				errorMessage = string.Format(Strings.Error_FrameworkVersion, uri);
				execptionMessage = ex.Message; 

				return false;
			}
			catch (Exception ex)
			{
				// the assembly info could not be obtained
				errorMessage = string.Format(Strings.Error_Unknown, uri);
				execptionMessage = ex.Message; 

				return false;
			}            
        }
Esempio n. 2
0
        private AssemblyInfo ReflectAssemblyInfo(string archivePath, string uri, CompileProcess compileProcess)
        {
            AssemblyInfo info = new AssemblyInfo();

            AppDomain domain = null;

            try
            {
                string appDomainName = "Interrogator";                

                AppDomainSetup domainSetup = new AppDomainSetup();
                domainSetup.ApplicationName = appDomainName;
                domainSetup.ApplicationBase = new FileInfo(Application.ExecutablePath).DirectoryName; 

                domain = AppDomain.CreateDomain(appDomainName, null, domainSetup);

                AssemblyInterrogator remoteWorker = (AssemblyInterrogator)domain.CreateInstanceAndUnwrap(typeof(AssemblyInterrogator).Assembly.FullName, typeof(AssemblyInterrogator).FullName);

				string errorMessage, exceptionMessage;

				if (!remoteWorker.GetAssemblyInfo(archivePath, uri, out info, out errorMessage, out exceptionMessage))
                {
					if (RC.ShouldWrite(ConsoleVerbosity.Debug) && String.IsNullOrEmpty(exceptionMessage) == false)
					{
						throw new Exception(errorMessage + Environment.NewLine + exceptionMessage);
					}
					else
					{
						throw new Exception(errorMessage); 
					}
                    //throw new Exception(string.Format("{0} : {1}", Rpx.Strings.Compiler_UnableToLoadAsm, uri));					
                }
            
                info = AssemblyInfo.Clone(info); 

                compileProcess.PassArguments = info.PassArgs;
            }
            finally
            {
                if (domain != null)
                    AppDomain.Unload(domain);
            }

            return info;
        }
Esempio n. 3
0
        public static AssemblyInfo Clone(AssemblyInfo other)
        {
            AssemblyInfo info = new AssemblyInfo();

            info.Title = other.Title;
            info.Description = other.Description;
            info.Configuration = other.Configuration;
            info.Company = other.Company;
            info.Product = other.Product;
            info.Copyright = other.Copyright;
            info.Trademark = other.Trademark;
            info.Culture = other.Culture;
            info.Version = other.Version;
            info.FileVersion = other.FileVersion;
            info.PassArgs = other.PassArgs;

            return info;
        }
Esempio n. 4
0
        private bool Compile(string archivePath, string outputFile, ref string replacedFile, AssemblyInfo assemblyInfo)
        {
            bool allGood = false; 

            #region Compile

            List<string> defines = new List<string>();
            Dictionary<string, string> replacements = new Dictionary<string, string>();

            #region Create the CodeDomProvider

            FileInfo fileInfo = new FileInfo(outputFile);

            if (!fileInfo.Directory.Exists)
                fileInfo.Directory.Create();

            CodeDomProvider csc = new CSharpCodeProvider();
            CompilerParameters cp = new CompilerParameters();

            cp.GenerateExecutable = true;
            cp.OutputAssembly = outputFile;
            cp.CompilerOptions = " /filealign:512 /optimize+ /platform:x86";

            if (Helper.IsNotNullOrEmpty(IconFile))
                cp.CompilerOptions += string.Format(" /win32icon:\"{0}\"", IconFile);

            if (Debug)
            {
                defines.Add("DEBUG");
                cp.CompilerOptions += " /debug+ /debug:full";
            }

            #endregion

            #region Defines

            defines.Add("RPX");

            string targetName;

            ConsoleThemeColor targetColour = ConsoleThemeColor.SubTextGood; 

            switch (ExecutableType)
            {
                case ExecutableType.Console:
                    cp.CompilerOptions += " /target:exe";
                    targetName = Rpx.Strings.Compiler_ConsoleApplication;
                    break;
                case ExecutableType.Forms:
                    cp.CompilerOptions += " /target:winexe";
                    defines.Add("WinExec");
                    targetName = Rpx.Strings.Compiler_FormsApplication;
                    break;
                default:
                    cp.CompilerOptions += " /target:exe";
                    targetName = Rpx.Strings.Compiler_DefaultApplication;
                    targetColour = ConsoleThemeColor.SubTextBad; 
                    break;
            }

            if (RC.ShouldWrite(ConsoleVerbosity.Verbose))
            {
                RC.WriteLine(ConsoleVerbosity.Verbose, targetColour, " - " + string.Format(Rpx.Strings.Compiler_AssemblyTarget, targetName));
            }
            else
            {
                RC.WriteLine(ConsoleVerbosity.Minimal, targetColour, " " + string.Format(Rpx.Strings.Compiler_AssemblyTarget, targetName));
            }

            if (Decorate)
                defines.Add("Decorate");

            if (Assemblys.Count > 1 || Tools.Count > 0)
                defines.Add("HasAdditionalAssemblys");

            if (Tools.Count > 0)
                defines.Add("IsToolkit");

            if (PassArguments)
                defines.Add("PassArguments");

            if (GetAssembyInfoFrom != AssemblyInfoMode.SourceFile)
            {
                defines.Add("ExpliciteInfo");

                replacements.Add("[Asm_Title]", Helper.MakeNonNull(assemblyInfo.Title));

                string description = Helper.MakeNonNull(assemblyInfo.Description);

                if (Helper.IsNotNullOrEmpty(description)) //  Asm_Description))
                    description += "\n";

                string watermark = "RPX " + typeof(CompileProcess).Assembly.GetName().Version.ToString();

                if (!description.Contains(watermark))
                    description += watermark;

                replacements.Add("[Asm_Description]", Helper.MakeNonNullAndEscape(description));
                replacements.Add("[Asm_Configuration]", Helper.MakeNonNullAndEscape(assemblyInfo.Configuration));
                replacements.Add("[Asm_Company]", Helper.MakeNonNullAndEscape(assemblyInfo.Company));
                replacements.Add("[Asm_Product]", Helper.MakeNonNullAndEscape(assemblyInfo.Product));
                replacements.Add("[Asm_Copyright]", Helper.MakeNonNullAndEscape(assemblyInfo.Copyright));
                replacements.Add("[Asm_Trademark]", Helper.MakeNonNullAndEscape(assemblyInfo.Trademark));
                replacements.Add("[Asm_Culture]", Helper.MakeNonNullAndEscape(assemblyInfo.Culture));
                replacements.Add("[Asm_Version]", Helper.MakeNonNullAndEscape(assemblyInfo.Version));
                replacements.Add("[Asm_FileVersion]", Helper.MakeNonNullAndEscape(assemblyInfo.FileVersion));

                //RC.WriteLine(ConsoleVerbosity.Normal, ConsoleColorExt.Gray, " " + Rpx.Strings.Compiler_AssemblyInfoHasBeenReflected);

                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Product.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Product));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Version.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Version));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_FileVersion.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.FileVersion));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Configuration.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Configuration));

                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Company.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Company));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Description.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Description));

                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Copyright.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Copyright));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Trademark.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Trademark));
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + Rpx.Strings.AssemblyInfo_Culture.PadRight(16) + ": " + Helper.MakeNonNullAndEscape(assemblyInfo.Culture));

            }
            else
            {
                RC.WriteLine(ConsoleVerbosity.Normal, ConsoleThemeColor.Text, " " + Rpx.Strings.Compiler_AssemblyInfoFromFile);
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.SubText, " - " + AssembyInfoSourceFilePath);
                // 
            }


            if (Compression.Protected)
            {
                if (Helper.IsNotNullOrEmpty(Compression.Password))
                {
                    RC.WriteLine(ConsoleVerbosity.Normal, ConsoleThemeColor.Text, " " + Rpx.Strings.Compiler_IncludeCryptographyCode);

                    defines.Add("UseCryptography");
                }
                else
                {
                    RC.WriteLine(ConsoleVerbosity.Normal, ConsoleThemeColor.Text, " " + Rpx.Strings.Compiler_IncludeDisguiseCode);
                }

                defines.Add("Hidden");
            }

            foreach (string define in defines)
                cp.CompilerOptions += " /define:" + define;

            #endregion

            #region Include the Archive Resource

            string archiveName = "a.zip";

            if (Compression.Protected)
            {
                archiveName = "a";
            }

            #region Finalise Package

            Compression.FinalisePackage(Compression.OutputFile);

            #endregion

            cp.CompilerOptions += string.Format(" /resource:\"{0}\",{1}", archivePath, archiveName);

            #endregion

            #region Add Default Refrences

            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            cp.CompilerOptions += string.Format(" /reference:\"{0}\"", typeof(Package).Assembly.Location);

            #endregion

            #region Define Remaining Code Template Replacements

            if (GetAssembyInfoFrom == AssemblyInfoMode.SourceFile)
            {
                replacements.Add("[#AssemblyInfo.cs#]", File.ReadAllText(AssembyInfoSourceFilePath).Replace("using System.Reflection;", ""));
            }

            replacements.Add("#ResourceLocation#", archiveName);

            if (Compression.Protected)
            {
                if (Helper.IsNotNullOrEmpty(Compression.Password))
                {
                    replacements.Add("[#PasswordVector#]", Helpers.EncryptHelper.InitVector);
                    replacements.Add("[#PasswordSalt#]", Compression.SaltValue);
                    replacements.Add("[#Password#]", Compression.Password);
                    replacements.Add("[#PasswordStrength#]", "2");
                }
            }

            List<Dictionary<string, string>> AssemblyReplacements = new List<Dictionary<string, string>>();
            List<Dictionary<string, string>> ToolReplacements = new List<Dictionary<string, string>>();

            #region Build Assembly Code

            int asmId = 0;

            if (Assemblys.Count > 1)
            {
                foreach (string asm in Assemblys)
                {
                    string path = PackageHelper.MakeUriSafe(asm.Substring(asm.LastIndexOf("\\") + 1));

                    if (path != InitialAssemblyPath)
                    {
                        Dictionary<string, string> asmReplacements = new Dictionary<string, string>();

                        string idStr = "a" + (asmId++).ToString();

                        asmReplacements.Add("[#Asm_IdStr#]", idStr);
                        asmReplacements.Add("[#Asm_Path#]", PackageHelper.MakeUriSafe(path));
                        AssemblyReplacements.Add(asmReplacements);
                    }
                }
            }

            #endregion

            #region Build Tools Code

            if (Tools.Count > 0)
            {
                RC.WriteLine(ConsoleVerbosity.Verbose, ConsoleThemeColor.TitleText, "\n" + Rpx.Strings.Compiler_Toolkit);

                if (RC.ShouldWrite(ConsoleVerbosity.Normal))
                {
                    RC.WriteLine(ConsoleVerbosity.Minimal, ConsoleThemeColor.Text, " " + string.Format(Rpx.Strings.Compiler_Tools, Tools.Count.ToString()));
                }
                else if (RC.ShouldWrite(ConsoleVerbosity.Minimal))
                {
                    RC.Write(ConsoleVerbosity.Minimal, ConsoleThemeColor.TitleText, " " + string.Format(Rpx.Strings.Compiler_Tools, Tools.Count.ToString()));
                    RC.Write(ConsoleVerbosity.Minimal, ConsoleThemeColor.SubText, " (");
                }

                StringBuilder ToolsString = new StringBuilder();

                bool firstMenu = true;

                foreach (KeyValuePair<string, string> tool in Tools)
                {
                    string toolName = tool.Key;
                    string asm = tool.Value;
                    string path = PackageHelper.MakeUriSafe(asm.Substring(asm.LastIndexOf("\\") + 1));

                    if (path != InitialAssemblyPath)
                    {
                        Dictionary<string, string> asmReplacements = new Dictionary<string, string>();
                        Dictionary<string, string> toolReplacements = new Dictionary<string, string>();

                        string idStr = "a" + (asmId++).ToString();

                        asmReplacements.Add("[#Asm_IdStr#]", idStr);
                        asmReplacements.Add("[#Asm_Path#]", PackageHelper.MakeUriSafe(path));

                        AssemblyReplacements.Add(asmReplacements);

                        toolReplacements.Add("[#Tool_Name#]", toolName);
                        toolReplacements.Add("[#Tool_Asm#]", idStr);

                        ToolReplacements.Add(toolReplacements);

                        if (RC.ShouldWrite(ConsoleVerbosity.Normal))
                        {
                            RC.Write(ConsoleVerbosity.Normal, ConsoleThemeColor.SubText, " - " + toolName.PadRight(15, ' ') + " ");
                            RC.WriteLine(ConsoleVerbosity.Normal, ConsoleThemeColor.SubText2, "(" + path + ")");
                        }
                        else if (RC.ShouldWrite(ConsoleVerbosity.Minimal))
                        {
                            if (!firstMenu)
                                RC.Write(ConsoleVerbosity.Minimal, ConsoleThemeColor.SubText, ", ");

                            RC.Write(ConsoleVerbosity.Minimal, ConsoleThemeColor.SubText2, toolName);

                            firstMenu = false;
                        }

                        ToolsString.Append("\\n  " + toolName);
                    }
                }

                if (!RC.ShouldWrite(ConsoleVerbosity.Normal) && RC.ShouldWrite(ConsoleVerbosity.Minimal))
                {
                    RC.WriteLine(ConsoleVerbosity.Minimal, ConsoleThemeColor.SubText, ")");
                }

                replacements.Add("[#ToolsString#]", ToolsString.ToString());
            }

            #endregion

            replacements.Add("#InitialAssemblyPath#", PackageHelper.MakeUriSafe(InitialAssemblyPath));

            #endregion

            #region Rename the Output File If It Exits So It Can Be Rolled Back

            if (File.Exists(outputFile))
            {
                RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.TitleText, "\n" + Rpx.Strings.Compiler_BackupAsm);

                string name = new FileInfo(outputFile).Name;

                replacedFile = Application.UserAppDataPath + @"\" + Guid.NewGuid().GetHashCode().ToString() + "_" + name + ".InUse";

                RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.Text, " " + replacedFile);

                File.Move(outputFile, replacedFile);
            }

            #endregion

            #region Compile Assembly From Code Template

            RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.TitleText, "\n" + Rpx.Strings.Compiler_Compiling);
            RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.Text, string.Format(" {0}: {1}", Rpx.Strings.Compiler_Options, cp.CompilerOptions.Replace("/", "\n    /")));

            CodeFileBuilder builder = GetCodeFileBuilderFromResource("Rpx.Packing.Embedded.RpxWrapperSource.xml");

            Dictionary<string, List<Dictionary<string, string>>> ObjReplacements = new Dictionary<string, List<Dictionary<string, string>>>();

            ObjReplacements.Add("Assemblys", AssemblyReplacements);
            ObjReplacements.Add("Tools", ToolReplacements);

            string source = builder.BuildCodeFile(replacements, ObjReplacements, defines);

            // Compile standalone executable with input files embedded as resource
            CompilerResults cr = csc.CompileAssemblyFromSource(cp, source);

            // yell if compilation error
            if (cr.Errors.Count > 0)
            {
                if (replacedFile != null)
                {
                    File.Move(replacedFile, outputFile);
                    replacedFile = null;
                }

                string msg = string.Format(Rpx.Strings.Compiler_BuildErrors, cr.PathToAssembly);

                foreach (CompilerError ce in cr.Errors)
                {
                    msg += Environment.NewLine + ce.ToString();
                }

                if (!LogToConsole)
                    MessageBox.Show(msg);
                else
                {
                    RC.WriteError(02, string.Format(Rpx.Strings.Compiler_CompileFailed, cr.Errors.Count));
                    RC.WriteLine(ConsoleThemeColor.ErrorColor2, msg);

                    if (RC.Verbosity == ConsoleVerbosity.Debug)
                    {
						if (RC.IsBuildMode == false) RC.Write(ConsoleThemeColor.SubTextBad, new string(ConsoleChars.GetShade(ConsoleShade.Dim), RC.BufferWidth));
						else RC.Write(ConsoleThemeColor.SubTextBad, new string('*', RC.BufferWidth));

                        RC.WriteLine();
                        RC.WriteLine(ConsoleThemeColor.Text, source.Replace("\t", "  "));
                        RC.WriteLine();
						
						if (RC.IsBuildMode == false) RC.Write(ConsoleThemeColor.SubTextBad, new string(ConsoleChars.GetShade(ConsoleShade.Dim), RC.BufferWidth));
						else RC.Write(ConsoleThemeColor.SubTextBad, new string('*', RC.BufferWidth));
                    }
                }
            }
            else
            {
                if (RC.Verbosity == ConsoleVerbosity.Debug)
                {
                    RC.Write(ConsoleThemeColor.SubTextGood, new string(ConsoleChars.GetShade(ConsoleShade.Dim), RC.BufferWidth));
                    RC.WriteLine();
                    RC.WriteLine(ConsoleThemeColor.Text, source.Replace("\t", "  "));
                    RC.WriteLine();
                    RC.Write(ConsoleThemeColor.SubTextGood, new string(ConsoleChars.GetShade(ConsoleShade.Dim), RC.BufferWidth));
                }

                FinalFileSize = new FileInfo(outputFile).Length;

                if (replacedFile != null)
                {                    
                    File.SetAttributes(replacedFile, FileAttributes.Temporary);

                    string junkFile = replacedFile.Substring(0, replacedFile.Length - 6) + ".junk";

                    RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.TitleText, "\n" + Rpx.Strings.Compiler_JunkingBackupAsm);
                    RC.WriteLine(ConsoleVerbosity.Debug, ConsoleThemeColor.Text, " " + junkFile);

                    File.Move(replacedFile, junkFile);
                    replacedFile = null;
                }

				allGood = true;
            }

            #endregion

            #endregion

            return allGood; 
        }