Skip to content

Commit c1cad5d

Browse files
author
KadVenku
committed
Added base files.
0 parents  commit c1cad5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+21069
-0
lines changed

Assets/Assets.cpp

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Asset Loader subsystem
3+
4+
Loads various types of assets by name. The core function is LoadFile which
5+
loads any file in the virtual FS. It first looks through the physical search
6+
paths for the file, before searching the file index constructed from the
7+
MegaFiles listed in MegaFiles.xml. The latter is read during Initialization().
8+
9+
The Asset manager also keeps track of changed files, and notifies the resource
10+
that was loaded from it when this has happened so it can reload itself.
11+
*/
12+
#include "Assets/Assets.h"
13+
#include "Assets/FileIndex.h"
14+
#include <algorithm>
15+
#include <map>
16+
#include <list>
17+
using namespace std;
18+
19+
namespace Assets
20+
{
21+
// Base paths for various asset types.
22+
// Note: They must end with a path delimiter!
23+
static const char* BASE_PATH_CINEMATICS = "Data\\Art\\Cinematics\\";
24+
static const char* BASE_PATH_MAPS = "Data\\Art\\Maps\\";
25+
static const char* BASE_PATH_ANIMATIONS = "Data\\Art\\Models\\";
26+
static const char* BASE_PATH_MODELS = "Data\\Art\\Models\\";
27+
static const char* BASE_PATH_PARTICLES = "Data\\Art\\Models\\";
28+
static const char* BASE_PATH_TEXTURES = "Data\\Art\\Textures\\";
29+
static const char* BASE_PATH_SHADERS = "Data\\Art\\Shaders\\";
30+
static const char* BASE_PATH_SFX = "Data\\Audio\\SFX\\";
31+
static const char* BASE_PATH_MUSIC = "Data\\Audio\\Music\\";
32+
static const char* BASE_PATH_SPEECH = "Data\\Audio\\Speech\\English\\";
33+
static const char* BASE_PATH_SCRIPTS = "Data\\Scripts\\";
34+
static const char* BASE_PATH_XML = "Data\\XML\\";
35+
36+
ptr<File> LoadXML(const std::string& filename)
37+
{
38+
static const char* const extensions[] = {"xml", NULL};
39+
return LoadFile(BASE_PATH_XML + filename, extensions);
40+
}
41+
42+
ptr<File> LoadTexture(const std::string& filename)
43+
{
44+
static const char* const extensions[] = {"tga", "dds", NULL};
45+
return LoadFile(BASE_PATH_TEXTURES + filename, extensions);
46+
}
47+
48+
ptr<File> LoadAnimation(const std::string& filename)
49+
{
50+
static const char* const extensions[] = {"ala", NULL};
51+
return LoadFile(BASE_PATH_ANIMATIONS + filename, extensions);
52+
}
53+
54+
ptr<File> LoadModelParticle(const std::string& filename)
55+
{
56+
static const char* const extensions[] = {"alo", NULL};
57+
return LoadFile(BASE_PATH_MODELS + filename, extensions);
58+
}
59+
60+
ptr<File> LoadShader(const std::string& filename)
61+
{
62+
static const char* const extensions[] = {"fx", "fxo"};
63+
return LoadFile(BASE_PATH_SHADERS + filename, extensions);
64+
}
65+
66+
ptr<File> LoadMap(const std::string& filename)
67+
{
68+
static const char* const extensions[] = {"ted", NULL};
69+
return LoadFile(BASE_PATH_MAPS + filename, extensions);
70+
}
71+
72+
ptr<File> LoadSFX(const std::string& filename)
73+
{
74+
static const char* const extensions[] = {"wav", NULL};
75+
return LoadFile(BASE_PATH_SFX + filename, extensions);
76+
}
77+
78+
ptr<File> LoadMusic(const std::string& filename)
79+
{
80+
static const char* const extensions[] = {"mp3", NULL};
81+
return LoadFile(BASE_PATH_MUSIC + filename, extensions);
82+
}
83+
84+
ptr<File> LoadSpeech(const std::string& filename)
85+
{
86+
ptr<File> f;
87+
static const char* const extensions[] = {"mp3", NULL};
88+
if ((f = LoadFile(BASE_PATH_SPEECH + filename, extensions)) == NULL)
89+
{
90+
static const char* const extensions[] = {"wav", NULL};
91+
f = LoadFile(BASE_PATH_SFX + filename, extensions);
92+
}
93+
return f;
94+
}
95+
96+
ptr<File> LoadScript(const std::string& filename)
97+
{
98+
static const char* const extensions[] = {"lua", NULL};
99+
return LoadFile(BASE_PATH_SCRIPTS + filename, extensions);
100+
}
101+
102+
ptr<File> LoadCinematic(const std::string& filename)
103+
{
104+
static const char* const extensions[] = {"tec", NULL};
105+
return LoadFile(BASE_PATH_CINEMATICS + filename, extensions);
106+
}
107+
108+
}

