Example #1
0
        private UpdateInstructions ParseUpdateInstructionsV1(UpdateInstructions instructions, XmlDocument doc)
        {
            //public string InstructionsVersion { get; set; } (already got)
            //public UpdateTypes UpdateType { get; set; }
            //public string WotmodFilenameInZip { get; set; }
            //public string WotmodMD5 { get; set; }
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                switch (node.Name)
                {
                case nameof(instructions.WotmodFilenameInZip):
                    instructions.WotmodFilenameInZip = node.InnerText.Trim();
                    break;

                case nameof(instructions.UpdateType):
                    instructions.UpdateType = (UpdateTypes)Enum.Parse(instructions.UpdateType.GetType(), node.InnerText.Trim());
                    break;

                case nameof(instructions.PatchUpdates):
                    instructions.PatchUpdates = ParsePatchUpdates(node);
                    break;
                }
            }
            return(instructions);
        }
        protected override UpdateInstructions GetInstructions()
        {
            var instructions = new UpdateInstructions();

            typeof(UpdateInstructions).GetProperty(nameof(UpdateInstructions.TargetFiles))
            .SetValue(instructions, new List <FileInformation>
            {
                new FileInformation
                {
                    Filename = "%basedir%\\existingFile.exe",
                    Hash     = Hash.Parse("322592d512976ad849b60a88dd34cf60e3db134974b09b5bf79c359f909a8c07"),
                    Length   = 1000
                },
                new FileInformation
                {
                    Filename = "%basedir%\\changedFile.txt",
                    Hash     = Hash.Parse("7324c19ad06ea51117d7ffdd01f7d030ac03651f6ea57472de1765b3dfa3a9e0"),
                    Length   = 1000
                },
                new FileInformation
                {
                    Filename = "%basedir%\\nonexistingFile.exe",
                    Hash     = Hash.Parse("ac6cb635c58754672d0e576442db9ecadd8043829916ca85330cf00bd7359df9"),
                    Length   = 1000
                }
            });

            typeof(UpdateInstructions).GetProperty(nameof(UpdateInstructions.FileSignatures))
            .SetValue(instructions, GenerateOnlineFileSignatures());

            return(instructions);
        }
Example #3
0
 public void Update(
     string id,
     UpdateInstructions <Snippet> updateInstructions)
 {
     this.snippetsCollection
     .UpdateOne(
         filter: ById(id: id),
         update: updateInstructions.ToMongoUpdate());
 }
Example #4
0
 public static UpdateDefinition <T> ToMongoUpdate <T>(
     this UpdateInstructions <T> updateInstructions)
 {
     return(Builders <T> .Update.Combine(
                updates : updateInstructions.SetOperations
                .Select(
                    selector: x => Builders <T> .Update.Set(
                        field: x.Key,
                        value: x.Value))));
 }
Example #5
0
        private UpdateInstructions ParseUpdateInstructions(XmlDocument doc)
        {
            UpdateInstructions instructions  = new UpdateInstructions();
            string             formatVersion = doc.DocumentElement.Attributes["formatVersion"].Value.Trim();

            instructions.InstructionsVersion = formatVersion;
            switch (formatVersion)
            {
            case "1.0":
                ParseUpdateInstructionsV1(instructions, doc);
                break;
            }

            return(instructions);
        }
        static void Main(string[] args)
        {
            bool showHelp     = false;
            var  instructions = new UpdateInstructions {
                StartDelay = 1000
            };

            var p = new OptionSet()
            {
                { "a|access=", "Required. The Amazon {ACCESS KEY} with permissions to access the S3 bucket",
                  v => instructions.AwsAccessKey = v },
                { "s|secret=", "Required. The Amazon {SECRET KEY} with permissions to access the S3 bucket",
                  v => instructions.AwsSecretKey = v },
                { "b|bucket=", "Required. The Amazon S3 {BUCKET} name containing the files",
                  v => instructions.BucketName = v },
                { "r|remotepath=", "Required. The {REMOTE_PATH} of the update zip file within the S3 bucket",
                  v => instructions.RemotePath = v },
                { "f|folder=", "Required. The local path of the folder containing the application. The ZIPFILE will be extracted here, replacing existing files",
                  v => instructions.Folder = v },
                { "e|exe=", "Required. The local path of the {EXECUTABLE} to launch after completion",
                  v => instructions.ExePath = v },
                { "v|service=", "Optional. Is the exe a TopShelf service? If yes, appends a start argument\nOptions: true or false. Default is false.",
                  (bool v) => instructions.IsService = v },
                { "d|delay=", "Optional. The {DELAY} in millseconds to wait before beginning the update process. Default is 1000.",
                  (int v) => instructions.StartDelay = v },
                { "t|temp=", "Optional. The local path to the {TEMPDIR} in which to store download files\nBy default \\temp in relation to updater will be used",
                  v => instructions.TempDir = v },
                { "u|url=", "Optional. The {URL} to POST errors to",
                  v => instructions.ErrorUrl = v },
                { "h|help", "show this message and exit",
                  v => showHelp = v != null },
            };

            try
            {
                p.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("There was an error: ");
                Console.WriteLine(e.Message);
                ShowHelp(p);
                return;
            }

            if (showHelp)
            {
                ShowHelp(p);
                return;
            }

            bool success = false;

            try
            {
                Console.WriteLine("Beginning update process");
                Console.Write("Delay is set to {0}ms. Waiting.", instructions.StartDelay);
                Thread.Sleep(instructions.StartDelay);
                Console.WriteLine("Continuing");
                // If temp directory wasn't set by an argument, set it to current folder\Temp
                if (string.IsNullOrWhiteSpace(instructions.TempDir))
                {
                    instructions.TempDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Temp";
                }

                // Make sure the temp directory exists
                Directory.CreateDirectory(instructions.TempDir);

                // Try to delete any files that exist in the directory
                try
                {
                    var dirInfo = new DirectoryInfo(instructions.TempDir);
                    foreach (System.IO.FileInfo file in dirInfo.GetFiles())
                    {
                        file.Delete();
                    }
                }
                catch (Exception)
                {
                    // Do nothing, deleting files isn't really all that important.
                }


                Console.WriteLine("Using TempDir: " + instructions.TempDir);

                // Download file to temp dir
                var downloader =
                    new FileDownloader(
                        new S3Caller(new SimpleCredentialsRetriever(instructions.AwsAccessKey, instructions.AwsSecretKey)),
                        instructions.BucketName);

                var    srcFilename    = instructions.RemotePath;
                string workerFilename = srcFilename;
                if (srcFilename.Contains('/'))
                {
                    workerFilename = srcFilename.Split('/').Last();
                }
                var destFullPath = Path.Combine(instructions.TempDir, workerFilename);

                Console.WriteLine("Downloading file from S3 bucket: " + instructions.BucketName);
                Console.WriteLine("Source file: " + srcFilename);
                Console.WriteLine("Destination: " + destFullPath);
                downloader.DownloadFile(srcFilename, destFullPath);

                // Extract zip to folder, replacing all files
                Console.WriteLine("Download complete. Extracting zip file to: " + instructions.Folder);
                Unzipper.Unzip(destFullPath, instructions.Folder, true);

                // Run the exe
                Console.WriteLine("Update complete. Launching: " + instructions.ExePath);
                success = true;
            }
            catch (Exception ex)
            {
                // TODO: Try to Post error message to url
                Console.WriteLine("THERE WAS AN ERROR!\n" + ex);
            }
            finally
            {
                if (instructions.IsService)
                {
                    Console.WriteLine("Launching as a service by appending the start argument");
                    Process.Start(instructions.ExePath, "start");
                }
                else
                {
                    Process.Start(instructions.ExePath);
                }

                if (success)
                {
                    Console.WriteLine("Success. Exiting.");
                }
            }
        }
