forked from freezy/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinbinary.cpp
186 lines (136 loc) · 3.27 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "StdAfx.h"
PinBinary::PinBinary()
{
m_pdata = NULL;
}
PinBinary::~PinBinary()
{
if (m_pdata)
{
delete [] m_pdata;
}
}
void PinBinary::ReadFromFile(char *szfilename)
{
HANDLE hFile = CreateFile(szfilename,
GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ShowError("The file could not be opened.");
return;
}
if (m_pdata)
{
delete [] m_pdata;
}
m_cdata = GetFileSize(hFile, NULL);
m_pdata = new char[m_cdata];
DWORD read;
/*int fFoo =*/ ReadFile(hFile, m_pdata, m_cdata, &read, NULL);
/*fFoo =*/ CloseHandle(hFile);
lstrcpy(m_szPath, szfilename);
TitleFromFilename(szfilename, m_szName);
lstrcpy(m_szInternalName, m_szName);
CharLowerBuff(m_szInternalName, lstrlen(m_szInternalName));
}
bool PinBinary::WriteToFile(char *szfilename)
{
HANDLE hFile = CreateFile(szfilename,
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ShowError("The temporary file could not be written.");
return false;
}
DWORD write;
/*int foo =*/ WriteFile(hFile, m_pdata, m_cdata, &write, NULL);
/*foo =*/ GetLastError();
CloseHandle(hFile);
return true;
}
HRESULT PinBinary::SaveToStream(IStream *pstream)
{
//HRESULT hr;
BiffWriter bw(pstream, NULL, NULL);
bw.WriteString(FID(NAME), m_szName);
bw.WriteString(FID(INME), m_szInternalName);
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, NULL, version, NULL, NULL);
br.Load();
return S_OK;
}
BOOL PinBinary::LoadToken(int id, BiffReader *pbr)
{
if (id == FID(NAME))
{
pbr->GetString(m_szName);
}
else if (id == FID(INME))
{
pbr->GetString(m_szInternalName);
}
else if (id == FID(PATH))
{
pbr->GetString(m_szPath);
}
else if (id == FID(SIZE))
{
pbr->GetInt(&m_cdata);
m_pdata = new char[m_cdata];
}
else if (id == FID(DATA))
{
// Size must come before data, otherwise our structure won't be allocated
pbr->GetStruct(m_pdata, m_cdata);
}
return fTrue;
}
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()
{
HDC hdcScreen = GetDC(NULL);
LOGFONT lf;
lf.lfCharSet = DEFAULT_CHARSET;
lstrcpy(lf.lfFaceName, "");
lf.lfPitchAndFamily = 0;
EnumFontFamiliesEx(hdcScreen, &lf, (FONTENUMPROC)EnumFontFamExProc, (size_t)this, 0);
ReleaseDC(NULL, hdcScreen);
char szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
char *szEnd = szPath + lstrlen(szPath);
while (szEnd > szPath)
{
if (*szEnd == '\\')
{
break;
}
szEnd--;
}
*(szEnd+1) = '\0'; // Get rid of exe name
lstrcat(szPath, "VPTemp.ttf");
strcpy_s(m_szTempFile, sizeof(m_szTempFile), szPath);
WriteToFile(m_szTempFile);
/*const int fonts =*/ AddFontResource(m_szTempFile);
}
void PinFont::UnRegister()
{
/*const BOOL fFoo =*/ RemoveFontResource(m_szTempFile);
DeleteFile(m_szTempFile);
}