xaymar.resource (Oder auch "ResourceManager")

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Xaymar

ehemals "Cgamer"

Betreff: xaymar.resource (Oder auch "ResourceManager")

BeitragFr, Jun 03, 2011 18:55
Antworten mit Zitat
Benutzer-Profile anzeigen
~-Was ist es?-~
xaymar.resource ist ein Module das euch erlaubt einfach und dennoch effektiv eure Dateien zu laden. Dies funktioniert sogar im Multithreading durch einen einzigen Mutex.
Das heißt das man sich ab sofort die umständliche Schreiberei einer Liste die durchgearbeitet wird und nebenbei den Ladebildschirm zeigt, sparen kann.

~-Das Module-~
BlitzMax: [AUSKLAPPEN]
SuperStrict

?Threaded
Import brl.threads
?
Import brl.linkedlist

Module xaymar.resource
ModuleInfo "License: Public Domain"
ModuleInfo "Original Author: Michael Dirks <support@levelnull.de>"
ModuleInfo "Purpose: Load your Files the better way! (Threading supported)"

Const TRM_SAVED:Int = -2
Const TRM_SAVING:Int = -1
Const TRM_NONE:Int = +0
Const TRM_LOADING:Int = +1
Const TRM_LOADED:Int = +2
Const TRM_ERROR:Int = $FF
Const TRM_ALL:Int = $FE

Const TRM_LS_ERROR:Int = 0
Const TRM_LS_SUCCESS:Int = 1
Const TRM_LS_NORESOURCE:Int = 2

Type TRLException
Method ToString:String()
Return "An Error appeared during Loading/Saving the Object."
End Method
End Type
Type TResourceManager
'Members
Field _ResList:TList = New TList
Field _ResourceLoaderFunc(Res:TResource)
Field _ResourceSaverFunc(Res:TResource)

'Members: Stats
Field _SavingRes:Int, _SavedRes:Int
Field _TotalRes:Int, _ErrorRes:Int
Field _LoadingRes:Int, _LoadedRes:Int

'Threaded Members
?Threaded Field _ResMutex:TMutex = CreateMutex()
?

'Methods
Method New();SetLoaderFunc(TResourceLoaderFunc);SetSaverFunc(TResourceSaverFunc);End Method
Method Remove()
?Threaded _ResMutex.Lock()
?
_ResList.Clear()
_ResList = Null
?Threaded _ResMutex.Unlock()
_ResMutex.Close()
_ResMutex = Null
?
End Method

Method SetLoaderFunc(LoaderFunc:Byte Ptr)
_ResourceLoaderFunc = LoaderFunc
End Method
Method SetSaverFunc(SaverFunc:Byte Ptr)
_ResourceSaverFunc = SaverFunc
End Method

Method Update()
?Threaded _ResMutex.Lock()
?
_TotalRes = _ResList.Count()
_LoadingRes = 0;_SavingRes = 0
_LoadedRes = 0;_SavedRes = 0
_ErrorRes = 0;

For Local _Res:TResource = EachIn _ResList
Select _Res._State
Case TRM_LOADING
_LoadingRes :+ 1
Case TRM_LOADED
_LoadedRes :+ 1
Case TRM_SAVING
_SavingRes :+ 1
Case TRM_SAVED
_SavedRes :+ 1
Case TRM_NONE
Default
_ErrorRes :+ 1
End Select
Next
?Threaded _ResMutex.Unlock()
?
End Method
Method GetCount:Int(Which:Int)
Select Which
Case TRM_NONE
Return _TotalRes-_LoadedRes-_LoadingRes-_SavedRes-_SavingRes-_ErrorRes
Case TRM_LOADING
Return _LoadingRes
Case TRM_LOADED
Return _LoadedRes
Case TRM_SAVING
Return _SavingRes
Case TRM_SAVED
Return _SavedRes
Case TRM_ERROR
Return _ErrorRes
Case TRM_ALL
Return _TotalRes
Default
Return 0
End Select
EndMethod

