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
|
MAX_CODE_GEN_LENGTH = 50
# NOTE: This is experimental/advanced stuff.
def needs_partitioning? target
target[:value].to_s.length > MAX_CODE_GEN_LENGTH
end
def partition target
return [] unless needs_partitioning? target
if target[:value].is_a? GTK::OpenEntity
target[:value] = target[:value].hash
end
results = []
idx = 0
left, right = target[:value].partition do
idx += 1
idx.even?
end
left, right = Hash[left], Hash[right]
left = { value: left }
right = { value: right}
[left, right]
end
def add_partition target, path, aggregate, final_result
partitions = partition target
partitions.each do |part|
if needs_partitioning? part
if part[:value].keys.length == 1
first_key = part[:value].keys[0]
new_part = { value: part[:value][first_key] }
path.push first_key
add_partition new_part, path, aggregate, final_result
path.pop
else
add_partition part, path, aggregate, final_result
end
else
final_result << { value: { __path__: [*path] } }
final_result << { value: part[:value] }
end
end
end
def state_to_string state
parts_queue = []
final_queue = []
add_partition({ value: state.hash },
[],
parts_queue,
final_queue)
final_queue.reject {|i| i[:value].keys.length == 0}.map do |i|
i[:value].to_s
end.join("\n#==================================================#\n")
end
def state_from_string string
Kernel.eval("$load_data = {}")
lines = string.split("\n#==================================================#\n")
lines.each do |l|
puts "todo: #{l}"
end
GTK::OpenEntity.parse_from_hash $load_data
end
def test_save_and_load args, assert
args.state.item_1.name = "Jane"
string = state_to_string args.state
state = state_from_string string
assert.equal! args.state.item_1.name, state.item_1.name
end
def test_save_and_load_big args, assert
size = 1000
size.map_with_index do |i|
args.state.send("k#{i}=".to_sym, i)
end
string = state_to_string args.state
state = state_from_string string
size.map_with_index do |i|
assert.equal! args.state.send("k#{i}".to_sym), state.send("k#{i}".to_sym)
assert.equal! args.state.send("k#{i}".to_sym), i
assert.equal! state.send("k#{i}".to_sym), i
end
end
def test_save_and_load_big_nested args, assert
args.state.player_one.friend.nested_hash.k0 = 0
args.state.player_one.friend.nested_hash.k1 = 1
args.state.player_one.friend.nested_hash.k2 = 2
args.state.player_one.friend.nested_hash.k3 = 3
args.state.player_one.friend.nested_hash.k4 = 4
args.state.player_one.friend.nested_hash.k5 = 5
args.state.player_one.friend.nested_hash.k6 = 6
args.state.player_one.friend.nested_hash.k7 = 7
args.state.player_one.friend.nested_hash.k8 = 8
args.state.player_one.friend.nested_hash.k9 = 9
string = state_to_string args.state
state = state_from_string string
end
$gtk.reset 100
$gtk.log_level = :off
|