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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* tuningCollection.cpp
* --------------------
* Purpose: Alternative sample tuning collection class.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "tuningcollection.h"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
#include "../common/serialization_utils.h"
#include <algorithm>
#include "Loaders.h"
#ifdef MODPLUG_TRACKER
#include "../common/mptFileIO.h"
#include "mpt/fs/fs.hpp"
#include "mpt/io_file/outputfile.hpp"
#include "../mptrack/TrackerSettings.h"
#endif //MODPLUG_TRACKER
OPENMPT_NAMESPACE_BEGIN
namespace Tuning {
/*
Version history:
2->3: Serialization revamp(August 2007)
1->2: Sizetypes of string serialisation from size_t(uint32)
to uint8. (March 2007)
*/
namespace CTuningS11n
{
void ReadStr(std::istream &iStrm, mpt::ustring &ustr, const std::size_t dummy, mpt::Charset charset);
void WriteStr(std::ostream &oStrm, const mpt::ustring &ustr);
} // namespace CTuningS11n
using namespace CTuningS11n;
static void ReadTuning(std::istream &iStrm, CTuningCollection &Tc, const std::size_t dummy, mpt::Charset defaultCharset)
{
MPT_UNREFERENCED_PARAMETER(dummy);
Tc.AddTuning(iStrm, defaultCharset);
}
static void WriteTuning(std::ostream& oStrm, const CTuning& t)
{
t.Serialize(oStrm);
}
CTuning* CTuningCollection::GetTuning(const mpt::ustring &name)
{
for(std::size_t i = 0; i<m_Tunings.size(); i++)
{
if(m_Tunings[i]->GetName() == name)
{
return m_Tunings[i].get();
}
}
return nullptr;
}
const CTuning* CTuningCollection::GetTuning(const mpt::ustring &name) const
{
for(std::size_t i = 0; i<m_Tunings.size(); i++)
{
if(m_Tunings[i]->GetName() == name)
{
return m_Tunings[i].get();
}
}
return nullptr;
}
const CTuning* CTuningCollection::FindIdenticalTuning(const CTuning &tuning) const
{
auto result = std::find_if(m_Tunings.begin(), m_Tunings.end(), [&tuning](const std::unique_ptr<CTuning> &other)
{
return other && tuning == *other;
});
return (result != m_Tunings.end()) ? result->get() : nullptr;
}
CTuning* CTuningCollection::FindIdenticalTuning(const CTuning &tuning)
{
auto result = std::find_if(m_Tunings.begin(), m_Tunings.end(), [&tuning](const std::unique_ptr<CTuning> &other)
{
return other && tuning == *other;
});
return (result != m_Tunings.end()) ? result->get() : nullptr;
}
Tuning::SerializationResult CTuningCollection::Serialize(std::ostream& oStrm, const mpt::ustring &name) const
{
srlztn::SsbWrite ssb(oStrm);
ssb.BeginWrite("TC", 3); // version
ssb.WriteItem(int8(1), "UTF8");
ssb.WriteItem(name, "0", &WriteStr);
uint16 dummyEditMask = 0xffff;
ssb.WriteItem(dummyEditMask, "1");
const size_t tcount = m_Tunings.size();
for(size_t i = 0; i<tcount; i++)
ssb.WriteItem(*m_Tunings[i], "2", &WriteTuning);
ssb.FinishWrite();
if(ssb.HasFailed())
return Tuning::SerializationResult::Failure;
else
return Tuning::SerializationResult::Success;
}
Tuning::SerializationResult CTuningCollection::Deserialize(std::istream &iStrm, mpt::ustring &name, mpt::Charset defaultCharset)
{
std::streamoff startpos = static_cast<std::streamoff>(iStrm.tellg());
const Tuning::SerializationResult oldLoadingResult = DeserializeOLD(iStrm, name, defaultCharset);
if(oldLoadingResult == Tuning::SerializationResult::NoMagic)
{ // An old version was not recognised - trying new version.
iStrm.clear();
iStrm.seekg(startpos, std::ios::beg);
srlztn::SsbRead ssb(iStrm);
ssb.BeginRead("TC", 3); // version
int8 use_utf8 = 0;
ssb.ReadItem(use_utf8, "UTF8");
const mpt::Charset charset = use_utf8 ? mpt::Charset::UTF8 : defaultCharset;
const srlztn::SsbRead::ReadIterator iterBeg = ssb.GetReadBegin();
const srlztn::SsbRead::ReadIterator iterEnd = ssb.GetReadEnd();
for(srlztn::SsbRead::ReadIterator iter = iterBeg; iter != iterEnd; iter++)
{
uint16 dummyEditMask = 0xffff;
if(ssb.MatchesId(iter, "0"))
ssb.ReadIterItem(iter, name, [charset](std::istream &iStrm, mpt::ustring &ustr, const std::size_t dummy){ return ReadStr(iStrm, ustr, dummy, charset); });
else if(ssb.MatchesId(iter, "1"))
ssb.ReadIterItem(iter, dummyEditMask);
else if(ssb.MatchesId(iter, "2"))
ssb.ReadIterItem(iter, *this, [charset](std::istream &iStrm, CTuningCollection &Tc, const std::size_t dummy){ return ReadTuning(iStrm, Tc, dummy, charset); });
}
if(ssb.HasFailed())
return Tuning::SerializationResult::Failure;
else
return Tuning::SerializationResult::Success;
}
else
{
return oldLoadingResult;
}
}
Tuning::SerializationResult CTuningCollection::DeserializeOLD(std::istream &inStrm, mpt::ustring &uname, mpt::Charset defaultCharset)
{
//1. begin marker:
uint32 beginMarker = 0;
mpt::IO::ReadIntLE<uint32>(inStrm, beginMarker);
if(beginMarker != MagicBE("TCSH")) // Magic is reversed in file, hence BE
return Tuning::SerializationResult::NoMagic;
//2. version
int32 version = 0;
mpt::IO::ReadIntLE<int32>(inStrm, version);
if(version > 2 || version < 1)
return Tuning::SerializationResult::Failure;
//3. Name
if(version < 2)
{
std::string name;
if(!mpt::IO::ReadSizedStringLE<uint32>(inStrm, name, 256))
return Tuning::SerializationResult::Failure;
uname = mpt::ToUnicode(defaultCharset, name);
}
else
{
std::string name;
if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, name))
return Tuning::SerializationResult::Failure;
uname = mpt::ToUnicode(defaultCharset, name);
}
//4. Editmask
int16 em = 0;
mpt::IO::ReadIntLE<int16>(inStrm, em);
//Not assigning the value yet, for if it sets some property const,
//further loading might fail.
//5. Tunings
{
uint32 s = 0;
mpt::IO::ReadIntLE<uint32>(inStrm, s);
if(s > 50)
return Tuning::SerializationResult::Failure;
for(size_t i = 0; i<s; i++)
{
if(!AddTuning(inStrm, defaultCharset))
{
return Tuning::SerializationResult::Failure;
}
}
}
//6. End marker
uint32 endMarker = 0;
mpt::IO::ReadIntLE<uint32>(inStrm, endMarker);
if(endMarker != MagicBE("TCSF")) // Magic is reversed in file, hence BE
return Tuning::SerializationResult::Failure;
return Tuning::SerializationResult::Success;
}
bool CTuningCollection::Remove(const CTuning *pT)
{
const auto it = std::find_if(m_Tunings.begin(), m_Tunings.end(),
[&] (const std::unique_ptr<CTuning> & upT) -> bool
{
return upT.get() == pT;
}
);
if(it == m_Tunings.end())
{
return false;
}
m_Tunings.erase(it);
return true;
}
bool CTuningCollection::Remove(const std::size_t i)
{
if(i >= m_Tunings.size())
{
return false;
}
m_Tunings.erase(m_Tunings.begin() + i);
return true;
}
CTuning* CTuningCollection::AddTuning(std::unique_ptr<CTuning> pT)
{
if(m_Tunings.size() >= s_nMaxTuningCount)
{
return nullptr;
}
if(!pT)
{
return nullptr;
}
CTuning *result = pT.get();
m_Tunings.push_back(std::move(pT));
return result;
}
CTuning* CTuningCollection::AddTuning(std::istream &inStrm, mpt::Charset defaultCharset)
{
if(m_Tunings.size() >= s_nMaxTuningCount)
{
return nullptr;
}
if(!inStrm.good())
{
return nullptr;
}
std::unique_ptr<CTuning> pT = CTuning::CreateDeserializeOLD(inStrm, defaultCharset);
if(!pT)
{
pT = CTuning::CreateDeserialize(inStrm, defaultCharset);
}
if(!pT)
{
return nullptr;
}
CTuning *result = pT.get();
m_Tunings.push_back(std::move(pT));
return result;
}
#ifdef MODPLUG_TRACKER
bool UnpackTuningCollection(const CTuningCollection &tc, const mpt::PathString &prefix)
{
bool error = false;
auto numberFmt = mpt::format_simple_spec<mpt::ustring>().Dec().FillNul().Width(1 + static_cast<int>(std::log10(tc.GetNumTunings())));
for(std::size_t i = 0; i < tc.GetNumTunings(); ++i)
{
const CTuning & tuning = *(tc.GetTuning(i));
mpt::PathString fn;
fn += prefix;
mpt::ustring tuningName = tuning.GetName();
if(tuningName.empty())
{
tuningName = U_("untitled");
}
fn += mpt::PathString::FromUnicode(MPT_UFORMAT("{} - {}")(mpt::ufmt::fmt(i + 1, numberFmt), tuningName)).AsSanitizedComponent();
fn += mpt::PathString::FromUTF8(CTuning::s_FileExtension);
if(mpt::native_fs{}.exists(fn))
{
error = true;
} else
{
mpt::IO::SafeOutputFile sfout(fn, std::ios::binary, mpt::IO::FlushModeFromBool(TrackerSettings::Instance().MiscFlushFileBuffersOnSave));
if(tuning.Serialize(sfout) != Tuning::SerializationResult::Success)
{
error = true;
}
}
}
return !error;
}
#endif
} // namespace Tuning
OPENMPT_NAMESPACE_END
|