package version_manifest import ( "encoding/json" "fmt" "time" ) // VersionManifestUrl is a URL to a version manifest JSON file of version 2. const VersionManifestUrl = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json" // VersionType is telling if it is a stable release or an intermedialry snapshot. type VersionType string const ( // VersionTypeSnapshot is an unstable intermediary version of a game. VersionTypeSnapshot VersionType = "snapshot" // VersionTypeRelease is a stable release version of a game. VersionTypeRelease VersionType = "release" ) // Version holds an information about a particular version. type Version struct { Id string `json:"id"` Type VersionType `json:"type"` Url string `json:"url"` Time time.Time `json:"time"` ReleaseTime time.Time `json:"releaseTime"` Sha1 string `json:"sha1"` } // VersionManifest holds an array of all game versions, also a latest metadata // that holds the latest release and snapshot. type VersionManifest struct { Latest struct { Release string `json:"release"` Snapshot string `json:"snapshot"` } `json:"latest"` Versions []Version `json:"versions"` } // New returns a new VersionManifest that holds info on every // known Minecraft version. func New(data []byte) (*VersionManifest, error) { vm := &VersionManifest{} if err := json.Unmarshal(data, vm); err != nil { return nil, err } return vm, nil } // GetLatest returns a latest release or snapshot depending on a type provided. func (vm *VersionManifest) GetLatest(typ VersionType) (Version, error) { var version string switch typ { case VersionTypeSnapshot: version = vm.Latest.Snapshot case VersionTypeRelease: version = vm.Latest.Release default: return Version{}, fmt.Errorf("version type %s doesn't exist", typ) } return vm.Get(version) } // Get returns a specific version of Minecraft. func (vm *VersionManifest) Get(version string) (Version, error) { for _, v := range vm.Versions { if v.Id == version { return v, nil } } return Version{}, fmt.Errorf("version %s not found", version) }