Skip to content

Commit

Permalink
Add Python/C way of checking if BVH/maps have been built
Browse files Browse the repository at this point in the history
Fixes namreeb#51.
  • Loading branch information
Gtker committed Sep 5, 2023
1 parent 7855f3e commit f400272
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MapBuilder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set(EXECUTABLE_NAME MapBuilder)
set(LIBRARY_NAME libmapbuild)
set(PYTHON_NAME mapbuild)

set(SRC BVHConstructor.cpp GameObjectBVHBuilder.cpp MeshBuilder.cpp RecastContext.cpp Worker.cpp MapBuilder_c_bindings.cpp)
set(SRC BVHConstructor.cpp GameObjectBVHBuilder.cpp MeshBuilder.cpp RecastContext.cpp Worker.cpp MapBuilder_c_bindings.cpp FileExist.cpp)

add_library(${LIBRARY_NAME} ${SRC})
target_include_directories(${LIBRARY_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../")
Expand Down
17 changes: 17 additions & 0 deletions MapBuilder/FileExist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "FileExist.hpp"

#include <filesystem>

namespace file_exist {

bool bvh_files_exist(const std::string& outputPath) {
// bvh.idx is created at the very end of building game objects
return std::filesystem::exists(outputPath + "/BVH/bvh.idx");
}

bool map_files_exist(const std::string& outputPath, const std::string& mapName) {
// mapName.map is created in MeshBuilder::SaveMap which should be after successful creation
return std::filesystem::exists(outputPath + "/" + mapName + ".map");
}

} // namespace file_exist
9 changes: 9 additions & 0 deletions MapBuilder/FileExist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <string>

namespace file_exist {

bool bvh_files_exist(const std::string& outputPath);

bool map_files_exist(const std::string& outputPath, const std::string& mapName);

} // namespace file_exist
39 changes: 39 additions & 0 deletions MapBuilder/MapBuilder_c_bindings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mapbuilder_c_bindings.h"

#include "FileExist.hpp"
#include "GameObjectBVHBuilder.hpp"
#include "MeshBuilder.hpp"
#include "Worker.hpp"
Expand Down Expand Up @@ -134,5 +135,43 @@ MapBuildResultType mapbuild_build_map(const char* const data_path,
return static_cast<MapBuildResultType>(Result::SUCCESS);
}

MapBuildResultType mapbuild_bvh_files_exist(const char* const output_path,
uint8_t* const exists) {
std::string outputPath = output_path;

try {
if (file_exist::bvh_files_exist(outputPath)) {
*exists = 1;
} else {
*exists = 0;
}
}
catch (...) {
return static_cast<MapBuildResultType>(Result::UNKNOWN_EXCEPTION);
}

return static_cast<MapBuildResultType>(Result::SUCCESS);
}

MapBuildResultType mapbuild_map_files_exist(const char* const output_path,
const char* const map_name,
uint8_t* const exists) {

std::string outputPath = output_path;
std::string mapName = map_name;

try {
if (file_exist::map_files_exist(outputPath, mapName)) {
*exists = 1;
} else {
*exists = 0;
}
}
catch (...) {
return static_cast<MapBuildResultType>(Result::UNKNOWN_EXCEPTION);
}

return static_cast<MapBuildResultType>(Result::SUCCESS);
}

}
19 changes: 18 additions & 1 deletion MapBuilder/mapbuilder_c_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,21 @@ MapBuildResultType mapbuild_build_map(const char* const data_path,
const char* const gameobject_csv,
uint32_t threads);

}
/*
Tests if gameobjects have been built in `output_path`.
`exists` will be `0` if the gameobject files haven't been built, and `1` if they have.
*/
MapBuildResultType mapbuild_bvh_files_exist(const char* const output_path,
uint8_t* const exists);

/*
Tests if the map with `map_name` has already been built in `output_path`.
`exists` will be `0` if the map hasn't been built, and `1` if it has.
*/
MapBuildResultType mapbuild_map_files_exist(const char* const output_path,
const char* const map_name,
uint8_t* const exists);

}
11 changes: 11 additions & 0 deletions MapBuilder/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MeshBuilder.hpp"
#include "Worker.hpp"
#include "parser/MpqManager.hpp"
#include "FileExist.hpp"

#include <boost/python.hpp>
#include <filesystem>
Expand Down Expand Up @@ -142,9 +143,19 @@ bool BuildADT(const std::string& dataPath, const std::string& outputPath,
return true;
}

bool BVHFilesExist(const std::string& outputPath) {
return file_exist::bvh_files_exist(outputPath);
}

bool MapFilesExist(const std::string& outputPath, const std::string& mapName) {
return file_exist::map_files_exist(outputPath, mapName);
}

BOOST_PYTHON_MODULE(mapbuild)
{
boost::python::def("build_bvh", BuildBVH);
boost::python::def("build_map", BuildMap);
boost::python::def("build_adt", BuildADT);
boost::python::def("map_files_exist", MapFilesExist);
boost::python::def("bvh_files_exist", BVHFilesExist);
}

0 comments on commit f400272

Please sign in to comment.