Przeglądaj źródła

Moved sdl_rwstream to std, added Monkey2FileSystem.java

Mark Sibly 8 lat temu
rodzic
commit
331c6c4ee7

+ 28 - 0
modules/std/filesystem/filesystem.monkey2

@@ -6,6 +6,10 @@ Using libc
 #Import "native/filesystem.h"
 #Import "native/filesystem.cpp"
 
+#If __TARGET__="android"
+#Import "native/Monkey2FileSystem.java"
+#endif
+
 Extern
 
 #rem monkeydoc Gets application directory.
@@ -178,6 +182,30 @@ Function HomeDir:String()
 #Endif
 End
 
+#rem monkeydoc Gets the filesystem directory of the app's internal storage directory.
+
+Note that only the mobile targets have an internal directory. Other targets will return an empty string.
+
+The internal directory is where your app should create and manage app specific files such as game saves, preferences, items purchased and so on.
+
+@return The app's internal directory.
+
+#end
+Function InternalDir:String()
+#If __TARGET__="android"
+	Local env:=sdl2.Android_JNI_GetEnv()
+	
+	Local cls:=env.FindClass( "com/monkey2/lib/Monkey2FileSystem" )
+	
+	Local mth:=env.GetStaticMethodID( cls,"getInternalDir","()Ljava/lang/String;" )
+	
+	Local dir:=env.CallStaticStringMethod( cls,mth,Null )
+	
+	Return dir
+#Endif
+	Return ""
+End
+
 #rem monkeydoc Extracts the root directory from a file system path.
 
 A root directory is a directory path that:

+ 31 - 0
modules/std/filesystem/native/Monkey2FileSystem.java

@@ -0,0 +1,31 @@
+package com.monkey2.lib;
+
+import android.util.Log;
+
+public class Monkey2FileSystem{
+
+    private static final String TAG = "Monkey2FileSystem";
+    
+    static public String getIntenalDir(){
+    
+		//Log.v( TAG,"getInternalDir()" );
+    
+	    java.io.File f=Monkey2Activity.instance().getFilesDir();
+
+		if( f!=null ) return f.getAbsolutePath()+"/";
+	    
+	    return "";
+    }
+    
+    static public String getExternalDir(){
+    
+		//Log.v( TAG,"getExternalDir()" );
+    
+	    java.io.File f=android.os.Environment.getExternalStorageDirectory();
+	    
+		if( f!=null ) return f.getAbsolutePath()+"/";
+		
+		return "";
+	}
+}
+	

+ 0 - 2
modules/std/requesters/requesters.monkey2

@@ -25,8 +25,6 @@ Namespace std.requesters
 
 #Elseif __TARGET__="android"
 
-	#Import "<sdl2>"
-	#Import "<jni>"
 	#Import "native/Monkey2Requesters.java"
 
 #Elseif __TARGET__="ios"

+ 25 - 0
modules/std/std.monkey2

@@ -7,6 +7,13 @@ Namespace std
 #import "<stb-vorbis>"
 #Import "<miniz>"
 
+#If __MOBILE_TARGET__
+#Import "<sdl2>"
+#If __TARGET__="android"
+#Import "<jni>"
+#Endif
+#Endif
+
 #Import "collections/container"
 #Import "collections/stack"
 #Import "collections/list"
@@ -18,6 +25,10 @@ Namespace std
 #Import "stream/stream"
 #Import "stream/filestream"
 
+#If __MOBILE_TARGET__
+#Import "stream/sdl_rwstream.monkey2"
+#Endif
+
 #Import "memory/byteorder"
 #Import "memory/databuffer"
 #Import "memory/datastream"
@@ -115,6 +126,20 @@ Function Main()
 		Return FileStream.Open( filesystem.AssetsDir()+path,mode )
 	End
 
+#Else If __TARGET__="android"
+
+	Stream.OpenFuncs["asset"]=Lambda:Stream( proto:String,path:String,mode:String )
+	
+		Return SDL_RWStream.Open( path,mode )
+	End
+
+#Else If __TARGET__="ios"
+
+	Stream.OpenFuncs["asset"]=Lambda:Stream( proto:String,path:String,mode:String )
+	
+		Return SDL_RWStream.Open( "assets/"+path,mode )
+	End
+	
 #Endif
 	
 End

