From b223f447218f36d13efb0037d38c2fef4c459b2f Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 4 Dec 2009 23:30:39 +0100 Subject: [PATCH] - Fixed cedserver to compile again (using generics now) - Added multilog dependency to cedserver for easier debugging - Several code cleanups --- Client/CentrED.lpi | 5 +- Server/UCEDServer.pas | 8 ++- Server/ULandscape.pas | 115 +++++++++++++++---------------- Server/ULargeScaleOperations.pas | 67 +++++++++--------- Server/cedserver.lpi | 18 ++--- Server/cedserver.lpr | 2 +- 6 files changed, 106 insertions(+), 109 deletions(-) diff --git a/Client/CentrED.lpi b/Client/CentrED.lpi index 4efae26..2994401 100644 --- a/Client/CentrED.lpi +++ b/Client/CentrED.lpi @@ -295,13 +295,12 @@ - + - - + diff --git a/Server/UCEDServer.pas b/Server/UCEDServer.pas index f89b1ec..f7b63ee 100644 --- a/Server/UCEDServer.pas +++ b/Server/UCEDServer.pas @@ -72,7 +72,7 @@ var implementation uses - UClientHandling; + Logging, UClientHandling; {$I version.inc} @@ -250,7 +250,11 @@ begin end; ANetState.LastAction := Now; except - Writeln(TimeStamp, 'Error processing buffer of client: ', ANetState.Socket.PeerAddress); + on E: Exception do + begin + Logger.SendException([lcServer], 'Error processing buffer', E); + Writeln(TimeStamp, 'Error processing buffer of client: ', ANetState.Socket.PeerAddress); + end; end; end; diff --git a/Server/ULandscape.pas b/Server/ULandscape.pas index 56eab5c..631d29d 100644 --- a/Server/ULandscape.pas +++ b/Server/ULandscape.pas @@ -21,7 +21,7 @@ * CDDL HEADER END * * - * Portions Copyright 2008 Andreas Schneider + * Portions Copyright 2009 Andreas Schneider *) unit ULandscape; @@ -51,7 +51,7 @@ type FTiledataProvider: TTiledataProvider; public { Fields } - Cells: array[0..63] of TList; + Cells: array[0..63] of TStaticItemList; property TiledataProvider: TTiledataProvider read FTiledataProvider write FTiledataProvider; { Methods } @@ -98,7 +98,7 @@ type procedure OnBlockChanged(ABlock: TMulBlock); procedure OnRemoveCachedObject(AObject: TObject); function GetMapCell(AX, AY: Word): TMapCell; - function GetStaticList(AX, AY: Word): TList; + function GetStaticList(AX, AY: Word): TStaticItemList; function GetBlockSubscriptions(AX, AY: Word): TLinkedList; procedure OnDrawMapPacket(ABuffer: TEnhancedMemoryStream; @@ -121,7 +121,7 @@ type property CellWidth: Word read FCellWidth; property CellHeight: Word read FCellHeight; property MapCell[X, Y: Word]: TMapCell read GetMapCell; - property StaticList[X, Y: Word]: TList read GetStaticList; + property StaticList[X, Y: Word]: TStaticItemList read GetStaticList; property BlockSubscriptions[X, Y: Word]: TLinkedList read GetBlockSubscriptions; property TiledataProvider: TTiledataProvider read FTiledataProvider; @@ -132,7 +132,7 @@ type procedure UpdateRadar(AX, AY: Word); function GetEffectiveAltitude(ATile: TMapCell): ShortInt; function GetLandAlt(AX, AY: Word; ADefault: ShortInt): ShortInt; - procedure SortStaticsList(AStatics: TList); + procedure SortStaticsList(AStatics: TStaticItemList); procedure Flush; procedure SaveBlock(AWorldBlock: TWorldBlock); @@ -185,13 +185,13 @@ var block: TMemoryStream; begin inherited Create; - FItems := TList.Create; + FItems := TStaticItemList.Create(False); FX := AX; FY := AY; for i := 0 to 63 do - Cells[i] := TList.Create; + Cells[i] := TStaticItemList.Create(True); if (AData <> nil) and (AIndex.Lookup > 0) and (AIndex.Size > 0) then begin @@ -220,21 +220,7 @@ begin FreeAndNil(FItems); for i := 0 to 63 do - begin - if Cells[i] <> nil then - begin - for j := 0 to Cells[i].Count - 1 do - begin - if Cells[i][j] <> nil then - begin - TStaticItem(Cells[i][j]).Free; - Cells[i][j] := nil; - end; - end; - Cells[i].Free; - Cells[i] := nil; - end; - end; + FreeAndNil(Cells[i]); inherited Destroy; end; @@ -263,9 +249,8 @@ begin for j := 0 to Cells[i].Count - 1 do begin FItems.Add(Cells[i].Items[j]); - TStaticItem(Cells[i].Items[j]).UpdatePriorities( - FTiledataProvider.StaticTiles[TStaticItem(Cells[i].Items[j]).TileID], - solver); + Cells[i].Items[j].UpdatePriorities( + FTiledataProvider.StaticTiles[Cells[i].Items[j].TileID], solver); Inc(solver); end; end; @@ -355,16 +340,16 @@ var i: Integer; begin for i := 0 to Length(FBlockSubscriptions) - 1 do - if FBlockSubscriptions[i] <> nil then FreeAndNil(FBlockSubscriptions[i]); - if FBlockCache <> nil then FreeAndNil(FBlockCache); - if FTiledataProvider <> nil then FreeAndNil(FTiledataProvider); - if FRadarMap <> nil then FreeAndNil(FRadarMap); + FreeAndNil(FBlockSubscriptions[i]); + FreeAndNil(FBlockCache); + FreeAndNil(FTiledataProvider); + FreeAndNil(FRadarMap); if FOwnsStreams then begin - if FMap <> nil then FreeAndNil(FMap); - if FStatics <> nil then FreeAndNil(FStatics); - if FStaIdx <> nil then FreeAndNil(FStaIdx); - if FTiledata <> nil then FreeAndNil(FTiledata); + FreeAndNil(FMap); + FreeAndNil(FStatics); + FreeAndNil(FStaIdx); + FreeAndNil(FTiledata); end; RegisterPacketHandler($06, nil); @@ -407,7 +392,7 @@ begin Result := ADefault; end; -function TLandscape.GetStaticList(AX, AY: Word): TList; +function TLandscape.GetStaticList(AX, AY: Word): TStaticItemList; var block: TSeperatedStaticBlock; begin @@ -424,7 +409,8 @@ procedure TLandscape.UpdateRadar(AX, AY: Word); var mapTile: TMapCell; tile: TWorldItem; - staticItems, tiles: TList; + staticItems: TStaticItemList; + tiles: TWorldItemList; i: Integer; begin if (AX mod 8 = 0) and (AY mod 8 = 0) then @@ -432,7 +418,7 @@ begin staticItems := GetStaticList(AX, AY); if staticItems <> nil then begin - tiles := TList.Create; + tiles := TWorldItemList.Create(False); mapTile := GetMapCell(AX, AY); if mapTile <> nil then begin @@ -443,16 +429,16 @@ begin end; for i := 0 to staticItems.Count - 1 do begin - TStaticItem(staticItems.Items[i]).UpdatePriorities( - FTiledataProvider.StaticTiles[TStaticItem(staticItems.Items[i]).TileID], + staticItems[i].UpdatePriorities( + FTiledataProvider.StaticTiles[staticItems[i].TileID], i + 1); - tiles.Add(staticItems.Items[i]); + tiles.Add(staticItems[i]); end; tiles.Sort(@CompareWorldItems); if tiles.Count > 0 then begin - tile := TWorldItem(tiles.Items[tiles.Count - 1]); + tile := tiles[tiles.Count - 1]; if tile is TStaticItem then FRadarMap.Update(AX div 8, AY div 8, tile.TileID + $4000) else @@ -464,15 +450,15 @@ begin end; end; -procedure TLandscape.SortStaticsList(AStatics: TList); +procedure TLandscape.SortStaticsList(AStatics: TStaticItemList); var i: Integer; begin for i := 0 to AStatics.Count - 1 do - TStaticItem(AStatics.Items[i]).UpdatePriorities( - FTiledataProvider.StaticTiles[TStaticItem(AStatics.Items[i]).TileID], + AStatics[i].UpdatePriorities( + FTiledataProvider.StaticTiles[AStatics[i].TileID], i + 1); - AStatics.Sort(@CompareWorldItems); + AStatics.Sort(@CompareStaticItems); end; function TLandscape.GetEffectiveAltitude(ATile: TMapCell): ShortInt; @@ -649,7 +635,7 @@ var x, y: Word; block: TSeperatedStaticBlock; staticItem: TStaticItem; - targetStaticList: TList; + targetStaticList: TStaticItemList; subscriptions: TLinkedList; subscriptionItem: PLinkedItem; packet: TInsertStaticPacket; @@ -689,7 +675,7 @@ procedure TLandscape.OnDeleteStaticPacket(ABuffer: TEnhancedMemoryStream; var block: TSeperatedStaticBlock; i: Integer; - statics: TList; + statics: TStaticItemList; staticInfo: TStaticInfo; staticItem: TStaticItem; subscriptions: TLinkedList; @@ -706,20 +692,22 @@ begin statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8]; for i := 0 to statics.Count - 1 do begin - staticItem := TStaticItem(statics.Items[i]); + staticItem := statics[i]; if (staticItem.Z = staticInfo.Z) and (staticItem.TileID = staticInfo.TileID) and (staticItem.Hue = staticInfo.Hue) then begin packet := TDeleteStaticPacket.Create(staticItem); - - statics.Delete(i); + staticItem.Delete; + statics.Delete(i); - subscriptions := FBlockSubscriptions[(staticInfo.Y div 8) * FWidth + (staticInfo.X div 8)]; + subscriptions := FBlockSubscriptions[(staticInfo.Y div 8) * FWidth + + (staticInfo.X div 8)]; subscriptionItem := nil; while subscriptions.Iterate(subscriptionItem) do - CEDServerInstance.SendPacket(TNetState(subscriptionItem^.Data), packet, False); + CEDServerInstance.SendPacket(TNetState(subscriptionItem^.Data), + packet, False); packet.Free; UpdateRadar(staticInfo.X, staticInfo.Y); @@ -735,7 +723,7 @@ procedure TLandscape.OnElevateStaticPacket(ABuffer: TEnhancedMemoryStream; var block: TSeperatedStaticBlock; i: Integer; - statics: TList; + statics: TStaticItemList; staticInfo: TStaticInfo; staticItem: TStaticItem; newZ: ShortInt; @@ -753,7 +741,7 @@ begin statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8]; for i := 0 to statics.Count - 1 do begin - staticItem := TStaticItem(statics.Items[i]); + staticItem := statics[i]; if (staticItem.Z = staticInfo.Z) and (staticItem.TileID = staticInfo.TileID) and (staticItem.Hue = staticInfo.Hue) then @@ -764,10 +752,12 @@ begin staticItem.Z := newZ; SortStaticsList(statics); - subscriptions := FBlockSubscriptions[(staticInfo.Y div 8) * FWidth + (staticInfo.X div 8)]; + subscriptions := FBlockSubscriptions[(staticInfo.Y div 8) * FWidth + + (staticInfo.X div 8)]; subscriptionItem := nil; while subscriptions.Iterate(subscriptionItem) do - CEDServerInstance.SendPacket(TNetState(subscriptionItem^.Data), packet, False); + CEDServerInstance.SendPacket(TNetState(subscriptionItem^.Data), + packet, False); packet.Free; UpdateRadar(staticInfo.X, staticInfo.Y); @@ -784,7 +774,7 @@ var sourceBlock, targetBlock: TSeperatedStaticBlock; sourceSubscriptions, targetSubscriptions: TList; i: Integer; - statics: TList; + statics: TStaticItemList; staticInfo: TStaticInfo; staticItem: TStaticItem; newX, newY: Word; @@ -816,7 +806,7 @@ begin i := 0; while (i < statics.Count) and (staticItem = nil) do begin - staticItem := TStaticItem(statics.Items[i]); + staticItem := statics[i]; if (staticItem.Z <> staticInfo.Z) or (staticItem.TileID <> staticInfo.TileID) or (staticItem.Hue <> staticInfo.Hue) then @@ -831,7 +821,10 @@ begin deletePacket := TDeleteStaticPacket.Create(staticItem); movePacket := TMoveStaticPacket.Create(staticItem, newX, newY); - statics.Remove(staticItem); + i := statics.IndexOf(staticItem); + statics[i] := nil; + statics.Delete(i); + statics := targetBlock.Cells[(newY mod 8) * 8 + newX mod 8]; statics.Add(staticItem); staticItem.UpdatePos(newX, newY, staticItem.Z); @@ -882,7 +875,7 @@ procedure TLandscape.OnHueStaticPacket(ABuffer: TEnhancedMemoryStream; var block: TSeperatedStaticBlock; i: Integer; - statics: TList; + statics: TStaticItemList; staticInfo: TStaticInfo; staticItem: TStaticItem; newHue: Word; @@ -900,7 +893,7 @@ begin statics := block.Cells[(staticInfo.Y mod 8) * 8 + staticInfo.X mod 8]; for i := 0 to statics.Count - 1 do begin - staticItem := TStaticItem(statics.Items[i]); + staticItem := statics[i]; if (staticItem.Z = staticInfo.Z) and (staticItem.TileID = staticInfo.TileID) and (staticItem.Hue = staticInfo.Hue) then @@ -935,7 +928,7 @@ var emptyBits: TBits; bitMask: array of TBits; mapTile: TMapCell; - statics: TList; + statics: TStaticItemList; operations: TList; clients: array of record NetState: TNetState; diff --git a/Server/ULargeScaleOperations.pas b/Server/ULargeScaleOperations.pas index 5f72ae7..29d20bc 100644 --- a/Server/ULargeScaleOperations.pas +++ b/Server/ULargeScaleOperations.pas @@ -21,7 +21,7 @@ * CDDL HEADER END * * - * Portions Copyright 2007 Andreas Schneider + * Portions Copyright 2009 Andreas Schneider *) unit ULargeScaleOperations; @@ -46,7 +46,7 @@ type protected FLandscape: TLandscape; public - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); virtual; abstract; end; @@ -62,7 +62,7 @@ type public property OffsetX: Integer read FOffsetX; property OffsetY: Integer read FOffsetY; - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); override; end; @@ -76,7 +76,7 @@ type FMaxZ: ShortInt; FRelativeZ: ShortInt; public - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); override; end; @@ -87,7 +87,7 @@ type protected FTileIDs: array of Word; public - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); override; end; @@ -100,7 +100,7 @@ type FMinZ: ShortInt; FMaxZ: ShortInt; public - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); override; end; @@ -114,7 +114,7 @@ type FPlacementType: TStaticsPlacement; FFixZ: ShortInt; public - procedure Apply(AMapCell: TMapCell; AStatics: TList; + procedure Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); override; end; @@ -145,12 +145,12 @@ begin FErase := AData.ReadBoolean; end; -procedure TLSCopyMove.Apply(AMapCell: TMapCell; AStatics: TList; +procedure TLSCopyMove.Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); var x, y: Word; targetCell: TMapCell; - targetStatics: TList; + targetStatics: TStaticItemList; targetStaticsBlock: TSeperatedStaticBlock; i: Integer; staticItem: TStaticItem; @@ -164,9 +164,7 @@ begin if FErase then begin for i := 0 to targetStatics.Count - 1 do - begin - TStaticItem(targetStatics.Items[i]).Delete; - end; + targetStatics[i].Delete; targetStatics.Clear; end; targetCell.TileID := AMapCell.TileID; @@ -179,23 +177,22 @@ begin staticItem := TStaticItem.Create(nil, nil, 0, 0); staticItem.X := x; staticItem.Y := y; - staticItem.Z := TStaticItem(AStatics.Items[i]).Z; - staticItem.TileID := TStaticItem(AStatics.Items[i]).TileID; - staticItem.Hue := TStaticItem(AStatics.Items[i]).Hue; + staticItem.Z := AStatics[i].Z; + staticItem.TileID := AStatics[i].TileID; + staticItem.Hue := AStatics[i].Hue; staticItem.Owner := targetStaticsBlock; targetStatics.Add(staticItem); end; end else begin - {for i := 0 to AStatics.Count - 1 do} - while AStatics.Count > 0 do + for i := 0 to AStatics.Count - 1 do begin - targetStatics.Add(AStatics.Items[0]); - TStaticItem(AStatics.Items[0]).UpdatePos(x, y, TStaticItem(AStatics.Items[0]).Z); - TStaticItem(AStatics.Items[0]).Owner := targetStaticsBlock; - AStatics.Delete(0); + targetStatics.Add(AStatics[i]); + AStatics[i].UpdatePos(x, y, AStatics[i].Z); + AStatics[i].Owner := targetStaticsBlock; + AStatics[i] := nil; end; - //AStatics.Clear; + AStatics.Clear; end; FLandscape.SortStaticsList(targetStatics); @@ -222,7 +219,7 @@ begin end; end; -procedure TLSSetAltitude.Apply(AMapCell: TMapCell; AStatics: TList; +procedure TLSSetAltitude.Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); var i: Integer; @@ -243,7 +240,7 @@ begin for i := 0 to AStatics.Count - 1 do begin - static := TStaticItem(AStatics.Items[i]); + static := AStatics[i]; static.Z := EnsureRange(static.Z + diff, -128, 127); end; end; @@ -261,7 +258,7 @@ begin AData.Read(FTileIDs[0], count * SizeOf(Word)); end; -procedure TLSDrawTerrain.Apply(AMapCell: TMapCell; AStatics: TList; +procedure TLSDrawTerrain.Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); begin if Length(FTileIDs) > 0 then @@ -283,7 +280,7 @@ begin FMaxZ := AData.ReadShortInt; end; -procedure TLSDeleteStatics.Apply(AMapCell: TMapCell; AStatics: TList; +procedure TLSDeleteStatics.Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); var i, j: Integer; @@ -292,7 +289,7 @@ begin i := 0; while i < AStatics.Count do begin - static := TStaticItem(AStatics.Items[i]); + static := AStatics[i]; if InRange(static.Z, FMinZ, FMaxZ) then begin if Length(FTileIDs) > 0 then @@ -301,8 +298,8 @@ begin begin if static.TileID = FTileIDs[j] - $4000 then begin - AStatics.Delete(i); static.Delete; + AStatics.Delete(i); Dec(i); Break; end; @@ -310,8 +307,8 @@ begin Inc(i); end else begin - AStatics.Delete(i); static.Delete; + AStatics.Delete(i); end; end else Inc(i); @@ -335,7 +332,7 @@ begin FFixZ := AData.ReadShortInt; end; -procedure TLSInsertStatics.Apply(AMapCell: TMapCell; AStatics: TList; +procedure TLSInsertStatics.Apply(AMapCell: TMapCell; AStatics: TStaticItemList; AAdditionalAffectedBlocks: TBits); var staticItem, static: TStaticItem; @@ -360,8 +357,10 @@ begin topZ := AMapCell.Z; for i := 0 to AStatics.Count - 1 do begin - static := TStaticItem(AStatics.Items[i]); - staticTop := EnsureRange(static.Z + CEDServerInstance.Landscape.TiledataProvider.StaticTiles[static.TileID].Height, -128, 127); + static := AStatics[i]; + staticTop := EnsureRange(static.Z + + CEDServerInstance.Landscape.TiledataProvider.StaticTiles[static.TileID].Height, + -128, 127); if staticTop > topZ then topZ := staticTop; end; end; @@ -372,8 +371,8 @@ begin end; AStatics.Add(staticItem); - staticItem.Owner := CEDServerInstance.Landscape.GetStaticBlock(staticItem.X div 8, - staticItem.Y div 8); + staticItem.Owner := CEDServerInstance.Landscape.GetStaticBlock( + staticItem.X div 8, staticItem.Y div 8); end; end. diff --git a/Server/cedserver.lpi b/Server/cedserver.lpi index ebdd3f4..ff6aa4a 100644 --- a/Server/cedserver.lpi +++ b/Server/cedserver.lpi @@ -21,13 +21,16 @@ - + - + - + + + + @@ -126,19 +129,18 @@ - + - - + + - + diff --git a/Server/cedserver.lpr b/Server/cedserver.lpr index 808cdd1..ccd1318 100644 --- a/Server/cedserver.lpr +++ b/Server/cedserver.lpr @@ -33,7 +33,7 @@ uses {$ENDIF}{$ENDIF} SysUtils, Classes, lnetbase, UConfig, UCEDServer, URadarMap, ULargeScaleOperations, UPackets, UAdminHandling, UClientHandling, ULandscape, - UPacketHandlers, URegions, UStatics; + UPacketHandlers, URegions, UStatics, Logging; {$I version.inc}