Implement reader in C ffs physfslayer
authorunc0rr
Sat, 17 Nov 2012 00:01:06 +0400
branchphysfslayer
changeset 8034 fc032c0f7b23
parent 8031 fc40b343c45c
child 8037 547f94509079
Implement reader in C ffs
CMakeLists.txt
hedgewars/uPhysFSLayer.pas
hedgewars/uScript.pas
misc/physfs/extras/CMakeLists.txt
misc/physfs/extras/physfslualoader.c
--- a/CMakeLists.txt	Fri Nov 16 00:46:33 2012 +0400
+++ b/CMakeLists.txt	Sat Nov 17 00:01:06 2012 +0400
@@ -239,8 +239,10 @@
 else()
     message(STATUS "LUA will be provided by the bundled sources")
     add_subdirectory(misc/liblua)
+    set(LUA_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/misc/liblua)
     #linking with liblua.a requires system readline -- this works everywhere, right?
     set(pascal_flags "-k${EXECUTABLE_OUTPUT_PATH}/lib${LUA_LIBRARY}.a" "-k-lreadline" ${pascal_flags})
+    add_dependencies(physfsrwops lua)
 endif()
 
 
--- a/hedgewars/uPhysFSLayer.pas	Fri Nov 16 00:46:33 2012 +0400
+++ b/hedgewars/uPhysFSLayer.pas	Sat Nov 17 00:01:06 2012 +0400
@@ -34,7 +34,7 @@
 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongInt; cdecl; external;
 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external;
 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external;
-function PHYSFS_read(f: PFSFile; buf: pointer; objSize: Longword; objCount: Longword): Int64; cdecl; external;
+function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external;
 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external;
 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external;
 
@@ -74,7 +74,7 @@
 begin
 s[0]:= #0;
 
-while (PHYSFS_read(f, @c, 1, 1) = 1) and (c <> #10) do
+while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
     if (c <> #13) and (s[0] < #255) then
         begin
         inc(s[0]);
@@ -85,7 +85,7 @@
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
 var r: Int64;
 begin
-    r:= PHYSFS_read(f, buf, 1, size);
+    r:= PHYSFS_readBytes(f, buf, size);
 
     if r <= 0 then
         pfsBlockRead:= 0
--- a/hedgewars/uScript.pas	Fri Nov 16 00:46:33 2012 +0400
+++ b/hedgewars/uScript.pas	Sat Nov 17 00:01:06 2012 +0400
@@ -1967,31 +1967,16 @@
 end;
 
 // custom script loader via physfs, passed to lua_load
-const BUFSIZE = 16;
-var phyfsReaderBuffer: pointer;
-
-function physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl;
-var fileSize: Longword;
-begin
-writeln(stdout, '==== reading');
-    if pfsEOF(f) then
-        physfsReader:= nil
-    else
-        begin
-        sz^:= pfsBlockRead(f, phyfsReaderBuffer, BUFSIZE);
-writeln(stdout, '==== read ' + inttostr(sz^));
-        if sz^ = 0 then
-            physfsReader:= nil
-        else
-            physfsReader:= phyfsReaderBuffer
-        end
-end;
+const BUFSIZE = 1024;
+var physfsReaderBuffer: pointer; external;
+function physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external;
 
 
 procedure ScriptLoad(name : shortstring);
 var ret : LongInt;
       s : shortstring;
       f : PFSFile;
+    buf : array[0..Pred(BUFSIZE)] of byte;
 begin
 s:= cPathz[ptData] + name;
 if not pfsExists(s) then
@@ -2001,9 +1986,8 @@
 if f = nil then 
     exit;
 
-GetMem(phyfsReaderBuffer, BUFSIZE);
+physfsReaderBuffer:= @buf;
 ret:= lua_load(luaState, @physfsReader, f, Str2PChar(s));
-FreeMem(phyfsReaderBuffer, BUFSIZE);
 pfsClose(f);
 
 if ret <> 0 then
--- a/misc/physfs/extras/CMakeLists.txt	Fri Nov 16 00:46:33 2012 +0400
+++ b/misc/physfs/extras/CMakeLists.txt	Sat Nov 17 00:01:06 2012 +0400
@@ -1,6 +1,7 @@
 find_package(SDL REQUIRED)
 
 include_directories(${SDL_INCLUDE_DIR})
+include_directories(${LUA_INCLUDE_DIR})
 include_directories(${CMAKE_SOURCE_DIR}/misc/physfs/src)
 
-add_library(physfsrwops STATIC physfsrwops.c)
+add_library(physfsrwops STATIC physfsrwops.c physfslualoader.c)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/physfs/extras/physfslualoader.c	Sat Nov 17 00:01:06 2012 +0400
@@ -0,0 +1,24 @@
+#include <lua51/lua.h>
+#include <physfs.h>
+
+#define BUFSIZE 1024
+
+void * physfsReaderBuffer;
+
+const char * physfsReader(lua_State *L, PHYSFS_File *f, size_t *size)
+{
+
+    if(PHYSFS_eof(f))
+    {
+        return NULL;
+    }
+    else
+    {
+        *size = PHYSFS_readBytes(f, physfsReaderBuffer, BUFSIZE);
+
+        if(*size == 0)
+            return NULL;
+        else
+            return physfsReaderBuffer;
+    }
+}