diff --git a/platform/file.go b/platform/file.go index 0dfc0c4..2b375a2 100644 --- a/platform/file.go +++ b/platform/file.go @@ -1,19 +1,63 @@ package platform -import "github.com/FooSoft/lazarus/formats/mpq" +import ( + "errors" + "path/filepath" + + "github.com/FooSoft/lazarus/formats/mpq" +) var fileState struct { mountPoints map[string]*mpq.Archive + mountPaths map[string]*mpq.Archive } -type File struct { -} +type File struct{} func FileMountArchive(mountPath, archivePath string) error { + archive, err := mpq.NewFromFile(archivePath) + if err != nil { + return err + } + + if fileState.mountPoints == nil { + fileState.mountPoints = make(map[string]*mpq.Archive) + } + + var count int + for _, path := range archive.GetPaths() { + resourcePath := filepath.Join(mountPath, path) + if _, ok := fileState.mountPoints[resourcePath]; !ok { + fileState.mountPoints[resourcePath] = archive + count++ + } + } + + if count == 0 { + archive.Close() + return errors.New("file archive could not be mounted") + } + return nil } func FileUnmountArchive(mountPath string) error { + archive, ok := fileState.mountPoints[mountPath] + if !ok { + return errors.New("file archive is nout mounted") + } + + var paths []string + for p, a := range fileState.mountPaths { + if archive == a { + paths = append(paths, p) + } + } + + for _, p := range paths { + delete(fileState.mountPaths, p) + } + return nil }