From: Ketmar Dark Date: Thu, 7 Apr 2016 22:36:40 +0000 (+0300) Subject: no more d2d:f built-in dfwad reader, it now using sfs (and can't load map, lol) X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=1070d03dd32edd73aced8786d3c5a8200a02ed00;p=d2df-sdl.git no more d2d:f built-in dfwad reader, it now using sfs (and can't load map, lol) --- diff --git a/src/game/g_map.pas b/src/game/g_map.pas index 90cb224..820dcf9 100644 --- a/src/game/g_map.pas +++ b/src/game/g_map.pas @@ -435,7 +435,7 @@ begin if log then begin e_WriteLog(Format('Error loading texture %s', [RecName]), MSG_WARNING); - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); end; WAD.Free(); @@ -475,7 +475,7 @@ begin if log then begin e_WriteLog(Format('Error loading animation texture %s', [RecName]), MSG_WARNING); - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); end; WAD.Free(); Exit; diff --git a/src/game/g_playermodel.pas b/src/game/g_playermodel.pas index a2ceab0..6174d87 100644 --- a/src/game/g_playermodel.pas +++ b/src/game/g_playermodel.pas @@ -247,7 +247,7 @@ begin WAD := TWADEditor_1.Create; WAD.ReadFile(FileName); - if WAD.GetLastError <> DFWAD_NOERROR then + if {WAD.GetLastError <> DFWAD_NOERROR} not WAD.isOpen then begin WAD.Free(); Exit; diff --git a/src/game/g_sound.pas b/src/game/g_sound.pas index 5509885..f5ce6c7 100644 --- a/src/game/g_sound.pas +++ b/src/game/g_sound.pas @@ -304,7 +304,9 @@ begin FreeMem(SoundData); end else - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + begin + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + end; WAD.Free(); @@ -352,7 +354,9 @@ begin FreeMem(SoundData); end else - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + begin + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + end; WAD.Free(); diff --git a/src/game/g_textures.pas b/src/game/g_textures.pas index eb067ea..c96d4c6 100644 --- a/src/game/g_textures.pas +++ b/src/game/g_textures.pas @@ -166,7 +166,7 @@ begin else begin e_WriteLog(Format('Error loading texture %s', [Resource]), MSG_WARNING); - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); end; WAD.Free(); end; @@ -213,7 +213,7 @@ begin else begin e_WriteLog(Format('Error loading texture %s', [Resource]), MSG_WARNING); - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); Result := False; end; WAD.Free(); @@ -409,7 +409,7 @@ begin begin WAD.Free(); e_WriteLog(Format('Error loading texture %s', [Resource]), MSG_WARNING); - e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); + //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING); Exit; end; diff --git a/src/shared/WADEDITOR.pas b/src/shared/WADEDITOR.pas index b98b159..903922f 100644 --- a/src/shared/WADEDITOR.pas +++ b/src/shared/WADEDITOR.pas @@ -10,43 +10,35 @@ WADEDITOR.PAS interface -uses WADSTRUCT; +uses + sfs, xstreams; + type SArray = array of ShortString; TWADEditor_1 = class(TObject) - private - FResData: Pointer; - FResTable: packed array of TResourceTableRec_1; - FHeader: TWADHeaderRec_1; - FDataSize: LongWord; - FOffset: LongWord; - FFileName: string; - FWADOpened: Byte; - FLastError: Integer; - FVersion: Byte; - function LastErrorString(): string; - function GetResName(ResName: string): Char16; + private + fFileName: string; // empty: not opened + fIter: TSFSFileList; + + function getIsOpen (): Boolean; + public constructor Create(); destructor Destroy(); override; + procedure FreeWAD(); - function ReadFile(FileName: string): Boolean; - function ReadMemory(Data: Pointer; Len: LongWord): Boolean; - function HaveResource(Section, Resource: string): Boolean; - function HaveSection(Section: string): Boolean; - function GetResource(Section, Resource: string; var pData: Pointer; - var Len: Integer): Boolean; - function GetSectionList(): SArray; - function GetResourcesList(Section: string): SArray; - - property GetLastError: Integer read FLastError; - property GetLastErrorStr: string read LastErrorString; - property GetResourcesCount: Word read FHeader.RecordsCount; - property GetVersion: Byte read FVersion; + + function ReadFile (FileName: string): Boolean; + function ReadMemory (Data: Pointer; Len: LongWord): Boolean; + function GetResource (Section, Resource: string; var pData: Pointer; var Len: Integer): Boolean; + function GetResourcesList (Section: string): SArray; + + property isOpen: Boolean read getIsOpen; end; +{ const DFWAD_NOERROR = 0; DFWAD_ERROR_WADNOTFOUND = -1; @@ -57,69 +49,25 @@ const DFWAD_ERROR_READRESOURCE = -6; DFWAD_ERROR_READWAD = -7; DFWAD_ERROR_WRONGVERSION = -8; +} + +procedure g_ProcessResourceStr (ResourceStr: String; var FileName, SectionName, ResourceName: String); overload; +procedure g_ProcessResourceStr (ResourceStr: String; FileName, SectionName, ResourceName: PString); overload; - procedure g_ProcessResourceStr(ResourceStr: String; var FileName, - SectionName, ResourceName: String); overload; - procedure g_ProcessResourceStr(ResourceStr: String; FileName, - SectionName, ResourceName: PString); overload; implementation uses - SysUtils, BinEditor, ZLib; - -const - DFWAD_OPENED_NONE = 0; - DFWAD_OPENED_FILE = 1; - DFWAD_OPENED_MEMORY = 2; + SysUtils, Classes, BinEditor, e_log; -procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer; - OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer); -var - strm: TZStreamRec; - P: Pointer; - BufInc: Integer; -begin - FillChar(strm, sizeof(strm), 0); - BufInc := (InBytes + 255) and not 255; - if OutEstimate = 0 then - OutBytes := BufInc - else - OutBytes := OutEstimate; - GetMem(OutBuf, OutBytes); - try - strm.next_in := InBuf; - strm.avail_in := InBytes; - strm.next_out := OutBuf; - strm.avail_out := OutBytes; - inflateInit_(strm, zlib_version, sizeof(strm)); - try - while inflate(strm, Z_FINISH) <> Z_STREAM_END do - begin - P := OutBuf; - Inc(OutBytes, BufInc); - ReallocMem(OutBuf, OutBytes); - strm.next_out := PByteF(PChar(OutBuf) + (PChar(strm.next_out) - PChar(P))); - strm.avail_out := BufInc; - end; - finally - inflateEnd(strm); - end; - ReallocMem(OutBuf, strm.total_out); - OutBytes := strm.total_out; - except - FreeMem(OutBuf); - raise - end; -end; -procedure g_ProcessResourceStr(ResourceStr: String; var FileName, - SectionName, ResourceName: String); +procedure g_ProcessResourceStr (ResourceStr: String; var FileName, SectionName, ResourceName: String); var a, i: Integer; begin + //e_WriteLog(Format('g_ProcessResourceStr0: [%s]', [ResourceStr]), MSG_NOTIFY); for i := Length(ResourceStr) downto 1 do if ResourceStr[i] = ':' then Break; @@ -133,12 +81,13 @@ begin SectionName := Copy(ResourceStr, i+1, Length(ResourceStr)-Length(ResourceName)-Length(FileName)-2); end; -procedure g_ProcessResourceStr(ResourceStr: AnsiString; FileName, - SectionName, ResourceName: PAnsiString); + +procedure g_ProcessResourceStr (ResourceStr: AnsiString; FileName, SectionName, ResourceName: PAnsiString); var a, i, l1, l2: Integer; begin + //e_WriteLog(Format('g_ProcessResourceStr1: [%s]', [ResourceStr]), MSG_NOTIFY); for i := Length(ResourceStr) downto 1 do if ResourceStr[i] = ':' then Break; @@ -168,365 +117,146 @@ end; { TWADEditor_1 } - constructor TWADEditor_1.Create(); begin - FResData := nil; - FResTable := nil; - FDataSize := 0; - FOffset := 0; - FHeader.RecordsCount := 0; - FFileName := ''; - FWADOpened := DFWAD_OPENED_NONE; - FLastError := DFWAD_NOERROR; - FVersion := DFWAD_VERSION; + fFileName := ''; end; + destructor TWADEditor_1.Destroy(); begin - FreeWAD(); - - inherited; + FreeWAD(); + inherited; end; -procedure TWADEditor_1.FreeWAD(); -begin - if FResData <> nil then FreeMem(FResData); - FResTable := nil; - FDataSize := 0; - FOffset := 0; - FHeader.RecordsCount := 0; - FFileName := ''; - FWADOpened := DFWAD_OPENED_NONE; - FLastError := DFWAD_NOERROR; - FVersion := DFWAD_VERSION; -end; -function TWADEditor_1.GetResName(ResName: string): Char16; +function TWADEditor_1.getIsOpen (): Boolean; begin - ZeroMemory(@Result[0], 16); - if ResName = '' then Exit; - - ResName := Trim(UpperCase(ResName)); - if Length(ResName) > 16 then SetLength(ResName, 16); - - CopyMemory(@Result[0], @ResName[1], Length(ResName)); + result := (fFileName <> ''); end; -function TWADEditor_1.HaveResource(Section, Resource: string): Boolean; -var - a: Integer; - CurrentSection: string; -begin - Result := False; - - if FResTable = nil then Exit; - - CurrentSection := ''; - Section := AnsiUpperCase(Section); - Resource := AnsiUpperCase(Resource); - - for a := 0 to High(FResTable) do - begin - if FResTable[a].Length = 0 then - begin - CurrentSection := FResTable[a].ResourceName; - Continue; - end; - - if (FResTable[a].ResourceName = Resource) and - (CurrentSection = Section) then - begin - Result := True; - Break; - end; - end; -end; -function TWADEditor_1.HaveSection(Section: string): Boolean; -var - a: Integer; +procedure TWADEditor_1.FreeWAD(); begin - Result := False; - - if FResTable = nil then Exit; - if Section = '' then - begin - Result := True; - Exit; - end; - - Section := AnsiUpperCase(Section); - - for a := 0 to High(FResTable) do - if (FResTable[a].Length = 0) and (FResTable[a].ResourceName = Section) then - begin - Result := True; - Exit; - end; + if fIter <> nil then FreeAndNil(fIter); + if fFileName <> '' then e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] closed', [fFileName]), MSG_NOTIFY); + fFileName := ''; end; -function TWADEditor_1.GetResource(Section, Resource: string; - var pData: Pointer; var Len: Integer): Boolean; -var - a: LongWord; - i: Integer; - WADFile: File; - CurrentSection: string; - TempData: Pointer; - OutBytes: Integer; -begin - Result := False; - - CurrentSection := ''; - - if FWADOpened = DFWAD_OPENED_NONE then - begin - FLastError := DFWAD_ERROR_WADNOTLOADED; - Exit; - end; - - Section := UpperCase(Section); - Resource := UpperCase(Resource); - - i := -1; - for a := 0 to High(FResTable) do - begin - if FResTable[a].Length = 0 then - begin - CurrentSection := FResTable[a].ResourceName; - Continue; - end; - - if (FResTable[a].ResourceName = Resource) and - (CurrentSection = Section) then - begin - i := a; - Break; - end; - end; - - if i = -1 then - begin - FLastError := DFWAD_ERROR_RESOURCENOTFOUND; - Exit; - end; - - if FWADOpened = DFWAD_OPENED_FILE then - begin - try - AssignFile(WADFile, FFileName); - Reset(WADFile, 1); - - Seek(WADFile, FResTable[i].Address+6+ - LongWord(SizeOf(TWADHeaderRec_1)+SizeOf(TResourceTableRec_1)*Length(FResTable))); - TempData := GetMemory(FResTable[i].Length); - BlockRead(WADFile, TempData^, FResTable[i].Length); - DecompressBuf(TempData, FResTable[i].Length, 0, pData, OutBytes); - FreeMem(TempData); - - Len := OutBytes; - - CloseFile(WADFile); - except - FLastError := DFWAD_ERROR_CANTOPENWAD; - CloseFile(WADFile); - Exit; - end; - end - else - begin - TempData := GetMemory(FResTable[i].Length); - CopyMemory(TempData, Pointer(LongWord(FResData)+FResTable[i].Address+6+ - LongWord(SizeOf(TWADHeaderRec_1)+SizeOf(TResourceTableRec_1)*Length(FResTable))), - FResTable[i].Length); - DecompressBuf(TempData, FResTable[i].Length, 0, pData, OutBytes); - FreeMem(TempData); - - Len := OutBytes; - end; - - FLastError := DFWAD_NOERROR; - Result := True; -end; -function TWADEditor_1.GetResourcesList(Section: string): SArray; +function TWADEditor_1.GetResource (Section, Resource: string; var pData: Pointer; var Len: Integer): Boolean; var - a: Integer; - CurrentSection: Char16; + f: Integer; + fi: TSFSFileInfo; + fs: TStream; + fn: string; begin - Result := nil; - - if FResTable = nil then Exit; - if Length(Section) > 16 then Exit; - - CurrentSection := ''; - - for a := 0 to High(FResTable) do - begin - if FResTable[a].Length = 0 then - begin - CurrentSection := FResTable[a].ResourceName; - Continue; - end; - - if CurrentSection = Section then + Result := False; + if not isOpen or (fIter = nil) then Exit; + if (length(Section) <> 0) and (Section[length(Section)] <> '/') then Section := Section+'/'; + for f := 0 to fIter.Count-1 do begin - SetLength(Result, Length(Result)+1); - Result[High(Result)] := FResTable[a].ResourceName; + fi := fIter.Files[f]; + if fi = nil then continue; + //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s] (%d, %d)', [Section, Resource, fFileName, fi.path, fi.name, SFSStrComp(fi.path, Section), SFSStrComp(fi.name, Resource)]), MSG_NOTIFY); + if (SFSStrComp(fi.path, Section) = 0) and (SFSStrComp(fi.name, Resource) = 0) then + begin + // i found her! + fn := fFileName+'::'+fi.path+fi.name; + fs := SFSFileOpen(fn); + if fs = nil then + begin + e_WriteLog(Format('DFWAD: can''t open file [%s]', [fn]), MSG_NOTIFY); + break; + end; + Len := Integer(fs.size); + GetMem(pData, Len); + fs.ReadBuffer(pData^, Len); + fs.Free; + result := true; + e_WriteLog(Format('DFWAD: file [%s%s] FOUND in [%s]; size is %d bytes', [Section, Resource, fFileName, Len]), MSG_NOTIFY); + exit; + end; end; - end; + e_WriteLog(Format('DFWAD: file [%s%s] not found in [%s]', [Section, Resource, fFileName]), MSG_WARNING); end; -function TWADEditor_1.GetSectionList(): SArray; + +function TWADEditor_1.GetResourcesList (Section: string): SArray; var - i: DWORD; + f: Integer; + fi: TSFSFileInfo; begin - Result := nil; - - if FResTable = nil then Exit; - - if FResTable[0].Length <> 0 then - begin - SetLength(Result, 1); - Result[0] := ''; - end; - - for i := 0 to High(FResTable) do - if FResTable[i].Length = 0 then + Result := nil; + if not isOpen or (fIter = nil) then Exit; + if (length(Section) <> 0) and (Section[length(Section)] <> '/') then Section := Section+'/'; + for f := 0 to fIter.Count-1 do begin - SetLength(Result, Length(Result)+1); - Result[High(Result)] := FResTable[i].ResourceName; + fi := fIter.Files[f]; + if fi = nil then continue; + if SFSStrComp(fi.path, Section) = 0 then + begin + SetLength(result, Length(result)+1); + result[high(result)] := fi.name; + end; end; end; -function TWADEditor_1.LastErrorString(): string; + +function TWADEditor_1.ReadFile (FileName: string): Boolean; begin - case FLastError of - DFWAD_NOERROR: Result := ''; - DFWAD_ERROR_WADNOTFOUND: Result := 'DFWAD file not found'; - DFWAD_ERROR_CANTOPENWAD: Result := 'Can''t open DFWAD file'; - DFWAD_ERROR_RESOURCENOTFOUND: Result := 'Resource not found'; - DFWAD_ERROR_FILENOTWAD: Result := 'File is not DFWAD'; - DFWAD_ERROR_WADNOTLOADED: Result := 'DFWAD file is not loaded'; - DFWAD_ERROR_READRESOURCE: Result := 'Read resource error'; - DFWAD_ERROR_READWAD: Result := 'Read DFWAD error'; - else Result := 'Unknown DFWAD error'; - end; + Result := False; + e_WriteLog(Format('TWADEditor_1.ReadFile: [%s]', [FileName]), MSG_NOTIFY); + FreeWAD(); + if not FileExists(FileName) then Exit; + fIter := SFSFileList(FileName); + if fIter = nil then Exit; + fFileName := FileName; + e_WriteLog(Format('TWADEditor_1.ReadFile: [%s] opened', [fFileName]), MSG_NOTIFY); + Result := True; end; -function TWADEditor_1.ReadFile(FileName: string): Boolean; + +var + uniqueCounter: Integer = 0; + +function TWADEditor_1.ReadMemory (Data: Pointer; Len: LongWord): Boolean; var - WADFile: File; Signature: array[0..4] of Char; a: Integer; + fn: string; + st: TStream = nil; begin - FreeWAD(); + Result := False; + FreeWAD(); + if (Data = nil) or (Len = 0) then Exit; - Result := False; + fn := Format(' -- memwad %d -- ', [uniqueCounter]); + Inc(uniqueCounter); + e_WriteLog(Format('TWADEditor_1.ReadMemory: [%s]', [fn]), MSG_NOTIFY); - if not FileExists(FileName) then - begin - FLastError := DFWAD_ERROR_WADNOTFOUND; - Exit; - end; - - FFileName := FileName; - - AssignFile(WADFile, FFileName); - - try - Reset(WADFile, 1); - except - FLastError := DFWAD_ERROR_CANTOPENWAD; - Exit; - end; - - try - BlockRead(WADFile, Signature, 5); - if Signature <> DFWAD_SIGNATURE then - begin - FLastError := DFWAD_ERROR_FILENOTWAD; - CloseFile(WADFile); - Exit; - end; - - BlockRead(WADFile, FVersion, 1); - if FVersion <> DFWAD_VERSION then - begin - FLastError := DFWAD_ERROR_WRONGVERSION; - CloseFile(WADFile); + try + st := TSFSMemoryStreamRO.Create(Data, Len); + if not SFSAddSubDataFile(fn, st) then + begin + st.Free; + Exit; + end; + except + st.Free; Exit; end; - BlockRead(WADFile, FHeader, SizeOf(TWADHeaderRec_1)); - SetLength(FResTable, FHeader.RecordsCount); - if FResTable <> nil then - begin - BlockRead(WADFile, FResTable[0], SizeOf(TResourceTableRec_1)*FHeader.RecordsCount); - - for a := 0 to High(FResTable) do - if FResTable[a].Length <> 0 then - FResTable[a].Address := FResTable[a].Address-6-(LongWord(SizeOf(TWADHeaderRec_1)+ - SizeOf(TResourceTableRec_1)*Length(FResTable))); - end; + fIter := SFSFileList(fn); + if fIter = nil then Exit; - CloseFile(WADFile); - except - FLastError := DFWAD_ERROR_READWAD; - CloseFile(WADFile); - Exit; - end; + fFileName := fn; + e_WriteLog(Format('TWADEditor_1.ReadMemory: [%s] opened', [fFileName]), MSG_NOTIFY); - FWADOpened := DFWAD_OPENED_FILE; - FLastError := DFWAD_NOERROR; - Result := True; + Result := True; end; -function TWADEditor_1.ReadMemory(Data: Pointer; Len: LongWord): Boolean; -var - Signature: array[0..4] of Char; - a: Integer; -begin - FreeWAD(); - - Result := False; - - CopyMemory(@Signature[0], Data, 5); - if Signature <> DFWAD_SIGNATURE then - begin - FLastError := DFWAD_ERROR_FILENOTWAD; - Exit; - end; - - CopyMemory(@FVersion, Pointer(LongWord(Data)+5), 1); - if FVersion <> DFWAD_VERSION then - begin - FLastError := DFWAD_ERROR_WRONGVERSION; - Exit; - end; - - CopyMemory(@FHeader, Pointer(LongWord(Data)+6), SizeOf(TWADHeaderRec_1)); - - SetLength(FResTable, FHeader.RecordsCount); - if FResTable <> nil then - begin - CopyMemory(@FResTable[0], Pointer(LongWord(Data)+6+SizeOf(TWADHeaderRec_1)), - SizeOf(TResourceTableRec_1)*FHeader.RecordsCount); - - for a := 0 to High(FResTable) do - if FResTable[a].Length <> 0 then - FResTable[a].Address := FResTable[a].Address-6-(LongWord(SizeOf(TWADHeaderRec_1)+ - SizeOf(TResourceTableRec_1)*Length(FResTable))); - end; - - GetMem(FResData, Len); - CopyMemory(FResData, Data, Len); - - FWADOpened := DFWAD_OPENED_MEMORY; - FLastError := DFWAD_NOERROR; - - Result := True; -end; +begin + sfsDiskDirs := '/data'; //FIXME end.