Method AddResource(Res:TResource)
?Threaded _ResMutex.Lock()
?
_ResList.AddLast(Res)
Res._State = TRM_NONE
?Threaded _ResMutex.Unlock()
?
End Method
Method RemoveResourceName(Name:String)
?Threaded _ResMutex.Lock()
?
For Local _Res:TResource = EachIn _ResList
If _Res.Name = Name Then
_ResList.Remove(_Res)
_Res = Null
End If
Next
?Threaded _ResMutex.Unlock()
?
End Method
Method RemoveResource(Res:TResource)
?Threaded _ResMutex.Lock()
?
_ResList.Remove(Res)
?Threaded _ResMutex.Unlock()
?
End Method
Method ClearResource(Which:Int=TRM_ALL)
?Threaded _ResMutex.Lock()
?
_ResList.Clear()
?Threaded _ResMutex.Unlock()
?
End Method

Method GetResourceName:TResource(Name:String, Which:Int=TRM_ALl)
Local _rRes:TResource
?Threaded _ResMutex.Lock()
?
For Local _Res:TResource = EachIn _ResList
If _Res.Name = Name And (_Res._State = Which Or Which = TRM_ALL)
_rRes = _Res
Exit
EndIf
Next
?Threaded _ResMutex.Unlock()
?
Return _rRes
End Method
Method GetResourcesName:TResource[](Name:String, Which:Int=TRM_ALL)
Local _rList:TList = New TList
?Threaded _ResMutex.Lock()
?
For Local _Res:TResource = EachIn _ResList
If _Res.Name = Name And (_Res._State = Which Or Which = TRM_ALL)
_rList.AddLast(_Res)
EndIf
Next
?Threaded _ResMutex.Unlock()
?
Return TResource[](_rList.ToArray())
End Method
Method GetResource:TResource(Which:Int=TRM_ALl)
Local _rRes:TResource
?Threaded _ResMutex.Lock()
?
For Local _Res:TResource = EachIn _ResList
If (_Res._State = Which Or Which = TRM_ALL)
_rRes = _Res
Exit
EndIf
Next
?Threaded _ResMutex.Unlock()
?
Return _rRes
End Method
Method GetResources:TResource[](Which:Int=TRM_ALL)
Local _rList:TList = New TList
?Threaded _ResMutex.Lock()
?
For Local _Res:TResource = EachIn _ResList
If (_Res._State = Which Or Which = TRM_ALL)
_rList.AddLast(_Res)
EndIf
Next
?Threaded _ResMutex.Unlock()
?
Return TResource[](_rList.ToArray())
End Method

Method LoadResource:Int(Which:Int=TRM_NONE)
Local _Res:TResource = GetResource(Which), _rVal:Int
If _Res = Null
Return TRM_LS_NORESOURCE
Else
?Threaded _ResMutex.Lock()
?
_Res._State = TRM_LOADING
?Threaded _ResMutex.Unlock()
?

_ResourceLoaderFunc(_Res)

?Threaded _ResMutex.Lock()
?
If _Res.Exception = Null And _Res.Resource <> Null
_Res._State = TRM_LOADED
_rVal = TRM_LS_SUCCESS
Else
If _Res.Exception = Null
_Res.Exception = New TRLException
End If
_Res._State = TRM_ERROR
_rVal = TRM_LS_ERROR
End If
?Threaded _ResMutex.Unlock()
?
Return _rVal
End If
End Method
Method SaveResource:Int(Which:Int=TRM_NONE)
Local _Res:TResource = GetResource(Which), _rVal:Int
If _Res = Null
Return TRM_LS_NORESOURCE
Else
?Threaded _ResMutex.Lock()
?
_Res._State = TRM_SAVING
?Threaded _ResMutex.Unlock()
?

_ResourceSaverFunc(_Res)

?Threaded _ResMutex.Lock()
?
If _Res.Exception = Null
_Res._State = TRM_SAVED
_rVal = TRM_LS_SUCCESS
Else
_Res._State = TRM_ERROR
_rVal = TRM_LS_ERROR
End If
?Threaded _ResMutex.Unlock()
?
Return _rVal
End If
End Method
End Type
Type TResource
Field _State:Int

Field Name:String, File:String
Field Resource:Object, Exception:Object

Method New();EndMethod
Method Set(Name:String, File:String)
Self.Name = Name
Self.File = File
Self.Resource = Null
Self.Exception = Null
End Method
Method Remove()
Self.Resource = Null
Self.Exception = Null
Self.File = Null
Self.Name = Null
End Method

Method _Load();End Method
Method _Save();End Method
End Type

