Manual_IO.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../AngelScript/APITemplates.h"
  24. #include "../AngelScript/Manual_IO.h"
  25. namespace Urho3D
  26. {
  27. // This function is called before ASRegisterGenerated()
  28. void ASRegisterManualFirst_IO(asIScriptEngine* engine)
  29. {
  30. }
  31. // ========================================================================================
  32. // unsigned char* VectorBuffer::GetModifiableData() | File: ../IO/VectorBuffer.h
  33. static unsigned char* VectorBufferAt(unsigned index, VectorBuffer* ptr)
  34. {
  35. if (index >= ptr->GetSize())
  36. {
  37. asGetActiveContext()->SetException("Index out of bounds");
  38. return nullptr;
  39. }
  40. return ptr->GetModifiableData() + index;
  41. }
  42. static void RegisterVectorBuffer(asIScriptEngine* engine)
  43. {
  44. // unsigned char* VectorBuffer::GetModifiableData() | File: ../IO/VectorBuffer.h
  45. engine->RegisterObjectMethod("VectorBuffer", "uint8 &opIndex(uint)", asFUNCTION(VectorBufferAt), asCALL_CDECL_OBJLAST);
  46. engine->RegisterObjectMethod("VectorBuffer", "const uint8 &opIndex(uint) const", asFUNCTION(VectorBufferAt), asCALL_CDECL_OBJLAST);
  47. }
  48. // ========================================================================================
  49. #ifdef URHO3D_LOGGING
  50. static void Print(const String& value, bool error)
  51. {
  52. Log::WriteRaw(value + "\n", error);
  53. }
  54. static void Print(int value, bool error)
  55. {
  56. Log::WriteRaw(String(value) + "\n", error);
  57. }
  58. static void Print(long long value, bool error)
  59. {
  60. Log::WriteRaw(String(value) + "\n", error);
  61. }
  62. static void Print(unsigned value, bool error)
  63. {
  64. Log::WriteRaw(String(value) + "\n", error);
  65. }
  66. static void Print(unsigned long long value, bool error)
  67. {
  68. Log::WriteRaw(String(value) + "\n", error);
  69. }
  70. static void Print(float value, bool error)
  71. {
  72. Log::WriteRaw(String(value) + "\n", error);
  73. }
  74. static void Print(bool value, bool error)
  75. {
  76. Log::WriteRaw(String(value) + "\n", error);
  77. }
  78. static void Print(const Variant& value, bool error)
  79. {
  80. Log::WriteRaw(value.ToString() + "\n", error);
  81. }
  82. static void PrintCallStack(bool error)
  83. {
  84. asIScriptContext* context = asGetActiveContext();
  85. if (context)
  86. Log::WriteRaw(Script::GetCallStack(context), error);
  87. }
  88. static void LogWrite(const String& str, bool error, Log* ptr)
  89. {
  90. Log::WriteRaw(str + "\n", error);
  91. }
  92. static void LogTrace(const String& str, Log* ptr)
  93. {
  94. Log::Write(LOG_TRACE, str);
  95. }
  96. static void LogDebug(const String& str, Log* ptr)
  97. {
  98. Log::Write(LOG_DEBUG, str);
  99. }
  100. static void LogInfo(const String& str, Log* ptr)
  101. {
  102. Log::Write(LOG_INFO, str);
  103. }
  104. static void LogWarning(const String& str, Log* ptr)
  105. {
  106. Log::Write(LOG_WARNING, str);
  107. }
  108. static void LogError(const String& str, Log* ptr)
  109. {
  110. Log::Write(LOG_ERROR, str);
  111. }
  112. #else
  113. static void Print(const String& value, bool error) { }
  114. static void Print(int value, bool error) { }
  115. static void Print(long long value, bool error) { }
  116. static void Print(unsigned value, bool error) { }
  117. static void Print(unsigned long long value, bool error) { }
  118. static void Print(float value, bool error) { }
  119. static void Print(bool value, bool error) { }
  120. static void Print(const Variant& value, bool error) { }
  121. static void PrintCallStack(bool error) { }
  122. static void LogWrite(const String& str, bool error, Log* ptr) { }
  123. static void LogTrace(const String& str, Log* ptr) { }
  124. static void LogDebug(const String& str, Log* ptr) { }
  125. static void LogInfo(const String& str, Log* ptr) { }
  126. static void LogWarning(const String& str, Log* ptr) { }
  127. static void LogError(const String& str, Log* ptr) { }
  128. #endif
  129. static void RegisterLog(asIScriptEngine* engine)
  130. {
  131. engine->RegisterObjectMethod("Log", "void Write(const String&in, bool error = false)", asFUNCTION(LogWrite), asCALL_CDECL_OBJLAST);
  132. engine->RegisterObjectMethod("Log", "void Trace(const String&in)", asFUNCTION(LogTrace), asCALL_CDECL_OBJLAST);
  133. engine->RegisterObjectMethod("Log", "void Debug(const String&in)", asFUNCTION(LogDebug), asCALL_CDECL_OBJLAST);
  134. engine->RegisterObjectMethod("Log", "void Info(const String&in)", asFUNCTION(LogInfo), asCALL_CDECL_OBJLAST);
  135. engine->RegisterObjectMethod("Log", "void Warning(const String&in)", asFUNCTION(LogWarning), asCALL_CDECL_OBJLAST);
  136. engine->RegisterObjectMethod("Log", "void Error(const String&in)", asFUNCTION(LogError), asCALL_CDECL_OBJLAST);
  137. // Register also Print() functions for convenience
  138. engine->RegisterGlobalFunction("void Print(const String&in, bool error = false)", asFUNCTIONPR(Print, (const String&, bool), void), asCALL_CDECL);
  139. engine->RegisterGlobalFunction("void Print(int, bool error = false)", asFUNCTIONPR(Print, (int, bool), void), asCALL_CDECL);
  140. engine->RegisterGlobalFunction("void Print(int64, bool error = false)", asFUNCTIONPR(Print, (long long, bool), void), asCALL_CDECL);
  141. engine->RegisterGlobalFunction("void Print(uint, bool error = false)", asFUNCTIONPR(Print, (unsigned, bool), void), asCALL_CDECL);
  142. engine->RegisterGlobalFunction("void Print(uint64, bool error = false)", asFUNCTIONPR(Print, (unsigned long long, bool), void), asCALL_CDECL);
  143. engine->RegisterGlobalFunction("void Print(float, bool error = false)", asFUNCTIONPR(Print, (float, bool), void), asCALL_CDECL);
  144. engine->RegisterGlobalFunction("void Print(bool, bool error = false)", asFUNCTIONPR(Print, (bool, bool), void), asCALL_CDECL);
  145. engine->RegisterGlobalFunction("void Print(const Variant&in, bool error = false)", asFUNCTIONPR(Print, (const Variant&, bool), void), asCALL_CDECL);
  146. engine->RegisterGlobalFunction("void PrintCallStack(bool error = false)", asFUNCTION(PrintCallStack), asCALL_CDECL);
  147. }
  148. // ========================================================================================
  149. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  150. static FileSystem* GetFileSystem()
  151. {
  152. return GetScriptContext()->GetSubsystem<FileSystem>();
  153. }
  154. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  155. static Log* GetLog()
  156. {
  157. return GetScriptContext()->GetSubsystem<Log>();
  158. }
  159. // This function is called after ASRegisterGenerated()
  160. void ASRegisterManualLast_IO(asIScriptEngine* engine)
  161. {
  162. RegisterVectorBuffer(engine);
  163. RegisterLog(engine);
  164. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  165. engine->RegisterGlobalFunction("FileSystem@+ get_fileSystem()", asFUNCTION(GetFileSystem), asCALL_CDECL);
  166. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  167. engine->RegisterGlobalFunction("Log@+ get_log()", asFUNCTION(GetLog), asCALL_CDECL);
  168. }
  169. // ========================================================================================
  170. // virtual unsigned Deserializer::Read(void *dest, unsigned size)=0 | File: ../IO/Deserializer.h
  171. CScriptArray* DeserializerRead(unsigned size, Deserializer* ptr)
  172. {
  173. PODVector<unsigned char> vector(size);
  174. unsigned bytesRead = size ? ptr->Read(&vector[0], size) : 0;
  175. vector.Resize(bytesRead);
  176. return VectorToArray(vector, "Array<uint8>");
  177. }
  178. // VectorBuffer(Deserializer& source, unsigned size) | File: .. / IO / VectorBuffer.h
  179. VectorBuffer DeserializerReadVectorBuffer(unsigned size, Deserializer* ptr)
  180. {
  181. return VectorBuffer(*ptr, size);
  182. }
  183. // ========================================================================================
  184. // virtual unsigned Serializer::Write(const void *data, unsigned size)=0 | File: ../IO/Serializer.h
  185. unsigned SerializerWrite(CScriptArray* arr, Serializer* ptr)
  186. {
  187. unsigned bytesToWrite = arr->GetSize();
  188. return bytesToWrite ? ptr->Write(arr->At(0), bytesToWrite) : 0;
  189. }
  190. // virtual unsigned Serializer::Write(const void *data, unsigned size)=0 | File: ../IO/Serializer.h
  191. bool SerializerWriteVectorBuffer(VectorBuffer* src, Serializer* ptr)
  192. {
  193. return ptr->Write(src->GetData(), src->GetSize()) == src->GetSize();
  194. }
  195. // ========================================================================================
  196. // void FileSystem::ScanDir(Vector< String > &result, const String &pathName, const String &filter, unsigned flags, bool recursive) const | File: ../IO/FileSystem.h
  197. CScriptArray* FileSystemScanDir(const String& pathName, const String& filter, unsigned flags, bool recursive, FileSystem* ptr)
  198. {
  199. Vector<String> result;
  200. ptr->ScanDir(result, pathName, filter, flags, recursive);
  201. return VectorToArray<String>(result, "Array<String>");
  202. }
  203. }