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
87using System ;
98using System . Collections . Generic ;
109using System . Collections . Immutable ;
10+ using System . Linq ;
1111
12- using War3Net . Build ;
1312using War3Net . Build . Info ;
1413using War3Net . Build . Script ;
15- using War3Net . CodeAnalysis . Jass ;
1614using War3Net . CodeAnalysis . Jass . Syntax ;
1715
1816namespace 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}
0 commit comments