import json import os def singleton(cls): instances = {} def wrapper(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return wrapper @singleton class Configuration: def __init__(self, game_name): self.game_name = game_name def save_2_json(self,file_path,output_type,data): if output_type=="{class_output_data}": file_path='config/'+self.game_name+'/module_output/'+file_path os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4) elif output_type=="{class_output_data_append}": file_path='config/'+self.game_name+'/module_output/'+file_path os.makedirs(os.path.dirname(file_path), exist_ok=True) if os.path.exists(file_path): with open(file_path, 'r', encoding='utf-8') as f: existing_data = json.load(f) else: existing_data = {"class_output_data":[]} existing_data["class_output_data"].append(data) with open(file_path, 'w', encoding='utf-8') as f: json.dump(existing_data, f, ensure_ascii=False, indent=4) else: file_path='config/'+self.game_name+'/module_output/'+file_path os.makedirs(os.path.dirname(file_path), exist_ok=True) if os.path.exists(file_path): with open(file_path, 'r', encoding='utf-8') as f: existing_data = json.load(f) else: existing_data = {"class_output_data":[]} existing_data["class_output_data"].append(data) with open(file_path, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4) def input_process(self,input,prompt_input): for input_key, input_value in input.items(): with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f: globals()[input_key] = json.load(f) prompt_input_values = {} for prompt_input_key, prompt_input_value in prompt_input.items(): prompt_input_values[prompt_input_key] = eval(prompt_input_value.format(**locals())) return prompt_input_values def read_prompt(self,prompt_path): with open('config/'+self.game_name+'/'+prompt_path,encoding='utf-8') as f: prompt = json.load(f) return prompt def read_chat_config(self,classname,id): with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f: data = json.load(f) chats=data["Chat"] for chat in chats: if chat["id"]==id and chat["class"]==classname: return chat["input"],chat["prompt_input"],chat["prompt"],chat["output"],chat["next_module"] def read_judgement_config(self,classname,id): with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f: data = json.load(f) judgements=data["Judgement"] for judgement in judgements: if judgement["id"]==id and judgement["class"]==classname: return judgement["input"],judgement["judgement_input"],judgement["value"],judgement def read_interact_config(self,classname,id): with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f: data = json.load(f) interacts=data["Interact"] for interact in interacts: if interact["id"]==id and interact["class"]==classname: return interact["input"],interact["choices"],interact["output"],interact["next_module"] def interact_input_process(self,input,value): for input_key, input_value in input.items(): with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f: globals()[input_key] = json.load(f) input_value = eval(value.format(**locals())) return input_value def read_json(self,path): with open('config/'+path,encoding='utf-8') as f: data = json.load(f) return data