@@ -30,7 +30,9 @@ namespace ClientUpdater;
30
30
using System . Text ;
31
31
using System . Threading ;
32
32
using System . Threading . Tasks ;
33
+
33
34
using ClientUpdater . Compression ;
35
+
34
36
using Rampastring . Tools ;
35
37
36
38
public static class Updater
@@ -429,6 +431,8 @@ internal static void DeleteFileAndWait(string filepath, int timeout = 10000)
429
431
{
430
432
mre . Set ( ) ;
431
433
} ;
434
+ if ( fileInfo . Exists )
435
+ fileInfo . IsReadOnly = false ;
432
436
fileInfo . Delete ( ) ;
433
437
mre . Wait ( timeout ) ;
434
438
}
@@ -613,15 +617,15 @@ private static async Task DoVersionCheckAsync()
613
617
614
618
UpdateUserAgent ( SharedHttpClient ) ;
615
619
616
- FileInfo downloadFile = SafePath . GetFile ( GamePath , FormattableString . Invariant ( $ "{ VERSION_FILE } _u") ) ;
620
+ FileInfo versionFile = SafePath . GetFile ( GamePath , FormattableString . Invariant ( $ "{ VERSION_FILE } _u") ) ;
617
621
618
622
while ( currentUpdateMirrorIndex < updateMirrors . Count )
619
623
{
620
624
try
621
625
{
622
626
Logger . Log ( "Updater: Trying to connect to update mirror " + updateMirrors [ currentUpdateMirrorIndex ] . URL ) ;
623
627
624
- FileStream fileStream = new FileStream ( downloadFile . FullName , FileMode . Create , FileAccess . Write , FileShare . None , 4096 , FileOptions . Asynchronous ) ;
628
+ FileStream fileStream = new FileStream ( versionFile . FullName , FileMode . Create , FileAccess . Write , FileShare . None , 4096 , FileOptions . Asynchronous ) ;
625
629
626
630
using ( fileStream )
627
631
{
@@ -650,7 +654,7 @@ private static async Task DoVersionCheckAsync()
650
654
}
651
655
652
656
Logger . Log ( "Updater: Downloaded version information." ) ;
653
- var version = new IniFile ( downloadFile . FullName ) ;
657
+ var version = new IniFile ( versionFile . FullName ) ;
654
658
string versionString = version . GetStringValue ( "DTA" , "Version" , string . Empty ) ;
655
659
string updaterVersionString = version . GetStringValue ( "DTA" , "UpdaterVersion" , "N/A" ) ;
656
660
string manualDownloadURLString = version . GetStringValue ( "DTA" , "ManualDownloadURL" , string . Empty ) ;
@@ -722,7 +726,7 @@ private static async Task DoVersionCheckAsync()
722
726
if ( versionString == GameVersion )
723
727
{
724
728
VersionState = VersionState . UPTODATE ;
725
- downloadFile . Delete ( ) ;
729
+ versionFile . Delete ( ) ;
726
730
DoFileIdentifiersUpdatedEvent ( ) ;
727
731
728
732
if ( AreCustomComponentsOutdated ( ) )
@@ -736,7 +740,7 @@ private static async Task DoVersionCheckAsync()
736
740
VersionState = VersionState . OUTDATED ;
737
741
ManualUpdateRequired = true ;
738
742
ManualDownloadURL = manualDownloadURLString ;
739
- downloadFile . Delete ( ) ;
743
+ versionFile . Delete ( ) ;
740
744
DoFileIdentifiersUpdatedEvent ( ) ;
741
745
}
742
746
else
@@ -779,7 +783,7 @@ private static async ValueTask ExecuteAfterUpdateScriptAsync()
779
783
try
780
784
{
781
785
string downloadFile = SafePath . CombineFilePath ( GamePath , "updateexec" ) ;
782
-
786
+
783
787
FileStream fileStream = new FileStream ( downloadFile , FileMode . Create , FileAccess . Write , FileShare . None , 4096 , FileOptions . Asynchronous ) ;
784
788
785
789
using ( fileStream )
@@ -811,7 +815,7 @@ private static async ValueTask<bool> ExecutePreUpdateScriptAsync()
811
815
try
812
816
{
813
817
string downloadFile = SafePath . CombineFilePath ( GamePath , "preupdateexec" ) ;
814
-
818
+
815
819
FileStream fileStream = new FileStream ( downloadFile , FileMode . Create , FileAccess . Write , FileShare . None , 4096 , FileOptions . Asynchronous ) ;
816
820
817
821
using ( fileStream )
@@ -851,7 +855,13 @@ private static void ExecuteScript(string fileName)
851
855
852
856
try
853
857
{
854
- SafePath . DeleteFileIfExists ( GamePath , key ) ;
858
+ FileInfo fileInfo = SafePath . GetFile ( GamePath , key ) ;
859
+
860
+ if ( fileInfo . Exists )
861
+ {
862
+ fileInfo . IsReadOnly = false ;
863
+ fileInfo . Delete ( ) ;
864
+ }
855
865
}
856
866
catch ( Exception ex )
857
867
{
@@ -869,10 +879,30 @@ private static void ExecuteScript(string fileName)
869
879
{
870
880
Logger . Log ( "Updater: " + fileName + ": Renaming file '" + key + "' to '" + newFilename + "'" ) ;
871
881
872
- FileInfo file = SafePath . GetFile ( GamePath , key ) ;
882
+ FileInfo srcFile = SafePath . GetFile ( GamePath , key ) ;
883
+
884
+ if ( srcFile . Exists )
885
+ {
886
+ bool isSrcReadOnly = srcFile . IsReadOnly ;
887
+ srcFile . IsReadOnly = false ;
888
+
889
+ {
890
+ FileInfo destFile = SafePath . GetFile ( GamePath , newFilename ) ;
891
+ if ( destFile . Exists )
892
+ {
893
+ destFile . IsReadOnly = false ;
894
+ destFile . Delete ( ) ;
895
+ }
896
+ }
897
+
898
+ srcFile . MoveTo ( SafePath . CombineFilePath ( GamePath , newFilename ) ) ;
873
899
874
- if ( file . Exists )
875
- file . MoveTo ( SafePath . CombineFilePath ( GamePath , newFilename ) ) ;
900
+ if ( isSrcReadOnly )
901
+ {
902
+ FileInfo destFile = SafePath . GetFile ( GamePath , newFilename ) ;
903
+ destFile . IsReadOnly = true ;
904
+ }
905
+ }
876
906
}
877
907
catch ( Exception ex )
878
908
{
@@ -890,10 +920,10 @@ private static void ExecuteScript(string fileName)
890
920
{
891
921
Logger . Log ( "Updater: " + fileName + ": Renaming directory '" + key + "' to '" + newDirectoryName + "'" ) ;
892
922
893
- DirectoryInfo directory = SafePath . GetDirectory ( GamePath , key ) ;
923
+ DirectoryInfo srcDirectory = SafePath . GetDirectory ( GamePath , key ) ;
894
924
895
- if ( directory . Exists )
896
- directory . MoveTo ( SafePath . CombineDirectoryPath ( GamePath , newDirectoryName ) ) ;
925
+ if ( srcDirectory . Exists )
926
+ srcDirectory . MoveTo ( SafePath . CombineDirectoryPath ( GamePath , newDirectoryName ) ) ;
897
927
}
898
928
catch ( Exception ex )
899
929
{
@@ -928,18 +958,29 @@ private static void ExecuteScript(string fileName)
928
958
FileInfo [ ] files = gameDirectory . GetFiles ( ) ;
929
959
foreach ( FileInfo file in files )
930
960
{
961
+ bool isSrcReadOnly = file . IsReadOnly ;
962
+ file . IsReadOnly = false ;
963
+
931
964
FileInfo fileToMergeInto = SafePath . GetFile ( directoryToMergeInto . FullName , file . Name ) ;
932
965
if ( fileToMergeInto . Exists )
933
966
{
934
967
Logger . Log ( "Updater: " + fileName + ": Destination file '" + directoryNameToMergeInto + "/" + file . Name +
935
968
"' exists, removing original source file " + directoryName + "/" + file . Name ) ;
936
- fileToMergeInto . Delete ( ) ;
969
+
970
+ // Note: Previously, the incorrect file was deleted as of commit fc939a06ff978b51daa6563eaa15a28cf48319ec.
971
+
972
+ // Remove the original source file
973
+ file . Delete ( ) ;
937
974
}
938
975
else
939
976
{
940
977
Logger . Log ( "Updater: " + fileName + ": Destination file '" + directoryNameToMergeInto + "/" + file . Name +
941
978
"' does not exist, moving original source file " + directoryName + "/" + file . Name ) ;
942
979
file . MoveTo ( fileToMergeInto . FullName ) ;
980
+
981
+ // Resume the read-only property
982
+ fileToMergeInto . Refresh ( ) ;
983
+ fileToMergeInto . IsReadOnly = isSrcReadOnly ;
943
984
}
944
985
}
945
986
}
@@ -959,7 +1000,17 @@ private static void ExecuteScript(string fileName)
959
1000
{
960
1001
Logger . Log ( "Updater: " + fileName + ": Deleting directory '" + key + "'" ) ;
961
1002
962
- SafePath . DeleteDirectoryIfExists ( true , GamePath , key ) ;
1003
+ DirectoryInfo directoryInfo = SafePath . GetDirectory ( GamePath , key ) ;
1004
+ if ( directoryInfo . Exists )
1005
+ {
1006
+ // Unset read-only attribute from all files in the directory.
1007
+ foreach ( FileInfo file in directoryInfo . GetFiles ( "*" , SearchOption . AllDirectories ) )
1008
+ {
1009
+ file . IsReadOnly = false ;
1010
+ }
1011
+
1012
+ directoryInfo . Delete ( true ) ;
1013
+ }
963
1014
}
964
1015
catch ( Exception ex )
965
1016
{
0 commit comments