Function TResourceLoaderFunc(Res:TResource)
Try
Res._Load()
Catch E:Object
Res.Exception = E
Res.Resource = Null
EndTry
End Function
Function TResourceSaverFunc(Res:TResource)
Try
Res._Save()
Catch E:Object
Res.Exception = E
Res.Resource = Null
EndTry
End Function


~-Funktioniert das nu schon?-~
Nein, ihr braucht auch die Module für die jeweiligen Module deren Lade- und Speicherfunktionen ihr haben wollt.
BlitzMax: [AUSKLAPPEN]
'!BRL.Audio + AudioSample
Import brl.audio
Import brl.audiosample
Type TSoundResource Extends TResource
Field Flags:Int
Method SetSound(Name:String, File:String, Flags:Int=-1)
Self.Flags = Flags
Set(Name,File)
EndMethod

Method _Load()
Self.Resource = LoadSound(Self.File, Self.Flags)
EndMethod
End Type
Type TAudioSampleResource Extends TResource
Method _Load()
Self.Resource = LoadAudioSample(Self.File)
End Method
End Type

'!BRL.Bank + BRL.BankStream
Import brl.bank
Type TBankResource Extends TResource
Method _Load()
Self.Resource = LoadBank(Self.File)
End Method
Method _Save()
SaveBank(TBank(Self.Resource), Self.File)
End Method
End Type

'!BRL.Font
Import brl.font
Type TFontResource Extends TResource
Field Size:Int
Field Style:Int

Method SetFont(Name:String, File:String, Size:Int, Style:Int=SMOOTHFONT)
Self.Size = Size
Self.Style = Style
Set(Name,File)
EndMethod

Method _Load()
Self.Resource = LoadFont(Self.File, Self.Size, Self.Style)
End Method
End Type

'BRL.Max2D
Import brl.max2d
Type TImageResource Extends TResource
Field Flags:Int = -1
Field Animated:Int = False
Field CellWidth:Int = 0
Field CellHeight:Int = 0
Field FirstCell:Int = 0
Field CellCount:Int = 0

Method SetImage(Name:String, File:String, Flags:Int=-1)
Self.Flags = Flags
Self.Animated = False
Set(Name,File)
End Method
Method SetAnimImage(Name:String, File:String, CellWidth:Int, CellHeight:Int, FirstCell:Int, CellCount:Int, Flags:Int=-1)
Self.Flags = Flags
Self.Animated = True
Self.CellWidth = CellWidth
Self.CellHeight = CellHeight
Self.FirstCell = FirstCell
Self.CellCount = CellCount
Set(Name,File)
EndMethod

Method _Load()
If Self.Animated = False
Self.Resource = LoadImage(Self.File, Self.Flags)
Else
Self.Resource = LoadAnimImage(Self.File, Self.CellWidth, Self.CellHeight, Self.FirstCell, Self.CellCount, Self.Flags)
EndIf
EndMethod
End Type

'BRL.Pixmap
Import brl.pixmap
Import brl.bmploader
Import brl.jpgloader
Import brl.pngloader
Import brl.tgaloader
Type TPixmapResource Extends TResource
Field IsPNG:Int
Field Parameter:Int

Method SetPixmapJPG(Name:String, File:String, Quality:Int=75)
Self.IsPNG = False
Self.Parameter = Quality
Set(Name, File)
End Method
Method SetPixmapPNG(Name:String, File:String, Compression:Int=5)
Self.IsPNG = True
Self.Parameter = Compression
Set(Name, File)
End Method

Method _Load()
Self.Resource = LoadPixmap(Self.File)
End Method
Method _Save()
If IsPNG = True
SavePixmapPNG(TPixmap(Self.Resource), Self.File, Self.Parameter)
Else
SavePixmapJPeg(TPixmap(Self.Resource), Self.File, Self.Parameter)
EndIf
End Method
End Type

'BRL.Stream
Import brl.stream
Type TByteArrayResource Extends TResource
Method _Load()
Self.Resource = LoadByteArray(Self.File)
End Method
Method _Save()
SaveByteArray(Byte[](Self.Resource), Self.File)
End Method
End Type
Type TObjectResource Extends TResource
Method _Load()
Self.Resource = LoadObject(Self.File)
End Method
Method _Save()
SaveObject(Self.Resource, Self.File)
End Method
End Type
Type TStringResource Extends TResource
Method _Load()
Self.Resource = LoadString(Self.File)
End Method
Method _Save()
SaveString(String(Self.Resource), Self.File)
End Method
End Type
Type TStreamResource Extends TResource
Field Readable:Int = False
Field Writeable:Int = False

