Ignore:
Timestamp:
Jan 8, 2025, 10:18:12 AM (30 hours ago)
Author:
chronos
Message:
  • Fixed: Cell cities were not correctly stored the saved game.
  • Fixed: ItemList references were loaded by item index instead of item id.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/ItemList.pas

    r399 r407  
    8383  public
    8484  type
    85     TAddEvent = function (constref AValue: TItem): SizeInt of object;
     85    TAddEvent = function(constref AValue: TItem): SizeInt of object;
    8686    TGetCountEvent = function: SizeInt of object;
    8787    TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object;
    88     TGetNameEvent = procedure (out Name: string) of object;
     88    TGetNameEvent = procedure(out Name: string) of object;
    8989    TGetItemEvent = function(Index: SizeInt): TItem of object;
    9090    TGetItemFieldsEvent = function: TItemFields of object;
    9191    TRemoveEvent = function(constref AValue: TItem): SizeInt of object;
    92     TGetNextAvailableNameEvent = procedure (Name: string; out NewName: string) of object;
     92    TGetNextAvailableNameEvent = procedure(Name: string; out NewName: string) of object;
    9393    TCreateItemEvent = function(Name: string = ''): TItem of object;
     94    TFindByIdEvent = function(Id: Integer): TItem of object;
    9495  private
    9596    FOnAdd: TAddEvent;
    9697    FOnCreateItem: TCreateItemEvent;
     98    FOnFindById: TFindByIdEvent;
    9799    FOnGetCount: TGetCountEvent;
    98100    FOnGetItem: TGetItemEvent;
     
    112114    function GetNextAvailableName(Name: string): string;
    113115    function GetItemFields: TItemFields;
     116    function FindById(Id: Integer): TItem;
    114117    property Count: SizeInt read GetCount;
    115118    property Items[Index: SizeInt]: TItem read GetItem write SetItem; default;
     
    125128    property OnCreateItem: TCreateItemEvent read FOnCreateItem
    126129      write FOnCreateItem;
     130    property OnFindById: TFindByIdEvent read FOnFindById
     131      write FOnFindById;
    127132  end;
    128133
     
    141146    function BaseGetItemFields: TItemFields;
    142147    function BaseCreateItem(Name: string = ''): TItem;
     148    function BaseFindById(Id: Integer): TItem;
    143149    procedure BaseGetNextAvailableName(Name: string; out NewName: string);
    144150  public
     
    245251end;
    246252
     253function TItemList<T>.BaseFindById(Id: Integer): TItem;
     254begin
     255  Result := FindById(Id);
     256end;
     257
    247258procedure TItemList<T>.LoadFromNode(Node: TDOMNode);
    248259var
     
    286297  FBaseItemList.OnGetNextAvailableName := BaseGetNextAvailableName;
    287298  FBaseItemList.OnGetName := BaseGetName;
     299  FBaseItemList.OnFindById := BaseFindById;
    288300  NewId := 1;
    289301end;
     
    483495  ReadId: Integer;
    484496  ReferenceList: TBaseItemList;
     497  RefItem: TItem;
    485498begin
    486499  if Field.DataType = dtString then begin
     
    503516    ReferenceList := GetReferenceList(Field.Index);
    504517    if (ReadId > 0) and Assigned(ReferenceList) then
    505       SetValueReference(Field.Index, TItem(ReferenceList[ReadId]));
     518      RefItem := ReferenceList.FindById(ReadId);
     519      if Assigned(RefItem) then
     520        SetValueReference(Field.Index, RefItem)
     521        else raise Exception.Create('Reference id ' + IntToStr(ReadId) + ' not found.');
    506522  end else
    507523    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     
    817833end;
    818834
     835function TBaseItemList.FindById(Id: Integer): TItem;
     836begin
     837  if Assigned(FOnFindById) then Result := FOnFindById(Id)
     838    else raise Exception.Create('Undefined FindById handler');
     839end;
     840
    819841end.
    820842
Note: See TracChangeset for help on using the changeset viewer.