-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexapunks_solution_write.lua
More file actions
86 lines (82 loc) · 2.58 KB
/
exapunks_solution_write.lua
File metadata and controls
86 lines (82 loc) · 2.58 KB
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
require("exapunks_solution")
function ExapunksSolution:_write(outputStream)
outputStream:write(string.pack("<I4", self.version))
outputStream:write(string.pack("<s4", self.file_id.string)) --Replace with PB040?
outputStream:write(string.pack("<s4", self.name.string))
outputStream:write(string.pack("<I4", self.competition_wins))
outputStream:write(string.pack("<I4", self.redshift_program_size))
outputStream:write(string.pack("<I4", self.win_stats_count))
for i=1,self.win_stats_count,1 do
self.win_stats[i]:_write(outputStream)
end
outputStream:write(string.pack("<I4", self.exa_instances_count))
for i=1,self.exa_instances_count,1 do
self.exa_instances[i]:_write(outputStream)
end
end
function ExapunksSolution.WinValuePair:_write(outputStream)
outputStream:write(string.pack("<I4", self.type.value, self.value))
end
function ExapunksSolution.ExaInstance:_write(outputStream)
outputStream:write("\n")
outputStream:write(string.pack("<s4", self.name.string))
outputStream:write(string.pack("<s4", self.code.string))
outputStream:write(string.pack("<I1", self.editor_display_status.value))
outputStream:write(string.pack("<I1", self.memory_scope.value))
outputStream:write(self.bitmap)
end
---Save state to a stream
---@param outputStream file*
---@return boolean?
---@return exitcode?
---@return integer?
function ExapunksSolution:save_to(outputStream)
self:_write(outputStream)
return outputStream:close()
end
---Save state to a file
---@param filename string
---@return boolean?
---@return exitcode?
---@return integer?
function ExapunksSolution:save_to_file(filename)
local fileStream, err = io.open(filename, "w")
if err then return false, err end
return self:save_to(fileStream --[[@as file*]])
end
---Change code in the exa at index
---@param index integer
---@param filename string
function ExapunksSolution:import_code(index, filename)
local code = self.exa_instances[index].code
local isRedshift = self.redshift_program_size > 0
local keyList = {}
code.string = ""
-- Setup Redshift Counting
if isRedshift then
self.redshift_program_size = 0
keyList = {
"MARK",
"SEEK",
"COPY",
"ADDI",
"SUBI",
"MULI",
"DIVI",
"SWIZ",
"JUMP",
"FJMP",
"TJMP",
"VOID",
}
end
-- Loop over lines to remove tabs
for line in io.lines(filename) do
-- Count the line if it contains a key
for _,v in ipairs(keyList) do
if line:find(v) then self.redshift_program_size = self.redshift_program_size + 1 end
end
code.string = code.string..
line:gsub("^[\t]+", "").."\n"
end
end