This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XAA.h
77 lines (72 loc) · 3.2 KB
/
XAA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include "Functions.h"
#include "Includes.h"
#include <regex>
#include <algorithm>
struct XAA {
System::Void SerializeToFile(System::Object^ clss, std::string path) {
System::Type^ t = clss->GetType();
cli::array<System::Reflection::FieldInfo^>^ pi = t->GetFields();
System::Text::StringBuilder^ sBuilder = gcnew System::Text::StringBuilder();
sBuilder->AppendLine(("<" + t->Name + ">"));
for (int idx = 0; idx < pi->Length; idx++)
sBuilder->AppendLine(pi[idx]->Name + ":" + pi[idx]->GetValue(clss)->ToString());
sBuilder->AppendLine(("</" + t->Name + ">"));
std::ofstream out(path.c_str());
out << msclr::interop::marshal_as<std::string>(sBuilder->ToString());
out.close();
delete sBuilder;
}
System::String^ Serialize(System::Object^ clss) {
System::Type^ t = clss->GetType();
cli::array<System::Reflection::FieldInfo^>^ pi = t->GetFields();
System::Text::StringBuilder^ sBuilder = gcnew System::Text::StringBuilder();
sBuilder->AppendLine(("<" + t->Name + ">"));
for (int idx = 0; idx < pi->Length; idx++)
sBuilder->AppendLine(pi[idx]->Name + ":" + pi[idx]->GetValue(clss)->ToString());
sBuilder->AppendLine(("</" + t->Name + ">"));
return sBuilder->ToString();
}
System::Object^ DeSerialize(cli::array<System::String^>^ xaa, System::Type^ clss) {
System::Object^ m = System::Activator::CreateInstance(clss);
for (int idx = 1; idx < xaa->Length - 2; idx++) {
cli::array<System::String^>^ tokenSplit = xaa[idx]->Split(':');
switch (Functions::SwitchString(msclr::interop::marshal_as<std::string>(clss->GetField(tokenSplit[0])->FieldType->ToString()).c_str())) {
case Functions::SwitchString("System.Int32"): {
clss->GetField(tokenSplit[0])->SetValue(m, System::Int32::Parse(tokenSplit[1]));
break;
}
case Functions::SwitchString("System.String"): {
clss->GetField(tokenSplit[0])->SetValue(m, tokenSplit[1]);
break;
}
case Functions::SwitchString("System.Collections.Generic.List`1[System.String]"): {
std::regex r("[(.*?)]");
std::smatch ma;
std::string atemp = msclr::interop::marshal_as<std::string>(tokenSplit[1]->ToString());
System::Collections::Generic::List<System::String^>^ tmp = gcnew System::Collections::Generic::List<System::String^>();
while(std::regex_search(atemp, ma, r)){ // Why regex will no longer take a "temporary" string as a parameter, I will not know.
tmp->Add(msclr::interop::marshal_as<System::String^>(ma.str(0)));
}
clss->GetField(tokenSplit[0])->SetValue(m, tmp);
delete tmp;
break;
}
case Functions::SwitchString("System.Collections.Generic.List`1[System.Int32]"): {
std::regex r("[(.*?)]");
std::smatch ma;
std::string atemp = msclr::interop::marshal_as<std::string>(tokenSplit[1]->ToString());
System::Collections::Generic::List<System::Int32>^ tmp = gcnew System::Collections::Generic::List<System::Int32>();
while (std::regex_search(atemp, ma, r)) { // Why regex will no longer take a "temporary" string as a parameter, I will not know.
tmp->Add(System::Int32::Parse(msclr::interop::marshal_as<System::String^>(ma.str(0))));
}
clss->GetField(tokenSplit[0])->SetValue(m, tmp);
delete tmp;
break;
}
}
delete tokenSplit;
}
return m;
}
};