Changeset 275 for trunk/UGameSystem.pas


Ignore:
Timestamp:
Feb 3, 2019, 11:28:03 PM (6 years ago)
Author:
chronos
Message:
  • Added: New forms for game systems management.
  • Added: Allow to load/save game system from/to file system as .xts file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGameSystem.pas

    r274 r275  
    66
    77uses
    8   Classes, SysUtils, fgl, UUnit, DOM, UXMLUtils, XMLConf;
     8  Classes, SysUtils, fgl, UUnit, DOM, XMLRead, XMLWrite, UXMLUtils, XMLConf;
    99
    1010type
     
    2020    EmptyCellsNeutral: Boolean;
    2121    UnitsMoveImmediately: Boolean;
     22    FileName: string;
    2223    constructor Create;
    2324    destructor Destroy; override;
     25    procedure Clear;
    2426    procedure Assign(Source: TGameSystem);
    2527    procedure LoadFromNode(Node: TDOMNode);
     
    3537  TGameSystems = class(TFPGObjectList<TGameSystem>)
    3638    function AddNew(Name: string): TGameSystem;
     39    function SearchByName(Name: string): TGameSystem;
    3740  end;
    3841
     42
    3943implementation
     44
     45resourcestring
     46  SWrongFileFormat = 'Wrong file format';
    4047
    4148{ TGameSystems }
     
    4653  Result.Name := Name;
    4754  Add(Result);
     55end;
     56
     57function TGameSystems.SearchByName(Name: string): TGameSystem;
     58var
     59  I: Integer;
     60begin
     61  I := 0;
     62  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     63  if I < Count then Result := Items[I]
     64    else Result := nil;
    4865end;
    4966
     
    5976  UnitKinds.Free;
    6077  inherited Destroy;
     78end;
     79
     80procedure TGameSystem.Clear;
     81begin
     82  UnitKinds.Clear;
    6183end;
    6284
     
    99121
    100122procedure TGameSystem.LoadFromFile(FileName: string);
     123var
     124  Doc: TXMLDocument;
     125  RootNode: TDOMNode;
    101126begin
    102 
     127  ReadXMLFile(Doc, FileName);
     128  Self.FileName := FileName;
     129  Clear;
     130  with Doc do try
     131    if Doc.DocumentElement.NodeName <> 'XtacticsRules' then
     132      raise Exception.Create(SWrongFileFormat);
     133    RootNode := Doc.DocumentElement;
     134    LoadFromNode(RootNode);
     135  finally
     136    FreeAndNil(Doc);
     137  end;
    103138end;
    104139
    105140procedure TGameSystem.SaveToFile(FileName: string);
     141var
     142  Doc: TXMLDocument;
     143  RootNode: TDOMNode;
    106144begin
    107 
     145  Self.FileName := FileName;
     146  Doc := TXMLDocument.Create;
     147  with Doc do try
     148    RootNode := CreateElement('XtacticsRules');
     149    AppendChild(RootNode);
     150    SaveToNode(RootNode);
     151    if ExtractFileDir(FileName) <> '' then
     152      ForceDirectories(ExtractFileDir(FileName));
     153    WriteXMLFile(Doc, FileName);
     154  finally
     155    FreeAndNil(Doc);
     156  end;
    108157end;
    109158
Note: See TracChangeset for help on using the changeset viewer.