This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HBChecker.py
executable file
·268 lines (249 loc) · 6.45 KB
/
HBChecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# HBChecker by Epicpkmn11
# https://github.com/Epicpkmn11/HBChecker/
# Feel free to make custom HBCheckerItems.py files for other uses
import os
import sys
import json
import zlib
# Fixing Python 2 compatibility
if(sys.version_info.major < (3)):
input = raw_input
# Creating variables
fileSetChoices = ''
fileFoundList = ''
filesFound = []
filesMissing = []
fileSets = {}
itemsFile = 'HBCheckerItems.json'
bufferSize = 65536
checkCRC = True
if sys.platform == 'win32':
def columns():
try:
return os.get_terminal_size(sys.__stdout__.fileno()).columns
except:
return 120
else:
def columns():
try:
return int(os.popen('stty size', 'r').read().split()[1])
except:
return 80
# Defining functions
# Allows clearing the screen on win32 & unix-based systems
def clear():
if sys.platform == 'win32':
os.system('cls')
else:
os.system('clear')
# Let's you convert a base 16 int to a string properly
def intToStr(n,base):
convertString = "0123456789ABCDEF"
if n < base:
return convertString[n]
else:
return intToStr(n//base,base) + convertString[n%base]
# Checks if the file sets exist
def checkExist(fileSet):
found = []
file = fileSets[fileSet][0]
chksum = file.find(';')
if chksum != -1:
crc = file[chksum+1:].upper()
file = file[:chksum]
newLine = file.find('\n')
if newLine != -1:
file = file[:(newLine-1)]
if os.path.exists(file):
if checkCRC:
if chksum != -1:
checkFile = open(file, 'rb')
buffr = checkFile.read(bufferSize)
crcValue = 0
while len(buffr) > 0:
crcValue = zlib.crc32(buffr, crcValue)
buffr = checkFile.read(bufferSize)
crcString = intToStr(crcValue, 16)
while len(crcString)<8:
crcString = '0' + crcString
if crcString == crc:
found = [fileSet]
else:
found = [fileSet]
else:
found = [fileSet]
return(found)
# Checks if the files within a set exist
def check(fileSet):
missing = []
files = fileSets[fileSet][1:]
for file in files:
chksum = file.find(';')
if chksum != -1:
crc = file[chksum+1:].upper()
file = file[:chksum]
fileCopy = file
newLine = fileCopy.find('\n')
if newLine != -1:
fileCopy = fileCopy[:(newLine-1)]
if os.path.exists(fileCopy):
if checkCRC:
if chksum != -1:
checkFile = open(fileCopy, 'rb')
buffr = checkFile.read(bufferSize)
crcValue = 0
while len(buffr) > 0:
crcValue = zlib.crc32(buffr, crcValue) & 0xffffffff
buffr = checkFile.read(bufferSize)
crcString = intToStr(crcValue, 16)
while len(crcString)<8:
crcString = '0' + crcString
if crcString != crc:
missing += [file + '\nwas corrupted: CRC-32 DIDN\'T MATCH ' + intToStr(crcValue, 16) + ':'+ crc]
else:
missing += [file + '\nwas missing']
return(missing)
# Prints out the missing files from a set
def printMissing(missingList, printLines):
filesMissing = ''
for file in missingList:
filesMissing += file + '\n\n'
if len(filesMissing)>0:
if printLines:
clear()
print('='*columns())
if checkCRC:
print('The following files were missing or corrupted:')
else:
print('The following files were missing:')
print('='*columns())
print('\n'+filesMissing[:-1])
else:
if printLines:
clear()
print('='*columns())
if not printLines:
print('No files were missing! (^ Make sure everything you need checked was found)')
else:
if checkCRC:
print('No files were missing or corrupted!')
else:
print('No files were missing!')
# Loading items json file
while True:
try:
with open(itemsFile, 'r') as itemsJson:
fileSets = json.load(itemsJson)
break
except:
clear()
print('='*columns())
print(itemsFile + ' not found')
print('Please type the name of an items file, Drag/Drop it here, or press Enter to quit')
print('='*columns())
itemsFile = input('> ')
# Remove \ on non windows systems
if sys.platform != 'win32':
while itemsFile.find('\\') != -1:
bkslsh = itemsFile.find('\\')
itemsFileCopy = itemsFile
itemsFileCopy = itemsFile[:bkslsh]
itemsFileCopy += itemsFile[bkslsh+1:]
itemsFile = itemsFileCopy
# Remove spaces from end of file names (Mac puts them on drag/drop)
if itemsFile[-1:] == ' ':
itemsFile = itemsFile[:-1]
# Remove " from file names (Windows puts them on drag/drop)
if itemsFile[:1] == '"':
itemsFile = itemsFile[1:]
if itemsFile[-1:] == '"':
itemsFile = itemsFile[:-1]
if itemsFile == '':
quit()
# Clearing the screen
clear()
# Checking if sets exist
for fileSet in fileSets:
filesFound += checkExist(fileSet)
# Preping to print sets found nicely
i=0
filesFoundAmount = len(filesFound)
for file in filesFound:
space = file.find(' ')
file = file[(space+1):]
if(i+2)<filesFoundAmount:
fileFoundList += file + ', '
elif(i+1)<filesFoundAmount:
if filesFoundAmount==2:
fileFoundList += file + ' and '
else:
fileFoundList += file + ', and '
else:
fileFoundList += file
i+=1
# Printing sets found
if len(fileFoundList)>0:
clear()
print('='*columns())
if filesFoundAmount>1:
print(fileFoundList + ' were found')
else:
print(fileFoundList + ' was found')
else:
print('='*columns())
print('Nothing was found')
# Printing missing files
for file in filesFound:
filesMissing += check(file)
printMissing(filesMissing,False)
# Prepping the file set options for manual checking
fileSetsAmount = len(fileSets)
i=1
for fileSet in sorted(fileSets):
if(i%3)!=0:
fileSetChoices += fileSet + ' '
if len(fileSet)<16:
fileSetChoices += ' '
if len(fileSet)<8:
fileSetChoices += ' '
else:
if i<fileSetsAmount:
fileSetChoices += fileSet + '\n'
else:
fileSetChoices += fileSet
i+=1
# Infinite loop so you can check as many options as you want
while True:
clearScreen = True
print('='*columns())
print('Would you like to check for more?')
if checkCRC:
print('Your options are:\n(Enter the number, 0 to disable checksum, or press Enter to quit)')
else:
print('Your options are:\n(Enter the number, 0 to enable checksum, or press Enter to quit)')
print('\n'+fileSetChoices)
manualCheck = input('> ')
for fileSet in fileSets:
if manualCheck == '':
quit()
elif manualCheck == '0':
if checkCRC:
checkCRC = False
else:
checkCRC = True
clearScreen = False
clear()
print('='*columns())
if checkCRC:
print('Corruption detection enabled')
else:
print('Corruption detection disabled')
break
elif manualCheck == fileSet[:len(manualCheck)]:
printMissing(check(fileSet),True)
clearScreen = False
break
if clearScreen:
clear()
print('='*columns())
print('Invalid File Set')