test_io.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* test_io.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_io.h"
  30. #ifdef MINIZIP_ENABLED
  31. #include "os/main_loop.h"
  32. #include "os/os.h"
  33. #include "scene/resources/texture.h"
  34. #include "print_string.h"
  35. #include "io/resource_loader.h"
  36. #include "io/resource_saver.h"
  37. #include "os/dir_access.h"
  38. #include "core/globals.h"
  39. #include "io/file_access_memory.h"
  40. namespace TestIO {
  41. class TestMainLoop : public MainLoop {
  42. bool quit;
  43. public:
  44. virtual void input_event(const InputEvent& p_event) {
  45. }
  46. virtual bool idle(float p_time) {
  47. return false;
  48. }
  49. virtual void request_quit() {
  50. quit=true;
  51. }
  52. virtual void init() {
  53. quit=true;
  54. }
  55. virtual bool iteration(float p_time) {
  56. return quit;
  57. }
  58. virtual void finish() {
  59. }
  60. };
  61. MainLoop* test() {
  62. print_line("this is test io");
  63. DirAccess* da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  64. da->change_dir(".");
  65. print_line("Opening current dir "+ da->get_current_dir());
  66. String entry;
  67. da->list_dir_begin();
  68. while ( (entry = da->get_next()) != "") {
  69. print_line("entry "+entry+" is dir: " + Variant(da->current_is_dir()));
  70. };
  71. da->list_dir_end();
  72. RES texture = ResourceLoader::load("test_data/rock.png");
  73. ERR_FAIL_COND_V(texture.is_null(), NULL);
  74. ResourceSaver::save("test_data/rock.xml",texture);
  75. print_line("localize paths");
  76. print_line(Globals::get_singleton()->localize_path("algo.xml"));
  77. print_line(Globals::get_singleton()->localize_path("c:\\windows\\algo.xml"));
  78. print_line(Globals::get_singleton()->localize_path(Globals::get_singleton()->get_resource_path()+"/something/something.xml"));
  79. print_line(Globals::get_singleton()->localize_path("somedir/algo.xml"));
  80. {
  81. FileAccess* z = FileAccess::open("test_data/archive.zip", FileAccess::READ);
  82. int len = z->get_len();
  83. Vector<uint8_t> zip;
  84. zip.resize(len);
  85. z->get_buffer(&zip[0], len);
  86. z->close();
  87. memdelete(z);
  88. FileAccessMemory::register_file("a_package", zip);
  89. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES);
  90. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM);
  91. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA);
  92. print_line("archive test");
  93. #if 0
  94. Archive arch;
  95. Archive::get_singleton()->add_package("a_package");
  96. FileAccessArchive f;
  97. print_line("opening for read");
  98. f._open("file.txt", FileAccess::READ);
  99. int pos = f.get_pos();
  100. printf("file has %i bytes, initial pos %i\n", (int)f.get_len(), pos);
  101. do {
  102. printf("%c", f.get_8());
  103. } while (!f.eof_reached());
  104. print_line("opening for stored seek");
  105. f.open("seek.bin", FileAccess::READ);
  106. pos = f.get_pos();
  107. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  108. f.seek(128);
  109. pos = f.get_pos();
  110. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  111. print_line("opening for deflated seek");
  112. f.open("seek_deflated.bin", FileAccess::READ);
  113. pos = f.get_pos();
  114. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  115. f.seek(128);
  116. pos = f.get_pos();
  117. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  118. pos = f.get_pos();
  119. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  120. pos = f.get_pos();
  121. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  122. f.seek(256);
  123. pos = f.get_pos();
  124. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  125. pos = f.get_pos();
  126. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  127. pos = f.get_pos();
  128. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  129. f.seek(4);
  130. pos = f.get_pos();
  131. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  132. pos = f.get_pos();
  133. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  134. pos = f.get_pos();
  135. printf("byte at pos %i is %i\n", pos, (int)f.get_8());
  136. f.close();
  137. DirAccessArchive d;
  138. String dir = "../blah1/blah2/blahask/../blah3/.//blah4/";
  139. printf("changing dir to %s\n", dir.utf8().get_data());
  140. d.change_dir(dir);
  141. printf("current dir is %s\n", d.get_current_dir().utf8().get_data());
  142. FileAccessMemory::cleanup();
  143. #endif
  144. };
  145. print_line("test done");
  146. return memnew( TestMainLoop );
  147. }
  148. }
  149. #else
  150. namespace TestIO {
  151. MainLoop* test() {
  152. return NULL;
  153. }
  154. }
  155. #endif