1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-24 03:47:18 +02:00

Rewrote a piece of CLodHandler::init.

This commit is contained in:
Frank Zago 2009-10-10 23:25:34 +00:00
parent c4e82959f1
commit 044875d500
5 changed files with 104 additions and 49 deletions

View File

@ -231,17 +231,17 @@ SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
}
}
bitmap_handler_mx.lock();
fseek(bitmaph->FLOD, e->offset, 0);
bitmaph->LOD.seekg(e->offset, std::ios::beg);
if (e->size==0) //file is not compressed
{
pcx = new unsigned char[e->realSize];
fread((char*)pcx, 1, e->realSize, bitmaph->FLOD);
bitmaph->LOD.read((char*)pcx, e->realSize);
bitmap_handler_mx.unlock();
}
else
{
unsigned char * pcd = new unsigned char[e->size];
fread((char*)pcd, 1, e->size, bitmaph->FLOD);
bitmaph->LOD.read((char*)pcd, e->size);
bitmap_handler_mx.unlock();
int res=bitmaph->infs2(pcd,e->size,e->realSize,pcx);
if(res!=0)

View File

@ -6,10 +6,13 @@
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <fstream>
#include "boost/filesystem/operations.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/thread.hpp>
#include <SDL_endian.h>
#ifdef max
#undef max
#endif
@ -39,6 +42,7 @@ DLL_EXPORT int readNormalNr (int pos, int bytCon, const unsigned char * str)
else return -1;
return ret;
}
unsigned char * CLodHandler::giveFile(std::string defName, int * length)
{
std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
@ -50,7 +54,7 @@ unsigned char * CLodHandler::giveFile(std::string defName, int * length)
}
if(length) *length = ourEntry->realSize;
mutex->lock();
fseek(FLOD, ourEntry->offset, 0);
unsigned char * outp;
if (ourEntry->offset<0) //file is in the sprites/ folder; no compression
{
@ -73,14 +77,18 @@ unsigned char * CLodHandler::giveFile(std::string defName, int * length)
else if (ourEntry->size==0) //file is not compressed
{
outp = new unsigned char[ourEntry->realSize];
fread((char*)outp, 1, ourEntry->realSize, FLOD);
LOD.seekg(ourEntry->offset, std::ios::beg);
LOD.read((char*)outp, ourEntry->realSize);
mutex->unlock();
return outp;
}
else //we will decompress file
{
outp = new unsigned char[ourEntry->size];
fread((char*)outp, 1, ourEntry->size, FLOD);
LOD.seekg(ourEntry->offset, std::ios::beg);
LOD.read((char*)outp, ourEntry->size);
mutex->unlock();
unsigned char * decomp = NULL;
int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
@ -89,6 +97,7 @@ unsigned char * CLodHandler::giveFile(std::string defName, int * length)
}
return NULL;
}
int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
{
int ret;
@ -238,13 +247,13 @@ void CLodHandler::extractFile(std::string FName, std::string name)
std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
if(buf1!=name)
continue;
fseek(FLOD, entries[i].offset, 0);
LOD.seekg(entries[i].offset, std::ios::beg);
std::string bufff = (FName);
unsigned char * outp;
if (entries[i].size==0) //file is not compressed
{
outp = new unsigned char[entries[i].realSize];
fread((char*)outp, 1, entries[i].realSize, FLOD);
LOD.read((char*)outp, entries[i].realSize);
std::ofstream out;
out.open(bufff.c_str(), std::ios::binary);
if(!out.is_open())
@ -263,8 +272,7 @@ void CLodHandler::extractFile(std::string FName, std::string name)
else //we will decompressing file
{
outp = new unsigned char[entries[i].size];
fread((char*)outp, 1, entries[i].size, FLOD);
fseek(FLOD, 0, 0);
LOD.read((char*)outp, entries[i].size);
std::ofstream destin;
destin.open(bufff.c_str(), std::ios::binary);
//int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
@ -299,47 +307,41 @@ void CLodHandler::init(std::string lodFile, std::string dirName)
{
myDir = dirName;
std::string Ts;
FLOD = fopen(lodFile.c_str(), "rb");
if(!FLOD)
{
Uint32 temp;
LOD.open(lodFile.c_str(), std::ios::in | std::ios::binary);
if (!LOD.is_open()) {
tlog1 << "Cannot open " << lodFile << std::endl;
return;
}
fseek(FLOD, 8, 0);
unsigned char temp[4];
fread((char*)temp, 1, 4, FLOD);
totalFiles = readNormalNr(temp,4);
fseek(FLOD, 0x5c, 0);
for (int i=0; i<totalFiles; i++)
LOD.seekg(8);
LOD.read((char *)&temp, 4);
totalFiles = SDL_SwapLE32(temp);
LOD.seekg(0x5c, std::ios::beg);
struct LodEntry *lodEntries = new struct LodEntry[totalFiles];
LOD.read((char *)lodEntries, sizeof(struct LodEntry) * totalFiles);
for (unsigned int i=0; i<totalFiles; i++)
{
Entry entry;
char bufc = -1;
bool valid = true;
entry.nameStr = "";
entry.nameStr = lodEntries[i].filename;
std::transform(entry.nameStr.begin(), entry.nameStr.end(),
entry.nameStr.begin(), toupper);
// Read name
for(int kk=0; kk<16; ++kk)
{
fread(&bufc, 1, 1, FLOD);
if (valid) {
if (bufc)
entry.nameStr+=toupper(bufc);
else
valid = false;
}
}
fread((char*)temp, 1, 4, FLOD);
entry.offset=readNormalNr(temp,4);
fread((char*)temp, 1, 4, FLOD);
entry.realSize=readNormalNr(temp,4);
fread((char*)temp, 1, 4, FLOD);
fread((char*)temp, 1, 4, FLOD);
entry.size=readNormalNr(temp,4);
entry.offset= SDL_SwapLE32(lodEntries[i].offset);
entry.realSize = SDL_SwapLE32(lodEntries[i].uncompressedSize);
entry.size = SDL_SwapLE32(lodEntries[i].size);
entries.push_back(entry);
}
delete [] lodEntries;
boost::filesystem::directory_iterator enddir;
if(boost::filesystem::exists(dirName))
{
@ -393,7 +395,6 @@ std::string CLodHandler::getTextFile(std::string name)
CLodHandler::CLodHandler()
{
mutex = new boost::mutex;
FLOD = NULL;
totalFiles = 0;
}

View File

@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include "../nodrze.h"
#include <SDL_stdinc.h>
/*
* CLodhandler.h, part of VCMI engine
@ -28,6 +29,14 @@ namespace NLoadHandlerHelp
const int fCHUNK = 50000;
}
struct LodEntry {
char filename[16];
Uint32 offset; /* little endian */
Uint32 uncompressedSize; /* little endian */
Uint32 unused; /* little endian */
Uint32 size; /* little endian */
};
struct Entry
{
// Info extracted from LOD file
@ -52,7 +61,7 @@ struct Entry
class DLL_EXPORT CLodHandler
{
public:
FILE* FLOD;
std::ifstream LOD;
nodrze<Entry> entries;
unsigned int totalFiles;
boost::mutex *mutex;

View File

@ -1,6 +1,7 @@
bin_PROGRAMS = vcmiserver
vcmiserver_LDADD = $(top_builddir)/lib/libvcmi.la
vcmiserver_CXXFLAGS = @SDL_CXXFLAGS@
vcmiserver_LDFLAGS = -L$(top_builddir)/lib
vcmiserver_SOURCES = \
CGameHandler.cpp \

View File

@ -54,12 +54,13 @@ CONFIG_CLEAN_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
PROGRAMS = $(bin_PROGRAMS)
am_vcmiserver_OBJECTS = CGameHandler.$(OBJEXT) CVCMIServer.$(OBJEXT) \
NetPacksServer.$(OBJEXT)
am_vcmiserver_OBJECTS = vcmiserver-CGameHandler.$(OBJEXT) \
vcmiserver-CVCMIServer.$(OBJEXT) \
vcmiserver-NetPacksServer.$(OBJEXT)
vcmiserver_OBJECTS = $(am_vcmiserver_OBJECTS)
vcmiserver_DEPENDENCIES = $(top_builddir)/lib/libvcmi.la
vcmiserver_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXXLD) $(vcmiserver_CXXFLAGS) \
$(CXXFLAGS) $(vcmiserver_LDFLAGS) $(LDFLAGS) -o $@
DEFAULT_INCLUDES = -I.@am__isrc@
depcomp = $(SHELL) $(top_srcdir)/depcomp
@ -214,6 +215,7 @@ top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
vcmiserver_LDADD = $(top_builddir)/lib/libvcmi.la
vcmiserver_CXXFLAGS = @SDL_CXXFLAGS@
vcmiserver_LDFLAGS = -L$(top_builddir)/lib
vcmiserver_SOURCES = \
CGameHandler.cpp \
@ -293,9 +295,9 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CGameHandler.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CVCMIServer.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NetPacksServer.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiserver-CGameHandler.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiserver-CVCMIServer.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiserver-NetPacksServer.Po@am__quote@
.cpp.o:
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@ -318,6 +320,48 @@ distclean-compile:
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $<
vcmiserver-CGameHandler.o: CGameHandler.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-CGameHandler.o -MD -MP -MF $(DEPDIR)/vcmiserver-CGameHandler.Tpo -c -o vcmiserver-CGameHandler.o `test -f 'CGameHandler.cpp' || echo '$(srcdir)/'`CGameHandler.cpp
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-CGameHandler.Tpo $(DEPDIR)/vcmiserver-CGameHandler.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='CGameHandler.cpp' object='vcmiserver-CGameHandler.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-CGameHandler.o `test -f 'CGameHandler.cpp' || echo '$(srcdir)/'`CGameHandler.cpp
vcmiserver-CGameHandler.obj: CGameHandler.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-CGameHandler.obj -MD -MP -MF $(DEPDIR)/vcmiserver-CGameHandler.Tpo -c -o vcmiserver-CGameHandler.obj `if test -f 'CGameHandler.cpp'; then $(CYGPATH_W) 'CGameHandler.cpp'; else $(CYGPATH_W) '$(srcdir)/CGameHandler.cpp'; fi`
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-CGameHandler.Tpo $(DEPDIR)/vcmiserver-CGameHandler.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='CGameHandler.cpp' object='vcmiserver-CGameHandler.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-CGameHandler.obj `if test -f 'CGameHandler.cpp'; then $(CYGPATH_W) 'CGameHandler.cpp'; else $(CYGPATH_W) '$(srcdir)/CGameHandler.cpp'; fi`
vcmiserver-CVCMIServer.o: CVCMIServer.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-CVCMIServer.o -MD -MP -MF $(DEPDIR)/vcmiserver-CVCMIServer.Tpo -c -o vcmiserver-CVCMIServer.o `test -f 'CVCMIServer.cpp' || echo '$(srcdir)/'`CVCMIServer.cpp
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-CVCMIServer.Tpo $(DEPDIR)/vcmiserver-CVCMIServer.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='CVCMIServer.cpp' object='vcmiserver-CVCMIServer.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-CVCMIServer.o `test -f 'CVCMIServer.cpp' || echo '$(srcdir)/'`CVCMIServer.cpp
vcmiserver-CVCMIServer.obj: CVCMIServer.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-CVCMIServer.obj -MD -MP -MF $(DEPDIR)/vcmiserver-CVCMIServer.Tpo -c -o vcmiserver-CVCMIServer.obj `if test -f 'CVCMIServer.cpp'; then $(CYGPATH_W) 'CVCMIServer.cpp'; else $(CYGPATH_W) '$(srcdir)/CVCMIServer.cpp'; fi`
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-CVCMIServer.Tpo $(DEPDIR)/vcmiserver-CVCMIServer.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='CVCMIServer.cpp' object='vcmiserver-CVCMIServer.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-CVCMIServer.obj `if test -f 'CVCMIServer.cpp'; then $(CYGPATH_W) 'CVCMIServer.cpp'; else $(CYGPATH_W) '$(srcdir)/CVCMIServer.cpp'; fi`
vcmiserver-NetPacksServer.o: NetPacksServer.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-NetPacksServer.o -MD -MP -MF $(DEPDIR)/vcmiserver-NetPacksServer.Tpo -c -o vcmiserver-NetPacksServer.o `test -f 'NetPacksServer.cpp' || echo '$(srcdir)/'`NetPacksServer.cpp
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-NetPacksServer.Tpo $(DEPDIR)/vcmiserver-NetPacksServer.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='NetPacksServer.cpp' object='vcmiserver-NetPacksServer.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-NetPacksServer.o `test -f 'NetPacksServer.cpp' || echo '$(srcdir)/'`NetPacksServer.cpp
vcmiserver-NetPacksServer.obj: NetPacksServer.cpp
@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -MT vcmiserver-NetPacksServer.obj -MD -MP -MF $(DEPDIR)/vcmiserver-NetPacksServer.Tpo -c -o vcmiserver-NetPacksServer.obj `if test -f 'NetPacksServer.cpp'; then $(CYGPATH_W) 'NetPacksServer.cpp'; else $(CYGPATH_W) '$(srcdir)/NetPacksServer.cpp'; fi`
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/vcmiserver-NetPacksServer.Tpo $(DEPDIR)/vcmiserver-NetPacksServer.Po
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='NetPacksServer.cpp' object='vcmiserver-NetPacksServer.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiserver_CXXFLAGS) $(CXXFLAGS) -c -o vcmiserver-NetPacksServer.obj `if test -f 'NetPacksServer.cpp'; then $(CYGPATH_W) 'NetPacksServer.cpp'; else $(CYGPATH_W) '$(srcdir)/NetPacksServer.cpp'; fi`
mostlyclean-libtool:
-rm -f *.lo