From d315b0fb8aa03ee6ecc7d93d884b606dc19c6ad5 Mon Sep 17 00:00:00 2001 From: Daniele Giuliani Date: Wed, 16 May 2018 00:50:57 +0200 Subject: [PATCH] added get_creation_time function for gdscript --- core/bind/core_bind.cpp | 8 ++++++++ core/bind/core_bind.h | 1 + core/io/file_access_buffered_fa.h | 6 ++++++ core/io/file_access_compressed.cpp | 9 +++++++++ core/io/file_access_compressed.h | 1 + core/io/file_access_encrypted.cpp | 6 ++++++ core/io/file_access_encrypted.h | 1 + core/io/file_access_memory.h | 1 + core/io/file_access_network.cpp | 18 ++++++++++++++++++ core/io/file_access_network.h | 1 + core/io/file_access_pack.h | 1 + core/io/file_access_zip.h | 1 + core/io/resource_loader.cpp | 1 + core/io/resource_saver.cpp | 1 + core/os/file_access.cpp | 13 +++++++++++++ core/os/file_access.h | 2 ++ drivers/unix/dir_access_unix.cpp | 20 ++++++++++++++++++++ drivers/unix/dir_access_unix.h | 1 + drivers/unix/file_access_unix.cpp | 16 ++++++++++++++++ drivers/unix/file_access_unix.h | 1 + drivers/windows/file_access_windows.cpp | 20 ++++++++++++++++++++ drivers/windows/file_access_windows.h | 1 + modules/mono/mono_gd/gd_mono_assembly.h | 2 ++ platform/android/file_access_android.h | 1 + 24 files changed, 133 insertions(+) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 3270b33f1ca..fa0921ae956 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -1855,6 +1855,12 @@ uint64_t _File::get_modified_time(const String &p_file) const { return FileAccess::get_modified_time(p_file); } +// NEW FUNCTION +uint64_t _File::get_creation_time(const String &p_file) const { + + return FileAccess::get_creation_time(p_file); +} + void _File::_bind_methods() { ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted); @@ -1906,6 +1912,7 @@ void _File::_bind_methods() { ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists); ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time); + ClassDB::bind_method(D_METHOD("get_creation_time", "file"), &_File::get_creation_time); // NEW FUNCTION ADD_PROPERTY(PropertyInfo(Variant::BOOL, "endian_swap"), "set_endian_swap", "get_endian_swap"); @@ -2106,6 +2113,7 @@ void _Directory::_bind_methods() { ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists); ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists); //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time); + //ClassDB::bind_method(D_METHOD("get_creation_time","file"),&_Directory::get_creation_time); // NEW FUNCTION ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left); ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy); ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index a363f5970f7..4bf4fd91460 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -481,6 +481,7 @@ public: bool file_exists(const String &p_name) const; ///< return true if a file exists uint64_t get_modified_time(const String &p_file) const; + uint64_t get_creation_time(const String &p_file) const; // NEW FUNCTION _File(); virtual ~_File(); diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h index 493fa1c2430..f9b3b29b885 100644 --- a/core/io/file_access_buffered_fa.h +++ b/core/io/file_access_buffered_fa.h @@ -143,6 +143,12 @@ public: return f._get_modified_time(p_file); } +// NEW FUNCTION + virtual uint64_t _get_creation_time(const String &p_file) { + + return f._get_creation_time(p_file); + } + FileAccessBufferedFA(){ }; diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index d6547ba19f9..5f08c4fc7ca 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -372,6 +372,15 @@ uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) { return 0; } +// NEW FUNCTION +uint64_t FileAccessCompressed::_get_creation_time(const String &p_file) { + + if (f) + return f->get_creation_time(p_file); + else + return 0; +} + FileAccessCompressed::FileAccessCompressed() { f = NULL; diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index 587f58a7c6f..834e5b18307 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -91,6 +91,7 @@ public: virtual bool file_exists(const String &p_name); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file); + virtual uint64_t _get_creation_time(const String &p_file);// NEW FUNCTION FileAccessCompressed(); virtual ~FileAccessCompressed(); diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index 221f457b783..84268c66983 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -302,6 +302,12 @@ uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) { return 0; } +// NEW FUNCTION +uint64_t FileAccessEncrypted::_get_creation_time(const String &p_file) { + + return 0; +} + FileAccessEncrypted::FileAccessEncrypted() { file = NULL; diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index b9365a9fd07..7cc9ab66379 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -79,6 +79,7 @@ public: virtual bool file_exists(const String &p_name); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file); + virtual uint64_t _get_creation_time(const String &p_file); // NEW FUNCTION FileAccessEncrypted(); ~FileAccessEncrypted(); diff --git a/core/io/file_access_memory.h b/core/io/file_access_memory.h index 2136f4cc0cd..3317c20e01f 100644 --- a/core/io/file_access_memory.h +++ b/core/io/file_access_memory.h @@ -70,6 +70,7 @@ public: virtual bool file_exists(const String &p_name); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file) { return 0; } + virtual uint64_t _get_creation_time(const String &p_file) { return 0; } // NEW FUNCTION FileAccessMemory(); }; diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 21e3a4172b7..98403bf3f1a 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -498,6 +498,24 @@ uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) { return exists_modtime; } +// NEW FUNCTION +uint64_t FileAccessNetwork::_get_creation_time(const String &p_file) { + + FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; + nc->lock_mutex(); + nc->put_32(id); + nc->put_32(COMMAND_GET_MODTIME); + CharString cs = p_file.utf8(); + nc->put_32(cs.length()); + nc->client->put_data((const uint8_t *)cs.ptr(), cs.length()); + nc->unlock_mutex(); + DEBUG_PRINT("MODTIME POST"); + nc->sem->post(); + sem->wait(); + + return exists_modtime; +} + void FileAccessNetwork::configure() { GLOBAL_DEF("network/remote_fs/page_size", 65536); diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index be9bdb1af6a..debd4386b8b 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -162,6 +162,7 @@ public: virtual bool file_exists(const String &p_path); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file); + virtual uint64_t _get_creation_time(const String &p_file); // NEW FUNCTION static void configure(); diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 8a40e6d78c0..513e2896c30 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -142,6 +142,7 @@ class FileAccessPack : public FileAccess { FileAccess *f; virtual Error _open(const String &p_path, int p_mode_flags); virtual uint64_t _get_modified_time(const String &p_file) { return 0; } + virtual uint64_t _get_creation_time(const String &p_file) { return 0; } // NEW FUNCTION public: virtual void close(); diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index df83575f6ac..11f9687e776 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -114,6 +114,7 @@ public: virtual bool file_exists(const String &p_name); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file) { return 0; } // todo + virtual uint64_t _get_creation_time(const String &p_file) { return 0; } // NEW FUNCTION FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file); ~FileAccessZip(); diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 1351030d1ec..86bf77318e1 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -231,6 +231,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p res->set_edited(false); if (timestamp_on_load) { uint64_t mt = FileAccess::get_modified_time(path); + uint64_t ct = FileAccess::get_creation_time(path); // NEW FUNCTION //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt); res->set_last_modified_time(mt); } diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 3dcd94880a9..e4bfd5a3210 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -80,6 +80,7 @@ Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t ((Resource *)p_resource.ptr())->set_edited(false); if (timestamp_on_save) { uint64_t mt = FileAccess::get_modified_time(p_path); + uint64_t ct = FileAccess::get_creation_time(p_path); // NEW FUNCTION ((Resource *)p_resource.ptr())->set_last_modified_time(mt); } diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 033b4b12b9e..96ebb79da56 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -489,6 +489,19 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { memdelete(fa); return mt; } +// NEW FUNCTION +uint64_t FileAccess::get_creation_time(const String &p_file) { + + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) + return 0; + + FileAccess *fa = create_for_path(p_file); + ERR_FAIL_COND_V(!fa, 0); + + uint64_t ct = fa->_get_creation_time(p_file); + memdelete(fa); + return ct; +} void FileAccess::store_string(const String &p_string) { diff --git a/core/os/file_access.h b/core/os/file_access.h index c4635fdfbbe..2b9477a79c4 100644 --- a/core/os/file_access.h +++ b/core/os/file_access.h @@ -59,6 +59,7 @@ protected: String fix_path(const String &p_path) const; virtual Error _open(const String &p_path, int p_mode_flags) = 0; ///< open a file virtual uint64_t _get_modified_time(const String &p_file) = 0; + virtual uint64_t _get_creation_time(const String &p_file) = 0; // NEW FUNCTION static FileCloseFailNotify close_fail_notify; @@ -153,6 +154,7 @@ public: static CreateFunc get_create_func(AccessType p_access); static bool exists(const String &p_name); ///< return true if a file exists static uint64_t get_modified_time(const String &p_file); + static uint64_t get_creation_time(const String &p_file); // NEW FUNCTION static void set_backup_save(bool p_enable) { backup_save = p_enable; }; static bool is_backup_save_enabled() { return backup_save; }; diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index 5a4be6df4f5..8b2d05c7e9e 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -124,6 +124,26 @@ uint64_t DirAccessUnix::get_modified_time(String p_file) { return 0; }; +// NEW FUNCTION +uint64_t DirAccessUnix::get_creation_time(String p_file) { + + if (p_file.is_rel_path()) + p_file = current_dir.plus_file(p_file); + + p_file = fix_path(p_file); + + struct stat flags; + bool success = (stat(p_file.utf8().get_data(), &flags) == 0); + + if (success) { + return flags.st_ctime; + } else { + + ERR_FAIL_V(0); + }; + return 0; +}; + String DirAccessUnix::get_next() { if (!dir_stream) diff --git a/drivers/unix/dir_access_unix.h b/drivers/unix/dir_access_unix.h index a55acdbd344..815de5239c4 100644 --- a/drivers/unix/dir_access_unix.h +++ b/drivers/unix/dir_access_unix.h @@ -75,6 +75,7 @@ public: virtual bool dir_exists(String p_dir); virtual uint64_t get_modified_time(String p_file); + virtual uint64_t get_creation_time(String p_file); // NEW FUNCTION virtual Error rename(String p_path, String p_new_path); virtual Error remove(String p_path); diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index c25d34125d7..76b2676c71a 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -297,6 +297,22 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { }; } +// NEW FUNCTION +uint64_t FileAccessUnix::_get_creation_time(const String &p_file) { + + String file = fix_path(p_file); + struct stat flags; + int err = stat(file.utf8().get_data(), &flags); + + if (!err) { + return flags.st_ctime; + } else { + print_line("ERROR IN: " + p_file); + + ERR_FAIL_V(0); + }; +} + Error FileAccessUnix::_chmod(const String &p_path, int p_mod) { int err = chmod(p_path.utf8().get_data(), p_mod); if (!err) { diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index 88bb39fbd10..f81a4847d22 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -84,6 +84,7 @@ public: virtual bool file_exists(const String &p_path); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file); + virtual uint64_t _get_creation_time(const String &p_file); // NEW FUNCTION virtual Error _chmod(const String &p_path, int p_mod); diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp index aa0fd34e0a6..17d279e908b 100644 --- a/drivers/windows/file_access_windows.cpp +++ b/drivers/windows/file_access_windows.cpp @@ -311,6 +311,26 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) { ERR_FAIL_V(0); }; +// NEW FUNCTION +uint64_t FileAccessWindows::_get_creation_time(const String &p_file) { + + String file = fix_path(p_file); + if (file.ends_with("/") && file != "/") + file = file.substr(0, file.length() - 1); + + struct _stat st; + int rv = _wstat(file.c_str(), &st); + + if (rv == 0) { + + return st.st_ctime; + } else { + print_line("no access to " + file); + } + + ERR_FAIL_V(0); +}; + FileAccessWindows::FileAccessWindows() { f = NULL; diff --git a/drivers/windows/file_access_windows.h b/drivers/windows/file_access_windows.h index 0462c1e942a..ee83acffd7b 100644 --- a/drivers/windows/file_access_windows.h +++ b/drivers/windows/file_access_windows.h @@ -77,6 +77,7 @@ public: virtual bool file_exists(const String &p_name); ///< return true if a file exists uint64_t _get_modified_time(const String &p_file); + uint64_t _get_creation_time(const String &p_file); // NEW FUNCTION FileAccessWindows(); virtual ~FileAccessWindows(); diff --git a/modules/mono/mono_gd/gd_mono_assembly.h b/modules/mono/mono_gd/gd_mono_assembly.h index 5cf744a5a2d..187eb10af29 100644 --- a/modules/mono/mono_gd/gd_mono_assembly.h +++ b/modules/mono/mono_gd/gd_mono_assembly.h @@ -77,6 +77,7 @@ class GDMonoAssembly { String name; String path; uint64_t modified_time; + uint64_t creation_time; // NEW FUNCTION HashMap cached_classes; Map cached_raw; @@ -116,6 +117,7 @@ public: _FORCE_INLINE_ String get_name() const { return name; } _FORCE_INLINE_ String get_path() const { return path; } _FORCE_INLINE_ uint64_t get_modified_time() const { return modified_time; } + _FORCE_INLINE_ uint64_t get_creation_time() const { return creation_time; } // NEW FUNCTION GDMonoClass *get_class(const StringName &p_namespace, const StringName &p_name); GDMonoClass *get_class(MonoClass *p_mono_class); diff --git a/platform/android/file_access_android.h b/platform/android/file_access_android.h index 03f4c595217..3d51e310054 100644 --- a/platform/android/file_access_android.h +++ b/platform/android/file_access_android.h @@ -70,6 +70,7 @@ public: virtual bool file_exists(const String &p_path); ///< return true if a file exists virtual uint64_t _get_modified_time(const String &p_file) { return 0; } + virtual uint64_t _get_creation_time(const String &p_file) { return 0; } // NEW FUNCTION //static void make_default();