forked from floaterxk/DatDefragAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTree.cpp
219 lines (166 loc) · 3.93 KB
/
BTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "StdAfx.h"
#include "BTree.h"
#include "BlockLoader.h"
BTree::BTree(BlockLoader *pBlockLoader)
{
m_pBlockLoader = pBlockLoader;
m_TreePos = -1;
m_bLeaf = TRUE;
ZeroMemory(&m_TreeData, sizeof(m_TreeData));
ZeroMemory(&m_pBranches, sizeof(m_pBranches));
}
BTree::~BTree()
{
for (DWORD i = 0; i < 0x3E; i++)
{
if (m_pBranches[i])
delete m_pBranches[i];
}
}
BOOL BTree::Load(DWORD TreePos, float ProgressBase, float ProgressDelta)
{
m_TreePos = TreePos;
if (!m_pBlockLoader->Load_Data(&m_TreeData, sizeof(m_TreeData) - sizeof(DWORD), TreePos))
{
Output("Failed loading BTree @ 0x%08X\r\n", TreePos);
return FALSE;
}
if (!m_TreeData.m_Branches[0])
{
m_bLeaf = TRUE;
}
else
{
m_bLeaf = FALSE;
// Load Children
DWORD BranchCount = m_TreeData.m_EntryCount + 1;
float DeltaStep = ProgressDelta / BranchCount;
for (DWORD i = 0; i < BranchCount; i++)
{
m_pBranches[i] = new BTree(m_pBlockLoader);
if (!m_pBranches[i]->Load(m_TreeData.m_Branches[i], ProgressBase, DeltaStep))
return FALSE;
if (g_bQuit)
return FALSE;
ProgressBase += DeltaStep;
UpdateProgressBar(ProgressBase);
}
}
return TRUE;
}
DWORD BTree::GetTotalEntries()
{
DWORD TotalEntries = m_TreeData.m_EntryCount;
if (!m_bLeaf)
{
DWORD Branches = m_TreeData.m_EntryCount + 1;
for (UINT i = 0; i < Branches; i++)
{
if (m_pBranches[i])
TotalEntries += m_pBranches[i]->GetTotalEntries();
}
}
return TotalEntries;
}
DWORD BTree::GetTotalBranches()
{
if (m_bLeaf)
return 0;
DWORD Branches = m_TreeData.m_EntryCount + 1;
DWORD TotalBranches = Branches;
for (UINT i = 0; i < Branches; i++)
{
if (m_pBranches[i])
TotalBranches += m_pBranches[i]->GetTotalBranches();
}
return TotalBranches;
}
BYTE* BTree::NodeDataPtr()
{
return((BYTE *)&m_TreeData.m_Branches);
}
DWORD BTree::NodeDataSize()
{
return(sizeof(TreeData) - sizeof(DWORD));
}
DWORD BTree::BlocksNeededForNode()
{
return(m_pBlockLoader->BlocksNeededForSize(NodeDataSize()));
}
BOOL BTree::AssignNodePositions(DWORD *pdwOffset)
{
// Assign target file offsets for nodes.
DWORD NodeBlocksSize = BlocksNeededForNode() * m_pBlockLoader->GetBlockSize();
m_TargetTreePos = *pdwOffset;
*pdwOffset = m_TargetTreePos + NodeBlocksSize;
if (!m_bLeaf)
{
DWORD BranchCount = m_TreeData.m_EntryCount + 1;
for (UINT i = 0; i < BranchCount; i++)
{
m_TreeData.m_Branches[i] = *pdwOffset;
if (!m_pBranches[i]->AssignNodePositions(pdwOffset))
return FALSE;
}
}
return TRUE;
}
BOOL BTree::CopyFiles(float ProgressBase, float ProgressDelta)
{
if (g_bQuit)
return FALSE;
DWORD EntryCount = m_TreeData.m_EntryCount;
DWORD BranchCount = m_TreeData.m_EntryCount + 1;
float DeltaStep = ProgressDelta / BranchCount;
for (UINT i = 0; i < EntryCount; i++)
{
if (!m_bLeaf)
{
if (m_pBranches[i])
{
if (!m_pBranches[i]->CopyFiles(ProgressBase, DeltaStep))
return FALSE;
}
else
return FALSE;
ProgressBase += DeltaStep;
UpdateProgressBar(ProgressBase);
}
// Copy this file.
DWORD dwTargetOffset = m_pBlockLoader->GetTargetBlockOffset();
if (!m_pBlockLoader->CopyBlockChain(m_TreeData.m_Entries[i].m_BlockHead))
{
return FALSE;
}
m_TreeData.m_Entries[i].m_BlockHead = dwTargetOffset;
}
if (!m_bLeaf)
{
if (m_pBranches[EntryCount])
m_pBranches[EntryCount]->CopyFiles(ProgressBase, DeltaStep);
else
return FALSE;
}
ProgressBase += DeltaStep;
UpdateProgressBar(ProgressBase);
return TRUE;
}
BOOL BTree::CopyTreeData(float ProgressBase, float ProgressDelta)
{
if (g_bQuit)
return FALSE;
m_pBlockLoader->CopyDataAsBlocks(NodeDataPtr(), NodeDataSize());
if (!m_bLeaf)
{
DWORD BranchCount = m_TreeData.m_EntryCount + 1;
float DeltaStep = ProgressDelta / BranchCount;
for (UINT i = 0; i < BranchCount; i++)
{
if (!m_pBranches[i]->CopyTreeData(ProgressBase, DeltaStep))
return FALSE;
ProgressBase += DeltaStep;
UpdateProgressBar(ProgressBase);
}
}
return TRUE;
}