Skip to content

Commit eba7f2a

Browse files
authored
Merge pull request #2 from BlueAmulet/optimize
Optimize code
2 parents 49764ba + dc7770c commit eba7f2a

28 files changed

+258
-402
lines changed

EDF_Tools/CANM.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "include/tinyxml2.h"
1313
#include "include/half.hpp"
1414

15-
void CANM::Read(std::wstring path)
15+
void CANM::Read(const std::wstring& path)
1616
{
1717
std::ifstream file(path + L".canm", std::ios::binary | std::ios::ate | std::ios::in);
1818

@@ -23,7 +23,7 @@ void CANM::Read(std::wstring path)
2323
if (file.read(buffer.data(), size))
2424
{
2525
// create xml
26-
tinyxml2::XMLDocument xml = new tinyxml2::XMLDocument();
26+
tinyxml2::XMLDocument xml;
2727
xml.InsertFirstChild(xml.NewDeclaration());
2828
tinyxml2::XMLElement* xmlHeader = xml.NewElement("CANM");
2929
xml.InsertEndChild(xmlHeader);
@@ -46,7 +46,7 @@ void CANM::Read(std::wstring path)
4646
file.close();
4747
}
4848

49-
void CANM::ReadData(std::vector<char> buffer, tinyxml2::XMLElement* header)
49+
void CANM::ReadData(const std::vector<char>& buffer, tinyxml2::XMLElement* header)
5050
{
5151
int position = 0;
5252
unsigned char seg[4];
@@ -94,7 +94,7 @@ void CANM::ReadData(std::vector<char> buffer, tinyxml2::XMLElement* header)
9494
std::wcout << L"===>CANM parsing completed!\n";
9595
}
9696

97-
void CANM::ReadAnimationData(tinyxml2::XMLElement* header, std::vector<char> buffer)
97+
void CANM::ReadAnimationData(tinyxml2::XMLElement* header, const std::vector<char>& buffer)
9898
{
9999
tinyxml2::XMLElement* xmldata = header->InsertNewChildElement("AnmData");
100100
for (int i = 0; i < i_AnmDataCount; i++)
@@ -211,7 +211,7 @@ void CANM::ReadAnimationDataWriteKeyFrame(tinyxml2::XMLElement* node, int num)
211211
}
212212
}
213213

