My Project
MonoScriptingState.h
1 #pragma once
2 
3 #ifdef WIN32
4 #define MONO_API_V2
5 #endif
6 
7 #include <mono/jit/jit.h>
8 #ifdef MONO_API_V2
9 #include <glib.h>
10 #endif
11 #include <mono/metadata/environment.h>
12 #include <mono/metadata/mono-config.h>
13 #include <mono/metadata/assembly.h>
14 #include <mono/metadata/threads.h>
15 #include <INPLScriptingState.h>
16 #include <set>
17 #include <util/mutex.h>
18 
19 // forward declare
20 struct AssemblyFile;
21 struct ClassObject;
23 
24 typedef std::map <std::string, AssemblyFile*> Assembly_File_Map_Type;
25 typedef std::map <std::string, ClassObject*> Class_Map_Type;
26 
29 {
31  std::string m_namespace_name;
32 
34  std::string m_class_name;
35 
37  MonoClass * m_class;
39  MonoMethod * m_activate_function;
40 };
41 
44 {
45  MonoAssembly* m_assembly;
46  MonoImage* m_image;
47 
48  Class_Map_Type m_classes;
49 };
50 
56 {
57 public:
60 
61  static GlobalAssembliesPool& GetSingleton();
62 
63  // Get mono domain
64  inline MonoDomain* GetMonoDomain() {return m_mono_domain;};
65  // Set root mono domain in to which all assemblies are loaded.
66  void SetMonoDomain(MonoDomain* domain) {m_mono_domain = domain;};
67 
75  ClassObject* GetClass(const string& filePath);
76 
77 private:
78 
80  Assembly_File_Map_Type m_assemblies;
81 
83  MonoDomain * m_mono_domain;
84 
85  // mutex for function GetClass().
86  ParaEngine::mutex m_mutex;
87 };
88 
91 {
92 public:
95 
97  void Init();
98 
100  const char* GetName() const;
101 
103  inline MonoDomain* GetMonoDomain() {return s_mono_domain;};
104 
106  void LoadNPLLib();
107 
109  void LoadParaLib();
110 
111 public:
113  virtual void Release();
114 
118  virtual void* GetState(){return (void*)GetMonoDomain();};
119 
121  virtual bool IsValid() {return s_mono_domain != 0;}
122 
129  virtual bool IsScriptFileLoaded(const string& filepath);
130 
139  virtual bool LoadFile(const string& filePath, bool bReload);
140 
145  virtual int DoString(const char* sCall, int nLength = 0);
146 
152  virtual NPL::NPLReturnCode ActivateFile(const string& filepath, const char * code = NULL, int nLength=0);
153 
159  virtual bool CreateState(const char* name, NPL::INPLRuntimeState* pState = 0);
160 
164  virtual void DestroyState();
165 
168  virtual void SetNPLRuntimeState(NPL::INPLRuntimeState* pState);
169 
171  virtual NPL::INPLRuntimeState* GetNPLRuntimeState();
172 
173 protected:
177  void CreateAppDomain();
178 
179 private:
181  string m_name;
182 
184  static MonoDomain * s_mono_domain;
185 
187  GlobalAssembliesPool* m_global_assemblies_pool;
188 
190  NPL::INPLRuntimeState* m_npl_runtime_state;
191 };
192 
193 
the .Net assembly file, which may contain many C# classes
Definition: MonoScriptingState.h:43
virtual bool IsValid()
return true if the runtime state is valid
Definition: MonoScriptingState.h:121
std::string m_class_name
keep a duplicate copy of class file name, like "HelloWorld"
Definition: MonoScriptingState.h:34
MonoMethod * m_activate_function
the static void activate() function inside the mono class.
Definition: MonoScriptingState.h:39
INPLRuntimeState interface for DLL interface.
Definition: INPLRuntimeState.h:27
this represent a class (with the same name as the file name) inside an assembly
Definition: MonoScriptingState.h:28
MonoDomain * GetMonoDomain()
get the mono domain.
Definition: MonoScriptingState.h:103
a singleton class that keeps dynamically loaded meta data.
Definition: MonoScriptingState.h:55
virtual void * GetState()
get the Mono Domain state.
Definition: MonoScriptingState.h:118
MonoClass * m_class
the Mono Class object of the class
Definition: MonoScriptingState.h:37
Definition: inftrees.h:24
This is the NPLMono DLL plugin interface.
Definition: INPLScriptingState.h:90
cross platform mutex
Definition: mutex.h:95
std::string m_namespace_name
name space name of the class
Definition: MonoScriptingState.h:31
Mono scripting state.
Definition: MonoScriptingState.h:90