~dscharrer/arx/master

« back to all changes in this revision

Viewing changes to src/lib/ArxIO.cpp

  • Committer: Eli2
  • Date: 2014-03-06 20:11:43 UTC
  • Revision ID: git-v1:39029e2623140bf00d14e72a60344d63fbf7e57f
Add library and python wrapper for decompressing ftl files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Arx Libertatis Team (see the AUTHORS file)
 
3
 *
 
4
 * This file is part of Arx Libertatis.
 
5
 *
 
6
 * Arx Libertatis is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * Arx Libertatis is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with Arx Libertatis.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "lib/ArxIO.h"
 
21
 
 
22
#include <deque>
 
23
#include <string>
 
24
#include <iostream>
 
25
#include <fstream>
 
26
#include <cstring>
 
27
 
 
28
#include "io/Blast.h"
 
29
#include "io/log/Logger.h"
 
30
#include "io/log/ConsoleLogger.h"
 
31
 
 
32
namespace logger {
 
33
 
 
34
class MemoryLogger : public Backend {
 
35
 
 
36
public:
 
37
        void log(const Source & file, int line, Logger::LogLevel level, const std::string & str) {
 
38
 
 
39
                ARX_UNUSED(file);
 
40
                ARX_UNUSED(line);
 
41
 
 
42
                if(level == Logger::Error) {
 
43
                        m_lastError = str;
 
44
                }
 
45
 
 
46
                std::string levelName;
 
47
                switch(level) {
 
48
                case Logger::Debug:    levelName = "Debug"; break;
 
49
                case Logger::Info:     levelName = "Info"; break;
 
50
                case Logger::Warning:  levelName = "Warning"; break;
 
51
                case Logger::Error:    levelName = "Error"; break;
 
52
                case Logger::Critical: levelName = "Critical"; break;
 
53
                case Logger::None: ARX_DEAD_CODE(); return;
 
54
                }
 
55
 
 
56
                m_Lines.push_back(levelName + ": " + str);
 
57
        }
 
58
 
 
59
        void flush() {}
 
60
 
 
61
        std::string m_lastError;
 
62
        std::deque<std::string> m_Lines;
 
63
};
 
64
 
 
65
}
 
66
 
 
67
static logger::MemoryLogger memLogger;
 
68
 
 
69
void ArxIO_init() {
 
70
        Logger::add(&memLogger);
 
71
        Logger::set("*", Logger::Debug);
 
72
 
 
73
        LogInfo << "Arx Io library initialized";
 
74
}
 
75
 
 
76
void ArxIO_getError(char * outMessage, int size) {
 
77
        if(!memLogger.m_lastError.empty()) {
 
78
                memLogger.m_lastError.copy(outMessage, size);
 
79
                memLogger.m_lastError.clear();
 
80
        }
 
81
}
 
82
 
 
83
int ArxIO_getLogLine(char * outMessage, int size) {
 
84
        if(memLogger.m_Lines.empty()) {
 
85
                return 0;
 
86
        }
 
87
 
 
88
        memLogger.m_Lines.front().copy(outMessage, size);
 
89
        memLogger.m_Lines.pop_front();
 
90
 
 
91
        return memLogger.m_Lines.size();
 
92
}
 
93
 
 
94
static size_t loadedFtlBufferSize = 0;
 
95
static char * loadedFtlBuffer = NULL;
 
96
 
 
97
int ArxIO_ftlLoad(char * filePath) {
 
98
 
 
99
        ArxIO_ftlRelease();
 
100
 
 
101
        std::ifstream is(filePath, std::ifstream::binary);
 
102
 
 
103
        if(!is) {
 
104
                LogError << "Opening file failed: " << filePath;
 
105
                return -1;
 
106
        }
 
107
 
 
108
        // get length of file:
 
109
        is.seekg(0, is.end);
 
110
        size_t compressedSize = is.tellg();
 
111
        is.seekg(0, is.beg);
 
112
 
 
113
        char * compressedData = new char[compressedSize];
 
114
 
 
115
        LogInfo << "Reading " << compressedSize << " uncompressed bytes";
 
116
        is.read(compressedData, compressedSize);
 
117
 
 
118
        if(is) {
 
119
                LogInfo << "File read successfully";
 
120
        } else {
 
121
                LogError << "Full read failed " << (compressedSize - is.gcount()) << " bytes left";
 
122
                return -1;
 
123
        }
 
124
 
 
125
        is.close();
 
126
 
 
127
        size_t allocsize;
 
128
        char * dat = blastMemAlloc(compressedData, compressedSize, allocsize);
 
129
        if(!dat) {
 
130
                LogError << "Decompressing failed " << filePath;
 
131
                return -1;
 
132
        }
 
133
 
 
134
        LogInfo << "Uncompressed size: " << allocsize;
 
135
 
 
136
        loadedFtlBufferSize = allocsize;
 
137
        loadedFtlBuffer = dat;
 
138
 
 
139
        return 0;
 
140
}
 
141
 
 
142
int ArxIO_ftlRelease() {
 
143
        if(!loadedFtlBuffer) {
 
144
                return -1;
 
145
        }
 
146
 
 
147
        LogInfo << "Releasing loaded ftl data";
 
148
 
 
149
        loadedFtlBufferSize = 0;
 
150
        free(loadedFtlBuffer);
 
151
        loadedFtlBuffer = NULL;
 
152
 
 
153
        return 0;
 
154
}
 
155
 
 
156
int ArxIO_ftlGetRawDataSize() {
 
157
        return loadedFtlBufferSize;
 
158
}
 
159
 
 
160
int ArxIO_ftlGetRawData(char * outData, int size) {
 
161
        if(!loadedFtlBuffer || loadedFtlBufferSize == 0) {
 
162
                LogError << "No buffer loaded";
 
163
                return -1;
 
164
        }
 
165
 
 
166
        if(size < loadedFtlBufferSize) {
 
167
                LogError << "Supplied buffer too small for data";
 
168
                return -2;
 
169
        }
 
170
 
 
171
        memcpy(outData, loadedFtlBuffer, size);
 
172
 
 
173
        return 0;
 
174
}