Esempio n. 1
0
def unpad(string, padding=None, blocksize=8, raw=False):
    """
    Custom string unpadding function relying on Tinyscript's data type check functions and a few implemented padding
     algorithms.
    
    NB: Default algorithm is the zero padding.
    
    :param padding:   padding characters or padding algorithm
    :param blocksize: block size to be considered for padding
    :param raw:       whether the input string must be considered as a normal string (disables the check for hexstring)
    """
    s, bs = string, blocksize
    isb, ish = is_bin(s) and not raw, is_hex(s) and not raw
    if padding in PAD and isb or padding == "bit" and not isb:
        raise ValueError("Incompatible padding algorithm")
    if blocksize <= 0:
        raise ValueError("Block size must be a positive integer")
    to_char = lambda x: [chr(x), "{:0>2}".format(hex(x)[2:])][ish]
    lastb = [
        litev("0x" + (s[i:i + 2 or len(s)] or "0"))
        for i in range(-bs * 2, 0, 2)
    ] if ish else list(map(ord, s[-bs:]))
    n = lastb[-1]
    lp = [1, 2][ish]
    if padding == "ansic9.23":
        if n <= bs and lastb[-n:-1] == [0] * (n - 1):
            return s[:-n * lp]
    elif padding == "bit":
        l = len(s)
        s = s.rstrip("0")
        if len(s) < l:
            return s[:-1]
    elif padding == "incremental":
        if n <= bs and lastb[-n:-1] == list(range(1, n)):
            return s[:-n * lp]
    elif padding == "iso7816-4":
        n = 0
        while lastb[-1] == 0:
            lastb.pop()
            n += 1
        if n < bs and lastb[-1] == 128:
            lastb.pop()
            n += 1
            return s[:-n * lp]
    elif padding in ["pkcs5", "pkcs7"]:
        if n <= bs and lastb[-n:-1] == (n - 1) * [n]:
            return s[:-n * lp]
    elif padding == "w3c":
        n = lastb[-1]
        if n <= bs:
            return s[:-n * lp or len(s)]
    else:
        return s.rstrip("0") if isb else pad(s.rstrip("0"), "0",
                                             2) if ish else s.rstrip("\x00")
    return s
Esempio n. 2
0
def init_new_data():
	
	scene = bge.logic.getCurrentScene()
	
	if not 'map' in globalDict.keys():
		
		first_map = expandPath('//maps/') + [m for m in os.listdir(expandPath('//maps/')) if m.endswith('.dwmap')][0]
		
		with open(first_map, 'r') as open_file:
			globalDict['map'] = litev(open_file.read())
	
	# Map
	if globalDict['map'] == {}:
		globalDict['map'] = default_map.copy()
	
	#pprint(globalDict)
	pass
Esempio n. 3
0
def open_map(cont):

	# Basic
	own = cont.owner
	scene = own.scene
	
	# Sensors
	s_mouse_over = cont.sensors['mouse_over']
	s_lmb = cont.sensors['lmb']
	
	# Properties
	p_maps_path = expandPath('//maps/')
	
	if s_mouse_over.positive and s_lmb.positive:
		
		with open(p_maps_path + own['description'], 'r') as open_file:
			loaded_map = litev(open_file.read())
			globalDict['map'] = loaded_map
			globalDict['state']['map_name'] = own['description'].replace('.dwmap', '')
			
		scene.replace('editor')
		
	pass
Esempio n. 4
0
def init_new_data():
	
	scene = bge.logic.getCurrentScene()
	
	if not 'database' in globalDict.keys():
		with open(expandPath('//map_editor.dat'), 'r') as open_file:
			globalDict['database'] = litev( open_file.read() )
			print('Loaded database from', open_file.name)
	
	# Global state
	globalDict['state'] = default_state.copy()
	print('State initialized from default')
	
	# Map
	globalDict['map'] = default_map.copy()
	print('Map initialized from empty default')
	
	# Tools and its specific settings
	globalDict[ 'tool_ground' ] = { 'current_tile' : 0,	'tile_depth' : 1, 'current_rotation' : 0 }
	globalDict[ 'tool_buildings' ] = { 'current_tile' : 0,	'tile_depth' : 2, 'floors' : 1, 'current_rotation' : 0 }
	globalDict[ 'tool_props' ] = { 'current_tile' : 0, 'tile_depth' : 3, 'current_rotation' : 0 }
	globalDict[ 'tool_spawns' ] = { 'current_tile' : 0, 'tile_depth' : 4, 'current_rotation' : 0}
	print('Tools settings initialized from default')
Esempio n. 5
0
from ast import literal_eval as litev

Config = {}
sizes_window = ((640, 480), (800, 600), (1024, 768), (1280, 720), (1366, 768),
                (1920, 1080))
sizes_fullscreen = ((0, 0), (640, 480), (800, 600), (1024, 768), (1280, 720),
                    (1366, 768), (1920, 1080))