Example #7
0
        private async Task <bool> ProcessWotmodUpdate(DatabasePackage package)
        {
            Logging.Editor("Processing wotmod update");

            DownloadInstructions downloadInstructions = package.DownloadInstructions;
            UpdateInstructions   updateInstructions   = package.UpdateInstructions;

            //verify that only one wotmod file exists in database file and get crc
            Logging.Editor("Checking for only 1 .wotmod file in the database zip file");
            using (ZipFile databaseZip = new ZipFile(downloadInstructions.DownloadedDatabaseZipFileLocation))
            {
                ZipEntry wotmodEntry = null;
                foreach (ZipEntry entry in databaseZip)
                {
                    if (entry.FileName.Contains(".wotmod"))
                    {
                        Logging.Editor("Found entry {0}", LogLevel.Info, entry.FileName);
                        if (wotmodEntry != null)
                        {
                            Logging.Editor("Entry for wotmod processing already exists and will be overriden!", LogLevel.Error);
                        }
                        wotmodEntry = entry;
                        updateInstructions.WotmodOldFilenameInZip = entry.FileName;
                    }
                }
                updateInstructions.WotmodDatabaseMD5 = await Utils.CreateMD5HashAsync(wotmodEntry.OpenReader());
            }

            //compare md5 of file in database zip to md5 of downloaded file
            updateInstructions.WotmodDownloadedMD5 = await Utils.CreateMD5HashAsync(downloadInstructions.DownloadedFileLocation);

            Logging.Editor("MD5 of download wotmod: {0}", LogLevel.Info, updateInstructions.WotmodDownloadedMD5);
            Logging.Editor("MD5 of database wotmod: {0}", LogLevel.Info, updateInstructions.WotmodDatabaseMD5);

            if (updateInstructions.WotmodDownloadedMD5.Equals(updateInstructions.WotmodDatabaseMD5))
            {
                Logging.Editor("MD5 files match, no need to update package");
                //DEBUG: comment this out to test method
                return(false);
            }

            //update wotmod file in zip
            Logging.Editor("MD5s don't match, updating wotmod in database zip with downloaded one");

            using (ZipFile databaseZip = new ZipFile(downloadInstructions.DownloadedDatabaseZipFileLocation))
            {
                //remove current entry and add new entry
                await Task.Run(() =>
                {
                    databaseZip.RemoveEntry(databaseZip[updateInstructions.WotmodOldFilenameInZip]);
                    databaseZip.AddEntry(updateInstructions.WotmodFilenameInZip, File.ReadAllBytes(downloadInstructions.DownloadedFileLocation));
                });

                //process patch instructions
                Logging.Editor("Processing patches");
                int patchesCount = 0;
                foreach (PatchUpdate patchUpdate in updateInstructions.PatchUpdates)
                {
                    Logging.Editor("Processing patch {0} of {1}", LogLevel.Info, ++patchesCount, updateInstructions.PatchUpdates.Count);
                    Logging.Editor(patchUpdate.PatchUpdateInformation);
                    Utils.AllowUIToUpdate();
                    if (!ProcessUpdatePatch(patchUpdate, databaseZip, package.PackageName))
                    {
                        Logging.Editor("Failed to process update patch {0}", LogLevel.Error, patchesCount);
                        return(false);
                    }
                }

                //save zip changes to disk
                Logging.Editor("Saving zip file changes to disk");
                databaseZip.SaveProgress += DatabaseZip_SaveProgress;
                await Task.Run(() =>
                {
                    databaseZip.Save();
                });
            }

            Logging.Editor("Save complete");
            AutoUpdateProgressBar.Value = AutoUpdateProgressBar.Minimum;
            return(true);
        }