source: trunk/ServerList.pas

Last change on this file was 346, checked in by chronos, 7 months ago
  • Fixed: Build on Linux.
  • Modified: Improved internal code structure.
File size: 3.2 KB
Line 
1unit ServerList;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, XMLConf;
7
8type
9
10 { TServerInfo }
11
12 TServerInfo = class
13 Name: string;
14 Address: string;
15 Port: Word;
16 PlayersCountCurrent: Integer;
17 PlayersCountMax: Integer;
18 Password: string;
19 MapName: string;
20 Latency: TTime;
21 procedure Assign(Source: TServerInfo);
22 constructor Create;
23 end;
24
25 { TServerInfos }
26
27 TServerInfos = class(TObjectList<TServerInfo>)
28 procedure Assign(Source: TServerInfos);
29 end;
30
31 { TServerList }
32
33 TServerList = class
34 Name: string;
35 Address: string;
36 Items: TServerInfos;
37 procedure Assign(Source: TServerList);
38 constructor Create;
39 destructor Destroy; override;
40 procedure LoadConfig(Config: TXmlConfig; Path: string);
41 procedure SaveConfig(Config: TXmlConfig; Path: string);
42 end;
43
44
45implementation
46
47uses
48 GameServer;
49
50{ TServerInfo }
51
52procedure TServerInfo.Assign(Source: TServerInfo);
53begin
54 Name := Source.Name;
55 Address := Source.Address;
56 Port := Source.Port;
57 PlayersCountCurrent := Source.PlayersCountCurrent;
58 PlayersCountMax := Source.PlayersCountMax;
59 Password := Source.Password;
60 MapName := Source.MapName;
61 Latency := Source.Latency;
62end;
63
64constructor TServerInfo.Create;
65begin
66 Port := DefaultServerPort;
67end;
68
69{ TServerInfos }
70
71procedure TServerInfos.Assign(Source: TServerInfos);
72var
73 I: Integer;
74begin
75 while Count > Source.Count do
76 Delete(Count - 1);
77 while Count < Source.Count do
78 Add(TServerInfo.Create);
79 for I := 0 to Count - 1 do begin
80 Items[I].Assign(Source.Items[I]);
81 end;
82end;
83
84{ TServerList }
85
86procedure TServerList.Assign(Source: TServerList);
87begin
88 Name := Source.Name;
89 Address := Source.Address;
90 Items.Assign(Source.Items);
91end;
92
93constructor TServerList.Create;
94begin
95 Items := TServerInfos.Create;
96end;
97
98destructor TServerList.Destroy;
99begin
100 FreeAndNil(Items);
101 inherited;
102end;
103
104procedure TServerList.LoadConfig(Config: TXmlConfig; Path: string);
105var
106 I: Integer;
107 Server: TServerInfo;
108begin
109 Items.Clear;
110 Items.Count := Config.GetValue(UnicodeString(Path + '/Count'), 0);
111 for I := 0 to Items.Count - 1 do begin
112 Server := TServerInfo.Create;
113 Server.Name := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), ''));
114 Server.Address := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), ''));
115 Server.Port := Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), 0);
116 Items[I] := Server;
117 end;
118end;
119
120procedure TServerList.SaveConfig(Config: TXmlConfig; Path: string);
121var
122 I: Integer;
123 Server: TServerInfo;
124begin
125 Config.SetValue(UnicodeString(Path + '/Count'), Items.Count);
126 for I := 0 to Items.Count - 1 do begin
127 Server := Items[I];
128 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), UnicodeString(Server.Name));
129 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), UnicodeString(Server.Address));
130 Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), Server.Port);
131 end;
132end;
133
134end.
135
Note: See TracBrowser for help on using the repository browser.