Assets/Assets.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef ASSETS_ASSETS_H
2+
#define ASSETS_ASSETS_H
3+
4+
#include <string>
5+
#include <vector>
6+
7+
#include "Assets/Files.h"
8+
#include "Assets/XML.h"
9+
#include "Assets/MTD.h"
10+
#include "Assets/Models.h"
11+
#include "Assets/Maps.h"
12+
#include "Assets/ParticleSystem.h"
13+
#include "Assets/StringList.h"
14+
15+
namespace Assets
16+
{
17+
// Initialize Assets subsystem with this list of search paths.
18+
void Initialize(const std::wstring& mod_path, const std::wstring& main_path, const std::wstring& old_path);
19+
void Uninitialize();
20+
21+
//
22+
// Enumerate assets.
23+
//
24+
// Use the filter to enumerate assets in the search paths and MegaFile index.
25+
// The filter accepts ? and * as wildcards. e.g.: "Data\\Art\\Models\\EV_*.ALO"
26+
//
27+
// Returns NULL when no files have been found. Otherwise, GetFileName() and
28+
// GetFileSize() can be used until Next() returns false.
29+
//
30+
class IEnumerator : public Object
31+
{
32+
public:
33+
virtual bool Next() = 0;
34+
virtual const std::string& GetFileName() const = 0;
35+
virtual size_t GetFileSize() const = 0;
36+
};
37+
38+
ptr<IEnumerator> Enumerate(const std::string& filter);
39+
40+
//
41+
// The following functions load various asset types.
42+
// If they fail for I/O reasons (e.g., file not found, read error, bad file),
43+
// they return NULL. Other exceptions are passed through.
44+
//
45+
ptr<File> LoadFile(const std::string& filename);
46+
ptr<File> LoadTexture(const std::string& filename);
47+
ptr<File> LoadAnimation(const std::string& filename);
48+
ptr<File> LoadModelParticle(const std::string& filename);
49+
ptr<File> LoadMap(const std::string& filename);
50+
ptr<File> LoadShader(const std::string& filename);
51+
ptr<File> LoadXML(const std::string& filename);
52+
ptr<File> LoadSFX(const std::string& filename);
53+
ptr<File> LoadMusic(const std::string& filename);
54+
ptr<File> LoadSpeech(const std::string& filename);
55+
ptr<File> LoadScript(const std::string& filename);
56+
ptr<File> LoadCinematic(const std::string& filename);
57+
}
58+
59+
#endif

