source: trunk/Packages/Common/URegistry.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 3.6 KB
Line 
1unit URegistry;
2
3{$MODE Delphi}
4
5interface
6
7uses
8 Classes, Registry;
9
10type
11 TRegistryRoot = (rrKeyClassesRoot = HKEY($80000000),
12 rrKeyCurrentUser = HKEY($80000001),
13 rrKeyLocalMachine = HKEY($80000002),
14 rrKeyUsers = HKEY($80000003),
15 rrKeyPerformanceData = HKEY($80000004),
16 rrKeyCurrentConfig = HKEY($80000005),
17 rrKeyDynData = HKEY($80000006));
18
19 { TRegistryContext }
20
21 TRegistryContext = record
22 RootKey: HKEY;
23 Key: string;
24 class operator Equal(A, B: TRegistryContext): Boolean;
25 end;
26
27 { TRegistryEx }
28
29 TRegistryEx = class(TRegistry)
30 private
31 function GetCurrentContext: TRegistryContext;
32 procedure SetCurrentContext(AValue: TRegistryContext);
33 public
34 function ReadBoolWithDefault(const Name: string;
35 DefaultValue: Boolean): Boolean;
36 function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
37 function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
38 function ReadFloatWithDefault(const Name: string;
39 DefaultValue: Double): Double;
40 function DeleteKeyRecursive(const Key: string): Boolean;
41 function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
42 property CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
43 end;
44
45function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
46
47
48implementation
49
50function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
51begin
52 Result.RootKey := RootKey;
53 Result.Key := Key;
54end;
55
56{ TRegistryContext }
57
58class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
59begin
60 Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
61end;
62
63{ TRegistryEx }
64
65function TRegistryEx.ReadIntegerWithDefault(const Name: string;
66 DefaultValue: Integer): Integer;
67begin
68 if ValueExists(Name) then Result := ReadInteger(Name)
69 else begin
70 WriteInteger(Name, DefaultValue);
71 Result := DefaultValue;
72 end;
73end;
74
75function TRegistryEx.ReadStringWithDefault(const Name: string;
76 DefaultValue: string): string;
77begin
78 if ValueExists(Name) then Result := ReadString(Name)
79 else begin
80 WriteString(Name, DefaultValue);
81 Result := DefaultValue;
82 end;
83end;
84
85function TRegistryEx.ReadFloatWithDefault(const Name: string;
86 DefaultValue: Double): Double;
87begin
88 if ValueExists(Name) then Result := ReadFloat(Name)
89 else begin
90 WriteFloat(Name, DefaultValue);
91 Result := DefaultValue;
92 end;
93end;
94
95function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
96var
97 SubKeys: TStringList;
98 I: Integer;
99begin
100 try
101 SubKeys := TStringList.Create;
102 if OpenKey(Key, False) and HasSubKeys then begin
103 GetKeyNames(SubKeys);
104 for I := 0 to SubKeys.Count - 1 do
105 DeleteKeyRecursive(Key + '\' + SubKeys[I]);
106 end;
107 Result := DeleteKey(Key);
108 finally
109 SubKeys.Free;
110 end;
111end;
112
113function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
114begin
115 {$IFDEF Linux}
116 CloseKey;
117 {$ENDIF}
118 Result := inherited OpenKey(Key, CanCreate);
119end;
120
121function TRegistryEx.GetCurrentContext: TRegistryContext;
122begin
123 Result.Key := CurrentPath;
124 Result.RootKey := RootKey;
125end;
126
127procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
128begin
129 RootKey := AValue.RootKey;
130 OpenKey(AValue.Key, True);
131end;
132
133function TRegistryEx.ReadBoolWithDefault(const Name: string;
134 DefaultValue: Boolean): Boolean;
135begin
136 if ValueExists(Name) then Result := ReadBool(Name)
137 else begin
138 WriteBool(Name, DefaultValue);
139 Result := DefaultValue;
140 end;
141end;
142
143end.
Note: See TracBrowser for help on using the repository browser.