summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/GlobalPreferences.cpp
blob: fb741b01ca90d1e50600131c4b91f02df59f7d71 (plain)
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
#include "UnityPrefix.h"

#include "Runtime/Utilities/PlayerPrefs.h"

#if UNITY_PLUGIN
#if UNITY_OSX
#include "OSXWebPluginUtility.h"
#endif // #if UNITY_OSX
#include "PlatformDependent/CommonWebPlugin/WebPluginUtility.h"
#endif // #if UNITY_PLUGIN

#include "GlobalPreferences.h"


#if UNITY_WIN
#include "PlatformDependent/Win/Registry.h"
#if UNITY_EDITOR
const char* kRegistryPath = "Software\\Unity\\UnityEditor";
#elif UNITY_STANDALONE
const char* kRegistryPath = "Software\\Unity\\UnityStandalone";
#else
const char* kRegistryPath = "Software\\Unity\\WebPlayer";
#endif
#endif // #if UNITY_WIN

#if UNITY_OSX
#if UNITY_EDITOR
const char* kPrefsAppID = "com.unity3d.UnityEditor";
#elif UNITY_STANDALONE
const char* kPrefsAppID = "com.unity3d.UnityStandalone";
#else
const char* kPrefsAppID = "com.unity3d.UnityWebPlayer";
#endif
#endif // #if UNITY_OSX

#if UNITY_LINUX
#include "PlatformDependent/Linux/XmlOptions.h"
#include "Runtime/Utilities/FileUtilities.h"
#include "Runtime/Utilities/PathNameUtility.h"

const char* kPrefsFileName = "global.prefs";
#endif // #if UNITY_LINUX


std::string GetGlobalPreference(const char *key)
{
	std::string result;

	#if UNITY_WIN && !UNITY_WINRT
	return registry::getString( kRegistryPath, key, "" );

	#elif UNITY_OSX
	CFStringRef cfPrefsAppID = CFStringCreateWithCString (NULL, kPrefsAppID, kCFStringEncodingASCII);
	CFStringRef cfKey = CFStringCreateWithCString (NULL, key, kCFStringEncodingASCII);
	CFStringRef val = (CFStringRef)CFPreferencesCopyAppValue (cfKey, cfPrefsAppID);
	if (val)
	{
		result = CFStringToString(val);
		CFRelease (val);
	}
	CFRelease (cfPrefsAppID);
	CFRelease (cfKey);

	#elif UNITY_LINUX
	std::string path = AppendPathName (GetUserConfigFolder (), kPrefsFileName);
	XmlOptions options;
	if (options.Load (path))
	{
		result = options.GetString (key, "");
	}

	#elif GAMERELEASE
	return PlayerPrefs::GetString (key);

	#else
	// TODO
	#endif
	return result;
}

bool GetGlobalBoolPreference(const char *key, bool defaultValue)
{
	std::string const pref = GetGlobalPreference (key);
	if (pref == "yes")
		return true;
	if (pref == "no")
		return false;
	return defaultValue;
}

void SetGlobalPreference(const char *key, std::string value)
{
	#if UNITY_WIN && !UNITY_WINRT
	registry::setString( kRegistryPath, key, value.c_str() );
	
	#elif UNITY_OSX
	CFStringRef cfPrefsAppID = CFStringCreateWithCString (NULL, kPrefsAppID, kCFStringEncodingASCII);
	CFStringRef cfKey = CFStringCreateWithCString (NULL, key, kCFStringEncodingASCII);
	CFStringRef cfValue = CFStringCreateWithCString (NULL, value.c_str(), kCFStringEncodingASCII);
	
	CFPreferencesSetAppValue( cfKey, cfValue, cfPrefsAppID );
	CFPreferencesAppSynchronize( cfPrefsAppID );

	#elif UNITY_LINUX
	std::string path = AppendPathName (GetUserConfigFolder (), kPrefsFileName);
	XmlOptions options;
	options.Load (path);
	options.SetString (key, value);
	options.Save (path);

	#elif GAMERELEASE
	PlayerPrefs::SetString (key, value);

	#else
	// TODO
	#endif
}

void SetGlobalBoolPreference(const char *key, bool value)
{
	SetGlobalPreference (key, value?"yes":"no");
}


#if WEBPLUG && !UNITY_PLUGIN
#include "Runtime/Misc/PlayerSettings.h"

std::string GetStrippedPlayerDomain ()
{
	std::string currentDomain = GetPlayerSettings().absoluteURL;
	if (currentDomain.find("http://") == 0 || currentDomain.find("https://") == 0)
	{
		//remove http://
		if (currentDomain.find("http://") == 0)
			currentDomain.erase(0, 7);
		else if (currentDomain.find("https://") == 0)
			currentDomain.erase(0, 8);
		
		//remove path
		std::string::size_type pos = currentDomain.find("/", 0);
		if (pos != std::string::npos)
			currentDomain.erase(currentDomain.begin() + pos, currentDomain.end());
		
		//remove port if present
		pos = currentDomain.find(":", 0);
		if (pos != std::string::npos)
			currentDomain.erase(currentDomain.begin() + pos, currentDomain.end());
	}
	return currentDomain;
}
#endif // #if WEBPLUG && !UNITY_PLUGIN