예제 #1
0
        public void UnPackTest()
        {
            DateTime    now      = DateTime.Now;
            string      fileName = DateTime.Now.Ticks + ".kpp";
            Guid        guid     = Guid.NewGuid();
            KPPDataHead head     = new KPPDataHead();
            PackageAttributeDataSection section = new PackageAttributeDataSection();

            section.SetField("PackName", "test.package");
            section.SetField("PackDescription", "test package description.");
            section.SetField("EnvironmentFlag", (byte)0x01);
            section.SetField("Version", "1.0.0");
            section.SetField("PackTime", now);
            section.SetField("ApplicationMainFileName", "1.txt");
            section.SetField("GlobalUniqueIdentity", guid);
            string tmpPath = PrepareTestFiles("res-files");

            KPPResource.Pack(tmpPath, fileName, head, true, section);
            Assert.IsTrue(File.Exists(fileName));

            KPPDataStructure structure = KPPResource.UnPack(fileName);

            Assert.IsTrue(structure.GetSectionField <string>(0x00, "PackName") == "test.package");
            Assert.IsTrue(structure.GetSectionField <string>(0x00, "PackDescription") == "test package description.");
            Assert.IsTrue(structure.GetSectionField <string>(0x00, "Version") == "1.0.0");
            Assert.IsTrue(structure.GetSectionField <string>(0x00, "ApplicationMainFileName") == "1.txt");
            Assert.IsTrue(structure.GetSectionField <DateTime>(0x00, "PackTime") == now);
            Assert.IsTrue(structure.GetSectionField <byte>(0x00, "EnvironmentFlag") == 0x01);
            Assert.IsTrue(structure.GetSectionField <Guid>(0x00, "GlobalUniqueIdentity") == guid);
            Assert.IsNotNull(structure);


            File.Delete(fileName);
            Directory.Delete(tmpPath, true);
        }
예제 #2
0
        public void PackNonCompletedEnvironmentTestWithoutDelete()
        {
            DateTime    now      = DateTime.Now;
            string      fileName = DateTime.Now.Ticks + ".kpp";
            Guid        guid     = Guid.NewGuid();
            KPPDataHead head     = new KPPDataHead();
            PackageAttributeDataSection section = new PackageAttributeDataSection();

            section.SetField("PackName", "test.package");
            section.SetField("PackDescription", "test package description.");
            section.SetField("EnvironmentFlag", (byte)0x01);
            section.SetField("Version", "1.0.0");
            section.SetField("PackTime", now);
            section.SetField("ApplicationMainFileName", "KJFramework.ApplicationEngine.ApplicationTest.dll");
            section.SetField("GlobalUniqueIdentity", guid);
            section.SetField("IsCompletedEnvironment", false);
            string tmpPath = PrepareTestFiles("res-files");

            KPPResource.Pack(tmpPath, fileName, head, false, section);
            Assert.IsTrue(File.Exists(fileName));
            Directory.Delete(tmpPath, true);
        }
        /// <summary>
        ///     读取通知KAE APP所在地址的文件,并将其进行解析
        /// </summary>
        /// <param name="fileFullPath">KAE APP文件的完整路径地址</param>
        /// <returns>返回解析后的KAE APP信息</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="FileNotFoundException">目标文件不存在</exception>
        public Tuple <string, ApplicationEntryInfo, KPPDataStructure> ReadKPPFrom(string fileFullPath)
        {
            if (string.IsNullOrEmpty(fileFullPath))
            {
                throw new ArgumentNullException(nameof(fileFullPath));
            }
            if (!File.Exists(fileFullPath))
            {
                throw new FileNotFoundException(fileFullPath);
            }
            try
            {
                //analyzes kpp package.
                KPPDataStructure dataStructure = KPPResource.UnPack(fileFullPath);
                if (dataStructure == null)
                {
                    return(null);
                }
                string targetPath = Path.Combine(Path.GetDirectoryName(fileFullPath), string.Format("temp-files\\apps\\tempfiles_{0}_{1}_{2}", Path.GetFileName(fileFullPath), dataStructure.GetSectionField <string>(0x00, "Version"), (ApplicationLevel)dataStructure.GetSectionField <byte>(0x00, "ApplicationLevel")));
                //releases files from kpp package.
                dataStructure.ReleaseFiles(targetPath);
                //picks up main file data.
                string mainFilePath = Path.Combine(targetPath, dataStructure.GetSectionField <string>(0x00, "ApplicationMainFileName"));
                //updating combined full path directory.
                dataStructure.SetSectionField(0x00, "ApplicationMainFileName", mainFilePath);

                Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(mainFilePath)));
                Type[]   types    = assembly.GetTypes();
                if (types.Length == 0)
                {
                    return(null);
                }
                Type targetAppType = null;
                foreach (Type type in types)
                {
                    try
                    {
                        //找到入口点
                        if (type.GetTypeInfo().IsClass&& !type.GetTypeInfo().IsAbstract&& type.GetTypeInfo().IsSubclassOf(typeof(Application)))
                        {
                            targetAppType = type;
                            break;
                        }
                    }
                    catch (ReflectionTypeLoadException) { }
                    catch (Exception ex) { _tracing.Error(ex, null); }
                }
                //use default application type when current package had missed the main application class.
                targetAppType = targetAppType ?? typeof(Application);
                ApplicationEntryInfo info = new ApplicationEntryInfo
                {
                    FilePath   = Path.GetFullPath(mainFilePath),
                    FolderPath = Path.GetFullPath(targetPath),
                    EntryPoint = targetAppType.FullName
                };
                byte[] data = File.ReadAllBytes(fileFullPath);
                Crc32  crc  = new Crc32();
                crc.Reset();
                crc.Update(data);
                info.FileCRC = crc.Value;
                return(new Tuple <string, ApplicationEntryInfo, KPPDataStructure>(dataStructure.GetSectionField <string>(0x00, "PackName"), info, dataStructure));
            }
            catch (ReflectionTypeLoadException) { return(null); }
            catch (Exception ex)
            {
                _tracing.Error(ex, null);
                return(null);
            }
        }