⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tconsole.cpp

📁 mpq文件的格式就是一种压缩格式
💻 CPP
📖 第 1 页 / 共 3 页
字号:

int TConsole::ChangeArchive(const char * szMpq)
{
    char szMpqFile[MAX_PATH] = "";
    char * szTemp;
    int nError = ERROR_SUCCESS;

    // Open the MPQ archive
    GetFullPathName(szMpq, sizeof(szMpqFile), szMpqFile, &szTemp);
    if(stricmp(szMpqFile, m_szLastMpq))
    {
        CloseLastAccessedMpq();
        strcpy(m_szLastMpq, szMpq);

        if(!SFileCreateArchiveEx(m_szLastMpq, OPEN_EXISTING, 0, &m_hLastMpq))
        {
            nError = GetLastError();
            printf(IDS_CONSOLE_CANTOPENMPQ, szMpq, nError);
            CloseLastAccessedMpq();
        }
    }

    return nError;
}

int TConsole::OpenOrCreate(char ** Args, DWORD dwDisposition)
{
    char * szFilePart = NULL;
    DWORD dwHashTableSize = 0x1000;
    int nError = ERROR_SUCCESS;

    // Close last accessed archive, is some
    CloseLastAccessedMpq();

    if(Args[1] != NULL)
        dwHashTableSize = StrToInt(Args[1], NULL, 16);

    // Create MPQ Archive
    GetFullPathName(Args[0], sizeof(m_szLastMpq), m_szLastMpq, &szFilePart);
    if(!SFileCreateArchiveEx(m_szLastMpq, dwDisposition, dwHashTableSize, &m_hLastMpq))
        nError = GetLastError();

    // Report the result
    if(nError != ERROR_SUCCESS)
        printf(IDS_CONSOLE_CANTOCMPQ, Args[0], nError);
    else
        printf(IDS_CONSOLE_MPQOC);
    return nError;
}

BOOL TConsole::SaveCommand()
{
    char * szBuff;                      // Pointer to the text from edit box
    int nLength;
    
    nLength = GetWindowTextLength(m_hConsole);
    szBuff  = new char[nLength + 2];
    *szBuff = 0;
    GetWindowText(m_hConsole, szBuff, nLength + 1);
    
    memset(m_szCommand, 0, sizeof(m_szCommand));
    strncpy(m_szCommand, szBuff + m_nStart, sizeof(m_szCommand) - 1);
    m_nCmdLength = strlen(m_szCommand);
    delete szBuff;

    return (m_nCmdLength != 0);
}

int TConsole::_printf(const char * fmt, va_list argList)
{
    char * szBuff;                      // Pointer to the text from edit box
    char * szTemp;
    char * szTrg;
    char szText[512];
    int nAddLength;
    int nLength;

    // Create the result string
    nAddLength = vsprintf(szText, fmt, argList);

    // Count the newlines
    szTemp = szText;
    while((szTemp = strchr(szTemp, '\n')) != NULL)
    {
        nAddLength++;
        szTemp++;
    }

    // Disable the redraw
    m_bEnablePaint = FALSE;

    // Get the current window text length
    nLength = GetWindowTextLength(m_hConsole);
    if((nLength + nAddLength) > m_nMaxLength)
    {
        szTemp = szBuff = new char[nLength + 1];
        GetWindowText(m_hConsole, szBuff, nLength+1);
        while((nLength + nAddLength) > (m_nMaxLength - 0x1000))
        {
            szTemp = strchr(szTemp, '\x0D') + 2;
            if(szTemp == NULL)
                break;
            nLength = strlen(szTemp);
        }
        SetWindowText(m_hConsole, szTemp);
        SendMessage(m_hConsole, EM_SETSEL, nLength, nLength);
        delete szBuff;
    }

    // Copy the text, replace \n with \n\r
    szTrg = szBuff = new char [nAddLength + 1];
    for(szTemp = szText; *szTemp != 0; szTemp++)
    {
        if(*szTemp == '\n')
            *szTrg++ = '\r';
        *szTrg++ = *szTemp;
    }
    *szTrg = 0;
    m_bEnablePaint = TRUE;
    SendMessage(m_hConsole, EM_REPLACESEL, FALSE, (LPARAM)szBuff);
    delete szBuff;
    return nAddLength;

}

