全站通知:
模块:Dialogue/data/accept gift/doc
刷
历
编
跳到导航
跳到搜索
当前页面的数据通过以下 Python 代码生成,后续如果游戏版本更新,可重新执行脚本,导出最新的数据。
import os
import json
from pathlib import Path
def process_stardew_dialogue():
dialogue_dir = Path(r"\Content (unpacked)\Characters\Dialogue")
if not dialogue_dir.exists():
print(f"错误:目录不存在 {dialogue_dir}")
return
all_accept_gifts = {}
for file_path in dialogue_dir.glob("*.zh-CN.json"):
character_name = file_path.stem.replace(".zh-CN", "")
try:
with open(file_path, 'r', encoding='utf-8') as f:
dialogue_data = json.load(f)
accept_gift_data = {}
for key, value in dialogue_data.items():
if key.startswith("AcceptGift_"):
accept_gift_data[key] = value
if accept_gift_data:
all_accept_gifts[character_name] = accept_gift_data
print(f"角色 {character_name}: 找到 {len(accept_gift_data)} 条 AcceptGift 对话")
else:
print(f"角色 {character_name}: 未找到 AcceptGift 对话")
except Exception as e:
print(f"处理文件 {file_path} 时出错: {e}")
return all_accept_gifts
def generate_lua_table_with_order(data, output_file="Dialogue_Data.lua"):
def dict_to_lua_with_order(obj, indent=0):
spaces = " " * indent
if isinstance(obj, dict):
if not obj:
return "{}"
lines = ["{"]
order_index = 1
for key, value in obj.items():
if isinstance(value, dict) and any(k.startswith("AcceptGift_") for k in value.keys()):
key_str = key if key.isidentifier() else f'["{key}"]'
dialogue_lines = ["{"]
dialogue_order = 1
for dialogue_key, dialogue_value in value.items():
dialogue_lines.append(f"{spaces} [{dialogue_order}] = {{")
dialogue_lines.append(f"{spaces} key = \"{dialogue_key}\",")
escaped_value = dialogue_value.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n")
dialogue_lines.append(f"{spaces} value = \"{escaped_value}\"")
dialogue_lines.append(f"{spaces} }},")
dialogue_order += 1
dialogue_lines.append(f"{spaces} }}")
value_str = "\n".join(dialogue_lines)
lines.append(f"{spaces} {key_str} = {value_str},")
else:
key_str = key if key.isidentifier() else f'["{key}"]'
value_str = dict_to_lua_with_order(value, indent + 1)
lines.append(f"{spaces} {key_str} = {value_str},")
order_index += 1
lines.append(f"{spaces}}}")
return "\n".join(lines)
elif isinstance(obj, str):
escaped = obj.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n")
return f'"{escaped}"'
elif isinstance(obj, (int, float)):
return str(obj)
elif isinstance(obj, bool):
return "true" if obj else "false"
elif obj is None:
return "nil"
else:
return f'"{str(obj)}"'
try:
lua_content = f"""-- Stardew Valley AcceptGift 对话数据
-- 生成时间: {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
-- 总角色数: {len(data)}
-- 数据结构: [角色名][序号] = {{key = "对话键", value = "对话内容"}}
local dialogue_data = {dict_to_lua_with_order(data)}
return dialogue_data
"""
with open(output_file, 'w', encoding='utf-8') as f:
f.write(lua_content)
print(f"成功生成 Lua 文件: {Path(output_file).absolute()}")
file_size = Path(output_file).stat().st_size
print(f"文件大小: {file_size:,} 字节 ({file_size/1024:.1f} KB)")
return True
except Exception as e:
print(f"生成 Lua 文件时出错: {e}")
return False
def main():
print("开始处理 Stardew Valley 对话数据...")
result = process_stardew_dialogue()
if result:
print(f"\n成功处理了 {len(result)} 个角色的 AcceptGift 对话")
print("\n正在生成带序号的Lua table文件...")
if generate_lua_table_with_order(result):
print("\n处理完成!")
total_dialogues = sum(len(dialogues) for dialogues in result.values())
print(f"总对话数: {total_dialogues}")
print(f"角色数: {len(result)}")
else:
print("生成 Lua 文件失败")
else:
print("没有提取到对话数据")
if __name__ == "__main__":
main()