2022-05-02 20:07:15 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2023-12-16 14:19:53 +00:00
|
|
|
"fmt"
|
2022-05-02 20:07:15 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-06-05 01:56:46 +00:00
|
|
|
"path/filepath"
|
2023-12-06 23:39:34 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/disk"
|
2022-05-02 20:07:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SHA256Data(f io.Reader) (string, error) {
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return "", fmt.Errorf("failed to compute hash: %w", err)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates chan<- GenericProgress, d disk.Disk) error {
|
2022-06-07 23:36:28 +00:00
|
|
|
hashFile := filepath.Join(location, ".smm")
|
2023-12-28 00:13:09 +00:00
|
|
|
|
|
|
|
exists, err := d.Exists(hashFile)
|
2022-06-07 23:36:28 +00:00
|
|
|
if err != nil {
|
2023-12-28 00:13:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
hashBytes, err := d.Read(hashFile)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to read .smm mod hash file: %w", err)
|
2022-06-07 23:36:28 +00:00
|
|
|
}
|
2023-12-28 00:13:09 +00:00
|
|
|
|
2022-06-07 23:36:28 +00:00
|
|
|
if hash == string(hashBytes) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:13:09 +00:00
|
|
|
exists, err = d.Exists(location)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-22 22:24:35 +00:00
|
|
|
|
2023-12-28 00:13:09 +00:00
|
|
|
if exists {
|
2022-06-22 22:24:35 +00:00
|
|
|
if err := d.Remove(location); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to remove directory: %s: %w", location, err)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
if err := d.MkDir(location); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := zip.NewReader(f, size)
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to read file as zip: %w", err)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
totalSize := int64(0)
|
|
|
|
|
|
|
|
for _, file := range reader.File {
|
|
|
|
totalSize += int64(file.UncompressedSize64)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalExtracted := int64(0)
|
|
|
|
totalExtractedPtr := &totalExtracted
|
|
|
|
|
|
|
|
channelUsers := sync.WaitGroup{}
|
|
|
|
|
|
|
|
if updates != nil {
|
|
|
|
defer func() {
|
|
|
|
channelUsers.Wait()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range reader.File {
|
2022-05-02 20:07:15 +00:00
|
|
|
if !file.FileInfo().IsDir() {
|
2022-06-05 01:56:46 +00:00
|
|
|
outFileLocation := filepath.Join(location, file.Name)
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
if err := d.MkDir(filepath.Dir(outFileLocation)); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
var fileUpdates chan GenericProgress
|
|
|
|
if updates != nil {
|
|
|
|
fileUpdates = make(chan GenericProgress)
|
|
|
|
channelUsers.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer channelUsers.Done()
|
|
|
|
for fileUpdate := range fileUpdates {
|
|
|
|
updates <- GenericProgress{
|
|
|
|
Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed,
|
|
|
|
Total: totalSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
if err := writeZipFile(outFileLocation, file, d, fileUpdates); err != nil {
|
|
|
|
return err
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2023-12-06 23:39:34 +00:00
|
|
|
|
|
|
|
atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64))
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
if err := d.Write(hashFile, []byte(hash)); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to write .smm mod hash file: %w", err)
|
2022-06-07 23:36:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if updates != nil {
|
2023-12-07 16:57:31 +00:00
|
|
|
updates <- GenericProgress{Completed: totalSize, Total: totalSize}
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
|
2023-12-06 23:39:34 +00:00
|
|
|
func writeZipFile(outFileLocation string, file *zip.File, d disk.Disk, updates chan<- GenericProgress) error {
|
|
|
|
if updates != nil {
|
|
|
|
defer close(updates)
|
|
|
|
}
|
|
|
|
|
2022-06-22 22:24:35 +00:00
|
|
|
outFile, err := d.Open(outFileLocation, os.O_CREATE|os.O_RDWR)
|
2022-06-03 22:17:02 +00:00
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to write to file: %s: %w", outFileLocation, err)
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
inFile, err := file.Open()
|
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to process mod zip: %w", err)
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
2023-12-06 23:39:34 +00:00
|
|
|
defer inFile.Close()
|
|
|
|
|
2023-12-28 00:13:09 +00:00
|
|
|
progressInWriter := &Progresser{
|
2023-12-06 23:39:34 +00:00
|
|
|
Total: int64(file.UncompressedSize64),
|
|
|
|
Updates: updates,
|
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
|
2023-12-28 00:13:09 +00:00
|
|
|
if _, err := io.Copy(io.MultiWriter(outFile, progressInWriter), inFile); err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return fmt.Errorf("failed to write to file: %s: %w", outFileLocation, err)
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|