github.com/df-mc/dragonfly@v0.9.13/server/internal/packbuilder/resource_pack.go (about)

     1  package packbuilder
     2  
     3  import (
     4  	_ "embed"
     5  	"github.com/rogpeppe/go-internal/dirhash"
     6  	"github.com/sandertv/gophertunnel/minecraft/resource"
     7  	"os"
     8  )
     9  
    10  //go:embed pack_icon.png
    11  var packIcon []byte
    12  
    13  // BuildResourcePack builds a resource pack based on custom features that have been registered to the server.
    14  // It creates a UUID based on the hash of the directory so the client will only be prompted to download it
    15  // once it is changed.
    16  func BuildResourcePack() (*resource.Pack, bool) {
    17  	dir, err := os.MkdirTemp("", "dragonfly_resource_pack-")
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  	defer os.RemoveAll(dir)
    22  
    23  	var assets int
    24  	var lang []string
    25  
    26  	itemCount, itemLang := buildItems(dir)
    27  	assets += itemCount
    28  	lang = append(lang, itemLang...)
    29  
    30  	blockCount, blockLang := buildBlocks(dir)
    31  	assets += blockCount
    32  	lang = append(lang, blockLang...)
    33  
    34  	if assets > 0 {
    35  		buildLanguageFile(dir, lang)
    36  		if err := os.WriteFile(dir+"/pack_icon.png", packIcon, 0666); err != nil {
    37  			panic(err)
    38  		}
    39  		hash, err := dirhash.HashDir(dir, "", dirhash.Hash1)
    40  		if err != nil {
    41  			panic(err)
    42  		}
    43  		var header, module [16]byte
    44  		copy(header[:], hash)
    45  		copy(module[:], hash[16:])
    46  		buildManifest(dir, header, module)
    47  		return resource.MustReadPath(dir), true
    48  	}
    49  	return nil, false
    50  }