Method SetStream(Name:String, File:String, Readable:Int, Writeable:Int)
Readable = Readable
Writeable = Writeable
Set(Name, File)
End Method

Method _Load()
Self.Resource = OpenStream(Self.File, Self.Readable, Self.Writeable)
End Method
Method _Save()
FlushStream(TStream(Self.Resource))
End Method
End Type

'BRL.TextStream
Import brl.textstream
Type TTextResource Extends TResource
Method _Load()
Self.Resource = LoadText(Self.File)
End Method
End Type

'Xaymar.DataPak
Import xaymar.datapak
Type TDataPakResource Extends TResource
Field Password:String
Field Compressed:Int
Field Stream:TStream

Method SetDataPak(Name:String, URL:Object, Password:String="", Compressed:Int=False)
Self.Password = Password
Self.Compressed = Compressed
If TStream(URL) <> Null Then
Self.Stream = TStream(URL)
Set(Name, "")
ElseIf String(URL) <> Null Then
Self.Stream = Null
Set(Name, String(URL))
EndIf
End Method

Method _Load()
If Stream = Null
Self.Resource = TDataPak.FromFile(Self.File, Self.Password)
Else
Self.Resource = TDataPak.FromStream(Self.Stream)
End If
End Method
Method _Save()
Local _Flags:Byte
If Self.Password <> "" Then _Flags :+ TDP_FLAG_PASSWORDED
If Self.Compressed <> 0 Then _Flags :+ TDP_FLAG_COMPRESSED
If Stream = Null
TDataPak(Self.Resource).ToFile(Self.File, _Flags, Self.Password)
Else
TDataPak(Self.Resource).ToStream(Self.Stream)
End If
End Method
End Type


~-Beispiele-~
Ohne Threading:
BlitzMax: [AUSKLAPPEN]
'Xaymar.Resource:    Test 01
' Loading of Images and Sounds
SuperStrict

Import xaymar.resource
Import xaymar.brlaudio
Import xaymar.brlmax2d
Import brl.audio
Import brl.max2d

Graphics(800,600,0,60)
SetVirtualResolution(100,100)

Global MyResource:TResourceManager = New TResourceManager

Local imgDir:String[] = LoadDir("data/img/")
For Local fileImg:String = EachIn imgDir
Local _Res:TImageResource = New TImageResource
_Res.SetImage(fileImg, "data/img/"+fileImg)
MyResource.AddResource(_Res)
Next

Local sndDir:String[] = LoadDir("data/snd/")
For Local fileSnd:String = EachIn sndDir
Local _Res:TSoundResource = New TSoundResource
_Res.SetSound(fileSnd, "data/snd/"+fileSnd)
MyResource.AddResource(_Res)
Next

Local _ExitLoad:Int = False
Repeat
Cls

Local _BarPerc:Float = Float(MyResource.GetCount(TRM_LOADED)) / Float(MyResource.GetCount(TRM_ALL))
Local _BarPercLoad:Float = Float(MyResource.GetCount(TRM_LOADING)) / Float(MyResource.GetCount(TRM_ALL))
Local _BarPercError:Float = Float(MyResource.GetCount(TRM_ERROR)) / Float(MyResource.GetCount(TRM_ALL))
Local _BarColorMod:Float = 0.5+Sin( Float(MilliSecs()) / Float(1000/360) )*0.5

SetBlend ALPHABLEND
SetColor 102+153*_BarColorMod, 102+153*_BarColorMod, 102+153*_BarColorMod
DrawLine( 5, 90, 95, 90)
DrawLine( 5, 90, 5, 95)
DrawLine( 5, 95, 95, 95)
DrawLine(95, 90, 95, 95)

SetAlpha 0.9+0.1*_BarColorMod
SetColor 51, 255, 51
DrawRect( 6, 91, 89*_BarPerc, 4)

SetAlpha 0.8+0.2*_BarColorMod
SetColor 255, 255, 51
DrawRect( 6+89*_BarPerc, 91, 89*_BarPercLoad, 4)

SetAlpha 0.7+0.3*_BarColorMod
SetColor 255, 51, 51
DrawRect( 6+89*_BarPerc+89*_BarPercLoad, 91, 89*_BarPercError, 4)

