Added a way to change the mod install directory (and changed the default to hack/mods) and added simpler way to add to init.lua

develop
Warmist 2014-06-02 17:57:59 +03:00
parent e4a6f13347
commit 49bbd41bc3
1 changed files with 47 additions and 5 deletions

@ -1,8 +1,29 @@
-- a graphical mod manager for df
local gui=require 'gui'
local widgets=require 'gui.widgets'
local entity_file=dfhack.getDFPath().."/raw/objects/entity_default.txt"
local init_file=dfhack.getDFPath().."/raw/init.lua"
local mod_dir=dfhack.getDFPath().."/hack/mods"
--[[ mod format: lua script that defines:
name - a name that is displayed in list
author - mod author, also displayed
description - mod description
OPTIONAL:
raws_list - a list (table) of file names that need to be copied over to df raws
patch_entity - a chunk of text to patch entity TODO: add settings to which entities to add
patch_init - a chunk of lua to add to lua init
patch_dofile - a list (table) of files to add to lua init as "dofile"
patch_files - a table of files to patch:
filename - a filename (in raws folder) to patch
patch - what to add
after - a string after which to insert
MORE OPTIONAL:
guard - a token that is used in raw files to find editions and remove them on uninstall
guard_init - a token for lua file
[pre|post]_(un)install - callback functions. Can trigger more complicated behavior
]]
function fileExists(filename)
local file=io.open(filename,"rb")
if file==nil then
@ -12,6 +33,10 @@ function fileExists(filename)
return true
end
end
if not fileExists(init_file) then
local initFile=io.open(initFileName,"a")
initFile:close()
end
function copyFile(from,to) --oh so primitive
local filefrom=io.open(from,"rb")
local fileto=io.open(to,"w+b")
@ -27,6 +52,16 @@ function patchInit(initFileName,patch_guard,code)
code,patch_guard[2]))
initFile:close()
end
function patchDofile( initFileName,patch_guard,dofile_list )
local initFile=io.open(initFileName,"a")
initFile:write(patch_guard[1].."\n")
for _,v in ipairs(dofile_list) do
local fixed_path=mod_dir:gsub("\\","/")
initFile:write(string.format("dofile('%s/%s')\n",fixed_path,v))
end
initFile:write(patch_guard[2].."\n")
initFile:close()
end
function patchFile(file_name,patch_guard,after_string,code)
local input_lines=patch_guard[1].."\n"..code.."\n"..patch_guard[2]
@ -109,15 +144,19 @@ manager=defclass(manager,gui.FramedScreen)
function manager:init(args)
self.mods={}
local mods=self.mods
local mlist=dfhack.internal.getDir("mods")
local mlist=dfhack.internal.getDir(mod_dir)
if #mlist==0 then
qerror("Mod directory not found! Are you sure it is in:"..mod_dir)
end
for k,v in ipairs(mlist) do
if v~="." and v~=".." then
local f,modData=pcall(dofile,"mods/".. v .. "/init.lua")
local f,modData=pcall(dofile,mod_dir.."/".. v .. "/init.lua")
if f then
mods[modData.name]=modData
modData.guard=modData.guard or {">>"..modData.name.." patch","<<End "..modData.name.." patch"}
modData.guard_init={"--"..modData.guard[1],"--"..modData.guard[2]}
modData.path=dfhack.getDFPath()..'/mods/'..v..'/'
modData.path=mod_dir.."/"..v..'/'
end
end
end
@ -254,7 +293,9 @@ function manager:install(trgMod,force)
if trgMod.patch_init then
patchInit(init_file,trgMod.guard_init,trgMod.patch_init)
end
if trgMod.patch_dofile then
patchDofile(init_file,trgMod.guard_init,trgMod.patch_dofile)
end
trgMod.installed=true
if trgMod.post_install then
@ -281,9 +322,10 @@ function manager:uninstall(trgMod)
unPatchFile(dfhack.getDFPath().."/raw/objects/"..v.filename,trgMod.guard)
end
end
if trgMod.patch_init then
if trgMod.patch_init or trgMod.patch_dofile then
unPatchFile(init_file,trgMod.guard_init)
end
trgMod.installed=false
if trgMod.post_uninstall then
trgMod.post_uninstall(args)