hedgewars/uScript.pas
changeset 12462 19e7838bedad
parent 12460 2660611f8b22
child 12463 3af8d755de17
equal deleted inserted replaced
12461:3827fd64bb58 12462:19e7838bedad
  2994 
  2994 
  2995 // custom script loader via physfs, passed to lua_load
  2995 // custom script loader via physfs, passed to lua_load
  2996 const BUFSIZE = 1024;
  2996 const BUFSIZE = 1024;
  2997 
  2997 
  2998 var inComment: boolean;
  2998 var inComment: boolean;
       
  2999 var inQuote: boolean;
       
  3000 var braceCount: LongWord;
       
  3001 var wordCount: LongWord;
  2999 var lastChar: char;
  3002 var lastChar: char;
  3000 // ⭒⭐⭒✨⭐⭒✨⭐☆✨⭐✨✧✨☆✨✧✨☆⭒✨☆⭐⭒☆✧✨⭒✨⭐✧⭒☆⭒✧☆✨✧⭐☆✨☆✧⭒✨✧⭒☆⭐☆✧
  3003 // ⭒⭐⭒✨⭐⭒✨⭐☆✨⭐✨✧✨☆✨✧✨☆⭒✨☆⭐⭒☆✧✨⭒✨⭐✧⭒☆⭒✧☆✨✧⭐☆✨☆✧⭒✨✧⭒☆⭐☆✧
  3001 function  ScriptReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; Cdecl;
  3004 function  ScriptReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; Cdecl;
  3002 var mybuf: PChar;
  3005 var mybuf: PChar;
  3003     i: LongInt;
  3006     i: LongInt;
  3017                         inComment := false 
  3020                         inComment := false 
  3018                 end;
  3021                 end;
  3019         end;
  3022         end;
  3020     ScriptReader:= mybuf
  3023     ScriptReader:= mybuf
  3021 end;
  3024 end;
       
  3025 function  ScriptLocaleReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; Cdecl;
       
  3026 var mybuf: PChar;
       
  3027     i: LongInt;
       
  3028 begin
       
  3029     mybuf := physfsReader(L, f, sz);
       
  3030     if (mybuf <> nil) and (sz^ > 0) then
       
  3031         begin
       
  3032             for i:= 0 to sz^-1 do
       
  3033                 begin
       
  3034                     if not inComment and (mybuf[i] = '"') and (lastChar <> '\') then
       
  3035                         inQuote := not inQuote;
       
  3036                     if (lastChar = '-') and (mybuf[i] = '-') then
       
  3037                         inComment := true;
       
  3038                     // gonna add any non-magic whitespace and skip - just to make comment avoidance easier
       
  3039                     if not inComment and not inQuote and 
       
  3040                         ((mybuf[i] = '(') or 
       
  3041                         (mybuf[i] = ')') or 
       
  3042                         (mybuf[i] = '+') or 
       
  3043                         (mybuf[i] = '#') or 
       
  3044                         (braceCount > 2) or
       
  3045                         (wordCount > 6)) then 
       
  3046                        CheckSum := $deadbeef;
       
  3047                     if not inComment and not inQuote and ((mybuf[i] = '{') or (mybuf[i] = '}')) then
       
  3048                         inc(braceCount);
       
  3049                     if not inComment and not inQuote and 
       
  3050                         (((byte(mybuf[i]) > $40) and (byte(mybuf[i]) < $5B)) or
       
  3051                         ((byte(mybuf[i]) > $60) and (byte(mybuf[i]) < $6B)) or
       
  3052                         ((byte(mybuf[i]) >= $30) and (byte(mybuf[i]) < $40))) then
       
  3053                         inc(wordCount);
       
  3054                     lastChar := mybuf[i];
       
  3055                     if (byte(mybuf[i]) = $0D) or (byte(mybuf[i]) = $0A) then
       
  3056                         inComment := false 
       
  3057                 end;
       
  3058         end;
       
  3059     ScriptLocaleReader:= mybuf
       
  3060 end;
  3022 // ⭒⭐⭒✨⭐⭒✨⭐☆✨⭐✨✧✨☆✨✧✨☆⭒✨☆⭐⭒☆✧✨⭒✨⭐✧⭒☆⭒✧☆✨✧⭐☆✨☆✧⭒✨✧⭒☆⭐☆✧
  3061 // ⭒⭐⭒✨⭐⭒✨⭐☆✨⭐✨✧✨☆✨✧✨☆⭒✨☆⭐⭒☆✧✨⭒✨⭐✧⭒☆⭒✧☆✨✧⭐☆✨☆✧⭒✨✧⭒☆⭐☆✧
  3023 
  3062 
  3024 procedure ScriptLoad(name : shortstring);
  3063 procedure ScriptLoad(name : shortstring);
  3025 var ret : LongInt;
  3064 var ret : LongInt;
  3026       s : shortstring;
  3065       s : shortstring;
  3027       f : PFSFile;
  3066       f : PFSFile;
  3028     buf : array[0..Pred(BUFSIZE)] of byte;
  3067     buf : array[0..Pred(BUFSIZE)] of byte;
  3029 begin
  3068 begin
  3030 inComment:= false;
  3069 inComment:= false;
       
  3070 inQuote:= false;
  3031 lastChar:= 'X';
  3071 lastChar:= 'X';
       
  3072 braceCount:= 0;
       
  3073 wordCount:= 0;
  3032 s:= cPathz[ptData] + name;
  3074 s:= cPathz[ptData] + name;
  3033 if not pfsExists(s) then
  3075 if not pfsExists(s) then
  3034     begin
  3076     begin
  3035     AddFileLog('[LUA] Script not found: ' + name);
  3077     AddFileLog('[LUA] Script not found: ' + name);
  3036     exit;
  3078     exit;
  3041     exit;
  3083     exit;
  3042 
  3084 
  3043 hedgewarsMountPackage(Str2PChar(copy(s, 1, length(s)-4)+'.hwp'));
  3085 hedgewarsMountPackage(Str2PChar(copy(s, 1, length(s)-4)+'.hwp'));
  3044 
  3086 
  3045 physfsReaderSetBuffer(@buf);
  3087 physfsReaderSetBuffer(@buf);
  3046 ret:= lua_load(luaState, @ScriptReader, f, Str2PChar(s));
  3088 if Pos('Data/Locale',s) <> 0 then
       
  3089      ret:= lua_load(luaState, @ScriptLocaleReader, f, Str2PChar(s))
       
  3090 else ret:= lua_load(luaState, @ScriptReader, f, Str2PChar(s));
  3047 pfsClose(f);
  3091 pfsClose(f);
  3048 
  3092 
  3049 if ret <> 0 then
  3093 if ret <> 0 then
  3050     begin
  3094     begin
  3051     LuaError('Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  3095     LuaError('Failed to load ' + name + '(error ' + IntToStr(ret) + ')');