mirror of https://github.com/OpenTTD/OpenTTD
Compare commits
2 Commits
ee4675cd9c
...
883d48112d
Author | SHA1 | Date |
---|---|---|
Peter Nelson | 883d48112d | |
Peter Nelson | 95cd14dec9 |
|
@ -313,8 +313,8 @@ static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
|
|||
static const uint NUM_SONGS_PLAYLIST = 32;
|
||||
|
||||
/* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
|
||||
char *GetMusicCatEntryName(const std::string &filename, size_t entrynum);
|
||||
uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen);
|
||||
std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum);
|
||||
std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum);
|
||||
|
||||
enum MusicTrackType {
|
||||
MTT_STANDARDMIDI, ///< Standard MIDI file
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
/** @file music.cpp The songs that OpenTTD knows. */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "string_func.h"
|
||||
|
||||
|
||||
/** The type of set we're replacing */
|
||||
|
@ -22,55 +23,50 @@
|
|||
* Read the name of a music CAT file entry.
|
||||
* @param filename Name of CAT file to read from
|
||||
* @param entrynum Index of entry whose name to read
|
||||
* @return Pointer to string, caller is responsible for freeing memory,
|
||||
* nullptr if entrynum does not exist.
|
||||
* @return Name of CAT file entry if it could be read.
|
||||
*/
|
||||
char *GetMusicCatEntryName(const std::string &filename, size_t entrynum)
|
||||
std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum)
|
||||
{
|
||||
if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr;
|
||||
if (!FioCheckFileExists(filename, BASESET_DIR)) return std::nullopt;
|
||||
|
||||
RandomAccessFile file(filename, BASESET_DIR);
|
||||
uint32_t ofs = file.ReadDword();
|
||||
size_t entry_count = ofs / 8;
|
||||
if (entrynum < entry_count) {
|
||||
file.SeekTo(entrynum * 8, SEEK_SET);
|
||||
file.SeekTo(file.ReadDword(), SEEK_SET);
|
||||
uint8_t namelen = file.ReadByte();
|
||||
char *name = MallocT<char>(namelen + 1);
|
||||
file.ReadBlock(name, namelen);
|
||||
name[namelen] = '\0';
|
||||
return name;
|
||||
}
|
||||
return nullptr;
|
||||
if (entrynum >= entry_count) return std::nullopt;
|
||||
|
||||
file.SeekTo(entrynum * 8, SEEK_SET);
|
||||
file.SeekTo(file.ReadDword(), SEEK_SET);
|
||||
uint8_t namelen = file.ReadByte();
|
||||
|
||||
std::string name(namelen, '\0');
|
||||
file.ReadBlock(name.data(), namelen);
|
||||
return StrMakeValid(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the full data of a music CAT file entry.
|
||||
* @param filename Name of CAT file to read from.
|
||||
* @param entrynum Index of entry to read
|
||||
* @param[out] entrylen Receives length of data read
|
||||
* @return Pointer to buffer with data read, caller is responsible for freeind memory,
|
||||
* nullptr if entrynum does not exist.
|
||||
* @return Data of CAT file entry.
|
||||
*/
|
||||
uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen)
|
||||
std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum)
|
||||
{
|
||||
entrylen = 0;
|
||||
if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr;
|
||||
if (!FioCheckFileExists(filename, BASESET_DIR)) return std::nullopt;
|
||||
|
||||
RandomAccessFile file(filename, BASESET_DIR);
|
||||
uint32_t ofs = file.ReadDword();
|
||||
size_t entry_count = ofs / 8;
|
||||
if (entrynum < entry_count) {
|
||||
file.SeekTo(entrynum * 8, SEEK_SET);
|
||||
size_t entrypos = file.ReadDword();
|
||||
entrylen = file.ReadDword();
|
||||
file.SeekTo(entrypos, SEEK_SET);
|
||||
file.SkipBytes(file.ReadByte());
|
||||
uint8_t *data = MallocT<uint8_t>(entrylen);
|
||||
file.ReadBlock(data, entrylen);
|
||||
return data;
|
||||
}
|
||||
return nullptr;
|
||||
if (entrynum >= entry_count) return std::nullopt;
|
||||
|
||||
file.SeekTo(entrynum * 8, SEEK_SET);
|
||||
size_t entrypos = file.ReadDword();
|
||||
size_t entrylen = file.ReadDword();
|
||||
file.SeekTo(entrypos, SEEK_SET);
|
||||
file.SkipBytes(file.ReadByte());
|
||||
|
||||
std::vector<uint8_t> data(entrylen);
|
||||
file.ReadBlock(data.data(), entrylen);
|
||||
return data;
|
||||
}
|
||||
|
||||
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet)
|
||||
|
@ -138,13 +134,12 @@ bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const
|
|||
/* Song has a CAT file index, assume it's MPS MIDI format */
|
||||
this->songinfo[i].filetype = MTT_MPSMIDI;
|
||||
this->songinfo[i].cat_index = atoi(item->value->c_str());
|
||||
char *songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index);
|
||||
if (songname == nullptr) {
|
||||
auto songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index);
|
||||
if (!songname.has_value()) {
|
||||
Debug(grf, 0, "Base music set song missing from CAT file: {}/{}", filename, this->songinfo[i].cat_index);
|
||||
continue;
|
||||
}
|
||||
this->songinfo[i].songname = songname;
|
||||
free(songname);
|
||||
this->songinfo[i].songname = *songname;
|
||||
} else {
|
||||
this->songinfo[i].filetype = MTT_STANDARDMIDI;
|
||||
}
|
||||
|
|
|
@ -843,11 +843,9 @@ bool MidiFile::LoadSong(const MusicSongInfo &song)
|
|||
return this->LoadFile(song.filename);
|
||||
case MTT_MPSMIDI:
|
||||
{
|
||||
size_t songdatalen = 0;
|
||||
uint8_t *songdata = GetMusicCatEntryData(song.filename, song.cat_index, songdatalen);
|
||||
if (songdata != nullptr) {
|
||||
bool result = this->LoadMpsData(songdata, songdatalen);
|
||||
free(songdata);
|
||||
auto songdata = GetMusicCatEntryData(song.filename, song.cat_index);
|
||||
if (songdata.has_value()) {
|
||||
bool result = this->LoadMpsData(songdata->data(), songdata->size());
|
||||
return result;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -1078,17 +1076,13 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
|||
return output_filename;
|
||||
}
|
||||
|
||||
uint8_t *data;
|
||||
size_t datalen;
|
||||
data = GetMusicCatEntryData(song.filename, song.cat_index, datalen);
|
||||
if (data == nullptr) return std::string();
|
||||
auto songdata = GetMusicCatEntryData(song.filename, song.cat_index);
|
||||
if (!songdata.has_value()) return std::string();
|
||||
|
||||
MidiFile midifile;
|
||||
if (!midifile.LoadMpsData(data, datalen)) {
|
||||
free(data);
|
||||
if (!midifile.LoadMpsData(songdata->data(), songdata->size())) {
|
||||
return std::string();
|
||||
}
|
||||
free(data);
|
||||
|
||||
if (midifile.WriteSMF(output_filename)) {
|
||||
return output_filename;
|
||||
|
|
Loading…
Reference in New Issue