//-----------------------------------------------------------------------------
// Console command handlers

// Opens an existing MPQ archive
int TConsole::Open(char ** Args)
{
    return OpenOrCreate(Args, OPEN_ALWAYS);
}

// Closes a MPQ archive
int TConsole::New(char ** Args)
{
    return OpenOrCreate(Args, CREATE_ALWAYS);
}

int TConsole::Add(char ** Args)
{
    char   szCurDir[MAX_PATH];
    char * szToAdd = "*";               // File(s) to add
    BOOL  bWildCards = FALSE;
    BOOL  bRecurse = FALSE;
    int  nRelOffs = 0;                  // Offset of relative path name
    int  nError = ERROR_SUCCESS;

    // Get the file(s) to add
    if(Args[1] != NULL)
        szToAdd = Args[1];

    // Get the recurse flag
    for(int i = 0; Args[i] != NULL; i++)
    {
        if(!stricmp(Args[i], "/r"))
            bRecurse = TRUE;
    }
    // Get the WildCards flag
    if(strchr(szToAdd, '*') || strchr(szToAdd, '?'))
        bWildCards = TRUE;

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    if(nError == ERROR_SUCCESS)
    {
        GetCurrentDirectory(sizeof(szCurDir)-1, szCurDir);
        AddBackslash(szCurDir);
        nRelOffs = strlen(szCurDir);

        if(bRecurse)
            nError = AddFolder(m_hLastMpq, szToAdd, nRelOffs);
        else
            nError = AddFiles(m_hLastMpq, szToAdd, nRelOffs);
    }
    return nError;
}

// Extracts file(s) from the archive
int TConsole::Extract(char ** Args)
{
    SFILE_FIND_DATA wf;
    HANDLE hFind = NULL;
    char szTargetFile[MAX_PATH] = "";
    char * szTemp = NULL;
    BOOL bFullPath = FALSE;             // TRUE if to extract with the full path
    BOOL bResult = TRUE;
    int  nError = ERROR_SUCCESS;

    // Get the target directory
    if(Args[2] != NULL)
    {
        if(Args[2][0] != '/')
            strcpy(szTargetFile, Args[2]);
        if(!stricmp(Args[2], "/fp") || !stricmp(Args[3], "/fp"))
            bFullPath = TRUE;
    }

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    if(nError == ERROR_SUCCESS)
    {
        AddBackslash(szTargetFile);
        szTemp = szTargetFile + strlen(szTargetFile);

        // Find all files in the archive and extract them.
        hFind = SFileFindFirstFile(m_hLastMpq, Args[1], &wf, NULL);

        // If file find failed, try to extract the file directly
        if(hFind == NULL)
        {
            // Create the name of the target file
            // Ensure that the target path exists
            strcpy(szTemp, (bFullPath) ? Args[1] : GetPlainName(Args[1]));
            if((nError = ForcePathExist(szTargetFile)) != ERROR_SUCCESS)
                printf(IDS_CONSOLE_CANTCRFILE, szTargetFile, nError);
            nError = ExtractFile(m_hLastMpq, Args[1], szTargetFile, LANG_NEUTRAL);
        }
        else
        {
            while(bResult == TRUE)
            {
                // Create the name of the target file
                // Ensure that the target path exists
                strcpy(szTemp, (bFullPath) ? wf.cFileName : GetPlainName(wf.cFileName));
                if((nError = ForcePathExist(szTargetFile)) != ERROR_SUCCESS)
                    printf(IDS_CONSOLE_CANTCRFILE, szTargetFile, nError);

                // Extract the file and find the next one.
                ExtractFile(m_hLastMpq, wf.cFileName, szTargetFile, LANG_NEUTRAL);
                bResult = SFileFindNextFile(hFind, &wf);
            }
            SFileFindClose(hFind);
        }
    }
    return nError;
}

int TConsole::Rename(char ** Args)
{
    int nError = ERROR_SUCCESS;

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    // Rename the file
    if(nError == ERROR_SUCCESS)
    {
        if(!SFileRenameFile(m_hLastMpq, Args[1], Args[2]))
        {
            nError = GetLastError();
            printf(IDS_CONSOLE_CANTRENAME, Args[1], Args[2], nError);
        }
    }

    if(nError == ERROR_SUCCESS)
        printf(IDS_CONSOLE_RENAMED);
    return nError;
}

