def json_load_file(owner): """选择json文件导入""" path = fefactory_api.choose_file("选择要读取的文件", file=getattr(owner, 'lastfile', None), wildcard='*.json') if path: owner.lastfile = path with open(path, encoding="utf-8") as file: return json.load(file)
def g3l2json(self, _=None): """g3l坐标转json""" path = fefactory_api.choose_file("选择要读取的文件", wildcard='*.g3l') if path: with open(path) as file: if not file.readline().strip() == '[Locks]': fefactory_api.alert('不支持的格式') return coord = [0, 0, 0] datas = [] while True: line = file.readline() if not line: break line = line.strip() if line.startswith('x='): coord[0] = float(line[2:]) elif line.startswith('y='): coord[1] = float(line[2:]) elif line.startswith('z='): coord[2] = float(line[2:]) elif line.startswith('desc='): datas.append({'name': line[5:], 'value': tuple(coord)}) jsonpath = path[:path.rfind('.') + 1] + 'json' with open(jsonpath, 'w', encoding="utf-8") as file: json.dump(datas, file, ensure_ascii=False) fefactory_api.alert('转换成功: ' + jsonpath)
def json_dump_file(owner, data, dumper=None): """选择json文件导出""" path = fefactory_api.choose_file("选择保存文件", file=getattr(owner, 'lastfile', None), wildcard='*.json') if path: owner.lastfile = path with open(path, 'w', encoding="utf-8") as file: if dumper is None: json.dump(data, file) else: dumper(data, file)
def onSaveAs(self, btn): tpl = self.longtext_dialog("输入模板", "{i:04X} {addr:08X}\n{text}\n") path = fefactory_api.choose_file("选择保存文件", wildcard='*.txt') if path: result = [] i = 0 for item in self.data_list: result.append(tpl.format(i=i, **item)) i += 1 with open(path, 'w', encoding="utf-8") as file: file.write('\n'.join(result)) print("保存成功: " + path)
def read_from_rom(self, menu): rom = fefactory_api.choose_file("选择火纹的Rom", wildcard='*.gba|*.gba') if not rom: return reader = FeRomRW(rom) if not reader.closed: print(reader.getRomTitle()) dialog = exui.ListDialog("选择执行导入的模块", listbox={'choices': self.module_names}) if dialog.showModal(): for i in dialog.listbox.getCheckedItems(): name = modules[i][1] try: Module = self.get_module(name) module = Module() module.attach() module.readFrom(reader) except Exception: print('加载模块%s失败' % name) traceback.print_exc()