CubismLogging.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using AOT;
  8. using Live2D.Cubism.Core.Unmanaged;
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using UnityEngine;
  12. namespace Live2D.Cubism.Core
  13. {
  14. /// <summary>
  15. /// Wrapper for core logs.
  16. /// </summary>
  17. internal static class CubismLogging
  18. {
  19. #region Delegates
  20. /// <summary>
  21. /// Delegate compatible with unmanaged log function.
  22. /// </summary>
  23. /// <param name="message">Message to log.</param>
  24. private unsafe delegate void UnmanagedLogDelegate(char* message);
  25. #endregion
  26. /// <summary>
  27. /// Delegate to pass to native Api.
  28. /// </summary>
  29. // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
  30. private static UnmanagedLogDelegate LogDelegate { get; set; }
  31. #region Initialization
  32. /// <summary>
  33. /// Registers delegates.
  34. /// </summary>
  35. [RuntimeInitializeOnLoadMethod]
  36. // ReSharper disable once UnusedMember.Local
  37. private static unsafe void Initialize()
  38. {
  39. LogDelegate = LogUnmanaged;
  40. var logFunction = Marshal.GetFunctionPointerForDelegate(LogDelegate);
  41. csmSetLogFunction(logFunction);
  42. }
  43. #endregion
  44. /// <summary>
  45. /// Prints an unmanaged, null-terminated message.
  46. /// </summary>
  47. /// <param name="message">Message to log.</param>
  48. [MonoPInvokeCallback(typeof(UnmanagedLogDelegate))]
  49. private static unsafe void LogUnmanaged(char* message)
  50. {
  51. // Marshal message and log it.
  52. var managedMessage = Marshal.PtrToStringAnsi(new IntPtr(message));
  53. Debug.LogFormat("[Cubism] Core: {0}.", managedMessage);
  54. }
  55. #region Extern C
  56. [DllImport(CubismCoreDll.DllName)]
  57. private static extern void csmSetLogFunction(IntPtr logFunction);
  58. #endregion
  59. }
  60. }