-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStructureSegment.cs
140 lines (129 loc) · 4.75 KB
/
StructureSegment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//NCShark - By AlSch092 @ Github, thanks to @Diamondo25 for MapleShark
using System;
using System.Collections.Generic;
using System.Text;
namespace NCShark
{
public sealed class StructureSegment
{
private byte[] mBuffer;
private Encoding encoding = Encoding.UTF8;
public StructureSegment(byte[] pBuffer, int pStart, int pLength, byte locale)
{
mBuffer = new byte[pLength];
try
{
switch (locale)
{
case Game.NC_GB:
encoding = Encoding.UTF8;
break;
default:
encoding = Encoding.UTF8;
break;
}
}
catch
{
encoding = Encoding.UTF8;
}
Buffer.BlockCopy(pBuffer, pStart, mBuffer, 0, pLength);
}
public byte? Byte { get { if (mBuffer.Length >= 1) return mBuffer[0]; return null; } }
public sbyte? SByte { get { if (mBuffer.Length >= 1) return (sbyte)mBuffer[0]; return null; } }
public ushort? UShort { get { if (mBuffer.Length >= 2) return BitConverter.ToUInt16(mBuffer, 0); return null; } }
public short? Short { get { if (mBuffer.Length >= 2) return BitConverter.ToInt16(mBuffer, 0); return null; } }
public uint? UInt { get { if (mBuffer.Length >= 4) return BitConverter.ToUInt32(mBuffer, 0); return null; } }
public int? Int { get { if (mBuffer.Length >= 4) return BitConverter.ToInt32(mBuffer, 0); return null; } }
public ulong? ULong { get { if (mBuffer.Length >= 8) return BitConverter.ToUInt64(mBuffer, 0); return null; } }
public long? Long { get { if (mBuffer.Length >= 8) return BitConverter.ToInt64(mBuffer, 0); return null; } }
public long? FlippedLong
{
get
{
if (mBuffer.Length >= 8)
{
long time = BitConverter.ToInt64(mBuffer, 0);
time = (long)(
((time << 32) & 0xFFFFFFFF) |
(time & 0xFFFFFFFF)
);
return time;
}
return null;
}
}
public string IpAddress
{
get
{
if (mBuffer.Length == 4)
{
return string.Format("{0}.{1}.{2}.{3}", mBuffer[0].ToString(), mBuffer[1].ToString(), mBuffer[2].ToString(), mBuffer[3].ToString());
}
return null;
}
}
public DateTime? Date
{
get
{
try
{
if (mBuffer.Length >= 8)
return DateTime.FromFileTimeUtc(BitConverter.ToInt64(mBuffer, 0));
}
catch { }
return null;
}
}
public DateTime? FlippedDate
{
get
{
try
{
if (mBuffer.Length >= 8)
{
long time = BitConverter.ToInt64(mBuffer, 0);
time = (long)(
((time << 32) & 0xFFFFFFFF) |
(time & 0xFFFFFFFF)
);
return DateTime.FromFileTimeUtc(time);
}
}
catch { }
return null;
}
}
public string String
{
get
{
if (mBuffer.Length == 0) return null;
if (mBuffer[0] == 0x00) return "";
for (int index = 0; index < mBuffer.Length; ++index) if (mBuffer[index] == 0x00) return encoding.GetString(mBuffer, 0, index);
return encoding.GetString(mBuffer, 0, mBuffer.Length);
}
}
public string StringIgnore
{
get
{
byte[] sBuffer = new byte[mBuffer.Length];
Buffer.BlockCopy(mBuffer, 0, sBuffer, 0, mBuffer.Length);
if (sBuffer.Length == 0) return null;
for (int index = 0; index < sBuffer.Length; ++index) if (sBuffer[index] >= 0x00 && sBuffer[index] < 0x20) sBuffer[index] = 0x2E;
return encoding.GetString(sBuffer, 0, sBuffer.Length);
}
}
public string Length
{
get
{
return mBuffer.Length + (mBuffer.Length != 1 ? " bytes" : " byte");
}
}
}
}