z_mirror/fmm

git clone https://git.raiguard.me/z_mirror/fmm

Caleb HeuerImprove organization269870f

predep-parsing
2.2 KiB107 linesraw
1package main
2
3import (
4	"bufio"
5	"fmt"
6	"os"
7	"strings"
8)
9
10func expandDependencies(mods []ModIdent) []ModIdent {
11	visited := make(map[string]bool)
12
13	dir, err := newDir(modsDir)
14	if err != nil {
15		abort(err)
16	}
17
18	for i := 0; i < len(mods); i += 1 {
19		mod := mods[i]
20		visited[mod.Name] = true
21		file, _, err := dir.Find(Dependency{mod, DependencyRequired, VersionAny})
22		var deps []Dependency
23		if file != nil {
24			realDeps, err := file.Dependencies()
25			if err != nil {
26				errorln(err)
27				continue
28			}
29			deps = *realDeps
30		} else {
31			deps, err = portalGetDependencies(mod)
32			if err != nil {
33				errorln(err)
34				continue
35			}
36		}
37		for _, dep := range deps {
38			// FIXME: Dependency kind or version might be different / incompatible
39			if dep.Ident.Name == "base" || visited[dep.Ident.Name] {
40				continue
41			}
42			if dep.Kind == DependencyRequired || dep.Kind == DependencyNoLoadOrder {
43				visited[dep.Ident.Name] = true
44				mods = append(mods, dep.Ident)
45			}
46		}
47	}
48
49	return mods
50}
51
52func parseLogFile(filepath string) []ModIdent {
53	var output []ModIdent
54	file, err := os.Open(filepath)
55	if err != nil {
56		fmt.Fprintf(os.Stderr, "Error reading %s: %s", filepath, err)
57		return output
58	}
59	defer file.Close()
60
61	fileScanner := bufio.NewScanner(file)
62	fileScanner.Split(bufio.ScanLines)
63
64	inChecksums := false
65	for fileScanner.Scan() {
66		line := fileScanner.Text()
67		if !strings.Contains(line, "Checksum of") {
68			if inChecksums {
69				break
70			} else {
71				continue
72			}
73		}
74		inChecksums = true
75		modName, _ := strings.CutSuffix(strings.Split(strings.TrimSpace(line), " ")[3], ":")
76		if modName == "base" {
77			continue
78		}
79		output = append(output, ModIdent{modName, nil})
80	}
81
82	return output
83}
84
85func parseMods(input []string, parseDependencies bool) []ModIdent {
86	var output []ModIdent
87
88	for _, input := range input {
89		if strings.HasSuffix(input, ".zip") {
90			// TODO: Read from save
91		} else if strings.HasSuffix(input, ".log") {
92			output = append(output, parseLogFile(input)...)
93		} else if strings.HasSuffix(input, ".json") {
94			// TODO: info.json
95		} else if strings.HasPrefix(input, "!") {
96			// TODO: Mod set
97		} else {
98			output = append(output, newModIdent(input))
99		}
100	}
101
102	if parseDependencies {
103		output = expandDependencies(output)
104	}
105
106	return output
107}