hedgewars/uUtils.pas
branchqmlfrontend
changeset 11612 f0dcdbb9b2fe
parent 11071 3851ce4f2061
child 11638 215b837c16f9
equal deleted inserted replaced
11611:99966b4a6e1e 11612:f0dcdbb9b2fe
    93 procedure initModule(isNotPreview: boolean);
    93 procedure initModule(isNotPreview: boolean);
    94 procedure freeModule;
    94 procedure freeModule;
    95 
    95 
    96 
    96 
    97 implementation
    97 implementation
    98 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
    98 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils, uPhysFSLayer;
    99 
    99 
   100 {$IFDEF DEBUGFILE}
   100 {$IFDEF DEBUGFILE}
   101 var logFile: textfile;
   101 var logFile: PFSFile;
   102 {$IFDEF USE_VIDEO_RECORDING}
   102 {$IFDEF USE_VIDEO_RECORDING}
   103     logMutex: TRTLCriticalSection; // mutex for debug file
   103     logMutex: TRTLCriticalSection; // mutex for debug file
   104 {$ENDIF}
   104 {$ENDIF}
   105 {$ENDIF}
   105 {$ENDIF}
   106 var CharArray: array[0..255] of Char;
   106 var CharArray: array[0..255] of Char;
   223 begin
   223 begin
   224 str(n, IntToStr)
   224 str(n, IntToStr)
   225 end;
   225 end;
   226 
   226 
   227 function StrToInt(s: shortstring): LongInt;
   227 function StrToInt(s: shortstring): LongInt;
   228 var c: LongInt;
   228 begin
   229 begin
       
   230 {$IFDEF PAS2C}
       
   231 val(s, StrToInt);
   229 val(s, StrToInt);
   232 {$ELSE}
       
   233 val(s, StrToInt, c);
       
   234 {$IFDEF DEBUGFILE}
       
   235 if c <> 0 then
       
   236     writeln(logFile, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
       
   237 {$ENDIF}
       
   238 {$ENDIF}
       
   239 end;
   230 end;
   240 
   231 
   241 function FloatToStr(n: hwFloat): shortstring;
   232 function FloatToStr(n: hwFloat): shortstring;
   242 begin
   233 begin
   243 FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue))
   234 FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue))
   359 {$IFDEF DEBUGFILE}
   350 {$IFDEF DEBUGFILE}
   360 
   351 
   361 {$IFDEF USE_VIDEO_RECORDING}
   352 {$IFDEF USE_VIDEO_RECORDING}
   362 EnterCriticalSection(logMutex);
   353 EnterCriticalSection(logMutex);
   363 {$ENDIF}
   354 {$ENDIF}
   364 writeln(logFile, inttostr(GameTicks)  + ': ' + s);
   355 pfsWriteLn(logFile, inttostr(GameTicks)  + ': ' + s);
   365 flush(logFile);
       
   366 
   356 
   367 {$IFDEF USE_VIDEO_RECORDING}
   357 {$IFDEF USE_VIDEO_RECORDING}
   368 LeaveCriticalSection(logMutex);
   358 LeaveCriticalSection(logMutex);
   369 {$ENDIF}
   359 {$ENDIF}
   370 
   360 
   377 {$IFNDEF PAS2C}
   367 {$IFNDEF PAS2C}
   378 {$IFDEF DEBUGFILE}
   368 {$IFDEF DEBUGFILE}
   379 {$IFDEF USE_VIDEO_RECORDING}
   369 {$IFDEF USE_VIDEO_RECORDING}
   380 EnterCriticalSection(logMutex);
   370 EnterCriticalSection(logMutex);
   381 {$ENDIF}
   371 {$ENDIF}
   382 write(logFile, s);
   372 // TODO: uncomment next two lines
   383 flush(logFile);
   373 // write(logFile, s);
       
   374 // flush(logFile);
   384 {$IFDEF USE_VIDEO_RECORDING}
   375 {$IFDEF USE_VIDEO_RECORDING}
   385 LeaveCriticalSection(logMutex);
   376 LeaveCriticalSection(logMutex);
   386 {$ENDIF}
   377 {$ENDIF}
   387 {$ENDIF}
   378 {$ENDIF}
   388 {$ENDIF}
   379 {$ENDIF}
   519 
   510 
   520 procedure initModule(isNotPreview: boolean);
   511 procedure initModule(isNotPreview: boolean);
   521 {$IFDEF DEBUGFILE}
   512 {$IFDEF DEBUGFILE}
   522 var logfileBase: shortstring;
   513 var logfileBase: shortstring;
   523     i: LongInt;
   514     i: LongInt;
   524     rwfailed: boolean;
       
   525 {$ENDIF}
   515 {$ENDIF}
   526 begin
   516 begin
   527 {$IFDEF DEBUGFILE}
   517 {$IFDEF DEBUGFILE}
   528     if isNotPreview then
   518     if isNotPreview then
   529     begin
   519     begin
   544         {$ENDIF}
   534         {$ENDIF}
   545 {$IFDEF USE_VIDEO_RECORDING}
   535 {$IFDEF USE_VIDEO_RECORDING}
   546     InitCriticalSection(logMutex);
   536     InitCriticalSection(logMutex);
   547 {$ENDIF}
   537 {$ENDIF}
   548 {$I-}
   538 {$I-}
   549     rwfailed:= false;
   539     // if log is locked, write to the next one
   550     (*if (length(UserPathPrefix) > 0) then
   540     i:= 0;
   551         begin
   541     while(i < 7) do
   552         {$IFNDEF PAS2C}
   542     begin
   553         // create directory if it doesn't exist
   543         logFile:= pfsOpenWrite('/Logs/' + logfileBase + inttostr(i) + '.log');
   554         if not FileExists(UserPathPrefix + '/Logs/') then
   544         if logFile <> nil then
   555             CreateDir(UserPathPrefix + '/Logs/');
   545             break;
   556         {$ENDIF}
   546         inc(i)
   557         // if log is locked, write to the next one
   547     end;
   558         i:= 0;
       
   559         while(i < 7) do
       
   560             begin
       
   561             assign(logFile, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
       
   562             Rewrite(logFile);
       
   563             // note: IOResult is a function in pascal and a variable in pas2c
       
   564             rwfailed:= (IOResult <> 0);
       
   565             if (not rwfailed) then
       
   566                 break;
       
   567             inc(i)
       
   568             end;
       
   569         end;
       
   570     *)
       
   571 {$IFNDEF PAS2C}
       
   572     // if everything fails, write to stderr
       
   573     //if (length(UserPathPrefix) = 0) or (rwfailed) then
       
   574         logFile:= stderr;
       
   575 {$ENDIF}
       
   576 {$I+}
   548 {$I+}
   577 {$ENDIF}
   549 {$ENDIF}
   578 
   550 
   579     //mobile stuff
   551     //mobile stuff
   580 {$IFDEF IPHONEOS}
   552 {$IFDEF IPHONEOS}
   592 end;
   564 end;
   593 
   565 
   594 procedure freeModule;
   566 procedure freeModule;
   595 begin
   567 begin
   596 {$IFDEF DEBUGFILE}
   568 {$IFDEF DEBUGFILE}
   597     writeln(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   569     pfsWriteLn(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   598     flush(logFile);
   570     pfsFlush(logFile);
   599     close(logFile);
   571     pfsClose(logFile);
   600 {$IFDEF USE_VIDEO_RECORDING}
   572 {$IFDEF USE_VIDEO_RECORDING}
   601     DoneCriticalSection(logMutex);
   573     DoneCriticalSection(logMutex);
   602 {$ENDIF}
   574 {$ENDIF}
   603 {$ENDIF}
   575 {$ENDIF}
   604 end;
   576 end;