index 0e383c3a565d8317103f87a0170500f4945f7eff..85a78356f67bd3e6794c5394d075320b4a011f82 100644 (file)
+{$ASSERTIONS ON}
unit g_resources;
interface
(**
g_GetResourceSection
Parse path in form 'path/to/file.wad:some/section/resouce' to
- wad = 'path/to/file.wa', section = 'some/section', name = 'resource'
+ wad = 'path/to/file.wad', section = 'some/section', name = 'resource'
g_DeleteFile
Delete file if it exists. Make backup if enabled.
+ return true when file not exists.
g_ReadResource
Read whole file from wad
Backup: Boolean;
procedure g_GetResourceSection (path: String; out wad, section, name: String);
- procedure g_DeleteFile(wad: String; backupPostfix: String = '.bak');
+ function g_DeleteFile(wad: String; backupPostfix: String = '.bak'): Boolean;
procedure g_ReadResource (wad, section, name: String; out data: PByte; out len: Integer);
procedure g_ReadSubResource (wad, section0, name0, section1, name1: String; out data: PByte; out len: Integer);
wad := Copy(path, 1, i - 1);
end;
- procedure g_DeleteFile (wad: String; backupPostfix: String = '.bak');
- var newwad: String;
+ function g_DeleteFile (wad: String; backupPostfix: String = '.bak'): Boolean;
+ var newwad: String; ok: Boolean;
begin
- if Backup then
+ SFSGCCollect;
+ SFSGCCollect;
+ SFSGCCollect;
+ ok := true;
+ if FileExists(wad) then
begin
- if FileExists(wad) then
+ if Backup then
begin
newwad := wad + backupPostfix;
- if FileExists(newwad) then
- ASSERT(DeleteFile(newwad));
- ASSERT(RenameFile(wad, newwad))
+ if FileExists(newwad) then ok := DeleteFile(newwad);
+ if ok then ok := RenameFile(wad, newwad);
end
- end
- else
- begin
- if FileExists(wad) then
- ASSERT(DeleteFile(wad))
- end
+ else
+ ok := DeleteFile(wad);
+ end;
+ result := ok;
end;
procedure g_AddResourceToDFWAD (wad, section, name: String; const data: PByte; len: Integer; out res: Integer);
var f: TWADEditor_1;
begin
res := 1; (* error *)
- wad := utf2win(wad);
section := utf2win(NoTrailing(section));
name := utf2win(name);
ASSERT(name <> '');
tmp, path: String;
ts: TFileStream;
dir: array of TFileInfo;
+ ok: Boolean;
procedure Add (name: String; data: PByte; len: Integer);
var ds: TSFSMemoryChunkStream;
path := NoTrailing(list.Files[i].path);
if (path <> section) or (list.Files[i].name <> name) then
begin
- g_ReadResource(wad, path, list.Files[i].name, data0, len0);
+ g_ReadResource(wad, win2utf(path), win2utf(list.Files[i].name), data0, len0);
ASSERT(data0 <> nil);
if path = '' then
path := list.Files[i].name
dfzip.writeCentralDir(ts, dir);
ts.Free;
- g_DeleteFile(wad);
- ASSERT(RenameFile(tmp, wad));
- res := 0
+ ok := g_DeleteFile(wad);
+ if not ok then e_WriteLog('Cant delete older wad [' + wad + ']', TRecordCategory.MSG_WARNING);
+ ok := RenameFile(tmp, wad);
+ if not ok then e_WriteLog('ERROR: Cant rename [' + tmp + '] -> [' + wad + ']', TRecordCategory.MSG_WARNING);
+ if ok then res := 0 else res := 2;
end;
procedure g_AddResource (wad, section, name: String; const data: PByte; len: Integer; out res: Integer);
tmp, path: String;
ts: TFileStream;
dir: array of TFileInfo;
+ ok: Boolean;
procedure Add (name: String; data: PByte; len: Integer);
var ds: TSFSMemoryChunkStream;
path := NoTrailing(list.Files[i].path);
if (path <> section) or (list.Files[i].name <> name) then
begin
- g_ReadResource(wad, path, list.Files[i].name, data0, len0);
+ g_ReadResource(wad, win2utf(path), win2utf(list.Files[i].name), data0, len0);
ASSERT(data0 <> nil);
if path = '' then
path := list.Files[i].name
dfzip.writeCentralDir(ts, dir);
ts.Free;
- g_DeleteFile(wad);
- ASSERT(RenameFile(tmp, wad));
- res := 0
+ ok := g_DeleteFile(wad);
+ if not ok then e_WriteLog('Cant delete older wad [' + wad + ']', TRecordCategory.MSG_WARNING);
+ ok := RenameFile(tmp, wad);
+ if not ok then e_WriteLog('ERROR: Cant rename [' + tmp + '] -> [' + wad + ']', TRecordCategory.MSG_WARNING);
+ if ok then res := 0 else res := 2;
end;
procedure g_DeleteResource (wad, section, name: String; out res: Integer);
for i := 0 to len - 1 do
data[i] := stream1.ReadByte();
stream1.Destroy
+ //stream0.Destroy (* leads to memory corruption, it destroyed with stream1? *)
+ end
+ else
+ begin
+ stream0.Destroy
end
end
- end
- else
- begin
- stream0.Destroy
+ else
+ begin
+ stream0.Destroy
+ end
end
end;
SFSGCCollect