瀏覽代碼

Modules tweaks + cleanups.

Mark Sibly 9 年之前
父節點
當前提交
477abc2355

+ 1 - 1
modules/openal/module.json

@@ -1,6 +1,6 @@
 {
 	"module":"openal",
-	"about":"OpenAL audio",
+	"about":"OpenAL library wrapper",
 	"author":"Mark Sibly",
 	"version":"1.0.0",
 	"support":"http://monkey2.monkey-x.com",

+ 25 - 15
modules/openal/openal.monkey2

@@ -1,28 +1,38 @@
 
 #import "<libc>"
 
-#if __HOSTOS__="windows"
+#if __TARGET__="desktop"
 
-	#import "openal-soft/bin/OpenAL32.dll"
-	#import "openal-soft/lib/OpenAL32.lib"
+	#if __HOSTOS__="windows"
 	
-	#import "openal-soft/include/*.h"
-	#import "<AL/al.h>"
-	#import "<AL/alc.h>"
-
-#else if __HOSTOS__="macos"
-
-	#import "<OpenAL.framework>"
+		#import "openal-soft/bin/OpenAL32.dll"
+		#import "openal-soft/lib/OpenAL32.lib"
+		
+		#import "openal-soft/include/*.h"
+		#import "<AL/al.h>"
+		#import "<AL/alc.h>"
+	
+	#else if __HOSTOS__="macos"
 	
-	#import "<OpenAL/al.h>"
-	#import "<OpenAL/alc.h>"
+		#import "<OpenAL.framework>"
+		
+		#import "<OpenAL/al.h>"
+		#import "<OpenAL/alc.h>"
+		
+	#else if __HOSTOS__="linux"
 	
-#else if __HOSTOS__="linux"
+		#import "<libopenal.a>"
+		#import "<AL/al.h>"
+		#import "<AL/alc.h>"
+		
+	#endif
+
+#else if __TARGET__="android"
 
-	#import "<libopenal.a>"
+	#import "openal-soft/include/*.h"
 	#import "<AL/al.h>"
 	#import "<AL/alc.h>"
-	
+
 #endif
 		
 Namespace openal

+ 3 - 0
modules/sdl2-mixer/module.json

@@ -1,5 +1,8 @@
 {
 	"module":"sdl2-mixer",
+	"about":"SDL mixer library",
+	"author":"Mark Sibly",
 	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
 	"depends":["sdl2"]
 }

+ 14 - 0
modules/sdl2/makefile.monkey2

@@ -2,13 +2,27 @@
 Namespace sdl2
 
 #If __TARGET__="desktop"
+
 	#If __HOSTOS__="macos"
+	
 		#Import "makefile_macos.monkey2"
+		
 	#Elseif __HOSTOS__="windows"
+	
 		#Import "makefile_windows.monkey2"
+		
 	#Elseif __HOSTOS__="linux"
+	
 		#Import "makefile_linux.monkey2"
+		
 	#Endif
+	
 #Elseif __TARGET__="emscripten"
+
 	#Import "makefile_emscripten.monkey2"
+	
+#Elseif __TARGET__="android"
+
+	#Import "makefile_android.monkey2"
+
 #Endif

+ 3 - 0
modules/sdl2/module.json

@@ -1,5 +1,8 @@
 {
 	"module":"sdl2",
+	"about":"SDL2 library wrapper",
+	"author":"Mark Sibly",
 	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
 	"depends":["libc","gles20"]
 }

+ 2 - 2
modules/sdl2/sdl2.monkey2

@@ -4,11 +4,11 @@ Namespace sdl2
 #Import "<libc>"
 #Import "<gles20>"
 
-#Import "makefile.monkey2"
-
 #Import "SDL/include/*.h"
 #Import "<SDL.h>"
 
+#Import "makefile.monkey2"
+
 Using libc
 
 Extern

+ 5 - 1
modules/stb-image-write/module.json

@@ -1,4 +1,8 @@
 {
 	"module":"stb-image-write",
-	"version":"1.0.0"
+	"about":"Minimal image saver lib",
+	"author":"Mark Sibly",
+	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
+	"depends":["libc"]
 }

+ 5 - 1
modules/stb-image/module.json

@@ -1,4 +1,8 @@
 {
 	"module":"stb-image",
-	"version":"1.0.0"
+	"about":"Minimal image loader",
+	"author":"Sean T Barret/Mark Sibly",
+	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
+	"depends":["libc"]
 }

+ 5 - 1
modules/stb-truetype/module.json

@@ -1,4 +1,8 @@
 {
 	"module":"stb-truetype",
-	"version":"1.0.0"
+	"about":"Minimal truetype lib",
+	"author":"Mark Sibly",
+	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
+	"depends":[]
 }

+ 3 - 3
modules/stb-vorbis/module.json

@@ -1,10 +1,10 @@
 {
 	"module":"stb-vorbis",
-	"about":"Ogg Vorbis IO",
+	"about":"Minimal vorbis loader",
 	"author":"Sean Barret",
 	"version":"1.0.0",
-	"support":"monkey2.monkey-x.com",
-	"depends":["libc"]
+	"support":"http://monkey2.monkey-x.com",
+	"depends":[]
 }
 
 	

+ 19 - 0
modules/std/audio/audiodata.monkey2

@@ -51,6 +51,25 @@ Class AudioData
 		Return BytesPerSample( _format ) * _length
 	End
 	
+	Method GetSample:Float( index:Int,channel:Int=0 )
+		DebugAssert( index>=0 And index<_length )
+		Select _format
+		Case AudioFormat.Mono8
+			Return _data[index]/128.0-1
+		Case AudioFormat.Stereo8
+			Return _data[index*2+(channel&1)]/128.0-1
+		Case AudioFormat.Mono16
+			Return Cast<Short Ptr>( _data )[index]/32767.0
+		Case AudioFormat.Stereo16
+			Return Cast<Short Ptr>( _data )[index*2+(channel&1)]/32767.0
+		End
+		Return 0
+	End
+	
+	Method SetSample( index:Int,channel:Int=0,sample:Float )
+		DebugAssert( index>=0 And index<_length )
+	End
+	
 	Method Discard()
 		If _discarded Return
 		_discarded=True

+ 11 - 5
modules/std/fiber/fcontext.monkey2

@@ -1,8 +1,18 @@
 
+'For testing purposes only - use Fiber instead!
+'
+'Will generally cause havoc on GC and debugger as they wont know you're messing with the stack...
+
+Namespace std.fiber
+
 #import "native/fcontext.cpp"
 #import "native/fcontext.h"
 
-#if __HOSTOS__="windows"
+#if __TARGET__="android"
+	#import "native/asm/make_arm_aapcs_elf_gas.S"
+	#import "native/asm/jump_arm_aapcs_elf_gas.S"
+	#import "native/asm/ontop_arm_aapcs_elf_gas.S"
+#else if __HOSTOS__="windows"
 	#import "native/asm/make_i386_ms_pe_gas.asm"
 	#import "native/asm/jump_i386_ms_pe_gas.asm"
 	#import "native/asm/ontop_i386_ms_pe_gas.asm"
@@ -16,10 +26,6 @@
 	#import "native/asm/ontop_x86_64_sysv_elf_gas.S"
 #end
 
-'Testing purposes only - use Fiber instead!
-'
-'Will generally cause havoc on GC and debugger as they wont know you're messing with the stack...
-'
 Extern
 
 Alias fcontext_t:Void Ptr

+ 18 - 1
modules/std/fiber/fiber.monkey2

@@ -52,6 +52,12 @@ Struct Fiber
 		_fiber=StartFiber( entry )
 	End
 	
+	#rem monkeydoc @hidden
+	#end
+	Property Debug:String()
+		Return _fiber
+	End
+	
 	#rem monkeydoc Resumes a suspended fiber.
 	#end	
 	Method Resume()
@@ -70,6 +76,15 @@ Struct Fiber
 		SuspendCurrentFiber()
 	End
 	
+	#rem monkeydoc Gets the main fiber.
+	
+	Suspending the main fiber will result in a runtime error.
+	
+	#end
+	Function Main:Fiber()
+		Return _main
+	End
+	
 	#rem monkeydoc Gets the currently running fiber.
 	#end
 	Function Current:Fiber()
@@ -81,11 +96,13 @@ Struct Fiber
 	Function CreateSuspended:Fiber( entry:Void() )
 		Return New Fiber( CreateFiber( entry ) )
 	End
-
+	
 	Private
 	
 	Field _fiber:Int
 	
+	Global _main:=New Fiber( 0 )
+	
 	Method New( fiber:Int )
 		_fiber=fiber
 	End

+ 131 - 70
modules/std/misc/filesystem.monkey2

@@ -87,6 +87,20 @@ Function AssetsDir:String()
 
 End
 
+Function DesktopDir:String()
+
+#If __TARGET__="desktop"
+	#If __HOSTOS__="windows"
+		Return (String.FromCString( getenv( "HOMEDRIVE" ) )+String.FromCString( getenv( "HOMEPATH" ) )).Replace( "\","/" )+"/Desktop/"
+	#Else
+		Return String.FromCString( getenv( "HOME" ) )+"/Desktop/"
+	#endif
+#else
+	RuntimeError( "Target has no desktop dir" )
+#endif
+
+End
+
 #rem monkeydoc Extracts the root directory from a file system path.
 
 @param path The filesystem path.
@@ -184,11 +198,15 @@ If `path` does not contain a directory component, an empty string is returned.
 Function ExtractDir:String( path:String )
 
 	path=StripSlashes( path )
+
 	If IsRootDir( path ) Return path
 	
 	Local i:=path.FindLast( "/" )
 	If i>=0 Return path.Slice( 0,i+1 )
 	
+	i=path.Find( "::" )
+	If i>=0 Return path.Slice( 0,i+2 )
+	
 	Return ""
 End
 
@@ -206,11 +224,15 @@ If `path` does not contain a directory component, `path` is returned without mod
 Function StripDir:String( path:String )
 
 	path=StripSlashes( path )
+
 	If IsRootDir( path ) Return ""
 
 	Local i:=path.FindLast( "/" )
 	If i>=0 Return path.Slice( i+1 )
 	
+	i=path.Find( "::" )
+	If i>=0 Return path.Slice( i+2 )
+	
 	Return path
 End
 
@@ -289,6 +311,26 @@ Function RealPath:String( path:String )
 	Return rpath
 End
 
+#rem monkeydoc Gets the type of the file at a filesystem path.
+
+@param path The filesystem path.
+
+@return The file type of the file at `path`, one of: FileType.None, FileType.File or FileType.Directory.
+
+#end
+Function GetFileType:FileType( path:String )
+
+	Local st:stat_t
+	If stat( StripSlashes( path ),Varptr st )<0 Return FileType.None
+	
+	Select st.st_mode & S_IFMT
+	Case S_IFREG Return FileType.File
+	Case S_IFDIR Return FileType.Directory
+	End
+	
+	Return FileType.Unknown
+End
+
 #rem monkeydoc Gets the time a file was most recently modified.
 
 @param path The filesystem path.
@@ -298,34 +340,27 @@ End
 #end
 Function GetFileTime:Long( path:String )
 
-	path=StripSlashes( path )
-
 	Local st:stat_t
-	If stat( path,Varptr st )<0 Return 0
+	If stat( StripSlashes( path ),Varptr st )<0 Return 0
 	
 	Return libc.tolong( st.st_mtime )
 End
 
-#rem monkeydoc Gets the type of the file at a filesystem path.
+#rem monkeydoc Gets the size of the file at a filesystem path.
 
 @param path The filesystem path.
 
-@return The file type of the file at `path`, one of: FileType.None, FileType.File or FileType.Directory.
+@return The size file at `path` in bytes.
 
 #end
-Function GetFileType:FileType( path:String )
+Function GetFileSize:Long( path:String )
 
 	path=StripSlashes( path )
 
 	Local st:stat_t
-	If stat( path,Varptr st )<0 Return FileType.None
-	
-	Select st.st_mode & S_IFMT
-	Case S_IFREG Return FileType.File
-	Case S_IFDIR Return FileType.Directory
-	End
-	
-	Return FileType.Unknown
+	If stat( path,Varptr st )<0 Return 0
+
+	return st.st_size
 End
 
 #rem monkeydoc Gets the process current directory.
@@ -353,9 +388,7 @@ End
 #end
 Function ChangeDir( path:String )
 
-	path=StripSlashes( path )
-	
-	chdir( path )
+	chdir( StripSlashes( path ) )
 End
 
 #rem monkeydoc Loads a directory.
@@ -389,63 +422,71 @@ Function LoadDir:String[]( path:String )
 	Return files.ToArray()
 End
 
+#rem monkeydoc Creates a file at a filesystem path.
+
+Any existing file at the path will be overwritten.
+
+@param path The filesystem path of the file file to create.
+
+@param createDir If true, also creates the file directory if necessary.
+
+@return True if a file at the given path was created.
+
+#end
+Function CreateFile:Bool( path:String,createDir:Bool=True )
+
+	If createDir And Not CreateDir( ExtractDir( path ),True ) Return False
+	
+	Local f:=fopen( path,"wb" )
+	If Not f Return False
+	
+	fclose( f )
+	Return True
+End
+
 #rem monkeydoc Creates a directory at a filesystem path.
 
-@param path The filesystem path of ther directory to create.
+@param path The filesystem path of the directory to create.
 
 @param recursive If true, any required parent directories are also created.
 
 @return True if a directory at `path` was successfully created or already existed.
 
 #end
-Function CreateDir:Bool( path:String,recursive:Bool=True )
-
-	path=StripSlashes( path )
+Function CreateDir:Bool( dir:String,recursive:Bool=True )
 
 	If recursive
-		Local parent:=ExtractDir( path )
+	
+		Local parent:=ExtractDir( dir )
 		If parent And Not IsRootDir( parent )
+		
 			Select GetFileType( parent )
 			Case FileType.None
 				If Not CreateDir( parent,True ) Return False
 			Case FileType.File
 				Return False
-			Case FileType.Directory
 			End
+			
 		Endif
+		
 	Endif
 
-	mkdir( path,$1ff )
-	Return GetFileType( path )=FileType.Directory
+	mkdir( StripSlashes( dir ),$1ff )
+	Return GetFileType( dir )=FileType.Directory
 End
 
-Private
+#rem monkeydoc Deletes a file at a filesystem path.
 
-Function DeleteAll:Bool( path:String )
+@param path The filesystem path.
 
-	Select GetFileType( path )
-	Case FileType.None
-	
-		Return True
-		
-	Case FileType.File
-	
-		Return DeleteFile( path )
-		
-	Case FileType.Directory
-	
-		For Local f:=Eachin LoadDir( path )
-			If Not DeleteAll( path+"/"+f ) Return False
-		Next
-		
-		rmdir( path )
-		Return GetFileType( path )=FileType.None
-	End
-	
-	Return False
-End
+@return True if the file was successfully deleted.
 
-Public
+#end
+Function DeleteFile:Bool( path:String )
+
+	remove( path )
+	Return GetFileType( path )=FileType.None
+End
 
 #rem monkeydoc Deletes a directory at a filesystem path.
 
@@ -456,40 +497,60 @@ Public
 @return True if the directory was successfully deleted or never existed.
 
 #end
-Function DeleteDir:Bool( path:String,recursive:Bool=False )
-
-	path=StripSlashes( path )
+Function DeleteDir:Bool( dir:String,recursive:Bool=False )
 
-	Select GetFileType( path )
+	Select GetFileType( dir )
 	Case FileType.None
-
-		Return True
-		
-	Case FileType.File
 	
-		Return False
+		Return True
 		
 	Case FileType.Directory
 	
-		If recursive Return DeleteAll( path )
+		If recursive
 		
-		rmdir( path )
-		Return GetFileType( path )=FileType.None
+			For Local f:=Eachin LoadDir( dir )
+			
+				Local p:=dir+"/"+f
+				
+				Select GetFileType( p )
+				Case FileType.File
+					If Not DeleteFile( p ) Return False
+				Case FileType.Directory
+					If Not DeleteDir( p,True ) Return false
+				End
+			Next
+			
+		Endif
+		
+		rmdir( StripSlashes( dir ) )
+		
+		Return GetFileType( dir )=FileType.None
 	End
 	
 	Return False
 End
 
-#rem monkeydoc Deletes a file at a filesystem path.
-
-@param path The filesystem path.
-
-@return True if the file was successfully deleted.
+Function CopyDir:Bool( srcDir:String,dstDir:String,recursive:Bool=True )
 
-#end
-Function DeleteFile:Bool( path:String )
+	If GetFileType( srcDir )<>FileType.Directory Return False
+	
+	If GetFileType( dstDir )<>FileType.Directory And Not CreateDir( dstDir,True ) Return False
+	
+	For Local f:=Eachin LoadDir( srcDir )
+	
+		Local src:=srcDir+"/"+f
+		Local dst:=dstDir+"/"+f
+		
+		Select GetFileType( src )
+		Case FileType.File
+			If Not CopyFile( src,dst ) Return False
+		Case FileType.Directory
+			If recursive And Not CopyDir( src,dst ) Return False
+		End
+		
+	Next
+	
+	Return True
 
-	remove( path )
-	Return GetFileType( path )=FileType.None
 End
 

+ 88 - 23
modules/std/misc/json.monkey2

@@ -82,19 +82,28 @@ Class JsonValue Abstract
 		Return stringio.SaveString( src,path )
 	End
 	
-	Function Load:JsonValue( path:String )
+	Function Load:JsonValue( path:String,throwex:Bool=False )
 	
 		Local src:=stringio.LoadString( path )
 		If Not src Return Null
 		
-		Local parser:=New JsonParser( src )
-		Local value:=parser.ParseValue()
-		
-		Return value
+		Return Parse( src,throwex )
+	End
+	
+	Function Parse:JsonValue( src:String,throwex:Bool=False )
+		Try
+			Local parser:=New JsonParser( src )
+			Return parser.ParseValue()
+		Catch ex:JsonError
+			If throwex Throw ex
+		End
+		Return Null
 	End
 	
 	Protected
 	
+	Global _indent:String
+	
 	#rem monkeydoc @hidden
 	#end
 	Method PushJson:Void( buf:StringStack ) Virtual
@@ -134,7 +143,8 @@ Class JsonBool Extends JsonValue
 	End
 	
 	Method ToJson:String() Override
-		Return _data ? "true" Else "false"
+		If _data Return "true"
+		Return "false"
 	End
 
 	Private
@@ -169,7 +179,7 @@ Class JsonNumber Extends JsonValue
 	End
 	
 	Method ToJson:String() Override
-		Return _data
+		Return String( _data )
 	End
 
 	Private
@@ -238,6 +248,24 @@ Class JsonArray Extends JsonValue
 		Return _data.Length
 	End
 	
+	Operator[]:JsonValue( index:Int )
+		DebugAssert( index>=0 )
+	
+		If index<_data.Length Return _data[index]
+		Return Null
+	End
+	
+	Operator[]=( index:Int,value:JsonValue )
+		DebugAssert( index>=0 )
+		
+		If index>=_data.Length _data.Resize( index+1 )
+		_data[index]=value
+	End
+	
+	Method Push( value:JsonValue )
+		_data.Push( value )
+	End
+	
 	Method Add( value:JsonValue )
 		_data.Add( value )
 	End
@@ -251,13 +279,30 @@ Class JsonArray Extends JsonValue
 	Field _data:Stack<JsonValue>
 	
 	Method PushJson:Void( buf:StringStack ) Override
+	
 		buf.Push( "[" )
-		Local t:=False
+		
+		Local indent:=_indent
+		
+		Local n:=0
+		
 		For Local value:=Eachin _data
-			If t buf.Push( "," )
-			If value value.PushJson( buf ) Else buf.Push( "null" )
-			t=True
+		
+			If n 
+				buf.Push( "," )
+			Endif
+			n+=1
+			
+			If value
+				buf.Push( value.ToJson() )
+			Else
+				buf.Push( "null" )
+			Endif
+			
 		Next
+		
+		_indent=indent
+
 		buf.Push( "]" )
 	End
 	
@@ -294,17 +339,20 @@ Class JsonObject Extends JsonValue
 		Return Data
 	End
 	
-	Function Load:JsonObject( path:String )
-		Local json:=std.stringio.LoadString( path )
-		If json Return Parse( json )
-		Return Null
+	Function Load:JsonObject( path:String,throwex:Bool=False )
+
+		Local src:=std.stringio.LoadString( path )
+		If Not src Return Null
+		
+		Return Parse( src,throwex )
 	End
 	
-	Function Parse:JsonObject( json:String )
+	Function Parse:JsonObject( json:String,throwex:Bool=False )
 		Try
 			Local parser:=New JsonParser( json )
 			Return New JsonObject( parser.ParseObject() )
 		Catch ex:JsonError
+			If throwex Throw ex
 		End
 		Return Null
 	End
@@ -314,15 +362,21 @@ Class JsonObject Extends JsonValue
 	Field _data:StringMap<JsonValue>
 
 	Method PushJson:Void( buf:StringStack ) Override
-		buf.Push( "{" )
+		buf.Push( "{~n" )
+		_indent+="~t"
 		Local t:=False
 		For Local it:=Eachin _data
-			If t buf.Push( "," )
-			buf.Push( "~q"+it.Key.Replace( "~q","\~q" )+"~q:" )
-			If it.Value it.Value.PushJson( buf ) Else buf.Push( "null" )
+			If t buf.Push( ",~n" )
+			buf.Push( _indent+"~q"+it.Key.Replace( "~q","\~q" )+"~q:" )
+			If it.Value
+				buf.Push( it.Value.ToJson() )
+			Else
+				buf.Push( "null" )
+			Endif
 			t=True
 		Next
-		buf.Push( "}" )
+		_indent=_indent.Slice( 0,-1 )
+		buf.Push( "~n"+_indent+"}" )
 	End
 	
 End
@@ -392,8 +446,19 @@ Class JsonParser
 	
 	Method Bump:String()
 	
-		While _pos<_text.Length And _text[_pos]<=32
-			_pos+=1
+		While _pos<_text.Length
+			If _text[_pos]=47
+				'// comment? - can't live without 'em!
+				If _pos+1=_text.Length Or _text[_pos+1]<>47 Exit
+				_pos+=2
+				While _pos<_text.Length And _text[_pos]<>10
+					_pos+=1
+				Wend
+				If _pos<_text.Length _pos+=1
+			Else
+				If _text[_pos]>32 Exit
+				_pos+=1
+			Endif
 		Wend
 		
 		If _pos=_text.Length

+ 1 - 1
modules/std/misc/stringio.monkey2

@@ -133,7 +133,7 @@ Function StringToULong:ULong( str:String,base:UInt )
 			value=value*base+(ch-48)
 		Else If ch>=65 And ch<65+(base-10)
 			value=value*base+(ch-55)
-		Else If ch>=97 And ch<07+(base-10)
+		Else If ch>=97 And ch<97+(base-10)
 			value=value*base+(ch-87)
 		Else
 			Exit

+ 3 - 0
modules/std/module.json

@@ -1,5 +1,8 @@
 {
 	"module":"std",
+	"about":"Standard monkey2 library",
+	"author":"Mark Sibly",
 	"version":"1.0.0",
+	"support":"http://monkey2.monkey-x.com",
 	"depends":["libc","miniz","stb-image","stb-image-write","stb-vorbis"]
 }

+ 2 - 0
modules/std/std.monkey2

@@ -46,7 +46,9 @@ Namespace std
 #Import "misc/filesystem"
 #Import "misc/stringio"
 #Import "misc/json"
+#Import "misc/jsonify"
 #Import "misc/time"
+#Import "misc/zipfile"
 
 #Import "fiber/fiber"
 #Import "fiber/future"