Planeshift
data.h
Go to the documentation of this file.
1 /*
2  * data.h
3  *
4  * Copyright (C) 2001-2010 Atomic Blue (info@planeshift.it, http://www.planeshift.it)
5  *
6  * Credits : Saul Leite <leite@engineer.com>
7  * Mathias 'AgY' Voeroes <agy@operswithoutlife.net>
8  * and all past and present planeshift coders
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation (version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 
24 #ifndef _SOUND_DATA_H_
25 #define _SOUND_DATA_H_
26 
27 /*
28  * i still have to make this doxygen compatible .. please dont mock me
29  * but i need to get this story out of my head and into this file
30  *
31  * How SoundData works: (Birth till Death)
32  *
33  * Birth:
34  *
35  * On Initialization it aquires references to vfs and a loader.
36  * It returns false if it fails and no further checking is done.
37  *
38  * On succes the very first thing that may happen is that LoadSoundLib is called.
39  * It fills libsoundfiles with the data it found in soundlib.xml
40  * Even if not called we can still process requests for filenames within our vfs.
41  *
42  * We assume LoadSoundLib has been called and the library is filled.
43  *
44  * Now Someone calls LoadSoundFile to load a named resource or file
45  * the one that calls expects a pointer to the SoundFile he requested
46  * depending on how successfull this is LoadSoundFile will fill it in and return
47  * true or false.
48  *
49  * GetSound is called and GetSound will search if theres a cached
50  * SoundFile or if theres a unloaded resource in our soundlib.
51  * if its cached then it will return that one. Thats best case
52  * because LoadSoundFile can return a pointer to the cached data.
53  * If its in our soundlib, then GetSound will make a copy and put that into our cache
54  * (using PutSound) on succes it will return a Pointer to a valid SoundFile
55  * on failure it will return NULL.
56  *
57  * GetSound may return NULL. In that case LoadSoundfile creates a new SoundFile
58  * (object) with the given name as filename to check if we got a (dynamic)
59  * file as name.
60  * That happens if we are playing voicefiles. Those are downloaded from the server
61  * and thus do not exist in our libraray. PutSound is used to add it to the cache.
62  *
63  * However theres always a valid SoundFile (object) for our loader
64  * Now LoadSoundFile tries to load that file (finally).
65  * There are two cases:
66  * 1) file exists and is loadable (succes .. return true)
67  * 2) file doesnt exist or ISNT loadable (failure .. return false)
68  *
69  * case 1) means that loaded (a bool) is set to true and snddata valid
70  * case 2) loaded remains false and snddata is invalid / NULL
71  *
72  * LoadSoundFile has returned and the caller
73  * might now do whatever he wanted todo with that snddata.
74  *
75  * Death:
76  *
77  * SoundData has a Update method that checks if there are expired SoundFiles
78  * theres a hardcoded caching time of 300 seconds. After 300 seconds it will
79  * check if there are still references on the snddata our SoundFile provides.
80  * If theres only one then its the SoundFile object itself.
81  * That means we go ahead and delete that object using DeleteSound.
82  *
83  * Now sometimes it isnt that easy ;) Maybe thats a looping background sound
84  * in that case our RefCount is at last higher then one.
85  * We set lasttouch to current ticks .. and check again .. in 300 seconds ;)
86  *
87  * Anyway UnloadSound is a public method and thus someone may call it for any
88  * reason. Be aware!
89  * It will crash your program if you unload data which is still in use ;)
90  */
91 
92 // FIXME i should be an option ;)
93 #define SOUNDFILE_CACHETIME 300000
94 
95 #include <cssysdef.h>
96 #include <iutil/objreg.h>
97 #include <csutil/csstring.h>
98 #include <csutil/hash.h>
99 #include <iutil/vfs.h>
100 #include <isndsys/ss_data.h>
101 #include <isndsys/ss_loader.h>
102 
110 {
111  public:
112  csString name;
113  csString filename;
114  csRef<iSndSysData> snddata;
115  bool loaded;
116  csTicks lasttouch;
117 
123  SoundFile(const char *newname, const char *newfilename);
128  SoundFile (SoundFile* const &copythat);
132  ~SoundFile();
133 };
134 
142 {
143  public:
144 
150  SoundData ();
155  ~SoundData ();
156 
162  bool Initialize(iObjectRegistry* objectReg);
170  bool LoadSoundLib (const char* filename, iObjectRegistry* objectReg);
175  void UnloadSoundLib ();
176 
183  bool LoadSoundFile (const char *name, csRef<iSndSysData> &snddata);
190  void UnloadSoundFile (const char *name);
197  void Update ();
198 
199  private:
200  csRef<iSndSysLoader> sndloader;
201  csHash<SoundFile *> soundfiles;
202  csHash<SoundFile *> libsoundfiles;
203  csRef<iVFS> vfs;
204 
209  SoundFile *GetSound (const char *name);
213  void PutSound (SoundFile* &sound);
217  void DeleteSound (SoundFile* &sound);
218 };
219 
220 #endif /*_SOUND_DATA_H_*/
~SoundFile()
Destructor.
SoundData is the datakeeper of.
Definition: data.h:141
Class that contains the most important informations about a soundfile It contains the name...
Definition: data.h:109
csString filename
filename in our vfs (maybe not unique)
Definition: data.h:113
csRef< iSndSysData > snddata
data in suitable format
Definition: data.h:114
csString name
name of this file/resource MUST be unique
Definition: data.h:112
csTicks lasttouch
last time when this SoundFile was used/touched
Definition: data.h:116
bool loaded
true if snddata is loaded, false if not
Definition: data.h:115
void void Initialize(iObjectRegistry *object_reg)
SoundFile(const char *newname, const char *newfilename)
Constructs a SoundFile.