+ 152 - 0
modules/std/stream/sdl_rwstream.monkey2

@@ -0,0 +1,152 @@
+
+Namespace std.stream
+
+Using sdl2
+
+Class SDL_RWStream Extends Stream
+
+	#rem monkeydoc True if no more data can be read from the stream.
+	
+	You can still write to a filestream even if `Eof` is true - disk space permitting!
+	
+	#end
+	Property Eof:Bool() Override
+		Return _pos>=_end
+	End
+
+	#rem monkeydoc Current filestream position.
+	
+	The current file read/write position.
+	
+	#end
+	Property Position:Int() Override
+		Return _pos
+	End
+
+	#rem monkeydoc Current filestream length.
+	
+	The length of the filestream. This is the same as the length of the file.
+	
+	The file length can increase if you write past the end of the file.
+	
+	#end	
+	Property Length:Int() Override
+		Return _end
+	End
+	
+	#rem monkeydoc Closes the filestream.
+	
+	Closing the filestream also sets its position and length to 0.
+	
+	#end
+	Method OnClose() Override
+		If Not _rwops Return
+		SDL_RWclose( _rwops )
+		_rwops=Null
+		_pos=0
+		_end=0
+	End
+	
+	#rem monkeydoc Seeks to a position in the filestream.
+	
+	@param offset The position to seek to.
+	
+	#end
+	Method Seek( position:Int ) Override
+
+		DebugAssert( position>=0 And position<=_end )
+		
+		SDL_RWseek( _rwops,position,RW_SEEK_SET )
+		
+		DebugAssert( SDL_RWtell( _rwops )=position )
+		
+		_pos=position
+	End
+	
+	#rem monkeydoc Reads data from the filestream.
+	
+	@param buf A pointer to the memory to read the data into.
+	
+	@param count The number of bytes to read.
+	
+	@return The number of bytes actually read.
+	
+	#end
+	Method Read:Int( buf:Void Ptr,count:Int ) Override
+	
+		count=Clamp( count,0,_end-_pos )
+		
+		count=SDL_RWread( _rwops,buf,1,count )
+		
+		DebugAssert( SDL_RWtell( _rwops )=_pos+count )
+		
+		_pos+=count
+		
+		Return count
+	End
+	
+	#rem monkeydoc Writes data to the filestream.
+	
+	Writing past the end of the file will increase the length of a filestream.
+	
+	@param buf A pointer to the memory to write the data from.
+	
+	@param count The number of bytes to write.
+	
+	@return The number of bytes actually written.
+	
+	#end
+	Method Write:Int( buf:Void Ptr,count:Int ) Override
+	
+		If count<=0 Return 0
+		
+		count=SDL_RWwrite( _rwops,buf,1,count )
+		
+		DebugAssert( SDL_RWtell( _rwops )=_pos+count )
+		
+		_pos+=count
+		
+		Return count
+	End
+	
+	#rem monkeydoc Opens a file and returns a new filestream.
+	
+	@param path The path of the file to open.
+	
+	@param mode The mode to open the file in: "r", "w" or "rw".
+	
+	@return A new filestream, or null if the file could not be opened.
+	
+	#end
+	Function Open:SDL_RWStream( path:String,mode:String )
+	
+		Select mode
+		Case "r"
+			mode="rb"
+		Case "w"
+			mode="wb"
+		Case "rw"
+			mode="W+b"
+		Default
+			Return Null
+		End
+		
+		Local rwops:=SDL_RWFromFile( path,mode )
+		If Not rwops Return Null
+		
+		Return New SDL_RWStream( rwops )
+	End
+	
+	Private
+	
+	Field _rwops:SDL_RWops Ptr
+	Field _pos:Int
+	Field _end:Int
+	
+	Method New( rwops:SDL_RWops Ptr )
+		_rwops=rwops
+		_pos=SDL_RWtell( _rwops )
+		_end=SDL_RWsize( _rwops )
+	End
+
+End