🐛 Use version-aware offsets when writing High Seas tiledata
The write path always computed legacy offsets and sizes while the loader detected and used the High Seas layout, so any tile edit on a High Seas tiledata.mul shifted records and corrupted the file.
This commit is contained in:
@@ -46,6 +46,7 @@ type
|
|||||||
FLandTiles: TLandTileDataArray;
|
FLandTiles: TLandTileDataArray;
|
||||||
FStaticTiles: TStaticTileDataArray;
|
FStaticTiles: TStaticTileDataArray;
|
||||||
FStaticCount: Cardinal;
|
FStaticCount: Cardinal;
|
||||||
|
FVersion: TTileDataVersion;
|
||||||
procedure InitArray;
|
procedure InitArray;
|
||||||
function CalculateOffset(AID: Integer): Integer; override;
|
function CalculateOffset(AID: Integer): Integer; override;
|
||||||
function GetData(AID, AOffset: Integer): TMulBlock; override;
|
function GetData(AID, AOffset: Integer): TMulBlock; override;
|
||||||
@@ -68,7 +69,7 @@ uses
|
|||||||
|
|
||||||
function TTiledataProvider.CalculateOffset(AID: Integer): Integer;
|
function TTiledataProvider.CalculateOffset(AID: Integer): Integer;
|
||||||
begin
|
begin
|
||||||
Result := GetTileDataOffset(AID);
|
Result := GetTileDataOffset(AID, FVersion);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TTiledataProvider.Create(AData: TStream; AReadOnly: Boolean = False);
|
constructor TTiledataProvider.Create(AData: TStream; AReadOnly: Boolean = False);
|
||||||
@@ -114,13 +115,17 @@ end;
|
|||||||
procedure TTiledataProvider.InitArray;
|
procedure TTiledataProvider.InitArray;
|
||||||
var
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
version: TTileDataVersion;
|
staticGroupSize: Integer;
|
||||||
begin
|
begin
|
||||||
//Guess the version by looking at the filesize.
|
//Guess the version by looking at the filesize.
|
||||||
if FData.Size >= 3188736 then
|
if FData.Size >= 3188736 then
|
||||||
version := tdvHighSeas
|
FVersion := tdvHighSeas
|
||||||
else
|
else
|
||||||
version := tdvLegacy;
|
FVersion := tdvLegacy;
|
||||||
|
if FVersion >= tdvHighSeas then
|
||||||
|
staticGroupSize := StaticTileGroupSizeHS
|
||||||
|
else
|
||||||
|
staticGroupSize := StaticTileGroupSize;
|
||||||
|
|
||||||
FData.Position := 0;
|
FData.Position := 0;
|
||||||
Logger.Send([lcInfo], 'Loading $4000 LandTiledata Entries');
|
Logger.Send([lcInfo], 'Loading $4000 LandTiledata Entries');
|
||||||
@@ -128,13 +133,13 @@ begin
|
|||||||
begin
|
begin
|
||||||
//In High Seas, the first header comes AFTER the unknown tile (for whatever
|
//In High Seas, the first header comes AFTER the unknown tile (for whatever
|
||||||
//reason). Therefore special handling is required.
|
//reason). Therefore special handling is required.
|
||||||
if ((version = tdvLegacy) and (i mod 32 = 0)) or
|
if ((FVersion = tdvLegacy) and (i mod 32 = 0)) or
|
||||||
((version >= tdvHighSeas) and ((i = 1) or ((i > 1) and (i mod 32 = 0)))) then
|
((FVersion >= tdvHighSeas) and ((i = 1) or ((i > 1) and (i mod 32 = 0)))) then
|
||||||
FData.Seek(4, soFromCurrent);
|
FData.Seek(4, soFromCurrent);
|
||||||
FLandTiles[i] := TLandTileData.Create(FData, version);
|
FLandTiles[i] := TLandTileData.Create(FData, FVersion);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
FStaticCount := ((FData.Size - FData.Position) div StaticTileGroupSize) * 32;
|
FStaticCount := ((FData.Size - FData.Position) div staticGroupSize) * 32;
|
||||||
Logger.Send([lcInfo], 'Loading $%x StaticTiledata Entries', [FStaticCount]);
|
Logger.Send([lcInfo], 'Loading $%x StaticTiledata Entries', [FStaticCount]);
|
||||||
SetLength(FStaticTiles, FStaticCount);
|
SetLength(FStaticTiles, FStaticCount);
|
||||||
|
|
||||||
@@ -142,7 +147,7 @@ begin
|
|||||||
begin
|
begin
|
||||||
if i mod 32 = 0 then
|
if i mod 32 = 0 then
|
||||||
FData.Seek(4, soFromCurrent);
|
FData.Seek(4, soFromCurrent);
|
||||||
FStaticTiles[i] := TStaticTileData.Create(FData, version);
|
FStaticTiles[i] := TStaticTileData.Create(FData, FVersion);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
+38
-2
@@ -37,6 +37,10 @@ const
|
|||||||
LandTileGroupSize = 4 + 32 * LandTileDataSize;
|
LandTileGroupSize = 4 + 32 * LandTileDataSize;
|
||||||
StaticTileDataSize = 37;
|
StaticTileDataSize = 37;
|
||||||
StaticTileGroupSize = 4 + 32 * StaticTileDataSize;
|
StaticTileGroupSize = 4 + 32 * StaticTileDataSize;
|
||||||
|
LandTileDataSizeHS = LandTileDataSize + 4;
|
||||||
|
LandTileGroupSizeHS = 4 + 32 * LandTileDataSizeHS;
|
||||||
|
StaticTileDataSizeHS = StaticTileDataSize + 4;
|
||||||
|
StaticTileGroupSizeHS = 4 + 32 * StaticTileDataSizeHS;
|
||||||
|
|
||||||
type
|
type
|
||||||
TTileDataFlag = (tdfBackground, tdfWeapon, tdfTransparent, tdfTranslucent,
|
TTileDataFlag = (tdfBackground, tdfWeapon, tdfTransparent, tdfTranslucent,
|
||||||
@@ -125,6 +129,7 @@ type
|
|||||||
procedure Write(AData: TStream); override;
|
procedure Write(AData: TStream); override;
|
||||||
protected
|
protected
|
||||||
FUnknown: LongInt;
|
FUnknown: LongInt;
|
||||||
|
FVersion: TTileDataVersion;
|
||||||
public
|
public
|
||||||
LandTileData: array[0..31] of TLandTiledata;
|
LandTileData: array[0..31] of TLandTiledata;
|
||||||
property Unknown: LongInt read FUnknown write FUnknown;
|
property Unknown: LongInt read FUnknown write FUnknown;
|
||||||
@@ -140,16 +145,18 @@ type
|
|||||||
procedure Write(AData: TStream); override;
|
procedure Write(AData: TStream); override;
|
||||||
protected
|
protected
|
||||||
FUnknown: LongInt;
|
FUnknown: LongInt;
|
||||||
|
FVersion: TTileDataVersion;
|
||||||
public
|
public
|
||||||
StaticTileData: array[0..31] of TStaticTiledata;
|
StaticTileData: array[0..31] of TStaticTiledata;
|
||||||
property Unknown: LongInt read FUnknown write FUnknown;
|
property Unknown: LongInt read FUnknown write FUnknown;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function GetTileDataOffset(ABlock: Integer): Integer;
|
function GetTileDataOffset(ABlock: Integer; AVersion: TTileDataVersion = tdvLegacy): Integer;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
function GetTileDataOffset(ABlock: Integer): Integer;
|
|
||||||
|
function GetTileDataOffset(ABlock: Integer; AVersion: TTileDataVersion): Integer;
|
||||||
var
|
var
|
||||||
group, tile: Integer;
|
group, tile: Integer;
|
||||||
begin
|
begin
|
||||||
@@ -159,6 +166,10 @@ begin
|
|||||||
group := ABlock div 32;
|
group := ABlock div 32;
|
||||||
tile := ABlock mod 32;
|
tile := ABlock mod 32;
|
||||||
|
|
||||||
|
if AVersion >= tdvHighSeas then
|
||||||
|
Result := 512 * LandTileGroupSizeHS + group * StaticTileGroupSizeHS + 4
|
||||||
|
+ tile * StaticTileDataSizeHS
|
||||||
|
else
|
||||||
Result := 512 * LandTileGroupSize + group * StaticTileGroupSize + 4
|
Result := 512 * LandTileGroupSize + group * StaticTileGroupSize + 4
|
||||||
+ tile * StaticTileDataSize;
|
+ tile * StaticTileDataSize;
|
||||||
end else
|
end else
|
||||||
@@ -166,6 +177,13 @@ begin
|
|||||||
group := ABlock div 32;
|
group := ABlock div 32;
|
||||||
tile := ABlock mod 32;
|
tile := ABlock mod 32;
|
||||||
|
|
||||||
|
if AVersion >= tdvHighSeas then
|
||||||
|
begin
|
||||||
|
if ABlock = 0 then
|
||||||
|
Result := 0
|
||||||
|
else
|
||||||
|
Result := ABlock * LandTileDataSizeHS + (group + 1) * 4;
|
||||||
|
end else
|
||||||
Result := group * LandTileGroupSize + 4 + tile * LandTileDataSize;
|
Result := group * LandTileGroupSize + 4 + tile * LandTileDataSize;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@@ -246,6 +264,9 @@ end;
|
|||||||
|
|
||||||
function TLandTiledata.GetSize: Integer;
|
function TLandTiledata.GetSize: Integer;
|
||||||
begin
|
begin
|
||||||
|
if FVersion >= tdvHighSeas then
|
||||||
|
GetSize := LandTileDataSizeHS
|
||||||
|
else
|
||||||
GetSize := LandTileDataSize;
|
GetSize := LandTileDataSize;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -325,6 +346,9 @@ end;
|
|||||||
|
|
||||||
function TStaticTiledata.GetSize: Integer;
|
function TStaticTiledata.GetSize: Integer;
|
||||||
begin
|
begin
|
||||||
|
if FVersion >= tdvHighSeas then
|
||||||
|
GetSize := StaticTileDataSizeHS
|
||||||
|
else
|
||||||
GetSize := StaticTileDataSize;
|
GetSize := StaticTileDataSize;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -335,6 +359,7 @@ constructor TLandTileGroup.Create(AData: TStream;
|
|||||||
var
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
begin
|
begin
|
||||||
|
FVersion := AVersion;
|
||||||
if assigned(AData) then
|
if assigned(AData) then
|
||||||
begin
|
begin
|
||||||
AData.Read(FUnknown, SizeOf(LongInt));
|
AData.Read(FUnknown, SizeOf(LongInt));
|
||||||
@@ -358,6 +383,7 @@ var
|
|||||||
begin
|
begin
|
||||||
Result := TLandTileGroup.Create(nil);
|
Result := TLandTileGroup.Create(nil);
|
||||||
Result.FUnknown := FUnknown;
|
Result.FUnknown := FUnknown;
|
||||||
|
Result.FVersion := FVersion;
|
||||||
for i := 0 to 31 do
|
for i := 0 to 31 do
|
||||||
Result.LandTileData[i] := LandTileData[i].Clone;
|
Result.LandTileData[i] := LandTileData[i].Clone;
|
||||||
end;
|
end;
|
||||||
@@ -373,16 +399,22 @@ end;
|
|||||||
|
|
||||||
function TLandTileGroup.GetSize: Integer;
|
function TLandTileGroup.GetSize: Integer;
|
||||||
begin
|
begin
|
||||||
|
if FVersion >= tdvHighSeas then
|
||||||
|
GetSize := LandTileGroupSizeHS
|
||||||
|
else
|
||||||
GetSize := LandTileGroupSize;
|
GetSize := LandTileGroupSize;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
{ TStaticTileGroup }
|
{ TStaticTileGroup }
|
||||||
|
|
||||||
|
|
||||||
constructor TStaticTileGroup.Create(AData: TStream;
|
constructor TStaticTileGroup.Create(AData: TStream;
|
||||||
AVersion: TTileDataVersion = tdvLegacy);
|
AVersion: TTileDataVersion = tdvLegacy);
|
||||||
var
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
begin
|
begin
|
||||||
|
FVersion := AVersion;
|
||||||
if assigned(AData) then
|
if assigned(AData) then
|
||||||
begin
|
begin
|
||||||
AData.Read(FUnknown, SizeOf(LongInt));
|
AData.Read(FUnknown, SizeOf(LongInt));
|
||||||
@@ -406,6 +438,7 @@ var
|
|||||||
begin
|
begin
|
||||||
Result := TStaticTileGroup.Create(nil);
|
Result := TStaticTileGroup.Create(nil);
|
||||||
Result.FUnknown := FUnknown;
|
Result.FUnknown := FUnknown;
|
||||||
|
Result.FVersion := FVersion;
|
||||||
for i := 0 to 31 do
|
for i := 0 to 31 do
|
||||||
Result.StaticTileData[i] := StaticTileData[i].Clone;
|
Result.StaticTileData[i] := StaticTileData[i].Clone;
|
||||||
end;
|
end;
|
||||||
@@ -421,6 +454,9 @@ end;
|
|||||||
|
|
||||||
function TStaticTileGroup.GetSize: Integer;
|
function TStaticTileGroup.GetSize: Integer;
|
||||||
begin
|
begin
|
||||||
|
if FVersion >= tdvHighSeas then
|
||||||
|
GetSize := StaticTileGroupSizeHS
|
||||||
|
else
|
||||||
GetSize := StaticTileGroupSize;
|
GetSize := StaticTileGroupSize;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user