MMORPG大型游戏设计与开发(客户端架构 part15 of vegine)

一个接口需要统一的派生接口,这样做的好处在于能够统一的进行管理。我所知的脚本语言中,接口有多重接口,也还有所谓的虚基类,这些都是方便类的管理。在vengine(微引擎)中,统一的的接口管理为kernel模块,类的管理采取了节点管理的模式,就是所有类对象使用一个实例进行管理。那么什么是节点,什么是树?这里的核心就相当于树,所有子类都是树中的一个节点。这种做法已经成为了一种模式,如果大家想要深入了解的话可以去查询一些相关的资料,我在这里也没有必要再强调这个模式了。接下来我们看看,所有模块的基类以及管理节点是如何实现的。

CODE

  模块kernel 文件base.h

  

/**
 * PAP Engine ( -- )
 * $Id config.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-12 11:15:08
 * @uses the vengine kernel base class
 */
#ifndef PAP_VENGINE_KERNEL_BASE_H_
#define PAP_VENGINE_KERNEL_BASE_H_
#include "vengine/config.h"
#include "vengine/kernel/node.h"

//插件必须导出一个名为DllMagicCode的U32整数,
//值等于 VENGINE_DLL_MAGIC_CODE|VENGINE_KERNEL_VERSION
#define VENGINE_DLL_MAGIC_CODE (static_cast<uint32_t>(0XC0DE0000))

namespace vengine_kernel {

//for plugin
typedef void (__stdcall* function_dllinit)(void*);
typedef void (__stdcall* function_dllrelease)(void);

class VENGINE_API Base {

 public:
   Base();
   virtual ~Base();

 public:
   //某个类注册到全局类检索表函数
   void registerclass(class_t* newclass);
   //检查两个类是否具有继承关系
   bool isinheritance(const class_t* thisclass, const class_t* baseclass) const;
   //根据类名获得定义类
   class_t* getclass(const char* name);

 //node operator
 public:
   virtual Node* newnode(const char* classname, 
                         const char* position, 
                         const char* nodename);
   virtual Node* newnode(const char* classname, 
                         Node* parentnode, 
                         const char* nodename);
   //get node by node name, sample: getnode("bin\\data")
   virtual Node* getnode(const char* name);
 
 //plugin
 public:
   bool loadplugin(const char* name, void* param);
   void shutdown(); //kernel close

 public:
   static Base* getself();
   static void* get_userdata();
   static void set_userdata(void* data);

 protected:
   static Base* self_;
   static void* userdata_;
   std::map<STRING, class_t*> classmap_; //is a map not a list
   class_t* firstclass_inmap_;
   Node noderoot_;

 protected:
   struct plugininfo_t {
     HMODULE hmodule;
     STRING pluginfile;
     function_dllinit dllinit_pointer;
     function_dllrelease dllrelease_pointer;
   };
   void free_allplugin();
   std::vector<plugininfo_t> pluginlist_; //all plugin in a vector
};

}; //namespace vengine_kernel
#endif //PAP_VENGINE_KERNEL_BASE_H_

  模块kernel 文件class.h

/**
 * PAP Engine ( -- )
 * $Id class.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-17 16:28:41
 * @uses vengine kernel class module
 */
#ifndef VENGINE_KERNEL_CLASS_H_
#define VENGINE_KERNEL_CLASS_H_

#include "vengine/config.h"
#include "vengine/exception/base.h"

namespace vengine_kernel {

class Node;

struct VENGINE_API class_t {
  const char* name; //class name
  int32_t objectsize; //class size
  Node* (__stdcall* newobject_pointer)(); //函数指针,用于生成一个Node类实例
  class_t* baseclass; //基类
  class_t* nextclass; //下一个类
  Node* newobject(const char* name); //生成对象方法
};

}; //namespace vengine_kernel

//variableflag 为命名空间准备的变量标识,
//如name1::name2::class1 则此值在函数中为name1_name2_class1
//如果不是命名空间直接填写类名即可

//据类名取得定义类
#define VENGINE_KERNEL_GETCLASS(classname, variableflag) (&classname::class_##variableflag##_)

