- Implemented saving and loading of locations to/from xml

This commit is contained in:
Andreas Schneider 2009-12-16 16:03:35 +01:00
parent 797abbfa07
commit 34c0e9c901
2 changed files with 2727 additions and 2668 deletions

File diff suppressed because it is too large Load Diff

View File

@ -35,7 +35,7 @@ uses
StdCtrls, Spin, UEnums, VirtualTrees, Buttons, UMulBlock, UWorldItem, math, StdCtrls, Spin, UEnums, VirtualTrees, Buttons, UMulBlock, UWorldItem, math,
LCLIntf, UOverlayUI, UStatics, UEnhancedMemoryStream, ActnList, LCLIntf, UOverlayUI, UStatics, UEnhancedMemoryStream, ActnList,
XMLPropStorage, fgl, ImagingClasses, dateutils, UPlatformTypes, UMap, UPacket, XMLPropStorage, fgl, ImagingClasses, dateutils, UPlatformTypes, UMap, UPacket,
UGLFont; UGLFont, DOM, XMLRead, XMLWrite;
type type
TAccessChangedListener = procedure(AAccessLevel: TAccessLevel) of object; TAccessChangedListener = procedure(AAccessLevel: TAccessLevel) of object;
@ -321,6 +321,7 @@ type
function GetSelectedRect: TRect; function GetSelectedRect: TRect;
procedure InitRender; procedure InitRender;
procedure InitSize; procedure InitSize;
procedure LoadLocations;
procedure MoveBy(AOffsetX, AOffsetY: Integer); inline; procedure MoveBy(AOffsetX, AOffsetY: Integer); inline;
procedure PrepareMapCell(AMapCell: TMapCell); procedure PrepareMapCell(AMapCell: TMapCell);
procedure PrepareScreenBlock(ABlockInfo: PBlockInfo); procedure PrepareScreenBlock(ABlockInfo: PBlockInfo);
@ -328,6 +329,7 @@ type
procedure ProcessAccessLevel; procedure ProcessAccessLevel;
procedure RebuildScreenBuffer; procedure RebuildScreenBuffer;
procedure Render; procedure Render;
procedure SaveLocations;
procedure SetCurrentTile(const AValue: TWorldItem); procedure SetCurrentTile(const AValue: TWorldItem);
procedure SetDarkLights; inline; procedure SetDarkLights; inline;
procedure SetNormalLights; inline; procedure SetNormalLights; inline;
@ -871,20 +873,9 @@ begin
vstChat.NodeDataSize := SizeOf(TChatInfo); vstChat.NodeDataSize := SizeOf(TChatInfo);
pnlChatHeader.AnchorSide[akBottom].Control := pnlBottom; pnlChatHeader.AnchorSide[akBottom].Control := pnlBottom;
FLocationsFile := FConfigDir + 'Locations.dat'; FLocationsFile := FConfigDir + 'Locations.xml';
vstLocations.NodeDataSize := SizeOf(TLocationInfo); vstLocations.NodeDataSize := SizeOf(TLocationInfo);
try LoadLocations;
if FileExists(FLocationsFile) then
vstLocations.LoadFromFile(FLocationsFile);
except
on E: EVirtualTreeError do
begin
MessageDlg('Warning', 'The Locations could not be loaded. Most likely it is an' + LineEnding +
'outdated version or the file is damaged.' + LineEnding + LineEnding +
'A backup will be made as "Locations.bak".', mtWarning, [mbOK], 0);
RenameFile(FLocationsFile, FConfigDir + 'Locations.bak');
end;
end;
RegisterPacketHandler($0C, TPacketHandler.Create(0, @OnClientHandlingPacket)); RegisterPacketHandler($0C, TPacketHandler.Create(0, @OnClientHandlingPacket));
@ -1192,8 +1183,8 @@ procedure TfrmMain.FormDestroy(Sender: TObject);
begin begin
CurrentTile := nil; CurrentTile := nil;
SelectedTile := nil; SelectedTile := nil;
vstLocations.SaveToFile(FLocationsFile); SaveLocations;
FreeAndNil(FTextureManager); FreeAndNil(FTextureManager);
FreeAndNil(FScreenBuffer); FreeAndNil(FScreenBuffer);
@ -1676,7 +1667,7 @@ var
locationInfo: PLocationInfo; locationInfo: PLocationInfo;
begin begin
locationInfo := Sender.GetNodeData(Node); locationInfo := Sender.GetNodeData(Node);
locationInfo^.Name := ''; locationInfo^.Name := EmptyStr;
end; end;
procedure TfrmMain.vstLocationsGetText(Sender: TBaseVirtualTree; procedure TfrmMain.vstLocationsGetText(Sender: TBaseVirtualTree;
@ -1902,6 +1893,46 @@ begin
glLoadIdentity; glLoadIdentity;
end; end;
procedure TfrmMain.LoadLocations;
var
xmlDoc: TXMLDocument;
location: TDOMElement;
locationNode: PVirtualNode;
locationInfo: PLocationInfo;
locations: TDOMNodeList;
i, j: Integer;
begin
vstLocations.Clear;
if FileExists(FLocationsFile) then
begin
ReadXMLFile(xmlDoc, FLocationsFile);
if xmlDoc.DocumentElement.NodeName = 'Locations' then
begin
locations := xmlDoc.DocumentElement.ChildNodes;
for i := 0 to locations.Count - 1 do
begin
location := TDOMElement(locations[i]);
locationNode := vstLocations.AddChild(nil);
locationInfo := vstLocations.GetNodeData(locationNode);
locationInfo^.Name := location.AttribStrings['Name'];
if TryStrToInt(location.AttribStrings['X'], j) then
locationInfo^.X := j
else
locationInfo^.X := 0;
if TryStrToInt(location.AttribStrings['Y'], j) then
locationInfo^.Y := j
else
locationInfo^.Y := 0;
end;
end;
xmlDoc.Free;
end;
end;
procedure TfrmMain.MoveBy(AOffsetX, AOffsetY: Integer); inline; procedure TfrmMain.MoveBy(AOffsetX, AOffsetY: Integer); inline;
begin begin
SetPos(EnsureRange(FX + AOffsetX, 0, FLandscape.CellWidth - 1), SetPos(EnsureRange(FX + AOffsetX, 0, FLandscape.CellWidth - 1),
@ -2218,6 +2249,33 @@ begin
FOverlayUI.Draw(oglGameWindow); FOverlayUI.Draw(oglGameWindow);
end; end;
procedure TfrmMain.SaveLocations;
var
xmlDoc: TXMLDocument;
location: TDOMElement;
locationNode: PVirtualNode;
locationInfo: PLocationInfo;
begin
xmlDoc := TXMLDocument.Create;
xmlDoc.AppendChild(xmlDoc.CreateElement('Locations'));
locationNode := vstLocations.GetFirst;
while locationNode <> nil do
begin
locationInfo := vstLocations.GetNodeData(locationNode);
location := xmlDoc.CreateElement('Location');
location.AttribStrings['Name'] := locationInfo^.Name;
location.AttribStrings['X'] := IntToStr(locationInfo^.X);
location.AttribStrings['Y'] := IntToStr(locationInfo^.Y);
xmlDoc.DocumentElement.AppendChild(location);
locationNode := vstLocations.GetNext(locationNode);
end;
WriteXMLFile(xmlDoc, FLocationsFile);
xmlDoc.Free;
end;
procedure TfrmMain.OnLandscapeChanged; procedure TfrmMain.OnLandscapeChanged;
begin begin
InvalidateScreenBuffer; InvalidateScreenBuffer;