File: Filesystem.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (170 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2015 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"
#include "Filesystem.h"

#include "ps/CLogger.h"
#include "ps/Profile.h"

#include "lib/res/h_mgr.h"	// h_reload
#include "lib/sysdep/dir_watch.h"
#include "lib/utf8.h"


PIVFS g_VFS;

static std::vector<std::pair<FileReloadFunc, void*> > g_ReloadFuncs;

bool VfsFileExists(const VfsPath& pathname)
{
	return g_VFS->GetFileInfo(pathname, 0) == INFO::OK;
}

bool VfsDirectoryExists(const VfsPath& pathname)
{
	ENSURE(pathname.IsDirectory());
	return g_VFS->GetDirectoryEntries(pathname, NULL, NULL) == INFO::OK;
}

void RegisterFileReloadFunc(FileReloadFunc func, void* obj)
{
	g_ReloadFuncs.emplace_back(func, obj);
}

void UnregisterFileReloadFunc(FileReloadFunc func, void* obj)
{
	g_ReloadFuncs.erase(std::remove(g_ReloadFuncs.begin(), g_ReloadFuncs.end(), std::make_pair(func, obj)));
}

// try to skip unnecessary work by ignoring uninteresting notifications.
static bool CanIgnore(const DirWatchNotification& notification)
{
	// ignore directories
	const OsPath& pathname = notification.Pathname();
	if(pathname.IsDirectory())
		return true;

	// ignore uninteresting file types (e.g. temp files, or the
	// hundreds of XMB files that are generated from XML)
	const OsPath extension = pathname.Extension();
	const wchar_t* extensionsToIgnore[] = { L".xmb", L".tmp" };
	for(size_t i = 0; i < ARRAY_SIZE(extensionsToIgnore); i++)
	{
		if(extension == extensionsToIgnore[i])
			return true;
	}

	return false;
}

Status ReloadChangedFiles()
{
	PROFILE3("hotload");

	std::vector<DirWatchNotification> notifications;
	RETURN_STATUS_IF_ERR(dir_watch_Poll(notifications));
	for(size_t i = 0; i < notifications.size(); i++)
	{
		if(!CanIgnore(notifications[i]))
		{
			VfsPath pathname;
			RETURN_STATUS_IF_ERR(g_VFS->GetVirtualPath(notifications[i].Pathname(), pathname));
			RETURN_STATUS_IF_ERR(g_VFS->RemoveFile(pathname));
			RETURN_STATUS_IF_ERR(g_VFS->RepopulateDirectory(pathname.Parent()/""));

			// Tell each hotloadable system about this file change:

			for (size_t j = 0; j < g_ReloadFuncs.size(); ++j)
				g_ReloadFuncs[j].first(g_ReloadFuncs[j].second, pathname);

			RETURN_STATUS_IF_ERR(h_reload(g_VFS, pathname));
		}
	}
	return INFO::OK;
}

std::wstring GetWstringFromWpath(const fs::wpath& path)
{
#if BOOST_FILESYSTEM_VERSION == 3
	return path.wstring();
#else
	return path.string();
#endif
}


CVFSFile::CVFSFile()
	: m_BufferSize(0)
{
}

CVFSFile::~CVFSFile()
{
}

PSRETURN CVFSFile::Load(const PIVFS& vfs, const VfsPath& filename, bool log /* = true */)
{
	// Load should never be called more than once, so complain
	if (m_Buffer)
	{
		DEBUG_WARN_ERR(ERR::LOGIC);
		return PSRETURN_CVFSFile_AlreadyLoaded;
	}

	Status ret = vfs->LoadFile(filename, m_Buffer, m_BufferSize);
	if (ret != INFO::OK)
	{
		if (log)
			LOGERROR("CVFSFile: file %s couldn't be opened (vfs_load: %lld)", filename.string8(), (long long)ret);
		m_Buffer.reset();
		m_BufferSize = 0;
		return PSRETURN_CVFSFile_LoadFailed;
	}

	return PSRETURN_OK;
}

const u8* CVFSFile::GetBuffer() const
{
	return m_Buffer.get();
}

size_t CVFSFile::GetBufferSize() const
{
	return m_BufferSize;
}

CStr CVFSFile::GetAsString() const
{
	return std::string((char*)GetBuffer(), GetBufferSize());
}

CStr CVFSFile::DecodeUTF8() const
{
	const u8* buffer = GetBuffer();

	// Detect if there's a UTF-8 BOM and strip it
	if (GetBufferSize() >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF)
	{
		return std::string(&buffer[3], buffer + GetBufferSize());
	}
	else
	{
		return std::string(buffer, buffer + GetBufferSize());
	}
}