Skip to content

Commit

Permalink
Step 02. Timer & File - Virtual File / String
Browse files Browse the repository at this point in the history
- system/FileSystem
- system/VFS
- utils/String
  • Loading branch information
adunStudio committed Feb 1, 2018
1 parent 8a14cc4 commit 78e41ce
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Sunny-Core/Sunny-Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<ClCompile Include="directx\BufferLayout.cpp" />
<ClCompile Include="directx\Context.cpp" />
<ClCompile Include="directx\IndexBuffer.cpp" />
<ClCompile Include="directx\Shader.cpp" />
<ClCompile Include="directx\ShaderResource.cpp" />
<ClCompile Include="directx\ShaderUniform.cpp" />
<ClCompile Include="directx\VertexArray.cpp" />
<ClCompile Include="directx\VertexBuffer.cpp" />
<ClCompile Include="main.cpp" />
Expand All @@ -34,6 +37,8 @@
<ClCompile Include="maths\vec3.cpp" />
<ClCompile Include="maths\vec4.cpp" />
<ClCompile Include="system\FileSystem.cpp" />
<ClCompile Include="system\VFS.cpp" />
<ClCompile Include="utils\String.cpp" />
<ClCompile Include="utils\Timer.cpp" />
</ItemGroup>
<ItemGroup>
Expand All @@ -43,6 +48,9 @@
<ClInclude Include="directx\Common.h" />
<ClInclude Include="directx\Context.h" />
<ClInclude Include="directx\IndexBuffer.h" />
<ClInclude Include="directx\Shader.h" />
<ClInclude Include="directx\ShaderResource.h" />
<ClInclude Include="directx\ShaderUniform.h" />
<ClInclude Include="directx\VertexArray.h" />
<ClInclude Include="directx\VertexBuffer.h" />
<ClInclude Include="maths\AABB.h" />
Expand All @@ -55,7 +63,11 @@
<ClInclude Include="maths\vec3.h" />
<ClInclude Include="maths\vec4.h" />
<ClInclude Include="sunny.h" />
<ClInclude Include="aa2.h" />
<ClInclude Include="aa.h" />
<ClInclude Include="system\FileSystem.h" />
<ClInclude Include="system\VFS.h" />
<ClInclude Include="utils\String.h" />
<ClInclude Include="utils\Timer.h" />
<ClInclude Include="utils\Timestep.h" />
</ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions Sunny-Core/Sunny-Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@
<ClCompile Include="directx\VertexArray.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="directx\Shader.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="directx\ShaderUniform.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="directx\ShaderResource.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="utils\String.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
<ClCompile Include="system\VFS.cpp">
<Filter>소스 파일</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="app\Application.h">
Expand Down Expand Up @@ -128,5 +143,26 @@
<ClInclude Include="directx\VertexArray.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="directx\Shader.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="directx\ShaderUniform.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="directx\ShaderResource.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="utils\String.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="system\VFS.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="aa2.h">
<Filter>헤더 파일</Filter>
</ClInclude>
<ClInclude Include="aa.h">
<Filter>헤더 파일</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Sunny-Core/app/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sunny
Application* Application::s_instance = nullptr;

Application::Application(const std::string& name, const WindowProperties& properties)
: m_name(name), m_properties(properties), m_running(false), m_paused(false)
: m_name(name), m_properties(properties), m_running(false), m_paused(false)
{
s_instance = this;
}
Expand Down
43 changes: 40 additions & 3 deletions Sunny-Core/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
#include <iostream>

#include "app/Application.h"
#include "system/VFS.h"

int main()
{
std::cout << "Hello, Sunny Project!" << std::endl;
//std::cout << "Hello, Sunny Project!" << std::endl;

sunny::Application game("Test", {800, 600, false, false});
//sunny::Application game("Test", {800, 600, false, false});

game.Start();
//game.Start();

// std::string a = "dd// dd ";

//std::cout << sunny::utils::FindToken(a, "dd");// << std::endl;

//const char* ad = "SDfsaaaaaaaaaaaaaaaaaad";

//std::string b = std::string(ad, 5);

//std::cout << b;// << std::endl;
sunny::VFS::Init();

const std::string path = "sunny.h";

std::string a = sunny::VFS::Get()->ReadTextFile(path);
unsigned char* a2 = sunny::VFS::Get()->ReadFile(path);
std::cout << a << std::endl;
std::cout << a2 << std::endl;

if (! sunny::VFS::Get()->WriteFile("aa.h", a2) )
{
std::cout << "쓰기 실패1" << std::endl;
}
else
{
std::cout << "쓰기 성공1" << std::endl;
}

if (!sunny::VFS::Get()->WriteTextFile("aa2.h", a))
{
std::cout << "쓰기 실패2" << std::endl;
}
else
{
std::cout << "쓰기 성공2" << std::endl;
}

return 0;
}
14 changes: 7 additions & 7 deletions Sunny-Core/system/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace sunny
}

/* Read */
char* FileSystem::ReadFile(const std::string& path)
unsigned char* FileSystem::ReadFile(const std::string& path)
{
long length = GetFileSize(path);

Expand All @@ -38,15 +38,15 @@ namespace sunny
char* buffer = new char[length];

if(ReadFile(path, buffer, length))
return buffer;
return (unsigned char*)(buffer);

delete[] buffer;

return nullptr;
}


