Skip to content

Commit

Permalink
Load xac and xsm with cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemrav committed Jan 31, 2025
1 parent da6c155 commit d176b17
Show file tree
Hide file tree
Showing 8 changed files with 1,599 additions and 4 deletions.
41 changes: 41 additions & 0 deletions extension/src/openvic-extension/singletons/ModelSingleton.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ModelSingleton.hpp"

#include <cstddef>
#include <numbers>

#include <godot_cpp/variant/utility_functions.hpp>
Expand All @@ -9,6 +10,9 @@
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"
//#include "godot_cpp/classes/node3d.hpp"
//#include "openvic-simulation/utility/Logger.hpp"
//#include "godot_cpp/classes/file_access.hpp"

using namespace godot;
using namespace OpenVic;
Expand All @@ -19,6 +23,8 @@ void ModelSingleton::_bind_methods() {
OV_BIND_METHOD(ModelSingleton::get_cultural_helmet_model, { "culture" });
OV_BIND_METHOD(ModelSingleton::get_flag_model, { "floating" });
OV_BIND_METHOD(ModelSingleton::get_buildings);
OV_BIND_METHOD(ModelSingleton::get_xsm_animation,{"animation_name"});
OV_BIND_METHOD(ModelSingleton::get_xac_model,{"model_name"});
}

ModelSingleton* ModelSingleton::get_singleton() {
Expand Down Expand Up @@ -481,3 +487,38 @@ TypedArray<Dictionary> ModelSingleton::get_buildings() {

return ret;
}

Ref<Animation> ModelSingleton::get_xsm_animation(String source_file) {
const xsm_map_t::const_iterator it = xsm_cache.find(source_file);
if(it != xsm_cache.end()) {
//Logger::info("Load XSM Animation from cache: ",Utilities::godot_to_std_string(source_file));
return it->second;
}

GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

String path = game_singleton->lookup_file_path(source_file);

//Logger::info("Load XSM Animation from file: ",Utilities::godot_to_std_string(source_file));

Ref<Animation> anim = _load_xsm_animation(FileAccess::open(path, FileAccess::READ));
xsm_cache.emplace(source_file,anim);
return anim;
}

Node3D* ModelSingleton::get_xac_model(String source_file) {
const xac_map_t::const_iterator it = xac_cache.find(source_file);
if(it != xac_cache.end()) {
//Logger::info("Load XAC Model from cache: ",Utilities::godot_to_std_string(source_file));
return it->second; //TODO: do we need to add a .instantiate or something to make it a duplicate node?
}

GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

String path = game_singleton->lookup_file_path(source_file);
Node3D* node = _load_xac_model(FileAccess::open(path, FileAccess::READ));
xac_cache.emplace(source_file,node);
return node;
}
13 changes: 13 additions & 0 deletions extension/src/openvic-extension/singletons/ModelSingleton.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#pragma once

#include <string_view>
#include <godot_cpp/classes/animation.hpp>
#include <godot_cpp/classes/object.hpp>

#include <openvic-simulation/interface/GFXObject.hpp>
#include <openvic-simulation/military/UnitInstanceGroup.hpp>
#include <openvic-simulation/types/OrderedContainers.hpp>
#include "../utility/XSMLoader.hpp"
#include "../utility/XACLoader.hpp"
//#include "godot_cpp/classes/node.hpp"
#include "godot_cpp/classes/node3d.hpp"

namespace OpenVic {
struct BuildingInstance;
Expand All @@ -31,9 +37,13 @@ namespace OpenVic {

using animation_map_t = deque_ordered_map<GFX::Actor::Animation const*, godot::Dictionary>;
using model_map_t = deque_ordered_map<GFX::Actor const*, godot::Dictionary>;
using xsm_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::Animation>>;
using xac_map_t = deque_ordered_map<godot::StringName, godot::Node3D*>;

animation_map_t animation_cache;
model_map_t model_cache;
xsm_map_t xsm_cache;
xac_map_t xac_cache;

godot::Dictionary get_animation_dict(GFX::Actor::Animation const& animation);
godot::Dictionary get_model_dict(GFX::Actor const& actor);
Expand All @@ -56,5 +66,8 @@ namespace OpenVic {
godot::Dictionary get_flag_model(bool floating);

godot::TypedArray<godot::Dictionary> get_buildings();

godot::Ref<godot::Animation> get_xsm_animation(godot::String source_file);
godot::Node3D* get_xac_model(godot::String source_file);
};
}
Empty file.
Loading

0 comments on commit d176b17

Please sign in to comment.