[TFS 1.x] lib folder in "data" like 0.4

zbizu

Veteran OT User
Joined
Nov 22, 2010
Messages
2,460
Reaction score
1,356
Location
Poland
A simple loader for libs. It brings data\lib folder functionality from 0.4

installation:

create folder: "data\lib\"
add to global.lua:
Code:
for dir in io.popen([[dir "data\lib\" /b /aa]]):lines() do
  dofile('data/lib/'..dir..'')
end
after:
Code:
dofile('data/compat.lua')
it should look like this:


now you may put anything in lib folder and it will be loaded like in 0.4.
example lib:

04_compat.lua
Code:
getThingFromPos = getThingfromPos
getThingPosition = getThingPos
MESSAGE_LOOT = MESSAGE_INFO_DESCR
TALKTYPE_MONSTER = TALKTYPE_MONSTER_SAY
TALKTYPE_ORANGE_1 = TALKTYPE_MONSTER_SAY
TALKTYPE_ORANGE_2 = TALKTYPE_MONSTER_YELL

getCreatureStorage = getPlayerStorageValue
doCreatureSetStorage = setPlayerStorageValue
function getItemNameById(itemId) return ItemType(itemId):getName() end

string.explode = function (str, sep, limit)
if(type(sep) ~= 'string' or isInArray({tostring(str):len(), sep:len()}, 0)) then
   return {}
end

Special thanks to @Limos for showing me how to do it.
 
Last edited:

Limos

Premium User
Joined
Jun 7, 2010
Messages
10,017
Reaction score
2,948
Location
Netherlands
It's default/not TFS related Lua code so it also works on other (windows) servers btw, incase there are people who want to use it on other servers like 0.2.
 

Codinablack

Dreamer
Joined
Dec 26, 2013
Messages
1,216
Reaction score
372
Yes I seen that post and have been utilizing this method for a couple weeks now I believe. It's great, recommended for everyone for organization purposes, so easy to add libs
 

Limos

Premium User
Joined
Jun 7, 2010
Messages
10,017
Reaction score
2,948
Location
Netherlands
It uses windows commands so with linux it will be a bit different, unfortunately I can't test it since I only have windows.
 

Evil Hero

Legacy Member
Joined
Dec 12, 2007
Messages
1,132
Reaction score
491
Location
Germany
I've been working on an auto parser aswell for lua files, for your needs you can use this (it's tested and confirmed working)

this function will tell us which operation system we use:
Code:
function getOS()
   -- Unix, Linux varients
   fh,err = assert(io.popen("uname -o 2>/dev/null","r"))
   if fh then
     osname = fh:read()
   end
   if osname then
     return osname
   end
   return "windows"
end
the only problem is, if you use this function at windows it'll write in your console that it didn't found the file it's basicly an error but you can ignore it.

and this method will fetch all lua files in a certain directory (it also checks all subdirectories)
Code:
function loadLibs()
   if getOS() == "windows" then
     for path in io.popen([[cd "data\lib\" && dir *.lua /b/s]]):lines() do
       dofile(path)
     end
   else
     for path in io.popen("cd data/lib && find . -type f | grep .lua"):lines() do
       dofile(path)
     end
   end
end
if someone wants a c function for the getOS() then he can use this:
Code:
int32_t LuaScriptInterface::luaGetOS(lua_State* L)
{
   //getOS()
   std::string os = "unix";
#ifdef _WIN32
   os = "windows";
#endif
   pushString(L, os);
   return 1;
}
 
OP
zbizu

zbizu

Veteran OT User
Joined
Nov 22, 2010
Messages
2,460
Reaction score
1,356
Location
Poland
your function was good too, to make loaders for older versions of tfs without LuaJIT
 

Ramirow

Advanced OT User
Joined
Aug 22, 2009
Messages
453
Reaction score
167
Location
Argentina
I don't have that line in data/global.lua ("dofile('data/compat.lua')")

How should I proceed then? Using tfs 1.2
 
Top