6099: Reduce duplicate code in editor and netban by extracting methods r=def- a=Robyt3

Duplicate code was found with [Duplo](https://github.com/dlidstrom/Duplo).

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-12-06 19:38:40 +00:00 committed by GitHub
commit ae17abe079
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 139 deletions

View file

@ -44,36 +44,13 @@ int CNetBan::CNetHash::MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17])
}
template<class T, int HashCount>
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Add(const T *pData, const CBanInfo *pInfo, const CNetHash *pNetHash)
void CNetBan::CBanPool<T, HashCount>::InsertUsed(CBan<T> *pBan)
{
if(!m_pFirstFree)
return 0;
// create new ban
CBan<T> *pBan = m_pFirstFree;
pBan->m_Data = *pData;
pBan->m_Info = *pInfo;
pBan->m_NetHash = *pNetHash;
if(pBan->m_pNext)
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
if(pBan->m_pPrev)
pBan->m_pPrev->m_pNext = pBan->m_pNext;
else
m_pFirstFree = pBan->m_pNext;
// add it to the hash list
if(m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash])
m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]->m_pHashPrev = pBan;
pBan->m_pHashPrev = 0;
pBan->m_pHashNext = m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash];
m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash] = pBan;
// insert it into the used list
if(m_pFirstUsed)
{
for(CBan<T> *p = m_pFirstUsed;; p = p->m_pNext)
{
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pBan->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER && pBan->m_Info.m_Expires <= p->m_Info.m_Expires))
{
// insert before
pBan->m_pNext = p;
@ -101,6 +78,35 @@ typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Add(const T *pData,
m_pFirstUsed = pBan;
pBan->m_pNext = pBan->m_pPrev = 0;
}
}
template<class T, int HashCount>
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Add(const T *pData, const CBanInfo *pInfo, const CNetHash *pNetHash)
{
if(!m_pFirstFree)
return 0;
// create new ban
CBan<T> *pBan = m_pFirstFree;
pBan->m_Data = *pData;
pBan->m_Info = *pInfo;
pBan->m_NetHash = *pNetHash;
if(pBan->m_pNext)
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
if(pBan->m_pPrev)
pBan->m_pPrev->m_pNext = pBan->m_pNext;
else
m_pFirstFree = pBan->m_pNext;
// add it to the hash list
if(m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash])
m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]->m_pHashPrev = pBan;
pBan->m_pHashPrev = 0;
pBan->m_pHashNext = m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash];
m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash] = pBan;
// insert it into the used list
InsertUsed(pBan);
// update ban count
++m_CountUsed;
@ -158,38 +164,7 @@ void CNetBan::CBanPool<T, HashCount>::Update(CBan<CDataType> *pBan, const CBanIn
m_pFirstUsed = pBan->m_pNext;
// insert it into the used list
if(m_pFirstUsed)
{
for(CBan<T> *p = m_pFirstUsed;; p = p->m_pNext)
{
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
{
// insert before
pBan->m_pNext = p;
pBan->m_pPrev = p->m_pPrev;
if(p->m_pPrev)
p->m_pPrev->m_pNext = pBan;
else
m_pFirstUsed = pBan;
p->m_pPrev = pBan;
break;
}
if(!p->m_pNext)
{
// last entry
p->m_pNext = pBan;
pBan->m_pPrev = p;
pBan->m_pNext = 0;
break;
}
}
}
else
{
m_pFirstUsed = pBan;
pBan->m_pNext = pBan->m_pPrev = 0;
}
InsertUsed(pBan);
}
void CNetBan::UnbanAll()

View file

@ -139,6 +139,8 @@ protected:
CBan<CDataType> *m_pFirstFree;
CBan<CDataType> *m_pFirstUsed;
int m_CountUsed;
void InsertUsed(CBan<CDataType> *pBan);
};
typedef CBanPool<NETADDR, 1> CBanAddrPool;

View file

