Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
MpqArchive.h
1 //
2 // Created by Lectem on 11/11/2016.
3 //
4 
5 #pragma once
6 
7 #include <vector>
8 #include "Archive.h"
9 #include "Stream.h"
10 
11 namespace WorldStone
12 {
13 
17 class MpqArchive : public Archive
18 {
19  using HANDLE = void*; // Do not expose stormlib
20 public:
21  MpqArchive() { setstate(badbit); }
22  MpqArchive(const char* MpqFileName, const char* listFilePath = nullptr);
23  MpqArchive(MpqArchive&& toMove);
24  MpqArchive& operator=(MpqArchive&& toMove);
25  ~MpqArchive() override;
26 
27  bool exists(const Path& filePath) override;
28  StreamPtr open(const Path& filePath) override;
29 
30  HANDLE getInternalHandle() { return mpqHandle; }
31 
32  void addListFile(const char* listFilePAth);
33  std::vector<Path> findFiles(const Path& searchMask = "*");
34 
35 private:
36  bool load() override;
37  bool is_loaded() override;
38  bool unload() override;
39 
40  Path mpqFileName;
41  HANDLE mpqHandle = nullptr;
42 };
43 
47 class MpqFileStream : public IStream
48 {
49  using HANDLE = void*;
50 
51  HANDLE file = nullptr;
52 
53 protected:
54  MpqFileStream() = default; // Needed to make tests easier
55 public:
56  MpqFileStream(MpqArchive& archive, const Path& filename);
57  bool open(MpqArchive& archive, const Path& filename);
58  bool is_open() const { return file != nullptr; }
59  bool close();
60 
61  size_t read(void* buffer, size_t size) override;
62 
63  long tell() override;
64  long size() override;
65 
69  bool seek(long offset, seekdir origin) override;
70 
71  ~MpqFileStream() override;
72 };
73 }
An interface for a stream of data.
Definition: Stream.h:22
bool seek(long offset, seekdir origin) override
Definition: MpqArchive.cpp:124
Base class to use/build archives.
Definition: Archive.h:24
long size() override
Compute the size of the file.
Definition: MpqArchive.cpp:131
long tell() override
Return the current position of the stream pointer.
Definition: MpqArchive.cpp:113
A wrapper to manage MPQ archives.
Definition: MpqArchive.h:17
size_t read(void *buffer, size_t size) override
Read data from the stream.
Definition: MpqArchive.cpp:98
A file from a MpqArchive.
Definition: MpqArchive.h:47