DEADSOFTWARE

fix building tools
[d2df-sdl.git] / src / tools / mapcvt.dpr
1 {$INCLUDE ../shared/a_modes.inc}
2 {$IFDEF WINDOWS}
3 {$APPTYPE CONSOLE}
4 {$ENDIF}
6 uses
7 SysUtils, Classes,
8 sfs in '../sfs/sfs.pas',
9 sfsPlainFS in '../sfs/sfsPlainFS.pas',
10 sfsZipFS in '../sfs/sfsZipFS.pas',
11 mempool in '../shared/mempool.pas',
12 xstreams in '../shared/xstreams.pas',
13 xparser in '../shared/xparser.pas',
14 xdynrec in '../shared/xdynrec.pas',
15 xprofiler in '../shared/xprofiler.pas',
16 utils in '../shared/utils.pas',
17 hashtable in '../shared/hashtable.pas',
18 conbuf in '../shared/conbuf.pas',
19 e_log in '../engine/e_log.pas',
20 wadreader in '../shared/wadreader.pas',
21 MAPDEF in '../shared/MAPDEF.pas';
24 // ////////////////////////////////////////////////////////////////////////// //
25 var
26 pr: TTextParser;
27 dfmapdef: TDynMapDef;
28 wr: TTextWriter;
29 map: TDynRecord;
30 st: TStream;
31 stt: UInt64;
32 inname: AnsiString = '';
33 outname: AnsiString = '';
34 totext: Integer = -1; // <0: guess; force outname extension
35 //sign: packed array[0..3] of AnsiChar;
36 wad: TWADFile = nil;
37 waddata: Pointer;
38 waddlen: Integer;
39 wasbin: Boolean = false;
40 lostdata: Boolean;
41 begin
42 if (ParamCount = 0) then
43 begin
44 writeln('usage: mapcvt inname outname');
45 Halt(1);
46 end;
48 inname := ParamStr(1);
49 //writeln('inname: [', inname, ']');
50 if (ParamCount = 1) then
51 begin
52 outname := forceFilenameExt(ParamStr(1), '');
53 if isWadPath(outname) then
54 begin
55 outname := SFSReplacePathDelims(g_ExtractFilePathName(outname), '/');
56 if (Length(outname) = 0) then begin writeln('FATAL: can''t guess output name!'); Halt(1); end;
57 end;
58 end
59 else
60 begin
61 outname := ParamStr(2);
62 if StrEquCI1251(getFilenameExt(outname), '.txt') then totext := 1
63 else if StrEquCI1251(getFilenameExt(outname), '.map') then totext := 0
64 else if StrEquCI1251(getFilenameExt(outname), '.dfmap') then totext := 0
65 else if (Length(getFilenameExt(outname)) = 0) then totext := -1
66 else begin writeln('FATAL: can''t guess output format!'); Halt(1); end;
67 end;
68 //writeln('outname: [', outname, ']; totext=', totext);
70 e_InitWritelnDriver();
71 conbufDumpToStdOut := true;
72 conbufConPrefix := false;
74 writeln('parsing "mapdef.txt"...');
75 //pr := TFileTextParser.Create('mapdef.txt');
76 pr := TStrTextParser.Create(defaultMapDef);
77 try
78 dfmapdef := TDynMapDef.Create(pr);
79 except on e: Exception do
80 begin
81 writeln('ERROR at (', pr.line, ',', pr.col, '): ', e.message);
82 Halt(1);
83 end;
84 end;
86 writeln('parsing "', inname, '"...');
88 if isWadPath(inname) then
89 begin
90 wad := TWADFile.Create();
91 wad.ReadFile(g_ExtractWadName(inname));
92 wad.GetMapResource(g_ExtractFilePathName(inname), waddata, waddlen);
93 st := TSFSMemoryChunkStream.Create(waddata, waddlen, true);
94 wad.Free();
95 end
96 else
97 begin
98 st := openDiskFileRO(inname);
99 end;
101 try
102 stt := getTimeMicro();
103 map := dfmapdef.parseMap(st, @wasbin);
104 stt := getTimeMicro()-stt;
105 if wasbin then write('binary') else write('text');
106 writeln(' map parsed in ', stt div 1000, '.', stt mod 1000, ' milliseconds');
107 except
108 on e: TDynParseException do
109 begin
110 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
111 Halt(1);
112 end;
113 on E: Exception do
114 begin
115 writeln('ERROR: ', e.message);
116 Halt(1);
117 end;
118 end;
120 {$IF DEFINED(D2D_DYNREC_PROFILER)}xdynDumpProfiles();{$ENDIF}
122 if (totext < 0) then
123 begin
124 if wasbin then begin outname := forceFilenameExt(outname, '.txt'); totext := 1; end
125 else begin outname := forceFilenameExt(outname, '.map'); totext := 0; end;
126 end;
128 assert(totext >= 0);
130 writeln('writing "', outname, '"...');
131 st := createDiskFile(outname);
132 if (totext = 0) then
133 begin
134 // write binary map
135 lostdata := false;
136 stt := getTimeMicro();
137 map.writeBinTo(lostdata, st);
138 stt := getTimeMicro()-stt;
139 if lostdata then writeln('***WARNING! some data was lost due to binary format limitations!');
140 write('binary');
141 end
142 else
143 begin
144 // write text map
145 wr := TFileTextWriter.Create(st);
146 stt := getTimeMicro();
147 map.writeTo(wr);
148 stt := getTimeMicro()-stt;
149 wr.Free();
150 write('text');
151 end;
152 writeln(' map written in ', stt div 1000, '.', stt mod 1000, ' milliseconds');
153 end.