package main import ( "encoding/json" "fmt" "io" "net/http" "os" "path/filepath" "strings" ) type InstanceMetadata struct { Version string `json:"Version"` ServerName string `json:"ServerName"` Port int `json:"Port"` Config map[string]interface{} `json:"config"` } func PrepareInstanceConfig(templateVersion string, instanceConfigPath string, meta InstanceMetadata, cfg *AppConfig) error { return SyncInstanceConfig(templateVersion, instanceConfigPath, meta, cfg) } func SyncInstanceConfig(templateVersion string, instanceConfigPath string, meta InstanceMetadata, cfg *AppConfig) error { templatePath := filepath.Join(cfg.Storage.ConfigTemplatesDir, templateVersion, "serverconfig.json") if err := ensureTemplateExists(templateVersion, templatePath, cfg); err != nil { return fmt.Errorf("failed ensuring configuration template availability: %w", err) } if _, err := os.Stat(instanceConfigPath); os.IsNotExist(err) { if err := os.MkdirAll(filepath.Dir(instanceConfigPath), 0755); err != nil { return fmt.Errorf("failed creating instance directory tree: %w", err) } source, err := os.Open(templatePath) if err != nil { return fmt.Errorf("failed opening baseline template file: %w", err) } defer source.Close() destination, err := os.Create(instanceConfigPath) if err != nil { return fmt.Errorf("failed creating target instance configuration: %w", err) } _, err = io.Copy(destination, source) destination.Close() if err != nil { return fmt.Errorf("failed cloning configuration template payload: %w", err) } } data, err := os.ReadFile(instanceConfigPath) if err != nil { return fmt.Errorf("failed reading configuration data: %w", err) } var rawConfig map[string]interface{} if err := json.Unmarshal(data, &rawConfig); err != nil { return fmt.Errorf("failed parsing configuration JSON payload: %w", err) } for key, value := range meta.Config { rawConfig[key] = value } rawConfig["ServerName"] = meta.ServerName rawConfig["Port"] = meta.Port instanceDir := filepath.Dir(instanceConfigPath) if worldConfig, ok := rawConfig["WorldConfig"].(map[string]interface{}); ok { worldConfig["SaveFileLocation"] = filepath.Join(instanceDir, "Saves", "default.vcdbs") } if modPaths, ok := rawConfig["ModPaths"].([]interface{}); ok { for i, pathVal := range modPaths { if strPath, ok := pathVal.(string); ok && (strings.Contains(strPath, "/vs-manager/instances/") || filepath.IsAbs(strPath)) && strPath != "Mods" { modPaths[i] = filepath.Join(instanceDir, "Mods") } } } updatedData, err := json.MarshalIndent(rawConfig, "", " ") if err != nil { return fmt.Errorf("failed marshaling updated configuration adjustments: %w", err) } return os.WriteFile(instanceConfigPath, updatedData, 0644) } func ensureTemplateExists(version string, targetPath string, cfg *AppConfig) error { if _, err := os.Stat(targetPath); err == nil { return nil } fmt.Printf("Template for version %s not found locally. Fetching remote layout from git server...\n", version) if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil { return fmt.Errorf("failed to create local template cache directory: %w", err) } remoteURL := fmt.Sprintf("https://git.bellsworne.tech/chrisbell/vssm_config_templates/raw/branch/main/%s/serverconfig.json", version) resp, err := http.Get(remoteURL) if err != nil { return fmt.Errorf("network error attempting to pull config template: %w", err) } defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { return fmt.Errorf("version template '%s' does not exist in the remote git repository asset tree", version) } else if resp.StatusCode != http.StatusOK { return fmt.Errorf("remote git mirror returned unexpected HTTP status: %s", resp.Status) } out, err := os.Create(targetPath) if err != nil { return fmt.Errorf("failed creating cache file hook on system storage: %w", err) } defer out.Close() _, err = io.Copy(out, resp.Body) if err != nil { return fmt.Errorf("failed stream-writing network payload cache frame to disk: %w", err) } fmt.Printf("Successfully cached configuration layout template for version %s\n", version) return nil }