mirror of
https://bitbucket.org/Ioncannon/project-meteor-server.git
synced 2025-06-09 14:04:41 +02:00
Ported over the network code from the map server. A lot of backedlogged changes are here as well.
This commit is contained in:
parent
af4a0d5546
commit
8c73f6e926
12 changed files with 443 additions and 184 deletions
|
@ -24,7 +24,7 @@ namespace FFXIVClassic_Lobby_Server.common
|
|||
public static string ByteArrayToHex(byte[] bytes)
|
||||
{
|
||||
var lookup32 = _lookup32;
|
||||
var result = new char[(bytes.Length * 3) + ((bytes.Length/16) < 1 ? 1 : (bytes.Length/16)*3) + bytes.Length+60];
|
||||
var result = new char[(bytes.Length * 3) + ((bytes.Length / 16) < 1 ? 1 : (bytes.Length / 16) * 3) + bytes.Length + 60];
|
||||
int numNewLines = 0;
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
|
@ -33,45 +33,151 @@ namespace FFXIVClassic_Lobby_Server.common
|
|||
result[(3 * i) + (17 * numNewLines) + 1] = (char)(val >> 16);
|
||||
result[(3 * i) + (17 * numNewLines) + 2] = ' ';
|
||||
|
||||
result[(numNewLines * (3*16+17)) + (3 * 16) + (i % 16)] = (char)bytes[i] >= 32 && (char)bytes[i] <= 126 ? (char)bytes[i] : '.';
|
||||
|
||||
if (i != bytes.Length - 1 && bytes.Length > 16 && i != 0 && (i+1) % 16 == 0)
|
||||
result[(numNewLines * (3 * 16 + 17)) + (3 * 16) + (i % 16)] = (char)bytes[i] >= 32 && (char)bytes[i] <= 126 ? (char)bytes[i] : '.';
|
||||
|
||||
if (i != bytes.Length - 1 && bytes.Length > 16 && i != 0 && (i + 1) % 16 == 0)
|
||||
{
|
||||
result[(numNewLines * (3*16+17)) + (3 * 16) + (16)] = '\n';
|
||||
result[(numNewLines * (3 * 16 + 17)) + (3 * 16) + (16)] = '\n';
|
||||
numNewLines++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return new string(result);
|
||||
}
|
||||
/*
|
||||
/// <summary>
|
||||
/// Grabs an instance of a class directly from the MySqlDataReader instead of manually building from GetString() etc.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of object to return</typeparam>
|
||||
/// <param name="reader">Reader instance</param>
|
||||
/// <returns>new T</returns>
|
||||
public static T GetSQLObject<T>(this MySql.Data.MySqlClient.MySqlDataReader reader)
|
||||
|
||||
public static UInt32 UnixTimeStampUTC()
|
||||
{
|
||||
var obj = Activator.CreateInstance(typeof(T));
|
||||
var fields = obj.GetType().GetFields();
|
||||
UInt32 unixTimeStamp;
|
||||
DateTime currentTime = DateTime.Now;
|
||||
DateTime zuluTime = currentTime.ToUniversalTime();
|
||||
DateTime unixEpoch = new DateTime(1970, 1, 1);
|
||||
unixTimeStamp = (UInt32)(zuluTime.Subtract(unixEpoch)).TotalSeconds;
|
||||
|
||||
foreach (var field in obj.GetType().GetFields())
|
||||
return unixTimeStamp;
|
||||
}
|
||||
|
||||
public static UInt64 MilisUnixTimeStampUTC()
|
||||
{
|
||||
UInt64 unixTimeStamp;
|
||||
DateTime currentTime = DateTime.Now;
|
||||
DateTime zuluTime = currentTime.ToUniversalTime();
|
||||
DateTime unixEpoch = new DateTime(1970, 1, 1);
|
||||
unixTimeStamp = (UInt64)(zuluTime.Subtract(unixEpoch)).TotalMilliseconds;
|
||||
|
||||
return unixTimeStamp;
|
||||
}
|
||||
|
||||
public static ulong swapEndian(ulong input)
|
||||
{
|
||||
return ((0x00000000000000FF) & (input >> 56) |
|
||||
(0x000000000000FF00) & (input >> 40) |
|
||||
(0x0000000000FF0000) & (input >> 24) |
|
||||
(0x00000000FF000000) & (input >> 8) |
|
||||
(0x000000FF00000000) & (input << 8) |
|
||||
(0x0000FF0000000000) & (input << 24) |
|
||||
(0x00FF000000000000) & (input << 40) |
|
||||
(0xFF00000000000000) & (input << 56));
|
||||
}
|
||||
|
||||
public static uint swapEndian(uint input)
|
||||
{
|
||||
return ((input >> 24) & 0xff) |
|
||||
((input << 8) & 0xff0000) |
|
||||
((input >> 8) & 0xff00) |
|
||||
((input << 24) & 0xff000000);
|
||||
}
|
||||
|
||||
public static int swapEndian(int input)
|
||||
{
|
||||
uint inputAsUint = (uint)input;
|
||||
|
||||
input = (int)
|
||||
(((inputAsUint >> 24) & 0xff) |
|
||||
((inputAsUint << 8) & 0xff0000) |
|
||||
((inputAsUint >> 8) & 0xff00) |
|
||||
((inputAsUint << 24) & 0xff000000));
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
public static uint MurmurHash2(string key, uint seed)
|
||||
{
|
||||
// 'm' and 'r' are mixing constants generated offline.
|
||||
// They're not really 'magic', they just happen to work well.
|
||||
|
||||
byte[] data = Encoding.ASCII.GetBytes(key);
|
||||
const uint m = 0x5bd1e995;
|
||||
const int r = 24;
|
||||
int len = key.Length;
|
||||
int dataIndex = len - 4;
|
||||
|
||||
// Initialize the hash to a 'random' value
|
||||
|
||||
uint h = seed ^ (uint)len;
|
||||
|
||||
// Mix 4 bytes at a time into the hash
|
||||
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
var attrs = field.GetCustomAttributes(typeof(System.Runtime.Serialization.DataMemberAttribute), true) as System.Runtime.Serialization.DataMemberAttribute[];
|
||||
h *= m;
|
||||
|
||||
if (attrs.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
uint k = (uint)BitConverter.ToInt32(data, dataIndex);
|
||||
k = ((k >> 24) & 0xff) | // move byte 3 to byte 0
|
||||
((k << 8) & 0xff0000) | // move byte 1 to byte 2
|
||||
((k >> 8) & 0xff00) | // move byte 2 to byte 1
|
||||
((k << 24) & 0xff000000); // byte 0 to byte 3
|
||||
|
||||
var fieldID = attrs[0].Name;
|
||||
var fieldType = field.FieldType;
|
||||
k *= m;
|
||||
k ^= k >> r;
|
||||
k *= m;
|
||||
|
||||
field.SetValue(obj, reader.GetValue(reader.GetOrdinal(fieldID)));
|
||||
h ^= k;
|
||||
|
||||
dataIndex -= 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
return (T)obj;
|
||||
}*/
|
||||
// Handle the last few bytes of the input array
|
||||
switch (len)
|
||||
{
|
||||
case 3:
|
||||
h ^= (uint)data[0] << 16; goto case 2;
|
||||
case 2:
|
||||
h ^= (uint)data[len - 2] << 8; goto case 1;
|
||||
case 1:
|
||||
h ^= data[len - 1];
|
||||
h *= m;
|
||||
break;
|
||||
};
|
||||
|
||||
// Do a few final mixes of the hash to ensure the last few
|
||||
// bytes are well-incorporated.
|
||||
|
||||
h ^= h >> 13;
|
||||
h *= m;
|
||||
h ^= h >> 15;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
public static byte[] ConvertBoolArrayToBinaryStream(bool[] array)
|
||||
{
|
||||
byte[] data = new byte[(array.Length / 8) + (array.Length % 8 != 0 ? 1 : 0)];
|
||||
|
||||
int dataCounter = 0;
|
||||
for (int i = 0; i < array.Length; i += 8)
|
||||
{
|
||||
for (int bitCount = 0; bitCount < 8; bitCount++)
|
||||
{
|
||||
if (i + bitCount >= array.Length)
|
||||
break;
|
||||
data[dataCounter] = (byte)(((array[i + bitCount] ? 1 : 0) << 7 - bitCount) | data[dataCounter]);
|
||||
}
|
||||
dataCounter++;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue