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 prompt_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_gpt_config(self,classname,id): with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f: data = json.load(f) gpts=data["GPT"] for gpt in gpts: if gpt["id"]==id and gpt["class"]==classname: return gpt["input"],gpt["prompt_input"],gpt["prompt"],gpt["output"],gpt["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["value"],judgement def judgement_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_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