Skip to content

Commit

Permalink
Merge branch 'development' manually because github was unable to reso…
Browse files Browse the repository at this point in the history
…lve changes from #197
  • Loading branch information
Nusiq committed Aug 31, 2022
2 parents 4594f35 + 701c692 commit 3bfc302
Show file tree
Hide file tree
Showing 26 changed files with 831 additions and 546 deletions.
19 changes: 12 additions & 7 deletions regolith/compatibility_other_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package regolith

import (
"fmt"
"runtime"
)

// venvScriptsPath is a folder name between "venv" and "python" that leads to
Expand All @@ -15,6 +14,9 @@ const venvScriptsPath = "bin"
// exeSuffix is a suffix for executable files.
const exeSuffix = ""

// Error used whe os.UserCacheDir fails
const osUserCacheDirError = "Failed to get user cache directory."

// copyFileSecurityInfo placeholder for a function which is necessary only
// on Windows.
func copyFileSecurityInfo(source string, target string) error {
Expand All @@ -24,25 +26,28 @@ func copyFileSecurityInfo(source string, target string) error {
type DirWatcher struct{}

func NewDirWatcher(path string) (*DirWatcher, error) {
return nil, fmt.Errorf("Not implemented for this system.")
return nil, WrappedError(notImplementedOnThisSystemError)
}

func (d *DirWatcher) WaitForChange() error {
return fmt.Errorf("Not implemented for this system.")
return WrappedError(notImplementedOnThisSystemError)
}

func (d *DirWatcher) WaitForChangeGroup(
groupTimeout uint32, interruptionChannel chan string,
interruptionMessage string,
) error {
return fmt.Errorf("Not implemented for this system.")
return WrappedError(notImplementedOnThisSystemError)
}

func (d *DirWatcher) Close() error {
return fmt.Errorf("Not implemented for this system.")
return WrappedError(notImplementedOnThisSystemError)
}

func FindMojangDir() (string, error) {
return "", WrappedErrorf(
"Unsupported operating system: '%s'", runtime.GOOS)
return "", WrappedError(notImplementedOnThisSystemError)
}

func FindPreviewDir() (string, error) {
return "", WrappedError(notImplementedOnThisSystemError)
}
33 changes: 22 additions & 11 deletions regolith/compatibility_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const venvScriptsPath = "Scripts"
// exeSuffix is a suffix for executable files.
const exeSuffix = ".exe"

// Error used whe os.UserCacheDir fails
const osUserCacheDirError = "Failed to resolve %LocalAppData% path."

// copyFileSecurityInfo copies the DACL info from source path to DACL of
// the target path
func copyFileSecurityInfo(source string, target string) error {
Expand All @@ -25,22 +28,19 @@ func copyFileSecurityInfo(source string, target string) error {
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION)
if err != nil {
return WrapErrorf(
err, "Unable to get security info of %q.", source)
return WrapError(err, "Unable to get security info from the source.")
}
dacl, _, err := securityInfo.DACL()
if err != nil {
return WrapErrorf(
err, "Unable to get DACL of %q.", source)
return WrapErrorf(err, "Unable to get DACL of the source.")
}
err = windows.SetNamedSecurityInfo(
target,
windows.SE_FILE_OBJECT,
windows.DACL_SECURITY_INFORMATION, nil, nil, dacl, nil,
)
if err != nil {
return WrapErrorf(
err, "Unable to set DACL of %q.", target)
return WrapErrorf(err, "Unable to set DACL of the target.")
}
return nil
}
Expand Down Expand Up @@ -146,13 +146,24 @@ func FindMojangDir() (string, error) {
"com.mojang")
if _, err := os.Stat(result); err != nil {
if os.IsNotExist(err) {
return "", WrapErrorf(
err, "The \"com.mojang\" folder is not at \"%s\".\n"+
"Does your system have multiple user accounts?", result)
return "", WrapErrorf(err, osStatErrorIsNotExist, result)
}
return "", WrapErrorf(err, osStatErrorAny, result)
}
return result, nil
}

func FindPreviewDir() (string, error) {
result := filepath.Join(
os.Getenv("LOCALAPPDATA"), "Packages",
"Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe", "LocalState", "games",
"com.mojang")
if _, err := os.Stat(result); err != nil {
if os.IsNotExist(err) {
return "", WrapErrorf(err, osStatErrorIsNotExist, result)
}
return "", WrapErrorf(
err, "Unable to access \"%s\".\n"+
"Are your user permissions correct?", result)
err, osStatErrorAny, result)
}
return result, nil
}
60 changes: 33 additions & 27 deletions regolith/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,40 @@ func ConfigFromObject(obj map[string]interface{}) (*Config, error) {
// Name
name, ok := obj["name"].(string)
if !ok {
return nil, WrappedError("The \"name\" property is missing.")
return nil, WrappedErrorf(jsonPathMissingError, "name")
}
result.Name = name
// Author
author, ok := obj["author"].(string)
if !ok {
return nil, WrappedError("The \"author\" is missing.")
return nil, WrappedErrorf(jsonPathMissingError, "author")
}
result.Author = author
// Packs
if packs, ok := obj["packs"]; ok {
packs, ok := packs.(map[string]interface{})
if !ok {
return nil, WrappedError("The \"packs\" property not a map.")
return nil, WrappedErrorf(jsonPathTypeError, "packs", "object")
}
// Packs can be empty, no need to check for errors
result.Packs = PacksFromObject(packs)
} else {
return nil, WrappedError("The \"packs\" property is missing.")
return nil, WrappedErrorf(jsonPathMissingError, "packs")
}
// Regolith
if regolith, ok := obj["regolith"]; ok {
regolith, ok := regolith.(map[string]interface{})
if !ok {
return nil, WrappedError("The \"regolith\" property is not a map.")
return nil, WrappedErrorf(
jsonPathTypeError, "regolith", "object")
}
regolithProject, err := RegolithProjectFromObject(regolith)
if err != nil {
return nil, WrapError(
err, "Could not parse \"regolith\" property.")
return nil, WrapErrorf(err, jsonPropertyParseError, "regolith")
}
result.RegolithProject = regolithProject
} else {
return nil, WrappedError("Missing \"regolith\" property.")
return nil, WrappedErrorf(jsonPropertyMissingError, "regolith")
}
return result, nil
}
Expand Down Expand Up @@ -107,11 +107,12 @@ func RegolithProjectFromObject(
}
// DataPath
if _, ok := obj["dataPath"]; !ok {
return result, WrappedError("The \"dataPath\" property is missing.")
return result, WrappedErrorf(jsonPropertyMissingError, "dataPath")
}
dataPath, ok := obj["dataPath"].(string)
if !ok {
return result, WrappedErrorf("The \"dataPath\" is not a string")
return result, WrappedErrorf(
jsonPropertyTypeError, "dataPath", "string")
}
result.DataPath = dataPath
// Filter definitions
Expand All @@ -121,33 +122,35 @@ func RegolithProjectFromObject(
filterDefinitionMap, ok := filterDefinition.(map[string]interface{})
if !ok {
return result, WrappedErrorf(
"The filter definition %q not a map.", filterDefinitionName)
jsonPropertyTypeError, "filterDefinitions",
"object")
}
filterInstaller, err := FilterInstallerFromObject(
filterDefinitionName, filterDefinitionMap)
if err != nil {
return result, WrapError(
err, "Could not parse the filter definition.")
return result, WrapErrorf(
err, jsonPropertyParseError, "filterDefinitions")
}
result.FilterDefinitions[filterDefinitionName] = filterInstaller
}
}
// Profiles
profiles, ok := obj["profiles"].(map[string]interface{})
if !ok {
return result, WrappedError("Missing \"profiles\" property.")
return result, WrappedErrorf(jsonPropertyMissingError, "profiles")
}
for profileName, profile := range profiles {
profileMap, ok := profile.(map[string]interface{})
if !ok {
return result, WrappedErrorf(
"Profile %q is not a map.", profileName)
jsonPropertyTypeError,
"profiles->"+profileName, "object")
}
profileValue, err := ProfileFromObject(
profileMap, result.FilterDefinitions)
if err != nil {
return result, WrapErrorf(
err, "Could not parse %q profile.", profileName)
err, jsonPropertyParseError, "profiles->"+profileName)
}
result.Profiles[profileName] = profileValue
}
Expand All @@ -156,8 +159,8 @@ func RegolithProjectFromObject(
if _, ok := obj["useAppData"]; ok {
useAppData, ok = obj["useAppData"].(bool)
if !ok {
return result, WrappedError(
"The \"useAppData\" property is not a boolean.")
return result, WrappedErrorf(
jsonPropertyTypeError, "useAppData", "boolean")
}
}
result.UseAppData = useAppData
Expand All @@ -167,28 +170,31 @@ func RegolithProjectFromObject(
// ExportTargetFromObject creates a "ExportTarget" object from
// map[string]interface{}
func ExportTargetFromObject(obj map[string]interface{}) (ExportTarget, error) {
// TODO - implement in a proper way
result := ExportTarget{}
// Target
target, ok := obj["target"].(string)
targetObj, ok := obj["target"]
if !ok {
return result, WrappedErrorf(jsonPropertyMissingError, "target")
}
target, ok := targetObj.(string)
if !ok {
return result, WrappedError(
"The\"target\" property in \"config.json\" is missing.")
return result, WrappedErrorf(
jsonPropertyTypeError, "target", "string")
}
result.Target = target
// RpPath
// RpPath - can be empty
rpPath, _ := obj["rpPath"].(string)
result.RpPath = rpPath
// BpPath
// BpPath - can be empty
bpPath, _ := obj["bpPath"].(string)
result.BpPath = bpPath
// WorldName
// WorldName - can be empty
worldName, _ := obj["worldName"].(string)
result.WorldName = worldName
// WorldPath
// WorldPath - can be empty
worldPath, _ := obj["worldPath"].(string)
result.WorldPath = worldPath
// ReadOnly
// ReadOnly - can be empty
readOnly, _ := obj["readOnly"].(bool)
result.ReadOnly = readOnly
return result, nil
Expand Down
27 changes: 13 additions & 14 deletions regolith/config_unparsed.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ import (
func LoadConfigAsMap() (map[string]interface{}, error) {
file, err := ioutil.ReadFile(ConfigFilePath)
if err != nil {
return nil, WrappedErrorf( // We don't need to pass OS error. It's confusing.
"Failed to open %q. This directory is not a Regolith project.\n"+
"Please make sure to run this command in a Regolith project directory.\n"+
"If you want to create new Regolith project here, use \"regolith init\".",
ConfigFilePath)
return nil, WrappedError( // We don't need to pass OS error. It's confusing.
"Failed to open \"config.json\". This directory is not a Regolith project.\n" +
"Please make sure to run this command in a Regolith project directory.\n" +
"If you want to create new Regolith project here, use \"regolith init\".")
}
var configJson map[string]interface{}
err = jsonc.Unmarshal(file, &configJson)
if err != nil {
return nil, WrapErrorf(
err, "Could not load %q as a JSON file.", ConfigFilePath)
return nil, WrapErrorf(err, jsonUnmarshalError, ConfigFilePath)
}
return configJson, nil
}
Expand All @@ -35,11 +33,11 @@ func LoadConfigAsMap() (map[string]interface{}, error) {
func dataPathFromConfigMap(config map[string]interface{}) (string, error) {
regolith, ok := config["regolith"].(map[string]interface{})
if !ok {
return "", WrappedError("Missing \"regolith\" property.")
return "", WrappedErrorf(jsonPathMissingError, "regolith")
}
dataPath, ok := regolith["dataPath"].(string)
if !ok {
return "", WrappedError("Missing \"regolith\"->\"dataPath\" property.")
return "", WrappedErrorf(jsonPathMissingError, "regolith->dataPath")
}
return dataPath, nil
}
Expand All @@ -51,11 +49,12 @@ func filterDefinitionsFromConfigMap(
) (map[string]interface{}, error) {
regolith, ok := config["regolith"].(map[string]interface{})
if !ok {
return nil, WrappedError("Missing \"regolith\" property.")
return nil, WrappedErrorf(jsonPathMissingError, "regolith")
}
filterDefinitions, ok := regolith["filterDefinitions"].(map[string]interface{})
if !ok {
return nil, WrappedError("Missing \"regolith\"->\"filterDefinitions\" property.")
return nil, WrappedErrorf(
jsonPathMissingError, "regolith->filterDefinitions")
}
return filterDefinitions, nil
}
Expand All @@ -65,16 +64,16 @@ func filterDefinitionsFromConfigMap(
func useAppDataFromConfigMap(config map[string]interface{}) (bool, error) {
regolith, ok := config["regolith"].(map[string]interface{})
if !ok {
return false, WrappedError("Missing \"regolith\" property.")
return false, WrappedErrorf(jsonPathMissingError, "regolith")
}
filterDefinitionsInterface, ok := regolith["useAppData"]
if !ok { // false by default
return false, nil
}
filterDefinitions, ok := filterDefinitionsInterface.(bool)
if !ok {
return false, WrappedError("\"regolith\"->\"useAppData\" property " +
"must be a boolean.")
return false, WrappedErrorf(
jsonPathTypeError, "regolith->useAppData", "bool")
}
return filterDefinitions, nil
}
Loading

0 comments on commit 3bfc302

Please sign in to comment.