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 extern "C" {
15 #include "string.h"
16 }
17 
18 TarLoader::TarLoader(AssetLoader *loader) : m_loader(loader) {
20 }
21 
24 }
25 
26 static int tar_read(mtar_t *tar, void *data, unsigned size) {
27  BufferReader *reader = static_cast<BufferReader *>(tar->stream);
28  if (tar->pos + size >= bufferreader_data_length(reader)) {
29  return MTAR_EREADFAIL;
30  }
31  memcpy(data, bufferreader_data(reader) + tar->pos, size);
32  return MTAR_ESUCCESS;
33 }
34 
35 static int tar_seek(mtar_t *tar, unsigned pos) {
36  tar->pos = pos;
37  return MTAR_ESUCCESS;
38 }
39 
40 static int tar_close(mtar_t *) {
41  return MTAR_ESUCCESS;
42 }
43 
44 
45 void TarLoader::load(BufferReader *reader, const std::string &filename) {
46  mtar_t tar;
47  mtar_header_t h;
48 
49  memset(&tar, 0, sizeof(tar));
50  tar.stream = reader;
51  tar.read = &tar_read;
52  tar.seek = &tar_seek;
53  tar.close = &tar_close;
54 
55  while ((mtar_read_header(&tar, &h)) == MTAR_ESUCCESS) {
56  if (m_loader->willLoad(h.name)) {
58  m_loader->load(m_reader, filename + ":/" + h.name);
59  }
60  mtar_next(&tar);
61  }
62 
63  mtar_close(&tar);
64 }
mtar_t::close
int(* close)(mtar_t *tar)
Definition: microtar.h:60
TarLoader::TarLoader
TarLoader(AssetLoader *loader)
Definition: TarLoader.cpp:18
bufferreader_init_from_tar_file
void bufferreader_init_from_tar_file(BufferReader *br, mtar_t *tar, mtar_header_t *h)
Definition: bufferreader.c:88
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:56
TarLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: TarLoader.cpp:45
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:26
mtar_header_t::name
char name[100]
Definition: microtar.h:49
mtar_t::pos
unsigned pos
Definition: microtar.h:62
TarLoader::m_loader
AssetLoader * m_loader
Definition: TarLoader.h:21
bufferreader_create
BufferReader * bufferreader_create()
Definition: bufferreader.c:31
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Definition: bufferreader.c:144
navar-midane_time.data
data
Definition: navar-midane_time.py:11
mtar_read_header
int mtar_read_header(mtar_t *tar, mtar_header_t *h)
Definition: microtar.c:270
mtar_t::read
int(* read)(mtar_t *tar, void *data, unsigned size)
Definition: microtar.h:57
mtar_header_t
Definition: microtar.h:43
mtar_t::stream
void * stream
Definition: microtar.h:61
mtar_next
int mtar_next(mtar_t *tar)
Definition: microtar.c:230
TarLoader::~TarLoader
virtual ~TarLoader()
Definition: TarLoader.cpp:22
mtar_close
int mtar_close(mtar_t *tar)
Definition: microtar.c:211
tar_seek
static int tar_seek(mtar_t *tar, unsigned pos)
Definition: TarLoader.cpp:35
MTAR_ESUCCESS
@ MTAR_ESUCCESS
Definition: microtar.h:22
bufferreader_destroy
void bufferreader_destroy(BufferReader *br)
Definition: bufferreader.c:40
tar_close
static int tar_close(mtar_t *)
Definition: TarLoader.cpp:40
bufferreader_data
char * bufferreader_data(BufferReader *br)
Definition: bufferreader.c:148
TarLoader.h
MTAR_EREADFAIL
@ MTAR_EREADFAIL
Definition: microtar.h:25
BufferReader
Definition: bufferreader.c:21
mtar_t::seek
int(* seek)(mtar_t *tar, unsigned pos)
Definition: microtar.h:59