HDiffPatcher.qml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import QtQuick 2.0
  2. import FileIO 1.0
  3. import Process 1.0
  4. Item {
  5. id: hdiffInstaller
  6. signal finished()
  7. signal error()
  8. property string outputDir
  9. readonly property alias isRunning: process.isRunning
  10. property var progress
  11. readonly property string hdiffFilesPath: outputDir + "hdifffiles.txt"
  12. property var filesToPatch: []
  13. property int totalFilesCount: 0
  14. Process {
  15. id: process
  16. property string hdiff
  17. property string lastOutput
  18. property string binPath: applicationDirPath + "/tools/"
  19. property string remoteFile
  20. property string sourceFile
  21. property string newFile
  22. property string patchFile
  23. workingDirectory: outputDir
  24. onFinished: {
  25. console.log("hpatchz exit code: " + code)
  26. if (code === 0) {
  27. handleSuccess()
  28. } else {
  29. handleError()
  30. }
  31. }
  32. onOutputRead: {
  33. lastOutput = output
  34. }
  35. function install(file) {
  36. ui.log("Updating " + file)
  37. let patchedCount = totalFilesCount - filesToPatch.length
  38. progress = {
  39. value: patchedCount,
  40. total: totalFilesCount,
  41. }
  42. remoteFile = file
  43. sourceFile = outputDir + file
  44. newFile = sourceFile + ".new"
  45. patchFile = sourceFile + ".hdiff"
  46. FileIO.removeFile(newFile)
  47. process.start(binPath + "hpatchz", [sourceFile, patchFile, newFile])
  48. }
  49. function handleSuccess() {
  50. FileIO.removeFile(sourceFile)
  51. FileIO.removeFile(patchFile)
  52. FileIO.rename(newFile, sourceFile)
  53. installNextPatch()
  54. }
  55. function handleError() {
  56. if (integrityCheck.check(remoteFile)) {
  57. ui.log(remoteFile + qsTr(" is already patched"))
  58. // file was already patched ingame, skipping
  59. FileIO.removeFile(patchFile)
  60. installNextPatch()
  61. return
  62. }
  63. ui.log(lastOutput)
  64. hdiffInstaller.error()
  65. }
  66. }
  67. function isInstallNeeded() {
  68. return FileIO.isFileExists(hdiffFilesPath)
  69. }
  70. function installAll() {
  71. ui.log("")
  72. ui.log(qsTr("Installing HDiff patches..."))
  73. let hdiffs = FileIO.readTextFile(hdiffFilesPath).split("\n")
  74. let files = []
  75. for (let hdiff of hdiffs) {
  76. if (hdiff.trim() === "")
  77. continue
  78. try {
  79. let json = JSON.parse(hdiff)
  80. let baseName = outputDir + json.remoteName
  81. if (FileIO.isFileExists(baseName + ".hdiff")) {
  82. files.push(json.remoteName)
  83. }
  84. } catch(e) {
  85. ui.log(file + ": " + e)
  86. }
  87. }
  88. installByList(files)
  89. }
  90. function installByList(files) {
  91. filesToPatch = files
  92. totalFilesCount = files.length
  93. installNextPatch()
  94. }
  95. function installNextPatch() {
  96. let file = filesToPatch.shift()
  97. if (!file) {
  98. ui.log(qsTr("HDiff patches have been installed."))
  99. FileIO.removeFile(hdiffFilesPath)
  100. finished()
  101. return
  102. }
  103. process.install(file)
  104. }
  105. function cancel() {
  106. process.terminate()
  107. }
  108. }