-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.cpp
106 lines (90 loc) · 2.79 KB
/
prefs.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
// prefs.c
#include "stdafx.h"
#if __APPLE__
#include <Cocoa/Cocoa.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "prefs.h"
#include "music.h"
#include "soundfx.h"
#include "hiscore.h"
#include "keyselect.h"
#if __APPLE__
#define PREF_NAME(x) @x
typedef NSString* PrefKeyName;
#endif
#if _WIN32
#define PREF_NAME(x) x
typedef const char* PrefKeyName;
#endif
#if LINUX
#define PREF_NAME(x) x
typedef const char* PrefKeyName;
#endif
struct Preference
{
PrefKeyName keyName;
void* valuePtr;
unsigned int valueLength;
};
Preference prefList[] =
{
{ PREF_NAME("MusicOn"), &musicOn, sizeof(MBoolean ) },
{ PREF_NAME("SoundOn"), &soundOn, sizeof(MBoolean ) },
{ PREF_NAME("KeyBindings"), playerKeys, sizeof(playerKeys) },
{ PREF_NAME("HighScores"), scores, sizeof(scores ) },
{ PREF_NAME("BestCombo"), &best, sizeof(best ) }
};
void LoadPrefs()
{
for (Preference& pref: prefList)
{
#if __APPLE__
NSData* data = [[NSUserDefaults standardUserDefaults] dataForKey:pref.keyName];
if ([data length] == pref.valueLength)
{
memcpy(pref.valuePtr, [data bytes], pref.valueLength);
}
#endif
#if _WIN32
HKEY hKey;
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\CandyCrisis"), 0, KEY_QUERY_VALUE, &hKey))
{
DWORD dwType = REG_BINARY;
DWORD dwLength = pref.valueLength;
if ((ERROR_SUCCESS == RegQueryValueEx(hKey, pref.keyName, 0, &dwType, NULL, &dwLength))
&& dwType == REG_BINARY
&& dwLength == pref.valueLength)
{
RegQueryValueEx(hKey, pref.keyName, 0, &dwType, reinterpret_cast<BYTE*>(pref.valuePtr), &dwLength);
}
RegCloseKey(hKey);
}
#endif
}
}
void SavePrefs()
{
for (Preference& pref: prefList)
{
#if __APPLE__
[[NSUserDefaults standardUserDefaults]
setObject:[NSData dataWithBytes:pref.valuePtr length:pref.valueLength]
forKey:pref.keyName];
#endif
#if _WIN32
HKEY hKey;
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\CandyCrisis"), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL))
{
RegSetValueEx(hKey, pref.keyName, 0, REG_BINARY, reinterpret_cast<BYTE*>(pref.valuePtr), pref.valueLength);
RegCloseKey(hKey);
}
#endif
}
#if __APPLE__
[[NSUserDefaults standardUserDefaults] synchronize];
#endif
}