forked from runuo/runuo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientVersion.cs
305 lines (257 loc) · 6.45 KB
/
ClientVersion.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/***************************************************************************
* ClientVersion.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : [email protected]
*
* $Id$
*
***************************************************************************/
/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
using System;
using System.Text;
using System.Collections;
namespace Server
{
public enum ClientType
{
Regular,
UOTD,
God,
SA
}
public class ClientVersion : IComparable, IComparer
{
private int m_Major, m_Minor, m_Revision, m_Patch;
private ClientType m_Type;
private string m_SourceString;
public int Major
{
get
{
return m_Major;
}
}
public int Minor
{
get
{
return m_Minor;
}
}
public int Revision
{
get
{
return m_Revision;
}
}
public int Patch
{
get
{
return m_Patch;
}
}
public ClientType Type
{
get
{
return m_Type;
}
}
public string SourceString
{
get
{
return m_SourceString;
}
}
public ClientVersion( int maj, int min, int rev, int pat ) : this( maj, min, rev, pat, ClientType.Regular )
{
}
public ClientVersion( int maj, int min, int rev, int pat, ClientType type )
{
m_Major = maj;
m_Minor = min;
m_Revision = rev;
m_Patch = pat;
m_Type = type;
m_SourceString = _ToStringImpl();
}
public static bool operator == ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) == 0 );
}
public static bool operator != ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) != 0 );
}
public static bool operator >= ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) >= 0 );
}
public static bool operator > ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) > 0 );
}
public static bool operator <= ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) <= 0 );
}
public static bool operator < ( ClientVersion l, ClientVersion r )
{
return ( Compare( l, r ) < 0 );
}
public override int GetHashCode()
{
return m_Major ^ m_Minor ^ m_Revision ^ m_Patch ^ (int)m_Type;
}
public override bool Equals( object obj )
{
if ( obj == null )
return false;
ClientVersion v = obj as ClientVersion;
if ( v == null )
return false;
return m_Major == v.m_Major
&& m_Minor == v.m_Minor
&& m_Revision == v.m_Revision
&& m_Patch == v.m_Patch
&& m_Type == v.m_Type;
}
private string _ToStringImpl()
{
StringBuilder builder = new StringBuilder(16);
builder.Append(m_Major);
builder.Append('.');
builder.Append(m_Minor);
builder.Append('.');
builder.Append(m_Revision);
if (m_Major <= 5 && m_Minor <= 0 && m_Revision <= 6) //Anything before 5.0.7
{
if (m_Patch > 0)
builder.Append((char)('a' + (m_Patch - 1)));
}
else
{
builder.Append('.');
builder.Append(m_Patch);
}
if (m_Type != ClientType.Regular)
{
builder.Append(' ');
builder.Append(m_Type.ToString());
}
return builder.ToString();
}
public override string ToString()
{
return _ToStringImpl();
}
public ClientVersion( string fmt )
{
m_SourceString = fmt;
try
{
fmt = fmt.ToLower();
int br1 = fmt.IndexOf( '.' );
int br2 = fmt.IndexOf( '.', br1 + 1 );
int br3 = br2 + 1;
while ( br3 < fmt.Length && Char.IsDigit( fmt, br3 ) )
br3++;
m_Major = Utility.ToInt32( fmt.Substring( 0, br1 ) );
m_Minor = Utility.ToInt32( fmt.Substring( br1 + 1, br2 - br1 - 1 ) );
m_Revision = Utility.ToInt32( fmt.Substring( br2 + 1, br3 - br2 - 1 ) );
if( br3 < fmt.Length )
{
if( m_Major <= 5 && m_Minor <= 0 && m_Revision <= 6 ) //Anything before 5.0.7
{
if( !Char.IsWhiteSpace( fmt, br3 ) )
m_Patch = (fmt[br3] - 'a') + 1;
}
else
{
m_Patch = Utility.ToInt32( fmt.Substring( br3+1, fmt.Length - br3 - 1 ) );
}
}
if ( fmt.IndexOf( "god" ) >= 0 || fmt.IndexOf( "gq" ) >= 0 )
m_Type = ClientType.God;
else if ( fmt.IndexOf( "third dawn" ) >= 0 || fmt.IndexOf( "uo:td" ) >= 0 || fmt.IndexOf( "uotd" ) >= 0 || fmt.IndexOf( "uo3d" ) >= 0 || fmt.IndexOf( "uo:3d" ) >= 0 )
m_Type = ClientType.UOTD;
else
m_Type = ClientType.Regular;
}
catch
{
m_Major = 0;
m_Minor = 0;
m_Revision = 0;
m_Patch = 0;
m_Type = ClientType.Regular;
}
}
public int CompareTo( object obj )
{
if ( obj == null )
return 1;
ClientVersion o = obj as ClientVersion;
if ( o == null )
throw new ArgumentException();
if ( m_Major > o.m_Major )
return 1;
else if ( m_Major < o.m_Major )
return -1;
else if ( m_Minor > o.m_Minor )
return 1;
else if ( m_Minor < o.m_Minor )
return -1;
else if ( m_Revision > o.m_Revision )
return 1;
else if ( m_Revision < o.m_Revision )
return -1;
else if ( m_Patch > o.m_Patch )
return 1;
else if ( m_Patch < o.m_Patch )
return -1;
else
return 0;
}
public static bool IsNull( object x )
{
return Object.ReferenceEquals( x, null );
}
public int Compare( object x, object y )
{
if ( IsNull( x ) && IsNull( y ) )
return 0;
else if ( IsNull( x ) )
return -1;
else if ( IsNull( y ) )
return 1;
ClientVersion a = x as ClientVersion;
ClientVersion b = y as ClientVersion;
if ( IsNull( a ) || IsNull( b ) )
throw new ArgumentException();
return a.CompareTo( b );
}
public static int Compare( ClientVersion a, ClientVersion b )
{
if ( IsNull( a ) && IsNull( b ) )
return 0;
else if ( IsNull( a ) )
return -1;
else if ( IsNull( b ) )
return 1;
return a.CompareTo( b );
}
}
}