hedgewars/uUtils.pas
changeset 11632 cac74d9075be
parent 11046 47a8c19ecb60
child 11633 215b837c16f9
child 11743 eefa04b23c3b
equal deleted inserted replaced
11631:b3db79b56f28 11632:cac74d9075be
    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
   543         logfileBase:= 'preview';
   533         logfileBase:= 'preview';
   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     // if log is locked, write to the next one
   549     rwfailed:= false;
   539     i:= 0;
   550     if (length(UserPathPrefix) > 0) then
   540     while(i < 7) do
   551         begin
   541     begin
   552         {$IFNDEF PAS2C}
   542         logFile:= pfsOpenWrite('/Logs/' + logfileBase + inttostr(i) + '.log');
   553         // create directory if it doesn't exist
   543         if logFile <> nil then
   554         if not FileExists(UserPathPrefix + '/Logs/') then
   544             break;
   555             CreateDir(UserPathPrefix + '/Logs/');
   545         inc(i)
   556         {$ENDIF}
   546     end;
   557         // if log is locked, write to the next one
       
   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+}
       
   577 {$ENDIF}
   547 {$ENDIF}
   578 
   548 
   579     //mobile stuff
   549     //mobile stuff
   580 {$IFDEF IPHONEOS}
   550 {$IFDEF IPHONEOS}
   581     mobileRecord.PerformRumble:= @AudioServicesPlaySystemSound;
   551     mobileRecord.PerformRumble:= @AudioServicesPlaySystemSound;
   592 end;
   562 end;
   593 
   563 
   594 procedure freeModule;
   564 procedure freeModule;
   595 begin
   565 begin
   596 {$IFDEF DEBUGFILE}
   566 {$IFDEF DEBUGFILE}
   597     writeln(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   567     pfsWriteLn(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   598     flush(logFile);
   568     pfsFlush(logFile);
   599     close(logFile);
   569     pfsClose(logFile);
   600 {$IFDEF USE_VIDEO_RECORDING}
   570 {$IFDEF USE_VIDEO_RECORDING}
   601     DoneCriticalSection(logMutex);
   571     DoneCriticalSection(logMutex);
   602 {$ENDIF}
   572 {$ENDIF}
   603 {$ENDIF}
   573 {$ENDIF}
   604 end;
   574 end;