def emulation_info_state(uuid): state_info = {} state_ref = dbm.retrieve_save_state(uuid=uuid)[0] other_save_states = dbm.retrieve_save_state(game_uuid=state_ref['game_uuid']) state_extra_files = dbm.retrieve_file_path(save_state_uuid=uuid) state_info['record'] = state_ref state_info['availableStates'] = filter(lambda s: True if s.get('save_state_source_data') or s.get('rl_starts_data') else False, other_save_states) state_info['fileMapping'] = {k: os.path.join('/game_data', v, k.split('/')[-1]) for k, v in map(lambda x: (x['file_path'], x['source_data']), state_extra_files)} if state_extra_files else None state_info['fileInformation'] = {f['file_path']: f for f in state_extra_files} # Save data is singular, and probably LZMA compressed if state_ref['save_state_source_data']: state_info['stateFileURL'] = os.path.join('/cite_data', state_ref['save_state_source_data'], '{}'.format(state_ref['uuid'])) # Save data is multiple and probably run length encoded if state_ref['rl_starts_data'] and state_ref['rl_lengths_data']: state_info['rlStartsURL'] = os.path.join('/cite_data', state_ref['rl_starts_data'], '{}'.format(state_ref['uuid'])) state_info['rlLengthsURL'] = os.path.join('/cite_data', state_ref['rl_lengths_data'], '{}'.format(state_ref['uuid'])) if state_extra_files: main_exec = dbm.retrieve_file_path(save_state_uuid=uuid, main_executable=True)[0] state_info['gameFileURL'] = os.path.join('/game_data', main_exec['source_data'], main_exec['file_path'].split('/')[-1]) else: game_ref = dbm.retrieve_game_ref(state_ref['game_uuid']) state_info['gameFileURL'] = os.path.join('/game_data', game_ref['source_data'], game_ref['data_image_source']) return jsonify(state_info)
def citation_page(uuid): game_ref = dbm.retrieve_game_ref(uuid) perf_ref = dbm.retrieve_perf_ref(uuid) derived_performances = dbm.retrieve_derived_performances(uuid) previous_performances = dbm.retrieve_performance_chain(uuid)[:-1] save_states = dbm.retrieve_save_state(game_uuid=uuid) extra_files = dbm.retrieve_file_path(game_uuid=uuid) if game_ref: return render_template('citation.html', citeref=game_ref, is_game=True, is_performance=False, derived_performances=derived_performances, extra_files=extra_files, save_states=save_states) elif perf_ref: performance_video = "/cite_data/{}/{}".format(perf_ref['replay_source_file_ref'], perf_ref['replay_source_file_name']) return render_template('citation.html', citeref=perf_ref, is_game=False, is_performance=True, previous_performances=previous_performances, performance_video=performance_video) return "No record found, sorry."
def add_rl_save_state_data(uuid): rl_starts_b64 = request.form.get('rl_starts') rl_lengths_b64 = request.form.get('rl_lengths') rl_total_length = int(request.form.get('rl_total_length')) rl_lengths_length = int(request.form.get('rl_lengths_length')) rl_starts_length = int(request.form.get('rl_starts_length')) rl_starts_b_array = bytearray(base64.b64decode(rl_starts_b64)) rl_lengths_b_array = bytearray(base64.b64decode(rl_lengths_b64)) if len(rl_starts_b_array) != rl_starts_length: rl_starts_b_array.extend([0 for _ in range(len(rl_starts_b_array), rl_starts_length)]) if len(rl_lengths_b_array) != rl_lengths_length: rl_lengths_b_array.extend([0 for _ in range(len(rl_lengths_b_array), rl_lengths_length)]) rl_start_hash, file_name = save_byte_array_to_store(rl_starts_b_array, file_name=uuid) rl_lengths_hash, file_name = save_byte_array_to_store(rl_lengths_b_array, file_name=uuid) dbm.update_table(dbm.GAME_SAVE_TABLE, ['rl_starts_data','rl_lengths_data','rl_total_length','compressed'], [rl_start_hash, rl_lengths_hash, rl_total_length, True], ['uuid'], [uuid]) return jsonify({'record': dbm.retrieve_save_state(uuid=uuid)[0]})
def add_save_state(uuid): performance_uuid = request.form.get('performance_uuid') performance_time_index = request.form.get('performance_time_index') # Save State File to DB fields = OrderedDict( description=request.form.get('description'), game_uuid=uuid, save_state_type=u'state', emulator_name=request.form.get('emulator'), emulator_version=request.form.get('emulator_version'), emt_stack_pointer=request.form.get('emt_stack_pointer'), stack_pointer=request.form.get('stack_pointer'), time=request.form.get('time'), created_on=None, created=datetime.datetime.now() ) state_uuid = dbm.add_to_save_state_table(fts=True, **fields) # Attach performance information if present if performance_uuid: # Sometimes a state maybe linked to a performance without a specific index? # Since we are adding a new state, it is always a state save action if performance_time_index: dbm.link_save_state_to_performance(state_uuid, performance_uuid, performance_time_index, 'save') else: dbm.link_save_state_to_performance(state_uuid, performance_uuid, 0, 'save') # Retrieve save state information to get uuid and ignore blank fields save_state = dbm.retrieve_save_state(uuid=state_uuid)[0] return jsonify({'record': save_state})
def play_page(uuid): init_state = request.values.get('init_state') cite_ref = dbm.retrieve_game_ref(uuid) state_dicts = dbm.retrieve_save_state(game_uuid=uuid) if init_state: init_state = dbm.retrieve_save_state(uuid=init_state)[0] has_exec = dbm.retrieve_file_path(save_state_uuid=init_state['uuid'], main_executable=True) else: has_exec = dbm.retrieve_file_path(game_uuid=uuid, save_state_uuid=None, main_executable=True) init_state = {'uuid': 'NO_ID'} if cite_ref and (cite_ref['data_image_source'] or has_exec): return render_template('play.html', init_state=init_state, cite_ref=cite_ref) return "No game data source found, sorry!"
def add_screen_data(uuid): screen_data = bytes(base64.b64decode(request.form.get('buffer'))) width = int(request.form.get('width')) height = int(request.form.get('height')) image = Image.frombytes("RGBA", (width, height), screen_data) # Doing the saving here for now, might move later? base_path = os.path.join(LOCAL_CITATION_DATA_STORE, uuid) if not os.path.exists(base_path): os.makedirs(base_path) image.save(os.path.join(base_path, 'screen_{}.png'.format(uuid)), "PNG") dbm.add_screen_to_state(uuid) # one screen per state, so only change 'has_screen' parameter state = dbm.retrieve_save_state(uuid=uuid)[0] return jsonify({'record': state})
def update_save_state(uuid): update_fields = json.loads(request.form.get('update_fields')) # if linking to a performance, do that and then throw away since GAME_SAVE_TABLE doesn't refer to performance if 'performance_uuid' in update_fields: performance_uuid = update_fields['performance_uuid'] performance_time_index = update_fields['performance_time_index'] action = update_fields['action'] dbm.link_save_state_to_performance(uuid, performance_uuid, performance_time_index, action) del update_fields['performance_uuid'] del update_fields['performance_time_index'] del update_fields['action'] # make sure that there are still fields to update if len(update_fields.keys()) > 0: dbm.update_table(dbm.GAME_SAVE_TABLE, update_fields.keys(),update_fields.values(), ['uuid'], [uuid]) return jsonify(dbm.retrieve_save_state(uuid=uuid)[0])
def add_save_state_data(uuid): save_state_data = request.form.get('buffer') compressed = True if request.form.get('compressed') == u'true' else False data_length = int(request.form.get('data_length')) save_state_b_array = bytearray(base64.b64decode(save_state_data)) # The base64 library will interpret trailing zeros as padding and remove them, this adds them back in if len(save_state_b_array) != data_length: save_state_b_array.extend([0 for _ in range(len(save_state_b_array), data_length)]) source_data_hash, file_name = save_byte_array_to_store(save_state_b_array, file_name=uuid) dbm.update_table(dbm.GAME_SAVE_TABLE, ['save_state_source_data', 'compressed'], [source_data_hash, compressed], ['uuid'], [uuid]) return jsonify({'record': dbm.retrieve_save_state(uuid=uuid)[0]})
def add_extra_file(uuid): extra_file_b64 = request.form.get('extra_file_data') sha1_hash = request.form.get('sha1_hash') data_length = int(request.form.get('data_length')) file_name = request.form.get('file_name') rel_file_path = request.form.get('rel_file_path') is_executable = request.form.get('is_executable') main_executable = request.form.get('main_executable') decoded_b64 = base64.b64decode(extra_file_b64) extra_b_array = bytearray(decoded_b64) main_executable = True if main_executable == 'true' else False is_executable = True if is_executable == 'true' else False if len(extra_b_array) != data_length: extra_b_array.extend([0 for _ in range(len(extra_b_array), data_length)]) hash_check = get_byte_array_hash(extra_b_array) if hash_check != sha1_hash: print "{} hash: {} not equal to client hash: {}".format(file_name, hash_check, sha1_hash) # If file with current hash and path already exists, add a new record for save state but # do not create a duplicate file file_id = dbm.check_for_existing_file(rel_file_path, hash_check) if file_id: dbm.link_existing_file_to_save_state(uuid, file_id) print "File {}:{} found.".format(file_name, rel_file_path) else: print "File {}:{} created.".format(file_name, rel_file_path) source_data_hash, file_name = save_byte_array_to_store(extra_b_array, file_name=file_name, store_path=LOCAL_GAME_DATA_STORE) fields = OrderedDict( game_uuid=None, save_state_uuid=uuid, is_executable=is_executable, main_executable=main_executable, file_path=rel_file_path, source_data=source_data_hash ) state_ref = dbm.retrieve_save_state(uuid=uuid)[0] fields['game_uuid'] = state_ref['game_uuid'] dbm.add_to_file_path_table(**fields) file_path = dbm.retrieve_file_path(save_state_uuid=uuid, file_path=rel_file_path)[0] return jsonify(file_path)
def compare_page(): uuids = request.values.get('uuids', "").split(',') game_uuids = [] state_uuids = [] for id in uuids: if dbm.retrieve_attr_from_db('uuid', id, dbm.GAME_CITATION_TABLE): game_uuids.append(id) state_uuids.append('NO_ID') elif dbm.retrieve_attr_from_db('uuid', id, dbm.GAME_SAVE_TABLE): state = dbm.retrieve_save_state(uuid=id)[0] state_uuids.append(id) game_uuids.append(state['game_uuid']) return render_template('compare.html', game_uuids=",".join(game_uuids), state_uuids=",".join(state_uuids) )
def get_cite_for_uuid(result): ref_type = result['tags'] uuid = result['uuid'] if ref_type == GAME_CITE_REF: table = dbm.GAME_CITATION_TABLE if ref_type == PERF_CITE_REF: table = dbm.PERFORMANCE_CITATION_TABLE if ref_type == STATE_CITE_REF: table = dbm.GAME_SAVE_TABLE if ref_type in (GAME_CITE_REF, PERF_CITE_REF): db_values = dbm.retrieve_attr_from_db('uuid', uuid, table, limit=1)[0] citation = dbm.create_cite_ref_from_db(ref_type, db_values) else: citation = dbm.retrieve_save_state(uuid=uuid)[0] citation['ref_type'] = STATE_CITE_REF return citation
def emulation_info_game(uuid): game_info = {} game_ref = dbm.retrieve_game_ref(uuid) game_extra_files = dbm.retrieve_file_path(game_uuid=uuid, save_state_uuid=None) if game_ref and (game_ref['data_image_source'] or game_extra_files): gis = game_ref['data_image_source'] gsd = game_ref['source_data'] main_exec = dbm.retrieve_file_path(game_uuid=uuid, save_state_uuid=None, main_executable=True) main_fp = main_exec[0]['file_path'].split('/')[-1] if main_exec else None game_info['gameFileURL'] = os.path.join('/game_data', gsd, gis) if gis else main_fp if game_extra_files: game_info['fileMapping'] = {k: os.path.join('/game_data', v, k.split('/')[-1]) for k, v in map(lambda x: (x['file_path'], x['source_data']), game_extra_files)} game_info['fileInformation'] = {f['file_path']: f for f in game_extra_files} game_info['stateFileURL'] = None game_info['record'] = game_ref.elements game_info['availableStates'] = filter(lambda s: True if s.get('save_state_source_data') or s.get('rl_starts_data') else False, dbm.retrieve_save_state(game_uuid=uuid)) game_info['availablePerformances'] = [dict(p.get_element_items()) for p in dbm.retrieve_derived_performances(uuid)] return jsonify(game_info)
def delete(ctx, uuid, keep_performances): no_prompts = ctx.obj['NO_PROMPTS'] verbose = ctx.obj['VERBOSE'] def check_delete(path): try: shutil.rmtree(path) except OSError as e: print e.message def delete_from_fts(uuid): ix = open_dir(LOCAL_FTS_INDEX) writer = ix.writer() writer.delete_by_term('id', uuid) writer.commit() game_ref = dbm.retrieve_game_ref(uuid) perf_ref = dbm.retrieve_perf_ref(uuid) state_ref = dbm.retrieve_save_state(uuid=uuid) if game_ref: perfs = dbm.retrieve_derived_performances(uuid) states = dbm.retrieve_save_state(game_uuid=uuid) files = dbm.retrieve_file_path(game_uuid=uuid) for f in files: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_GAME_DATA_STORE, f['source_data'])) dbm.delete_from_table(dbm.GAME_FILE_PATH_TABLE, ['game_uuid'], [uuid]) for s in states: if s['save_state_source_data']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, s['save_state_source_data'])) else: if s['rl_starts_data']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, s['rl_starts_data'])) check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, s['rl_lengths_data'])) if s['has_screen']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, s['uuid'])) dbm.delete_from_table(dbm.GAME_SAVE_TABLE, ['game_uuid'], [uuid]) for p in perfs: if p['replay_source_file_ref']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, p['replay_source_file_ref'])) dbm.delete_from_table(dbm.SAVE_STATE_PERFORMANCE_LINK_TABLE,['performance_uuid'], [p['uuid']]) dbm.delete_from_table(dbm.PERFORMANCE_CITATION_TABLE, ['game_uuid'], [uuid]) if game_ref['source_data']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_GAME_DATA_STORE, game_ref['source_data'])) dbm.delete_from_table(dbm.GAME_CITATION_TABLE, ['uuid'], [uuid]) delete_from_fts(uuid) elif perf_ref: dbm.delete_from_table(dbm.SAVE_STATE_PERFORMANCE_LINK_TABLE,['performance_uuid'], [uuid]) if perf_ref['replay_source_file_ref']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, perf_ref['replay_source_file_ref'])) dbm.delete_from_table(dbm.PERFORMANCE_CITATION_TABLE, ['uuid'], [uuid]) delete_from_fts(uuid) elif state_ref: state_ref = state_ref[0] # retrieve states returns lists files = dbm.retrieve_file_path(save_state_uuid=uuid) for f in files: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_GAME_DATA_STORE, f['source_data'])) dbm.delete_from_table(dbm.SAVE_STATE_PERFORMANCE_LINK_TABLE,['save_state_uuid'], [uuid]) if state_ref['has_screen']: check_delete(os.path.join(LOCAL_DATA_ROOT, LOCAL_CITATION_DATA_STORE, state_ref['uuid'])) dbm.delete_from_table(dbm.GAME_SAVE_TABLE, ['uuid'], [uuid]) delete_from_fts(uuid) else: click.echo('UUID {} not found.'.format(uuid)) sys.exit(1)