From 8b778dda61c45f5d7a4dc416478c42ff4aa1de7f Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 23 Feb 2022 23:13:27 +0100 Subject: Moved content management out to ContentBundle --- Stars45/ContentBundle.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) (limited to 'Stars45/ContentBundle.cpp') diff --git a/Stars45/ContentBundle.cpp b/Stars45/ContentBundle.cpp index 9f09833..534f550 100644 --- a/Stars45/ContentBundle.cpp +++ b/Stars45/ContentBundle.cpp @@ -18,12 +18,23 @@ // +--------------------------------------------------------------------+ +ContentBundle* ContentBundle::GetInstance() +{ + static ContentBundle instance{}; + return &instance; +} + +// +--------------------------------------------------------------------+ + +ContentBundle::ContentBundle() : ContentBundle("content", nullptr) +{ +} + +// +--------------------------------------------------------------------+ + ContentBundle::ContentBundle(const char* bundle, Locale* locale) { - Text file = FindFile(bundle, locale); - if (file.length() > 0) { - LoadBundle(file); - } + Load(bundle, locale); } // +--------------------------------------------------------------------+ @@ -34,6 +45,55 @@ ContentBundle::~ContentBundle() // +--------------------------------------------------------------------+ +bool +ContentBundle::Init() +{ + DataLoader* loader = DataLoader::GetLoader(); + List bundles; + + loader->SetDataPath("Content/"); + loader->ListFiles("content*", bundles); + + ListIter iter = bundles; + while (++iter) { + Text* filename = iter.value(); + int n = filename->indexOf('_'); + if (n > 0) { + Locale::ParseLocale(filename->data() + n); // unused? + } + } + + loader->SetDataPath(0); + return true; +} + +// +--------------------------------------------------------------------+ + +void +ContentBundle::UseLocale(Locale* locale) +{ + DataLoader* loader = DataLoader::GetLoader(); + loader->SetDataPath("Content/"); + Load("content", locale); + loader->SetDataPath(nullptr); +} + +// +--------------------------------------------------------------------+ + +void +ContentBundle::Load(const char* bundle, Locale* locale) +{ + if (values.size() > 0) { + values.clear(); + } + Text file = FindFile(bundle, locale); + if (file.length() > 0) { + LoadBundle(file); + } +} + +// +--------------------------------------------------------------------+ + Text ContentBundle::GetText(const char* key) const { -- cgit v1.1