SetColor 255,255,255
SetAlpha 1.0*_BarColorMod*_BarPerc
DrawRect( 5.5, 90.5, 90, 5)


SetAlpha 0.8
SetVirtualResolution(GraphicsWidth(),GraphicsHeight())
DrawText("Complete %:"+Int(_BarPerc*100),0,0)
DrawText("Loading %:"+Int(_BarPercLoad*100),0,15)
DrawText("Errored %:"+Int(_BarPercError*100),0,30)

If MyResource.GetResource(TRM_NONE) <> Null
DrawText(MyResource.GetResource(TRM_NONE).File,800*0.05,600*0.80)
Else
DrawText("Done, Press any key to continue...",800*0.05,600*0.80)
EndIf
SetVirtualResolution(100,100)

Flip

Local LRCode:Int = MyResource.LoadResource()
MyResource.Update() 'Update Count

If AppTerminate()
End
End If
If LRCode = TRM_LS_NORESOURCE
For Local I:Int = 0 To 255
If KeyDown(I) = True
_ExitLoad = True
End If
Next
End If
Until _ExitLoad = True

SetVirtualResolution(GraphicsWidth(),GraphicsHeight())

Print "Resource List:"
For Local _Res:TResource = EachIn MyResource.GetResources(TRM_ALL)
Select _Res._State
Case TRM_LOADED, TRM_SAVED
Print " SUCCESS: ["+_Res.Name+"]"+_Res.File
Case TRM_LOADING, TRM_SAVING
Print " STUCK: ["+_Res.Name+"]"+_Res.File
Case TRM_ERROR, TRM_NONE
Print " FAILED: ["+_Res.Name+"]"+_Res.File + " //"+_Res.Exception.ToString()
End Select
Next
End

Mit Threading: Kommt eventuell noch...

~-Benutzung-~
  • TResourceManager
    • New - Erstellt einen neuen leeren TResourceManager
    • Remove - Nullt die Einträge eines TResourceManagers
    • SetLoaderFunc - Setzt eine neue gesicherte Funktion zum Laden
    • SetSaverFunc - Setzt eine neue gesicherte Funktion zum Speichern
    • Update - Erneuert die Zahlen die man mit GetCount auslesen kann
    • GetCount - Gibt dir die gewählte Zahl zurück
    • AddResource - Fügt eine neue Resource zur Liste hinzu
    • RemoveResourceName - Entfernt einen benannten Eintrag aus der Liste(löscht diesen aber nicht)
    • RemoveResource - Entfernt eine bekannte Resource aus der Liste(löscht diese aber nicht)
    • ClearResource - Entfernt alle Resource aus der Liste(löscht diese aber nicht)
    • GetResourceName - Liefert eine benannte Resource mit einem bestimmten Status zurück
    • GetResourcesName - Liefert alle benannten Resourcen mit einem bestimmten Status zurück
    • GetResource - Liefert eine Resource mit einem bestimmten Status zurück
    • GetResources - Liefert alle Resourcen mit einem bestimmten Status zurück
    • LoadResource - Lädt die nächstbeste Resource
    • SaveResource - Speichert die nächstbeste Resource

  • TResource
    • New - Erstellt eine neue leere TResource
    • Set - Setzt die Standard Daten fest
    • Remove - Nullt alle bekannten Einträge einer TResource
    • _Load - Interne Lademethode
    • _Save - Interne Speichermethode

  • Konstanten
    • TRM_NONE - Resource wurde noch nicht verändert
    • TRM_LOADING - Resource ist derzeit am laden
    • TRM_SAVING - Resource ist derzeit am speichern
    • TRM_LOADED - Resource wurde geladen
    • TRM_SAVED - Resource wurde gespeichert
    • TRM_ERROR - Resource hatte beim laden/speichern einen Fehler
    • TRM_ALL - Alle Resourcestati vereint
    • TRM_LS_ERROR - Load/SaveResource hatte einen Fehler verursacht
    • TRM_LS_SUCCESS - Load/SaveResource wurde erfolgreich ausgeführt
    • TRM_LS_NORESOURCE - Load/SaveResource hat keine weiteren Resourcen gefunden


Erweiterungen der TResource Type werden nicht aufgelistet, die Grundfunktionen sind aber hauptsächlich dieselben.
Warbseite

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group