-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfile_handle.lua
215 lines (176 loc) · 6.97 KB
/
file_handle.lua
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
--[[ TO DO:
-- Clean construct_database of extraneous details
]]
files = require('files')
xml = require('xml')
function import_parse(file_name)
local path = '/data/export/'..file_name
import = files.new(path..'.xml', true)
parsed, err = xml.read(import)
if not parsed then
message(err or 'XML error: Unknown error.')
return
end
imported_database = construct_database(parsed)
merge_tables(database,imported_database)
-- Add nonblocks in for old version
for mob,players in pairs(database) do
for player,player_table in pairs(players) do
if player_table['defense'] and player_table['defense']['block'] and not player_table['defense']['nonblock'] then
player_table['defense']['nonblock'] = player_table['defense']['hit']
end
end
end
-- Add total_damage in for old version
for mob,players in pairs(database) do
for player,player_table in pairs(players) do
if not player_table.total_damage then
player_table.total_damage = find_total_damage(player,mob)
end
end
end
message('Parse ['..file_name..'] was imported to database!')
end
function export_parse(file_name)
if not windower.dir_exists(windower.addon_path..'data') then
windower.create_dir(windower.addon_path..'data')
end
if not windower.dir_exists(windower.addon_path..'data/export') then
windower.create_dir(windower.addon_path..'data/export')
end
local path = windower.addon_path..'data/export/'
if file_name then
path = path..file_name
else
path = path..os.date(' %H %M %S%p %y-%d-%m')
end
if windower.file_exists(path..'.xml') then
path = path..'_'..os.clock()
end
local f = io.open(path..'.xml','w+')
f:write('<database>\n')
--filter mobs
for mob,data in pairs(database) do
if check_filters('mob',mob) then
f:write(' <'..mob..'>\n')
f:write(to_xml(data,' '))
f:write(' </'..mob..'>\n')
end
end
f:write('</database>')
f:close()
message('Database was exported to '..path..'.xml!')
if get_filters()~="" then
message('Note that the database was filtered by [ '..get_filters()..' ]')
end
end
function to_xml(t,indent_string)
local indent = indent_string or ' '
local xml_string = ""
for key,value in pairs(t) do
key = tostring(key)
xml_string = xml_string .. indent .. '<'..key:gsub(" ","_")..'>'
if type(value)=='number' then
xml_string = xml_string .. value
xml_string = xml_string .. '</'..key:gsub(" ","_")..'>\n'
elseif type(value)=='table' then
xml_string = xml_string .. '\n' .. to_xml(value,indent..' ')
xml_string = xml_string .. indent .. '</'..key:gsub(" ","_")..'>\n'
end
end
return xml_string
end
---------------------------------------------------------
-- Function credit to the Windower Luacore config library
---------------------------------------------------------
function construct_database(node, settings, key, meta)
settings = settings or T{}
key = key or 'settings'
meta = meta
local t = T{}
if node.type ~= 'tag' then
return t
end
if not node.children:all(function(n)
return n.type == 'tag' or n.type == 'comment'
end) and not (#node.children == 1 and node.children[1].type == 'text') then
error('Malformatted settings file.')
return t
end
-- TODO: Type checking necessary? merge should take care of that.
if #node.children == 1 and node.children[1].type == 'text' then
local val = node.children[1].value
if node.children[1].cdata then
--meta.cdata:add(key)
return val
end
if val:lower() == 'false' then
return false
elseif val:lower() == 'true' then
return true
end
local num = tonumber(val)
if num ~= nil then
return num
end
return val
end
for child in node.children:it() do
if child.type == 'comment' then
meta.comments[key] = child.value:trim()
elseif child.type == 'tag' then
key = child.name
local childdict
if table.containskey(settings, key) then
childdict = table.copy(settings)
else
childdict = settings
end
t[child.name] = construct_database(child, childdict, key, meta)
end
end
return t
end
function log_data(player,mob,action_type,value,spellName)
if not logging then return end
if not windower.dir_exists(windower.addon_path..'data') then
windower.create_dir(windower.addon_path..'data')
end
if not windower.dir_exists(windower.addon_path..'data/log') then
windower.create_dir(windower.addon_path..'data/log')
end
if not windower.dir_exists(windower.addon_path..'data/log/'..windower.ffxi.get_player().name) then
windower.create_dir(windower.addon_path..'data/log/'..windower.ffxi.get_player().name)
end
local file = files.new('data/log/'..windower.ffxi.get_player().name..'/'..player..'_'..mob..'_'..action_type..'.log')
if not file:exists() then
file:create()
end
if not logs[player..'_'..mob..'_'..action_type] then
file:append(os.date('======= %H:%M:%S %p %m-%d-%y =======')..'\n')
logs[player..'_'..mob..'_'..action_type] = true
end
file:append('%s %s\n':format(spellName or '',value or ''))
end
--Copyright (c) 2013~2016, F.R
--All rights reserved.
--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are met:
-- * Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- * Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
-- * Neither the name of <addon name> nor the
-- names of its contributors may be used to endorse or promote products
-- derived from this software without specific prior written permission.
--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
--ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
--WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
--DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
--DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
--(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
--LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
--ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
--(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.