Skip to content

Commit 6294088

Browse files
qaisjpccw808
authored andcommitted
Refactor UTF8BOM checks out (on resource start)
1 parent 94dadac commit 6294088

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

MTA10/mods/shared_logic/lua/CLuaMain.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,25 +2014,11 @@ bool CLuaMain::LoadScriptFromBuffer ( const char* cpInBuffer, unsigned int uiInS
20142014
g_pClientGame->TellServerSomethingImportant( 1003, SStringX( "CLIENT SCRIPT ERROR: " ) + strMessage, false );
20152015
return false;
20162016
}
2017-
2018-
bool bUTF8;
2019-
2020-
// UTF-8 BOM? Compare by checking the standard UTF-8 BOM
2021-
if ( IsUTF8BOM ( cpBuffer, uiSize ) )
2022-
{
2023-
// If there's a BOM, load ignoring the first 3 bytes
2024-
bUTF8 = true;
2025-
cpBuffer += 3;
2026-
uiSize -= 3;
2027-
}
2028-
else
2029-
{
2030-
// Maybe not UTF-8, if we have a >80% heuristic detection confidence, assume it is
2031-
bUTF8 = ( GetUTF8Confidence ( (const unsigned char*) cpBuffer, uiSize ) >= 80 );
2032-
}
2017+
2018+
bool bUTF8 = CLuaShared::CheckUTF8BOMAndUpdate ( &cpBuffer, &uiSize );
20332019

20342020
// If compiled script, make sure correct chunkname is embedded
2035-
EmbedChunkName( strNiceFilename, &cpBuffer, &uiSize );
2021+
CLuaShared::EmbedChunkName( strNiceFilename, &cpBuffer, &uiSize );
20362022

20372023
if ( m_luaVM )
20382024
{

MTA10_Server/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,10 @@ bool CLuaMain::LoadScriptFromBuffer ( const char* cpInBuffer, unsigned int uiInS
307307
return false;
308308
}
309309

310-
bool bUTF8;
311-
312-
// UTF-8 BOM? Compare by checking the standard UTF-8 BOM
313-
if ( IsUTF8BOM( cpBuffer, uiSize ) )
314-
{
315-
// If there's a BOM, load ignoring the first 3 bytes
316-
bUTF8 = true;
317-
cpBuffer += 3;
318-
uiSize -= 3;
319-
}
320-
else
321-
{
322-
// Maybe not UTF-8, if we have a >80% heuristic detection confidence, assume it is
323-
bUTF8 = ( GetUTF8Confidence ( (const unsigned char*) cpBuffer, uiSize ) >= 80 );
324-
}
310+
bool bUTF8 = CLuaShared::CheckUTF8BOMAndUpdate ( &cpBuffer, &uiSize );
325311

326312
// If compiled script, make sure correct chunkname is embedded
327-
EmbedChunkName( strNiceFilename, &cpBuffer, &uiSize );
313+
CLuaShared::EmbedChunkName( strNiceFilename, &cpBuffer, &uiSize );
328314

329315
if ( m_luaVM )
330316
{

Shared/mods/deathmatch/logic/CLuaShared.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "StdInc.h"
1111

1212
// If compiled script, make sure correct chunkname is embedded
13-
void EmbedChunkName( SString strChunkName, const char** pcpOutBuffer, uint* puiOutSize )
13+
void CLuaShared::EmbedChunkName( SString strChunkName, const char** pcpOutBuffer, uint* puiOutSize )
1414
{
1515
const char*& cpBuffer = *pcpOutBuffer;
1616
uint& uiSize = *puiOutSize;
@@ -47,4 +47,27 @@ void EmbedChunkName( SString strChunkName, const char** pcpOutBuffer, uint* puiO
4747

4848
cpBuffer = store.GetData();
4949
uiSize = store.GetSize();
50+
}
51+
52+
bool CLuaShared::CheckUTF8BOMAndUpdate ( const char ** pcpOutBuffer, uint * puiOutSize )
53+
{
54+
const char*& cpBuffer = *pcpOutBuffer;
55+
uint& uiSize = *puiOutSize;
56+
bool bUTF8;
57+
58+
// UTF-8 BOM? Compare by checking the standard UTF-8 BOM
59+
if ( IsUTF8BOM ( cpBuffer, uiSize ) )
60+
{
61+
// If there's a BOM, load ignoring the first 3 bytes
62+
bUTF8 = true;
63+
cpBuffer += 3;
64+
uiSize -= 3;
65+
}
66+
else
67+
{
68+
// Maybe not UTF-8, if we have a >80% heuristic detection confidence, assume it is
69+
bUTF8 = ( GetUTF8Confidence ( (const unsigned char*) cpBuffer, uiSize ) >= 80 );
70+
}
71+
72+
return bUTF8;
5073
}

Shared/mods/deathmatch/logic/CLuaShared.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
* FILE: mods/shared_logic/CLuaShared.h
77
*
88
*****************************************************************************/
9-
10-
void EmbedChunkName ( SString strChunkName, const char** pcpOutBuffer, uint* puiOutSize );
9+
#pragma once
10+
static class CLuaShared
11+
{
12+
public:
13+
static void EmbedChunkName ( SString strChunkName, const char** pcpOutBuffer, uint* puiOutSize );
14+
static bool CheckUTF8BOMAndUpdate ( const char** pcpOutBuffer, uint* puiOutSize );
15+
};

0 commit comments

Comments
 (0)