Skip to content

Commit

Permalink
Add hashes package
Browse files Browse the repository at this point in the history
  • Loading branch information
xypwn committed Apr 27, 2024
1 parent bc147e2 commit 5991ffa
Show file tree
Hide file tree
Showing 5 changed files with 49,085 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hashes/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package hashes

//go:generate go run github.com/xypwn/filediver/hashes/generate
78 changes: 78 additions & 0 deletions hashes/generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"bufio"
"fmt"
"net/http"
"os"
"slices"
"strings"
)

const prolog = `// Copied from Hellextractor by Xaymar (https://github.com/Xaymar/Hellextractor)
// as well as from hd2-name-db by DTZxPorter (https://github.com/dtzxporter/hd2-name-db)
// This is a list of all known file type and file path strings.
// Their respective hashes will be generated automatically.
`

func appendHTTPFile(strs *map[string]struct{}, url string, transform func(string) string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

rd := bufio.NewScanner(resp.Body)
for rd.Scan() {
s := rd.Text()
if len(s) == 0 || strings.HasPrefix(s, "//") || strings.HasPrefix(s, "#") {
continue
}
if transform != nil {
s = transform(s)
}
(*strs)[s] = struct{}{}
}
return nil
}

func main() {
strs := make(map[string]struct{})
if err := appendHTTPFile(&strs, "https://raw.githubusercontent.com/Xaymar/Hellextractor/root/files.txt", nil); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := appendHTTPFile(&strs, "https://raw.githubusercontent.com/Xaymar/Hellextractor/root/types.txt", nil); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := appendHTTPFile(&strs, "https://raw.githubusercontent.com/dtzxporter/hd2-name-db/main/assets.txt", func(s string) string {
_, rhs, _ := strings.Cut(s, ",")
return rhs
}); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

out, err := os.Create("hashes.txt")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if _, err := out.Write([]byte(prolog)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

strsSorted := make([]string, 0, len(strs))
for k := range strs {
strsSorted = append(strsSorted, k)
}
slices.Sort(strsSorted)
for _, line := range strsSorted {
if _, err := fmt.Fprintln(out, line); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
11 changes: 11 additions & 0 deletions hashes/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hashes

import (
_ "embed"
)

//go:embed hashes.txt
var Hashes string

//go:embed material_textures.txt
var MaterialTextures string
Loading

0 comments on commit 5991ffa

Please sign in to comment.