nutildef.h中主要定义了运行Nebula2程序所需要的最小依赖以及简化声明和使用Nebula2对象的工具宏,代码如下:
/****************************************************************************/
/* Nebula2 - Tutorial Utils */
/* nKernelInfo - Nebula2 实用工具宏及静态库 */
/* author: happykevins */
/****************************************************************************/
#pragma once
///----------------------------------------------------------------------------
/// +必要的链接库
#ifdef KS_STATICLINK_NEBULA2
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dxerr9.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9d.lib")
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "ogg_d.lib")
#pragma comment(lib, "theora_d.lib")
#pragma comment(lib, "sqlite_d.lib")
#pragma comment(lib, "d_microtcl.lib")
#pragma comment(lib, "d_nkernel.lib")
#pragma comment(lib, "d_nnebula.lib")
#pragma comment(lib, "d_ntoollib.lib")
#pragma comment(lib, "d_ngui.lib")
#pragma comment(lib, "d_ndinput8.lib")
#pragma comment(lib, "d_ndirect3d9.lib")
#endif
/// -必要的链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +声明Nebula2对象工具宏
/// 声明使用指定的Module
#define nNebulaUseModule(MODULE)
extern void *n_new_##MODULE (void);
extern bool n_init_##MODULE (nClass *, nKernelServer *);
/// 将指定Module添加到KernelServer
#define nNebulaAddModule(MODULE)
nKernelServer::Instance()->AddModule( #MODULE,
n_init_##MODULE, n_new_##MODULE);
/// 声明为Nebula2 Module
#define nNebulaModule(CLASS, MODULE, BASEMODULENAME)
extern bool n_init_##MODULE (nClass* clazz, nKernelServer* kernelServer);
extern void* n_new_##MODULE ();
bool n_init_##MODULE (nClass* clazz, nKernelServer* kernelServer) {
clazz->SetProperName(#MODULE);
clazz->SetInstanceSize(sizeof(CLASS));
kernelServer->AddClass(BASEMODULENAME, clazz);
return true;
};
void* n_new_##MODULE () { return n_new(CLASS()); };
/// 命令初始化函数名
#define nNebulaScriptInitCmds(MODULE) n_initcmds_##MODULE
/// 声明为Nebula2脚本Module
#define nNebulaScriptModule(CLASS, MODULE, BASEMODULENAME)
extern bool n_init_##MODULE (nClass* clazz, nKernelServer* kernelServer);
extern void* n_new_##MODULE ();
extern void n_initcmds_##MODULE (nClass *);
bool n_init_##MODULE (nClass* clazz, nKernelServer* kernelServer) {
clazz->SetProperName(#MODULE);
clazz->SetInstanceSize(sizeof(CLASS));
kernelServer->AddClass(BASEMODULENAME, clazz);
n_initcmds_##MODULE (clazz);
return true;
};
void* n_new_##MODULE () { return n_new(CLASS()); };
/// -声明Nebula2对象工具宏
///----------------------------------------------------------------------------
nkernelinfo类主要是对第3章代码的整理,目前支持察看Nebula2的Class列表和当前NOH中的对象两项功能。
nkernelinfo.h代码如下:
/****************************************************************************/
/* Nebula2 - Tutorial Utils */
/* nKernelInfo - Nebula2 内核信息调试器 */
/* author: happykevins */
/****************************************************************************/
#pragma once
#include "kernel/nkernelserver.h"
#include "util/nstack.h"
#include "util/nhashlist.h"
// 使用标准输出流(否则输出到Nebula2的日志系统)
#ifdef KS_USE_STDOUT
#define ks_print printf
#else
#define ks_print n_printf
#endif
class nKernelInfoHelper : public nRoot
{
public:
/// 构造nKernelInfoHelper
nKernelInfoHelper(nKernelServer* _ks = nKernelServer::Instance());
/// 设置KernelServer实例
void SetKernelServer( nKernelServer* _ks );
/// 将指定NOH分支的当前信息输出到Nebula日至系统
void LogNOH(const char* szRootPath);
/// 将当前的类列表输出到Nebula日至系统
void LogCLS();
protected:
nKernelServer* ks;
};
nkernelinfo.cpp代码如下:
// nkernelinfo.cpp
#pragma warning(push)
#pragma warning(disable: 4267 4244)
#include "nkernelinfo.h"
#include "nutildefs.h"
/// 声明为Nebula2脚本支持类
nNebulaScriptModule(nKernelInfoHelper, nkernelinfohelper, "nroot");
///----------------------------------------------------------------------------
/// +Common Section
///
/// 构造nKernelInfoHelper
nKernelInfoHelper::nKernelInfoHelper(nKernelServer* _ks)
: ks(_ks)
{
}
/// 设置KernelServer实例
void nKernelInfoHelper::SetKernelServer( nKernelServer* _ks )
{
this->ks = _ks;
}
/// 将指定NOH分支的当前信息输出到Nebula日至系统
void nKernelInfoHelper::LogNOH(const char* szRootPath)
{
n_assert(ks);
n_assert(szRootPath);
nRoot* pRoot = ks->Lookup(szRootPath);
if ( !pRoot )
{
ks_print("--NONE:%s---- ", szRootPath);
return;
}
// 内核加锁
ks->Lock();
// 打印根节点NOH名
if ( pRoot->GetParent() )
{
ks_print("%s ", pRoot->GetFullName().Get());
}
else
{
ks_print("%s ", pRoot->GetName());
}
// 打印所有子节点
if ( pRoot->GetHead() )
{
// 层次栈
// 在开始时将根节点入栈
nStack<nRoot*> deepStack;
deepStack.Push(pRoot->GetHead());
// 遍历树
do
{
// 获得当前栈顶元素
nRoot* obj = deepStack.Pop();
// 打印该元素NOH名
if ( obj->GetParent() )
{
ks_print("%s ", obj->GetFullName().Get());
}
else
{
ks_print("%s ", obj->GetName());
}
// 兄弟节点入栈
if ( obj->IsLinked() && obj->GetSucc() )
{
deepStack.Push(obj->GetSucc());
}
// 第一个子节点入栈
if ( obj->GetHead() )
{
deepStack.Push(obj->GetHead());
}
} while(!deepStack.IsEmpty());
}
// 内核解锁
ks->Unlock();
}
/// 将当前的类列表输出到Nebula日至系统
void nKernelInfoHelper::LogCLS()
{
n_assert(ks);
// 内核加锁
ks->Lock();
// 获得Class列表
const nHashList* classList = ks->GetClassList();
nHashNode* node = (nClass*)classList->GetHead();
// 遍历输出Class列表
do
{
nClass* cls = (nClass*)node;
ks_print("%s ", cls->GetName());
node = node->GetSucc();
} while ( node );
// 内核解锁
ks->Unlock();
}
///
/// -Common Section
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +Script Section
///
/// LogCLS的脚本支持
static void n_logcls(void* slf, nCmd* cmd);
/// LogNOH的脚本支持
static void n_lognoh(void* slf, nCmd* cmd);
void nNebulaScriptInitCmds(nkernelinfohelper) (nClass* cl)
{
cl->BeginCmds();
cl->AddCmd("v_logcls_v", 'LCLS', n_logcls);
cl->AddCmd("v_lognoh_s", 'LNOH', n_lognoh);
cl->EndCmds();
}
static void n_logcls(void* slf, nCmd* cmd)
{
nKernelInfoHelper* self = (nKernelInfoHelper*) slf;
self->LogCLS();
}
static void n_lognoh(void* slf, nCmd* cmd)
{
nKernelInfoHelper* self = (nKernelInfoHelper*) slf;
const char* root = cmd->In()->GetS();
self->LogNOH(root);
}
///
/// -Script Section
///----------------------------------------------------------------------------
#pragma warning(pop)
查看本文来源