IntegrityCheck.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import QtQuick 2.0
  2. import FileIO 1.0
  3. import Async 1.0
  4. Item {
  5. id: integrityCheck
  6. readonly property string pkgVersionFilePath: settings.gamePath + "pkg_version"
  7. signal finished()
  8. property var progress: ({})
  9. property var files: []
  10. property var checked: ({})
  11. property int okCount: 0
  12. property int errorCount: 0
  13. property int ignoredCount: 0
  14. property bool isWinePatchInstalled: false
  15. readonly property var filesToIgnore: !isWinePatchInstalled ? []
  16. : ["crashreport.exe", "upload_crash.exe", "UnityPlayer.dll", "xlua.dll"]
  17. Async {
  18. id: async
  19. onFinished: {
  20. ui.log(qsTr("Integrity check finished."))
  21. ui.log(qsTr("Files OK: ") + okCount + qsTr(", Errors: ") + errorCount + qsTr(", Ignored: ") + ignoredCount)
  22. integrityCheck.finished()
  23. }
  24. onResult: {
  25. progress = {
  26. value: okCount + errorCount + ignoredCount + 1,
  27. total: files.length
  28. }
  29. let filePath = values[0]
  30. let fileName = filePath.replace(/^.*\//, "")
  31. let fileMd5 = values[1]
  32. if (!fileMd5) {
  33. if (filesToIgnore.indexOf(fileName) !== -1) {
  34. ++ignoredCount
  35. } else {
  36. ++errorCount
  37. ui.log("⚠️ MISSING: " + filePath)
  38. }
  39. return
  40. }
  41. let pkg = checked[filePath]
  42. if (!pkg) {
  43. ++errorCount
  44. ui.log("⚠️ Unknown File" + filePath)
  45. return
  46. }
  47. fileMd5 = fileMd5.toLowerCase()
  48. let pkgMd5 = pkg.md5.toLowerCase()
  49. if (fileMd5 !== pkgMd5) {
  50. if (filesToIgnore.indexOf(fileName) !== -1) {
  51. ++ignoredCount
  52. } else {
  53. ui.log(qsTr("⚠️ WRONG MD5: ") + fileMd5 + qsTr(" vs ") + pkgMd5 + ": " + pkg.remoteName)
  54. ++errorCount
  55. }
  56. } else {
  57. ++okCount
  58. }
  59. }
  60. }
  61. function reload() {
  62. let pkgs = FileIO.readTextFile(pkgVersionFilePath).split("\n")
  63. let files = []
  64. for (let pkg of pkgs) {
  65. if (pkg.trim() === "")
  66. continue
  67. try {
  68. let json = JSON.parse(pkg)
  69. files.push(json)
  70. } catch(e) {
  71. ui.log(file + ": " + e)
  72. }
  73. }
  74. integrityCheck.files = files
  75. }
  76. function stop() {
  77. async.stop()
  78. }
  79. function checkAll() {
  80. isWinePatchInstalled = FileIO.isFileExists(settings.gamePath + 'launcher.bat')
  81. okCount = 0
  82. errorCount = 0
  83. ignoredCount = 0
  84. ui.log("")
  85. ui.log(qsTr("Integrity check started. This may take time..."))
  86. checked = {}
  87. let result = true
  88. let fileNames = []
  89. for (let file of files) {
  90. let fileName = settings.gamePath + file.remoteName
  91. fileNames.push(fileName)
  92. checked[fileName] = file
  93. }
  94. async.md5List(fileNames)
  95. }
  96. function check(remoteName) {
  97. let pkg = getPkg(remoteName)
  98. let fileSize = FileIO.size(settings.gamePath + remoteName)
  99. if (fileSize !== pkg.fileSize) {
  100. ui.log(qsTr("File size doesn't match: ")
  101. + fileSize + qsTr(" vs ") + pkg.fileSize + ": " + remoteName)
  102. return false
  103. }
  104. let md5 = FileIO.md5(settings.gamePath + remoteName).toLowerCase()
  105. let pkgMd5 = pkg.md5.toLowerCase()
  106. if (md5 !== pkgMd5) {
  107. ui.log(qsTr("File md5 doesn't match: ")
  108. + md5 + qsTr(" vs ") + pkgMd5 + ": " + remoteName)
  109. return false
  110. }
  111. ui.log(remoteName + qsTr(" integrity ok"))
  112. return true
  113. }
  114. function getPkg(remoteName) {
  115. for (let file of files) {
  116. if (file.remoteName === remoteName) {
  117. return file
  118. }
  119. }
  120. }
  121. }