MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / loadG1

Function loadG1

src/OpenLoco/src/Graphics/Gfx.cpp:75–159  ·  view source on GitHub ↗

0x0044733C

Source from the content-addressed store, hash-verified

73
74 // 0x0044733C
75 void loadG1()
76 {
77 auto g1Path = Environment::getPath(Environment::PathId::g1);
78 std::ifstream stream(g1Path, std::ios::in | std::ios::binary);
79 if (!stream)
80 {
81 throw Exception::RuntimeError("Opening g1 file failed.");
82 }
83
84 G1Header header;
85 if (!readData(stream, header))
86 {
87 throw Exception::RuntimeError("Reading g1 file header failed.");
88 }
89
90 if (header.numEntries != G1ExpectedCount::kDisc)
91 {
92 if (header.numEntries == G1ExpectedCount::kSteam)
93 {
94 Logging::verbose("Got Steam G1.DAT variant, will fix elements automatically.");
95 }
96 else
97 {
98 Logging::warn("G1 element count doesn't match expected value:\nExpected {}; Got {}", G1ExpectedCount::kDisc, header.numEntries);
99 }
100 }
101
102 // Read element headers
103 auto elements32 = std::vector<G1Element32>(header.numEntries);
104 if (!readData(stream, elements32.data(), header.numEntries))
105 {
106 throw Exception::RuntimeError("Reading g1 element headers failed.");
107 }
108 auto elements = convertElements(elements32);
109
110 // Read element data
111 auto elementData = std::make_unique<std::byte[]>(header.totalSize);
112 if (!readData(stream, elementData.get(), header.totalSize))
113 {
114 throw Exception::RuntimeError("Reading g1 elements failed.");
115 }
116 stream.close();
117
118 // The steam G1.DAT is missing two localised tutorial icons, and a smaller font variant
119 // This code copies the closest variants into their place, and moves other elements accordingly
120 if (header.numEntries == G1ExpectedCount::kSteam)
121 {
122 // Temporarily convert offsets to absolute indexes
123 for (size_t i = 0; i < elements.size(); i++)
124 {
125 if (elements[i].hasFlags(G1ElementFlags::hasZoomSprites))
126 {
127 elements[i].zoomOffset = static_cast<int16_t>(i - elements[i].zoomOffset);
128 }
129 }
130
131 elements.resize(G1ExpectedCount::kDisc);
132

Callers 1

initialiseFunction · 0.85

Calls 12

getPathFunction · 0.85
verboseFunction · 0.85
warnFunction · 0.85
convertElementsFunction · 0.85
dataMethod · 0.45
getMethod · 0.45
closeMethod · 0.45
sizeMethod · 0.45
hasFlagsMethod · 0.45
resizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected