add live select pannel
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class ArrayExtensions {
|
||||
|
||||
public static void ZeroMem(this int[] array) {
|
||||
if (array == null) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < array.Length; ++i) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ZeroMem(this uint[] array) {
|
||||
if (array == null) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < array.Length; ++i) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ZeroMem(this byte[] array) {
|
||||
if (array == null) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < array.Length; ++i) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ZeroMem(this float[] array) {
|
||||
if (array == null) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < array.Length; ++i) {
|
||||
array[i] = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
public static int Write(this byte[] array, uint value, int index) {
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
}
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
array[index + i] = bytes[i];
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static int Write<T>(this byte[] array, T value, int index) where T : struct {
|
||||
var size = Marshal.SizeOf(typeof(T));
|
||||
var bytes = new byte[size];
|
||||
var ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(value, ptr, true);
|
||||
Marshal.Copy(ptr, bytes, 0, size);
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
Array.Copy(bytes, 0, array, index, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
public static int Write(this byte[] array, byte[] value, int index) {
|
||||
var length = value.Length;
|
||||
Array.Copy(value, 0, array, index, length);
|
||||
return length;
|
||||
}
|
||||
|
||||
public static byte[] Append(this byte[] data, byte[] toAppend) {
|
||||
var buffer = new byte[data.Length + toAppend.Length];
|
||||
data.CopyTo(buffer, 0);
|
||||
toAppend.CopyTo(buffer, data.Length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static byte[] Append(this byte[] data, byte toAppend) {
|
||||
var buffer = new byte[data.Length + 1];
|
||||
data.CopyTo(buffer, 0);
|
||||
buffer[buffer.Length - 1] = toAppend;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static int IndexOf<T>(this T[] array, T item) {
|
||||
for (var i = 0; i < array.Length; ++i) {
|
||||
if (EqualityComparer<T>.Default.Equals(array[i], item)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1cfdaab710a2ee046b5af4a155189dde
|
||||
timeCreated: 1531897074
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class DereToreHelper {
|
||||
|
||||
public static ushort SwapEndian(ushort v) {
|
||||
var r = (ushort)(v & 0xff);
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= (ushort)(v & 0xff);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static short SwapEndian(short v) {
|
||||
unchecked {
|
||||
var s = (ushort)v;
|
||||
return (short)SwapEndian(s);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint SwapEndian(uint v) {
|
||||
var r = v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static int SwapEndian(int v) {
|
||||
unchecked {
|
||||
var s = (uint)v;
|
||||
return (int)SwapEndian(s);
|
||||
}
|
||||
}
|
||||
|
||||
public static ulong SwapEndian(ulong v) {
|
||||
var r = v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
r <<= 8;
|
||||
v >>= 8;
|
||||
r |= v & 0xff;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static long SwapEndian(long v) {
|
||||
unchecked {
|
||||
var s = (ulong)v;
|
||||
return (long)SwapEndian(s);
|
||||
}
|
||||
}
|
||||
|
||||
public static float SwapEndian(float v) {
|
||||
var bytes = BitConverter.GetBytes(v);
|
||||
var b = bytes[0];
|
||||
bytes[0] = bytes[1];
|
||||
bytes[1] = b;
|
||||
b = bytes[2];
|
||||
bytes[2] = bytes[3];
|
||||
bytes[3] = b;
|
||||
return BitConverter.ToSingle(bytes, 0);
|
||||
}
|
||||
|
||||
public static double SwapEndian(double v) {
|
||||
var bytes = BitConverter.GetBytes(v);
|
||||
Array.Reverse(bytes);
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
public static void Swap<T>(ref T t1, ref T t2) {
|
||||
var t = t1;
|
||||
t1 = t2;
|
||||
t2 = t;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 47686260da6f9ee49b544500cbbe9f39
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public abstract class DisposableBase : IDisposable {
|
||||
|
||||
protected DisposableBase() {
|
||||
_isDisposed = false;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if (_isDisposed) {
|
||||
return;
|
||||
}
|
||||
Dispose(true);
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
public bool IsDisposed { get { return _isDisposed; } }
|
||||
|
||||
protected abstract void Dispose(bool disposing);
|
||||
|
||||
private bool _isDisposed;
|
||||
|
||||
~DisposableBase() {
|
||||
if (_isDisposed) {
|
||||
return;
|
||||
}
|
||||
Dispose(false);
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dab724b4f02f71747b62e2fbf8502fd9
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace DereTore.Common {
|
||||
public enum Endian {
|
||||
|
||||
BigEndian,
|
||||
LittleEndian
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17d9c97d6a3f7aa4ca6dc74325e6245f
|
||||
timeCreated: 1531897074
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public class EndianBinaryReader : BinaryReader {
|
||||
|
||||
public static string NameOf<T>(Expression<Func<T>> e)
|
||||
{
|
||||
return ((MemberExpression)e.Body).Member.Name;
|
||||
}
|
||||
public EndianBinaryReader(Stream stream, Endian endian)
|
||||
: base(stream) {
|
||||
Endian = endian;
|
||||
}
|
||||
|
||||
public Endian Endian { get; set; }
|
||||
|
||||
public void Seek(long position, SeekOrigin origin) {
|
||||
switch (origin) {
|
||||
case SeekOrigin.Begin:
|
||||
Position = position;
|
||||
break;
|
||||
case SeekOrigin.Current:
|
||||
Position += position;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
Position = BaseStream.Length - position;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(NameOf(() => origin), origin, null);
|
||||
}
|
||||
}
|
||||
|
||||
public long Position {
|
||||
get { return BaseStream.Position; }
|
||||
set { BaseStream.Position = value; }
|
||||
}
|
||||
|
||||
public override short ReadInt16() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadInt16();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
public override int ReadInt32() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadInt32();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadInt32();
|
||||
}
|
||||
}
|
||||
|
||||
public override long ReadInt64() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadInt64();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadInt64();
|
||||
}
|
||||
}
|
||||
|
||||
public override ushort ReadUInt16() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadUInt16();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadUInt16();
|
||||
}
|
||||
}
|
||||
|
||||
public override uint ReadUInt32() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadUInt32();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadUInt32();
|
||||
}
|
||||
}
|
||||
|
||||
public override ulong ReadUInt64() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadUInt64();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadUInt64();
|
||||
}
|
||||
}
|
||||
|
||||
public override float ReadSingle() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadSingle();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadSingle();
|
||||
}
|
||||
}
|
||||
|
||||
public override double ReadDouble() {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
var value = base.ReadDouble();
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
return value;
|
||||
} else {
|
||||
return base.ReadDouble();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cecbe86bdbc8682499db69046ecbc94e
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public sealed class EndianBinaryWriter : BinaryWriter {
|
||||
|
||||
public EndianBinaryWriter(Stream stream, Endian endian)
|
||||
: base(stream) {
|
||||
Endian = endian;
|
||||
}
|
||||
public static string NameOf<T>(Expression<Func<T>> e)
|
||||
{
|
||||
return ((MemberExpression)e.Body).Member.Name;
|
||||
}
|
||||
|
||||
public Endian Endian { get; set; }
|
||||
|
||||
public void Seek(long position, SeekOrigin origin) {
|
||||
switch (origin) {
|
||||
case SeekOrigin.Begin:
|
||||
Position = position;
|
||||
break;
|
||||
case SeekOrigin.Current:
|
||||
Position += position;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
Position = BaseStream.Length - position;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(NameOf(() => origin), origin, null);
|
||||
}
|
||||
}
|
||||
|
||||
public long Position {
|
||||
get { return BaseStream.Position; }
|
||||
set { BaseStream.Position = value; }
|
||||
}
|
||||
|
||||
public override void Write(short value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(int value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(long value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(ushort value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(uint value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(ulong value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(float value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(double value) {
|
||||
if (Endian != SystemEndian.Type) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
base.Write(value);
|
||||
} else {
|
||||
base.Write(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf31f4449606e634fb58cfe1cac82eba
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class EnumerableExtensions {
|
||||
|
||||
public static int FirstIndexOf<T>(this IEnumerable<T> enumerable, Predicate<T> predicate, int startIndex) {
|
||||
var i = startIndex;
|
||||
var newEnumerable = startIndex == 0 ? enumerable : enumerable.Skip(startIndex);
|
||||
foreach (var obj in newEnumerable) {
|
||||
if (predicate(obj)) {
|
||||
return i;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int FirstIndexOf<T>(this IEnumerable<T> enumerable, Predicate<T> predicate) {
|
||||
return FirstIndexOf(enumerable, predicate, 0);
|
||||
}
|
||||
|
||||
public static string BuildString<T>(this IEnumerable<T> enumerable) {
|
||||
return BuildString(enumerable, DefaultEnumerableStringSeparator);
|
||||
}
|
||||
|
||||
public static string BuildString<T>(this IEnumerable<T> enumerable, string separator) {
|
||||
var stringBuilder = new StringBuilder();
|
||||
var processedItemCount = 0;
|
||||
using (var enumerator = enumerable.GetEnumerator()) {
|
||||
while (enumerator.MoveNext()) {
|
||||
if (processedItemCount > 0) {
|
||||
stringBuilder.Append(separator);
|
||||
}
|
||||
stringBuilder.Append(enumerator.Current);
|
||||
++processedItemCount;
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public static bool CountMoreThan<T>(this IEnumerable<T> enumerable, int n) {
|
||||
var counter = 0;
|
||||
using (var iter = enumerable.GetEnumerator()) {
|
||||
while (iter.MoveNext()) {
|
||||
++counter;
|
||||
if (counter > n) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static readonly string DefaultEnumerableStringSeparator = ", ";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 262170c4070c6ee4884736777bf63769
|
||||
timeCreated: 1531897074
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
|
||||
namespace DereTore.Common
|
||||
{
|
||||
public static class EventExtensions
|
||||
{
|
||||
|
||||
public static void Raise<T>(this EventHandler<T> handler, object sender, T e) where T : EventArgs
|
||||
{
|
||||
if (handler != null)
|
||||
{
|
||||
handler.Invoke(sender, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Raise(this EventHandler handler, object sender, EventArgs e)
|
||||
{
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
handler.Invoke(sender, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static IAsyncResult RaiseAsync<T>(this EventHandler<T> handler, object sender, T e) where T : EventArgs
|
||||
{
|
||||
if (handler != null)
|
||||
{
|
||||
return handler.BeginInvoke(sender, e, handler.EndInvoke, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IAsyncResult RaiseAsync(this EventHandler handler, object sender, EventArgs e)
|
||||
{
|
||||
if (handler != null)
|
||||
{
|
||||
return handler.BeginInvoke(sender, e, handler.EndInvoke, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3a3340580b92f64888ad16663ee191d
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class MathHelper {
|
||||
|
||||
public static double ClampUpper(double value, double minimum) {
|
||||
return value < minimum ? minimum : value;
|
||||
}
|
||||
|
||||
public static double ClampLower(double value, double maximum) {
|
||||
return value > maximum ? maximum : value;
|
||||
}
|
||||
|
||||
public static double Clamp(double value, double minimum, double maximum) {
|
||||
return value < minimum ? minimum : (value > maximum ? maximum : value);
|
||||
}
|
||||
|
||||
public static float Clamp(float value, float minimum, float maximum) {
|
||||
return value < minimum ? minimum : (value > maximum ? maximum : value);
|
||||
}
|
||||
|
||||
public static int NextRandomPositiveInt32() {
|
||||
return Random.Next(1, int.MaxValue);
|
||||
}
|
||||
|
||||
public static int NextRandomInt32() {
|
||||
return Random.Next();
|
||||
}
|
||||
|
||||
public static int NextRandomInt32(int maxValue) {
|
||||
return Random.Next(maxValue);
|
||||
}
|
||||
|
||||
public static int NextRandomInt32(int minValue, int maxValue) {
|
||||
return Random.Next(minValue, maxValue);
|
||||
}
|
||||
|
||||
public static long NextRandomInt64() {
|
||||
var v1 = (long)NextRandomInt32();
|
||||
var v2 = NextRandomInt32();
|
||||
return (v1 << 32) + v2;
|
||||
}
|
||||
|
||||
public static float NextRandomSingle() {
|
||||
return (float)Random.NextDouble();
|
||||
}
|
||||
|
||||
public static double NextRandomDouble() {
|
||||
return Random.NextDouble();
|
||||
}
|
||||
|
||||
public static uint GreatestCommonFactor(uint a, uint b) {
|
||||
while (true) {
|
||||
if (a < b) {
|
||||
var t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
var m = a % b;
|
||||
if (m == 0 || m == 1) {
|
||||
return b;
|
||||
} else {
|
||||
a = b;
|
||||
b = m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static uint GreatestCommonFactor(params uint[] numbers) {
|
||||
if (numbers.Length == 0) {
|
||||
throw new ArgumentException();
|
||||
}
|
||||
if (numbers.Length == 1) {
|
||||
return numbers[0];
|
||||
}
|
||||
var gcd = GreatestCommonFactor(numbers[0], numbers[1]);
|
||||
if (numbers.Length == 2) {
|
||||
return gcd;
|
||||
}
|
||||
var currentIndex = 2;
|
||||
while (currentIndex < numbers.Length) {
|
||||
if (gcd == 1) {
|
||||
break;
|
||||
}
|
||||
// Please note the param order.
|
||||
gcd = GreatestCommonFactor(numbers[currentIndex], gcd);
|
||||
++currentIndex;
|
||||
}
|
||||
return gcd;
|
||||
}
|
||||
|
||||
public static bool IsMultipleOf(this int a, int b) {
|
||||
return a / b * b == a;
|
||||
}
|
||||
|
||||
public static int RoundUpTo(int value, int align) {
|
||||
if (value % align == 0) {
|
||||
return value;
|
||||
}
|
||||
return value + (align - (value % align));
|
||||
}
|
||||
|
||||
public static int RoundUpToNext(int value, int align) {
|
||||
if (value % align == 0) {
|
||||
return value + align;
|
||||
}
|
||||
return value + (align - (value % align));
|
||||
}
|
||||
|
||||
public static readonly Random Random = new Random();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f1dd4494cbba784ea44e0a0932f6de5
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class SingletonManager<T> where T : class {
|
||||
|
||||
static SingletonManager() {
|
||||
SyncObject = new T[0];
|
||||
}
|
||||
|
||||
public static T Instance {
|
||||
get {
|
||||
if (_instance == null) {
|
||||
lock (SyncObject) {
|
||||
if (_instance == null) {
|
||||
var t = typeof(T);
|
||||
var constructor = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new Type[0], null);
|
||||
if (constructor == null) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
_instance = (T)constructor.Invoke(new object[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private static T _instance;
|
||||
private static readonly T[] SyncObject;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a35908730a879f438a156e0adfacf32
|
||||
timeCreated: 1531897075
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,594 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace DereTore.Common {
|
||||
// TODO: Merge StreamExtensions to EndianBinary[Reader/Writer].
|
||||
public static class StreamExtensions {
|
||||
|
||||
public static void CopyTo(this Stream source, Stream destination) {
|
||||
var buffer = new byte[1024];
|
||||
var read = source.Read(buffer, 0, buffer.Length);
|
||||
while (read > 0) {
|
||||
destination.Write(buffer, 0, read);
|
||||
if (read < buffer.Length) {
|
||||
break;
|
||||
}
|
||||
read = source.Read(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Write(this Stream stream, sbyte value) {
|
||||
unchecked {
|
||||
stream.WriteByte((byte)value);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static int Write(this Stream stream, int value) {
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
}
|
||||
stream.Write(BitConverter.GetBytes(value), 0, 4);
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static int Write(this Stream stream, short value) {
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
}
|
||||
stream.Write(BitConverter.GetBytes(value), 0, 2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
public static int Write(this Stream stream, float value) {
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
value = DereToreHelper.SwapEndian(value);
|
||||
}
|
||||
stream.Write(BitConverter.GetBytes(value), 0, 4);
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static int Write<T>(this Stream stream, T value) where T : struct {
|
||||
var size = Marshal.SizeOf(typeof(T));
|
||||
var bytes = new byte[size];
|
||||
var ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(value, ptr, true);
|
||||
Marshal.Copy(ptr, bytes, 0, size);
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
stream.Write(bytes, 0, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
public static void WriteRange<T>(this Stream stream, T[] value) where T : struct {
|
||||
var size = Marshal.SizeOf(typeof(T));
|
||||
var count = value.Length;
|
||||
var bytes = new byte[size * count];
|
||||
var ptr = Marshal.AllocHGlobal(size);
|
||||
var baseIndex = 0;
|
||||
for (var i = 0; i < count; ++i) {
|
||||
Marshal.StructureToPtr(value[i], ptr, true);
|
||||
stream.Write(bytes, 0, size);
|
||||
Marshal.Copy(ptr, bytes, baseIndex, size);
|
||||
baseIndex += size;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
}
|
||||
|
||||
public static int Read<T>(this Stream stream, out T value) where T : struct {
|
||||
var size = Marshal.SizeOf(typeof(T));
|
||||
var bytes = new byte[size];
|
||||
var bytesRead = stream.Read(bytes, 0, size);
|
||||
var ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.Copy(bytes, 0, ptr, size);
|
||||
value = (T)Marshal.PtrToStructure(ptr, typeof(T));
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public static T Read<T>(this Stream stream) where T : struct {
|
||||
T value;
|
||||
Read(stream, out value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static int Skip(this Stream stream, int length) {
|
||||
var buffer = new byte[length];
|
||||
return stream.Read(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public static sbyte PeekSByte(this Stream stream) {
|
||||
return PeekSByte(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static byte PeekByte(this Stream stream) {
|
||||
return PeekByte(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static short PeekInt16LE(this Stream stream) {
|
||||
return PeekInt16LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static short PeekInt16BE(this Stream stream) {
|
||||
return PeekInt16BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static ushort PeekUInt16LE(this Stream stream) {
|
||||
return PeekUInt16LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static ushort PeekUInt16BE(this Stream stream) {
|
||||
return PeekUInt16BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static int PeekInt32LE(this Stream stream) {
|
||||
return PeekInt32LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static int PeekInt32BE(this Stream stream) {
|
||||
return PeekInt32BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static uint PeekUInt32LE(this Stream stream) {
|
||||
return PeekUInt32LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static uint PeekUInt32BE(this Stream stream) {
|
||||
return PeekUInt32BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static long PeekInt64LE(this Stream stream) {
|
||||
return PeekInt64LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static long PeekInt64BE(this Stream stream) {
|
||||
return PeekInt64BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static ulong PeekUInt64LE(this Stream stream) {
|
||||
return PeekUInt64LE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static ulong PeekUInt64BE(this Stream stream) {
|
||||
return PeekUInt64BE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static float PeekSingleLE(this Stream stream) {
|
||||
return PeekSingleBE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static float PeekSingleBE(this Stream stream) {
|
||||
return PeekSingleBE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static double PeekDoubleLE(this Stream stream) {
|
||||
return PeekDoubleLE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static double PeekDoubleBE(this Stream stream) {
|
||||
return PeekDoubleBE(stream, stream.Position);
|
||||
}
|
||||
|
||||
public static sbyte PeekSByte(this Stream stream, long offset) {
|
||||
unchecked {
|
||||
return (sbyte)PeekByte(stream, offset);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte PeekByte(this Stream stream, long offset) {
|
||||
var position = stream.Position;
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
var value = stream.ReadByte();
|
||||
stream.Position = position;
|
||||
return (byte)value;
|
||||
}
|
||||
|
||||
public static short PeekInt16BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(short), false);
|
||||
return BitConverter.ToInt16(bytes, 0);
|
||||
}
|
||||
|
||||
public static short PeekInt16LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(short), true);
|
||||
return BitConverter.ToInt16(bytes, 0);
|
||||
}
|
||||
|
||||
public static ushort PeekUInt16BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(ushort), false);
|
||||
return BitConverter.ToUInt16(bytes, 0);
|
||||
}
|
||||
|
||||
public static ushort PeekUInt16LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(ushort), true);
|
||||
return BitConverter.ToUInt16(bytes, 0);
|
||||
}
|
||||
|
||||
public static int PeekInt32BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(int), false);
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static int PeekInt32LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(int), true);
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static uint PeekUInt32BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(uint), false);
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static uint PeekUInt32LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(uint), true);
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static long PeekInt64BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(long), false);
|
||||
return BitConverter.ToInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static long PeekInt64LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(long), true);
|
||||
return BitConverter.ToInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static ulong PeekUInt64BE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(ulong), false);
|
||||
return BitConverter.ToUInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static ulong PeekUInt64LE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(ulong), true);
|
||||
return BitConverter.ToUInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static float PeekSingleBE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(float), false);
|
||||
return BitConverter.ToSingle(bytes, 0);
|
||||
}
|
||||
|
||||
public static float PeekSingleLE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(float), true);
|
||||
return BitConverter.ToSingle(bytes, 0);
|
||||
}
|
||||
|
||||
public static double PeekDoubleBE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(double), false);
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
public static double PeekDoubleLE(this Stream stream, long offset) {
|
||||
var bytes = GetNumberBytes(stream, offset, sizeof(double), true);
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
public static string PeekZeroEndedString(this Stream stream, long offset, Encoding encoding) {
|
||||
var streamLength = stream.Length;
|
||||
var stringLength = 0;
|
||||
var originalPosition = stream.Position;
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
for (var i = offset; i <= streamLength; i++) {
|
||||
var dummy = stream.ReadByte();
|
||||
if (dummy > 0) {
|
||||
stringLength++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
var stringBytes = PeekBytes(stream, offset, stringLength);
|
||||
var ret = encoding.GetString(stringBytes);
|
||||
stream.Position = originalPosition;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string PeekZeroEndedStringAsAscii(this Stream stream, long offset) {
|
||||
return PeekZeroEndedString(stream, offset, Encoding.ASCII);
|
||||
}
|
||||
|
||||
public static string PeekZeroEndedStringAsUtf8(this Stream stream, long offset) {
|
||||
return PeekZeroEndedString(stream, offset, Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(byte[] array, int offset, int length) {
|
||||
var ret = new byte[length];
|
||||
Array.Copy(array, offset, ret, 0, length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static byte[] PeekBytes(this Stream stream, int offset, int length) {
|
||||
return PeekBytes(stream, (long)offset, length);
|
||||
}
|
||||
|
||||
public static byte[] PeekBytes(this Stream stream, long offset, int length) {
|
||||
var originalPosition = stream.Position;
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
var finalBuffer = new byte[length];
|
||||
var secondBuffer = new byte[length];
|
||||
var bytesLeft = length;
|
||||
var currentIndex = 0;
|
||||
int read;
|
||||
do {
|
||||
read = stream.Read(secondBuffer, 0, bytesLeft);
|
||||
Array.Copy(secondBuffer, 0, finalBuffer, currentIndex, read);
|
||||
bytesLeft -= read;
|
||||
currentIndex += read;
|
||||
} while (bytesLeft > 0 && read > 0);
|
||||
stream.Position = originalPosition;
|
||||
return finalBuffer;
|
||||
}
|
||||
|
||||
public static void SeekAndWriteBytes(this Stream stream, byte[] data, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteBytes(stream, data);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteByte(this Stream stream, byte value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteByte(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteSByte(this Stream stream, sbyte value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteSByte(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt16LE(this Stream stream, ushort value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt16LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt16BE(this Stream stream, ushort value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt16BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt16LE(this Stream stream, short value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt16LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt16BE(this Stream stream, short value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt16BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt32LE(this Stream stream, uint value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt32LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt32BE(this Stream stream, uint value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt32BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt32LE(this Stream stream, int value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt32LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt32BE(this Stream stream, int value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt32BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt64LE(this Stream stream, ulong value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt64LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteUInt64BE(this Stream stream, ulong value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteUInt64BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt64LE(this Stream stream, long value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt64LE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteInt64BE(this Stream stream, long value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteInt64BE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteSingleLE(this Stream stream, float value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteSingleLE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteSingleBE(this Stream stream, float value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteSingleBE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteDoubleLE(this Stream stream, double value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteDoubleLE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteDoubleBE(this Stream stream, double value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteDoubleBE(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteZeroEndedStringAsAscii(this Stream stream, string value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteZeroEndedStringAsAscii(stream, value);
|
||||
}
|
||||
|
||||
public static void SeekAndWriteZeroEndedStringAsUtf8(this Stream stream, string value, long offset) {
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
WriteZeroEndedStringAsUtf8(stream, value);
|
||||
}
|
||||
|
||||
public static void WriteBytes(this Stream stream, byte[] data) {
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static void WriteByte(this Stream stream, byte value) {
|
||||
stream.WriteByte(value);
|
||||
}
|
||||
|
||||
public static void WriteSByte(this Stream stream, sbyte value) {
|
||||
unchecked {
|
||||
stream.WriteByte((byte)value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteUInt16LE(this Stream stream, ushort value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteUInt16BE(this Stream stream, ushort value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt16LE(this Stream stream, short value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt16BE(this Stream stream, short value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteUInt32LE(this Stream stream, uint value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteUInt32BE(this Stream stream, uint value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt32LE(this Stream stream, int value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt32BE(this Stream stream, int value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteUInt64LE(this Stream stream, ulong value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteUInt64BE(this Stream stream, ulong value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt64LE(this Stream stream, long value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteInt64BE(this Stream stream, long value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteSingleLE(this Stream stream, float value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteSingleBE(this Stream stream, float value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteDoubleLE(this Stream stream, double value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (!BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteDoubleBE(this Stream stream, double value) {
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
if (BitConverter.IsLittleEndian) {
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
|
||||
public static void WriteZeroEndedStringAsAscii(this Stream stream, string value) {
|
||||
WriteZeroEndedString(stream, value, Encoding.ASCII);
|
||||
}
|
||||
|
||||
public static void WriteZeroEndedStringAsUtf8(this Stream stream, string value) {
|
||||
WriteZeroEndedString(stream, value, Encoding.UTF8);
|
||||
}
|
||||
|
||||
private static void WriteZeroEndedString(this Stream stream, string value, Encoding encoding) {
|
||||
if (value != null) {
|
||||
var bytes = encoding.GetBytes(value);
|
||||
WriteBytes(stream, bytes);
|
||||
}
|
||||
stream.WriteByte(0);
|
||||
}
|
||||
|
||||
private static byte[] GetNumberBytes(Stream stream, long offset, int byteCount, bool isLittleEndian) {
|
||||
var data = PeekBytes(stream, offset, byteCount);
|
||||
if (BitConverter.IsLittleEndian != isLittleEndian) {
|
||||
Array.Reverse(data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3dc19a81daca1c4090d9c67a6e9eceb
|
||||
timeCreated: 1531897076
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace DereTore.Common {
|
||||
public static class SystemEndian {
|
||||
|
||||
public static readonly Endian Type = BitConverter.IsLittleEndian ? Endian.LittleEndian : Endian.BigEndian;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc6cb614e9a47944cb88cfa56c29dab4
|
||||
timeCreated: 1531897076
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user