@ -537,6 +537,13 @@ void CEditor::RenderGrid(CLayerGroup *pGroup)
Graphics()->LinesEnd();
}
void CEditor::SnapToGrid(float &x, float &y)
{
const int GridDistance = GetLineDistance() * m_GridFactor;
x = (int)((x + (x >= 0 ? 1.0f : -1.0f) * GridDistance / 2) / GridDistance) * GridDistance;
y = (int)((y + (y >= 0 ? 1.0f : -1.0f) * GridDistance / 2) / GridDistance) * GridDistance;
}
void CEditor::RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness)
{
Graphics()->TextureSet(Texture);
@ -1314,29 +1321,12 @@ void CEditor::DoSoundSource(CSoundSource *pSource, int Index)
{
if(s_Operation == OP_MOVE)
{
float x = wx;
float y = wy;
if(m_GridActive && !IgnoreGrid)
{
int LineDistance = GetLineDistance();
float x = 0.0f;
float y = 0.0f;
if(wx >= 0)
x = (int)((wx + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
x = (int)((wx - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
if(wy >= 0)
y = (int)((wy + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
y = (int)((wy - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
pSource->m_Position.x = f2fx(x);
pSource->m_Position.y = f2fx(y);
}
else
{
pSource->m_Position.x = f2fx(wx);
pSource->m_Position.y = f2fx(wy);
}
SnapToGrid(x, y);
pSource->m_Position.x = f2fx(x);
pSource->m_Position.y = f2fx(y);
}
}
@ -1443,48 +1433,20 @@ void CEditor::DoQuad(CQuad *pQuad, int Index)
// check if we only should move pivot
if(s_Operation == OP_MOVE_PIVOT)
{
float x = wx;
float y = wy;
if(m_GridActive && !IgnoreGrid)
{
int LineDistance = GetLineDistance();
float x = 0.0f;
float y = 0.0f;
if(wx >= 0)
x = (int)((wx + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
x = (int)((wx - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
if(wy >= 0)
y = (int)((wy + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
y = (int)((wy - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
pQuad->m_aPoints[4].x = f2fx(x);
pQuad->m_aPoints[4].y = f2fx(y);
}
else
{
pQuad->m_aPoints[4].x = f2fx(wx);
pQuad->m_aPoints[4].y = f2fx(wy);
}
SnapToGrid(x, y);
pQuad->m_aPoints[4].x = f2fx(x);
pQuad->m_aPoints[4].y = f2fx(y);
}
else if(s_Operation == OP_MOVE_ALL)
{
// move all points including pivot
float x = wx;
float y = wy;
// move all points including pivot
if(m_GridActive && !IgnoreGrid)
{
int LineDistance = GetLineDistance();
if(wx >= 0)
x = (int)((wx + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
x = (int)((wx - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
if(wy >= 0)
y = (int)((wy + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
y = (int)((wy - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
}
SnapToGrid(x, y);
int OffsetX = f2fx(x) - pQuad->m_aPoints[4].x;
int OffsetY = f2fx(y) - pQuad->m_aPoints[4].y;
@ -1688,18 +1650,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
float x = wx;
float y = wy;
if(m_GridActive && !IgnoreGrid)
{
int LineDistance = GetLineDistance();
if(wx >= 0)
x = (int)((wx + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
x = (int)((wx - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
if(wy >= 0)
y = (int)((wy + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
y = (int)((wy - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
}
SnapToGrid(x, y);
int OffsetX = f2fx(x) - pQuad->m_aPoints[V].x;
int OffsetY = f2fx(y) - pQuad->m_aPoints[V].y;
@ -2233,19 +2184,9 @@ void CEditor::DoQuadEnvPoint(const CQuad *pQuad, int QIndex, int PIndex)
{
if(m_GridActive && !IgnoreGrid)
{
int LineDistance = GetLineDistance();
float x = 0.0f;
float y = 0.0f;
if(wx >= 0)
x = (int)((wx + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
x = (int)((wx - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
if(wy >= 0)
y = (int)((wy + (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
else
y = (int)((wy - (LineDistance / 2) * m_GridFactor) / (LineDistance * m_GridFactor)) * (LineDistance * m_GridFactor);
float x = wx;
float y = wy;
SnapToGrid(x, y);
pEnvelope->m_vPoints[PIndex].m_aValues[0] = f2fx(x) - pQuad->m_aPoints[4].x;
pEnvelope->m_vPoints[PIndex].m_aValues[1] = f2fx(y) - pQuad->m_aPoints[4].y;
}

View file

@ -1092,6 +1092,7 @@ public:
void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness);
void RenderGrid(CLayerGroup *pGroup);
void SnapToGrid(float &x, float &y);
void UiInvokePopupMenu(void *pID, int Flags, float X, float Y, float W, float H, int (*pfnFunc)(CEditor *pEditor, CUIRect Rect, void *pContext), void *pContext = nullptr);
void UiDoPopupMenu();