Skip to content

Commit

Permalink
Add torrent edition
Browse files Browse the repository at this point in the history
  • Loading branch information
xackery committed Aug 23, 2023
1 parent 5202551 commit a00522f
Show file tree
Hide file tree
Showing 8 changed files with 822 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME ?= launcheq
VERSION ?= 0.0.9
VERSION ?= 0.0.10
FILELIST_URL ?= https://raw.githubusercontent.com/xackery/launcheq/rof
PATCHER_URL ?= https://github.com/xackery/launcheq/releases/latest/download/

Expand Down Expand Up @@ -53,4 +53,4 @@ build-windows:
@echo "Building Windows ${VERSION}"
go install github.com/akavel/rsrc@latest
rsrc -ico launcheq.ico -manifest launcheq.exe.manifest
GOOS=windows GOARCH=amd64 go build -buildmode=pie -ldflags="-X main.Version=${VERSION} -X main.PatcherUrl=${PATCHER_URL} -s -w" -o bin/${NAME}.exe
GOOS=windows GOARCH=amd64 go build -buildmode=pie -ldflags="-X main.Version=${VERSION} -X main.PatcherURL=${PATCHER_URL} -s -w" -o bin/${NAME}.exe
44 changes: 34 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"crypto/md5"
"embed"
"fmt"
"io"
"net/http"
Expand All @@ -19,6 +20,9 @@ import (
"github.com/fynelabs/selfupdate"
)

//go:embed rof2.torrent
var torrentContent embed.FS

