diff --git a/KKdBaseLib/DCC.cs b/KKdBaseLib/DCC.cs index f17f755..238875a 100644 --- a/KKdBaseLib/DCC.cs +++ b/KKdBaseLib/DCC.cs @@ -2,7 +2,7 @@ namespace KKdBaseLib { - public static class DCC //Databank_Checksum_Calculator + public static class DCC //Databank Checksum Calculator { private static readonly ushort[] ChecksumLookupTable = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, diff --git a/KKdBaseLib/Extensions.cs b/KKdBaseLib/Extensions.cs index 535f1a4..7602d8c 100644 --- a/KKdBaseLib/Extensions.cs +++ b/KKdBaseLib/Extensions.cs @@ -90,14 +90,38 @@ namespace KKdBaseLib public static byte[] buf = new byte[8]; public static byte* bufPtr = buf.GetPtr(); + + public static byte[] Endian(this byte[] LE, byte Len) + { for (byte i = 0; i < Len; i++) bufPtr[i] = LE[i]; + for (byte i = 0; i < Len; i++) LE[Len - i - 1] = bufPtr[i]; return LE; } + + public static byte[] Endian(this byte[] LE, byte Len, bool IsBE) + { if (IsBE) { for (byte i = 0; i < Len; i++) bufPtr[i] = LE[i]; + for (byte i = 0; i < Len; i++) LE[Len - i - 1] = bufPtr[i]; } return LE; } - public static long Endian(this long LE, byte Len, bool IsBE) - { if (IsBE) { for (byte i = 0; i < Len; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; - for (byte i = 0; i < Len; i++) { LE |= bufPtr[i]; if (i < Len - 1) LE <<= 8; } } return LE; } + public static short Endian(this short LE, bool IsBE) + { if (IsBE) { int TLE = 0; for (byte i = 0; i < 2; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 2; i++) { TLE |= bufPtr[i]; if (i < 1) TLE <<= 8; } LE = (short)TLE; } return LE; } - public static ulong Endian(this ulong LE, byte Len, bool IsBE) - { if (IsBE) { for (byte i = 0; i < Len; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; - for (byte i = 0; i < Len; i++) { LE |= bufPtr[i]; if (i < Len - 1) LE <<= 8; } } return LE; } + public static ushort Endian(this ushort LE, bool IsBE) + { if (IsBE) { for (byte i = 0; i < 2; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 2; i++) { LE |= bufPtr[i]; if (i < 1) LE <<= 8; } } return LE; } + + public static int Endian(this int LE, bool IsBE) + { if (IsBE) { for (byte i = 0; i < 4; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 4; i++) { LE |= bufPtr[i]; if (i < 3) LE <<= 8; } } return LE; } + + public static uint Endian(this uint LE, bool IsBE) + { if (IsBE) { for (byte i = 0; i < 4; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 4; i++) { LE |= bufPtr[i]; if (i < 3) LE <<= 8; } } return LE; } + + public static long Endian(this long LE, bool IsBE) + { if (IsBE) { for (byte i = 0; i < 8; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 8; i++) { LE |= bufPtr[i]; if (i < 7) LE <<= 8; } } return LE; } + + public static ulong Endian(this ulong LE, bool IsBE) + { if (IsBE) { for (byte i = 0; i < 8; i++) { bufPtr[i] = (byte)LE; LE >>= 8; } LE = 0; + for (byte i = 0; i < 8; i++) { LE |= bufPtr[i]; if (i < 7) LE <<= 8; } } return LE; } public static sbyte CITSB(this int c) { if (c > 0x0000007F) c = 0x0000007F; @@ -168,8 +192,8 @@ namespace KKdBaseLib public static float ToSingle(this int i) => *( float*)&i; public static float ToSingle(this uint i) => *( float*)&i; - public static double ToSingle(this long i) => *(double*)&i; - public static double ToSingle(this ulong i) => *(double*)&i; + public static double ToDouble(this long i) => *(double*)&i; + public static double ToDouble(this ulong i) => *(double*)&i; public static sbyte* GetPtr(this sbyte[] array) { fixed ( sbyte* tempPtr = array) return tempPtr; } public static byte* GetPtr(this byte[] array) { fixed ( byte* tempPtr = array) return tempPtr; } @@ -183,7 +207,7 @@ namespace KKdBaseLib public static double* GetPtr(this double[] array) { fixed (double* tempPtr = array) return tempPtr; } public static string ToString(this int d, bool IsBE) => - BitConverter.GetBytes((int)((long)d).Endian(4, IsBE)).ToASCII(); + BitConverter.GetBytes(d.Endian(IsBE)).ToASCII(); public static string ToString(this object d) { diff --git a/KKdBaseLib/F2/ENRS.cs b/KKdBaseLib/F2/ENRS.cs index 9fb21be..d3554b4 100644 --- a/KKdBaseLib/F2/ENRS.cs +++ b/KKdBaseLib/F2/ENRS.cs @@ -1,6 +1,6 @@ namespace KKdBaseLib.F2 { - public struct ENRS + public unsafe struct ENRS { public int Offset; public int Count; @@ -8,7 +8,7 @@ public int Repeat; public SubENRS[] Sub; - public static unsafe ENRS[] Read(byte[] data) + public static ENRS[] Read(byte[] data) { byte* ptr = data.GetPtr(); int i, i0; @@ -16,14 +16,16 @@ ENRS[] ENRSArr = new ENRS[ENRSCount]; ptr += 0x10; - ENRS ENR = new ENRS(); for (i = 0; i < ENRSCount; i++) { - ENR.Offset = ReadENRSValue(ref ptr) + ENR.Offset; + ref ENRS ENR = ref ENRSArr[i]; + ENR.Offset = ReadENRSValue(ref ptr); ENR.Count = ReadENRSValue(ref ptr); ENR.Size = ReadENRSValue(ref ptr); ENR.Repeat = ReadENRSValue(ref ptr); + if (i > 0) ENR.Offset += ENRSArr[i - 1].Offset; + if (ENR.Repeat > 0) { ENR.Sub = new SubENRS[ENR.Count]; @@ -35,42 +37,142 @@ if (ENR.Sub[i0].Type == Type.Invalid) return null; if (i0 > 0) ENR.Sub[i0].Skip += ENR.Sub[i0 - 1].Skip + ENR.Sub[i0 - 1].Reverse * - ((ENR.Sub[i0].Type == Type.WORD) ? 2 : (ENR.Sub[i0].Type == Type.DWORD) ? 4 : 8); + (2 << ((byte)ENR.Sub[i0].Type >> 16)); } } else ENR.Sub = null; - ENRSArr[i] = ENR; } return ENRSArr; } - private static unsafe int ReadENRSValue(ref byte* ptr, out Type Rev) + public static byte[] Write(ENRS[] ENRSArr) + { + int i, i0; + byte[] data; + byte* ptr; + + if (ENRSArr == null || ENRSArr.Length < 1) + return new byte[0x10]; + + int length = 0x10; + for (i = 0; i < ENRSArr.Length; i++) + { + length += 0x10; + ref ENRS ENR = ref ENRSArr[i]; + if (ENR.Repeat > 0 && ENR.Sub != null) + { + ENR.Count = ENR.Sub.Length; + length += 0x8 * ENR.Count; + } + else ENR.Repeat = 0; + } + data = new byte[length]; + ptr = data.GetPtr(); + ((int*)ptr)[1] = ENRSArr.Length; + ptr += 0x10; + + for (i = 0; i < ENRSArr.Length; i++) + { + ref ENRS ENR = ref ENRSArr[i]; + WriteENRSValue(ref ptr, i > 0 ? ENR.Offset - ENRSArr[i - 1].Offset : ENR.Offset); + WriteENRSValue(ref ptr, ENR.Count ); + WriteENRSValue(ref ptr, ENR.Size ); + WriteENRSValue(ref ptr, ENR.Repeat); + + if (ENR.Repeat > 0) + for (i0 = 0; i0 < ENR.Count; i0++) + { + if (ENR.Sub[i0].Type < Type.WORD || ENR.Sub[i0].Type > Type.QWORD) + return GetFinalArray(data, ptr); + + WriteENRSValue(ref ptr, i0 > 0 ? ENR.Sub[i0].Skip - ENR.Sub[i0 - 1].Skip - ENR.Sub[i0 - 1].Reverse * + (2 << (((byte)ENR.Sub[i0].Type & 0x30) >> 16)) : ENR.Sub[i0].Skip, ENR.Sub[i0].Type); + WriteENRSValue(ref ptr, ENR.Sub[i0].Reverse); + } + } + return GetFinalArray(data, ptr); + } + + private static byte[] GetFinalArray(byte[] data, byte* ptr) + { + byte* Ptr = data.GetPtr(); + long length = (long)ptr - (long)Ptr; + byte[] tempdata = new byte[length.Align(0x10)]; + for (int i = 0; i < length; i++) tempdata[i] = data[i]; + data = null; + return tempdata; + } + + private static int ReadENRSValue(ref byte* ptr, out Type Type) { int V = *ptr & 0xF; - Rev = (Type)(*ptr & 0x30); - Value Val = (Value)(*ptr & 0xC0); + Type = ( Type)(*ptr & 0x30); + Value Value = (Value)(*ptr & 0xC0); ptr++; - if (Val == Value.Int32 ) + if (Value == Value.Int32 ) { V = (V << 24) | (ptr[0] << 16) | (ptr[1] << 8) | ptr[2]; ptr += 3; } - else if (Val == Value.Int16 ) + else if (Value == Value.Int16 ) { V = (V << 8) | ptr[0]; ptr += 1; } - else if (Val == Value.Invalid) V = 0; + else if (Value == Value.Invalid) V = 0; return V; } - private static unsafe int ReadENRSValue(ref byte* ptr) + private static int ReadENRSValue(ref byte* ptr) { int V = *ptr & 0x3F; - Value Val = (Value)(*ptr & 0xC0); + Value Value = (Value)(*ptr & 0xC0); ptr++; - if (Val == Value.Int32 ) + if (Value == Value.Int32 ) { V = (V << 24) | (ptr[0] << 16) | (ptr[1] << 8) | ptr[2]; ptr += 3; } - else if (Val == Value.Int16 ) + else if (Value == Value.Int16 ) { V = (V << 8) | ptr[0]; ptr += 1; } - else if (Val == Value.Invalid) V = 0; + else if (Value == Value.Invalid) V = 0; return V; } + private static void WriteENRSValue(ref byte* ptr, int Val, Type Type) + { + Value Value = Value.Invalid; + if (Val < 0x00000040) Value = Value.Int8 ; + else if (Val < 0x00004000) Value = Value.Int16; + else if (Val < 0x40000000) Value = Value.Int32; + *ptr = (byte)Value; + *ptr |= (byte)(((byte)Type) & 0x30); + + if (Val < 0x00000010) + { *ptr |= (byte)( Val & 0x0F); } + else if (Val < 0x00001000) + { *ptr |= (byte)((Val >> 8) & 0x0F); ptr++; + *ptr = (byte)( Val & 0xFF); } + else if (Val < 0x10000000) + { *ptr |= (byte)((Val >> 24) & 0x0F); ptr++; + *ptr = (byte)((Val >> 16) & 0xFF); ptr++; + *ptr = (byte)((Val >> 8) & 0xFF); ptr++; + *ptr = (byte)( Val & 0xFF); } + ptr++; + } + + private static void WriteENRSValue(ref byte* ptr, int Val) + { + Value Value = Value.Invalid; + if (Val < 0x00000040) Value = Value.Int8 ; + else if (Val < 0x00004000) Value = Value.Int16; + else if (Val < 0x40000000) Value = Value.Int32; + *ptr = (byte)Value; + + if (Val < 0x00000040) + { *ptr |= (byte)( Val & 0x3F); } + else if (Val < 0x00004000) + { *ptr |= (byte)((Val >> 8) & 0x3F); ptr++; + *ptr = (byte)( Val & 0xFF); } + else if (Val < 0x40000000) + { *ptr |= (byte)((Val >> 24) & 0x3F); ptr++; + *ptr = (byte)((Val >> 16) & 0xFF); ptr++; + *ptr = (byte)((Val >> 8) & 0xFF); ptr++; + *ptr = (byte)( Val & 0xFF); } + ptr++; + } + public enum Type : byte { WORD = 0b00000000, diff --git a/KKdBaseLib/F2/POF.cs b/KKdBaseLib/F2/POF.cs index b2797fd..c8ace8a 100644 --- a/KKdBaseLib/F2/POF.cs +++ b/KKdBaseLib/F2/POF.cs @@ -1,8 +1,8 @@ namespace KKdBaseLib.F2 { - public struct POF + public unsafe struct POF { - public static unsafe KKdList Read(byte[] data, bool ShiftX) + public static KKdList Read(byte[] data, bool ShiftX) { Value Val = 0; KKdList Offsets = KKdList.New; @@ -27,7 +27,7 @@ return Offsets; } - public static unsafe byte[] Write(KKdList Offsets, bool ShiftX) + public static byte[] Write(KKdList Offsets, bool ShiftX) { Offsets.Sort(); int Length = 5; @@ -49,7 +49,7 @@ byte[] data = new byte[Length.Align(0x10)]; byte* ptr = data.GetPtr(); - Value Val = 0; + byte Val = 0; *(int*)ptr = Length; ptr += 4; for (int i = 0; i < Offsets.Count; i++) { @@ -57,14 +57,14 @@ if (i > 0) { Offset -= Offsets[i - 1]; if (Offset == 0) continue; } Offset >>= BitShift; - Val = Offset > Max2 ? Value.Int32 : Offset > Max1 ? Value.Int16 : Value.Int8; - if (Offset <= Max1) *ptr = (byte)((byte)Val | Offset ); - else if (Offset <= Max2) { *ptr = (byte)((byte)Val | (Offset >> 8)); ptr++; - *ptr = (byte) Offset ; } - else { *ptr = (byte)((byte)Val | (Offset >> 24)); ptr++; - *ptr = (byte) (Offset >> 16) ; ptr++; - *ptr = (byte) (Offset >> 8) ; ptr++; - *ptr = (byte) Offset ; } + Val = (byte)(Offset > Max2 ? Value.Int32 : Offset > Max1 ? Value.Int16 : Value.Int8); + if (Offset <= Max1) *ptr = (byte)(Val | Offset ); + else if (Offset <= Max2) { *ptr = (byte)(Val | (Offset >> 8)); ptr++; + *ptr = (byte) Offset ; } + else { *ptr = (byte)(Val | (Offset >> 24)); ptr++; + *ptr = (byte) (Offset >> 16) ; ptr++; + *ptr = (byte) (Offset >> 8) ; ptr++; + *ptr = (byte) Offset ; } ptr++; } return data; diff --git a/KKdBaseLib/Half.cs b/KKdBaseLib/Half.cs index 8a69c02..0f178da 100644 --- a/KKdBaseLib/Half.cs +++ b/KKdBaseLib/Half.cs @@ -6,78 +6,82 @@ namespace KKdBaseLib { private ushort _value; + public static Half NaN => new Half { _value = 0x7FFF }; + public static Half PositiveNaN => new Half { _value = 0x7FFF }; + public static Half NegativeNaN => new Half { _value = 0xFFFF }; + public static Half PositiveZero => new Half { _value = 0x0000 }; + public static Half NegativeZero => new Half { _value = 0x8000 }; + public static Half PositiveInfinity => new Half { _value = 0x7C00 }; + public static Half NegativeInfinity => new Half { _value = 0xFC00 }; + public static explicit operator Half(ushort bits) => new Half() { _value = bits }; public static explicit operator ushort(Half bits) => bits._value; - public static explicit operator float(Half h) => (float)(double)h; - - public static explicit operator double(Half h) + public static unsafe implicit operator float(Half h) { - if (h._value == 0x0000) return +0; - else if (h._value == 0x8000) return -0; - else if (h._value == 0x7C00) return double.PositiveInfinity; - else if (h._value == 0xFC00) return double.NegativeInfinity; - else if (h._value >> 10 == 0x001F) return double.NaN; - else if (h._value >> 10 == 0x003F) return -double.NaN; + int sign = (h._value >> 15) & 0x001; + int exponent = ((h._value >> 10) & 0x01F) + 127 - 15; + int mantissa = h._value & 0x3FF; - long exponent = ((h._value >> 10) & 0x1F); - long mantissa = (h._value & 0x3FF); - sbyte n = (sbyte)(((h._value >> 15 & 0x01) == 0) ? 1 : -1); - - double m = (((long)1 << 10) | mantissa) / Math.Pow(2, 10); - double x = Math.Pow(2, exponent - (0x1F >> 1)); - double d = n * m * x; - return d; + int si32 = (sign << 31) | (exponent << 23) | (mantissa << 13); + return *(float*)&si32; } - public static explicit operator Half( float val) => (Half)(double)val; - - public static explicit operator Half(double val) + public static unsafe implicit operator Half(float val) { - Half h = new Half(); - if (val == +0 ) h._value = 0x0000; - else if (val == -0 ) h._value = 0x8000; - else if (val == double.PositiveInfinity) h._value = 0x7C00; - else if (val == double.NegativeInfinity) h._value = 0xFC00; - else if (val == double.NaN ) h._value = 0x7FFF; - else if (val == -double.NaN ) h._value = 0xFFFF; - else - { - ushort Sign = (ushort)(val < 0 ? 0x8000 : 0); - val = Math.Abs(val); - double Pow1 = 1; - double Pow2 = 1 << 10; - double x = 0; + int si32 = *(int*)&val; + ushort sign = (ushort)( (si32 >> 16) & 0x8000); + short exponent = ( short)(((si32 >> 23) & 0x00FF) - 127 + 15); + ushort mantissa = (ushort)( (si32 >> 13) & 0x03FF); - int MaxPow = (1 << 4); + if (exponent < 0) { exponent = 0; mantissa = 0; } + else if (exponent > 30) exponent = 31; - int i = 0; - while (i < MaxPow && i > -MaxPow + 1) - { - Pow1 = Math.Pow(2, i); - x = val / Pow1; - if (x >= 1 && x < 2) - { - ushort exponent_max = (ushort)Math.Ceiling(x * Pow2); - ushort exponent_min = (ushort)Math.Floor (x * Pow2); - ushort exponent = Math.Abs(x - exponent_max / Pow2) > - Math.Abs(x - exponent_min / Pow2) ? exponent_max : exponent_min; - ushort mantissa = (ushort)(i + MaxPow - 1); - h._value = (ushort)(Sign | ((mantissa & 0x001F) << 10) | (exponent & 0x03FF)); - return h; - } - if (val < 1) i--; - else i++; - } - h._value = (ushort)(Sign | 0x7C00); - } - return h; + return new Half { _value = (ushort)(sign | (exponent << 10) | mantissa) }; } + public static unsafe implicit operator double(Half h) + { + int sign = (h._value >> 15) & 0x001; + int exponent = ((h._value >> 10) & 0x01F) + 1023 - 15; + int mantissa = h._value & 0x3FF; + + long si64 = ((long)sign << 63) | ((long)exponent << 52) | ((long)mantissa << 42); + return *(double*)&si64; + } + + public static unsafe implicit operator Half(double val) + { + long si64 = *(long*)&val; + ushort sign = (ushort) ((si64 >> 48) & 0x8000); + short exponent = ( short)(((si64 >> 52) & 0x07FF) - 1023 + 15); + ushort mantissa = (ushort) ((si64 >> 42) & 0x03FF); + + if (exponent < 0) { exponent = 0; mantissa = 0; } + else if (exponent > 30) exponent = 31; + + return new Half { _value = (ushort)(sign | (exponent << 10) | mantissa) }; + } + + public static Half operator + (Half a, Half b) => (float)a + (float)b; + public static Half operator - (Half a, Half b) => (float)a - (float)b; + public static Half operator * (Half a, Half b) => (float)a * (float)b; + public static Half operator / (Half a, Half b) => (float)a / (float)b; + public static bool operator > (Half a, Half b) => (float)a > (float)b; + public static bool operator < (Half a, Half b) => (float)a < (float)b; + public static bool operator >=(Half a, Half b) => (float)a >= (float)b; + public static bool operator <=(Half a, Half b) => (float)a <= (float)b; + public static bool operator ==(Half a, Half b) => (float)a == (float)b; + public static bool operator !=(Half a, Half b) => (float)a != (float)b; + + public int CompareTo(object obj) => CompareTo((Half)obj); + public int CompareTo(Half h) => this == h ? 0 : (this > h ? 1 : -1); + public bool Equals(Half other) => this == other; + public override bool Equals(object obj) => base.Equals(obj); public override string ToString() => Extensions.ToString((double)this); public string ToString(string format, IFormatProvider formatProvider) => - ((double)this).ToString(format, formatProvider); + ((float)this).ToString(format, formatProvider); public override int GetHashCode() => base.GetHashCode(); } } diff --git a/KKdBaseLib/IKF.cs b/KKdBaseLib/IKF.cs index d936081..3f6b383 100644 --- a/KKdBaseLib/IKF.cs +++ b/KKdBaseLib/IKF.cs @@ -4,6 +4,11 @@ { TKey F { get; set; } + KFT0 ToT0(); + KFT1 ToT1(); + KFT2 ToT2(); + KFT3 ToT3(); + IKF Check(); string ToString(); string ToString(bool Brackets); @@ -17,15 +22,22 @@ { this.F = F; } public KFT0 ToT0() => this; - public KFT1 ToT1() => new KFT1(F); - public KFT2 ToT2() => new KFT2(F); - public KFT3 ToT3() => new KFT3(F); + public KFT1 ToT1() => this; + public KFT2 ToT2() => this; + public KFT3 ToT3() => this; public IKF Check() => this; public override string ToString() => ToString(true); public string ToString(bool Brackets = true) => Extensions.ToString(F); + + public static implicit operator KFT1(KFT0 KF) => + new KFT1(KF.F); + public static implicit operator KFT2(KFT0 KF) => + new KFT2(KF.F); + public static implicit operator KFT3(KFT0 KF) => + new KFT3(KF.F); } public struct KFT1 : IKF @@ -36,18 +48,25 @@ public KFT1(TKey F = default, TVal V = default) { this.F = F; this.V = V; } - public KFT0 ToT0() => new KFT0(F); + public KFT0 ToT0() => this; public KFT1 ToT1() => this; - public KFT2 ToT2() => new KFT2(F, V); - public KFT3 ToT3() => new KFT3(F, V); + public KFT2 ToT2() => this; + public KFT3 ToT3() => this; public IKF Check() => - V.Equals(default(TVal)) ? (IKF)ToT0() : this; + V.Equals(default(TVal)) ? (KFT0)this : (IKF)this; public override string ToString() => ToString(true); public string ToString(bool Brackets = true) => (Brackets ? "(" : "") + Extensions.ToString(F) + "," + Extensions.ToString(V) + (Brackets ? ")" : ""); + + public static implicit operator KFT0(KFT1 KF) => + new KFT0(KF.F); + public static implicit operator KFT2(KFT1 KF) => + new KFT2(KF.F, KF.V); + public static implicit operator KFT3(KFT1 KF) => + new KFT3(KF.F, KF.V); } public struct KFT2 : IKF @@ -59,10 +78,10 @@ public KFT2(TKey F = default, TVal V = default, TVal T = default) { this.F = F; this.V = V; this.T = T; } - public KFT0 ToT0() => new KFT0(F); - public KFT1 ToT1() => new KFT1(F, V); + public KFT0 ToT0() => this; + public KFT1 ToT1() => this; public KFT2 ToT2() => this; - public KFT3 ToT3() => new KFT3(F, V, T, T); + public KFT3 ToT3() => this; public KFT3 ToT3(IKF Previous) => Previous is KFT2 PreviousT2 ? @@ -71,12 +90,19 @@ public IKF Check() => T.Equals(default(TVal)) ? (V.Equals(default(TVal)) ? - (IKF)ToT0() : ToT1()) : this; + (KFT0)this : (IKF)this) : this; public override string ToString() => ToString(true); public string ToString(bool Brackets) => (Brackets ? "(" : "") + Extensions.ToString(F) + "," + Extensions. ToString(V) + "," + Extensions.ToString(T) + (Brackets ? ")" : ""); + + public static implicit operator KFT0(KFT2 KF) => + new KFT0(KF.F); + public static implicit operator KFT1(KFT2 KF) => + new KFT1(KF.F, KF.V); + public static implicit operator KFT3(KFT2 KF) => + new KFT3(KF.F, KF.V, KF.T, KF.T); } public struct KFT3 : IKF @@ -89,21 +115,28 @@ public KFT3(TKey F = default, TVal V = default, TVal T1 = default, TVal T2 = default) { this.F = F; this.V = V; this.T1 = T1; this.T2 = T2; } - public KFT0 ToT0() => new KFT0(F); - public KFT1 ToT1() => new KFT1(F, V); - public KFT2 ToT2() => new KFT2(F, V, T1); + public KFT0 ToT0() => this; + public KFT1 ToT1() => this; + public KFT2 ToT2() => this; public KFT3 ToT3() => this; public IKF Check() => T1.Equals(default(TVal)) && T2.Equals(default(TVal)) ? - (V.Equals(default(TVal)) ? ToT0() : (IKF)ToT1()) : - T1.Equals(T2) ? (IKF)ToT2() : this; + (V.Equals(default(TVal)) ? (KFT0)this : (IKF)this) : + T1.Equals(T2) ? (KFT2)this : (IKF)this; public override string ToString() => ToString(true); public string ToString(bool Brackets) => (Brackets ? "(" : "") + Extensions.ToString(F) + "," + Extensions.ToString(V) + "," + Extensions.ToString(T1) + "," + Extensions.ToString(T2) + (Brackets ? ")" : ""); + public static implicit operator KFT0(KFT3 KF) => + new KFT0(KF.F); + public static implicit operator KFT1(KFT3 KF) => + new KFT1(KF.F, KF.V); + public static implicit operator KFT2(KFT3 KF) => + new KFT2(KF.F, KF.V, KF.T1); + public IKF ToT2(IKF Previous, out IKF Current) { Current = Previous is KFT2 PreviousT2 diff --git a/KKdBaseLib/KKdList.cs b/KKdBaseLib/KKdList.cs index 1c189c0..adaef99 100644 --- a/KKdBaseLib/KKdList.cs +++ b/KKdBaseLib/KKdList.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace KKdBaseLib { - public struct KKdList : IEnumerator, IEnumerable + public struct KKdList : System.IDisposable, IEnumerator, IEnumerable { public static KKdList Null => new KKdList(); public static KKdList New => new KKdList() { Capacity = 0 }; @@ -29,7 +29,11 @@ namespace KKdBaseLib object IEnumerator.Current => Current; - public T this[int index] + public T this[ int index] + { get => array != null ? array[index] : default; + set { if (array != null) array[index] = value; } } + + public T this[uint index] { get => array != null ? array[index] : default; set { if (array != null) array[index] = value; } } @@ -95,12 +99,12 @@ namespace KKdBaseLib } public void Sort() - { List List = (List)this; List.Sort(); array = List.ToArray(); Count = List.Count; } + { List List = this; List.Sort(); array = List.ToArray(); Count = List.Count; } - public static explicit operator KKdList( List List) => + public static implicit operator KKdList( List List) => new KKdList { array = List.ToArray(), Count = List.Count }; - public static explicit operator List(KKdList List) + public static implicit operator List(KKdList List) { List list = new List(); for (int i = 0; i < List.Count; i++) list.Add(List[i]); diff --git a/KKdBaseLib/MsgPack.cs b/KKdBaseLib/MsgPack.cs index c9e5b37..e80c774 100644 --- a/KKdBaseLib/MsgPack.cs +++ b/KKdBaseLib/MsgPack.cs @@ -43,22 +43,20 @@ namespace KKdBaseLib (Array != null ? ((Name != null ? " " : "") + "Elements Count: " + Array.Length) : Object.ToString())); - public MsgPack Add(string Val, byte[] val) => val != null ? Add(new MsgPack(Val, val)) : this; - public MsgPack Add(string Val, string val) => val != null ? Add(new MsgPack(Val, val)) : this; - - public static explicit operator MsgPack(byte[] val) => new MsgPack(null, val); - public static explicit operator MsgPack(string val) => new MsgPack(null, val); - public static explicit operator MsgPack( sbyte val) => new MsgPack(null, val); - public static explicit operator MsgPack( byte val) => new MsgPack(null, val); - public static explicit operator MsgPack( short val) => new MsgPack(null, val); - public static explicit operator MsgPack(ushort val) => new MsgPack(null, val); - public static explicit operator MsgPack( int val) => new MsgPack(null, val); - public static explicit operator MsgPack( uint val) => new MsgPack(null, val); - public static explicit operator MsgPack( long val) => new MsgPack(null, val); - public static explicit operator MsgPack( ulong val) => new MsgPack(null, val); - public static explicit operator MsgPack( float val) => new MsgPack(null, val); - public static explicit operator MsgPack(double val) => new MsgPack(null, val); - + public static implicit operator MsgPack(byte[] val) => new MsgPack(null, val); + public static implicit operator MsgPack(string val) => new MsgPack(null, val); + public static implicit operator MsgPack( sbyte val) => new MsgPack(null, val); + public static implicit operator MsgPack( byte val) => new MsgPack(null, val); + public static implicit operator MsgPack( short val) => new MsgPack(null, val); + public static implicit operator MsgPack(ushort val) => new MsgPack(null, val); + public static implicit operator MsgPack( int val) => new MsgPack(null, val); + public static implicit operator MsgPack( uint val) => new MsgPack(null, val); + public static implicit operator MsgPack( long val) => new MsgPack(null, val); + public static implicit operator MsgPack( ulong val) => new MsgPack(null, val); + public static implicit operator MsgPack( float val) => new MsgPack(null, val); + public static implicit operator MsgPack(double val) => new MsgPack(null, val); + + public MsgPack Add( bool? val) => val.HasValue ? Add(new MsgPack(null, val.Value)) : this; public MsgPack Add( sbyte? val) => val.HasValue ? Add(new MsgPack(null, val.Value)) : this; public MsgPack Add( byte? val) => val.HasValue ? Add(new MsgPack(null, val.Value)) : this; public MsgPack Add( short? val) => val.HasValue ? Add(new MsgPack(null, val.Value)) : this; @@ -84,6 +82,7 @@ namespace KKdBaseLib public MsgPack Add( float val) => Add(new MsgPack(null, val)); public MsgPack Add(double val) => Add(new MsgPack(null, val)); + public MsgPack Add(string Val, bool? val) => val.HasValue ? Add(Val, val.Value) : this; public MsgPack Add(string Val, sbyte? val) => val.HasValue ? Add(Val, val.Value) : this; public MsgPack Add(string Val, byte? val) => val.HasValue ? Add(Val, val.Value) : this; public MsgPack Add(string Val, short? val) => val.HasValue ? Add(Val, val.Value) : this; @@ -94,7 +93,9 @@ namespace KKdBaseLib public MsgPack Add(string Val, ulong? val) => val.HasValue ? Add(Val, val.Value) : this; public MsgPack Add(string Val, float? val) => val.HasValue ? Add(Val, val.Value) : this; public MsgPack Add(string Val, double? val) => val.HasValue ? Add(Val, val.Value) : this; - + + public MsgPack Add(string Val, byte[] val) => Add(new MsgPack(Val, val)); + public MsgPack Add(string Val, string val) => Add(new MsgPack(Val, val)); public MsgPack Add(string Val, bool val) => Add(new MsgPack(Val, val)); public MsgPack Add(string Val, sbyte val) => Add(new MsgPack(Val, val)); public MsgPack Add(string Val, byte val) => Add(new MsgPack(Val, val)); diff --git a/KKdBaseLib/Properties/AssemblyInfo.cs b/KKdBaseLib/Properties/AssemblyInfo.cs index e10f814..c162d7f 100644 --- a/KKdBaseLib/Properties/AssemblyInfo.cs +++ b/KKdBaseLib/Properties/AssemblyInfo.cs @@ -11,5 +11,5 @@ using System.Runtime.InteropServices; [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("437F63F1-8C23-429E-AB14-38B85C9EDB16")] -[assembly: AssemblyVersion("0.4.7.2")] -[assembly: AssemblyFileVersion("0.4.7.2")] +[assembly: AssemblyVersion("0.4.7.3")] +[assembly: AssemblyFileVersion("0.4.7.3")] diff --git a/KKdMainLib/A3DA.cs b/KKdMainLib/A3DA.cs index 37bc534..5a7cdfd 100644 --- a/KKdMainLib/A3DA.cs +++ b/KKdMainLib/A3DA.cs @@ -200,8 +200,8 @@ namespace KKdMainLib.A3DA CameraApertureH, nameView + "camera_aperture_h"); Dict.FindValue(out Data.CameraRoot[i0].VP. CameraApertureW, nameView + "camera_aperture_w"); - Dict.FindValue(out Data.CameraRoot[i0].VP. - FOVHorizontal , nameView + "fov_is_horizontal"); + Dict.FindValue(out i1, nameView + "fov_is_horizontal"); + Data.CameraRoot[i0].VP.FOVHorizontal = i1 != 0; Data.CameraRoot[i0]. MT = Dict.ReadMT(name); Data.CameraRoot[i0].Interest = Dict.ReadMT(name + "interest" + d); @@ -486,60 +486,62 @@ namespace KKdMainLib.A3DA IO.Write("_.compress_f16=", Data._.CompressF16); IO.Write("_.converter.version=", Data._.ConverterVersion); - IO.Write("_.file_name=", Data._.FileName); - IO.Write("_.property.version=", Data._.PropertyVersion); + IO.Write("_.file_name=" , Data._.FileName ); + IO.Write("_.property.version=" , Data._. PropertyVersion); if (Data.Ambient != null && Data.Format == Format.MGF) { SO0 = Data.Ambient.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.Ambient.Length; i0++) { SOi0 = SO0[i0]; name = "ambient" + d + SOi0 + d; + ref Ambient Ambient = ref Data.Ambient[SOi0]; - IO.Write(Data.Ambient[SOi0]. LightDiffuse, name, "light.Diffuse", A3DC); - IO.Write(name + "name=", Data.Ambient[SOi0].Name); - IO.Write(Data.Ambient[SOi0].RimLightDiffuse, name, "rimlight.Diffuse", A3DC); + IO.Write(Ambient. LightDiffuse, name + "light.Diffuse", A3DC); + IO.Write(name + "name=", Ambient.Name); + IO.Write(Ambient.RimLightDiffuse, name + "rimlight.Diffuse", A3DC); } - IO.Write("fog.length=", Data.Fog.Length); + IO.Write("ambient.length=", Data.Fog.Length); } if (Data.CameraAuxiliary != null) { name = "camera_auxiliary" + d; - IO.Write(Data.CameraAuxiliary.AutoExposure, name, "auto_exposure", A3DC); - IO.Write(Data.CameraAuxiliary. Exposure, name, "exposure", A3DC); - IO.Write(Data.CameraAuxiliary.Gamma , name, "gamma" , A3DC); - IO.Write(Data.CameraAuxiliary.GammaRate , name, "gamma_rate" , A3DC); - IO.Write(Data.CameraAuxiliary.Saturate , name, "saturate" , A3DC); + ref CameraAuxiliary CA = ref Data.CameraAuxiliary; + + IO.Write(CA.AutoExposure, name + "auto_exposure", true, A3DC); + IO.Write(CA. Exposure, name + "exposure", true, A3DC); + IO.Write(CA.Gamma , name + "gamma" , true, A3DC); + IO.Write(CA.GammaRate , name + "gamma_rate" , true, A3DC); + IO.Write(CA.Saturate , name + "saturate" , true, A3DC); } if (Data.CameraRoot != null) { SO0 = Data.CameraRoot.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.CameraRoot.Length; i0++) { SOi0 = SO0[i0]; name = "camera_root" + d + SOi0 + d; nameView = name + "view_point" + d; + ref CameraRoot CR = ref Data.CameraRoot[SOi0]; - IO.Write(Data.CameraRoot[SOi0].Interest, name + "interest" + d, A3DC, IsX); - IO.Write(Data.CameraRoot[SOi0].MT, name, A3DC, IsX, 0b11110); - IO.Write(nameView + "aspect=", Data.CameraRoot[i0].VP.Aspect); - if (Data.CameraRoot[i0].VP.CameraApertureH != null) - IO.Write(nameView + "camera_aperture_h=", - Data.CameraRoot[i0].VP.CameraApertureH); - if (Data.CameraRoot[i0].VP.CameraApertureW != null) - IO.Write(nameView + "camera_aperture_w=", Data.CameraRoot[i0].VP.CameraApertureW); - IO.Write(Data.CameraRoot[SOi0].VP.FocalLength, nameView + "focal_length" + d, A3DC); - IO.Write(Data.CameraRoot[SOi0].VP.FOV, nameView + "fov" + d, A3DC); - IO.Write(nameView + "fov_is_horizontal=", Data.CameraRoot[i0].VP.FOVHorizontal); - IO.Write(Data.CameraRoot[SOi0].VP.MT , nameView, A3DC, IsX, 0b10000); - IO.Write(Data.CameraRoot[SOi0].VP.Roll, nameView + "roll" + d, A3DC); - IO.Write(Data.CameraRoot[SOi0].VP.MT , nameView, A3DC, IsX, 0b01111); - IO.Write(Data.CameraRoot[SOi0] .MT , name , A3DC, IsX, 0b00001); + IO.Write(CR.Interest, name + "interest" + d, A3DC, IsX); + IO.Write(CR.MT, name, A3DC, IsX, 0b11110); + IO.Write(nameView + "aspect=", CR.VP.Aspect); + if (CR.VP.CameraApertureH != null) + IO.Write(nameView + "camera_aperture_h=", CR.VP.CameraApertureH); + if (CR.VP.CameraApertureW != null) + IO.Write(nameView + "camera_aperture_w=", CR.VP.CameraApertureW); + IO.Write(CR.VP.FocalLength, nameView + "focal_length" + d, A3DC); + IO.Write(CR.VP.FOV, nameView + "fov" + d, A3DC); + if (CR.VP.FOVHorizontal != null) + IO.Write(nameView + "fov_is_horizontal=", CR.VP.FOVHorizontal.Value ? 1 : 0); + IO.Write(CR.VP.MT , nameView, A3DC, IsX, 0b10000); + IO.Write(CR.VP.Roll, nameView + "roll" + d, A3DC); + IO.Write(CR.VP.MT , nameView, A3DC, IsX, 0b01111); + IO.Write(CR .MT , name , A3DC, IsX, 0b00001); } IO.Write("camera_root.length=", Data.CameraRoot.Length); } @@ -547,25 +549,22 @@ namespace KKdMainLib.A3DA if (Data.Chara != null) { SO0 = Data.Chara.Length.SortWriter(); - name = "chara" + d; - for (i0 = 0; i0 < Data.Chara.Length; i0++) - IO.Write(Data.Chara[SO0[i0]], name + SO0[i0] + d, A3DC, IsX); - IO.Write(name + "length=", Data.Chara.Length); + IO.Write(Data.Chara[SO0[i0]], "chara" + d + SO0[i0] + d, A3DC, IsX); + IO.Write("chara.length=", Data.Chara.Length); } if (Data.Curve != null) { SO0 = Data.Curve.Length.SortWriter(); - SOi0 = 0; - for (i0 = 0; i0 < Data.Curve.Length; i0++) { SOi0 = SO0[i0]; name = "curve" + d + SOi0 + d; + ref Curve Curve = ref Data.Curve[SOi0]; - IO.Write(Data.Curve[SOi0].CV, name + "cv" + d, A3DC); - IO.Write(name + "name=", Data.Curve[SOi0].Name); + IO.Write(Curve.CV, name + "cv" + d, A3DC); + IO.Write(name + "name=", Curve.Name); } IO.Write("curve.length=", Data.Curve.Length); } @@ -579,38 +578,38 @@ namespace KKdMainLib.A3DA if (Data.Event != null) { SO0 = Data.Event.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.Event.Length; i0++) { SOi0 = SO0[i0]; name = "event" + d + SOi0 + d; + ref Event Event = ref Data.Event[SOi0]; - IO.Write(name + "begin=" , Data.Event[SOi0].Begin ); - IO.Write(name + "clip_begin=" , Data.Event[SOi0].ClipBegin ); - IO.Write(name + "clip_en=" , Data.Event[SOi0].ClipEnd ); - IO.Write(name + "end=" , Data.Event[SOi0].End ); - IO.Write(name + "name=" , Data.Event[SOi0].Name ); - IO.Write(name + "param1=" , Data.Event[SOi0].Param1 ); - IO.Write(name + "ref=" , Data.Event[SOi0].Ref ); - IO.Write(name + "time_ref_scale=", Data.Event[SOi0].TimeRefScale); - IO.Write(name + "type=" , Data.Event[SOi0].Type ); + IO.Write(name + "begin=" , Event.Begin ); + IO.Write(name + "clip_begin=" , Event.ClipBegin ); + IO.Write(name + "clip_en=" , Event.ClipEnd ); + IO.Write(name + "end=" , Event.End ); + IO.Write(name + "name=" , Event.Name ); + IO.Write(name + "param1=" , Event.Param1 ); + IO.Write(name + "ref=" , Event.Ref ); + IO.Write(name + "time_ref_scale=", Event.TimeRefScale); + IO.Write(name + "type=" , Event.Type ); } } if (Data.Fog != null) { SO0 = Data.Fog.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.Fog.Length; i0++) { SOi0 = SO0[i0]; name = "fog" + d + SOi0 + d; + ref Fog Fog = ref Data.Fog[SOi0]; - IO.Write(Data.Fog[SOi0].Diffuse, name, "Diffuse", A3DC); - IO.Write(Data.Fog[SOi0].Density, name, "density", A3DC); - IO.Write(Data.Fog[SOi0].End , name, "end" , A3DC); - IO.Write(name + "id=", Data.Fog[SOi0].Id); - IO.Write(Data.Fog[SOi0].Start , name, "start" , A3DC); + IO.Write(Fog.Diffuse, name + "Diffuse", A3DC); + IO.Write(Fog.Density, name + "density", true, A3DC); + IO.Write(Fog.End , name + "end" , true, A3DC); + IO.Write(name + "id=", Fog.Id); + IO.Write(Fog.Start , name + "start" , true, A3DC); } IO.Write("fog.length=", Data.Fog.Length); } @@ -618,21 +617,21 @@ namespace KKdMainLib.A3DA if (Data.Light != null) { SO0 = Data.Light.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.Light.Length; i0++) { SOi0 = SO0[i0]; name = "light" + d + SOi0 + d; + ref Light Light = ref Data.Light[SOi0]; - IO.Write(Data.Light[SOi0].Ambient , name, "Ambient" , A3DC); - IO.Write(Data.Light[SOi0].Diffuse , name, "Diffuse" , A3DC); - IO.Write(Data.Light[SOi0].Incandescence, name, "Incandescence", A3DC); - IO.Write(Data.Light[SOi0].Specular , name, "Specular" , A3DC); - IO.Write(name + "id=" , Data.Light[SOi0].Id ); - IO.Write(name + "name=", Data.Light[SOi0].Name); - IO.Write(Data.Light[SOi0].Position , name + "position" + d, A3DC, IsX); - IO.Write(Data.Light[SOi0].SpotDirection, name + "spot_direction" + d, A3DC, IsX); - IO.Write(name + "type=", Data.Light[SOi0].Type); + IO.Write(Light.Ambient , name + "Ambient" , A3DC); + IO.Write(Light.Diffuse , name + "Diffuse" , A3DC); + IO.Write(Light.Incandescence, name + "Incandescence", A3DC); + IO.Write(Light.Specular , name + "Specular" , A3DC); + IO.Write(name + "id=" , Light.Id ); + IO.Write(name + "name=", Light.Name); + IO.Write(Light.Position , name + "position" + d, A3DC, IsX); + IO.Write(Light.SpotDirection, name + "spot_direction" + d, A3DC, IsX); + IO.Write(name + "type=", Light.Type); } IO.Write("light.length=", Data.Light.Length); } @@ -640,65 +639,60 @@ namespace KKdMainLib.A3DA if (Data.MObjectHRC != null) { SO0 = Data.MObjectHRC.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.MObjectHRC.Length; i0++) { SOi0 = SO0[i0]; name = "m_objhrc" + d + SOi0 + d; + ref MObjectHRC MObjectHRC = ref Data.MObjectHRC[SOi0]; - if (IsX && Data.MObjectHRC[SOi0].JointOrient.NotNull) + if (IsX && MObjectHRC.JointOrient.NotNull) { - IO.Write(name + "joint_orient.x=", Data.MObjectHRC[SOi0].JointOrient.X); - IO.Write(name + "joint_orient.y=", Data.MObjectHRC[SOi0].JointOrient.Y); - IO.Write(name + "joint_orient.z=", Data.MObjectHRC[SOi0].JointOrient.Z); + IO.Write(name + "joint_orient.x=", MObjectHRC.JointOrient.X); + IO.Write(name + "joint_orient.y=", MObjectHRC.JointOrient.Y); + IO.Write(name + "joint_orient.z=", MObjectHRC.JointOrient.Z); } - if (Data.MObjectHRC[SOi0].Instances != null) + if (MObjectHRC.Instances != null) { - nameView = name + "instance" + d; - SO1 = Data.MObjectHRC[SOi0].Instances.Length.SortWriter(); - for (i1 = 0; i1 < Data.MObjectHRC[SOi0].Instances.Length; i1++) + SO1 = MObjectHRC.Instances.Length.SortWriter(); + for (i1 = 0; i1 < MObjectHRC.Instances.Length; i1++) { SOi1 = SO1[i1]; - IO.Write(Data.MObjectHRC[SOi0].Instances[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b10000); - IO.Write(nameView + SOi1 + d + "name=", - Data.MObjectHRC[SOi0].Instances[SOi1]. Name); - IO.Write(Data.MObjectHRC[SOi0].Instances[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b01100); - IO.Write(nameView + SOi1 + d + "shadow=", - Data.MObjectHRC[SOi0].Instances[SOi1]. Shadow); - IO.Write(Data.MObjectHRC[SOi0].Instances[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b00010); - IO.Write(nameView + SOi1 + d + "uid_name=", - Data.MObjectHRC[SOi0].Instances[SOi1].UIDName); - IO.Write(Data.MObjectHRC[SOi0].Instances[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b00001); + nameView = name + "instance" + d + SOi1 + d; + ref MObjectHRC.Instance Instance = ref MObjectHRC.Instances[SOi1]; + + IO.Write(Instance.MT, nameView, A3DC, IsX, 0b10000); + IO.Write(nameView + "name=", Instance. Name); + IO.Write(Instance.MT, nameView, A3DC, IsX, 0b01100); + IO.Write(nameView + "shadow=", Instance. Shadow); + IO.Write(Instance.MT, nameView, A3DC, IsX, 0b00010); + IO.Write(nameView + "uid_name=", Instance.UIDName); + IO.Write(Instance.MT, nameView, A3DC, IsX, 0b00001); } - IO.Write(nameView + "length=", Data.MObjectHRC[SOi0].Instances.Length); + IO.Write(name + "instance.length=", MObjectHRC.Instances.Length); } - IO.Write(Data.MObjectHRC[SOi0].MT, name, A3DC, IsX, 0b10000); - IO.Write(name + "name=", Data.MObjectHRC[SOi0].Name); + IO.Write(MObjectHRC.MT, name, A3DC, IsX, 0b10000); + IO.Write(name + "name=", MObjectHRC.Name); - if (Data.MObjectHRC[SOi0].Node != null) + if (MObjectHRC.Node != null) { - nameView = name + "node" + d; - SO1 = Data.MObjectHRC[SOi0].Node.Length.SortWriter(); - for (i1 = 0; i1 < Data.MObjectHRC[SOi0].Node.Length; i1++) + SO1 = MObjectHRC.Node.Length.SortWriter(); + for (i1 = 0; i1 < MObjectHRC.Node.Length; i1++) { SOi1 = SO1[i1]; - IO.Write(Data.MObjectHRC[SOi0].Node[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b10000); - IO.Write(nameView + SOi1 + d + "name=", Data.MObjectHRC[SOi0].Node[SOi1].Name ); - IO.Write(nameView + SOi1 + d + "parent=", Data.MObjectHRC[SOi0].Node[SOi1].Parent); - IO.Write(Data.MObjectHRC[SOi0].Node[SOi1].MT, - nameView + SOi1 + d, A3DC, IsX, 0b01111); + nameView = name + "node" + d + SOi1 + d; + ref Node Node = ref MObjectHRC.Node[SOi1]; + + IO.Write(Node.MT, nameView, A3DC, IsX, 0b10000); + IO.Write(nameView + "name=", Node.Name ); + IO.Write(nameView + "parent=", Node.Parent); + IO.Write(Node.MT, nameView, A3DC, IsX, 0b01111); } - IO.Write(nameView + "length=", Data. MObjectHRC[SOi0].Node.Length); + IO.Write(name + "node.length=", MObjectHRC.Node.Length); } - IO.Write(Data.MObjectHRC[SOi0].MT, name, A3DC, IsX, 0b01111); + IO.Write(MObjectHRC.MT, name, A3DC, IsX, 0b01111); } IO.Write("m_objhrc.length=", Data.MObjectHRC.Length); } @@ -706,97 +700,95 @@ namespace KKdMainLib.A3DA if (Data.MObjectHRCList != null) { SO0 = Data.MObjectHRCList.Length.SortWriter(); - name = "m_objhrc_list" + d; for (i0 = 0; i0 < Data.MObjectHRCList.Length; i0++) - IO.Write(name + SO0[i0] + "=", Data.MObjectHRCList[SO0[i0]]); - IO.Write(name + "length=", Data.MObjectHRCList.Length); + IO.Write("m_objhrc_list" + d + SO0[i0] + "=", Data.MObjectHRCList[SO0[i0]]); + IO.Write("m_objhrc_list.length=", Data.MObjectHRCList.Length); } if (Data.MaterialList != null && IsX) { SO0 = Data.MaterialList.Length.SortWriter(); - name = "material_list" + d; for (i0 = 0; i0 < Data.MaterialList.Length; i0++) { SOi0 = SO0[i0]; - IO.Write(Data.MaterialList[SOi0].BlendColor , name + SOi0 + d, "blend_color" + d, A3DC); - IO.Write(Data.MaterialList[SOi0].GlowIntensity, name + SOi0 + d + "glow_intensity" , A3DC); - IO.Write(name + SOi0 + "hash_name=", Data.MaterialList[SOi0].HashName); - IO.Write(Data.MaterialList[SOi0].Incandescence, name + SOi0 + d, "incandescence" + d, A3DC); - IO.Write(name + SOi0 + "name=", Data.MaterialList[SOi0].Name); + name = "material_list" + d + SOi0 + d; + ref MaterialList ML = ref Data.MaterialList[SOi0]; + + IO.Write(ML.BlendColor , name + "blend_color" + d, A3DC); + IO.Write(ML.GlowIntensity, name + "glow_intensity" + d, A3DC); + IO.Write(name + "hash_name=", ML.HashName); + IO.Write(ML.Incandescence, name + "incandescence" + d, A3DC); + IO.Write(name + "name=", ML. Name); } - IO.Write(name + "length=", Data.MaterialList.Length); + IO.Write("material_list.length=", Data.MaterialList.Length); } if (Data.Motion != null) { SO0 = Data.Motion.Length.SortWriter(); - name = "motion" + d; for (i0 = 0; i0 < Data.Motion.Length; i0++) IO.Write(name + SO0[i0] + d + "name=", Data.Motion[SO0[i0]]); - IO.Write(name + "length=", Data.Motion.Length); + IO.Write("motion.length=", Data.Motion.Length); } if (Data.Object != null) { SO0 = Data.Object.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.Object.Length; i0++) { SOi0 = SO0[i0]; name = "object" + d + SOi0 + d; + ref Object Object = ref Data.Object[SOi0]; - IO.Write(Data.Object[SOi0].MT, name, A3DC, IsX, 0b10000); - if (Data.Object[SOi0].Morph != null) + IO.Write(Object.MT, name, A3DC, IsX, 0b10000); + if (Object.Morph != null) { - IO.Write(name + "morph=" , Data.Object[SOi0].Morph ); - IO.Write(name + "morph_offset=", Data.Object[SOi0].MorphOffset); + IO.Write(name + "morph=" , Object.Morph ); + IO.Write(name + "morph_offset=", Object.MorphOffset); } - IO.Write(name + "name=" , Data.Object[SOi0].Name ); - IO.Write(name + "parent_name=", Data.Object[SOi0].ParentName); - IO.Write(Data.Object[SOi0].MT, name, A3DC, IsX, 0b01100); + IO.Write(name + "name=" , Object.Name ); + IO.Write(name + "parent_name=", Object.ParentName); + IO.Write(Object.MT, name, A3DC, IsX, 0b01100); - if (Data.Object[SOi0].TexPat != null) + if (Object.TexPat != null) { - nameView = name + "tex_pat" + d; - SO1 = Data.Object[SOi0].TexPat.Length.SortWriter(); - for (i1 = 0; i1 < Data.Object[SOi0].TexPat.Length; i1++) + SO1 = Object.TexPat.Length.SortWriter(); + for (i1 = 0; i1 < Object.TexPat.Length; i1++) { SOi1 = SO1[i1]; - IO.Write(nameView + SOi1 + d + "name=" , Data.Object[SOi0].TexPat[SOi1].Name ); - IO.Write(nameView + SOi1 + d + "pat=" , Data.Object[SOi0].TexPat[SOi1].Pat ); - IO.Write(nameView + SOi1 + d + "pat_offset=", Data.Object[SOi0].TexPat[SOi1].PatOffset); + nameView = name + "tex_pat" + d + SOi1 + d; + ref Object.TexturePattern TexPat = ref Object.TexPat[SOi1]; + + IO.Write(nameView + "name=" , TexPat.Name ); + IO.Write(nameView + "pat=" , TexPat.Pat ); + IO.Write(nameView + "pat_offset=", TexPat.PatOffset); } - IO.Write(nameView + "length=", Data.Object[SOi0].TexPat.Length); + IO.Write(nameView + "length=", Object.TexPat.Length); } - if (Data.Object[SOi0].TexTrans != null) + if (Object.TexTrans != null) { - SO1 = Data.Object[SOi0].TexTrans.Length.SortWriter(); - nameView = name + "tex_transform" + d; - for (i1 = 0; i1 < Data.Object[SOi0].TexTrans.Length; i1++) + SO1 = Object.TexTrans.Length.SortWriter(); + for (i1 = 0; i1 < Object.TexTrans.Length; i1++) { SOi1 = SO1[i1]; - IO.Write(nameView + SOi1 + d + "name=", Data.Object[SOi0].TexTrans[SOi1].Name); - IO.Write(Data.Object[SOi0].TexTrans[SOi1].Coverage , - nameView + SOi1 + d, "coverage" , A3DC); - IO.Write(Data.Object[SOi0].TexTrans[SOi1].Offset , - nameView + SOi1 + d, "offset" , A3DC); - IO.Write(Data.Object[SOi0].TexTrans[SOi1].Repeat , - nameView + SOi1 + d, "repeat" , A3DC); - IO.Write(Data.Object[SOi0].TexTrans[SOi1]. Rotate , - nameView + SOi1 + d, "rotate" , A3DC); - IO.Write(Data.Object[SOi0].TexTrans[SOi1]. RotateFrame, - nameView + SOi1 + d, "rotateFrame" , A3DC); - IO.Write(Data.Object[SOi0].TexTrans[SOi1].TranslateFrame, - nameView + SOi1 + d, "translateFrame", A3DC); + nameView = name + "tex_transform" + d + SOi1 + d; + ref Object.TextureTransform TexTrans = ref Object.TexTrans[SOi1]; + + IO.Write(nameView + "name=", Object.TexTrans[SOi1].Name); + IO.Write(TexTrans.Coverage , nameView + "coverage" , A3DC); + IO.Write(TexTrans.Offset , nameView + "offset" , A3DC); + IO.Write(TexTrans.Repeat , nameView + "repeat" , A3DC); + IO.Write(TexTrans. Rotate , nameView + "rotate" , A3DC); + IO.Write(TexTrans. RotateFrame, nameView + "rotateFrame" , A3DC); + IO.Write(TexTrans.TranslateFrame, nameView + "translateFrame", A3DC); } - IO.Write(nameView + "length=", + Data.Object[SOi0].TexTrans.Length); + IO.Write(name + "tex_transform.length=", + Object.TexTrans.Length); } - IO.Write(Data.Object[SOi0].MT, name, A3DC, IsX, 0b00010); - IO.Write(name + "uid_name=", Data.Object[SOi0].UIDName); - IO.Write(Data.Object[SOi0].MT, name, A3DC, IsX, 0b00001); + IO.Write(Object.MT, name, A3DC, IsX, 0b00010); + IO.Write(name + "uid_name=", Object.UIDName); + IO.Write(Object.MT, name, A3DC, IsX, 0b00001); } IO.Write("object.length=", Data.Object.Length); } @@ -812,38 +804,41 @@ namespace KKdMainLib.A3DA if (Data.ObjectHRC != null) { SO0 = Data.ObjectHRC.Length.SortWriter(); - SOi0 = 0; for (i0 = 0; i0 < Data.ObjectHRC.Length; i0++) { SOi0 = SO0[i0]; name = "objhrc" + d + SOi0 + d; - IO.Write(name + "name=", Data.ObjectHRC[SOi0].Name); + ref ObjectHRC ObjectHRC = ref Data.ObjectHRC[SOi0]; - if (IsX && Data.ObjectHRC[SOi0].JointOrient.NotNull) + IO.Write(name + "name=", ObjectHRC.Name); + + if (IsX && ObjectHRC.JointOrient.NotNull) { - IO.Write(name + "joint_orient.x=", Data.ObjectHRC[SOi0].JointOrient.X); - IO.Write(name + "joint_orient.y=", Data.ObjectHRC[SOi0].JointOrient.Y); - IO.Write(name + "joint_orient.z=", Data.ObjectHRC[SOi0].JointOrient.Z); + IO.Write(name + "joint_orient.x=", ObjectHRC.JointOrient.X); + IO.Write(name + "joint_orient.y=", ObjectHRC.JointOrient.Y); + IO.Write(name + "joint_orient.z=", ObjectHRC.JointOrient.Z); } - if (Data.ObjectHRC[SOi0].Node != null) + if (ObjectHRC.Node != null) { - SO1 = Data.ObjectHRC[SOi0].Node.Length.SortWriter(); - nameView = name + "node" + d; - for (i1 = 0; i1 < Data.ObjectHRC[SOi0].Node.Length; i1++) + SO1 = ObjectHRC.Node.Length.SortWriter(); + for (i1 = 0; i1 < ObjectHRC.Node.Length; i1++) { SOi1 = SO1[i1]; - IO.Write(Data.ObjectHRC[SOi0].Node[SOi1].MT, nameView + SOi1 + d, A3DC, IsX, 0b10000); - IO.Write(nameView + SOi1 + d + "name=" , Data.ObjectHRC[SOi0].Node[SOi1].Name ); - IO.Write(nameView + SOi1 + d + "parent=", Data.ObjectHRC[SOi0].Node[SOi1].Parent); - IO.Write(Data.ObjectHRC[SOi0].Node[SOi1].MT, nameView + SOi1 + d, A3DC, IsX, 0b01111); + nameView = name + "node" + d + SOi1 + d; + ref Node Node = ref ObjectHRC.Node[SOi1]; + + IO.Write(Node.MT, nameView, A3DC, IsX, 0b10000); + IO.Write(nameView + "name=" , Node.Name ); + IO.Write(nameView + "parent=", Node.Parent); + IO.Write(Node.MT, nameView, A3DC, IsX, 0b01111); } - IO.Write(nameView + "length=", Data.ObjectHRC[SOi0].Node.Length); + IO.Write(nameView + "length=", ObjectHRC.Node.Length); } - if (Data.ObjectHRC[SOi0].Shadow != null) - IO.Write(name + "shadow=", Data.ObjectHRC[SOi0].Shadow); - IO.Write(name + "uid_name=", Data.ObjectHRC[SOi0].UIDName); + if (ObjectHRC.Shadow != null) + IO.Write(name + "shadow=", ObjectHRC.Shadow); + IO.Write(name + "uid_name=", ObjectHRC.UIDName); } IO.Write("objhrc.length=", Data.ObjectHRC.Length); } @@ -861,21 +856,22 @@ namespace KKdMainLib.A3DA IO.Write("play_control.div=", Data.PlayControl.Div); IO.Write("play_control.fps=", Data.PlayControl.FPS); if (Data.PlayControl.Offset != null) - { if ( A3DC) { IO.Write("play_control.offset=", Data.PlayControl.Offset); - IO.Write("play_control.size=" , Data.PlayControl.Size ); } + { if (A3DC) { IO.Write("play_control.offset=", Data.PlayControl.Offset); + IO.Write("play_control.size=" , Data.PlayControl.Size ); } else IO.Write("play_control.size=", Data.PlayControl.Size + Data.PlayControl.Offset); } else IO.Write("play_control.size=", Data.PlayControl.Size); if (Data.PostProcess != null) { + ref PostProcess PP = ref Data.PostProcess; name = "post_process" + d; - IO.Write(Data.PostProcess.Ambient , name, "Ambient" , A3DC); - IO.Write(Data.PostProcess.Diffuse , name, "Diffuse" , A3DC); - IO.Write(Data.PostProcess.Specular , name, "Specular" , A3DC); - IO.Write(Data.PostProcess.LensFlare, name, "lens_flare", A3DC); - IO.Write(Data.PostProcess.LensGhost, name, "lens_ghost", A3DC); - IO.Write(Data.PostProcess.LensShaft, name, "lens_shaft", A3DC); + IO.Write(PP.Ambient , name + "Ambient" , A3DC); + IO.Write(PP.Diffuse , name + "Diffuse" , A3DC); + IO.Write(PP.Specular , name + "Specular" , A3DC); + IO.Write(PP.LensFlare, name + "lens_flare", true, A3DC); + IO.Write(PP.LensGhost, name + "lens_ghost", true, A3DC); + IO.Write(PP.LensShaft, name + "lens_shaft", true, A3DC); } if (Data.Point != null) @@ -1252,10 +1248,10 @@ namespace KKdMainLib.A3DA private void Write(ref Key Key, bool F16 = false) { - if (Key == null || Key.Type == null) return; + if (Key.Type == null) return; int i = 0; - if (Key.Trans != null) + if (Key.Keys != null) { Key.BinOffset = IO.Position; int Type = (int)Key.Type & 0xFF; @@ -1264,18 +1260,14 @@ namespace KKdMainLib.A3DA IO.Write(Type); IO.Write(0x00); IO.Write((float)Key.Max); - IO.Write(Key.Trans.Length); - for (i = 0; i < Key.Trans.Length; i++) + IO.Write(Key.Keys.Length); + for (i = 0; i < Key.Keys.Length; i++) { - ref KFT3 KF = ref Key.Trans[i]; - if (F16 && CompressF16 > 0) - { IO.Write((ushort)KF.F ); IO.Write(( Half)KF.V ); } - else - { IO.Write(( float)KF.F ); IO.Write((float)KF.V ); } - if (F16 && CompressF16 == 2) - { IO.Write(( Half)KF.T1); IO.Write(( Half)KF.T2); } - else - { IO.Write(( float)KF.T1); IO.Write((float)KF.T2); } + ref KFT3 KF = ref Key.Keys[i]; + if (F16 && CompressF16 > 0) { IO.Write((ushort)KF.F ); IO.Write((Half)KF.V ); } + else { IO.Write( KF.F ); IO.Write( KF.V ); } + if (F16 && CompressF16 == 2) { IO.Write(( Half)KF.T1); IO.Write((Half)KF.T2); } + else { IO.Write( KF.T1); IO.Write( KF.T2); } } } else @@ -1356,7 +1348,7 @@ namespace KKdMainLib.A3DA Aspect = ViewPoint.ReadNDouble("Aspect" ), CameraApertureH = ViewPoint.ReadNDouble("CameraApertureH"), CameraApertureW = ViewPoint.ReadNDouble("CameraApertureW"), - FOVHorizontal = ViewPoint.ReadNDouble("FOVHorizontal" ), + FOVHorizontal = ViewPoint.ReadBoolean("FOVHorizontal" ), FocalLength = ViewPoint.ReadKey ("FocalLength" ), FOV = ViewPoint.ReadKey ("FOV" ), Roll = ViewPoint.ReadKey ("Roll" ), @@ -1789,7 +1781,7 @@ namespace KKdMainLib.A3DA { MsgPack MObjectHRCList = new MsgPack(Data.MObjectHRCList.Length, "MObjectHRCList"); for (i = 0; i < Data.MObjectHRCList.Length; i++) - MObjectHRCList[i] = (MsgPack)Data.MObjectHRCList[i]; + MObjectHRCList[i] = Data.MObjectHRCList[i]; A3D.Add(MObjectHRCList); } @@ -1809,7 +1801,7 @@ namespace KKdMainLib.A3DA if (Data.Motion != null) { MsgPack Motion = new MsgPack(Data.Motion.Length, "Motion"); - for (i = 0; i < Data.Motion.Length; i++) Motion[i] = (MsgPack)Data.Motion[i]; + for (i = 0; i < Data.Motion.Length; i++) Motion[i] = Data.Motion[i]; A3D.Add(Motion); } @@ -1884,14 +1876,14 @@ namespace KKdMainLib.A3DA if (Data.ObjectHRCList != null) { MsgPack ObjectHRCList = new MsgPack(Data.ObjectHRCList.Length, "ObjectHRCList"); - for (i = 0; i < Data.ObjectHRCList.Length; i++) ObjectHRCList[i] = (MsgPack)Data.ObjectHRCList[i]; + for (i = 0; i < Data.ObjectHRCList.Length; i++) ObjectHRCList[i] = Data.ObjectHRCList[i]; A3D.Add(ObjectHRCList); } if (Data.ObjectList != null) { MsgPack ObjectList = new MsgPack(Data.ObjectList.Length, "ObjectList"); - for (i = 0; i < Data.ObjectList.Length; i++) ObjectList[i] = (MsgPack)Data.ObjectList[i]; + for (i = 0; i < Data.ObjectList.Length; i++) ObjectList[i] = Data.ObjectList[i]; A3D.Add(ObjectList); } @@ -1959,9 +1951,8 @@ namespace KKdMainLib.A3DA { Key Key = new Key(); if ( Dict.FindValue(out Key.BinOffset, Temp + BO )) return Key; - if (!Dict.FindValue(out int Type , Temp + "type")) return null; + if (!Dict.FindValue(out int Type , Temp + "type")) return default; - if (Key.BinOffset != null) return null; Key.Type = (Key.Interpolation)Type; if (Type == 0x0000) return Key; if (Type == 0x0001) { Dict.FindValue(out Key.Value, Temp + "value"); return Key; } @@ -1985,47 +1976,47 @@ namespace KKdMainLib.A3DA int DS = Key.RawData.KeyType + 1; Key.Length = Key.RawData.ValueListSize / DS; - Key.Trans = new KFT3[Key.Length]; + Key.Keys = new KFT3[Key.Length]; if (Key.RawData.KeyType == 0) for (i = 0; i < Key.Length; i++) - Key.Trans[i] = new KFT3 - (ValueList[i * DS + 0].ToDouble()); + Key.Keys[i] = new KFT3 + (ValueList[i * DS + 0].ToSingle()); else if (Key.RawData.KeyType == 1) for (i = 0; i < Key.Length; i++) - Key.Trans[i] = new KFT3 - (ValueList[i * DS + 0].ToDouble(), ValueList[i * DS + 1].ToDouble()); + Key.Keys[i] = new KFT3 + (ValueList[i * DS + 0].ToSingle(), ValueList[i * DS + 1].ToSingle()); else if (Key.RawData.KeyType == 2) for (i = 0; i < Key.Length; i++) - Key.Trans[i] = new KFT3 - (ValueList[i * DS + 0].ToDouble(), ValueList[i * DS + 1].ToDouble(), - ValueList[i * DS + 2].ToDouble(), ValueList[i * DS + 2].ToDouble()); + Key.Keys[i] = new KFT3 + (ValueList[i * DS + 0].ToSingle(), ValueList[i * DS + 1].ToSingle(), + ValueList[i * DS + 2].ToSingle(), ValueList[i * DS + 2].ToSingle()); else if (Key.RawData.KeyType == 3) for (i = 0; i < Key.Length; i++) - Key.Trans[i] = new KFT3 - (ValueList[i * DS + 0].ToDouble(), ValueList[i * DS + 1].ToDouble(), - ValueList[i * DS + 2].ToDouble(), ValueList[i * DS + 3].ToDouble()); + Key.Keys[i] = new KFT3 + (ValueList[i * DS + 0].ToSingle(), ValueList[i * DS + 1].ToSingle(), + ValueList[i * DS + 2].ToSingle(), ValueList[i * DS + 3].ToSingle()); Key.RawData.ValueList = null; } else { - Key.Trans = new KFT3[Key.Length]; + Key.Keys = new KFT3[Key.Length]; for (i = 0; i < Key.Length; i++) { if (!Dict.FindValue(out value, Temp + "key" + d + i + d + "data")) continue; dataArray = value.Replace("(", "").Replace(")", "").Split(','); Type = dataArray.Length - 1; - if (Type == 0) Key.Trans[i] = new KFT3 - (dataArray[0].ToDouble()); - else if (Type == 1) Key.Trans[i] = new KFT3 - (dataArray[0].ToDouble(), dataArray[1].ToDouble()); - else if (Type == 2) Key.Trans[i] = new KFT3 - (dataArray[0].ToDouble(), dataArray[1].ToDouble(), - dataArray[2].ToDouble(), dataArray[2].ToDouble()); - else if (Type == 3) Key.Trans[i] = new KFT3 - (dataArray[0].ToDouble(), dataArray[1].ToDouble(), - dataArray[2].ToDouble(), dataArray[3].ToDouble()); + if (Type == 0) Key.Keys[i] = new KFT3 + (dataArray[0].ToSingle()); + else if (Type == 1) Key.Keys[i] = new KFT3 + (dataArray[0].ToSingle(), dataArray[1].ToSingle()); + else if (Type == 2) Key.Keys[i] = new KFT3 + (dataArray[0].ToSingle(), dataArray[1].ToSingle(), + dataArray[2].ToSingle(), dataArray[2].ToSingle()); + else if (Type == 3) Key.Keys[i] = new KFT3 + (dataArray[0].ToSingle(), dataArray[1].ToSingle(), + dataArray[2].ToSingle(), dataArray[3].ToSingle()); } } return Key; @@ -2045,35 +2036,35 @@ namespace KKdMainLib.A3DA if ((Flags & 0b00001) == 0b00001) IO.Write(MT.Visibility, Temp + "visibility" + d, A3DC); } - public static void Write(this Stream IO, Vector4 RGBA, string Temp, string Data, bool A3DC = false) + public static void Write(this Stream IO, Vector4 RGBA, string Temp, bool A3DC = false) { - if (RGBA.X == null && RGBA.Y == null && RGBA.Z == null && RGBA.W == null) return; - IO.Write(Temp + Data + "=", "true"); - IO.Write(RGBA.W, Temp + Data + d + "a" + d, A3DC); - IO.Write(RGBA.Z, Temp + Data + d + "b" + d, A3DC); - IO.Write(RGBA.Y, Temp + Data + d + "g" + d, A3DC); - IO.Write(RGBA.X, Temp + Data + d + "r" + d, A3DC); + if (RGBA.X.Type == null && RGBA.Y.Type == null && RGBA.Z.Type == null && RGBA.W.Type == null) return; + IO.Write(Temp + "=", "true"); + IO.Write(RGBA.W, Temp + d + "a" + d, A3DC); + IO.Write(RGBA.Z, Temp + d + "b" + d, A3DC); + IO.Write(RGBA.Y, Temp + d + "g" + d, A3DC); + IO.Write(RGBA.X, Temp + d + "r" + d, A3DC); } public static void Write(this Stream IO, Vector3 Key, string Temp, bool A3DC = false) { IO.Write(Key.X, Temp + "x" + d, A3DC); IO.Write(Key.Y, Temp + "y" + d, A3DC); IO.Write(Key.Z, Temp + "z" + d, A3DC); } - public static void Write(this Stream IO, Vector2 UV, string Temp, string Data, bool A3DC = false) - { IO.Write(UV.X, Temp, Data + "U", A3DC); IO.Write(UV.Y, Temp, Data + "V", A3DC); } + public static void Write(this Stream IO, Vector2 UV, string Temp, bool A3DC = false) + { IO.Write(UV.X, Temp + "U", true, A3DC); IO.Write(UV.Y, Temp + "V", true, A3DC); } - public static void Write(this Stream IO, Key Key, string Temp, string Data, bool A3DC = false) - { if (Key != null) { IO.Write(Temp + Data + "=", "true"); IO.Write(Key, Temp + Data + d, A3DC); } } + public static void Write(this Stream IO, Key Key, string Temp, bool SetBoolean, bool A3DC = false) + { if (Key.Type == null) return; if (SetBoolean) IO.Write(Temp + "=", "true"); IO.Write(Key, Temp + d, A3DC); } public static void Write(this Stream IO, Key Key, string Temp, bool A3DC = false) { - if (Key == null || Key.Type == null) return; + if (Key.Type == null) return; if (A3DC) { IO.Write(Temp + BO + "=", Key.BinOffset); return; } int i = 0; - if (Key.Trans != null) - if (Key.Trans.Length == 0) + if (Key.Keys != null) + if (Key.Keys.Length == 0) { IO.Write(Temp + "type=", (int)Key.Type); if (Key.Type > 0) IO.Write(Temp + "value=", Key.Value); @@ -2082,50 +2073,50 @@ namespace KKdMainLib.A3DA if (Key.EPTypePost != null) IO.Write(Temp + "ep_type_post=", Key.EPTypePost); if (Key.EPTypePre != null) IO.Write(Temp + "ep_type_pre=" , Key.EPTypePre ); - if (Key.RawData.KeyType == 0 && Key.Trans != null) + if (Key.RawData.KeyType == 0 && Key.Keys != null) { - IKF KF; - SO = Key.Trans.Length.SortWriter(); - for (i = 0; i < Key.Trans.Length; i++) + IKF KF; + SO = Key.Keys.Length.SortWriter(); + for (i = 0; i < Key.Keys.Length; i++) { SOi = SO[i]; - KF = Key.Trans[SOi].Check(); - IO.Write(Temp + "key" + d + SOi + d + "data=", KF.ToString()); + KF = Key.Keys[SOi].Check(); + IO.Write(Temp + "key" + d + SOi + d + "data=", KF.Round(7).ToString()); int Type = 0; - if (KF is KFT0) Type = 0; - else if (KF is KFT1) Type = 1; - else if (KF is KFT2) Type = 2; - else if (KF is KFT3) Type = 3; + if (KF is KFT0) Type = 0; + else if (KF is KFT1) Type = 1; + else if (KF is KFT2) Type = 2; + else if (KF is KFT3) Type = 3; IO.Write(Temp + "key" + d + SOi + d + "type=", Type); } IO.Write(Temp + "key.length=", Key.Length); if (Key.Max != null) IO.Write(Temp + "max=", Key.Max); } - else if (Key.Trans != null) + else if (Key.Keys != null) { - int Length = Key.Trans.Length; + int Length = Key.Keys.Length; ref int KeyType = ref Key.RawData.KeyType; KeyType = 0; - IKF KF; + IKF KF; if (Key.Max != null) IO.Write(Temp + "max=", Key.Max); for (i = 0; i < Length; i++) { - KF = Key.Trans[i].Check(); - if (KF is KFT0 && KeyType < 0) KeyType = 0; - else if (KF is KFT1 && KeyType < 1) KeyType = 1; - else if (KF is KFT2 && KeyType < 2) KeyType = 2; - else if (KF is KFT3 && KeyType < 3) break; + KF = Key.Keys[i].Check(); + if (KF is KFT0 && KeyType < 0) KeyType = 0; + else if (KF is KFT1 && KeyType < 1) KeyType = 1; + else if (KF is KFT2 && KeyType < 2) KeyType = 2; + else if (KF is KFT3 && KeyType < 3) { KeyType = 3; break; } } Key.RawData.ValueListSize = Length * KeyType + Length; IO.Write(Temp + "raw_data.value_list="); if (KeyType == 0) for (i = 0; i < Length; i++) - IO.Write(Key.Trans[i].ToT0().ToString(false) + ((i + 1 < Length) ? "," : "")); + IO.Write(Key.Keys[i].ToT0().ToString(false) + (i + 1 < Length ? "," : "")); else if (KeyType == 1) for (i = 0; i < Length; i++) - IO.Write(Key.Trans[i].ToT1().ToString(false) + ((i + 1 < Length) ? "," : "")); + IO.Write(Key.Keys[i].ToT1().ToString(false) + (i + 1 < Length ? "," : "")); else if (KeyType == 2) for (i = 0; i < Length; i++) - IO.Write(Key.Trans[i].ToT2().ToString(false) + ((i + 1 < Length) ? "," : "")); + IO.Write(Key.Keys[i].ToT2().ToString(false) + (i + 1 < Length ? "," : "")); else if (KeyType == 3) for (i = 0; i < Length; i++) - IO.Write(Key.Trans[i].ToT3().ToString(false) + ((i + 1 < Length) ? "," : "")); + IO.Write(Key.Keys[i] .ToString(false) + (i + 1 < Length ? "," : "")); IO.Position--; IO.Write('\n'); IO.Write(Temp + "raw_data.value_list_size=", Key.RawData.ValueListSize); @@ -2133,7 +2124,7 @@ namespace KKdMainLib.A3DA IO.Write(Temp + "raw_data_key_type=" , Key.RawData. KeyType ); } IO.Write(Temp + "type=", (int)Key.Type); - if (Key.RawData.KeyType == 0 && Key.Trans == null && Key.Value != null) + if (Key.RawData.KeyType == 0 && Key.Keys == null && Key.Value != null) if (Key.Value != 0) IO.Write(Temp + "value=", Key.Value); } @@ -2167,12 +2158,10 @@ namespace KKdMainLib.A3DA public static void ReadKey(this Stream IO, ref Key Key, int C_F16, bool F16 = false) { - if (Key == null) return; if (Key.BinOffset == null || Key.BinOffset < 0) return; IO.Position = (int)Key.BinOffset; int Type = IO.ReadInt32(); - Key.Value = IO.ReadSingle(); Key.Type = (Key.Interpolation)(Type & 0xFF); if (Key.Type < Key.Interpolation.Lerp) return; @@ -2185,22 +2174,18 @@ namespace KKdMainLib.A3DA if (Key.EPTypePost == 0) Key.EPTypePost = null; if (Key.EPTypePre == 0) Key.EPTypePre = null; } - Key.Trans = new KFT3[Key.Length]; - KFT3 Temp; + Key.Keys = new KFT3[Key.Length]; for (int i = 0; i < Key.Length; i++) { - Temp = new KFT3(); if (F16 && C_F16 > 0) - { Temp.F = IO.ReadUInt16(); Temp.V = (double)IO.ReadHalf (); } + { Key.Keys[i].F = IO.ReadUInt16(); Key.Keys[i].V = IO.ReadHalf (); } else - { Temp.F = IO.ReadSingle(); Temp.V = IO.ReadSingle(); } + { Key.Keys[i].F = IO.ReadSingle(); Key.Keys[i].V = IO.ReadSingle(); } if (F16 && C_F16 == 2) - { Temp.T1 = (double)IO.ReadHalf (); Temp.T2 = (double)IO.ReadHalf (); } + { Key.Keys[i].T1 = IO.ReadHalf (); Key.Keys[i].T2 = IO.ReadHalf (); } else - { Temp.T1 = IO.ReadSingle(); Temp.T2 = IO.ReadSingle(); } - - Key.Trans[i] = Temp; + { Key.Keys[i].T1 = IO.ReadSingle(); Key.Keys[i].T2 = IO.ReadSingle(); } } } @@ -2264,7 +2249,7 @@ namespace KKdMainLib.A3DA public static Key ReadKey(this MsgPack k) { - if (k.Object == null) return null; + if (k.Object == null) return default; Key Key = new Key { EPTypePost = k.ReadNInt32("EPTypePost"), EPTypePre = k.ReadNInt32("EPTypePre"), Max = k.ReadNDouble("Max"), Value = k.ReadNDouble("Value") }; @@ -2277,25 +2262,25 @@ namespace KKdMainLib.A3DA if (!k.ElementArray("Trans", out MsgPack Trans)) return Key; Key.Length = Trans.Array.Length; - Key.Trans = new KFT3[Key.Length]; + Key.Keys = new KFT3[Key.Length]; for (int i = 0; i < Key.Length; i++) { if (Trans[i].Array == null) continue; else if (Trans[i].Array.Length == 0) continue; else if (Trans[i].Array.Length == 1) - Key.Trans[i] = new KFT3 - (Trans[i][0].ReadDouble()); + Key.Keys[i] = new KFT3 + (Trans[i][0].ReadSingle()); else if (Trans[i].Array.Length == 2) - Key.Trans[i] = new KFT3 - (Trans[i][0].ReadDouble(), Trans[i][1].ReadDouble()); + Key.Keys[i] = new KFT3 + (Trans[i][0].ReadSingle(), Trans[i][1].ReadSingle()); else if (Trans[i].Array.Length == 3) - Key.Trans[i] = new KFT3 - (Trans[i][0].ReadDouble(), Trans[i][1].ReadDouble(), - Trans[i][2].ReadDouble(), Trans[i][2].ReadDouble()); + Key.Keys[i] = new KFT3 + (Trans[i][0].ReadSingle(), Trans[i][1].ReadSingle(), + Trans[i][2].ReadSingle(), Trans[i][2].ReadSingle()); else if (Trans[i].Array.Length == 4) - Key.Trans[i] = new KFT3 - (Trans[i][0].ReadDouble(), Trans[i][1].ReadDouble(), - Trans[i][2].ReadDouble(), Trans[i][3].ReadDouble()); + Key.Keys[i] = new KFT3 + (Trans[i][0].ReadSingle(), Trans[i][1].ReadSingle(), + Trans[i][2].ReadSingle(), Trans[i][3].ReadSingle()); } return Key; } @@ -2313,7 +2298,7 @@ namespace KKdMainLib.A3DA .Add("Visibility", MT.Visibility)); public static MsgPack Add(this MsgPack MsgPack, string name, Vector4 RGBA) => - (RGBA.X == null && RGBA.Y == null && RGBA.Z == null && RGBA.W == null) ? MsgPack : + (RGBA.X.Type == null && RGBA.Y.Type == null && RGBA.Z.Type == null && RGBA.W.Type == null) ? MsgPack : MsgPack.Add(new MsgPack(name).Add("R", RGBA.X).Add("G", RGBA.Y) .Add("B", RGBA.Z).Add("A", RGBA.W)); @@ -2321,36 +2306,32 @@ namespace KKdMainLib.A3DA MsgPack.Add(new MsgPack(name).Add("X", Key.X).Add("Y", Key.Y).Add("Z", Key.Z)); public static MsgPack Add(this MsgPack MsgPack, string name, Vector2 UV) => - (UV.X == null && UV.Y == null) ? MsgPack : + (UV.X.Type == null && UV.Y.Type == null) ? MsgPack : MsgPack.Add(new MsgPack(name).Add("U", UV.X).Add("V", UV.Y)); public static MsgPack Add(this MsgPack MsgPack, string name, Key Key) { - if (Key == null || Key.Type == null) return MsgPack; + if (Key.Type == null) return MsgPack; MsgPack Keys = new MsgPack(name).Add("Type", Key.Type.ToString()); - if (Key.Trans != null && Key.Type != Key.Interpolation.Null) + if (Key.Keys != null && Key.Type != Key.Interpolation.Null) { Keys = Keys.Add("Max", Key.Max).Add("EPTypePost", Key.EPTypePost).Add("EPTypePre", Key.EPTypePre); if (Key.RawData.KeyType != 0) Keys.Add("RawData", true); - MsgPack Trans = new MsgPack(Key.Trans.Length, "Trans"); - for (int i = 0; i < Key.Trans.Length; i++) + MsgPack Trans = new MsgPack(Key.Keys.Length, "Trans"); + for (int i = 0; i < Key.Keys.Length; i++) { - IKF KF = Key.Trans[i].Check(); - if (KF is KFT0 KFT0) - Trans[i] = new MsgPack(null, new MsgPack[] - { (MsgPack)KFT0.F }); - else if (KF is KFT1 KFT1) - Trans[i] = new MsgPack(null, new MsgPack[] - { (MsgPack)KFT1.F, (MsgPack)KFT1.V }); - else if (KF is KFT2 KFT2) - Trans[i] = new MsgPack(null, new MsgPack[] - { (MsgPack)KFT2.F, (MsgPack)KFT2.V, (MsgPack)KFT2.T }); - else if (KF is KFT3 KFT3) - Trans[i] = new MsgPack(null, new MsgPack[] - { (MsgPack)KFT3.F, (MsgPack)KFT3.V, (MsgPack)KFT3.T1, (MsgPack)KFT3.T2, }); + IKF KF = Key.Keys[i].Check(); + if (KF is KFT0 KFT0) Trans[i] = new MsgPack(null, new MsgPack[] + { KFT0.F }); + else if (KF is KFT1 KFT1) Trans[i] = new MsgPack(null, new MsgPack[] + { KFT1.F, KFT1.V }); + else if (KF is KFT2 KFT2) Trans[i] = new MsgPack(null, new MsgPack[] + { KFT2.F, KFT2.V, KFT2.T }); + else if (KF is KFT3 KFT3) Trans[i] = new MsgPack(null, new MsgPack[] + { KFT3.F, KFT3.V, KFT3.T1, KFT3.T2, }); } Keys.Add(Trans); } @@ -2448,8 +2429,8 @@ namespace KKdMainLib.A3DA public struct ViewPoint { + public bool? FOVHorizontal; public double? Aspect; - public double? FOVHorizontal; public double? CameraApertureH; public double? CameraApertureW; public Key FOV; @@ -2493,7 +2474,7 @@ namespace KKdMainLib.A3DA public Vector4 Diffuse; } - public class Key + public struct Key { public Interpolation? Type; public int Length; @@ -2503,7 +2484,7 @@ namespace KKdMainLib.A3DA public double? Max; public double? Value; public RawD RawData; - public KFT3[] Trans; + public KFT3[] Keys; public struct RawD { diff --git a/KKdMainLib/Aet.cs b/KKdMainLib/Aet.cs index c1714b1..e155eeb 100644 --- a/KKdMainLib/Aet.cs +++ b/KKdMainLib/Aet.cs @@ -459,7 +459,7 @@ namespace KKdMainLib.Aet else if (val.Count > 1) { MsgPack Val = new MsgPack(val.Count, Name); - for (int i = 0; i < val.Count; i++) Val[i] = (MsgPack)val[i]; + for (int i = 0; i < val.Count; i++) Val[i] = val[i]; return MsgPack.Add(Val); } return MsgPack; diff --git a/KKdMainLib/Extensions.cs b/KKdMainLib/Extensions.cs index ecf351c..c370a9a 100644 --- a/KKdMainLib/Extensions.cs +++ b/KKdMainLib/Extensions.cs @@ -79,7 +79,7 @@ namespace KKdMainLib public static class StructExtensions { - public static Struct ReadStruct(byte[] Data) + public static Struct ReadStruct(this byte[] Data) { Stream stream = File.OpenReader(Data); Struct Struct = stream.ReadStruct(stream.ReadHeader(false)); @@ -126,6 +126,11 @@ namespace KKdMainLib if (SubStructs.Capacity > 0) Struct.SubStructs = SubStructs.ToArray(); return Struct; } + + public static void Write(this Stream stream, Struct Struct) + { + + } } public static class MPExt @@ -204,4 +209,31 @@ namespace KKdMainLib public static void ToMsgPack(this string file) => file.ReadMP(true).Write(file ).Dispose(); } + + public static class IKFExt + { + public static IKF Round(this IKF KF, int d) + { + if (KF is KFT0 KFT0) { KFT0.F = KFT0.F.Round(d); return KFT0; } + else if (KF is KFT1 KFT1) { KFT1.F = KFT1.F.Round(d); + KFT1.V = KFT1.V.Round(d); return KFT1; } + else if (KF is KFT2 KFT2) { KFT2.F = KFT2.F.Round(d); + KFT2.V = KFT2.V.Round(d); KFT2.T = KFT2.T .Round(d); return KFT2; } + else if (KF is KFT3 KFT3) { KFT3.F = KFT3.F.Round(d); + KFT3.V = KFT3.V.Round(d); KFT3.T1 = KFT3.T1.Round(d); KFT3.T2 = KFT3.T2.Round(d); return KFT3; } + return KF; + } + + public static IKF Round(this IKF KF, int d) + { + if (KF is KFT0 KFT0) { KFT0.F = KFT0.F.Round(d); return KFT0; } + else if (KF is KFT1 KFT1) { KFT1.F = KFT1.F.Round(d); + KFT1.V = KFT1.V.Round(d); return KFT1; } + else if (KF is KFT2 KFT2) { KFT2.F = KFT2.F.Round(d); + KFT2.V = KFT2.V.Round(d); KFT2.T = KFT2.T .Round(d); return KFT2; } + else if (KF is KFT3 KFT3) { KFT3.F = KFT3.F.Round(d); + KFT3.V = KFT3.V.Round(d); KFT3.T1 = KFT3.T1.Round(d); KFT3.T2 = KFT3.T2.Round(d); return KFT3; } + return KF; + } + } } diff --git a/KKdMainLib/IO/Extensions.cs b/KKdMainLib/IO/Extensions.cs index 351a746..273cc04 100644 --- a/KKdMainLib/IO/Extensions.cs +++ b/KKdMainLib/IO/Extensions.cs @@ -20,11 +20,11 @@ namespace KKdMainLib.IO return s.ToArray(); } - public static byte PeekByte(this Stream stream) + public static byte PeekByte (this Stream stream) { byte val = stream.ReadByte(); stream.LongPosition--; return val; } public static char PeekCharASCII(this Stream stream) { byte val = stream.ReadByte(); stream.LongPosition--; return (char)val; } - public static char PeekCharUTF8(this Stream stream) + public static char PeekCharUTF8 (this Stream stream) { long LongPosition = stream.LongPosition; char val = stream.ReadCharUTF8(); stream.LongPosition = LongPosition; return val; } @@ -106,44 +106,44 @@ namespace KKdMainLib.IO return s; } - public static Pointer ReadPointerStringShiftJIS(this Stream IO) - { Pointer val = IO.ReadPointer(); - val.Value = IO.ReadStringShiftJISAtOffset(val.Offset); return val; } + public static Pointer ReadPointerStringShiftJIS(this Stream stream) + { Pointer val = stream.ReadPointer(); + val.Value = stream.ReadStringShiftJISAtOffset(val.Offset); return val; } - public static string ReadStringShiftJISAtOffset(this Stream IO, long Offset = 0, long Length = 0) => - Text.ShiftJIS.GetString(IO.ReadAtOffset(Offset, Length)); + public static string ReadStringShiftJISAtOffset(this Stream stream, long Offset = 0, long Length = 0) => + Text.ShiftJIS.GetString(stream.ReadAtOffset(Offset, Length)); - public static void WriteShiftJIS(this Stream IO, string String) => - IO.Write(Text.ShiftJIS.GetBytes(String)); + public static void WriteShiftJIS(this Stream stream, string String) => + stream.Write(Text.ShiftJIS.GetBytes(String)); - public static Pointer ReadPointer(this Stream IO) => - new Pointer { Offset = IO.ReadInt32() }; + public static Pointer ReadPointer(this Stream stream) => + new Pointer { Offset = stream.ReadInt32() }; - public static Pointer ReadPointerString(this Stream IO) - { Pointer val = IO.ReadPointer(); - val.Value = IO.ReadStringAtOffset(val.Offset); return val; } + public static Pointer ReadPointerString(this Stream stream) + { Pointer val = stream.ReadPointer(); + val.Value = stream.ReadStringAtOffset(val.Offset); return val; } - public static CountPointer ReadCountPointer(this Stream IO) => - new CountPointer { Count = IO.ReadInt32(), Offset = IO.ReadInt32() }; + public static CountPointer ReadCountPointer(this Stream stream) => + new CountPointer { Count = stream.ReadInt32(), Offset = stream.ReadInt32() }; - public static Pointer ReadPointerEndian(this Stream IO) => - new Pointer { Offset = IO.ReadInt32Endian() }; + public static Pointer ReadPointerEndian(this Stream stream) => + new Pointer { Offset = stream.ReadInt32Endian() }; - public static Pointer ReadPointerStringEndian(this Stream IO) - { Pointer val = IO.ReadPointerEndian(); - val.Value = IO.ReadStringAtOffset(val.Offset); return val; } + public static Pointer ReadPointerStringEndian(this Stream stream) + { Pointer val = stream.ReadPointerEndian(); + val.Value = stream.ReadStringAtOffset(val.Offset); return val; } - public static CountPointer ReadCountPointerEndian(this Stream IO) => - new CountPointer { Count = IO.ReadInt32Endian(), Offset = IO.ReadInt32Endian() }; + public static CountPointer ReadCountPointerEndian(this Stream stream) => + new CountPointer { Count = stream.ReadInt32Endian(), Offset = stream.ReadInt32Endian() }; - public static Pointer ReadPointerX(this Stream IO) => - new Pointer { Offset = (int)IO.ReadIntX() }; + public static Pointer ReadPointerX(this Stream stream) => + new Pointer { Offset = (int)stream.ReadIntX() }; - public static Pointer ReadPointerStringX(this Stream IO) - { Pointer val = IO.ReadPointerX(); - val.Value = IO.ReadStringAtOffset(val.Offset); return val; } + public static Pointer ReadPointerStringX(this Stream stream) + { Pointer val = stream.ReadPointerX(); + val.Value = stream.ReadStringAtOffset(val.Offset); return val; } - public static CountPointer ReadCountPointerX(this Stream IO) => - new CountPointer { Count = (int)IO.ReadIntX(), Offset = (int)IO.ReadIntX() }; + public static CountPointer ReadCountPointerX(this Stream stream) => + new CountPointer { Count = (int)stream.ReadIntX(), Offset = (int)stream.ReadIntX() }; } } diff --git a/KKdMainLib/IO/Stream.cs b/KKdMainLib/IO/Stream.cs index e560c07..86f46f9 100644 --- a/KKdMainLib/IO/Stream.cs +++ b/KKdMainLib/IO/Stream.cs @@ -7,8 +7,9 @@ namespace KKdMainLib.IO public unsafe class Stream : IDisposable { private MSIO.Stream stream; - private int I, i, i0, BitRead, BitWrite, TempBitRead, TempBitWrite, ValRead, ValWrite; + private int I, i, BitRead, BitWrite, TempBitRead, TempBitWrite, ValRead, ValWrite; private byte[] buf; + private byte* ptr; private Format _format = Format.NULL; @@ -55,6 +56,7 @@ namespace KKdMainLib.IO stream = output; Format = Format.NULL; buf = new byte[128]; + ptr = buf.GetPtr(); IsBE = isBE; } @@ -71,10 +73,7 @@ namespace KKdMainLib.IO { if (offset == null) return null; return stream.Seek((long)offset, (MSIO.SeekOrigin)(int)origin); } public void Dispose() - { CheckWrited(); Dispose(true); } - - private void Dispose(bool disposing) - { if (disposing && stream != MSIO.Stream.Null) stream.Flush(); stream.Dispose(); } + { CW(); if (stream != MSIO.Stream.Null) { stream.Flush(); stream.Dispose(); } } public MSIO.Stream BaseStream { get { stream.Flush(); return stream; } set { stream = value; } } @@ -105,43 +104,37 @@ namespace KKdMainLib.IO public bool ReadBoolean() => stream.ReadByte() == 1; public sbyte ReadSByte() => ( sbyte)stream.ReadByte(); public byte ReadByte() => ( byte)stream.ReadByte(); - public sbyte ReadInt8() => ( sbyte) IntFromArray(1); - public byte ReadUInt8() => ( byte)UIntFromArray(1); - public short ReadInt16() => ( short) IntFromArray(2); - public ushort ReadUInt16() => (ushort)UIntFromArray(2); - public int ReadInt24() => ( int) IntFromArray(3); - public int ReadInt32() => ( int) IntFromArray(4); - public uint ReadUInt32() => ( uint)UIntFromArray(4); - public long ReadInt64() => IntFromArray(8); - public ulong ReadUInt64() => UIntFromArray(8); + public sbyte ReadInt8() => ( sbyte)stream.ReadByte(); + public byte ReadUInt8() => ( byte)stream.ReadByte(); + public short ReadInt16() { stream.Read(buf, 0, 2); return *( short*)ptr; } + public ushort ReadUInt16() { stream.Read(buf, 0, 2); return *(ushort*)ptr; } + public int ReadInt32() { stream.Read(buf, 0, 4); return *( int*)ptr; } + public uint ReadUInt32() { stream.Read(buf, 0, 4); return *( uint*)ptr; } + public long ReadInt64() { stream.Read(buf, 0, 8); return *( long*)ptr; } + public ulong ReadUInt64() { stream.Read(buf, 0, 8); return *( ulong*)ptr; } public Half ReadHalf() { ushort a = ReadUInt16(); return ( Half ) a; } public float ReadSingle() { uint a = ReadUInt32(); return *( float*)&a; } public double ReadDouble() { ulong a = ReadUInt64(); return *(double*)&a; } - public short ReadInt16Endian() => ( short) IntFromArray(2, IsBE); - public ushort ReadUInt16Endian() => (ushort)UIntFromArray(2, IsBE); - public int ReadInt24Endian() => ( int) IntFromArray(3, IsBE); - public int ReadInt32Endian() => ( int) IntFromArray(4, IsBE); - public uint ReadUInt32Endian() => ( uint)UIntFromArray(4, IsBE); - public long ReadInt64Endian() => IntFromArray(8, IsBE); - public ulong ReadUInt64Endian() => UIntFromArray(8, IsBE); + public short ReadInt16Endian() { stream.Read(buf, 0, 2); buf.Endian(2, IsBE); return *( short*)ptr; } + public ushort ReadUInt16Endian() { stream.Read(buf, 0, 2); buf.Endian(2, IsBE); return *(ushort*)ptr; } + public int ReadInt32Endian() { stream.Read(buf, 0, 4); buf.Endian(4, IsBE); return *( int*)ptr; } + public uint ReadUInt32Endian() { stream.Read(buf, 0, 4); buf.Endian(4, IsBE); return *( uint*)ptr; } + public long ReadInt64Endian() { stream.Read(buf, 0, 8); buf.Endian(8, IsBE); return *( long*)ptr; } + public ulong ReadUInt64Endian() { stream.Read(buf, 0, 8); buf.Endian(8, IsBE); return *( ulong*)ptr; } public Half ReadHalfEndian() { ushort a = ReadUInt16Endian(); return ( Half ) a; } public float ReadSingleEndian() { uint a = ReadUInt32Endian(); return *( float*)&a; } public double ReadDoubleEndian() { ulong a = ReadUInt64Endian(); return *(double*)&a; } - public short ReadInt16Endian(bool IsBE) => ( short) IntFromArray(2, IsBE); - public ushort ReadUInt16Endian(bool IsBE) => (ushort)UIntFromArray(2, IsBE); - public int ReadInt24Endian(bool IsBE) => ( int) IntFromArray(3, IsBE); - public int ReadInt32Endian(bool IsBE) => ( int) IntFromArray(4, IsBE); - public uint ReadUInt32Endian(bool IsBE) => ( uint)UIntFromArray(4, IsBE); - public long ReadInt64Endian(bool IsBE) => IntFromArray(8, IsBE); - public ulong ReadUInt64Endian(bool IsBE) => UIntFromArray(8, IsBE); - public Half ReadHalfEndian(bool IsBE) - { ushort a = ReadUInt16Endian(IsBE); return ( Half ) a; } - public float ReadSingleEndian(bool IsBE) - { uint a = ReadUInt32Endian(IsBE); return *( float*)&a; } - public double ReadDoubleEndian(bool IsBE) - { ulong a = ReadUInt64Endian(IsBE); return *(double*)&a; } + public short ReadInt16Endian(bool IsBE) { stream.Read(buf, 0, 2); buf.Endian(2, IsBE); return *( short*)ptr; } + public ushort ReadUInt16Endian(bool IsBE) { stream.Read(buf, 0, 2); buf.Endian(2, IsBE); return *(ushort*)ptr; } + public int ReadInt32Endian(bool IsBE) { stream.Read(buf, 0, 4); buf.Endian(4, IsBE); return *( int*)ptr; } + public uint ReadUInt32Endian(bool IsBE) { stream.Read(buf, 0, 4); buf.Endian(4, IsBE); return *( uint*)ptr; } + public long ReadInt64Endian(bool IsBE) { stream.Read(buf, 0, 8); buf.Endian(8, IsBE); return *( long*)ptr; } + public ulong ReadUInt64Endian(bool IsBE) { stream.Read(buf, 0, 8); buf.Endian(8, IsBE); return *( ulong*)ptr; } + public Half ReadHalfEndian(bool IsBE) { ushort a = ReadUInt16Endian(IsBE); return ( Half ) a; } + public float ReadSingleEndian(bool IsBE) { uint a = ReadUInt32Endian(IsBE); return *( float*)&a; } + public double ReadDoubleEndian(bool IsBE) { ulong a = ReadUInt64Endian(IsBE); return *(double*)&a; } public void Write(byte[] Val) => stream.Write(Val, 0, Val. Length); public void Write(byte[] Val, int Length) => stream.Write(Val, 0 , Length); @@ -151,18 +144,18 @@ namespace KKdMainLib.IO public void WriteByte(byte val) => stream.WriteByte(val); - public void Write( bool val) => stream.WriteByte((byte)(val ? 1 : 0)); - public void Write( sbyte val) => stream.WriteByte((byte) val); - public void Write( byte val) => stream.WriteByte( val); - public void Write( short val) => ToArray(2, val); - public void Write(ushort val) => ToArray(2, val); - public void Write( int val) => ToArray(4, val); - public void Write( uint val) => ToArray(4, val); - public void Write( long val) => ToArray(8, val); - public void Write( ulong val) => ToArray(8, val); - public void Write( Half val) => ToArray(2, (ushort) val); - public void Write( float val) => ToArray(4, *( uint*)&val); - public void Write(double val) => ToArray(8, *(ulong*)&val); + public void Write( bool val) { CW(); stream.WriteByte((byte)(val ? 1 : 0)); } + public void Write( sbyte val) { CW(); stream.WriteByte((byte) val); } + public void Write( byte val) { CW(); stream.WriteByte( val); } + public void Write( short val) { CW(); *( short*)ptr = val; stream.Write(buf, 0, 2); } + public void Write(ushort val) { CW(); *(ushort*)ptr = val; stream.Write(buf, 0, 2); } + public void Write( int val) { CW(); *( int*)ptr = val; stream.Write(buf, 0, 4); } + public void Write( uint val) { CW(); *( uint*)ptr = val; stream.Write(buf, 0, 4); } + public void Write( long val) { CW(); *( long*)ptr = val; stream.Write(buf, 0, 8); } + public void Write( ulong val) { CW(); *( ulong*)ptr = val; stream.Write(buf, 0, 8); } + public void Write( Half val) => Write( (ushort ) val); + public void Write( float val) => Write(*( uint*)&val); + public void Write(double val) => Write(*( ulong*)&val); public void Write( sbyte? val) => Write(val.GetValueOrDefault()); public void Write( byte? val) => Write(val.GetValueOrDefault()); @@ -174,125 +167,46 @@ namespace KKdMainLib.IO public void Write( ulong? val) => Write(val.GetValueOrDefault()); public void Write( float? val) => Write(val.GetValueOrDefault()); public void Write(double? val) => Write(val.GetValueOrDefault()); - - public void Write( bool* val, int Length) - { for (i = 0; i < Length; i++) stream.WriteByte((byte)(val[i] ? 1 : 0)); } - public void Write( sbyte* val, int Length) - { for (i = 0; i < Length; i++) stream.WriteByte((byte) val[i]); } - public void Write( byte* val, int Length) - { for (i = 0; i < Length; i++) stream.WriteByte( val[i]); } - public void Write( short* val, int Length) - { for (i = 0; i < Length; i++) ToArray(2, val[i]); } - public void Write(ushort* val, int Length) - { for (i = 0; i < Length; i++) ToArray(2, val[i]); } - public void Write( int* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, val[i]); } - public void Write( uint* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, val[i]); } - public void Write( long* val, int Length) - { for (i = 0; i < Length; i++) ToArray(8, val[i]); } - public void Write( ulong* val, int Length) - { for (i = 0; i < Length; i++) ToArray(8, val[i]); } - public void Write( float* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, *( uint*)&val[i]); } - public void Write(double* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, *(ulong*)&val[i]); } - - public void WriteEndian( short* val, int Length) - { for (i = 0; i < Length; i++) ToArray(2, Endian(val[i], 2, IsBE)); } - public void WriteEndian(ushort* val, int Length) - { for (i = 0; i < Length; i++) ToArray(2, Endian(val[i], 2, IsBE)); } - public void WriteEndian( int* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, Endian(val[i], 4, IsBE)); } - public void WriteEndian( uint* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, Endian(val[i], 4, IsBE)); } - public void WriteEndian( long* val, int Length) - { for (i = 0; i < Length; i++) ToArray(8, Endian(val[i], 8, IsBE)); } - public void WriteEndian( ulong* val, int Length) - { for (i = 0; i < Length; i++) ToArray(8, Endian(val[i], 8, IsBE)); } - public void WriteEndian( float* val, int Length) - { for (i = 0; i < Length; i++) ToArray(4, Endian(*( uint*)&val[i], 4, IsBE)); } - public void WriteEndian(double* val, int Length) - { for (i = 0; i < Length; i++) ToArray(8, Endian(*(ulong*)&val[i], 8, IsBE)); } - - public void WriteEndian( short* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(2, Endian(val[i], 2, IsBE)); } - public void WriteEndian(ushort* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(2, Endian(val[i], 2, IsBE)); } - public void WriteEndian( int* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(4, Endian(val[i], 4, IsBE)); } - public void WriteEndian( uint* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(4, Endian(val[i], 4, IsBE)); } - public void WriteEndian( long* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(8, Endian(val[i], 8, IsBE)); } - public void WriteEndian( ulong* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(8, Endian(val[i], 8, IsBE)); } - public void WriteEndian( float* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(4, Endian(*( uint*)&val[i], 4, IsBE)); } - public void WriteEndian(double* val, int Length, bool IsBE) - { for (i = 0; i < Length; i++) ToArray(8, Endian(*(ulong*)&val[i], 8, IsBE)); } public void Write( char val, bool UTF8 = true) { if (UTF8) Write(val.ToString().ToUTF8()); else Write(val.ToString().ToASCII()); } public void Write(string val, bool UTF8 = true) { if (UTF8) Write(val .ToUTF8()); else Write(val .ToASCII()); } - public void WriteEndian( short val) => ToArray(2, Endian(val, 2, IsBE)); - public void WriteEndian(ushort val) => ToArray(2, Endian(val, 2, IsBE)); - public void WriteEndian( int val) => ToArray(4, Endian(val, 4, IsBE)); - public void WriteEndian( uint val) => ToArray(4, Endian(val, 4, IsBE)); - public void WriteEndian( long val) => ToArray(8, Endian(val, 8, IsBE)); - public void WriteEndian( ulong val) => ToArray(8, Endian(val, 8, IsBE)); - public void WriteEndian( float val) => ToArray(4, Endian(*( uint*)&val, 4, IsBE)); - public void WriteEndian(double val) => ToArray(8, Endian(*(ulong*)&val, 8, IsBE)); + public void WriteEndian( short val) { CW(); *( short*)ptr = val; buf.Endian(2, IsBE); stream.Write(buf, 0, 2); } + public void WriteEndian(ushort val) { CW(); *(ushort*)ptr = val; buf.Endian(2, IsBE); stream.Write(buf, 0, 2); } + public void WriteEndian( int val) { CW(); *( int*)ptr = val; buf.Endian(4, IsBE); stream.Write(buf, 0, 4); } + public void WriteEndian( uint val) { CW(); *( uint*)ptr = val; buf.Endian(4, IsBE); stream.Write(buf, 0, 4); } + public void WriteEndian( long val) { CW(); *( long*)ptr = val; buf.Endian(8, IsBE); stream.Write(buf, 0, 8); } + public void WriteEndian( ulong val) { CW(); *( ulong*)ptr = val; buf.Endian(8, IsBE); stream.Write(buf, 0, 8); } + public void WriteEndian( Half val) => WriteEndian( (ushort ) val); + public void WriteEndian( float val) => WriteEndian(*( uint*)&val); + public void WriteEndian(double val) => WriteEndian(*( ulong*)&val); - public void WriteEndian( short val, bool IsBE) => ToArray(2, Endian(val, 2, IsBE)); - public void WriteEndian(ushort val, bool IsBE) => ToArray(2, Endian(val, 2, IsBE)); - public void WriteEndian( int val, bool IsBE) => ToArray(4, Endian(val, 4, IsBE)); - public void WriteEndian( uint val, bool IsBE) => ToArray(4, Endian(val, 4, IsBE)); - public void WriteEndian( long val, bool IsBE) => ToArray(8, Endian(val, 8, IsBE)); - public void WriteEndian( ulong val, bool IsBE) => ToArray(8, Endian(val, 8, IsBE)); - public void WriteEndian( float val, bool IsBE) => ToArray(4, Endian(*( uint*)&val, 4, IsBE)); - public void WriteEndian(double val, bool IsBE) => ToArray(8, Endian(*(ulong*)&val, 8, IsBE)); - - public long Endian( long BE, byte Length, bool IsBE) - { if (IsBE) { for (I = 0; I < Length; I++) { buf[I] = (byte)BE; BE >>= 8; } BE = 0; - for (I = 0; I < Length; I++) { BE |= buf[I]; if (I < Length - 1) BE <<= 8; } } return BE; } - - public ulong Endian( ulong BE, byte Length, bool IsBE) - { if (IsBE) { for (I = 0; I < Length; I++) { buf[I] = (byte)BE; BE >>= 8; } BE = 0; - for (I = 0; I < Length; I++) { BE |= buf[I]; if (I < Length - 1) BE <<= 8; } } return BE; } - - private void ToArray(byte L, long val) - { CheckWrited(); for (I = 0; I < L; I++) { buf[I] = (byte)val; val >>= 8; } stream.Write(buf, 0, L); } - - private void ToArray(byte L, ulong val) - { CheckWrited(); for (I = 0; I < L; I++) { buf[I] = (byte)val; val >>= 8; } stream.Write(buf, 0, L); } - - private long IntFromArray(byte L) { stream.Read(buf, 0, L); long val = 0; - for (I = L; I > 0; I--) { val <<= 8; val |= buf[I - 1]; } return val; } - - private ulong UIntFromArray(byte L) { stream.Read(buf, 0, L); ulong val = 0; - for (I = L; I > 0; I--) { val <<= 8; val |= buf[I - 1]; } return val; } - - private long IntFromArray(byte L, bool IsBE) { stream.Read(buf, 0, L); long val = 0; if (IsBE) - for (I = 0; I < L; I++) { val <<= 8; val |= buf[I ]; } else - for (I = L; I > 0; I--) { val <<= 8; val |= buf[I - 1]; } return val; } - - private ulong UIntFromArray(byte L, bool IsBE) { stream.Read(buf, 0, L); ulong val = 0; if (IsBE) - for (I = 0; I < L; I++) { val <<= 8; val |= buf[I ]; } else - for (I = L; I > 0; I--) { val <<= 8; val |= buf[I - 1]; } return val; } + public void WriteEndian( short val, bool IsBE) + { CW(); *( short*)ptr = val; buf.Endian(2, IsBE); stream.Write(buf, 0, 2); } + public void WriteEndian(ushort val, bool IsBE) + { CW(); *(ushort*)ptr = val; buf.Endian(2, IsBE); stream.Write(buf, 0, 2); } + public void WriteEndian( int val, bool IsBE) + { CW(); *( int*)ptr = val; buf.Endian(4, IsBE); stream.Write(buf, 0, 4); } + public void WriteEndian( uint val, bool IsBE) + { CW(); *( uint*)ptr = val; buf.Endian(4, IsBE); stream.Write(buf, 0, 4); } + public void WriteEndian( long val, bool IsBE) + { CW(); *( long*)ptr = val; buf.Endian(8, IsBE); stream.Write(buf, 0, 8); } + public void WriteEndian( ulong val, bool IsBE) + { CW(); *( ulong*)ptr = val; buf.Endian(8, IsBE); stream.Write(buf, 0, 8); } + public void WriteEndian( Half val, bool IsBE) => WriteEndian( (ushort ) val, IsBE); + public void WriteEndian( float val, bool IsBE) => WriteEndian(*( uint*)&val, IsBE); + public void WriteEndian(double val, bool IsBE) => WriteEndian(*( ulong*)&val, IsBE); - public char ReadChar(bool UTF8 = true) - { if (UTF8) return ReadCharUTF8(); - else return (char)stream.ReadByte(); } + public char ReadChar(bool UTF8 = true) => UTF8 ? ReadCharUTF8() : (char)stream.ReadByte(); public char ReadCharUTF8() { byte t; int T; int val = 0; - for (I = 0, i0 = 4; I < i0; I++) + for (I = 0, i = 4; I < i; I++) { T = stream.ReadByte(); if (T == -1) return '\uFFFF'; @@ -300,25 +214,23 @@ namespace KKdMainLib.IO if ((t & 0xC0) == 0x80 && I > 0) val = (val << 6) | (t & 0x3F); else if ((t & 0x80) == 0x00 && I == 0) return (char)t; - else if ((t & 0xE0) == 0xC0 && I == 0) { val = t & 0x1F; i0 = 2; } - else if ((t & 0xF0) == 0xE0 && I == 0) { val = t & 0x0F; i0 = 3; } - else if ((t & 0xF8) == 0xF0 && I == 0) { val = t & 0x07; i0 = 4; } + else if ((t & 0xE0) == 0xC0 && I == 0) { val = t & 0x1F; i = 2; } + else if ((t & 0xF0) == 0xE0 && I == 0) { val = t & 0x0F; i = 3; } + else if ((t & 0xF8) == 0xF0 && I == 0) { val = t & 0x07; i = 4; } else return '\uFFFF'; } return (char)val; } - - public string ReadString(long Length, bool UTF8 = true) - { if (UTF8) return ReadStringUTF8 (Length); - else return ReadStringASCII(Length); } + + public string ReadString(long Length, bool UTF8 = true) => + UTF8 ? ReadStringUTF8(Length) : ReadStringASCII(Length); public string ReadStringUTF8 (long Length) => ReadBytes(Length).ToUTF8 (); public string ReadStringASCII(long Length) => ReadBytes(Length).ToASCII(); - public string ReadString(long? Length, bool UTF8 = true) - { if (UTF8) return ReadStringUTF8 (Length); - else return ReadStringASCII(Length); } + public string ReadString(long? Length, bool UTF8 = true) => + UTF8 ? ReadStringUTF8(Length) : ReadStringASCII(Length); public string ReadStringUTF8 (long? Length) => ReadBytes(Length).ToUTF8 (); public string ReadStringASCII(long? Length) => ReadBytes(Length).ToASCII(); @@ -335,7 +247,7 @@ namespace KKdMainLib.IO public void ReadBytes(long Length, byte Bits, byte[] Buf, long Offset = -1) { if (Offset > -1) stream.Seek(Offset, 0); - if (Bits > 0 && Bits < 8) for (i0 = 0; i0 < Length; i0++) Buf[i0] = ReadBits(Bits); } + if (Bits > 0 && Bits < 8) for (i = 0; i < Length; i++) Buf[i] = ReadBits(Bits); } public byte ReadBits(byte Bits) { @@ -368,8 +280,10 @@ namespace KKdMainLib.IO ValWrite &= 0xFF; } - public void CheckRead () { if (BitRead > 0) ValRead = 0; BitRead = 8; } - public void CheckWrited() { if (BitWrite > 0) { WriteByte((byte)ValWrite); ValWrite = 0; BitWrite = 0; } } + public void CR() //CheckRead + { if (BitRead > 0) ValRead = 0; BitRead = 8; } + public void CW() //CheckWrited + { if (BitWrite > 0) { WriteByte((byte)ValWrite); ValWrite = 0; BitWrite = 0; } } public byte[] ToArray(bool Close) { byte[] Data = ToArray(); if (Close) this.Close(); return Data; } diff --git a/KKdMainLib/Main.cs b/KKdMainLib/Main.cs index 4860e3e..a5f66d1 100644 --- a/KKdMainLib/Main.cs +++ b/KKdMainLib/Main.cs @@ -1,10 +1,8 @@ using System; using System.Linq; -using System.Windows.Forms; using System.Globalization; using System.Collections.Generic; using KKdBaseLib; -using KKdMainLib.IO; namespace KKdMainLib { @@ -41,89 +39,6 @@ namespace KKdMainLib else Console.Write (TimeFormatHHmmssfff + " - " + Text, time.Hours, time.Minutes, time.Seconds, time.Milliseconds); } - - private static string GetArgs(string name, bool And, params string[] ext) - { - string Out = ""; - if (And) Out = "|"; - Out += name + " files ("; - for (int i = 0; i < ext.Length; i++) - { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ", "; } - Out += ")|"; - for (int i = 0; i < ext.Length; i++) - { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ";"; } - - return Out; - } - - private static string GetArgs(string name, params string[] ext) - { - string Out = name + " files ("; - for (int i = 0; i < ext.Length; i++) - { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ", "; } - Out += ")|"; - for (int i = 0; i < ext.Length; i++) - { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ";"; } - - return Out; - } - - public static string Choose(int code, string filetype, out string[] FileNames) - { - string MsgPack = GetArgs("MessagePack", true, "mp"); - string JSON = GetArgs("JSON", true, "json"); - string BIN = GetArgs("BIN", true, "bin"); - string WAV = GetArgs("WAV", true, "wav"); - - FileNames = new string[0]; - if (code == 1) - { - OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath, - Multiselect = true, Title = "Choose file(s) to open:" }; - - ofd.Filter = GetArgs("All;", false, "*"); - if (filetype == "a3da") ofd.Filter = GetArgs("A3DA", "a3da", "farc", "json", "mp") + - GetArgs("A3DA", true, "a3da") + GetArgs("FARC", true, "farc") + JSON + MsgPack; - else if (filetype == "bin" ) ofd.Filter = GetArgs("BIN" , "bin", "json", "mp") + - BIN + JSON + MsgPack; - else if (filetype == "blt" ) ofd.Filter = GetArgs("BLT" , "blt"); - else if (filetype == "bon" ) ofd.Filter = GetArgs("BON" , "bon", "bin", "json", "mp") + - GetArgs("BON", true, "bon") + BIN + JSON + MsgPack; - else if (filetype == "cct" ) ofd.Filter = GetArgs("CCT" , "cct"); - else if (filetype == "databank") ofd.Filter = GetArgs("DAT", "dat", "json", "mp") + - GetArgs("DAT", true, "dat") + JSON + MsgPack; - else if (filetype == "dex" ) ofd.Filter = GetArgs("DEX" , "dex", "bin", "json", "mp") + - GetArgs("DEX", true, "dex") + BIN + JSON + MsgPack; - else if (filetype == "dft") ofd.Filter = GetArgs("DFT" , "dft"); - else if (filetype == "diva") ofd.Filter = GetArgs("DIVA", "diva", "wav") + - GetArgs("DIVA", true, "diva") + GetArgs("WAV", true, "wav"); - else if (filetype == "dsc" ) ofd.Filter = GetArgs("DSC" , "dsc", "json", "mp") + - GetArgs("DSC", true, "dsc") + JSON + MsgPack; - else if (filetype == "farc") ofd.Filter = "FARC Archives (*.farc)|*.farc"; - else if (filetype == "json") ofd.Filter = "JSON (*.json)|*.json"; - else if (filetype == "mp" ) ofd.Filter = GetArgs("MessagePack", "mp"); - else if (filetype == "lit") ofd.Filter = GetArgs("LIT" , "lit"); - else if (filetype == "str" ) ofd.Filter = GetArgs("STR" , "str", "bin", "json", "mp") + - GetArgs("STR", true, "str") + BIN + JSON + MsgPack; - else if (filetype == "vag" ) ofd.Filter = GetArgs("VAG" , "vag", "wav") + - GetArgs("VAG", true, "vag") + GetArgs("WAV", true, "wav"); - - if (ofd.ShowDialog() == DialogResult.OK) FileNames = ofd.FileNames; - ofd.Dispose(); - } - else if (code == 2) - { - OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath, - ValidateNames = false, CheckFileExists = false, Filter = " | ", CheckPathExists = true, - Title = "Choose any file in folder:", FileName = "Folder Selection." }; - string Return = ""; - if (ofd.ShowDialog() == DialogResult.OK) - Return = Path.GetDirectoryName(ofd.FileName); - ofd.Dispose(); - return Return; - } - return ""; - } public static string NullTerminated(this string Source, ref int i, byte End) { diff --git a/KKdMainLib/Mot.cs b/KKdMainLib/Mot.cs index 6e72cd8..7e01b52 100644 --- a/KKdMainLib/Mot.cs +++ b/KKdMainLib/Mot.cs @@ -256,35 +256,35 @@ namespace KKdMainLib if (KeySet.Type == KeySetType.None) continue; KeySets[i0] = new MsgPack(2); - KeySets[i0].Array[0] = (MsgPack)(byte)KeySet.Type; + KeySets[i0].Array[0] = (byte)KeySet.Type; KeySets[i0].Array[1] = new MsgPack(KeySet.Keys.Length); if (KeySet.Type == KeySetType.Static) { KeySets[i0].Array[1][0] = new MsgPack(2); - KeySets[i0].Array[1][0].Array[0] = (MsgPack)KeySet.Keys[0].F; - KeySets[i0].Array[1][0].Array[1] = (MsgPack)KeySet.Keys[0].V; + KeySets[i0].Array[1][0].Array[0] = KeySet.Keys[0].F; + KeySets[i0].Array[1][0].Array[1] = KeySet.Keys[0].V; } else if (KeySet.Type == KeySetType.Linear) for (i1 = 0; i1 < KeySet.Keys.Length; i1++) { KeySets[i0].Array[1][i1] = new MsgPack(2); - KeySets[i0].Array[1][i1].Array[0] = (MsgPack)KeySet.Keys[i1].F; - KeySets[i0].Array[1][i1].Array[1] = (MsgPack)KeySet.Keys[i1].V; + KeySets[i0].Array[1][i1].Array[0] = KeySet.Keys[i1].F; + KeySets[i0].Array[1][i1].Array[1] = KeySet.Keys[i1].V; } else for (i1 = 0; i1 < KeySet.Keys.Length; i1++) { KeySets[i0].Array[1][i1] = new MsgPack(3); - KeySets[i0].Array[1][i1].Array[0] = (MsgPack)KeySet.Keys[i1].F; - KeySets[i0].Array[1][i1].Array[1] = (MsgPack)KeySet.Keys[i1].V; - KeySets[i0].Array[1][i1].Array[2] = (MsgPack)KeySet.Keys[i1].T; + KeySets[i0].Array[1][i1].Array[0] = KeySet.Keys[i1].F; + KeySets[i0].Array[1][i1].Array[1] = KeySet.Keys[i1].V; + KeySets[i0].Array[1][i1].Array[2] = KeySet.Keys[i1].T; } } MOT.Add(KeySets); MsgPack BoneInfo = new MsgPack(Mot.BoneInfo.Value.Length, "BoneInfo"); for (i0 = 0; i0 < Mot.BoneInfo.Value.Length; i0++) - BoneInfo[i0] = (MsgPack)Mot.BoneInfo.Value[i0].Id; + BoneInfo[i0] = Mot.BoneInfo.Value[i0].Id; MOT.Add(BoneInfo); return MOT; diff --git a/KKdMainLib/Properties/AssemblyInfo.cs b/KKdMainLib/Properties/AssemblyInfo.cs index 8b73bc5..497ebfb 100644 --- a/KKdMainLib/Properties/AssemblyInfo.cs +++ b/KKdMainLib/Properties/AssemblyInfo.cs @@ -11,5 +11,5 @@ using System.Runtime.InteropServices; [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("2BA7EFC6-91D1-8BBC-C487-06C7F36CC789")] -[assembly: AssemblyVersion("0.4.7.2")] -[assembly: AssemblyFileVersion("0.4.7.2")] +[assembly: AssemblyVersion("0.4.7.3")] +[assembly: AssemblyFileVersion("0.4.7.3")] diff --git a/KKdSoundLib/DIVA.cs b/KKdSoundLib/DIVA.cs index 33ade02..fdf0035 100644 --- a/KKdSoundLib/DIVA.cs +++ b/KKdSoundLib/DIVA.cs @@ -95,7 +95,7 @@ namespace KKdSoundLib ref currentclampPtr[c], ref stepindexPtr[c]); writer.Write(value, 4); } - writer.CheckWrited(); + writer.CW(); writer.LongPosition = 0x00; writer.Write("DIVA"); diff --git a/KKdSoundLib/VAG.cs b/KKdSoundLib/VAG.cs index 2ce3e03..81248f3 100644 --- a/KKdSoundLib/VAG.cs +++ b/KKdSoundLib/VAG.cs @@ -117,8 +117,8 @@ namespace KKdSoundLib d1 = four_bitPtr[i2]; if (d0 > 7) d0 -= 16; if (d1 > 7) d1 -= 16; - d0 = d0 << (20 - ShF); - d1 = d1 << (20 - ShF); + d0 <<= (20 - ShF); + d1 <<= (20 - ShF); g = ((tS1 >> 8) * VAG_1 + (tS2 >> 8) * VAG_2) >> 5; tS2 = tS1; tS1 = g + d0; @@ -155,8 +155,8 @@ namespace KKdSoundLib d1 = four_bitPtr[i2]; if (d0 > 7) d0 -= 16; if (d1 > 7) d1 -= 16; - d0 = d0 << (20 - ShF); - d1 = d1 << (20 - ShF); + d0 <<= (20 - ShF); + d1 <<= (20 - ShF); g = ((tS1 >> 8) * HEVAG_1 + (tS2 >> 8) * HEVAG_2 + (tS3 >> 8) * HEVAG_3 + (tS4 >> 8) * HEVAG_4) >> 5; @@ -612,7 +612,7 @@ namespace KKdSoundLib ShF = 0; ShM = 0x4000; - min = min >> 8; + min >>= 8; while (ShF < 12) { @@ -635,7 +635,7 @@ namespace KKdSoundLib if (d0 > 7) d0 = 7; if (d0 < -8) d0 = -8; four_bitPtr[i] = d0 & 0xF; - d0 = d0 << (20 - ShF); + d0 <<= (20 - ShF); S2 = S1; S1 = d0 - e; } @@ -713,7 +713,7 @@ namespace KKdSoundLib if (d0 > 7) d0 = 7; if (d0 < -8) d0 = -8; four_bitPtr[i] = d0 & 0xF; - d0 = d0 << (20 - ShF); + d0 <<= (20 - ShF); tS4 = tS3; tS3 = tS2; tS2 = tS1; tS1 = d0 - e; i++; diff --git a/PD_Tool/Program.cs b/PD_Tool/Program.cs index 4bb44d6..14610c9 100644 --- a/PD_Tool/Program.cs +++ b/PD_Tool/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Windows.Forms; using KKdMainLib; using KKdMainLib.IO; using KKdMain = KKdMainLib.Main; @@ -84,7 +85,7 @@ namespace PD_Tool if (function == "1" || function == "2") FARC.Processor(function == "1"); else if (function == "3" || function == "4") { - KKdMain.Choose(1, "", out string[] FileNames); + Program.Choose(1, "", out string[] FileNames); foreach (string FileName in FileNames) DIVAFILE.Decrypt(FileName); } else if (function == "5") DataBase.Processor(JSON); @@ -152,7 +153,7 @@ namespace PD_Tool } else if (function == "8") { - KKdMain.Choose(1, JSON ? "mp" : "json", out string[] FileNames); + Program.Choose(1, JSON ? "mp" : "json", out string[] FileNames); foreach (string file in FileNames) if (JSON) { @@ -168,5 +169,84 @@ namespace PD_Tool } public static void Exit() => Environment.Exit(0); + + private static string GetArgs(string name, bool And, params string[] ext) + { + string Out = ""; + if (And) Out = "|"; + Out += name + " files ("; + for (int i = 0; i < ext.Length; i++) + { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ", "; } + Out += ")|"; + for (int i = 0; i < ext.Length; i++) + { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ";"; } + + return Out; + } + + private static string GetArgs(string name, params string[] ext) + { + string Out = name + " files ("; + for (int i = 0; i < ext.Length; i++) + { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ", "; } + Out += ")|"; + for (int i = 0; i < ext.Length; i++) + { Out += "*." + ext[i]; if (i + 1 < ext.Length) Out += ";"; } + + return Out; + } + + public static string Choose(int code, string filetype, out string[] FileNames) + { + string MsgPack = GetArgs("MessagePack", true, "mp"); + string JSON = GetArgs("JSON", true, "json"); + string BIN = GetArgs("BIN", true, "bin"); + string WAV = GetArgs("WAV", true, "wav"); + + FileNames = new string[0]; + if (code == 1) + { + OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath, + Multiselect = true, Title = "Choose file(s) to open:" }; + + ofd.Filter = GetArgs("All;", false, "*"); + if (filetype == "a3da") ofd.Filter = GetArgs("A3DA", "a3da", "farc", "json", "mp") + + GetArgs("A3DA", true, "a3da") + GetArgs("FARC", true, "farc") + JSON + MsgPack; + else if (filetype == "bin" ) ofd.Filter = GetArgs("BIN" , "bin", "json", "mp") + + BIN + JSON + MsgPack; + else if (filetype == "blt" ) ofd.Filter = GetArgs("BLT" , "blt"); + else if (filetype == "cct" ) ofd.Filter = GetArgs("CCT" , "cct"); + else if (filetype == "databank") ofd.Filter = GetArgs("DAT", "dat", "json", "mp") + + GetArgs("DAT", true, "dat") + JSON + MsgPack; + else if (filetype == "dex" ) ofd.Filter = GetArgs("DEX" , "dex", "bin", "json", "mp") + + GetArgs("DEX", true, "dex") + BIN + JSON + MsgPack; + else if (filetype == "dft") ofd.Filter = GetArgs("DFT" , "dft"); + else if (filetype == "diva") ofd.Filter = GetArgs("DIVA", "diva", "wav") + + GetArgs("DIVA", true, "diva") + GetArgs("WAV", true, "wav"); + else if (filetype == "farc") ofd.Filter = "FARC Archives (*.farc)|*.farc"; + else if (filetype == "json") ofd.Filter = "JSON (*.json)|*.json"; + else if (filetype == "mp" ) ofd.Filter = GetArgs("MessagePack", "mp"); + else if (filetype == "lit") ofd.Filter = GetArgs("LIT" , "lit"); + else if (filetype == "str" ) ofd.Filter = GetArgs("STR" , "str", "bin", "json", "mp") + + GetArgs("STR", true, "str") + BIN + JSON + MsgPack; + else if (filetype == "vag" ) ofd.Filter = GetArgs("VAG" , "vag", "wav") + + GetArgs("VAG", true, "vag") + GetArgs("WAV", true, "wav"); + + if (ofd.ShowDialog() == DialogResult.OK) FileNames = ofd.FileNames; + ofd.Dispose(); + } + else if (code == 2) + { + OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath, + ValidateNames = false, CheckFileExists = false, Filter = " | ", CheckPathExists = true, + Title = "Choose any file in folder:", FileName = "Folder Selection." }; + string Return = ""; + if (ofd.ShowDialog() == DialogResult.OK) + Return = Path.GetDirectoryName(ofd.FileName); + ofd.Dispose(); + return Return; + } + return ""; + } } } \ No newline at end of file diff --git a/PD_Tool/classes/DataBase.cs b/PD_Tool/classes/DataBase.cs index 3a7448b..52c0b1b 100644 --- a/PD_Tool/classes/DataBase.cs +++ b/PD_Tool/classes/DataBase.cs @@ -32,7 +32,7 @@ namespace PD_Tool { Console.Title = "Auth DB Converter"; DB.Auth Auth; - Main.Choose(1, "bin", out string[] FileNames); + Program.Choose(1, "bin", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; @@ -62,7 +62,7 @@ namespace PD_Tool { Console.Title = "AET DB Converter"; DB.Aet Aet; - Main.Choose(1, "bin", out string[] FileNames); + Program.Choose(1, "bin", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; @@ -92,7 +92,7 @@ namespace PD_Tool { Console.Title = "SPR DB Converter"; DB.Spr Spr; - Main.Choose(1, "bin", out string[] FileNames); + Program.Choose(1, "bin", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/FARC.cs b/PD_Tool/classes/FARC.cs index b8587f6..b2324b9 100644 --- a/PD_Tool/classes/FARC.cs +++ b/PD_Tool/classes/FARC.cs @@ -14,14 +14,14 @@ namespace PD_Tool if (Extract) { Console.Title = "FARC Extractor"; - Main.Choose(1, "farc", out string[] FileNames); + Program.Choose(1, "farc", out string[] FileNames); foreach (string FileName in FileNames) if (FileName != "" && File.Exists(FileName)) new KKdFARC(FileName).UnPack(); } else { - string file = Main.Choose(2, "", out string[] FileNames); + string file = Program.Choose(2, "", out string[] FileNames); Console.Clear(); Console.Title = "FARC Creator"; if (file != "") diff --git a/PD_Tool/classes/Tools/A3D.cs b/PD_Tool/classes/Tools/A3D.cs index 4e8f4fe..1a0398e 100644 --- a/PD_Tool/classes/Tools/A3D.cs +++ b/PD_Tool/classes/Tools/A3D.cs @@ -12,7 +12,7 @@ namespace PD_Tool.Tools public static void Processor(bool JSON) { Console.Title = "A3DA Converter"; - Main.Choose(1, "a3da", out string[] FileNames); + Program.Choose(1, "a3da", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/AET.cs b/PD_Tool/classes/Tools/AET.cs index dd8d50e..f833166 100644 --- a/PD_Tool/classes/Tools/AET.cs +++ b/PD_Tool/classes/Tools/AET.cs @@ -11,7 +11,7 @@ namespace PD_Tool.Tools { Console.Title = "AET Converter"; KKdAet Aet; - Main.Choose(1, "bin", out string[] FileNames); + Program.Choose(1, "bin", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/BLT.cs b/PD_Tool/classes/Tools/BLT.cs index ce7cf7b..d425869 100644 --- a/PD_Tool/classes/Tools/BLT.cs +++ b/PD_Tool/classes/Tools/BLT.cs @@ -11,7 +11,7 @@ namespace PD_Tool.Tools { Console.Title = "Bloom Converter"; Bloom Bloom; - Main.Choose(1, "blt", out string[] FileNames); + Program.Choose(1, "blt", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/CCT.cs b/PD_Tool/classes/Tools/CCT.cs index 09529fb..f88091a 100644 --- a/PD_Tool/classes/Tools/CCT.cs +++ b/PD_Tool/classes/Tools/CCT.cs @@ -11,7 +11,7 @@ namespace PD_Tool.Tools { Console.Title = "Color Correction Converter"; ColorCorrection ColorCorrection; - Main.Choose(1, "cct", out string[] FileNames); + Program.Choose(1, "cct", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/DB.cs b/PD_Tool/classes/Tools/DB.cs index e92e2b4..40961dc 100644 --- a/PD_Tool/classes/Tools/DB.cs +++ b/PD_Tool/classes/Tools/DB.cs @@ -9,7 +9,7 @@ namespace PD_Tool.Tools public static void Processor(bool JSON) { Console.Title = "DataBank Converter"; - Main.Choose(1, "databank", out string[] FileNames); + Program.Choose(1, "databank", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/DEX.cs b/PD_Tool/classes/Tools/DEX.cs index 96863d0..7ea257d 100644 --- a/PD_Tool/classes/Tools/DEX.cs +++ b/PD_Tool/classes/Tools/DEX.cs @@ -12,7 +12,7 @@ namespace PD_Tool.Tools { Console.Title = "DEX Converter"; KKdDEX DEX; - Main.Choose(1, "dex", out string[] FileNames); + Program.Choose(1, "dex", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/DFT.cs b/PD_Tool/classes/Tools/DFT.cs index 4d44278..b7cafc2 100644 --- a/PD_Tool/classes/Tools/DFT.cs +++ b/PD_Tool/classes/Tools/DFT.cs @@ -11,7 +11,7 @@ namespace PD_Tool.Tools { Console.Title = "DOF Converter"; DOF DOF; - Main.Choose(1, "dft", out string[] FileNames); + Program.Choose(1, "dft", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/DIV.cs b/PD_Tool/classes/Tools/DIV.cs index 1fc2b58..e09d5ca 100644 --- a/PD_Tool/classes/Tools/DIV.cs +++ b/PD_Tool/classes/Tools/DIV.cs @@ -10,7 +10,7 @@ namespace PD_Tool.Tools public static void Processor() { Console.Title = "DIVA Converter"; - Main.Choose(1, "diva", out string[] FileNames); + Program.Choose(1, "diva", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/LIT.cs b/PD_Tool/classes/Tools/LIT.cs index f9dea83..7d059ba 100644 --- a/PD_Tool/classes/Tools/LIT.cs +++ b/PD_Tool/classes/Tools/LIT.cs @@ -11,7 +11,7 @@ namespace PD_Tool.Tools { Console.Title = "Light Converter"; Light LIT; - Main.Choose(1, "lit", out string[] FileNames); + Program.Choose(1, "lit", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/MOT.cs b/PD_Tool/classes/Tools/MOT.cs index 8383c4b..9110d69 100644 --- a/PD_Tool/classes/Tools/MOT.cs +++ b/PD_Tool/classes/Tools/MOT.cs @@ -10,7 +10,7 @@ namespace PD_Tool.Tools { Console.Title = "MOT Converter"; Mot Mot; - Main.Choose(1, "bin", out string[] FileNames); + Program.Choose(1, "bin", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = ""; diff --git a/PD_Tool/classes/Tools/STR.cs b/PD_Tool/classes/Tools/STR.cs index 898abce..ceddd5c 100644 --- a/PD_Tool/classes/Tools/STR.cs +++ b/PD_Tool/classes/Tools/STR.cs @@ -10,7 +10,7 @@ namespace PD_Tool.Tools public static void Processor(bool JSON) { Console.Title = "STR Converter"; - Main.Choose(1, "str", out string[] FileNames); + Program.Choose(1, "str", out string[] FileNames); KKdSTR Data; string filepath = ""; diff --git a/PD_Tool/classes/Tools/VAG.cs b/PD_Tool/classes/Tools/VAG.cs index b22bfec..0b452e2 100644 --- a/PD_Tool/classes/Tools/VAG.cs +++ b/PD_Tool/classes/Tools/VAG.cs @@ -10,7 +10,7 @@ namespace PD_Tool.Tools public static void Processor() { Console.Title = "VAG Converter"; - Main.Choose(1, "vag", out string[] FileNames); + Program.Choose(1, "vag", out string[] FileNames); if (FileNames.Length < 1) return; string filepath = ""; string ext = "";