1 | unit ServerList;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, XMLConf;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
45 | implementation
|
---|
46 |
|
---|
47 | uses
|
---|
48 | GameServer;
|
---|
49 |
|
---|
50 | { TServerInfo }
|
---|
51 |
|
---|
52 | procedure TServerInfo.Assign(Source: TServerInfo);
|
---|
53 | begin
|
---|
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;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | constructor TServerInfo.Create;
|
---|
65 | begin
|
---|
66 | Port := DefaultServerPort;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | { TServerInfos }
|
---|
70 |
|
---|
71 | procedure TServerInfos.Assign(Source: TServerInfos);
|
---|
72 | var
|
---|
73 | I: Integer;
|
---|
74 | begin
|
---|
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;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | { TServerList }
|
---|
85 |
|
---|
86 | procedure TServerList.Assign(Source: TServerList);
|
---|
87 | begin
|
---|
88 | Name := Source.Name;
|
---|
89 | Address := Source.Address;
|
---|
90 | Items.Assign(Source.Items);
|
---|
91 | end;
|
---|
92 |
|
---|
93 | constructor TServerList.Create;
|
---|
94 | begin
|
---|
95 | Items := TServerInfos.Create;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | destructor TServerList.Destroy;
|
---|
99 | begin
|
---|
100 | FreeAndNil(Items);
|
---|
101 | inherited;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TServerList.LoadConfig(Config: TXmlConfig; Path: string);
|
---|
105 | var
|
---|
106 | I: Integer;
|
---|
107 | Server: TServerInfo;
|
---|
108 | begin
|
---|
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;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TServerList.SaveConfig(Config: TXmlConfig; Path: string);
|
---|
121 | var
|
---|
122 | I: Integer;
|
---|
123 | Server: TServerInfo;
|
---|
124 | begin
|
---|
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;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | end.
|
---|
135 |
|
---|