コード例 #1
0
ファイル: BinaryCreator.cs プロジェクト: Azyyyyyy/TinyUpdate
        private bool AddLoaderFile(
            TemporaryFolder temporaryFolder,
            ApplicationMetadata applicationMetadata,
            ZipArchive zipArchive,
            SemanticVersion newVersion,
            string applicationLocation,
            OSPlatform?intendedOs      = null,
            SemanticVersion?oldVersion = null,
            string?outputLocation      = null)
        {
            //TODO: See why this is making another .shasum file (but working)
            using var loaderLocation = temporaryFolder.CreateTemporaryFile(applicationMetadata.ApplicationName + ".exe");
            // ReSharper disable once LocalFunctionHidesMethod
            bool AddFile() => BinaryCreator.AddFile(
                zipArchive,
                loaderLocation.GetStream(FileMode.Open),
                applicationMetadata.ApplicationName + ".exe.load",
                false);

            //TODO: Grab metadata from .exe and drop it into Loader
            var iconLocation = Path.Combine(applicationLocation, "app.ico");

            iconLocation = File.Exists(iconLocation) ? iconLocation : null;

            var successful = LoaderCreator.CreateLoader(
                temporaryFolder,
                $"{newVersion.GetApplicationFolder()}\\{applicationMetadata.ApplicationName}.exe",
                iconLocation,
                loaderLocation.Location,
                applicationMetadata.ApplicationName,
                intendedOs);

            if (successful != LoadCreateStatus.Successful)
            {
                var canContinue = successful == LoadCreateStatus.UnableToCreate;
                _logger.Error("We wasn't able to create loader! (Going to fail file creation?: {0})", canContinue);
                return(canContinue);
            }
            if (oldVersion == null || !Directory.Exists(outputLocation))
            {
                return(AddFile());
            }

            //If we get here then we might also have the old loader, try to diff by using it
            foreach (var file in Directory.EnumerateFiles(outputLocation !, "*" + Extension))
            {
                if (string.IsNullOrWhiteSpace(file))
                {
                    _logger.Warning("We somehow got an entry for {0} which was nothing", outputLocation);
                    continue;
                }

                /*Don't try it with delta file, more then likely going to
                 * have diff loader itself and we can't work with that*/
                var fileName = Path.GetFileNameWithoutExtension(file);
                if (fileName.EndsWith("-delta") ||
                    fileName.ToVersion() != oldVersion)
                {
                    continue;
                }

                Stream?    stream = null;
                ZipArchive fileArch;
                try
                {
                    stream   = File.OpenRead(file);
                    fileArch = new ZipArchive(stream, ZipArchiveMode.Read);
                }
                catch (Exception e)
                {
                    stream?.Dispose();
                    _logger.Error(e);
                    continue;
                }

                //We want to grab the loader file
                var loaderFileIndex = fileArch.Entries.IndexOf(x => x?.Name == applicationMetadata.ApplicationName + ".exe.load");
                if (loaderFileIndex == -1)
                {
                    fileArch.Dispose();
                    continue;
                }

                var fileEntry = fileArch.Entries[loaderFileIndex];
                using var tmpFile = temporaryFolder.CreateTemporaryFile(Path.GetFileNameWithoutExtension(fileEntry.Name));
                fileEntry.ExtractToFile(tmpFile.Location, true);

                var deltaSuccessful =
                    AddDeltaFile(temporaryFolder,
                                 zipArchive,
                                 tmpFile.Location,
                                 loaderLocation.Location,
                                 extensionEnd: "load");
                if (!deltaSuccessful)
                {
                    _logger.Warning("Wasn't able to diff loader, just going to add the load in as normal");
                }
                fileArch.Dispose();
                return(deltaSuccessful || AddFile());
            }

            //If we get here then we can't do any kind of diff logic
            return(AddFile());
        }