public Form1() { InitializeComponent(); bool FoundConfig = ConfigurationManager.LoadCityConfig(); Logger.Initialize("Log.txt"); Logger.WarnEnabled = true; Logger.DebugEnabled = true; GonzoNet.Logger.OnMessageLogged += new MessageLoggedDelegate(Logger_OnMessageLogged); if (!FoundConfig) { Logger.LogWarning("Couldn't find a ServerConfig.ini file!"); //TODO: This doesn't work... Application.Exit(); } //This has to happen for the static constructor to be called... NetworkFacade m_NetworkFacade = new NetworkFacade(); var dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MAIN_DB"]; DataAccess.ConnectionString = dbConnectionString.ConnectionString; /** TODO: Test the database **/ using (var db = DataAccess.Get()) { var testAccount = db.Accounts.GetByUsername("root"); if (testAccount == null) { db.Accounts.Create(new Account { AccountName = "root", Password = "******" }); } } m_Listener = new Listener(); //m_Listener.OnReceiveEvent += new OnReceiveDelegate(m_Listener_OnReceiveEvent); m_LoginClient = new NetworkClient("127.0.0.1", 2108); m_LoginClient.OnNetworkError += new NetworkErrorDelegate(m_LoginClient_OnNetworkError); m_LoginClient.OnConnected += new OnConnectedDelegate(m_LoginClient_OnConnected); m_LoginClient.Connect(null); //Send a pulse to the LoginServer every second. m_PulseTimer = new System.Timers.Timer(1000); m_PulseTimer.AutoReset = true; m_PulseTimer.Elapsed += new ElapsedEventHandler(m_PulseTimer_Elapsed); m_PulseTimer.Start(); m_Listener.Initialize(Settings.BINDING); }
public Form1() { InitializeComponent(); bool FoundConfig = ConfigurationManager.LoadCityConfig(); Logger.Initialize("Log.txt"); Logger.WarnEnabled = true; Logger.DebugEnabled = true; GonzoNet.Logger.OnMessageLogged += new GonzoNet.MessageLoggedDelegate(Logger_OnMessageLogged); CityDataModel.Logger.OnMessageLogged += new CityDataModel.MessageLoggedDelegate(Logger_OnMessageLogged); ProtocolAbstractionLibraryD.Logger.OnMessageLogged += new ProtocolAbstractionLibraryD.MessageLoggedDelegate(Logger_OnMessageLogged); if (!FoundConfig) { Logger.LogWarning("Couldn't find a ServerConfig.ini file!"); //TODO: This doesn't work... Application.Exit(); } //This has to happen for the static constructor to be called... NetworkFacade m_NetworkFacade = new NetworkFacade(); var dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MAIN_DB"]; DataAccess.ConnectionString = dbConnectionString.ConnectionString; m_Listener = new Listener(); //m_Listener.OnReceiveEvent += new OnReceiveDelegate(m_Listener_OnReceiveEvent); m_LoginClient = new NetworkClient("127.0.0.1", 2108); m_LoginClient.OnNetworkError += new NetworkErrorDelegate(m_LoginClient_OnNetworkError); m_LoginClient.OnConnected += new OnConnectedDelegate(m_LoginClient_OnConnected); m_LoginClient.Connect(null); //Send a pulse to the LoginServer every second. m_PulseTimer = new System.Timers.Timer(1000); m_PulseTimer.AutoReset = true; m_PulseTimer.Elapsed += new ElapsedEventHandler(m_PulseTimer_Elapsed); m_PulseTimer.Start(); m_Listener.Initialize(Settings.BINDING); }
/// <summary> /// Received client token. /// </summary> public static void HandleCityToken(NetworkClient Client, ProcessedPacket P) { try { //bool ClientAuthenticated = false; ClientToken TokenToRemove = new ClientToken(); using (DataAccess db = DataAccess.Get()) { string Token = P.ReadString(); ClientToken Tok; if (Token == string.Empty) { return; } Tok = NetworkFacade.GetClientToken(new Guid(Token)); if (Tok != null) { //ClientAuthenticated = true; TokenToRemove = Tok; Character Char = db.Characters.GetForCharacterGUID(new Guid(Tok.CharacterGUID)); if (Char != null) { NetworkFacade.CurrentSession.AddPlayer(Client, Char); PacketStream SuccessPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); SuccessPacket.WriteByte((byte)CityTransferStatus.Success); House[] Houses = NetworkFacade.CurrentSession.GetHousesInSession(); SuccessPacket.WriteUInt16((ushort)Houses.Length); //Ho, ho, ho... foreach (House Ho in Houses) { SuccessPacket.WriteInt32(Ho.HouseID); SuccessPacket.WriteString(Ho.Name); SuccessPacket.WriteUInt16((ushort)Ho.X); SuccessPacket.WriteUInt16((ushort)Ho.Y); SuccessPacket.WriteByte((byte)Ho.Flags); //Might have to save this as unsigned in DB? SuccessPacket.WriteInt32(Ho.Cost); } SuccessPacket.WriteInt32(Char.Money); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, SuccessPacket.ToArray()); } /*else * { * ClientAuthenticated = false; * break; * }*/ } NetworkFacade.TransferringClients.TryRemove(out TokenToRemove); //This is not really valid anymore, because if the token doesn't exist yet, //the client will now receive it when it arrives - see LoginPacketHandlers.cs // - HandleClientToken() /*if (!ClientAuthenticated) * { * PacketStream ErrorPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); * ErrorPacket.WriteByte((byte)CityTransferStatus.GeneralError); * Client.SendEncrypted((byte)PacketType.CITY_TOKEN, ErrorPacket.ToArray()); * }*/ } } catch (Exception E) { Logger.LogDebug("Exception in HandleCityToken: " + E.ToString()); Debug.WriteLine("Exception in HandleCityToken: " + E.ToString()); PacketStream ErrorPacket = new PacketStream((byte)PacketType.CITY_TOKEN, 0); ErrorPacket.WriteByte((byte)CityTransferStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CITY_TOKEN, ErrorPacket.ToArray()); } }
/// <summary> /// Client wanted to create a new character. /// </summary> public static void HandleCharacterCreate(NetworkClient Client, ProcessedPacket P) { try { Logger.LogInfo("Received CharacterCreate!"); bool ClientAuthenticated = false; byte AccountStrLength = (byte)P.ReadByte(); byte[] AccountNameBuf = new byte[AccountStrLength]; if (P.BufferLength >= AccountStrLength) { P.Read(AccountNameBuf, 0, AccountStrLength); string AccountName = Encoding.ASCII.GetString(AccountNameBuf); } else { return; } using (DataAccess db = DataAccess.Get()) { //No need to check for empty string here, because all it will do is have ClientAuthenticated be false string Token = P.ReadString(); string GUID = ""; int AccountID = 0; ClientToken TokenToRemove = NetworkFacade.GetClientToken(Client.RemoteIP); //Just in case... if (TokenToRemove != null) { if (TokenToRemove.Token.Equals(Token, StringComparison.CurrentCultureIgnoreCase)) { PacketStream SuccessPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY, 0); SuccessPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.Success); House[] Houses = NetworkFacade.CurrentSession.GetHousesInSession(); SuccessPacket.WriteUInt16((ushort)Houses.Length); //Ho, ho, ho... foreach (House Ho in Houses) { SuccessPacket.WriteInt32(Ho.HouseID); SuccessPacket.WriteString(Ho.Name); SuccessPacket.WriteUInt16((ushort)Ho.X); SuccessPacket.WriteUInt16((ushort)Ho.Y); SuccessPacket.WriteByte((byte)Ho.Flags); //Might have to save this as unsigned in DB? SuccessPacket.WriteInt32(Ho.Cost); } Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY, SuccessPacket.ToArray()); ClientAuthenticated = true; GUID = TokenToRemove.CharacterGUID; AccountID = TokenToRemove.AccountID; Sim Char = new Sim(new Guid(GUID)); Char.Timestamp = P.ReadString(); Char.Name = P.ReadString(); Char.Sex = P.ReadString(); Char.Description = P.ReadString(); Char.HeadOutfitID = P.ReadUInt64(); Char.BodyOutfitID = P.ReadUInt64(); Char.Appearance = (AppearanceType)P.ReadByte(); Char.CreatedThisSession = true; //These are going into DB, so be nazi. Sieg heil! if (Char.Timestamp == string.Empty || Char.Name == string.Empty || Char.Sex == string.Empty || Char.Description == string.Empty) { //TODO: Tell loginserver to clean up DB? ClientAuthenticated = false; } var characterModel = new Character(); characterModel.Name = Char.Name; characterModel.Sex = Char.Sex; characterModel.Description = Char.Description; characterModel.LastCached = ProtoHelpers.ParseDateTime(Char.Timestamp); characterModel.GUID = Char.GUID; characterModel.HeadOutfitID = (long)Char.HeadOutfitID; characterModel.BodyOutfitID = (long)Char.BodyOutfitID; characterModel.AccountID = AccountID; characterModel.AppearanceType = (int)Char.Appearance; characterModel.Money = NetworkFacade.INITIAL_MONEY; NetworkFacade.CurrentSession.AddPlayer(Client, characterModel); var status = db.Characters.CreateCharacter(characterModel); } } NetworkFacade.TransferringClients.TryRemove(out TokenToRemove); } //Invalid token, should never occur... if (!ClientAuthenticated) { NetworkFacade.CurrentSession.RemovePlayer(Client); PacketStream FailPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, (int)(PacketHeaders.ENCRYPTED + 1)); FailPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, FailPacket.ToArray()); } } catch (Exception E) { Debug.WriteLine("Exception in HandleCharacterCreate: " + E.ToString()); Logger.LogDebug("Exception in HandleCharacterCreate: " + E.ToString()); PacketStream FailPacket = new PacketStream((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, (int)(PacketHeaders.ENCRYPTED + 1)); FailPacket.WriteByte((byte)CityDataModel.Entities.CharacterCreationStatus.GeneralError); Client.SendEncrypted((byte)PacketType.CHARACTER_CREATE_CITY_FAILED, FailPacket.ToArray()); } }
static void Main(string[] args) { bool FoundConfig = ConfigurationManager.LoadCityConfig(); Logger.Initialize("Log.txt"); Logger.WarnEnabled = true; Logger.DebugEnabled = true; GonzoNet.Logger.OnMessageLogged += new GonzoNet.MessageLoggedDelegate(Logger_OnMessageLogged); CityDataModel.Logger.OnMessageLogged += new CityDataModel.MessageLoggedDelegate(Logger_OnMessageLogged); ProtocolAbstractionLibraryD.Logger.OnMessageLogged += new ProtocolAbstractionLibraryD.MessageLoggedDelegate(Logger_OnMessageLogged); if (!FoundConfig) { Console.WriteLine("Couldn't find a ServerConfig.ini file!"); Console.ReadLine(); Environment.Exit(0); } //This has to happen for the static constructor to be called... NetworkFacade m_NetworkFacade = new NetworkFacade(); var dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MAIN_DB"]; DataAccess.ConnectionString = dbConnectionString.ConnectionString; NetworkFacade.NetworkListener = new Listener(EncryptionMode.AESCrypto); //Remove a player from the current session when it disconnects. NetworkFacade.NetworkListener.OnDisconnected += new OnDisconnectedDelegate(NetworkFacade.CurrentSession.RemovePlayer); m_LoginClient = new NetworkClient("127.0.0.1", 2108, EncryptionMode.AESCrypto, true); m_LoginClient.OnNetworkError += new NetworkErrorDelegate(m_LoginClient_OnNetworkError); m_LoginClient.OnConnected += new OnConnectedDelegate(m_LoginClient_OnConnected); m_LoginClient.Connect(null); //Adds all houses from DB to the current session. using(DataAccess db = DataAccess.Get()) { IQueryable<Character> Chars = db.Characters.GetAllCharsWithHouses(); var CharsWithHouses = Chars.Where(x => x.HouseHouse != null); foreach(Character Char in CharsWithHouses) NetworkFacade.CurrentSession.AddHouse(Char, Char.HouseHouse); } NetworkFacade.NetworkListener.Initialize(Settings.BINDING); m_NancyHost = new NancyHost(new Uri("http://173.248.136.133:8888/city/")); m_NancyHost.Start(); try { NetworkFacade.CurrentTerrain.Initialize("East Jerome"); NetworkFacade.CurrentTerrain.LoadContent(); NetworkFacade.CurrentTerrain.GenerateCityMesh(); } catch(Exception e) { Console.WriteLine("Couldn't load elevation data!"); Console.WriteLine(e.ToString()); Console.ReadLine(); Environment.Exit(0); } m_VM = new VM.VM(); m_VM.Init(); while (true) { m_VM.Update(); Thread.Sleep(1000); } }