ChunkDataView.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : ChunkView *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/ChunkView/ChunkDataView.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 9/28/99 9:48a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. // ChunkDataView.cpp : implementation file
  36. //
  37. #include "stdafx.h"
  38. #include "ChunkView.h"
  39. #include "ChunkDataView.h"
  40. #include "ChunkViewDoc.h"
  41. #include "ChunkFileImage.h"
  42. #include "HexToString.h"
  43. #include <assert.h>
  44. #ifdef _DEBUG
  45. #define new DEBUG_NEW
  46. #undef THIS_FILE
  47. static char THIS_FILE[] = __FILE__;
  48. #endif
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CChunkDataView
  51. IMPLEMENT_DYNCREATE(CChunkDataView, CListView)
  52. CChunkDataView::CChunkDataView() :
  53. WordSize(WORD_SIZE_BYTE),
  54. DisplayMode(DISPLAY_MODE_HEX)
  55. {
  56. }
  57. CChunkDataView::~CChunkDataView()
  58. {
  59. }
  60. BEGIN_MESSAGE_MAP(CChunkDataView, CListView)
  61. //{{AFX_MSG_MAP(CChunkDataView)
  62. // NOTE - the ClassWizard will add and remove mapping macros here.
  63. //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CChunkDataView drawing
  67. void CChunkDataView::OnDraw(CDC* pDC)
  68. {
  69. CDocument* pDoc = GetDocument();
  70. // TODO: add draw code here
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CChunkDataView diagnostics
  74. #ifdef _DEBUG
  75. void CChunkDataView::AssertValid() const
  76. {
  77. CListView::AssertValid();
  78. }
  79. void CChunkDataView::Dump(CDumpContext& dc) const
  80. {
  81. CListView::Dump(dc);
  82. }
  83. #endif //_DEBUG
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CChunkDataView message handlers
  86. void CChunkDataView::OnInitialUpdate()
  87. {
  88. CListView::OnInitialUpdate();
  89. CListCtrl &list = GetListCtrl();
  90. long flags = list.GetStyle();
  91. flags |= LVS_REPORT;
  92. SetWindowLong(list.GetSafeHwnd(), GWL_STYLE, flags);
  93. //list.SetFont ();
  94. ::SendMessage (list, WM_SETFONT, (WPARAM)GetStockObject (ANSI_FIXED_FONT), 0L);
  95. }
  96. void CChunkDataView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  97. {
  98. if (this == pSender) return;
  99. // Get the document and currently selected chunk
  100. CListCtrl &list = GetListCtrl();
  101. CChunkViewDoc * doc= (CChunkViewDoc *)GetDocument();
  102. const ChunkImageClass * chunk = doc->Get_Cur_Chunk();
  103. // Reset the list
  104. list.DeleteAllItems();
  105. // Rebuild the list view
  106. if (chunk != NULL) {
  107. Display_Chunk(chunk);
  108. }
  109. }
  110. void CChunkDataView::Display_Chunk(const ChunkImageClass * chunk)
  111. {
  112. CListCtrl &list = GetListCtrl();
  113. CChunkViewDoc * doc= (CChunkViewDoc *)GetDocument();
  114. if (chunk->Get_Data() == NULL) {
  115. Display_Chunk_Sub_Chunks(chunk);
  116. } else {
  117. switch (DisplayMode) {
  118. case DISPLAY_MODE_HEX:
  119. Display_Chunk_Hex(chunk);
  120. break;
  121. case DISPLAY_MODE_MICROCHUNKS:
  122. Display_Chunk_Micro_Chunks(chunk);
  123. break;
  124. };
  125. }
  126. }
  127. void CChunkDataView::Display_Chunk_Sub_Chunks(const ChunkImageClass * chunk)
  128. {
  129. char _buf[256];
  130. Reset_Columns();
  131. CListCtrl &list = GetListCtrl();
  132. static LV_COLUMN IDColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 160, "Chunk ID", 0,0 };
  133. static LV_COLUMN LengthColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 160, "Chunk Length", 0,0 };
  134. list.InsertColumn(0, &IDColumn);
  135. list.InsertColumn(1, &LengthColumn);
  136. for (int i=0; i<chunk->Get_Child_Count(); i++) {
  137. const ChunkImageClass * child = chunk->Get_Child(i);
  138. sprintf(_buf,"0x%08X",child->Get_ID());
  139. int list_item = list.InsertItem(i, _buf);
  140. sprintf(_buf,"0x%08X",child->Get_Length());
  141. list.SetItemText(list_item, 1, _buf);
  142. }
  143. }
  144. void CChunkDataView::Display_Chunk_Hex(const ChunkImageClass * chunk)
  145. {
  146. Reset_Columns();
  147. CListCtrl &list = GetListCtrl();
  148. static LV_COLUMN HexColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 750, "Hex Dump", 0,0 };
  149. list.InsertColumn(0, &HexColumn);
  150. HexToStringClass * hexconverter = Create_Hex_Converter(chunk->Get_Data(),chunk->Get_Length());
  151. assert(hexconverter != NULL);
  152. int rowcounter = 0;
  153. while (!hexconverter->Is_Done()) {
  154. list.InsertItem(rowcounter++,hexconverter->Get_Next_Line());
  155. }
  156. Destroy_Hex_Converter(hexconverter);
  157. }
  158. void CChunkDataView::Display_Chunk_Micro_Chunks(const ChunkImageClass * chunk)
  159. {
  160. Reset_Columns();
  161. CListCtrl &list = GetListCtrl();
  162. static LV_COLUMN IDColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 48, "ID", 0,0 };
  163. static LV_COLUMN LengthColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 48, "Size", 0,0 };
  164. static LV_COLUMN DataColumn = { LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_LEFT, 750, "Micro Chunk Data", 0,0 };
  165. list.InsertColumn(0, &IDColumn);
  166. list.InsertColumn(1, &LengthColumn);
  167. list.InsertColumn(2, &DataColumn);
  168. int rowcounter = 0;
  169. const uint8 * workptr = chunk->Get_Data();
  170. static char _buf[256];
  171. while (workptr < chunk->Get_Data() + chunk->Get_Length()) {
  172. uint8 micro_id = *workptr++;
  173. uint8 micro_size = *workptr++;
  174. // Add a line for the id
  175. CString tmp_string;
  176. tmp_string.Format("%02x",micro_id);
  177. int list_item = list.InsertItem(rowcounter++, tmp_string);
  178. // Set the size (second column)
  179. tmp_string.Format("%02x",micro_size);
  180. list.SetItemText(list_item, 1, tmp_string);
  181. // Set the first line of hex data
  182. HexToStringClass * hexconverter = Create_Hex_Converter(workptr,micro_size);
  183. tmp_string = hexconverter->Get_Next_Line();
  184. list.SetItemText(list_item, 2, tmp_string);
  185. while (!hexconverter->Is_Done()) {
  186. tmp_string = hexconverter->Get_Next_Line();
  187. list_item = list.InsertItem(rowcounter++, "");
  188. list.SetItemText(list_item, 2, tmp_string);
  189. }
  190. workptr += micro_size;
  191. Destroy_Hex_Converter(hexconverter);
  192. }
  193. }
  194. void CChunkDataView::Reset_Columns(void)
  195. {
  196. CListCtrl &list = GetListCtrl();
  197. BOOL hascolumns = TRUE;
  198. while (hascolumns) {
  199. hascolumns = list.DeleteColumn(0);
  200. }
  201. }
  202. HexToStringClass * CChunkDataView::Create_Hex_Converter(const uint8 * data,const uint32 size)
  203. {
  204. HexToStringClass * hexconv = NULL;
  205. switch(WordSize) {
  206. case WORD_SIZE_LONG:
  207. hexconv = new HexToStringLongClass(data,size);
  208. break;
  209. case WORD_SIZE_SHORT:
  210. hexconv = new HexToStringShortClass(data,size);
  211. break;
  212. default:
  213. hexconv = new HexToStringByteClass(data,size);
  214. break;
  215. }
  216. return hexconv;
  217. }
  218. void CChunkDataView::Destroy_Hex_Converter(HexToStringClass * hexconv)
  219. {
  220. assert(hexconv != NULL);
  221. delete hexconv;
  222. }