Assets/ChunkFile.cpp

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include "Assets/ChunkFile.h"
2+
#include "General/Exceptions.h"
3+
#include "General/ExactTypes.h"
4+
#include <cassert>
5+
#include <vector>
6+
using namespace std;
7+
8+
namespace Assets {
9+
10+
#pragma pack(1)
11+
struct CHUNKHDR
12+
{
13+
uint32_t type;
14+
uint32_t size;
15+
};
16+
17+
struct MINICHUNKHDR
18+
{
19+
uint8_t type;
20+
uint8_t size;
21+
};
22+
#pragma pack()
23+
24+
ChunkType ChunkReader::nextMini()
25+
{
26+
assert(m_curDepth >= 0);
27+
assert(m_size >= 0);
28+
29+
if (m_miniSize >= 0)
30+
{
31+
// We're in a mini chunk, so skip it
32+
skip();
33+
}
34+
35+
if (m_file.GetPosition() == m_offsets[m_curDepth])
36+
{
37+
// We're at the end of the current chunk, move up one
38+
m_curDepth--;
39+
m_size = -1;
40+
m_position = 0;
41+
return -1;
42+
}
43+
44+
MINICHUNKHDR hdr;
45+
if (m_file.Read((void*)&hdr, sizeof(MINICHUNKHDR)) != sizeof(MINICHUNKHDR))
46+
{
47+
throw ReadException();
48+
}
49+
50+
m_miniSize = letohl(hdr.size);
51+
m_miniOffset = m_file.GetPosition() + m_miniSize;
52+
m_position = 0;
53+
54+
return letohl(hdr.type);
55+
}
56+
57+
ChunkType ChunkReader::next()
58+
{
59+
assert(m_curDepth >= 0);
60+
61+
if (m_size >= 0)
62+
{
63+
// We're in a data chunk, so skip it
64+
m_file.SetPosition(m_offsets[m_curDepth--]);
65+
}
66+
67+
if (m_file.GetPosition() == m_offsets[m_curDepth])
68+
{
69+
// We're at the end of the current chunk, move up one
70+
m_curDepth--;
71+
m_size = -1;
72+
m_position = 0;
73+
return -1;
74+
}
75+
76+
CHUNKHDR hdr;
77+
if (m_file.Read((void*)&hdr, sizeof(CHUNKHDR)) != sizeof(CHUNKHDR))
78+
{
79+
throw ReadException();
80+
}
81+
82+
unsigned long size = letohl(hdr.size);
83+
m_offsets[ ++m_curDepth ] = m_file.GetPosition() + (size & 0x7FFFFFFF);
84+
m_size = (~size & 0x80000000) ? size : -1;
85+
m_miniSize = -1;
86+
m_position = 0;
87+
88+
return letohl(hdr.type);
89+
}
90+
91+
void ChunkReader::skip()
92+
{
93+
if (m_miniSize >= 0)
94+
{
95+
m_file.SetPosition(m_miniOffset);
96+
}
97+
else
98+
{
99+
m_file.SetPosition(m_offsets[m_curDepth--]);
100+
m_size = -1;
101+
m_position = 0;
102+
}
103+
}
104+
105+
size_t ChunkReader::size()
106+
{
107+
return (m_miniSize >= 0) ? m_miniSize : m_size;
108+
}
109+
110+
string ChunkReader::readString()
111+
{
112+
vector<char> data(size() / sizeof(char));
113+
read(&data[0], size());
114+
return &data[0];
115+
}
116+
117+
wstring ChunkReader::readWideString()
118+
{
119+
vector<wchar_t> data(size() / sizeof(wchar_t));
120+
read(&data[0], size());
121+
return &data[0];
122+
}
123+
124+
unsigned char ChunkReader::readByte()
125+
{
126+
uint8_t value;
127+
read(&value, sizeof(value));
128+
return value;
129+
}
130+
131+
unsigned short ChunkReader::readShort()
132+
{
133+
uint16_t value;
134+
read(&value, sizeof(value));
135+
return letohs(value);
136+
}
137+
138+
unsigned long ChunkReader::readInteger()
139+
{
140+
uint32_t value;
141+
read(&value, sizeof(value));
142+
return letohl(value);
143+
}
144+
145+
size_t ChunkReader::read(void* buffer, size_t size, bool check)
146+
{
147+
if (m_size >= 0)
148+
{
149+
size_t s = m_file.Read(buffer, min(m_position + (long)size, (long)this->size()) - m_position);
150+
m_position += (long)s;
151+
if (check && s != size)
152+
{
153+
throw ReadException();
154+
}
155+
return size;
156+
}
157+
throw ReadException();
158+
}
159+
160+
ChunkReader::ChunkReader(File& file)
161+
: m_file(file)
162+
{
163+
m_offsets[0] = (unsigned long)m_file.GetSize();
164+
m_curDepth = 0;
165+
m_size = -1;
166+
m_miniSize = -1;
167+
m_file.AddRef();
168+
m_file.SetPosition(0);
169+
}
170+
171+
ChunkReader::~ChunkReader()
172+
{
173+
m_file.Release();
174+
}
175+
176+
}

0 commit comments

Comments
 (0)