Skip to content

Commit 85df552

Browse files
committed
Fix already guarded scope checking.
1 parent 4bc152b commit 85df552

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

ServerCodeExciser/ServerCodeExcisionProcessor.cs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
203203
{
204204
// We want to excise this entire file.
205205
serverCodeInjections.Add(new KeyValuePair<int, string>(0, excisionLanguage.ServerScopeStartString + "\r\n"));
206-
serverCodeInjections.Add(new KeyValuePair<int, string>(script.Length, excisionLanguage.ServerScopeEndString));
206+
serverCodeInjections.Add(new KeyValuePair<int, string>(script.Length, excisionLanguage.ServerScopeEndString + "\r\n"));
207207
stats.CharactersExcised += script.Length;
208208
}
209209
else if (excisionMode == EExcisionMode.AllFunctions)
@@ -241,17 +241,17 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
241241
{
242242
if (currentScope.StartIndex == -1
243243
|| currentScope.StopIndex == -1
244-
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StartIndex, true, excisionLanguage.ServerScopeStartString)
245-
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StartIndex, false, excisionLanguage.ServerScopeStartString)
246-
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StopIndex, false, excisionLanguage.ServerScopeEndString))
244+
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StartIndex, true, true, excisionLanguage.ServerScopeStartString)
245+
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StartIndex, false, false, excisionLanguage.ServerScopeStartString)
246+
|| InjectedMacroAlreadyExistsAtLocation(answerText, currentScope.StopIndex, false, false, excisionLanguage.ServerScopeEndString))
247247
{
248248
continue;
249249
}
250250

251251
// If there are already injected macros where we want to go, we should skip injecting.
252252
System.Diagnostics.Debug.Assert(currentScope.StopIndex > currentScope.StartIndex, "There must be some invalid pattern here! Stop is before start!");
253-
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StartIndex, excisionLanguage.ServerScopeStartString));
254-
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StopIndex, currentScope.Opt_ElseContent + excisionLanguage.ServerScopeEndString));
253+
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StartIndex, "\r\n" + excisionLanguage.ServerScopeStartString));
254+
serverCodeInjections.Add(new KeyValuePair<int, string>(currentScope.StopIndex, currentScope.Opt_ElseContent + excisionLanguage.ServerScopeEndString + "\r\n"));
255255
stats.CharactersExcised += currentScope.StopIndex - currentScope.StartIndex;
256256
}
257257

@@ -266,10 +266,10 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
266266
dummyRefDataBlockString.Append("\r\n\t" + dummyVarDef);
267267
}
268268

269-
dummyRefDataBlockString.Append("\r\n" + excisionLanguage.ServerScopeEndString + "\r\n");
269+
dummyRefDataBlockString.Append("\r\n" + excisionLanguage.ServerScopeEndString + "\r\n\r\n");
270270

271271
// If there is already a block of dummy reference variables we skip adding new ones, there is no guarantee we are adding the right code.
272-
if (InjectedMacroAlreadyExistsAtLocation(answerText, dummyRefDataPair.Key, false, dummyVarScope + "\r\n"))
272+
if (InjectedMacroAlreadyExistsAtLocation(answerText, dummyRefDataPair.Key, false, true, dummyVarScope + "\r\n"))
273273
{
274274
continue;
275275
}
@@ -323,19 +323,47 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
323323
return stats;
324324
}
325325

326-
private bool InjectedMacroAlreadyExistsAtLocation(StringBuilder script, int index, bool lookAhead, string macro)
326+
private static bool IsWhitespace(char c)
327327
{
328-
int startIndex = lookAhead ? index : (index - macro.Length);
329-
int endIndex = lookAhead ? (index + macro.Length) : index;
328+
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
329+
}
330330

331-
if (startIndex < 0 || startIndex >= script.Length
332-
|| endIndex < 0 || endIndex >= script.Length)
331+
private bool InjectedMacroAlreadyExistsAtLocation(StringBuilder script, int index, bool lookAhead, bool ignoreWhitespace, string macro)
332+
{
333+
if (lookAhead)
333334
{
334-
return false;
335+
if (ignoreWhitespace)
336+
{
337+
while (index < script.Length && IsWhitespace(script[index]))
338+
{
339+
index++;
340+
}
341+
}
342+
343+
if (script.Length - index < macro.Length)
344+
{
345+
return false;
346+
}
347+
348+
return script.ToString(index, macro.Length).Equals(macro);
335349
}
350+
else
351+
{
352+
if (ignoreWhitespace)
353+
{
354+
while (index > 0 && IsWhitespace(script[index]))
355+
{
356+
index--;
357+
}
358+
}
336359

337-
string scriptSection = script.ToString(startIndex, macro.Length);
338-
return scriptSection == macro;
360+
if (index - macro.Length < 0)
361+
{
362+
return false;
363+
}
364+
365+
return script.ToString(index - macro.Length, macro.Length).Equals(macro);
366+
}
339367
}
340368
}
341369
}

