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 = class(TObjectList<TServerInfo>)
|
---|
26 | end;
|
---|
27 |
|
---|
28 | { TServerList }
|
---|
29 |
|
---|
30 | TServerList = class
|
---|
31 | Name: string;
|
---|
32 | Address: string;
|
---|
33 | Items: TServerInfos;
|
---|
34 | procedure Assign(Source: TServerList);
|
---|
35 | constructor Create;
|
---|
36 | destructor Destroy; override;
|
---|
37 | procedure LoadConfig(Config: TXmlConfig; Path: string);
|
---|
38 | procedure SaveConfig(Config: TXmlConfig; Path: string);
|
---|
39 | end;
|
---|
40 |
|
---|
41 |
|
---|
42 | implementation
|
---|
43 |
|
---|
44 | uses
|
---|
45 | GameServer;
|
---|
46 |
|
---|
47 | { TServerInfo }
|
---|
48 |
|
---|
49 | procedure TServerInfo.Assign(Source: TServerInfo);
|
---|
50 | begin
|
---|
51 | Name := Source.Name;
|
---|
52 | Address := Source.Address;
|
---|
53 | Port := Source.Port;
|
---|
54 | PlayersCountCurrent := Source.PlayersCountCurrent;
|
---|
55 | PlayersCountMax := Source.PlayersCountMax;
|
---|
56 | Password := Source.Password;
|
---|
57 | MapName := Source.MapName;
|
---|
58 | Latency := Source.Latency;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | constructor TServerInfo.Create;
|
---|
62 | begin
|
---|
63 | Port := DefaultServerPort;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | { TServerList }
|
---|
67 |
|
---|
68 | procedure TServerList.Assign(Source: TServerList);
|
---|
69 | var
|
---|
70 | I: Integer;
|
---|
71 | begin
|
---|
72 | while Items.Count > Source.Items.Count do
|
---|
73 | Items.Delete(Items.Count - 1);
|
---|
74 | while Items.Count < Source.Items.Count do
|
---|
75 | Items.Add(TServerInfo.Create);
|
---|
76 | for I := 0 to Items.Count - 1 do begin
|
---|
77 | Items[I].Assign(Source.Items[I]);
|
---|
78 | end;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | constructor TServerList.Create;
|
---|
82 | begin
|
---|
83 | Items := TServerInfos.Create;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | destructor TServerList.Destroy;
|
---|
87 | begin
|
---|
88 | FreeAndNil(Items);
|
---|
89 | inherited;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TServerList.LoadConfig(Config: TXmlConfig; Path: string);
|
---|
93 | var
|
---|
94 | I: Integer;
|
---|
95 | Server: TServerInfo;
|
---|
96 | begin
|
---|
97 | Items.Clear;
|
---|
98 | Items.Count := Config.GetValue(UnicodeString(Path + '/Count'), 0);
|
---|
99 | for I := 0 to Items.Count - 1 do begin
|
---|
100 | Server := TServerInfo.Create;
|
---|
101 | Server.Name := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), ''));
|
---|
102 | Server.Address := string(Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), ''));
|
---|
103 | Server.Port := Config.GetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), 0);
|
---|
104 | Items[I] := Server;
|
---|
105 | end;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TServerList.SaveConfig(Config: TXmlConfig; Path: string);
|
---|
109 | var
|
---|
110 | I: Integer;
|
---|
111 | Server: TServerInfo;
|
---|
112 | begin
|
---|
113 | Config.SetValue(UnicodeString(Path + '/Count'), Items.Count);
|
---|
114 | for I := 0 to Items.Count - 1 do begin
|
---|
115 | Server := Items[I];
|
---|
116 | Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Name'), UnicodeString(Server.Name));
|
---|
117 | Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Address'), UnicodeString(Server.Address));
|
---|
118 | Config.SetValue(UnicodeString(Path + '/Item' + IntToStr(I) + '/Port'), Server.Port);
|
---|
119 | end;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | end.
|
---|
123 |
|
---|