feat: finished adding winrt directory and file

This commit is contained in:
CT5
2026-01-26 17:07:12 +11:00
parent 15fa8a8f6a
commit aae6b3c89d
11 changed files with 328 additions and 45 deletions

View File

@@ -11,7 +11,7 @@ set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/)
add_subdirectory(projects/WinDurango.Common)
add_subdirectory(projects/WinDurango.Implementation.WinRT)
add_subdirectory(projects/WinDurango.Implementation.Native)
add_subdirectory(projects/WinDurango.WinRT)
#add_subdirectory(projects/WinDurango.WinRT)
add_subdirectory(projects/WinDurango.etwplus)
add_subdirectory(projects/WinDurango.KernelX)
add_subdirectory(projects/WinDurango.D3D11X)
@@ -20,7 +20,7 @@ add_custom_target(WinDurango ALL DEPENDS
WinDurango.Common
WinDurango.Implementation.WinRT
WinDurango.Implementation.Native
WinDurango.WinRT
#WinDurango.WinRT
WinDurango.etwplus
WinDurango.KernelX
WinDurango.D3D11X

View File

@@ -20,7 +20,7 @@ namespace wd::common::interfaces::storage {
virtual bool open() = 0;
virtual File* CreateFile(std::filesystem::path path) = 0; // todo maybe return stream type, todo can we use this in uwp context??? I forgor
virtual Directory* CreateDirectory(std::filesystem::path path) = 0;
virtual Directory* CreateFolder(std::filesystem::path path) = 0;
virtual std::filesystem::path dirpath() = 0;

View File

@@ -15,7 +15,7 @@ namespace wd::common::interfaces::storage {
virtual bool open() = 0;
virtual bool create() = 0;
virtual std::istream& read() = 0;
virtual std::string read() = 0;
virtual void operator<<(std::string data) = 0; // write
virtual bool close() = 0;

View File

@@ -17,6 +17,7 @@ add_compile_definitions(WDIMPL_API_EXPORTS)
add_library(WinDurango.Implementation.WinRT SHARED ${FILES})
target_include_directories(WinDurango.Implementation.WinRT PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/WinDurango.Implementation.WinRT>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
)

View File

@@ -3,10 +3,12 @@
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/base.h>
#include "WinDurango.h"
#include "WinDurango.Implementation.WinRT/WinDurangoWinRT.h"
#include "WinDurango.Common/interfaces/Storage/Directory.h"
#include "WinDurango.Implementation.WinRT/Interfaces/Storage/File.h"
using namespace winrt::Windows::Storage;
using namespace winrt;
/*
* I wonder if this is too confusing?
@@ -14,11 +16,11 @@ using namespace winrt::Windows::Storage;
namespace wd::impl::winrt::interfaces::storage {
class WDIMPL_API WinRTDirectory : public wd::common::interfaces::storage::Directory {
public:
WinRTDirectory(std::filesystem::path dirpath) : path(dirpath) {}
WinRTDirectory(std::filesystem::path dirpath) : path(dirpath), dir(nullptr) {}
virtual bool open() override;
virtual wd::common::interfaces::storage::File* CreateFile(std::filesystem::path path) override;
virtual wd::common::interfaces::storage::Directory* CreateDirectory(std::filesystem::path path) override;
virtual wd::common::interfaces::storage::Directory* CreateFolder(std::filesystem::path path) override;
virtual std::filesystem::path dirpath() override;
@@ -28,6 +30,6 @@ namespace wd::impl::winrt::interfaces::storage {
virtual bool copy(std::filesystem::path path) override;
private:
std::filesystem::path path;
StorageFolder* dir = nullptr;
StorageFolder dir;
};
}

View File

@@ -1,17 +1,24 @@
#pragma once
#include <istream>
#include <filesystem>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/base.h>
#include "WinDurango.Implementation.WinRT/WinDurangoWinRT.h"
#include "WinDurango.Common/Interfaces/Storage/File.h"
using namespace winrt::Windows::Storage;
using namespace winrt;
namespace wd::impl::winrt::interfaces::storage {
class WDIMPL_API WinRTFile : public wd::common::interfaces::storage::File {
public:
WinRTFile(std::filesystem::path filepath) : path(filepath) {}
WinRTFile(std::filesystem::path filepath) : path(filepath), file(nullptr) {}
virtual bool open() override;
virtual bool create() override;
virtual std::istream& read() override;
virtual std::string read() override;
virtual void operator<<(std::string data) override; // write
virtual bool close() override;
@@ -23,5 +30,6 @@ namespace wd::impl::winrt::interfaces::storage {
virtual bool copy(std::filesystem::path path) override;
private:
std::filesystem::path path;
StorageFile file;
};
}

View File

@@ -1,39 +1,180 @@
#include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h"
#include "Interfaces/Storage/Directory.h"
/*
* This is a Dir Class and all the funcs inside
* are specifically for managing this dir.
*/
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::open() {
StorageFolder sf = ApplicationData::Current().LocalFolder();
if (sf) {
dir = &sf;
return true;
if (dir != nullptr) {
return false;
}
try {
StorageFolder sf = ApplicationData::Current().LocalFolder();
for (const auto& part : path) {
hstring partStr = hstring(part.wstring());
sf = sf.GetFolderAsync(partStr).get();
}
if (sf) {
dir = sf;
return true;
}
} catch (const hresult_error& ex) {
return false;
}
return false;
}
wd::common::interfaces::storage::File* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFile(std::filesystem::path path) {
auto file = localFolder.CreateFileAsync(winrt::hstring(path.wstring()), winrt::Windows::Storage::CreationCollisionOption::FailIfExists).get();
return nullptr;
/*
* Todo:
* Add logging
*
* TargetPath is the filename btw
* any path will be ignored.
*/
wd::common::interfaces::storage::File* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFile(std::filesystem::path targetPath) {
if (dir == nullptr) {
return nullptr;
}
try {
auto file = dir.CreateFileAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists).get();
wd::impl::winrt::interfaces::storage::WinRTFile* fileRT = new wd::impl::winrt::interfaces::storage::WinRTFile(path / targetPath.filename());
return fileRT;
}
catch (const hresult_error& ex) {
return nullptr;
}
}
wd::common::interfaces::storage::Directory* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateDirectory(std::filesystem::path path) {
return nullptr;
/*
* TODO: Use Unique Pointers
*/
wd::common::interfaces::storage::Directory* wd::impl::winrt::interfaces::storage::WinRTDirectory::CreateFolder(std::filesystem::path targetPath) {
if (dir == nullptr) {
return nullptr;
}
try {
auto file = dir.CreateFolderAsync(hstring(targetPath.filename().wstring()), CreationCollisionOption::OpenIfExists).get();
wd::impl::winrt::interfaces::storage::WinRTDirectory* folderRT = new wd::impl::winrt::interfaces::storage::WinRTDirectory(path / targetPath.filename());
return folderRT;
}
catch (const hresult_error& ex) {
return nullptr;
}
}
std::filesystem::path wd::impl::winrt::interfaces::storage::WinRTDirectory::dirpath() {
return path;
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::rename(std::string) {
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::rename(std::string newName) {
if (dir == nullptr) {
return false;
}
try {
dir.RenameAsync(to_hstring(newName)).get();
path.replace_filename(newName);
return true;
}
catch (const hresult_error& ex) {
return false;
}
return false;
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::remove() {
if (dir == nullptr) {
return false;
}
try {
dir.DeleteAsync().get();
return true;
}
catch (const hresult_error& ex) {
return false;
}
return false;
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::move(std::filesystem::path path) {
bool MoveFolder(StorageFolder source, StorageFolder destinationContainer) {
try {
StorageFolder destinationFolder = destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get();
for (auto file : source.GetFilesAsync().get()) {
file.MoveAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get();
}
for (auto folder : source.GetFoldersAsync().get()) {
MoveFolder(folder, destinationFolder);
}
return true;
}
catch (const hresult_error& ex) {
return false;
}
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::move(std::filesystem::path targetPath) {
if (dir == nullptr) {
return false;
}
try {
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto& part : targetPath) {
hstring partStr = hstring(part.wstring());
target = target.GetFolderAsync(partStr).get();
}
if (MoveFolder(dir, target)) {
path = targetPath;
return true;
}
}
catch (const hresult_error& ex) {
return false;
}
return false;
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::copy(std::filesystem::path path) {
bool CopyFolder(StorageFolder source, StorageFolder destinationContainer) {
try {
StorageFolder destinationFolder = destinationContainer.CreateFolderAsync(source.Name(), CreationCollisionOption::OpenIfExists).get();
for (auto file : source.GetFilesAsync().get()) {
file.CopyAsync(destinationFolder, file.Name(), NameCollisionOption::GenerateUniqueName).get();
}
for (auto folder : source.GetFoldersAsync().get()) {
CopyFolder(folder, destinationFolder);
}
return true;
}
catch (const hresult_error& ex) {
return false;
}
}
bool wd::impl::winrt::interfaces::storage::WinRTDirectory::copy(std::filesystem::path targetPath) {
if (dir == nullptr) {
return false;
}
try {
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto& part : targetPath) {
hstring partStr = hstring(part.wstring());
target = target.GetFolderAsync(partStr).get();
}
if (CopyFolder(dir, target)) {
path = targetPath;
return true;
}
}
catch (const hresult_error& ex) {
return false;
}
return false;
}

View File

@@ -1,36 +1,89 @@
#include "WinDurango.Implementation.WinRT/interfaces/Storage/File.h"
#include "interfaces/Storage/File.h"
namespace wd::impl::winrt::interfaces::storage {
bool WinRTFile::open() {
if (file != nullptr) {
return false;
}
try {
StorageFolder sf = ApplicationData::Current().LocalFolder();
for (const auto& part : path.parent_path()) {
hstring partStr = hstring(part.wstring());
sf = sf.GetFolderAsync(partStr).get();
}
if (!sf) {
return false;
}
StorageFile npFile = sf.GetFileAsync(hstring(path.filename().wstring())).get();
file = npFile;
if (!npFile) {
return false;
}
} catch (const hresult_error& ex) {
return false;
}
return false;
}
bool WinRTFile::create() {
if (file != nullptr) {
return false;
}
try {
StorageFolder sf = ApplicationData::Current().LocalFolder();
for (const auto& part : path.parent_path()) {
hstring partStr = hstring(part.wstring());
sf = sf.GetFolderAsync(partStr).get();
}
if (!sf) {
return false;
}
file = sf.CreateFileAsync(hstring(path.filename().wstring()), CreationCollisionOption::OpenIfExists).get();
if (!file) {
return false;
}
return true;
} catch (const hresult_error& ex) {
return false;
}
return false;
}
/*
* From Stack Overflow
* Why did this take me 4 hours to figure out??
* btw these docs are pretty useful
* https://learn.microsoft.com/en-us/windows/uwp/get-started/fileio-learning-track
*/
struct MyIstream : std::streambuf {
public:
int_type underflow() override {
return traits_type::eof();
std::string WinRTFile::read() {
if (file == nullptr) {
return "Failed";
}
};
std::istream& WinRTFile::read() {
MyIstream nullp;
std::istream np(&nullp);
return np;
try {
return to_string(FileIO::ReadTextAsync(file).get());
} catch (const hresult_error& ex) {
return "Failed";
}
return "Failed";
}
void WinRTFile::operator<<(std::string data) {
if (file == nullptr) {
return;
}
try {
FileIO::WriteTextAsync(file, to_hstring(data)).get();
} catch (const hresult_error& ex) {
return;
}
}
bool WinRTFile::close() {
if (file == nullptr) {
return false;
}
file = nullptr;
return false;
}
@@ -38,19 +91,80 @@ namespace wd::impl::winrt::interfaces::storage {
return path;
}
bool WinRTFile::rename(std::string) {
bool WinRTFile::rename(std::string name) {
if (file == nullptr) {
return false;
}
try {
file.RenameAsync(to_hstring(name), NameCollisionOption::GenerateUniqueName).get();
path.replace_filename(name);
return true;
} catch (const hresult_error& ex) {
return false;
}
return false;
}
bool WinRTFile::remove() {
if (file == nullptr) {
return false;
}
try {
file.DeleteAsync().get();
return true;
} catch (const hresult_error& ex) {
return false;
}
return false;
}
bool WinRTFile::move(std::filesystem::path path) {
bool WinRTFile::move(std::filesystem::path targetPath) {
if (file == nullptr) {
return false;
}
try {
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto& part : targetPath.parent_path()) {
hstring partStr = hstring(part.wstring());
target = target.GetFolderAsync(partStr).get();
}
if (!target) {
return false;
}
file.MoveAsync(target).get();
std::filesystem::path filename = path.filename();
path = targetPath;
path.replace_filename(filename);
return true;
} catch (const hresult_error& ex) {
return false;
}
return false;
}
bool WinRTFile::copy(std::filesystem::path path) {
bool WinRTFile::copy(std::filesystem::path targetPath) {
if (file == nullptr) {
return false;
}
try {
StorageFolder target = ApplicationData::Current().LocalFolder();
for (const auto& part : targetPath.parent_path()) {
hstring partStr = hstring(part.wstring());
target = target.GetFolderAsync(partStr).get();
}
if (!target) {
return false;
}
file.CopyAsync(target).get();
return true;
} catch (const hresult_error& ex) {
return false;
}
return false;
}
}

View File

@@ -16,7 +16,6 @@ target_link_libraries(WinDurango.KernelX PRIVATE WinDurango.Common)
target_include_directories(WinDurango.KernelX PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include/WinDurango.KernelX/
../WinDurango.Common/include/
)
set_target_properties(WinDurango.KernelX PROPERTIES

View File

@@ -1,8 +1,11 @@
#include "pch.h"
#include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h"
#include <iostream>
using namespace winrt;
using namespace Windows;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation::Numerics;
using namespace Windows::UI;
@@ -15,6 +18,7 @@ struct App : implements<App, IFrameworkViewSource, IFrameworkView>
VisualCollection m_visuals{ nullptr };
Visual m_selected{ nullptr };
float2 m_offset{};
bool runFirst = true;
IFrameworkView CreateView()
{
@@ -88,6 +92,20 @@ struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
AddVisual(point);
}
if (runFirst)
{
Package pkg = Package::Current();
hstring id = pkg.Id().FamilyName();
std::wcout << L"Name: " << id.c_str() << L"\n";
wd::impl::winrt::interfaces::storage::WinRTDirectory dir("");
dir.open();
auto wddir = dir.CreateFolder("WinDurango");
wddir->open();
auto wdlog = wddir->CreateFile("log.txt");
wdlog->open();
(*wdlog) << std::string("Testing");
runFirst = false;
}
}
void OnPointerMoved(IInspectable const &, PointerEventArgs const & args)

View File

@@ -79,7 +79,7 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>..\..\build\projects\$(MSBuildProjectName)\$(Configuration)\</OutDir>
<OutDir>..\..\build\bin\$(Configuration)\</OutDir>
<IntDir>..\..\build\projects\$(MSBuildProjectName)\$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup>
@@ -98,10 +98,10 @@
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">D:\WinDurango\projects\WinDurango.Common\include\WinDurango.Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\build\projects\WinDurango.Common\Debug\WinDurango.Common.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Console</SubSystem>
</Link>
</ItemDefinitionGroup>