hedgewars/uUtils.pas
branchqmlfrontend
changeset 10606 8e95911cb86b
parent 10515 7705784902e1
parent 10605 df7a73db2c43
child 10748 dc587913987c
equal deleted inserted replaced
10519:af019fa70080 10606:8e95911cb86b
    33 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    33 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    34 function  EnumToStr(const en : TStatInfoType) : shortstring; overload;
    34 function  EnumToStr(const en : TStatInfoType) : shortstring; overload;
    35 function  EnumToStr(const en : THogEffect) : shortstring; overload;
    35 function  EnumToStr(const en : THogEffect) : shortstring; overload;
    36 function  EnumToStr(const en : TCapGroup) : shortstring; overload;
    36 function  EnumToStr(const en : TCapGroup) : shortstring; overload;
    37 function  EnumToStr(const en : TSprite) : shortstring; overload;
    37 function  EnumToStr(const en : TSprite) : shortstring; overload;
       
    38 function  EnumToStr(const en : TMapGen) : shortstring; overload;
    38 
    39 
    39 function  Min(a, b: LongInt): LongInt; inline;
    40 function  Min(a, b: LongInt): LongInt; inline;
       
    41 function  MinD(a, b: double) : double; inline;
    40 function  Max(a, b: LongInt): LongInt; inline;
    42 function  Max(a, b: LongInt): LongInt; inline;
    41 
    43 
    42 function  IntToStr(n: LongInt): shortstring;
    44 function  IntToStr(n: LongInt): shortstring;
    43 function  StrToInt(s: shortstring): LongInt;
    45 function  StrToInt(s: shortstring): LongInt;
    44 function  FloatToStr(n: hwFloat): shortstring;
    46 function  FloatToStr(n: hwFloat): shortstring;
    93 
    95 
    94 implementation
    96 implementation
    95 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
    97 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
    96 
    98 
    97 {$IFDEF DEBUGFILE}
    99 {$IFDEF DEBUGFILE}
    98 var f: textfile;
   100 var logFile: textfile;
    99 {$IFDEF USE_VIDEO_RECORDING}
   101 {$IFDEF USE_VIDEO_RECORDING}
   100     logMutex: TRTLCriticalSection; // mutex for debug file
   102     logMutex: TRTLCriticalSection; // mutex for debug file
   101 {$ENDIF}
   103 {$ENDIF}
   102 {$ENDIF}
   104 {$ENDIF}
   103 var CharArray: array[0..255] of Char;
   105 var CharArray: array[0..255] of Char;
   183 function EnumToStr(const en: TSprite) : shortstring; overload;
   185 function EnumToStr(const en: TSprite) : shortstring; overload;
   184 begin
   186 begin
   185 EnumToStr := GetEnumName(TypeInfo(TSprite), ord(en))
   187 EnumToStr := GetEnumName(TypeInfo(TSprite), ord(en))
   186 end;
   188 end;
   187 
   189 
       
   190 function EnumToStr(const en: TMapGen) : shortstring; overload;
       
   191 begin
       
   192 EnumToStr := GetEnumName(TypeInfo(TMapGen), ord(en))
       
   193 end;
       
   194 
   188 
   195 
   189 function Min(a, b: LongInt): LongInt;
   196 function Min(a, b: LongInt): LongInt;
   190 begin
   197 begin
   191 if a < b then
   198 if a < b then
   192     Min:= a
   199     Min:= a
   193 else
   200 else
   194     Min:= b
   201     Min:= b
       
   202 end;
       
   203 
       
   204 function MinD(a, b: double): double;
       
   205 begin
       
   206 if a < b then
       
   207     MinD:= a
       
   208 else
       
   209     MinD:= b
   195 end;
   210 end;
   196 
   211 
   197 function Max(a, b: LongInt): LongInt;
   212 function Max(a, b: LongInt): LongInt;
   198 begin
   213 begin
   199 if a > b then
   214 if a > b then
   215 val(s, StrToInt);
   230 val(s, StrToInt);
   216 {$ELSE}
   231 {$ELSE}
   217 val(s, StrToInt, c);
   232 val(s, StrToInt, c);
   218 {$IFDEF DEBUGFILE}
   233 {$IFDEF DEBUGFILE}
   219 if c <> 0 then
   234 if c <> 0 then
   220     writeln(f, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
   235     writeln(logFile, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
   221 {$ENDIF}
   236 {$ENDIF}
   222 {$ENDIF}
   237 {$ENDIF}
   223 end;
   238 end;
   224 
   239 
   225 function FloatToStr(n: hwFloat): shortstring;
   240 function FloatToStr(n: hwFloat): shortstring;
   278 end;
   293 end;
   279 
   294 
   280 
   295 
   281 function DecodeBase64(s: shortstring): shortstring;
   296 function DecodeBase64(s: shortstring): shortstring;
   282 const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
   297 const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
   283 var i, t, c: Longword;
   298 var i, t, c: LongInt;
   284 begin
   299 begin
   285 c:= 0;
   300 c:= 0;
   286 for i:= 1 to Length(s) do
   301 for i:= 1 to Length(s) do
   287     begin
   302     begin
   288     t:= Pos(s[i], table);
   303     t:= Pos(s[i], table);
   343 {$IFDEF DEBUGFILE}
   358 {$IFDEF DEBUGFILE}
   344 
   359 
   345 {$IFDEF USE_VIDEO_RECORDING}
   360 {$IFDEF USE_VIDEO_RECORDING}
   346 EnterCriticalSection(logMutex);
   361 EnterCriticalSection(logMutex);
   347 {$ENDIF}
   362 {$ENDIF}
   348 writeln(f, inttostr(GameTicks)  + ': ' + s);
   363 writeln(logFile, inttostr(GameTicks)  + ': ' + s);
   349 flush(f);
   364 flush(logFile);
   350 
   365 
   351 {$IFDEF USE_VIDEO_RECORDING}
   366 {$IFDEF USE_VIDEO_RECORDING}
   352 LeaveCriticalSection(logMutex);
   367 LeaveCriticalSection(logMutex);
   353 {$ENDIF}
   368 {$ENDIF}
   354 
   369 
   361 {$IFNDEF PAS2C}
   376 {$IFNDEF PAS2C}
   362 {$IFDEF DEBUGFILE}
   377 {$IFDEF DEBUGFILE}
   363 {$IFDEF USE_VIDEO_RECORDING}
   378 {$IFDEF USE_VIDEO_RECORDING}
   364 EnterCriticalSection(logMutex);
   379 EnterCriticalSection(logMutex);
   365 {$ENDIF}
   380 {$ENDIF}
   366 write(f, s);
   381 write(logFile, s);
   367 flush(f);
   382 flush(logFile);
   368 {$IFDEF USE_VIDEO_RECORDING}
   383 {$IFDEF USE_VIDEO_RECORDING}
   369 LeaveCriticalSection(logMutex);
   384 LeaveCriticalSection(logMutex);
   370 {$ENDIF}
   385 {$ENDIF}
   371 {$ENDIF}
   386 {$ENDIF}
   372 {$ENDIF}
   387 {$ENDIF}
   503 
   518 
   504 procedure initModule(isNotPreview: boolean);
   519 procedure initModule(isNotPreview: boolean);
   505 {$IFDEF DEBUGFILE}
   520 {$IFDEF DEBUGFILE}
   506 var logfileBase: shortstring;
   521 var logfileBase: shortstring;
   507     i: LongInt;
   522     i: LongInt;
       
   523     rwfailed: boolean;
   508 {$ENDIF}
   524 {$ENDIF}
   509 begin
   525 begin
   510 {$IFDEF DEBUGFILE}
   526 {$IFDEF DEBUGFILE}
   511     if isNotPreview then
   527     if isNotPreview then
   512     begin
   528     begin
   527         {$ENDIF}
   543         {$ENDIF}
   528 {$IFDEF USE_VIDEO_RECORDING}
   544 {$IFDEF USE_VIDEO_RECORDING}
   529     InitCriticalSection(logMutex);
   545     InitCriticalSection(logMutex);
   530 {$ENDIF}
   546 {$ENDIF}
   531 {$I-}
   547 {$I-}
   532 {$IFNDEF PAS2C}
   548     rwfailed:= false;
   533     f:= stderr; // if everything fails, write to stderr
   549     (*if (length(UserPathPrefix) > 0) then
   534 {$ENDIF}
       
   535 (*    if (length(UserPathPrefix) > 0) then
       
   536         begin
   550         begin
   537         {$IFNDEF PAS2C}
   551         {$IFNDEF PAS2C}
   538         // create directory if it doesn't exist
   552         // create directory if it doesn't exist
   539         if not FileExists(UserPathPrefix + '/Logs/') then
   553         if not FileExists(UserPathPrefix + '/Logs/') then
   540             CreateDir(UserPathPrefix + '/Logs/');
   554             CreateDir(UserPathPrefix + '/Logs/');
   541         {$ENDIF}
   555         {$ENDIF}
   542         // if log is locked, write to the next one
   556         // if log is locked, write to the next one
   543         i:= 0;
   557         i:= 0;
   544         while(i < 7) do
   558         while(i < 7) do
   545             begin
   559             begin
   546             assign(f, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
   560             assign(logFile, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
   547             if IOResult = 0 then
   561             Rewrite(logFile);
       
   562             // note: IOResult is a function in pascal and a variable in pas2c
       
   563             rwfailed:= (IOResult <> 0);
       
   564             if (not rwfailed) then
   548                 break;
   565                 break;
   549             inc(i)
   566             inc(i)
   550             end;
   567             end;
   551         end;*)
   568         end;
   552     Rewrite(f);
   569     *)
       
   570 {$IFNDEF PAS2C}
       
   571     // if everything fails, write to stderr
       
   572     //if (length(UserPathPrefix) = 0) or (rwfailed) then
       
   573         logFile:= stderr;
       
   574 {$ENDIF}
   553 {$I+}
   575 {$I+}
   554 {$ENDIF}
   576 {$ENDIF}
   555 
   577 
   556     //mobile stuff
   578     //mobile stuff
   557 {$IFDEF IPHONEOS}
   579 {$IFDEF IPHONEOS}
   569 end;
   591 end;
   570 
   592 
   571 procedure freeModule;
   593 procedure freeModule;
   572 begin
   594 begin
   573 {$IFDEF DEBUGFILE}
   595 {$IFDEF DEBUGFILE}
   574     writeln(f, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   596     writeln(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
   575     flush(f);
   597     flush(logFile);
   576     close(f);
   598     close(logFile);
   577 {$IFDEF USE_VIDEO_RECORDING}
   599 {$IFDEF USE_VIDEO_RECORDING}
   578     DoneCriticalSection(logMutex);
   600     DoneCriticalSection(logMutex);
   579 {$ENDIF}
   601 {$ENDIF}
   580 {$ENDIF}
   602 {$ENDIF}
   581 end;
   603 end;