Repository navigation
Expand file tree
/
Copy pathFileFixer.cs
More file actions
121 lines (105 loc) · 4.27 KB
/
Copy pathFileFixer.cs
File metadata and controls
121 lines (105 loc) · 4.27 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
using System;
using System.Collections.Generic;
using System.IO;
namespace MassiveAssaultScnFixer
{
public class FileFixer
{
private readonly string _scnFilePath;
private readonly string _mscFilePath;
private bool _lastScnLineWasEmpty = false;
private readonly Dictionary<int, int> _unitIdMapping = new Dictionary<int, int>();
private int _alienUnitCounter = 1;
private int _humanUnitCounter = 4001;
public FileFixer(string file)
{
var directory = Path.GetDirectoryName(file)
?? throw new ArgumentException("File is not exists", nameof(file));
_scnFilePath = file;
_mscFilePath = Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(file)}.msc");
}
public void Fix()
{
if (!File.Exists(_mscFilePath))
{
Console.WriteLine($"{_scnFilePath} is a NET scenario. Skipped.");
return;
}
Console.WriteLine($"Fixing: {_scnFilePath}");
FixFile(_scnFilePath, ProcessScnLine);
Console.WriteLine($"Fixing: {_mscFilePath}");
FixFile(_mscFilePath, ProcessMscLine);
}
private void FixFile(string originalPath, Func<string?, string?> processingFunc)
{
var newScnFilePath = $"{originalPath}.new";
using (var newScnFile = File.CreateText(newScnFilePath))
using (var originalFile = File.OpenText(originalPath))
{
for (string? originalLine = originalFile.ReadLine(); originalLine != null;
originalLine = originalFile.ReadLine())
{
var fixedLine = processingFunc(originalLine);
if (fixedLine != null)
newScnFile.WriteLine(fixedLine);
}
}
File.Replace(newScnFilePath, originalPath, null);
}
private string? ProcessScnLine(string? originalLine)
{
if (originalLine == null
|| string.IsNullOrEmpty(originalLine) && _lastScnLineWasEmpty
|| originalLine.StartsWith(';'))
return null;
if (originalLine == string.Empty)
{
_lastScnLineWasEmpty = true;
return string.Empty;
}
_lastScnLineWasEmpty = false;
var tokens = originalLine.Split(',');
if (tokens[0].EndsWith("UNIT"))
{
var originalUnitId = int.Parse(tokens[1]);
if (_unitIdMapping.ContainsKey(originalUnitId))
tokens[1] = _unitIdMapping[originalUnitId].ToString();
else
{
var isHumanUnit = tokens[3].Contains("HUMANS");
if (isHumanUnit)
UpdateUnitId(tokens, originalUnitId, ref _humanUnitCounter);
else
UpdateUnitId(tokens, originalUnitId, ref _alienUnitCounter);
}
return string.Join(',', tokens);
}
return originalLine;
}
private void UpdateUnitId(string[] tokens, int originalUnitId, ref int unitCounter)
{
_unitIdMapping[originalUnitId] = unitCounter;
tokens[1] = unitCounter.ToString();
++unitCounter;
}
private string? ProcessMscLine(string? originalLine)
{
if (originalLine != null && (originalLine.StartsWith("SELECT_UNIT2") || originalLine.StartsWith("UNSELECT_UNIT2")))
{
var tokens = originalLine.Split(' ');
var unitId = int.Parse(tokens[1]);
//fix for convoy scenario (there is typo in the Id)
if (unitId == 41050)
unitId = 4105;
if (!_unitIdMapping.ContainsKey(unitId))
{
Console.WriteLine($"Unit with id {unitId} is not mapped!");
return null;
}
tokens[1] = _unitIdMapping[unitId].ToString();
return string.Join(' ', tokens);
}
return originalLine;
}
}
}