PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/map_extractor/loadlib.cpp

https://gitlab.com/tkrokli/TrinityCore_434
C++ | 82 lines | 53 code | 11 blank | 18 comment | 8 complexity | 593f6b79cdb9b4f43290d9251d1f81df MD5 | raw file
 1/*
 2 * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
 3 * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
 4 *
 5 * This program is free software; you can redistribute it and/or modify it
 6 * under the terms of the GNU General Public License as published by the
 7 * Free Software Foundation; either version 2 of the License, or (at your
 8 * option) any later version.
 9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define _CRT_SECURE_NO_DEPRECATE
20
21#include "loadlib.h"
22#include <cstdio>
23
24u_map_fcc MverMagic = { {'R','E','V','M'} };
25
26FileLoader::FileLoader()
27{
28    data = 0;
29    data_size = 0;
30    version = 0;
31}
32
33FileLoader::~FileLoader()
34{
35    free();
36}
37
38bool FileLoader::loadFile(HANDLE mpq, char* filename, bool log)
39{
40    free();
41    HANDLE file;
42    if (!SFileOpenFileEx(mpq, filename, SFILE_OPEN_PATCHED_FILE, &file))
43    {
44        if (log)
45            printf("No such file %s\n", filename);
46        return false;
47    }
48
49    data_size = SFileGetFileSize(file, NULL);
50    data = new uint8[data_size];
51    SFileReadFile(file, data, data_size, NULL/*bytesRead*/, NULL);
52    if (prepareLoadedData())
53    {
54        SFileCloseFile(file);
55        return true;
56    }
57
58    printf("Error loading %s\n", filename);
59    SFileCloseFile(file);
60    free();
61
62    return false;
63}
64
65bool FileLoader::prepareLoadedData()
66{
67    // Check version
68    version = (file_MVER *) data;
69    if (version->fcc != MverMagic.fcc)
70        return false;
71    if (version->ver != FILE_FORMAT_VERSION)
72        return false;
73    return true;
74}
75
76void FileLoader::free()
77{
78    if (data) delete[] data;
79    data = 0;
80    data_size = 0;
81    version = 0;
82}