Skip to content

Commit

Permalink
attempt to make custom component batch files
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 5, 2024
1 parent 8e3a0dd commit d9dc703
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,19 @@ set(SHADER_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/shaders)
add_dependencies(${PROJECT_NAME} compile_shaders)
# ##############################################################################

if(WIN32)
add_custom_target(update_components
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/components/updateComponents.bat
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running updateComponents.bat script"
)
else()
add_custom_target(update_components
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/components/updateComponents.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running updateComponents.sh script"
)
endif()

add_dependencies(${PROJECT_NAME} update_components)

Expand Down
82 changes: 82 additions & 0 deletions components/createComponent.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
@echo off

if "%1"=="" (
echo Usage: %0 ClassName
exit /b 1
)

set CLASS_NAME=%1
set SCRIPT_DIR=%~dp0

set HEADER_FILE=%SCRIPT_DIR%\%CLASS_NAME%.h
set CPP_FILE=%SCRIPT_DIR%\%CLASS_NAME%.cpp

echo Creating C++ class: %CLASS_NAME%...

(
echo #pragma once

echo #include ^<goop/Components.h^>
echo.
echo #include ^<imgui.h^>
echo.
echo class %CLASS_NAME% : public goop::CustomComponent
echo {
echo public:
echo %CLASS_NAME%(goop::Entity e) : goop::CustomComponent(e) {name = "%CLASS_NAME%";}
echo.
echo void init();
echo void update(float dt);
echo void gui();
echo.
echo private:
echo void onCollisionEnter(goop::Entity other) final;
echo void onCollisionExit(goop::Entity other) final;
echo };
) > "%HEADER_FILE%"

echo Created %HEADER_FILE%

(
echo #include "%CLASS_NAME%.h"
echo.
echo // Gets called when the game starts
echo void %CLASS_NAME%::init^()
echo {
echo /\* do not remove this line \*/
echo CustomComponent::init();
echo.
echo //...
echo }
echo.
echo // Gets called every frame
echo void %CLASS_NAME%::update(float dt^)
echo {
echo //...
echo }
echo.
echo // Collision callbacks
echo void PlayerInput::onCollisionEnter(goop::Entity other^)
echo {
echo // std::cout ^<^< "Player started colliding with " ^<^< other.getComponent^<goop::TagComponent^>().tag ^<^< std::endl;
echo //...
echo }
echo.
echo void PlayerInput::onCollisionExit(goop::Entity other^)
echo {
echo // std::cout ^<^< "Player stopped colliding with " ^<^< other.getComponent^<goop::TagComponent^>().tag ^<^< std::endl;
echo //...
echo }
echo.
echo // Editor GUI - Shown in inspector view
echo // Refer to ImGui documentation for more info
echo void %CLASS_NAME%::gui^()
echo {
echo // ImGui::Text("Hello from %s", name.c_str());
echo //...
echo }
) > "%CPP_FILE%"

echo Created %CPP_FILE%

echo C++ class '%CLASS_NAME%' created successfully!
262 changes: 262 additions & 0 deletions components/updateComponents.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
@echo off
setlocal enabledelayedexpansion

:: Get the directory of the script
for %%I in ("%~dp0") do set "script_dir=%%~fI"

:: Move to the script's directory
cd /d "%script_dir%" || exit /b

:: Find .cpp files
set "cpp_files="
for %%F in ("%script_dir%\*.cpp") do (
set "cpp_files=!cpp_files! %%~nxF"
)

:: Calculate hash of CustomComponents.h if it exists
set "existing_hash="
if exist CustomComponents.h (
for /f %%H in ('CertUtil -hashfile CustomComponents.h SHA256 ^| findstr /v /c:SHA256') do (
set "existing_hash=%%H"
)
)

:: Function to calculate hash for a file
:calculate_hash
set "file=%~1"
for /f %%H in ('CertUtil -hashfile "!file!" SHA256 ^| findstr /v /c:SHA256') do (
set "hash_result=%%H"
)
exit /b

:: Concatenate all .cpp file names together to form a string
set "file_names_concatenated="
for %%F in (%cpp_files%) do (
set "file_names_concatenated=!file_names_concatenated! %%~nxF"
)

:: Calculate the hash of the concatenated file names
set "updated_hash="
echo !file_names_concatenated! | CertUtil -hashfile - SHA256 | findstr /v /c:SHA256 > hash_result.txt
set /p updated_hash=<hash_result.txt
del hash_result.txt

:: Get the existing hash from the first line comment
set "existing_hash_comment="
for /f "tokens=*" %%A in (CustomComponents.h) do (
set "existing_hash_comment=%%A"
goto :hash_comment_found
)
:hash_comment_found

set "existing_hash="
for /f "tokens=2 delims=/" %%B in ("!existing_hash_comment!") do (
set "existing_hash=%%B"
)

:: Check if the file needs updating based on the hash
set "needs_update=false"
if "!existing_hash!" neq "!updated_hash!" (
set "needs_update=true"
)

if not !needs_update! == false (
echo CustomComponents.h is up to date
exit /b 0
)

:: Clear the contents of CustomComponents.h
type nul > CustomComponents.h

:: Loop through each .cpp file and generate include statements
for %%F in (%cpp_files%) do (
set "filename=%%~nF"
echo #include "!filename!.h"
) >> CustomComponents.h

:: Add remaining content to CustomComponents.h
(
echo #include ^<goop/Scene.h^>
echo #include ^<goop/Entity.h^>
echo #include ^<map^>
echo #include ^<variant^>
echo #include ^<imgui.h^>
echo.
echo using CustomComponentVariant = std::variant^<
)

