1- using System ;
1+ using Microsoft . Win32 ;
2+ using System ;
23using System . Collections . Generic ;
34using System . Diagnostics ;
45using System . Globalization ;
@@ -44,10 +45,30 @@ public void Install() {
4445 Console . WriteLine ( ) ;
4546
4647 // Sneaky step: Copy the installer.
48+ string installerPath = null ;
4749 try {
48- string installerPath = Assembly . GetEntryAssembly ( ) . Location ;
49- File . Copy ( installerPath , Path . Combine ( Info . CurrentGamePath , Path . GetFileName ( installerPath ) ) , true ) ;
50+ string installerTmpPath = Assembly . GetEntryAssembly ( ) . Location ;
51+ installerPath = Path . Combine ( Info . CurrentGamePath , Path . GetFileName ( installerTmpPath ) ) ;
52+ File . Copy ( installerTmpPath , installerPath , true ) ;
5053 } catch {
54+ installerPath = null ;
55+ }
56+ // Sneaky step: Set up URI handler.
57+ if ( ! string . IsNullOrEmpty ( Info . ModURIProtocol ) &&
58+ ! string . IsNullOrEmpty ( Info . ModsDir ) ) {
59+ try {
60+ RegistryKey regClasses = Registry
61+ . CurrentUser
62+ ? . OpenSubKey ( "Software" , true )
63+ ? . OpenSubKey ( "Classes" , true ) ;
64+ RegistryKey regProtocol = regClasses ? . CreateSubKey ( Info . ModURIProtocol ) ;
65+ if ( regProtocol != null ) {
66+ regProtocol . SetValue ( "" , $ "URL:{ Info . ModURIProtocol } ") ;
67+ regProtocol . SetValue ( "URL Protocol" , "" ) ;
68+ regProtocol . CreateSubKey ( @"shell\open\command" ) . SetValue ( "" , $ "\" { installerPath } \" --uri %1") ;
69+ }
70+ } catch {
71+ }
5172 }
5273
5374 _Install ( ) ;
@@ -72,6 +93,19 @@ public void Uninstall() {
7293 OnStart ? . Invoke ( ) ;
7394 try {
7495
96+ // Sneaky step: Remove URI handler.
97+ if ( ! string . IsNullOrEmpty ( Info . ModURIProtocol ) &&
98+ ! string . IsNullOrEmpty ( Info . ModsDir ) ) {
99+ try {
100+ RegistryKey regClasses = Registry
101+ . CurrentUser
102+ ? . OpenSubKey ( "Software" , true )
103+ ? . OpenSubKey ( "Classes" , true ) ;
104+ regClasses ? . DeleteSubKey ( Info . ModURIProtocol , false ) ;
105+ } catch {
106+ }
107+ }
108+
75109 _Restore ( ) ;
76110
77111 } catch ( Exception e ) {
@@ -86,6 +120,45 @@ public void Uninstall() {
86120 OnFinish ? . Invoke ( ) ;
87121 }
88122
123+ public void DownloadMod ( string url ) {
124+ Console . WriteLine ( "Starting download" ) ;
125+
126+ Uri uri = new Uri ( url ) ;
127+
128+ string modRoot = Info . ModsDir ;
129+ if ( ! Directory . Exists ( modRoot ) )
130+ modRoot = Path . Combine ( Info . CurrentGamePath , modRoot ) ;
131+ if ( ! Directory . Exists ( modRoot ) )
132+ Directory . CreateDirectory ( modRoot ) ;
133+
134+ string modPath = Path . Combine ( modRoot , Path . GetFileName ( uri . AbsolutePath ) ) ;
135+ if ( File . Exists ( modPath ) )
136+ File . Delete ( modPath ) ;
137+
138+ Console . WriteLine ( $ "Downloading: { url } ") ;
139+
140+ OnStart ? . Invoke ( ) ;
141+ try {
142+
143+ byte [ ] zipData = _Download ( url ) ;
144+
145+ Console . WriteLine ( "Writing data to file" ) ;
146+ File . WriteAllBytes ( modPath , zipData ) ;
147+
148+ } catch ( Exception e ) {
149+ Console . WriteLine ( $ "Failed downloading { url } ") ;
150+ Console . WriteLine ( e ) ;
151+ Console . WriteLine ( "Error! Please check installer-log.txt" ) ;
152+ OnError ? . Invoke ( e ) ;
153+ if ( Debugger . IsAttached )
154+ throw ;
155+ return ;
156+ }
157+ Console . WriteLine ( $ "{ Path . GetFileName ( modPath ) } downloaded!") ;
158+ Console . WriteLine ( ) ;
159+ OnFinish ? . Invoke ( ) ;
160+ }
161+
89162 private void _DownloadAndUnpack ( ) {
90163 string root = Info . CurrentGamePath ;
91164 Console . WriteLine ( $ "STEP: DOWNLOAD & UNPACK") ;
0 commit comments