暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Exception.cpp 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "utils/Exception.h"
  2. #include "utils/StringUtils.h"
  3. #include "il2cpp-object-internals.h"
  4. struct Il2CppClass;
  5. namespace il2cpp
  6. {
  7. namespace utils
  8. {
  9. std::string Exception::FormatException(const Il2CppException* ex)
  10. {
  11. std::string exception_namespace = ex->klass->namespaze;
  12. std::string exception_type = ex->klass->name;
  13. if (ex->message)
  14. return exception_namespace + "." + exception_type + ": " + il2cpp::utils::StringUtils::Utf16ToUtf8(il2cpp::utils::StringUtils::GetChars(ex->message));
  15. else
  16. return exception_namespace + "." + exception_type;
  17. }
  18. std::string Exception::FormatInvalidCastException(const Il2CppClass* fromType, const Il2CppClass* toType)
  19. {
  20. std::string message;
  21. if (fromType != NULL && toType != NULL)
  22. {
  23. message += "Unable to cast object of type '";
  24. message += fromType->name;
  25. message += "' to type '";
  26. message += toType->name;
  27. message += "'.";
  28. }
  29. return message;
  30. }
  31. std::string Exception::FormatStackTrace(const Il2CppException* ex)
  32. {
  33. // Exception.RestoreExceptionDispatchInfo() will clear stack_trace, so we need to ensure that we read it only once
  34. Il2CppString* stack_trace = ex->stack_trace;
  35. if (stack_trace)
  36. return il2cpp::utils::StringUtils::Utf16ToUtf8(il2cpp::utils::StringUtils::GetChars(stack_trace));
  37. return "";
  38. }
  39. std::string Exception::FormatBaselibErrorState(const Baselib_ErrorState& errorState)
  40. {
  41. const auto len = Baselib_ErrorState_Explain(&errorState, nullptr, 0, Baselib_ErrorState_ExplainVerbosity_ErrorType_SourceLocation_Explanation);
  42. std::string buffer(len, ' ');
  43. // std::string::data() is const only until C++17
  44. Baselib_ErrorState_Explain(&errorState, &buffer[0], len, Baselib_ErrorState_ExplainVerbosity_ErrorType_SourceLocation_Explanation);
  45. return buffer;
  46. }
  47. } // utils
  48. } // il2cpp