TrinityCore
Loading...
Searching...
No Matches
mpq_libmpq.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef MPQ_H
19#define MPQ_H
20
21#include "Define.h"
22#include <libmpq/mpq.h>
23#include <string>
24#include <utility>
25#include <vector>
26#include <cstring>
27
29{
30public:
31 mpq_archive_s *mpq_a;
32
33 MPQArchive(char const* filename);
34 MPQArchive(MPQArchive const&) = delete;
35 MPQArchive(MPQArchive&& other) noexcept : mpq_a(std::exchange(other.mpq_a, nullptr)) { }
36 MPQArchive& operator=(MPQArchive const&) = delete;
37 MPQArchive& operator=(MPQArchive&& other) noexcept { if (this != &other) mpq_a = std::exchange(other.mpq_a, nullptr); return *this; }
39 void close();
40
41 void GetFileListTo(std::vector<std::string>& filelist) {
42 uint32_t filenum;
43 if(!mpq_a || libmpq__file_number(mpq_a, "(listfile)", &filenum)) return;
44 libmpq__off_t size, transferred;
45 libmpq__file_size_unpacked(mpq_a, filenum, &size);
46
47 char *buffer = new char[size+1];
48 buffer[size] = '\0';
49
50 libmpq__file_read(mpq_a, filenum, (unsigned char*)buffer, size, &transferred);
51
52 char seps[] = "\n";
53 char *token;
54
55 token = strtok( buffer, seps );
56 uint32 counter = 0;
57 while ((token != nullptr) && (counter < size)) {
58 //cout << token << endl;
59 token[strlen(token) - 1] = 0;
60 std::string s = token;
61 filelist.push_back(s);
62 counter += strlen(token) + 2;
63 token = strtok(nullptr, seps);
64 }
65
66 delete[] buffer;
67 }
68};
69typedef std::vector<MPQArchive> ArchiveSet;
71
73{
74 //MPQHANDLE handle;
75 bool eof;
76 char *buffer;
77 libmpq__off_t pointer,size;
78
79 // disable copying
80 MPQFile(MPQFile const& /*f*/) = delete;
81 void operator=(MPQFile const& /*f*/) = delete;
82
83public:
84 MPQFile(char const* filename); // filenames are not case sensitive
85 ~MPQFile() { close(); }
86 size_t read(void* dest, size_t bytes);
87 size_t getSize() { return size; }
88 size_t getPos() { return pointer; }
89 char* getBuffer() { return buffer; }
90 char* getPointer() { return buffer + pointer; }
91 bool isEof() { return eof; }
92 void seek(int offset);
93 void seekRelative(int offset);
94 void close();
95};
96
97inline void flipcc(char *fcc)
98{
99 char t;
100 t=fcc[0];
101 fcc[0]=fcc[3];
102 fcc[3]=t;
103 t=fcc[1];
104 fcc[1]=fcc[2];
105 fcc[2]=t;
106}
107
108namespace MPQ
109{
110bool OpenArchives(std::string_view inputPath, std::string_view localeName);
111void CloseArchives();
112}
113
114#endif
uint32_t uint32
Definition Define.h:133
MPQArchive(MPQArchive &&other) noexcept
Definition mpq_libmpq.h:35
MPQArchive & operator=(MPQArchive const &)=delete
MPQArchive(MPQArchive const &)=delete
MPQArchive & operator=(MPQArchive &&other) noexcept
Definition mpq_libmpq.h:37
mpq_archive_s * mpq_a
Definition mpq_libmpq.h:31
void GetFileListTo(std::vector< std::string > &filelist)
Definition mpq_libmpq.h:41
void close()
size_t getSize()
Definition mpq_libmpq.h:87
size_t read(void *dest, size_t bytes)
void operator=(MPQFile const &)=delete
MPQFile(MPQFile const &)=delete
char * buffer
Definition mpq_libmpq.h:76
char * getPointer()
Definition mpq_libmpq.h:90
bool isEof()
Definition mpq_libmpq.h:91
void close()
libmpq__off_t size
Definition mpq_libmpq.h:77
size_t getPos()
Definition mpq_libmpq.h:88
void seek(int offset)
void seekRelative(int offset)
libmpq__off_t pointer
Definition mpq_libmpq.h:77
char * getBuffer()
Definition mpq_libmpq.h:89
bool eof
Definition mpq_libmpq.h:75
std::vector< MPQArchive > ArchiveSet
Definition mpq_libmpq.h:69
ArchiveSet gOpenArchives
void flipcc(char *fcc)
Definition mpq_libmpq.h:97
void CloseArchives()
bool OpenArchives(std::string_view inputPath, std::string_view localeName)