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 authored and Spartan322 committed Feb 28, 2025
1 parent 9e5fc1b commit 6016b8f
Show file tree
Hide file tree
Showing 10 changed files with 1,762 additions and 11 deletions.
14 changes: 14 additions & 0 deletions extension/doc_classes/ModelSingleton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@
<description>
</description>
</method>
<method name="get_xac_model">
<return type="Node3D" />
<param index="0" name="model_name" type="String" />
<description>
Gets the animation from an internal cache or file system using the provided file path.
</description>
</method>
<method name="get_xsm_animation">
<return type="Animation" />
<param index="0" name="animation_name" type="String" />
<description>
Gets the animation from an internal cache or file system using the provided file path.
</description>
</method>
</methods>
</class>
40 changes: 40 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,7 @@
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"
#include "godot_cpp/classes/node3d.hpp"

using namespace godot;
using namespace OpenVic;
Expand All @@ -19,6 +21,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 +485,39 @@ 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()) {
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()) {
return (Node3D*)it->second->duplicate();
}

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 XAC Model from file: ",Utilities::godot_to_std_string(source_file));

Node3D* node = _load_xac_model(FileAccess::open(path, FileAccess::READ));
xac_cache.emplace(source_file,node);
return node;
}
12 changes: 12 additions & 0 deletions extension/src/openvic-extension/singletons/ModelSingleton.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#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/node3d.hpp"

namespace OpenVic {
struct BuildingInstance;
Expand All @@ -31,9 +36,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 +65,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 6016b8f

Please sign in to comment.