Skip to content

Commit

Permalink
fix: never progress backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Dec 29, 2023
1 parent b1da443 commit c169e7e
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"sync"
"sync/atomic"

"github.com/satisfactorymodding/ficsit-cli/cli/disk"
)
Expand Down Expand Up @@ -69,15 +68,6 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates
}

totalExtracted := int64(0)
totalExtractedPtr := &totalExtracted

channelUsers := sync.WaitGroup{}

if updates != nil {
defer func() {
channelUsers.Wait()
}()
}

for _, file := range reader.File {
if !file.FileInfo().IsDir() {
Expand All @@ -87,26 +77,32 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates
return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
}

channelUsers := sync.WaitGroup{}

var fileUpdates chan GenericProgress
if updates != nil {
fileUpdates = make(chan GenericProgress)
channelUsers.Add(1)
beforeProgress := totalExtracted
go func() {
defer channelUsers.Done()
for fileUpdate := range fileUpdates {
updates <- GenericProgress{
Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed,
Completed: beforeProgress + fileUpdate.Completed,
Total: totalSize,
}
}
}()
}

if err := writeZipFile(outFileLocation, file, d, fileUpdates); err != nil {
channelUsers.Wait()
return err
}

atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64))
channelUsers.Wait()

totalExtracted += int64(file.UncompressedSize64)
}
}

Expand Down

0 comments on commit c169e7e

Please sign in to comment.