// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT using System; using System.Drawing; using System.IO; using System.Net; using ClassicalSharp.Gui.Screens; using ClassicalSharp.Network; using ClassicalSharp.TexturePack; using OpenTK; using OpenTK.Input; #if ANDROID using Android.Graphics; #endif namespace ClassicalSharp { /// Represents a connection to either a multiplayer server, or an internal single player server. public abstract class IServerConnection { public abstract bool IsSinglePlayer { get; } /// Opens a connection to the given IP address and port, and prepares the initial state of the client. public abstract void Connect( IPAddress address, int port ); public abstract void SendChat( string text, bool partial ); /// Informs the server of the client's current position and orientation. public abstract void SendPosition( Vector3 pos, float yaw, float pitch ); /// Informs the server that using the given mouse button, /// the client clicked on a particular block or entity. public abstract void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ); public abstract void Tick( ScheduledTask task ); public abstract void Dispose(); public string ServerName; public string ServerMotd; /// Whether the network processor is currently connected to a server. public bool Disconnected; /// Whether the client should use extended player list management, with group names and ranks. public bool UsingExtPlayerList; /// Whether the server supports handling PlayerClick packets from the client. public bool UsingPlayerClick; /// Whether the server can handle partial message packets or not. public bool SupportsPartialMessages; /// Whether the server supports receiving all code page 437 characters from this client. public bool SupportsFullCP437; #region Texture pack / terrain.png protected Game game; protected void WarningScreenTick( WarningScreen screen ) { string identifier = (string)screen.Metadata; DownloadedItem item; if( !game.AsyncDownloader.TryGetItem( identifier, out item ) || item.Data == null ) return; long contentLength = (long)item.Data; if( contentLength <= 0 ) return; string url = identifier.Substring( 3 ); float contentLengthMB = (contentLength / 1024f / 1024f ); string address = url; if( url.StartsWith( "https://" ) ) address = url.Substring( 8 ); if( url.StartsWith( "http://" ) ) address = url.Substring( 7 ); screen.SetText( "Do you want to download the server's texture pack?", "Texture pack url:", address, "Download size: " + contentLengthMB.ToString( "F3" ) + " MB" ); } protected internal void RetrieveTexturePack( string url ) { if( !game.AcceptedUrls.HasEntry( url ) && !game.DeniedUrls.HasEntry( url ) ) { game.AsyncDownloader.RetrieveContentLength( url, true, "CL_" + url ); string address = url; if( url.StartsWith( "https://" ) ) address = url.Substring( 8 ); if( url.StartsWith( "http://" ) ) address = url.Substring( 7 ); game.Gui.ShowWarning( new WarningScreen( game, "CL_" + url, true, true, "Do you want to download the server's texture pack?", DownloadTexturePack, null, WarningScreenTick, "Texture pack url:", address, "Download size: Determining..." ) ); } else { DownloadTexturePack( url ); } } void DownloadTexturePack( WarningScreen screen ) { DownloadTexturePack( ((string)screen.Metadata).Substring( 3 ) ); } void DownloadTexturePack( string url ) { if( game.DeniedUrls.HasEntry( url ) ) return; DateTime lastModified = TextureCache.GetLastModified( url, game.LastModified ); string etag = TextureCache.GetETag( url, game.ETags ); if( url.Contains( ".zip" ) ) game.AsyncDownloader.DownloadData( url, true, "texturePack", lastModified, etag ); else game.AsyncDownloader.DownloadImage( url, true, "terrain", lastModified, etag ); } protected void CheckAsyncResources() { DownloadedItem item; if( game.AsyncDownloader.TryGetItem( "terrain", out item ) ) { TexturePackExtractor.ExtractTerrainPng( game, item ); } if( game.AsyncDownloader.TryGetItem( "texturePack", out item ) ) { TexturePackExtractor.ExtractTexturePack( game, item ); } } #endregion } }