hedgewars/uUtils.pas
changeset 10015 4feced261c68
parent 9998 736015b847e3
parent 9966 01e198990211
child 10080 ac51bcb534ef
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
    23 interface
    23 interface
    24 uses uTypes, uFloat;
    24 uses uTypes, uFloat;
    25 
    25 
    26 procedure SplitBySpace(var a, b: shortstring);
    26 procedure SplitBySpace(var a, b: shortstring);
    27 procedure SplitByChar(var a, b: shortstring; c: char);
    27 procedure SplitByChar(var a, b: shortstring; c: char);
       
    28 
       
    29 {$IFNDEF PAS2C}
    28 procedure SplitByChar(var a, b: ansistring; c: char);
    30 procedure SplitByChar(var a, b: ansistring; c: char);
       
    31 {$ENDIF}
    29 
    32 
    30 function  EnumToStr(const en : TGearType) : shortstring; overload;
    33 function  EnumToStr(const en : TGearType) : shortstring; overload;
    31 function  EnumToStr(const en : TVisualGearType) : shortstring; overload;
    34 function  EnumToStr(const en : TVisualGearType) : shortstring; overload;
    32 function  EnumToStr(const en : TSound) : shortstring; overload;
    35 function  EnumToStr(const en : TSound) : shortstring; overload;
    33 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    36 function  EnumToStr(const en : TAmmoType) : shortstring; overload;
    65 function  CheckNoTeamOrHH: boolean; inline;
    68 function  CheckNoTeamOrHH: boolean; inline;
    66 
    69 
    67 function  GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
    70 function  GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
    68 function  GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
    71 function  GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
    69 
    72 
       
    73 {$IFNDEF PAS2C}
    70 procedure Write(var f: textfile; s: shortstring);
    74 procedure Write(var f: textfile; s: shortstring);
    71 procedure WriteLn(var f: textfile; s: shortstring);
    75 procedure WriteLn(var f: textfile; s: shortstring);
       
    76 {$ENDIF}
    72 
    77 
    73 function  isPhone: Boolean; inline;
    78 function  isPhone: Boolean; inline;
    74 
    79 
    75 {$IFDEF IPHONEOS}
    80 {$IFDEF IPHONEOS}
    76 procedure startLoadingIndicator; cdecl; external;
    81 procedure startLoadingIndicator; cdecl; external;
    86 procedure initModule(isNotPreview: boolean);
    91 procedure initModule(isNotPreview: boolean);
    87 procedure freeModule;
    92 procedure freeModule;
    88 
    93 
    89 
    94 
    90 implementation
    95 implementation
    91 uses typinfo, Math, uConsts, uVariables, SysUtils;
    96 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
    92 
    97 
    93 {$IFDEF DEBUGFILE}
    98 {$IFDEF DEBUGFILE}
    94 var f: textfile;
    99 var f: textfile;
    95 {$IFDEF USE_VIDEO_RECORDING}
   100 {$IFDEF USE_VIDEO_RECORDING}
    96     logMutex: TRTLCriticalSection; // mutex for debug file
   101     logMutex: TRTLCriticalSection; // mutex for debug file
    97 {$ENDIF}
   102 {$ENDIF}
    98 {$ENDIF}
   103 {$ENDIF}
    99 var CharArray: array[byte] of Char;
   104 var CharArray: array[0..255] of Char;
   100 
   105 
   101 procedure SplitBySpace(var a,b: shortstring);
   106 procedure SplitBySpace(var a,b: shortstring);
   102 begin
   107 begin
   103 SplitByChar(a,b,' ');
   108 SplitByChar(a,b,' ');
   104 end;
   109 end;
   113     for t:= 1 to Pred(i) do
   118     for t:= 1 to Pred(i) do
   114         if (a[t] >= 'A')and(a[t] <= 'Z') then
   119         if (a[t] >= 'A')and(a[t] <= 'Z') then
   115             Inc(a[t], 32);
   120             Inc(a[t], 32);
   116     b:= copy(a, i + 1, Length(a) - i);
   121     b:= copy(a, i + 1, Length(a) - i);
   117     a[0]:= char(Pred(i))
   122     a[0]:= char(Pred(i))
       
   123     {$IFDEF PAS2C}
       
   124        a[i] := 0;
       
   125     {$ENDIF}
   118     end
   126     end
   119 else
   127 else
   120     b:= '';
   128     b:= '';
   121 end;
   129 end;
   122 
   130 
       
   131 {$IFNDEF PAS2C}
   123 procedure SplitByChar(var a, b: ansistring; c: char);
   132 procedure SplitByChar(var a, b: ansistring; c: char);
   124 var i: LongInt;
   133 var i: LongInt;
   125 begin
   134 begin
   126 i:= Pos(c, a);
   135 i:= Pos(c, a);
   127 if i > 0 then
   136 if i > 0 then
   128     begin
   137     begin
   129     b:= copy(a, i + 1, Length(a) - i);
   138     b:= copy(a, i + 1, Length(a) - i);
   130     setlength(a, Pred(i));
   139     setlength(a, Pred(i));
   131     end else b:= '';
   140     end else b:= '';
   132 end;
   141 end; { SplitByChar }
       
   142 {$ENDIF}
   133 
   143 
   134 function EnumToStr(const en : TGearType) : shortstring; overload;
   144 function EnumToStr(const en : TGearType) : shortstring; overload;
   135 begin
   145 begin
   136 EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en))
   146 EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en))
   137 end;
   147 end;
   187 function IntToStr(n: LongInt): shortstring;
   197 function IntToStr(n: LongInt): shortstring;
   188 begin
   198 begin
   189 str(n, IntToStr)
   199 str(n, IntToStr)
   190 end;
   200 end;
   191 
   201 
   192 function  StrToInt(s: shortstring): LongInt;
   202 function StrToInt(s: shortstring): LongInt;
   193 var c: LongInt;
   203 var c: LongInt;
   194 begin
   204 begin
       
   205 {$IFDEF PAS2C}
       
   206 val(s, StrToInt);
       
   207 {$ELSE}
   195 val(s, StrToInt, c);
   208 val(s, StrToInt, c);
   196 {$IFDEF DEBUGFILE}
   209 {$IFDEF DEBUGFILE}
   197 if c <> 0 then
   210 if c <> 0 then
   198     writeln(f, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
   211     writeln(f, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
       
   212 {$ENDIF}
   199 {$ENDIF}
   213 {$ENDIF}
   200 end;
   214 end;
   201 
   215 
   202 function FloatToStr(n: hwFloat): shortstring;
   216 function FloatToStr(n: hwFloat): shortstring;
   203 begin
   217 begin
   288 DecodeBase64[0]:= char(t - 1)
   302 DecodeBase64[0]:= char(t - 1)
   289 end;
   303 end;
   290 
   304 
   291 
   305 
   292 function Str2PChar(const s: shortstring): PChar;
   306 function Str2PChar(const s: shortstring): PChar;
   293 begin
   307 var i :Integer ;
   294 CharArray:= s;
   308 begin
       
   309    for i:= 1 to Length(s) do
       
   310       begin
       
   311       CharArray[i - 1] := s[i];
       
   312       end;
   295 CharArray[Length(s)]:= #0;
   313 CharArray[Length(s)]:= #0;
   296 Str2PChar:= @CharArray
   314    Str2PChar:= @(CharArray[0]);
   297 end;
   315 end;
   298 
   316 
   299 
   317 
   300 function endian(independent: LongWord): LongWord; inline;
   318 function endian(independent: LongWord): LongWord; inline;
   301 begin
   319 begin
   310 end;
   328 end;
   311 
   329 
   312 
   330 
   313 procedure AddFileLog(s: shortstring);
   331 procedure AddFileLog(s: shortstring);
   314 begin
   332 begin
   315 s:= s;
   333 // s:= s;
   316 {$IFDEF DEBUGFILE}
   334 {$IFDEF DEBUGFILE}
       
   335 
   317 {$IFDEF USE_VIDEO_RECORDING}
   336 {$IFDEF USE_VIDEO_RECORDING}
   318 EnterCriticalSection(logMutex);
   337 EnterCriticalSection(logMutex);
   319 {$ENDIF}
   338 {$ENDIF}
   320 writeln(f, inttostr(GameTicks)  + ': ' + s);
   339 writeln(f, inttostr(GameTicks)  + ': ' + s);
   321 flush(f);
   340 flush(f);
       
   341 
   322 {$IFDEF USE_VIDEO_RECORDING}
   342 {$IFDEF USE_VIDEO_RECORDING}
   323 LeaveCriticalSection(logMutex);
   343 LeaveCriticalSection(logMutex);
   324 {$ENDIF}
   344 {$ENDIF}
       
   345 
   325 {$ENDIF}
   346 {$ENDIF}
   326 end;
   347 end;
   327 
   348 
   328 procedure AddFileLogRaw(s: pchar); cdecl;
   349 procedure AddFileLogRaw(s: pchar); cdecl;
   329 begin
   350 begin
   330 s:= s;
   351 s:= s;
       
   352 {$IFNDEF PAS2C}
   331 {$IFDEF DEBUGFILE}
   353 {$IFDEF DEBUGFILE}
   332 {$IFDEF USE_VIDEO_RECORDING}
   354 {$IFDEF USE_VIDEO_RECORDING}
   333 EnterCriticalSection(logMutex);
   355 EnterCriticalSection(logMutex);
   334 {$ENDIF}
   356 {$ENDIF}
   335 write(f, s);
   357 write(f, s);
   336 flush(f);
   358 flush(f);
   337 {$IFDEF USE_VIDEO_RECORDING}
   359 {$IFDEF USE_VIDEO_RECORDING}
   338 LeaveCriticalSection(logMutex);
   360 LeaveCriticalSection(logMutex);
       
   361 {$ENDIF}
   339 {$ENDIF}
   362 {$ENDIF}
   340 {$ENDIF}
   363 {$ENDIF}
   341 end;
   364 end;
   342 
   365 
   343 function CheckCJKFont(s: ansistring; font: THWFont): THWFont;
   366 function CheckCJKFont(s: ansistring; font: THWFont): THWFont;
   368        ((#$4E00  <= u) and (u <= #$9FFF))  or // CJK Unified Ideographs
   391        ((#$4E00  <= u) and (u <= #$9FFF))  or // CJK Unified Ideographs
   369        ((#$AC00  <= u) and (u <= #$D7AF))  or // Hangul Syllables
   392        ((#$AC00  <= u) and (u <= #$D7AF))  or // Hangul Syllables
   370        ((#$F900  <= u) and (u <= #$FAFF))  or // CJK Compatibility Ideographs
   393        ((#$F900  <= u) and (u <= #$FAFF))  or // CJK Compatibility Ideographs
   371        ((#$FE30  <= u) and (u <= #$FE4F))  or // CJK Compatibility Forms
   394        ((#$FE30  <= u) and (u <= #$FE4F))  or // CJK Compatibility Forms
   372        ((#$FF66  <= u) and (u <= #$FF9D)))    // halfwidth katakana
   395        ((#$FF66  <= u) and (u <= #$FF9D)))    // halfwidth katakana
   373        then 
   396        then
   374         begin
   397         begin
   375             CheckCJKFont:=  THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) );
   398             CheckCJKFont:=  THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) );
   376             exit;
   399             exit;
   377         end;
   400         end;
   378     inc(i)
   401     inc(i)
   406 function CheckNoTeamOrHH: boolean;
   429 function CheckNoTeamOrHH: boolean;
   407 begin
   430 begin
   408 CheckNoTeamOrHH:= (CurrentTeam = nil) or (CurrentHedgehog^.Gear = nil);
   431 CheckNoTeamOrHH:= (CurrentTeam = nil) or (CurrentHedgehog^.Gear = nil);
   409 end;
   432 end;
   410 
   433 
       
   434 {$IFNDEF PAS2C}
   411 procedure Write(var f: textfile; s: shortstring);
   435 procedure Write(var f: textfile; s: shortstring);
   412 begin
   436 begin
   413 system.write(f, s)
   437 system.write(f, s)
   414 end;
   438 end;
   415 
   439 
   416 procedure WriteLn(var f: textfile; s: shortstring);
   440 procedure WriteLn(var f: textfile; s: shortstring);
   417 begin
   441 begin
   418 system.writeln(f, s)
   442 system.writeln(f, s)
   419 end;
   443 end;
   420 
   444 {$ENDIF}
   421 
   445 
   422 // this function is just to determine whether we are running on a limited screen device
   446 // this function is just to determine whether we are running on a limited screen device
   423 function isPhone: Boolean; inline;
   447 function isPhone: Boolean; inline;
   424 begin
   448 begin
   425     isPhone:= false;
   449     isPhone:= false;
   442     for i:= 1 to length(s) do
   466     for i:= 1 to length(s) do
   443         if (s[i] < #32) or (s[i] > #127) then
   467         if (s[i] < #32) or (s[i] > #127) then
   444             r[i]:= '?'
   468             r[i]:= '?'
   445             else
   469             else
   446             r[i]:= s[i];
   470             r[i]:= s[i];
   447             
   471 
   448     sanitizeForLog:= r
   472     sanitizeForLog:= r
   449 end;
   473 end;
   450 
   474 
   451 function  sanitizeCharForLog(c: char): shortstring;
   475 function  sanitizeCharForLog(c: char): shortstring;
   452 var r: shortstring;
   476 var r: shortstring;
   453 begin
   477 begin
   454     if (c < #32) or (c > #127) then
   478     if (c < #32) or (c > #127) then
   455         r:= '#' + inttostr(byte(c))
   479         r:= '#' + inttostr(byte(c))
   456         else
   480         else
   457         r:= c;
   481         begin
   458             
   482         // some magic for pas2c
       
   483         r[0]:= #1;
       
   484         r[1]:= c;
       
   485         end;
       
   486 
   459     sanitizeCharForLog:= r
   487     sanitizeCharForLog:= r
   460 end;
   488 end;
   461 
   489 
   462 procedure initModule(isNotPreview: boolean);
   490 procedure initModule(isNotPreview: boolean);
   463 {$IFDEF DEBUGFILE}
   491 {$IFDEF DEBUGFILE}
   477         logfileBase:= 'preview';
   505         logfileBase:= 'preview';
   478 {$IFDEF USE_VIDEO_RECORDING}
   506 {$IFDEF USE_VIDEO_RECORDING}
   479     InitCriticalSection(logMutex);
   507     InitCriticalSection(logMutex);
   480 {$ENDIF}
   508 {$ENDIF}
   481 {$I-}
   509 {$I-}
       
   510 {$IFNDEF PAS2C}
   482     f:= stderr; // if everything fails, write to stderr
   511     f:= stderr; // if everything fails, write to stderr
       
   512 {$ENDIF}
   483     if (UserPathPrefix <> '') then
   513     if (UserPathPrefix <> '') then
   484         begin
   514         begin
       
   515         {$IFNDEF PAS2C}
   485         // create directory if it doesn't exist
   516         // create directory if it doesn't exist
   486         if not FileExists(UserPathPrefix + '/Logs/') then
   517         if not FileExists(UserPathPrefix + '/Logs/') then
   487             CreateDir(UserPathPrefix + '/Logs/');
   518             CreateDir(UserPathPrefix + '/Logs/');
   488 
   519         {$ENDIF}
   489         // if log is locked, write to the next one
   520         // if log is locked, write to the next one
   490         i:= 0;
   521         i:= 0;
   491         while(i < 7) do
   522         while(i < 7) do
   492             begin
   523             begin
   493             assign(f, UserPathPrefix + '/Logs/' + logfileBase + inttostr(i) + '.log');
   524             assign(f, UserPathPrefix + '/Logs/' + logfileBase + inttostr(i) + '.log');