mirror of
https://github.com/pret/pokemon-reverse-engineering-tools.git
synced 2026-03-21 17:24:42 -05:00
Rewrite scan_includes.py. Add include paths.
This commit is contained in:
parent
a87f66964c
commit
c9e7f22f8f
|
|
@ -1,36 +1,59 @@
|
|||
#!/bin/python
|
||||
# coding: utf-8
|
||||
|
||||
"""
|
||||
Recursively scan an asm file for rgbasm INCLUDEs and INCBINs.
|
||||
Used to generate dependencies for each rgbasm object.
|
||||
Recursively scan an asm file for dependencies.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
import configuration
|
||||
conf = configuration.Config()
|
||||
|
||||
def recursive_scan(filename, includes = []):
|
||||
if (filename[-4:] == '.asm' or filename[-3] == '.tx') and os.path.exists(filename):
|
||||
lines = open(filename).readlines()
|
||||
for line in lines:
|
||||
for directive in ('INCLUDE', 'INCBIN'):
|
||||
if directive in line:
|
||||
line = line[:line.find(';')]
|
||||
if directive in line:
|
||||
include = line.split('"')[1]
|
||||
if include not in includes:
|
||||
includes += [include]
|
||||
includes = recursive_scan(os.path.join(conf.path, include), includes)
|
||||
break
|
||||
return includes
|
||||
class IncludeReader:
|
||||
"""
|
||||
Usage:
|
||||
includer = IncludeReader()
|
||||
includer.read(filename)
|
||||
or
|
||||
includer = IncludeReader(filename='filename.asm')
|
||||
includer.read()
|
||||
"""
|
||||
path = ''
|
||||
includes = []
|
||||
directives = ['INCLUDE', 'INCBIN']
|
||||
extensions = ['.asm', '.tx']
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
def read(self, filename=None):
|
||||
if filename is None:
|
||||
if hasattr(self, 'filename'):
|
||||
filename = os.path.join(self.path, self.filename)
|
||||
else:
|
||||
raise Exception, 'no filename given!'
|
||||
if os.path.splitext(filename)[1] in self.extensions and os.path.exists(filename):
|
||||
for line in open(filename).readlines():
|
||||
self.read_line(line)
|
||||
|
||||
def read_line(self, line):
|
||||
parts = line[:line.find(';')].split()
|
||||
for directive in self.directives:
|
||||
if directive in map(str.upper, parts):
|
||||
include = os.path.join(self.path, parts[parts.index(directive) + 1].split('"')[1])
|
||||
if include not in self.includes:
|
||||
self.includes.append(include)
|
||||
self.read(include)
|
||||
|
||||
if __name__ == '__main__':
|
||||
filenames = sys.argv[1:]
|
||||
dependencies = []
|
||||
for filename in filenames:
|
||||
dependencies += recursive_scan(os.path.join(conf.path, filename))
|
||||
dependencies = list(set(dependencies))
|
||||
sys.stdout.write(' '.join(dependencies))
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('-i', default='')
|
||||
ap.add_argument('filenames', nargs='*')
|
||||
args = ap.parse_args()
|
||||
|
||||
includes = IncludeReader(path=args.i)
|
||||
for filename in args.filenames:
|
||||
includes.read(filename)
|
||||
sys.stdout.write(' '.join(includes.includes))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user