Skip to content

Commit a683b13

Browse files
committed
Major refactor of Jass Decompiler for ObjectManager data (units, items, regions, cameras, sounds)
1 parent 6c74fc1 commit a683b13

22 files changed

Lines changed: 1713 additions & 1414 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ------------------------------------------------------------------------------
2+
// <copyright file="RegionExtensions.cs" company="Drake53">
3+
// Licensed under the MIT license.
4+
// See the LICENSE file in the project root for more information.
5+
// </copyright>
6+
// ------------------------------------------------------------------------------
7+
8+
using War3Net.Build.Audio;
9+
using War3Net.Build.Environment;
10+
11+
namespace War3Net.Build.Extensions
12+
{
13+
14+
public static class SoundExtensions
15+
{
16+
public static string GetVariableName(this Sound sound)
17+
{
18+
return $"gg_snd_{sound.Name.Replace(' ', '_')}";
19+
}
20+
}
21+
}

src/War3Net.Build/Extensions/UnitDataExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static class UnitDataExtensions
2525

2626
public static string GetVariableName(this UnitData unitData)
2727
{
28-
return $"gg_unit_{unitData.TypeId.ToRawcode()}_{unitData.CreationNumber:D4}";
28+
return $"{(unitData.IsItem() ? "gg_item_" : "gg_unit_")}{unitData.TypeId.ToRawcode()}_{unitData.CreationNumber:D4}";
2929
}
3030

3131
public static string GetDropItemsFunctionName(this UnitData unitData, int id)

src/War3Net.Build/War3Net.Build.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
<PackageReference Include="War3Net.CodeAnalysis.Transpilers" Version="$(War3NetCodeAnalysisTranspilersVersion)" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<ProjectReference Include="..\War3Net.CodeAnalysis.Decompilers\War3Net.CodeAnalysis.Decompilers.csproj" />
25+
</ItemGroup>
26+
2327
</Project>

src/War3Net.CodeAnalysis.Decompilers/Audio/MapSoundsDecompiler.cs

Lines changed: 326 additions & 335 deletions
Large diffs are not rendered by default.
Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,35 @@
11
// ------------------------------------------------------------------------------
2-
// <copyright file="DecompilationContext.cs" company="Drake53">
2+
//
33
// Licensed under the MIT license.
44
// See the LICENSE file in the project root for more information.
5-
// </copyright>
65
// ------------------------------------------------------------------------------
76

87
using System;
98
using System.Collections.Generic;
109
using System.Collections.Immutable;
10+
using System.Linq;
1111

12-
using War3Net.Build;
1312
using War3Net.Build.Info;
1413
using War3Net.Build.Script;
15-
using War3Net.CodeAnalysis.Jass;
1614
using War3Net.CodeAnalysis.Jass.Syntax;
1715

