Skip to content

Commit

Permalink
Fix record validation
Browse files Browse the repository at this point in the history
- Add INVALID_RECORD_ID constant
- Renamed INVALID_MOVE_PATH to INVALID_MOVEPATH_ID
- Update README
  • Loading branch information
ziggi committed Oct 19, 2016
1 parent baef56e commit 4b3f7ac
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions pawn/FCNPC.inc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions src/CPlayback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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<int>(m_recordData->v_dwTime.size())) {
if (m_iCurrentIndex >= static_cast<int>(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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/CPlayback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
10 changes: 5 additions & 5 deletions src/CPlayerData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/CRecordManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand All @@ -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 ?
Expand All @@ -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
Expand Down Expand Up @@ -96,7 +96,7 @@ bool CRecordManager::Unload(int iRecordId)

bool CRecordManager::IsValid(int iRecordId)
{
return static_cast<int>(m_vSyncData.size()) > iRecordId;
return iRecordId >= 0 && static_cast<int>(m_vSyncData.size()) > iRecordId;
}

int CRecordManager::Find(char *szFile)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b3f7ac

Please sign in to comment.