11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Linq ;
45using Microsoft . Build . Framework ;
56using Microsoft . Build . Utilities ;
67
7- namespace UnityModStudio . Build . Tasks
8+ namespace UnityModStudio . Build . Tasks ;
9+
10+ public class ConfigureDoorstop : Task
811{
9- public class ConfigureDoorstop : Task
12+ [ Required ]
13+ public ITaskItem ? ConfigPath { get ; set ; }
14+
15+ public ITaskItem ? TargetAssemblyPath { get ; set ; }
16+
17+ public bool UseForModLoading { get ; set ; }
18+
19+ public override bool Execute ( )
1020 {
11- [ Required ]
12- public ITaskItem ? ConfigPath { get ; set ; }
21+ var configPath = ConfigPath ! . GetMetadata ( "FullPath" ) ;
22+ if ( ! File . Exists ( configPath ) )
23+ {
24+ Log . LogError ( "Unity Doorstop config file does not exist." ) ;
25+ return false ;
26+ }
1327
14- [ Required ]
15- public ITaskItem ? TargetAssemblyPath { get ; set ; }
28+ var values = new List < ( string , string ) >
29+ {
30+ ( "enabled" , "true" )
31+ } ;
1632
17- public override bool Execute ( )
33+ if ( UseForModLoading )
1834 {
19- var configPath = ConfigPath ! . GetMetadata ( "FullPath" ) ;
20- if ( ! File . Exists ( configPath ) )
21- {
22- Log . LogError ( "Unity Doorstop config file does not exist." ) ;
23- return false ;
24- }
25- var targetAssemblyPath = TargetAssemblyPath ! . GetMetadata ( "FullPath" ) ;
35+ var targetAssemblyPath = TargetAssemblyPath ? . GetMetadata ( "FullPath" ) ;
2636 if ( ! File . Exists ( targetAssemblyPath ) )
2737 {
2838 Log . LogError ( "Target assembly file does not exist." ) ;
2939 return false ;
3040 }
41+ values . Add ( ( "target_assembly" , GetRelativePath ( targetAssemblyPath ! , configPath ) ) ) ;
42+ }
3143
32- SetIniValues ( configPath , "General" ,
33- ( "enabled" , "true" ) ,
34- ( "target_assembly" , GetRelativePath ( targetAssemblyPath , configPath ) ) ) ;
44+ SetIniValues ( configPath , "General" , values ) ;
3545
36- return true ;
37- }
46+ return true ;
47+ }
3848
39- private static string GetRelativePath ( string path , string relativeToPath ) =>
40- new Uri ( Path . GetFullPath ( relativeToPath ) )
41- . MakeRelativeUri ( new Uri ( Path . GetFullPath ( path ) ) ) . ToString ( )
42- . Replace ( '/' , Path . DirectorySeparatorChar ) ;
49+ private static string GetRelativePath ( string path , string relativeToPath ) =>
50+ new Uri ( Path . GetFullPath ( relativeToPath ) )
51+ . MakeRelativeUri ( new Uri ( Path . GetFullPath ( path ) ) ) . ToString ( )
52+ . Replace ( '/' , Path . DirectorySeparatorChar ) ;
4353
44- private static readonly char [ ] IniEntrySeparator = { '=' } ;
45- private static readonly char [ ] IniCommentSeparator = { '#' } ;
54+ private static readonly char [ ] IniEntrySeparator = [ '=' ] ;
55+ private static readonly char [ ] IniCommentSeparator = [ '#' ] ;
4656
47- private static void SetIniValues ( string iniFilePath , string section , params ( string key , string value ) [ ] values )
48- {
49- var lines = File . ReadAllLines ( iniFilePath ) ;
50- var linesInSection = lines
51- . Select ( ( line , index ) => ( text : line . TrimStart ( ) , index ) )
52- . SkipWhile ( line => ! line . text . StartsWith ( $ "[{ section } ]") )
53- . Skip ( 1 )
54- . TakeWhile ( line => ! line . text . StartsWith ( "[" ) ) ;
55- var linesToChange =
56- from line in linesInSection
57- where ! line . text . StartsWith ( "#" )
58- let parts = line . text . Split ( IniEntrySeparator , 2 )
59- where parts . Length > 1
60- let comment = parts [ 1 ] . Split ( IniCommentSeparator , 2 ) . ElementAtOrDefault ( 1 )
61- join kv in values on parts [ 0 ] equals kv . key
62- select ( line . index , kv . key , kv . value , comment ) ;
63- foreach ( var ( index , key , value , comment ) in linesToChange )
64- lines [ index ] = $ "{ key } ={ value } " + ( comment != null ? $ " # { comment } " : "" ) ;
65- File . WriteAllLines ( iniFilePath , lines ) ;
66- }
57+ private static void SetIniValues ( string iniFilePath , string section , IEnumerable < ( string key , string value ) > values )
58+ {
59+ var lines = File . ReadAllLines ( iniFilePath ) ;
60+ var linesInSection = lines
61+ . Select ( ( line , index ) => ( text : line . TrimStart ( ) , index ) )
62+ . SkipWhile ( line => ! line . text . StartsWith ( $ "[{ section } ]") )
63+ . Skip ( 1 )
64+ . TakeWhile ( line => ! line . text . StartsWith ( "[" ) ) ;
65+ var linesToChange =
66+ from line in linesInSection
67+ where ! line . text . StartsWith ( "#" )
68+ let parts = line . text . Split ( IniEntrySeparator , 2 )
69+ where parts . Length > 1
70+ let comment = parts [ 1 ] . Split ( IniCommentSeparator , 2 ) . ElementAtOrDefault ( 1 )
71+ join kv in values on parts [ 0 ] equals kv . key
72+ select ( line . index , kv . key , kv . value , comment ) ;
73+ foreach ( var ( index , key , value , comment ) in linesToChange )
74+ lines [ index ] = $ "{ key } ={ value } " + ( comment != null ? $ " # { comment } " : "" ) ;
75+ File . WriteAllLines ( iniFilePath , lines ) ;
6776 }
6877}
0 commit comments