raSystem  1.0 bata
raScriptEngine.h
Go to the documentation of this file.
1 #pragma managed
2 
3 #using <mscorlib.dll>
4 #using <system.dll>
5 
6 
7 using namespace System;
8 using namespace System::IO;
9 using namespace System::Reflection;
10 using namespace System::CodeDom;
11 using namespace System::CodeDom::Compiler;
12 
13 using namespace System::Security;
14 using namespace System::Security::Policy;
15 using namespace System::Security::Permissions;
16 
17 using namespace Microsoft::CSharp;
18 
19 
20 #define SCRIPT_TYPE_NAME "raScriptClass"
21 #define SCRIPT_UPDATE_TEST "UpdatePosition"
22 
23 #ifdef GetTempFileName
24 #undef GetTempFileName
25 #endif
26 
27 namespace raSystem
28 {
29  public ref class raScriptEngine sealed
30  {
31  public:
32  static bool Create()
33  {
34  return true;
35  }
36  static void Destroy()
37  {
38 
39  }
40  static void ResetObjects()
41  {
42  m_pScriptAssembly = nullptr;
43  m_pScriptType = nullptr;
44  m_hasNotSecurityException = false;
45 
46  if((m_pSavedFile != nullptr ) && (m_pSavedFile->Length > 0))
47  {
48  try
49  {
50  File::Delete(m_pSavedFile);
51  }
52  catch(Exception^)
53  {
54  }
55  m_pSavedFile = nullptr;
56  }
57  }
58  static bool LoadScriptData(System::String^ pScriptName)
59  {
60  ResetObjects();
61 
62  if(pScriptName == nullptr || pScriptName->Length == 0)
63  return false;
64 
65  StreamReader^ pReader = nullptr;
66  try
67  {
68  pReader = gcnew StreamReader(pScriptName);
69  System::String^ pScriptText = pReader->ReadToEnd();
70 
71  if(pScriptText == nullptr || pScriptText->Length == 0)
72  return false;
73 
74  CSharpCodeProvider^ pProvider = gcnew CSharpCodeProvider();
75  CompilerParameters^ pParams = gcnew CompilerParameters();
76  pParams->GenerateExecutable = false;
77  pParams->GenerateInMemory = false;
78  m_pSavedFile = System::String::Concat(Path::GetTempFileName(), ".dll");
79  pParams->OutputAssembly = m_pSavedFile;
80 
81  CompilerResults^ pResult = pProvider->CompileAssemblyFromSource(pParams,
82  gcnew array<System::String^>{ pScriptText });
83 
84  if(pResult->Errors->Count > 0)
85  {
86  System::Diagnostics::Debugger::Log(0, nullptr,
87  pResult->Errors[0]->ErrorText);
88  return false;
89  }
90  else
91  {
92  m_pScriptAssembly = System::Reflection::Assembly::LoadFrom(pResult->PathToAssembly);
93  m_pScriptType = m_pScriptAssembly->GetType(SCRIPT_TYPE_NAME, false, true);
94  if(m_pScriptType = nullptr)
95  {
96  m_pScriptAssembly = nullptr;
97  return false;
98  }
99  return true;
100  }
101  }
102  catch(Exception^ e)
103  {
104  System::Diagnostics::Debugger::Log(0, nullptr,
105  e->ToString());
106  ResetObjects();
107  return false;
108  }
109  __finally
110  {
111  delete pReader;
112  }
113  }
114  static bool UpdatePosition(float fRunTime, float *pX, float *pY, float *pZ)
115  {
116  if(pX == NULL || pY == NULL || pZ == NULL)
117  return false;
118  if(m_pScriptAssembly == nullptr || m_pScriptType == nullptr)
119  return false;
120 
121  if(!m_canUpdatePosition)
122  return false;
123 
124  try
125  {
126  array<System::Object^>^pParams = { fRunTime, *pX, *pY, *pZ };
127  m_pScriptType->InvokeMember(SCRIPT_UPDATE_TEST, (BindingFlags)(
128  BindingFlags::InvokeMethod |
129  BindingFlags::DeclaredOnly |
130  BindingFlags::Public |
131  BindingFlags::NonPublic |
132  BindingFlags::Static),
133  nullptr, nullptr, pParams);
134 
135  *pX = static_cast<float>(pParams[1]);
136  *pY = static_cast<float>(pParams[2]);
137  *pZ = static_cast<float>(pParams[3]);
138  }
139  catch(TargetInvocationException^ e)
140  {
141  System::Diagnostics::Debugger::Log(0, nullptr,
142  e->ToString());
143  m_canUpdatePosition = false;
144  return false;
145  }
146  return true;
147  }
148  private:
149  static Assembly^ m_pScriptAssembly = nullptr;
150  static Type^ m_pScriptType = nullptr;
151  static String^ m_pSavedFile = nullptr;
152  static bool m_hasNotSecurityException = false;
153  static bool m_canUpdatePosition = true;
154 
155  };
156 }
static void Destroy()
#define SCRIPT_UPDATE_TEST
static bool LoadScriptData(System::String^ pScriptName)
static bool Create()
static bool UpdatePosition(float fRunTime, float *pX, float *pY, float *pZ)
#define SCRIPT_TYPE_NAME
static void ResetObjects()