Skip to content

Commit

Permalink
Pakfile search improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaco committed Dec 22, 2018
1 parent 5c49a90 commit 85be03d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Binary file removed .DS_Store
Binary file not shown.
17 changes: 9 additions & 8 deletions lumps/pakfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,26 @@ func (lump *Pakfile) FromBytes(raw []byte, length int32) {
}
}

// Get internal format structure data
// GetData Get internal format structure data
func (lump *Pakfile) GetData() *zip.Reader {
return lump.zipReader
}

// Returns the contents of this lump as a []byte
// ToBytes Returns the contents of this lump as a []byte
func (lump *Pakfile) ToBytes() []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, lump.data)
return buf.Bytes()
}

// Get a specific file from the pak
// GetFile Get a specific file from the pak
// This function does have the expectation that filenames are case-insensitive,
// so in the (shouldn't happen) case of name collisions over case-insensitivity,
// there may be issues
func (lump *Pakfile) GetFile(filePath string) ([]byte, error) {
filePath = lump.sanitisePath(filePath)
for _, f := range lump.zipReader.File {
if strings.ToLower(f.Name) == filePath {
if lump.sanitisePath(f.Name) == filePath {
rc, err := f.Open()
if err != nil {
return nil, err
Expand All @@ -53,9 +56,7 @@ func (lump *Pakfile) GetFile(filePath string) ([]byte, error) {
return []byte{}, nil
}

// ensures that the requested path matches internal casing
// sanitisePath ensures that the requested path matches internal casing
func (lump *Pakfile) sanitisePath(filePath string) string {
filePath = strings.ToLower(filePath)
filePath = strings.Replace(filePath, "/", "\\", -1)
return filePath
return strings.Replace(strings.ToLower(filePath), "\\", "/", -1)
}

0 comments on commit 85be03d

Please sign in to comment.