forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinbinary.cpp
164 lines (126 loc) · 3.48 KB
/
pinbinary.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
#include "stdafx.h"
PinBinary::PinBinary()
{
m_pdata = nullptr;
m_cdata = 0;
}
PinBinary::~PinBinary()
{
if (m_pdata)
{
delete[] m_pdata;
}
}
bool PinBinary::ReadFromFile(const string& szFileName)
{
const HANDLE hFile = CreateFile(szFileName.c_str(),
GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
const string text = "The file \"" + szFileName + "\" could not be opened.";
ShowError(text);
return false;
}
if (m_pdata)
{
delete[] m_pdata;
}
m_cdata = GetFileSize(hFile, nullptr);
m_pdata = new char[m_cdata];
DWORD read;
/*BOOL foo =*/ ReadFile(hFile, m_pdata, m_cdata, &read, nullptr);
/*foo =*/ CloseHandle(hFile);
m_szPath = szFileName;
m_szName = TitleFromFilename(szFileName);
return true;
}
bool PinBinary::WriteToFile(const string& szfilename)
{
const HANDLE hFile = CreateFile(szfilename.c_str(),
GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
const string bla = "The temporary file \"" + szfilename + "\" could not be written.";
ShowError(bla);
return false;
}
DWORD write;
/*int foo =*/ WriteFile(hFile, m_pdata, m_cdata, &write, nullptr);
/*foo =*/ GetLastError();
CloseHandle(hFile);
return true;
}
HRESULT PinBinary::SaveToStream(IStream *pstream)
{
BiffWriter bw(pstream, 0);
bw.WriteString(FID(NAME), m_szName);
bw.WriteString(FID(PATH), m_szPath);
bw.WriteInt(FID(SIZE), m_cdata);
bw.WriteStruct(FID(DATA), m_pdata, m_cdata);
bw.WriteTag(FID(ENDB));
return S_OK;
}
HRESULT PinBinary::LoadFromStream(IStream *pstream, int version)
{
BiffReader br(pstream, this, nullptr, version, 0, 0);
br.Load();
return S_OK;
}
bool PinBinary::LoadToken(const int id, BiffReader * const pbr)
{
switch(id)
{
case FID(NAME): pbr->GetString(m_szName); break;
case FID(PATH): pbr->GetString(m_szPath); break;
case FID(SIZE):
{
pbr->GetInt(m_cdata);
m_pdata = new char[m_cdata];
break;
}
// Size must come before data, otherwise our structure won't be allocated
case FID(DATA): pbr->GetStruct(m_pdata, m_cdata); break;
}
return true;
}
int CALLBACK EnumFontFamExProc(
ENUMLOGFONTEX *lpelfe, // logical-font data
NEWTEXTMETRICEX *lpntme, // physical-font data
DWORD FontType, // type of font
LPARAM lParam // application-defined data
)
{
return 1;
}
void PinFont::Register()
{
const HDC hdcScreen = GetDC(nullptr);
LOGFONT lf;
lf.lfCharSet = DEFAULT_CHARSET;
lstrcpy(lf.lfFaceName, "");
lf.lfPitchAndFamily = 0;
EnumFontFamiliesEx(hdcScreen, &lf, (FONTENUMPROC)EnumFontFamExProc, (size_t)this, 0);
ReleaseDC(nullptr, hdcScreen);
char szPath[MAXSTRING];
GetModuleFileName(nullptr, szPath, MAXSTRING);
char *szEnd = szPath + lstrlen(szPath);
while (szEnd > szPath)
{
if (*szEnd == PATH_SEPARATOR_CHAR)
break;
szEnd--;
}
*(szEnd + 1) = '\0'; // Get rid of exe name
static int tempFontNumber = -1;
tempFontNumber++;
m_szTempFile = szPath + "VPTemp"s + std::to_string(tempFontNumber) + ".ttf";
WriteToFile(m_szTempFile);
/*const int fonts =*/ AddFontResource(m_szTempFile.c_str());
}
void PinFont::UnRegister()
{
/*const BOOL foo =*/ RemoveFontResource(m_szTempFile.c_str());
DeleteFile(m_szTempFile.c_str());
}