Exemplo n.º 1
0
        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);
        }