Poodletooth-iLand/build/make.py

206 lines
6.2 KiB
Python
Raw Normal View History

2015-06-04 16:30:23 -05:00
from panda3d.core import *
import argparse, marshal, struct
import glob, sys, os
import rc4
parser = argparse.ArgumentParser()
parser.add_argument('--compile-cxx', '-c', action='store_true',
help='Compile the CXX codes and generate Nirai.exe into built.')
parser.add_argument('--make-nri', '-n', action='store_true',
help='Generate stride NRI.')
args = parser.parse_args()
2015-06-05 00:05:01 -05:00
# BEGIN (STRIPPED AND MODIFIED) COPY FROM niraitools.py
2015-06-04 16:30:23 -05:00
class NiraiPackager:
HEADER = 'NRI\n'
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def __init__(self, outfile):
self.modules = {}
self.outfile = outfile
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def __read_file(self, filename, mangler=None):
with open(filename, 'rb') as f:
data = f.read()
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
base = filename.rsplit('.', 1)[0].replace('\\', '/').replace('/', '.')
pkg = base.endswith('.__init__')
moduleName = base.rsplit('.', 1)[0] if pkg else base
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
name = moduleName
if mangler is not None:
name = mangler(name)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
if not name:
return '', ('', 0)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
try:
data = self.compile_module(name, data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
except:
print 'WARNING: Failed to compile', filename
return '', ('', 0)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
size = len(data) * (-1 if pkg else 1)
return name, (data, size)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def compile_module(self, name, data):
return marshal.dumps(compile(data, name, 'exec'))
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def add_module(self, moduleName, data, size=None, compile=False):
if compile:
data = self.compile_module(moduleName, data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
if size is None:
size = len(data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
self.modules[moduleName] = (data, size)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def add_file(self, filename, mangler=None):
print 'Adding file', filename
moduleName, (data, size) = self.__read_file(filename, mangler)
if moduleName:
moduleName = os.path.basename(filename).rsplit('.', 1)[0]
self.add_module(moduleName, data, size)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def add_directory(self, dir, mangler=None):
print 'Adding directory', dir
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def _recurse_dir(dir):
for f in os.listdir(dir):
f = os.path.join(dir, f)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
if os.path.isdir(f):
_recurse_dir(f)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
elif f.endswith('py'):
moduleName, (data, size) = self.__read_file(f, mangler)
if moduleName:
self.add_module(moduleName, data, size)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
_recurse_dir(dir)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def get_mangle_base(self, *path):
return len(os.path.join(*path).rsplit('.', 1)[0].replace('\\', '/').replace('/', '.')) + 1
def write_out(self):
f = open(self.outfile, 'wb')
f.write(self.HEADER)
f.write(self.process_modules())
f.close()
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def generate_key(self, size=256):
return os.urandom(size)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def dump_key(self, key):
for k in key:
print ord(k),
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
print
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def process_modules(self):
# Pure virtual
raise NotImplementedError('process_datagram')
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def get_file_contents(self, filename, keysize=0):
with open(filename, 'rb') as f:
data = f.read()
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
if keysize:
key = self.generate_key(keysize)
rc4.rc4_setkey(key)
data = key + rc4.rc4(data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
return data
2015-06-05 00:05:01 -05:00
# END COPY FROM niraitools.py
2015-06-04 16:30:23 -05:00
class StridePackager(NiraiPackager):
HEADER = 'STRIDETT'
BASEDIR = '..' + os.sep
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def __init__(self, outfile):
NiraiPackager.__init__(self, outfile)
self.__manglebase = self.get_mangle_base(self.BASEDIR)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def add_source_dir(self, dir):
self.add_directory(self.BASEDIR + dir, mangler=self.__mangler)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def add_data_file(self, file):
mb = self.get_mangle_base('data/')
self.add_file('data/%s.py' % file, mangler=lambda x: x[mb:])
2015-06-05 00:05:01 -05:00
def __mangler(self, name):
2015-06-04 16:30:23 -05:00
if name.endswith('AI') or name.endswith('UD') or name in ('ToontownAIRepository', 'ToontownUberRepository',
'ToontownInternalRepository'):
if not 'NonRepeatableRandomSource' in name:
return ''
return name[self.__manglebase:].strip('.')
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def generate_niraidata(self):
print 'Generating niraidata'
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
config = self.get_file_contents('../dependencies/config/release/en.prc')
config += '\n\n' + self.get_file_contents('../dependencies/config/general.prc')
key = self.generate_key(128)
rc4.rc4_setkey(key)
config = key + rc4.rc4(config)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
niraidata = 'CONFIG = %r' % config
niraidata += '\nDC = %r' % self.get_file_contents('../dependencies/astron/dclass/stride.dc', 128)
self.add_module('niraidata', niraidata, compile=True)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
def process_modules(self):
with open('base.dg', 'rb') as f:
basesize, = struct.unpack('<I', f.read(4))
data = f.read()
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
dg = Datagram()
dg.addUint32(len(self.modules) + basesize)
dg.appendData(data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
for moduleName in self.modules:
data, size = self.modules[moduleName]
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
dg.addString(moduleName)
dg.addInt32(size)
dg.appendData(data)
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
data = dg.getMessage()
compressed = compressString(data, 9)
key = self.generate_key(100)
fixed = ''.join(chr((i ^ (5 * i + 7)) % ((i + 6) * 10)) for i in xrange(28))
rc4.rc4_setkey(key + fixed)
2015-06-05 00:05:01 -05:00
data = rc4.rc4(compressed)
2015-06-04 16:30:23 -05:00
return key + data
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
# 1. Make the NRI
if args.make_nri:
if not os.path.exists('built'):
os.mkdir('built')
2015-06-05 18:21:47 -05:00
2015-06-04 16:30:23 -05:00
pkg = StridePackager('built/stride.dist')
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
pkg.add_source_dir('otp')
pkg.add_source_dir('toontown')
pkg.add_data_file('NiraiStart')
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
pkg.generate_niraidata()
pkg.write_out()
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
# 2. Compile CXX stuff
if args.compile_cxx:
if not os.path.exists('built'):
os.mkdir('built')
2015-06-05 18:21:47 -05:00
2015-06-04 16:30:23 -05:00
sys.path.append('../../../N2')
from niraitools import NiraiCompiler
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
compiler = NiraiCompiler('stride.exe', r'"C:\\Users\\Usuario\\workspace\\nirai-panda3d\\thirdparty\\win-libs-vc10"',
libs=set(glob.glob('libpandadna/libpandadna.dir/Release/*.obj')))
compiler.add_nirai_files()
compiler.add_source('src/stride.cxx')
2015-06-05 00:05:01 -05:00
2015-06-04 16:30:23 -05:00
compiler.run()