up3
Build and deploy / Build (push) Has been cancelled
Build and deploy / Update Contributors (push) Has been cancelled
Build and deploy / Deploy (push) Has been cancelled

This commit is contained in:
2026-07-09 08:33:57 +08:00
commit 26ed99fda6
845 changed files with 75419 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
package packbuilder
import (
"encoding/json"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"strings"
_ "unsafe" // Imported for compiler directives.
"github.com/df-mc/dragonfly/server/world"
)
// buildBlocks builds all the block-related files for the resource pack. This includes textures, geometries, language
// entries and terrain texture atlas.
func buildBlocks(reg world.BlockRegistry, dir string) (count int, lang []string) {
if err := os.MkdirAll(filepath.Join(dir, "models/blocks"), os.ModePerm); err != nil {
panic(err)
}
if err := os.MkdirAll(filepath.Join(dir, "textures/blocks"), os.ModePerm); err != nil {
panic(err)
}
textureData := make(map[string]any)
for identifier, blk := range reg.CustomBlocks() {
b, ok := blk.(world.CustomBlockBuildable)
if !ok {
continue
}
name := strings.Split(identifier, ":")[1]
lang = append(lang, fmt.Sprintf("tile.%s.name=%s", identifier, b.Name()))
for name, texture := range b.Textures() {
textureData[name] = map[string]string{"textures": "textures/blocks/" + name}
buildBlockTexture(dir, name, texture)
}
if b.Geometry() != nil {
if err := os.WriteFile(filepath.Join(dir, "models/blocks", fmt.Sprintf("%s.geo.json", name)), b.Geometry(), 0666); err != nil {
panic(err)
}
}
count++
}
buildBlockAtlas(dir, map[string]any{
"resource_pack_name": "vanilla",
"texture_name": "atlas.terrain",
"padding": 8,
"num_mip_levels": 4,
"texture_data": textureData,
})
return
}
// buildBlockTexture creates a PNG file for the block from the provided image and name and writes it to the pack.
func buildBlockTexture(dir, name string, img image.Image) {
texture, err := os.Create(filepath.Join(dir, fmt.Sprintf("textures/blocks/%s.png", name)))
if err != nil {
panic(err)
}
if err := png.Encode(texture, img); err != nil {
_ = texture.Close()
panic(err)
}
if err := texture.Close(); err != nil {
panic(err)
}
}
// buildBlockAtlas creates the identifier to texture mapping and writes it to the pack.
func buildBlockAtlas(dir string, atlas map[string]any) {
b, err := json.Marshal(atlas)
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(dir, "textures/terrain_texture.json"), b, 0666); err != nil {
panic(err)
}
}
+70
View File
@@ -0,0 +1,70 @@
package packbuilder
import (
"encoding/json"
"fmt"
"github.com/df-mc/dragonfly/server/world"
"image"
"image/png"
"os"
"path/filepath"
"strings"
_ "unsafe" // Imported for compiler directives.
)
// buildItems builds all the item-related files for the resource pack. This includes textures, language
// entries and item atlas.
func buildItems(dir string) (count int, lang []string) {
if err := os.Mkdir(filepath.Join(dir, "items"), os.ModePerm); err != nil {
panic(err)
}
if err := os.MkdirAll(filepath.Join(dir, "textures/items"), os.ModePerm); err != nil {
panic(err)
}
textureData := make(map[string]any)
for _, item := range world.CustomItems() {
identifier, _ := item.EncodeItem()
lang = append(lang, fmt.Sprintf("item.%s.name=%s", identifier, item.Name()))
name := strings.Split(identifier, ":")[1]
textureData[identifier] = map[string]string{"textures": fmt.Sprintf("textures/items/%s.png", name)}
buildItemTexture(dir, name, item.Texture())
count++
}
buildItemAtlas(dir, map[string]any{
"resource_pack_name": "vanilla",
"texture_name": "atlas.items",
"texture_data": textureData,
})
return
}
// buildItemTexture creates a PNG file for the item from the provided image and name and writes it to the pack.
func buildItemTexture(dir, name string, img image.Image) {
texture, err := os.Create(filepath.Join(dir, "textures/items", name+".png"))
if err != nil {
panic(err)
}
if err := png.Encode(texture, img); err != nil {
_ = texture.Close()
panic(err)
}
if err := texture.Close(); err != nil {
panic(err)
}
}
// buildItemAtlas creates the identifier to texture mapping and writes it to the pack.
func buildItemAtlas(dir string, atlas map[string]any) {
b, err := json.Marshal(atlas)
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(dir, "textures/item_texture.json"), b, 0666); err != nil {
panic(err)
}
}
+17
View File
@@ -0,0 +1,17 @@
package packbuilder
import (
"os"
"path/filepath"
"strings"
)
// buildLanguageFile creates a lang file and writes all of the language entries to the pack.
func buildLanguageFile(dir string, lang []string) {
if err := os.Mkdir(filepath.Join(dir, "texts"), os.ModePerm); err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(dir, "texts/en_US.lang"), []byte(strings.Join(lang, "\n")), 0666); err != nil {
panic(err)
}
}
+53
View File
@@ -0,0 +1,53 @@
package packbuilder
import (
"encoding/json"
"github.com/google/uuid"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/resource"
"os"
"path/filepath"
"strconv"
"strings"
)
// buildManifest creates a JSON manifest file for the client to be able to read the resource pack. It creates
// basic information and writes it to the pack.
func buildManifest(dir string, headerUUID, moduleUUID uuid.UUID) {
m, err := json.Marshal(resource.Manifest{
FormatVersion: 2,
Header: resource.Header{
Name: "dragonfly auto-generated resource pack",
Description: "This resource pack contains auto-generated content from dragonfly",
UUID: headerUUID,
Version: [3]int{0, 0, 1},
MinimumGameVersion: parseVersion(protocol.CurrentVersion),
},
Modules: []resource.Module{
{
UUID: moduleUUID.String(),
Description: "This resource pack contains auto-generated content from dragonfly",
Type: "resources",
Version: [3]int{0, 0, 1},
},
},
})
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), m, 0666); err != nil {
panic(err)
}
}
// parseVersion parses the version passed in the format of a.b.c as a [3]int.
func parseVersion(ver string) [3]int {
frag := strings.Split(ver, ".")
if len(frag) != 3 {
panic("invalid version number " + ver)
}
a, _ := strconv.ParseInt(frag[0], 10, 64)
b, _ := strconv.ParseInt(frag[1], 10, 64)
c, _ := strconv.ParseInt(frag[2], 10, 64)
return [3]int{int(a), int(b), int(c)}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

@@ -0,0 +1,52 @@
package packbuilder
import (
_ "embed"
"os"
"github.com/df-mc/dragonfly/server/world"
"github.com/sandertv/gophertunnel/minecraft/resource"
"golang.org/x/mod/sumdb/dirhash"
)
//go:embed pack_icon.png
var packIcon []byte
// BuildResourcePack builds a resource pack based on custom features that have been registered to the server.
// It creates a UUID based on the hash of the directory so the client will only be prompted to download it
// once it is changed.
func BuildResourcePack(reg world.BlockRegistry) (*resource.Pack, bool) {
dir, err := os.MkdirTemp("", "dragonfly_resource_pack-")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
var assets int
var lang []string
itemCount, itemLang := buildItems(dir)
assets += itemCount
lang = append(lang, itemLang...)
blockCount, blockLang := buildBlocks(reg, dir)
assets += blockCount
lang = append(lang, blockLang...)
if assets > 0 {
buildLanguageFile(dir, lang)
if err := os.WriteFile(dir+"/pack_icon.png", packIcon, 0666); err != nil {
panic(err)
}
hash, err := dirhash.HashDir(dir, "", dirhash.Hash1)
if err != nil {
panic(err)
}
var header, module [16]byte
copy(header[:], hash)
copy(module[:], hash[16:])
buildManifest(dir, header, module)
return resource.MustReadPath(dir), true
}
return nil, false
}