Extended.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FF8
  9. {
  10. //Class that provides language extensions made by Maki
  11. static class Extended
  12. {
  13. //https://stackoverflow.com/a/2887/4509036
  14. public static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
  15. {
  16. var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  17. try
  18. {
  19. return (T)Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
  20. }
  21. finally
  22. {
  23. handle.Free();
  24. }
  25. }
  26. #if DEBUG || _WINDOWS
  27. public static void DumpBuffer(byte[] buffer, string path)
  28. => System.IO.File.WriteAllBytes(path, buffer);
  29. public static void DumpBuffer(System.IO.MemoryStream ms, string path)
  30. => System.IO.File.WriteAllBytes(path, ms.GetBuffer());
  31. public static void DumpBuffer(System.IO.MemoryStream ms)
  32. => System.IO.File.WriteAllBytes(GetUnixFullPath(System.IO.Path.Combine(Memory.FF8DIR, "debugUnpack.debug")), ms.GetBuffer());
  33. #endif
  34. //https://stackoverflow.com/questions/1130698/checking-if-an-object-is-a-number-in-c-sharp
  35. public static bool IsNumber(object value) => value is sbyte
  36. || value is byte
  37. || value is short
  38. || value is ushort
  39. || value is int
  40. || value is uint
  41. || value is long
  42. || value is ulong
  43. || value is float
  44. || value is double
  45. || value is decimal;
  46. public static double Distance3D(Vector3 xo, Vector3 xa) => Vector3.Distance(xo, xa);
  47. /// <summary>
  48. /// Some debug text is crashing due to brackets not appearing in chartable. This function removes brackets inside string
  49. /// </summary>
  50. /// <param name="s"></param>
  51. /// <returns></returns>
  52. public static string RemoveBrackets(string s) => s.Replace('{', ' ').Replace('}', ' ');
  53. public static bool GetBit(byte @object, int positionFromRight) => ((@object >> positionFromRight) & 1) > 0;
  54. public static bool GetBit(int @object, int positionFromRight) => ((@object >> positionFromRight) & 1) > 0;
  55. public static bool IsLinux
  56. {
  57. get
  58. {
  59. int p = (int)Environment.OSVersion.Platform;
  60. return (p == 4) || (p == 6) || (p == 128);
  61. }
  62. }
  63. public static string GetUnixFullPath(string pt)
  64. {
  65. #if _WINDOWS
  66. return System.IO.Path.GetFullPath(pt.Replace('/', '\\'));
  67. #else
  68. return System.IO.Path.GetFullPath(pt.Replace("\\", "/"));
  69. #endif
  70. }
  71. public static bool In(int _in, Vector2 range) =>
  72. _in >= range.X && _in <= range.Y;
  73. //: false;
  74. public static bool In(int _in, int min, int max) => In(_in, new Vector2(min, max));
  75. public static Matrix GetRotationMatrixX(float angle)
  76. => new Matrix(
  77. 1, 0, 0, 0,
  78. 0, (float)Math.Cos(Radians(angle)), -(float)Math.Sin(Radians(angle)), 0,
  79. 0, (float)Math.Sin(Radians(angle)), (float)Math.Cos(Radians(angle)), 0,
  80. 0, 0, 0, 0);
  81. public static Matrix GetRotationMatrixY(double angle)
  82. => new Matrix(
  83. (float)Math.Cos(Radians(angle)), 0, (float)Math.Sin(Radians(angle)), 0,
  84. 0, 1, 0, 0,
  85. -(float)Math.Sin(Radians(angle)), 0, (float)Math.Cos(Radians(angle)), 0,
  86. 0, 0, 0, 0);
  87. public static Matrix GetRotationMatrixZ(float angle)
  88. => new Matrix(
  89. (float)Math.Cos(Radians(angle)), -(float)Math.Sin(Radians(angle)), 0, 0,
  90. (float)Math.Sin(Radians(angle)), (float)Math.Cos(Radians(angle)), 0, 0,
  91. 0, 0, 1, 0,
  92. 0, 0, 0, 0);
  93. /// <summary>
  94. /// This Matrix operation performs Matrix multiplication and transposing in-place
  95. /// </summary>
  96. /// <param name="a"></param>
  97. /// <param name="b"></param>
  98. /// <returns></returns>
  99. public static Matrix MatrixMultiply_transpose(Matrix a, Matrix b)
  100. => new Matrix(
  101. b.M11 * a.M11 + b.M21 * a.M12 + b.M31 * a.M13, b.M11 * a.M21 + b.M21 * a.M22 + b.M31 * a.M23, b.M11 * a.M31 + b.M21 * a.M32 + b.M31 * a.M33, 0,
  102. b.M12 * a.M11 + b.M22 * a.M12 + b.M32 * a.M13, b.M12 * a.M21 + b.M22 * a.M22 + b.M32 * a.M23, b.M12 * a.M31 + b.M22 * a.M32 + b.M32 * a.M33, 0,
  103. b.M13 * a.M11 + b.M23 * a.M12 + b.M33 * a.M13, b.M13 * a.M21 + b.M23 * a.M22 + b.M33 * a.M23, b.M13 * a.M31 + b.M23 * a.M32 + b.M33 * a.M33, 0,
  104. 0, 0, 0, 0);
  105. //This is the first time I had issue with precision. Cosinus from 270o was different for float and double. MathHelper is broken...
  106. public static double Radians(double angle) => angle * Math.PI / 180;
  107. public static double Cos(double angle) => Math.Cos(Radians(angle));
  108. public static double Sin(double angle) => Math.Sin(Radians(angle));
  109. public static ushort UshortLittleEndian(ushort ushort_)
  110. => (ushort)((ushort_ << 8) | (ushort_ >> 8));
  111. public static short ShortLittleEndian(short ushort_)
  112. => (short)((ushort_ << 8) | (ushort_ >> 8));
  113. public static uint UintLittleEndian(uint uint_)
  114. => (uint_ << 24) | ((uint_ << 8) & 0x00FF0000) |
  115. ((uint_ >> 8) & 0x0000FF00) | (uint_ >> 24);
  116. public static int UintLittleEndian(int uint_)
  117. => (uint_ << 24) | ((uint_ << 8) & 0x00FF0000) |
  118. ((uint_ >> 8) & 0x0000FF00) | (uint_ >> 24);
  119. public static int ClampOverload(int a, int min, int max)
  120. => a < min ? max - Math.Abs(a) : a > max ? a - max : a;
  121. public static float ClampOverload(float a, float min, float max)
  122. => a < min ? max - Math.Abs(a) : a > max ? a - max : a;
  123. }
  124. }