int TConsole::Move(char ** Args)
{
    char szNewName[MAX_PATH] = "";
    int nError = ERROR_SUCCESS;

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    if(nError == ERROR_SUCCESS)
    {
        // Create the name of the new file
        strcpy(szNewName, Args[2]);
        AddBackslash(szNewName);
        strcat(szNewName, GetPlainName(Args[1]));

        // "Move" the file
        if(!SFileRenameFile(m_hLastMpq, Args[1], szNewName))
        {
            nError = GetLastError();
            printf(IDS_CONSOLE_CANTMOVE, Args[1], Args[2], nError);
        }
    }

    if(nError == ERROR_SUCCESS)
        printf(IDS_CONSOLE_MOVED);
    return nError;
}

int TConsole::Delete(char ** Args)
{
    int nError = ERROR_SUCCESS;

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    if(nError == ERROR_SUCCESS)
    {
        if(!SFileRemoveFile(m_hLastMpq, Args[1]))
        {
            nError = GetLastError();
            printf(IDS_CONSOLE_CANTDEL, Args[1], nError);
        }
    }

    if(nError == ERROR_SUCCESS)
        printf(IDS_CONSOLE_DELETED);
    return nError;        
}

int TConsole::Flush(char ** Args)
{
    char * szListFile = Args[1];
    int nError = ERROR_SUCCESS;

    // Open the MPQ archive (If different from the last one)
    if(nError == ERROR_SUCCESS)
        nError = ChangeArchive(Args[0]);

    // If no listfile, extract the internal one
    if(nError == ERROR_SUCCESS && szListFile == NULL)
    {
        szListFile = LISTFILE_NAME;
        nError = ExtractFile(m_hLastMpq, szListFile, szListFile, LANG_NEUTRAL);
        if(nError != ERROR_SUCCESS)
            printf(IDS_CONSOLE_NOLISTFILE, m_szLastMpq);
    }

    if(nError == ERROR_SUCCESS)
    {
        printf(IDS_CONSOLE_COMPACTING);
        if(!SFileCompactArchive(m_hLastMpq, szListFile))
        {
            nError = GetLastError();
            printf(IDS_CONSOLE_CANTCOMPACT, nError);
        }
        else
            printf(IDS_CONSOLE_COMPACTED);
    }
    return nError;
}

int TConsole::Close(char ** /* Args */)
{
    if(m_hLastMpq != NULL)
    {
        CloseLastAccessedMpq();
        printf(IDS_CONSOLE_MPQCLOSED);
    }
    else
        printf(IDS_CONSOLE_NOMPQ);
    return ERROR_SUCCESS;
}

// Runs a script.
int TConsole::Script(char ** Args)
{
    char   szSaveDir[MAX_PATH] = "";
    char   szNewDir[MAX_PATH] = "";
    FILE * fp = NULL;
    char * szTemp;
    int    nError = ERROR_SUCCESS;

    // Set the working directory for the script
    if(nError == ERROR_SUCCESS)
    {
        strcpy(szNewDir, Args[0]);
        *GetPlainName(szNewDir) = 0;
        if(szNewDir[0] != 0)
        {
            GetCurrentDirectory(sizeof(szSaveDir), szSaveDir);
            if(!SetCurrentDirectory(szNewDir))
                nError = GetLastError();
        }
    }

    // Open the script file
    if(nError == ERROR_SUCCESS)
    {
        GetFullPathName(Args[0], sizeof(m_szScriptName), m_szScriptName, &szTemp);
        if((fp = fopen(Args[0], "rt")) == NULL)
        {
            printf(IDS_CONSOLE_EOPENFILE, Args[0]);
            nError = ERROR_FILE_NOT_FOUND;
        }
    }

    // Run the script file
    if(nError == ERROR_SUCCESS)
    {
        while(!feof(fp))
        {
            if(!ReadLine(fp, m_szCommand, sizeof(m_szCommand)-1))
                continue;
            ShowPrompt();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -