diff --git a/ServerBrowserMod.cs b/ServerBrowserMod.cs index 0da00e8..010f0db 100644 --- a/ServerBrowserMod.cs +++ b/ServerBrowserMod.cs @@ -4,6 +4,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ServerBrowser.UI; +using System; using Terraria; using Terraria.ModLoader; @@ -68,12 +69,21 @@ class ServerBrowserMod : Mod { //////////////// + private bool HasErrored = false; + private void DrawBrowser( GameTime game_time ) { if( !Main.gameMenu ) { return; } Main.spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Main.UIScaleMatrix ); - this.Dialog.Draw( Main.spriteBatch ); + try { + this.Dialog.Draw( Main.spriteBatch ); + } catch( Exception e ) { + if( !this.HasErrored ) { + this.HasErrored = true; + LogHelpers.Log( e.ToString() ); + } + } Vector2 bonus = Main.DrawThickCursor( false ); Main.DrawCursor( bonus, false ); diff --git a/UI/UIServerBrowserDialog.cs b/UI/UIServerBrowserDialog.cs index 18a8556..a4bc42f 100644 --- a/UI/UIServerBrowserDialog.cs +++ b/UI/UIServerBrowserDialog.cs @@ -19,6 +19,7 @@ partial class UIServerBrowserDialog : UIDialog { private UITextPanelButton SortByPlayersButton; private UITextField FilterByModInput; private UITextField FilterByNameInput; + private UIText ServerListErr; private UIServerBrowserList ServerList; @@ -92,7 +93,7 @@ partial class UIServerBrowserDialog : UIDialog { this.SortByPingButton = new UITextPanelButton( this.Theme, "Sort by ping", 1.2f ); this.SortByPingButton.Top.Set( 12f, 0f ); - this.SortByPingButton.Left.Set( 128f + 8f, 0f ); + this.SortByPingButton.Left.Set( 128f + 12f, 0f ); this.SortByPingButton.Width.Set( 128f, 0f ); this.SortByPingButton.Height.Set( 32f, 0f ); this.SortByPingButton.OnClick += delegate ( UIMouseEvent evt, UIElement listening_element ) { @@ -171,6 +172,11 @@ partial class UIServerBrowserDialog : UIDialog { this.ServerList.Initialize(); this.InnerContainer.Append( (UIElement)this.ServerList ); + this.ServerListErr = new UIText( "", 1f ); + this.ServerListErr.Top.Set( 60f, 0f ); + this.ServerListErr.Left.Set( -128, 0.5f ); + this.InnerContainer.Append( (UIElement)this.ServerListErr ); + //// var modrecommend_url = new UIWebUrl( this.Theme, "Trouble choosing mods?", "https://sites.google.com/site/terrariamodsuggestions/", true, 0.86f ); @@ -192,8 +198,16 @@ partial class UIServerBrowserDialog : UIDialog { this.Close(); }; + Action on_success = () => { + this.ServerListErr.SetText( "" ); + }; + + Action on_err = () => { + this.ServerListErr.SetText( "Server busy. Try again later." ); + }; + base.OnActivate(); - this.ServerList.RefreshServerList( pre_join ); + this.ServerList.RefreshServerList( pre_join, on_success, on_err ); } diff --git a/UI/UIServerBrowserList.cs b/UI/UIServerBrowserList.cs index f640723..0069d8c 100644 --- a/UI/UIServerBrowserList.cs +++ b/UI/UIServerBrowserList.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Terraria; using Terraria.GameContent.UI.Elements; using Terraria.UI; @@ -20,23 +19,25 @@ class UIServerBrowserList : UIPanel { public static UIServerDataElement[] GetListFromJsonStr( UITheme theme, string json_str, Func comparator, - Action pre_join ) { + Action pre_join, Action on_err ) { + UIServerDataElement[] list = new UIServerDataElement[0]; + try { var data = JsonConfig>.Deserialize( json_str ); - UIServerDataElement[] list = new UIServerDataElement[data.Count]; + list = new UIServerDataElement[data.Count]; int i = 0; foreach( var kv in data ) { list[i++] = new UIServerDataElement( theme, kv.Value, comparator, pre_join ); } - - return list; } catch( Exception e ) { int len = json_str.Length > 64 ? 64 : json_str.Length; LogHelpers.Log( "GetListFromJsonStr - " + e.ToString() + " - " + json_str.Substring( 0, len ) ); + + on_err(); } - return new UIServerDataElement[0]; + return list; } @@ -96,14 +97,13 @@ class UIServerBrowserList : UIPanel { if( this.MyList.Count > 0 ) { this.MyList.Clear(); } - this.MyList.AddRange( list ); - this.MyList.Recalculate(); } + this.MyList.Recalculate(); } - public void RefreshServerList( Action pre_join ) { + public void RefreshServerList( Action pre_join, Action on_success, Action on_err ) { lock( UIServerBrowserList.MyLock ) { if( this.MyList.Count > 0 ) { this.MyList.Clear(); @@ -112,7 +112,7 @@ class UIServerBrowserList : UIPanel { } Action list_ready = delegate ( string output ) { - this.FullServerList = UIServerBrowserList.GetListFromJsonStr( this.Theme, output, this.Comparator, pre_join ); + this.FullServerList = UIServerBrowserList.GetListFromJsonStr( this.Theme, output, this.Comparator, pre_join, on_err ); if( this.FullServerList.Count > 0 ) { lock( UIServerBrowserList.MyLock ) { @@ -120,6 +120,7 @@ class UIServerBrowserList : UIPanel { this.Recalculate(); } } + on_success(); }; Action list_error = delegate ( Exception e, string output ) { @@ -135,7 +136,9 @@ class UIServerBrowserList : UIPanel { //////////////// internal void UpdateOrder() { - this.MyList.UpdateOrder(); + lock( UIServerBrowserList.MyLock ) { + this.MyList.UpdateOrder(); + } } private int Comparator( UIServerDataElement prev, UIServerDataElement next ) { @@ -187,14 +190,7 @@ class UIServerBrowserList : UIPanel { var server_data_elem = (UIServerDataElement)item; this.ModListPopup.SetServer( server_data_elem.Data ); - - CalculatedStyle dim = this.ModListPopup.GetDimensions(); - float top = Main.mouseY + 16f; - float left = Main.mouseX - ( dim.Width * 0.5f ); - - this.ModListPopup.Top.Set( top, 0f ); - this.ModListPopup.Left.Set( left, 0f ); - this.ModListPopup.Recalculate(); + break; } if( is_hovering_server ) { diff --git a/UI/UIServerDataElement.cs b/UI/UIServerDataElement.cs index dce72c4..6ce858b 100644 --- a/UI/UIServerDataElement.cs +++ b/UI/UIServerDataElement.cs @@ -61,6 +61,26 @@ class UIServerDataElement : UIPanel { return way; } + //////////////// + + public static string[] GetMotdLines( string motd, int line_width ) { + string[] motd_chunks; + + if( motd.Length > line_width ) { + int chunks = (int)Math.Ceiling( (float)motd.Length / (float)line_width ); + + motd_chunks = new string[chunks]; + for( int i = 0; i < motd_chunks.Length; i++ ) { + int width = ( i + 1 ) * line_width <= motd.Length ? line_width : motd.Length - ( i * line_width ); + motd_chunks[i] = motd.Substring( i * line_width, width ); + } + } else { + motd_chunks = new string[] { motd }; + } + + return motd_chunks; + } + //////////////// @@ -161,12 +181,18 @@ class UIServerDataElement : UIPanel { //// if( this.Data.Motd != "" ) { - var motd_label = new UIText( this.Data.Motd, 0.8f ); - motd_label.Left.Set( UIServerDataElement.MotdLabelLeft, 0f ); - motd_label.Top.Set( UIServerDataElement.MotdLabelTop, 0f ); - this.Append( (UIElement)motd_label ); + int line_height = 24; + string[] motd_chunks = UIServerDataElement.GetMotdLines( this.Data.Motd, 96 ); + + for( int i=0; i Main.screenHeight ) { + top = Main.screenHeight - dim.Height; + left = (Main.mouseX - 16f) - dim.Width; + } + + this.Top.Set( top, 0f ); + this.Left.Set( left, 0f ); + this.Recalculate(); + } + + public override void Draw( SpriteBatch sb ) { if( this.CurrentEntry == null ) { return; } + this.UpdatePosition(); + base.Draw( sb ); float x = this.Left.Pixels + 8f; diff --git a/build.txt b/build.txt index 56e9a7d..f77f772 100644 --- a/build.txt +++ b/build.txt @@ -1,5 +1,5 @@ author = hamstar -version = 1.0.0.1 +version = 1.0.1 displayName = Server Browser modReferences = HamstarHelpers@1.4.3 buildIgnore = *.csproj, *.user, *.bat, obj\*, bin\*, .vs\*, .git\* diff --git a/v1.0.0 UI (blue).png b/v1.0.0 UI (blue).png new file mode 100644 index 0000000..948e8c7 Binary files /dev/null and b/v1.0.0 UI (blue).png differ diff --git a/v1.0.0 UI (red).png b/v1.0.0 UI (red).png new file mode 100644 index 0000000..c6d3ef1 Binary files /dev/null and b/v1.0.0 UI (red).png differ