Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
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

25
MPQFile::MPQFile(HANDLE mpq, const char* filename, bool warnNoExist /*= true*/) :
26
    eof(false),
27
    buffer(0),
28
    pointer(0),
29
    size(0)
30
{
31
    HANDLE file;
32
    if (!SFileOpenFileEx(mpq, filename, SFILE_OPEN_PATCHED_FILE, &file))
33
    {
34
        if (warnNoExist || GetLastError() != ERROR_FILE_NOT_FOUND)
35
            fprintf(stderr, "Can't open %s, err=%u!\n", filename, GetLastError());
36
        eof = true;
37
        return;
38
    }
39

40
    DWORD hi = 0;
41
    size = SFileGetFileSize(file, &hi);
42

43
    if (hi)
44
    {
45
        fprintf(stderr, "Can't open %s, size[hi] = %u!\n", filename, uint32(hi));
46
        SFileCloseFile(file);
47
        eof = true;
48
        return;
49
    }
50

51
    if (size <= 1)
52
    {
53
        fprintf(stderr, "Can't open %s, size = %u!\n", filename, uint32(size));
54
        SFileCloseFile(file);
55
        eof = true;
56
        return;
57
    }
58

59
    DWORD read = 0;
60
    buffer = new char[size];
61
    if (!SFileReadFile(file, buffer, size, &read) || size != read)
62
    {
63
        fprintf(stderr, "Can't read %s, size=%u read=%u!\n", filename, uint32(size), uint32(read));
64
        SFileCloseFile(file);
65
        eof = true;
66
        return;
67
    }
68

69
    SFileCloseFile(file);
70
}
71

72
size_t MPQFile::read(void* dest, size_t bytes)
73
{
74
    if (eof) return 0;
75

76
    size_t rpos = pointer + bytes;
77
    if (rpos > size) {
78
        bytes = size - pointer;
79
        eof = true;
80
    }
81

82
    memcpy(dest, &(buffer[pointer]), bytes);
83

84
    pointer = rpos;
85

86
    return bytes;
87
}
88

89
void MPQFile::seek(int offset)
90
{
91
    pointer = offset;
92
    eof = (pointer >= size);
93
}
94

95
void MPQFile::seekRelative(int offset)
96
{
97
    pointer += offset;
98
    eof = (pointer >= size);
99
}
100

101
void MPQFile::close()
102
{
103
    if (buffer) delete[] buffer;
104
    buffer = 0;
105
    eof = true;
106
}
107

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.