forked from VANCOLD/fm_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FM_MAPDIR_API.sma
128 lines (97 loc) · 2.67 KB
/
FM_MAPDIR_API.sma
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
#include "feckinmad/fm_global"
new g_sMapDir[] = "maps/"
new Array:g_ValidMapList
new g_iValidMapCount
public plugin_natives()
{
register_native("fm_IsValidMap", "Native_IsValidMap")
register_native("fm_GetValidMapName", "Native_GetValidMapName")
register_native("fm_GetValidMapCount", "Native_GetValidMapCount")
register_native("fm_GetValidMapNameByIndex", "Native_GetValidMapNameByIndex")
register_library("fm_mapdir_api")
}
public plugin_precache()
{
g_ValidMapList = ArrayCreate(MAX_MAP_LEN)
ReadMapDir()
}
public plugin_init()
{
fm_RegisterPlugin()
}
public plugin_end()
ArrayDestroy(g_ValidMapList)
ReadMapDir()
{
new sMap[MAX_MAP_LEN], iLen
new iDirHandle = open_dir(g_sMapDir, sMap, charsmax(sMap))
if (!iDirHandle)
{
fm_WarningLog("Unable to open maps directory")
return 0
}
do {
iLen = strlen(sMap) - 4
if (iLen < 0)
continue
if(!equali(sMap[iLen], ".bsp"))
continue
sMap[iLen] = 0
ArrayPushString(g_ValidMapList, sMap)
g_iValidMapCount++
} while (next_file(iDirHandle, sMap, charsmax(sMap)))
close_dir(iDirHandle)
log_amx("Loaded %d valid maps from \"%s\"", g_iValidMapCount, g_sMapDir)
ArraySort(g_ValidMapList, "Handle_Compare")
return 1
}
public Handle_Compare(Array:List, iItem1, iItem2)
{
static sItem1[MAX_MAP_LEN]; ArrayGetString(List, iItem1, sItem1, charsmax(sItem1))
static sItem2[MAX_MAP_LEN]; ArrayGetString(List, iItem2, sItem2, charsmax(sItem2))
return strcmp(sItem1, sItem2, 1)
}
BinarySearch(sMap[], iLow, iHigh)
{
if (iHigh < iLow)
return -1 // Not found
new iMid = iLow + ((iHigh - iLow) / 2)
static sBuffer[MAX_MAP_LEN]; ArrayGetString(g_ValidMapList, iMid, sBuffer, charsmax(sBuffer))
new iRet = strcmp(sBuffer, sMap, 1)
if (iRet > 0)
return BinarySearch(sMap, iLow, iMid - 1)
else if (iRet < 0)
return BinarySearch(sMap, iMid + 1, iHigh)
return iMid // Found
}
public Native_IsValidMap()
{
new sMap[MAX_MAP_LEN]; get_string(1, sMap, charsmax(sMap))
return BinarySearch(sMap, 0, g_iValidMapCount - 1) != -1 ? 1 : 0
}
public Native_GetValidMapName()
{
new sMap[MAX_MAP_LEN]; get_string(1, sMap, charsmax(sMap))
new iIndex = BinarySearch(sMap, 0, g_iValidMapCount - 1)
if (iIndex != -1)
{
ArrayGetString(g_ValidMapList, iIndex, sMap, charsmax(sMap))
set_string(2, sMap, get_param(3))
return 1
}
return 0
}
public Native_GetValidMapCount()
return g_iValidMapCount
public Native_GetValidMapNameByIndex()
{
new iIndex = get_param(1)
if (iIndex < 0 || iIndex >= g_iValidMapCount)
{
log_error(AMX_ERR_NATIVE, "Invalid map index (%d)", iIndex)
return 0
}
new sMap[MAX_MAP_LEN]; ArrayGetString(g_ValidMapList, iIndex, sMap, charsmax(sMap))
set_string(2, sMap, get_param(3))
return 1
}