Skip to content

Commit

Permalink
Merge pull request #256 from ko4life-net/refactor-duplications
Browse files Browse the repository at this point in the history
Refactor codebase for clarity, portability, and consistency improvements.
  • Loading branch information
stevewgr committed Nov 13, 2024
2 parents 758ec00 + dc0412d commit 7a6a7d6
Show file tree
Hide file tree
Showing 224 changed files with 549 additions and 8,395 deletions.
64 changes: 56 additions & 8 deletions src/common/N3Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,77 @@
#include <string>
#include <ranges>
#include <algorithm>
#include <filesystem>
#include <fstream>

namespace N3 {
namespace fs = std::filesystem;

#if defined(_WIN32) || defined(_WIN64)
#define VC_EXTRALEAN
#include <Windows.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#elif defined(__linux__)
#include <unistd.h>
#endif // #if defined(_WIN32) || defined(_WIN64)

namespace n3std {

inline fs::path get_app_path() {
static fs::path app_path = [] {
fs::path::value_type buff[512]{};
#if defined(_WIN32) || defined(_WIN64)
GetModuleFileNameW(NULL, buff, static_cast<DWORD>(std::size(buff)));
#elif defined(__APPLE__)
_NSGetExecutablePath(buff, (uint32_t *)sizeof(buff));
#elif defined(__linux__)
readlink("/proc/self/exe", buff, sizeof(buff) - 1);
#endif // #if defined(_WIN32) || defined(_WIN64)
return fs::path(buff);
}();

return app_path;
}

inline fs::path get_app_dir() {
static fs::path app_dir = get_app_path().parent_path();

return app_dir;
}

inline void log_file_write(std::string_view log_msg) {
static fs::path log_path = get_app_path().replace_extension(".log");

std::ofstream ofile(log_path, std::ios::app);
if (ofile.is_open()) {
ofile << log_msg;
}
}

inline void log_file_write(const char * log_msg) {
log_file_write(std::string_view(log_msg));
}

static bool iequals(const std::string_view & lhs, const std::string_view & rhs) {
auto to_lower{std::ranges::views::transform(::tolower)};
return std::ranges::equal(lhs | to_lower, rhs | to_lower);
}

static std::string BytesToHex(const uint8_t * const byData, const size_t iSize) {
if (!byData || iSize == 0) {
static std::string bytes_to_hex(const uint8_t * const data, const size_t size) {
if (!data || size == 0) {
return "<null>";
}

static const char * hex_digits = "0123456789ABCDEF";

std::string result;
result.reserve(iSize * 2);
for (size_t i = 0; i < iSize; ++i) {
result += hex_digits[(byData[i] & 0xF0) >> 4];
result += hex_digits[byData[i] & 0x0F];
result.reserve(size * 2);
for (size_t i = 0; i < size; ++i) {
result += hex_digits[(data[i] & 0xF0) >> 4];
result += hex_digits[data[i] & 0x0F];
}

return result;
}

} // namespace N3
} // namespace n3std
2 changes: 0 additions & 2 deletions src/common/StdBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@

#include <random>
#include <filesystem>

namespace fs = std::filesystem;
8 changes: 1 addition & 7 deletions src/engine/N3Base/DFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@

#pragma once

// TODO: Note that there is some weird mixture of files between engine and game.
// This file is specifically being used by the game's UIs that these UIs not necessarly
// belongs in engine and could be abstracted better, since they all together not being built
// when building the engine as a library.
// I added TODO instead of doing it now to avoid complications during refactoring of
// the directory structure.
#include "N3Base/N3Base.h"
#include "N3Base.h"

// Font creation flags
#define D3DFONT_BOLD 0x0001
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/JpegFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "JpegFile.h"
#include "N3Base/My_3DStruct.h"
#include "My_3DStruct.h"

#include <tchar.h>

Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/My_3DStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ const DWORD OBJ_DUMMY = 0x10000000;
const DWORD OBJ_EFFECT = 0x20000000;
const DWORD OBJ_ANIM_CONTROL = 0x40000000;

#include "CrtDbg.h"
#include <crtdbg.h>

#ifndef _DEBUG
#define __ASSERT(expr, expMessage) void(0)
Expand Down
3 changes: 2 additions & 1 deletion src/engine/N3Base/N3Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "N3Base.h"

#include "N3Mesh.h"
#include "N3VMesh.h"
#include "N3PMesh.h"
#include "N3FXPMesh.h"
#include "N3FXShape.h"
#include "N3Chr.h"
#include "N3Base.h"

#include <mmsystem.h>

//////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 1 addition & 7 deletions src/engine/N3Base/N3Eng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ CN3Eng::CN3Eng() {
exit(-1);
}

// 프로그램이 실행된 경로..
if (CN3Base::PathGet().empty()) {
char szPath[256];
char szDrive[_MAX_DRIVE], szDir[_MAX_DIR];
::GetModuleFileName(NULL, szPath, 256);
_splitpath(szPath, szDrive, szDir, NULL, NULL);
sprintf(szPath, "%s%s", szDrive, szDir);
CN3Base::PathSet(szPath); // 경로 설정..
CN3Base::PathSet(n3std::get_app_dir().string());
}

#ifdef _N3GAME
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/N3EngTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "N3Base/N3Eng.h"
#include "N3Eng.h"

struct __EXPORT_OPTION {
char szID[8]; // ID "N3Scene1"
Expand Down
4 changes: 2 additions & 2 deletions src/engine/N3Base/N3FXBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "N3FXPartMesh.h"
#include "N3FXPartBottomBoard.h"

#include "N3Base/N3SndMgr.h"
#include "N3Base/N3SndObj.h"
#include "N3SndMgr.h"
#include "N3SndObj.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/N3PMeshCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "N3PMeshCreate.h"
#include "N3PMesh.h"
#include "N3PMeshInstance.h"
#include "N3Base/N3Mesh.h"
#include "N3Mesh.h"

// Maximum buffer sizes. For greater flexibility, the buffers should reallocate dynamically.
#define MAX_COLLAPSES 5000
Expand Down
4 changes: 2 additions & 2 deletions src/engine/N3Base/N3SkyMng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#include "N3Sun.h"
#include "N3Cloud.h"
#include "N3Star.h"
#include "mmsystem.h"
#include "N3Texture.h"

#include "N3GERain.h"
#include "N3GESnow.h"

Expand All @@ -20,6 +18,8 @@
#include "N3SndMgr.h"
#endif // #ifdef _N3GAME

#include <mmsystem.h>

typedef typename std::vector<__SKY_DAYCHANGE>::iterator it_SDC;

//////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/N3SndObj2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "StdAfx.h"
#include "N3SndMgr.h"
#include "N3SndObj.h"
#include "N3Base/N3Base.h"
#include "N3Base.h"
#include <math.h>

//////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/N3Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "N3Texture.h"

#ifdef _N3TOOL
#include "BitmapFile.h"
#include "BitMapFile.h"
#endif // #ifdef _N3TOOL

//////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/Pick.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "N3Base/N3Mesh.h"
#include "N3Mesh.h"

class CPick {
public:
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/StreamSoundObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "N3SndEng.h"
#include "StreamSoundObj.h"
#include "N3SndMgr.h"
#include "N3Base/N3Base.h"
#include "N3Base.h"

CStreamSoundObj::CStreamSoundObj() {
m_WaveSize = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/N3Base/WaveFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#pragma once

#include <windows.h>
#include <Windows.h>
#include <mmsystem.h>
#include <mmreg.h>

Expand Down
2 changes: 1 addition & 1 deletion src/game/Compress.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Compress.cpp : implementation of the CMainFrame class
//
#include "StdAfx.h"
#include "implode.h"
#include "Implode.h"
#include "Compress.h"

#define DO_CRC_INSTREAM 1
Expand Down
2 changes: 1 addition & 1 deletion src/game/GameProcMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "UISkillTreeDlg.h"
#include "UIHotKeyDlg.h"
#include "UIClassChange.h"
#include "UINpcEvent.h"
#include "UINPCEvent.h"
#include "UIItemExchange.h"
#include "UIRepairTooltipDlg.h"
#include "UINpcTalk.h"
Expand Down
6 changes: 3 additions & 3 deletions src/game/GameProcedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

#include "GameDef.h"
#include "GameEng.h"
#include "packetdef.h"
#include "PacketDef.h"
#include "LocalInput.h"
#include "APISocket.h"
#include "UIMessageBox.h"
#include "UIMessageBoxManager.h"
#include "UIManager.h"

#include "N3FXMgr.h"
#include "PlayerMyself.h"
#include "PlayerMySelf.h"
#include "GameProcedure.h"
#include "GameProcLogIn.h"
//#include "GameProcStart.h"
Expand All @@ -40,7 +40,7 @@
#include "N3Base/N3TableBase.h"
#include "N3Base/N3FXBundle.h"

#include "N3Base/BitmapFile.h"
#include "N3Base/BitMapFile.h"
#include "N3Base/Jpeg.h"
#include "N3Base/JpegFile.h"

Expand Down
2 changes: 1 addition & 1 deletion src/game/N3FXMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "StdAfx.h"
#include "N3FXMgr.h"
#include "GameBase.h"
#include "GameProcmain.h"
#include "GameProcMain.h"
#include "GameProcedure.h"
#include "PlayerOtherMgr.h"
#include "PlayerNPC.h"
Expand Down
2 changes: 1 addition & 1 deletion src/game/UIClassChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "APISocket.h"
#include "UIVarious.h"

#include "UIHotkeyDlg.h"
#include "UIHotKeyDlg.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
Expand Down
2 changes: 1 addition & 1 deletion src/game/UICmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "GameProcMain.h"
#include "PlayerOtherMgr.h"
//#include "GameProcLogIn.h"
#include "PlayerMyself.h"
#include "PlayerMySelf.h"
#include "UITransactionDlg.h"
#include "N3Base/N3UIButton.h"
#include "UIManager.h"
Expand Down
2 changes: 1 addition & 1 deletion src/game/UIDroppedItemDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "LocalInput.h"
#include "APISocket.h"
#include "GameProcMain.h"
#include "PlayerMyself.h"
#include "PlayerMySelf.h"

#include "N3UIWndBase.h"
#include "UIImageTooltipDlg.h"
Expand Down
3 changes: 1 addition & 2 deletions src/game/UIMessageBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
#include "UIPartyBBS.h"
#include "GameEng.h"
#include "GameProcedure.h"
#include "GameProcLogin.h"
#include "GameProcLogIn.h"
#include "LocalInput.h"
#include "UIMessageBoxManager.h"
#include "SubProcPerTrade.h"
#include "ShellApi.h"

#include "N3Base/N3UIButton.h"
#include "N3Base/N3UIString.h"
Expand Down
2 changes: 1 addition & 1 deletion src/game/UIPerTradeDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "LocalInput.h"
#include "APISocket.h"
#include "GameProcMain.h"
#include "PlayerMyself.h"
#include "PlayerMySelf.h"
#include "N3UIWndBase.h"

#include "UIImageTooltipDlg.h"
Expand Down
2 changes: 1 addition & 1 deletion src/game/UIStateBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ void CUIStateBar::DelMagic(__TABLE_UPC_SKILL * pSkill) {
__DurationMagicImg * pMagicImg = (*it);
CN3UIDBCLButton * pIcon = pMagicImg->pIcon;
CN3Texture * pTex = pIcon->GetTex();
if (pTex && N3::iequals(szTexFN, pTex->FileName())) {
if (pTex && n3std::iequals(szTexFN, pTex->FileName())) {
itRemove = it;
}
if (itRemove != ite) {
Expand Down
Loading

0 comments on commit 7a6a7d6

Please sign in to comment.