// Client wraps the entire UI
type Client struct {
baseName string
Expand Down Expand Up @@ -68,19 +72,38 @@ func New(version string, patcherUrl string) (*Client, error) {
return c, nil
}

func (c *Client) Patch() {
start := time.Now()
isErrored := false
func (c *Client) PrePatch() {

_, err := os.Stat("eqgame.exe")
if err != nil {
fmt.Printf("eqgame.exe must be in the same directory as %s.\n", c.baseName)
fmt.Println("Automatically exiting in 10 seconds...")
time.Sleep(10 * time.Second)
os.Exit(1)
_, err = os.Stat("everquest_rof2/eqgame.exe")
if err == nil {
err = c.CopyBackup()
if err != nil {
fmt.Printf("Failed to copy from everquest_rof2: %s\n", err)
fmt.Println("Automatically exiting in 10 seconds...")
time.Sleep(10 * time.Second)
}
return
}

err = c.Torrent()
if err != nil {
fmt.Printf("Failed to download: %s\n", err)
fmt.Println("Automatically exiting in 10 seconds...")
time.Sleep(10 * time.Second)

os.Exit(1)
}
}
}

// Patch starts the patching process
func (c *Client) Patch() {
start := time.Now()
isErrored := false

err = c.selfUpdateAndPatch()
err := c.selfUpdateAndPatch()
if err != nil {
c.logf("Failed patch: %s", err)
isErrored = true
Expand Down Expand Up @@ -227,14 +250,15 @@ func (c *Client) selfUpdate() error {
return fmt.Errorf("read %s: %w", url, err)
}

remoteHash := strings.TrimSpace(string(data))
myHash = strings.ToUpper(strings.TrimSpace(myHash))
remoteHash := strings.ToUpper(strings.TrimSpace(string(data)))

if remoteHash == "Not Found" {
c.logf("Remote site down, ignoring self update")
return nil
}

if strings.ToUpper(myHash) == strings.ToUpper(remoteHash) {
if myHash == remoteHash {
c.logf("Self update not needed")
return nil
}
Expand Down
67 changes: 67 additions & 0 deletions client/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package client

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

func (c *Client) CopyBackup() error {
c.logf("Copying files from everquest_rof2...")
// copy all files in everquest_rof2 to current path
err := filepath.Walk("everquest_rof2", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

dst := strings.TrimPrefix(path, "everquest_rof2/")

fi, err := os.Stat(dst)
if err == nil {
// check if file mod date is newer and file size is around same
if fi.ModTime().After(info.ModTime()) && fi.Size() > info.Size()-100 && fi.Size() < info.Size()+100 {
return nil
}

}

r, err := os.Open(path)
if err != nil {
return fmt.Errorf("open %s: %w", path, err)
}
defer r.Close()

err = os.MkdirAll(filepath.Dir(dst), os.ModePerm)
if err != nil {
return fmt.Errorf("mkdir %s: %w", filepath.Dir(path), err)
}

w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create %s: %w", dst, err)
}
defer w.Close()

_, err = io.Copy(w, r)
if err != nil {
return fmt.Errorf("copy %s: %w", dst, err)
}

err = w.Sync()
if err != nil {
return fmt.Errorf("sync %s: %w", dst, err)
}
return nil
})
if err != nil {
return fmt.Errorf("walk: %w", err)
}

return nil
}
89 changes: 89 additions & 0 deletions client/rof2.torrent

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions client/torrent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package client

import (
"bytes"
"fmt"
"time"

"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"

"github.com/c2h5oh/datasize"
)

// Torrent downloads the torrent
func (c *Client) Torrent() error {
cfg := torrent.NewDefaultClientConfig()
cfg.DataDir = "."
cfg.Debug = false
cfg.Seed = false
torrentClient, err := torrent.NewClient(cfg)
if err != nil {
return fmt.Errorf("newClient: %w", err)
}

data, err := torrentContent.ReadFile("rof2.torrent")
if err != nil {
return fmt.Errorf("readFile %s: %w", "rof2.torrent", err)
}

mi, err := metainfo.Load(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("metainfo load: %w", err)
}
tr, err := torrentClient.AddTorrent(mi)
if err != nil {
return fmt.Errorf("addTorrent: %w", err)
}

start := time.Now()

<-tr.GotInfo()

defer tr.Drop()
go func() {
tick := time.NewTicker(6 * time.Second)

for {
select {
case <-tick.C:
st := tr.Stats()

dataRate := (datasize.ByteSize(float64(st.BytesRead.Int64())/time.Since(start).Seconds()) * datasize.B)
remainingTime := float64(tr.Info().TotalLength()) / float64(dataRate)

totalPercent := float64(tr.BytesCompleted()) / float64(tr.Info().TotalLength()) * float64(100)

fmt.Printf("peers: %d, seeders: %d, %s/s %0.2f%% of %s, ETA %0.1f minutes\n",
st.ActivePeers,
st.ConnectedSeeders,
dataRate.HR(),
totalPercent,
(datasize.ByteSize(tr.Info().TotalLength()) * datasize.B).HR(),
remainingTime/60)
case <-tr.Closed():
return
}
}
}()
c.logf("Downloading %s via Torrent", tr.Name())
tr.DownloadAll()
torrentClient.WaitAll()

err = c.CopyBackup()
if err != nil {
return fmt.Errorf("copyBackup: %w", err)
}

fmt.Printf("Finished in %0.2f seconds\n", time.Since(start).Seconds())

return nil
}
67 changes: 66 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,72 @@ require (
)

require (
crawshaw.io/sqlite v0.3.3-0.20220618202545-d1964889ea3c // indirect
github.com/RoaringBitmap/roaring v1.2.3 // indirect
github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
github.com/anacrolix/chansync v0.3.0 // indirect
github.com/anacrolix/dht/v2 v2.19.2-0.20221121215055-066ad8494444 // indirect
github.com/anacrolix/envpprof v1.2.1 // indirect
github.com/anacrolix/generics v0.0.0-20230428105757-683593396d68 // indirect
github.com/anacrolix/go-libutp v1.3.1 // indirect
github.com/anacrolix/log v0.14.0 // indirect
github.com/anacrolix/missinggo v1.3.0 // indirect
github.com/anacrolix/missinggo/perf v1.0.0 // indirect
github.com/anacrolix/missinggo/v2 v2.7.2-0.20230527121029-a582b4f397b9 // indirect
github.com/anacrolix/mmsg v1.0.0 // indirect
github.com/anacrolix/multiless v0.3.0 // indirect
github.com/anacrolix/stm v0.4.0 // indirect
github.com/anacrolix/sync v0.4.0 // indirect
github.com/anacrolix/torrent v1.52.5
github.com/anacrolix/upnp v0.1.3-0.20220123035249-922794e51c96 // indirect
github.com/anacrolix/utp v0.1.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/benbjohnson/immutable v0.3.0 // indirect
github.com/bits-and-blooms/bitset v1.2.2 // indirect
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/lispad/go-generics-tools v1.1.0 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/pion/datachannel v1.5.2 // indirect
github.com/pion/dtls/v2 v2.2.4 // indirect
github.com/pion/ice/v2 v2.2.6 // indirect
github.com/pion/interceptor v0.1.11 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.5 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.9 // indirect
github.com/pion/rtp v1.7.13 // indirect
github.com/pion/sctp v1.8.2 // indirect
github.com/pion/sdp/v3 v3.0.5 // indirect
github.com/pion/srtp/v2 v2.0.9 // indirect
github.com/pion/stun v0.3.5 // indirect
github.com/pion/transport v0.13.1 // indirect
github.com/pion/transport/v2 v2.0.0 // indirect
github.com/pion/turn/v2 v2.0.8 // indirect
github.com/pion/udp v0.1.4 // indirect
github.com/pion/webrtc/v3 v3.1.42 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
github.com/tidwall/btree v1.6.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opentelemetry.io/otel v1.8.0 // indirect
go.opentelemetry.io/otel/trace v1.8.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
Loading

0 comments on commit a00522f

Please sign in to comment.