• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PredatorCZ / HavokLib / 89

06 Nov 2025 01:09PM UTC coverage: 63.837% (-3.2%) from 67.014%
89

push

github

PredatorCZ
s

3354 of 5254 relevant lines covered (63.84%)

129082.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/source/hk_packfile.cpp
1
/*  Havok Format Library
2
    Copyright(C) 2016-2025 Lukas Cone
3

4
    This program is free software : you can redistribute it and / or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 3 of the License, or
7
    (at your option) any later version.
8

9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
    GNU General Public License for more details.
13

14
    You should have received a copy of the GNU General Public License
15
    along with this program.If not, see <https://www.gnu.org/licenses/>.
16
*/
17

18
#include "toolset.hpp"
19

20
#include "format_new.hpp"
21
#include "format_old.hpp"
22
#include "hklib/hk_rootlevelcontainer.hpp"
23
#include "internal/hk_internal_api.hpp"
24
#include "spike/except.hpp"
25
#include "spike/io/binreader.hpp"
26
#include "spike/io/binwritter.hpp"
27
#include "spike/master_printer.hpp"
28

29
IhkPackFile::Ptr IhkPackFile::Create(BinReaderRef_e rd,
906✔
30
                                     IhkPackFile *compendium) {
31
  struct {
32
    uint32 ID1, ID2;
33
    void NoSwap();
34
  } testerStruct;
35

36
  rd.Read(testerStruct);
906✔
37
  rd.Seek(0);
906✔
38

39
  if (testerStruct.ID1 == hkMagic1 && testerStruct.ID2 == hkMagic2) {
906✔
40
    auto hdr = std::make_unique<hkxHeader>();
906✔
41
    hdr->Load(rd);
906✔
42
    return hdr;
906✔
43
  } else if (testerStruct.ID2 == HK_HEADER_TAG) {
906✔
44
    auto hdr = std::make_unique<hkxNewHeader>();
×
45
    if (compendium) {
×
46
      hdr->SetCompendium(compendium);
×
47
    }
48
    hdr->Load(rd);
×
49
    return hdr;
×
50
  } else if (testerStruct.ID2 == HK_HEADER_TCM) {
×
51
    auto hdr = std::make_unique<hkCompendium>();
×
52
    hdr->Load(rd);
×
53
    return hdr;
×
54
  }
55

56
  throw es::InvalidHeaderError(testerStruct.ID1);
×
57
}
58

59
IhkPackFile::Ptr IhkPackFile::Create(BinReaderRef_e rd) {
906✔
60
  return Create(rd, nullptr);
906✔
61
}
62

63
IhkPackFile::Ptr IhkPackFile::Create(const std::string &fileName) {
906✔
64
  BinReader rd(fileName);
906✔
65

66
  return Create(rd);
1,812✔
67
}
906✔
68

69
const IhkVirtualClass *IhkPackFile::GetClass(const void *ptr) {
2,418✔
70
  VirtualClasses &classes = GetAllClasses();
2,418✔
71

72
  for (auto &c : classes) {
9,790✔
73
    if (c->GetPointer() == ptr)
9,406✔
74
      return c.get();
2,034✔
75
  }
76

77
  return nullptr;
384✔
78
}
79

80
IhkPackFile::VirtualClassesRef IhkPackFile::GetClasses(JenHash hash) {
2,709✔
81
  VirtualClasses &classes = GetAllClasses();
2,709✔
82
  VirtualClassesRef buffa;
2,709✔
83

84
  for (auto &c : classes) {
17,002✔
85
    auto cls = checked_deref_cast<const hkVirtualClass>(c.get());
14,293✔
86

87
    if (cls->HasHash(hash)) {
14,293✔
88
      buffa.push_back(c.get());
2,469✔
89
    }
90
  }
91

92
  return buffa;
2,709✔
93
}
×
94

95
void IhkPackFile::ToPackFile(const std::string &fileName, hkToolset toolset,
×
96
                             uint32 rule) {
97
  if (toolset == HKUNKVER || toolset > HK2014) {
×
98
    throw es::InvalidVersionError(toolset);
×
99
  }
100

101
  hkxHeaderlayout layout;
102
  layout.emptyBaseClassOptimization = rule & 0xf;
×
103
  layout.reusePaddingOptimization = (rule >> 4) & 0xf;
×
104
  layout.littleEndian = (rule >> 8) & 0xf;
×
105
  layout.bytesInPointer = (rule >> 12) & 0xf;
×
106

107
  if (layout.bytesInPointer > 4) {
×
108
    layout.bytesInPointer = 8;
×
109
  } else {
110
    layout.bytesInPointer = 4;
×
111
  }
112

113
  if (layout.littleEndian > 1) {
×
114
    printwarning(
×
115
        "[Havok] Unknown layout rule littleEndian, expected [0,1], got: "
116
        << layout.littleEndian << ". Handling as true.");
117
    layout.littleEndian = 1;
×
118
  }
119

120
  if (layout.emptyBaseClassOptimization > 1) {
×
121
    printwarning("[Havok] Unknown layout rule emptyBaseClassOptimization, "
×
122
                 "expected [0,1], got: "
123
                 << layout.emptyBaseClassOptimization << ". Handling as true.");
124
    layout.emptyBaseClassOptimization = 1;
×
125
  }
126

127
  if (layout.reusePaddingOptimization > 1) {
×
128
    printwarning("[Havok] Unknown layout rule reusePaddingOptimization, "
×
129
                 "expected [0,1], got: "
130
                 << layout.reusePaddingOptimization << ". Handling as true.");
131
    layout.reusePaddingOptimization = 1;
×
132
  }
133

134
  BinWritter wr(fileName);
×
135

136
  auto prop = xmlToolsetProps.at(toolset);
×
137

138
  hkxHeader hkHead;
×
139
  hkHead.layout = layout;
×
140
  hkHead.version = prop.version;
×
141
  hkHead.toolset = toolset;
×
142
  memcpy(hkHead.contentsVersion, prop.name, sizeof(hkHead.contentsVersion));
×
143
  hkHead.Save(wr, GetAllClasses());
×
144
}
145

146
const hkRootLevelContainer *IhkPackFile::GetRootLevelContainer() {
×
147
  return safe_deref_cast<const hkRootLevelContainer>(
×
148
      GetClasses(hkRootLevelContainer::GetHash())[0]);
×
149
}
150

151
void hkVirtualClass::Save(BinWritterRef, hkFixups &) const {}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc