Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.1 KiB

  1. // This is the main DLL file.
  2. #include "stdafx.h"
  3. extern int TestRandomNumberGeneration( int bottom, int top ) ;
  4. extern const char *TestFileSystemHook( ) ;
  5. using namespace ResponseRulesCLI;
  6. namespace MikeCTest
  7. {
  8. public ref class Person
  9. {
  10. private:
  11. String^ _name;
  12. public:
  13. Person( String^ name )
  14. {
  15. _name = name;
  16. }
  17. String^ Description()
  18. {
  19. // this is the big test!
  20. int rndNum = TestRandomNumberGeneration( 0, 10 );
  21. String^ foo = gcnew String(TestFileSystemHook());
  22. return ( "My name is " + _name +
  23. " filesystem is " + foo
  24. // " my ID is " + rndNum.ToString()
  25. );
  26. }
  27. static bool HopefullyDontCrash()
  28. {
  29. TestFileSystemHook( );
  30. return true;
  31. }
  32. };
  33. }
  34. #include "response_system.h"
  35. StrToAnsi::StrToAnsi( String ^unicodestr )
  36. {
  37. m_pStr = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(unicodestr);
  38. }
  39. StrToAnsi::~StrToAnsi( )
  40. {
  41. System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)(void*)m_pStr);
  42. }
  43. StrToAnsi::operator TCHAR *() const
  44. {
  45. return m_pStr;
  46. }
  47. /*
  48. using namespace System::Runtime::InteropServices; // for class Marshal
  49. void PrintMessage(System::String^ str)
  50. {
  51. const char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
  52. printf(str2);
  53. Marshal::FreeHGlobal((System::IntPtr)(void*)str2);
  54. }
  55. PrintMessage("Method 2");
  56. */
  57. using namespace ResponseRulesCLI;
  58. // Allocate the native object on the C++ Heap via a constructor
  59. ResponseSystemCLI::ResponseSystemCLI()
  60. {
  61. m_pImpl = new ResponseSystemImplementationCLI();
  62. m_CriteriaDict = gcnew CriterionDictWrapper_t(&m_pImpl->m_Criteria);
  63. m_ResponseGroupsDict = gcnew ResponseGroupDictWrapper_t(&m_pImpl->m_Responses);
  64. m_RulesContainer = gcnew RulesAsList(this);
  65. }
  66. // Deallocate the native object on a destructor
  67. ResponseSystemCLI::~ResponseSystemCLI()
  68. {
  69. this->!ResponseSystemCLI();
  70. }
  71. // Deallocate the native object on the finalizer just in case no destructor is called
  72. ResponseSystemCLI::!ResponseSystemCLI()
  73. {
  74. delete m_pImpl;
  75. }
  76. void ResponseSystemCLI::LoadFromFile( String^ filename )
  77. {
  78. return m_pImpl->LoadFromFile( StrToAnsi(filename) );
  79. }
  80. int ResponseSystemCLI::CountRules()
  81. {
  82. return m_pImpl->CountRules();
  83. }
  84. ResponseSystemImplementationCLI *ResponseSystemCLI::GetNativePtr()
  85. {
  86. return m_pImpl;
  87. }
  88. void ResponseSystemCLI::RulesAsList::RulesIterator::Reset()
  89. {
  90. m_bIsBefore = true;
  91. m_idx = m_System->GetNativePtr()->m_RulePartitions.First();
  92. }
  93. bool ResponseSystemCLI::RulesAsList::RulesIterator::MoveNext()
  94. {
  95. if (m_bIsBefore)
  96. {
  97. m_bIsBefore = false;
  98. m_idx = m_System->GetNativePtr()->m_RulePartitions.First();
  99. return IsValid();
  100. }
  101. else
  102. {
  103. if ( !IsValid() )
  104. {
  105. return false;
  106. }
  107. else
  108. {
  109. // stick the index on the stack temporarily so we can pass a reference to it
  110. ResponseRules::ResponseRulePartition::tIndex tmp = m_idx;
  111. m_idx = m_System->GetNativePtr()->m_RulePartitions.Next(tmp);
  112. return IsValid();
  113. }
  114. }
  115. }
  116. bool ResponseSystemCLI::RulesAsList::RulesIterator::IsValid()
  117. {
  118. // stick the index on the stack temporarily so we can pass a reference to it
  119. ResponseRules::ResponseRulePartition::tIndex tmp = m_idx;
  120. return m_System->GetNativePtr()->m_RulePartitions.IsValid( tmp );
  121. }
  122. Object^ ResponseSystemCLI::RulesAsList::RulesIterator::Current::get()
  123. {
  124. if (IsValid())
  125. {
  126. ResponseRules::ResponseRulePartition::tIndex i = m_idx;
  127. return gcnew Rule(&m_System->GetNativePtr()->m_RulePartitions[i],
  128. i,
  129. m_System->GetNativePtr()->m_RulePartitions.GetElementName(i) );
  130. }
  131. else
  132. {
  133. throw gcnew System::InvalidOperationException();
  134. }
  135. }
  136. /*
  137. /// access to the dictionary of index->criteria
  138. Criterion ^ ResponseSystemCLI::Criteria::get( short key )
  139. {
  140. return gcnew Criterion(&m_pImpl->m_Criteria[key], key);
  141. }
  142. unsigned int ResponseSystemCLI::CriteriaCount::get()
  143. {
  144. return m_pImpl->m_Criteria.Count();
  145. }
  146. */
  147. Rule ^ ResponseSystemCLI::FindBestMatchingRule( System::Collections::IDictionary ^facts )
  148. {
  149. ResponseRules::CriteriaSet criteria;
  150. TurnIDictIntoCriteriaSet( facts, &criteria );
  151. float bestscore; // of matching rule
  152. ResponseRules::ResponseRulePartition::tIndex bestRuleIndex = m_pImpl->FindBestMatchingRule( criteria, false, bestscore );
  153. if ( m_pImpl->m_RulePartitions.IsValid( bestRuleIndex ) )
  154. {
  155. return RuleFromIdx(bestRuleIndex);
  156. }
  157. else
  158. {
  159. return nullptr;
  160. }
  161. }
  162. Rule ^ResponseSystemCLI::RuleFromIdx( ResponseRules::ResponseRulePartition::tIndex idx )
  163. {
  164. return gcnew Rule( &m_pImpl->m_RulePartitions[idx],
  165. idx,
  166. m_pImpl->m_RulePartitions.GetElementName(idx) );
  167. }
  168. void ResponseSystemCLI::TurnIDictIntoCriteriaSet( System::Collections::IDictionary ^facts, ResponseRules::CriteriaSet *critset )
  169. {
  170. // for each key and value in the dictionary, add to set.
  171. for each ( System::Collections::DictionaryEntry pair in facts )
  172. {
  173. critset->AppendCriteria( StrToAnsi(pair.Key->ToString()), StrToAnsi(pair.Value->ToString()) );
  174. }
  175. }
  176. array< System::Collections::Generic::KeyValuePair<Rule ^,float> >^ ResponseSystemCLI::FindAllRulesMatchingCriteria( System::Collections::IDictionary ^facts )
  177. {
  178. ResponseRules::CriteriaSet crits;
  179. TurnIDictIntoCriteriaSet( facts, &crits );
  180. ResponseSystemImplementationCLI::FindAllRulesRetval_t ruleresults;
  181. m_pImpl->FindAllRulesMatchingCriteria( &ruleresults, crits );
  182. if ( ruleresults.Count() < 1 )
  183. {
  184. // return empty array.
  185. return gcnew array< System::Collections::Generic::KeyValuePair<Rule ^,float> >(0);
  186. }
  187. else
  188. {
  189. const int count = ruleresults.Count();
  190. array< System::Collections::Generic::KeyValuePair<Rule ^,float> >^ retval = gcnew array< System::Collections::Generic::KeyValuePair<Rule ^,float> >(count);
  191. for (int i = 0 ; i < count ; ++i )
  192. {
  193. const ResponseSystemImplementationCLI::RuleAndScorePair_t &pair = ruleresults[i];
  194. retval[i] = System::Collections::Generic::KeyValuePair<Rule ^,float>(RuleFromIdx(pair.ruleidx),pair.score);
  195. /*
  196. retval[i].Key = RuleFromIdx(pair.ruleidx);
  197. retval[i].Value = pair.score;
  198. */
  199. }
  200. return retval;
  201. }
  202. }
  203. /*
  204. #pragma unmanaged
  205. #include "../../responserules/runtime/response_types_internal.h"
  206. #pragma managed
  207. */