stereo_modes = ("nostereo", "anaglyph", "sidebyside", "syncdoubling",
                "3dtvtopbottom", "interlace", "vinterlace", "hwpageflip")
bit_depths = (16, 24, 32)
refresh_rates = (30, 45, 60, 75, 90, 120)

try:
    with open('Settings.cfg', 'r') as openedfile:

        Config = litev(openedfile.read())
        pprint('Settings loaded from ' + openedfile.name)

except:
    pprint('Couldnt open Settings.cfg, using defaults')
    Config = {
        'videomode': 0,
        'stereomode': 0,
        'nomipmap': 0,
        'fixedtime': 0,
        'sizewindow': 2,
        'sizefullscreen': 0,
        'bitdepth': 2,
        'refreshrate': 2,
        'pathplayer': 'engine/blenderplayer.exe',
        'pathfile': 'data/main.blend'
def gen_city(cont):
    """ Generates a random city, using a pre made set of rules. """

    own = cont.owner
    scene = own.scene

    # Sensors
    sensor = cont.sensors[0].positive

    # Properties
    max_x = own['max_x'] * 100
    max_y = own['max_y'] * 100
    streets = ('DL', 'DLR', 'DR', 'LR', 'UD', 'UDL', 'UDLR', 'UDR', 'UL',
               'ULR', 'UR', 'N')

    #### INITIALIZE ####
    if sensor:

        setMipmapping(False)

        scene.active_camera.worldPosition = (max_x / 2, -max_y / 2, 200)

        if not globalDict['player_active']:
            character = scene.addObject('character')
            character.groupMembers['char_collision'].worldPosition = (max_x /
                                                                      2,
                                                                      -max_y /
                                                                      2, 1)

        ### Generates the given number of streets ###
        for x in range(0, max_x, 100):

            ### Add streets at Y axis of current X street ###
            for y in range(0, max_y, 100):

                cur_street = {'position': (x, -y, 0), 'type': 'UD'}

                if cur_street['position'][1] == 0:

                    if cur_street['position'][0] == 0:

                        cur_street['type'] = choice([
                            i for i in streets
                            if 'D' in i or 'R' in i and not 'N' in i
                        ])
                        own['last_added'] = str(cur_street)
                        cur_street['position'] = (x, 0, 0)
                        own['current_top'] = str(cur_street)

                    elif cur_street['position'][0] > 0:

                        cur_left = globalDict['map'][str((x - 100, 0, 0))]
                        possible_streets = []

                        if 'R' in cur_left['type']:
                            possible_streets = [i for i in streets if 'L' in i]

                        elif not 'R' in cur_left['type']:
                            possible_streets = [
                                i for i in streets if not 'L' in i
                            ]

                        cur_street['position'] = (x, 0, 0)
                        cur_street['type'] = choice(possible_streets)
                        own['current_top'] = str(cur_street)
                        own['current_left'] = str(cur_left)
                        own['last_added'] = str(cur_street)

                elif cur_street['position'][1] < 0:

                    if cur_street['position'][0] == 0:

                        possible_streets = []
                        last_added = litev(own['last_added'])

                        if 'D' in last_added['type']:
                            possible_streets = [
                                i for i in streets if 'U' in i and not 'N' in i
                            ]

                        if not 'D' in last_added['type']:
                            possible_streets = [
                                i for i in streets if not 'U' in i
                            ]

                        cur_street['type'] = choice(possible_streets)
                        own['last_added'] = str(cur_street)

                    if cur_street['position'][0] > 0:

                        cur_left = globalDict['map'][str((x - 100, -y, 0))]
                        possible_streets = []
                        last_added = litev(own['last_added'])

                        if 'D' in last_added[
                                'type'] and not 'R' in cur_left['type']:
                            possible_streets = [
                                i for i in streets
                                if 'U' in i and not 'L' in i and not 'N' in i
                            ]

                        if not 'D' in last_added['type'] and 'R' in cur_left[
                                'type']:
                            possible_streets = [
                                i for i in streets
                                if not 'U' in i and 'L' in i and not 'N' in i
                            ]

                        if 'D' in last_added['type'] and 'R' in cur_left[
                                'type']:
                            possible_streets = [
                                i for i in streets
                                if 'U' in i and 'L' in i and not 'N' in i
                            ]

                        if not 'D' in last_added[
                                'type'] and not 'R' in cur_left['type']:
                            possible_streets = [
                                i for i in streets
                                if not 'U' in i and not 'L' in i
                            ]

                        cur_street['type'] = choice(possible_streets)
                        own['last_added'] = str(cur_street)

                else:
                    cur_street['type'] = 'N'

                ### Add current street to globalDict and continue iteration
                globalDict['map'][str(cur_street['position'])] = cur_street

        ### Add streets from globalDict to scene ###
        for obj in globalDict['map'].values():

            added = scene.addObject(obj['type'] + '_group')

            for i in added.groupMembers:
                i.setParent(added, 0, 0)

            added.worldPosition = obj['position']

    pass