-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (98 loc) · 3.06 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
package main
import (
"flag"
"os"
"github.com/aaron-jencks/reversi/checkpoints"
"github.com/aaron-jencks/reversi/gameplay"
"github.com/aaron-jencks/reversi/utils"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
func main() {
p := message.NewPrinter(language.English)
var restore_file string = "../../checkpoint.bin"
var hash_string string = "simple"
var unhash_string string = "simple"
var bsize uint = 8
flag.StringVar(&restore_file, "file", restore_file, "specifies the file to parse")
flag.StringVar(&hash_string, "hash", hash_string, "specifies which hash function to use for rehashing the boards, must be within (simple, spiral, linear)")
flag.StringVar(&unhash_string, "unhash", unhash_string, "specifies which hash function to use for unhashing the boards, must be within (simple, spiral, linear)")
flag.UintVar(&bsize, "size", bsize, "specifies the size of the board")
flag.Parse()
var hash gameplay.BoardHashFunc
var unhash gameplay.BoardUnhashFunc
switch hash_string {
case "simple":
hash = gameplay.SimpleHash
case "spiral":
hash = gameplay.SpiralHash
case "linear":
hash = gameplay.LinearHash
default:
p.Printf("Unknown hash method: %s\n", hash_string)
return
}
switch unhash_string {
case "simple":
unhash = gameplay.SimpleUnhashBoard
case "spiral":
unhash = gameplay.SpiralUnhashBoard
case "linear":
unhash = gameplay.LinearUnhashBoard
default:
p.Printf("Unknown unhash method: %s\n", unhash_string)
return
}
stats, err := checkpoints.CheckpointStats(restore_file)
if err != nil {
p.Printf("Failed to read %s: %s\n", restore_file, err.Error())
return
}
p.Println(stats)
bcount := stats.CacheSize + stats.WalkerBoards
p.Printf("Converting %d boards\n", bcount)
fp, err := os.OpenFile(restore_file, os.O_RDWR, 0777)
if err != nil {
p.Printf("Failed to open checkpoint %s for reading and writing: %s\n", restore_file, err.Error())
return
}
defer fp.Close()
// reusable buffer for reading uint64 data
i128buff := make([]byte, 16)
// the file offset where the boards begin
bstart := int64(stats.VLen) + 56
// move to start of boards
_, err = fp.Seek(bstart, 0)
if err != nil {
p.Printf("Failed to reposition fp to %d: %s\n", bstart, err.Error())
return
}
// read in all of the boards
boards := make([]gameplay.Board, bcount)
for bi := range boards {
_, err := fp.Read(i128buff)
if err != nil {
p.Printf("Failed to read in board %d: %s\n", bi, err.Error())
return
}
boards[bi] = unhash(uint8(bsize), utils.Uint128FromBytes(i128buff))
p.Printf("\rUnhashed %d/%d boards", bi+1, bcount)
}
p.Println("\nFinished reading boards, rehashing")
// move back to start of boards
_, err = fp.Seek(bstart, 0)
if err != nil {
p.Printf("Failed to reposition fp to %d: %s\n", bstart, err.Error())
return
}
for bi, b := range boards {
bh := hash(b)
_, err := fp.Write(utils.Uint128ToBytes(bh))
if err != nil {
p.Printf("Failed to rewrite board %d: %s\n", bi, err.Error())
return
}
p.Printf("\rRehashed %d/%d boards", bi+1, bcount)
}
p.Println("\nFinished rehashing checkpoint file")
}