少年修仙传客户端基础资源
hch
2024-05-11 eccde9043d770a64d9181c69393d3ff7c20e21bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2015-2019 Beebyte Limited. All rights reserved.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
 
namespace Beebyte.Obfuscator
{
    public class FileBackup
    {
        /**
         * Returns a map consisting of keys of the original file and a proposed target location.
         */
        public static IDictionary<string, string> GetBackupMap(IEnumerable<string> locations)
        {
            IDictionary<string, string> pathMap = new Dictionary<string, string>();
        
            foreach (string location in locations)
            {
                string backupLocation = location + ".bb_obf_backup.pre";
                pathMap.Add(location, backupLocation);
            }
 
            return pathMap;
        }
        
        /**
         * Backs up files specified in the keys of the map to their corresponding values.
         */
        public static void Backup(IDictionary<string, string> backupMap)
        {
            foreach (KeyValuePair<string,string> keyValuePair in backupMap)
            {
                //This throws an exception if the backup already exists - we want this to happen
                File.Copy(keyValuePair.Key, keyValuePair.Value);
            }
        }
 
        /**
         *
         */
        public static void Restore(IDictionary<string, string> pathMap)
        {
            foreach (KeyValuePair<string, string> entry in pathMap)
            {
                string target = entry.Key;
                string backup = entry.Value;
                
                try
                {
                    if (!File.Exists(backup)) continue;
                        
                    DeleteFileWhenPermitted(target);
                    File.Move(backup, target);
                }
                catch (Exception e)
                {
                    Debug.LogError("Could not restore original DLL to " + target + "\n" + e);
                }
            }
        }
        
        private static void DeleteFileWhenPermitted(string target)
        {
            int attempts = 60;
            while (attempts > 0)
            {
                try
                {
                    if (File.Exists(target)) File.Delete(target);
                    if (attempts < 60) Debug.LogWarning("Successfully accessed " + target);
                    return;
                }
                catch (Exception)
                {
                    Debug.LogWarning("Failed to access " + target + " - Retrying...");
                    Thread.Sleep(500);
                    if (--attempts <= 0) throw;
                }
            }
        }
    }
}