1816
namespace War3Net.CodeAnalysis.Decompilers
1917
{
20-
internal sealed class DecompilationContext
18+
public class DecompilationContext
2119
{
22-
public DecompilationContext(Map map, Campaign? campaign, TriggerData? triggerData)
20+
public DecompilationContext(JassCompilationUnitSyntax compilationUnit, DecompileOptions options = null, MapInfo mapInfo = null, TriggerData triggerData = null)
2321
{
24-
if (map is null)
25-
{
26-
throw new ArgumentNullException(nameof(map));
27-
}
28-
29-
if (map.Info is null)
30-
{
31-
throw new Exception();
32-
}
33-
34-
if (map.Info.ScriptLanguage != ScriptLanguage.Jass)
35-
{
36-
throw new Exception();
37-
}
22+
CompilationUnit = compilationUnit;
23+
Options = options ?? new DecompileOptions();
24+
MapInfo = mapInfo;
3825

39-
if (string.IsNullOrEmpty(map.Script))
40-
{
41-
throw new Exception();
42-
}
43-
44-
ObjectData = new ObjectDataContext(map, campaign);
4526
TriggerData = new TriggerDataContext(triggerData);
4627

47-
var compilationUnit = JassSyntaxFactory.ParseCompilationUnit(map.Script);
48-
4928
var comments = new List<JassCommentSyntax>();
5029
var functionDeclarationsBuilder = ImmutableDictionary.CreateBuilder<string, FunctionDeclarationContext>(StringComparer.Ordinal);
5130
var variableDeclarationsBuilder = ImmutableDictionary.CreateBuilder<string, VariableDeclarationContext>(StringComparer.Ordinal);
5231

53-
foreach (var declaration in compilationUnit.Declarations)
32+
foreach (var declaration in CompilationUnit.Declarations)
5433
{
5534
if (declaration is JassCommentSyntax comment)
5635
{
@@ -80,21 +59,91 @@ public DecompilationContext(Map map, Campaign? campaign, TriggerData? triggerDat
8059
FunctionDeclarations = functionDeclarationsBuilder.ToImmutable();
8160
VariableDeclarations = variableDeclarationsBuilder.ToImmutable();
8261

62+
8363
ImportedFileNames = new(StringComparer.OrdinalIgnoreCase);
64+
MaxPlayerSlots = mapInfo != null && mapInfo.EditorVersion >= EditorVersion.v6060 ? 24 : 12;
65+
}
66+
67+
public TriggerDataContext TriggerData { get; }
68+
public ImmutableDictionary<string, FunctionDeclarationContext> FunctionDeclarations { get; }
69+
public ImmutableDictionary<string, VariableDeclarationContext> VariableDeclarations { get; }
70+
public HashSet<string> ImportedFileNames { get; }
71+
public int MaxPlayerSlots { get; }
72+
73+
public HashSet<IStatementLineSyntax> HandledStatements = new HashSet<IStatementLineSyntax>();
74+
public JassCompilationUnitSyntax CompilationUnit { get; }
75+
public MapInfo MapInfo { get; }
76+
public DecompileOptions Options { get; }
8477

85-
MaxPlayerSlots = map.Info.EditorVersion >= EditorVersion.v6060 ? 24 : 12;
78+
private readonly Dictionary<string, object> _variableNameToValueMapping = new();
79+
private readonly List<object> _values = new();
80+
private int _lastCreationNumber;
81+
82+
public int GetNextCreationNumber()
83+
{
84+
return _lastCreationNumber++;
8685
}
8786

88-
public ObjectDataContext ObjectData { get; }
87+
public string GetVariableName(object value)
88+
{
89+
return _variableNameToValueMapping.FirstOrDefault(x => x.Value == value).Key;
90+
}
8991

90-
public TriggerDataContext TriggerData { get; }
92+
public void Add<T>(T value, string variableName = null) where T : class
93+
{
94+
if (variableName != null)
95+
{
96+
_variableNameToValueMapping[variableName] = value;
97+
}
9198

92-
public ImmutableDictionary<string, FunctionDeclarationContext> FunctionDeclarations { get; }
99+
_values.Add(value);
100+
}
93101

94-
public ImmutableDictionary<string, VariableDeclarationContext> VariableDeclarations { get; }
102+
public void Add_Struct<T>(T value, string variableName = null) where T : struct
103+
{
104+
Add(new Nullable_Class<T>(value), variableName);
105+
}
95106

96-
public HashSet<string> ImportedFileNames { get; }
107+
public T Get<T>(string variableName) where T : class
108+
{
109+
if (variableName == null)
110+
{
111+
return default;
112+
}
113+
114+
return _variableNameToValueMapping.GetValueOrDefault(variableName) as T;
115+
}
116+
117+
public Nullable_Class<T> Get_Struct<T>(string variableName = null) where T : struct
118+
{
119+
return Get<Nullable_Class<T>>(variableName);
120+
}
121+
122+
public T GetLastCreated<T>() where T : class
123+
{
124+
return _values.OfType<T>().LastOrDefault();
125+
}
126+
127+
public Nullable_Class<T> GetLastCreated_Struct<T>() where T : struct
128+
{
129+
return GetLastCreated<Nullable_Class<T>>();
130+
}
131+
132+
public IEnumerable<T> GetAll<T>() where T : class
133+
{
134+
return _values.OfType<T>();
135+
}
136+
137+
public IEnumerable<Nullable_Class<T>> GetAll_Struct<T>() where T : struct
138+
{
139+
return GetAll<Nullable_Class<T>>();
140+
}
141+
142+
private const string PSEUDO_VARIABLE_PREFIX = "##PSEUDO_VARIABLE_PREFIX##";
143+
internal string CreatePseudoVariableName(string type, string name = "")
144+
{
145+
return PSEUDO_VARIABLE_PREFIX + "_" + type.ToString() + "_" + name;
146+
}
97147

98-
public int MaxPlayerSlots { get; }
99148
}
100149
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ------------------------------------------------------------------------------
2+
//
3+
// Licensed under the MIT license.
4+
// See the LICENSE file in the project root for more information.
5+
//
6+
// ------------------------------------------------------------------------------
7+
8+
using War3Net.Build.Audio;
9+
using War3Net.Build.Environment;
10+
using War3Net.Build.Widget;
11+
12+
namespace War3Net.CodeAnalysis.Decompilers
13+
{
14+
public class DecompileOptions
15+
{
16+
public MapCamerasFormatVersion mapCamerasFormatVersion;
17+
public bool mapCamerasUseNewFormat;
18+
public MapRegionsFormatVersion mapRegionsFormatVersion;
19+
public MapSoundsFormatVersion mapSoundsFormatVersion;
20+
public MapWidgetsFormatVersion mapWidgetsFormatVersion;
21+
public MapWidgetsSubVersion mapWidgetsSubVersion;
22+
public bool mapWidgetsUseNewFormat = default;
23+
public SpecialDoodadVersion specialDoodadVersion;
24+
}
25+
}

0 commit comments

Comments
 (0)