zhenxun_plugin_farm/config.py

107 lines
3.0 KiB
Python
Raw Normal View History

2025-03-16 19:11:05 +08:00
import json
from pathlib import Path
from zhenxun.configs.path_config import DATA_PATH
2025-03-16 19:11:05 +08:00
from zhenxun.services.log import logger
g_sDBPath = DATA_PATH / "farm_db"
g_sDBFilePath = DATA_PATH / "farm_db/farm.db"
g_sResourcePath = Path(__file__).resolve().parent / "resource"
2025-03-16 19:11:05 +08:00
class CJsonManager:
def __init__(self):
self.m_pItem = {}
self.m_pPlant = {}
self.m_pLevel = {}
self.m_pSoil = {}
async def init(self) -> bool:
if not await self.initItem():
return False
if not await self.initPlant():
return False
2025-03-16 19:11:05 +08:00
if not await self.initLevel():
2025-03-16 19:11:05 +08:00
return False
if not await self.initSoil():
2025-03-16 19:11:05 +08:00
return False
return True
async def initItem(self) -> bool:
2025-03-16 19:11:05 +08:00
current_file_path = Path(__file__)
try:
with open(
current_file_path.resolve().parent / "config/item.json",
encoding="utf-8",
) as file:
self.m_pItem = json.load(file)
2025-03-16 19:11:05 +08:00
return True
except FileNotFoundError:
logger.warning("item.json 打开失败")
2025-03-16 19:11:05 +08:00
return False
except json.JSONDecodeError as e:
logger.warning(f"item.json JSON格式错误: {e}")
return False
async def initPlant(self) -> bool:
2025-03-16 19:11:05 +08:00
current_file_path = Path(__file__)
try:
with open(
current_file_path.resolve().parent / "config/plant.json",
encoding="utf-8",
) as file:
self.m_pPlant = json.load(file)
return True
except FileNotFoundError:
logger.warning("plant.json 打开失败")
return False
except json.JSONDecodeError as e:
logger.warning(f"plant.json JSON格式错误: {e}")
return False
async def initLevel(self) -> bool:
current_file_path = Path(__file__)
try:
with open(
current_file_path.resolve().parent / "config/level.json",
encoding="utf-8",
) as file:
self.m_pLevel = json.load(file)
return True
except FileNotFoundError:
logger.warning("level.json 打开失败")
return False
except json.JSONDecodeError as e:
logger.warning(f"level.json JSON格式错误: {e}")
return False
async def initSoil(self) -> bool:
current_file_path = Path(__file__)
try:
with open(
current_file_path.resolve().parent / "config/soil.json",
encoding="utf-8",
) as file:
self.m_pSoil = json.load(file)
2025-03-16 19:11:05 +08:00
return True
except FileNotFoundError:
logger.warning("soil.json 打开失败")
2025-03-16 19:11:05 +08:00
return False
except json.JSONDecodeError as e:
logger.warning(f"soil.json JSON格式错误: {e}")
return False
g_pJsonManager = CJsonManager()