Example #1
0
        // Loads update files from an XML for SuperSonic16's Modloader
        public ModUpdate GetUpdateFromXML(Mod mod, XDocument xml)
        {
            // Checks
            if (xml.Root == null)
            {
                return(null);
            }

            if (xml.Root.Element("UpdateInfo") == null)
            {
                return(null);
            }

            var root               = xml.Root;
            var modUpdateInfo      = xml.Root.Element("UpdateInfo");
            var modUpdateFiles     = xml.Root.Element("UpdateFiles");
            var modVersionAtt      = modUpdateInfo.Attribute("version");
            var modDownloadSizeAtt = modUpdateInfo.Attribute("downloadSize");

            // Checks
            if (modVersionAtt == null || modDownloadSizeAtt == null || modUpdateFiles == null)
            {
                return(null);
            }

            var update = new ModUpdate()
            {
                VersionString      = modVersionAtt.Value,
                Name               = mod.Title,
                DownloadSizeString = modDownloadSizeAtt.Value,
                ChangeLog          = modUpdateInfo.Value
            };

            // Reads the list of files
            foreach (var element in modUpdateFiles.Elements())
            {
                if (element.Name != "UpdateFile")
                {
                    continue;
                }

                var filePathAtt = element.Element("FilePath");
                var URLAtt      = element.Element("URL");
                var SHA256Att   = element.Element("SHA256");

                // Checks
                if (filePathAtt == null || URLAtt == null)
                {
                    continue;
                }

                if (Path.IsPathRooted(filePathAtt.Value) ||
                    filePathAtt.Value.Contains(".."))
                {
                    continue; // Path must not go back or be absolute
                }
                var file = new ModUpdateFile();
                file.FileName = filePathAtt.Value;
                file.URL      = URLAtt.Value;

                // Convert any Download links
                file.URL = DownloadTools.GetDirectDownloadURL(file.URL);

                if (SHA256Att != null)
                {
                    file.SHA256 = SHA256Att.Value;
                }

                update.Files.Add(file);
            }

            return(update);
        }
 public CreateUpdateForm(Mod mod) : this()
 {
     TextBox_NewMod.Text    = mod.FilePath;
     TextBox_NewMod.Enabled = false;
     TextBox_Version.Text   = mod.Version;
 }
Example #3
0
 // Loads update files from a TXT file for Radfordhound's Modloader
 public ModUpdate GetUpdateFromTXT(Mod mod, string data)
 {
     // TODO
     return(null);
 }
        public void CreateXMLUpdate(Mod modifiedMod, Mod unmodifiedMod, string outPath)
        {
            if (MessageBox.Show("NOTE: All files in the selected directory will be deleted", Program.ProgramName,
                                MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }

            var sha  = new SHA256Managed();
            var root = new XElement("Update");

            // Recreates the directory
            if (Directory.Exists(outPath))
            {
                Directory.Delete(outPath, true);
            }
            Directory.CreateDirectory(outPath);

            try
            {
                // Creates all of the directories
                foreach (string path in Directory.GetDirectories(
                             modifiedMod.RootDirectory, "*", SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(path.Replace(modifiedMod.RootDirectory, outPath));
                }

                // List of items that have been modified
                var items = new List <FileItem>();

                foreach (var item in Items)
                {
                    string unmodifiedFilePath = Path.Combine(unmodifiedMod.RootDirectory, item.RelativePath);

                    // Checks if the file exists in the unmodified folder
                    if (!File.Exists(unmodifiedFilePath))
                    {
                        items.Add(item);
                        continue;
                    }

                    var modifiedBytes   = File.ReadAllBytes(item.FullPath);
                    var unModifiedBytes = File.ReadAllBytes(unmodifiedFilePath);
                    if (CompareByteArray(modifiedBytes, unModifiedBytes))
                    {
                        items.Add(item);
                    }
                }

                // UpdateInfo
                var updateInfo = new XElement("UpdateInfo", TextBox_Changelog.Text);
                updateInfo.Add(new XAttribute("version", modifiedMod.Version));
                updateInfo.Add(new XAttribute("downloadSize", textBox5.Text));

                // UpdateFiles
                var updateFiles = new XElement("UpdateFiles");

                foreach (var item in items)
                {
                    string sha256 = "";
                    using (var stream = File.OpenRead(item.FullPath))
                        sha256 = Program.ByteArrayToString(sha.ComputeHash(stream));

                    // UpdateFile
                    var updateFile = new XElement("UpdateFile");
                    updateFile.Add(new XElement("FilePath", item.RelativePath));
                    updateFile.Add(new XElement("URL", item.URL.Replace('\\', '/')));
                    updateFile.Add(new XElement("SHA256", sha256));
                    updateFiles.Add(updateFile);
                }

                // Copies all the modified files into the output directory
                foreach (var item in items)
                {
                    File.Copy(item.FullPath, Path.Combine(outPath, item.RelativePath), true);
                }

                // Saves the XML file
                root.Add(updateInfo);
                root.Add(updateFiles);
                var xml = new XDocument(root);
                xml.Save(Path.Combine(outPath, unmodifiedMod.Title + ".xml"));

                // Opens the output folder in explorer
                Process.Start("explorer", outPath);
            }
            catch (Exception ex)
            {
                MainForm.AddMessage("Exception thrown while creating update files", ex);
            }
        }