Files
dr1p4ns1-mirror/dr1p4ns1.py
decoded d56b021400 init
2022-02-17 22:41:00 -06:00

227 lines
9.8 KiB
Python

import sys
#######################################################################################################################
class C:
###################################################################################################################
commands = {
'A': 'up',
'B': 'down',
'C': 'forward',
'D': 'back',
'H': 'position',
'f': 'position',
'J': 'erase',
'K': 'erase_line',
's': 'save_cursor',
'u': 'restore_cursor',
'm': 'color',
'R': 'report_cursor_position'
}
###################################################################################################################
ascii_table="""
ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒ\
áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐\
└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀\
αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ 
"""
###################################################################################################################
for r in (('\n',''),(' ','')):
ascii_table=ascii_table.replace(*r)
###################################################################################################################
asc2uni=dict(zip([i for i in range(128,256)],[ord(c) for c in ascii_table]))
###################################################################################################################
asc2chr=dict(zip([i for i in range(128,256)],[c for c in ascii_table]))
###################################################################################################################
def __init__(self,ansifile="",width=80,height=25):
self.width=width
self.height=height
self.openansi(ansifile)
self.code='\x1b[0m'
print("\x1bc\x1b[1;31m[ DR1P LOADED ]\x1b[0m\n")
###################################################################################################################
def openansi(self,s):
try:
f=open(s,'rb')
self.ansifile=f.read().decode('cp437')
f.close()
except:
print(f'\n\x1b[1;31m[ ERROR: "{s}" FILE NOT FOUND ] - *USAGE: `python3 {sys.argv[0]} work.ans`\x1b[0m')
sys.exit(1)
###################################################################################################################
def printascii(self):
for i,_ in enumerate(self.ascii_table):
_int=str(i+128).zfill(3)
_hex=hex(i+128)[2:].upper().zfill(2)
_ord=str(ord(_)).zfill(4)
print(f'{_} - {_int} - {_hex} - {_ord}')
###################################################################################################################
def printrange(self,n,nn):
for index,_ in enumerate(c.ansifile):
h=hex(_)[2:].upper()
i=str(_).zfill(3)
s=''
if _>47 and _<127:
s=chr(_)
else:
s='.'
l=str(index).zfill(len(str(len(c.ansifile))))
if index >=n and index <=nn:
print(f"{l} {i} {h} {s}")
if index==nn: break
###################################################################################################################
def printhex(self):
###########################################
theasc=[]
thehex=[]
buffer=''
###########################################
for _ in self.ansifile:
buffer+=hex(ord(_)).upper()[2:].zfill(2)
###########################################
for _ in self.chunk(buffer,32):
subchunks=''
for i,__ in enumerate(self.chunk(_,2)):
if i>0 and i%4==0: subchunks+=" "
subchunks+=f'{__} '
thehex.append(subchunks.strip())
###########################################
for __ in thehex:
buffer=''
for _ in __.split():
i=int(_,16)
if i>47 and i<127:
buffer+=chr(i)
else:
buffer+='.'
theasc.append(buffer)
###########################################
for _ in range(len(thehex)):
old=buffer
buffer={thehex[_]}
if not buffer == old:
print(f"{hex(16 * _)[2:].zfill(8)} {thehex[_]} {theasc[_]}")
###################################################################################################################
def hexdump(self,visible=True):
if visible == True:
self.printhex()
self.codes=[]
code_start=0
code_end=0
code_state=False
buffer=''
for i,_ in enumerate(self.ansifile):
if code_state==False:
if _ == '\x1b':
code_state=True
code_start=i
elif _ == '\x1a':
code=self.ansifile[i:]
self.codes.append([code,i,len(self.ansifile)])
else:
if 64 <= ord(_) <= 126:
if _ in self.commands:
code_state=False
code_end=i+1
code=self.ansifile[code_start:code_end]
self.codes.append([code,code_start,code_end])
###################################################################################################################
def findall(self,s,w):
return [i for i in range(len(s)) if s.startswith(w, i)]
###################################################################################################################
def getsauce(self):
try:
self.ansifile[-128:]
self.ansifile_xwidth=ord(c.ansifile[-128:][96])
self.ansifile_yheight=ord(c.ansifile[-128:][98])
self.width=self.ansifile_xwidth
self.height=self.ansifile_yheight
print(f'\x1b[31m[ SAUCE INFO FOUND ] - X: {self.width} Y: {self.height}\x1b[0m\n')
except:
print('\x1b[31m[ NO SAUCE INFO FOUND ]\x1b[0m\n')
###################################################################################################################
def chunk(self,s,n):
return [s[i:i+n] for i in range(0,len(s),n)]
###################################################################################################################
def rgb(self,t='',r=0,g=0,b=0):
"""colorize text with rgb values"""
return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m"
###################################################################################################################
def vga(self,t='',i=0):
"""colorize text using vga color set"""
vga = ['#000000','#aa0000','#00aa00','#aa5500','#0000aa','#aa00aa','#00aaaa','#aaaaaa',
'#555555','#ff5555','#55ff55','#ffff55','#5555ff','#ff55ff','#55ffff','#ffffff',]
try:
r,g,b=int(vga[i][1:][0:2],16),int(vga[i][1:][2:4],16),int(vga[i][1:][4:6],16)
except:
r,g,b=0,0,0
return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m"
#######################################################################################################################
if __name__ == "__main__":
################################################################
try:
input_file=sys.argv[1]
except:
input_file='work.ans'
c=C(ansifile=input_file,width=80)
c.getsauce()
c.hexdump(visible=False)
################################################################
op=[]
fb=c.ansifile
xnew=0
xold=0
############
for _ in c.codes:
xnew=_[1]
distance=xnew-xold
if not distance == 0:
gap=fb[xold:xold+distance]
op.append(gap)
op.append(_[0])
xold=_[2]
################################################################
optype=[]
############
for i,_op in enumerate(op):
if _op.startswith('\x1b'):
if _op.endswith('m'):
optype.append(2)
########################################################
elif _op.endswith('C'):
optype.append(3)
########################################################
else:
optype.append(0)
############################################################
elif _op.startswith('\x1a'):
optype.append(4)
############################################################
else:
optype.append(1)
################################################################
offset=0
processed=[]
processing=''
############
for i,_optype in enumerate(optype):
if _optype==2:
processing+=op[i]
offset+=len(op[i])
############################################################
elif _optype==3:
count=int(op[i].split('[')[1].split('C')[0])
processing+=' '*count
############################################################
elif _optype==1:
processing+=op[i]
############################################################
if len(processing)-offset > c.width:
while len(processing)-offset > c.width:
deconcatenate=processing[0:c.width+offset]
processed.append(deconcatenate)
processing=processing.replace(deconcatenate,'',1)
offset=0
#################################################################
for _ in processed: print(_)
print('\x1b[1;31m[ COMPLETED ]\x1b[0m\n')
#####################################################################