hedgewars/uScript.pas
branchphysfslayer
changeset 8031 fc40b343c45c
parent 8025 07862ab415c8
child 8034 fc032c0f7b23
equal deleted inserted replaced
8028:dc30104660d3 8031:fc40b343c45c
    79     uRenderUtils,
    79     uRenderUtils,
    80     uTextures,
    80     uTextures,
    81     uLandGraphics,
    81     uLandGraphics,
    82     SDLh,
    82     SDLh,
    83     SysUtils, 
    83     SysUtils, 
    84     uIO;
    84     uIO,
       
    85     uPhysFSLayer
       
    86     ;
    85 
    87 
    86 var luaState : Plua_State;
    88 var luaState : Plua_State;
    87     ScriptAmmoLoadout : shortstring;
    89     ScriptAmmoLoadout : shortstring;
    88     ScriptAmmoProbability : shortstring;
    90     ScriptAmmoProbability : shortstring;
    89     ScriptAmmoDelay : shortstring;
    91     ScriptAmmoDelay : shortstring;
  1962 ScriptSetInteger('ScreenHeight', cScreenHeight);
  1964 ScriptSetInteger('ScreenHeight', cScreenHeight);
  1963 ScriptSetInteger('ScreenWidth', cScreenWidth);
  1965 ScriptSetInteger('ScreenWidth', cScreenWidth);
  1964 ScriptCall('onScreenResize');
  1966 ScriptCall('onScreenResize');
  1965 end;
  1967 end;
  1966 
  1968 
       
  1969 // custom script loader via physfs, passed to lua_load
       
  1970 const BUFSIZE = 16;
       
  1971 var phyfsReaderBuffer: pointer;
       
  1972 
       
  1973 function physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl;
       
  1974 var fileSize: Longword;
       
  1975 begin
       
  1976 writeln(stdout, '==== reading');
       
  1977     if pfsEOF(f) then
       
  1978         physfsReader:= nil
       
  1979     else
       
  1980         begin
       
  1981         sz^:= pfsBlockRead(f, phyfsReaderBuffer, BUFSIZE);
       
  1982 writeln(stdout, '==== read ' + inttostr(sz^));
       
  1983         if sz^ = 0 then
       
  1984             physfsReader:= nil
       
  1985         else
       
  1986             physfsReader:= phyfsReaderBuffer
       
  1987         end
       
  1988 end;
       
  1989 
  1967 
  1990 
  1968 procedure ScriptLoad(name : shortstring);
  1991 procedure ScriptLoad(name : shortstring);
  1969 var ret : LongInt;
  1992 var ret : LongInt;
  1970       s : shortstring;
  1993       s : shortstring;
  1971 begin
  1994       f : PFSFile;
  1972 s:= cPathz[ptData] + '/' + name;
  1995 begin
  1973 if not FileExists(s) then
  1996 s:= cPathz[ptData] + name;
       
  1997 if not pfsExists(s) then
  1974     exit;
  1998     exit;
  1975 
  1999 
  1976 ret:= luaL_loadfile(luaState, Str2PChar(s));
  2000 f:= pfsOpenRead(s);
       
  2001 if f = nil then 
       
  2002     exit;
       
  2003 
       
  2004 GetMem(phyfsReaderBuffer, BUFSIZE);
       
  2005 ret:= lua_load(luaState, @physfsReader, f, Str2PChar(s));
       
  2006 FreeMem(phyfsReaderBuffer, BUFSIZE);
       
  2007 pfsClose(f);
       
  2008 
  1977 if ret <> 0 then
  2009 if ret <> 0 then
  1978     begin
  2010     begin
  1979     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  2011     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  1980     LuaError('Lua: ' + lua_tostring(luaState, -1));
  2012     LuaError('Lua: ' + lua_tostring(luaState, -1));
  1981     end
  2013     end