-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathStringGenerator.cs
More file actions
165 lines (142 loc) · 3.55 KB
/
StringGenerator.cs
File metadata and controls
165 lines (142 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Collections.Generic;
using System.Text;
namespace il2cpp
{
internal class StringGenerator
{
private class StringProp
{
public int ConstIndex;
public int UnitIndex;
}
private readonly Dictionary<string, StringProp> StringMap = new Dictionary<string, StringProp>();
private int CurrConstIndex;
private int CurrUnitIndex = 1;
private int CurrAccLength;
public bool HasStrings { get; private set; }
public string AddString(string str)
{
HasStrings = true;
if (!StringMap.TryGetValue(str, out var prop))
{
prop = new StringProp();
prop.ConstIndex = ++CurrConstIndex;
StringMap.Add(str, prop);
}
return GetConstName(prop.ConstIndex);
}
public void Generate(Dictionary<string, CompileUnit> unitMap, uint strTypeID)
{
foreach (var unit in unitMap.Values)
{
foreach (string strDep in unit.StringDepends)
{
unit.ImplDepends.Add(StringToUnitName(strDep));
}
}
Dictionary<int, List<string>> unitStrMap = new Dictionary<int, List<string>>();
foreach (var kv in StringMap)
{
int unitIdx = kv.Value.UnitIndex;
if (!unitStrMap.TryGetValue(unitIdx, out var strList))
{
strList = new List<string>();
unitStrMap.Add(unitIdx, strList);
}
strList.Add(kv.Key);
}
foreach (var kv in unitStrMap)
{
var unit = new CompileUnit();
unit.Name = GetUnitName(kv.Key);
unit.DeclCode = GenStringCode(kv.Value, strTypeID);
unitMap[unit.Name] = unit;
}
}
private string StringToUnitName(string str)
{
StringProp prop = StringMap[str];
if (prop.UnitIndex == 0)
{
prop.UnitIndex = CurrUnitIndex;
CurrAccLength += str.Length;
if (CurrAccLength > 3000)
{
CurrAccLength = 0;
++CurrUnitIndex;
}
}
return GetUnitName(prop.UnitIndex);
}
private string GenStringCode(List<string> strList, uint strTypeID)
{
CodePrinter prt = new CodePrinter();
foreach (string str in strList)
{
StringProp prop = StringMap[str];
string strAry = StringToArrayOrRaw(str, out bool isRaw);
prt.AppendFormatLine("// {0} ", Helper.EscapeString(str));
prt.AppendFormatLine("static const struct {{ cls_Object obj; uint32_t len; {5} str[{0}]; }} {1} {{ {{{2}}}, {3}, {4} }};",
str.Length + 1,
GetConstName(prop.ConstIndex),
strTypeID,
str.Length,
strAry,
isRaw ? "char16_t" : "uint16_t");
}
return prt.ToString();
}
public static string StringToArrayOrRaw(string str, out bool isRaw)
{
isRaw = true;
StringBuilder sbRaw = new StringBuilder();
foreach (char c in str)
{
if (c >= 0x21 && c <= 0x7E)
{
switch (c)
{
case '\\':
sbRaw.Append("\\\\");
break;
case '"':
sbRaw.Append("\\\"");
break;
default:
sbRaw.Append(c);
break;
}
}
else
{
isRaw = false;
break;
}
}
if (isRaw)
return "u\"" + sbRaw + "\"";
return StringToArray(str);
}
private static string StringToArray(string str)
{
StringBuilder sb = new StringBuilder();
sb.Append('{');
for (int i = 0; i < str.Length; ++i)
{
if (i != 0)
sb.Append(',');
sb.Append((ushort)str[i]);
}
sb.Append(",0}");
return sb.ToString();
}
private static string GetConstName(int idx)
{
return "il2cppStr_" + idx;
}
private static string GetUnitName(int idx)
{
return "il2cppString_" + idx;
}
}
}