-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBaseGame.cs
More file actions
110 lines (88 loc) · 2.17 KB
/
BaseGame.cs
File metadata and controls
110 lines (88 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
namespace Mocha;
public class BaseGame : IGame
{
public static BaseGame Current { get; set; }
public BaseGame()
{
Current = this;
Event.Register( this );
Event.RegisterStatics();
Event.Run( Event.Game.LoadAttribute.Name );
}
/// <summary>
/// This contains a list of user methods that have thrown exceptions, we
/// don't want to keep calling them if they're problematic so anything in here
/// should be checked and not called again.
/// </summary>
private List<int> FailedMethods { get; } = new();
private void TryCallMethodOnEntity( string methodName )
{
Actor.All.ToList().ForEach( entity =>
{
var method = entity.GetType().GetMethod( methodName )!;
var methodHash = HashCode.Combine( method, entity );
try
{
// Has this method already called an exception?
// If so, don't call it again!
if ( FailedMethods.Contains( methodHash ) )
return;
method.Invoke( entity, null );
}
catch ( Exception ex )
{
var targetEx = ex.InnerException ?? ex;
Notify.AddError( targetEx.GetType().Name, targetEx.Message, FontAwesome.Exclamation );
Log.Error( targetEx );
FailedMethods.Add( methodHash );
}
} );
}
public void FrameUpdate()
{
TryCallMethodOnEntity( "FrameUpdate" );
}
public void Update()
{
if ( Core.IsClient )
{
// HACK: Clear DebugOverlay here because doing it
// per-frame doesn't play nice with tick-based
// entries (needs fix)
DebugOverlay.screenTextList.Clear();
DebugOverlay.currentLine = 0;
}
DebugOverlay.ScreenText( $"BaseGame.Update assembly {GetType().Assembly.GetHashCode()}" );
// Call tick logic on all entities
TryCallMethodOnEntity( "Update" );
// Fire tick event
Event.Run( Event.TickAttribute.Name );
}
public void Shutdown()
{
OnShutdown();
}
public void Startup()
{
OnStartup();
}
#region "Public API"
/// <summary>
/// Called on the server when the game starts up
/// </summary>
public virtual void OnStartup()
{
}
/// <summary>
/// Called on the server when the game shuts down
/// </summary>
public virtual void OnShutdown()
{
}
#endregion
[Event.Game.Hotload]
public void OnHotload()
{
FailedMethods.Clear();
}
}