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 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 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 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
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
'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) print('{0} {1} {2}'.format(icon_state, dirn, floors.states[icon_state].dirs))
def compare(theirsfile, minefile, parser, reportstream, **kwargs): # print('\tD %s -> %s' % (theirsfile, minefile)) theirs = [] theirsDMI = None mine = [] mineDMI = None states = [] new2mineFilename = minefile.replace('.dmi', '.new.dmi') new2theirsFilename = theirsfile.replace('.dmi', '.new.dmi') new2mine=None if os.path.isfile(new2mineFilename): os.remove(new2mineFilename) if kwargs.get('newfile_mine',True): new2mine = DMI(new2mineFilename) new2theirs=None if os.path.isfile(new2theirsFilename): os.remove(new2theirsFilename) if kwargs.get('newfile_theirs',False): new2theirs = DMI(new2theirsFilename) o = '' if(os.path.isfile(theirsfile)): try: theirsDMI = DMI(theirsfile) theirsDMI.loadAll() theirs = theirsDMI.states except SystemError as e: print("!!! Received SystemError in %s, halting: %s" % (theirs.filename, traceback.format_exc(e))) print('# of cells: %d' % len(theirs.states)) print('Image h/w: %s' % repr(theirs.size)) sys.exit(1) except Exception as e: print("Received error, continuing: %s" % traceback.format_exc()) o += "\n {0}: Received error, continuing: {1}".format(theirsfile, traceback.format_exc()) for stateName in theirs: if stateName not in states: states.append(stateName) if(os.path.isfile(minefile)): try: mineDMI = DMI(minefile) mineDMI.loadAll() mine = mineDMI.states except SystemError as e: print("!!! Received SystemError in %s, halting: %s" % (mine.filename, traceback.format_exc(e))) print('# of cells: %d' % len(mine.states)) print('Image h/w: %s' % repr(mine.size)) sys.exit(1) except Exception as e: print("Received error, continuing: %s" % traceback.format_exc()) o += "\n {0}: Received error, continuing: {1}".format(minefile, traceback.format_exc()) for stateName in mine: if stateName not in states: states.append(stateName) for state in sorted(states): inTheirs = state in theirs inMine = state in mine if inTheirs and not inMine: o += '\n + {1}'.format(minefile, state) if new2mine is not None: new2mine.states[state] = theirsDMI.states[state] elif not inTheirs and inMine: o += '\n - {1}'.format(theirsfile, state) if new2theirs is not None: new2theirs.states[state] = mineDMI.states[state] elif inTheirs and inMine: if theirs[state].ToString() != mine[state].ToString(): o += '\n - {0}: {1}'.format(mine[state].displayName(), mine[state].ToString()) o += '\n + {0}: {1}'.format(theirs[state].displayName(), theirs[state].ToString()) elif kwargs.get('check_changed',True): diff_count=0 for i in xrange(len(theirs[state].icons)): theirF = theirs[state].icons[i] myF = theirs[state].icons[i] theirData = list(theirF.getdata()) myData = list(myF.getdata()) #diff = [] for i in xrange(len(theirData)): dr = theirData[i][0] - myData[i][0] dg = theirData[i][1] - myData[i][1] db = theirData[i][2] - myData[i][2] #diff[idx] = (abs(dr), abs(dg), abs(db)) if((dr != 0) or (dg != 0) or (db != 0)): diff_count += 1 break if diff_count > 0: o += '\n ! {0}: {1} frames differ'.format(theirs[state].displayName(), diff_count) if new2mine is not None: new2mine.states[state] = theirsDMI.states[state] if new2theirs is not None: new2theirs.states[state] = mineDMI.states[state] if o != '': reportstream.write('\n--- {0}'.format(theirsfile)) reportstream.write('\n+++ {0}'.format(minefile)) reportstream.write(o) if new2mine is not None: if len(new2mine.states) > 0: new2mine.save(new2mineFilename) else: if os.path.isfile(new2mineFilename): os.remove(new2mineFilename) #print('RM {0}'.format(new2mineFilename)) if new2theirs is not None: if len(new2theirs.states) > 0: new2theirs.save(new2theirsFilename) else: if os.path.isfile(new2theirsFilename): os.remove(new2theirsFilename)
'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) print('{0} {1} {2}'.format(icon_state, dirn,