diff --git a/Torch.Server/Program.cs b/Torch.Server/Program.cs index 05d744e..274371e 100644 --- a/Torch.Server/Program.cs +++ b/Torch.Server/Program.cs @@ -92,29 +92,49 @@ namespace Torch.Server private static void CopyNative(string binPath) { - var apiSource = Path.Combine(binPath, "steam_api64.dll"); - var apiTarget = Path.Combine(AppContext.BaseDirectory, "steam_api64.dll"); - if (!File.Exists(apiTarget)) - { - File.Copy(apiSource, apiTarget); - } - else if (File.GetLastWriteTime(apiTarget) < File.GetLastWriteTime(binPath)) - { - File.Delete(apiTarget); - File.Copy(apiSource, apiTarget); - } + var log = LogManager.GetLogger("TorchLauncher"); - var havokSource = Path.Combine(binPath, "Havok.dll"); - var havokTarget = Path.Combine(AppContext.BaseDirectory, "Havok.dll"); - - if (!File.Exists(havokTarget)) + var workingDir = new DirectoryInfo(Directory.GetCurrentDirectory()); + if (workingDir.Attributes.HasFlag(FileAttributes.ReadOnly)) { - File.Copy(havokSource, havokTarget); + log.Warn("Game directory is readonly. You should copy steam_api64.dll, Havok.dll from bin manually"); + return; } - else if (File.GetLastWriteTime(havokTarget) < File.GetLastWriteTime(havokSource)) - { - File.Delete(havokTarget); - File.Copy(havokSource, havokTarget); + + try + { + var apiSource = Path.Combine(binPath, "steam_api64.dll"); + var apiTarget = Path.Combine(workingDir.FullName, "steam_api64.dll"); + if (!File.Exists(apiTarget)) + { + File.Copy(apiSource, apiTarget); + } + else if (File.GetLastWriteTime(apiTarget) < File.GetLastWriteTime(binPath)) + { + File.Delete(apiTarget); + File.Copy(apiSource, apiTarget); + } + + var havokSource = Path.Combine(binPath, "Havok.dll"); + var havokTarget = Path.Combine(workingDir.FullName, "Havok.dll"); + + if (!File.Exists(havokTarget)) + { + File.Copy(havokSource, havokTarget); + } + else if (File.GetLastWriteTime(havokTarget) < File.GetLastWriteTime(havokSource)) + { + File.Delete(havokTarget); + File.Copy(havokSource, havokTarget); + } + } + catch (UnauthorizedAccessException) + { + // file is being used by another process, probably previous torch has not been closed yet + } + catch (Exception e) + { + log.Error(e); } } }