2014-06-30 14:37:42 +00:00
|
|
|
unit Game.Registry;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
|
|
|
Game.Base;
|
|
|
|
|
|
|
|
type
|
|
|
|
TGameRegistryEnumerable = class(TObject)
|
|
|
|
public
|
|
|
|
function GetEnumerator: TEnumerator<TCustomGameClass>;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TGameRegistry = class(TObject)
|
|
|
|
protected
|
|
|
|
class var SRegisteredGames: TList<TCustomGameClass>;
|
|
|
|
class var SEnumerable: TGameRegistryEnumerable;
|
|
|
|
|
|
|
|
class procedure Initialize;
|
|
|
|
class procedure Finalize;
|
|
|
|
public
|
|
|
|
class function RegisteredGames: TGameRegistryEnumerable;
|
2014-06-30 16:07:40 +00:00
|
|
|
class function ByClassName(const AClassName: string): TCustomGameClass;
|
2014-06-30 14:37:42 +00:00
|
|
|
|
|
|
|
class procedure Register(AGameClass: TCustomGameClass);
|
|
|
|
class procedure Unregister(AGameClass: TCustomGameClass);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
|
|
|
|
{ TGameRegistry }
|
|
|
|
class procedure TGameRegistry.Initialize;
|
|
|
|
begin
|
|
|
|
SRegisteredGames := TList<TCustomGameClass>.Create;
|
|
|
|
SEnumerable := TGameRegistryEnumerable.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
class procedure TGameRegistry.Finalize;
|
|
|
|
begin
|
|
|
|
FreeAndNil(SEnumerable);
|
|
|
|
FreeAndNil(SRegisteredGames);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
class function TGameRegistry.RegisteredGames: TGameRegistryEnumerable;
|
|
|
|
begin
|
|
|
|
Result := SEnumerable;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-06-30 16:07:40 +00:00
|
|
|
class function TGameRegistry.ByClassName(const AClassName: string): TCustomGameClass;
|
|
|
|
var
|
|
|
|
gameClass: TCustomGameClass;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
|
|
|
|
for gameClass in RegisteredGames do
|
|
|
|
if SameText(gameClass.ClassName, AClassName) then
|
|
|
|
begin
|
|
|
|
Result := gameClass;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-06-30 14:37:42 +00:00
|
|
|
class procedure TGameRegistry.Register(AGameClass: TCustomGameClass);
|
|
|
|
begin
|
|
|
|
if not SRegisteredGames.Contains(AGameClass) then
|
|
|
|
SRegisteredGames.Add(AGameClass);
|
|
|
|
end;
|
|
|
|
|
|
|
|
class procedure TGameRegistry.Unregister(AGameClass: TCustomGameClass);
|
|
|
|
begin
|
|
|
|
SRegisteredGames.Remove(AGameClass);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TGameRegistryEnumerable }
|
|
|
|
function TGameRegistryEnumerable.GetEnumerator: TEnumerator<TCustomGameClass>;
|
|
|
|
begin
|
|
|
|
Result := TGameRegistry.SRegisteredGames.GetEnumerator;
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
TGameRegistry.Initialize;
|
|
|
|
|
|
|
|
finalization
|
|
|
|
TGameRegistry.Finalize;
|
|
|
|
|
|
|
|
end.
|