Skip to content
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
.vscode/
.vscode/
.cache/
.clangd
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "external/cryptopp-cmake"]
path = external/cryptopp-cmake
url = https://github.com/abdes/cryptopp-cmake.git
[submodule "external/json"]
path = external/json
url = https://github.com/nlohmann/json.git
99 changes: 51 additions & 48 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
cmake_minimum_required(VERSION 3.14)
project(datacoe)

# Debug mode, feel free to modify to your desire
set(CMAKE_BUILD_TYPE Debug)
if(MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "/Zi /DEBUG")
set(CMAKE_C_FLAGS_DEBUG "/Zi /DEBUG")
else()
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_C_FLAGS_DEBUG "-g")
endif()
project(datacoe VERSION 0.1.0 LANGUAGES CXX)

include(FetchContent)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Currently fetching googletest v1.16.0,
# Feel free to upgrade and change the URL into the <commit hash>.zip file you would like to upgrade
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/6910c9d9165801d8827d628cb72eb7ea9dd538c5.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
if(WIN32)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
FetchContent_MakeAvailable(googletest)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(BUILD_TESTS "Build the test suite" ON)

# dependencies from external/ (git submodules)
add_subdirectory(external/cryptopp-cmake)
add_subdirectory(external/json)

# project directories
add_subdirectory(include)
add_subdirectory(src)

if(BUILD_TESTS)
include(FetchContent)
# Currently fetching googletest v1.16.0,
# Feel free to upgrade and change the URL into the <commit hash>.zip file you would like to upgrade
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/6910c9d9165801d8827d628cb72eb7ea9dd538c5.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
if(WIN32)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
FetchContent_MakeAvailable(googletest)

add_subdirectory(tests)
endif()

# Currently fetching nlohmann/json v3.11.3,
# Feel free to upgrade to your desired version.
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
install(
FILES "${CMAKE_SOURCE_DIR}/external/json/single_include/nlohmann/json.hpp"
DESTINATION include/nlohmann
)
FetchContent_MakeAvailable(json)

add_subdirectory(tests)

add_library(${PROJECT_NAME}
src/game_data.cpp
src/data_reader_writer.cpp
src/data_manager.cpp
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/datacoeconfigversion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

target_link_libraries(${PROJECT_NAME} PRIVATE cryptopp)

target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include
${json_SOURCE_DIR}/single_include/nlohmann)
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
set(CMAKE_INSTALL_LIBDIR lib)
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/datacoeconfigversion.cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/datacoeconfig.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/datacoe"
)

# "Treat warnings as errors" behavior, Feel free to remove/modify it.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Werror)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(${PROJECT_NAME} PRIVATE /W3 /WX)
endif()
install(
EXPORT datacoeTargets
FILE datacoeTargets.cmake
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/datacoe"
)
75 changes: 43 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ cmake ..
cmake --build .

# Run tests (optional)
./all_tests
./tests/all_tests
```

### Integration Steps
Expand All @@ -120,13 +120,27 @@ cmake --build .

4. **Integrate into Your Project:**

Add the library to your project's CMakeLists.txt:
There are two ways to integrate datacoe into your project:

### 4.1. Use as Subdirectory (Recommended for development)

```cmake
add_subdirectory(path/to/datacoe)
target_link_libraries(your_game_executable PRIVATE datacoe)
```

### 4.2. Install and Use with find_package (Better for distribution)

Then link your executable with the library:
```cmake
# Build and install datacoe
cd path/to/datacoe
mkdir build && cd build
cmake ..
cmake --build .
cmake --install . --prefix <install_path>

# In your project's CMakeLists.txt
find_package(datacoe REQUIRED)
target_link_libraries(your_game_executable PRIVATE datacoe)
```

Expand All @@ -147,7 +161,7 @@ The library consists of three main components:
#### Initialize and Save Game Data

```cpp
#include "data_manager.hpp"
#include <datacoe/data_manager.hpp>

// Initialize with a file path
datacoe::DataManager manager;
Expand All @@ -164,7 +178,7 @@ bool saveSuccess = manager.saveGame();
#### Load Game Data

```cpp
#include "data_manager.hpp"
#include <datacoe/data_manager.hpp>

// Initialize with the same file path
datacoe::DataManager manager;
Expand All @@ -182,7 +196,7 @@ int score = data.getHighscore();
#### Create New Game

```cpp
#include "data_manager.hpp"
#include <datacoe/data_manager.hpp>

datacoe::DataManager manager;
manager.init("save_game.json");
Expand Down Expand Up @@ -221,23 +235,23 @@ To adapt this library for your game, you'll need to modify the core components t

## Dependencies

All dependencies are now automatically handled:
All dependencies are automatically handled:

