omegafox/launcher/validate.go
daijro 662e62fb2c Fix launcher issues on macos #7
Fixes properties.json path not being found.
2024-09-12 08:03:36 -05:00

75 lines
1.6 KiB
Go

package main
import (
"fmt"
"math"
"os"
"reflect"
"runtime"
)
type Property struct {
Property string `json:"property"`
Type string `json:"type"`
}
func validateConfig(configMap map[string]interface{}) {
properties := loadProperties()
// Create a map for quick lookup of property types
propertyTypes := make(map[string]string)
for _, prop := range properties {
propertyTypes[prop.Property] = prop.Type
}
for key, value := range configMap {
expectedType, exists := propertyTypes[key]
if !exists {
fmt.Printf("Warning: Unknown property %s in config\n", key)
continue
}
if !validateType(value, expectedType) {
fmt.Printf("Invalid type for property %s. Expected %s, got %T\n", key, expectedType, value)
os.Exit(1)
}
}
}
func loadProperties() []Property {
// Get the path to the properties.json file
var propertiesPath string
if normalizeOS(runtime.GOOS) == "macos" {
propertiesPath = getPath("Camoufox.app/Contents/Resources/properties.json")
} else {
propertiesPath = getPath("properties.json")
}
var properties []Property
// Parse the JSON file
parseJson(propertiesPath, &properties)
return properties
}
func validateType(value interface{}, expectedType string) bool {
switch expectedType {
case "str":
_, ok := value.(string)
return ok
case "int":
v, ok := value.(float64)
return ok && v == math.Trunc(v)
case "uint":
v, ok := value.(float64)
return ok && v == math.Trunc(v) && v >= 0
case "double":
_, ok := value.(float64)
return ok
case "bool":
_, ok := value.(bool)
return ok
case "array":
return reflect.TypeOf(value).Kind() == reflect.Slice
default:
return false
}
}