214-
void CANM::ReadAnimationPointData(tinyxml2::XMLElement* header, std::vector<char> buffer)
214+
void CANM::ReadAnimationPointData(tinyxml2::XMLElement* header, const std::vector<char>& buffer)
215215
{
216216
tinyxml2::XMLElement* xmldata = header->InsertNewChildElement("AnmKey");
217217
for (int i = 0; i < i_AnmPointCount; i++)
@@ -276,7 +276,7 @@ void CANM::ReadAnimationPointData(tinyxml2::XMLElement* header, std::vector<char
276276
// end
277277
}
278278

279-
void CANM::ReadBoneListData(tinyxml2::XMLElement* header, std::vector<char> buffer)
279+
void CANM::ReadBoneListData(tinyxml2::XMLElement* header, const std::vector<char>& buffer)
280280
{
281281
tinyxml2::XMLElement* xmlbone = header->InsertNewChildElement("BoneList");
282282
for (int i = 0; i < i_BoneCount; i++)
@@ -305,7 +305,7 @@ void CANM::ReadBoneListData(tinyxml2::XMLElement* header, std::vector<char> buff
305305
}
306306
}
307307

308-
CANMAnmKey CANM::ReadAnimationFrameData(std::vector<char> buffer, int pos)
308+
CANMAnmKey CANM::ReadAnimationFrameData(const std::vector<char>& buffer, int pos)
309309
{
310310
CANMAnmKey out;
311311

@@ -374,10 +374,7 @@ void CANM::Write(const std::wstring& path)
374374
/**/
375375
std::ofstream newFile(path + L".CANM", std::ios::binary | std::ios::out | std::ios::ate);
376376

377-
for (int i = 0; i < bytes.size(); i++)
378-
{
379-
newFile << bytes[i];
380-
}
377+
newFile.write(bytes.data(), bytes.size());
381378

382379
newFile.close();
383380

@@ -453,8 +450,7 @@ std::vector<char> CANM::WriteData(tinyxml2::XMLElement* Data)
453450
{
454451
int offset = kfbytes.size() + size_AnmPoint - v_AnmKey[i].pos;
455452
memcpy(&v_AnmKey[i].bytes[0x1C], &offset, 4U);
456-
for (size_t j = 0; j < v_AnmKey[i].kf[0].bytes.size(); j++)
457-
kfbytes.push_back(v_AnmKey[i].kf[0].bytes[j]);
453+
kfbytes.insert(kfbytes.end(), v_AnmKey[i].kf[0].bytes.begin(), v_AnmKey[i].kf[0].bytes.end());
458454
}
459455
std::wcout << L"\r" + std::to_wstring(i + 1);
460456
}
@@ -476,20 +472,14 @@ std::vector<char> CANM::WriteData(tinyxml2::XMLElement* Data)
476472
std::wcout << L"Write CANM animation keyframe......";
477473
for (size_t i = 0; i < v_AnmKey.size(); i++)
478474
{
479-
for (size_t j = 0; j < v_AnmKey[i].bytes.size(); j++)
480-
bytes.push_back(v_AnmKey[i].bytes[j]);
475+
bytes.insert(bytes.end(), v_AnmKey[i].bytes.begin(), v_AnmKey[i].bytes.end());
481476
//std::wcout << L"\r" + std::to_wstring(i + 1);
482477
}
483478
//std::wcout << L" Complete!\n";
484479
// write keyframe data
485480
//std::wcout << L"Write CANM animation keyframe data......";
486481
//float progress = kfbytes.size() / 100.0f;
487-
for (size_t i = 0; i < kfbytes.size(); i++)
488-
{
489-
bytes.push_back(kfbytes[i]);
490-
// progress is not shown because it is too slow to show it!
491-
//std::wcout << L"\r" + std::to_wstring((i + 1)/ progress) +L"\%";
492-
}
482+
bytes.insert(bytes.end(), kfbytes.begin(), kfbytes.end());
493483
std::wcout << L" Complete!\n";
494484
// write animation point header
495485
bytes[0x14] = 0x20;
@@ -509,22 +499,19 @@ std::vector<char> CANM::WriteData(tinyxml2::XMLElement* Data)
509499
{
510500
// need to save this location
511501
v_AnmData[i].pos = bytes.size();
512-
for (size_t j = 0; j < v_AnmData[i].bytes.size(); j++)
513-
bytes.push_back(v_AnmData[i].bytes[j]);
502+
bytes.insert(bytes.end(), v_AnmData[i].bytes.begin(), v_AnmData[i].bytes.end());
514503
//std::wcout << L"\r" + std::to_wstring(i + 1);
515504
}
516505
std::wcout << L" Complete!\n";
517506
// write bone data
518-
for (size_t i = 0; i < bdbytes.size(); i++)
519-
bytes.push_back(bdbytes[i]);
507+
bytes.insert(bytes.end(), bdbytes.begin(), bdbytes.end());
520508
// write animation data header
521509
memcpy(&bytes[0x8], &i_AnmDataCount, 4U);
522510
memcpy(&bytes[0xC], &i_AnmDataOffset, 4U);
523511

524512
// write bone list
525513
i_BoneOffset = bytes.size();
526-
for (size_t i = 0; i < blbytes.size(); i++)
527-
bytes.push_back(blbytes[i]);
514+
bytes.insert(bytes.end(), blbytes.begin(), blbytes.end());
528515
// write bone list header
529516
memcpy(&bytes[0x18], &i_BoneCount, 4U);
530517
memcpy(&bytes[0x1C], &i_BoneOffset, 4U);

EDF_Tools/CANM.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ struct CANMAnmData
3737
class CANM
3838
{
3939
public:
40-
void Read(std::wstring path);
41-
void ReadData(std::vector<char> buffer, tinyxml2::XMLElement* header);
40+
void Read(const std::wstring& path);
41+
void ReadData(const std::vector<char>& buffer, tinyxml2::XMLElement* header);
4242
// old
43-
void ReadAnimationPointData(tinyxml2::XMLElement* header, std::vector<char> buffer);
43+
void ReadAnimationPointData(tinyxml2::XMLElement* header, const std::vector<char>& buffer);
4444
// now
45-
void ReadAnimationData(tinyxml2::XMLElement* header, std::vector<char> buffer);
45+
void ReadAnimationData(tinyxml2::XMLElement* header, const std::vector<char>& buffer);
4646
void ReadAnimationDataWriteKeyFrame(tinyxml2::XMLElement* node, int num);
47-
void ReadBoneListData(tinyxml2::XMLElement* header, std::vector<char> buffer);
48-
CANMAnmKey ReadAnimationFrameData(std::vector<char> buffer, int pos);
47+
void ReadBoneListData(tinyxml2::XMLElement* header, const std::vector<char>& buffer);
48+
CANMAnmKey ReadAnimationFrameData(const std::vector<char>& buffer, int pos);
4949
// faster with the pointer version
5050
CANMAnmKey ReadAnimationFrameData(std::vector<char> *buf, int pos, int mask);
5151

0 commit comments

Comments
 (0)