-
Notifications
You must be signed in to change notification settings - Fork 16
/
GameRunnerBase.cs
131 lines (118 loc) · 4.2 KB
/
GameRunnerBase.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Diagnostics;
using System.IO;
using AutomaticTypeMapper;
using EndlessClient.Initialization;
using EOLib.Config;
using EOLib.Graphics;
using EOLib.Localization;
using System;
#if !LINUX
using System.Windows.Forms;
#endif
namespace EndlessClient.GameExecution
{
public abstract class GameRunnerBase : IGameRunner
{
private readonly ITypeRegistry _registry;
private readonly string[] _args;
protected GameRunnerBase(ITypeRegistry registry, string[] args)
{
_registry = registry;
_args = args;
}
public virtual bool SetupDependencies()
{
_registry.RegisterDiscoveredTypes();
var initializers = _registry.ResolveAll<IGameInitializer>();
try
{
foreach (var initializer in initializers)
{
initializer.Initialize();
}
}
catch (ConfigLoadException cle)
{
ShowErrorMessage(cle.Message, "Error loading config file!");
return false;
}
catch (DataFileLoadException dfle)
{
ShowErrorMessage(dfle.Message, "Error loading data files!");
return false;
}
catch (DirectoryNotFoundException dnfe)
{
ShowErrorMessage(dnfe.Message, "Missing required directory");
return false;
}
catch (FileNotFoundException fnfe)
{
ShowErrorMessage(fnfe.Message, "Missing required file");
return false;
}
catch (LibraryLoadException lle)
{
var message =
$"There was an error loading GFX{(int) lle.WhichGFX:000}.EGF : {lle.WhichGFX}. Place all .GFX files in .\\gfx\\. The error message is:\n\n\"{lle.Message}\"";
ShowErrorMessage(message, "GFX Load Error");
return false;
}
for (int i = 0; i < _args.Length; ++i)
{
var arg = _args[i];
if (string.Equals(arg, "--host") && i < _args.Length - 1)
{
var host = _args[i + 1];
_registry.Resolve<IConfigurationRepository>()
.Host = host;
i++;
}
else if (string.Equals(arg, "--version") && i < _args.Length - 1)
{
var versionStr = _args[i + 1];
if (!byte.TryParse(versionStr, out var version))
{
Debug.WriteLine($"Version must be a byte (0-255).");
}
else
{
_registry.Resolve<IConfigurationRepository>()
.VersionBuild = version;
}
i++;
}
else if (string.Equals(arg, "--account_delay_ms") && i < _args.Length - 1)
{
var accountCreateTimeStr = _args[i + 1];
if (!int.TryParse(accountCreateTimeStr, out var accountCreateTime) || accountCreateTime < 2000)
{
Debug.WriteLine($"Account create timeout must be greater than or equal to 2000ms.");
}
else
{
_registry.Resolve<IConfigurationRepository>()
.AccountCreateTimeout = TimeSpan.FromMilliseconds(accountCreateTime);
}
i++;
}
else
{
Debug.WriteLine($"Unrecognized argument: {arg}. Will be ignored.");
}
}
return true;
}
private void ShowErrorMessage(string message, string caption)
{
#if !LINUX
MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
}
public virtual void RunGame()
{
var game = _registry.Resolve<IEndlessGame>();
game.Run();
}
}
}