WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
GameFile.cpp
Go to the documentation of this file.
1#include "GameFile.h"
2#include <cstring> // memcpy
3#include "Logger.h"
4
5size_t GameFile::read(void* dest, size_t bytes)
6{
7 if (eof)
8 return 0;
9
10 const size_t rpos = pointer + bytes;
11 if (rpos > size)
12 {
13 bytes = size - pointer;
14 eof = true;
15 }
16
17 memcpy(dest, &(buffer[pointer]), bytes);
18
19 pointer = rpos;
20
21 return bytes;
22}
23
25{
26 return eof;
27}
28
29void GameFile::seek(size_t offset)
30{
31 pointer = offset;
32 eof = (pointer >= size);
33}
34
35void GameFile::seekRelative(size_t offset)
36{
37 pointer += offset;
38 eof = (pointer >= size);
39}
40
41bool GameFile::open(bool useMemoryBuffer /* = true */)
42{
43 if (isAlreadyOpened())
44 return true;
45
46 eof = true;
47
48 LOG_INFO << "GameFile::open - openFile() for " << filepath;
49 if (!openFile())
50 return false;
51
52 m_useMemoryBuffer = useMemoryBuffer;
53
54 if (getFileSize(size))
55 {
56 LOG_INFO << "GameFile::open - size=" << size << " buffered=" << m_useMemoryBuffer << " for " << filepath;
58 {
60
61 LOG_INFO << "GameFile::open - readFile() for " << filepath;
62 if (readFile() != 0)
63 eof = false;
64
65 LOG_INFO << "GameFile::open - doPostOpenOperation() for " << filepath;
67 }
68 }
69
70 LOG_INFO << "GameFile::open - done for " << filepath;
71 return true;
72}
73
75{
76 delete[] originalBuffer;
77 originalBuffer = nullptr;
78 buffer = nullptr;
79 eof = true;
80 chunks.clear();
81 return doPostCloseOperation();
82}
83
84void GameFile::allocate(unsigned long long s)
85{
87 delete[] originalBuffer;
88
89 size = s;
90
91 originalBuffer = new unsigned char[size];
93
94 if (size == 0)
95 eof = true;
96 else
97 eof = false;
98}
99
100bool GameFile::setChunk(std::string chunkName, bool resetToStart)
101{
102 bool result = false;
103
104 // save current pointer if a chunk is currently under reading
105 for (auto& it : chunks)
106 {
107 if (it.magic == curChunk)
108 {
109 it.pointer = pointer;
110 break;
111 }
112 }
113
114 for (auto& it : chunks)
115 {
116 if (it.magic == chunkName)
117 {
118 buffer = originalBuffer + it.start;
119 pointer = (resetToStart ? 0 : it.pointer);
120 size = it.size;
121 curChunk = chunkName;
122 result = true;
123 eof = (pointer >= size);
124 break;
125 }
126 }
127
128 // if (!result)
129 // LOG_ERROR << __FUNCTION__ << "Cannot find chunk" << chunkName.c_str();
130
131 return result;
132}
133
135{
136 return size;
137}
138
140{
141 return pointer;
142}
143
144unsigned char* GameFile::getBuffer() const
145{
146 return buffer;
147}
148
149unsigned char* GameFile::getPointer()
150{
151 return buffer + pointer;
152}
153
155{
156 LOG_INFO << "Structure for file" << filepath;
157 for (auto it : chunks)
158 LOG_INFO << "Chunk :" << it.magic.c_str() << it.start << it.size;
159}
#define LOG_INFO
Definition Logger.h:10
virtual void doPostOpenOperation()=0
bool eof
Definition GameFile.h:81
unsigned char * getPointer()
Pointer to the current read position within the buffer.
Definition GameFile.cpp:149
virtual bool doPostCloseOperation()=0
bool m_useMemoryBuffer
Definition GameFile.h:96
bool open(bool useMemoryBuffer=true)
Open the file, optionally loading into a memory buffer.
Definition GameFile.cpp:41
std::string filepath
Definition GameFile.h:84
std::string curChunk
Definition GameFile.h:102
virtual void seek(size_t offset)
Seek to an absolute byte offset.
Definition GameFile.cpp:29
size_t getPos()
Current read position.
Definition GameFile.cpp:139
size_t getSize()
Total size of the file in bytes.
Definition GameFile.cpp:134
std::vector< Chunk > chunks
Definition GameFile.h:95
unsigned char * buffer
Definition GameFile.h:82
virtual void dumpStructure()
Definition GameFile.cpp:154
virtual unsigned long readFile()=0
virtual bool isAlreadyOpened()=0
virtual bool openFile()=0
unsigned char * getBuffer() const
Pointer to the start of the internal buffer.
Definition GameFile.cpp:144
unsigned long long size
Definition GameFile.h:83
bool close()
Close the file and release the internal buffer.
Definition GameFile.cpp:74
virtual size_t read(void *dest, size_t bytes)
Read bytes from the file into dest.
Definition GameFile.cpp:5
unsigned long long pointer
Definition GameFile.h:83
void seekRelative(size_t offset)
Advance the read pointer by offset bytes.
Definition GameFile.cpp:35
unsigned char * originalBuffer
Definition GameFile.h:101
bool setChunk(std::string chunkName, bool resetToStart=true)
Switch the active read window to the named chunk.
Definition GameFile.cpp:100
virtual bool getFileSize(unsigned long long &s)=0
void allocate(unsigned long long size)
Allocate (or reallocate) the internal buffer to the given size.
Definition GameFile.cpp:84
bool isEof()
True if the read pointer has reached the end of the file.
Definition GameFile.cpp:24