/inc/scannerthread.h

https://code.google.com/p/dwarftherapist/ · C Header · 183 lines · 147 code · 10 blank · 26 comment · 1 complexity · d86ef6537f2d4f1c6238152f5897b013 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef SCANNER_THREAD_H
  21. #define SCANNER_THREAD_H
  22. #include <QThread>
  23. #include "dfinstance.h"
  24. #include "scannerjob.h"
  25. #include "translationvectorsearchjob.h"
  26. #include "stdstringsearchjob.h"
  27. #include "nullterminatedstringsearchjob.h"
  28. #include "stonevectorsearchjob.h"
  29. #include "vectorsearchjob.h"
  30. #include "dwarfraceindexsearchjob.h"
  31. #include "creaturevectorsearchjob.h"
  32. #include "positionvectorsearchjob.h"
  33. #include "narrowingvectorsearchjob.h"
  34. #include "squadvectorsearchjob.h"
  35. #include "currentyearsearchjob.h"
  36. class ScannerThread : public QThread {
  37. Q_OBJECT
  38. public:
  39. ScannerThread(QObject *parent = 0)
  40. : QThread(parent)
  41. , m_meta(QByteArray())
  42. , m_result(NULL)
  43. {}
  44. void set_job(SCANNER_JOB_TYPE type) {
  45. m_type = type;
  46. }
  47. void set_meta(const QByteArray &meta) {
  48. m_meta = meta;
  49. }
  50. void set_search_vector(const QVector<VIRTADDR> &searchvector) {
  51. m_searchvector = searchvector;
  52. }
  53. void * get_result() {
  54. return m_result;
  55. }
  56. void clear_result() {
  57. m_result = NULL;
  58. }
  59. void run() {
  60. // Don't forget your 'break' when adding new sections, Trey. You've done
  61. // it twice now for a total waste of about 50 minutes >:|
  62. switch (m_type) {
  63. case FIND_TRANSLATIONS_VECTOR:
  64. m_job = new TranslationVectorSearchJob;
  65. break;
  66. case FIND_STONE_VECTOR:
  67. m_job = new StoneVectorSearchJob;
  68. break;
  69. case FIND_DWARF_RACE_INDEX:
  70. m_job = new DwarfRaceIndexSearchJob;
  71. break;
  72. case FIND_CREATURE_VECTOR:
  73. m_job = new CreatureVectorSearchJob;
  74. break;
  75. case FIND_POSITION_VECTOR:
  76. m_job = new PositionVectorSearchJob;
  77. break;
  78. case FIND_STD_STRING:
  79. {
  80. StdStringSearchJob *job = new StdStringSearchJob;
  81. job->set_needle(m_meta);
  82. m_job = job;
  83. }
  84. break;
  85. case FIND_NULL_TERMINATED_STRING:
  86. {
  87. // what is this, Java?
  88. NullTerminatedStringSearchJob *job = new NullTerminatedStringSearchJob;
  89. job->set_needle(m_meta);
  90. m_job = job;
  91. }
  92. break;
  93. case FIND_VECTORS_OF_SIZE:
  94. {
  95. VectorSearchJob *job = new VectorSearchJob;
  96. job->set_needle(m_meta);
  97. m_job = job;
  98. }
  99. break;
  100. case FIND_NARROWING_VECTORS_OF_SIZE:
  101. {
  102. NarrowingVectorSearchJob * job = new NarrowingVectorSearchJob;
  103. job->set_needle(m_meta);
  104. job->set_search_vector(m_searchvector);
  105. m_job = job;
  106. }
  107. break;
  108. case FIND_SQUADS_VECTOR:
  109. {
  110. m_job = new SquadVectorSearchJob;
  111. break;
  112. }
  113. break;
  114. case FIND_CURRENT_YEAR:
  115. {
  116. m_job = new CurrentYearSearchJob;
  117. }
  118. break;
  119. default:
  120. LOGW << "JOB TYPE NOT SET, EXITING THREAD";
  121. return;
  122. }
  123. // forward the status signals
  124. connect(m_job->df(), SIGNAL(scan_total_steps(int)),
  125. SIGNAL(sub_scan_total_steps(int)));
  126. connect(m_job->df(), SIGNAL(scan_progress(int)),
  127. SIGNAL(sub_scan_progress(int)));
  128. connect(m_job->df(), SIGNAL(scan_message(const QString&)),
  129. SIGNAL(scan_message(const QString&)));
  130. connect(m_job, SIGNAL(scan_message(const QString&)),
  131. SIGNAL(scan_message(const QString&)));
  132. connect(m_job, SIGNAL(found_address(const QString&, const quint32&)),
  133. SIGNAL(found_address(const QString&, const quint32&)));
  134. connect(m_job, SIGNAL(found_offset(const QString&, const int&)),
  135. SIGNAL(found_offset(const QString&, const int&)));
  136. connect(m_job, SIGNAL(main_scan_total_steps(int)),
  137. SIGNAL(main_scan_total_steps(int)));
  138. connect(m_job, SIGNAL(main_scan_progress(int)),
  139. SIGNAL(main_scan_progress(int)));
  140. connect(m_job, SIGNAL(sub_scan_total_steps(int)),
  141. SIGNAL(sub_scan_total_steps(int)));
  142. connect(m_job, SIGNAL(sub_scan_progress(int)),
  143. SIGNAL(sub_scan_progress(int)));
  144. connect(m_job, SIGNAL(quit()), this, SLOT(quit()));
  145. connect(m_job, SIGNAL(got_result(void *)), this, SLOT(set_result(void *)));
  146. QTimer::singleShot(0, m_job, SLOT(go()));
  147. exec();
  148. m_job->deleteLater();
  149. }
  150. private:
  151. SCANNER_JOB_TYPE m_type;
  152. ScannerJob *m_job;
  153. QByteArray m_meta;
  154. QVector<VIRTADDR> m_searchvector;
  155. protected:
  156. void * m_result;
  157. protected slots:
  158. void set_result(void * result) {
  159. m_result = result;
  160. }
  161. signals:
  162. void main_scan_total_steps(int);
  163. void main_scan_progress(int);
  164. void sub_scan_total_steps(int);
  165. void sub_scan_progress(int);
  166. void scan_message(const QString&);
  167. void found_address(const QString&, const quint32&);
  168. void found_offset(const QString&, const int&);
  169. };
  170. #endif