def save_world(create=False): gfx.title('Saving...') logging.debug('Offloading world...') if create: maps.cache_all_clusters() _config_directory, _worlds_directory = profiles.has_reactor3() _version_file = os.path.join(_worlds_directory, WORLD_INFO['id'], 'version.txt') with open(_version_file, 'w') as version_file: version_file.write(VERSION) logging.debug('Saving items...') items.save_all_items() maps.save_map('map', base_dir=profiles.get_world_directory(WORLD_INFO['id'])) logging.debug('Saving life...') _life = life.save_all_life() with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'w') as e: e.write(_life) with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'w') as e: e.write(json.dumps(ITEMS)) #cache.commit_cache('items') #cache.save_cache('items') items.reload_all_items() logging.info('World saved.')
def load_world(world): global LOADED_CHUNKS gfx.title('Loading...') LOADED_CHUNKS = {} WORLD_INFO['id'] = world maps.load_map('map', base_dir=profiles.get_world_directory(world)) logging.debug('Loading life from disk...') with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'r') as e: LIFE.update(json.loads(e.readline())) logging.debug('Loading items from disk...') with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'r') as e: ITEMS.update(json.loads(e.readline())) logging.debug('Loading historic items...') #with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items_history.dat'), 'r') as e: # ITEMS_HISTORY.update(json.loads(''.join(e.readlines()))) maps.reset_lights() SETTINGS['controlling'] = None SETTINGS['following'] = None for life in LIFE.values(): if life['dead']: continue if 'player' in life: SETTINGS['controlling'] = life['id'] lfe.focus_on(life) break lfe.load_all_life() items.reload_all_items() if SETTINGS['chunk_handler']: SETTINGS['chunk_handler'].check_chunks(force=True) #logging.debug('Rendering map slices...') #maps.render_map_slices() logging.info('World loaded.')
def commit_cache(cache_name): _path = os.path.join(profiles.get_world_directory(WORLD_INFO['id']), '%s_history.dat' % cache_name) _write_cache = [] if not os.path.exists(_path): return False with open(_path, 'r') as f: _cache = f.readlines() for _line in _cache: line = _line.rstrip() if line == '\n' or not line: continue print repr(line) _historic_item = json.loads(line) _historic_item['_allow_dump'] = True _write_cache.append(json.dumps(_historic_item)) if cache_name == 'items': for item_uid in ITEMS_HISTORY: _historic_item = ITEMS_HISTORY[item_uid] if not _historic_item['_on_disk']: continue _historic_item['_allow_dump'] = True with open(_path, 'w') as f: f.write('\n'.join(_write_cache)) logging.debug('Cache: Committed.')
def save_cache(cache_name): _path = os.path.join(profiles.get_world_directory(WORLD_INFO['id']), '%s_history.dat' % cache_name) _write_cache = [] if not os.path.exists(_path): return False with open(_path, 'r') as f: _cache = f.readlines() for _line in _cache: line = _line.rstrip() if line == '\n' or not line: continue _historic_item = json.loads(line) if not _historic_item['_allow_dump']: continue _dump_string = json.dumps(_historic_item) _write_cache.append(_dump_string) with open(_path, 'w') as f: f.write('\n'.join(_write_cache)) logging.debug('Cache: Saved to disk.')
def load_world(world): global LOADED_CHUNKS gfx.title('Loading...') LOADED_CHUNKS = {} WORLD_INFO['id'] = world maps.load_map('map', base_dir=profiles.get_world_directory(world)) logging.debug('Loading life from disk...') with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'r') as e: LIFE.update(json.loads(e.readline())) logging.debug('Loading items from disk...') with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'r') as e: ITEMS.update(json.loads(e.readline())) logging.debug('Loading historic items...') #with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items_history.dat'), 'r') as e: # ITEMS_HISTORY.update(json.loads(''.join(e.readlines()))) maps.reset_lights() SETTINGS['controlling'] = None SETTINGS['following'] = None for life in LIFE.values(): if life['dead']: continue if 'player' in life: SETTINGS['controlling'] = life['id'] lfe.focus_on(life) break lfe.load_all_life() items.reload_all_items() if SETTINGS['chunk_handler']: SETTINGS['chunk_handler'].check_chunks(force=True) #logging.debug('Rendering map slices...') #maps.render_map_slices() logging.info('World loaded.')
def _read_from_cache(cache_name, uid): with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), '%s_history.dat' % cache_name)) as f: for item in [json.loads(s) for s in f]: if item['uid'] == uid: item['_on_disk'] = False item['date'] = WORLD_INFO['ticks'] ITEMS_HISTORY[item['uid']].update(item) logging.debug('Cache: Loaded item %s from cache.' % uid) return item
def save_world(create=False): gfx.title('Saving...') logging.debug('Offloading world...') if create: maps.cache_all_clusters() _config_directory, _worlds_directory = profiles.has_reactor3() _version_file = os.path.join(_worlds_directory, WORLD_INFO['id'], 'version.txt') with open(_version_file, 'w') as version_file: version_file.write(VERSION) logging.debug('Saving items...') items.save_all_items() maps.save_map('map', base_dir=profiles.get_world_directory(WORLD_INFO['id'])) logging.debug('Saving life...') _life = life.save_all_life() with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'w') as e: e.write(_life) with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'w') as e: e.write(json.dumps(ITEMS)) #cache.commit_cache('items') #cache.save_cache('items') items.reload_all_items() logging.info('World saved.')
def _write_to_cache(cache_name, data): with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), '%s_history.dat' % cache_name), 'a') as f: f.write(json.dumps(data)+'\n\n')