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; MiniYaml parent;
allUnits.TryGetValue( inherits.Value, out parent ); allUnits.TryGetValue( inherits.Value, out parent );
if( parent == null ) if (parent == null)
return null; throw new InvalidOperationException(
"Bogus inheritance -- actor type {0} does not exist".F(inherits.Value));
return parent; return parent;
} }

View File

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

View File

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