-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfumen.hpp
More file actions
116 lines (91 loc) · 2.87 KB
/
fumen.hpp
File metadata and controls
116 lines (91 loc) · 2.87 KB
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
#pragma once
#include <vector>
#include <string>
#include <optional>
#include <details/intdef.hpp>
#include <details/encoder.hpp>
#include <details/decoder.hpp>
namespace fumen {
using field = fumen::details::field;
using piece_type = fumen::details::piece_type;
using rotation = fumen::details::rotation_type;
using operation = fumen::details::field_operation;
struct fumen_page {
field m_field;
std::string m_comment;
std::optional<operation> m_operation;
union flags {
struct {
u8 lock_bit : 1;
u8 mirror_bit : 1;
u8 colorize_bit : 1;
u8 rise_bit : 1;
u8 reserved : 4;
};
u8 all = 0;
} m_flags;
};
using fumen_pages = std::vector<fumen_page>;
inline static char piece_to_char(piece_type _p)
{ return fumen::details::defs::to_char(_p); }
inline static piece_type char_to_piece(char _ch)
{ return fumen::details::defs::to_piece(_ch); }
inline static bool is_valid_piece(char _p)
{ return static_cast<u8>(char_to_piece(_p)) <= 8u; }
inline static bool is_valid_piece(piece_type _p)
{ return static_cast<u8>(_p) <= 8u; }
inline static std::string encode(const fumen_pages& _pgs) {
fumen::details::encode_pages _epgs;
_epgs.reserve(_pgs.size());
for (const auto& _pg : _pgs) {
fumen::details::encode_page _epg;
_epg.m_field = _pg.m_field;
_epg.m_comment = _pg.m_comment;
if (_pg.m_operation)
_epg.m_operation = fumen::details::field_operation {
_pg.m_operation->m_piece,
_pg.m_operation->m_rotation,
_pg.m_operation->m_x,
_pg.m_operation->m_y
};
_epg.m_flags.all = _pg.m_flags.all;
_epgs.push_back(std::move(_epg));
}
return "v115@" + fumen::details::encoder::encode(_epgs);
}
inline static fumen_pages decode(const std::string& _str) {
fumen::details::pages _pgs = fumen::details::decoder::decode(_str);
fumen_pages _fpgs; _fpgs.reserve(_pgs.size());
for (const fumen::details::page& _pg : _pgs) {
fumen_page _fpg;
_fpg.m_field = _pg.m_inner_field;
_fpg.m_comment = _pg.m_comment.value_or("");
if (_pg.m_operation)
_fpg.m_operation = {
_pg.m_operation->m_piece,
_pg.m_operation->m_rotation,
_pg.m_operation->m_x,
_pg.m_operation->m_y
};
_fpg.m_flags.all = _pg.m_flags.all;
_fpgs.push_back(std::move(_fpg));
}
return _fpgs;
}
inline static bool is_valid(const std::string& _str) {
try {
fumen::decode(_str);
return true;
} catch (...) {
return false;
}
}
inline static bool try_decode(const std::string& _input, fumen_pages& _output) {
try {
_output = fumen::decode(_input);
return true;
} catch (...) {
return false;
}
}
}