1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package dynamic
import (
"encoding/json"
"fmt"
"os"
components "github.com/DegustatorPonos/RuinesOfRafdolon/Components"
descriptors "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic/Descriptors"
settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
utils "github.com/DegustatorPonos/RuinesOfRafdolon/Utils"
rl "github.com/gen2brain/raylib-go/raylib"
)
// The defenition of the package
type PackageDescription struct {
Name string `json:"name"`
Version uint64 `json:"version"`
Type string `json:"type"`
MinimumVersion *settings.AppVersion
MaximumVersion *settings.AppVersion
}
func (base *PackageDescription) IsValid() error {
if base.Name == "" {
return fmt.Errorf("Invalid package descriptor: the name cannot be empty")
}
if base.Version == 0 {
return fmt.Errorf("Invalid package descriptor: the version cannot be 0")
}
if base.MinimumVersion == nil {
return fmt.Errorf("Invalid package descriptor: The package must specify the minimal version")
}
if !base.MinimumVersion.IsCompatible() || (base.MaximumVersion != nil && !base.MaximumVersion.IsLessThan(&settings.Current.Version)){
return fmt.Errorf("Invalid package descriptor: The package is made for the newer or older version")
}
return nil
}
// The dynamic collection of the things
type Package struct {
Description PackageDescription
Textures *descriptors.TexturesDescriptor
Worlds map[string]*descriptors.WorldDescriptor
location string
}
func (base *Package) String() string {
var outp, jsonErr = json.Marshal(base)
if jsonErr != nil {
return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
}
return string(outp)
}
func ReadPackage(dir os.DirEntry) (*Package, error) {
var dirPath = utils.JoinFileLocation(settings.Current.PackagesLocation, dir.Name())
var desc = PackageDescription{}
if descErr := ReadValidJSONfromFile(utils.JoinFileLocation(dirPath, "Description.json"), &desc); descErr != nil {
return nil, descErr
}
return &Package{
Description: desc,
}, nil
}
func loadAssetsFromDir[T Validatable](dirLocation string, InitializeFunc func()T) ([]T, error) {
var files, err = os.ReadDir(dirLocation)
if err != nil {
return nil, err
}
var outp = make([]T, 0)
for _, v := range files {
var new = InitializeFunc()
var loadErr = ReadValidJSONfromFile(utils.JoinFileLocation(dirLocation, v.Name()), new)
if loadErr != nil {
rl.TraceLog(rl.LogWarning, "Failed to load the asset %s: %s", v.Name(), loadErr)
continue
}
outp = append(outp, new)
}
return outp, nil
}
func (base *Package) ReadTextures() {
var textures = &descriptors.TexturesDescriptor{}
var texturesLocation = utils.JoinFileLocation(base.location, descriptors.TexturesDescriptorDirectoryName)
var loadErr = utils.ReadJSONfromFile(utils.JoinFileLocation(texturesLocation, descriptors.TexturesDescriptorFileName), textures)
if loadErr != nil {
rl.TraceLog(rl.LogWarning, "Failed to load texture description from the module %s: %s", base.location, loadErr)
} else {
base.Textures = textures
}
}
// Loads textures in the resource manager
func (base *Package) LoadTextures() {
for _, v := range base.Textures.Avaliable {
var location = base.Textures.GetTexturePath(base.location, v)
var displayName = fmt.Sprintf("%v/%v", base.Description.Name, v)
components.Resources.Textures.LoadTexture(location, displayName)
}
}
func (base *Package) ReadWorlds() {
if base.Description.Type != "story" {
return
}
var worlds, loadErr = loadWorldsFromDir(utils.JoinFileLocation(base.location, "Worlds"))
if loadErr != nil {
rl.TraceLog(rl.LogWarning, "Failed to load world from the module %s: %s", base.location, loadErr)
} else {
base.Worlds = worlds
}
}
func (base *Package) LoadWorlds() {
if base.Description.Type != "story" {
return
}
for _, v := range base.Worlds {
var parsed = v.Parse()
components.Resources.Worlds[v.Name] = &parsed
}
}
func loadWorldsFromDir(dirLocation string) (map[string]*descriptors.WorldDescriptor, error) {
var loaded, err = loadAssetsFromDir(dirLocation, func() *descriptors.WorldDescriptor{ return &descriptors.WorldDescriptor{} })
if err != nil {
return nil, err
}
return descriptors.MapWorldDescriptors(loaded), nil
}
// Deprecated
func loadTilesFromDir(dirLocation string) (map[uint64]*descriptors.TileDescriptor, error) {
var loaded, err = loadAssetsFromDir(dirLocation, func() *descriptors.TileDescriptor{ return &descriptors.TileDescriptor{} })
if err != nil {
return nil, err
}
return descriptors.MapTileDescriptors(loaded), nil
}
|