diff --git a/LibBSP/Source/Structs/Common/Entity.cs b/LibBSP/Source/Structs/Common/Entity.cs index 8f03906..7d2455f 100644 --- a/LibBSP/Source/Structs/Common/Entity.cs +++ b/LibBSP/Source/Structs/Common/Entity.cs @@ -197,12 +197,22 @@ namespace LibBSP { foreach (string line in lines) { string current = line.Trim(' ', '\t', '\r'); - // Cull everything after a // + // Cull everything after a "//" bool inQuotes = false; - for (int i = 0; i < current.Length; ++i) { - if (current[i] == '\"' && (i == 0 || current[i - 1] != '\\')) { - inQuotes = !inQuotes; + + if (current[i] == '\"') { + if (i == 0) { + inQuotes = !inQuotes; + } else if (current[i - 1] != '\\') { + // Allow for escape-sequenced quotes to not affect the state machine, but only if the quote isn't at the end of a line. + // Some Source engine entities use escape sequence quotes in values, but MoHAA has a map with an obvious erroneous backslash before a quote at the end of a line. + if (inQuotes && (i + 1 >= current.Length || current[i + 1] == '\n' || current[i + 1] == '\r')) { + inQuotes = false; + } + } else { + inQuotes = !inQuotes; + } } if (!inQuotes && current[i] == '/' && i != 0 && current[i - 1] == '/') { diff --git a/LibBSP/Source/Structs/Common/Lumps/Entities.cs b/LibBSP/Source/Structs/Common/Lumps/Entities.cs index e03ffee..68d2b8f 100644 --- a/LibBSP/Source/Structs/Common/Lumps/Entities.cs +++ b/LibBSP/Source/Structs/Common/Lumps/Entities.cs @@ -54,9 +54,18 @@ namespace LibBSP { for (int offset = 0; offset < data.Length; ++offset) { currentChar = (char)data[offset]; - // Allow for escape-sequenced quotes to not affect the state machine. - if (currentChar == '\"' && (offset == 0 || (char)data[offset - 1] != '\\')) { - inQuotes = !inQuotes; + if (currentChar == '\"') { + if (offset == 0) { + inQuotes = !inQuotes; + } else if ((char)data[offset - 1] != '\\') { + // Allow for escape-sequenced quotes to not affect the state machine, but only if the quote isn't at the end of a line. + // Some Source engine entities use escape sequence quotes in values, but MoHAA has a map with an obvious erroneous backslash before a quote at the end of a line. + if (inQuotes && (offset + 1 >= data.Length || (char)data[offset + 1] == '\n' || (char)data[offset + 1] == '\r')) { + inQuotes = false; + } + } else { + inQuotes = !inQuotes; + } } if (!inQuotes) { @@ -89,7 +98,7 @@ namespace LibBSP { } if (braceCount != 0) { - throw new ArgumentException(string.Format("Brace mismatch when parsing entities! Brace level: {0}", braceCount)); + throw new ArgumentException(string.Format("Brace mismatch when parsing entities! Entity: {0} Brace level: {1}", Count, braceCount)); } }