- **CryptoPP:** Automatically fetched via the cryptopp-cmake Git submodule (currently points to version 8.9.0)
- **nlohmann/json:** Automatically fetched by CMake during configuration (currently version 3.11.3)
- **Google Test:** Automatically fetched by CMake during configuration (currently version 1.16.0)
- **CryptoPP:** Added as a git submodule at external/cryptopp-cmake
- **nlohmann/json:** Added as a git submodule at external/json
- **Google Test:** Automatically fetched by CMake during configuration only if BUILD_TESTS is ON (currently version 1.16.0)

You no longer need to manually download or build these dependencies.
The nlohmann/json library is now included as a submodule (like cryptopp-cmake) rather than being fetched via CMake, providing more consistent dependency management and offline build capability.

### Updating Dependencies (optional)

#### Updating the cryptopp-cmake submodule
#### Updating the cryptopp-cmake or nlohmann/json submodules

If you want to update the cryptopp-cmake submodule to a different version:
If you want to update either submodule to a different version:

```bash
# Navigate to the cryptopp-cmake directory
cd external/cryptopp-cmake
# Navigate to the submodule directory
cd external/cryptopp-cmake # or external/json

# Fetch all tags
git fetch --tags
Expand All @@ -246,30 +260,20 @@ git fetch --tags
git tag -l

# Checkout the specific tag you want
git checkout <tag_name> # e.g., CRYPTOPP_8_9_0
git checkout <tag_name> # e.g., CRYPTOPP_8_9_0 or v3.11.3

# Return to the main project directory
cd ../..

# Now commit the submodule update
git add external/cryptopp-cmake
git commit -m "Update cryptopp-cmake to <tag_name>"
git add external/cryptopp-cmake # or external/json
git commit -m "Update submodule to <tag_name>"
```

#### Updating nlohmann/json and Google Test
#### Updating Google Test

To update nlohmann/json or Google Test to newer versions, modify the FetchContent_Declare section in your CMakeLists.txt:
To update Google Test to a newer version, modify the FetchContent_Declare section in your CMakeLists.txt:

For nlohmann/json:
```cmake
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3 # Change this to the desired version
)
```

For Google Test:
```cmake
FetchContent_Declare(
googletest
Expand All @@ -295,15 +299,15 @@ To run all tests:

```bash
cd build
./all_tests
./tests/all_tests
```

To build and run individual test executables, enable the `BUILD_INDIVIDUAL_TESTS` option:

```bash
cmake -DBUILD_INDIVIDUAL_TESTS=ON ..
cmake --build .
./error_handling_tests # Run a specific test
./tests/error_handling_tests # Run a specific test
```

### Customizing Tests
Expand Down Expand Up @@ -410,6 +414,13 @@ Game-specific implementations will have their own tags (e.g., `worm-v1.0.0`) to

## Version History

### [v0.1.2](https://github.com/nircoe/datacoe/releases/tag/v0.1.2) (CMake Modernization)
- Reorganized CMake structure with subdirectory CMakeLists.txt files
- Converted nlohmann/json from FetchContent to git submodule
- Added proper installation targets for headers, library, and CMake config files
- Implemented exported targets for easier integration in other projects
- Added find_package support

### [v0.1.1](https://github.com/nircoe/datacoe/releases/tag/v0.1.1) (Optional Encryption)
- Added ability to disable encryption when not needed
- Implemented automatic encryption detection for backwards compatibility
Expand Down
5 changes: 5 additions & 0 deletions cmake/datacoeconfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include(CMakeFindDependencyMacro)

find_dependency(nlohmann_json REQUIRED)

include("${CMAKE_CURRENT_LIST_DIR}/datacoeTargets.cmake")
1 change: 1 addition & 0 deletions external/json
Submodule json added at 9cca28
18 changes: 18 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_library(datacoe_headers INTERFACE)
target_include_directories(datacoe_headers
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/external/json/single_include>
$<INSTALL_INTERFACE:include>
)

install(
DIRECTORY datacoe
DESTINATION include
FILES_MATCHING PATTERN "*.hpp"
)

install(
TARGETS datacoe_headers
EXPORT datacoeTargets
)
2 changes: 0 additions & 2 deletions include/datacoe/data_manager.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include <string>
#include <optional>
#include "game_data.hpp"
#include "data_reader_writer.hpp"

namespace datacoe
{
Expand Down
2 changes: 1 addition & 1 deletion include/datacoe/game_data.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include "json.hpp"
#include <nlohmann/json.hpp>

using json = nlohmann::json;

Expand Down
32 changes: 32 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
add_library(datacoe
data_manager.cpp
data_reader_writer.cpp
game_data.cpp
)

target_link_libraries(datacoe
PRIVATE
cryptopp
PUBLIC
datacoe_headers
)

target_include_directories(datacoe
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(datacoe PRIVATE -Wall -Wextra -Werror)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(datacoe PRIVATE /W3 /WX)
endif()

install(
TARGETS datacoe
EXPORT datacoeTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
2 changes: 2 additions & 0 deletions src/data_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "datacoe/data_manager.hpp"
#include "datacoe/data_reader_writer.hpp"
#include <optional>

namespace datacoe
{
Expand Down
Loading
Loading