From 4b3f7ac2fea9e32e48e3fbcd2910a355b0ed3a40 Mon Sep 17 00:00:00 2001 From: Sergei Marochkin Date: Wed, 19 Oct 2016 11:08:19 +0300 Subject: [PATCH] Fix record validation - Add INVALID_RECORD_ID constant - Renamed INVALID_MOVE_PATH to INVALID_MOVEPATH_ID - Update README --- README.md | 11 ++++++----- pawn/FCNPC.inc.in | 5 +++-- src/CPlayback.cpp | 32 ++++++++++++++++---------------- src/CPlayback.hpp | 2 +- src/CPlayerData.cpp | 10 +++++----- src/CRecordManager.cpp | 12 ++++++------ src/Common.h | 4 +++- 7 files changed, 40 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 9521c2e..73c04ab 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ make #define NODE_TYPE_VEHICLE (1) #define NODE_TYPE_BOAT (2) -#define INVALID_MOVE_PATH (-1) +#define INVALID_MOVEPATH_ID (-1) +#define INVALID_RECORD_ID (-1) ``` # Callbacks @@ -119,8 +120,8 @@ native Float:FCNPC_GetAngle(npcid); native FCNPC_SetQuaternion(npcid, Float:w, Float:x, Float:y, Float:z); native FCNPC_GiveQuaternion(npcid, Float:w, Float:x, Float:y, Float:z); native FCNPC_GetQuaternion(npcid, &Float:w, &Float:x, &Float:y, &Float:z); -native FCNPC_SetVelocity(npcid, Float:x, Float:y, Float:z); -native FCNPC_GiveVelocity(npcid, Float:x, Float:y, Float:z); +native FCNPC_SetVelocity(npcid, Float:x, Float:y, Float:z, bool:update_pos = false); +native FCNPC_GiveVelocity(npcid, Float:x, Float:y, Float:z, bool:update_pos = false); native FCNPC_GetVelocity(npcid, &Float:x, &Float:y, &Float:z); native FCNPC_SetInterior(npcid, interiorid); native FCNPC_GetInterior(npcid); @@ -227,12 +228,12 @@ native FCNPC_SetSurfingPlayerObject(npcid, objectid); native FCNPC_GetSurfingPlayerObject(npcid); native FCNPC_StopSurfing(npcid); -native FCNPC_StartPlayingPlayback(npcid, file[] = "", playbackid = -1, bool:auto_unload = false); +native FCNPC_StartPlayingPlayback(npcid, file[] = "", recordid = INVALID_RECORD_ID, bool:auto_unload = false); native FCNPC_StopPlayingPlayback(npcid); native FCNPC_PausePlayingPlayback(npcid); native FCNPC_ResumePlayingPlayback(npcid); native FCNPC_LoadPlayingPlayback(file[]); -native FCNPC_UnloadPlayingPlayback(playbackid); +native FCNPC_UnloadPlayingPlayback(recordid); native FCNPC_SetPlayingPlaybackPath(npcid, path[]); native FCNPC_GetPlayingPlaybackPath(npcid, path[], const size = sizeof(path)); diff --git a/pawn/FCNPC.inc.in b/pawn/FCNPC.inc.in index 3d122e6..157d738 100644 --- a/pawn/FCNPC.inc.in +++ b/pawn/FCNPC.inc.in @@ -38,7 +38,8 @@ public FCNPC_IncludeVersion = FCNPC_INCLUDE_VERSION; #define NODE_TYPE_VEHICLE (1) #define NODE_TYPE_BOAT (2) -#define INVALID_MOVE_PATH (-1) +#define INVALID_MOVEPATH_ID (-1) +#define INVALID_RECORD_ID (-1) // Callbacks forward FCNPC_OnCreate(npcid); @@ -202,7 +203,7 @@ native FCNPC_SetSurfingPlayerObject(npcid, objectid); native FCNPC_GetSurfingPlayerObject(npcid); native FCNPC_StopSurfing(npcid); -native FCNPC_StartPlayingPlayback(npcid, file[] = "", recordid = -1, bool:auto_unload = false); +native FCNPC_StartPlayingPlayback(npcid, file[] = "", recordid = INVALID_RECORD_ID, bool:auto_unload = false); native FCNPC_StopPlayingPlayback(npcid); native FCNPC_PausePlayingPlayback(npcid); native FCNPC_ResumePlayingPlayback(npcid); diff --git a/src/CPlayback.cpp b/src/CPlayback.cpp index 898c57a..bdf10aa 100644 --- a/src/CPlayback.cpp +++ b/src/CPlayback.cpp @@ -19,14 +19,14 @@ CPlayback::CPlayback(char *szFile, char *szPlayingPath, bool bAutoUnload) char szPath[MAX_PATH]; snprintf(szPath, sizeof(szPath), "%s%s.rec", szPlayingPath, szFile); m_iRecordId = pServer->GetRecordManager()->Load(szPath); - if (m_iRecordId == -1) { + if (m_iRecordId == INVALID_RECORD_ID) { logprintf("FCNPC Error: file '%s' not found.", szPath); } m_bAutoUnload = bAutoUnload; // Reset variables m_dwStartTime = 0; m_bPaused = false; - m_recordData = NULL; + m_pRecordData = NULL; m_iCurrentIndex = 0; } @@ -38,7 +38,7 @@ CPlayback::CPlayback(int iRecordId, bool bAutoUnload) // Reset variables m_dwStartTime = 0; m_bPaused = false; - m_recordData = NULL; + m_pRecordData = NULL; m_iCurrentIndex = 0; } @@ -52,27 +52,27 @@ CPlayback::~CPlayback() bool CPlayback::Initialize() { - if (m_iRecordId == -1) { + if (m_iRecordId == INVALID_RECORD_ID) { return false; } - m_recordData = pServer->GetRecordManager()->Get(m_iRecordId); + m_pRecordData = pServer->GetRecordManager()->Get(m_iRecordId); m_dwStartTime = GetTickCount(); return true; } bool CPlayback::Process(CPlayerData *pPlayerData) { - if (m_iCurrentIndex >= static_cast(m_recordData->v_dwTime.size())) { + if (m_iCurrentIndex >= static_cast(m_pRecordData->v_dwTime.size())) { return false; } // Dont process if its paused if (m_bPaused) { // Process the player - if (m_recordData->iPlaybackType == PLAYBACK_TYPE_DRIVER) { + if (m_pRecordData->iPlaybackType == PLAYBACK_TYPE_DRIVER) { // Set the state pPlayerData->SetState(PLAYER_STATE_DRIVER); // Pause the sync data - CVehicleSyncData *vehicleSyncData = &m_recordData->v_vehicleSyncData.at(m_iCurrentIndex); + CVehicleSyncData *vehicleSyncData = &m_pRecordData->v_vehicleSyncData.at(m_iCurrentIndex); vehicleSyncData->wUDAnalog = 0; vehicleSyncData->wLRAnalog = 0; vehicleSyncData->wKeys = 0; @@ -81,11 +81,11 @@ bool CPlayback::Process(CPlayerData *pPlayerData) pPlayerData->SetVehicleSync(vehicleSyncData); // Update the player pPlayerData->UpdateSync(UPDATE_STATE_DRIVER); - } else if (m_recordData->iPlaybackType == PLAYBACK_TYPE_ONFOOT) { + } else if (m_pRecordData->iPlaybackType == PLAYBACK_TYPE_ONFOOT) { // Set the state pPlayerData->SetState(PLAYER_STATE_ONFOOT); // Pause the sync data - CSyncData *playerSyncData = &m_recordData->v_playerSyncData.at(m_iCurrentIndex); + CSyncData *playerSyncData = &m_pRecordData->v_playerSyncData.at(m_iCurrentIndex); playerSyncData->wUDAnalog = 0; playerSyncData->wLRAnalog = 0; playerSyncData->wKeys = 0; @@ -96,16 +96,16 @@ bool CPlayback::Process(CPlayerData *pPlayerData) pPlayerData->UpdateSync(UPDATE_STATE_ONFOOT); } // Update the starting time - m_dwStartTime = (GetTickCount() - m_recordData->v_dwTime[m_iCurrentIndex]); + m_dwStartTime = (GetTickCount() - m_pRecordData->v_dwTime[m_iCurrentIndex]); return true; } // Check the time - if ((GetTickCount() - m_dwStartTime) >= m_recordData->v_dwTime[m_iCurrentIndex]) { + if ((GetTickCount() - m_dwStartTime) >= m_pRecordData->v_dwTime[m_iCurrentIndex]) { // Read the first recording data - if (m_recordData->iPlaybackType == PLAYBACK_TYPE_DRIVER) { + if (m_pRecordData->iPlaybackType == PLAYBACK_TYPE_DRIVER) { // Read the in car sync data CVehicleSyncData vehicleSyncData; - memcpy(&vehicleSyncData, &m_recordData->v_vehicleSyncData[m_iCurrentIndex], sizeof(CVehicleSyncData)); + memcpy(&vehicleSyncData, &m_pRecordData->v_vehicleSyncData[m_iCurrentIndex], sizeof(CVehicleSyncData)); // Get the vehicle interface CVehicle *pVehicle = pPlayerData->GetVehicle(); @@ -135,10 +135,10 @@ bool CPlayback::Process(CPlayerData *pPlayerData) // Update the player pPlayerData->Update(UPDATE_STATE_DRIVER); - } else if (m_recordData->iPlaybackType == PLAYBACK_TYPE_ONFOOT) { + } else if (m_pRecordData->iPlaybackType == PLAYBACK_TYPE_ONFOOT) { // Read the on foot sync data CSyncData syncData; - memcpy(&syncData, &m_recordData->v_playerSyncData[m_iCurrentIndex], sizeof(CSyncData)); + memcpy(&syncData, &m_pRecordData->v_playerSyncData[m_iCurrentIndex], sizeof(CSyncData)); // Apply the sync data pPlayerData->SetState(PLAYER_STATE_ONFOOT); diff --git a/src/CPlayback.hpp b/src/CPlayback.hpp index 30fa82a..8c16865 100644 --- a/src/CPlayback.hpp +++ b/src/CPlayback.hpp @@ -32,7 +32,7 @@ class CPlayback int m_iRecordId; DWORD m_dwStartTime; bool m_bPaused; - Record_t *m_recordData; + Record_t *m_pRecordData; int m_iCurrentIndex; }; diff --git a/src/CPlayerData.cpp b/src/CPlayerData.cpp index f7ee7f6..daf75e8 100644 --- a/src/CPlayerData.cpp +++ b/src/CPlayerData.cpp @@ -56,7 +56,7 @@ CPlayerData::CPlayerData(WORD playerId, char *szName) m_wSurfingInfo = 0; m_pWeaponInfo = new CWeaponInfo(); m_wMoveId = INVALID_PLAYER_ID; - m_iMovePath = INVALID_MOVE_PATH; + m_iMovePath = INVALID_MOVEPATH_ID; m_iMovePoint = 0; m_iMoveType = MOVE_TYPE_AUTO; m_fMoveRadius = 0.0f; @@ -1418,7 +1418,7 @@ void CPlayerData::StopMoving() // Reset moving flag m_bMoving = false; m_wMoveId = INVALID_PLAYER_ID; - m_iMovePath = INVALID_MOVE_PATH; + m_iMovePath = INVALID_MOVEPATH_ID; m_iMovePoint = 0; // Reset the player data SetVelocity(CVector(0.0f, 0.0f, 0.0f)); @@ -1443,12 +1443,12 @@ bool CPlayerData::IsMoving() bool CPlayerData::IsMovingAtPlayer(WORD wPlayerId) { - return m_bMoving && m_wMoveId == wPlayerId && m_wMoveId != INVALID_PLAYER_ID; + return m_bMoving && m_wMoveId != INVALID_PLAYER_ID && m_wMoveId == wPlayerId; } bool CPlayerData::IsMovingByMovePath(int iMovePath) { - return m_bMoving && m_iMovePath == iMovePath && m_iMovePath != INVALID_MOVE_PATH; + return m_bMoving && m_iMovePath != INVALID_MOVEPATH_ID && m_iMovePath == iMovePath; } void CPlayerData::ToggleReloading(bool bToggle) @@ -2021,7 +2021,7 @@ bool CPlayerData::StartPlayingPlayback(char *szFile, int iRecordId, bool bAutoUn } // Create a new playback instance - if (iRecordId != -1) { + if (iRecordId != INVALID_RECORD_ID) { m_pPlayback = new CPlayback(iRecordId, bAutoUnload); } else if (szFile) { m_pPlayback = new CPlayback(szFile, m_szPlayingPath, bAutoUnload); diff --git a/src/CRecordManager.cpp b/src/CRecordManager.cpp index dadc366..7f48601 100644 --- a/src/CRecordManager.cpp +++ b/src/CRecordManager.cpp @@ -14,7 +14,7 @@ int CRecordManager::Load(char *szFile) { // try to find file in memory int index = Find(szFile); - if (index != -1) { + if (index != INVALID_RECORD_ID) { return index; } @@ -23,7 +23,7 @@ int CRecordManager::Load(char *szFile) // Try to open the playback file fopen_s(&pFile, szFile, "rb"); if (!pFile) { - return -1; + return INVALID_RECORD_ID; } // Check the file size @@ -32,7 +32,7 @@ int CRecordManager::Load(char *szFile) fseek(pFile, 0, SEEK_SET); if (!sSize) { fclose(pFile); - return -1; + return INVALID_RECORD_ID; } // Read the playback type and file info ? @@ -43,7 +43,7 @@ int CRecordManager::Load(char *szFile) // Check the playback type if (iPlaybackType != PLAYBACK_TYPE_DRIVER && iPlaybackType != PLAYBACK_TYPE_ONFOOT) { fclose(pFile); - return -1; + return INVALID_RECORD_ID; } // add new record @@ -96,7 +96,7 @@ bool CRecordManager::Unload(int iRecordId) bool CRecordManager::IsValid(int iRecordId) { - return static_cast(m_vSyncData.size()) > iRecordId; + return iRecordId >= 0 && static_cast(m_vSyncData.size()) > iRecordId; } int CRecordManager::Find(char *szFile) @@ -106,7 +106,7 @@ int CRecordManager::Find(char *szFile) return std::distance(m_vSyncData.begin(), it); } } - return -1; + return INVALID_RECORD_ID; } Record_t *CRecordManager::Get(int iRecordId) diff --git a/src/Common.h b/src/Common.h index efffe10..4e915ea 100644 --- a/src/Common.h +++ b/src/Common.h @@ -58,7 +58,9 @@ extern logprintf_t logprintf; #define MOVE_SPEED_RUN 0.56444f #define MOVE_SPEED_SPRINT 0.926784f // Move path -#define INVALID_MOVE_PATH -1 +#define INVALID_MOVEPATH_ID -1 +// Record +#define INVALID_RECORD_ID -1 // Playback types #define PLAYBACK_TYPE_NONE 0 #define PLAYBACK_TYPE_DRIVER 1