obfuz/Runtime/ConstUtility.cs

83 lines
2.3 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
2025-05-07 22:55:07 +08:00
using UnityEngine.Assertions;
namespace Obfuz
{
public static class ConstUtility
{
public static int GetInt(byte[] data, int offset)
{
return BitConverter.ToInt32(data, offset);
}
public static long GetLong(byte[] data, int offset)
{
return BitConverter.ToInt64(data, offset);
}
public static float GetFloat(byte[] data, int offset)
{
return BitConverter.ToSingle(data, offset);
}
public static double GetDouble(byte[] data, int offset)
{
return BitConverter.ToDouble(data, offset);
}
public static string GetString(byte[] data, int offset, int length)
{
return Encoding.UTF8.GetString(data, offset, length);
}
public static byte[] GetBytes(byte[] data, int offset, int length)
{
byte[] result = new byte[length];
Array.Copy(data, offset, result, 0, length);
return result;
}
2025-05-07 22:55:07 +08:00
public static int[] GetInts(byte[] data, int offset, int byteLength)
{
Assert.IsTrue(byteLength % 4 == 0);
int[] result = new int[byteLength >> 2];
Buffer.BlockCopy(data, offset, result, 0, byteLength);
return result;
}
2025-04-23 13:46:50 +08:00
public static void InitializeArray(Array array, byte[] data, int offset, int length)
{
Buffer.BlockCopy(data, offset, array, 0, length);
}
2025-05-16 17:44:28 +08:00
public static unsafe int CastFloatAsInt(float value)
{
2025-05-16 17:44:28 +08:00
int* intValue = (int*)&value;
return *intValue;
}
2025-05-16 17:44:28 +08:00
public static unsafe float CastIntAsFloat(int value)
{
2025-05-16 17:44:28 +08:00
float* floatValue = (float*)&value;
return *floatValue;
}
2025-05-16 17:44:28 +08:00
public static unsafe long CastDoubleAsLong(double value)
{
2025-05-16 17:44:28 +08:00
long* longValue = (long*)&value;
return *longValue;
}
2025-05-16 17:44:28 +08:00
public static unsafe double CastLongAsDouble(long value)
{
2025-05-16 17:44:28 +08:00
double* doubleValue = (double*)&value;
return *doubleValue;
}
}
}