2016-09-19 03:26:44 +02:00
Public Class TextureManager
2016-09-07 18:50:38 +02:00
Public Shared DefaultTexture As Texture2D
Public Shared Sub InitializeTextures ( )
2018-02-21 16:34:06 +01:00
DefaultTexture = LoadDirect ( " GUI\no_texture.png " )
2016-09-07 18:50:38 +02:00
End Sub
Public Shared TextureList As New Dictionary ( Of String , Texture2D )
2016-10-06 19:42:31 +02:00
Public Shared TextureRectList As New Dictionary ( Of KeyValuePair ( Of Int32 , Rectangle ) , Texture2D )
2016-09-07 18:50:38 +02:00
2018-02-21 16:34:06 +01:00
Public Shared Function LoadDirect ( textureFile As String ) As Texture2D
Using stream As Stream = File . Open ( GameController . GamePath & " \Content\ " & textureFile , IO . FileMode . OpenOrCreate )
Return Texture2D . FromStream ( Core . GraphicsDevice , stream )
End Using
End Function
2016-09-07 18:50:38 +02:00
''' <summary>
''' Returns a texture.
''' </summary>
''' <param name="Name">The name of the texture.</param>
Public Shared Function GetTexture ( ByVal Name As String ) As Texture2D
Dim cContent As ContentManager = ContentPackManager . GetContentManager ( Name , " .xnb,.png " )
Dim tKey As String = cContent . RootDirectory & " \ " & Name & " ,FULL_IMAGE "
If TextureList . ContainsKey ( tKey ) = False Then
Dim t As Texture2D = Nothing
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " .xnb " ) = False Then
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " .png " ) = True Then
Using stream As System . IO . Stream = System . IO . File . Open ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " .png " , IO . FileMode . OpenOrCreate )
Try
t = Texture2D . FromStream ( Core . GraphicsDevice , stream )
Catch ex As Exception
Logger . Log ( Logger . LogTypes . ErrorMessage , " Something went wrong while XNA tried to load a texture. Return default. " )
Return DefaultTexture
End Try
End Using
Else
2023-07-22 12:32:22 +02:00
Logger . Log ( Logger . LogTypes . ErrorMessage , " TextureManager.vb: Texture "" " & GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " "" was not found! " )
2016-09-07 18:50:38 +02:00
Return DefaultTexture
End If
Else
t = cContent . Load ( Of Texture2D ) ( Name )
End If
TextureList . Add ( tKey , ApplyEffect ( TextureRectangle ( t , New Rectangle ( 0 , 0 , t . Width , t . Height ) , 1 ) ) )
cContent . Unload ( )
End If
Return TextureList ( tKey )
End Function
Private Shared Function ApplyEffect ( ByVal t As Texture2D ) As Texture2D
If GameController . Hacker = True Then
Dim newT As New Texture2D ( Core . GraphicsDevice , t . Width , t . Height )
Dim newC As New List ( Of Color )
Dim oldC ( t . Width * t . Height - 1 ) As Color
t . GetData ( oldC )
For Each c As Color In oldC
newC . Add ( c . Invert ( ) )
Next
newT . SetData ( newC . ToArray ( ) )
Return newT
Else
Return t
End If
End Function
''' <summary>
''' Returns a texture.
''' </summary>
''' <param name="Name">The name of the texture.</param>
''' <param name="r">The rectangle to get the texture from.</param>
''' <param name="TexturePath">The texturepath to load a texture from.</param>
Public Shared Function GetTexture ( ByVal Name As String , ByVal r As Rectangle , ByVal TexturePath As String ) As Texture2D
Dim tSource As TextureSource = ContentPackManager . GetTextureReplacement ( TexturePath & Name , r )
Dim cContent As ContentManager = ContentPackManager . GetContentManager ( tSource . TexturePath , " .xnb,.png " )
2022-02-09 21:49:54 +01:00
Dim resolution As Single = ContentPackManager . GetTextureResolution ( TexturePath & Name )
2016-09-07 18:50:38 +02:00
2021-10-18 21:38:20 +02:00
Dim tKey As String = cContent . RootDirectory & " \ " & tSource . TexturePath & " , " & tSource . TextureRectangle . X & " , " & tSource . TextureRectangle . Y & " , " & tSource . TextureRectangle . Width & " , " & tSource . TextureRectangle . Height & " , " & resolution
2016-09-07 18:50:38 +02:00
If TextureList . ContainsKey ( tKey ) = False Then
Dim t As Texture2D = Nothing
Dim doApplyEffect As Boolean = True
2021-10-18 21:38:20 +02:00
If TextureList . ContainsKey ( cContent . RootDirectory & " \ " & tSource . TexturePath ) = True Then
t = TextureList ( cContent . RootDirectory & " \ " & tSource . TexturePath )
2016-09-07 18:50:38 +02:00
doApplyEffect = False
Else
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & tSource . TexturePath & " .xnb " ) = False Then
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & tSource . TexturePath & " .png " ) = True Then
Using stream As System . IO . Stream = System . IO . File . Open ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & tSource . TexturePath & " .png " , IO . FileMode . OpenOrCreate )
Try
t = Texture2D . FromStream ( Core . GraphicsDevice , stream )
Catch ex As Exception
Logger . Log ( Logger . LogTypes . ErrorMessage , " Something went wrong while XNA tried to load a texture. Return default. " )
Return DefaultTexture
End Try
End Using
Else
2023-07-28 12:35:25 +02:00
Logger . Log ( Logger . LogTypes . ErrorMessage , " TextureManager.vb: Texture "" " & GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & TexturePath & Name & " "" was not found! " )
2016-09-07 18:50:38 +02:00
Return DefaultTexture
End If
Else
t = cContent . Load ( Of Texture2D ) ( tSource . TexturePath )
End If
2021-10-18 21:38:20 +02:00
TextureList . Add ( cContent . RootDirectory & " \ " & tSource . TexturePath , ApplyEffect ( t . Copy ( ) ) )
2016-09-07 18:50:38 +02:00
End If
If doApplyEffect = True Then
If TextureList . ContainsKey ( tKey ) = False Then TextureList . Add ( tKey , ApplyEffect ( TextureRectangle ( t , tSource . TextureRectangle , resolution ) ) )
Else
If TextureList . ContainsKey ( tKey ) = False Then TextureList . Add ( tKey , TextureRectangle ( t , tSource . TextureRectangle , resolution ) )
End If
cContent . Unload ( )
End If
Return TextureList ( tKey )
End Function
''' <summary>
''' Returns the texture. The default texture path is "Textures\".
''' </summary>
''' <param name="Name">The name of the texture.</param>
''' <param name="r">The rectangle to get from the texture.</param>
Public Shared Function GetTexture ( ByVal Name As String , ByVal r As Rectangle ) As Texture2D
Return GetTexture ( Name , r , " Textures\ " )
End Function
2022-02-09 21:49:54 +01:00
Public Shared Function GetTexture ( ByVal Texture As Texture2D , ByVal Rectangle As Rectangle , Optional ByVal Factor As Single = 1 ) As Texture2D
2017-02-15 03:41:52 +01:00
Dim tex As Texture2D = Nothing
2018-01-07 18:01:32 +01:00
If TextureRectList . TryGetValue ( New KeyValuePair ( Of Int32 , Rectangle ) ( Texture . GetHashCode ( ) , Rectangle ) , tex ) Then
2016-10-06 19:42:31 +02:00
Return tex
End If
tex = TextureRectangle ( Texture , Rectangle , Factor )
TextureRectList . Add ( New KeyValuePair ( Of Integer , Rectangle ) ( Texture . GetHashCode ( ) , Rectangle ) , tex )
Return tex
End Function
2022-02-09 21:49:54 +01:00
Private Shared Function TextureRectangle ( ByVal Texture As Texture2D , ByVal Rectangle As Rectangle , Optional ByVal Factor As Single = 1 ) As Texture2D
2018-01-07 18:01:32 +01:00
If Rectangle = Rectangle . Empty Then
2016-09-07 18:50:38 +02:00
Return Texture
Else
2022-02-09 21:49:54 +01:00
Rectangle = New Rectangle ( CInt ( Rectangle . X * Factor ) , CInt ( Rectangle . Y * Factor ) , CInt ( Rectangle . Width * Factor ) , CInt ( Rectangle . Height * Factor ) )
2016-09-07 18:50:38 +02:00
Dim tRectangle As New Rectangle ( 0 , 0 , Texture . Width , Texture . Height )
If tRectangle . Contains ( Rectangle ) = False Then
2023-07-22 12:32:22 +02:00
Logger . Log ( Logger . LogTypes . ErrorMessage , " TextureManager.vb: The rectangle for a texture was out of bounds! " )
2016-09-07 18:50:38 +02:00
Return DefaultTexture
End If
Dim Data ( Rectangle . Width * Rectangle . Height - 1 ) As Color
Texture . GetData ( 0 , Rectangle , Data , 0 , Rectangle . Width * Rectangle . Height )
Dim newTex As New Texture2D ( Core . GraphicsDevice , Rectangle . Width , Rectangle . Height )
newTex . SetData ( Data )
Return newTex
End If
End Function
Public Shared Function TextureExist ( ByVal Name As String ) As Boolean
Dim cContent As ContentManager = ContentPackManager . GetContentManager ( Name , " .xnb,.png " )
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " .xnb " ) = True Then
Return True
Else
If System . IO . File . Exists ( GameController . GamePath & " \ " & cContent . RootDirectory & " \ " & Name & " .png " ) = True Then
Return True
End If
End If
Return False
End Function
2016-09-19 03:26:44 +02:00
End Class