Legends-of-Azeroth-Pandaria-5.4.8
106 строк · 2.6 Кб
1/*
2* Copyright (C) 2011-2016 Project SkyFire <http://www.projectskyfire.org/>
3* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
4* Copyright (C) 2005-2016 MaNGOS <http://getmangos.com/>
5*
6* This program is free software; you can redistribute it and/or modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation; either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but WITHOUT
12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14* more details.
15*
16* You should have received a copy of the GNU General Public License along
17* with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "mpqfile.h"21#include <deque>22#include <cstdio>23#include "StormLib.h"24
25MPQFile::MPQFile(HANDLE mpq, const char* filename, bool warnNoExist /*= true*/) :26eof(false),27buffer(0),28pointer(0),29size(0)30{
31HANDLE file;32if (!SFileOpenFileEx(mpq, filename, SFILE_OPEN_PATCHED_FILE, &file))33{34if (warnNoExist || GetLastError() != ERROR_FILE_NOT_FOUND)35fprintf(stderr, "Can't open %s, err=%u!\n", filename, GetLastError());36eof = true;37return;38}39
40DWORD hi = 0;41size = SFileGetFileSize(file, &hi);42
43if (hi)44{45fprintf(stderr, "Can't open %s, size[hi] = %u!\n", filename, uint32(hi));46SFileCloseFile(file);47eof = true;48return;49}50
51if (size <= 1)52{53fprintf(stderr, "Can't open %s, size = %u!\n", filename, uint32(size));54SFileCloseFile(file);55eof = true;56return;57}58
59DWORD read = 0;60buffer = new char[size];61if (!SFileReadFile(file, buffer, size, &read) || size != read)62{63fprintf(stderr, "Can't read %s, size=%u read=%u!\n", filename, uint32(size), uint32(read));64SFileCloseFile(file);65eof = true;66return;67}68
69SFileCloseFile(file);70}
71
72size_t MPQFile::read(void* dest, size_t bytes)73{
74if (eof) return 0;75
76size_t rpos = pointer + bytes;77if (rpos > size) {78bytes = size - pointer;79eof = true;80}81
82memcpy(dest, &(buffer[pointer]), bytes);83
84pointer = rpos;85
86return bytes;87}
88
89void MPQFile::seek(int offset)90{
91pointer = offset;92eof = (pointer >= size);93}
94
95void MPQFile::seekRelative(int offset)96{
97pointer += offset;98eof = (pointer >= size);99}
100
101void MPQFile::close()102{
103if (buffer) delete[] buffer;104buffer = 0;105eof = true;106}
107