Forum

> > CS2D > Scripts > Read maps for a menu
Forums overviewCS2D overview Scripts overviewLog in to reply

English Read maps for a menu

12 replies
To the start Previous 1 Next To the start

old Read maps for a menu

Suprise
BANNED Off Offline

Quote
Hey, yo today i decided to update one of my script (guess which) and I need a function for it. I tried to use this script:
file cs2d Stream functions library for Lua
Not sure if it can do what i need.
Basically i want all maps in the "maps/" folder to be in an infinite menu. Can someone help me? (I have the infinite menu already)

old Re: Read maps for a menu

_Yank
User Off Offline

Quote
Ive already tried to do that. The unic problem is that the briefing files (map.txt) appears to.
I cant send you the source code of it because the computer when the file is stored dont turns on. If i get the PC back i tell you.

Admin/mod comment

Removed the random off-topic message from your post /DC

old Re: Read maps for a menu

archmage
User Off Offline

Quote
You'll need to get a table of the files in the map directory. This should work for GNU/Linux or Windows, but it's untested.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function get_map_table(d)
  local t = {}
  
  -- Detect OS
  if ( os.execute("uname -o") == "GNU/Linux" ) then
    -- We're on a GNU/Linux machine, will list only maps
    local s = os.execute("ls -aX maps/ | grep *.map")
    for w in string.gmatch(s, "[^\n]+") do
       t[#t+1] = w
     end
     return t
  else
     -- Assume Windows, will list all files even if they aren't maps  (you could filter it out by removing any strings that don't end with '.map')
     local s = os.execute("dir /B maps/")
     for w in string.gmatch(s, "[^\n]+") do
       t[#t+1] = w
     end
     return t
  end
end

old Re: Read maps for a menu

Suprise
BANNED Off Offline

Quote
fuck @user VADemon won the party.

That's really shows all the maps, now I'm trying to make all the maps to be shown in the menu.

@user VADemon: Can you help me with a menu example with parse("maps")?

old Re: Read maps for a menu

MikuAuahDark
User Off Offline

Quote
thread cs2d Load maps in a lua Menu

io.popen version(Windows)
1
2
3
4
5
6
7
8
9
10
11
function ScanMap(ext)
	local temp={}
	local lpstr=io.popen("dir /b maps\\")
	for w in lpstr:read("*a"):gmatch("[^\n]+") do
		if w:sub(-4)==".map" then
			table.insert(temp,ext==true and w:sub(1,-5) or w)
		end
	end
	lpstr:close()
	return temp
end

old Re: Read maps for a menu

VADemon
User Off Offline

Quote
1) Getting map list
• addhook cs2d lua hook log > function to save map names
• parse "maps"
• map list is saved by now (log hook), removehook log

2) By now every map name should be in the table, sorting into "official" map types (de_ zm_ etc.), everything else in alphabetical order
3) Use file cs2d [EngiN33R] UniMenu (with caching on server start)
4) PROFIT
I am lazy.

old Re: Read maps for a menu

EngiN33R
Moderator Off Offline

Quote
user VADemon has written
The magical all-in-one-line code:
1
parse("maps")
cs2d cmd maps

It's all well and good, except you also need to grab the output from the console. Still, it's not that difficult, and it's more universal and generally safer than using the system shell, so user VADemon wins a prize for thinking differently.

Try this, I tested it and it should work well.

EDIT: As per VADemon's suggestion, I shortened the code and generally made it a bit more elegant.

1
2
3
4
5
6
7
8
9
10
11
12
13
maplist = {}

function grabmaps(txt)
	if (txt ~= "----- Maps -----") then
		maplist[#maplist+1] = txt
	end
end

function showMaps()
	addhook("log","grabmaps")
	parse("maps")
	freehook("log","grabmaps")
end

Previous version >

old Re: Read maps for a menu

Suprise
BANNED Off Offline

Quote
It Works like a dream lolz
If anyone want to make something similiar in the future i used this piece of code:
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
os.execute('dir "maps" > tmp')

maps2={}

for file in io.lines("tmp") do
     if file:sub(-4) == ".map" then
          table.insert(maps2,string.sub(file,38,-5))
     end
end

os.execute('del tmp') --windows

addhook("say","_say")
function _say(id,txt)
	if txt == "!lol" then
		for _,map in ipairs(maps2) do
			msg(map) --showing that it works
		end
	end
end

function selectmenu(id, page)
	local page = page or 1
	local pages = math.ceil(#maps2 / 6)
	if page < 1 then page = pages end
	if page > pages then page = 1 end
	local m = 'Start a vote for a new map P'.. page
	for i = 6 * page - 5, 6 * page do
		if maps2[i] then m = m ..', '.. maps2[i] else m = m ..',' end
	end
	if page == pages then m = m ..',,<<- First page' else m = m ..',,Next page -->' end
	if page == 1 then m = m ..',Last page ->>' else m = m ..',<-- Previvius page' end
	menu(id, m)
end

Thanks all for help !
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview