Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor magic numbers #1151

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/game/client/gui/gamewindowmanagerscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1583,11 +1583,11 @@ GameWindow *Parse_Window(File *in_file, char *buffer)
GameWindow *GameWindowManager::Win_Create_From_Script(Utf8String filename, WindowLayoutInfo *info)
{
static char buffer[2096];
char path[260];
char path[PATH_MAX];
const char *fname = filename.Str();
GameWindow *window = nullptr;
memset(path, 0, sizeof(path));
strcpy(path, "Window\\");
memset(&path[8], 0, 0xFCu);
WindowLayoutInfo new_info;
Utf8String str;
Reset_Window_Stack();
Expand Down
4 changes: 2 additions & 2 deletions src/game/client/maputil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ bool Load_Map(Utf8String name)
char name_without_ext[PATH_MAX];

if (len >= 4) {
memset(name_without_ext, 0, PATH_MAX);
memset(name_without_ext, 0, sizeof(name_without_ext));
strncpy(name_without_ext, name_copy, len - 4);
}

Expand Down Expand Up @@ -499,7 +499,7 @@ unsigned int Calc_CRC(Utf8String dir, Utf8String name)
char name_without_ext[PATH_MAX];

if (len >= 4) {
memset(name_without_ext, 0, PATH_MAX);
memset(name_without_ext, 0, sizeof(name_without_ext));
strncpy(name_without_ext, name_copy, len - 4);
}

Expand Down
16 changes: 8 additions & 8 deletions src/game/logic/ai/aigroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,13 +718,13 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType
int i7 = 10000;
int i8 = 10000;

for (int j = 0; j < 3; j++) {
for (int j = 0; j < ARRAY_SIZE(counts1); j++) {
if (counts1[j] < i7) {
i7 = counts1[j];
}
}

for (int j = 0; j < 5; j++) {
for (int j = 0; j < ARRAY_SIZE(counts2); j++) {
if (counts2[j] < i8) {
i8 = counts2[j];
}
Expand All @@ -733,7 +733,7 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType
int i9 = 10000;
int i10 = -1;

for (int j = 0; j < 3; j++) {
for (int j = 0; j < ARRAY_SIZE(counts1); j++) {
if (counts1[j] == i7) {
int i11 = i6 + 1 - j;

Expand All @@ -756,7 +756,7 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType
i9 = 10000;
i10 = -1;

for (int j = 0; j < 5; j++) {
for (int j = 0; j < ARRAY_SIZE(counts2); j++) {
if (counts2[j] == i8) {
int i12 = i5 + 2 - j;

Expand Down Expand Up @@ -1204,12 +1204,12 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c
int i7 = 10000;
int i8 = 10000;

for (int j = 0; j < 3; j += 2) {
for (int j = 0; j < ARRAY_SIZE(counts1); j += 2) {
if (counts1[j] < i7) {
i7 = counts1[j];
}
}
for (int j = 0; j < 3; j++) {
for (int j = 0; j < ARRAY_SIZE(counts2); j++) {
if (counts2[j] < i8) {
i8 = counts2[j];
}
Expand All @@ -1218,7 +1218,7 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c
int i9 = 10000;
int i10 = -1;

for (int j = 0; j < 3; j += 2) {
for (int j = 0; j < ARRAY_SIZE(counts1); j += 2) {
if (counts1[j] == i7) {
int i11 = i6 + 1 - j;

Expand All @@ -1241,7 +1241,7 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c
i9 = 10000;
i10 = -1;

for (int j = 0; j < 3; j++) {
for (int j = 0; j < ARRAY_SIZE(counts2); j++) {
if (counts2[j] == i8) {
int i12 = i5 + 1 - j;

Expand Down
1 change: 1 addition & 0 deletions src/platform/audio/milesaudiofilecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ AudioDataHandle MilesAudioFileCache::Open_File(const AudioEventRTS *audio_event)
open_audio.wave_data = file_data;
}

static_assert(sizeof(open_audio.info) == sizeof(sound_info), "Expected equal");
memcpy(&open_audio.info, &sound_info, sizeof(sound_info));
open_audio.data_size = file_size;
open_audio.ref_count = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/w3dengine/client/heightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ int HeightMapRenderObjClass::Init_Height_Data(
if (map->Get_Extra_Alpha_UV_Data(x_pos, y_pos, u, v, alpha, &need_flip, &is_cliff)) {
if (m_numExtraBlendTiles >= m_extraBlendTilePositionsSize) {
unsigned int *tiles = new unsigned int[m_extraBlendTilePositionsSize + 512];
memcpy(tiles, m_extraBlendTilePositions, 4 * m_extraBlendTilePositionsSize);
memcpy(tiles, m_extraBlendTilePositions, sizeof(unsigned int) * m_extraBlendTilePositionsSize);
delete[] m_extraBlendTilePositions;
m_extraBlendTilePositions = tiles;
m_extraBlendTilePositionsSize += 512;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/w3dengine/client/worldheightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ bool WorldHeightMap::Parse_Blend_Tile_Data(DataChunkInput &file, DataChunkInfo *
file.Read_Byte_Array(reinterpret_cast<uint8_t *>(m_extraBlendTileNdxes), 2 * m_dataSize);

if (!g_theWriteableGlobalData->m_use3WayTerrainBlends) {
memset(m_extraBlendTileNdxes, 0, 2 * m_dataSize);
memset(m_extraBlendTileNdxes, 0, sizeof(short) * m_dataSize);
}
}

Expand Down Expand Up @@ -2033,7 +2033,7 @@ void WorldHeightMap::Setup_Alpha_Tiles()
if (!s_alphaTiles[0]) {
for (int i = 0; i < 12; i++) {
bool b[16];
memset(b, false, 16);
memset(b, false, sizeof(b));
int i2 = i;

if (i >= 6) {
Expand Down
2 changes: 1 addition & 1 deletion src/w3d/lib/ffactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ FileClass *SimpleFileFactoryClass::Get_File(const char *filename)
new_name.Format("%s%s", token, stripped_name.Peek_Buffer());
file->Set_Name(new_name);

if (file->Open(1)) {
if (file->Open(FM_READ)) {
file->Close();
break;
}
Expand Down
19 changes: 9 additions & 10 deletions src/w3d/renderer/assetmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ W3DAssetManager::W3DAssetManager() :
Register_Prototype_Loader(&s_sphereLoader);
Register_Prototype_Loader(&s_particleEmitterLoader);
m_prototypeHashTable = new PrototypeClass *[PROTOTYPE_HASH_TABLE_SIZE];
memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(uintptr_t));
memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(PrototypeClass *));
}

// 0x008143C0
Expand Down Expand Up @@ -191,7 +191,7 @@ void W3DAssetManager::Free_Assets()
}
}

memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(uintptr_t));
memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(PrototypeClass *));
m_hAnimManager.Free_All_Anims();
m_hTreeManager.Free_All_Trees();
Release_All_Textures();
Expand Down Expand Up @@ -219,7 +219,7 @@ void W3DAssetManager::Free_Assets_With_Exclusion_List(DynamicVectorClass<StringC
}

m_prototypes.Reset_Active();
memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(uintptr_t));
memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(PrototypeClass *));

for (int i = 0; i < vector.Count(); i++) {
Add_Prototype(vector[i]);
Expand Down Expand Up @@ -262,9 +262,9 @@ RenderObjClass *W3DAssetManager::Create_Render_Obj(const char *name)
char asset_filename[256]{};
auto *period = strchr(name, '.');
if (period == nullptr) {
snprintf(asset_filename, 256, "%s.w3d", name);
snprintf(asset_filename, ARRAY_SIZE(asset_filename), "%s.w3d", name);
} else {
snprintf(asset_filename, 256, "%s.w3d", period + 1);
snprintf(asset_filename, ARRAY_SIZE(asset_filename), "%s.w3d", period + 1);
}

if (Load_3D_Assets(asset_filename) == false) {
Expand Down Expand Up @@ -324,7 +324,7 @@ HAnimClass *W3DAssetManager::Get_HAnim(const char *name)
asset++;

char asset_filename[256]{};
snprintf(asset_filename, 256, "%s.w3d", asset);
snprintf(asset_filename, ARRAY_SIZE(asset_filename), "%s.w3d", asset);

if (Load_3D_Assets(asset_filename) == false) {
StringClass new_filename = StringClass{ "..\\", true } + asset_filename;
Expand Down Expand Up @@ -410,8 +410,7 @@ void W3DAssetManager::Release_All_Textures()
// 0x00815D90
void W3DAssetManager::Release_Unused_Textures()
{
constexpr int unused_textures_size = 256;
TextureClass *unused_textures[unused_textures_size]{};
TextureClass *unused_textures[256]{};
int unused_textures_count = 0;

for (HashTemplateIterator<StringClass, TextureClass *> texture(m_textureHash); !texture.Is_Done(); texture.Next()) {
Expand All @@ -421,7 +420,7 @@ void W3DAssetManager::Release_Unused_Textures()

unused_textures[unused_textures_count++] = texture.Peek_Value();

if (unused_textures_count < unused_textures_size) {
if (unused_textures_count < ARRAY_SIZE(unused_textures)) {
continue;
}

Expand Down Expand Up @@ -508,7 +507,7 @@ HTreeClass *W3DAssetManager::Get_HTree(const char *name)
}

char asset_filename[256]{};
snprintf(asset_filename, 256, "%s.w3d", name);
snprintf(asset_filename, ARRAY_SIZE(asset_filename), "%s.w3d", name);

if (Load_3D_Assets(asset_filename) == false) {
StringClass new_filename = StringClass{ "..\\", true } + asset_filename;
Expand Down
26 changes: 13 additions & 13 deletions src/w3d/renderer/dx8renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool Textures_Material_And_Shader_Booking_Struct::Add_Textures_Material_And_Shad
for (unsigned int i = 0; i < m_addedTypeCount; i++) {
bool b = true;

for (int j = 0; j < 2; j++) {
for (int j = 0; j < ARRAY_SIZE(m_addedTextures); j++) {
b = b && texs[j] == m_addedTextures[j][i];
}

Expand All @@ -91,7 +91,7 @@ bool Textures_Material_And_Shader_Booking_Struct::Add_Textures_Material_And_Shad
}

captainslog_assert(m_addedTypeCount < MAX_ADDED_TYPE_COUNT);
for (int k = 0; k < 2; k++) {
for (int k = 0; k < ARRAY_SIZE(m_addedTextures); k++) {
m_addedTextures[k][m_addedTypeCount] = texs[k];
}

Expand Down Expand Up @@ -207,7 +207,7 @@ TextureClass *Vertex_Split_Table::Peek_Texture(unsigned int pidx, unsigned int p
if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) {
return m_mmc->Peek_Texture(pidx, pass, stage);
} else {
captainslog_dbgassert(0, "GapFillerClass removed");
captainslog_dbgassert(false, "GapFillerClass removed");
return nullptr;
}
} else {
Expand All @@ -221,7 +221,7 @@ VertexMaterialClass *Vertex_Split_Table::Peek_Material(unsigned int pidx, unsign
if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) {
return m_mmc->Peek_Material(m_mmc->Get_Polygon_Array()[pidx][0], pass);
} else {
captainslog_dbgassert(0, "GapFillerClass removed");
captainslog_dbgassert(false, "GapFillerClass removed");
return nullptr;
}
} else {
Expand All @@ -235,7 +235,7 @@ ShaderClass Vertex_Split_Table::Peek_Shader(unsigned int pidx, unsigned int pass
if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) {
return m_mmc->Get_Shader(pidx, pass);
} else {
captainslog_dbgassert(0, "GapFillerClass removed");
captainslog_dbgassert(false, "GapFillerClass removed");
return ShaderClass();
}
} else {
Expand Down Expand Up @@ -471,7 +471,7 @@ DX8TextureCategoryClass::DX8TextureCategoryClass(
captainslog_assert(pass >= 0);
captainslog_assert(pass < DX8FVFCategoryContainer::MAX_PASSES);

for (int i = 0; i < 2; i++) {
for (int i = 0; i < ARRAY_SIZE(m_textures); i++) {
m_textures[i] = nullptr;
Ref_Ptr_Set(m_textures[i], texs[i]);
}
Expand All @@ -493,7 +493,7 @@ DX8TextureCategoryClass::~DX8TextureCategoryClass()
g_theDX8MeshRenderer.Unregister_Mesh_Type(r->Get_Mesh_Model_Class());
}

for (int i = 0; i < 2; i++) {
for (int i = 0; i < ARRAY_SIZE(m_textures); i++) {
Ref_Ptr_Release(m_textures[i]);
}

Expand All @@ -510,7 +510,7 @@ void DX8TextureCategoryClass::Add_Render_Task(DX8PolygonRendererClass *p_rendere

void DX8TextureCategoryClass::Render()
{
for (int i = 0; i < 2; i++) {
for (int i = 0; i < ARRAY_SIZE(m_textures); i++) {
DX8Wrapper::Set_Texture(i, Peek_Texture(i));
}

Expand Down Expand Up @@ -931,7 +931,7 @@ void DX8FVFCategoryContainer::Change_Polygon_Renderer_Texture(MultiListClass<DX8
DX8TextureCategoryClass *tc = Find_Matching_Texture_Category(new_texture, pass, stage, texcat);

if (tc == nullptr) {
TextureClass *tmp_textures[2];
TextureClass *tmp_textures[DX8TextureCategoryClass::TextureSize];
tmp_textures[0] = texcat->Peek_Texture(0);
tmp_textures[1] = texcat->Peek_Texture(1);
tmp_textures[stage] = new_texture;
Expand Down Expand Up @@ -1012,7 +1012,7 @@ void DX8FVFCategoryContainer::Change_Polygon_Renderer_Material(
DX8TextureCategoryClass *tc = Find_Matching_Texture_Category(new_vmat, pass, texcat);

if (tc == nullptr) {
TextureClass *tmp_textures[2];
TextureClass *tmp_textures[DX8TextureCategoryClass::TextureSize];
tmp_textures[0] = texcat->Peek_Texture(0);
tmp_textures[1] = texcat->Peek_Texture(1);
tc = new DX8TextureCategoryClass(this, tmp_textures, texcat->Get_Shader(), new_vmat, pass);
Expand Down Expand Up @@ -1132,7 +1132,7 @@ DX8TextureCategoryClass *DX8FVFCategoryContainer::Find_Matching_Texture_Category
DX8TextureCategoryClass *tc = dest_it.Peek_Obj();
bool b = true;

for (int i = 0; i < 2; i++) {
for (int i = 0; i < DX8TextureCategoryClass::TextureSize; i++) {
b = b && (tc->Peek_Texture(i) == ref_category->Peek_Texture(i));
}

Expand All @@ -1159,7 +1159,7 @@ DX8TextureCategoryClass *DX8FVFCategoryContainer::Find_Matching_Texture_Category
DX8TextureCategoryClass *tc = dest_it.Peek_Obj();
bool b = true;

for (int i = 0; i < 2; i++) {
for (int i = 0; i < DX8TextureCategoryClass::TextureSize; i++) {
if (stage != i) {
b = b && (tc->Peek_Texture(i) == ref_category->Peek_Texture(i));
}
Expand Down Expand Up @@ -1253,7 +1253,7 @@ void DX8FVFCategoryContainer::Generate_Texture_Categories(Vertex_Split_Table &sp
int old_used_indices = m_usedIndices;

for (int j = 0; j < polygon_count; j++) {
TextureClass *texs[2];
TextureClass *texs[DX8TextureCategoryClass::TextureSize];
texs[0] = split_table.Peek_Texture(j, i, 0);
texs[1] = split_table.Peek_Texture(j, i, 1);
VertexMaterialClass *vmat = split_table.Peek_Material(j, i);
Expand Down
3 changes: 3 additions & 0 deletions src/w3d/renderer/dx8renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class DX8TextureCategoryClass : public MultiListObjectClass
#else
static bool s_forceMultiply;
#endif

public:
static constexpr int TextureSize = ARRAY_SIZE(m_textures); // Thyme specific
};

class DX8FVFCategoryContainer : public MultiListObjectClass
Expand Down
20 changes: 10 additions & 10 deletions src/w3d/renderer/hlod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ bool HLodDefClass::SubObjectArrayClass::Save_W3D(ChunkSaveClass &csave)
W3dHLodSubObjectStruct subobjheader;
memset(&subobjheader, 0, sizeof(subobjheader));
subobjheader.BoneIndex = m_boneIndex[i];
strncpy(subobjheader.Name, m_modelName[i], 32);
subobjheader.Name[31] = 0;
strncpy(subobjheader.Name, m_modelName[i], ARRAY_SIZE(subobjheader.Name));
subobjheader.Name[ARRAY_SIZE(subobjheader.Name) - 1] = 0;
b = csave.Write(&subobjheader, sizeof(W3dHLodSubObjectStruct)) == sizeof(W3dHLodSubObjectStruct);
csave.End_Chunk();
}
Expand Down Expand Up @@ -336,10 +336,10 @@ W3DErrorType HLodDefClass::Save_Header(ChunkSaveClass &csave)
memset(&header, 0, sizeof(header));
header.Version = 0x10000;
header.LodCount = m_lodCount;
strncpy(header.Name, m_name, 16);
header.Name[15] = 0;
strncpy(header.HierarchyName, m_hierarchyTreeName, 16);
header.HierarchyName[15] = 0;
strncpy(header.Name, m_name, ARRAY_SIZE(header.Name));
header.Name[ARRAY_SIZE(header.Name) - 1] = 0;
strncpy(header.HierarchyName, m_hierarchyTreeName, ARRAY_SIZE(header.HierarchyName));
header.HierarchyName[ARRAY_SIZE(header.HierarchyName) - 1] = 0;

W3DErrorType error = W3D_ERROR_OK;

Expand Down Expand Up @@ -1226,8 +1226,8 @@ void HLodClass::Include_NULL_Lod(bool include)

float *costs = new float[m_lodCount];
float *values = new float[m_lodCount + 1];
memcpy(costs, &m_cost + 1, m_lodCount * 4);
memcpy(values, &m_value + 1, (m_lodCount * 4) + 4);
memcpy(costs, &m_cost + 1, m_lodCount * sizeof(float));
memcpy(values, &m_value + 1, (m_lodCount * sizeof(float)) + sizeof(float));

delete[] m_lod;
delete[] m_cost;
Expand All @@ -1253,8 +1253,8 @@ void HLodClass::Include_NULL_Lod(bool include)

float *costs = new float[m_lodCount];
float *values = new float[m_lodCount + 1];
memcpy(costs + 1, &m_cost, m_lodCount * 4);
memcpy(values + 1, &m_value, (m_lodCount * 4) + 4);
memcpy(costs + 1, &m_cost, m_lodCount * sizeof(float));
memcpy(values + 1, &m_value, (m_lodCount * sizeof(float)) + sizeof(float));

delete[] m_lod;
delete[] m_cost;
Expand Down
Loading
Loading