141 lines
3.4 KiB
C++
141 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <a8/singleton.h>
|
|
|
|
#include <f8/utils.h>
|
|
|
|
template<typename T>
|
|
void SafeCallStaticPreInit(...) { };
|
|
template<typename T, void() = &T::StaticPreInit>
|
|
void SafeCallStaticPreInit(int) { T::StaticPreInit(); };
|
|
|
|
template<typename T>
|
|
void SafeCallStaticPostInit(...) { };
|
|
template<typename T, void() = &T::StaticPostInit>
|
|
void SafeCallStaticPostInit(int) { T::StaticPostInit(); };
|
|
|
|
template<typename T>
|
|
void SafeCallInit1(...) { };
|
|
template<typename T, void() = &T::Init1>
|
|
void SafeCallInit1(int)
|
|
{
|
|
for (auto item : T::raw_list) {
|
|
item->Init1();
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
void SafeCallInit2(...) { };
|
|
template<typename T, void() = &T::Init2>
|
|
void SafeCallInit2(int)
|
|
{
|
|
for (auto item : T::raw_list) {
|
|
item->Init2();
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
void SafeCallInit3(...) { };
|
|
template<typename T, void() = &T::Init3>
|
|
void SafeCallInit3(int)
|
|
{
|
|
for (auto item : T::raw_list) {
|
|
item->Init3();
|
|
}
|
|
};
|
|
|
|
namespace mt
|
|
{
|
|
|
|
struct MetaTable
|
|
{
|
|
std::function<void()> static_pre_init_cb;
|
|
std::function<void()> load_cb;
|
|
std::vector<std::function<void()>> init_cbs;
|
|
std::function<void()> static_post_init_cb;
|
|
};
|
|
|
|
class MetaMgr : public a8::Singleton<MetaMgr>
|
|
{
|
|
|
|
private:
|
|
MetaMgr() {};
|
|
friend class a8::Singleton<MetaMgr>;
|
|
|
|
public:
|
|
void Init();
|
|
void UnInit();
|
|
|
|
template<class T>
|
|
mt::MetaTable* RegMetaTable(const std::string& dir)
|
|
{
|
|
mt::MetaTable* p = new mt::MetaTable();
|
|
p->static_pre_init_cb =
|
|
[] ()
|
|
{
|
|
SafeCallStaticPreInit<T>(0);
|
|
};
|
|
p->load_cb =
|
|
[dir] ()
|
|
{
|
|
f8::ReadCsvMetaFile(dir + T::table_name, T::raw_list);
|
|
int id = 0;
|
|
for (auto item : T::raw_list) {
|
|
switch (T::table_type) {
|
|
case 0:
|
|
{
|
|
T::id_hash[id++] = item;
|
|
}
|
|
break;
|
|
case 1:
|
|
{
|
|
long long key = 0.0f;
|
|
T::id_hash[key] = item;
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
std::string key = "";
|
|
T::name_hash[key] = item;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
abort();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
p->init_cbs =
|
|
{
|
|
[] ()
|
|
{
|
|
SafeCallInit1<T>(0);
|
|
},
|
|
[] ()
|
|
{
|
|
SafeCallInit2<T>(0);
|
|
},
|
|
[] ()
|
|
{
|
|
SafeCallInit3<T>(0);
|
|
},
|
|
};
|
|
p->static_post_init_cb =
|
|
[] ()
|
|
{
|
|
SafeCallStaticPostInit<T>(0);
|
|
};
|
|
meta_tables.push_back(p);
|
|
return p;
|
|
}
|
|
|
|
const std::string GetResDir() { return res_path_; }
|
|
private:
|
|
std::vector<MetaTable*> meta_tables;
|
|
std::string res_path_;
|
|
};
|
|
|
|
}
|