From fcff7026e83bc8f871f56196f2000dfbd1ea2c1f Mon Sep 17 00:00:00 2001 From: guigang xie Date: Tue, 31 Oct 2023 11:44:35 +0800 Subject: [PATCH] make imporovements for the resource heklpoer --- src/LINQ/RQL/NodeMap.vb | 16 ++++++++-- src/LINQ/RQL/Resource.vb | 69 ++++++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/LINQ/RQL/NodeMap.vb b/src/LINQ/RQL/NodeMap.vb index 6b1bc4d..88543c0 100644 --- a/src/LINQ/RQL/NodeMap.vb +++ b/src/LINQ/RQL/NodeMap.vb @@ -1,6 +1,4 @@ -Imports Microsoft.VisualBasic.Data.IO - -''' +''' ''' A descriptor of the specific resource file ''' Public Class NodeMap @@ -16,6 +14,18 @@ Public Class NodeMap End Get End Property + ''' + ''' add resource with duplicated removes + ''' + ''' + Public Sub add(resource As String) + If resource Is Nothing Then + Return + ElseIf resources.IndexOf(resource) = -1 Then + Call resources.Add(resource) + End If + End Sub + Public Overrides Function ToString() As String Return $"link_size:{size}" End Function diff --git a/src/LINQ/RQL/Resource.vb b/src/LINQ/RQL/Resource.vb index 322f3da..b8597ef 100644 --- a/src/LINQ/RQL/Resource.vb +++ b/src/LINQ/RQL/Resource.vb @@ -17,6 +17,12 @@ Public Class Resource : Implements IDisposable Private disposedValue As Boolean + Public ReadOnly Property Archive As StreamPack + Get + Return buf + End Get + End Property + Sub New(res As StreamPack) Dim indexfile = res.OpenFile("/index.dat", FileMode.OpenOrCreate, FileAccess.Read) Dim parser As New IndexReader(indexfile) @@ -25,27 +31,55 @@ Public Class Resource : Implements IDisposable index = parser.Read End Sub + ''' + ''' Add a string resource into current arhive file, + ''' and associated this string resource data with + ''' a given value. + ''' + ''' + ''' + ''' Public Function Add(key As String, str As String) Return Add(key, Encoding.UTF8.GetBytes(str)) End Function - Public Function ReadString(map As String) As String - Return Encoding.UTF8.GetString(ReadBuffer(map)) + Public Function ReadString(map As String, Optional category As String = "") As String + Return Encoding.UTF8.GetString(ReadBuffer(map, category)) End Function - Public Function ReadBuffer(map As String) As Byte() - Dim path As String = URL(map) + ''' + ''' Read the resource pack data from the archive via a given resource map key + ''' + ''' A resource key which is from + ''' the archive index via a given query text + ''' + ''' + ''' this function just returns nothing if the given resource + ''' is not exists inside of current archive file. + ''' + Public Function ReadBuffer(map As String, Optional category As String = "") As Byte() + Dim path As String = URL(map, category) + + If Not buf.FileExists(path) Then + Return Nothing + End If + Dim file As Stream = buf.OpenFile(path, FileMode.Open, FileAccess.Read) Dim bytes As Byte() = New Byte(file.Length - 1) {} Call file.Read(bytes, Scan0, bytes.Length) Return bytes End Function + ''' + ''' generates the internal package resource reference url + ''' + ''' + ''' - Private Shared Function URL(map As String) As String - Return $"/pool/{map.Substring(4, 2)}/{map.Substring(16, 6)}/{map}" + Private Shared Function URL(map As String, category As String) As String + Return $"/pool/{category}/{map.Substring(4, 2)}/{map.Substring(16, 6)}/{map}" End Function Public Shared Function GetHashKey(data As Byte()) As String @@ -76,21 +110,24 @@ Public Class Resource : Implements IDisposable ''' this resource data is generated via a specific hash algorithm based on ''' this data payload. ''' - Public Function Add(key As String, data As Byte()) As Boolean + Public Function Add(key As String, data As Byte(), Optional category As String = "") As Boolean Dim tokens As String() = Strings.LCase(key).Split Dim map As String = GetHashKey(data) - Dim path As String = URL(map) + Dim path As String = URL(map, category) For Each si As String In tokens - Dim v = index.Add(si) - Dim page As NodeMap = v.data - - If page Is Nothing Then - v.data = New NodeMap With {.resources = New List(Of String)} - page = v.data - End If + For len As Integer = 1 To si.Length - 1 + Dim sij = si.Substring(0, len) + Dim v = index.Add(sij) + Dim page As NodeMap = v.data + + If page Is Nothing Then + v.data = New NodeMap With {.resources = New List(Of String)} + page = v.data + End If - Call page.resources.Add(map) + Call page.add(map) + Next Next Dim file As Stream = buf.OpenFile(path, FileMode.OpenOrCreate, FileAccess.Write)