diff --git a/CMakeLists.txt b/CMakeLists.txt index bfb8296..bc65dd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,29 +1,57 @@ cmake_minimum_required(VERSION 3.20) project(CPP_TEMPLATE VERSION 0.1.0 LANGUAGES C CXX) +# Set C++ standard set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) +# Set C standard +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) + +# Export compile commands for IDE support (clangd, etc.) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Create symlink to compile_commands.json in project root for LSP +if(CMAKE_EXPORT_COMPILE_COMMANDS) + add_custom_target(symlink_compile_commands ALL + COMMAND ${CMAKE_COMMAND} -E create_symlink + ${CMAKE_BINARY_DIR}/compile_commands.json + ${CMAKE_SOURCE_DIR}/compile_commands.json + COMMENT "Creating symlink to compile_commands.json in project root" + ) +endif() + # Set output directories set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -# Add include directories -include_directories(${CMAKE_SOURCE_DIR}/include) -include_directories(${CMAKE_SOURCE_DIR}/libraries) +# Build type defaults +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE) +endif() -# Gather all source files (.cpp, .c) +# Options +option(ENABLE_SANITIZERS "Enable address and undefined behavior sanitizers" ON) +option(ENABLE_STATIC_ANALYSIS "Enable static analysis warnings" ON) +set(OUTPUT_NAME "" CACHE STRING "Name of the output executable (defaults to project name)") + +# Gather source files (avoid GLOB_RECURSE - explicitly list files is better practice) +# But keeping it for template flexibility file(GLOB_RECURSE PROJECT_SOURCES - ${CMAKE_SOURCE_DIR}/include/*.cpp - ${CMAKE_SOURCE_DIR}/include/*.c - ${CMAKE_SOURCE_DIR}/libraries/*.cpp - ${CMAKE_SOURCE_DIR}/libraries/*.c ${CMAKE_SOURCE_DIR}/source/*.cpp ${CMAKE_SOURCE_DIR}/source/*.c ) -# Gather all header files (.hpp, .h) +file(GLOB_RECURSE LIBRARY_SOURCES + ${CMAKE_SOURCE_DIR}/libraries/*.cpp + ${CMAKE_SOURCE_DIR}/libraries/*.c +) + +# Don't glob headers from include/ as sources (they should only be included) file(GLOB_RECURSE PROJECT_HEADERS ${CMAKE_SOURCE_DIR}/include/*.hpp ${CMAKE_SOURCE_DIR}/include/*.h @@ -33,25 +61,51 @@ file(GLOB_RECURSE PROJECT_HEADERS ${CMAKE_SOURCE_DIR}/source/*.h ) -# Allow user to set output program name -option(OUTPUT_NAME "Name of the output executable" "") -if(OUTPUT_NAME STREQUAL "") - set(EXECUTABLE_NAME ${PROJECT_NAME}) -else() +# Combine all sources +set(ALL_SOURCES ${PROJECT_SOURCES} ${LIBRARY_SOURCES}) + +# Determine executable name +if(OUTPUT_NAME) set(EXECUTABLE_NAME ${OUTPUT_NAME}) +else() + set(EXECUTABLE_NAME ${PROJECT_NAME}) endif() -# Add executable with all sources -add_executable(${EXECUTABLE_NAME} - ${PROJECT_SOURCES} +# Create executable +add_executable(${EXECUTABLE_NAME} ${ALL_SOURCES}) + +# Set target properties +set_target_properties(${EXECUTABLE_NAME} PROPERTIES + OUTPUT_NAME ${EXECUTABLE_NAME} + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} ) -set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME}) +# Include directories - use target-specific commands +target_include_directories(${EXECUTABLE_NAME} PRIVATE + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/libraries +) -# Enable warnings and extra diagnostics -if (MSVC) - target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze) -else() +# Compiler-specific flags +if(MSVC) + target_compile_options(${EXECUTABLE_NAME} PRIVATE + /W4 # Warning level 4 + /permissive- # Standards conformance + /Zc:__cplusplus # Correct __cplusplus macro + /Zc:inline # Remove unreferenced COMDAT + /WX- # Don't treat warnings as errors by default + ) + + if(ENABLE_STATIC_ANALYSIS) + target_compile_options(${EXECUTABLE_NAME} PRIVATE /analyze) + endif() + + # MSVC debug flags + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(${EXECUTABLE_NAME} PRIVATE /Zi /Od) + endif() + +else() # GCC/Clang target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra @@ -62,12 +116,73 @@ else() -Wuninitialized -Wunused -Werror=return-type - -fsanitize=address,undefined - -g + -Wcast-align + -Wformat=2 + -Wnull-dereference ) - target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined) + + # Additional warnings for static analysis + if(ENABLE_STATIC_ANALYSIS) + target_compile_options(${EXECUTABLE_NAME} PRIVATE + -Wcast-qual + -Wdouble-promotion + -Wold-style-cast + ) + endif() + + # Sanitizers (Debug builds) + if(ENABLE_SANITIZERS AND CMAKE_BUILD_TYPE STREQUAL "Debug") + # Check if sanitizers are available + include(CheckCXXCompilerFlag) + check_cxx_compiler_flag("-fsanitize=address" HAS_ASAN) + check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN) + + if(HAS_ASAN AND HAS_UBSAN) + target_compile_options(${EXECUTABLE_NAME} PRIVATE + -fsanitize=address,undefined,leak + -fno-omit-frame-pointer + -g + ) + target_link_options(${EXECUTABLE_NAME} PRIVATE + -fsanitize=address,undefined,leak + ) + message(STATUS "Sanitizers enabled: address, undefined, leak") + else() + message(WARNING "Sanitizers requested but not available - skipping") + endif() + endif() + + # Optimization flags for Release + if(CMAKE_BUILD_TYPE STREQUAL "Release") + target_compile_options(${EXECUTABLE_NAME} PRIVATE + -O3 + -march=native + -DNDEBUG + ) + endif() + + # Debug flags + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(${EXECUTABLE_NAME} PRIVATE + -O0 + -g3 + -ggdb + ) + endif() endif() +# Print configuration summary +message(STATUS "=== Configuration Summary ===") +message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}") +message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}") +message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}") +message(STATUS "C Standard: ${CMAKE_C_STANDARD}") +message(STATUS "Executable Name: ${EXECUTABLE_NAME}") +message(STATUS "Sanitizers: ${ENABLE_SANITIZERS}") +message(STATUS "Static Analysis: ${ENABLE_STATIC_ANALYSIS}") +message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}") +message(STATUS "============================") + # Optionally, enable testing # enable_testing() # add_subdirectory(tests) diff --git a/PlayerTracker b/PlayerTracker index c06afbe..942ac71 100755 Binary files a/PlayerTracker and b/PlayerTracker differ diff --git a/build.sh b/build.sh index a31f134..7498857 100755 --- a/build.sh +++ b/build.sh @@ -1,37 +1,250 @@ -if [ -z "$1" ]; then - echo "Error: Invalid Argument" - echo "Usage: $0 " +#!/bin/bash + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +MAGENTA='\033[0;35m' +NC='\033[0m' # No Color +BOLD='\033[1m' + +# Unicode symbols +CHECK="✓" +CROSS="✗" +ARROW="➜" +GEAR="⚙" +HAMMER="🔨" +ROCKET="🚀" + +# Default values +BUILD_TYPE="Debug" +CLEAN_BUILD=false +VERBOSE=false +JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) + +# Print functions +print_header() { + echo -e "${BOLD}${BLUE}╔════════════════════════════════════════════════════════╗${NC}" + echo -e "${BOLD}${BLUE}║${NC} ${HAMMER} ${BOLD}${CYAN} C/C++ Project Builder${NC} ${BOLD}${BLUE}║${NC}" + echo -e "${BOLD}${BLUE}╚════════════════════════════════════════════════════════╝${NC}" + echo "" +} + +print_separator() { + echo -e "${BLUE}────────────────────────────────────────────────────────${NC}" +} + +print_success() { + echo -e "${GREEN}${CHECK}${NC} $1" +} + +print_error() { + echo -e "${RED}${CROSS}${NC} $1" +} + +print_info() { + echo -e "${CYAN}${ARROW}${NC} $1" +} + +print_step() { + echo -e "${YELLOW}${GEAR}${NC} ${BOLD}$1${NC}" +} + +show_progress() { + local duration=$1 + local prefix=$2 + local size=40 + already_done() { for ((done=0; done<$elapsed; done++)); do printf "▓"; done } + remaining() { for ((remain=$elapsed; remain<$size; remain++)); do printf " "; done } + percentage() { printf "| %s%%" $(( (($elapsed)*100)/($size)*100/100 )); } + + for (( elapsed=1; elapsed<=$size; elapsed++ )); do + printf "\r${CYAN}${prefix}${NC} [$(already_done)$(remaining)] $(percentage)" + sleep $(echo "scale=3; $duration/$size" | bc) + done + printf "\n" +} + +usage() { + echo -e "${BOLD}Usage:${NC} $0 [OPTIONS] " + echo "" + echo -e "${BOLD}Options:${NC}" + echo -e " -r, --release Build in Release mode (default: Debug)" + echo -e " -c, --clean Clean build directory before building" + echo -e " -v, --verbose Verbose make output" + echo -e " -j, --jobs Number of parallel jobs (default: $JOBS)" + echo -e " -h, --help Show this help message" + echo "" + echo -e "${BOLD}Examples:${NC}" + echo -e " $0 myprogram" + echo -e " $0 -r -j8 myprogram" + echo -e " $0 --clean --release myprogram" exit 1 -fi +} -if [ ! -d "./build" ]; then - echo "Creating build directory..." - mkdir build -fi - -echo ">>> Building C++ Project <<<" -cd ./build -cmake -DOUTPUT_NAME=$1 .. -echo ">>> Compiling... <<<" -make -if [ $? -ne 0 ]; then - echo "Error: Build failed" - exit 1 -fi - -cp ./bin/$1 ../ -cd .. - -echo ">>> Copying PlayerTracker to AC Servers <<<" -for server in ../servers/*; do - if [ -d "$server/utils" ]; then - target="$server/utils/ACPlayer_tracker" - mkdir -p "$target" - cp ./PlayerTracker "$target/" - echo "Installed PlayerTracker to: $target" - fi +# Parse arguments +POSITIONAL_ARGS=() +while [[ $# -gt 0 ]]; do + case $1 in + -r|--release) + BUILD_TYPE="Release" + shift + ;; + -c|--clean) + CLEAN_BUILD=true + shift + ;; + -v|--verbose) + VERBOSE=true + shift + ;; + -j|--jobs) + JOBS="$2" + shift 2 + ;; + -h|--help) + usage + ;; + *) + POSITIONAL_ARGS+=("$1") + shift + ;; + esac done -echo ">>> Build Complete <<<" -echo ">>> Executable: $1 <<<" +set -- "${POSITIONAL_ARGS[@]}" +# Check if executable name is provided +if [ -z "$1" ]; then + print_header + print_error "No executable name provided" + echo "" + usage +fi + +EXECUTABLE_NAME=$1 + +# Start build process +print_header + +# Build configuration info +print_info "Build Configuration:" +echo -e " ${BOLD}Executable:${NC} $EXECUTABLE_NAME" +echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE" +echo -e " ${BOLD}Jobs:${NC} $JOBS" +echo -e " ${BOLD}Clean Build:${NC} $CLEAN_BUILD" +echo "" +print_separator +echo "" + +# Clean build directory if requested +if [ "$CLEAN_BUILD" = true ] && [ -d "./build" ]; then + print_step "Cleaning build directory..." + rm -rf ./build + print_success "Build directory cleaned" + echo "" +fi + +# Create build directory +if [ ! -d "./build" ]; then + print_step "Creating build directory..." + mkdir -p build + print_success "Build directory created" +else + print_info "Using existing build directory" +fi +echo "" + +# CMake configuration +print_step "Configuring CMake..." +print_separator +echo "" + +cd ./build + +CMAKE_CMD="cmake -DOUTPUT_NAME=$EXECUTABLE_NAME -DCMAKE_BUILD_TYPE=$BUILD_TYPE .." + +if [ "$VERBOSE" = true ]; then + eval $CMAKE_CMD +else + eval $CMAKE_CMD > /dev/null 2>&1 +fi + +if [ $? -ne 0 ]; then + print_error "CMake configuration failed" + exit 1 +fi + +print_success "CMake configuration complete" +echo "" + +# Compilation +print_step "Compiling project..." +print_separator +echo "" + +MAKE_CMD="make -j$JOBS" +if [ "$VERBOSE" = true ]; then + MAKE_CMD="$MAKE_CMD VERBOSE=1" +fi + +START_TIME=$(date +%s) + +if [ "$VERBOSE" = true ]; then + eval $MAKE_CMD + BUILD_RESULT=$? +else + eval $MAKE_CMD 2>&1 | tee build.log | while IFS= read -r line; do + if echo "$line" | grep -q "\[.*%\]"; then + printf "\r${CYAN}${ARROW}${NC} Compiling: %s" "$line" + elif echo "$line" | grep -qE "error:|Error|ERROR"; then + echo "" + print_error "$line" + fi + done + BUILD_RESULT=${PIPESTATUS[0]} +fi + +END_TIME=$(date +%s) +BUILD_TIME=$((END_TIME - START_TIME)) + +echo "" + +if [ $BUILD_RESULT -ne 0 ]; then + print_error "Build failed!" + echo "" + print_info "Check build/build.log for details" + exit 1 +fi + +print_success "Compilation complete (${BUILD_TIME}s)" +echo "" + +# Copy executable +print_step "Copying executable to project root..." + +if [ ! -f "./bin/$EXECUTABLE_NAME" ]; then + print_error "Executable not found: ./bin/$EXECUTABLE_NAME" + exit 1 +fi + +cp ./bin/$EXECUTABLE_NAME ../ +print_success "Executable copied" +echo "" + +# Build summary +print_separator +echo -e "${BOLD}${GREEN}${ROCKET} Build Complete!${NC}" +print_separator +echo "" +echo -e "${BOLD}Summary:${NC}" +echo -e " ${BOLD}Executable:${NC} ./$EXECUTABLE_NAME" +echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE" +echo -e " ${BOLD}Build Time:${NC} ${BUILD_TIME}s" +echo -e " ${BOLD}Binary Location:${NC} ./build/bin/$EXECUTABLE_NAME" + +echo "" +print_info "Run with: ${BOLD}./$EXECUTABLE_NAME${NC}" +echo "" diff --git a/compile_commands.json b/compile_commands.json index 25eb4b2..4dcb23c 120000 --- a/compile_commands.json +++ b/compile_commands.json @@ -1 +1 @@ -build/compile_commands.json \ No newline at end of file +/home/afonsocmsousa/Developer/ac_sevice/ACPlayer_tracker/build/compile_commands.json \ No newline at end of file diff --git a/include/server_structs.h b/include/server_structs.h index 5e8ac4e..24fbe07 100644 --- a/include/server_structs.h +++ b/include/server_structs.h @@ -80,8 +80,8 @@ typedef enum flag { typedef struct carAtributes { // Related to ACSP_CAR_INFO - u_char isConnected; // 1 = connected, 0 = disconnected - u_char isLoading; // 1 = loading, 0 = not isLoading + char isConnected; // 1 = connected, 0 = disconnected + char isLoading; // 1 = loading, 0 = not isLoading char *car_model; char *car_skin; @@ -111,8 +111,8 @@ typedef struct carAtributes { typedef struct carAtributesAPI { // Related to ACSP_CAR_INFO - u_char isConnected; // 1 = connected, 0 = disconnected - u_char isLoading; // 1 = loading, 0 = not isLoading + char isConnected; // 1 = connected, 0 = disconnected + char isLoading; // 1 = loading, 0 = not isLoading char car_model[64]; char car_skin[64]; @@ -196,7 +196,7 @@ typedef struct trackAtributesAPI { } __attribute__((packed)) trackAtributesAPI; typedef struct api_packet { - u_char message_type; // ACSP_MessageType + char message_type; // ACSP_MessageType u_int8_t tracker_id; u_int8_t connected_players; diff --git a/include/file.cpp b/source/file.cpp similarity index 93% rename from include/file.cpp rename to source/file.cpp index a40f1ce..b6051a9 100644 --- a/include/file.cpp +++ b/source/file.cpp @@ -25,7 +25,9 @@ app_info parce_args(int argc, char *argv[]) { if (argc <= 1) { throw invalid_argument("No argc provided"); - } + } else if (argc != 4) { + throw invalid_argument("Invalid number of args provided"); + } __processed_info.app_id = (u_int16_t)atoi(argv[1]); __processed_info.app_port_in = (u_int16_t)atoi(argv[2]); diff --git a/source/main.cpp b/source/main.cpp index 8d85960..e0549fe 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -24,9 +24,11 @@ int main(int argc, char *argv[]) { sock.send(); } catch (const runtime_error &e) { cerr << "Error: " << e.what() << endl; + cout << "Usage: ./PlayerTracker " << endl; return 1; } return 0; } + diff --git a/include/net.cpp b/source/net.cpp similarity index 100% rename from include/net.cpp rename to source/net.cpp diff --git a/include/parcer.c b/source/parcer.c similarity index 97% rename from include/parcer.c rename to source/parcer.c index 510d3f6..0c877fd 100644 --- a/include/parcer.c +++ b/source/parcer.c @@ -1,6 +1,8 @@ #include "parcer.h" #include "server_structs.h" +#include #include +#include int ensure(size_t recv_len, size_t offset, size_t need) { return (offset + need <= recv_len); @@ -47,7 +49,7 @@ int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok int32_t v; memcpy(&v, buf + *offset, sizeof(v)); *offset += sizeof(v); - return (int32_t)ntohl(v); + return (int32_t)ntohl((u_int32_t)v); } float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) { diff --git a/include/socket.c b/source/socket.c similarity index 100% rename from include/socket.c rename to source/socket.c