~welt/murse

murse/main.go -rw-r--r-- 3.3 KiB
db85e43dwelt guiprep: adjust logging 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"crypto/rsa"
	"database/sql"
	"log"
	"os"
	"path/filepath"
)

// If this is set we're doing a first-time installation
var install bool

// The developer public key
var publicKey *rsa.PublicKey

func init() {
	log.SetFlags(0)
}

func main() {
	if fileExists(lfd) && !opt.IgnoreLockfile {
		log.Fatal("Lockfile exists, are you running another installation? Re-run with -b to run regardless of the lockfile.")
	}

	if !fileExists(lmd) {
		install = true
	}

	m, err := get(opt.URL + "/ofmanifest.db")
	if err != nil {
		log.Fatal(err)
	}

	// In ofatomic, to ensure the files are legitimate it checks
	// *every file* for if the signature is correct. This is bad for
	// performance. Instead we verify the database and then trust its
	// hashes.
	if !opt.DisableSigning {
		ms, err := get(opt.URL + "ofmanifest.sig")
		if err != nil {
			log.Fatal(err)
		}
		if !verifySig(ms, m) {
			log.Fatal("invalid database signature")
		}
	}

	cd, err := os.UserCacheDir()
	if err != nil {
		log.Fatal("Failed to determine cache directory: " + err.Error())
	}

	mcd := filepath.Join(cd, "murse")
	tf := filepath.Join(mcd, "ofmanifest.db")

	err = os.WriteFile(tf, *m, 0777)
	if err != nil {
		log.Fatal(err)
	}

	db, err := sql.Open("sqlite", tf)
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	var lrev int
	var localFiles map[string]string
	if !install {
		err = manifest.read()
		if err != nil {
			log.Fatal(err)
		}

		localFiles, err = filesFromSQLite(db, manifest.DownloadedRevision)
		if err != nil {
			log.Fatal(err)
		}
		lrev = manifest.DownloadedRevision
	}

	nrev, err := latestRevisionFromSQL(db)
	if err != nil {
		log.Fatal(err)
	}

	newFiles, err := getRevisedFiles(db, lrev, nrev)
	if err != nil {
		log.Fatal(err)
	}

	if !fileExists(opt.Directory) {
		err := os.Mkdir(opt.Directory, 0700)
		if err != nil {
			log.Fatal("could not create game directory: " + err.Error())
		}
	}

	confdir := filepath.Join(opt.Directory, ".murse")

	if !fileExists(confdir) {
		err := os.Mkdir(confdir, 0700)
		if err != nil {
			log.Fatal("unable to create .murse directory: " + err.Error())
		}
	}

	lockInstall()
	defer unlockInstall()

	s, errs := startDownloadVerify(newFiles, localFiles)
	if !s {
		if len(errs) != 0 {
			for _, v := range errs {
				log.Println(v)
			}
			log.Fatal("Operation failed due to errors. Include this log if you file a bug report.")
		} else {
			log.Fatal("Download not completed.")
		}
		unlockInstall()
		os.Exit(1)

	}
	// Sadly ofatomic has no sense of files being removed
	//	if !install {
	//		remOutdated()
	//	}

	manifest.DownloadedRevision = lrev
	err = manifest.write()
	if err != nil {
		log.Fatal("Couldn't write manifest file, any custom files may be overwritten unless it is fixed: " + err.Error())
	}

	// Now we update the gameinfo.txt, so that the game will launch once everything
	// has been downloaded.
	if install {
		b, err := get("https://raw.githubusercontent.com/int-72h/ofatomic/main/ofatomic/gameinfo.txt")
		if err != nil {
			log.Fatal("Unable to download gameinfo.txt, game will not start: " + err.Error())
		}

		err = os.WriteFile(filepath.Join(opt.Directory, "gameinfo.txt"), *b, 0777)
		if err != nil {
			log.Fatal("Failed to write gameinfo.txt to disk: " + err.Error())
		}
	}

	log.Println("Completed successfully. Happy fragging!")
}