OSDN Git Service

バックバッファを作ってみた。
[shooting3/shootinggame.git] / ShootingGame / BasicReaderWriter.cpp
1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
5 ////
6 //// Copyright (c) Microsoft Corporation. All rights reserved
7
8 #include "pch.h"
9 #include "BasicReaderWriter.h"
10 #include <ppltasks.h>
11
12 using namespace Microsoft::WRL;
13 using namespace Windows::Storage;
14 using namespace Windows::Storage::FileProperties;
15 using namespace Windows::Storage::Streams;
16 using namespace Windows::Foundation;
17 using namespace Windows::ApplicationModel;
18 using namespace Concurrency;
19
20 BasicReaderWriter::BasicReaderWriter()
21 {
22     m_location = Package::Current->InstalledLocation;
23     m_locationPath = Platform::String::Concat(m_location->Path, "\\");
24 }
25
26 BasicReaderWriter::BasicReaderWriter(
27     _In_ Windows::Storage::StorageFolder^ folder
28     )
29 {
30     m_location = folder;
31     Platform::String^ path = m_location->Path;
32     if (path->Length() == 0)
33     {
34         // Applications are not permitted to access certain
35         // folders, such as the Documents folder, using this
36         // code path.  In such cases, the Path property for
37         // the folder will be an empty string.
38         throw ref new Platform::FailureException();
39     }
40     m_locationPath = Platform::String::Concat(path, "\\");
41 }
42
43 Platform::Array<byte>^ BasicReaderWriter::ReadData(
44     _In_ Platform::String^ filename
45     )
46 {
47     CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
48     extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
49     extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
50     extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
51     extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52     extendedParams.lpSecurityAttributes = nullptr;
53     extendedParams.hTemplateFile = nullptr;
54
55     Wrappers::FileHandle file(
56         CreateFile2(
57             Platform::String::Concat(m_locationPath, filename)->Data(),
58             GENERIC_READ,
59             0,
60             OPEN_EXISTING,
61             &extendedParams
62             )
63         );
64     if (file.Get()==INVALID_HANDLE_VALUE)
65     {
66         throw ref new Platform::FailureException();
67     }
68
69     FILE_STANDARD_INFO fileInfo = {0};
70     if (!GetFileInformationByHandleEx(
71         file.Get(),
72         FileStandardInfo,
73         &fileInfo,
74         sizeof(fileInfo)
75         ))
76     {
77         throw ref new Platform::FailureException();
78     }
79
80     if (fileInfo.EndOfFile.HighPart != 0)
81     {
82         throw ref new Platform::OutOfMemoryException();
83     }
84
85     Platform::Array<byte>^ fileData = ref new Platform::Array<byte>(fileInfo.EndOfFile.LowPart);
86
87     if (!ReadFile(
88         file.Get(),
89         fileData->Data,
90         fileData->Length,
91         nullptr,
92         nullptr
93         ) )
94     {
95         throw ref new Platform::FailureException();
96     }
97
98     return fileData;
99 }
100
101 void BasicReaderWriter::ReadDataAsync(
102     _In_ Platform::String^ filename,
103     _In_ ReadDataAsyncCallback^ callback
104     )
105 {
106     task<StorageFile^>(m_location->GetFileAsync(filename)).then([=](StorageFile^ file)
107     {
108         return FileIO::ReadBufferAsync(file);
109     }).then([=](IBuffer^ buffer)
110     {
111         auto fileData = ref new Platform::Array<byte>(buffer->Length);
112         DataReader::FromBuffer(buffer)->ReadBytes(fileData);
113         callback(fileData, AsyncStatus::Completed);
114     });
115 }
116
117 uint32 BasicReaderWriter::WriteData(
118     _In_ Platform::String^ filename,
119     _In_ Platform::Array<byte>^ fileData
120     )
121 {
122     CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
123     extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
124     extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
125     extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
126     extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
127     extendedParams.lpSecurityAttributes = nullptr;
128     extendedParams.hTemplateFile = nullptr;
129
130     Wrappers::FileHandle file(
131         CreateFile2(
132             Platform::String::Concat(m_locationPath, filename)->Data(),
133             GENERIC_WRITE,
134             0,
135             CREATE_ALWAYS,
136             &extendedParams
137             )
138         );
139     if (file.Get()==INVALID_HANDLE_VALUE)
140     {
141         throw ref new Platform::FailureException();
142     }
143
144     DWORD numBytesWritten;
145     if (
146         !WriteFile(
147             file.Get(),
148             fileData->Data,
149             fileData->Length,
150             &numBytesWritten,
151             nullptr
152             ) || 
153         numBytesWritten != fileData->Length
154         )
155     {
156         throw ref new Platform::FailureException();
157     }
158
159     return numBytesWritten;
160 }
161
162 void BasicReaderWriter::WriteDataAsync(
163     _In_ Platform::String^ filename,
164     _In_ Platform::Array<byte>^ fileData,
165     _In_ WriteDataAsyncCallback^ callback
166     )
167 {
168     task<StorageFile^>(m_location->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting)).then([=](StorageFile^ file)
169     {
170         return FileIO::WriteBytesAsync(file, fileData);
171     }).then([=]()
172     {
173         callback(AsyncStatus::Completed);
174     });
175 }