Manual_IO.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../AngelScript/APITemplates.h"
  5. #include "../AngelScript/Manual_IO.h"
  6. namespace Urho3D
  7. {
  8. // This function is called before ASRegisterGenerated()
  9. void ASRegisterManualFirst_IO(asIScriptEngine* engine)
  10. {
  11. }
  12. // ========================================================================================
  13. // byte* VectorBuffer::GetModifiableData() | File: ../IO/VectorBuffer.h
  14. static byte* VectorBufferAt(unsigned index, VectorBuffer* ptr)
  15. {
  16. if (index >= ptr->GetSize())
  17. {
  18. asGetActiveContext()->SetException("Index out of bounds");
  19. return nullptr;
  20. }
  21. return ptr->GetModifiableData() + index;
  22. }
  23. static void RegisterVectorBuffer(asIScriptEngine* engine)
  24. {
  25. // unsigned char* VectorBuffer::GetModifiableData() | File: ../IO/VectorBuffer.h
  26. engine->RegisterObjectMethod("VectorBuffer", "uint8 &opIndex(uint)", AS_FUNCTION_OBJLAST(VectorBufferAt), AS_CALL_CDECL_OBJLAST);
  27. engine->RegisterObjectMethod("VectorBuffer", "const uint8 &opIndex(uint) const", AS_FUNCTION_OBJLAST(VectorBufferAt), AS_CALL_CDECL_OBJLAST);
  28. }
  29. // ========================================================================================
  30. #ifdef URHO3D_LOGGING
  31. static void Print(const String& value, bool error)
  32. {
  33. Log::WriteRaw(value + "\n", error);
  34. }
  35. static void Print(int value, bool error)
  36. {
  37. Log::WriteRaw(String(value) + "\n", error);
  38. }
  39. static void Print(long long value, bool error)
  40. {
  41. Log::WriteRaw(String(value) + "\n", error);
  42. }
  43. static void Print(unsigned value, bool error)
  44. {
  45. Log::WriteRaw(String(value) + "\n", error);
  46. }
  47. static void Print(unsigned long long value, bool error)
  48. {
  49. Log::WriteRaw(String(value) + "\n", error);
  50. }
  51. static void Print(float value, bool error)
  52. {
  53. Log::WriteRaw(String(value) + "\n", error);
  54. }
  55. static void Print(bool value, bool error)
  56. {
  57. Log::WriteRaw(String(value) + "\n", error);
  58. }
  59. static void Print(const Variant& value, bool error)
  60. {
  61. Log::WriteRaw(value.ToString() + "\n", error);
  62. }
  63. static void PrintCallStack(bool error)
  64. {
  65. asIScriptContext* context = asGetActiveContext();
  66. if (context)
  67. Log::WriteRaw(Script::GetCallStack(context), error);
  68. }
  69. static void LogWrite(const String& str, bool error, Log* ptr)
  70. {
  71. Log::WriteRaw(str + "\n", error);
  72. }
  73. static void LogTrace(const String& str, Log* ptr)
  74. {
  75. Log::Write(LOG_TRACE, str);
  76. }
  77. static void LogDebug(const String& str, Log* ptr)
  78. {
  79. Log::Write(LOG_DEBUG, str);
  80. }
  81. static void LogInfo(const String& str, Log* ptr)
  82. {
  83. Log::Write(LOG_INFO, str);
  84. }
  85. static void LogWarning(const String& str, Log* ptr)
  86. {
  87. Log::Write(LOG_WARNING, str);
  88. }
  89. static void LogError(const String& str, Log* ptr)
  90. {
  91. Log::Write(LOG_ERROR, str);
  92. }
  93. #else
  94. static void Print(const String& value, bool error) { }
  95. static void Print(int value, bool error) { }
  96. static void Print(long long value, bool error) { }
  97. static void Print(unsigned value, bool error) { }
  98. static void Print(unsigned long long value, bool error) { }
  99. static void Print(float value, bool error) { }
  100. static void Print(bool value, bool error) { }
  101. static void Print(const Variant& value, bool error) { }
  102. static void PrintCallStack(bool error) { }
  103. static void LogWrite(const String& str, bool error, Log* ptr) { }
  104. static void LogTrace(const String& str, Log* ptr) { }
  105. static void LogDebug(const String& str, Log* ptr) { }
  106. static void LogInfo(const String& str, Log* ptr) { }
  107. static void LogWarning(const String& str, Log* ptr) { }
  108. static void LogError(const String& str, Log* ptr) { }
  109. #endif
  110. static void RegisterLog(asIScriptEngine* engine)
  111. {
  112. engine->RegisterObjectMethod("Log", "void Write(const String&in, bool error = false)", AS_FUNCTION_OBJLAST(LogWrite), AS_CALL_CDECL_OBJLAST);
  113. engine->RegisterObjectMethod("Log", "void Trace(const String&in)", AS_FUNCTION_OBJLAST(LogTrace), AS_CALL_CDECL_OBJLAST);
  114. engine->RegisterObjectMethod("Log", "void Debug(const String&in)", AS_FUNCTION_OBJLAST(LogDebug), AS_CALL_CDECL_OBJLAST);
  115. engine->RegisterObjectMethod("Log", "void Info(const String&in)", AS_FUNCTION_OBJLAST(LogInfo), AS_CALL_CDECL_OBJLAST);
  116. engine->RegisterObjectMethod("Log", "void Warning(const String&in)", AS_FUNCTION_OBJLAST(LogWarning), AS_CALL_CDECL_OBJLAST);
  117. engine->RegisterObjectMethod("Log", "void Error(const String&in)", AS_FUNCTION_OBJLAST(LogError), AS_CALL_CDECL_OBJLAST);
  118. // Register also Print() functions for convenience
  119. engine->RegisterGlobalFunction("void Print(const String&in, bool error = false)", AS_FUNCTIONPR(Print, (const String&, bool), void), AS_CALL_CDECL);
  120. engine->RegisterGlobalFunction("void Print(int, bool error = false)", AS_FUNCTIONPR(Print, (int, bool), void), AS_CALL_CDECL);
  121. engine->RegisterGlobalFunction("void Print(int64, bool error = false)", AS_FUNCTIONPR(Print, (long long, bool), void), AS_CALL_CDECL);
  122. engine->RegisterGlobalFunction("void Print(uint, bool error = false)", AS_FUNCTIONPR(Print, (unsigned, bool), void), AS_CALL_CDECL);
  123. engine->RegisterGlobalFunction("void Print(uint64, bool error = false)", AS_FUNCTIONPR(Print, (unsigned long long, bool), void), AS_CALL_CDECL);
  124. engine->RegisterGlobalFunction("void Print(float, bool error = false)", AS_FUNCTIONPR(Print, (float, bool), void), AS_CALL_CDECL);
  125. engine->RegisterGlobalFunction("void Print(bool, bool error = false)", AS_FUNCTIONPR(Print, (bool, bool), void), AS_CALL_CDECL);
  126. engine->RegisterGlobalFunction("void Print(const Variant&in, bool error = false)", AS_FUNCTIONPR(Print, (const Variant&, bool), void), AS_CALL_CDECL);
  127. engine->RegisterGlobalFunction("void PrintCallStack(bool error = false)", AS_FUNCTION(PrintCallStack), AS_CALL_CDECL);
  128. }
  129. // ========================================================================================
  130. // template <class T> T* Context::GetSubsystem() const | File: ../Core/Context.h
  131. static FileSystem* GetFileSystem()
  132. {
  133. return GetScriptContext()->GetSubsystem<FileSystem>();
  134. }
  135. // template <class T> T* Context::GetSubsystem() const | File: ../Core/Context.h
  136. static Log* GetLog()
  137. {
  138. return GetScriptContext()->GetSubsystem<Log>();
  139. }
  140. // This function is called after ASRegisterGenerated()
  141. void ASRegisterManualLast_IO(asIScriptEngine* engine)
  142. {
  143. RegisterVectorBuffer(engine);
  144. RegisterLog(engine);
  145. // template <class T> T* Context::GetSubsystem() const | File: ../Core/Context.h
  146. engine->RegisterGlobalFunction("FileSystem@+ get_fileSystem()", AS_FUNCTION(GetFileSystem), AS_CALL_CDECL);
  147. // template <class T> T* Context::GetSubsystem() const | File: ../Core/Context.h
  148. engine->RegisterGlobalFunction("Log@+ get_log()", AS_FUNCTION(GetLog), AS_CALL_CDECL);
  149. }
  150. }