# HG changeset patch # User unc0rr # Date 1391803052 -14400 # Node ID cefede760264d2b06111f33b49bad02107a12240 # Parent 8b65699beb56e617469c946a02dfe942b6dd1f7d Revert 88929358d2e1 in favor of ansistrings implementation in pas2c diff -r 8b65699beb56 -r cefede760264 hedgewars/hwengine.pas --- a/hedgewars/hwengine.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/hwengine.pas Fri Feb 07 23:57:32 2014 +0400 @@ -458,7 +458,6 @@ if complete then begin - uLocale.initModule; uPhysFSLayer.initModule; uTextures.initModule; {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF} @@ -515,7 +514,6 @@ {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF} uTextures.freeModule; uPhysFSLayer.freeModule; - uLocale.freeModule; end; uIO.freeModule; diff -r 8b65699beb56 -r cefede760264 hedgewars/uLocale.pas --- a/hedgewars/uLocale.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uLocale.pas Fri Feb 07 23:57:32 2014 +0400 @@ -26,24 +26,21 @@ procedure LoadLocale(FileName: shortstring); function Format(fmt: shortstring; var arg: shortstring): shortstring; -function FormatA(fmt: PChar; arg: ansistring): ansistring; -function GetEventString(e: TEventId): PChar; -procedure initModule; -procedure freeModule; +function FormatA(fmt: ansistring; var arg: ansistring): ansistring; +function GetEventString(e: TEventId): ansistring; {$IFDEF HWLIBRARY} procedure LoadLocaleWrapper(str: pchar); cdecl; export; {$ENDIF} implementation -uses uRandom, uVariables, uDebug, uPhysFSLayer, sysutils, uUtils; +uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer; -var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of PChar; +var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring; trevt_n: array[TEventId] of integer; procedure LoadLocale(FileName: shortstring); -var s: PChar = nil; - sc: PChar; +var s: ansistring = ''; f: pfsFile; a, b, c: LongInt; first: array[TEventId] of boolean; @@ -60,48 +57,47 @@ while not pfsEof(f) do begin pfsReadLnA(f, s); - if (StrLength(s) > 0) and (s[0] >= '0') and (s[0] <= '9') then - begin - TryDo(StrLength(s) > 6, 'Load locale: empty string', true); - val(s[0]+s[1], a, c); - TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true); - TryDo(s[2] = ':', 'Load locale: ":" expected', true); - val(s[3]+s[4], b, c); - TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true); - TryDo(s[5] = '=', 'Load locale: "=" expected', true); - sc:= StrAlloc(StrLength(s) - 5); - StrCopy(sc, @s[6]); - case a of - 0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then - trammo[TAmmoStrId(b)]:= sc; - 1: if (b >=0) and (b <= ord(High(TMsgStrId))) then - trmsg[TMsgStrId(b)]:= sc; - 2: if (b >=0) and (b <= ord(High(TEventId))) then + if Length(s) = 0 then + continue; + if (s[1] < '0') or (s[1] > '9') then + continue; + TryDo(Length(s) > 6, 'Load locale: empty string', true); + val(s[1]+s[2], a, c); + TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true); + TryDo(s[3] = ':', 'Load locale: ":" expected', true); + val(s[4]+s[5], b, c); + TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true); + TryDo(s[6] = '=', 'Load locale: "=" expected', true); + Delete(s, 1, 6); + case a of + 0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then + trammo[TAmmoStrId(b)]:= s; + 1: if (b >=0) and (b <= ord(High(TMsgStrId))) then + trmsg[TMsgStrId(b)]:= s; + 2: if (b >=0) and (b <= ord(High(TEventId))) then + begin + TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false); + if first[TEventId(b)] then begin - TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false); - if first[TEventId(b)] then - begin - trevt_n[TEventId(b)]:= 0; - first[TEventId(b)]:= false; - end; - trevt[TEventId(b)][trevt_n[TEventId(b)]]:= sc; - inc(trevt_n[TEventId(b)]); + trevt_n[TEventId(b)]:= 0; + first[TEventId(b)]:= false; end; - 3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then - trammoc[TAmmoStrId(b)]:= sc; - 4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then - trammod[TAmmoStrId(b)]:= sc; - 5: if (b >=0) and (b <= ord(High(TGoalStrId))) then - trgoal[TGoalStrId(b)]:= sc; - end; - end; - StrDispose(s); - end; + trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s; + inc(trevt_n[TEventId(b)]); + end; + 3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then + trammoc[TAmmoStrId(b)]:= s; + 4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then + trammod[TAmmoStrId(b)]:= s; + 5: if (b >=0) and (b <= ord(High(TGoalStrId))) then + trgoal[TGoalStrId(b)]:= s; + end; + end; pfsClose(f); end; end; -function GetEventString(e: TEventId): PChar; +function GetEventString(e: TEventId): ansistring; begin if trevt_n[e] = 0 then // no messages for this event type? GetEventString:= '*missing translation*' @@ -119,17 +115,14 @@ Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg) end; -function FormatA(fmt: PChar; arg: ansistring): ansistring; +function FormatA(fmt: ansistring; var arg: ansistring): ansistring; var i: LongInt; - s: ansistring; begin -s:= fmt; - -i:= Pos('%1', s); +i:= Pos('%1', fmt); if i = 0 then - FormatA:= s + FormatA:= fmt else - FormatA:= copy(s, 1, i - 1) + arg + FormatA(PChar(copy(s, i + 2, Length(s) - i - 1)), arg) + FormatA:= copy(fmt, 1, i - 1) + arg + FormatA(copy(fmt, i + 2, Length(fmt) - i - 1), arg) end; {$IFDEF HWLIBRARY} @@ -139,22 +132,4 @@ end; {$ENDIF} -procedure initModule; -var e: TEventId; - i: LongInt; -begin - for e:= Low(TEventId) to High(TEventId) do - for i:= 0 to Pred(MAX_EVENT_STRINGS) do - trevt[e][i]:= nil; -end; - -procedure freeModule; -var e: TEventId; - i: LongInt; -begin - for e:= Low(TEventId) to High(TEventId) do - for i:= 0 to Pred(trevt_n[e]) do - StrDispose(trevt[e][i]); -end; - end. diff -r 8b65699beb56 -r cefede760264 hedgewars/uPhysFSLayer.pas --- a/hedgewars/uPhysFSLayer.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uPhysFSLayer.pas Fri Feb 07 23:57:32 2014 +0400 @@ -23,7 +23,7 @@ function pfsClose(f: PFSFile): boolean; procedure pfsReadLn(f: PFSFile; var s: shortstring); -procedure pfsReadLnA(f: PFSFile; var s: PChar); +procedure pfsReadLnA(f: PFSFile; var s: ansistring); function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64; function pfsEOF(f: PFSFile): boolean; @@ -99,34 +99,26 @@ end end; -procedure pfsReadLnA(f: PFSFile; var s: PChar); -var l, bufsize: Longword; - r: Int64; - b: PChar; +procedure pfsReadLnA(f: PFSFile; var s: ansistring); +var c: char; + b: shortstring; begin -bufsize:= 256; -s:= StrAlloc(bufsize); -l:= 0; +s:= ''; +b[0]:= #0; -repeat - r:= PHYSFS_readBytes(f, @s[l], 1); - - if (r = 1) and (s[l] <> #13) then +while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do + if (c <> #13) then begin - inc(l); - if l = bufsize then + inc(b[0]); + b[byte(b[0])]:= c; + if b[0] = #255 then begin - b:= s; - inc(bufsize, 256); - s:= StrAlloc(bufsize); - StrCopy(s, b); - StrDispose(b) + s:= s + b; + b[0]:= #0 end end; - -until (r = 0) or (s[l - 1] = #10); - -if (r = 0) then s[l]:= #0 else s[l - 1]:= #0 + +s:= s + b end; function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64; diff -r 8b65699beb56 -r cefede760264 hedgewars/uScript.pas --- a/hedgewars/uScript.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uScript.pas Fri Feb 07 23:57:32 2014 +0400 @@ -228,7 +228,7 @@ begin if lua_gettop(L) = 5 then begin - ShowMission(lua_tolstring(L, 1, nil), lua_tolstring(L, 2, nil), lua_tolstring(L, 3, nil), lua_tointeger(L, 4), lua_tointeger(L, 5)); + ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); end else LuaParameterCountError('ShowMission', 'caption, subcaption, text, icon, time', lua_gettop(L)); @@ -308,10 +308,10 @@ function lc_addcaption(L : Plua_State) : LongInt; Cdecl; begin if lua_gettop(L) = 1 then - AddCaption(lua_tolstring(L, 1, nil), cWhiteColor, capgrpMessage) + AddCaption(lua_tostring(L, 1), cWhiteColor, capgrpMessage) else if lua_gettop(L) = 3 then begin - AddCaption(lua_tolstring(L, 1, nil), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3))); + AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3))); end else LuaParameterCountError('AddCaption', 'text[, color, captiongroup]', lua_gettop(L)); diff -r 8b65699beb56 -r cefede760264 hedgewars/uStore.pas --- a/hedgewars/uStore.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uStore.pas Fri Feb 07 23:57:32 2014 +0400 @@ -44,7 +44,7 @@ procedure LoadHedgehogHat(var HH: THedgehog; newHat: shortstring); procedure SetupOpenGL; procedure SetScale(f: GLfloat); -function RenderHelpWindow(caption, subcaption, description, extra: PChar; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; +function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; procedure RenderWeaponTooltip(atype: TAmmoType); procedure ShowWeaponTooltip(x, y: LongInt); procedure FreeWeaponTooltip; @@ -1229,7 +1229,7 @@ Step:= 0 end; -function RenderHelpWindow(caption, subcaption, description, extra: PChar; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; +function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; var tmpsurf: PSDL_SURFACE; w, h, i, j: LongInt; font: THWFont; @@ -1258,13 +1258,13 @@ // TODO: Recheck height/position calculation // get caption's dimensions -TTF_SizeUTF8(Fontz[font].Handle, caption, @i, @j); +TTF_SizeUTF8(Fontz[font].Handle, PChar(caption), @i, @j); // width adds 36 px (image + space) w:= i + 36 + wa; h:= j + ha; // get sub caption's dimensions -TTF_SizeUTF8(Fontz[font].Handle, subcaption, @i, @j); +TTF_SizeUTF8(Fontz[font].Handle, PChar(subcaption), @i, @j); // width adds 36 px (image + space) if w < (i + 36 + wa) then w:= i + 36 + wa; @@ -1288,7 +1288,7 @@ if extra <> '' then begin // get extra label's dimensions - TTF_SizeUTF8(Fontz[font].Handle, extra, @i, @j); + TTF_SizeUTF8(Fontz[font].Handle, PChar(extra), @i, @j); if w < (i + wa) then w:= i + wa; inc(h, j + ha); @@ -1309,9 +1309,9 @@ DrawRoundRect(@r, cWhiteColor, cNearBlackColor, tmpsurf, true); // render caption -r:= WriteInRect(tmpsurf, 36 + cFontBorder + 2, ha, $ffffffff, font, caption); +r:= WriteInRect(tmpsurf, 36 + cFontBorder + 2, ha, $ffffffff, font, PChar(caption)); // render sub caption -r:= WriteInRect(tmpsurf, 36 + cFontBorder + 2, r.y + r.h, $ffc7c7c7, font, subcaption); +r:= WriteInRect(tmpsurf, 36 + cFontBorder + 2, r.y + r.h, $ffc7c7c7, font, PChar(subcaption)); // render all description lines tmpdesc:= description; @@ -1333,7 +1333,7 @@ end; if extra <> '' then - r:= WriteInRect(tmpsurf, cFontBorder + 2, r.y + r.h, extracolor, font, extra); + r:= WriteInRect(tmpsurf, cFontBorder + 2, r.y + r.h, extracolor, font, PChar(extra)); r.x:= cFontBorder + 6; r.y:= cFontBorder + 4; @@ -1349,7 +1349,7 @@ procedure RenderWeaponTooltip(atype: TAmmoType); var r: TSDL_Rect; i: LongInt; - extra: PChar; + extra: ansistring; extracolor: LongInt; begin // don't do anything if the window shouldn't be shown diff -r 8b65699beb56 -r cefede760264 hedgewars/uVariables.pas --- a/hedgewars/uVariables.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uVariables.pas Fri Feb 07 23:57:32 2014 +0400 @@ -2385,11 +2385,11 @@ aTexCoord: GLint; aColor: GLint; -var trammo: array[TAmmoStrId] of PChar; // name of the weapon - trammoc: array[TAmmoStrId] of PChar; // caption of the weapon - trammod: array[TAmmoStrId] of PChar; // description of the weapon - trmsg: array[TMsgStrId] of PChar; // message of the event - trgoal: array[TGoalStrId] of PChar; // message of the goal +var trammo: array[TAmmoStrId] of ansistring; // name of the weapon + trammoc: array[TAmmoStrId] of ansistring; // caption of the weapon + trammod: array[TAmmoStrId] of ansistring; // description of the weapon + trmsg: array[TMsgStrId] of ansistring; // message of the event + trgoal: array[TGoalStrId] of ansistring; // message of the goal cTestLua : Boolean; procedure preInitModule; @@ -2397,7 +2397,7 @@ procedure freeModule; implementation -uses strutils, sysutils; +uses strutils; procedure preInitModule; begin @@ -2442,21 +2442,7 @@ end; procedure initModule; -var asid: TAmmoStrId; - msid: TMsgStrId; - gsid: TGoalStrId; begin - for asid:= Low(TAmmoStrId) to High(TAmmoStrId) do - begin - trammo[asid]:= nil; - trammoc[asid]:= nil; - trammod[asid]:= nil; - end; - for msid:= Low(TMsgStrId) to High(TMsgStrId) do - trmsg[msid]:= nil; - for gsid:= Low(TGoalStrId) to High(TGoalStrId) do - trgoal[gsid]:= nil; - // TODO: fixme {$IFDEF PAS2C} cLocale:= 'en'; @@ -2644,20 +2630,7 @@ end; procedure freeModule; -var asid: TAmmoStrId; - msid: TMsgStrId; - gsid: TGoalStrId; begin - for asid:= Low(TAmmoStrId) to High(TAmmoStrId) do - begin - if trammo[asid] <> nil then StrDispose(trammo[asid]); - if trammoc[asid] <> nil then StrDispose(trammoc[asid]); - if trammod[asid] <> nil then StrDispose(trammod[asid]); - end; - for msid:= Low(TMsgStrId) to High(TMsgStrId) do - if trmsg[msid] <> nil then StrDispose(trmsg[msid]); - for gsid:= Low(TGoalStrId) to High(TGoalStrId) do - if trgoal[gsid] <> nil then StrDispose(trgoal[gsid]); end; end. diff -r 8b65699beb56 -r cefede760264 hedgewars/uWorld.pas --- a/hedgewars/uWorld.pas Fri Feb 07 22:42:57 2014 +0400 +++ b/hedgewars/uWorld.pas Fri Feb 07 23:57:32 2014 +0400 @@ -31,7 +31,7 @@ procedure DrawWorld(Lag: LongInt); procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode); -procedure ShowMission(caption, subcaption, mtext: PChar; icon, time : LongInt); +procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt); procedure HideMission; procedure ShakeCamera(amount: LongInt); procedure InitCameraBorders; @@ -220,7 +220,7 @@ // if the string has been set, show it for (default timeframe) seconds if length(g) > 0 then - ShowMission(trgoal[gidCaption], trgoal[gidSubCaption], PChar(g), 1, 0); + ShowMission(trgoal[gidCaption], trgoal[gidSubCaption], g, 1, 0); cWaveWidth:= SpritesData[sprWater].Width; //cWaveHeight:= SpritesData[sprWater].Height; @@ -1968,7 +1968,7 @@ WorldDx:= 1024; end; -procedure ShowMission(caption, subcaption, mtext: PChar; icon, time : LongInt); +procedure ShowMission(caption, subcaption, text: ansistring; icon, time : LongInt); var r: TSDL_Rect; begin r.w:= 32; @@ -1983,13 +1983,13 @@ begin r.x:= 0; r.y:= icon * 32; - missionTex:= RenderHelpWindow(caption, subcaption, mtext, '', 0, MissionIcons, @r) + missionTex:= RenderHelpWindow(caption, subcaption, text, '', 0, MissionIcons, @r) end else begin r.x:= ((-icon - 1) shr 4) * 32; r.y:= ((-icon - 1) mod 16) * 32; - missionTex:= RenderHelpWindow(caption, subcaption, mtext, '', 0, SpritesData[sprAMAmmos].Surface, @r) + missionTex:= RenderHelpWindow(caption, subcaption, text, '', 0, SpritesData[sprAMAmmos].Surface, @r) end; end;