Remove " " space from the names

This commit is contained in:
AtelierWindows 2021-01-28 20:56:27 +13:00
parent bef3032886
commit 8e451633dc
59 changed files with 391 additions and 391 deletions

View file

@ -0,0 +1,121 @@
using HISP.Properties;
using System.IO;
namespace HISP.Server
{
class ConfigReader
{
public static int Port;
public static string BindIP;
public static string DatabaseIP;
public static string DatabaseUsername;
public static string DatabaseName;
public static string DatabasePassword;
public static int DatabasePort;
public static int IntrestRate;
public static string Motd;
public static string MapFile;
public static string GameDataFile;
public static string CrossDomainPolicyFile;
public static bool Debug;
public static bool AllUsersSubbed;
public static bool BadWords;
public static bool DoCorrections;
public const int MAX_STACK = 40;
private static string ConfigurationFileName = "server.properties";
public static void OpenConfig()
{
if (!File.Exists(ConfigurationFileName))
{
Logger.WarnPrint(ConfigurationFileName+" not found! writing default.");
File.WriteAllText(ConfigurationFileName,Resources.DefaultServerProperties);
Logger.InfoPrint("! Its very likely database connection will fail...");
}
string[] configFile = File.ReadAllLines(ConfigurationFileName);
foreach (string setting in configFile)
{
/*
* Avoid crashing.
*/
if (setting.Length < 1)
continue;
if (setting[0] == '#')
continue;
if (!setting.Contains("="))
continue;
string[] dataPair = setting.Split('=');
string key = dataPair[0];
string data = dataPair[1];
/*
* Parse configuration file
*/
switch (key)
{
case "port":
Port = int.Parse(data);
break;
case "ip":
BindIP = data;
break;
case "db_ip":
DatabaseIP = data;
break;
case "db_username":
DatabaseUsername = data;
break;
case "db_password":
DatabasePassword = data;
break;
case "db_name":
DatabaseName = data;
break;
case "db_port":
DatabasePort = int.Parse(data);
break;
case "map":
MapFile = data;
break;
case "motd":
Motd = data;
break;
case "gamedata":
GameDataFile = data;
break;
case "crossdomain":
CrossDomainPolicyFile = data;
break;
case "all_users_subscribed":
AllUsersSubbed = data == "true";
break;
case "enable_corrections":
BadWords = data == "true";
break;
case "enable_word_filter":
DoCorrections = data == "true";
break;
case "intrest_rate":
IntrestRate = int.Parse(data);
break;
case "debug":
Debug = data == "true";
break;
}
}
}
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HISP.Server
{
class Converters
{
// Thanks Stackoverflow (https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array)
private static int getHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
public static byte[] StringToByteArray(string hex)
{
if (hex.Length % 2 == 1)
throw new ArgumentException("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((getHexVal(hex[i << 1]) << 4) + (getHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,338 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using HISP.Player;
using HISP.Game;
using HISP.Game.Horse;
namespace HISP.Server
{
class GameClient
{
public Socket ClientSocket;
public string RemoteIp;
public bool LoggedIn = false;
public User LoggedinUser;
private Thread recvPackets;
private Timer updateTimer;
private Timer inactivityTimer;
private Timer warnTimer;
private Timer kickTimer;
private Timer minuteTimer;
private int keepAliveInterval = 60 * 1000;
private int updateInterval = 60 * 1000;
private int totalMinutesElapsed = 0;
private int oneMinute = 60 * 1000;
private int warnInterval = GameServer.IdleWarning * 60 * 1000;
private int kickInterval = GameServer.IdleTimeout * 60 * 1000;
private void minuteTimerTick(object state)
{
totalMinutesElapsed++;
if (LoggedIn)
{
LoggedinUser.FreeMinutes -= 1;
if (LoggedinUser.FreeMinutes <= 0)
{
LoggedinUser.FreeMinutes = 0;
if (!LoggedinUser.Subscribed && !LoggedinUser.Moderator && !LoggedinUser.Administrator)
Kick(Messages.KickReasonNoTime);
}
// unsure of actural timings, would be more or less impossible to know
// without the original source code :(
// From testing hunger seemed to go down fastest, then thirst, and finally tiredness.
foreach(HorseInstance horse in LoggedinUser.HorseInventory.HorseList)
{
if (totalMinutesElapsed % 2 == 0)
{
horse.BasicStats.Thirst--;
horse.BasicStats.Hunger--;
}
if (totalMinutesElapsed % 2 == 0 && (horse.BasicStats.Thirst <= 100 || horse.BasicStats.Thirst <= 100 || horse.BasicStats.Tiredness <= 100))
horse.BasicStats.Health--;
if (totalMinutesElapsed % 60 == 0)
{
horse.BasicStats.Mood--;
horse.BasicStats.Shoes--;
horse.BasicStats.Tiredness--;
}
}
if (totalMinutesElapsed % 1 == 0)
LoggedinUser.Thirst--;
if (totalMinutesElapsed % 5 == 0)
LoggedinUser.Hunger--;
if (totalMinutesElapsed % 10 == 0)
LoggedinUser.Tiredness--;
}
minuteTimer.Change(oneMinute, oneMinute);
}
private void keepAliveTimerTick(object state)
{
Logger.DebugPrint("Sending keep-alive packet to "+ LoggedinUser.Username);
byte[] updatePacket = PacketBuilder.CreateKeepAlive();
SendPacket(updatePacket);
}
private void warnTimerTick(object state)
{
Logger.DebugPrint("Sending inactivity warning to: " + RemoteIp);
byte[] chatPacket = PacketBuilder.CreateChat(Messages.FormatIdleWarningMessage(), PacketBuilder.CHAT_BOTTOM_RIGHT);
SendPacket(chatPacket);
if (LoggedIn)
LoggedinUser.Idle = true;
warnTimer.Dispose();
warnTimer = null;
}
private void kickTimerTick(object state)
{
Kick(Messages.FormatIdleKickMessage());
}
private void updateTimerTick(object state)
{
GameServer.UpdateWorld(this);
GameServer.UpdatePlayer(this);
}
public void Login(int id)
{
// Check for duplicate
foreach(GameClient Client in GameServer.ConnectedClients)
{
if(Client.LoggedIn)
{
if (Client.LoggedinUser.Id == id)
Client.Kick(Messages.KickReasonDuplicateLogin);
}
}
LoggedinUser = new User(this,id);
LoggedIn = true;
updateTimer = new Timer(new TimerCallback(updateTimerTick), null, updateInterval, updateInterval);
inactivityTimer = new Timer(new TimerCallback(keepAliveTimerTick), null, keepAliveInterval, keepAliveInterval);
}
private void receivePackets()
{
// HI1 Packets are terminates by 0x00 so we have to read until we receive that terminator
MemoryStream ms = new MemoryStream();
while(ClientSocket.Connected)
{
try
{
if (ClientSocket.Available >= 1)
{
byte[] buffer = new byte[ClientSocket.Available];
ClientSocket.Receive(buffer);
foreach (Byte b in buffer)
{
ms.WriteByte(b);
if (b == 0x00)
{
ms.Seek(0x00, SeekOrigin.Begin);
byte[] fullPacket = ms.ToArray();
parsePackets(fullPacket);
ms.Close();
ms = new MemoryStream();
}
}
}
}
catch(SocketException e)
{
Logger.ErrorPrint("Socket exception occured: " + e.Message);
Disconnect();
break;
}
}
}
private void parsePackets(byte[] Packet)
{
if (Packet.Length < 1)
{
Logger.ErrorPrint("Received an invalid packet (size: "+Packet.Length+")");
}
byte identifier = Packet[0];
// Reset timers
if (inactivityTimer != null && identifier != PacketBuilder.PACKET_KEEP_ALIVE)
{
if (LoggedIn)
LoggedinUser.Idle = false;
inactivityTimer.Change(keepAliveInterval, keepAliveInterval);
}
if (kickTimer != null && identifier != PacketBuilder.PACKET_KEEP_ALIVE)
kickTimer = new Timer(new TimerCallback(kickTimerTick), null, kickInterval, kickInterval);
if (warnTimer != null && identifier != PacketBuilder.PACKET_KEEP_ALIVE)
warnTimer = new Timer(new TimerCallback(warnTimerTick), null, warnInterval, warnInterval);
if (!LoggedIn) // Must be either login or policy-file-request
{
if (Encoding.UTF8.GetString(Packet).StartsWith("<policy-file-request/>")) // Policy File Request
{
GameServer.OnCrossdomainPolicyRequest(this);
}
switch (identifier)
{
case PacketBuilder.PACKET_LOGIN:
GameServer.OnLoginRequest(this, Packet);
break;
}
}
else
{
switch (identifier)
{
case PacketBuilder.PACKET_LOGIN:
GameServer.OnUserInfoRequest(this, Packet);
break;
case PacketBuilder.PACKET_MOVE:
GameServer.OnMovementPacket(this, Packet);
break;
case PacketBuilder.PACKET_PLAYERINFO:
GameServer.OnPlayerInfoPacket(this, Packet);
break;
case PacketBuilder.PACKET_PLAYER:
GameServer.OnProfilePacket(this, Packet);
break;
case PacketBuilder.PACKET_CHAT:
GameServer.OnChatPacket(this, Packet);
break;
case PacketBuilder.PACKET_CLICK:
GameServer.OnClickPacket(this, Packet);
break;
case PacketBuilder.PACKET_KEEP_ALIVE:
GameServer.OnKeepAlive(this, Packet);
break;
case PacketBuilder.PACKET_TRANSPORT:
GameServer.OnTransportUsed(this, Packet);
break;
case PacketBuilder.PACKET_INVENTORY:
GameServer.OnInventoryRequested(this, Packet);
break;
case PacketBuilder.PACKET_DYNAMIC_BUTTON:
GameServer.OnDynamicButtonPressed(this, Packet);
break;
case PacketBuilder.PACKET_DYNAMIC_INPUT:
GameServer.OnDynamicInputReceived(this, Packet);
break;
case PacketBuilder.PACKET_ITEM_INTERACTION:
GameServer.OnItemInteraction(this,Packet);
break;
case PacketBuilder.PACKET_QUIT:
GameServer.OnQuitPacket(this, Packet);
break;
case PacketBuilder.PACKET_NPC:
GameServer.OnNpcInteraction(this, Packet);
break;
case PacketBuilder.PACKET_SWFMODULE:
GameServer.OnSwfModuleCommunication(this, Packet);
break;
case PacketBuilder.PACKET_HORSE:
GameServer.OnHorseInteraction(this, Packet);
break;
case PacketBuilder.PACKET_WISH:
GameServer.OnWish(this, Packet);
break;
default:
Logger.ErrorPrint("Unimplemented Packet: " + BitConverter.ToString(Packet).Replace('-', ' '));
break;
}
}
}
public void Disconnect()
{
if(updateTimer != null)
updateTimer.Dispose();
if(inactivityTimer != null)
inactivityTimer.Dispose();
if(warnTimer != null)
warnTimer.Dispose();
if(kickTimer != null)
kickTimer.Dispose();
GameServer.OnDisconnect(this);
LoggedIn = false;
LoggedinUser = null;
ClientSocket.Close();
ClientSocket.Dispose();
//recvPackets.Anort();
}
public void Kick(string Reason)
{
byte[] kickPacket = PacketBuilder.CreateKickMessage(Reason);
SendPacket(kickPacket);
Disconnect();
Logger.InfoPrint("CLIENT: "+RemoteIp+" KICKED for: "+Reason);
}
public void SendPacket(byte[] PacketData)
{
try
{
ClientSocket.Send(PacketData);
}
catch (Exception e)
{
Logger.ErrorPrint("Exception occured: " + e.Message);
Disconnect();
}
}
public GameClient(Socket clientSocket)
{
ClientSocket = clientSocket;
RemoteIp = clientSocket.RemoteEndPoint.ToString();
Logger.DebugPrint("Client connected @ " + RemoteIp);
kickTimer = new Timer(new TimerCallback(kickTimerTick), null, kickInterval, kickInterval);
warnTimer = new Timer(new TimerCallback(warnTimerTick), null, warnInterval, warnInterval);
minuteTimer = new Timer(new TimerCallback(minuteTimerTick), null, oneMinute, oneMinute);
recvPackets = new Thread(() =>
{
receivePackets();
});
recvPackets.Start();
}
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
using System;
namespace HISP.Server
{
class Logger
{
public static void HackerPrint(string text) // When someone is obviously cheating.
{
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[HACK] " + text);
Console.ForegroundColor = prevColor;
}
public static void DebugPrint(string text)
{
if (ConfigReader.Debug)
Console.WriteLine("[DEBUG] " + text);
}
public static void WarnPrint(string text)
{
Console.WriteLine("[WARN] " + text);
}
public static void ErrorPrint(string text)
{
Console.WriteLine("[ERROR] " + text);
}
public static void InfoPrint(string text)
{
Console.WriteLine("[INFO] " + text);
}
}
}

View file

@ -0,0 +1,844 @@
using System;
using System.IO;
using System.Text;
using HISP.Game;
using HISP.Game.Horse;
using HISP.Game.SwfModules;
namespace HISP.Server
{
class PacketBuilder
{
public const byte PACKET_TERMINATOR = 0x00;
public const byte PACKET_CLIENT_TERMINATOR = 0x0A;
public const byte PACKET_LOGIN = 0x7F;
public const byte PACKET_CHAT = 0x14;
public const byte PACKET_MOVE = 0x15;
public const byte PACKET_CLICK = 0x77;
public const byte PACKET_USERINFO = 0x81;
public const byte PACKET_WORLD = 0x7A;
public const byte PACKET_BASE_STATS = 0x7B;
public const byte PACKET_SWF_CUTSCENE = 0x29;
public const byte PACKET_SWF_MODULE_FORCE = 0x28;
public const byte PACKET_SWF_MODULE_GENTLE = 0x2A;
public const byte PACKET_PLACE_INFO = 0x1E;
public const byte PACKET_HORSE = 0x19;
public const byte PACKET_AREA_DEFS = 0x79;
public const byte PACKET_ITEM_INTERACTION = 0x1E;
public const byte PACKET_ANNOUNCEMENT = 0x7E;
public const byte PACKET_TILE_FLAGS = 0x75;
public const byte PACKET_PLAYSOUND = 0x23;
public const byte PACKET_KEEP_ALIVE = 0x7C;
public const byte PACKET_DYNAMIC_BUTTON = 0x45;
public const byte PACKET_DYNAMIC_INPUT = 0x46;
public const byte PACKET_PLAYER = 0x18;
public const byte PACKET_INVENTORY = 0x17;
public const byte PACKET_TRANSPORT = 0x29;
public const byte PACKET_KICK = 0x80;
public const byte PACKET_LEAVE = 0x7D;
public const byte PACKET_NPC = 0x28;
public const byte PACKET_QUIT = 0x7D;
public const byte PACKET_PLAYERINFO = 0x16;
public const byte PACKET_INFORMATION = 0x28;
public const byte PACKET_WISH = 0x2C;
public const byte PACKET_SWFMODULE = 0x50;
public const byte HORSE_LIST = 0x0A;
public const byte HORSE_LOOK = 0x14;
public const byte HORSE_FEED = 0x15;
public const byte HORSE_PET = 0x18;
public const byte HORSE_PROFILE = 0x2C;
public const byte HORSE_PROFILE_EDIT = 0x14;
public const byte HORSE_TRY_CAPTURE = 0x1C;
public const byte HORSE_RELEASE = 0x19;
public const byte HORSE_TACK = 0x16;
public const byte HORSE_DRINK = 0x2B;
public const byte HORSE_GIVE_FEED = 0x1B;
public const byte HORSE_TACK_EQUIP = 0x3C;
public const byte HORSE_TACK_UNEQUIP = 0x3D;
public const byte HORSE_VET_SERVICE = 0x2A;
public const byte HORSE_VET_SERVICE_ALL = 0x2F;
public const byte HORSE_MOUNT = 0x46;
public const byte HORSE_DISMOUNT = 0x47;
public const byte HORSE_ESCAPE = 0x1E;
public const byte HORSE_CAUGHT = 0x1D;
public const byte SWFMODULE_BRICKPOET = 0x5A;
public const byte BRICKPOET_LIST_ALL = 0x14;
public const byte BRICKPOET_MOVE = 0x55;
public const byte WISH_MONEY = 0x31;
public const byte WISH_ITEMS = 0x32;
public const byte WISH_WORLDPEACE = 0x33;
public const byte SECCODE_QUEST = 0x32;
public const byte SECCODE_GIVE_ITEM = 0x28;
public const byte SECCODE_DELETE_ITEM = 0x29;
public const byte SECCODE_SCORE = 0x3D;
public const byte SECCODE_TIME = 0x3E;
public const byte SECCODE_MONEY = 0x1E;
public const byte SECCODE_AWARD = 0x33;
public const byte NPC_START_CHAT = 0x14;
public const byte NPC_CONTINUE_CHAT = 0x15;
public const byte PLAYERINFO_LEAVE = 0x16;
public const byte PLAYERINFO_UPDATE_OR_CREATE = 0x15;
public const byte PLAYERINFO_PLAYER_LIST = 0x14;
public const byte PROFILE_HIGHSCORES_LIST = 0x51;
public const byte PROFILE_BESTTIMES_LIST = 0x52;
public const byte VIEW_PROFILE = 0x14;
public const byte SAVE_PROFILE = 0x15;
public const byte AREA_SEPERATOR = 0x5E;
public const byte AREA_TOWN = 0x54;
public const byte AREA_AREA = 0x41;
public const byte AREA_ISLE = 0x49;
public const byte MOVE_UP = 0x14;
public const byte MOVE_DOWN = 0x15;
public const byte MOVE_RIGHT = 0x16;
public const byte MOVE_LEFT = 0x17;
public const byte MOVE_ESCAPE = 0x18;
public const byte MOVE_UPDATE = 0x0A;
public const byte CHAT_BOTTOM_LEFT = 0x14;
public const byte CHAT_BOTTOM_RIGHT = 0x15;
public const byte CHAT_DM_RIGHT = 0x16;
public const byte ITEM_INFORMATON = 0x14;
public const byte ITEM_INFORMATON_ID = 0x15;
public const byte NPC_INFORMATION = 0x16;
public const byte ITEM_DROP = 0x1E;
public const byte ITEM_PICKUP = 0x14;
public const byte ITEM_PICKUP_ALL = 0x15;
public const byte ITEM_BUY = 0x33;
public const byte ITEM_BUY_AND_CONSUME = 0x34;
public const byte ITEM_BUY_5 = 0x35;
public const byte ITEM_BUY_25 = 0x37;
public const byte ITEM_SELL = 0x3C;
public const byte ITEM_SELL_ALL = 0x3D;
public const byte ITEM_WEAR = 0x46;
public const byte ITEM_REMOVE = 0x47;
public const byte ITEM_CONSUME = 0x51;
public const byte ITEM_DRINK = 0x52;
public const byte ITEM_BINOCULARS = 0x5C;
public const byte ITEM_MAGNIFYING = 0x5D;
public const byte ITEM_RAKE = 0x5B;
public const byte ITEM_SHOVEL = 0x5A;
public const byte LOGIN_INVALID_USER_PASS = 0x15;
public const byte LOGIN_CUSTOM_MESSAGE = 0x16;
public const byte LOGIN_SUCCESS = 0x14;
public const byte DIRECTION_UP = 0;
public const byte DIRECTION_RIGHT = 1;
public const byte DIRECTION_DOWN = 2;
public const byte DIRECTION_LEFT = 3;
public const byte DIRECTION_TELEPORT = 4;
public const byte DIRECTION_NONE = 10;
public static byte[] CreateBrickPoetMovePacket(Brickpoet.PoetryPeice peice)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PacketBuilder.PACKET_SWFMODULE);
ms.WriteByte(PacketBuilder.BRICKPOET_MOVE);
string packetStr = "|";
packetStr += peice.Id + "|";
packetStr += peice.X + "|";
packetStr += peice.Y + "|";
packetStr += "^";
byte[] infoBytes = Encoding.UTF8.GetBytes(packetStr);
ms.Write(infoBytes, 0x00, infoBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
return ms.ToArray();
}
public static byte[] CreateBrickPoetListPacket(Brickpoet.PoetryPeice[] room)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PacketBuilder.PACKET_SWFMODULE);
string packetStr = "";
foreach(Brickpoet.PoetryPeice peice in room)
{
packetStr += "A";
packetStr += "|";
packetStr += peice.Id;
packetStr += "|";
packetStr += peice.Word.ToUpper();
packetStr += "|";
packetStr += peice.X;
packetStr += "|";
packetStr += peice.Y;
packetStr += "|";
packetStr += "^";
}
byte[] packetBytes = Encoding.UTF8.GetBytes(packetStr);
ms.Write(packetBytes, 0x00, packetBytes.Length);
ms.WriteByte(PacketBuilder.PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
return ms.ToArray();
}
public static byte[] CreatePlaysoundPacket(string sound)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_PLAYSOUND);
byte[] strBytes = Encoding.UTF8.GetBytes(sound);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreatePlayerLeavePacket(string username)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_PLAYERINFO);
ms.WriteByte(PLAYERINFO_LEAVE);
byte[] strBytes = Encoding.UTF8.GetBytes(username);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreatePlayerInfoUpdateOrCreate(int x, int y, int facing, int charId, string username)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_PLAYERINFO);
ms.WriteByte(PLAYERINFO_UPDATE_OR_CREATE);
ms.WriteByte((byte)(((x - 4) / 64) + 20));
ms.WriteByte((byte)(((x - 4) % 64) + 20));
ms.WriteByte((byte)(((y - 1) / 64) + 20));
ms.WriteByte((byte)(((y - 1) % 64) + 20));
ms.WriteByte((byte)(facing + 20));
ms.WriteByte((byte)((charId / 64) + 20)); //6
ms.WriteByte((byte)((charId % 64) + 20)); //7
byte[] strBytes = Encoding.UTF8.GetBytes(username);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateLoginPacket(bool Success, string message="")
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_LOGIN);
if (message != "")
ms.WriteByte(LOGIN_CUSTOM_MESSAGE);
else if (Success)
ms.WriteByte(LOGIN_SUCCESS);
else
ms.WriteByte(LOGIN_INVALID_USER_PASS);
byte[] loginFailMessage = Encoding.UTF8.GetBytes(message);
ms.Write(loginFailMessage, 0x00, loginFailMessage.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateProfilePacket(string userProfile)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_PLAYER);
byte[] strBytes = Encoding.UTF8.GetBytes(userProfile);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateHorseRidePacket(int x, int y, int charId, int facing, int direction, bool walk)
{
// Header information
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_MOVE);
ms.WriteByte((byte)(((x - 4) / 64) + 20)); //1
ms.WriteByte((byte)(((x - 4) % 64) + 20)); //2
ms.WriteByte((byte)(((y - 1) / 64) + 20)); //3
ms.WriteByte((byte)(((y - 1) % 64) + 20)); //4
ms.WriteByte((byte)(facing + 20)); //5
ms.WriteByte((byte)((charId / 64) + 20)); //6
ms.WriteByte((byte)((charId % 64) + 20)); //7
ms.WriteByte((byte)(direction + 20)); //8
ms.WriteByte((byte)(Convert.ToInt32(walk) + 20)); //9\
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] packetData = ms.ToArray();
ms.Dispose();
return packetData;
}
public static byte[] CreateMovementPacket(int x, int y,int charId,int facing, int direction, bool walk)
{
// Header information
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_MOVE);
ms.WriteByte((byte)(((x-4) / 64) + 20)); //1
ms.WriteByte((byte)(((x-4) % 64) + 20)); //2
ms.WriteByte((byte)(((y-1) / 64) + 20)); //3
ms.WriteByte((byte)(((y-1) % 64) + 20)); //4
ms.WriteByte((byte)(facing + 20)); //5
ms.WriteByte((byte)((charId / 64) + 20)); //6
ms.WriteByte((byte)((charId % 64) + 20)); //7
ms.WriteByte((byte)(direction + 20)); //8
ms.WriteByte((byte)(Convert.ToInt32(walk) + 20)); //9
// Map Data
bool moveTwo = false;
if(direction >= 20)
{
direction -= 20;
moveTwo = true;
}
int ystart = y - 4;
int xstart = x - 6;
int xend = xstart + 12;
int yend = ystart + 9;
if (direction == DIRECTION_UP)
{
int totalY = 0;
if (moveTwo)
{
ystart++;
totalY = 1;
}
for (int yy = ystart; yy >= ystart - totalY; yy--)
{
for (int xx = xstart; xx <= xend; xx++)
{
int tileId = Map.GetTileId(xx, yy, false);
int otileId = Map.GetTileId(xx, yy, true);
if (tileId >= 190)
{
ms.WriteByte((byte)190);
tileId -= 100;
}
ms.WriteByte((byte)tileId);
if (otileId >= 190)
{
ms.WriteByte((byte)190);
otileId -= 100;
}
ms.WriteByte((byte)otileId);
}
}
}
if (direction == DIRECTION_LEFT)
{
int totalX = 0;
if (moveTwo)
{
xstart++;
totalX = 1;
}
for (int xx = xstart; xx >= xstart - totalX; xx--)
{
for (int yy = ystart; yy <= yend; yy++)
{
int tileId = Map.GetTileId(xx, yy, false);
int otileId = Map.GetTileId(xx, yy, true);
if (tileId >= 190)
{
ms.WriteByte((byte)190);
tileId -= 100;
}
ms.WriteByte((byte)tileId);
if (otileId >= 190)
{
ms.WriteByte((byte)190);
otileId -= 100;
}
ms.WriteByte((byte)otileId);
}
}
}
if (direction == DIRECTION_RIGHT)
{
int totalX = 0;
if (moveTwo)
{
xend--;
totalX = 1;
}
for (int xx = xend; xx <= xend + totalX; xx++)
{
for (int yy = ystart; yy <= yend; yy++)
{
int tileId = Map.GetTileId(xx, yy, false);
int otileId = Map.GetTileId(xx, yy, true);
if (tileId >= 190)
{
ms.WriteByte((byte)190);
tileId -= 100;
}
ms.WriteByte((byte)tileId);
if (otileId >= 190)
{
ms.WriteByte((byte)190);
otileId -= 100;
}
ms.WriteByte((byte)otileId);
}
}
}
if (direction == DIRECTION_DOWN)
{
int totalY = 0;
if (moveTwo)
{
yend--;
totalY = 1;
}
for (int yy = yend; yy <= yend + totalY; yy++)
{
for (int xx = xstart; xx <= xend; xx++)
{
int tileId = Map.GetTileId(xx, yy, false);
int otileId = Map.GetTileId(xx, yy, true);
if (tileId >= 190)
{
ms.WriteByte((byte)190);
tileId -= 100;
}
ms.WriteByte((byte)tileId);
if (otileId >= 190)
{
ms.WriteByte((byte)190);
otileId -= 100;
}
ms.WriteByte((byte)otileId);
}
}
}
if (direction == DIRECTION_TELEPORT)
{
for(int rely = 0; rely <= 9; rely++)
{
for (int relx = 0; relx <= 12; relx++)
{
int tileId = Map.GetTileId(xstart + relx, ystart + rely, false);
int otileId = Map.GetTileId(xstart + relx, ystart + rely, true);
if(tileId >= 190)
{
ms.WriteByte((byte)190);
tileId -= 100;
}
ms.WriteByte((byte)tileId);
if (otileId >= 190)
{
ms.WriteByte((byte)190);
otileId -= 100;
}
ms.WriteByte((byte)otileId);
}
}
}
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateClickTileInfoPacket(string text)
{
byte[] strBytes = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_CLICK);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateMetaPacket(string formattedText)
{
byte[] strBytes = Encoding.UTF8.GetBytes(formattedText);
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_PLACE_INFO);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateChat(string formattedText, byte chatWindow)
{
byte[] strBytes = Encoding.UTF8.GetBytes(formattedText);
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_CHAT);
ms.WriteByte(chatWindow);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateWorldData(int gameTime, int gameDay, int gameYear, string weather)
{
byte[] strBytes = Encoding.UTF8.GetBytes(weather);
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_WORLD);
ms.WriteByte((byte)((gameTime / 64) + 20));
ms.WriteByte((byte)((gameTime % 64) + 20));
ms.WriteByte((byte)((gameDay / 64) + 20));
ms.WriteByte((byte)((gameDay % 64) + 20));
ms.WriteByte((byte)((gameYear / 64) + 20));
ms.WriteByte((byte)((gameYear % 64) + 20));
ms.Write(strBytes,0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateKeepAlive()
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_KEEP_ALIVE);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreatePlaceData(World.Isle[] isles, World.Town[] towns, World.Area[] areas)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_AREA_DEFS);
// Write Towns
foreach (World.Town town in towns)
{
byte[] strBytes = Encoding.UTF8.GetBytes(town.Name);
ms.WriteByte(AREA_SEPERATOR);
ms.WriteByte(AREA_TOWN);
ms.WriteByte((byte)(((town.StartX - 4) / 64) + 20));
ms.WriteByte((byte)(((town.StartX - 4) % 64) + 20));
ms.WriteByte((byte)(((town.EndX - 4) / 64) + 20));
ms.WriteByte((byte)(((town.EndX - 4) % 64) + 20));
ms.WriteByte((byte)(((town.StartY - 1) / 64) + 20));
ms.WriteByte((byte)(((town.StartY - 1) % 64) + 20));
ms.WriteByte((byte)(((town.EndY - 1) / 64) + 20));
ms.WriteByte((byte)(((town.EndY - 1) % 64) + 20));
ms.Write(strBytes, 0x00, strBytes.Length);
}
// Write Areas
foreach (World.Area area in areas)
{
byte[] strBytes = Encoding.UTF8.GetBytes(area.Name);
ms.WriteByte(AREA_SEPERATOR);
ms.WriteByte(AREA_AREA);
ms.WriteByte((byte)(((area.StartX - 4) / 64) + 20));
ms.WriteByte((byte)(((area.StartX - 4) % 64) + 20));
ms.WriteByte((byte)(((area.EndX - 4) / 64) + 20));
ms.WriteByte((byte)(((area.EndX - 4) % 64) + 20));
ms.WriteByte((byte)(((area.StartY - 1) / 64) + 20));
ms.WriteByte((byte)(((area.StartY - 1) % 64) + 20));
ms.WriteByte((byte)(((area.EndY - 1) / 64) + 20));
ms.WriteByte((byte)(((area.EndY - 1) % 64) + 20));
ms.Write(strBytes, 0x00, strBytes.Length);
}
// Write Isles
foreach (World.Isle isle in isles)
{
byte[] strBytes = Encoding.UTF8.GetBytes(isle.Name);
ms.WriteByte(AREA_SEPERATOR);
ms.WriteByte(AREA_ISLE);
ms.WriteByte((byte)(((isle.StartX - 4) / 64) + 20));
ms.WriteByte((byte)(((isle.StartX - 4) % 64) + 20));
ms.WriteByte((byte)(((isle.EndX - 4) / 64) + 20));
ms.WriteByte((byte)(((isle.EndX - 4) % 64) + 20));
ms.WriteByte((byte)(((isle.StartY - 1) / 64) + 20));
ms.WriteByte((byte)(((isle.StartY - 1) % 64) + 20));
ms.WriteByte((byte)(((isle.EndY - 1) / 64) + 20));
ms.WriteByte((byte)(((isle.EndY - 1) % 64) + 20));
ms.WriteByte((byte)isle.Tileset.ToString()[0]);
ms.Write(strBytes, 0x00, strBytes.Length);
}
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreatePlayerData(int money, int playerCount, int mail)
{
byte[] moneyStrBytes = Encoding.UTF8.GetBytes(money.ToString("N0"));
byte[] playerStrBytes = Encoding.UTF8.GetBytes(playerCount.ToString("N0"));
byte[] mailStrBytes = Encoding.UTF8.GetBytes(mail.ToString("N0"));
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_BASE_STATS);
ms.Write(moneyStrBytes, 0x00, moneyStrBytes.Length);
ms.WriteByte((byte)'|');
ms.Write(playerStrBytes, 0x00, playerStrBytes.Length);
ms.WriteByte((byte)'|');
ms.Write(mailStrBytes, 0x00, mailStrBytes.Length);
ms.WriteByte((byte)'|');
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateTileOverlayFlags(int[] tileDepthFlags)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_TILE_FLAGS);
foreach(int tileDepthFlag in tileDepthFlags)
{
ms.WriteByte((byte)tileDepthFlag.ToString()[0]);
}
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateSecCode(byte[] SecCodeSeed, int SecCodeInc, bool Admin, bool Moderator)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_USERINFO);
ms.WriteByte((byte)(SecCodeSeed[0] + 33));
ms.WriteByte((byte)(SecCodeSeed[1] + 33));
ms.WriteByte((byte)(SecCodeSeed[2] + 33));
ms.WriteByte((byte)(SecCodeInc + 33));
char userType = 'N'; // Normal?
if (Moderator)
userType = 'M';
if (Admin)
userType = 'A';
ms.WriteByte((byte)userType);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateSwfModulePacket(string swf,byte type)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(type);
byte[] strBytes = Encoding.UTF8.GetBytes(swf);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateAnnouncement(string announcement)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_ANNOUNCEMENT);
byte[] strBytes = Encoding.UTF8.GetBytes(announcement);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateKickMessage(string reason)
{
MemoryStream ms = new MemoryStream();
ms.WriteByte(PACKET_KICK);
byte[] strBytes = Encoding.UTF8.GetBytes(reason);
ms.Write(strBytes, 0x00, strBytes.Length);
ms.WriteByte(PACKET_TERMINATOR);
ms.Seek(0x00, SeekOrigin.Begin);
byte[] Packet = ms.ToArray();
ms.Dispose();
return Packet;
}
public static byte[] CreateMotd()
{
string formattedMotd = Messages.FormatMOTD();
return CreateAnnouncement(formattedMotd);
}
public static byte[] CreateWelcomeMessage(string username)
{
string formattedStr = Messages.FormatWelcomeMessage(username);
return CreateChat(formattedStr, CHAT_BOTTOM_RIGHT);
}
}
}