package packages import ( "fmt" "os" settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings" 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) { var dir, dirErr = os.ReadDir(settings.Current.PackagesLocation) if dirErr != nil { return nil, dirErr } var outp = PackageManager{ 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", packagePath, packageErr.Error()) continue } // 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() if err != nil { panic(fmt.Sprintf("Failed to load packages: %v", err.Error())) } Manager = manager }