X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Feditor%2Fg_resources.pas;h=85a78356f67bd3e6794c5394d075320b4a011f82;hb=ce2fbcabd169f7ae0ebcffaba3c01e41050aa4e2;hp=0e383c3a565d8317103f87a0170500f4945f7eff;hpb=ecf69fd49c810dc4fe78092ec1f0204cbb719e01;p=d2df-editor.git diff --git a/src/editor/g_resources.pas b/src/editor/g_resources.pas index 0e383c3..85a7835 100644 --- a/src/editor/g_resources.pas +++ b/src/editor/g_resources.pas @@ -1,3 +1,4 @@ +{$ASSERTIONS ON} unit g_resources; interface @@ -5,10 +6,11 @@ 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 @@ -39,7 +41,7 @@ interface 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); @@ -97,31 +99,31 @@ implementation 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 <> ''); @@ -147,6 +149,7 @@ implementation tmp, path: String; ts: TFileStream; dir: array of TFileInfo; + ok: Boolean; procedure Add (name: String; data: PByte; len: Integer); var ds: TSFSMemoryChunkStream; @@ -176,7 +179,7 @@ implementation 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 @@ -198,9 +201,11 @@ implementation 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); @@ -245,6 +250,7 @@ implementation tmp, path: String; ts: TFileStream; dir: array of TFileInfo; + ok: Boolean; procedure Add (name: String; data: PByte; len: Integer); var ds: TSFSMemoryChunkStream; @@ -274,7 +280,7 @@ implementation 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 @@ -290,9 +296,11 @@ implementation 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); @@ -386,12 +394,17 @@ implementation 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