DST模组开发-资源编译问题

2025-08-02

背景

作为《饥荒:联机版》(DST)玩家,在长时间有完后难免会遇到一些没有被解决的痛点,同时也对模组制作抱有一种期待。因此,我决定开发我的第一个模组:一个在玩家出生时自动获得的静态攻略书

我的目标很明确:

  1. 创建一个新物品 atlas_book
  2. 为该物品提供一个自定义的物品栏图标
  3. 玩家出生时在物品栏中生成该物品

我创建了基本的模组结构, modinfo.luamodmain.lua 和 Prefab 文件。但很快遇到了第一个问题:游戏引擎不直接使用 .png 文件,而是需要经过编译的 .tex (纹理) 和 .xml (图像)

一、自动编译失效

根据教程,DST 的资源编译是自动化的。理论上:

  1. 在项目根目录创建 exported 文件夹
  2. exported 内,按规定的结构存入 .png ( exported/images/inventoryimages/atlas_book.png)
  3. 启动游戏时会自动编译 exported 里的资源,生成对应的 .tex.xml 文件

然而,这个流程在我的环境中并未生效

我反复检查了文件路径,修改了 modinfo.lua 中的配置,无数次查看启动日志。但无论如何,.tex.xml 始终没有被编译。从日志内容来看,甚至没有提到编译的事,仅仅是指出缺少对应的texxml

二、解决方案:手动执行 autocompiler.exe

在查阅无数教程后,我发现科雷官方提供的工具包里有自动编译工具: Don't Starve Mod Tools/mod_tools/autocompiler.exe

  • 正确编译:直接运行 autocompiler.exe 只会出现一个空白的命令窗。经过多次摸索,我通过将 Don't Starve Mod Tools/mod_tools/ 下的整个 compiler 文件夹复制到模组根目录中,成功进行了编译

三、 源码示例

  1. modinfo.lua:指向正确的图片路径
    -- modinfo.lua
    name = "Codex Astralis"
    icon_atlas = "modicon.xml"
    icon = "modicon.tex"
    -- ...
    
  2. Prefab (scripts/atlas_book.lua) 中,声明需要加载的资源,指定物品栏图标的图集
    -- scripts/atlas_book.lua
    local assets = {
        Asset("ATLAS", "images/inventoryimages/atlas_book.xml"),
        Asset("IMAGE", "images/inventoryimages/atlas_book.tex"),
    }
    -- ...
    inst.components.inventoryitem.atlasname = "images/inventoryimages/atlas_book.xml"
    
  3. 编写核心逻辑 modmain.lua:监听玩家进入世界的事件,并在服务器主世界 (mastersim) 中添加物品
    -- modmain.lua
    PrefabFiles = { "atlas_book" }
    
    AddPrefabPostInit("player_classified", function(inst)
        inst:ListenForEvent("ms_playeractivated", function(inst, player)
            if TheWorld.ismastersim and not player.persists.has_atlas_book then
                player.components.inventory:GiveItem(SpawnPrefab("atlas_book"))
                player.persists.has_atlas_book = true
            end
        end)
    end)