Changeset 35 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 13, 2019, 2:28:55 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Optimized speed of loading/storing game state to registry. Under Linux simulated as XML file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r34 r35  
    4343    NewItemPos: TPoint;
    4444    NewItemValue: Integer;
    45     procedure SaveToRegistry(RegContext: TRegistryContext);
    46     procedure LoadFromRegistry(RegContext: TRegistryContext);
     45    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     46    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    4747  end;
    4848
     
    5050
    5151  THistoryMoves = class(TFPGObjectList<THistoryMove>)
    52     procedure SaveToRegistry(RegContext: TRegistryContext);
    53     procedure LoadFromRegistry(RegContext: TRegistryContext);
     52    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     53    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    5454  end;
    5555
     
    6363    constructor Create;
    6464    destructor Destroy; override;
    65     procedure SaveToRegistry(RegContext: TRegistryContext);
    66     procedure LoadFromRegistry(RegContext: TRegistryContext);
     65    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     66    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    6767  end;
    6868
     
    8282    function GetEmptyTilesCount: Integer;
    8383    procedure GetEmptyTiles(EmptyTiles: TTiles);
    84     procedure SaveToRegistry(RegContext: TRegistryContext);
    85     procedure LoadFromRegistry(RegContext: TRegistryContext);
     84    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     85    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    8686    destructor Destroy; override;
    8787    property Size: TPoint read FSize write SetSize;
     
    157157{ THistoryMoves }
    158158
    159 procedure THistoryMoves.SaveToRegistry(RegContext: TRegistryContext);
     159procedure THistoryMoves.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    160160var
    161161  I: Integer;
    162162begin
    163   with TRegistryEx.Create do
    164   try
     163  with Reg do begin
    165164    CurrentContext := RegContext;
    166165    WriteInteger('Count', Count);
    167166    for I := 0 to Count - 1 do begin
    168       Items[I].SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
    169     end;
    170   finally
    171     Free;
    172   end;
    173 end;
    174 
    175 procedure THistoryMoves.LoadFromRegistry(RegContext: TRegistryContext);
     167      Items[I].SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
     168    end;
     169  end;
     170end;
     171
     172procedure THistoryMoves.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    176173var
    177174  I: Integer;
     
    179176  HistoryMove: THistoryMove;
    180177begin
    181   with TRegistryEx.Create do
    182   try
     178  with Reg do begin
    183179    CurrentContext := RegContext;
    184180    C := ReadIntegerWithDefault('Count', 0);
     
    186182      HistoryMove := THistoryMove.Create;
    187183      Add(HistoryMove);
    188       HistoryMove.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
    189     end;
    190   finally
    191     Free;
     184      HistoryMove.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '/' + IntToStr(I)));
     185    end;
    192186  end;
    193187end;
     
    195189{ THistoryMove }
    196190
    197 procedure THistoryMove.SaveToRegistry(RegContext: TRegistryContext);
    198 begin
    199   with TRegistryEx.Create do
    200   try
     191procedure THistoryMove.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     192begin
     193  with Reg do begin
    201194    CurrentContext := RegContext;
    202195    WriteInteger('Direction', Integer(Direction));
     
    204197    WriteInteger('NewItemPosY', NewItemPos.Y);
    205198    WriteInteger('NewItemValue', NewItemValue);
    206   finally
    207     Free;
    208   end;
    209 end;
    210 
    211 procedure THistoryMove.LoadFromRegistry(RegContext: TRegistryContext);
    212 begin
    213   with TRegistryEx.Create do
    214   try
     199  end;
     200end;
     201
     202procedure THistoryMove.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
     203begin
     204  with Reg do begin
    215205    CurrentContext := RegContext;
    216206    Direction := TMoveDirection(ReadIntegerWithDefault('Direction', Integer(drNone)));
     
    218208      ReadIntegerWithDefault('NewItemPosY', 0));
    219209    NewItemValue := ReadIntegerWithDefault('NewItemValue', 0);
    220   finally
    221     Free;
    222210  end;
    223211end;
     
    254242end;
    255243
    256 procedure THistory.SaveToRegistry(RegContext: TRegistryContext);
     244procedure THistory.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    257245var
    258246  I: Integer;
    259247begin
    260   with TRegistryEx.Create do
    261   try
     248  with Reg do begin
    262249    CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\InitialTilesPos');
    263250    WriteInteger('Count', Length(InitialTilesPos));
     
    266253      WriteInteger('Y' + IntToStr(I), InitialTilesPos[I].Y);
    267254    end;
    268   finally
    269     Free;
    270   end;
    271   Moves.SaveToRegistry(RegContext);
    272 end;
    273 
    274 procedure THistory.LoadFromRegistry(RegContext: TRegistryContext);
     255    Moves.SaveToRegistry(Reg, RegContext);
     256  end;
     257end;
     258
     259procedure THistory.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    275260var
    276261  I: Integer;
    277262begin
    278   with TRegistryEx.Create do
    279   try
     263  with Reg do begin
    280264    CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\InitialTilesPos');
    281265    SetLength(InitialTilesPos, ReadIntegerWithDefault('Count', 0));
     
    284268        ReadIntegerWithDefault('Y' + IntToStr(I), 0));
    285269    end;
    286   finally
    287     Free;
    288   end;
    289   Moves.LoadFromRegistry(RegContext);
     270  end;
     271  Moves.LoadFromRegistry(Reg, RegContext);
    290272end;
    291273
     
    349331end;
    350332
    351 procedure TBoard.SaveToRegistry(RegContext: TRegistryContext);
     333procedure TBoard.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    352334var
    353335  X, Y: Integer;
    354336  Value: string;
    355337begin
    356   with TRegistryEx.Create do
    357   try
     338  with Reg do begin
    358339    CurrentContext := RegContext;
    359340
     
    369350    end;
    370351    WriteString('TileValues', Value);
    371   finally
    372     Free;
    373   end;
    374 end;
    375 
    376 procedure TBoard.LoadFromRegistry(RegContext: TRegistryContext);
     352  end;
     353end;
     354
     355procedure TBoard.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    377356var
    378357  X, Y: Integer;
     
    381360  Number: Integer;
    382361begin
    383   with TRegistryEx.Create do
    384   try
     362  with Reg do begin
    385363    CurrentContext := RegContext;
    386364
     
    400378    Lines.Free;
    401379    Items.Free;
    402   finally
    403     Free;
    404380  end;
    405381end;
     
    912888
    913889procedure TGame.SaveToRegistry(RegContext: TRegistryContext);
    914 begin
    915   with TRegistryEx.Create do
     890var
     891  Reg: TRegistryEx;
     892begin
     893  Reg := TRegistryEx.Create;
     894  with Reg do
    916895  try
    917896    CurrentContext := RegContext;
     
    923902    WriteBool('CanUndo', FCanUndo);
    924903    WriteBool('UndoEnabled', UndoEnabled);
     904    FBoardUndo.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
     905    Board.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
     906    History.SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\History'));
    925907  finally
    926908    Free;
    927909  end;
    928   FBoardUndo.SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    929   Board.SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
    930   History.SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\History'));
    931910end;
    932911
    933912procedure TGame.LoadFromRegistry(RegContext: TRegistryContext);
    934 begin
    935   with TRegistryEx.Create do
     913var
     914  Reg: TRegistryEx;
     915begin
     916  Reg := TRegistryEx.Create;
     917  with Reg do
    936918  try
    937919    CurrentContext := RegContext;
     
    942924    FCanUndo := ReadBoolWithDefault('CanUndo', False);
    943925    UndoEnabled := ReadBoolWithDefault('UndoEnabled', True);
     926    FBoardUndo.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
     927    Board.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
     928    History.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\History'));
    944929  finally
    945930    Free;
    946931  end;
    947   FBoardUndo.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo'));
    948   Board.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board'));
    949   History.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\History'));
    950932  DoChange;
    951933end;
Note: See TracChangeset for help on using the changeset viewer.