mirror of
https://github.com/packwiz/packwiz.git
synced 2025-11-19 01:24:32 +01:00
Implement pack importing/exporting for downloaded Curseforge packs
Abstract out hash implementations Implement file saving/downloading
This commit is contained in:
119
curseforge/packinterop/translation.go
Normal file
119
curseforge/packinterop/translation.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package packinterop
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/comp500/packwiz/core"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ReadMetadata(s ImportPackSource) ImportPackMetadata {
|
||||
var packImport ImportPackMetadata
|
||||
metaFile := s.GetPackFile()
|
||||
rdr, err := metaFile.Open()
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading file: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Read the whole file (as we are going to parse it multiple times)
|
||||
fileData, err := ioutil.ReadAll(rdr)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading file: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Determine what format the file is
|
||||
var jsonFile map[string]interface{}
|
||||
err = json.Unmarshal(fileData, &jsonFile)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing JSON: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
isManifest := false
|
||||
if v, ok := jsonFile["manifestType"]; ok {
|
||||
isManifest = v.(string) == "minecraftModpack"
|
||||
}
|
||||
if isManifest {
|
||||
packMeta := cursePackMeta{importSrc: s}
|
||||
err = json.Unmarshal(fileData, &packMeta)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing JSON: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
packImport = packMeta
|
||||
} else {
|
||||
// Replace FileNameOnDisk with fileNameOnDisk
|
||||
fileData = bytes.ReplaceAll(fileData, []byte("FileNameOnDisk"), []byte("fileNameOnDisk"))
|
||||
packMeta := twitchInstalledPackMeta{importSrc: s}
|
||||
err = json.Unmarshal(fileData, &packMeta)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing JSON: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
packImport = packMeta
|
||||
}
|
||||
|
||||
return packImport
|
||||
}
|
||||
|
||||
// AddonFileReference is a pair of Project ID and File ID to reference a single file on CurseForge
|
||||
type AddonFileReference struct {
|
||||
ProjectID int
|
||||
FileID int
|
||||
}
|
||||
|
||||
func WriteManifestFromPack(pack core.Pack, fileRefs []AddonFileReference, out io.Writer) error {
|
||||
// TODO: should Required be false sometimes?
|
||||
files := make([]struct {
|
||||
ProjectID int `json:"projectID"`
|
||||
FileID int `json:"fileID"`
|
||||
Required bool `json:"required"`
|
||||
}, len(fileRefs))
|
||||
for i, fr := range fileRefs {
|
||||
files[i] = struct {
|
||||
ProjectID int `json:"projectID"`
|
||||
FileID int `json:"fileID"`
|
||||
Required bool `json:"required"`
|
||||
}{ProjectID: fr.ProjectID, FileID: fr.FileID, Required: true}
|
||||
}
|
||||
|
||||
modLoaders := make([]modLoaderDef, 0, 1)
|
||||
forgeVersion, ok := pack.Versions["forge"]
|
||||
if ok {
|
||||
modLoaders = append(modLoaders, modLoaderDef{
|
||||
ID: "forge-" + forgeVersion,
|
||||
Primary: true,
|
||||
})
|
||||
}
|
||||
|
||||
manifest := cursePackMeta{
|
||||
Minecraft: struct {
|
||||
Version string `json:"version"`
|
||||
ModLoaders []modLoaderDef `json:"modLoaders"`
|
||||
}{
|
||||
Version: pack.Versions["minecraft"],
|
||||
ModLoaders: modLoaders,
|
||||
},
|
||||
ManifestType: "minecraftModpack",
|
||||
ManifestVersion: 1,
|
||||
NameInternal: pack.Name,
|
||||
Version: "", // TODO: store or take this?
|
||||
Author: "", // TODO: store or take this?
|
||||
ProjectID: 0, // TODO: store or take this?
|
||||
Files: files,
|
||||
Overrides: "overrides",
|
||||
}
|
||||
|
||||
w := json.NewEncoder(out)
|
||||
w.SetIndent("", " ") // Match CF export
|
||||
err := w.Encode(manifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user