-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUDictionary.pas
78 lines (61 loc) · 1.69 KB
/
UDictionary.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
unit UDictionary;
interface
type
TDictionary = record
ID, LanguageName: string;
end;
const LST_DICTIONARY: array[0..1] of TDictionary = (
(ID: 'EN'; LanguageName: 'English'),
(ID: 'BR'; LanguageName: 'Portuguese Brazil')
);
function GetCurrentDictionaryIndex: Integer;
procedure LoadDictionaryLetters;
function GetRandomLetter: Char;
implementation
uses UVars, System.SysUtils, System.Classes, System.Types;
function GetCurrentDictionaryIndex: Integer;
var
I: Integer;
begin
for I := 0 to High(LST_DICTIONARY) do
if LST_DICTIONARY[I].ID = pubServerProps.DictionaryID then Exit(I);
raise Exception.Create('Internal: Dictionary not found by ID');
end;
procedure LoadDictionaryLetters;
var
R: TResourceStream;
S: TStringList;
I: Integer;
Name, Value: string;
Letters: string;
begin
S := TStringList.Create;
try
R := TResourceStream.Create(HInstance, 'DIC_'+pubServerProps.DictionaryID, RT_RCDATA);
try
S.LoadFromStream(R);
finally
R.Free;
end;
if S.Count=0 then
raise Exception.Create('Internal: No letters found in the resource');
for I := 0 to S.Count-1 do
begin
Name := S.Names[I];
Value := S.ValueFromIndex[I];
if Name.Length<>1 then
raise Exception.Create('Internal: Letter must contain exactly one character');
if StrToIntDef(Value, 0)<=0 then
raise Exception.Create('Internal: Letter occurrences number are invalid');
Letters := Letters + StringOfChar(Name[1], Value.ToInteger);
end;
finally
S.Free;
end;
pubServerProps.Letters := Letters;
end;
function GetRandomLetter: Char;
begin
Result := pubServerProps.Letters[Random(pubServerProps.Letters.Length)+1];
end;
end.