2018-12-08 23:44:41 -07:00
|
|
|
#include "CAssetID.h"
|
2019-02-02 17:30:36 -07:00
|
|
|
#include "CRandom.h"
|
2018-12-08 23:44:41 -07:00
|
|
|
#include "TString.h"
|
|
|
|
|
|
2025-12-17 13:44:30 -05:00
|
|
|
#include "Common/FileIO/IInputStream.h"
|
|
|
|
|
#include "Common/FileIO/IOutputStream.h"
|
|
|
|
|
|
2026-02-09 11:13:39 -05:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
2018-12-08 23:44:41 -07:00
|
|
|
void CAssetID::Write(IOutputStream& rOutput, EIDLength ForcedLength /*= eInvalidIDLength*/) const
|
|
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
const auto Length = (ForcedLength == EIDLength::kInvalidIDLength ? mLength : ForcedLength);
|
2018-12-08 23:44:41 -07:00
|
|
|
|
2020-06-11 11:08:59 -04:00
|
|
|
if (Length == EIDLength::k32Bit)
|
2026-01-12 14:32:52 -05:00
|
|
|
rOutput.WriteU32(ToU32());
|
2018-12-08 23:44:41 -07:00
|
|
|
else
|
2026-01-12 14:32:52 -05:00
|
|
|
rOutput.WriteU64(ToU64());
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CAssetID::CAssetID(IInputStream& rInput, EIDLength Length)
|
|
|
|
|
: mLength(Length)
|
|
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
if (Length == EIDLength::k32Bit)
|
2026-01-12 11:09:45 -05:00
|
|
|
mID = rInput.ReadU32();
|
2020-06-11 11:08:59 -04:00
|
|
|
else
|
2026-01-12 11:09:45 -05:00
|
|
|
mID = rInput.ReadU64();
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CAssetID::CAssetID(IInputStream& rInput, EGame Game)
|
|
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
*this = CAssetID(rInput, (Game <= EGame::Echoes ? EIDLength::k32Bit : EIDLength::k64Bit));
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TString CAssetID::ToString(EIDLength ForcedLength /*= eInvalidIDLength*/) const
|
|
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
const auto Length = (ForcedLength == EIDLength::kInvalidIDLength ? mLength : ForcedLength);
|
|
|
|
|
if (Length == EIDLength::k32Bit)
|
2026-02-09 11:13:39 -05:00
|
|
|
return fmt::format("{:08X}", ToU32());
|
2018-12-08 23:44:41 -07:00
|
|
|
else
|
2026-02-09 11:13:39 -05:00
|
|
|
return fmt::format("{:016X}", ToU64());
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ************ STATIC ************
|
|
|
|
|
CAssetID CAssetID::FromString(const TString& rkString)
|
|
|
|
|
{
|
|
|
|
|
// If the input is a hex ID in string form, then preserve it... otherwise, generate an ID by hashing the string
|
2026-01-11 10:08:59 -05:00
|
|
|
const auto Name = rkString.GetFileName(false);
|
|
|
|
|
const auto NameLength = Name.Length();
|
2018-12-08 23:44:41 -07:00
|
|
|
|
|
|
|
|
if (Name.IsHexString())
|
|
|
|
|
{
|
2018-12-09 02:38:38 -07:00
|
|
|
if (NameLength == 8) return CAssetID(Name.ToInt32(16));
|
|
|
|
|
if (NameLength == 16) return CAssetID(Name.ToInt64(16));
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CAssetID(rkString.Hash64());
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 17:30:36 -07:00
|
|
|
CAssetID CAssetID::RandomID(EIDLength Length)
|
2018-12-08 23:44:41 -07:00
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
if (Length != EIDLength::k64Bit)
|
2019-02-02 17:30:36 -07:00
|
|
|
{
|
|
|
|
|
CAssetID Out;
|
2020-06-11 11:08:59 -04:00
|
|
|
Out.mLength = EIDLength::k32Bit;
|
2026-01-11 11:49:12 -05:00
|
|
|
Out.mID = static_cast<uint64_t>(CRandom::GlobalRandom().Int32()) & 0xFFFFFFFF;
|
2019-02-02 17:30:36 -07:00
|
|
|
return Out;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CAssetID Out;
|
2020-06-11 11:08:59 -04:00
|
|
|
Out.mLength = EIDLength::k64Bit;
|
2026-01-11 11:49:12 -05:00
|
|
|
Out.mID = static_cast<uint64_t>(CRandom::GlobalRandom().Int64());
|
2019-02-02 17:30:36 -07:00
|
|
|
return Out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CAssetID CAssetID::RandomID(EGame Game)
|
|
|
|
|
{
|
2020-06-11 11:08:59 -04:00
|
|
|
return RandomID(GameIDLength(Game));
|
2018-12-08 23:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ************ STATIC MEMBER INITIALIZATION ************
|
2026-01-12 16:14:55 -05:00
|
|
|
constinit const CAssetID CAssetID::skInvalidID32 = CAssetID(UINT64_MAX, EIDLength::k32Bit);
|
|
|
|
|
constinit const CAssetID CAssetID::skInvalidID64 = CAssetID(UINT64_MAX, EIDLength::k64Bit);
|