def buildDMI(directory, output): dmi = DMI(output) logging.info('Creating {0}...'.format(output)) for root, _, files in os.walk(directory): for filename in files: if filename.endswith('.dmi') and not filename.endswith('.new.dmi'): filepath = os.path.join(root, filename) logging.info('Adding {0}...'.format(filename, output)) subdmi = DMI(filepath) subdmi.loadAll() if subdmi.icon_height != 32 or subdmi.icon_width != 32: logging.warn( 'Skipping {0} - Invalid icon size.'.format(filepath)) changes = 0 for state_name in subdmi.states: if state_name in dmi.states: logging.warn( 'Skipping state {0}:{1} - State exists.'.format( filepath, subdmi.states[state_name].displayName())) continue dmi.states[state_name] = subdmi.states[state_name] changes += 1 logging.info('Added {0} states.'.format(changes)) # save logging.info('Saving {0} states to {1}...'.format(len(dmi.states), output)) dmi.save(output)
def from_dmi(cls, path: Union[BytesIO, str, Path]) -> "Rsi": try: from byond.DMI import DMI except ImportError: raise ImportError("Unable to import byondtoolsv3.") # We'll set the copyright to the url if applicable url_copyright = None if isinstance(path, Path): path = str(path) elif isinstance(path, BytesIO): pass else: # Try URL / filepath try: with urlopen(path) as response: buffer = BytesIO() buffer.write(response.read()) url_copyright = path path = buffer except ValueError: path = Path(path) # type: ignore if not path.exists(): raise FileNotFoundError path = str(path) # N3X15, if you are reading this: # You are awful at API design. dmi = DMI(path) dmi.loadAll() rsi = Rsi((dmi.icon_width, dmi.icon_height)) if url_copyright is not None: rsi.copyright = url_copyright for dmstate in dmi.states.values(): rsstate = rsi.new_state(dmstate.dirs, dmstate.name) # type: State # BYOND does not permit direction specific delays so this is easy. for x in range(rsstate.directions): direction = Direction(x) rsstate.delays[x] = [] for y in range(dmstate.frames): # Circumvent around a BYOND bug (?) # where states have more delays than actual frames. if dmstate.frames <= y: break if dmstate.frames != 1: # BYOND delays are in deciseconds, not seconds! delay = float(dmstate.delay[y]) / 10 else: delay = 1.0 rsstate.delays[x].append(delay) rsstate.icons[x].append( dmstate.getFrame(direction.to_byond(), y)) return rsi
def makeDMI(): dmi = DMI('state_limit.dmi') # for i in range(513): # Make a new tile img = Image.new('RGBA', (32, 32)) # Set up PIL's drawing stuff draw = ImageDraw.Draw(img) # Define a font. font = ImageFont.truetype('arial.ttf', 10) # Draw the tile number draw.text((10, 0), str(i + 1), (0, 0, 0), font=font) # Make state state_name = 'state {0}'.format(i + 1) state = State(state_name) state.dirs = 1 state.frames = 1 state.icons = [img] # Add state to DMI dmi.states[state_name] = state #save dmi.save('state_limit.dmi', sort=False)
def from_dmi(cls, path: Union[str, Path]) -> "Rsi": try: from byond.DMI import DMI except ImportError: raise ImportError("Unable to import byondtoolsv3.") if isinstance(path, Path): path = str(path) # N3X15, if you are reading this: # You are awful at API design. dmi = DMI(path) dmi.loadAll() rsi = Rsi((dmi.icon_width, dmi.icon_height)) for dmstate in dmi.states.values(): rsstate = rsi.new_state(dmstate.dirs, dmstate.name) # type: State # BYOND does not permit direction specific delays so this is easy. for x in range(rsstate.directions): direction = Direction(x) rsstate.delays[x] = [] for y in range(dmstate.frames): # Circumvent around a BYOND bug (?) # where states have more delays than actual frames. if dmstate.frames <= y: break if dmstate.frames != 1: # BYOND delays are in deciseconds, not seconds! delay = float(dmstate.delay[y]) / 10 else: delay = 1.0 rsstate.delays[x].append(delay) rsstate.icons[x].append( dmstate.getFrame(direction.to_byond(), y)) return rsi
def makeDMI(): dmi = DMI('snowfx.dmi') # LAYER 1 state_name = 'snowlayer1' state = State(state_name) state.dirs = 1 state.frames = 31 state.icons = animate_layer(Image.open('snow1.png'), layer1_op, 31) # Add state to DMI dmi.states[state_name] = state # LAYER 2 state_name = 'snowlayer2' state = State(state_name) state.dirs = 1 state.frames = 15 state.icons = animate_layer(Image.open('snow2.png'), layer2_op, 15) # Add state to DMI dmi.states[state_name] = state #save dmi.save('snowfx.dmi', sort=False)
def renderAtom(self, atom, basedir, skip_alpha=False): if 'icon' not in atom.properties: logging.critical('UNKNOWN ICON IN ATOM #{0} ({1})'.format( atom.ID, atom.path)) logging.info(atom.MapSerialize()) logging.info(atom.MapSerialize(Atom.FLAG_INHERITED_PROPERTIES)) return None # else: # logging.info('Icon found for #{}.'.format(atom.ID)) dmi_file = atom.properties['icon'].value if dmi_file is None: return None # Grab default icon_state ('') if we can't find the one defined. state = atom.getProperty('icon_state', '') direction = SOUTH if 'dir' in atom.properties: try: direction = int(atom.properties['dir'].value) except ValueError: logging.critical('FAILED TO READ dir = ' + repr(atom.properties['dir'].value)) return None icon_key = '{0}|{1}|{2}'.format(dmi_file, state, direction) frame = None pixel_x = 0 pixel_y = 0 if icon_key in _icons: frame, pixel_x, pixel_y = _icons[icon_key] else: dmi_path = os.path.join(basedir, dmi_file) dmi = None if dmi_path in _dmis: dmi = _dmis[dmi_path] else: try: dmi = DMI(dmi_path) dmi.loadAll() _dmis[dmi_path] = dmi except Exception as e: print(str(e)) for prop in ['icon', 'icon_state', 'dir']: print('\t{0}'.format(atom.dumpPropInfo(prop))) pass if dmi.img is None: logging.warning('Unable to open {0}!'.format(dmi_path)) return None if dmi.img.mode not in ('RGBA', 'P'): logging.warn('{} is mode {}!'.format(dmi_file, dmi.img.mode)) if direction not in IMAGE_INDICES: logging.warn('Unrecognized direction {} on atom {}!'.format( direction, str(atom))) direction = SOUTH # DreamMaker property editor shows dir = 2. WTF? frame = dmi.getFrame(state, direction, 0) if frame == None: # Get the error/default state. frame = dmi.getFrame("", direction, 0) if frame == None: return None if frame.mode != 'RGBA': frame = frame.convert("RGBA") pixel_x = 0 if 'pixel_x' in atom.properties: pixel_x = int(atom.properties['pixel_x'].value) pixel_y = 0 if 'pixel_y' in atom.properties: pixel_y = int(atom.properties['pixel_y'].value) _icons[icon_key] = (frame, pixel_x, pixel_y) # Handle BYOND alpha and coloring c_frame = frame alpha = int(atom.getProperty('alpha', 255)) if skip_alpha: alpha = 255 color = atom.getProperty('color', '#FFFFFF') if alpha != 255 or color != '#FFFFFF': c_frame = tint_image(frame, BYOND2RGBA(color, alpha)) return c_frame
def RenderToMapTile(self, passnum, basedir, renderflags, **kwargs): img = Image.new('RGBA', (96, 96)) self.offset = (32, 32) foundAPixelOffset = False render_types = kwargs.get('render_types', ()) skip_alpha = kwargs.get('skip_alpha', False) # for atom in sorted(self.GetAtoms(), reverse=True): for atom in self.SortAtoms(): if len(render_types) > 0: found = False for path in render_types: if atom.path.startswith(path): found = True if not found: continue aid = atom.ID # Ignore /areas. They look like ass. if atom.path.startswith('/area'): if not (renderflags & MapRenderFlags.RENDER_AREAS): continue # We're going to turn space black for smaller images. if atom.path == '/turf/space': if not (renderflags & MapRenderFlags.RENDER_STARS): continue if 'icon' not in atom.properties: logging.critical('UNKNOWN ICON IN {0} (atom #{1})'.format( self.origID, aid)) logging.info(atom.MapSerialize()) logging.info(atom.MapSerialize(Atom.FLAG_INHERITED_PROPERTIES)) continue dmi_file = atom.properties['icon'].value if 'icon_state' not in atom.properties: # Grab default icon_state ('') if we can't find the one defined. atom.properties['icon_state'] = BYONDString("") state = atom.properties['icon_state'].value direction = SOUTH if 'dir' in atom.properties: try: direction = int(atom.properties['dir'].value) except ValueError: logging.critical('FAILED TO READ dir = ' + repr(atom.properties['dir'].value)) continue icon_key = '{0}|{1}|{2}'.format(dmi_file, state, direction) frame = None pixel_x = 0 pixel_y = 0 if icon_key in _icons: frame, pixel_x, pixel_y = _icons[icon_key] else: dmi_path = os.path.join(basedir, dmi_file) dmi = None if dmi_path in _dmis: dmi = _dmis[dmi_path] else: try: dmi = DMI(dmi_path) dmi.loadAll() _dmis[dmi_path] = dmi except Exception as e: print(str(e)) for prop in ['icon', 'icon_state', 'dir']: print('\t{0}'.format(atom.dumpPropInfo(prop))) pass if dmi.img is None: logging.warning('Unable to open {0}!'.format(dmi_path)) continue if dmi.img.mode not in ('RGBA', 'P'): logging.warn('{} is mode {}!'.format( dmi_file, dmi.img.mode)) if direction not in IMAGE_INDICES: logging.warn( 'Unrecognized direction {} on atom {} in tile {}!'. format(direction, atom.MapSerialize(), self.origID)) direction = SOUTH # DreamMaker property editor shows dir = 2. WTF? frame = dmi.getFrame(state, direction, 0) if frame == None: # Get the error/default state. frame = dmi.getFrame("", direction, 0) if frame == None: continue if frame.mode != 'RGBA': frame = frame.convert("RGBA") pixel_x = 0 if 'pixel_x' in atom.properties: pixel_x = int(atom.properties['pixel_x'].value) pixel_y = 0 if 'pixel_y' in atom.properties: pixel_y = int(atom.properties['pixel_y'].value) _icons[icon_key] = (frame, pixel_x, pixel_y) # Handle BYOND alpha and coloring c_frame = frame alpha = int(atom.getProperty('alpha', 255)) if skip_alpha: alpha = 255 color = atom.getProperty('color', '#FFFFFF') if alpha != 255 or color != '#FFFFFF': c_frame = tint_image(frame, BYOND2RGBA(color, alpha)) img.paste(c_frame, (32 + pixel_x, 32 - pixel_y), c_frame) # Add to the top of the stack. if pixel_x != 0 or pixel_y != 0: if passnum == 0: return # Wait for next pass foundAPixelOffset = True if passnum == 1 and not foundAPixelOffset: return None if not self.areaSelected: # Fade out unselected tiles. bands = list(img.split()) # Excluding alpha band for i in range(3): bands[i] = bands[i].point(lambda x: x * 0.4) img = Image.merge(img.mode, bands) return img
opt = argparse.ArgumentParser() opt.add_argument( 'dir', help= 'The directory to scan for *.dmi files with an excess number of icon states.' ) args = opt.parse_args() if (not path.isdir(args.dir)): print('Not a directory') sys.exit(1) failed = False logging.getLogger().setLevel( logging.ERROR) # we don't care that byond is bad at pngs # This section parses all *.dmi files in the given directory, recursively. for root, subdirs, files in walk(args.dir): for filename in files: if not filename.endswith('.dmi'): continue file_path = path.join(root, filename) dmi = DMI(file_path) dmi.loadMetadata() number_of_icon_states = len(dmi.states) if number_of_icon_states > 512: failed = True print("{0} had too many icon states. {1}/512".format( file_path, number_of_icon_states)) if failed: sys.exit(1)
'floor', 'red', 'blue', 'green', 'yellow', 'neutral', 'orange', 'purple', 'brown', 'vault' ] } } knownQuads = [{}, {}, {}, {}] def isKnownQuad(i, im, icon_state): for name, kq in knownQuads[i].items(): if equal(kq, im): return True return False floors = DMI(sys.argv[1]) floors.loadAll() basedir = 'floor_quads' if os.path.isdir('floor_quads'): shutil.rmtree(basedir) os.makedirs('floor_quads') for icon_state in extract: if icon_state not in floors.states: print('Can\'t find state {0}!'.format(icon_state)) continue for d in range(floors.states[icon_state].dirs): dirf = 0 dirn = 'SOUTH' if floors.states[icon_state].dirs > 1: dirf = directions.IMAGE_INDICES[d] dirn = directions.getNameFromDir(dirf)