summaryrefslogtreecommitdiff
path: root/Runtime/File/ApplicationSpecificPersistentDataPath.cpp
blob: 5ec0255ceced8ed3cbc26f97e31346c2ad3f13db (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "UnityPrefix.h"
#include "Runtime/Misc/Player.h"
#include "Runtime/Misc/PlayerSettings.h"
#include "Runtime/Misc/SystemInfo.h"
#include "Runtime/Utilities/File.h"
#include "Runtime/Utilities/FileUtilities.h"

#include <ctype.h>

std::string GetAppDataPath()
{
#if UNITY_EDITOR
	return AppendPathName (File::GetCurrentDirectory (), "Assets");
#elif WEBPLUG || UNITY_PEPPER || UNITY_FLASH || UNITY_WEBGL
	std::string url = GetPlayerSettings().absoluteURL;
	size_t param = url.find('?');
	return DeleteLastPathNameComponent(url.substr(0, param));
#elif UNITY_WIN
	return SelectDataFolder ();
#elif UNITY_OSX
	return AppendPathName (GetApplicationPath (), "Contents");
#elif UNITY_WII
	return AppendPathName (GetApplicationFolder (), "/Data");
#elif UNITY_PS3
	return GetApplicationFolder();
#elif UNITY_XENON
	return "game:\\Media";
#elif UNITY_IPHONE
	return AppendPathName (GetApplicationFolder (), "Data");
#elif UNITY_ANDROID
	return GetApplicationPath();	// full path to the .apk
#elif UNITY_BB10
	return AppendPathName (GetApplicationFolder (), "app/native/Data");
#elif UNITY_TIZEN
	return AppendPathName (GetApplicationFolder (), "data");
#elif UNITY_LINUX
	return SelectDataFolder ();
#else
#error "Unknown platform"
#endif
}



#if SUPPORT_DIRECT_FILE_ACCESS

// We fucked this one up badly. In 3.4, this function would incorrectly strip 
// illegal characters from the file name, by changing the wrong indices.
// Now this a) does not solve the problem of illegal path names, and
// b) looks stupid. But, by fixing it, we'd lose all data cached by previous
// versions. So, what we do instead, is first check for the presence of
// a broken file name, and use that if it exists, and use the correct one otherwise.
// S.A. CachingManager.cpp.
void ConvertToLegalPathNameCorrectly(std::string& path)
{
	for (size_t i = path.size(); i > 0; --i)
	{
		char c = path[i-1];
		if (isalnum(c) || isspace(c))
			continue;
		path[i-1] = '_';
	}
}

void ConvertToLegalPathNameBroken(std::string& path)
{
	size_t size = path.size();
	for (size_t i = path.size(); i > 0; --i)
	{
		char c = path[i-1];
		if (isalnum(c) || isspace(c))
			continue;
		if (i < size)
			path[i] = '_';
	}
}

std::string GetApplicationSpecificDataPathAppendix(bool broken)
{
	if (UNITY_EMULATE_PERSISTENT_DATAPATH)
	{
		std::string companyName = GetPlayerSettings().companyName;
		std::string productName = GetPlayerSettings().productName;
		if (broken)
		{
			ConvertToLegalPathNameBroken(companyName);
			ConvertToLegalPathNameBroken(productName);
		}
		else
		{
#if UNITY_OSX && !UNITY_EDITOR
			// In the OS X standalone, return the bundle ID to match App Store requirements.
			return CFStringToString(CFBundleGetIdentifier(CFBundleGetMainBundle()));
#endif
			ConvertToLegalPathNameCorrectly(companyName);
			ConvertToLegalPathNameCorrectly(productName);
		}
		return AppendPathName(companyName, productName);
	}
	return "";
}

std::string GetPersistentDataPathApplicationSpecific()
{
	std::string dataPath = systeminfo::GetPersistentDataPath();
	#if !UNITY_WINRT
	if (dataPath.empty())
		return string();
	std::string brokenPath = AppendPathName(dataPath, GetApplicationSpecificDataPathAppendix(true)); 
	if (IsDirectoryCreated (brokenPath))
		dataPath = brokenPath;
	else
		dataPath = AppendPathName(dataPath, GetApplicationSpecificDataPathAppendix(false));
		
	if (!CreateDirectoryRecursive(dataPath))
		return string();
	#endif
	return dataPath;
}


std::string GetTemporaryCachePathApplicationSpecific()
{
	std::string cachePath = systeminfo::GetTemporaryCachePath();
	#if !UNITY_WINRT
	if (cachePath.empty())
		return string();
	std::string brokenPath = AppendPathName(cachePath, GetApplicationSpecificDataPathAppendix(true)); 
	if (IsDirectoryCreated (brokenPath))
		cachePath = brokenPath;
	else
		cachePath = AppendPathName(cachePath, GetApplicationSpecificDataPathAppendix(false));
	if (!CreateDirectoryRecursive(cachePath))
		return string();
	#endif
	return cachePath;
}

std::string GetStreamingAssetsPath()
{
#if (UNITY_EDITOR || UNITY_WIN || UNITY_WII || UNITY_LINUX) && !WEBPLUG
	return AppendPathName (GetAppDataPath(), "StreamingAssets");
#elif UNITY_OSX && !WEBPLUG
	return AppendPathName (SelectDataFolder(), "StreamingAssets");
#elif UNITY_IPHONE || UNITY_XENON || UNITY_PS3 || UNITY_BB10 || UNITY_TIZEN
	return AppendPathName (GetAppDataPath() , "Raw");
#elif UNITY_ANDROID
	return "jar:file://" + GetAppDataPath() + "!/assets";
#else
	ErrorString ("StreamingAssets is not available on this platform.");
	return "";
#endif
}

#else

std::string GetPersistentDataPathApplicationSpecific()
{
	return "";
}

std::string GetTemporaryCachePathApplicationSpecific()
{
	return "";
}

std::string GetStreamingAssetsPath()
{
	return "";
}
#endif