private static string Compile(string strTmpRoot, PlgxPluginInfo plgx, string strBuildPre, string strBuildPost) { if (strTmpRoot == null) { Debug.Assert(false); return(null); } RunBuildCommand(strBuildPre, UrlUtil.EnsureTerminatingSeparator( strTmpRoot, false), null); PlgxCsprojLoader.LoadDefault(strTmpRoot, plgx); List <string> vCustomRefs = new List <string>(); foreach (string strIncRefAsm in plgx.IncludedReferencedAssemblies) { string strSrcAsm = plgx.GetAbsPath(UrlUtil.ConvertSeparators( strIncRefAsm)); string strCached = PlgxCache.AddCacheFile(strSrcAsm, plgx); if (string.IsNullOrEmpty(strCached)) { throw new InvalidOperationException(); } vCustomRefs.Add(strCached); } CompilerParameters cp = plgx.CompilerParameters; cp.OutputAssembly = UrlUtil.EnsureTerminatingSeparator(strTmpRoot, false) + UrlUtil.GetFileName(PlgxCache.GetCacheFile(plgx, false, false)); cp.GenerateExecutable = false; cp.GenerateInMemory = false; cp.IncludeDebugInformation = false; cp.TreatWarningsAsErrors = false; cp.ReferencedAssemblies.Add(WinUtil.GetExecutable()); foreach (string strCustomRef in vCustomRefs) { cp.ReferencedAssemblies.Add(strCustomRef); } cp.CompilerOptions = "-define:" + GetDefines(); CompileEmbeddedRes(plgx); PrepareSourceFiles(plgx); string[] vCompilers; Version vClr = Environment.Version; int iClrMajor = vClr.Major, iClrMinor = vClr.Minor; if ((iClrMajor >= 5) || ((iClrMajor == 4) && (iClrMinor >= 5))) { vCompilers = new string[] { null, "v4.5", "v4", // Suggested in CodeDomProvider.CreateProvider doc "v4.0", // Suggested in community content of the above "v4.0.30319", // Deduced from file system "v3.5" }; } else if (iClrMajor == 4) // 4.0 { vCompilers = new string[] { null, "v4", // Suggested in CodeDomProvider.CreateProvider doc "v4.0", // Suggested in community content of the above "v4.0.30319", // Deduced from file system "v4.5", "v3.5" }; } else // <= 3.5 { vCompilers = new string[] { null, "v3.5", "v4", // Suggested in CodeDomProvider.CreateProvider doc "v4.0", // Suggested in community content of the above "v4.0.30319", // Deduced from file system "v4.5" }; } CompilerResults cr = null; StringBuilder sbCompilerLog = new StringBuilder(); bool bCompiled = false; for (int iCmp = 0; iCmp < vCompilers.Length; ++iCmp) { if (CompileAssembly(plgx, out cr, vCompilers[iCmp])) { bCompiled = true; break; } if (cr != null) { AppendCompilerResults(sbCompilerLog, vCompilers[iCmp], cr); } } if (!bCompiled) { if (Program.CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null) { SaveCompilerResults(plgx, sbCompilerLog); } throw new InvalidOperationException(); } Program.TempFilesPool.Add(cr.PathToAssembly); Debug.Assert(cr.PathToAssembly == cp.OutputAssembly); string strCacheAsm = PlgxCache.AddCacheAssembly(cr.PathToAssembly, plgx); RunBuildCommand(strBuildPost, UrlUtil.EnsureTerminatingSeparator( strTmpRoot, false), UrlUtil.GetFileDirectory(strCacheAsm, true, false)); return(strCacheAsm); }
private static string ReadFile(BinaryReader br, PlgxPluginInfo plgx, IStatusLogger slStatus) { uint uSig1 = br.ReadUInt32(); uint uSig2 = br.ReadUInt32(); uint uVersion = br.ReadUInt32(); if ((uSig1 != PlgxSignature1) || (uSig2 != PlgxSignature2)) { return(null); // Ignore file, don't throw } if ((uVersion & PlgxVersionMask) > (PlgxVersion & PlgxVersionMask)) { throw new PlgxException(KLRes.FileVersionUnsupported); } string strPluginPath = null; string strTmpRoot = null; bool? obContent = null; string strBuildPre = null, strBuildPost = null; while (true) { KeyValuePair <ushort, byte[]> kvp = ReadObject(br); if (kvp.Key == PlgxEOF) { break; } else if (kvp.Key == PlgxFileUuid) { plgx.FileUuid = new PwUuid(kvp.Value); } else if (kvp.Key == PlgxBaseFileName) { plgx.BaseFileName = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxCreationTime) { } // Ignore else if (kvp.Key == PlgxGeneratorName) { } else if (kvp.Key == PlgxGeneratorVersion) { } else if (kvp.Key == PlgxPrereqKP) { ulong uReq = MemUtil.BytesToUInt64(kvp.Value); if (uReq > PwDefs.FileVersion64) { throw new PlgxException(KLRes.FileNewVerReq); } } else if (kvp.Key == PlgxPrereqNet) { ulong uReq = MemUtil.BytesToUInt64(kvp.Value); ulong uInst = WinUtil.GetMaxNetFrameworkVersion(); if ((uInst != 0) && (uReq > uInst)) { throw new PlgxException(KPRes.NewerNetRequired); } } else if (kvp.Key == PlgxPrereqOS) { string strOS = "," + WinUtil.GetOSStr() + ","; string strReq = "," + StrUtil.Utf8.GetString(kvp.Value) + ","; if (strReq.IndexOf(strOS, StrUtil.CaseIgnoreCmp) < 0) { throw new PlgxException(KPRes.PluginOperatingSystemUnsupported); } } else if (kvp.Key == PlgxPrereqPtr) { uint uReq = MemUtil.BytesToUInt32(kvp.Value); if (uReq > (uint)IntPtr.Size) { throw new PlgxException(KPRes.PluginOperatingSystemUnsupported); } } else if (kvp.Key == PlgxBuildPre) { strBuildPre = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxBuildPost) { strBuildPost = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxBeginContent) { if (obContent.HasValue) { throw new PlgxException(KLRes.FileCorrupted); } string strCached = PlgxCache.GetCacheFile(plgx, true, false); if (!string.IsNullOrEmpty(strCached) && plgx.AllowCached) { strPluginPath = strCached; break; } if (slStatus != null) { slStatus.SetText(KPRes.PluginsCompilingAndLoading, LogStatusType.Info); } obContent = true; if (plgx.LogStream != null) { plgx.LogStream.WriteLine("Content:"); } } else if (kvp.Key == PlgxFile) { if (!obContent.HasValue || !obContent.Value) { throw new PlgxException(KLRes.FileCorrupted); } if (strTmpRoot == null) { strTmpRoot = CreateTempDirectory(); } ExtractFile(kvp.Value, strTmpRoot, plgx); } else if (kvp.Key == PlgxEndContent) { if (!obContent.HasValue || !obContent.Value) { throw new PlgxException(KLRes.FileCorrupted); } obContent = false; } else { Debug.Assert(false); } } if ((strPluginPath == null) && plgx.AllowCompile) { strPluginPath = Compile(strTmpRoot, plgx, strBuildPre, strBuildPost); } return(strPluginPath); }
private void LoadPlugins(List <FileInfo> lFiles, string strTypeName, string strDisplayFilePath, bool bSkipCacheFiles) { string strCacheRoot = PlgxCache.GetCacheRoot(); foreach (FileInfo fi in lFiles) { if (bSkipCacheFiles && fi.FullName.StartsWith(strCacheRoot, StrUtil.CaseIgnoreCmp)) { continue; } FileVersionInfo fvi = null; try { fvi = FileVersionInfo.GetVersionInfo(fi.FullName); if ((fvi == null) || (fvi.ProductName == null) || (fvi.ProductName != AppDefs.PluginProductName)) { continue; } } catch (Exception) { continue; } Exception exShowStd = null; try { PluginInfo pi = new PluginInfo(fi.FullName, fvi, strDisplayFilePath); pi.Interface = CreatePluginInstance(pi.FilePath, strTypeName); if (!pi.Interface.Initialize(m_host)) { continue; // Fail without error } m_vPlugins.Add(pi); } catch (BadImageFormatException exBif) { if (Is1xPlugin(fi.FullName)) { MessageService.ShowWarning(KPRes.PluginIncompatible + MessageService.NewLine + fi.FullName + MessageService.NewParagraph + KPRes.Plugin1x + MessageService.NewParagraph + KPRes.Plugin1xHint); } else { exShowStd = exBif; } } catch (Exception exLoad) { if (Program.CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null) { MessageService.ShowWarningExcp(fi.FullName, exLoad); } else { exShowStd = exLoad; } } if (exShowStd != null) { ShowLoadError(fi.FullName, exShowStd, null); } } }
private void LoadPlugins(List <string> lFiles, string strTypeName, string strDisplayFilePath, bool bSkipCacheFiles) { string strCacheRoot = UrlUtil.EnsureTerminatingSeparator( PlgxCache.GetCacheRoot(), false); foreach (string strFile in lFiles) { if (bSkipCacheFiles && strFile.StartsWith(strCacheRoot, StrUtil.CaseIgnoreCmp)) { continue; } FileVersionInfo fvi = null; try { fvi = FileVersionInfo.GetVersionInfo(strFile); if ((fvi == null) || (fvi.ProductName == null) || (fvi.ProductName != AppDefs.PluginProductName)) { continue; } } catch (Exception) { continue; } Exception exShowStd = null; try { string strHash = Convert.ToBase64String(CryptoUtil.HashSha256( strFile), Base64FormattingOptions.None); PluginInfo pi = new PluginInfo(strFile, fvi, strDisplayFilePath); pi.Interface = CreatePluginInstance(pi.FilePath, strTypeName); CheckCompatibility(strHash, pi.Interface); // CheckCompatibilityRefl(strFile); if (!pi.Interface.Initialize(m_host)) { continue; // Fail without error } m_vPlugins.Add(pi); } catch (BadImageFormatException exBif) { if (Is1xPlugin(strFile)) { MessageService.ShowWarning(KPRes.PluginIncompatible + MessageService.NewLine + strFile + MessageService.NewParagraph + KPRes.Plugin1x + MessageService.NewParagraph + KPRes.Plugin1xHint); } else { exShowStd = exBif; } } catch (Exception exLoad) { if (Program.CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null) { MessageService.ShowWarningExcp(strFile, exLoad); } else { exShowStd = exLoad; } } if (exShowStd != null) { ShowLoadError(strFile, exShowStd, null); } } }
private static string Compile(string strTmpRoot, PlgxPluginInfo plgx, string strBuildPre, string strBuildPost) { if (strTmpRoot == null) { Debug.Assert(false); return(null); } RunBuildCommand(strBuildPre, UrlUtil.EnsureTerminatingSeparator( strTmpRoot, false), null); PlgxCsprojLoader.LoadDefault(strTmpRoot, plgx); List <string> vCustomRefs = new List <string>(); foreach (string strIncRefAsm in plgx.IncludedReferencedAssemblies) { string strSrcAsm = plgx.GetAbsPath(UrlUtil.ConvertSeparators( strIncRefAsm)); string strCached = PlgxCache.AddCacheFile(strSrcAsm, plgx); if (string.IsNullOrEmpty(strCached)) { throw new InvalidOperationException(); } vCustomRefs.Add(strCached); } CompilerParameters cp = plgx.CompilerParameters; cp.OutputAssembly = UrlUtil.EnsureTerminatingSeparator(strTmpRoot, false) + UrlUtil.GetFileName(PlgxCache.GetCacheFile(plgx.FileUuid, false, false)); cp.GenerateExecutable = false; cp.GenerateInMemory = false; cp.IncludeDebugInformation = false; cp.TreatWarningsAsErrors = false; cp.ReferencedAssemblies.Add(WinUtil.GetExecutable()); foreach (string strCustomRef in vCustomRefs) { cp.ReferencedAssemblies.Add(strCustomRef); } CompileEmbeddedRes(plgx); PrepareSourceFiles(plgx); CompilerResults cr; if (!CompileAssembly(plgx, out cr, null)) { if (!CompileAssembly(plgx, out cr, "v3.5")) { throw new InvalidOperationException(); } } Program.TempFilesPool.Add(cr.PathToAssembly); Debug.Assert(cr.PathToAssembly == cp.OutputAssembly); string strCacheAsm = PlgxCache.AddCacheAssembly(cr.PathToAssembly, plgx); RunBuildCommand(strBuildPost, UrlUtil.EnsureTerminatingSeparator( strTmpRoot, false), UrlUtil.GetFileDirectory(strCacheAsm, true, false)); return(strCacheAsm); }