-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuddlyworld.pas
252 lines (229 loc) · 6.22 KB
/
cuddlyworld.pas
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
program cuddlyworld;
uses
{$IFDEF DEBUG} debug, {$ENDIF}
sysutils, baseunix, client, corenetwork, exceptions,
storable,
physics, world, locations, player,
cuddlycamp;
type
TMain = class
constructor Create();
destructor Destroy(); override;
procedure Run(); virtual; abstract;
procedure ReportException(E: Exception);
protected
FWorld: TWorld;
FOldReportExceptionMethod: TReportExceptionEvent;
procedure InitialiseWorld();
procedure SaveWorld();
{$IFDEF DEBUG}
procedure ReportStatus(Perspective: TPlayer);
{$ENDIF}
end;
TNetworkMain = class(TMain)
constructor Create(Port: Word);
destructor Destroy(); override;
procedure Run(); override;
protected
FServer: TCuddlyWorldServer;
end;
TCommandLineMain = class(TMain)
constructor Create(Username, Password: UTF8String);
destructor Destroy(); override;
procedure Run(); override;
procedure HandleForceDisconnect();
procedure HandleAvatarMessage(Message: UTF8String);
protected
FPlayer: TPlayer;
end;
var
Aborted: Boolean = False;
procedure SigIntHandler(Signal: Longint; Info: PSigInfo; Context: PSigContext); cdecl;
begin
Aborted := True;
end;
constructor TMain.Create();
var
NewAction: PSigActionRec;
begin
Writeln('CuddlyWorld initializing...');
inherited;
{$IFDEF DEBUG}
Writeln('CuddlyWorld debugging enabled.');
player.StatusReport := @Self.ReportStatus;
{$ENDIF}
InitialiseWorld();
New(NewAction);
if (not Assigned(NewAction)) then
OutOfMemoryError();
try
NewAction^.sa_handler := @SigIntHandler;
NewAction^.sa_flags := SA_ONESHOT;
{$ifdef Linux} NewAction^.sa_restorer := nil; {$endif}
FillByte(NewAction^.sa_mask, SizeOf(NewAction^.sa_mask), 0);
if (fpSigAction(SIGINT, NewAction, nil) <> 0) then
raise EKernelError.Create(fpGetErrNo);
finally
Dispose(NewAction);
end;
FOldReportExceptionMethod := SetReportExceptionMethod(@Self.ReportException);
end;
procedure TMain.InitialiseWorld();
begin
FWorld := ReadObjectFromFile(kWorldFileName) as TWorld;
if (not Assigned(FWorld)) then
raise Exception.Create('World not found. Run genesis to create ' + kWorldFileName + ' before running cuddlyworld.');
end;
procedure TMain.SaveWorld();
begin
StoreObjectToFile(kWorldFileName, FWorld, kSaveDataVersion);
FWorld.Saved();
end;
destructor TMain.Destroy();
begin
{$IFDEF DEBUG}
player.StatusReport := nil;
{$ENDIF}
FWorld.Free();
SetReportExceptionMethod(FOldReportExceptionMethod);
inherited;
end;
procedure TMain.ReportException(E: Exception);
begin
Writeln(E.Message);
Dump_Stack(Output, Get_Frame);
end;
{$IFDEF DEBUG}
procedure TMain.ReportStatus(Perspective: TPlayer);
begin
Perspective.SendRawMessage('Players: ' + IntToStr(FWorld.GetPlayerCount()));
end;
{$ENDIF}
constructor TNetworkMain.Create(Port: Word);
begin
inherited Create();
FServer := TCuddlyWorldServer.Create(Port, FWorld);
end;
destructor TNetworkMain.Destroy();
begin
FServer.Free();
inherited;
end;
procedure TNetworkMain.Run();
begin
Writeln('CuddlyWorld running...');
repeat
FServer.Select(timeoutForever);
FWorld.CheckForDisconnectedPlayers();
EmptyDisposalQueue();
if (FWorld.Dirty) then
SaveWorld();
until Aborted;
Writeln('CuddlyWorld aborted');
end;
constructor TCommandLineMain.Create(Username, Password: UTF8String);
begin
inherited Create();
FPlayer := FWorld.GetPlayer(Username) as TPlayer;
if (Assigned(FPlayer)) then
begin
if (FPlayer.GetPassword() = Password) then
begin
FPlayer.Adopt(@Self.HandleAvatarMessage, @Self.HandleForceDisconnect);
Writeln('Logged in as ' + FPlayer.GetDefiniteName(nil) + '.');
Writeln('');
FPlayer.DoLook();
FPlayer.DoInventory();
Writeln('');
end
else
begin
raise Exception.Create('Wrong Password');
end;
end
else
begin
FPlayer := TPlayer.Create(Username, Password, pSingularThey);
FPlayer.Adopt(@Self.HandleAvatarMessage, @Self.HandleForceDisconnect);
FWorld.AddPlayer(FPlayer); { this puts it into the world }
FPlayer.AnnounceAppearance();
Writeln('Welcome to CuddlyWorld, ' + FPlayer.GetDefiniteName(nil) + '.');
Writeln('');
FPlayer.DoLook();
Writeln('');
end;
end;
procedure TCommandLineMain.HandleAvatarMessage(Message: UTF8String);
begin
Writeln(Message);
end;
procedure TCommandLineMain.HandleForceDisconnect();
begin
end;
destructor TCommandLineMain.Destroy();
begin
FPlayer.Abandon();
inherited;
end;
procedure TCommandLineMain.Run();
var
S: UTF8String;
begin
repeat
Write('> ');
Readln(S);
FWorld.Perform(S, FPlayer);
EmptyDisposalQueue();
SaveWorld();
until Aborted;
end;
const
PortVariable = 'CUDDLYWORLDPORT';
DefaultPort = 10000;
var
Main: TMain;
Port, Error: Word;
begin
{$IFDEF DEBUG}
RegisterStorableClassSynonym('TTestWorld', TWorld);
{$ENDIF}
try
if (ParamCount() > 0) then
begin
if (ParamCount() <> 2) then
begin
Writeln('Usage: cuddlyworld [username password]');
Writeln('Without arguments, runs a WebSocket server on the port given by the environment variable ', PortVariable ,'.');
Writeln('The default port if the variable is not set, is ', DefaultPort, '.');
Writeln('With arguments, runs locally in single-user mode using the given username and password.');
end
else
begin
Main := TCommandLineMain.Create(ParamStr(1), ParamStr(2));
end;
end
else
begin
Val(GetEnvironmentVariable(PortVariable), Port, Error);
if (Error <> 0) then
begin
Port := DefaultPort;
end;
Main := TNetworkMain.Create(Port);
Writeln('Server active on port ', Port, '.');
end;
try
Main.Run();
finally
Main.Free();
end;
except
on E: Exception do
begin
Writeln(E.Message);
DumpExceptionBackTrace(Output);
end;
end;
end.