test_io.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* test_io.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "test_io.h"
  31. #ifdef MINIZIP_ENABLED
  32. #include "core/project_settings.h"
  33. #include "io/resource_loader.h"
  34. #include "io/resource_saver.h"
  35. #include "os/dir_access.h"
  36. #include "os/main_loop.h"
  37. #include "os/os.h"
  38. #include "print_string.h"
  39. #include "scene/resources/texture.h"
  40. #include "io/file_access_memory.h"
  41. namespace TestIO {
  42. class TestMainLoop : public MainLoop {
  43. bool quit;
  44. public:
  45. virtual void input_event(const InputEvent &p_event) {
  46. }
  47. virtual bool idle(float p_time) {
  48. return false;
  49. }
  50. virtual void request_quit() {
  51. quit = true;
  52. }
  53. virtual void init() {
  54. quit = true;
  55. }
  56. virtual bool iteration(float p_time) {
  57. return quit;
  58. }
  59. virtual void finish() {
  60. }
  61. };
  62. MainLoop *test() {
  63. print_line("this is test io");
  64. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  65. da->change_dir(".");
  66. print_line("Opening current dir " + da->get_current_dir());
  67. String entry;
  68. da->list_dir_begin();
  69. while ((entry = da->get_next()) != "") {
  70. print_line("entry " + entry + " is dir: " + Variant(da->current_is_dir()));
  71. };
  72. da->list_dir_end();
  73. RES texture = ResourceLoader::load("test_data/rock.png");
  74. ERR_FAIL_COND_V(texture.is_null(), NULL);
  75. ResourceSaver::save("test_data/rock.xml", texture);
  76. print_line("localize paths");
  77. print_line(ProjectSettings::get_singleton()->localize_path("algo.xml"));
  78. print_line(ProjectSettings::get_singleton()->localize_path("c:\\windows\\algo.xml"));
  79. print_line(ProjectSettings::get_singleton()->localize_path(ProjectSettings::get_singleton()->get_resource_path() + "/something/something.xml"));
  80. print_line(ProjectSettings::get_singleton()->localize_path("somedir/algo.xml"));
  81. {
  82. FileAccess *z = FileAccess::open("test_data/archive.zip", FileAccess::READ);
  83. int len = z->get_len();
  84. Vector<uint8_t> zip;
  85. zip.resize(len);
  86. z->get_buffer(&zip[0], len);
  87. z->close();
  88. memdelete(z);
  89. FileAccessMemory::register_file("a_package", zip);
  90. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES);
  91. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM);
  92. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA);
  93. print_line("archive test");
  94. };
  95. print_line("test done");
  96. return memnew(TestMainLoop);
  97. }
  98. } // namespace TestIO
  99. #else
  100. namespace TestIO {
  101. MainLoop *test() {
  102. return NULL;
  103. }
  104. } // namespace TestIO
  105. #endif