:: Check if there are any .cpp files
set "file_count=0"
for %%F in (%cpp_files%) do (
set /a "file_count+=1"
)

:: Generate variant types or default if no .cpp files are found
if %file_count% neq 0 (
for %%F in (%cpp_files%) do (
set "filename=%%~nF"
echo !filename!,
)
) else (
echo std::monostate,
)

:: Remove the last comma
echo ^>^;
echo.
echo static std::map^<std::string, std::function^<CustomComponentVariant(entt::entity, goop::Scene*)^>^> customComponentFactoryMap;
echo.

:: Check if there are any .cpp files for generating additional content
if %file_count% neq 0 (
echo #define REGISTER_CUSTOM_COMPONENT(name, type) \\
echo static CustomComponentVariant create##type(entt::entity e, goop::Scene* s) \\
echo ^{ \\
echo return type(goop::Entity(e, s)); \\
echo ^} \\
echo static bool registered##type = ^[^] \\
echo ^{ \\
echo customComponentFactoryMap[name] = create##type; \\
echo return true; \\
echo ^}^(

:: Register components
for %%F in (%cpp_files%) do (
set "filename=%%~nF"
echo REGISTER_CUSTOM_COMPONENT^("!filename!", !filename!^);
)
)

) >> CustomComponents.h

(
echo inline void initCustomComponents(goop::Scene* s^)
echo {
) > CustomComponents.h

:: Checking if there are .cpp files
if %file_count% neq 0 (
(
echo for (auto^& [name, factory] : customComponentFactoryMap^)
echo {
echo auto variant = factory(entt::null, s^);
echo std::visit([s](auto^& arg^)
echo {
echo using T = std::decay_t<decltype(arg^)>;
echo auto view = s->view<T^>();
echo for (auto e : view^)
echo {
echo goop::Entity(e, s).getComponent<T^>().init^();
echo }
echo ^}, variant^);
echo }
) >> CustomComponents.h
)

(
echo }
echo.
echo inline void updateCustomComponents(goop::Scene* s, float dt^)
echo {
) >> CustomComponents.h

:: Checking if there are .cpp files
if %file_count% neq 0 (
(
echo for (auto^& [name, factory] : customComponentFactoryMap^)
echo {
echo auto variant = factory(entt::null, s^);
echo std::visit([s, dt](auto^& arg^)
echo {
echo using T = std::decay_t<decltype(arg^)>;
echo auto view = s->view<T^>();
echo for (auto e : view^)
echo {
echo goop::Entity(e, s).getComponent<T^>().update(dt^);
echo }
echo ^}, variant^);
echo }
) >> CustomComponents.h
)

(
echo }
echo.
echo inline void guiCustomComponents(goop::Scene* s^)
echo {
) >> CustomComponents.h

:: Checking if there are .cpp files
if %file_count% neq 0 (
(
echo for (auto^& [name, factory] : customComponentFactoryMap^)
echo {
echo auto variant = factory(entt::null, s^);
echo std::visit([s, ^&name](auto^& arg^)
echo {
echo using T = std::decay_t<decltype(arg^)>;
echo auto view = s->view<T^>();
echo for (auto e : view^)
echo {
echo ImGui::Text("^%s^", name.c_str()^);
echo goop::Entity(e, s).getComponent<T^>().gui^();
echo }
echo ^}, variant^);
echo }
) >> CustomComponents.h
)

(
echo }
echo.
echo inline void addCustomComponent(const std::string^& name, goop::Entity e, goop::Scene* scene^)
echo {
) >> CustomComponents.h

:: Checking if there are .cpp files
if %file_count% neq 0 (
(
echo auto variant = customComponentFactoryMap[name](e.getEntity(), scene^);
echo std::visit([e](auto^& arg^)
echo {
echo using T = std::decay_t<decltype(arg^)>;
echo e.addComponent<T>(arg^);
echo ^}, variant^);
) >> CustomComponents.h
)

(
echo }
echo.
echo inline void saveCustomComponents(goop::Scene* scene, goop::Entity e, nlohmann::json^& json^)
echo {
) >> CustomComponents.h

:: Checking if there are .cpp files
if %file_count% neq 0 (
(
echo for (auto^& [n, factory] : customComponentFactoryMap^)
echo {
echo auto variant = factory(entt::null, nullptr^);
echo std::visit([&e, ^&json, ^&n](auto^& arg^)
echo {
echo using T = std::decay_t<decltype(arg^)>;
echo if (e.hasComponent<T>()^)
echo {
echo nlohmann::json j;
echo j["type"] = n;
echo json.push_back(j^);
echo }
echo ^}, variant^);
echo }
) >> CustomComponents.h
)

(
echo }
) >> CustomComponents.h

:: Update the hash in the first line comment of CustomComponents.h
powershell -Command "(Get-Content CustomComponents.h) | ForEach-Object { $_ -replace '\/\/.*', '\/\/!updated_hash!' } | Set-Content CustomComponents.h"

echo CustomComponents.h updated
8 changes: 8 additions & 0 deletions editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ file(GLOB_RECURSE APP_SRC_FILES "*.cpp" "*.h")
# Create a library for the "app" source files
add_library(${PROJECT_NAME} ${APP_SRC_FILES})

if(WIN32)
add_custom_target(editor_update_components
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../components/updateComponents.bat
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running updateComponents.bat script"
)
else()
add_custom_target(editor_update_components
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../components/updateComponents.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running updateComponents.sh script"
)
endif()

add_dependencies(${PROJECT_NAME} editor_update_components)
include_directories("../libs/json/")
Expand Down
Loading

0 comments on commit d9dc703

Please sign in to comment.