Compare commits
729 Commits
Author | SHA1 | Date | |
---|---|---|---|
99015f5838 | |||
cdaf0dfd2f | |||
38421b7a53 | |||
e0362bbb58 | |||
a02885704d | |||
0a223af698 | |||
283783954f | |||
d71bf7d4a5 | |||
4274bfa1b7 | |||
b3fe4de0d4 | |||
e9e6d7002a | |||
662cbf0827 | |||
9bfa7f2048 | |||
45d80522eb | |||
77f92f9294 | |||
5241490590 | |||
8bb2a01c8e | |||
85bbe43827 | |||
a532a9f4c1 | |||
c66b7da63b | |||
e1bb874943 | |||
365a8992cd | |||
cd3aae7201 | |||
aa84fb8544 | |||
4dd4c5f20d | |||
30b3114bc1 | |||
58be497903 | |||
50879a1f3f | |||
55dd38fb2c | |||
b7f5dbc4da | |||
81d458f7de | |||
58091ad534 | |||
6783d39571 | |||
e2c8c92279 | |||
cbe2e1d9af | |||
b0efd6b6b4 | |||
290f02cfbc | |||
42c3ed8ca8 | |||
b1b9dbdb77 | |||
c27647bf01 | |||
1fec4d363e | |||
8ad7d86f94 | |||
df3f7203d3 | |||
00d47cde0c | |||
7298645850 | |||
6d714c972b | |||
446724f9ca | |||
6e01b255a6 | |||
01b162883d | |||
c4e15fbbfd | |||
6f4dfad53e | |||
5c38245e23 | |||
4d05d0d135 | |||
8cf6430181 | |||
f247be0177 | |||
f9916cbce7 | |||
c55a5d7ddd | |||
091d6307d1 | |||
05db2cc32e | |||
5d884c1c7b | |||
ea3ca7806c | |||
beeca3b5f9 | |||
4051ea8874 | |||
42745ead11 | |||
d8048b1d89 | |||
048a9f54fb | |||
59339d5bc6 | |||
7f8c0b5eec | |||
2fbd2fb466 | |||
de1e9f9019 | |||
d74db533dc | |||
83ea6f5a0c | |||
bb0ca985cd | |||
8af5e772d6 | |||
aebce25f3b | |||
b39428aa39 | |||
700af29e43 | |||
0f13d2d5fd | |||
820aaeff85 | |||
ed6ae7ad8a | |||
5ef6e2b4a3 | |||
6d0975b0a1 | |||
111c500f3b | |||
a471272c52 | |||
190c3ef680 | |||
e210837329 | |||
e339ec3e4c | |||
522699152b | |||
e9d0f4ce1a | |||
155ce0bb22 | |||
cbe94e6308 | |||
3064925cd2 | |||
7af998db15 | |||
1a084e6bfb | |||
5e2fae0f14 | |||
58017e1216 | |||
7fa8e09c10 | |||
8f70b0075f | |||
54f75781f3 | |||
d7a38416aa | |||
ed496859cb | |||
33c0826f3b | |||
2d7a204a30 | |||
92fb34cfa8 | |||
ad625e3e35 | |||
342e216372 | |||
88d198f408 | |||
ec8487a4c6 | |||
4f12e4816f | |||
30e9f091d7 | |||
c2e94afc1c | |||
e2c58c708f | |||
c52bf734d3 | |||
ca60681de7 | |||
8f3c38e273 | |||
119b87f3df | |||
2778725b91 | |||
4ca5809597 | |||
0cdd6e569e | |||
8c11be48cf | |||
9fcf6f3379 | |||
b45cf9cba7 | |||
28234d9587 | |||
1803e1dee7 | |||
166478e4bb | |||
c05b771425 | |||
9db2ded366 | |||
9aed79f599 | |||
221887e447 | |||
36013d4e47 | |||
cf8dda1ea2 | |||
0f4aec4355 | |||
ddf738ee27 | |||
bd834cff4a | |||
adf5019e2a | |||
a19a3d9b81 | |||
b50f771d6e | |||
8089e724f9 | |||
18f9fbea60 | |||
6ea487ff13 | |||
a83b16c829 | |||
b48bd7b8ba | |||
b56722b2d1 | |||
5854801a53 | |||
668f46d260 | |||
b672889008 | |||
08a12097a4 | |||
7535ce04f5 | |||
bede668e73 | |||
c2c28518ba | |||
bfa1423f64 | |||
395c094890 | |||
a284edd130 | |||
b57959b4bf | |||
724cb10bc1 | |||
97e759f57d | |||
3ce8ad72ed | |||
c23d79e3f5 | |||
71294db3c1 | |||
825818679c | |||
84acb2c12d | |||
9c034c0f7b | |||
aa7452aead | |||
f86562b2f2 | |||
8fa2bd1e81 | |||
1f58390298 | |||
e40c98ae1b | |||
642a9563ba | |||
4a1fa48352 | |||
4dc0767e91 | |||
9e82c04898 | |||
0776eca49d | |||
af01fb2119 | |||
9044631fe7 | |||
3b22d4fce3 | |||
5393ff1451 | |||
e34a9cc868 | |||
440581261e | |||
b78e99c59c | |||
3db8f61e3b | |||
8b64165fb7 | |||
799b1e12da | |||
957270c41a | |||
bc51a45df8 | |||
9b091a0084 | |||
98e35fc6f6 | |||
d83f447e82 | |||
898885b116 | |||
fd05f6c1dd | |||
86da71f2cb | |||
ba2f1d4858 | |||
3687a70aec | |||
8da54093aa | |||
b057ea8074 | |||
e7bc9d0294 | |||
08de91b04d | |||
ea70fbb051 | |||
cee6a08240 | |||
f04b9cfb75 | |||
1dcbc03829 | |||
d89423caf5 | |||
c9b348dd4f | |||
636f45f03f | |||
7fce9591a9 | |||
f65e4f1900 | |||
a4d0e3867a | |||
83b9c27e94 | |||
864ea91be0 | |||
ea02fc22bd | |||
60cc763cd0 | |||
8955b69cdf | |||
a7aff4d625 | |||
22cb8f0cb4 | |||
db07ace927 | |||
094c3fe2a3 | |||
cc156e0d81 | |||
9833052849 | |||
a6a0ac3f9e | |||
82060a6403 | |||
02441eb328 | |||
2fe2a40e74 | |||
754e3be906 | |||
65c225e93b | |||
74ce265016 | |||
2eba0e490f | |||
82f769f798 | |||
a008760918 | |||
832facdf5b | |||
d5ba09cbea | |||
59b4f14fb3 | |||
4d13c06f27 | |||
de72fcf572 | |||
1470dbb5aa | |||
ce31f3e15f | |||
f6741d0acc | |||
910e3ce277 | |||
52adef8e9b | |||
3d7639b94e | |||
b306a9d3d1 | |||
0cc15f0c8e | |||
0959b5cc25 | |||
44c54cd837 | |||
6e553b9ebb | |||
88a46127d7 | |||
4310b67e7b | |||
7deff3c6be | |||
5f4d35b3b3 | |||
5b510fa5da | |||
17042b6664 | |||
3fb123ff30 | |||
ca928eb731 | |||
906c82972b | |||
cee74b9e31 | |||
25b4f3ccb7 | |||
d1b9b9607b | |||
6ccc86c28d | |||
3d75cd9a11 | |||
b30d35c937 | |||
a8231f7c86 | |||
ed60859406 | |||
0d0902735a | |||
5bed3cf05b | |||
5d520609b5 | |||
bf53298706 | |||
3d09ec8c94 | |||
bc083541aa | |||
5b04426cd3 | |||
8d606524fc | |||
5bab0ca648 | |||
fb31e833cb | |||
346040574a | |||
959d07b0a9 | |||
c41d41c881 | |||
f09e3d3545 | |||
12b3373aeb | |||
9480904db7 | |||
bee3401283 | |||
fe4280aa2e | |||
3e273d67ba | |||
fcec43f694 | |||
904581c520 | |||
b983e32d56 | |||
f2aea2bd40 | |||
cc8e1f7ce4 | |||
749e1395bc | |||
21af9582f8 | |||
26dd373e7b | |||
1bd35d2c51 | |||
d275f61602 | |||
f5f1a5de67 | |||
56153f5eed | |||
b4984c0b3a | |||
dad399facf | |||
b536871da7 | |||
bd028c1b72 | |||
fa9303c0a1 | |||
d030c48b4d | |||
beab3d7f8b | |||
0b2ea3ddbc | |||
b143fb49ce | |||
8f211568be | |||
6d276c99ac | |||
72a93721f9 | |||
a1fd420493 | |||
d72afaa75b | |||
2784544884 | |||
c39a02a1c2 | |||
4c397ab2f7 | |||
6b3a9c9f0a | |||
1c1560f17f | |||
0699bc5614 | |||
d86da00258 | |||
d1ad68debe | |||
b937285a74 | |||
69fe03475b | |||
e5956f2b34 | |||
fdb4ab55a3 | |||
e30df8bdbc | |||
251b7bc352 | |||
61b72107ae | |||
d6208f19fb | |||
93d9fdd4b8 | |||
8670482c04 | |||
d2b71e41c9 | |||
29a8ac2136 | |||
a9fc5130bb | |||
b51c7d2352 | |||
56fd0936bb | |||
2b3898655f | |||
1b30d3e62c | |||
bdbb1fcb07 | |||
659f2165ff | |||
a5439430fb | |||
0f683e403c | |||
cf6a7a795b | |||
f664708165 | |||
a7be55f2b0 | |||
398cec1fc5 | |||
a8b9ba0af4 | |||
f54564c567 | |||
a2c5850fcd | |||
f4411629e4 | |||
17d620ff0c | |||
5887b1267a | |||
400723d83b | |||
07babcb0ff | |||
f7bfe07660 | |||
3690adf5dd | |||
48349b598d | |||
d6e3d3ad00 | |||
c57c82bdd1 | |||
4bc921d47e | |||
8907dd19b0 | |||
2221658e7a | |||
916c55b4b2 | |||
3d8a4a64a9 | |||
6cb36a7064 | |||
1fc5ea8ac6 | |||
6fd8e44d10 | |||
ff19b45a9f | |||
f467832115 | |||
0a25b5260d | |||
c877437ab6 | |||
676d19f452 | |||
fd7c1e6635 | |||
c98536e8ab | |||
a013d59379 | |||
2e69035618 | |||
072703efe7 | |||
0c796c4d7a | |||
ee4b0c5483 | |||
ba7a458e7c | |||
06d9df304e | |||
578e10bbd7 | |||
c685e634d5 | |||
824f401b77 | |||
aafc326f95 | |||
88a6410439 | |||
d6e1b19d32 | |||
6fa459e5d3 | |||
bde6216295 | |||
e03dbef5ff | |||
e2c683567e | |||
89f391f040 | |||
92a3d5018f | |||
d510fec57b | |||
054b17811d | |||
94d5f7f82e | |||
0e4445e52b | |||
c5214dd477 | |||
ed2c216cf4 | |||
609645c108 | |||
d280d27a0a | |||
5b727065f3 | |||
7aa2577059 | |||
c1b7e9ab55 | |||
99384397ed | |||
a2391aeb22 | |||
2207c80951 | |||
bf578d7cb9 | |||
0bc57c368f | |||
b64f85aaa2 | |||
93fa2f2d7e | |||
d72216de8b | |||
df5133a91a | |||
88b959a118 | |||
310bf91fbd | |||
699f47f98d | |||
24e881b138 | |||
2eef1b5c4e | |||
c18ceb50d4 | |||
27ca1a7292 | |||
dc59176eac | |||
aec7470ff8 | |||
983cba6843 | |||
871782e73f | |||
9aaa96fe10 | |||
22190a29c1 | |||
613ca467d0 | |||
536a3066b2 | |||
43488cd002 | |||
a11ccbd39c | |||
76df4929a7 | |||
505b826627 | |||
26a8675922 | |||
b89e1f983e | |||
717e77b47e | |||
2f10c79dee | |||
55f7790f89 | |||
ae5f4a8c16 | |||
1851bd3c54 | |||
6d74685cb7 | |||
03fd7f6d01 | |||
28c878f3e5 | |||
7736eba14d | |||
2e54b38d2b | |||
da60dc0903 | |||
215f72b3b5 | |||
a93c081dd8 | |||
456782930a | |||
ba3cbbd64c | |||
dfc3e9ca06 | |||
f567a2b78e | |||
c7e7bd8a77 | |||
67720fd0e1 | |||
1a30149942 | |||
a755cc13bd | |||
9a51cf1b56 | |||
256c656e9c | |||
2ac7f05316 | |||
28d46dbabe | |||
18b3e0bc84 | |||
23f2e7c1f2 | |||
998713b41f | |||
3561354231 | |||
0cccb170c1 | |||
d2480b8a6f | |||
6837d3f7ee | |||
3ecf3b4bfc | |||
1da8647ff1 | |||
2f19e8daad | |||
07f62c7aeb | |||
39db8dfa45 | |||
f04cd370f1 | |||
5c4acd54ce | |||
82dbc5c479 | |||
1f1663d714 | |||
a3c5392caa | |||
6c983cc2cb | |||
3bf3fdac3d | |||
f2f3dba098 | |||
3728360dd2 | |||
887837bb3d | |||
99b4c2dfc1 | |||
e59769158a | |||
ba3238614b | |||
a115999aab | |||
393947db9f | |||
e9b2a7f894 | |||
9c73455761 | |||
59c2210359 | |||
4fab20953a | |||
ee7b0f5081 | |||
33ee7a9a87 | |||
900bd7b77a | |||
6bd32c9aef | |||
13893b2853 | |||
23789c15eb | |||
a1f7418d32 | |||
274a823d02 | |||
ba6239068a | |||
ff8c925f32 | |||
2a6a33e60c | |||
8910b1f4a0 | |||
36dddea4d9 | |||
6a648c2dcd | |||
2d4087dc89 | |||
f91aacd78e | |||
9c08cbf0d2 | |||
88d35e4eaf | |||
675ce68073 | |||
db0165d145 | |||
7015426300 | |||
0d4cc5e208 | |||
e7ce0985fb | |||
eb6dafbd60 | |||
b6e238780e | |||
c7ea6f1d4b | |||
4a5b2a6889 | |||
b84d645aee | |||
67b44db1ae | |||
ee4399109a | |||
87ef534f59 | |||
87362b47c5 | |||
f60ba1088d | |||
abb7ad6f24 | |||
880b475c07 | |||
4707c40e6a | |||
7f87c23da2 | |||
6df10837fe | |||
42cb54de1d | |||
9fd685b8b3 | |||
b364005741 | |||
7d938de409 | |||
bb4ecfcd8c | |||
ff410529b0 | |||
fdc55a8e3b | |||
fc8512ff63 | |||
1b1ed42a1b | |||
b437925f92 | |||
c04e50e959 | |||
314cdb9935 | |||
77c91d015a | |||
291a018c13 | |||
18ff4b8e16 | |||
7714c277fd | |||
d6c2ac6be6 | |||
682fe38d40 | |||
3e525842cb | |||
3dd25b51a8 | |||
041c1e374e | |||
16b1d323dc | |||
c4d5e5f480 | |||
187f07d2c9 | |||
4e9d7e5b87 | |||
7df5b15e2e | |||
4863aa0ae7 | |||
eb53c3465b | |||
f683d61298 | |||
fbd03c8037 | |||
da68c8b877 | |||
c0744a3464 | |||
3ca3746cec | |||
5e76ddf2cd | |||
dca1ba304e | |||
8dd32afb74 | |||
313824b4bb | |||
b166c0f5ef | |||
300e44bd4b | |||
ab6f983392 | |||
596c6395e4 | |||
404a36f9b8 | |||
1711fbadf7 | |||
84b7a6d183 | |||
6d0fd0f9ec | |||
b407ba88c4 | |||
bd256ba1a6 | |||
623c53f79a | |||
969fdc8069 | |||
cbc874dd72 | |||
450bd52095 | |||
5727fcf177 | |||
8ab0c2698b | |||
6c7b52d93c | |||
91f55cd9a3 | |||
a1ce459a0e | |||
9700992c3a | |||
f9a1ea72fe | |||
507b656eab | |||
c16776aee9 | |||
e109e0bd24 | |||
776a615464 | |||
5514b6cf37 | |||
8932d1b8d0 | |||
10988847b3 | |||
c4b139c7a4 | |||
130896df1f | |||
5d5c519a1d | |||
3d1a11f78b | |||
cc985844cd | |||
feffbaa5a6 | |||
d0f0c8ce6d | |||
3fdc236b1d | |||
46870e163a | |||
601f64cc61 | |||
c015b60dc3 | |||
5e01b654bd | |||
2304257201 | |||
02794d88b9 | |||
9f73c8ffad | |||
c1c354959d | |||
94428d9e18 | |||
5198ecec1f | |||
6779b88055 | |||
c5dab3a232 | |||
a7608bcd7e | |||
be64bc76b5 | |||
39bc34fd42 | |||
4185303bd2 | |||
3280693e8f | |||
29432feabc | |||
86559c681e | |||
79f11b9c33 | |||
5b9149cb34 | |||
9d6bdd968f | |||
bc4fec33ef | |||
09e917dbe8 | |||
1003a0e199 | |||
e3a805b855 | |||
4222176979 | |||
43c87fba70 | |||
d0a23aaf30 | |||
a09a5686d7 | |||
609bb317d0 | |||
898fb7d557 | |||
c39f258a19 | |||
4fdd4e1935 | |||
7662011d60 | |||
c24372b308 | |||
5e4c53113a | |||
4f93995bbd | |||
5b14466059 | |||
1d3aa85446 | |||
6efe70d751 | |||
555c88855c | |||
105aacc133 | |||
ea9000f2b0 | |||
4e851d9b73 | |||
0a1e512f41 | |||
a7baef2c9d | |||
723ec937ad | |||
7c77ba83f8 | |||
6da4b96b24 | |||
e8f8593555 | |||
2b5bc5dd08 | |||
cf6186aa16 | |||
4d7773a2ac | |||
e8f74399f2 | |||
ca72f7adb5 | |||
5e19a8b886 | |||
a416f772d4 | |||
989a6b5554 | |||
732af15d9b | |||
8cdab36cd6 | |||
44180c7e0f | |||
e589e37640 | |||
94107de97d | |||
13c55dc23e | |||
617eddc030 | |||
7475b19547 | |||
0049ace91a | |||
356f4df9a9 | |||
ff5928b556 | |||
1f0ac2a2e9 | |||
95628f07d1 | |||
3bead4f1b3 | |||
ec9b23f797 | |||
8175ca7e82 | |||
451ebbe91a | |||
da2313ab57 | |||
c5571e7d17 | |||
959157255f | |||
06d8012675 | |||
571320630b | |||
207dee9932 | |||
d7b0ca77e9 | |||
e55642cdeb | |||
595fd74662 | |||
3f7becf580 | |||
5d17555744 | |||
e6d94f248c | |||
b0c70bc62e | |||
8eb0b11027 | |||
7f2cfe94c1 | |||
e4524bda0b | |||
226fb15eb1 | |||
645c4af2f2 | |||
6e0f41c7fd | |||
b9001ed9b2 | |||
04abf59521 | |||
251f92532d | |||
f5df56687b | |||
b582da90e5 | |||
815f48fe06 | |||
1470fa87dd | |||
4b4356aaab | |||
a8658856ca | |||
1477e907e6 | |||
f559cea826 | |||
d363042036 | |||
69e4ecd0a9 | |||
35d2e06625 | |||
358e654f51 | |||
d16548e570 | |||
c7201dd62c | |||
e43a0e62b7 | |||
4bcf76819c | |||
31155c909c | |||
ab2670358a | |||
3da70bdccc | |||
e370e1937c | |||
d9f6dd33d4 | |||
e5d6e549bd | |||
145c0ce6c8 | |||
a0174b94c6 | |||
109b489104 | |||
784c3fc338 | |||
77d6ac2caf | |||
321af22775 | |||
39ef9815e8 | |||
bb3afba11f | |||
e4fa160a90 | |||
34df9e2257 | |||
cfdb5f021e | |||
d08eea5c1e | |||
ce30b5427b | |||
e38fed89e9 | |||
d4b12bf3f7 | |||
40d75a91c6 |
@@ -1,6 +1,9 @@
|
|||||||
# Remove the line below if you want to inherit .editorconfig settings from higher directories
|
# Remove the line below if you want to inherit .editorconfig settings from higher directories
|
||||||
root = true
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
|
||||||
# C# files
|
# C# files
|
||||||
[*.cs]
|
[*.cs]
|
||||||
|
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -68,3 +68,4 @@ crashlytics-build.properties
|
|||||||
/UserSettings
|
/UserSettings
|
||||||
/*.zip
|
/*.zip
|
||||||
*.lnk
|
*.lnk
|
||||||
|
/HybridCLRData
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: ff84d017aa87efe40a398305f6c66f4d
|
|
||||||
folderAsset: yes
|
|
||||||
timeCreated: 1611481582
|
|
||||||
licenseType: Free
|
|
||||||
DefaultImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: c96c5f91fd684d14d9d4c09a5f34ee5d
|
|
||||||
timeCreated: 1637144068
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 2162866fb7549724a85cb3c854a136ad
|
|
||||||
timeCreated: 1637144548
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 6e50faab4238b5548bb05a7d421f5405
|
|
||||||
timeCreated: 1637145598
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 8b9f1682ee8026b468ad15885e1ff6a5
|
|
||||||
NativeFormatImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
mainObjectFileID: 7400000
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 0cc97e0bf50ad9c4e834c28e6eddf416
|
|
||||||
timeCreated: 1637145877
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 9f6fb5723dba03d42b9e8a3c3e9091a5
|
|
||||||
NativeFormatImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
mainObjectFileID: 7400000
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 63a0e2075f5ba8d489d8cd318c14720b
|
|
||||||
timeCreated: 1637144068
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: cc249d3462d795b46aff263bc04baee2
|
|
||||||
timeCreated: 1637144561
|
|
||||||
licenseType: Free
|
|
||||||
NativeFormatImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,8 +0,0 @@
|
|||||||
using System.Reflection;
|
|
||||||
|
|
||||||
[assembly: AssemblyCompany("Cryville")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © Cryville 2020-2022")]
|
|
||||||
[assembly: AssemblyDefaultAlias("Cosmo Resona")]
|
|
||||||
[assembly: AssemblyProduct("Cosmo Resona")]
|
|
||||||
[assembly: AssemblyTitle("Cosmo Resona")]
|
|
||||||
[assembly: AssemblyVersion("0.5.0")]
|
|
Binary file not shown.
Binary file not shown.
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 8a36c371ab6077d43ac28fe09b0fe675
|
|
||||||
timeCreated: 1620725915
|
|
||||||
licenseType: Free
|
|
||||||
DefaultImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common {
|
namespace Cryville.Common {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -20,13 +20,13 @@ namespace Cryville.Common {
|
|||||||
/// <param name="succeeded">Whether the task has succeeded.</param>
|
/// <param name="succeeded">Whether the task has succeeded.</param>
|
||||||
/// <param name="result">The result.</param>
|
/// <param name="result">The result.</param>
|
||||||
public void Deliver(bool succeeded, T result) {
|
public void Deliver(bool succeeded, T result) {
|
||||||
if (Destination != null) Destination(succeeded, result);
|
Destination?.Invoke(succeeded, result);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Cancels the task.
|
/// Cancels the task.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Cancel() {
|
public void Cancel() {
|
||||||
if (CancelSource != null) CancelSource();
|
CancelSource?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,7 +13,7 @@ namespace Cryville.Common {
|
|||||||
public static Binder CreateBinderOfType(Type type) {
|
public static Binder CreateBinderOfType(Type type) {
|
||||||
var l = type.GetCustomAttributes(typeof(BinderAttribute), true);
|
var l = type.GetCustomAttributes(typeof(BinderAttribute), true);
|
||||||
if (l.Length > 0) {
|
if (l.Length > 0) {
|
||||||
return (Binder)ReflectionHelper.InvokeEmptyConstructor(
|
return (Binder)Activator.CreateInstance(
|
||||||
((BinderAttribute)l[0]).BinderType
|
((BinderAttribute)l[0]).BinderType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -33,12 +33,12 @@ namespace Cryville.Common {
|
|||||||
public override object ChangeType(object value, Type type, CultureInfo culture) {
|
public override object ChangeType(object value, Type type, CultureInfo culture) {
|
||||||
if (value == null)
|
if (value == null)
|
||||||
return null;
|
return null;
|
||||||
else if (type == value.GetType())
|
else if (type.IsAssignableFrom(value.GetType()))
|
||||||
return value;
|
return value;
|
||||||
else if (type.IsEnum && value is string) {
|
else if (type.IsEnum && value is string strValue) {
|
||||||
return Enum.Parse(type, (string)value);
|
return Enum.Parse(type, strValue);
|
||||||
}
|
}
|
||||||
throw new InvalidCastException();
|
throw new InvalidCastException(string.Format("Cannot cast {0} to {1}", value.GetType(), type));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ReorderArgumentArray(ref object[] args, object state) {
|
public override void ReorderArgumentArray(ref object[] args, object state) {
|
||||||
|
@@ -1,65 +0,0 @@
|
|||||||
namespace Cryville.Common.Buffers {
|
|
||||||
/// <summary>
|
|
||||||
/// A resource pool that allows reusing instances of arrays of type <typeparamref name="T" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The item type of the arrays in the pool.</typeparam>
|
|
||||||
public class ArrayPool<T> {
|
|
||||||
private class Bucket : ObjectPool<T[]> {
|
|
||||||
readonly int _size;
|
|
||||||
public Bucket(int size, int capacity) : base(capacity) {
|
|
||||||
_size = size;
|
|
||||||
}
|
|
||||||
protected override T[] Construct() {
|
|
||||||
return new T[_size];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
readonly Bucket[] _buckets;
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="ArrayPool{T}" /> class with the default maximum list size and bucket capacity.
|
|
||||||
/// </summary>
|
|
||||||
public ArrayPool() : this(0x40000000, 256) { }
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="ArrayPool{T}" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxSize">The maximum size of the arrays in the pool.</param>
|
|
||||||
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups arrays of similar sizes into buckets for faster access.</param>
|
|
||||||
public ArrayPool(int maxSize, int capacityPerBucket) {
|
|
||||||
if (maxSize < 16) maxSize = 16;
|
|
||||||
int num = GetID(maxSize) + 1;
|
|
||||||
_buckets = new Bucket[num];
|
|
||||||
for (int i = 0; i < num; i++) {
|
|
||||||
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Rents an array that is at least the specified size from the pool.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="size">The minimum size of the array.</param>
|
|
||||||
/// <returns>An array of type <see cref="T" /> that is at least the specified size.</returns>
|
|
||||||
public T[] Rent(int size) {
|
|
||||||
int len2 = size;
|
|
||||||
if (len2 < 16) len2 = 16;
|
|
||||||
var arr = _buckets[GetID(len2)].Rent();
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a rented array to the pool.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="arr">The array to return.</param>
|
|
||||||
public void Return(T[] arr) {
|
|
||||||
int len2 = arr.Length;
|
|
||||||
if (len2 < 16) len2 = 16;
|
|
||||||
_buckets[GetID(len2)].Return(arr);
|
|
||||||
}
|
|
||||||
static int GetID(int size) {
|
|
||||||
size -= 1;
|
|
||||||
size >>= 4;
|
|
||||||
int num = 0;
|
|
||||||
for (; size != 0; size >>= 1) num++;
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
static int GetSize(int id) {
|
|
||||||
return 0x10 << id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,71 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Buffers {
|
|
||||||
/// <summary>
|
|
||||||
/// A resource pool that allows reusing instances of lists of type <typeparamref name="T" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The item type of the lists in the pool.</typeparam>
|
|
||||||
public class ListPool<T> {
|
|
||||||
private class Bucket : ObjectPool<List<T>> {
|
|
||||||
readonly int _size;
|
|
||||||
public Bucket(int size, int capacity) : base(capacity) {
|
|
||||||
_size = size;
|
|
||||||
}
|
|
||||||
protected override List<T> Construct() {
|
|
||||||
return new List<T>(_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
readonly Bucket[] _buckets;
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="ListPool{T}" /> class with the default maximum list size and bucket capacity.
|
|
||||||
/// </summary>
|
|
||||||
public ListPool() : this(0x40000000, 256) { }
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="ListPool{T}" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxSize">The maximum size of the lists in the pool.</param>
|
|
||||||
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups lists of similar sizes into buckets for faster access.</param>
|
|
||||||
public ListPool(int maxSize, int capacityPerBucket) {
|
|
||||||
if (maxSize < 16) maxSize = 16;
|
|
||||||
int num = GetID(maxSize) + 1;
|
|
||||||
_buckets = new Bucket[num];
|
|
||||||
for (int i = 0; i < num; i++) {
|
|
||||||
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Rents a list of the specified size from the pool. The size of the list must not be changed when it is rented.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="size">The size of the list.</param>
|
|
||||||
/// <returns>A <see cref="List{T}" /> of the specified size.</returns>
|
|
||||||
public List<T> Rent(int size) {
|
|
||||||
int len2 = size;
|
|
||||||
if (len2 < 16) len2 = 16;
|
|
||||||
var list = _buckets[GetID(len2)].Rent();
|
|
||||||
if (list.Count < size)
|
|
||||||
for (int i = list.Count; i < size; i++) list.Add(default(T));
|
|
||||||
else if (list.Count > size)
|
|
||||||
list.RemoveRange(size, list.Count - size);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a rented list to the pool.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="list">The list to return.</param>
|
|
||||||
public void Return(List<T> list) {
|
|
||||||
int len2 = list.Capacity;
|
|
||||||
if (len2 < 16) len2 = 16;
|
|
||||||
_buckets[GetID(len2)].Return(list);
|
|
||||||
}
|
|
||||||
static int GetID(int size) {
|
|
||||||
size -= 1;
|
|
||||||
size >>= 4;
|
|
||||||
int num = 0;
|
|
||||||
for (; size != 0; size >>= 1) num++;
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
static int GetSize(int id) {
|
|
||||||
return 0x10 << id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,42 +0,0 @@
|
|||||||
namespace Cryville.Common.Buffers {
|
|
||||||
/// <summary>
|
|
||||||
/// A resource pool that allows reusing instances of type <typeparamref name="T" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
|
|
||||||
public abstract class ObjectPool<T> where T : class {
|
|
||||||
int _index;
|
|
||||||
readonly T[] _objs;
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="ObjectPool{T}" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="capacity">The capacity of the pool.</param>
|
|
||||||
public ObjectPool(int capacity) {
|
|
||||||
_objs = new T[capacity];
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Rents a object from the pool.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The rented object.</returns>
|
|
||||||
public T Rent() {
|
|
||||||
T obj = null;
|
|
||||||
if (_index < _objs.Length) {
|
|
||||||
obj = _objs[_index];
|
|
||||||
_objs[_index++] = null;
|
|
||||||
}
|
|
||||||
if (obj == null) obj = Construct();
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a rented object to the pool.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="obj">The object to return.</param>
|
|
||||||
public void Return(T obj) {
|
|
||||||
if (_index > 0) _objs[--_index] = obj;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Constructs a new instance of type <typeparamref name="T" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The new instance.</returns>
|
|
||||||
protected abstract T Construct();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
namespace Cryville.Common.Buffers {
|
|
||||||
/// <summary>
|
|
||||||
/// A resource pool that allows reusing instances of type <typeparamref name="T" />, which has a parameterless constructor.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
|
|
||||||
public class SimpleObjectPool<T> : ObjectPool<T> where T : class, new() {
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="SimpleObjectPool{T}" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="capacity">The capacity of the pool.</param>
|
|
||||||
public SimpleObjectPool(int capacity) : base(capacity) { }
|
|
||||||
protected override T Construct() {
|
|
||||||
return new T();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.ComponentModel {
|
namespace Cryville.Common.ComponentModel {
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.ComponentModel {
|
namespace Cryville.Common.ComponentModel {
|
||||||
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
|
||||||
|
@@ -1,14 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.ComponentModel {
|
namespace Cryville.Common.ComponentModel {
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
||||||
public class RangeAttribute : Attribute {
|
public class RangeAttribute : Attribute {
|
||||||
public RangeAttribute(float min, float max) {
|
public RangeAttribute(double min, double max) {
|
||||||
Min = min;
|
Min = min;
|
||||||
Max = max;
|
Max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Min { get; set; }
|
public double Min { get; set; }
|
||||||
public float Max { get; set; }
|
public double Max { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.ComponentModel {
|
namespace Cryville.Common.ComponentModel {
|
||||||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
||||||
public class StepAttribute : Attribute {
|
public class StepAttribute : Attribute {
|
||||||
public StepAttribute(float step) {
|
public StepAttribute(double step) {
|
||||||
Step = step;
|
Step = step;
|
||||||
}
|
}
|
||||||
public float Step { get; set; }
|
public double Step { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
Assets/Cryville/Common/Coroutine.cs
Normal file
26
Assets/Cryville/Common/Coroutine.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Cryville.Common {
|
||||||
|
public class Coroutine {
|
||||||
|
readonly IEnumerator<float> _enumerator;
|
||||||
|
readonly Stopwatch _stopwatch = new();
|
||||||
|
public float Progress { get; private set; }
|
||||||
|
public Coroutine(IEnumerator<float> enumerator) {
|
||||||
|
_enumerator = enumerator;
|
||||||
|
}
|
||||||
|
public bool TickOnce() {
|
||||||
|
if (!_enumerator.MoveNext()) return false;
|
||||||
|
Progress = _enumerator.Current;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public bool Tick(double minTime) {
|
||||||
|
_stopwatch.Restart();
|
||||||
|
while (_stopwatch.Elapsed.TotalSeconds < minTime) {
|
||||||
|
if (!_enumerator.MoveNext()) return false;
|
||||||
|
Progress = _enumerator.Current;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: df66519fa93e1b94ea5bb1702cc91b3f
|
guid: 387adc7d494be0147b7cb930bc2e726b
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common {
|
namespace Cryville.Common {
|
||||||
public class FileStringAttribute : Attribute {
|
public class FileStringAttribute : Attribute {
|
||||||
|
111
Assets/Cryville/Common/Font/FontFile.cs
Normal file
111
Assets/Cryville/Common/Font/FontFile.cs
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
using Cryville.Common.IO;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Font {
|
||||||
|
public abstract class FontFile : IEnumerable<Typeface> {
|
||||||
|
public abstract int Count { get; }
|
||||||
|
public abstract Typeface this[int index] { get; }
|
||||||
|
protected FileInfo File { get; private set; }
|
||||||
|
protected BinaryReader Reader { get; private set; }
|
||||||
|
public FontFile(FileInfo file) {
|
||||||
|
File = file;
|
||||||
|
Reader = new BinaryReaderBE(new FileStream(file.FullName, FileMode.Open, FileAccess.Read));
|
||||||
|
}
|
||||||
|
public void Close() { Reader.Close(); }
|
||||||
|
|
||||||
|
public static FontFile Create(FileInfo file) => file.Extension switch {
|
||||||
|
".ttf" or ".otf" => new FontFileTTF(file),
|
||||||
|
".ttc" or ".otc" => new FontFileTTC(file),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
public Enumerator GetEnumerator() {
|
||||||
|
return new Enumerator(this);
|
||||||
|
}
|
||||||
|
IEnumerator<Typeface> IEnumerable<Typeface>.GetEnumerator() {
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() {
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Enumerator : IEnumerator<Typeface> {
|
||||||
|
readonly FontFile _self;
|
||||||
|
int _index;
|
||||||
|
internal Enumerator(FontFile self) {
|
||||||
|
_self = self;
|
||||||
|
_index = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly Typeface Current {
|
||||||
|
get {
|
||||||
|
if (_index < 0)
|
||||||
|
throw new InvalidOperationException(_index == -1 ? "Enum not started" : "Enum ended");
|
||||||
|
return _self[_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
readonly object IEnumerator.Current => Current;
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
_index = -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext() {
|
||||||
|
if (_index == -2) return false;
|
||||||
|
_index++;
|
||||||
|
if (_index >= _self.Count) {
|
||||||
|
_index = -2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset() {
|
||||||
|
_index = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class FontFileTTF : FontFile {
|
||||||
|
public override int Count { get { return 1; } }
|
||||||
|
public override Typeface this[int index] {
|
||||||
|
get {
|
||||||
|
if (index != 0) throw new ArgumentOutOfRangeException("index");
|
||||||
|
try {
|
||||||
|
return new TypefaceTTF(Reader, File, index);
|
||||||
|
}
|
||||||
|
catch (Exception) {
|
||||||
|
throw new InvalidDataException("Invalid font");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public FontFileTTF(FileInfo file) : base(file) { }
|
||||||
|
}
|
||||||
|
public class FontFileTTC : FontFile {
|
||||||
|
readonly IReadOnlyList<uint> _offsets;
|
||||||
|
public override int Count { get { return _offsets.Count; } }
|
||||||
|
public override Typeface this[int index] {
|
||||||
|
get {
|
||||||
|
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException("index");
|
||||||
|
Reader.BaseStream.Position = _offsets[index];
|
||||||
|
try {
|
||||||
|
return new TypefaceTTF(Reader, File, index);
|
||||||
|
}
|
||||||
|
catch (Exception) {
|
||||||
|
throw new InvalidDataException("Invalid font");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public FontFileTTC(FileInfo file) : base(file) {
|
||||||
|
try {
|
||||||
|
_offsets = new TTCHeader(Reader, 0).GetItems();
|
||||||
|
}
|
||||||
|
catch (Exception) {
|
||||||
|
throw new InvalidDataException("Invalid font");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 2b7b45ff20c33ac47b476371673b037c
|
guid: c9f44ccf8ddd364418b4f4965414ff9c
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
64
Assets/Cryville/Common/Font/FontManager.cs
Normal file
64
Assets/Cryville/Common/Font/FontManager.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Font {
|
||||||
|
public abstract class FontManager {
|
||||||
|
public IReadOnlyDictionary<string, Typeface> MapFullNameToTypeface { get; private set; }
|
||||||
|
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapNameToTypefaces { get; private set; }
|
||||||
|
public FontManager() {
|
||||||
|
var map1 = new Dictionary<string, Typeface>();
|
||||||
|
var map2 = new Dictionary<string, List<Typeface>>();
|
||||||
|
foreach (var f in EnumerateAllTypefaces()) {
|
||||||
|
if (!map1.ContainsKey(f.FullName)) {
|
||||||
|
map1.Add(f.FullName, f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Shared.Logger.Log(3, "UI", "Discarding a font with a duplicate full name {0}", f.FullName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!map2.TryGetValue(f.FamilyName, out List<Typeface> set2)) {
|
||||||
|
map2.Add(f.FamilyName, set2 = new List<Typeface>());
|
||||||
|
}
|
||||||
|
set2.Add(f);
|
||||||
|
}
|
||||||
|
MapFullNameToTypeface = map1;
|
||||||
|
MapNameToTypefaces = map2.ToDictionary(i => i.Key, i => (IReadOnlyCollection<Typeface>)i.Value);
|
||||||
|
}
|
||||||
|
protected abstract IEnumerable<Typeface> EnumerateAllTypefaces();
|
||||||
|
protected static IEnumerable<Typeface> ScanDirectoryForTypefaces(string dir) {
|
||||||
|
foreach (var f in new DirectoryInfo(dir).EnumerateFiles()) {
|
||||||
|
FontFile file;
|
||||||
|
try {
|
||||||
|
file = FontFile.Create(f);
|
||||||
|
}
|
||||||
|
catch (InvalidDataException) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (file == null) continue;
|
||||||
|
var enumerator = file.GetEnumerator();
|
||||||
|
while (enumerator.MoveNext()) {
|
||||||
|
Typeface ret;
|
||||||
|
try {
|
||||||
|
ret = enumerator.Current;
|
||||||
|
}
|
||||||
|
catch (InvalidDataException) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
yield return ret;
|
||||||
|
}
|
||||||
|
file.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class FontManagerAndroid : FontManager {
|
||||||
|
protected override IEnumerable<Typeface> EnumerateAllTypefaces() {
|
||||||
|
return ScanDirectoryForTypefaces("/system/fonts");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class FontManagerWindows : FontManager {
|
||||||
|
protected override IEnumerable<Typeface> EnumerateAllTypefaces() {
|
||||||
|
return ScanDirectoryForTypefaces("C:/Windows/Fonts");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
345
Assets/Cryville/Common/Font/FontMatcher.cs
Normal file
345
Assets/Cryville/Common/Font/FontMatcher.cs
Normal file
@@ -0,0 +1,345 @@
|
|||||||
|
using Cryville.Culture;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Font {
|
||||||
|
public abstract class FontMatcher {
|
||||||
|
protected FontManager Manager { get; private set; }
|
||||||
|
public FontMatcher(FontManager manager) { Manager = manager; }
|
||||||
|
public abstract IEnumerable<Typeface> MatchLanguage(LanguageId lang, bool distinctFamily = false);
|
||||||
|
}
|
||||||
|
public class FallbackListFontMatcher : FontMatcher {
|
||||||
|
readonly LanguageMatching _matcher;
|
||||||
|
static readonly string UltimateFallbackScript = "zzzz";
|
||||||
|
public Dictionary<string, List<string>> MapScriptToTypefaces = new();
|
||||||
|
public static Dictionary<string, List<string>> GetDefaultWindowsFallbackMap() {
|
||||||
|
var map = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
FillKeysWithScripts(map, () => new List<string>());
|
||||||
|
// Reference: https://github.com/chromium/chromium/blob/main/third_party/blink/renderer/platform/fonts/win/font_fallback_win.cc
|
||||||
|
map[UltimateFallbackScript].Insert(0, "SimSun"); // Custom
|
||||||
|
map[UltimateFallbackScript].Insert(0, "SimHei"); // Custom
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Microsoft YaHei"); // Custom
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Arial");
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Times New Roman");
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Segoe UI"); // Custom
|
||||||
|
map["arab"].Insert(0, "Tahoma");
|
||||||
|
map["cyrl"].Insert(0, "Times New Roman");
|
||||||
|
map["grek"].Insert(0, "Times New Roman");
|
||||||
|
map["hebr"].Insert(0, "David");
|
||||||
|
map["jpan"].Insert(0, "MS PGothic");
|
||||||
|
map["latn"].Insert(0, "Times New Roman");
|
||||||
|
map["hans"].Insert(0, "SimSun");
|
||||||
|
map["hans"].Insert(0, "SimHei"); // Custom
|
||||||
|
map["thai"].Insert(0, "Tahoma");
|
||||||
|
map["hans"].Insert(0, "PMingLiU");
|
||||||
|
// Reference: https://learn.microsoft.com/en-us/globalization/input/font-support
|
||||||
|
var ver = Environment.OSVersion.Version;
|
||||||
|
if (ver >= new Version(5, 0)) { // Windows 2000
|
||||||
|
map["armn"].Insert(0, "Sylfaen");
|
||||||
|
map["deva"].Insert(0, "Mangal");
|
||||||
|
map["geor"].Insert(0, "Sylfaen");
|
||||||
|
map["taml"].Insert(0, "Latha");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(5, 1)) { // Windows XP
|
||||||
|
map["gujr"].Insert(0, "Shruti");
|
||||||
|
map["guru"].Insert(0, "Raavi");
|
||||||
|
map["knda"].Insert(0, "Tunga");
|
||||||
|
map["syrc"].Insert(0, "Estrangelo Edessa");
|
||||||
|
map["telu"].Insert(0, "Gautami");
|
||||||
|
map["thaa"].Insert(0, "MV Boli");
|
||||||
|
// SP2
|
||||||
|
map["beng"].Insert(0, "Vrinda");
|
||||||
|
map["mlym"].Insert(0, "Kartika");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(6, 0)) { // Windows Vista
|
||||||
|
map["cans"].Insert(0, "Euphemia");
|
||||||
|
map["cher"].Insert(0, "Plantagenet");
|
||||||
|
map["ethi"].Insert(0, "Nyala");
|
||||||
|
map["khmr"].Insert(0, "DaunPenh MoolBoran");
|
||||||
|
map["kore"].Insert(0, "Malgun Gothic"); // Reference: https://en.wikipedia.org/wiki/List_of_typefaces_included_with_Microsoft_Windows
|
||||||
|
map["laoo"].Insert(0, "DokChampa");
|
||||||
|
map["mong"].Insert(0, "Mongolian Baiti");
|
||||||
|
map["orya"].Insert(0, "Kalinga");
|
||||||
|
map["sinh"].Insert(0, "Iskoola Pota");
|
||||||
|
map["tibt"].Insert(0, "Microsoft Himalaya");
|
||||||
|
map["yiii"].Insert(0, "Microsoft Yi Baiti");
|
||||||
|
map["arab"].Insert(0, "Segoe UI");
|
||||||
|
map["cyrl"].Insert(0, "Segoe UI");
|
||||||
|
map["grek"].Insert(0, "Segoe UI");
|
||||||
|
map["latn"].Insert(0, "Segoe UI");
|
||||||
|
map["hans"].Add("SimSun-ExtB");
|
||||||
|
map["hant"].Add("MingLiU-ExtB");
|
||||||
|
map["hant"].Add("MingLiU_HKSCS-ExtB");
|
||||||
|
map["arab"].Add("Microsoft Uighur");
|
||||||
|
map["zmth"].Insert(0, "Cambria Math");
|
||||||
|
// Reference: https://en.wikipedia.org/wiki/List_of_CJK_fonts
|
||||||
|
map["jpan"].Insert(0, "Meiryo");
|
||||||
|
map["hans"].Insert(0, "Microsoft YaHei");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(6, 1)) { // Windows 7
|
||||||
|
map["brai"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["dsrt"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["talu"].Insert(0, "Microsoft New Tai Lue");
|
||||||
|
map["ogam"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["osma"].Insert(0, "Ebrima");
|
||||||
|
map["phag"].Insert(0, "Microsoft PhagsPa");
|
||||||
|
map["runr"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["zsym"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["tale"].Insert(0, "Microsoft Tai Le");
|
||||||
|
map["tfng"].Insert(0, "Ebrima");
|
||||||
|
map["vaii"].Insert(0, "Ebrima");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(6, 2)) { // Windows 8
|
||||||
|
map["glag"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["goth"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["hang"].Add("Malgun Gothic");
|
||||||
|
map["ital"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["lisu"].Insert(0, "Segoe UI");
|
||||||
|
map["mymr"].Insert(0, "Myanmar Text");
|
||||||
|
map["nkoo"].Insert(0, "Ebrima");
|
||||||
|
map["orkh"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["ethi"].Insert(0, "Ebrima");
|
||||||
|
map["cans"].Insert(0, "Gadugi");
|
||||||
|
map["hant"].Insert(0, "Microsoft JhengHei UI");
|
||||||
|
map["hans"].Insert(0, "Microsoft YaHei UI");
|
||||||
|
map["beng"].Insert(0, "Nirmala UI");
|
||||||
|
map["deva"].Insert(0, "Nirmala UI");
|
||||||
|
map["gujr"].Insert(0, "Nirmala UI");
|
||||||
|
map["guru"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||||
|
map["knda"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||||
|
map["mlym"].Insert(0, "Nirmala UI");
|
||||||
|
map["orya"].Insert(0, "Nirmala UI");
|
||||||
|
map["sinh"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||||
|
map["taml"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||||
|
map["telu"].Insert(0, "Nirmala UI");
|
||||||
|
map["armn"].Insert(0, "Segoe UI");
|
||||||
|
map["geor"].Insert(0, "Segoe UI");
|
||||||
|
map["hebr"].Insert(0, "Segoe UI");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(6, 3)) { // Windows 8.1
|
||||||
|
map["bugi"].Insert(0, "Leelawadee UI");
|
||||||
|
map["copt"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["java"].Insert(0, "Javanese Text");
|
||||||
|
map["merc"].Insert(0, "Segoe UI Symbol");
|
||||||
|
map["olck"].Insert(0, "Nirmala UI");
|
||||||
|
map["sora"].Insert(0, "Nirmala UI");
|
||||||
|
map["khmr"].Insert(0, "Leelawadee UI");
|
||||||
|
map["laoo"].Insert(0, "Leelawadee UI");
|
||||||
|
map["thai"].Insert(0, "Leelawadee UI");
|
||||||
|
map["zsye"].Insert(0, "Segoe UI Emoji");
|
||||||
|
}
|
||||||
|
if (ver >= new Version(10, 0)) { // Windows 10
|
||||||
|
map["brah"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["cari"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["cprt"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["egyp"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["armi"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["phli"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["prti"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["khar"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["lyci"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["lydi"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["phnx"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["xpeo"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["sarb"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["shaw"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["xsux"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["ugar"].Insert(0, "Segoe UI Historic");
|
||||||
|
// Segoe UI Symbol -> Segoe UI Historic
|
||||||
|
map["glag"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["goth"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["merc"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["ogam"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["ital"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["orkh"].Insert(0, "Segoe UI Historic");
|
||||||
|
map["runr"].Insert(0, "Segoe UI Historic");
|
||||||
|
//
|
||||||
|
map["jpan"].Insert(0, "Yu Gothic UI");
|
||||||
|
map["zsym"].Add("Segoe MDL2 Assets");
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
public static Dictionary<string, List<string>> GetDefaultAndroidFallbackMap() {
|
||||||
|
var map = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
FillKeysWithScripts(map, () => new List<string>());
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Noto Sans CJK TC"); // TODO Modify default fallback
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Noto Sans CJK JP");
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Noto Sans CJK SC");
|
||||||
|
map[UltimateFallbackScript].Insert(0, "Roboto");
|
||||||
|
map["zsye"].Insert(0, "Noto Color Emoji");
|
||||||
|
map["zsye"].Add("Noto Color Emoji Flags");
|
||||||
|
map["arab"].Insert(0, "Noto Naskh Arabic");
|
||||||
|
map["adlm"].Insert(0, "Noto Sans Adlam");
|
||||||
|
map["ahom"].Insert(0, "Noto Sans Ahom");
|
||||||
|
map["hluw"].Insert(0, "Noto Sans Anatolian Hieroglyphs");
|
||||||
|
map["armn"].Insert(0, "Noto Sans Armenian");
|
||||||
|
map["avst"].Insert(0, "Noto Sans Avestan");
|
||||||
|
map["bali"].Insert(0, "Noto Sans Balinese");
|
||||||
|
map["bamu"].Insert(0, "Noto Sans Bamum");
|
||||||
|
map["bass"].Insert(0, "Noto Sans Bassa Vah");
|
||||||
|
map["batk"].Insert(0, "Noto Sans Batak");
|
||||||
|
map["beng"].Insert(0, "Noto Sans Bengali");
|
||||||
|
map["bhks"].Insert(0, "Noto Sans Bhaiksuki");
|
||||||
|
map["brah"].Insert(0, "Noto Sans Brahmi");
|
||||||
|
map["bugi"].Insert(0, "Noto Sans Buginese");
|
||||||
|
map["buhd"].Insert(0, "Noto Sans Buhid");
|
||||||
|
map["jpan"].Insert(0, "Noto Sans CJK JP");
|
||||||
|
map["kore"].Insert(0, "Noto Sans CJK KR");
|
||||||
|
map["hans"].Insert(0, "Noto Sans CJK SC");
|
||||||
|
map["hant"].Insert(0, "Noto Sans CJK TC");
|
||||||
|
map["hant"].Add("Noto Sans CJK HK");
|
||||||
|
map["cans"].Insert(0, "Noto Sans Canadian Aboriginal");
|
||||||
|
map["cari"].Insert(0, "Noto Sans Carian");
|
||||||
|
map["cakm"].Insert(0, "Noto Sans Chakma");
|
||||||
|
map["cham"].Insert(0, "Noto Sans Cham");
|
||||||
|
map["cher"].Insert(0, "Noto Sans Cherokee");
|
||||||
|
map["copt"].Insert(0, "Noto Sans Coptic");
|
||||||
|
map["xsux"].Insert(0, "Noto Sans Cuneiform");
|
||||||
|
map["cprt"].Insert(0, "Noto Sans Cypriot");
|
||||||
|
map["dsrt"].Insert(0, "Noto Sans Deseret");
|
||||||
|
map["deva"].Insert(0, "Noto Sans Devanagari");
|
||||||
|
map["egyp"].Insert(0, "Noto Sans Egyptian Hieroglyphs");
|
||||||
|
map["elba"].Insert(0, "Noto Sans Elbasan");
|
||||||
|
map["ethi"].Insert(0, "Noto Sans Ethiopic");
|
||||||
|
map["geor"].Insert(0, "Noto Sans Georgian");
|
||||||
|
map["glag"].Insert(0, "Noto Sans Glagolitic");
|
||||||
|
map["goth"].Insert(0, "Noto Sans Gothic");
|
||||||
|
map["gran"].Insert(0, "Noto Sans Grantha");
|
||||||
|
map["gujr"].Insert(0, "Noto Sans Gujarati");
|
||||||
|
map["gong"].Insert(0, "Noto Sans Gunjala Gondi");
|
||||||
|
map["guru"].Insert(0, "Noto Sans Gurmukhi");
|
||||||
|
map["rohg"].Insert(0, "Noto Sans Hanifi Rohingya");
|
||||||
|
map["hano"].Insert(0, "Noto Sans Hanunoo");
|
||||||
|
map["hatr"].Insert(0, "Noto Sans Hatran");
|
||||||
|
map["hebr"].Insert(0, "Noto Sans Hebrew");
|
||||||
|
map["armi"].Insert(0, "Noto Sans Imperial Aramaic");
|
||||||
|
map["phli"].Insert(0, "Noto Sans Inscriptional Pahlavi");
|
||||||
|
map["prti"].Insert(0, "Noto Sans Inscriptional Parthian");
|
||||||
|
map["java"].Insert(0, "Noto Sans Javanese");
|
||||||
|
map["kthi"].Insert(0, "Noto Sans Kaithi");
|
||||||
|
map["knda"].Insert(0, "Noto Sans Kannada");
|
||||||
|
map["kali"].Insert(0, "Noto Sans KayahLi");
|
||||||
|
map["khar"].Insert(0, "Noto Sans Kharoshthi");
|
||||||
|
map["khmr"].Insert(0, "Noto Sans Khmer");
|
||||||
|
map["khoj"].Insert(0, "Noto Sans Khojki");
|
||||||
|
map["laoo"].Insert(0, "Noto Sans Lao");
|
||||||
|
map["lepc"].Insert(0, "Noto Sans Lepcha");
|
||||||
|
map["limb"].Insert(0, "Noto Sans Limbu");
|
||||||
|
map["lina"].Insert(0, "Noto Sans Linear A");
|
||||||
|
map["linb"].Insert(0, "Noto Sans Linear B");
|
||||||
|
map["lisu"].Insert(0, "Noto Sans Lisu");
|
||||||
|
map["lyci"].Insert(0, "Noto Sans Lycian");
|
||||||
|
map["lydi"].Insert(0, "Noto Sans Lydian");
|
||||||
|
map["mlym"].Insert(0, "Noto Sans Malayalam");
|
||||||
|
map["mand"].Insert(0, "Noto Sans Mandiac");
|
||||||
|
map["mani"].Insert(0, "Noto Sans Manichaean");
|
||||||
|
map["marc"].Insert(0, "Noto Sans Marchen");
|
||||||
|
map["gonm"].Insert(0, "Noto Sans Masaram Gondi");
|
||||||
|
map["medf"].Insert(0, "Noto Sans Medefaidrin");
|
||||||
|
map["mtei"].Insert(0, "Noto Sans Meetei Mayek");
|
||||||
|
map["merc"].Insert(0, "Noto Sans Meroitic");
|
||||||
|
map["mero"].Insert(0, "Noto Sans Meroitic");
|
||||||
|
map["plrd"].Insert(0, "Noto Sans Miao");
|
||||||
|
map["modi"].Insert(0, "Noto Sans Modi");
|
||||||
|
map["mong"].Insert(0, "Noto Sans Mongolian");
|
||||||
|
map["mroo"].Insert(0, "Noto Sans Mro");
|
||||||
|
map["mult"].Insert(0, "Noto Sans Multani");
|
||||||
|
map["mymr"].Insert(0, "Noto Sans Myanmar");
|
||||||
|
map["nkoo"].Insert(0, "Noto Sans Nko");
|
||||||
|
map["nbat"].Insert(0, "Noto Sans Nabataean");
|
||||||
|
map["talu"].Insert(0, "Noto Sans New Tai Lue");
|
||||||
|
map["newa"].Insert(0, "Noto Sans Newa");
|
||||||
|
map["ogam"].Insert(0, "Noto Sans Ogham");
|
||||||
|
map["olck"].Insert(0, "Noto Sans Ol Chiki");
|
||||||
|
map["ital"].Insert(0, "Noto Sans Old Italian");
|
||||||
|
map["narb"].Insert(0, "Noto Sans Old North Arabian");
|
||||||
|
map["perm"].Insert(0, "Noto Sans Old Permic");
|
||||||
|
map["xpeo"].Insert(0, "Noto Sans Old Persian");
|
||||||
|
map["sarb"].Insert(0, "Noto Sans Old South Arabian");
|
||||||
|
map["orkh"].Insert(0, "Noto Sans Old Turkic");
|
||||||
|
map["orya"].Insert(0, "Noto Sans Oriya");
|
||||||
|
map["osge"].Insert(0, "Noto Sans Osage");
|
||||||
|
map["osma"].Insert(0, "Noto Sans Osmanya");
|
||||||
|
map["hmng"].Insert(0, "Noto Sans Pahawh Hmong");
|
||||||
|
map["palm"].Insert(0, "Noto Sans Palmyrene");
|
||||||
|
map["pauc"].Insert(0, "Noto Sans Pau Cin Hau");
|
||||||
|
map["phag"].Insert(0, "Noto Sans Phags Pa");
|
||||||
|
map["phnx"].Insert(0, "Noto Sans Phoenician");
|
||||||
|
map["rjng"].Insert(0, "Noto Sans Rejang");
|
||||||
|
map["runr"].Insert(0, "Noto Sans Runic");
|
||||||
|
map["samr"].Insert(0, "Noto Sans Samaritan");
|
||||||
|
map["saur"].Insert(0, "Noto Sans Saurashtra");
|
||||||
|
map["shrd"].Insert(0, "Noto Sans Sharada");
|
||||||
|
map["shaw"].Insert(0, "Noto Sans Shavian");
|
||||||
|
map["sinh"].Insert(0, "Noto Sans Sinhala");
|
||||||
|
map["sora"].Insert(0, "Noto Sans Sora Sompeng");
|
||||||
|
map["soyo"].Insert(0, "Noto Sans Soyombo");
|
||||||
|
map["sund"].Insert(0, "Noto Sans Sundanese");
|
||||||
|
map["sylo"].Insert(0, "Noto Sans Syloti Nagri");
|
||||||
|
map["zsym"].Insert(0, "Noto Sans Symbols");
|
||||||
|
map["syrc"].Add("Noto Sans Syriac Eastern");
|
||||||
|
map["syrc"].Add("Noto Sans Syriac Western");
|
||||||
|
map["syrc"].Add("Noto Sans Syriac Estrangela");
|
||||||
|
map["tglg"].Insert(0, "Noto Sans Tagalog");
|
||||||
|
map["tagb"].Insert(0, "Noto Sans Tagbanwa");
|
||||||
|
map["tale"].Insert(0, "Noto Sans Tai Le");
|
||||||
|
map["lana"].Insert(0, "Noto Sans Tai Tham");
|
||||||
|
map["tavt"].Insert(0, "Noto Sans Tai Viet");
|
||||||
|
map["takr"].Insert(0, "Noto Sans Takri");
|
||||||
|
map["taml"].Insert(0, "Noto Sans Tamil");
|
||||||
|
map["telu"].Insert(0, "Noto Sans Telugu");
|
||||||
|
map["thaa"].Insert(0, "Noto Sans Thaana");
|
||||||
|
map["thai"].Insert(0, "Noto Sans Thai");
|
||||||
|
map["tfng"].Insert(0, "Noto Sans Tifinagh");
|
||||||
|
map["ugar"].Insert(0, "Noto Sans Ugaritic");
|
||||||
|
map["vaii"].Insert(0, "Noto Sans Vai");
|
||||||
|
map["wcho"].Insert(0, "Noto Sans Wancho");
|
||||||
|
map["wara"].Insert(0, "Noto Sans Warang Citi");
|
||||||
|
map["yiii"].Insert(0, "Noto Sans Yi");
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
static void FillKeysWithScripts<T>(IDictionary<string, T> map, Func<T> value) {
|
||||||
|
foreach (var s in IdValidity.Enumerate("script")) map.Add(s, value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public FallbackListFontMatcher(LanguageMatching matcher, FontManager manager) : base(manager) {
|
||||||
|
_matcher = matcher;
|
||||||
|
}
|
||||||
|
public override IEnumerable<Typeface> MatchLanguage(LanguageId lang, bool distinctFamily = false) {
|
||||||
|
var supported = MapScriptToTypefaces.Keys.Select(i => new LanguageId(i)).ToList();
|
||||||
|
bool flag = false;
|
||||||
|
while (_matcher.Match(lang, supported, out var match, out var distance)) {
|
||||||
|
if (distance > 40) break;
|
||||||
|
Shared.Logger.Log(0, "UI", "Matching fonts for language {0}, distance = {1}", match, distance);
|
||||||
|
if (match.Script.Equals(UltimateFallbackScript, StringComparison.OrdinalIgnoreCase)) {
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
var candidates = MapScriptToTypefaces[match.Script];
|
||||||
|
foreach (var typeface in EnumerateTypefaces(candidates, distinctFamily)) {
|
||||||
|
yield return typeface;
|
||||||
|
}
|
||||||
|
supported.Remove(match);
|
||||||
|
}
|
||||||
|
if (flag) yield break;
|
||||||
|
Shared.Logger.Log(0, "UI", "Matching fallback fonts");
|
||||||
|
foreach (var typeface in EnumerateTypefaces(MapScriptToTypefaces[UltimateFallbackScript], distinctFamily)) {
|
||||||
|
yield return typeface;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IEnumerable<Typeface> EnumerateTypefaces(List<string> candidates, bool distinctFamily) {
|
||||||
|
foreach (var candidate in candidates) {
|
||||||
|
if (Manager.MapFullNameToTypeface.TryGetValue(candidate, out var typeface1)) {
|
||||||
|
yield return typeface1;
|
||||||
|
}
|
||||||
|
if (distinctFamily) continue;
|
||||||
|
if (Manager.MapNameToTypefaces.TryGetValue(candidate, out IReadOnlyCollection<Typeface> typefaces2)) {
|
||||||
|
foreach (var typeface in typefaces2) {
|
||||||
|
if (typeface1 == typeface) continue;
|
||||||
|
yield return typeface;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 2745c44c3cc32be4ab3a43888c14b9a1
|
guid: afcde0ad1865db24da79ca1ce7256791
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
254
Assets/Cryville/Common/Font/FontTable.cs
Normal file
254
Assets/Cryville/Common/Font/FontTable.cs
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
#pragma warning disable IDE0049
|
||||||
|
namespace Cryville.Common.Font {
|
||||||
|
public abstract class FontTable<T> {
|
||||||
|
protected UInt32 Offset { get; private set; }
|
||||||
|
protected BinaryReader Reader { get; private set; }
|
||||||
|
|
||||||
|
protected FontTable(BinaryReader reader, UInt32 offset) {
|
||||||
|
Reader = reader;
|
||||||
|
Offset = offset;
|
||||||
|
reader.BaseStream.Position = offset;
|
||||||
|
}
|
||||||
|
public abstract IReadOnlyList<T> GetItems();
|
||||||
|
}
|
||||||
|
public abstract class FontTable<T, U> : FontTable<T> {
|
||||||
|
protected FontTable(BinaryReader reader, UInt32 offset) : base(reader, offset) { }
|
||||||
|
public abstract U GetSubTable(T item);
|
||||||
|
}
|
||||||
|
public sealed class TTCHeader : FontTable<UInt32, TableDirectory> {
|
||||||
|
readonly String ttcTag;
|
||||||
|
readonly UInt16 majorVersion;
|
||||||
|
readonly UInt16 minorVersion;
|
||||||
|
readonly UInt32 numFonts;
|
||||||
|
readonly List<UInt32> tableDirectoryOffsets = new();
|
||||||
|
#pragma warning disable IDE0052 // Reserved
|
||||||
|
readonly String dsigTag;
|
||||||
|
readonly UInt32 dsigLength;
|
||||||
|
readonly UInt32 dsigOffset;
|
||||||
|
#pragma warning restore IDE0052 // Reserved
|
||||||
|
public TTCHeader(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||||
|
ttcTag = reader.ReadTag();
|
||||||
|
if (ttcTag != "ttcf") throw new NotSupportedException();
|
||||||
|
majorVersion = reader.ReadUInt16();
|
||||||
|
minorVersion = reader.ReadUInt16();
|
||||||
|
if (minorVersion != 0) throw new NotSupportedException();
|
||||||
|
numFonts = reader.ReadUInt32();
|
||||||
|
for (UInt32 i = 0; i < numFonts; i++) tableDirectoryOffsets.Add(reader.ReadUInt32());
|
||||||
|
if (majorVersion == 2) {
|
||||||
|
dsigTag = reader.ReadTag();
|
||||||
|
dsigLength = reader.ReadUInt32();
|
||||||
|
dsigOffset = reader.ReadUInt32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override IReadOnlyList<UInt32> GetItems() {
|
||||||
|
return tableDirectoryOffsets;
|
||||||
|
}
|
||||||
|
public override TableDirectory GetSubTable(UInt32 item) {
|
||||||
|
var i = item;
|
||||||
|
return new TableDirectory(Reader, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public sealed class TableDirectory : FontTable<TableRecord, object> {
|
||||||
|
readonly UInt32 sfntVersion;
|
||||||
|
readonly UInt16 numTables;
|
||||||
|
#pragma warning disable IDE0052 // Reserved
|
||||||
|
readonly UInt16 searchRange;
|
||||||
|
readonly UInt16 entrySelector;
|
||||||
|
readonly UInt16 rangeShift;
|
||||||
|
#pragma warning restore IDE0052 // Reserved
|
||||||
|
readonly List<TableRecord> tableRecords = new();
|
||||||
|
public TableDirectory(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||||
|
sfntVersion = reader.ReadUInt32();
|
||||||
|
if (sfntVersion != 0x00010000 && sfntVersion != 0x4F54544F &&
|
||||||
|
sfntVersion != 0x74727565 && sfntVersion != 0x74797031) throw new NotSupportedException();
|
||||||
|
numTables = reader.ReadUInt16();
|
||||||
|
searchRange = reader.ReadUInt16();
|
||||||
|
entrySelector = reader.ReadUInt16();
|
||||||
|
rangeShift = reader.ReadUInt16();
|
||||||
|
for (int i = 0; i < numTables; i++)
|
||||||
|
tableRecords.Add(new TableRecord {
|
||||||
|
tableTag = reader.ReadTag(),
|
||||||
|
checksum = reader.ReadUInt32(),
|
||||||
|
offset = reader.ReadUInt32(),
|
||||||
|
length = reader.ReadUInt32(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public override IReadOnlyList<TableRecord> GetItems() {
|
||||||
|
return tableRecords;
|
||||||
|
}
|
||||||
|
public override object GetSubTable(TableRecord item) => item.tableTag switch {
|
||||||
|
"name" => new NameTable(Reader, item.offset),
|
||||||
|
"meta" => new MetaTable(Reader, item.offset),
|
||||||
|
_ => throw new NotImplementedException(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public struct TableRecord {
|
||||||
|
public string tableTag;
|
||||||
|
public UInt32 checksum;
|
||||||
|
public UInt32 offset;
|
||||||
|
public UInt32 length;
|
||||||
|
}
|
||||||
|
public sealed class NameTable : FontTable<NameRecord> {
|
||||||
|
readonly UInt16 version;
|
||||||
|
readonly UInt16 count;
|
||||||
|
readonly UInt16 storageOffset;
|
||||||
|
readonly List<NameRecord> nameRecord = new();
|
||||||
|
readonly UInt16 langTagCount;
|
||||||
|
readonly List<LangTagRecord> langTagRecord = new();
|
||||||
|
public NameTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||||
|
version = reader.ReadUInt16();
|
||||||
|
count = reader.ReadUInt16();
|
||||||
|
storageOffset = reader.ReadUInt16();
|
||||||
|
for (UInt16 i = 0; i < count; i++)
|
||||||
|
nameRecord.Add(new NameRecord(
|
||||||
|
reader.ReadUInt16(),
|
||||||
|
reader.ReadUInt16(),
|
||||||
|
reader.ReadUInt16(),
|
||||||
|
(NameID)reader.ReadUInt16(),
|
||||||
|
reader.ReadUInt16(),
|
||||||
|
reader.ReadUInt16()
|
||||||
|
));
|
||||||
|
if (version == 1) {
|
||||||
|
langTagCount = reader.ReadUInt16();
|
||||||
|
for (UInt16 i = 0; i < langTagCount; i++)
|
||||||
|
langTagRecord.Add(new LangTagRecord(
|
||||||
|
reader.ReadUInt16(),
|
||||||
|
reader.ReadUInt16()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
foreach (var i in nameRecord)
|
||||||
|
i.Load(reader, offset + storageOffset);
|
||||||
|
if (version == 1) {
|
||||||
|
foreach (var i in langTagRecord)
|
||||||
|
i.Load(reader, offset + storageOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public sealed override IReadOnlyList<NameRecord> GetItems() {
|
||||||
|
return nameRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class NameRecord {
|
||||||
|
public UInt16 PlatformID { get; private set; }
|
||||||
|
public UInt16 EncodingID { get; private set; }
|
||||||
|
public UInt16 LanguageID { get; private set; }
|
||||||
|
public NameID NameID { get; private set; }
|
||||||
|
public UInt16 Length { get; private set; }
|
||||||
|
public UInt16 StringOffset { get; private set; }
|
||||||
|
public String Value { get; private set; }
|
||||||
|
public NameRecord(UInt16 platformID, UInt16 encodingID, UInt16 languageID, NameID nameID, UInt16 length, UInt16 stringOffset) {
|
||||||
|
PlatformID = platformID;
|
||||||
|
EncodingID = encodingID;
|
||||||
|
LanguageID = languageID;
|
||||||
|
NameID = nameID;
|
||||||
|
Length = length;
|
||||||
|
StringOffset = stringOffset;
|
||||||
|
}
|
||||||
|
public void Load(BinaryReader reader, UInt32 origin) {
|
||||||
|
reader.BaseStream.Position = origin + StringOffset;
|
||||||
|
Encoding encoding;
|
||||||
|
try {
|
||||||
|
switch (PlatformID) {
|
||||||
|
case 0: encoding = Encoding.BigEndianUnicode; break;
|
||||||
|
case 1: encoding = Encoding.GetEncoding(10000 + EncodingID); break;
|
||||||
|
case 3: encoding = Encoding.BigEndianUnicode; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (NotSupportedException) { return; }
|
||||||
|
catch (ArgumentException) { return; }
|
||||||
|
Value = encoding.GetString(reader.ReadBytes(Length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public enum NameID : UInt16 {
|
||||||
|
CopyrightNotice = 0,
|
||||||
|
FontFamilyName = 1,
|
||||||
|
FontSubfamilyName = 2,
|
||||||
|
UniqueFontIdentifier = 3,
|
||||||
|
FullFontName = 4,
|
||||||
|
VersionString = 5,
|
||||||
|
PostScriptName = 6,
|
||||||
|
Trademark = 7,
|
||||||
|
ManufacturerName = 8,
|
||||||
|
Designer = 9,
|
||||||
|
Description = 10,
|
||||||
|
URLVendor = 11,
|
||||||
|
URLDesigner = 12,
|
||||||
|
LicenseDescription = 13,
|
||||||
|
LicenseInfoURL = 14,
|
||||||
|
|
||||||
|
TypographicFamilyName = 16,
|
||||||
|
TypographicSubfamilyName = 17,
|
||||||
|
CompatibleFull = 18,
|
||||||
|
SampleText = 19,
|
||||||
|
PostScriptCIDFindfontName = 20,
|
||||||
|
WWSFamilyName = 21,
|
||||||
|
WWSSubfamilyName = 22,
|
||||||
|
LightBackgroundPalette = 23,
|
||||||
|
DarkBackgroundPalette = 24,
|
||||||
|
VariationsPostScriptNamePrefix = 25,
|
||||||
|
}
|
||||||
|
public class LangTagRecord {
|
||||||
|
public UInt16 Length { get; private set; }
|
||||||
|
public UInt16 LangTagOffset { get; private set; }
|
||||||
|
public String Value { get; private set; }
|
||||||
|
public LangTagRecord(UInt16 length, UInt16 langTagOffset) {
|
||||||
|
Length = length;
|
||||||
|
LangTagOffset = langTagOffset;
|
||||||
|
}
|
||||||
|
public void Load(BinaryReader reader, UInt32 origin) {
|
||||||
|
reader.BaseStream.Position = origin + LangTagOffset;
|
||||||
|
Value = Encoding.BigEndianUnicode.GetString(reader.ReadBytes(Length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public sealed class MetaTable : FontTable<DataMap> {
|
||||||
|
readonly UInt32 version;
|
||||||
|
#pragma warning disable IDE0052 // Reserved
|
||||||
|
readonly UInt32 flags;
|
||||||
|
#pragma warning restore IDE0052 // Reserved
|
||||||
|
readonly UInt32 dataMapCount;
|
||||||
|
readonly List<DataMap> dataMaps = new();
|
||||||
|
public MetaTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||||
|
version = reader.ReadUInt32();
|
||||||
|
if (version != 1) throw new NotSupportedException();
|
||||||
|
flags = reader.ReadUInt32();
|
||||||
|
reader.ReadUInt32();
|
||||||
|
dataMapCount = reader.ReadUInt32();
|
||||||
|
for (UInt32 i = 0; i < dataMapCount; i++)
|
||||||
|
dataMaps.Add(new DataMap (
|
||||||
|
reader.ReadTag(),
|
||||||
|
reader.ReadUInt32(),
|
||||||
|
reader.ReadUInt32()
|
||||||
|
));
|
||||||
|
foreach (var i in dataMaps)
|
||||||
|
i.Load(reader, offset);
|
||||||
|
}
|
||||||
|
public sealed override IReadOnlyList<DataMap> GetItems() {
|
||||||
|
return dataMaps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class DataMap {
|
||||||
|
public String Tag { get; private set; }
|
||||||
|
public UInt32 DataOffset { get; private set; }
|
||||||
|
public UInt32 DataLength { get; private set; }
|
||||||
|
public String Value { get; private set; }
|
||||||
|
public DataMap(String tag, UInt32 dataOffset, UInt32 dataLength) {
|
||||||
|
Tag = tag;
|
||||||
|
DataOffset = dataOffset;
|
||||||
|
DataLength = dataLength;
|
||||||
|
}
|
||||||
|
public void Load(BinaryReader reader, UInt32 origin) {
|
||||||
|
reader.BaseStream.Position = origin + DataOffset;
|
||||||
|
Value = Encoding.ASCII.GetString(reader.ReadBytes((int)DataLength));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static class BinaryReaderExtensions {
|
||||||
|
public static string ReadTag(this BinaryReader reader) {
|
||||||
|
return Encoding.ASCII.GetString(reader.ReadBytes(4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: b8cd439340f088d4eb83711a5bc6384d
|
guid: 3eed6aa2387582346b7b21c6f8de5e1f
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
@@ -1,9 +0,0 @@
|
|||||||
using System.Globalization;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Font {
|
|
||||||
public static class FontUtil {
|
|
||||||
/*public static string MatchFontNameWithLang(string lang) {
|
|
||||||
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
|
31
Assets/Cryville/Common/Font/Typeface.cs
Normal file
31
Assets/Cryville/Common/Font/Typeface.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Font {
|
||||||
|
public abstract class Typeface {
|
||||||
|
public FileInfo File { get; private set; }
|
||||||
|
public int IndexInFile { get; private set; }
|
||||||
|
public string FamilyName { get; protected set; }
|
||||||
|
public string SubfamilyName { get; protected set; }
|
||||||
|
public string FullName { get; protected set; }
|
||||||
|
protected abstract void GetName(BinaryReader reader);
|
||||||
|
|
||||||
|
public Typeface(BinaryReader reader, FileInfo file, int index) {
|
||||||
|
File = file;
|
||||||
|
IndexInFile = index;
|
||||||
|
GetName(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class TypefaceTTF : Typeface {
|
||||||
|
public TypefaceTTF(BinaryReader reader, FileInfo file, int index)
|
||||||
|
: base(reader, file, index) { }
|
||||||
|
|
||||||
|
protected override void GetName(BinaryReader reader) {
|
||||||
|
var dir = new TableDirectory(reader, (uint)reader.BaseStream.Position);
|
||||||
|
var nameTable = (NameTable)dir.GetSubTable((from i in dir.GetItems() where i.tableTag == "name" select i).Single());
|
||||||
|
FamilyName = (from i in nameTable.GetItems() where i.NameID == NameID.FontFamilyName && i.Value != null select i.Value).First();
|
||||||
|
SubfamilyName = (from i in nameTable.GetItems() where i.NameID == NameID.FontSubfamilyName && i.Value != null select i.Value).First();
|
||||||
|
FullName = (from i in nameTable.GetItems() where i.NameID == NameID.FullFontName && i.Value != null select i.Value).First();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Font/Typeface.cs.meta
Normal file
11
Assets/Cryville/Common/Font/Typeface.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0968fc12b50cffb4682f0c28d0d14703
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: b9bd9e24d7c553341a2a12391843542f
|
guid: aaa0d8cecafb37b46a6abe372cfefd93
|
||||||
folderAsset: yes
|
folderAsset: yes
|
||||||
DefaultImporter:
|
DefaultImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
75
Assets/Cryville/Common/IO/BinaryReaderBE.cs
Normal file
75
Assets/Cryville/Common/IO/BinaryReaderBE.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Cryville.Common.IO {
|
||||||
|
public class BinaryReaderBE : BinaryReader {
|
||||||
|
readonly byte[] m_buffer = new byte[8];
|
||||||
|
|
||||||
|
public BinaryReaderBE(Stream input) : base(input) { }
|
||||||
|
|
||||||
|
public BinaryReaderBE(Stream input, Encoding encoding) : base(input, encoding) { }
|
||||||
|
|
||||||
|
public BinaryReaderBE(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) { }
|
||||||
|
|
||||||
|
public override short ReadInt16() {
|
||||||
|
FillBuffer(2);
|
||||||
|
return (short)(m_buffer[1] | (m_buffer[0] << 8));
|
||||||
|
}
|
||||||
|
public override ushort ReadUInt16() {
|
||||||
|
FillBuffer(2);
|
||||||
|
return (ushort)(m_buffer[1] | (m_buffer[0] << 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int ReadInt32() {
|
||||||
|
FillBuffer(4);
|
||||||
|
return m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24);
|
||||||
|
}
|
||||||
|
public override uint ReadUInt32() {
|
||||||
|
FillBuffer(4);
|
||||||
|
return (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override long ReadInt64() {
|
||||||
|
FillBuffer(8);
|
||||||
|
uint num = (uint)(m_buffer[7] | (m_buffer[6] << 8) | (m_buffer[5] << 16) | (m_buffer[4] << 24));
|
||||||
|
uint num2 = (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||||
|
return (long)(((ulong)num2 << 32) | num);
|
||||||
|
}
|
||||||
|
public override ulong ReadUInt64() {
|
||||||
|
FillBuffer(8);
|
||||||
|
uint num = (uint)(m_buffer[7] | (m_buffer[6] << 8) | (m_buffer[5] << 16) | (m_buffer[4] << 24));
|
||||||
|
uint num2 = (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||||
|
return ((ulong)num2 << 32) | num;
|
||||||
|
}
|
||||||
|
protected new void FillBuffer(int numBytes) {
|
||||||
|
if (m_buffer != null && (numBytes < 0 || numBytes > m_buffer.Length)) {
|
||||||
|
throw new ArgumentOutOfRangeException("numBytes", "Requested numBytes is larger than the internal buffer size");
|
||||||
|
}
|
||||||
|
|
||||||
|
int num = 0, num2;
|
||||||
|
if (BaseStream == null) {
|
||||||
|
throw new IOException("File not open");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numBytes == 1) {
|
||||||
|
num2 = BaseStream.ReadByte();
|
||||||
|
if (num2 == -1) {
|
||||||
|
throw new EndOfStreamException("The end of the stream is reached before numBytes could be read");
|
||||||
|
}
|
||||||
|
m_buffer[0] = (byte)num2;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
num2 = BaseStream.Read(m_buffer, num, numBytes - num);
|
||||||
|
if (num2 == 0) {
|
||||||
|
throw new EndOfStreamException("The end of the stream is reached before numBytes could be read");
|
||||||
|
}
|
||||||
|
|
||||||
|
num += num2;
|
||||||
|
}
|
||||||
|
while (num < numBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/IO/BinaryReaderBE.cs.meta
Normal file
11
Assets/Cryville/Common/IO/BinaryReaderBE.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: aee537c74ab935940b54cb5d784b7f56
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -25,7 +25,7 @@ namespace Cryville.Common {
|
|||||||
/// <param name="encoding">The encoding of the string.</param>
|
/// <param name="encoding">The encoding of the string.</param>
|
||||||
/// <returns>The string read from the reader.</returns>
|
/// <returns>The string read from the reader.</returns>
|
||||||
public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) {
|
public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) {
|
||||||
if (encoding == null) encoding = Encoding.UTF8;
|
encoding ??= Encoding.UTF8;
|
||||||
var len = reader.ReadUInt16();
|
var len = reader.ReadUInt16();
|
||||||
byte[] buffer = reader.ReadBytes(len);
|
byte[] buffer = reader.ReadBytes(len);
|
||||||
return encoding.GetString(buffer);
|
return encoding.GetString(buffer);
|
||||||
@@ -38,7 +38,7 @@ namespace Cryville.Common {
|
|||||||
/// <param name="value">The string to write by the writer.</param>
|
/// <param name="value">The string to write by the writer.</param>
|
||||||
/// <param name="encoding">The encoding of the string.</param>
|
/// <param name="encoding">The encoding of the string.</param>
|
||||||
public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) {
|
public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) {
|
||||||
if (encoding == null) encoding = Encoding.UTF8;
|
encoding ??= Encoding.UTF8;
|
||||||
byte[] buffer = encoding.GetBytes(value);
|
byte[] buffer = encoding.GetBytes(value);
|
||||||
writer.Write((ushort)buffer.Length);
|
writer.Write((ushort)buffer.Length);
|
||||||
writer.Write(buffer);
|
writer.Write(buffer);
|
||||||
|
@@ -1,34 +1,35 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common {
|
namespace Cryville.Common {
|
||||||
public struct Identifier : IEquatable<Identifier> {
|
public struct Identifier : IEquatable<Identifier> {
|
||||||
|
public static Identifier Empty = new(0);
|
||||||
public int Key { get; private set; }
|
public int Key { get; private set; }
|
||||||
public object Name { get { return IdentifierManager.SharedInstance.Retrieve(Key); } }
|
public readonly object Name => IdentifierManager.Shared.Retrieve(Key);
|
||||||
public Identifier(int key) {
|
public Identifier(int key) {
|
||||||
Key = key;
|
Key = key;
|
||||||
}
|
}
|
||||||
public Identifier(object name) {
|
public Identifier(object name) {
|
||||||
Key = IdentifierManager.SharedInstance.Request(name);
|
Key = IdentifierManager.Shared.Request(name);
|
||||||
}
|
}
|
||||||
public override bool Equals(object obj) {
|
public override readonly bool Equals(object obj) {
|
||||||
if (obj == null || !(obj is Identifier)) return false;
|
if (obj == null || obj is not Identifier other) return false;
|
||||||
return Equals((Identifier)obj);
|
return Equals(other);
|
||||||
}
|
}
|
||||||
public bool Equals(Identifier other) {
|
public readonly bool Equals(Identifier other) {
|
||||||
return Key == other.Key;
|
return Key == other.Key;
|
||||||
}
|
}
|
||||||
public override int GetHashCode() {
|
public override readonly int GetHashCode() {
|
||||||
return Key;
|
return Key;
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override readonly string ToString() {
|
||||||
if (Key == 0) return "";
|
if (Key == 0) return "";
|
||||||
return Name.ToString();
|
return Name.ToString();
|
||||||
}
|
}
|
||||||
public static implicit operator Identifier(string identifier) {
|
public static bool operator ==(Identifier lhs, Identifier rhs) {
|
||||||
return new Identifier(identifier);
|
return lhs.Equals(rhs);
|
||||||
}
|
}
|
||||||
public static implicit operator string(Identifier identifier) {
|
public static bool operator !=(Identifier lhs, Identifier rhs) {
|
||||||
return identifier.ToString();
|
return !lhs.Equals(rhs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,52 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Cryville.Common {
|
|
||||||
/// <summary>
|
|
||||||
/// A manager that assigns each given identifiers a unique integer ID.
|
|
||||||
/// </summary>
|
|
||||||
public class IdentifierManager {
|
|
||||||
/// <summary>
|
|
||||||
/// A shared instance of the <see cref="IdentifierManager" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public static IdentifierManager SharedInstance = new IdentifierManager();
|
|
||||||
|
|
||||||
Dictionary<object, int> _idents = new Dictionary<object, int>();
|
|
||||||
List<object> _ids = new List<object>();
|
|
||||||
|
|
||||||
object _syncRoot = new object();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="IdentifierManager" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public IdentifierManager() {
|
|
||||||
Request(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Requests an integer ID for an identifier.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ident">The identifier.</param>
|
|
||||||
/// <returns>The integer ID.</returns>
|
|
||||||
public int Request(object ident) {
|
|
||||||
lock (_syncRoot) {
|
|
||||||
int id;
|
|
||||||
if (!_idents.TryGetValue(ident, out id)) {
|
|
||||||
_idents.Add(ident, id = _idents.Count);
|
|
||||||
_ids.Add(ident);
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves the identifier assigned with an integer ID.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id">The integer ID.</param>
|
|
||||||
/// <returns>The identifier.</returns>
|
|
||||||
public object Retrieve(int id) {
|
|
||||||
lock (_syncRoot) {
|
|
||||||
return _ids[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 478086496f56eaf46be4df4e2ad37fee
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,127 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Cryville.Common {
|
|
||||||
/// <summary>
|
|
||||||
/// A logger.
|
|
||||||
/// </summary>
|
|
||||||
public abstract class Logger {
|
|
||||||
static readonly Dictionary<string, Logger> Instances = new Dictionary<string, Logger>();
|
|
||||||
static readonly Dictionary<string, StreamWriter> Files = new Dictionary<string, StreamWriter>();
|
|
||||||
static string logPath = null;
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the path where the log files shall be stored.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
public static void SetLogPath(string path) {
|
|
||||||
logPath = path;
|
|
||||||
var dir = new DirectoryInfo(path);
|
|
||||||
if (!dir.Exists) dir.Create();
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Logs to the specified logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The key of the logger.</param>
|
|
||||||
/// <param name="level">The severity level.</param>
|
|
||||||
/// <param name="module">The module that is logging.</param>
|
|
||||||
/// <param name="format">The format string.</param>
|
|
||||||
/// <param name="args">The arguments for formatting.</param>
|
|
||||||
public static void Log(string key, int level, string module, string format, params object[] args) {
|
|
||||||
if (!Instances.ContainsKey(key)) return;
|
|
||||||
Instances[key].Log(level, module, string.Format(format, args));
|
|
||||||
if (Files.ContainsKey(key)) Files[key].WriteLine("[{0:O}] [{1}] <{2}> {3}", DateTime.UtcNow, level, module, string.Format(format, args));
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a created logger to the shared logger manager.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The key of the logger.</param>
|
|
||||||
/// <param name="logger">The logger.</param>
|
|
||||||
public static void Create(string key, Logger logger) {
|
|
||||||
Instances[key] = logger;
|
|
||||||
if (logPath != null) {
|
|
||||||
Files[key] = new StreamWriter(logPath + "/" + ((int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString() + "-" + key + ".log") {
|
|
||||||
AutoFlush = true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Closes all loggers and related file streams.
|
|
||||||
/// </summary>
|
|
||||||
public static void Close() {
|
|
||||||
Instances.Clear();
|
|
||||||
foreach (var f in Files) f.Value.Dispose();
|
|
||||||
Files.Clear();
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Logs to the logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="level">The severity level.</param>
|
|
||||||
/// <param name="module">The module that is logging.</param>
|
|
||||||
/// <param name="msg">The message.</param>
|
|
||||||
public virtual void Log(int level, string module, string msg) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A <see cref="Logger" /> that calls a callback function on log.
|
|
||||||
/// </summary>
|
|
||||||
public class InstantLogger : Logger {
|
|
||||||
readonly Action<int, string, string> callback;
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="InstantLogger" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="callback">The callback function.</param>
|
|
||||||
/// <exception cref="ArgumentNullException"><paramref name="callback" /> is <see langword="null" />.</exception>
|
|
||||||
public InstantLogger(Action<int, string, string> callback) {
|
|
||||||
if (callback == null)
|
|
||||||
throw new ArgumentNullException("callback");
|
|
||||||
this.callback = callback;
|
|
||||||
}
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void Log(int level, string module, string msg) {
|
|
||||||
base.Log(level, module, msg);
|
|
||||||
callback(level, module, msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A <see cref="Logger" /> that buffers the logs for enumeration.
|
|
||||||
/// </summary>
|
|
||||||
public class BufferedLogger : Logger {
|
|
||||||
readonly List<LogEntry> buffer = new List<LogEntry>();
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an instance of the <see cref="BufferedLogger" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public BufferedLogger() { }
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void Log(int level, string module, string msg) {
|
|
||||||
base.Log(level, module, msg);
|
|
||||||
lock (buffer) {
|
|
||||||
buffer.Add(new LogEntry(level, module, msg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Enumerates the buffered logs.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="callback">The callback function to receive the logs.</param>
|
|
||||||
public void Enumerate(Action<int, string, string> callback) {
|
|
||||||
lock (buffer) {
|
|
||||||
foreach (var i in buffer) {
|
|
||||||
callback(i.level, i.module, i.msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buffer.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct LogEntry {
|
|
||||||
public int level;
|
|
||||||
public string module;
|
|
||||||
public string msg;
|
|
||||||
public LogEntry(int level, string module, string msg) {
|
|
||||||
this.level = level;
|
|
||||||
this.module = module;
|
|
||||||
this.msg = msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 1c1729cfde78f1c479c9f7eb166e0107
|
|
||||||
timeCreated: 1611126212
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -42,28 +42,25 @@ namespace Cryville.Common.Math {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs dot operation with a <see cref="System.Single" /> column vector.
|
/// Performs dot operation with a <see cref="float" /> column vector.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="lhs">The lefthand column vector.</param>
|
/// <param name="lhs">The lefthand column vector.</param>
|
||||||
/// <param name="o">The vector operator.</param>
|
/// <param name="o">The vector operator.</param>
|
||||||
/// <returns>The result of the dot operation.</returns>
|
/// <returns>The result of the dot operation.</returns>
|
||||||
public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) {
|
public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) {
|
||||||
T res = default(T);
|
T res = default;
|
||||||
for (var i = 0; i < Size; i++)
|
for (var i = 0; i < Size; i++)
|
||||||
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
|
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="System.Single" /> column vector and fills it with polynomial coefficients.
|
/// Fills a <see cref="System.Single" /> column vector with polynomial coefficients.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="size">The size of the column vector.</param>
|
/// <param name="vec">The column vector.</param>
|
||||||
/// <param name="num">The base number.</param>
|
/// <param name="num">The base number.</param>
|
||||||
/// <returns>A <see cref="System.Single" /> column vector filled with polynomial coefficients.</returns>
|
public static void FillWithPolynomialCoefficients(ColumnVector<float> vec, float num) {
|
||||||
public static ColumnVector<float> WithPolynomialCoefficients(int size, float num) {
|
for (var i = 0; i < vec.Size; i++)
|
||||||
var m = new ColumnVector<float>(size);
|
vec[i] = (float)System.Math.Pow(num, i);
|
||||||
for (var i = 0; i < size; i++)
|
|
||||||
m[i] = (float)System.Math.Pow(num, i);
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
Assets/Cryville/Common/Math/CubicBezier.cs
Normal file
27
Assets/Cryville/Common/Math/CubicBezier.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using SMath = System.Math;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Math {
|
||||||
|
// Ported from https://github.com/arian/cubic-bezier/blob/master/index.js
|
||||||
|
public static class CubicBezier {
|
||||||
|
public static float Evaluate(float t, float x1, float y1, float x2, float y2, float epsilon) {
|
||||||
|
float x = t, t0 = 0, t1 = 1, t2 = x;
|
||||||
|
|
||||||
|
if (t2 < t0) return Curve(t0, y1, y2);
|
||||||
|
if (t2 > t1) return Curve(t1, y1, y2);
|
||||||
|
|
||||||
|
while (t0 < t1) {
|
||||||
|
float tx = Curve(t2, x1, x2);
|
||||||
|
if (SMath.Abs(tx - x) < epsilon) return Curve(t2, y1, y2);
|
||||||
|
if (x > tx) t0 = t2;
|
||||||
|
else t1 = t2;
|
||||||
|
t2 = (t1 - t0) * .5f + t0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Curve(t2, y1, y2);
|
||||||
|
}
|
||||||
|
static float Curve(float t, float p1, float p2) {
|
||||||
|
float v = 1 - t;
|
||||||
|
return 3 * v * v * t * p1 + 3 * v * t * t * p2 + t * t * t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Math/CubicBezier.cs.meta
Normal file
11
Assets/Cryville/Common/Math/CubicBezier.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 17dd6f775fc965f43960da7166b55b87
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.Math {
|
namespace Cryville.Common.Math {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -12,12 +12,12 @@ namespace Cryville.Common.Math {
|
|||||||
/// <param name="error">The error.</param>
|
/// <param name="error">The error.</param>
|
||||||
/// <param name="n">The numerator.</param>
|
/// <param name="n">The numerator.</param>
|
||||||
/// <param name="d">The denominator.</param>
|
/// <param name="d">The denominator.</param>
|
||||||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is less than 0 or <paramref name="error" /> is not greater than 0 or not less than 1.</exception>
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is less than 0 or <paramref name="error" /> is not greater than 0 or greater than 1.</exception>
|
||||||
public static void ToFraction(double value, double error, out int n, out int d) {
|
public static void ToFraction(double value, double error, out int n, out int d) {
|
||||||
if (value < 0.0)
|
if (value < 0.0)
|
||||||
throw new ArgumentOutOfRangeException("value", "Must be >= 0.");
|
throw new ArgumentOutOfRangeException("value", "Must be >= 0.");
|
||||||
if (error <= 0.0 || error >= 1.0)
|
if (error <= 0.0 || error > 1.0)
|
||||||
throw new ArgumentOutOfRangeException("accuracy", "Must be > 0 and < 1.");
|
throw new ArgumentOutOfRangeException("error", "Must be > 0 and <= 1.");
|
||||||
|
|
||||||
int num = (int)System.Math.Floor(value);
|
int num = (int)System.Math.Floor(value);
|
||||||
value -= num;
|
value -= num;
|
||||||
@@ -48,5 +48,31 @@ namespace Cryville.Common.Math {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the greatest common divisor (GCD) of two integers.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="n">The first integer.</param>
|
||||||
|
/// <param name="d">The second integer.</param>
|
||||||
|
/// <returns>The greatest common divisor (GCD) of the two integers.</returns>
|
||||||
|
public static int GreatestCommonDivisor(int n, int d) {
|
||||||
|
while (d != 0) {
|
||||||
|
int t = d;
|
||||||
|
d = n % d;
|
||||||
|
n = t;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Simplifies a fraction.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="n">The numerator.</param>
|
||||||
|
/// <param name="d">The denominator.</param>
|
||||||
|
public static void Simplify(ref int n, ref int d) {
|
||||||
|
var gcd = GreatestCommonDivisor(n, d);
|
||||||
|
n /= gcd;
|
||||||
|
d /= gcd;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,13 @@
|
|||||||
namespace Cryville.Common.Math {
|
using UnsafeIL;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Math {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a square matrix.
|
/// Represents a square matrix.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SquareMatrix {
|
public class SquareMatrix {
|
||||||
readonly float[,] content;
|
readonly float[,] content;
|
||||||
|
readonly float[,] buffer;
|
||||||
|
readonly int[] refl;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The size of the matrix.
|
/// The size of the matrix.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -17,6 +21,8 @@
|
|||||||
/// <param name="size">The size of the matrix.</param>
|
/// <param name="size">The size of the matrix.</param>
|
||||||
public SquareMatrix(int size) {
|
public SquareMatrix(int size) {
|
||||||
content = new float[size, size];
|
content = new float[size, size];
|
||||||
|
buffer = new float[size, size];
|
||||||
|
refl = new int[size];
|
||||||
Size = size;
|
Size = size;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -34,42 +40,43 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The vector type.</typeparam>
|
/// <typeparam name="T">The vector type.</typeparam>
|
||||||
/// <param name="v">The column vector.</param>
|
/// <param name="v">The column vector.</param>
|
||||||
|
/// <param name="result">The result column vector.</param>
|
||||||
/// <param name="o">The column operator.</param>
|
/// <param name="o">The column operator.</param>
|
||||||
/// <returns>The column vector eliminated.</returns>
|
public void Eliminate<T>(ColumnVector<T> v, ColumnVector<T> result, IVectorOperator<T> o) {
|
||||||
public ColumnVector<T> Eliminate<T>(ColumnVector<T> v, IVectorOperator<T> o) {
|
|
||||||
int s = Size;
|
int s = Size;
|
||||||
float[,] d = (float[,])content.Clone();
|
FillBuffer();
|
||||||
int[] refl = new int[s];
|
for (int i = 0; i < s; i++) refl[i] = i;
|
||||||
for (int i = 0; i < s; i++)
|
|
||||||
refl[i] = i;
|
|
||||||
for (int r = 0; r < s; r++) {
|
for (int r = 0; r < s; r++) {
|
||||||
for (int r0 = r; r0 < s; r0++)
|
for (int r0 = r; r0 < s; r0++)
|
||||||
if (d[refl[r0], r] != 0) {
|
if (buffer[refl[r0], r] != 0) {
|
||||||
refl[r] = r0;
|
refl[r] = r0;
|
||||||
refl[r0] = r;
|
refl[r0] = r;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int or = refl[r];
|
int or = refl[r];
|
||||||
float sf0 = d[or, r];
|
float sf0 = buffer[or, r];
|
||||||
for (int c0 = r; c0 < s; c0++)
|
for (int c0 = r; c0 < s; c0++)
|
||||||
d[or, c0] /= sf0;
|
buffer[or, c0] /= sf0;
|
||||||
v[or] = o.ScalarMultiply(1 / sf0, v[or]);
|
v[or] = o.ScalarMultiply(1 / sf0, v[or]);
|
||||||
for (int r1 = r + 1; r1 < s; r1++) {
|
for (int r1 = r + 1; r1 < s; r1++) {
|
||||||
int or1 = refl[r1];
|
int or1 = refl[r1];
|
||||||
float sf1 = d[or1, r];
|
float sf1 = buffer[or1, r];
|
||||||
for (int c1 = r; c1 < s; c1++)
|
for (int c1 = r; c1 < s; c1++)
|
||||||
d[or1, c1] -= d[or, c1] * sf1;
|
buffer[or1, c1] -= buffer[or, c1] * sf1;
|
||||||
v[or1] = o.Add(v[or1], o.ScalarMultiply(-sf1, v[or]));
|
v[or1] = o.Add(v[or1], o.ScalarMultiply(-sf1, v[or]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
T[] res = new T[s];
|
|
||||||
for (int r2 = s - 1; r2 >= 0; r2--) {
|
for (int r2 = s - 1; r2 >= 0; r2--) {
|
||||||
var v2 = v[refl[r2]];
|
var v2 = v[refl[r2]];
|
||||||
for (int c2 = r2 + 1; c2 < s; c2++)
|
for (int c2 = r2 + 1; c2 < s; c2++)
|
||||||
v2 = o.Add(v2, o.ScalarMultiply(-d[refl[r2], c2], res[refl[c2]]));
|
v2 = o.Add(v2, o.ScalarMultiply(-buffer[refl[r2], c2], result[refl[c2]]));
|
||||||
res[refl[r2]] = v2;
|
result[refl[r2]] = v2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsafe void FillBuffer() {
|
||||||
|
fixed (void* ptrc = content, ptrb = buffer) {
|
||||||
|
Unsafe.CopyBlock(ptrb, ptrc, (uint)(Size * Size * sizeof(float)));
|
||||||
}
|
}
|
||||||
return new ColumnVector<T>(res);
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a square matrix and fills it with polynomial coefficients.
|
/// Creates a square matrix and fills it with polynomial coefficients.
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: f8303a3eeefeacf4ca0c02b5d32e0cff
|
|
||||||
folderAsset: yes
|
|
||||||
timeCreated: 1621071543
|
|
||||||
licenseType: Free
|
|
||||||
DefaultImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,156 +0,0 @@
|
|||||||
using Microsoft.Win32;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
|
||||||
public class HttpClient {
|
|
||||||
private readonly string _directHost;
|
|
||||||
protected string DirectHost { get { return _directHost; } }
|
|
||||||
|
|
||||||
private readonly int _directPort;
|
|
||||||
protected int DirectPort { get { return _directPort; } }
|
|
||||||
|
|
||||||
readonly Uri _baseUri;
|
|
||||||
readonly int origPort;
|
|
||||||
|
|
||||||
protected string Version = "HTTP/1.1";
|
|
||||||
protected TcpClient TcpClient { get; private set; }
|
|
||||||
protected Stream RawTcpStream {
|
|
||||||
get {
|
|
||||||
return TcpClient.GetStream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected virtual Stream Stream {
|
|
||||||
get {
|
|
||||||
return TcpClient.GetStream();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected virtual string WindowsProxyProtocolName {
|
|
||||||
get {
|
|
||||||
return "http";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly bool _proxied = false;
|
|
||||||
|
|
||||||
public Dictionary<string, string> Headers { get; set; }
|
|
||||||
|
|
||||||
public HttpClient(Uri baseUri, int port = 80) {
|
|
||||||
_directHost = baseUri.Host;
|
|
||||||
_directPort = port;
|
|
||||||
_baseUri = baseUri;
|
|
||||||
origPort = _baseUri.Port;
|
|
||||||
Headers = new Dictionary<string, string>();
|
|
||||||
_proxied = GetProxy(ref _directHost, ref _directPort);
|
|
||||||
Logger.Log("main", 0, "Network", "Connecting to {0}:{1}", DirectHost, DirectPort);
|
|
||||||
TcpClient = new TcpClient(DirectHost, DirectPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void Connect() {
|
|
||||||
if (_proxied) {
|
|
||||||
Request(RawTcpStream, "CONNECT", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _baseUri.Host, origPort));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void Close() {
|
|
||||||
TcpClient.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse Request(string method, Uri uri, string body = null, Encoding encoding = null) {
|
|
||||||
string struri = GetUri(uri).PathAndQuery;
|
|
||||||
return Request(Stream, method, struri, body, encoding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) {
|
|
||||||
var headers = new Dictionary<string, string>();
|
|
||||||
foreach (var h in Headers)
|
|
||||||
headers.Add(h.Key, h.Value);
|
|
||||||
headers["Host"] = _baseUri.Host;
|
|
||||||
byte[] payload = new byte[0];
|
|
||||||
if (body != null) {
|
|
||||||
if (encoding == null)
|
|
||||||
encoding = Encoding.UTF8;
|
|
||||||
payload = encoding.GetBytes(body);
|
|
||||||
headers.Add("Content-Encoding", encoding.EncodingName);
|
|
||||||
headers.Add("Content-Length", payload.Length.ToString());
|
|
||||||
}
|
|
||||||
string request_line = string.Format(
|
|
||||||
"{0} {1} {2}\r\n", method, uri, Version
|
|
||||||
);
|
|
||||||
string header_fields = string.Concat((
|
|
||||||
from h in headers select h.Key + ":" + h.Value + "\r\n"
|
|
||||||
).ToArray());
|
|
||||||
byte[] buffer0 = Encoding.ASCII.GetBytes(string.Format(
|
|
||||||
"{0}{1}\r\n", request_line, header_fields
|
|
||||||
));
|
|
||||||
byte[] buffer1 = new byte[buffer0.Length + payload.Length];
|
|
||||||
Array.Copy(buffer0, buffer1, buffer0.Length);
|
|
||||||
Array.Copy(payload, 0, buffer1, buffer0.Length, payload.Length);
|
|
||||||
Logger.Log("main", 0, "Network", Encoding.UTF8.GetString(buffer1));
|
|
||||||
stream.Write(buffer1, 0, buffer1.Length);
|
|
||||||
stream.Flush();
|
|
||||||
var response = new HttpResponse(stream);
|
|
||||||
Logger.Log("main", 0, "Network", "{0}", response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected bool GetProxy(ref string host, ref int port) {
|
|
||||||
// TODO use winhttp.dll
|
|
||||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
|
|
||||||
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
|
|
||||||
var proxyEnable = (int)reg.GetValue("ProxyEnable");
|
|
||||||
if (proxyEnable == 0) return false;
|
|
||||||
var proxyStr = (string)reg.GetValue("ProxyServer");
|
|
||||||
if (!string.IsNullOrEmpty(proxyStr)) {
|
|
||||||
string[] proxies = proxyStr.Split(';');
|
|
||||||
foreach (var p in proxies) {
|
|
||||||
if (!p.Contains('=')) {
|
|
||||||
string[] s = p.Split(':');
|
|
||||||
host = s[0];
|
|
||||||
port = int.Parse(s[1]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (p.StartsWith(WindowsProxyProtocolName + "=")) {
|
|
||||||
string[] s = p.Split('=', ':');
|
|
||||||
host = s[1];
|
|
||||||
port = int.Parse(s[2]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Uri GetUri(string path) {
|
|
||||||
Uri address;
|
|
||||||
if (_baseUri != null) {
|
|
||||||
if (!Uri.TryCreate(_baseUri, path, out address)) {
|
|
||||||
return new Uri(Path.GetFullPath(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!Uri.TryCreate(path, UriKind.Absolute, out address)) {
|
|
||||||
return new Uri(Path.GetFullPath(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return GetUri(address);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Uri GetUri(Uri address) {
|
|
||||||
if (address == null) {
|
|
||||||
throw new ArgumentNullException("address");
|
|
||||||
}
|
|
||||||
Uri uri = address;
|
|
||||||
if (!address.IsAbsoluteUri && _baseUri != null && !Uri.TryCreate(_baseUri, address, out uri)) {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 5ea931bf5488011468f3d1243a038874
|
|
||||||
timeCreated: 1622589817
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,61 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
|
||||||
public class HttpResponse {
|
|
||||||
static readonly char[] spchar = new char[]{ ' ' };
|
|
||||||
public string HttpVersion { get; private set; }
|
|
||||||
public string StatusCode { get; private set; }
|
|
||||||
public string ReasonPhase { get; private set; }
|
|
||||||
public Dictionary<string, string> Headers { get; private set; }
|
|
||||||
public HttpResponseStream MessageBody { get; private set; }
|
|
||||||
internal HttpResponse(Stream stream) {
|
|
||||||
var reader = new BinaryReader(stream, Encoding.ASCII);
|
|
||||||
var statu_line = ReadLine(reader).Split(spchar, 3);
|
|
||||||
HttpVersion = statu_line[0];
|
|
||||||
StatusCode = statu_line[1];
|
|
||||||
ReasonPhase = statu_line[2];
|
|
||||||
Logger.Log("main", 0, "Network", "Receive Response: {0} {1} {2}", HttpVersion, StatusCode, ReasonPhase);
|
|
||||||
Headers = new Dictionary<string, string>();
|
|
||||||
while (ParseHeader(reader, Headers)) ;
|
|
||||||
if (Headers.ContainsKey("content-length")) {
|
|
||||||
int length = int.Parse(Headers["content-length"]);
|
|
||||||
MessageBody = new HttpResponseBlockStream(reader, length);
|
|
||||||
}
|
|
||||||
else if (Headers.ContainsKey("transfer-encoding") && Headers["transfer-encoding"] == "chunked") {
|
|
||||||
MessageBody = new HttpResponseChunkedStream(reader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() {
|
|
||||||
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static bool ParseHeader(BinaryReader reader, Dictionary<string, string> headers) {
|
|
||||||
// TODO Multiline header
|
|
||||||
var header = ReadLine(reader);
|
|
||||||
if (header == "") return false;
|
|
||||||
var s = header.Split(':');
|
|
||||||
string field_name = s[0].Trim().ToLower();
|
|
||||||
string field_value = s[1].Trim();
|
|
||||||
if (headers.ContainsKey(field_name)) headers[field_name] += "," + field_value;
|
|
||||||
else headers.Add(field_name, field_value);
|
|
||||||
Logger.Log("main", 0, "Network", "Receive Header {0}: {1}", field_name, field_value);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string ReadLine(BinaryReader reader) {
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
char c;
|
|
||||||
while (true) {
|
|
||||||
c = reader.ReadChar();
|
|
||||||
if (c == '\r') break;
|
|
||||||
result.Append(c);
|
|
||||||
}
|
|
||||||
// TODO Unseekable
|
|
||||||
reader.ReadByte();
|
|
||||||
return result.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 07e8215a93e3eb1418685009f0c58dcd
|
|
||||||
timeCreated: 1622596274
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,135 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
|
||||||
public abstract class HttpResponseStream : Stream {
|
|
||||||
public override bool CanRead { get { return true; } }
|
|
||||||
|
|
||||||
public override bool CanSeek { get { return false; } }
|
|
||||||
|
|
||||||
public override bool CanWrite { get { return false; } }
|
|
||||||
|
|
||||||
public override long Length { get { throw new NotSupportedException(); } }
|
|
||||||
|
|
||||||
public override long Position {
|
|
||||||
get { throw new NotSupportedException(); }
|
|
||||||
set { throw new NotSupportedException(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Flush() {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract byte[] ReadToEnd();
|
|
||||||
|
|
||||||
public override long Seek(long offset, SeekOrigin origin) {
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetLength(long value) {
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Write(byte[] buffer, int offset, int count) {
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed class HttpResponseBlockStream : HttpResponseStream {
|
|
||||||
readonly BinaryReader _reader;
|
|
||||||
readonly int _length;
|
|
||||||
int _pos = 0;
|
|
||||||
internal HttpResponseBlockStream(BinaryReader reader, int length) {
|
|
||||||
_reader = reader;
|
|
||||||
_length = length;
|
|
||||||
}
|
|
||||||
public override int Read(byte[] buffer, int offset, int count) {
|
|
||||||
int recv = 0;
|
|
||||||
int recv_len = System.Math.Min(count, _length - _pos);
|
|
||||||
if (recv_len == 0) return 0;
|
|
||||||
while (recv < recv_len) {
|
|
||||||
recv += _reader.Read(buffer, offset + recv, count - recv);
|
|
||||||
Logger.Log("main", 0, "Network", "Message body received: {0}/{1}/{2}", recv, recv_len, _length);
|
|
||||||
}
|
|
||||||
_pos += recv_len;
|
|
||||||
return recv_len;
|
|
||||||
}
|
|
||||||
public override byte[] ReadToEnd() {
|
|
||||||
byte[] buffer = new byte[_length - _pos];
|
|
||||||
Read(buffer, 0, buffer.Length);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed class HttpResponseChunkedStream : HttpResponseStream {
|
|
||||||
readonly BinaryReader _reader;
|
|
||||||
byte[] _chunk = null;
|
|
||||||
int _pos = 0;
|
|
||||||
internal HttpResponseChunkedStream(BinaryReader reader) {
|
|
||||||
_reader = reader;
|
|
||||||
ReadChunk();
|
|
||||||
}
|
|
||||||
public void ReadChunk() {
|
|
||||||
if (_chunk != null && _chunk.Length == 0) return;
|
|
||||||
string[] chunkHeader = HttpResponse.ReadLine(_reader).Split(';');
|
|
||||||
// int chunkSize = Array.IndexOf(LEN, chunkHeader[0].ToLower()[0]);
|
|
||||||
int chunkSize = int.Parse(chunkHeader[0], NumberStyles.HexNumber);
|
|
||||||
if (chunkSize == -1)
|
|
||||||
throw new IOException("Corrupted chunk received");
|
|
||||||
if (chunkSize == 0) {
|
|
||||||
_chunk = new byte[0];
|
|
||||||
// TODO TE Header, now just discard
|
|
||||||
var headers = new Dictionary<string, string>();
|
|
||||||
while (HttpResponse.ParseHeader(_reader, headers)) ;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_chunk = new byte[chunkSize];
|
|
||||||
int recv = 0;
|
|
||||||
while (recv < chunkSize) {
|
|
||||||
recv += _reader.Read(_chunk, recv, chunkSize - recv);
|
|
||||||
Logger.Log("main", 0, "Network", "Message chunk received: {0}/{1}", recv, chunkSize);
|
|
||||||
}
|
|
||||||
_pos = 0;
|
|
||||||
if (HttpResponse.ReadLine(_reader) != "")
|
|
||||||
throw new IOException("Corrupted chunk received");
|
|
||||||
}
|
|
||||||
public override int Read(byte[] buffer, int offset, int count) {
|
|
||||||
if (_chunk.Length == 0) return 0;
|
|
||||||
int recv = 0;
|
|
||||||
while (true) {
|
|
||||||
if (count - recv <= _chunk.Length - _pos) break;
|
|
||||||
Array.Copy(_chunk, _pos, buffer, recv, _chunk.Length - _pos);
|
|
||||||
recv += _chunk.Length - _pos;
|
|
||||||
ReadChunk();
|
|
||||||
if (_chunk.Length == 0) return recv;
|
|
||||||
}
|
|
||||||
Array.Copy(_chunk, _pos, buffer, recv, count - recv);
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
public override byte[] ReadToEnd() {
|
|
||||||
if (_chunk.Length == 0) return new byte[0];
|
|
||||||
List<byte[]> segs = new List<byte[]>();
|
|
||||||
while (true) {
|
|
||||||
if (_pos != 0) {
|
|
||||||
var buffer = new byte[_chunk.Length - _pos];
|
|
||||||
Array.Copy(_chunk, _pos, buffer, 0, buffer.Length);
|
|
||||||
segs.Add(buffer);
|
|
||||||
}
|
|
||||||
else segs.Add(_chunk);
|
|
||||||
ReadChunk();
|
|
||||||
if (_chunk.Length == 0) {
|
|
||||||
var result = new byte[segs.Sum(i => i.Length)];
|
|
||||||
int p = 0;
|
|
||||||
foreach (var i in segs) {
|
|
||||||
Array.Copy(i, 0, result, p, i.Length);
|
|
||||||
p += i.Length;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: f191de447a708da4f9d230e6545ce0a6
|
|
||||||
timeCreated: 1635470462
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,33 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
|
||||||
public class HttpsClient : HttpClient {
|
|
||||||
readonly TlsClient _tlsClient;
|
|
||||||
|
|
||||||
protected override Stream Stream {
|
|
||||||
get {
|
|
||||||
return _tlsClient.Stream;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
protected override string WindowsProxyProtocolName {
|
|
||||||
get {
|
|
||||||
return "https";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpsClient(Uri baseUri) : base(baseUri, 443) {
|
|
||||||
_tlsClient = new TlsClient(RawTcpStream, baseUri.Host);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Connect() {
|
|
||||||
base.Connect();
|
|
||||||
_tlsClient.Connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Close() {
|
|
||||||
_tlsClient.Close();
|
|
||||||
base.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 9b35290e0e147a342acc29a20c8fceaf
|
|
||||||
timeCreated: 1622503538
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,100 +0,0 @@
|
|||||||
using Org.BouncyCastle.Security;
|
|
||||||
using Org.BouncyCastle.Tls;
|
|
||||||
using Org.BouncyCastle.Tls.Crypto;
|
|
||||||
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
|
|
||||||
using System.Collections;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using BcTlsClient = Org.BouncyCastle.Tls.TlsClient;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
|
||||||
public class TlsClient {
|
|
||||||
readonly TlsClientProtocol _protocol;
|
|
||||||
readonly BcTlsClient _tlsClient;
|
|
||||||
public Stream Stream { get; private set; }
|
|
||||||
public TlsClient(Stream baseStream, string hostname) {
|
|
||||||
_protocol = new TlsClientProtocol(baseStream);
|
|
||||||
_tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Connect() {
|
|
||||||
_protocol.Connect(_tlsClient);
|
|
||||||
Stream = _protocol.Stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Close() {
|
|
||||||
_protocol.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private class InternalTlsClient : DefaultTlsClient {
|
|
||||||
string _host;
|
|
||||||
|
|
||||||
public InternalTlsClient(string host, TlsCrypto crypto) : base(crypto) {
|
|
||||||
_host = host;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override ProtocolVersion[] GetSupportedVersions() {
|
|
||||||
return ProtocolVersion.TLSv13.DownTo(ProtocolVersion.TLSv12);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override IList GetProtocolNames() {
|
|
||||||
IList list = new ArrayList {
|
|
||||||
ProtocolName.Http_1_1
|
|
||||||
};
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static readonly int[] supportedCipherSuites = {
|
|
||||||
CipherSuite.TLS_AES_128_GCM_SHA256,
|
|
||||||
CipherSuite.TLS_AES_256_GCM_SHA384,
|
|
||||||
CipherSuite.TLS_CHACHA20_POLY1305_SHA256,
|
|
||||||
CipherSuite.TLS_AES_128_CCM_SHA256,
|
|
||||||
CipherSuite.TLS_AES_128_CCM_8_SHA256,
|
|
||||||
};
|
|
||||||
protected override int[] GetSupportedCipherSuites() {
|
|
||||||
return base.GetSupportedCipherSuites().Union(supportedCipherSuites).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override IList GetSupportedSignatureAlgorithms() {
|
|
||||||
var result = base.GetSupportedSignatureAlgorithms();
|
|
||||||
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP256r1tls13_sha256);
|
|
||||||
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP384r1tls13_sha384);
|
|
||||||
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP512r1tls13_sha512);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override IList GetSniServerNames() {
|
|
||||||
return new ArrayList { new ServerName(0, Encoding.ASCII.GetBytes(_host)) };
|
|
||||||
}
|
|
||||||
|
|
||||||
public override TlsAuthentication GetAuthentication() {
|
|
||||||
return new NullTlsAuthentication();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void NotifyAlertReceived(short alertLevel, short alertDescription) {
|
|
||||||
Logger.Log("main", 0, "Network/TLS", "TLS Alert {0} {1}", alertLevel, alertDescription);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void NotifyServerVersion(ProtocolVersion serverVersion) {
|
|
||||||
base.NotifyServerVersion(serverVersion);
|
|
||||||
Logger.Log("main", 0, "Network/TLS", "NotifyServerVersion {0}", serverVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void NotifySelectedCipherSuite(int selectedCipherSuite) {
|
|
||||||
base.NotifySelectedCipherSuite(selectedCipherSuite);
|
|
||||||
Logger.Log("main", 0, "Network/TLS", "NotifySelectedCipherSuite {0}", selectedCipherSuite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class NullTlsAuthentication : TlsAuthentication {
|
|
||||||
public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void NotifyServerCertificate(TlsServerCertificate serverCertificate) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: c9c242bb90fc1cc479a8df1407f21940
|
|
||||||
timeCreated: 1622021660
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,7 +1,21 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates that the attributed member is an element list.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>An element list is a <see cref="System.Collections.IDictionary" /> or <see cref="Cryville.Common.Collections.IPairList" /> that represents a collection of PDT elements. There must be at most one element list in a class.</para>
|
||||||
|
/// </remarks>
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
public class ElementListAttribute : Attribute { }
|
public class ElementListAttribute : Attribute { }
|
||||||
public class ComponentListAttribute : Attribute { }
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates that the attributed member is a property list.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>A property list is a <see cref="System.Collections.IDictionary" /> or <see cref="Cryville.Common.Collections.IPairList" /> that represents a collection of PDT properties. There must be at most one property list in a class.</para>
|
||||||
|
/// </remarks>
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
public class PropertyListAttribute : Attribute { }
|
public class PropertyListAttribute : Attribute { }
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -16,32 +17,30 @@ namespace Cryville.Common.Pdt {
|
|||||||
readonly StackFrame[] _stack = new StackFrame[256];
|
readonly StackFrame[] _stack = new StackFrame[256];
|
||||||
readonly byte[] _mem = new byte[0x100000];
|
readonly byte[] _mem = new byte[0x100000];
|
||||||
bool _revokepttconst;
|
bool _revokepttconst;
|
||||||
LinkedListNode<PdtInstruction> _rip;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Evaluates an expression and passes the result to a target operator.
|
/// Evaluates an expression and passes the result to a target operator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="target">The target operator.</param>
|
/// <param name="target">The target operator.</param>
|
||||||
/// <param name="exp">The expression to evaluate.</param>
|
/// <param name="exp">The expression to evaluate.</param>
|
||||||
public void Evaluate(PdtOperator target, PdtExpression exp) {
|
/// <returns>Whether the evaluaton succeeded.</returns>
|
||||||
_framecount = 0;
|
public bool Evaluate(PdtOperator target, PdtExpression exp) {
|
||||||
_goffset = 0;
|
var prevFrameCount = _framecount;
|
||||||
_revokepttconst = false;
|
try {
|
||||||
for (_rip = exp.Instructions.First; _rip != null; _rip = _rip.Next)
|
_revokepttconst = false;
|
||||||
_rip.Value.Execute(this);
|
for (var ip = exp.Instructions.First; ip != null; ip = ip.Next)
|
||||||
Operate(target, _framecount, true);
|
ip.Value.Execute(this, ref ip);
|
||||||
if (exp.IsPotentialConstant) {
|
if (exp.IsPotentialConstant) {
|
||||||
exp.IsConstant = exp.IsPotentialConstant = !_revokepttconst;
|
exp.IsConstant = exp.IsPotentialConstant = !_revokepttconst;
|
||||||
|
}
|
||||||
|
var ret = Operate(target, _framecount - prevFrameCount, true);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
throw new EvaluationFailureException(exp, ex);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
for (var i = prevFrameCount; i < _framecount; i++) DiscardStack();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Patches an expression with a lefthand variable and a compound operator.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">The name of the lefthand variable.</param>
|
|
||||||
/// <param name="op">The name of the compound operator.</param>
|
|
||||||
/// <param name="exp">The expression.</param>
|
|
||||||
public void PatchCompound(int target, int op, PdtExpression exp) {
|
|
||||||
exp.Instructions.AddFirst(new PdtInstruction.PushVariable(target));
|
|
||||||
exp.Instructions.AddLast(new PdtInstruction.Operate(op, 2));
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Optimizes an expression by merging its instructions.
|
/// Optimizes an expression by merging its instructions.
|
||||||
@@ -53,12 +52,14 @@ namespace Cryville.Common.Pdt {
|
|||||||
List<PdtInstruction.Collapse> ct;
|
List<PdtInstruction.Collapse> ct;
|
||||||
var cols = new Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>>();
|
var cols = new Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>>();
|
||||||
var il = exp.Instructions;
|
var il = exp.Instructions;
|
||||||
for (_rip = il.First; _rip != null; _rip = _rip == null ? il.First : _rip.Next) {
|
var ip = il.First;
|
||||||
var i = _rip.Value;
|
while (ip != null) {
|
||||||
if (i is PdtInstruction.Operate) {
|
bool nextFlag = false;
|
||||||
|
var i = ip.Value;
|
||||||
|
if (i is PdtInstruction.Operate iop) {
|
||||||
int fc0 = _framecount;
|
int fc0 = _framecount;
|
||||||
int fc1 = ((PdtInstruction.Operate)i).Signature.ParamCount;
|
int fc1 = iop.Signature.ParamCount;
|
||||||
try { i.Execute(this); } catch (Exception) { }
|
try { i.Execute(this, ref ip); } catch (Exception) { }
|
||||||
if (fc0 - _framecount == fc1) {
|
if (fc0 - _framecount == fc1) {
|
||||||
unsafe {
|
unsafe {
|
||||||
fixed (StackFrame* frame = &_stack[_framecount++]) {
|
fixed (StackFrame* frame = &_stack[_framecount++]) {
|
||||||
@@ -70,29 +71,37 @@ namespace Cryville.Common.Pdt {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var frame = _stack[_framecount - 1];
|
var frame = _stack[_framecount - 1];
|
||||||
_rip = il.AddAfter(_rip, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length));
|
if (frame.Type != PdtInternalType.Error) {
|
||||||
for (var j = 0; j <= fc1; j++) il.Remove(_rip.Previous);
|
ReplaceIP(il, ref ip, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length), cols);
|
||||||
|
for (var j = 0; j < fc1; j++) il.Remove(ip.Previous);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (i is PdtInstruction.Collapse) {
|
else if (i is PdtInstruction.Collapse t) {
|
||||||
var t = (PdtInstruction.Collapse)i;
|
|
||||||
try {
|
try {
|
||||||
var pins = _rip;
|
var pins = ip;
|
||||||
i.Execute(this);
|
i.Execute(this, ref ip);
|
||||||
if (_rip == pins) {
|
if (_stack[_framecount - 1].Type == PdtInternalType.Error) {
|
||||||
_rip = _rip.Next;
|
throw new EvaluationFailureException();
|
||||||
il.Remove(_rip.Previous);
|
}
|
||||||
il.Remove(_rip.Previous);
|
if (ip == pins) {
|
||||||
_rip = _rip.Previous;
|
ip = ip.Next;
|
||||||
|
il.Remove(ip.Previous);
|
||||||
|
il.Remove(ip.Previous);
|
||||||
|
ip = ip.Previous;
|
||||||
|
if (ip == null) {
|
||||||
|
ip = il.First;
|
||||||
|
nextFlag = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_rip = pins.Previous;
|
ip = pins.Previous;
|
||||||
while (_rip.Next != t.Target) il.Remove(_rip.Next);
|
while (ip.Next != t.Target) il.Remove(ip.Next);
|
||||||
il.Remove(_rip.Next);
|
il.Remove(ip.Next);
|
||||||
if (cols.TryGetValue(t.Target, out ct)) {
|
if (cols.TryGetValue(t.Target, out ct)) {
|
||||||
foreach (var u in ct) u.Target = _rip;
|
foreach (var u in ct) u.Target = ip;
|
||||||
cols.Remove(t.Target);
|
cols.Remove(t.Target);
|
||||||
cols.Add(_rip, ct);
|
cols.Add(ip, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,15 +111,14 @@ namespace Cryville.Common.Pdt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (i is PdtInstruction.PushVariable) {
|
else if (i is PdtInstruction.PushVariable) {
|
||||||
i.Execute(this);
|
i.Execute(this, ref ip);
|
||||||
var frame = _stack[_framecount - 1];
|
var frame = _stack[_framecount - 1];
|
||||||
if (frame.Type != PdtInternalType.Undefined) {
|
if (frame.Type != PdtInternalType.Undefined && frame.Type != PdtInternalType.Error) {
|
||||||
_rip = il.AddAfter(_rip, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length));
|
ReplaceIP(il, ref ip, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length), cols);
|
||||||
il.Remove(_rip.Previous);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else i.Execute(this);
|
else i.Execute(this, ref ip);
|
||||||
if (_rip != null && cols.TryGetValue(_rip, out ct)) {
|
if (ip != null && cols.TryGetValue(ip, out ct)) {
|
||||||
unsafe {
|
unsafe {
|
||||||
fixed (StackFrame* frame = &_stack[_framecount - 1]) {
|
fixed (StackFrame* frame = &_stack[_framecount - 1]) {
|
||||||
frame->Type = PdtInternalType.Error;
|
frame->Type = PdtInternalType.Error;
|
||||||
@@ -119,19 +127,23 @@ namespace Cryville.Common.Pdt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!nextFlag) ip = ip.Next;
|
||||||
}
|
}
|
||||||
exp.IsConstant = true;
|
exp.IsConstant = true;
|
||||||
exp.IsPotentialConstant = true;
|
exp.IsPotentialConstant = true;
|
||||||
for (var ins = il.First; ins != null; ins = ins.Next) {
|
for (var ins = il.First; ins != null; ins = ins.Next) {
|
||||||
if (!(ins.Value is PdtInstruction.PushConstant)) {
|
if (ins.Value is not PdtInstruction.PushConstant) {
|
||||||
exp.IsConstant = false;
|
exp.IsConstant = false;
|
||||||
}
|
break;
|
||||||
else if (!(ins.Value is PdtInstruction.PushVariable)) {
|
|
||||||
exp.IsPotentialConstant = false;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void ReplaceIP(LinkedList<PdtInstruction> il, ref LinkedListNode<PdtInstruction> ip, PdtInstruction ins, Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>> cols) {
|
||||||
|
if (cols.TryGetValue(ip, out List<PdtInstruction.Collapse> cins)) cols.Remove(ip);
|
||||||
|
ip = il.AddAfter(ip, ins);
|
||||||
|
il.Remove(ip.Previous);
|
||||||
|
if (cins != null) cols.Add(ip, cins);
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Revokes the potential constant mark of the current expression.
|
/// Revokes the potential constant mark of the current expression.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -147,10 +159,9 @@ namespace Cryville.Common.Pdt {
|
|||||||
_goffset += value.Length;
|
_goffset += value.Length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal unsafe void PushVariable(int name) {
|
internal unsafe void PushVariable(int name, bool forced) {
|
||||||
fixed (StackFrame* frame = &_stack[_framecount++]) {
|
fixed (StackFrame* frame = &_stack[_framecount++]) {
|
||||||
byte[] value;
|
GetVariable(name, forced, out frame->Type, out byte[] value);
|
||||||
GetVariable(name, out frame->Type, out value);
|
|
||||||
frame->Offset = _goffset;
|
frame->Offset = _goffset;
|
||||||
frame->Length = value.Length;
|
frame->Length = value.Length;
|
||||||
Array.Copy(value, 0, _mem, _goffset, value.Length);
|
Array.Copy(value, 0, _mem, _goffset, value.Length);
|
||||||
@@ -161,16 +172,17 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// Gets a variable of the specified name.
|
/// Gets a variable of the specified name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">The name of the variable.</param>
|
/// <param name="name">The name of the variable.</param>
|
||||||
|
/// <param name="forced">Whether to produce an error stack instead of an identifier stack if the variable is not found.</param>
|
||||||
/// <param name="type">The type of the variable.</param>
|
/// <param name="type">The type of the variable.</param>
|
||||||
/// <param name="value">The value of the variable.</param>
|
/// <param name="value">The value of the variable.</param>
|
||||||
protected abstract void GetVariable(int name, out int type, out byte[] value);
|
protected abstract void GetVariable(int name, bool forced, out int type, out byte[] value);
|
||||||
internal void Operate(PdtOperatorSignature sig) {
|
internal void Operate(PdtOperatorSignature sig) {
|
||||||
PdtOperator op;
|
PdtOperator op;
|
||||||
try { op = GetOperator(sig); }
|
try { op = GetOperator(sig); }
|
||||||
catch (Exception) {
|
catch (Exception ex) {
|
||||||
for (int i = 0; i < sig.ParamCount; i++)
|
for (int i = 0; i < sig.ParamCount; i++)
|
||||||
DiscardStack();
|
DiscardStack();
|
||||||
throw;
|
throw new EvaluationFailureException(string.Format("Failed to get operator {0}", sig), ex);
|
||||||
}
|
}
|
||||||
Operate(op, sig.ParamCount);
|
Operate(op, sig.ParamCount);
|
||||||
}
|
}
|
||||||
@@ -182,23 +194,36 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <returns>An operator of the specific name.</returns>
|
/// <returns>An operator of the specific name.</returns>
|
||||||
/// <remarks>The parameter count of the returned operator does not necessarily equal to <paramref name="pc" />.</remarks>
|
/// <remarks>The parameter count of the returned operator does not necessarily equal to <paramref name="pc" />.</remarks>
|
||||||
protected abstract PdtOperator GetOperator(PdtOperatorSignature sig);
|
protected abstract PdtOperator GetOperator(PdtOperatorSignature sig);
|
||||||
unsafe void Operate(PdtOperator op, int pc, bool noset = false) {
|
unsafe bool Operate(PdtOperator op, int pc, bool noset = false) {
|
||||||
fixed (byte* pmem = _mem) {
|
fixed (byte* pmem = _mem) {
|
||||||
op.Begin(this);
|
op.Begin(this, pc);
|
||||||
for (int i = 0; i < pc; i++) {
|
for (int i = 0; i < pc; i++) {
|
||||||
var frame = _stack[--_framecount];
|
var frame = _stack[--_framecount];
|
||||||
|
if (frame.Type == PdtInternalType.Error) {
|
||||||
|
_framecount -= pc - i - 1;
|
||||||
|
_stack[_framecount++] = new StackFrame { Type = PdtInternalType.Error, Offset = -1, Length = 0 };
|
||||||
|
return false;
|
||||||
|
}
|
||||||
op.LoadOperand(new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length));
|
op.LoadOperand(new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length));
|
||||||
_goffset -= frame.Length;
|
_goffset -= frame.Length;
|
||||||
}
|
}
|
||||||
op.Call(pmem + _goffset, noset);
|
op.Call(pmem + _goffset, noset);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal unsafe void Collapse(int name, LinkedListNode<PdtInstruction> target) {
|
internal unsafe void Collapse(int name, ref LinkedListNode<PdtInstruction> self, LinkedListNode<PdtInstruction> target) {
|
||||||
fixed (byte* pmem = _mem) {
|
fixed (byte* pmem = _mem) {
|
||||||
var frame = _stack[--_framecount];
|
var frame = _stack[--_framecount];
|
||||||
|
_goffset -= frame.Length;
|
||||||
|
if (frame.Type == PdtInternalType.Error) {
|
||||||
|
_stack[_framecount++] = new StackFrame { Type = PdtInternalType.Error, Offset = -1, Length = 0 };
|
||||||
|
self = target;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (Collapse(name, new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length))) {
|
if (Collapse(name, new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length))) {
|
||||||
_framecount++;
|
_framecount++;
|
||||||
_rip = target;
|
_goffset += frame.Length;
|
||||||
|
self = target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,4 +247,29 @@ namespace Cryville.Common.Pdt {
|
|||||||
_goffset -= _stack[--_framecount].Length;
|
_goffset -= _stack[--_framecount].Length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The exception that is thrown when the evaluation of a <see cref="PdtExpression" /> fails.
|
||||||
|
/// </summary>
|
||||||
|
public class EvaluationFailureException : Exception {
|
||||||
|
/// <inheritdoc />
|
||||||
|
public EvaluationFailureException() : base("Evaluation failed") { }
|
||||||
|
/// <inheritdoc />
|
||||||
|
public EvaluationFailureException(string message) : base(message) { }
|
||||||
|
/// <inheritdoc />
|
||||||
|
public EvaluationFailureException(string message, Exception innerException) : base(message, innerException) { }
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected EvaluationFailureException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of the <see cref="EvaluationFailureException" /> class with the failing expression.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exp">The failing expression.</param>
|
||||||
|
public EvaluationFailureException(PdtExpression exp) : base("Evaluation failed for the expression: " + exp.ToString()) { }
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of the <see cref="EvaluationFailureException" /> class with the failing expression and the inner exception.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exp">The failing expression.</param>
|
||||||
|
/// <param name="innerException">The inner exception.</param>
|
||||||
|
public EvaluationFailureException(PdtExpression exp, Exception innerException) : base("Evaluation failed for the expression: " + exp.ToString(), innerException) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -7,11 +7,23 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// PDT expression.
|
/// PDT expression.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PdtExpression {
|
public class PdtExpression {
|
||||||
|
/// <summary>
|
||||||
|
/// The empty expression.
|
||||||
|
/// </summary>
|
||||||
|
public static readonly PdtExpression Empty;
|
||||||
|
static PdtExpression() {
|
||||||
|
var ins = new LinkedList<PdtInstruction>();
|
||||||
|
ins.AddLast(new PdtInstruction.PushConstant(
|
||||||
|
PdtInternalType.Number, BitConverter.GetBytes(1f)
|
||||||
|
));
|
||||||
|
Empty = new PdtExpression(ins);
|
||||||
|
}
|
||||||
|
|
||||||
internal LinkedList<PdtInstruction> Instructions;
|
internal LinkedList<PdtInstruction> Instructions;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the value of this expression is constant.
|
/// Whether the value of this expression is constant.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>The value of this property is <c>false</c> until it is optimized.</remarks>
|
/// <remarks>The value of this property is <see langword="false" /> until it is optimized.</remarks>
|
||||||
public bool IsConstant { get; internal set; }
|
public bool IsConstant { get; internal set; }
|
||||||
internal bool IsPotentialConstant;
|
internal bool IsPotentialConstant;
|
||||||
internal PdtExpression(LinkedList<PdtInstruction> ins) {
|
internal PdtExpression(LinkedList<PdtInstruction> ins) {
|
||||||
@@ -27,9 +39,19 @@ namespace Cryville.Common.Pdt {
|
|||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Patches an expression with a lefthand variable and a compound operator.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">The name of the lefthand variable.</param>
|
||||||
|
/// <param name="op">The name of the compound operator.</param>
|
||||||
|
/// <param name="exp">The expression.</param>
|
||||||
|
public static void PatchCompound(int target, int op, PdtExpression exp) {
|
||||||
|
exp.Instructions.AddFirst(new PdtInstruction.PushVariable(target));
|
||||||
|
exp.Instructions.AddLast(new PdtInstruction.Operate(op, 2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
internal abstract class PdtInstruction {
|
internal abstract class PdtInstruction {
|
||||||
internal abstract void Execute(PdtEvaluatorBase etor);
|
internal abstract void Execute(PdtEvaluatorBase etor, ref LinkedListNode<PdtInstruction> self);
|
||||||
public class PushConstant : PdtInstruction {
|
public class PushConstant : PdtInstruction {
|
||||||
public int Type { get; private set; }
|
public int Type { get; private set; }
|
||||||
public byte[] Value { get; private set; }
|
public byte[] Value { get; private set; }
|
||||||
@@ -42,7 +64,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
Value = new byte[len];
|
Value = new byte[len];
|
||||||
Array.Copy(buffer, offset, Value, 0, len);
|
Array.Copy(buffer, offset, Value, 0, len);
|
||||||
}
|
}
|
||||||
internal override void Execute(PdtEvaluatorBase etor) {
|
internal override void Execute(PdtEvaluatorBase etor, ref LinkedListNode<PdtInstruction> self) {
|
||||||
etor.PushConstant(Type, Value);
|
etor.PushConstant(Type, Value);
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
@@ -51,13 +73,14 @@ namespace Cryville.Common.Pdt {
|
|||||||
}
|
}
|
||||||
public class PushVariable : PdtInstruction {
|
public class PushVariable : PdtInstruction {
|
||||||
public int Name { get; private set; }
|
public int Name { get; private set; }
|
||||||
public PushVariable(int name) { Name = name; }
|
public bool Forced { get; private set; }
|
||||||
public PushVariable(string name) : this(IdentifierManager.SharedInstance.Request(name)) { }
|
public PushVariable(int name, bool forced = false) { Name = name; Forced = forced; }
|
||||||
internal override void Execute(PdtEvaluatorBase etor) {
|
public PushVariable(string name, bool forced = false) : this(IdentifierManager.Shared.Request(name)) { Forced = forced; }
|
||||||
etor.PushVariable(Name);
|
internal override void Execute(PdtEvaluatorBase etor, ref LinkedListNode<PdtInstruction> self) {
|
||||||
|
etor.PushVariable(Name, Forced);
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return string.Format("pushv {0}", IdentifierManager.SharedInstance.Retrieve(Name));
|
return string.Format(Forced ? "pushv ?{0}" : "pushv {0}", IdentifierManager.Shared.Retrieve(Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public class Operate : PdtInstruction {
|
public class Operate : PdtInstruction {
|
||||||
@@ -68,7 +91,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
public Operate(string name, int paramCount) {
|
public Operate(string name, int paramCount) {
|
||||||
Signature = new PdtOperatorSignature(name, paramCount);
|
Signature = new PdtOperatorSignature(name, paramCount);
|
||||||
}
|
}
|
||||||
internal override void Execute(PdtEvaluatorBase etor) {
|
internal override void Execute(PdtEvaluatorBase etor, ref LinkedListNode<PdtInstruction> self) {
|
||||||
etor.Operate(Signature);
|
etor.Operate(Signature);
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
@@ -79,19 +102,19 @@ namespace Cryville.Common.Pdt {
|
|||||||
public int Name { get; private set; }
|
public int Name { get; private set; }
|
||||||
public LinkedListNode<PdtInstruction> Target { get; internal set; }
|
public LinkedListNode<PdtInstruction> Target { get; internal set; }
|
||||||
public Collapse(string name, LinkedListNode<PdtInstruction> target) {
|
public Collapse(string name, LinkedListNode<PdtInstruction> target) {
|
||||||
Name = IdentifierManager.SharedInstance.Request(name);
|
Name = IdentifierManager.Shared.Request(name);
|
||||||
Target = target;
|
Target = target;
|
||||||
}
|
}
|
||||||
internal override void Execute(PdtEvaluatorBase etor) {
|
internal override void Execute(PdtEvaluatorBase etor, ref LinkedListNode<PdtInstruction> self) {
|
||||||
etor.Collapse(Name, Target);
|
etor.Collapse(Name, ref self, Target);
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return string.Format("col {0}{{{1}}}", IdentifierManager.SharedInstance.Retrieve(Name), Target.Value);
|
return string.Format("col {0}{{{1}}}", IdentifierManager.Shared.Retrieve(Name), Target.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public partial class PdtInterpreter<T> {
|
public partial class PdtInterpreter {
|
||||||
readonly static Dictionary<char, int> OP_PRIORITY = new Dictionary<char, int> {
|
static readonly Dictionary<char, int> OP_PRIORITY = new() {
|
||||||
{ '@', 7 },
|
{ '@', 7 },
|
||||||
{ '*', 6 }, { '/', 6 }, { '%', 6 },
|
{ '*', 6 }, { '/', 6 }, { '%', 6 },
|
||||||
{ '+', 5 }, { '-', 5 },
|
{ '+', 5 }, { '-', 5 },
|
||||||
@@ -102,7 +125,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
{ ',', 0 },
|
{ ',', 0 },
|
||||||
{ '$', -1 },
|
{ '$', -1 },
|
||||||
};
|
};
|
||||||
readonly static Dictionary<char, int> OP_TYPE = new Dictionary<char, int> {
|
static readonly Dictionary<char, int> OP_TYPE = new() {
|
||||||
{ '@', 0 },
|
{ '@', 0 },
|
||||||
{ '*', 0 }, { '/', 0 }, { '%', 0 },
|
{ '*', 0 }, { '/', 0 }, { '%', 0 },
|
||||||
{ '+', 0 }, { '-', 0 },
|
{ '+', 0 }, { '-', 0 },
|
||||||
@@ -114,36 +137,27 @@ namespace Cryville.Common.Pdt {
|
|||||||
{ '$', -1 },
|
{ '$', -1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly static PdtExpression _emptyexp;
|
|
||||||
static PdtInterpreter() {
|
|
||||||
var ins = new LinkedList<PdtInstruction>();
|
|
||||||
ins.AddLast(new PdtInstruction.PushConstant(
|
|
||||||
PdtInternalType.Number, BitConverter.GetBytes(1f)
|
|
||||||
));
|
|
||||||
_emptyexp = new PdtExpression(ins);
|
|
||||||
}
|
|
||||||
|
|
||||||
PdtExpToken GetToken() {
|
PdtExpToken GetToken() {
|
||||||
ws();
|
ws();
|
||||||
var result = new PdtExpToken {
|
var result = new PdtExpToken {
|
||||||
Type = ct & 0x0fe0
|
Type = ct & (CharCategory)0x0fe0
|
||||||
};
|
};
|
||||||
switch (result.Type) {
|
switch (result.Type) {
|
||||||
case 0x0020: result.Value = GetIdentifier(); break;
|
case CharCategory.IdentifierBegin: result.Value = GetIdentifier(); break;
|
||||||
case 0x0040: result.Value = GetNumber(); break;
|
case CharCategory.Digit : result.Value = GetNumber(); break;
|
||||||
case 0x0100: result.Value = GetString(); break;
|
case CharCategory.StringDelimiter: result.Value = GetString(); break;
|
||||||
default: result.Value = cc.ToString(); Position++; break;
|
default: result.Value = cc.ToString(); Position++; break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
private struct PdtExpToken {
|
private struct PdtExpToken {
|
||||||
public int Type { get; set; }
|
public CharCategory Type { get; set; }
|
||||||
public string Value { get; set; }
|
public string Value { get; set; }
|
||||||
public override string ToString() {
|
public override readonly string ToString() {
|
||||||
return string.Format("0x{0:x4}: {1}", Type, Value);
|
return string.Format("0x{0:x4}: {1}", Type, Value);
|
||||||
}
|
}
|
||||||
public readonly static PdtExpToken EmptyOperator = new PdtExpToken {
|
public static readonly PdtExpToken EmptyOperator = new() {
|
||||||
Type = 0x0080,
|
Type = CharCategory.Operator,
|
||||||
Value = "$",
|
Value = "$",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -178,7 +192,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
t2 = InterpretExpBlock(ins);
|
t2 = InterpretExpBlock(ins);
|
||||||
panic:
|
panic:
|
||||||
switch (t2.Type) {
|
switch (t2.Type) {
|
||||||
case 0x0080:
|
case CharCategory.Operator:
|
||||||
if (OP_TYPE[t1.Value[0]] != -1) {
|
if (OP_TYPE[t1.Value[0]] != -1) {
|
||||||
int p1 = OP_PRIORITY[t1.Value[0]];
|
int p1 = OP_PRIORITY[t1.Value[0]];
|
||||||
int p2 = OP_PRIORITY[t2.Value[0]];
|
int p2 = OP_PRIORITY[t2.Value[0]];
|
||||||
@@ -194,16 +208,16 @@ namespace Cryville.Common.Pdt {
|
|||||||
default: pc++; break;
|
default: pc++; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (OP_TYPE[t2.Value[0]] == 1) {
|
if (OP_TYPE[t2.Value[0]] == 1) {
|
||||||
colp = new Dictionary<LinkedListNode<PdtInstruction>, string> { { ins.Last, t2.Value } };
|
colp = new Dictionary<LinkedListNode<PdtInstruction>, string> { { ins.Last, t2.Value } };
|
||||||
}
|
}
|
||||||
t1 = t2;
|
t1 = t2;
|
||||||
break;
|
break;
|
||||||
case 0x0400:
|
case CharCategory.ClosingBracket:
|
||||||
if (enc == -2) throw new FormatException("Expression not enclosed correctly: Too many closing brackets");
|
if (enc == -2) throw new FormatException("Expression not enclosed correctly: Too many closing brackets");
|
||||||
if (ins.Count == insc0) pc = 0;
|
if (ins.Count == insc0) pc = 0;
|
||||||
goto exit;
|
goto exit;
|
||||||
case 0x0800:
|
case CharCategory.EndOfExpression:
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,7 +240,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <returns>The expression token following this expression block.</returns>
|
/// <returns>The expression token following this expression block.</returns>
|
||||||
PdtExpToken InterpretExpBlock(LinkedList<PdtInstruction> ins) {
|
PdtExpToken InterpretExpBlock(LinkedList<PdtInstruction> ins) {
|
||||||
var t = GetToken();
|
var t = GetToken();
|
||||||
if (t.Type == 0x0080) {
|
if (t.Type == CharCategory.Operator) {
|
||||||
var r = InterpretExpBlock(ins);
|
var r = InterpretExpBlock(ins);
|
||||||
ins.AddLast(new PdtInstruction.Operate(t.Value, 1));
|
ins.AddLast(new PdtInstruction.Operate(t.Value, 1));
|
||||||
return r;
|
return r;
|
||||||
@@ -234,24 +248,27 @@ namespace Cryville.Common.Pdt {
|
|||||||
bool flag = false;
|
bool flag = false;
|
||||||
PdtExpToken? buf = null;
|
PdtExpToken? buf = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (buf != null && t.Type != 0x0200) {
|
if (buf != null && t.Type != CharCategory.OpeningBracket) {
|
||||||
PdtExpression def;
|
if (defs.TryGetValue(buf.Value.Value, out PdtExpression def)) {
|
||||||
if (defs.TryGetValue(buf.Value.Value, out def)) {
|
|
||||||
foreach (var i in def.Instructions) ins.AddLast(i);
|
foreach (var i in def.Instructions) ins.AddLast(i);
|
||||||
}
|
}
|
||||||
else ins.AddLast(new PdtInstruction.PushVariable(buf.Value.Value));
|
else {
|
||||||
|
var name = buf.Value.Value;
|
||||||
|
if (name[0] == '?') ins.AddLast(new PdtInstruction.PushVariable(name[1..], true));
|
||||||
|
else ins.AddLast(new PdtInstruction.PushVariable(name));
|
||||||
|
}
|
||||||
buf = null;
|
buf = null;
|
||||||
TryPushAdjMul(ins, ref flag);
|
TryPushAdjMul(ins, ref flag);
|
||||||
}
|
}
|
||||||
switch (t.Type) {
|
switch (t.Type) {
|
||||||
case 0x0020:
|
case CharCategory.IdentifierBegin:
|
||||||
buf = t;
|
buf = t;
|
||||||
break;
|
break;
|
||||||
case 0x0040:
|
case CharCategory.Digit:
|
||||||
float num = float.Parse(t.Value);
|
float num = float.Parse(t.Value);
|
||||||
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.Number, BitConverter.GetBytes(num)));
|
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.Number, BitConverter.GetBytes(num)));
|
||||||
break;
|
break;
|
||||||
case 0x0100:
|
case CharCategory.StringDelimiter:
|
||||||
int strlen = t.Value.Length;
|
int strlen = t.Value.Length;
|
||||||
unsafe {
|
unsafe {
|
||||||
var strbuf = new byte[strlen * sizeof(char) + sizeof(int)];
|
var strbuf = new byte[strlen * sizeof(char) + sizeof(int)];
|
||||||
@@ -262,7 +279,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.String, strbuf));
|
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.String, strbuf));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x0200:
|
case CharCategory.OpeningBracket:
|
||||||
int pc;
|
int pc;
|
||||||
InterpretExp(ins, -1, out pc);
|
InterpretExp(ins, -1, out pc);
|
||||||
if (buf != null) {
|
if (buf != null) {
|
||||||
|
59
Assets/Cryville/Common/Pdt/PdtFragmentInterpreter.cs
Normal file
59
Assets/Cryville/Common/Pdt/PdtFragmentInterpreter.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Pdt {
|
||||||
|
/// <summary>
|
||||||
|
/// Interpreter for fragments in Property Definition Tree (PDT) file format.
|
||||||
|
/// </summary>
|
||||||
|
public class PdtFragmentInterpreter : PdtInterpreter {
|
||||||
|
public PdtFragmentInterpreter() : base(null, new EmptyBinder()) { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the new source string for the fragment interpreter and resets the position.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The new source string.</param>
|
||||||
|
public void SetSource(string value) {
|
||||||
|
Source = value;
|
||||||
|
Position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The binder.
|
||||||
|
/// </summary>
|
||||||
|
public Binder Binder {
|
||||||
|
get { return _binder; }
|
||||||
|
set { _binder = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads the current character and increments the position.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The current character.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
public new char GetChar() { return base.GetChar(); }
|
||||||
|
/// <summary>
|
||||||
|
/// Reads an identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An identifier.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
public new string GetIdentifier() { return base.GetIdentifier(); }
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a number.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A number.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
public new string GetNumber() { return base.GetNumber(); }
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A string.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
public new string GetString() { return base.GetString(); }
|
||||||
|
/// <summary>
|
||||||
|
/// Reads an expression.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An expression.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
public new PdtExpression GetExp() { return base.GetExp(); }
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Pdt/PdtFragmentInterpreter.cs.meta
Normal file
11
Assets/Cryville/Common/Pdt/PdtFragmentInterpreter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f7088ba23ed4b424eadaf664be48e376
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,4 +1,4 @@
|
|||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The identifiers of the internal types of PDT.
|
/// The identifiers of the internal types of PDT.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -6,30 +6,30 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Error type.
|
/// Error type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Error = 0x00525245;
|
public const int Error = 0x00525245;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Array of a same variable-length type, with a suffix indicating the element count and the element type.
|
/// Array of a same variable-length type, with a suffix indicating the element count and the element type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Array = 0x00525241;
|
public const int Array = 0x00525241;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Null type.
|
/// Null type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Null = 0x4c4c554e;
|
public const int Null = 0x4c4c554e;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// IEEE 754 32-bit floating-point number.
|
/// IEEE 754 32-bit floating-point number.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Number = 0x004d554e;
|
public const int Number = 0x004d554e;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A sequence of UTF-16 code units, with a prefix indicating the number of the code units.
|
/// A sequence of UTF-16 code units, with a prefix indicating the number of the code units.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int String = 0x00525453;
|
public const int String = 0x00525453;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A sequence of UTF-16 code units, with a prefix indicating the number of the code units, representing the name of an undefined variable.
|
/// A sequence of UTF-16 code units, with a prefix indicating the number of the code units, representing the name of an undefined variable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Undefined = 0x00444e55;
|
public const int Undefined = 0x00444e55;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vector of a same constant-length type, with a suffix indicating the element type.
|
/// Vector of a same constant-length type, with a suffix indicating the element type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static int Vector = 0x00434556;
|
public const int Vector = 0x00434556;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,42 +1,40 @@
|
|||||||
using System;
|
using Cryville.Common.Collections;
|
||||||
using System.Collections;
|
using Cryville.Common.Reflection;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text;
|
||||||
|
using CMath = System.Math;
|
||||||
|
|
||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interpreter for Property Definition Tree (PDT) file format.
|
/// Interpreter for Property Definition Tree (PDT) file format.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The object type represented by the PDT.</typeparam>
|
public partial class PdtInterpreter {
|
||||||
public partial class PdtInterpreter<T> {
|
[Flags]
|
||||||
/// <summary>
|
protected enum CharCategory {
|
||||||
/// The character category map.
|
WhiteSpace = 0x0001,
|
||||||
/// </summary>
|
Identifier = 0x0010,
|
||||||
/// <remarks>
|
IdentifierBegin = 0x0020,
|
||||||
/// <list type="bullet">
|
Digit = 0x0040,
|
||||||
/// <item><term><c>0x0001</c></term><description>White Space</description></item>
|
Operator = 0x0080,
|
||||||
/// <item><term><c>0x0010</c></term><description>Identifier</description></item>
|
StringDelimiter = 0x0100,
|
||||||
/// <item><term><c>0x0020</c></term><description>Identifier Begin</description></item>
|
OpeningBracket = 0x0200,
|
||||||
/// <item><term><c>0x0040</c></term><description>Digit</description></item>
|
ClosingBracket = 0x0400,
|
||||||
/// <item><term><c>0x0080</c></term><description>Operator</description></item>
|
EndOfExpression = 0x0800,
|
||||||
/// <item><term><c>0x0100</c></term><description>String</description></item>
|
EndOfKey = 0x1000,
|
||||||
/// <item><term><c>0x0200</c></term><description>Opening Bracket</description></item>
|
}
|
||||||
/// <item><term><c>0x0400</c></term><description>Closing Bracket</description></item>
|
static readonly int[] cm = new int[] {
|
||||||
/// <item><term><c>0x0800</c></term><description>End of Expression</description></item>
|
|
||||||
/// <item><term><c>0x1000</c></term><description>End of Key</description></item>
|
|
||||||
/// </list>
|
|
||||||
/// </remarks>
|
|
||||||
readonly static int[] cm = new int[] {
|
|
||||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000,
|
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000,
|
||||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||||
0x0001, 0x0080, 0x0100, 0x0000, 0x0030, 0x0080, 0x0080, 0x0000, 0x0200, 0x0400, 0x0080, 0x0080, 0x0080, 0x0080, 0x0040, 0x0080,
|
0x0001, 0x0080, 0x0100, 0x0000, 0x0030, 0x0080, 0x0080, 0x0000, 0x0200, 0x0400, 0x0080, 0x0080, 0x0080, 0x0080, 0x0040, 0x0080,
|
||||||
0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x1000, 0x1800, 0x0080, 0x0080, 0x0080, 0x0000,
|
0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x1000, 0x1800, 0x0080, 0x0080, 0x0080, 0x0030,
|
||||||
0x0080, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
|
0x0080, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
|
||||||
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0030,
|
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0000, 0x0080, 0x0000, 0x0080, 0x0030,
|
||||||
0x0000, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
|
0x0000, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
|
||||||
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x1000, 0x0080, 0x1000, 0x0000, 0x0000,
|
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x1000, 0x0080, 0x1000, 0x0080, 0x0000,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -44,61 +42,120 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="src">The source string.</param>
|
/// <param name="src">The source string.</param>
|
||||||
/// <returns>The interpreted object.</returns>
|
/// <returns>The interpreted object.</returns>
|
||||||
public static T Interpret(string src) {
|
public static T Interpret<T>(string src) {
|
||||||
return Interpret(src, BinderAttribute.CreateBinderOfType(typeof(T)));
|
return Interpret<T>(src, BinderAttribute.CreateBinderOfType(typeof(T)));
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interprets a source string to an object of type <typeparamref name="T" /> with a binder.
|
/// Interprets a source string to an object of type <typeparamref name="T"/> with a binder.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="src">The source string.</param>
|
/// <param name="src">The source string.</param>
|
||||||
/// <param name="binder">The binder.</param>
|
/// <param name="binder">The binder.</param>
|
||||||
/// <returns>The interpreted object.</returns>
|
/// <returns>The interpreted object.</returns>
|
||||||
public static T Interpret(string src, Binder binder) {
|
public static T Interpret<T>(string src, Binder binder) {
|
||||||
return new PdtInterpreter<T>(src, binder).Interpret();
|
return (T)new PdtInterpreter(src, binder).Interpret(typeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly string _src;
|
/// <summary>
|
||||||
readonly Binder _binder;
|
/// The source string.
|
||||||
protected int Position { get; private set; }
|
/// </summary>
|
||||||
|
public string Source { get; protected set; }
|
||||||
|
protected Binder _binder;
|
||||||
|
/// <summary>
|
||||||
|
/// The current position in the string being parsed by the interpreter.
|
||||||
|
/// </summary>
|
||||||
|
public int Position { get; protected set; }
|
||||||
|
|
||||||
|
readonly StringBuilder _sb = new();
|
||||||
#pragma warning disable IDE1006
|
#pragma warning disable IDE1006
|
||||||
protected char cc { get { return _src[Position]; } }
|
/// <summary>
|
||||||
protected int ct { get { return cm[cc]; } }
|
/// The character at the current position.
|
||||||
protected string tokenb(int flag) { // Token Whitelist
|
/// </summary>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
protected char cc { get { return Source[Position]; } }
|
||||||
|
/// <summary>
|
||||||
|
/// The category of the character.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
|
protected CharCategory ct { get { return (CharCategory)cm[cc]; } }
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a token until a character of type <paramref name="flag" /> is met.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flag">The type filter.</param>
|
||||||
|
/// <returns>A token from the current position (inclusive) to the next character of type <paramref name="flag" /> (exclusive).</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">No character of type <paramref name="flag" /> is met.</exception>
|
||||||
|
protected string tokenb(CharCategory flag) {
|
||||||
int sp = Position;
|
int sp = Position;
|
||||||
while ((ct & flag) == 0) Position++;
|
while ((ct & flag) == 0) Position++;
|
||||||
return _src.Substring(sp, Position - sp);
|
return Source[sp..Position];
|
||||||
}
|
}
|
||||||
protected string tokenw(int flag) { // Token Whitelist
|
/// <summary>
|
||||||
|
/// Reads a token until a character that is not of type <paramref name="flag" /> is met.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flag">The type filter.</param>
|
||||||
|
/// <returns>A token from the current position (inclusive) to the next character that is not of type <paramref name="flag" /> (exclusive).</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">No character that is not of type <paramref name="flag" /> is met.</exception>
|
||||||
|
protected string tokenw(CharCategory flag) {
|
||||||
int sp = Position;
|
int sp = Position;
|
||||||
while ((ct & flag) != 0) Position++;
|
while ((ct & flag) != 0) Position++;
|
||||||
return _src.Substring(sp, Position - sp);
|
return Source[sp..Position];
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Skips over whitespaces.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected void ws() {
|
protected void ws() {
|
||||||
while ((ct & 0x0001) != 0) Position++;
|
while ((ct & CharCategory.WhiteSpace) != 0) Position++;
|
||||||
}
|
}
|
||||||
#pragma warning restore IDE1006
|
#pragma warning restore IDE1006
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads the current character and increments the position.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The current character.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected char GetChar() {
|
protected char GetChar() {
|
||||||
char r = cc;
|
char r = cc;
|
||||||
Position++;
|
Position++;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Reads an identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An identifier.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected string GetIdentifier() {
|
protected string GetIdentifier() {
|
||||||
if ((ct & 0x0020) == 0) return "";
|
if ((ct & CharCategory.IdentifierBegin) == 0) return "";
|
||||||
return tokenw(0x0010);
|
return tokenw(CharCategory.Identifier);
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a number.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A number.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected string GetNumber() {
|
protected string GetNumber() {
|
||||||
return tokenw(0x0040);
|
return tokenw(CharCategory.Digit);
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A string.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected string GetString() {
|
protected string GetString() {
|
||||||
int sp = Position;
|
_sb.Clear();
|
||||||
do {
|
|
||||||
if (cc == '\\') Position++;
|
|
||||||
Position++;
|
|
||||||
} while (ct != 0x0100);
|
|
||||||
Position++;
|
Position++;
|
||||||
return Regex.Replace(_src.Substring(sp + 1, Position - sp - 2), @"\\(.)", "$1");
|
while (ct != CharCategory.StringDelimiter) {
|
||||||
|
if (cc == '\\') Position++;
|
||||||
|
_sb.Append(cc);
|
||||||
|
Position++;
|
||||||
|
}
|
||||||
|
Position++;
|
||||||
|
return _sb.ToString();
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Reads an expression.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An expression.</returns>
|
||||||
|
/// <exception cref="IndexOutOfRangeException">The end of the source string is reached.</exception>
|
||||||
protected PdtExpression GetExp() {
|
protected PdtExpression GetExp() {
|
||||||
var ins = new LinkedList<PdtInstruction>();
|
var ins = new LinkedList<PdtInstruction>();
|
||||||
int _;
|
int _;
|
||||||
@@ -106,25 +163,35 @@ namespace Cryville.Common.Pdt {
|
|||||||
return new PdtExpression(ins);
|
return new PdtExpression(ins);
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly Dictionary<string, PdtExpression> defs = new Dictionary<string, PdtExpression>();
|
readonly Dictionary<string, PdtExpression> defs = new();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the <see cref="PdtInterpreter{T}" /> class.
|
/// Creates an instance of the <see cref="PdtInterpreter" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="src">The source string.</param>
|
/// <param name="src">The source string.</param>
|
||||||
/// <param name="binder">The binder. May be <c>null</c>.</param>
|
/// <param name="binder">The binder. May be <c>null</c>.</param>
|
||||||
public PdtInterpreter(string src, Binder binder) {
|
public PdtInterpreter(string src, Binder binder) {
|
||||||
_src = src;
|
Source = src;
|
||||||
_binder = binder;
|
_binder = binder;
|
||||||
if (_binder == null)
|
}
|
||||||
_binder = BinderAttribute.CreateBinderOfType(typeof(T));
|
int[] m_formatVersion;
|
||||||
|
public int[] GetFormatVersion() {
|
||||||
|
if (m_formatVersion == null) InterpretDirectives();
|
||||||
|
return m_formatVersion;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interprets the source to an object of type <typeparamref name="T" />.
|
/// Interprets the source to an object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="type">The output type.</param>
|
||||||
/// <returns>The interpreted object.</returns>
|
/// <returns>The interpreted object.</returns>
|
||||||
public T Interpret() {
|
public object Interpret(Type type) {
|
||||||
InterpretDirectives();
|
try {
|
||||||
return (T)InterpretObject(typeof(T));
|
if (m_formatVersion == null) InterpretDirectives();
|
||||||
|
_binder ??= BinderAttribute.CreateBinderOfType(type);
|
||||||
|
return InterpretObject(type);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
throw new PdtParsingException(this, ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void InterpretDirectives() {
|
void InterpretDirectives() {
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
@@ -134,11 +201,14 @@ namespace Cryville.Common.Pdt {
|
|||||||
switch (GetIdentifier()) {
|
switch (GetIdentifier()) {
|
||||||
case "ver":
|
case "ver":
|
||||||
ws();
|
ws();
|
||||||
Logger.Log("main", 3, "PDT", "Legacy PDT directive #ver={0} found. Ignoring.", GetNumber());
|
Shared.Logger.Log(3, "PDT", "Legacy PDT directive #ver={0} found. Ignoring.", GetNumber());
|
||||||
break;
|
break;
|
||||||
case "format":
|
case "format":
|
||||||
ws();
|
ws();
|
||||||
if (GetNumber() != "1")
|
m_formatVersion = (from i in GetNumber().Split('.') select int.Parse(i)).ToArray();
|
||||||
|
if (m_formatVersion.Length == 0)
|
||||||
|
throw new FormatException("Invalid format version");
|
||||||
|
if (m_formatVersion[0] != 1)
|
||||||
throw new NotSupportedException("Format not supported");
|
throw new NotSupportedException("Format not supported");
|
||||||
flag = true;
|
flag = true;
|
||||||
break;
|
break;
|
||||||
@@ -158,75 +228,103 @@ namespace Cryville.Common.Pdt {
|
|||||||
if (!flag) throw new FormatException("Format directive not found");
|
if (!flag) throw new FormatException("Format directive not found");
|
||||||
}
|
}
|
||||||
object InterpretObject(Type type) {
|
object InterpretObject(Type type) {
|
||||||
var result = ReflectionHelper.InvokeEmptyConstructor(type);
|
var result = Activator.CreateInstance(type);
|
||||||
bool dictflag = ReflectionHelper.IsGenericDictionary(type);
|
bool pcflag = PairCollection.IsPairCollection(type);
|
||||||
while (true) {
|
while (true) {
|
||||||
try { ws(); }
|
try { ws(); }
|
||||||
catch (IndexOutOfRangeException) { return result; }
|
catch (IndexOutOfRangeException) { return result; }
|
||||||
|
if (cc == '}') {
|
||||||
|
GetChar();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
object pkey = InterpretKey(type);
|
object pkey = InterpretKey(type);
|
||||||
char c = GetChar();
|
char c = GetChar();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '{':
|
case '{':
|
||||||
if (dictflag) {
|
InterpretObjectInternal<ElementListAttribute>(pcflag, type, pkey, result, type => InterpretObject(type));
|
||||||
var ktype = type.GetGenericArguments()[0];
|
|
||||||
var ptype = type.GetGenericArguments()[1];
|
|
||||||
object key = _binder.ChangeType(pkey, ktype, null);
|
|
||||||
object value = InterpretObject(ptype);
|
|
||||||
((IDictionary)result).Add(key, value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
MemberInfo prop;
|
|
||||||
bool flag = ReflectionHelper.TryFindMemberWithAttribute<ElementListAttribute>(type, out prop);
|
|
||||||
if (!flag && pkey is string) prop = ReflectionHelper.GetMember(type, (string)pkey);
|
|
||||||
Type ptype = ReflectionHelper.GetMemberType(prop);
|
|
||||||
if (ReflectionHelper.IsGenericDictionary(ptype)) {
|
|
||||||
var ktype = ptype.GetGenericArguments()[0];
|
|
||||||
var vtype = ptype.GetGenericArguments()[1];
|
|
||||||
if (flag) {
|
|
||||||
object key = _binder.ChangeType(pkey, ktype, null);
|
|
||||||
object value = InterpretObject(vtype);
|
|
||||||
((IDictionary)ReflectionHelper.GetValue(prop, result)).Add(key, value);
|
|
||||||
}
|
|
||||||
else ReflectionHelper.SetValue(prop, result, InterpretObject(ptype));
|
|
||||||
}
|
|
||||||
else ReflectionHelper.SetValue(prop, result, InterpretObject(ptype));
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
case ';':
|
case ';':
|
||||||
var exp = c == ';' ? _emptyexp : GetExp();
|
var exp = c == ';' ? PdtExpression.Empty : GetExp();
|
||||||
if (dictflag) {
|
InterpretObjectInternal<PropertyListAttribute>(pcflag, type, pkey, result, type => _binder.ChangeType(exp, type, null));
|
||||||
var ktype = type.GetGenericArguments()[0];
|
|
||||||
var vtype = type.GetGenericArguments()[1];
|
|
||||||
object key = _binder.ChangeType(pkey, ktype, null);
|
|
||||||
object value = _binder.ChangeType(exp, vtype, null);
|
|
||||||
((IDictionary)result).Add(key, value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
MemberInfo prop;
|
|
||||||
bool flag = ReflectionHelper.TryFindMemberWithAttribute<PropertyListAttribute>(type, out prop);
|
|
||||||
if (!flag && pkey is string) prop = ReflectionHelper.GetMember(type, (string)pkey);
|
|
||||||
var ptype = ReflectionHelper.GetMemberType(prop);
|
|
||||||
if (!typeof(IDictionary).IsAssignableFrom(ptype)) {
|
|
||||||
object value = _binder.ChangeType(exp, ptype, null);
|
|
||||||
ReflectionHelper.SetValue(prop, result, value, _binder);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var ktype = ptype.GetGenericArguments()[0];
|
|
||||||
var vtype = ptype.GetGenericArguments()[1];
|
|
||||||
object key = _binder.ChangeType(pkey, ktype, null);
|
|
||||||
object value = _binder.ChangeType(exp, vtype, null);
|
|
||||||
((IDictionary)ReflectionHelper.GetValue(prop, result)).Add(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case '}':
|
default:
|
||||||
return result;
|
throw new InvalidOperationException("Internal error: Invalid key interpretation");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void InterpretObjectInternal<T>(bool pcflag, Type type, object pkey, object result, Func<Type, object> vfunc) where T : Attribute {
|
||||||
|
if (pcflag) {
|
||||||
|
using var collection = new PairCollection(result);
|
||||||
|
var ktype = type.GetGenericArguments()[0];
|
||||||
|
var ptype = type.GetGenericArguments()[1];
|
||||||
|
object key = _binder.ChangeType(pkey, ktype, null);
|
||||||
|
object value = vfunc(ptype);
|
||||||
|
collection.Add(key, value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
MemberInfo prop = null;
|
||||||
|
bool flag = false;
|
||||||
|
if (pkey is string pname) prop = FieldLikeHelper.GetMember(type, pname);
|
||||||
|
if (prop == null) {
|
||||||
|
prop = FieldLikeHelper.FindMemberWithAttribute<T>(type);
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
if (prop == null) throw new MissingMemberException(string.Format("The property \"{0}\" is not found", pkey));
|
||||||
|
Type ptype = FieldLikeHelper.GetMemberType(prop);
|
||||||
|
if (flag) {
|
||||||
|
var origCollection = FieldLikeHelper.GetValue(prop, result);
|
||||||
|
if (origCollection == null) {
|
||||||
|
FieldLikeHelper.SetValue(prop, result, origCollection = Activator.CreateInstance(ptype));
|
||||||
|
}
|
||||||
|
using var collection = new PairCollection(origCollection);
|
||||||
|
var ktype = ptype.GetGenericArguments()[0];
|
||||||
|
var vtype = ptype.GetGenericArguments()[1];
|
||||||
|
object key = _binder.ChangeType(pkey, ktype, null);
|
||||||
|
object value = vfunc(vtype);
|
||||||
|
collection.Add(key, value);
|
||||||
|
}
|
||||||
|
else FieldLikeHelper.SetValue(prop, result, vfunc(ptype), _binder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Interprets a key from the current position.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The interpreted key.</returns>
|
||||||
protected virtual object InterpretKey(Type type) {
|
protected virtual object InterpretKey(Type type) {
|
||||||
return tokenb(0x1000).Trim();
|
return tokenb(CharCategory.EndOfKey).Trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// The exception that is thrown when the interpretation of a PDT fails.
|
||||||
|
/// </summary>
|
||||||
|
public class PdtParsingException : Exception {
|
||||||
|
public PdtParsingException(PdtInterpreter interpreter) : this(interpreter, null) { }
|
||||||
|
public PdtParsingException(PdtInterpreter interpreter, Exception innerException)
|
||||||
|
: base(GenerateMessage(interpreter, innerException), innerException) { }
|
||||||
|
static string GenerateMessage(PdtInterpreter interpreter, Exception innerException) {
|
||||||
|
string src = interpreter.Source;
|
||||||
|
int pos = interpreter.Position;
|
||||||
|
if (pos >= src.Length) return "Failed to interpret the PDT: There are some missing or redundant tokens";
|
||||||
|
int lineStartPos = src.LastIndexOf('\n', pos) + 1;
|
||||||
|
int previewStartPos = src.LastIndexOf('\n', pos, CMath.Min(64, pos));
|
||||||
|
if (previewStartPos == -1) {
|
||||||
|
previewStartPos = pos - 64;
|
||||||
|
if (previewStartPos < 0) previewStartPos = 0;
|
||||||
|
}
|
||||||
|
else previewStartPos++;
|
||||||
|
int previewEndPos = src.IndexOf('\n', pos, CMath.Min(64, src.Length - pos));
|
||||||
|
if (previewEndPos == -1) {
|
||||||
|
previewEndPos = pos + 64;
|
||||||
|
if (previewEndPos > src.Length) previewEndPos = src.Length;
|
||||||
|
}
|
||||||
|
return string.Format(
|
||||||
|
"Failed to interpret the PDT at line {0}, position {1}: {2}\n{3}",
|
||||||
|
src.Take(interpreter.Position).Count(c => c == '\n') + 1,
|
||||||
|
pos - lineStartPos + 1,
|
||||||
|
innerException == null ? "Unknown error" : innerException.Message,
|
||||||
|
src[previewStartPos..previewEndPos]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PDT operator.
|
/// PDT operator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public unsafe abstract class PdtOperator {
|
public abstract unsafe class PdtOperator {
|
||||||
byte* _prmem;
|
byte* _prmem;
|
||||||
int _loadindex;
|
int _loadindex;
|
||||||
readonly PdtVariableMemory[] _operands;
|
readonly PdtVariableMemory[] _operands;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The count of the operands loaded.
|
/// The count of the operands loaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected int LoadedOperandCount { get { return ParamCount - _loadindex; } }
|
protected int LoadedOperandCount { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the operand at the specified index.
|
/// Gets the operand at the specified index.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -19,47 +19,45 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <returns>The operand at the specified index.</returns>
|
/// <returns>The operand at the specified index.</returns>
|
||||||
/// <exception cref="IndexOutOfRangeException"><paramref name="index" /> is not less than <see cref="LoadedOperandCount" /> or less than 0.</exception>
|
/// <exception cref="IndexOutOfRangeException"><paramref name="index" /> is not less than <see cref="LoadedOperandCount" /> or less than 0.</exception>
|
||||||
protected PdtVariableMemory GetOperand(int index) {
|
protected PdtVariableMemory GetOperand(int index) {
|
||||||
if (index >= LoadedOperandCount || index < 0) throw new IndexOutOfRangeException();
|
if (index >= LoadedOperandCount || index < 0)
|
||||||
|
throw new ArgumentOutOfRangeException("index");
|
||||||
int i = index + _loadindex;
|
int i = index + _loadindex;
|
||||||
return _operands[i];
|
return _operands[i];
|
||||||
}
|
}
|
||||||
internal int ParamCount { get; private set; }
|
readonly int _pc;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the <see cref="PdtOperator" /> class.
|
/// Creates an instance of the <see cref="PdtOperator" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pc">The suggested parameter count.</param>
|
/// <param name="pc">The suggested parameter count.</param>
|
||||||
protected PdtOperator(int pc) {
|
protected PdtOperator(int pc) {
|
||||||
ParamCount = pc;
|
_pc = pc;
|
||||||
_operands = new PdtVariableMemory[pc];
|
_operands = new PdtVariableMemory[pc];
|
||||||
}
|
}
|
||||||
PdtEvaluatorBase _etor;
|
PdtEvaluatorBase _etor;
|
||||||
bool _failure = false;
|
|
||||||
bool _rfreq = true;
|
bool _rfreq = true;
|
||||||
internal void Begin(PdtEvaluatorBase etor) {
|
internal void Begin(PdtEvaluatorBase etor, int pc) {
|
||||||
_etor = etor;
|
_etor = etor;
|
||||||
_failure = false;
|
_loadindex = LoadedOperandCount = pc;
|
||||||
_loadindex = ParamCount;
|
|
||||||
}
|
}
|
||||||
internal void LoadOperand(PdtVariableMemory mem) {
|
internal void LoadOperand(PdtVariableMemory mem) {
|
||||||
if (_loadindex == 0) return;
|
if (--_loadindex >= _pc) return;
|
||||||
_operands[--_loadindex] = mem;
|
_operands[_loadindex] = mem;
|
||||||
}
|
}
|
||||||
internal void Call(byte* prmem, bool noset) {
|
internal void Call(byte* prmem, bool noset) {
|
||||||
_prmem = prmem;
|
_prmem = prmem;
|
||||||
_rfreq = false;
|
_rfreq = false;
|
||||||
try { Execute(); } catch (Exception ex) {
|
try { Execute(); } catch (Exception ex) {
|
||||||
if (_rfreq) _etor.DiscardStack();
|
if (_rfreq) _etor.DiscardStack();
|
||||||
throw new InvalidOperationException("Evaluation failed", ex);
|
throw new EvaluationFailureException("Evaluation failed", ex);
|
||||||
}
|
}
|
||||||
if (_failure) {
|
if (!_rfreq && !noset) throw new EvaluationFailureException("Return frame not set");
|
||||||
if (_rfreq) _etor.DiscardStack();
|
|
||||||
throw new InvalidOperationException("Evaluation failed");
|
|
||||||
}
|
|
||||||
if (!_rfreq && !noset) throw new InvalidOperationException("Return frame not set");
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes the operator.
|
/// Executes the operator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>When overridden, this method reads operands by calling <see cref="GetOperand(int)" />, and writes the result to the frame obtained by calling <see cref="GetReturnFrame(int, int)" />.</para>
|
||||||
|
/// </remarks>
|
||||||
protected abstract void Execute();
|
protected abstract void Execute();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a return frame.
|
/// Gets a return frame.
|
||||||
@@ -69,10 +67,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <returns>The return frame.</returns>
|
/// <returns>The return frame.</returns>
|
||||||
/// <exception cref="InvalidOperationException">The return frame has already been requested.</exception>
|
/// <exception cref="InvalidOperationException">The return frame has already been requested.</exception>
|
||||||
protected PdtVariableMemory GetReturnFrame(int type, int len) {
|
protected PdtVariableMemory GetReturnFrame(int type, int len) {
|
||||||
if (_rfreq) {
|
if (_rfreq) throw new InvalidOperationException("Return frame requested twice");
|
||||||
_failure = true;
|
|
||||||
throw new InvalidOperationException("Return frame already requested");
|
|
||||||
}
|
|
||||||
_rfreq = true;
|
_rfreq = true;
|
||||||
return _etor.StackAlloc(type, _prmem, len);
|
return _etor.StackAlloc(type, _prmem, len);
|
||||||
}
|
}
|
||||||
@@ -96,7 +91,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="name">The name of the operator.</param>
|
/// <param name="name">The name of the operator.</param>
|
||||||
/// <param name="paramCount">The parameter count.</param>
|
/// <param name="paramCount">The parameter count.</param>
|
||||||
public PdtOperatorSignature(string name, int paramCount)
|
public PdtOperatorSignature(string name, int paramCount)
|
||||||
: this(IdentifierManager.SharedInstance.Request(name), paramCount) { }
|
: this(IdentifierManager.Shared.Request(name), paramCount) { }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an operator signature.
|
/// Creates an operator signature.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -107,18 +102,18 @@ namespace Cryville.Common.Pdt {
|
|||||||
ParamCount = paramCount;
|
ParamCount = paramCount;
|
||||||
_hash = Name ^ ((ParamCount << 16) | (ParamCount >> 16));
|
_hash = Name ^ ((ParamCount << 16) | (ParamCount >> 16));
|
||||||
}
|
}
|
||||||
public override bool Equals(object obj) {
|
public override readonly bool Equals(object obj) {
|
||||||
if (!(obj is PdtOperatorSignature)) return false;
|
if (obj is not PdtOperatorSignature other) return false;
|
||||||
return Equals((PdtOperatorSignature)obj);
|
return Equals(other);
|
||||||
}
|
}
|
||||||
public bool Equals(PdtOperatorSignature other) {
|
public readonly bool Equals(PdtOperatorSignature other) {
|
||||||
return Name == other.Name && ParamCount == other.ParamCount;
|
return Name == other.Name && ParamCount == other.ParamCount;
|
||||||
}
|
}
|
||||||
public override int GetHashCode() {
|
public override readonly int GetHashCode() {
|
||||||
return _hash;
|
return _hash;
|
||||||
}
|
}
|
||||||
public override string ToString() {
|
public override readonly string ToString() {
|
||||||
return string.Format("{0}({1})", IdentifierManager.SharedInstance.Retrieve(Name), ParamCount);
|
return string.Format("{0}({1})", IdentifierManager.Shared.Retrieve(Name), ParamCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using UnsafeIL;
|
||||||
|
|
||||||
namespace Cryville.Common.Pdt {
|
namespace Cryville.Common.Pdt {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Span on the memory of a <see cref="PdtEvaluatorBase" />.
|
/// Span on the memory of a <see cref="PdtEvaluatorBase" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public unsafe struct PdtVariableMemory {
|
public unsafe struct PdtVariableMemory : IEquatable<PdtVariableMemory> {
|
||||||
readonly byte* _ptr;
|
readonly byte* _ptr;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The length of the span.
|
/// The length of the span.
|
||||||
@@ -23,7 +24,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// Copies the memory in the span to another span.
|
/// Copies the memory in the span to another span.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dest">The destination span.</param>
|
/// <param name="dest">The destination span.</param>
|
||||||
public void CopyTo(PdtVariableMemory dest) {
|
public readonly void CopyTo(PdtVariableMemory dest) {
|
||||||
CopyTo(dest._ptr, 0, Length);
|
CopyTo(dest._ptr, 0, Length);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -31,7 +32,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dest">The destination buffer.</param>
|
/// <param name="dest">The destination buffer.</param>
|
||||||
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
|
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
|
||||||
public void CopyTo(byte[] dest, int destOffset) {
|
public readonly void CopyTo(byte[] dest, int destOffset) {
|
||||||
fixed (byte* ptr = dest) {
|
fixed (byte* ptr = dest) {
|
||||||
CopyTo(ptr, destOffset, Length);
|
CopyTo(ptr, destOffset, Length);
|
||||||
}
|
}
|
||||||
@@ -42,17 +43,27 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="dest">The destination buffer.</param>
|
/// <param name="dest">The destination buffer.</param>
|
||||||
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
|
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
|
||||||
/// <param name="length">The length to copy.</param>
|
/// <param name="length">The length to copy.</param>
|
||||||
public void CopyTo(byte* dest, int destOffset, int length) {
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is greater than the length of the span.</exception>
|
||||||
|
public readonly void CopyTo(byte* dest, int destOffset, int length) {
|
||||||
|
if (length > Length) throw new ArgumentOutOfRangeException("length");
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
dest[destOffset + i] = _ptr[i];
|
dest[destOffset + i] = _ptr[i];
|
||||||
}
|
}
|
||||||
|
/// <inheritdoc />
|
||||||
|
public readonly bool Equals(PdtVariableMemory obj) {
|
||||||
|
if (Type != obj.Type || Length != obj.Length) return false;
|
||||||
|
for (int i = 0; i < Length; i++) {
|
||||||
|
if (*(_ptr + i) != *(obj._ptr + i)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the memory of the span as a number.
|
/// Gets the memory of the span as a number.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="offset">The offset on the span to start reading from.</param>
|
/// <param name="offset">The offset on the span to start reading from.</param>
|
||||||
/// <returns>A number.</returns>
|
/// <returns>A number.</returns>
|
||||||
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
|
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
|
||||||
public float AsNumber(int offset = 0) {
|
public readonly float AsNumber(int offset = 0) {
|
||||||
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
|
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
|
||||||
throw new InvalidCastException("Not a number");
|
throw new InvalidCastException("Not a number");
|
||||||
float value;
|
float value;
|
||||||
@@ -68,7 +79,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="offset">The offset from the start of the span.</param>
|
/// <param name="offset">The offset from the start of the span.</param>
|
||||||
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
|
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
|
||||||
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
|
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
|
||||||
public void SetNumber(float value, int offset = 0) {
|
public readonly void SetNumber(float value, int offset = 0) {
|
||||||
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
|
if (Type != PdtInternalType.Number && Type != PdtInternalType.Vector)
|
||||||
throw new InvalidCastException("Not a number");
|
throw new InvalidCastException("Not a number");
|
||||||
if (Length < sizeof(float) + offset)
|
if (Length < sizeof(float) + offset)
|
||||||
@@ -83,7 +94,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="offset">The offset on the span to start reading from.</param>
|
/// <param name="offset">The offset on the span to start reading from.</param>
|
||||||
/// <returns>A string.</returns>
|
/// <returns>A string.</returns>
|
||||||
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
|
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
|
||||||
public string AsString(int offset = 0) {
|
public readonly string AsString(int offset = 0) {
|
||||||
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
|
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
|
||||||
throw new InvalidCastException("Not a string");
|
throw new InvalidCastException("Not a string");
|
||||||
var len = *(int*)(_ptr + offset);
|
var len = *(int*)(_ptr + offset);
|
||||||
@@ -96,7 +107,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="offset">The offset from the start of the span.</param>
|
/// <param name="offset">The offset from the start of the span.</param>
|
||||||
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
|
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
|
||||||
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
|
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
|
||||||
public void SetString(string value, int offset = 0) {
|
public readonly void SetString(string value, int offset = 0) {
|
||||||
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
|
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
|
||||||
throw new InvalidCastException("Not a string");
|
throw new InvalidCastException("Not a string");
|
||||||
int strlen = value.Length;
|
int strlen = value.Length;
|
||||||
@@ -113,15 +124,48 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="offset">The offset on the span to start reading from.</param>
|
/// <param name="offset">The offset on the span to start reading from.</param>
|
||||||
/// <returns>The name of an undefined identifier.</returns>
|
/// <returns>The name of an undefined identifier.</returns>
|
||||||
/// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception>
|
/// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception>
|
||||||
public int AsIdentifier(int offset = 0) {
|
public readonly int AsIdentifier(int offset = 0) {
|
||||||
if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array)
|
if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array)
|
||||||
throw new InvalidCastException("Not an identifier");
|
throw new InvalidCastException("Not an identifier");
|
||||||
return *(int*)(_ptr + offset);
|
return *(int*)(_ptr + offset);
|
||||||
}
|
}
|
||||||
internal void* TrustedAsOfLength(int len) {
|
/// <summary>
|
||||||
if (Length < len)
|
/// Gets the memory of the span as an instance of the specified type.
|
||||||
throw new InvalidCastException("Type not matched");
|
/// </summary>
|
||||||
return _ptr;
|
/// <typeparam name="T">The specified type.</typeparam>
|
||||||
|
/// <param name="offset">The offset on the span to start reading from.</param>
|
||||||
|
/// <returns>An instance of the specified type.</returns>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset" /> is not less than the length of the span.</exception>
|
||||||
|
/// <exception cref="InvalidCastException">The length of the span is not sufficient.</exception>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>Use <see cref="AsNumber(int)" /> instead while reading an unaligned number.</para>
|
||||||
|
/// </remarks>
|
||||||
|
public readonly T As<T>(int offset = 0) {
|
||||||
|
var len = Unsafe.SizeOf<T>();
|
||||||
|
if (offset >= Length)
|
||||||
|
throw new ArgumentOutOfRangeException("offset");
|
||||||
|
if (offset + len > Length)
|
||||||
|
throw new InvalidCastException("Frame length not sufficient");
|
||||||
|
return Unsafe.Read<T>(_ptr + offset);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the memory of the span to an instance of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The specified type.</typeparam>
|
||||||
|
/// <param name="value">The value.</param>
|
||||||
|
/// <param name="offset">The offset from the start of the span.</param>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset" /> is not less than the length of the span.</exception>
|
||||||
|
/// <exception cref="InvalidCastException">The length of the span is not sufficient.</exception>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>Use <see cref="SetNumber(float, int)" /> instead while writing an unaligned number.</para>
|
||||||
|
/// </remarks>
|
||||||
|
public readonly void Set<T>(T value, int offset = 0) {
|
||||||
|
var len = Unsafe.SizeOf<T>();
|
||||||
|
if (offset >= Length)
|
||||||
|
throw new ArgumentOutOfRangeException("offset");
|
||||||
|
if (offset + len > Length)
|
||||||
|
throw new InvalidCastException("Frame length not sufficient");
|
||||||
|
Unsafe.Write(_ptr + offset, value);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the array suffix.
|
/// Gets the array suffix.
|
||||||
@@ -129,7 +173,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="arrtype">The type of the array.</param>
|
/// <param name="arrtype">The type of the array.</param>
|
||||||
/// <param name="pc">The item count of the array.</param>
|
/// <param name="pc">The item count of the array.</param>
|
||||||
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
|
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
|
||||||
public void GetArraySuffix(out int arrtype, out int pc) {
|
public readonly void GetArraySuffix(out int arrtype, out int pc) {
|
||||||
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
|
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
|
||||||
throw new InvalidCastException("Not an array or vector");
|
throw new InvalidCastException("Not an array or vector");
|
||||||
arrtype = *(int*)(_ptr + Length - sizeof(int));
|
arrtype = *(int*)(_ptr + Length - sizeof(int));
|
||||||
@@ -142,7 +186,7 @@ namespace Cryville.Common.Pdt {
|
|||||||
/// <param name="arrtype">The type of the array.</param>
|
/// <param name="arrtype">The type of the array.</param>
|
||||||
/// <param name="pc">The item count of the array.</param>
|
/// <param name="pc">The item count of the array.</param>
|
||||||
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
|
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
|
||||||
public void SetArraySuffix(int arrtype, int pc = 0) {
|
public readonly void SetArraySuffix(int arrtype, int pc = 0) {
|
||||||
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
|
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
|
||||||
throw new InvalidCastException("Not an array or vector");
|
throw new InvalidCastException("Not an array or vector");
|
||||||
*(int*)(_ptr + Length - sizeof(int)) = arrtype;
|
*(int*)(_ptr + Length - sizeof(int)) = arrtype;
|
||||||
|
28
Assets/Cryville/Common/Qualified.cs
Normal file
28
Assets/Cryville/Common/Qualified.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Cryville.Common {
|
||||||
|
public struct Qualified<T> : IFormattable where T : IConvertible {
|
||||||
|
static readonly string _prefixes = "yzafpnμm kMGTPEZY";
|
||||||
|
public T Value { get; set; }
|
||||||
|
public string Unit { get; set; }
|
||||||
|
|
||||||
|
public Qualified(string unit) : this(default, unit) { }
|
||||||
|
public Qualified(T value, string unit) {
|
||||||
|
Value = value;
|
||||||
|
Unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override readonly string ToString() { return ToString("G3"); }
|
||||||
|
public readonly string ToString(string format) { return ToString(format, null); }
|
||||||
|
public readonly string ToString(string format, IFormatProvider formatProvider) {
|
||||||
|
double value = Value.ToDouble(formatProvider);
|
||||||
|
int expIndex = (int)System.Math.Log10(value) / 3;
|
||||||
|
if (expIndex == 0) {
|
||||||
|
return value.ToString(format, formatProvider) + Unit;
|
||||||
|
}
|
||||||
|
int prefixIndex = System.Math.Clamp(expIndex + 8, 0, _prefixes.Length - 1);
|
||||||
|
value /= System.Math.Pow(1e3, prefixIndex - 8);
|
||||||
|
return value.ToString(format, formatProvider) + _prefixes[prefixIndex] + Unit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Qualified.cs.meta
Normal file
11
Assets/Cryville/Common/Qualified.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 909a28e78d4239f4c9507c3c9128baf2
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,186 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Cryville.Common {
|
|
||||||
/// <summary>
|
|
||||||
/// Provides a set of <see langword="static" /> methods for refletion.
|
|
||||||
/// </summary>
|
|
||||||
public static class ReflectionHelper {
|
|
||||||
static readonly Type[] emptyTypeArray = {};
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the parameterless constructor of a type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <returns>The parameterless constructor of the type.</returns>
|
|
||||||
public static ConstructorInfo GetEmptyConstructor(Type type) {
|
|
||||||
return type.GetConstructor(emptyTypeArray);
|
|
||||||
}
|
|
||||||
static readonly object[] emptyObjectArray = {};
|
|
||||||
/// <summary>
|
|
||||||
/// Invokes the parameterless constructor of a type and returns the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <returns>The created instance.</returns>
|
|
||||||
public static object InvokeEmptyConstructor(Type type) {
|
|
||||||
return GetEmptyConstructor(type).Invoke(emptyObjectArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to find a member with the specified attribute type in a type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The attribute type.</typeparam>
|
|
||||||
/// <param name="t">The type containing the member with the specified attribute type.</param>
|
|
||||||
/// <param name="mi">The member.</param>
|
|
||||||
/// <returns>Whether the member is found.</returns>
|
|
||||||
public static bool TryFindMemberWithAttribute<T>(Type t, out MemberInfo mi) where T : Attribute {
|
|
||||||
try {
|
|
||||||
mi = FindMemberWithAttribute<T>(t);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (MissingMemberException) {
|
|
||||||
mi = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Finds a member with the specified attribute type in a type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The attribute type.</typeparam>
|
|
||||||
/// <param name="type">The type containing the member with the specified attribute type.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
/// <exception cref="MissingMemberException">The member is not found or multiple members are found.</exception>
|
|
||||||
public static MemberInfo FindMemberWithAttribute<T>(Type type) where T : Attribute {
|
|
||||||
var mil = type.FindMembers(
|
|
||||||
MemberTypes.Field | MemberTypes.Property,
|
|
||||||
BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static,
|
|
||||||
(m, o) => m.GetCustomAttributes(typeof(T), true).Length != 0,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
if (mil.Length != 1)
|
|
||||||
throw new MissingMemberException(type.Name, typeof(T).Name);
|
|
||||||
return mil[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets whether a type is a <see cref="Dictionary{TKey, TValue}" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <returns>Whether the type is a <see cref="Dictionary{TKey, TValue}" />.</returns>
|
|
||||||
public static bool IsGenericDictionary(Type type) {
|
|
||||||
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the member from a type with the specified name.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <param name="name">The name of the member.</param>
|
|
||||||
/// <returns>The member.</returns>
|
|
||||||
/// <exception cref="MissingMemberException">The member is not found or multiple members are found.</exception>
|
|
||||||
public static MemberInfo GetMember(Type type, string name) {
|
|
||||||
var mil = type.GetMember(
|
|
||||||
name,
|
|
||||||
MemberTypes.Field | MemberTypes.Property,
|
|
||||||
BindingFlags.Public | BindingFlags.Instance
|
|
||||||
);
|
|
||||||
if (mil.Length != 1)
|
|
||||||
throw new MissingMemberException(type.Name, name);
|
|
||||||
return mil[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the type of a member.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mi">The member.</param>
|
|
||||||
/// <returns>The type of the member.</returns>
|
|
||||||
/// <exception cref="ArgumentException"><paramref name="mi" /> is not a field or a property.</exception>
|
|
||||||
public static Type GetMemberType(MemberInfo mi) {
|
|
||||||
if (mi is FieldInfo)
|
|
||||||
return ((FieldInfo)mi).FieldType;
|
|
||||||
if (mi is PropertyInfo)
|
|
||||||
return ((PropertyInfo)mi).PropertyType;
|
|
||||||
else
|
|
||||||
throw new ArgumentException("Member is not field or property.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the value of a member of an object.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mi">The member.</param>
|
|
||||||
/// <param name="obj">The object.</param>
|
|
||||||
/// <returns>The value.</returns>
|
|
||||||
/// <exception cref="ArgumentException"><paramref name="mi" /> is not a field or a property.</exception>
|
|
||||||
public static object GetValue(MemberInfo mi, object obj) {
|
|
||||||
if (mi is FieldInfo)
|
|
||||||
return ((FieldInfo)mi).GetValue(obj);
|
|
||||||
else if (mi is PropertyInfo)
|
|
||||||
return ((PropertyInfo)mi).GetValue(obj, new object[]{});
|
|
||||||
else
|
|
||||||
throw new ArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the value of a member of an object.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="mi">The member.</param>
|
|
||||||
/// <param name="obj">The object.</param>
|
|
||||||
/// <param name="value">The value.</param>
|
|
||||||
/// <param name="binder">An optional binder to convert the value.</param>
|
|
||||||
/// <exception cref="ArgumentException"><paramref name="mi" /> is not a field or a property.</exception>
|
|
||||||
public static void SetValue(MemberInfo mi, object obj, object value, Binder binder = null) {
|
|
||||||
if (mi is FieldInfo)
|
|
||||||
((FieldInfo)mi).SetValue(obj, value, BindingFlags.Default, binder, null);
|
|
||||||
else if (mi is PropertyInfo)
|
|
||||||
((PropertyInfo)mi).SetValue(obj, value, BindingFlags.Default, binder, emptyObjectArray, null);
|
|
||||||
else
|
|
||||||
throw new ArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets all the subclasses of a type in the current app domain.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type.</typeparam>
|
|
||||||
/// <returns>An array containing all the subclasses of the type in the current app domain.</returns>
|
|
||||||
public static Type[] GetSubclassesOf<T>() where T : class {
|
|
||||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
||||||
IEnumerable<Type> r = new List<Type>();
|
|
||||||
foreach (var a in assemblies)
|
|
||||||
r = r.Concat(a.GetTypes().Where(
|
|
||||||
t => t.IsClass
|
|
||||||
&& !t.IsAbstract
|
|
||||||
&& t.IsSubclassOf(typeof(T))
|
|
||||||
));
|
|
||||||
return r.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a simple name of a type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <returns>A simple name of the class.</returns>
|
|
||||||
public static string GetSimpleName(Type type) {
|
|
||||||
string result = type.Name;
|
|
||||||
var typeargs = type.GetGenericArguments();
|
|
||||||
if (typeargs.Length > 0) {
|
|
||||||
result = string.Format("{0}[{1}]", result, string.Join(",", from a in typeargs select GetSimpleName(a)));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the namespace qualified name of a type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type.</param>
|
|
||||||
/// <returns>The namespace qualified name of the class.</returns>
|
|
||||||
public static string GetNamespaceQualifiedName(Type type) {
|
|
||||||
string result = type.Namespace + "." + type.Name;
|
|
||||||
var typeargs = type.GetGenericArguments();
|
|
||||||
if (typeargs.Length > 0) {
|
|
||||||
result = string.Format("{0}[{1}]", result, string.Join(",", from a in typeargs select GetNamespaceQualifiedName(a)));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 16857d076fc8d534788424e6c6d180e0
|
|
||||||
timeCreated: 1608801352
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
7
Assets/Cryville/Common/Shared.cs
Normal file
7
Assets/Cryville/Common/Shared.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using Cryville.Common.Logging;
|
||||||
|
|
||||||
|
namespace Cryville.Common {
|
||||||
|
public static class Shared {
|
||||||
|
public static readonly Logger Logger = new();
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Shared.cs.meta
Normal file
11
Assets/Cryville/Common/Shared.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ef30832cb8f75dd4bb24744d068553f2
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ namespace Cryville.Common {
|
|||||||
/// <param name="s">The file name or file path.</param>
|
/// <param name="s">The file name or file path.</param>
|
||||||
/// <returns>The file name or file path with the extension removed.</returns>
|
/// <returns>The file name or file path with the extension removed.</returns>
|
||||||
public static string TrimExt(string s) {
|
public static string TrimExt(string s) {
|
||||||
return s.Substring(0, s.LastIndexOf("."));
|
return s[..s.LastIndexOf(".")];
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts the value of a <see cref="TimeSpan" /> to a human-readable string.
|
/// Converts the value of a <see cref="TimeSpan" /> to a human-readable string.
|
||||||
@@ -44,5 +44,21 @@ namespace Cryville.Common {
|
|||||||
if (result.Length == 0) return "_";
|
if (result.Length == 0) return "_";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the process path from a command.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">The command.</param>
|
||||||
|
/// <returns>The process path.</returns>
|
||||||
|
public static string GetProcessPathFromCommand(string command) {
|
||||||
|
command = command.Trim();
|
||||||
|
if (command[0] == '"') {
|
||||||
|
return command[1..command.IndexOf('"', 1)];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int e = command.IndexOf(' ');
|
||||||
|
if (e == -1) return command;
|
||||||
|
else return command[..e];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
95
Assets/Cryville/Common/Tweener.cs
Normal file
95
Assets/Cryville/Common/Tweener.cs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Cryville.Common {
|
||||||
|
public class PropertyTweener<T> {
|
||||||
|
readonly Func<T> _getter;
|
||||||
|
readonly Action<T> _setter;
|
||||||
|
readonly Tweener<T> _tweener;
|
||||||
|
public PropertyTweener(Func<T> getter, Action<T> setter, Tweener<T> tweener) {
|
||||||
|
_getter = getter;
|
||||||
|
_setter = setter;
|
||||||
|
_tweener = tweener;
|
||||||
|
var initialValue = getter();
|
||||||
|
_tweener.Start(initialValue, initialValue, float.Epsilon);
|
||||||
|
}
|
||||||
|
public PropertyTweener<T> Start(T endValue, float duration) {
|
||||||
|
_tweener.Start(_getter(), endValue, duration);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public void Advance(float deltaTime) {
|
||||||
|
if (!_tweener.Advance(deltaTime, out var value)) return;
|
||||||
|
_setter(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public delegate T Addition<T>(T a, T b);
|
||||||
|
public delegate T Multiplication<T>(float k, T b);
|
||||||
|
public delegate float EasingFunction(float t);
|
||||||
|
public class Tweener<T> {
|
||||||
|
readonly Addition<T> _addition;
|
||||||
|
readonly Multiplication<T> _multiplication;
|
||||||
|
public Tweener(Addition<T> addition, Multiplication<T> multiplication) {
|
||||||
|
_addition = addition;
|
||||||
|
_multiplication = multiplication;
|
||||||
|
}
|
||||||
|
public EasingFunction EasingFunction { get; set; } = EasingFunctions.Linear;
|
||||||
|
public Tweener<T> With(EasingFunction easing) {
|
||||||
|
EasingFunction = easing;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T _startValue = default;
|
||||||
|
T _endValue = default;
|
||||||
|
float _duration = float.Epsilon;
|
||||||
|
float _time;
|
||||||
|
bool _endOfTween;
|
||||||
|
public Tweener<T> Start(T startValue, T endValue, float duration) {
|
||||||
|
_startValue = startValue;
|
||||||
|
_endValue = endValue;
|
||||||
|
_duration = duration;
|
||||||
|
_time = 0;
|
||||||
|
_endOfTween = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public bool Advance(float deltaTime, out T value) {
|
||||||
|
if (_endOfTween) {
|
||||||
|
value = _endValue;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_time >= _duration) {
|
||||||
|
value = _endValue;
|
||||||
|
_endOfTween = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
_time += deltaTime;
|
||||||
|
var ratio = EasingFunction(System.Math.Clamp(_time / _duration, 0, 1));
|
||||||
|
value = _addition(_multiplication(1 - ratio, _startValue), _multiplication(ratio, _endValue));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public Tweener<object> Boxed() {
|
||||||
|
return new Tweener<object>((a, b) => _addition((T)a, (T)b), (k, v) => _multiplication(k, (T)v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static class Tweeners {
|
||||||
|
public static Tweener<byte> Byte => new((a, b) => (byte)(a + b), (k, v) => (byte)(k * v));
|
||||||
|
public static Tweener<sbyte> SByte => new((a, b) => (sbyte)(a + b), (k, v) => (sbyte)(k * v));
|
||||||
|
public static Tweener<short> Int16 => new((a, b) => (short)(a + b), (k, v) => (short)(k * v));
|
||||||
|
public static Tweener<ushort> UInt16 => new((a, b) => (ushort)(a + b), (k, v) => (ushort)(k * v));
|
||||||
|
public static Tweener<int> Int32 => new((a, b) => a + b, (k, v) => (int)(k * v));
|
||||||
|
public static Tweener<uint> UInt32 => new((a, b) => a + b, (k, v) => (uint)(k * v));
|
||||||
|
public static Tweener<long> Int64 => new((a, b) => a + b, (k, v) => (long)(k * v));
|
||||||
|
public static Tweener<ulong> UInt64 => new((a, b) => a + b, (k, v) => (ulong)(k * v));
|
||||||
|
public static Tweener<IntPtr> IntPtr => new((a, b) => new IntPtr((long)a + (long)b), (k, v) => new IntPtr((long)(k * (long)v)));
|
||||||
|
public static Tweener<UIntPtr> UIntPtr => new((a, b) => new UIntPtr((ulong)a + (ulong)b), (k, v) => new UIntPtr((ulong)(k * (ulong)v)));
|
||||||
|
public static Tweener<float> Float => new((a, b) => a + b, (k, v) => k * v);
|
||||||
|
public static Tweener<double> Double => new((a, b) => a + b, (k, v) => k * v);
|
||||||
|
}
|
||||||
|
public static class EasingFunctions {
|
||||||
|
public static float Linear(float x) => x;
|
||||||
|
public static float InQuad(float x) => x * x;
|
||||||
|
public static float InCubic(float x) => x * x * x;
|
||||||
|
public static float InSine(float x) => 1 - OutSine(1 - x);
|
||||||
|
public static float OutQuad(float x) => 1 - InQuad(1 - x);
|
||||||
|
public static float OutCubic(float x) => 1 - InCubic(1 - x);
|
||||||
|
public static float OutSine(float x) => MathF.Sin(x * MathF.PI / 2);
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Cryville/Common/Tweener.cs.meta
Normal file
11
Assets/Cryville/Common/Tweener.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0b4037ba4138aae47b8da984f30b4db9
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -1,26 +0,0 @@
|
|||||||
using System;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Unity {
|
|
||||||
static class CallHelper {
|
|
||||||
public static bool HasFlag(this Enum obj, Enum flag) {
|
|
||||||
ulong num = Convert.ToUInt64(flag);
|
|
||||||
ulong num2 = Convert.ToUInt64(obj);
|
|
||||||
return (num2 & num) == num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Purge(Transform obj) {
|
|
||||||
foreach (Transform i in obj)
|
|
||||||
GameObject.Destroy(i.gameObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*public static void DownloadAndUnzip(string url, FileInfo file) {
|
|
||||||
using (DownloadDialog d = new DownloadDialog()) {
|
|
||||||
d.Download(url, file);
|
|
||||||
}
|
|
||||||
using (ZipFile z = new ZipFile(file.FullName)) {
|
|
||||||
z.ExtractAll(file.DirectoryName, ExtractExistingFileAction.OverwriteSilently);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 98b3d4b7cc1ce054598780159356da35
|
|
||||||
timeCreated: 1608801352
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,142 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.UI;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Unity {
|
|
||||||
public class FileDialog : MonoBehaviour {
|
|
||||||
Transform panel;
|
|
||||||
Transform title;
|
|
||||||
Transform drives;
|
|
||||||
Transform dirs;
|
|
||||||
Transform files;
|
|
||||||
|
|
||||||
public Action Callback { private get; set; }
|
|
||||||
|
|
||||||
#if UNITY_ANDROID && !UNITY_EDITOR_WIN
|
|
||||||
string androidStorage = "";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
string fileName = "";
|
|
||||||
public string FileName {
|
|
||||||
get { return fileName; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public string[] m_filter = new string[]{};
|
|
||||||
public string[] Filter {
|
|
||||||
set { m_filter = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma warning disable IDE0051
|
|
||||||
void Start() {
|
|
||||||
panel = gameObject.transform.Find("Panel");
|
|
||||||
title = panel.Find("Title/Text");
|
|
||||||
drives = panel.Find("Drives/DrivesInner");
|
|
||||||
dirs = panel.Find("Directories/DirectoriesInner");
|
|
||||||
files = panel.Find("Files/FilesInner");
|
|
||||||
if (CurrentDirectory == null) {
|
|
||||||
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
|
||||||
CurrentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
||||||
#elif UNITY_ANDROID
|
|
||||||
using (AndroidJavaClass ajc=new AndroidJavaClass("android.os.Environment"))
|
|
||||||
using (AndroidJavaObject file=ajc.CallStatic<AndroidJavaObject>("getExternalStorageDirectory")) {
|
|
||||||
androidStorage = file.Call<string>("getAbsolutePath");
|
|
||||||
CurrentDirectory = new DirectoryInfo(androidStorage);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#error No default directory
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
UpdateGUI();
|
|
||||||
}
|
|
||||||
#pragma warning restore IDE0051
|
|
||||||
|
|
||||||
public void Show() {
|
|
||||||
fileName = null;
|
|
||||||
gameObject.SetActive(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Close() {
|
|
||||||
if (Callback != null) Callback.Invoke();
|
|
||||||
gameObject.SetActive(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DirectoryInfo CurrentDirectory;
|
|
||||||
|
|
||||||
void OnDriveChanged(string s) {
|
|
||||||
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
|
|
||||||
CurrentDirectory = new DirectoryInfo(s);
|
|
||||||
#elif UNITY_ANDROID
|
|
||||||
switch (s) {
|
|
||||||
case "?storage":
|
|
||||||
CurrentDirectory = new DirectoryInfo(androidStorage);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#error No change drive logic
|
|
||||||
#endif
|
|
||||||
UpdateGUI();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnDirectoryChanged(string s) {
|
|
||||||
CurrentDirectory = new DirectoryInfo(CurrentDirectory.FullName + "/" + s);
|
|
||||||
UpdateGUI();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnFileChanged(string s) {
|
|
||||||
fileName = s;
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateGUI() {
|
|
||||||
title.GetComponent<Text>().text = CurrentDirectory.FullName;
|
|
||||||
|
|
||||||
CallHelper.Purge(drives);
|
|
||||||
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
|
|
||||||
var dl = Directory.GetLogicalDrives();
|
|
||||||
foreach (string d in dl) {
|
|
||||||
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
|
|
||||||
btn.GetComponentInChildren<Text>().text = d;
|
|
||||||
var ts = d;
|
|
||||||
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged(ts));
|
|
||||||
btn.transform.SetParent(drives, false);
|
|
||||||
}
|
|
||||||
#elif UNITY_ANDROID
|
|
||||||
GameObject sbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
|
|
||||||
sbtn.GetComponentInChildren<Text>().text = "Storage";
|
|
||||||
sbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged("?storage"));
|
|
||||||
sbtn.transform.SetParent(drives, false);
|
|
||||||
#else
|
|
||||||
#error No update GUI logic
|
|
||||||
#endif
|
|
||||||
|
|
||||||
CallHelper.Purge(dirs);
|
|
||||||
DirectoryInfo[] subdirs = CurrentDirectory.GetDirectories();
|
|
||||||
GameObject pbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
|
|
||||||
pbtn.GetComponentInChildren<Text>().text = "..";
|
|
||||||
pbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged(".."));
|
|
||||||
pbtn.transform.SetParent(dirs, false);
|
|
||||||
foreach (DirectoryInfo d in subdirs) {
|
|
||||||
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
|
|
||||||
btn.GetComponentInChildren<Text>().text = d.Name;
|
|
||||||
var ts = d.Name;
|
|
||||||
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged(ts));
|
|
||||||
btn.transform.SetParent(dirs, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
CallHelper.Purge(files);
|
|
||||||
FileInfo[] fl = CurrentDirectory.GetFiles();
|
|
||||||
foreach (FileInfo d in fl) {
|
|
||||||
foreach (string ext in m_filter)
|
|
||||||
if (d.Extension == ext) goto ext_matched;
|
|
||||||
continue;
|
|
||||||
ext_matched:
|
|
||||||
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
|
|
||||||
btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB");
|
|
||||||
var ts = d.FullName;
|
|
||||||
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnFileChanged(ts));
|
|
||||||
btn.transform.SetParent(files, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 9865f498871e30548959e6b28f91feae
|
|
||||||
timeCreated: 1608801352
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,9 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 7548d5a078795b04b8c54524389ba0fe
|
|
||||||
folderAsset: yes
|
|
||||||
timeCreated: 1611035780
|
|
||||||
licenseType: Free
|
|
||||||
DefaultImporter:
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,108 +0,0 @@
|
|||||||
using System;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Unity.Input {
|
|
||||||
public delegate void InputEventDelegate(InputIdentifier id, InputVector vec);
|
|
||||||
public abstract class InputHandler : IDisposable {
|
|
||||||
public event InputEventDelegate OnInput;
|
|
||||||
|
|
||||||
~InputHandler() {
|
|
||||||
Dispose(false);
|
|
||||||
}
|
|
||||||
public void Dispose() {
|
|
||||||
Dispose(true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Activated { get; private set; }
|
|
||||||
public void Activate() {
|
|
||||||
if (Activated) return;
|
|
||||||
Activated = true;
|
|
||||||
ActivateImpl();
|
|
||||||
}
|
|
||||||
protected abstract void ActivateImpl();
|
|
||||||
public void Deactivate() {
|
|
||||||
if (!Activated) return;
|
|
||||||
Activated = false;
|
|
||||||
DeactivateImpl();
|
|
||||||
}
|
|
||||||
protected abstract void DeactivateImpl();
|
|
||||||
public abstract void Dispose(bool disposing);
|
|
||||||
public abstract bool IsNullable(int type);
|
|
||||||
public abstract byte GetDimension(int type);
|
|
||||||
public abstract string GetTypeName(int type);
|
|
||||||
public abstract double GetCurrentTimestamp();
|
|
||||||
protected void Feed(int type, int id, InputVector vec) {
|
|
||||||
var del = OnInput;
|
|
||||||
if (del != null) del(new InputIdentifier { Source = new InputSource { Handler = this, Type = type }, Id = id }, vec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct InputSource : IEquatable<InputSource> {
|
|
||||||
public InputHandler Handler { get; set; }
|
|
||||||
public int Type { get; set; }
|
|
||||||
public override bool Equals(object obj) {
|
|
||||||
if (obj == null || !(obj is InputSource)) return false;
|
|
||||||
return Equals((InputSource)obj);
|
|
||||||
}
|
|
||||||
public bool Equals(InputSource other) {
|
|
||||||
return Handler == other.Handler && Type == other.Type;
|
|
||||||
}
|
|
||||||
public override int GetHashCode() {
|
|
||||||
return Handler.GetHashCode() ^ Type;
|
|
||||||
}
|
|
||||||
public override string ToString() {
|
|
||||||
return string.Format("{0}:{1}", ReflectionHelper.GetSimpleName(Handler.GetType()), Handler.GetTypeName(Type));
|
|
||||||
}
|
|
||||||
public static bool operator ==(InputSource lhs, InputSource rhs) {
|
|
||||||
return lhs.Equals(rhs);
|
|
||||||
}
|
|
||||||
public static bool operator !=(InputSource lhs, InputSource rhs) {
|
|
||||||
return !lhs.Equals(rhs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct InputIdentifier : IEquatable<InputIdentifier> {
|
|
||||||
public InputSource Source { get; set; }
|
|
||||||
public int Id { get; set; }
|
|
||||||
public override bool Equals(object obj) {
|
|
||||||
if (obj == null || !(obj is InputIdentifier)) return false;
|
|
||||||
return Equals((InputIdentifier)obj);
|
|
||||||
}
|
|
||||||
public bool Equals(InputIdentifier other) {
|
|
||||||
return Source == other.Source && Id == other.Id;
|
|
||||||
}
|
|
||||||
public override int GetHashCode() {
|
|
||||||
return Source.GetHashCode() ^ ((Id << 16) | (Id >> 16));
|
|
||||||
}
|
|
||||||
public override string ToString() {
|
|
||||||
return string.Format("{0},{1}", Source, Id);
|
|
||||||
}
|
|
||||||
public static bool operator ==(InputIdentifier lhs, InputIdentifier rhs) {
|
|
||||||
return lhs.Equals(rhs);
|
|
||||||
}
|
|
||||||
public static bool operator !=(InputIdentifier lhs, InputIdentifier rhs) {
|
|
||||||
return !lhs.Equals(rhs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct InputVector {
|
|
||||||
public double Time { get; set; }
|
|
||||||
public bool IsNull { get; set; }
|
|
||||||
public Vector3 Vector { get; set; }
|
|
||||||
public InputVector(double time) {
|
|
||||||
Time = time;
|
|
||||||
IsNull = true;
|
|
||||||
Vector = default(Vector3);
|
|
||||||
}
|
|
||||||
public InputVector(double time, Vector3 vector) {
|
|
||||||
Time = time;
|
|
||||||
IsNull = false;
|
|
||||||
Vector = vector;
|
|
||||||
}
|
|
||||||
public override string ToString() {
|
|
||||||
if (IsNull) return string.Format("null@{0}", Time);
|
|
||||||
else return string.Format("{0}@{1}", Vector.ToString("G9"), Time);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 139cf0675329c9d46b902249fecdb500
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@@ -1,94 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Cryville.Common.Unity.Input {
|
|
||||||
public class InputManager {
|
|
||||||
static readonly List<Type> HandlerRegistries = new List<Type> {
|
|
||||||
typeof(WindowsPointerHandler),
|
|
||||||
typeof(UnityKeyHandler<UnityKeyboardReceiver>),
|
|
||||||
typeof(UnityKeyHandler<UnityMouseButtonReceiver>),
|
|
||||||
typeof(UnityMouseHandler),
|
|
||||||
typeof(UnityTouchHandler),
|
|
||||||
};
|
|
||||||
// TODO set private
|
|
||||||
public readonly List<InputHandler> _handlers = new List<InputHandler>();
|
|
||||||
readonly Dictionary<Type, InputHandler> _typemap = new Dictionary<Type, InputHandler>();
|
|
||||||
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
|
|
||||||
readonly object _lock = new object();
|
|
||||||
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
|
|
||||||
readonly List<InputEvent> _events = new List<InputEvent>();
|
|
||||||
public InputManager() {
|
|
||||||
foreach (var t in HandlerRegistries) {
|
|
||||||
try {
|
|
||||||
if (!typeof(InputHandler).IsAssignableFrom(t)) continue;
|
|
||||||
var h = (InputHandler)ReflectionHelper.InvokeEmptyConstructor(t);
|
|
||||||
_typemap.Add(t, h);
|
|
||||||
h.OnInput += OnInput;
|
|
||||||
_handlers.Add(h);
|
|
||||||
_timeOrigins.Add(h, 0);
|
|
||||||
Logger.Log("main", 1, "Input", "Initialized {0}", ReflectionHelper.GetSimpleName(t));
|
|
||||||
}
|
|
||||||
catch (TargetInvocationException ex) {
|
|
||||||
Logger.Log("main", 1, "Input", "Cannot initialize {0}: {1}", ReflectionHelper.GetSimpleName(t), ex.InnerException.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public InputHandler GetHandler(string name) {
|
|
||||||
return _typemap[Type.GetType(name)];
|
|
||||||
}
|
|
||||||
public void Activate() {
|
|
||||||
lock (_lock) {
|
|
||||||
_events.Clear();
|
|
||||||
}
|
|
||||||
foreach (var h in _handlers) h.Activate();
|
|
||||||
}
|
|
||||||
public void SyncTime(double time) {
|
|
||||||
foreach (var h in _handlers) {
|
|
||||||
_timeOrigins[h] = time - h.GetCurrentTimestamp();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void Deactivate() {
|
|
||||||
foreach (var h in _handlers) h.Deactivate();
|
|
||||||
}
|
|
||||||
void OnInput(InputIdentifier id, InputVector vec) {
|
|
||||||
lock (_lock) {
|
|
||||||
double timeOrigin = _timeOrigins[id.Source.Handler];
|
|
||||||
vec.Time += timeOrigin;
|
|
||||||
InputVector vec0;
|
|
||||||
if (_vectors.TryGetValue(id, out vec0)) {
|
|
||||||
_events.Add(new InputEvent {
|
|
||||||
Id = id,
|
|
||||||
From = vec0,
|
|
||||||
To = vec,
|
|
||||||
});
|
|
||||||
if (vec.IsNull) _vectors.Remove(id);
|
|
||||||
else _vectors[id] = vec;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_events.Add(new InputEvent {
|
|
||||||
Id = id,
|
|
||||||
From = new InputVector(vec.Time),
|
|
||||||
To = vec,
|
|
||||||
});
|
|
||||||
_vectors.Add(id, vec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void EnumerateEvents(Action<InputEvent> cb) {
|
|
||||||
lock (_lock) {
|
|
||||||
foreach (var ev in _events) cb(ev);
|
|
||||||
_events.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct InputEvent {
|
|
||||||
public InputIdentifier Id { get; set; }
|
|
||||||
public InputVector From { get; set; }
|
|
||||||
public InputVector To { get; set; }
|
|
||||||
public override string ToString() {
|
|
||||||
return string.Format("[{0}] {1} -> {2}", Id, From, To);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user