-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
445 lines (400 loc) · 13.2 KB
/
main.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package main
import (
"crypto/md5"
"errors" // standard console
"io"
"path/filepath"
"runtime"
"bytes"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
// to be notified when a repertory is modified (file creation ,deletion etc)
// Location of the game data storage directory.
// "." by default, it means that the tool executable is put directly in this directory.
var storageDirectory = "."
// Name of the log file. Will be written in the storage directory.
var logFileName = "rocksmithprofilesync.log"
// Location of the "cloud sync" folder.
// Located next to the storage directory by default if you use Syncthing.
// You may have to changed it if it's OneDrive, Dropbox etc.
var syncDirectory = "..\\Sync"
var filesHashMap = make(map[string]([]byte)) // Map referenced by the file names which keeps the MD5 hash of all files (profiles and localprofiles.json)
var filesLastAccessMap = make(map[string]time.Time)
var minPeriodBetweenFileEvents int64 = 700000000 // nanoseconds
var localProfileID []byte
// Open a files, and keep trying a few times if this file is already used by another process
func insistentOpen(filePath string) (*os.File, error) {
var numberOfTries = 60
file, err := os.Open(filePath)
pathErr, ok := err.(*os.PathError)
for numberOfTries > 0 && ok && pathErr != os.ErrExist && pathErr != os.ErrInvalid && pathErr != os.ErrNotExist && pathErr != os.ErrPermission && err != nil {
file, err = os.Open(filePath)
pathErr, ok = err.(*os.PathError)
numberOfTries--
// Sometimes the file can't be accessed because of syncthing-inotify locking the file
log.Printf("Erreur : le fichier est inaccessible. Nouvel essai (%d restant)...\n", numberOfTries)
time.Sleep(time.Second)
}
//log.Println("file :", file)
return file, err
}
func isProfileDataBaseFile(fileName string) bool {
var result = false
if strings.HasSuffix(fileName, "_prfldb") {
result = true
} else {
result = false
}
return result
}
func isLocalProfilesJSONFile(fileName string) bool {
var result = false
if fileName == "localprofiles.json" {
result = true
} else {
result = false
}
return result
}
func saveEventTime(fileName string) {
var eventTime = time.Now()
//eventTime = eventTime.Truncate(minPeriodBetweenFileEvents)
filesLastAccessMap[fileName] = eventTime
}
func eventIsTooRecent(fileName string) bool {
lastEventTime := filesLastAccessMap[fileName]
now := time.Now()
sinceLastEvent := now.Sub(lastEventTime)
var sinceLastEventNanoSec = sinceLastEvent.Nanoseconds()
var result = false
//log.Println("sinceLastEventNanoSec :", sinceLastEventNanoSec)
//log.Println("minPeriodBetweenFileEvents :", minPeriodBetweenFileEvents)
if sinceLastEventNanoSec <= minPeriodBetweenFileEvents {
//log.Println("Too recent")
result = true
} else {
//log.Println("Ok")
result = false
}
return result
}
func saveHash(fileName string, parentFolder string) error {
var result []byte
file, err := insistentOpen(parentFolder + string(filepath.Separator) + fileName)
if err != nil {
return err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return err
}
filesHashMap[fileName] = hash.Sum(result)
return nil
}
func hasSameHash(fileName string, parentFolder string) (bool, error) {
file, err := insistentOpen(parentFolder + string(filepath.Separator) + fileName)
if err != nil {
return false, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return false, err
}
var result []byte
fileHash := hash.Sum(result)
var boolresult = false
if bytes.Equal(fileHash, filesHashMap[fileName]) {
boolresult = true
} else {
boolresult = false
}
return boolresult, nil
}
func deleteHash(fileName string) {
delete(filesHashMap, fileName)
}
func importFile(fileName string, profileID []byte) error {
if len(profileID) != 4 {
err := errors.New("Longueur du profile ID incorrecte !")
return err
}
srcFile, err := insistentOpen(syncDirectory + string(filepath.Separator) + fileName)
if err != nil {
log.Fatal(err)
}
defer srcFile.Close()
dstFile, err := os.Create(storageDirectory + string(filepath.Separator) + fileName)
if err != nil {
log.Fatal(err)
}
defer dstFile.Close()
// Copy the first 8 bytes
var nbCopiedBytes int64 = 8
var buffer = make([]byte, 8)
nbReadBytes, err := srcFile.Read(buffer)
if err != nil || nbReadBytes != len(buffer) {
log.Fatal(err)
}
nbWrittenBytes, err := dstFile.Write(buffer)
if err != nil || nbWrittenBytes != len(buffer) {
log.Fatal(err)
}
// Ignore the 4-bytes long profile ID we don't need
var profileIDLength = 4
nbReadBytes, err = srcFile.Read(make([]byte, profileIDLength))
if nbReadBytes != profileIDLength {
err := errors.New("Incorrect profile ID length !")
return err
}
if err != nil {
log.Fatal(err)
}
// Copy the profile ID which belongs to this PC
nbWrittenBytes, err = dstFile.Write(profileID)
if nbWrittenBytes != 4 || err != nil {
log.Fatal(err)
}
nbCopiedBytes = nbCopiedBytes + 4
// Copy the rest of the file...
//srcReader := bufio.NewReader(srcFile)
//dstWriter := bufio.NewWriter(dstFile)
n, err := io.Copy(dstFile, srcFile)
nbCopiedBytes += n
if err != nil {
log.Fatal(err)
}
//log.Printf("%d copied bytes\n", nbCopiedBytes+12) // To delete after debugging
//dstFile.Write(b []byte)
//nbReadBytes, err = srcFile.ReadAt(profileID, 8)
//dstFile.Write(b []byte)
return nil
}
func exportFile(fileName string) error {
srcFile, err := insistentOpen(storageDirectory + string(filepath.Separator) + fileName)
if err != nil {
log.Fatal(err)
}
defer srcFile.Close()
dstFile, err := os.Create(syncDirectory + string(filepath.Separator) + fileName)
if err != nil {
log.Fatal(err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
log.Fatal(err)
}
//log.Println("File exported and now ready to be synchronized : ", fileName)
return nil
}
func hasProfileID(fileName string, parentFolder string, wantedProfileID []byte) (bool, error) {
if len(wantedProfileID) != 4 {
err := errors.New("Incorrect profile ID length !")
return false, err
}
testedFile, err := insistentOpen(storageDirectory + string(filepath.Separator) + fileName)
if err != nil {
log.Fatal(err)
}
defer testedFile.Close()
var testedProfileID = make([]byte, 4)
testedFile.ReadAt(testedProfileID, 8)
var result = false
if bytes.Equal(testedProfileID, wantedProfileID) {
result = true
} else {
result = false
}
return result, nil
}
func inSyncFolder(filePath string) bool {
parentFolder := filepath.Dir(filePath)
var result = false
if path.Clean(parentFolder) == path.Clean(syncDirectory) {
result = true
} else {
result = false
}
return result
}
func inStorageFolder(filePath string) bool {
parentFolder := filepath.Dir(filePath)
var result = false
if path.Clean(parentFolder) == path.Clean(storageDirectory) {
result = true
} else {
result = false
}
return result
}
func filesManager() {
filesWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer filesWatcher.Close()
err = filesWatcher.Add(syncDirectory)
if err != nil {
log.Fatal(err)
}
err = filesWatcher.Add(storageDirectory)
if err != nil {
log.Fatal(err)
}
for {
select {
case event := <-filesWatcher.Events:
//log.Println("Évènement :", event)
// Do something only if the file is a profile file or localprofiles.json
if isProfileDataBaseFile(filepath.Base(event.Name)) || isLocalProfilesJSONFile(filepath.Base(event.Name)) {
if eventIsTooRecent(filepath.Base(event.Name)) {
} else {
//log.Println("Event : ", event)
if (event.Op&fsnotify.Write == fsnotify.Write) || event.Op&fsnotify.Create == fsnotify.Create {
if inSyncFolder(event.Name) {
hasProfileID, _ := hasProfileID(event.Name, syncDirectory, localProfileID)
if hasProfileID {
// Don't do anything
} else {
importFile(filepath.Base(event.Name), localProfileID)
err = saveHash(filepath.Base(event.Name), syncDirectory)
if err != nil {
log.Fatal(err)
}
if event.Op&fsnotify.Create == fsnotify.Create {
if isProfileDataBaseFile(filepath.Base(event.Name)) {
log.Println("New profile created by another PC !", filepath.Base(event.Name))
} else if isLocalProfilesJSONFile(filepath.Base(event.Name)) {
log.Println("localprofiles.json file created by another PC")
}
} else {
if isProfileDataBaseFile(filepath.Base(event.Name)) {
log.Println("Profile has been updated from another PC: ", filepath.Base(event.Name))
} else if isLocalProfilesJSONFile(filepath.Base(event.Name)) {
log.Println("localprofiles.json has been updated from another PC")
}
}
}
} else if inStorageFolder(event.Name) {
hasSameHash, _ := hasSameHash(filepath.Base(event.Name), storageDirectory)
if hasSameHash {
// Don't do anything
} else {
exportFile(filepath.Base(event.Name))
if event.Op&fsnotify.Create == fsnotify.Create {
if isProfileDataBaseFile(filepath.Base(event.Name)) {
log.Println("New profile created by Rocksmith!", filepath.Base(event.Name))
} else if isLocalProfilesJSONFile(filepath.Base(event.Name)) {
log.Println("localprofiles.json created by Rocksmith")
}
} else {
if isProfileDataBaseFile(filepath.Base(event.Name)) {
log.Println("Profil has been updated by Rocksmith: ", filepath.Base(event.Name))
} else if isLocalProfilesJSONFile(filepath.Base(event.Name)) {
log.Println("localprofiles.json has been updated by Rocksmith")
}
}
}
}
// A file is deleted
} else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
if inSyncFolder(event.Name) {
oldPath := storageDirectory + string(filepath.Separator) + filepath.Base(event.Name)
newPath := oldPath + ".old"
os.Rename(oldPath, newPath)
deleteHash(filepath.Base(event.Name))
log.Println("File has been deleted by another PC (added \".old\" extension): ", filepath.Base(event.Name))
} else if inStorageFolder(event.Name) {
oldPath := syncDirectory + string(filepath.Separator) + filepath.Base(event.Name)
newPath := oldPath + ".old"
os.Rename(oldPath, newPath)
deleteHash(filepath.Base(event.Name))
if isProfileDataBaseFile(filepath.Base(event.Name)) {
log.Println("Profile has been deleted by Rocksmith (added \".old\" extension)! ", filepath.Base(event.Name))
} else if isLocalProfilesJSONFile(filepath.Base(event.Name)) {
log.Println("localprofiles.json has been deleted by Rocksmith (added \".old\" extension)! ", filepath.Base(event.Name))
}
}
}
saveEventTime(filepath.Base(event.Name))
}
}
case err := <-filesWatcher.Errors:
log.Println("Error: ", err)
}
}
}
func main() {
logFile, err := os.Create(logFileName)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
log.SetOutput(logFile)
localprofiles, err := insistentOpen(storageDirectory + string(filepath.Separator) + "localprofiles.json")
if err != nil {
log.Println("Make sure you put this executable in the \"Storage\" folder which contains the Rocksmith profiles.")
log.Fatal(err)
}
// Creates a profile ID variable with a length of 4 bytes, to get the profile ID from the localprofiles.json file
profileID := make([]byte, 4)
nbReadBytes, err := localprofiles.ReadAt(profileID, 8)
if nbReadBytes != 4 || err != nil {
log.Fatal("Problem with localprofiles.json\n", err)
}
localProfileID = profileID
log.Println("Analysing localprofiles.json...")
log.Printf("Profile ID found: 0x%08x\n", localProfileID)
err = localprofiles.Close()
if err != nil {
log.Fatal(err)
}
// Make a list of all files to be imported from Sync to Storage
syncFiles, _ := ioutil.ReadDir(syncDirectory)
log.Println("List of files to retrieve from the sync folder:")
var syncFilesToImport []os.FileInfo
for _, file := range syncFiles {
if isProfileDataBaseFile(file.Name()) || isLocalProfilesJSONFile(file.Name()) {
// Add these files to the list of files that must be imported
syncFilesToImport = append(syncFilesToImport, file)
}
}
for _, file := range syncFilesToImport {
log.Println(file.Name())
}
log.Println("Updating local profiles with the data from synchronized profiles:")
// Delete old profiles in the Storage folder (adding a ".old" to their name)
storageFiles, _ := ioutil.ReadDir(storageDirectory)
//log.Println("List of deleted files in the storage folder (added .old extension):")
for _, file := range storageFiles {
if strings.HasSuffix(file.Name(), "_prfldb") || file.Name() == "localprofiles.json" {
log.Println("Deleted file (.old):", file.Name())
err = os.Rename(file.Name(), file.Name()+".old")
if err != nil {
log.Fatal(err)
}
}
}
// Import the files from Sync to Storage
for _, file := range syncFilesToImport {
err := importFile(file.Name(), localProfileID)
if err != nil {
log.Fatal(err)
}
log.Println("Imported file: ", file.Name())
}
log.Println("Ready! make sure the ", syncDirectory, " folder is synchronized. Syncthing must be running.")
go filesManager()
for {
time.Sleep(time.Hour)
runtime.GC()
}
}