summaryrefslogtreecommitdiff
path: root/engine/Components/Packages
diff options
context:
space:
mode:
Diffstat (limited to 'engine/Components/Packages')
-rw-r--r--engine/Components/Packages/Manager.go22
-rw-r--r--engine/Components/Packages/Package.go38
-rw-r--r--engine/Components/Packages/Validation.go15
3 files changed, 66 insertions, 9 deletions
diff --git a/engine/Components/Packages/Manager.go b/engine/Components/Packages/Manager.go
index 47b0476..8a2786b 100644
--- a/engine/Components/Packages/Manager.go
+++ b/engine/Components/Packages/Manager.go
@@ -8,13 +8,14 @@ import (
rl "github.com/gen2brain/raylib-go/raylib"
)
+// The collection of all avaliable packages that are loaded in the system
type PackageManager struct {
AvaliablePackages map[string]Package
}
var Manager *PackageManager = nil
-func LoadPackages() (*PackageManager, error) {
+func loadPackages() (*PackageManager, error) {
var dir, dirErr = os.ReadDir(settings.Current.PackagesLocation)
if dirErr != nil {
return nil, dirErr
@@ -23,19 +24,32 @@ func LoadPackages() (*PackageManager, error) {
AvaliablePackages: make(map[string]Package),
}
for _, v := range dir {
+ var packagePath = ConcantinateFileLocation(settings.Current.PackagesLocation, v.Name())
var newPackage, packageErr = ReadPackage(v)
if packageErr != nil {
- rl.TraceLog(rl.LogWarning, "Failed to load the module %v: %s", v.Name(), packageErr.Error())
+ rl.TraceLog(rl.LogWarning, "Failed to load the module %v: %s", packagePath, packageErr.Error())
continue
}
- _ = newPackage
+
+ // Resource packages cannot contain worlds
+ if newPackage.Description.Type == "story" {
+ var worlds, worldsErr = loadWorldsFromDir(ConcantinateFileLocation(packagePath, "Worlds"))
+ if worldsErr != nil {
+ rl.TraceLog(rl.LogWarning, "Failed to load the world from the module %s: %s", v.Name(), worldsErr)
+ } else {
+ newPackage.Worlds = worlds
+ }
+ }
+
+ outp.AvaliablePackages[newPackage.Description.Name] = *newPackage
rl.TraceLog(rl.LogError, "Loaded the module %s", newPackage.Description.Name)
}
return &outp, nil
}
+// Loads the packages from the specified in settings directory. Panics on serious error
func Init() {
- var manager, err = LoadPackages()
+ var manager, err = loadPackages()
if err != nil {
panic(fmt.Sprintf("Failed to load packages: %v", err.Error()))
}
diff --git a/engine/Components/Packages/Package.go b/engine/Components/Packages/Package.go
index 51e2a80..9f2fdd2 100644
--- a/engine/Components/Packages/Package.go
+++ b/engine/Components/Packages/Package.go
@@ -1,37 +1,48 @@
package packages
import (
+ "encoding/json"
"fmt"
"os"
+ world "github.com/DegustatorPonos/RuinesOfRafdolon/Components/World"
settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
+ rl "github.com/gen2brain/raylib-go/raylib"
)
type PackageDescription struct {
Name string `json:"name"`
Version uint64 `json:"version"`
Type string `json:"type"`
+ MinimumVersion *settings.AppVersion
+ MaximumVersion *settings.AppVersion
}
type Package struct {
Description PackageDescription `json:"description"`
+ Worlds map[string]world.World `json:"-"`
+}
+
+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 = ConcantinateFileLocation(settings.Current.PackagesLocation, dir.Name())
var desc = PackageDescription{}
- if descErr := ReadJSONfromFile(ConcantinateFileLocation(dirPath, "Description.json"), &desc); descErr != nil {
+ if descErr := ReadValidJSONfromFile(ConcantinateFileLocation(dirPath, "Description.json"), &desc); descErr != nil {
return nil, descErr
}
- if descErr := desc.Validate(); descErr != nil {
- return nil, fmt.Errorf("Failed to parse descriptor: %s", descErr.Error())
- }
return &Package{
Description: desc,
}, nil
}
-func (base *PackageDescription) Validate() error {
+func (base *PackageDescription) IsValid() error {
if base.Name == "" {
return fmt.Errorf("Invalid package descriptor: the name cannot be empty")
}
@@ -40,3 +51,20 @@ func (base *PackageDescription) Validate() error {
}
return nil
}
+
+func loadWorldsFromDir(dirLocation string) (map[string]world.World, error) {
+ var files, err = os.ReadDir(dirLocation)
+ if err != nil {
+ return nil, err
+ }
+ var outp = make(map[string]world.World)
+ for _, v := range files {
+ var new = world.World{}
+ var loadErr = ReadJSONfromFile(ConcantinateFileLocation(dirLocation, v.Name()), &new)
+ if loadErr != nil {
+ rl.TraceLog(rl.LogWarning, "Failed to load the world %s: %s", v.Name(), loadErr)
+ continue
+ }
+ }
+ return outp, nil
+}
diff --git a/engine/Components/Packages/Validation.go b/engine/Components/Packages/Validation.go
new file mode 100644
index 0000000..04428d2
--- /dev/null
+++ b/engine/Components/Packages/Validation.go
@@ -0,0 +1,15 @@
+package packages
+
+type Validatable interface {
+ // If the object is returned invalid this function should return the reason
+ IsValid() error
+}
+
+// Reads object fron the file and validates it
+func ReadValidJSONfromFile(fileLocation string, v Validatable) error {
+ var err = ReadJSONfromFile(fileLocation, v)
+ if err != nil {
+ return err
+ }
+ return v.IsValid()
+}