AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [ H3LP ] Parsing BSP File (https://forums.alliedmods.net/showthread.php?t=313914)

DarthMan 01-27-2019 14:40

[ H3LP ] Parsing BSP File
 
Hello. I am trying to parse a bsp file to get the keys and values of each entity, but ti doesn't seem to work well. No text is parsed.

PHP Code:

stock int strtok(const char[] szTextchar[] szLeft, const int iLeftLenchar[] szRight, const int iRightLen, const char[] szToken, const int iTrimSpaces 0// from max_utils.inc
{
    
int iPosLen 0;
    
    
iPosLen StrContains(szTextszToken);
    
    if(!
iPosLen)
        return 
0;
    
    
SplitString(szTextszTokenszLeftiLeftLen);
    
    
strcopy(szRightiRightLenszText[iPosLen strlen(szToken)]);
    
    if(
iTrimSpaces)
    {
        
TrimString(szLeft);
        
TrimString(szRight);
    }
    
    return 
strlen(szText);
}

void ParseEntities()
{
    
char szMapName[128], szMapPath[512], szFileLine[2048];
    
int iEntID START_ENTITY;
    
    
GetCurrentMap(szMapNamecharsmax(szMapName));
    
    
FormatEx(szMapPathcharsmax(szMapPath), "maps/%s.bsp"szMapName);
    
    
Handle hFile OpenFile(szMapPath"r");
    
    if(
hFile == INVALID_HANDLE)
    {
        
LogError("Error parsing map '%s'!"szMapName);
        
        return;
    }
    
    while(!
IsEndOfFile(hFile) && ReadFileLine(hFileszFileLinecharsmax(szFileLine)))
    {
        if(
strlen(szFileLine) == 0)
            continue;
        
        if(
StrContains(szFileLine"\"classname\"") != -1)
        {
            if(
StrContains(szFileLine"\"hammerid\"") != -1// on Fortress Forever hammerid doesn't exist
                
continue;
            
            while(!
IsEndOfFile(hFile) && ReadFileLine(hFileszFileLinecharsmax(szFileLine)))
            {
                if(
strlen(szFileLine) == 0)
                    continue;
                
                if(
szFileLine[0] != '"' && szFileLine[0] != '{' && szFileLine[0] != '}')
                    break;
                
                if(
iEntID == START_ENTITY)
                    
PrintToServer(szFileLine);
                
                if(!
IsArrayValid(g_aEntVars[iEntID]))
                    
g_aEntVars[iEntID] = CreateArray(g_iArrayLeng_iArraySize);
                
                
char szLeft[128], szRight[128], szText[256];
                
strtok(szFileLineszLeftcharsmax(szLeft), szRightcharsmax(szRight), " ");
                
                
StripQuotes(szLeft);
                
StripQuotes(szRight);
                
                
FormatEx(szTextcharsmax(szText), "%s*%s"szLeftszRight);
                
                
PushArrayString(g_aEntVars[iEntID], szText);
                
                
// DEBUG
                
if(iEntID == START_ENTITY)
                    
PrintToServer(szText);
                
                if(
szFileLine[0] == '}')
                    
iEntID++;
            }
            
            break;
        }
    }
    
    
CloseHandle(hFile);



asherkin 01-27-2019 15:16

Re: [ H3LP ] Parsing BSP File
 
The OnLevelInit provides the extracted entity lump for the current map, you can load it as KeyValues.

DarthMan 01-27-2019 15:21

Re: [ H3LP ] Parsing BSP File
 
Quote:

Originally Posted by asherkin (Post 2636890)
The OnLevelInit provides the extracted entity lump for the current map, you can load it as KeyValues.

PHP Code:

Action OnLevelInit(const char[] szMapNamechar szMapEntities[2097152])
{
    
// ?


How would I do that exactly?
Is szMapEntities the char that holds the keys and values for each entity?

asherkin 01-27-2019 15:29

Re: [ H3LP ] Parsing BSP File
 
Quote:

Originally Posted by DarthMan (Post 2636892)
Is szMapEntities the char that holds the keys and values for each entity?

Yes.

https://sm.alliedmods.net/new-api/ke...portFromString

DarthMan 01-27-2019 15:38

Re: [ H3LP ] Parsing BSP File
 
Quote:

Originally Posted by asherkin (Post 2636893)

I did that for a test, and it doesn't seem to work.

PHP Code:

public Action OnLevelInit(const char[] szMapNamechar szMapEntities[2097152])
{
    if(!
StrEqual(szMapName"ff_xpress_b1"true))
        return 
Plugin_Continue;
    
    
char szMapPath[160];
    
    
FormatEx(szMapPathcharsmax(szMapPath), "%s-dump.txt"szMapName);
    
    
Handle hFile OpenFile(szMapPath"w");
    
    
WriteFileString(hFileszMapEntitiesfalse);
    
    
CloseHandle(hFile);
    
    return 
Plugin_Continue;


It's like the forward isn't called and I have #include <sdkhooks>
And I made it to only dump if the map is ff_xpress_b1.

DarthMan 01-27-2019 16:14

Re: [ H3LP ] Parsing BSP File
 
Quote:

Originally Posted by asherkin (Post 2636893)

Apparently berni said somewhere in the forum that it doesn't work (OnLevelInit forward), and that was sometimes in 2011.
Perhaps it just doesn't work on Fortress Forever and maybe other source games.
Seems to work fine on TF2 from what I read (forum posts from 2014).

Can the forward be fixed?

Read here too: https://forums.alliedmods.net/showpo...postcount=2660

Edit: I got it to work with #pragma dynamic 2097152


All times are GMT -4. The time now is 06:25.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.