-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
236 lines (205 loc) · 7.81 KB
/
Program.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
CS to GDScript transpiler
by mjt, 2019
*/
using System;
using System.Collections.Generic;
namespace CS2GDScript
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("# CS2GDScript by mjt 2019\n");
bool writeOrig = true;
bool firstFunc = false;
bool comment = false;
string outstr = "";
string[] lines;
List<string> funcs = new List<string>();
if (args.Length > 0)
{
lines = System.IO.File.ReadAllLines(args[0]);
}
else
{
lines = System.IO.File.ReadAllLines("scripts/Player_kinematic.cs");
}
// otetaan funkkarinimet ylös jos sellaisia asetettu (nämä funkkarit muutetaan SomeFunc() -> some_func() )
foreach (string line in lines)
{
if (line.Contains("//@f "))
funcs.Add(line.Trim().Split(' ')[1]);
}
for (int linenum = 0; linenum < lines.Length; linenum++)
{
string line = lines[linenum];
if (line == "" || line.Contains("using ") || line.Contains("{"))
continue;
if (line.Contains("//@f "))
continue;
outstr += "\n";
string lvl = GetLevel(line);
if (line.Contains("}"))
continue;
if (line.Contains("/*"))
{
comment = true;
outstr += line.Replace("/*", "\"\"\"");
continue;
}
if (line.Contains("*/"))
{
comment = false;
outstr += line.Replace("*/", "\"\"\"");
continue;
}
if (comment)
{
outstr += line;
continue;
}
if (line.Contains("//"))
line = line.Replace("//", "#");
if (line.Contains("class "))
line = "extends " + line.Trim().Split(' ')[4];
if (writeOrig)
{
outstr += "\n" + lvl + "# " + line.Trim() + "\n";
}
foreach (string fn in funcs)
{
if (line.Contains(fn))
{
line = line.Replace(fn, ChangeFuncName(fn));
}
}
if (line.Contains("new "))
{
line = line.Replace("new ", "");
}
if (line.Contains("for(") || line.Contains("for ("))
{
line = "for _x in range(_y): #TODO fix this";
}
else
if (line.Contains("while(") || line.Contains("while ("))
{
line = line.Replace(")", "):");
}
else
if (line.Contains("if(") || line.Contains("if ("))
{
if (line.Contains(")))"))
line = line.Replace(")))", "))):");
else
if (line.Contains("))"))
line = line.Replace("))", ")):");
else
if (line.Contains(")"))
line = line.Replace(")", "):");
}
else
if (line.Contains("else"))
line = line.Replace("else", "else:");
else
if (line.Contains("(") && line.Contains(")")) // jos funkkari tai ehkä kutsu funkkariin?
{
int start = 0, end = line.IndexOf('(');
for (int qq = end; qq > 0; qq--)
{
// etitään funkkarinimi
if (line[qq] == '.' || line[qq] == ' ')
{
start = qq;
break;
}
}
string s = line.Substring(start + 1, end - start);
string newword = "";
// varattuja sanoja ei muokata
if (s == "Vector2(" || s == "Vector3(" ||
s == "Transform2D(" || s == "Transform(")
{
newword = s;
}
else
if (s.Contains("GetNode"))
{
newword = s.Replace("GetNode", "get_node");
int st = newword.IndexOf("<");
if (st > 0)
{
string neww = newword.Substring(0, st);
newword = neww + "(";
}
}
else
{
newword = ChangeFuncName(s);
}
line = line.Replace(s, newword);
if (lines[linenum + 1].Contains("{"))
{
line = line.Replace("int ", "");
line = line.Replace("float ", "");
line = line.Replace("bool ", "");
line = line.Replace("string ", "");
line = line.Replace("void ", "");
line = line.Replace(")", "):");
line = "func " + line.Trim();
firstFunc = true;
}
}
line = line.Replace("Math.", "");
line = line.Replace("public ", "");
line = line.Replace("private ", "");
line = line.Replace("override ", "");
line = line.Replace("int ", "var ");
line = line.Replace("float ", "var ");
line = line.Replace("bool ", "var ");
line = line.Replace("string ", "var ");
// ennen ekaa metodia, indent=0
if (firstFunc == false)
lvl = "";
if (line.Contains("const "))
{
line = line.Replace("var ", "").Trim();
outstr += lvl + "#TODO put every const to new line\n";
}
if (line.Contains("func "))
outstr += line.Trim();
else
outstr += lvl + line.Trim();
}
Console.WriteLine(outstr);
//Console.ReadLine();
}
static string GetLevel(string line)
{
string l = "";
for (int q = 0; q < line.Length; q++)
if (line[q] != ' ' && line[q] != '\t')
break;
else l += " ";
return l;
}
static string ChangeFuncName(string s)
{
string newword = "";
for (int i = 0; i < s.Length; i++)
{
if (i == 0 && s[i] == '_') continue;
if (s[i] >= 'A' && s[i] <= 'Z') // iso kirjain
{
// alaviiva tulee ennen isoja kirjaimia kunhan ei ole eka kirjain eikä edellinen kirjain ollut piste
if (i > 0 && s[i - 1] != '.')
newword += "_";
}
newword += s[i];
}
newword = newword.ToLower();
return newword;
}
}
}