12345678910111213141516171819202122232425262728293031323334 |
- import configparser
- import json
- def parse_ini_to_json(ini_path):
- config = configparser.ConfigParser()
- config.read(ini_path, encoding="utf-8")
- levels = []
- level_names = [section for section in config.sections() if section != "Metadata"]
- for h, level_name in enumerate(level_names):
- i = h + 1
- level_data = config[level_name]
- levels.append({
- "id": str(i),
- "index": h + 10,
- "name": level_name,
- "pre_narrative": level_data.get("pre_narrative", ""),
- "post_narrative": level_data.get("post_narrative", ""),
- "next_level": str(i + 1) if i + 1 < len(level_names) else None,
- "decisions": None,
- "ending": "win" if h == len(level_names) - 1 else None
- })
- return json.dumps([{
- "name": config["Metadata"]["name"],
- "id": config["Metadata"]["id"],
- "init_level": "1",
- "levels": levels}], indent=4)
- ini_path = "story.ini"
- json_output = parse_ini_to_json(ini_path)
- print(json_output)
|