//定义声明宏
#define VENGINE_KERNEL_DECLARE_DYNAMIC(variableflag) \
 public: \
   static vengine_kernel::class_t class_##variableflag##_; \
   virtual const vengine_kernel::class_t* getclass() const; \
   static vengine_kernel::Node* __stdcall newobject();

//定义实现宏
#define VENGINE_KERNEL_IMPLEMENT_DYNAMIC(classname, baseclass, variableflag) \
static char name_##variableflag[] = #classname; \
vengine_kernel::class_t classname::class_##variableflag##_ = { \
  name_##variableflag, sizeof(classname), classname::newobject, baseclass, NULL \
}; \
const vengine_kernel::class_t* classname::getclass() const { \
  return &classname::class_##variableflag##_; \
}; \
vengine_kernel::Node* classname::newobject() { \
  return new classname; \
}

//纯虚类类定义实现宏
#define VENGINE_KERNEL_IMPLEMENT_VIRTUAL_DYNAMIC(classname, baseclass, variableflag) \
static char name_##variableflag[] = #classname; \
vengine_kernel::class_t classname::class_##variableflag##_ = { \
  name_##variableflag, sizeof(classname), classname::newobject, baseclass, NULL \
}; \
const vengine_kernel::class_t* classname::getclass() const { \
  return &classname::class_##variableflag##_; \
}; \
vengine_kernel::Node* classname::newobject() { \
  VENGINE_SHOW("pure a virtual class"); \
  return NULL; \
}

#define VENGINE_KERNEL_DECLARE_LOGICAL(havelogical) \
 public: \
   virtual inline bool is_havelogical() { return havelogical; }

#endif //VENGINE_KERNEL_CLASS_H_

  模块kernel 文件node.h

/**
 * PAP Engine ( -- )
 * $Id node.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-12 11:15:08
 * @uses vengine kernel node class module
 */
#ifndef VENGINE_KERNEL_NODE_H_
#define VENGINE_KERNEL_NODE_H_

#include "vengine/config.h"
#include "vengine/kernel/class.h"

namespace vengine_kernel {

class VENGINE_API Node {
 public:
   Node();
   Node(const char* name);
   virtual ~Node();
   //返回子节点数目
   int32_t get_childnumber() const;
   //获得子节点名称
   virtual const STRING& getname() const;
   //获得子节点列表
   virtual std::list<Node*>& get_childlist();
   //返回父节点
   virtual Node* getparent() const;
   //初始化节点
   virtual void init(void*) {}; //空指针,子类可以用这个继承这个指针初始化
   //逻辑轮循环
   virtual void tick();

 public:
   //查找子节点,如果没有找到,返回NULL
   virtual Node* lookup_child(const char* name);
   //添加子节点
   virtual void addchild(Node* node);
   //删除子节点,如果该子节点存在则返回true,否则为false
   virtual bool removechild(Node* node);
   //删除所有子节点
   virtual void remove_allchild();
   //释放资源
   virtual void release();
 
VENGINE_KERNEL_DECLARE_DYNAMIC(vengine_kernel_Node); //类节点自动定义实现

 protected:
   typedef std::list<Node*> childlist;
   STRING name_; //节点名
   childlist childlist_; //子节点列表
   Node* parent_; //父节点指针
   friend struct class_t;
   
};

}; //namespace vengine_kernel

#endif //VENGINE_KERNEL_NODE_H_

 

总结

  其实如果一个代码有足够的注释,则不必过多的解释,否则会造成读者的困惑。不过这里面需要注意的是,几个实现的宏VENGINE_KERNEL_*,希望大家仔细去理解一下它实现的原理。这里的核心实现也是依赖于此宏来实现,如果你将这些宏弄清楚了则核心模块的原理也就掌握了。下一节讲的是客户端的游戏模块,是提供给游戏实现的基础接口,比较多,同时希望大家不会错过。

posted @ 2014-08-23 10:48  恋月  阅读(1134)  评论(0编辑  收藏  举报