ServerCodeExciserTest/ExcisionIntegrationTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public static int Main(string[] args)
2525
ref numTestFailures,
2626
ref numTestCases);
2727

28-
Console.WriteLine("----------------------------");
29-
Console.WriteLine($"{numTestCases} *.as test(s) executed.");
30-
Console.WriteLine($"{numTestFailures} *.as test(s) failed.");
31-
3228
// Run for "common"
3329
var commonResult = RunExciserIntegrationTests(
3430
".common",
@@ -38,8 +34,8 @@ public static int Main(string[] args)
3834
ref numTestCases);
3935

4036
Console.WriteLine("----------------------------");
41-
Console.WriteLine($"{numTestCases} *.common test(s) executed.");
42-
Console.WriteLine($"{numTestFailures} *.common test(s) failed.");
37+
Console.WriteLine($"{numTestCases} test(s) executed.");
38+
Console.WriteLine($"{numTestFailures} test(s) failed.");
4339
}
4440
catch (Exception e)
4541
{
@@ -58,7 +54,7 @@ private static EExciserReturnValues RunExciserIntegrationTests(string fileExtens
5854
Directory.Delete(outputPath, true);
5955
}
6056

61-
string searchPattern = "*" + fileExtension.TrimStart('.');
57+
string searchPattern = "*." + fileExtension.TrimStart('.');
6258
Console.WriteLine($"Running integration tests for {searchPattern} files...");
6359

6460
EExciserReturnValues returnCode;

ServerCodeExciserTest/Problems/Common/FullExcise1/FullExcise1Test.common.solution

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#ifdef WITH_SERVER
32
class UFullExcise1Test
43
{

ServerCodeExcisionCommon/ServerCodeExcisionUtils.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,6 @@ public static int FindScriptIndexForCodePoint(string script, int line, int colum
146146
return (linesTraversed == line) ? (cursor + column) : -1;
147147
}
148148

149-
public static int ShrinkServerScope(string script, int start, int end)
150-
{
151-
bool skip = true;
152-
while (skip)
153-
{
154-
skip = false;
155-
156-
int search = script.IndexOfAny(NewLineChars, start) + 2;
157-
if ((search < end) && SkippableScopeChars.Contains<char>(script.ElementAt(search)))
158-
{
159-
skip = true;
160-
++start;
161-
}
162-
}
163-
164-
return start;
165-
}
166-
167149
public static Type FindFirstDirectChildOfType<Type>(Antlr4.Runtime.Tree.IParseTree currentContext)
168150
where Type : class
169151
{

UnrealAngelscriptServerCodeExcision/UnrealAngelscriptServerCodeExcisionLanguage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class UnrealAngelscriptServerCodeExcisionLanguage : IServerCodeExcisionLa
2424

2525
public string ServerPrecompilerSymbol { get { return "WITH_SERVER"; } }
2626

27-
public string ServerScopeStartString { get { return "\r\n#ifdef " + ServerPrecompilerSymbol; } }
27+
public string ServerScopeStartString { get { return "#ifdef " + ServerPrecompilerSymbol; } }
2828

29-
public string ServerScopeEndString { get { return "#endif // " + ServerPrecompilerSymbol + "\r\n"; } }
29+
public string ServerScopeEndString { get { return "#endif // " + ServerPrecompilerSymbol; } }
3030

3131
public T CreateLexer<T>(Antlr4.Runtime.AntlrInputStream inputStream)
3232
where T : Antlr4.Runtime.Lexer

UnrealAngelscriptServerCodeExcision/UnrealAngelscriptSymbolVisitor.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ public override UnrealAngelscriptNode VisitPostfixExpression(UnrealAngelscriptPa
262262
ExcisionUtils.FindScriptIndexForCodePoint(Script, simpleDeclaration.Stop.Line, simpleDeclaration.Stop.Column) + 1,
263263
ExcisionUtils.FindScriptIndexForCodePoint(Script, parentScope.Stop.Line, 0));
264264

265-
// We need to correct the start index to skip all the possible empty characters/new lines,
266-
// if not we can miss the detection of a manually placed #ifdef
267-
//newData.StartIndex = ExcisionUtils.ShrinkServerScope(Script, newData.StartIndex, newData.StopIndex);
268-
269265
if (returnData.ReturnType != EReturnType.NoReturn)
270266
{
271267
string scopeIndentation = BuildIndentationForColumnCount(simpleDeclaration.Start.Column);

0 commit comments

Comments
 (0)