diff --git a/.travis.yml b/.travis.yml index 6aa2a24..811e7b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,7 @@ before_install: install: - if [ "$CXX" = "g++" ]; then export CXX="g++-6" CC="gcc-6"; fi before_script: + - sed -i '/#define C_DLLEXPORT/c\//#define C_DLLEXPORT' third_party/metamod-hl1/metamod/osdep.h - cmake --version - cmake . script: diff --git a/CMakeLists.txt b/CMakeLists.txt index 11a2374..450c973 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,23 +17,35 @@ add_subdirectory(test) file(GLOB_RECURSE SOURCE_FILES ${SRC_DIR}/*.h ${SRC_DIR}/*.cpp) add_library(${PROJECT_NAME} SHARED ${SRC_LIST} ${SOURCE_FILES}) -set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32 -static-libstdc++") +set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32 -O2 -flto -static-libstdc++" LINK_FLAGS "-m32 -fwhole-program -static-libstdc++") target_compile_features(${PROJECT_NAME} PRIVATE cxx_range_for) add_library(${PROJECT_NAME}_static STATIC ${SRC_LIST} ${SOURCE_FILES}) set_target_properties(${PROJECT_NAME}_static PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") target_compile_features(${PROJECT_NAME}_static PRIVATE cxx_range_for) +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'Release' as none was specified.") + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release") +else() + message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") +endif() + if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) # -Wundef -Wlong-long -Wpadded set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wzero-as-null-pointer-constant -Weffc++ -Wunknown-pragmas") - if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + if (NOT CMAKE_UILD_TYPE STREQUAL "Debug") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") - set(CMAKE_CXX_FLAGS "-O2 -finline-functions") - set(CMAKE_SHARED_LINKER_FLAGS "-s") + set(CMAKE_SHARED_LINKER_FLAGS "") endif() endif() if (CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-D_DEBUG) endif() + + + diff --git a/README.md b/README.md index dc4b700..d7e1081 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ cmake, boost, gcc, g++ packages may be required.
 sudo apt-get install libboost-dev cxxtest cmake gcc g++ # gcc > 4.8 required
 sudo apt-get install gcc-multilib g++-multilib libc6-dev-i386 libc6-i386 # If cross compiling from 64 bit linux  
+sed -i '/#define C_DLLEXPORT/c\//#define C_DLLEXPORT' third_party/metamod-hl1/metamod/osdep.h # Patch osdep.h
 cmake .
 make -j$((2 * `getconf _NPROCESSORS_ONLN`)) # Multicore build
 
diff --git a/src/config.cpp b/src/config.cpp index bbfedc6..655a491 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -31,9 +31,12 @@ */ #include "config.h" +#include "helper/shared.h" #include "helper/string_utils.h" + #include #include + Config::Config(const std::shared_ptr &logger) : logger_(logger), @@ -162,7 +165,7 @@ void Config::loadConfig(const std::string &path) std::string line; while(std::getline(file, line)) { - logger_->debug(std::string(__FUNCTION__) + " Reading line" + line); + logger_->debug(FNAME + " Reading line" + line); this->readLine(line); } @@ -222,19 +225,19 @@ bool Config::readLine(const std::string &lineRef) std::string line = trim(removeComments(lineRef)); if(line.empty()) { - logger_->debug(std::string(__FUNCTION__) + " Line is empty. Return."); + logger_->debug(FNAME + " Line is empty. Return."); return false; } std::string::size_type equalitySignPos = line.find("="); if(equalitySignPos == std::string::npos) { - logger_->warning(std::string(__FUNCTION__) + " Equality sign not found. Return."); + logger_->warning(FNAME + " Equality sign not found. Return."); return false; } std::string left = trim(line.substr(0, equalitySignPos)); std::string right = trim(line.substr(equalitySignPos + 1, std::string::npos)); - logger_->debug(std::string(__FUNCTION__) + " Left right: " + left + " = " + right); + logger_->debug(FNAME + " Left right: " + left + " = " + right); try { decltype(options_)::mapped_type &value = options_.at(left); @@ -258,7 +261,7 @@ bool Config::readLine(const std::string &lineRef) try { switchMap.at(localCopy)(); } catch (std::exception&) { - logger_->error(std::string(__FUNCTION__) + " Exception occured. Return from BF"); + logger_->error(FNAME + " Exception occured. Return from BF"); return false; } return true; @@ -269,7 +272,7 @@ bool Config::readLine(const std::string &lineRef) case Config::OptionType::Boolean: { bool result = BooleanFunction(right); - logger_->debug(std::string(__FUNCTION__) + " BooleanFunction(" + right + ") result = " + std::to_string(result)); + logger_->debug(FNAME + " BooleanFunction(" + right + ") result = " + std::to_string(result)); break; } case Config::OptionType::Integer: @@ -284,7 +287,7 @@ bool Config::readLine(const std::string &lineRef) } case Config::OptionType::String: { - logger_->debug(std::string(__FUNCTION__) + " Value.second = " + right); + logger_->debug(FNAME + " Value.second = " + right); value.second = right; break; } @@ -292,7 +295,7 @@ bool Config::readLine(const std::string &lineRef) } catch (std::exception &e) { - logger_->error(std::string(__FUNCTION__) + " Exception occured. " + e.what() + " Continue."); + logger_->error(FNAME + " Exception occured. " + e.what() + " Continue."); return false; } diff --git a/src/helper/external_api.h b/src/helper/external_api.h index d405dc7..69de74f 100644 --- a/src/helper/external_api.h +++ b/src/helper/external_api.h @@ -1,27 +1,29 @@ -#ifndef EXTERNAL_API_H // We do not need to include this file twice +#ifndef EXTERNAL_API_H #define EXTERNAL_API_H // This is some "hacky" way to avoid multiple "zero as null" errors in metamod and hlsdk headers -#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" -#include -#include - -#ifndef CXXTEST_RUNNING // If generated runner.cpp for some reason appears error. -#pragma GCC diagnostic error "-Wzero-as-null-pointer-constant" +#ifndef CXXTEST_RUNNING // cxxtest generated runner.cpp file for some reason creates error. + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" + #define C_DLLEXPORT extern "C" __attribute__((externally_visible)) + #include + #include + #pragma GCC diagnostic pop #endif -// SDK defines its own max/min, but it makes conflicts. + +// SDK defines its own max/min, but it creates conflicts. #if defined (max) || (min) -#include + #include -#ifdef max -#undef max -using std::max; -#endif + #ifdef max + #undef max + using std::max; + #endif -#ifdef min -#undef min -using std::min; -#endif + #ifdef min + #undef min + using std::min; + #endif #endif diff --git a/src/helper/shared.h b/src/helper/shared.h index 56c85a0..6c811dd 100644 --- a/src/helper/shared.h +++ b/src/helper/shared.h @@ -1,6 +1,8 @@ -#ifndef SHARED_TYPEDEFS_H -#define SHARED_TYPEDEFS_H +#ifndef SHARED_H +#define SHARED_H + +#define FNAME std::string(__func__) // Here we can put typedefs that shared by many files -#endif // SHARED_TYPEDEFS_H +#endif // SHARED_H diff --git a/src/main.cpp b/src/main.cpp index c1f5aac..a22efb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,13 +61,13 @@ static META_FUNCTIONS gMetaFunctionTable = { plugin_info_t Plugin_info = { META_INTERFACE_VERSION, "Ultimate Unprecacher", - "Beta 1.0", + "Beta 2.0", "2016/05/18", "Alik Aslanyan ", "https://github.com/in-line/metamod_unprecacher", "", - PT_ANYTIME, - PT_ANYTIME, + PT_CHANGELEVEL, + PT_CHANGELEVEL, }; meta_globals_t *gpMetaGlobals; diff --git a/src/module.cpp b/src/module.cpp index eb273fa..dbbad4a 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -120,14 +120,14 @@ bool Module::readLine(const std::string &lineRef) std::vector tokens; std::string line = lineRef; - logger_->debug(std::string(__FUNCTION__) + " Line raw text: " + line); + logger_->debug(FNAME + " Line raw text: " + line); line = trim(removeComments(line)); - logger_->debug(std::string(__FUNCTION__) + " Line text after: " + line); + logger_->debug(FNAME + " Line text after: " + line); if(line.empty()) { - logger_->debug(std::string(__FUNCTION__) + " Line is empty. Continue to next."); + logger_->debug(FNAME + " Line is empty. Continue to next."); return false; } tokens = parseString(line, true); @@ -140,7 +140,7 @@ bool Module::readLine(const std::string &lineRef) // Template: ".***" extension = path.substr(path.size() - 4, std::string::npos); } catch (std::out_of_range) { - logger_->error(std::string(__FUNCTION__) + " Cannot find extension. Continue."); + logger_->error(FNAME + " Cannot find extension. Continue."); return false; } @@ -149,7 +149,7 @@ bool Module::readLine(const std::string &lineRef) try { currentMapType = extensions.at(extension); } catch (std::exception&) { - logger_->error(std::string(__FUNCTION__) + " Unrecognized extension. Continue"); + logger_->error(FNAME + " Unrecognized extension. Continue"); return false; } @@ -170,10 +170,10 @@ bool Module::readLine(const std::string &lineRef) if(size > 1) { try { - options = UnprecacheOptions::analyzeBitSetAlphabitePattern(tokens[1]); + options = UnprecacheOptions::analyzeBitSetAlphabetPattern(tokens[1]); } catch (std::exception &e) { - logger_->error(std::string(__FUNCTION__) + " Exception when reading alphabite patter " + std::string(e.what())); - logger_->error(std::string(__FUNCTION__) + " Reseting unprecache options"); + logger_->error(FNAME + " Exception when reading alphabet pattern " + e.what()); + logger_->error(FNAME + " Reseting unprecache options"); options = UnprecacheOptions(); } @@ -183,7 +183,7 @@ bool Module::readLine(const std::string &lineRef) options.setReplacedPath(tokens[2]); else { - logger_->error(std::string(__FUNCTION__) + " Can not find replace path, disabling replace function"); + logger_->error(FNAME + " Can not find replace path, disabling replace function"); options.setReplace(false); } } @@ -191,7 +191,7 @@ bool Module::readLine(const std::string &lineRef) } } // Insert our job result - logger_->debug(std::string(__FUNCTION__) + " Insert to mapType " + extensionPrefixes[currentMapType]); + logger_->debug(FNAME + " Insert to mapType " + extensionPrefixes[currentMapType]); maps_[currentMapType][path] = options; return true; } @@ -200,7 +200,7 @@ void Module::loadLists(const std::string &path) { std::fstream inputFile; { - logger_->debug(std::string(__FUNCTION__) + " Try to open " + path); + logger_->debug(FNAME + " Try to open " + path); inputFile.open ( path, std::ios::in @@ -208,26 +208,26 @@ void Module::loadLists(const std::string &path) if(!inputFile) { - logger_->warning(std::string(__FUNCTION__) + " Cannot open, try to create"); + logger_->warning(FNAME + " Cannot open, try to create"); inputFile.open(path, std::ios::out | std::ios::in | std::ios::trunc); } if(!inputFile) { - logger_->criticalError(std::string(__FUNCTION__) + " Fail. Throwing exception..."); + logger_->criticalError(FNAME + " Fail. Throwing exception..."); throw std::ios::failure("Cannot create/open file: " + path); } } { // Line has format: "SOME_PATH" "flags" "additional" - logger_->debug(std::string(__FUNCTION__) + " Start file reading"); + logger_->debug(FNAME + " Start file reading"); std::string line; while(getline(inputFile, line)) { this->readLine(line); } - logger_->debug(std::string(__FUNCTION__) + " Input file close"); + logger_->debug(FNAME + " Input file close"); inputFile.close(); } this->revalidateEnds(); diff --git a/src/module.h b/src/module.h index 039a611..e145abb 100644 --- a/src/module.h +++ b/src/module.h @@ -40,13 +40,13 @@ #include "unprecacheoptions.h" #include "logger.h" #include "config.h" +#include "helper/shared.h" #include #include #include #include -#pragma GCC diagnostic warning "-Wzero-as-null-pointer-constant" class Module { @@ -67,7 +67,7 @@ class Module if(result != mapsEnds_[mapType]) { #ifdef _DEBUG - logger_->debug(std::string(__FUNCTION__) + " Match. " + path + " -> " + result->first); + logger_->debug(FNAME + " Match. " + path + " -> " + result->first); #endif lastHitPoint_ = result->second; return true; diff --git a/src/unprecacheoptions.cpp b/src/unprecacheoptions.cpp index fe36698..adc22fb 100644 --- a/src/unprecacheoptions.cpp +++ b/src/unprecacheoptions.cpp @@ -31,8 +31,10 @@ */ #include "unprecacheoptions.h" + #include #include + bool UnprecacheOptions::replace() const { return replace_; @@ -103,7 +105,7 @@ void UnprecacheOptions::setDeleteEntity(bool deleteEntity) deleteEntity_ = deleteEntity; } -UnprecacheOptions UnprecacheOptions::analyzeBitSetAlphabitePattern(std::string pattern) +UnprecacheOptions UnprecacheOptions::analyzeBitSetAlphabetPattern(std::string pattern) { // a - delete entity // b - not delete human entities diff --git a/src/unprecacheoptions.h b/src/unprecacheoptions.h index c93d48a..b9b77d9 100644 --- a/src/unprecacheoptions.h +++ b/src/unprecacheoptions.h @@ -37,10 +37,10 @@ class UnprecacheOptions { -bool deleteEntity_; -bool notDeleteHuman_; -bool replace_; -std::string replacedPath_; + bool deleteEntity_; + bool notDeleteHuman_; + bool replace_; + std::string replacedPath_; public: UnprecacheOptions(); bool operator ==(const UnprecacheOptions &obj) const; @@ -58,7 +58,7 @@ std::string replacedPath_; const std::string& replacedPath() const; void setReplacedPath(const std::string &replacedPath); - static UnprecacheOptions analyzeBitSetAlphabitePattern(std::string pattern); + static UnprecacheOptions analyzeBitSetAlphabetPattern(std::string pattern); bool isNotDefault() const; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 78585db..d6e1e1e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,12 +6,11 @@ find_package(CxxTest REQUIRED) #cmake_policy(SET CMP0040 NEW) if(CXXTEST_FOUND) -enable_testing() -file(GLOB_RECURSE TESTES ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -CXXTEST_ADD_TEST(${PROJECT_NAME} runner.cpp ${TESTES}) -target_link_libraries(${PROJECT_NAME} unprecacher_static) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_range_for) -set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") - -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ./${PROJECT_NAME}) + enable_testing() + file(GLOB_RECURSE TESTES ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + CXXTEST_ADD_TEST(${PROJECT_NAME} runner.cpp ${TESTES}) + target_link_libraries(${PROJECT_NAME} unprecacher_static) + target_compile_features(${PROJECT_NAME} PRIVATE cxx_range_for) + set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ./${PROJECT_NAME}) endif() diff --git a/test/test_config.h b/test/test_config.h index f0e26e9..914ee9a 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -35,6 +35,7 @@ #include #include "../src/config.h" + class TestConfig : public CxxTest::TestSuite { private: diff --git a/test/test_module.h b/test/test_module.h index ae232d9..f6bf620 100644 --- a/test/test_module.h +++ b/test/test_module.h @@ -23,7 +23,7 @@ class TestModule : public CxxTest::TestSuite TS_ASSERT(module.readLine("sprites/w_usp.spr abc models/v_knife_custom.something")); TS_ASSERT(module.readLine("sound/w_usp.wav abc models/v_knife_custom.something")); module.revalidateEnds(); - UnprecacheOptions o = UnprecacheOptions::analyzeBitSetAlphabitePattern("abc"); + UnprecacheOptions o = UnprecacheOptions::analyzeBitSetAlphabetPattern("abc"); o.setReplacedPath("models/v_knife_custom.something"); TS_ASSERT(module.checkModel("models/w_usp.mdl")); diff --git a/test/test_unprecacheoptions.h b/test/test_unprecacheoptions.h index 031584d..8e5cdce 100644 --- a/test/test_unprecacheoptions.h +++ b/test/test_unprecacheoptions.h @@ -8,22 +8,22 @@ class TestUnprecacheOptions : public CxxTest::TestSuite { public: - void Test_analyzeBitSetAlphabitePattern() + void Test_analyzeBitSetAlphabetPattern() { UnprecacheOptions options; std::string testPattern = "abc "; - TS_ASSERT_THROWS_NOTHING(UnprecacheOptions::analyzeBitSetAlphabitePattern(testPattern)); + TS_ASSERT_THROWS_NOTHING(UnprecacheOptions::analyzeBitSetAlphabetPattern(testPattern)); - options = UnprecacheOptions::analyzeBitSetAlphabitePattern(testPattern); + options = UnprecacheOptions::analyzeBitSetAlphabetPattern(testPattern); TS_ASSERT_EQUALS(options.isNotDefault(), true); TS_ASSERT_EQUALS(options.deleteEntity(), true); TS_ASSERT_EQUALS(options.notDeleteHuman(), true); TS_ASSERT_EQUALS(options.replace(), true); - TS_ASSERT_THROWS_ANYTHING(UnprecacheOptions::analyzeBitSetAlphabitePattern("jlgke3$%%^#@@!@#$% ")); + TS_ASSERT_THROWS_ANYTHING(UnprecacheOptions::analyzeBitSetAlphabetPattern("jlgke3$%%^#@@!@#$% ")); - options = UnprecacheOptions::analyzeBitSetAlphabitePattern(" "); + options = UnprecacheOptions::analyzeBitSetAlphabetPattern(" "); TS_ASSERT_EQUALS(options.isNotDefault(), false); TS_ASSERT_EQUALS(options.deleteEntity(), false);