Crossfire Server, Trunk
TarLoader.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020-2021 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "TarLoader.h"
14 #include "string.h"
15 
16 TarLoader::TarLoader(AssetLoader *loader) : m_loader(loader) {
18 }
19 
22 }
23 
24 static int tar_read(mtar_t *tar, void *data, unsigned size) {
25  BufferReader *reader = static_cast<BufferReader *>(tar->stream);
26  if (tar->pos + size >= bufferreader_data_length(reader)) {
27  return MTAR_EREADFAIL;
28  }
29  memcpy(data, bufferreader_data(reader) + tar->pos, size);
30  return MTAR_ESUCCESS;
31 }
32 
33 static int tar_seek(mtar_t *tar, unsigned pos) {
34  tar->pos = pos;
35  return MTAR_ESUCCESS;
36 }
37 
38 static int tar_close(mtar_t *) {
39  return MTAR_ESUCCESS;
40 }
41 
42 
43 void TarLoader::load(BufferReader *reader, const std::string &filename) {
44  mtar_t tar;
45  mtar_header_t h;
46 
47  memset(&tar, 0, sizeof(tar));
48  tar.stream = reader;
49  tar.read = &tar_read;
50  tar.seek = &tar_seek;
51  tar.close = &tar_close;
52 
53  while ((mtar_read_header(&tar, &h)) == MTAR_ESUCCESS) {
54  if (m_loader->willLoad(h.name)) {
56  m_loader->load(m_reader, filename + ":/" + h.name);
57  }
58  mtar_next(&tar);
59  }
60 
61  mtar_close(&tar);
62 }
mtar_t::close
int(* close)(mtar_t *tar)
Definition: microtar.h:55
bufferreader_data
char * bufferreader_data(BufferReader *br)
Definition: bufferreader.cpp:148
TarLoader::TarLoader
TarLoader(AssetLoader *loader)
Definition: TarLoader.cpp:16
AssetLoader::load
virtual void load(BufferReader *reader, const std::string &filename)=0
AssetLoader::willLoad
virtual bool willLoad(const std::string &filename)=0
mtar_t
Definition: microtar.h:51
mtar_next
int mtar_next(mtar_t *tar)
Definition: microtar.cpp:230
TarLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: TarLoader.cpp:43
bufferreader_destroy
void bufferreader_destroy(BufferReader *br)
Definition: bufferreader.cpp:40
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Definition: bufferreader.cpp:144
TarLoader::m_reader
BufferReader * m_reader
Definition: TarLoader.h:20
AssetLoader
Definition: AssetLoader.h:24
npc_dialog.filename
filename
Definition: npc_dialog.py:99
tar_read
static int tar_read(mtar_t *tar, void *data, unsigned size)
Definition: TarLoader.cpp:24
mtar_header_t::name
char name[100]
Definition: microtar.h:44
mtar_close
int mtar_close(mtar_t *tar)
Definition: microtar.cpp:211
mtar_t::pos
unsigned pos
Definition: microtar.h:57
TarLoader::m_loader
AssetLoader * m_loader
Definition: TarLoader.h:21
navar-midane_time.data
data
Definition: navar-midane_time.py:11
mtar_t::read
int(* read)(mtar_t *tar, void *data, unsigned size)
Definition: microtar.h:52
mtar_read_header
int mtar_read_header(mtar_t *tar, mtar_header_t *h)
Definition: microtar.cpp:270
mtar_header_t
Definition: microtar.h:38
mtar_t::stream
void * stream
Definition: microtar.h:56
TarLoader::~TarLoader
virtual ~TarLoader()
Definition: TarLoader.cpp:20
tar_seek
static int tar_seek(mtar_t *tar, unsigned pos)
Definition: TarLoader.cpp:33
MTAR_ESUCCESS
@ MTAR_ESUCCESS
Definition: microtar.h:17
tar_close
static int tar_close(mtar_t *)
Definition: TarLoader.cpp:38
bufferreader_init_from_tar_file
void bufferreader_init_from_tar_file(BufferReader *br, mtar_t *tar, mtar_header_t *h)
Definition: bufferreader.cpp:88
bufferreader_create
BufferReader * bufferreader_create()
Definition: bufferreader.cpp:31
TarLoader.h
MTAR_EREADFAIL
@ MTAR_EREADFAIL
Definition: microtar.h:20
BufferReader
Definition: bufferreader.cpp:21
mtar_t::seek
int(* seek)(mtar_t *tar, unsigned pos)
Definition: microtar.h:54