[godot] Clean-up SkeletonFileResource

This commit is contained in:
Mario Zechner 2022-04-20 10:48:37 +02:00
parent 83c0e95405
commit 5c58111ab3
2 changed files with 48 additions and 45 deletions

View File

@ -28,67 +28,63 @@
*****************************************************************************/ *****************************************************************************/
#include "SpineSkeletonFileResource.h" #include "SpineSkeletonFileResource.h"
#include "core/os/file_access.h"
void SpineSkeletonFileResource::_bind_methods() { void SpineSkeletonFileResource::_bind_methods() {
} }
Error SpineSkeletonFileResource::load_from_file(const String &p_path) { Error SpineSkeletonFileResource::load_from_file(const String &path) {
Error err; Error error;
if (path.ends_with("spjson"))
if (p_path.ends_with("spjson")) json = FileAccess::get_file_as_string(path, &error);
json = FileAccess::get_file_as_string(p_path, &err);
else else
binary = FileAccess::get_file_as_array(p_path, &err); binary = FileAccess::get_file_as_array(path, &error);
return err; return error;
} }
Error SpineSkeletonFileResource::save_to_file(const String &p_path) { Error SpineSkeletonFileResource::save_to_file(const String &path) {
Error err; Error error;
FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err); FileAccess *file = FileAccess::open(path, FileAccess::WRITE, &error);
if (err != OK) { if (error != OK) {
if (file) file->close(); if (file) file->close();
return err; return error;
} }
if (!is_binary()) if (!is_binary())
file->store_string(json); file->store_string(json);
else else
file->store_buffer(binary.ptr(), binary.size()); file->store_buffer(binary.ptr(), binary.size());
file->close(); file->close();
return OK; return OK;
} }
RES SpineSkeletonFileResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) { RES SpineSkeletonFileResourceFormatLoader::load(const String &path, const String &original_path, Error *error) {
Ref<SpineSkeletonFileResource> skeleton = memnew(SpineSkeletonFileResource); Ref<SpineSkeletonFileResource> skeleton_file = memnew(SpineSkeletonFileResource);
skeleton->load_from_file(p_path); skeleton_file->load_from_file(path);
if (error) *error = OK;
if (r_error) { return skeleton_file;
*r_error = OK;
}
return skeleton;
} }
void SpineSkeletonFileResourceFormatLoader::get_recognized_extensions(List<String> *r_extensions) const { void SpineSkeletonFileResourceFormatLoader::get_recognized_extensions(List<String> *extensions) const {
r_extensions->push_back("spjson"); extensions->push_back("spjson");
r_extensions->push_back("spskel"); extensions->push_back("spskel");
} }
String SpineSkeletonFileResourceFormatLoader::get_resource_type(const String &p_path) const { String SpineSkeletonFileResourceFormatLoader::get_resource_type(const String &path) const {
return "SpineSkeletonFileResource"; return "SpineSkeletonFileResource";
} }
bool SpineSkeletonFileResourceFormatLoader::handles_type(const String &p_type) const { bool SpineSkeletonFileResourceFormatLoader::handles_type(const String &type) const {
return p_type == "SpineSkeletonFileResource" || ClassDB::is_parent_class(p_type, "SpineSkeletonFileResource"); return type == "SpineSkeletonFileResource" || ClassDB::is_parent_class(type, "SpineSkeletonFileResource");
} }
Error SpineSkeletonFileResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
Ref<SpineSkeletonFileResource> res = p_resource.get_ref_ptr(); Error SpineSkeletonFileResourceFormatSaver::save(const String &path, const RES &resource, uint32_t flags) {
Error error = res->save_to_file(p_path); Ref<SpineSkeletonFileResource> res = resource.get_ref_ptr();
Error error = res->save_to_file(path);
return error; return error;
} }
void SpineSkeletonFileResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { void SpineSkeletonFileResourceFormatSaver::get_recognized_extensions(const RES &resource, List<String> *p_extensions) const {
if (Object::cast_to<SpineSkeletonFileResource>(*p_resource)) { if (Object::cast_to<SpineSkeletonFileResource>(*resource)) {
p_extensions->push_back("spjson"); p_extensions->push_back("spjson");
p_extensions->push_back("spskel"); p_extensions->push_back("spskel");
} }

View File

@ -30,7 +30,6 @@
#ifndef GODOT_SPINESKELETONFILERESOURCE_H #ifndef GODOT_SPINESKELETONFILERESOURCE_H
#define GODOT_SPINESKELETONFILERESOURCE_H #define GODOT_SPINESKELETONFILERESOURCE_H
#include "core/variant_parser.h"
#include "core/io/resource_loader.h" #include "core/io/resource_loader.h"
#include "core/io/resource_saver.h" #include "core/io/resource_saver.h"
@ -44,30 +43,38 @@ protected:
Vector<uint8_t> binary; Vector<uint8_t> binary;
public: public:
inline const bool is_binary() { return !binary.empty(); } const bool is_binary() { return !binary.empty(); }
inline const Vector<uint8_t> &get_binary() { return binary; }
inline const String &get_json() { return json; }
Error load_from_file(const String &p_path); const Vector<uint8_t> &get_binary() { return binary; }
Error save_to_file(const String &p_path);
const String &get_json() { return json; }
Error load_from_file(const String &path);
Error save_to_file(const String &path);
}; };
class SpineSkeletonFileResourceFormatLoader : public ResourceFormatLoader { class SpineSkeletonFileResourceFormatLoader : public ResourceFormatLoader {
GDCLASS(SpineSkeletonFileResourceFormatLoader, ResourceFormatLoader); GDCLASS(SpineSkeletonFileResourceFormatLoader, ResourceFormatLoader);
public: public:
virtual RES load(const String &p_path, const String &p_original_path, Error *r_error = NULL); virtual RES load(const String &path, const String &original_path, Error *error);
virtual void get_recognized_extensions(List<String> *r_extensions) const;
virtual bool handles_type(const String &p_type) const; virtual void get_recognized_extensions(List<String> *extensions) const;
virtual String get_resource_type(const String &p_path) const;
virtual bool handles_type(const String &type) const;
virtual String get_resource_type(const String &path) const;
}; };
class SpineSkeletonFileResourceFormatSaver : public ResourceFormatSaver { class SpineSkeletonFileResourceFormatSaver : public ResourceFormatSaver {
GDCLASS(SpineSkeletonFileResourceFormatSaver, ResourceFormatSaver); GDCLASS(SpineSkeletonFileResourceFormatSaver, ResourceFormatSaver);
public: public:
Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0) override; Error save(const String &path, const RES &resource, uint32_t flags) override;
void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const override;
void get_recognized_extensions(const RES &resource, List<String> *p_extensions) const override;
bool recognize(const RES &p_resource) const override; bool recognize(const RES &p_resource) const override;
}; };