File: resourcetype_module.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (150 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
	$Id: resourcetype_module.cpp,v 1.6 2001/12/16 19:18:09 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------

	File purpose:
		Module resource type

*/

#include "Core/precomp.h"
#include "resourcetype_module.h"
#include "module_reader.h"
#include <API/Core/System/cl_assert.h>
#include <API/Core/Resources/resource.h>

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceData_Module construction:

CL_ResourceData_Module::CL_ResourceData_Module(CL_Resource &resource)
	: CL_ResourceData(resource)
{
	module=NULL;

	slot_load_file = resource.sig_load_file().connect(
		this, &CL_ResourceData_Module::on_load_file);

	slot_load_datafile = resource.sig_load_datafile().connect(
		this, &CL_ResourceData_Module::on_load_datafile);

	slot_save_datafile = resource.sig_save_datafile().connect(
		this, &CL_ResourceData_Module::on_save_datafile);

	slot_unload = resource.sig_unload().connect(
		this, &CL_ResourceData_Module::on_unload);

	resource.attach_data("module", this);
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceData_Module implementation:

void CL_ResourceData_Module::on_load_file()
{
	CL_ResourceManager manager = get_resource().get_manager();
	std::string location = get_resource().get_location();

	CL_InputSource *input = manager.get_resource_provider()->open_source(location.c_str());

	try
	{
		MREADER *reader=new_clanlib_reader((void *) input);
		cl_assert ( reader != NULL );

		module = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);

		if (module == NULL)
		{
			throw CL_Error(MikMod_strerror(MikMod_errno));
		}
		delete_clanlib_reader(reader);
	}
	catch (...)
	{
		delete input;
		throw;
	}

	delete input;
}

void CL_ResourceData_Module::on_load_datafile(CL_InputSourceProvider *provider)
{
	CL_InputSource *input = provider->open_source(get_resource().get_name().c_str());

	try
	{
		MREADER *reader=new_clanlib_reader((void *) input);
		cl_assert ( reader != NULL );

		module = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);

		if (module == NULL)
		{
			throw CL_Error(MikMod_strerror(MikMod_errno));
		}
		delete_clanlib_reader(reader);
	}
	catch (...)
	{
		delete input;
		throw;
	}

	delete input;
}

void CL_ResourceData_Module::on_save_datafile(CL_OutputSourceProvider *provider)
{
	CL_ResourceManager manager = get_resource().get_manager();
	std::string location = get_resource().get_location();

	CL_InputSource *input = manager.get_resource_provider()->open_source(location.c_str());
	CL_OutputSource *output = provider->open_source(get_resource().get_name().c_str());

	try
	{
		/*
		 * We do not use the module stored in memory,
		 * instead we re-read the file from the disk
		 * and copy it "as is" in the datafile.
		 * We *need* to do this since libMikMod does not
		 * know how to save a file... Therefore we store
		 * the genuine .mod or .xm file in the .dat file,
		 * and will call the MikMod reader again when
		 * we read the module from the datafile.
		 */
		int length = input->size();
		unsigned char *buffer = new unsigned char[length];
		cl_assert (buffer != NULL);
		input->read(buffer, length);
		output->write(buffer, length);
	}
	catch (...)
	{
		delete output;
		delete input;
		throw;
	}

	delete output;
	delete input;
}

void CL_ResourceData_Module::on_unload()
{
	if (module)
	{
		Player_Free(module);
		module=NULL;
	}
}