bool FileSystem::ReadFile(const std::string& path, char* buffer, long length)
bool FileSystem::ReadFile(const std::string& path, void* buffer, long length)
{
std::ifstream file(path, std::ifstream::in | std::ifstream::binary);

Expand All @@ -59,8 +59,8 @@ namespace sunny
length = file.tellg();

file.seekg(0, file.beg);

file.read(buffer, length);
file.read(static_cast<char*>(buffer), length);

file.close();

Expand All @@ -80,15 +80,15 @@ namespace sunny
}

/* Write */
bool FileSystem::WriteFile(const std::string& path, char* buffer, bool overwrite)
bool FileSystem::WriteFile(const std::string& path, void* buffer, bool overwrite)
{

if(!overwrite && FileExists(path))
return false;

std::ofstream file(path.c_str(), std::ifstream::binary);

file.write(buffer, strlen(buffer));
file.write(static_cast<char*>(buffer), strlen(static_cast<char*>(buffer)));

file.close();

Expand Down
6 changes: 3 additions & 3 deletions Sunny-Core/system/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ namespace sunny
static long GetFileSize(const std::string& path);

// 파일 일기
static char* ReadFile(const std::string& path);
static bool ReadFile(const std::string& path, char* buffer, long length = -1);
static unsigned char* ReadFile(const std::string& path);
static bool ReadFile(const std::string& path, void* buffer, long length = -1);
static std::string ReadTextFile(const std::string& path);

// 파일 쓰기
static bool WriteFile(const std::string& path, char* buffer, bool overwrite = true);
static bool WriteFile(const std::string& path, void* buffer, bool overwrite = true);
static bool WriteTextFile(const std::string& path, const std::string& text, bool overwrite = true);
};
}
Expand Down
114 changes: 114 additions & 0 deletions Sunny-Core/system/VFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "VFS.h"

namespace sunny
{
VFS* VFS::s_instance = nullptr;

void VFS::Init()
{
s_instance = new VFS();
}

void VFS::Shutdown()
{
delete s_instance;
}

void VFS::Mount(const std::string& virtualPath, const std::string& physicalPath)
{
if (!s_instance)
{
// Debug System
}

m_mountPoints[virtualPath].push_back(physicalPath);
}

void VFS::UnMount(const std::string& path)
{
if (!s_instance)
{
// Debug System
}

m_mountPoints[path].clear();
}

bool VFS::ResolvePhysicalPath(const std::string& path, std::string& outPhysicalPath)
{
if (path[0] != '/')
{
outPhysicalPath = path;
return system::FileSystem::FileExists(path);
}

std::vector<std::string> dirs = utils::SplitString(path, '/');

const std::string& virtualDir = dirs.front();

if (m_mountPoints.find(virtualDir) == m_mountPoints.end() || m_mountPoints[virtualDir].empty())
return false;

std::string remainder = path.substr(virtualDir.size() + 1, path.size() - virtualDir.size());

for (const std::string& physicalPath : m_mountPoints[virtualDir])
{
std::string path = physicalPath + remainder;
if (system::FileSystem::FileExists(path))
{
outPhysicalPath = path;
return true;
}
}

return false;
}

unsigned char* VFS::ReadFile(const std::string& path)
{
if (!s_instance)
{
// Debug System
}
std::string physicalPath;

return ResolvePhysicalPath(path, physicalPath) ? system::FileSystem::ReadFile(physicalPath) : nullptr;
}

std::string VFS::ReadTextFile(const std::string& path)
{
if (!s_instance)
{
// Debug System
}

std::string physicalPath;

return ResolvePhysicalPath(path, physicalPath) ? system::FileSystem::ReadTextFile(physicalPath) : nullptr;
}

bool VFS::WriteFile(const std::string& path, unsigned char* buffer)
{
if (!s_instance)
{
// Debug System
}

std::string physicalPath;

return ResolvePhysicalPath(path, physicalPath) ? system::FileSystem::WriteFile(physicalPath, buffer) : nullptr;
}

bool VFS::WriteTextFile(const std::string& path, const std::string& text)
{
if (!s_instance)
{
// Debug System
}

std::string physicalPath;

return ResolvePhysicalPath(path, physicalPath) ? system::FileSystem::WriteTextFile(physicalPath, text) : nullptr;
}

}
35 changes: 35 additions & 0 deletions Sunny-Core/system/VFS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "../sunny.h"
#include "../utils/String.h"
#include "FileSystem.h"

namespace sunny
{
class VFS
{
private:
static VFS* s_instance;

private:
std::unordered_map<std::string, std::vector<std::string>> m_mountPoints;

public:
void Mount(const std::string& virtualPath, const std::string& physicalPath);
void UnMount(const std::string& path);

bool ResolvePhysicalPath(const std::string& path, std::string& outPhysicalPath);

unsigned char* ReadFile(const std::string& path);
std::string ReadTextFile(const std::string& path);

bool WriteFile(const std::string& path, unsigned char* buffer);
bool WriteTextFile(const std::string& path, const std::string& text);

public:
static void Init();
static void Shutdown();

inline static VFS* Get() { return s_instance; }
};
}
Loading

0 comments on commit 78e41ce

Please sign in to comment.