local dir = "./data/monster/"
local csdir = "./data/creaturescripts/"
local monstersXML = "monsters.xml"
local csXML = "creaturescripts.xml"
local csTag = '\n <event type="%t" name="%n" script="%s"/>' -- Tag format on creaturescripts.xml (%t = type, %n = name, %s = script)
local defaultText =
[[function onCreatureAppear(self, creature) return false end
function onCreatureDisappear(self, creature) return false end
function onCreatureMove(self, creature, oldPosition, newPosition) return false end
function onCreatureSay(self, creature, type, message) return false end
function onThink(self, interval) return false end]]
local events = {
["monsterdeath"] = {type = "death", file="monsterdeath.lua", defaultText=[[
function onDeath(cid, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
return true
end]]},
}
------- Do not edit after this if you don't know what you are doing.
function parseXML(file)
local ret = {["monster"] = {}, ["event"] = {}}
local data = ""
for line in file:lines() do
data = data .. line .. "\n"
line = line:match("<(.-)/>")
if line then
local element = line:match("^%s*(.-)%s")
if element == "monster" or element == "event" then
local t = {}
for i, x in line:gmatch('%s+(.-)="(.-)"') do
t[i] = x
end
table.insert(ret[element], t)
end
end
end
return ret, data:sub(1, #data-1)
end
function init()
----- Parsing monsters.xml -----
local monstersfile = io.open(dir .. monstersXML, "r")
print("Parsing XML " .. dir .. monstersXML .. "...")
local xmltable = parseXML(monstersfile)
monstersfile:close()
--------------------------------
-- Creating scripts directory --
local testfile = io.open(dir .. "scripts/test", "w")
if not testfile then
os.execute("mkdir data\\monster\\scripts")
print("Directory " .. dir .. "scripts has been created.")
else
testfile:close()
end
os.remove(dir .. "scripts/test")
---------------------------------
------ Creating default.lua -------
local defaultlua = io.open(dir .. "scripts/default.lua", "w")
defaultlua:write(defaultText)
defaultlua:close()
print("File " .. dir .. "scripts/default.lua has been created.")
---------------------------------
---- Modify all monsters XML! ----
local modified = 0
for i, contents in ipairs(xmltable["monster"]) do
local monsterxml = io.open(dir .. contents.file, "r")
if monsterxml then
local xmldata = monsterxml:read("*a")
local _mod = false
local s, f, content = xmldata:find("<monster%s(.-)>")
local r, p, cs = content:find('script="(.-)"')
monsterxml:close()
newc = content
if not cs then
xmldata = xmldata:gsub(content, newc .. ' script="default.lua"')
_mod = true
elseif cs ~= "default.lua" then
print('Warning: The file "' .. contents.file .. '" already has a script.')
end
local s, f, content = xmldata:find("<monster%s(.-)>")
local x, q, scriptcontent = xmldata:find("<script>(.-)</script>")
local readyevents = {}
local addevents = ""
if scriptcontent then
for line in scriptcontent:gmatch("(.-)\n") do
local event = line:match('<event%s-name="(.-)"')
if event then
readyevents[event] = true
end
end
end
for event, _c in pairs(events) do
if not readyevents[event] then
addevents = addevents .. '<event name="' .. event .. '"/>\n'
end
end
if addevents ~= "" then
_mod = true
if scriptcontent and scriptcontent:find("event") then
xmldata = xmldata:gsub(scriptcontent, scriptcontent .. addevents)
elseif x and q then
xmldata = xmldata:sub(1, x-1) .. xmldata:sub(q+1)
xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
else
xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
end
end
if xmldata then
if _mod then
modified = modified+1
end
local monsterxmlwrite = io.open(dir .. contents.file, "w")
monsterxmlwrite:write(xmldata)
monsterxmlwrite:close()
else
print("Error in: " .. contents.name)
end
else
print("Warning: Error opening file " .. contents.file)
end
end
print("Total monsters XML modified: " .. modified)
----------------------------------
--- Parsing creaturescripts.xml --
local csfile = io.open(csdir .. csXML, "r")
print("Parsing XML " .. csdir .. csXML .. "...")
local csxmltable, xmldata = parseXML(csfile)
csfile:close()
----------------------------------
--- Modify creaturescripts.xml ---
local _mod = false
local readytags = {}
for i, contents in ipairs(csxmltable["event"]) do
if events[contents.name] then
readytags[contents.name] = true
end
end
for event, contents in pairs(events) do
if not readytags[event] then
_mod = true
local currenttag = csTag
currenttag = currenttag:gsub("%%t", contents.type)
currenttag = currenttag:gsub("%%n", event)
currenttag = currenttag:gsub("%%s", contents.file)
local r, w = xmldata:find("<creaturescripts>")
xmldata = xmldata:sub(1, w) .. currenttag .. xmldata:sub(w+1)
end
end
if _mod then
local csfilewrite = io.open(csdir .. csXML, "w")
csfilewrite:write(xmldata)
csfilewrite:close()
print("Modified " .. csdir .. csXML)
end
----------------------------------
----- Creating event scripts -----
print("Creating lua creaturescripts...")
for event, contents in pairs(events) do
local luafile = io.open(csdir .. "scripts/" .. contents.file, "w")
luafile:write(contents.defaultText)
luafile:close()
end
----------------------------------
print("All the modifications were done.")
return true
end
local check = io.open("meventsinstall.lua", "r")
if not check then
local ret = init()
if ret then
local file = io.open("meventsinstall.lua", "w")
file:write("true")
file:close()
end
else
check:close()
end