make it crash nicely when you inherit from a bogus actor type

This commit is contained in:
Chris Forbes
2011-03-30 20:48:09 +13:00
committed by Chris Forbes
parent cee021ef17
commit 1af23079eb
4 changed files with 420 additions and 403 deletions

View File

@@ -40,8 +40,9 @@ namespace OpenRA
MiniYaml parent;
allUnits.TryGetValue( inherits.Value, out parent );
if( parent == null )
return null;
if (parent == null)
throw new InvalidOperationException(
"Bogus inheritance -- actor type {0} does not exist".F(inherits.Value));
return parent;
}

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Widgets
// Common Funcs that most widgets will want
public Func<MouseInput, bool> OnMouseDown = mi => false;
public Func<MouseInput, bool> OnMouseUp = mi => false;
public Action<MouseInput> OnMouseMove = mi => {};
public Action<MouseInput> OnMouseMove = mi => { };
public Func<KeyInput, bool> OnKeyPress = e => false;
public Func<bool> IsVisible;
@@ -74,7 +74,7 @@ namespace OpenRA.Widgets
IsVisible = widget.IsVisible;
foreach(var child in widget.Children)
foreach (var child in widget.Children)
AddChild(child.Clone());
}
@@ -123,13 +123,18 @@ namespace OpenRA.Widgets
public void PostInit(Dictionary<string, object> args)
{
if( Delegate != null )
{
if (Delegate == null)
return;
args["widget"] = this;
Game.modData.ObjectCreator.CreateObject<IWidgetDelegate>(Delegate, args);
var iwd = Game.modData.ObjectCreator.CreateObject<IWidgetDelegate>(Delegate, args)
as IWidgetDelegateEx;
if (iwd != null)
iwd.Init();
args.Remove("widget");
}
}
public virtual Rectangle EventBounds { get { return RenderBounds; } }
public virtual Rectangle GetEventBounds()
@@ -206,7 +211,7 @@ namespace OpenRA.Widgets
public bool HandleMouseInputOuter(MouseInput mi)
{
// Are we able to handle this event?
if (!(Focused || (IsVisible() && GetEventBounds().Contains(mi.Location.X,mi.Location.Y))))
if (!(Focused || (IsVisible() && GetEventBounds().Contains(mi.Location.X, mi.Location.Y))))
return false;
// Send the event to the deepest children first and bubble up if unhandled
@@ -282,7 +287,7 @@ namespace OpenRA.Widgets
public virtual void AddChild(Widget child)
{
child.Parent = this;
Children.Add( child );
Children.Add(child);
}
public virtual void RemoveChild(Widget child) { Children.Remove(child); }
public virtual void RemoveChildren() { Children.Clear(); }
@@ -304,27 +309,27 @@ namespace OpenRA.Widgets
public T GetWidget<T>(string id) where T : Widget
{
var widget = GetWidget(id);
return (widget != null)? (T) widget : null;
return (widget != null) ? (T)widget : null;
}
public static void CloseWindow()
{
RootWidget.Children.Remove( WindowList.Pop() );
if( WindowList.Count > 0 )
rootWidget.Children.Add( WindowList.Peek() );
RootWidget.Children.Remove(WindowList.Pop());
if (WindowList.Count > 0)
rootWidget.Children.Add(WindowList.Peek());
}
public static Widget OpenWindow( string id )
public static Widget OpenWindow(string id)
{
return OpenWindow( id, new Dictionary<string, object>() );
return OpenWindow(id, new Dictionary<string, object>());
}
public static Widget OpenWindow(string id, Dictionary<string, object> args )
public static Widget OpenWindow(string id, Dictionary<string, object> args)
{
var window = Game.modData.WidgetLoader.LoadWidget( args, rootWidget, id );
if( WindowList.Count > 0 )
rootWidget.Children.Remove( WindowList.Peek() );
WindowList.Push( window );
var window = Game.modData.WidgetLoader.LoadWidget(args, rootWidget, id);
if (WindowList.Count > 0)
rootWidget.Children.Remove(WindowList.Peek());
WindowList.Push(window);
return window;
}
@@ -339,16 +344,19 @@ namespace OpenRA.Widgets
}
}
public class ContainerWidget : Widget {
public class ContainerWidget : Widget
{
public Func<string> GetBackground;
public string Background = null;
public ContainerWidget() : base()
public ContainerWidget()
: base()
{
GetBackground = () => Background;
}
public ContainerWidget(ContainerWidget other) : base(other)
public ContainerWidget(ContainerWidget other)
: base(other)
{
Background = other.Background;
GetBackground = other.GetBackground;
@@ -358,11 +366,16 @@ namespace OpenRA.Widgets
{
var bg = GetBackground();
if (bg != null)
WidgetUtils.DrawPanel(bg, RenderBounds );
WidgetUtils.DrawPanel(bg, RenderBounds);
}
public override string GetCursor(int2 pos) { return null; }
public override Widget Clone() { return new ContainerWidget(this); }
}
public interface IWidgetDelegate { }
public interface IWidgetDelegateEx : IWidgetDelegate
{
void Init();
}
}

View File

@@ -23,7 +23,7 @@ using System.Drawing;
namespace OpenRA.Mods.RA.Widgets.Delegates
{
public class GameInitDelegate : IWidgetDelegate
public class GameInitDelegate : IWidgetDelegateEx
{
GameInitInfoWidget Info;
@@ -31,22 +31,25 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
public GameInitDelegate([ObjectCreator.Param] Widget widget)
{
Info = (widget as GameInitInfoWidget);
}
public void Init()
{
Game.ConnectionStateChanged += orderManager =>
{
Widget.CloseWindow();
switch( orderManager.Connection.ConnectionState )
switch (orderManager.Connection.ConnectionState)
{
case ConnectionState.PreConnecting:
Widget.OpenWindow("MAINMENU_BG");
break;
case ConnectionState.Connecting:
Widget.OpenWindow( "CONNECTING_BG",
new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } } );
Widget.OpenWindow("CONNECTING_BG",
new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } });
break;
case ConnectionState.NotConnected:
Widget.OpenWindow( "CONNECTION_FAILED_BG",
new Dictionary<string, object> { { "orderManager", orderManager } } );
Widget.OpenWindow("CONNECTION_FAILED_BG",
new Dictionary<string, object> { { "orderManager", orderManager } });
break;
case ConnectionState.Connected:
var lobby = Game.OpenWindow(orderManager.world, "SERVER_LOBBY");
@@ -60,7 +63,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
};
if (FileSystem.Exists(Info.TestFile))
ContinueLoading(widget);
ContinueLoading();
else
{
MainMenuButtonsDelegate.DisplayModSelector();
@@ -115,7 +118,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
Action onComplete = () =>
{
if (!error)
Game.RunAfterTick(() => ContinueLoading(Info));
Game.RunAfterTick(ContinueLoading);
};
if (Info.InstallMode == "ra")
@@ -151,19 +154,19 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var error = false;
Action<string> parseOutput = s =>
{
if (s.Substring(0,5) == "Error")
if (s.StartsWith("Error"))
{
error = true;
ShowDownloadError(window, s);
}
if (s.Substring(0,6) == "Status")
if (s.StartsWith("Status"))
window.GetWidget<LabelWidget>("STATUS").GetText = () => s.Substring(7).Trim();
};
Action onComplete = () =>
{
if (!error)
Game.RunAfterTick(() => ContinueLoading(Info));
Game.RunAfterTick(ContinueLoading);
};
Game.RunAfterTick(() => Game.Utilities.ExtractZipAsync(file, FileSystem.SpecialPackageRoot+Info.PackagePath, parseOutput, onComplete));
@@ -185,7 +188,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
}
}
void ContinueLoading(Widget widget)
void ContinueLoading()
{
Game.LoadShellMap();
Widget.RootWidget.RemoveChildren();