client_Hale
2020-10-20 091cd9c88573f415e7897e9c298459dd6ba9fda7
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.secondworld.universalsdk;
 
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by Administrator on 2018/6/14 0014.
 */
 
public class FileUtil
{
 
    private static final String TAG = "H2Engine_FileUtil";
 
    /**
     * Unity随包资源(StreamingAssets)拷贝至 persistentDataPath 路径下
     */
    public static void copyAssets(final Activity activity)
    {
 
        // 判断是否已经执行过
        SharedPreferences _sp = activity.getPreferences(Context.MODE_PRIVATE);
        Boolean _isCopied = hasCopy(activity);
 
        LogUtil.i(TAG, "是否已经执行过拷贝逻辑: " + _isCopied);
 
        // 获取当前包的版本号
        final String _currentVer = UniversalUtil.GetVersionName(activity);
 
        // 已经拷贝过, 则再次判断一次版本号
        // 如果版本号不相同, 则重新进行拷贝
        // 获取本地存储的版本号
        String _installVer = _sp.getString(StaticDefine.LS_KEY_VERSION, "");
 
        if (!_installVer.equals(_currentVer))
        {
            LogUtil.i(TAG, "版本比较过后发现不同: " + _installVer + " != " + _currentVer);
            SharedPreferences.Editor _editor = _sp.edit();
            // 存储此次的版本信息
            _editor.putString(StaticDefine.LS_KEY_VERSION, _currentVer);
            _editor.apply();
 
            if (_isCopied)
            {
                // 将本地存储修改为未拷贝过
                deleteRecord(activity);
                _isCopied = false;
            }
        }
 
        if (_isCopied)
        {
            Map<String, Object> _msg = new HashMap<>();
            _msg.put("code", CodeA2U.AssetCopyFinished);
            UniversalUtil.sendMessageToUnity(_msg);
            return;
        }
 
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
 
                String _dest = activity.getExternalFilesDir("").getAbsolutePath();
                AssetManager _assetMgr = activity.getAssets();
 
                try
                {
 
                    String[] _fileNames = _assetMgr.list("android");
                    if (_fileNames != null)
                    {
                        for (String _fileName : _fileNames)
                        {
                            copy(activity, "android" + File.separator + _fileName,
                                 _dest + File.separator + _fileName);
                        }
                    }
 
                    recordCopy(activity);
 
                    Map<String, Object> _msg = new HashMap<>();
                    _msg.put("code", CodeA2U.AssetCopyFinished);
                    UniversalUtil.sendMessageToUnity(_msg);
 
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
 
            }
        }).start();
    }
 
    public static void copy(Context context, String fileName)
    {
        String _originalPath = "android" + File.separator + fileName;
        String _destPath = context.getExternalFilesDir(
                "").getAbsolutePath() + File.separator + fileName;
        String _destDir = _destPath.substring(0, _destPath.lastIndexOf('/') + 1);
        File _file = new File(_destDir);
        if(!_file.exists())
        {
            LogUtil.i(TAG,"单独拷贝 => 不存在指定路径: " + _destDir + ", 这里创建...");
            _file.mkdir();
        }
        try
        {
            InputStream _is = context.getAssets().open(_originalPath);
            FileOutputStream _fos = new FileOutputStream(new File(_destPath));
            byte[] _buffer = new byte[1024];
            int _byteCount;
            while ((_byteCount = _is.read(_buffer)) != -1)
            {
                _fos.write(_buffer, 0, _byteCount);
            }
            _fos.flush();
            _is.close();
            _fos.close();
            LogUtil.i("FileUtil", "单独拷贝 => 文件: " + _originalPath + " 已拷贝至: " + _destPath);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
    public static void copy(Context context, String original, String dest)
    {
        try
        {
            String _fileNames[] = context.getAssets().list(original);
            if (_fileNames.length > 0)
            {
                File _dir = new File(dest);
                _dir.mkdir();
                LogUtil.i("FileUtil", "[" + original + "] 是一个文件夹, 创建文件夹: [" + dest + "]");
                for (String _fileName : _fileNames)
                {
                    copy(context, original + File.separator + _fileName,
                         dest + File.separator + _fileName);
                }
            }
            else
            {
                InputStream _is = context.getAssets().open(original);
                FileOutputStream _fos = new FileOutputStream(new File(dest));
                byte[] _buffer = new byte[1024];
                int _byteCount;
                while ((_byteCount = _is.read(_buffer)) != -1)
                {
                    _fos.write(_buffer, 0, _byteCount);
                }
                _fos.flush();
                _is.close();
                _fos.close();
                LogUtil.i("FileUtil", "文件: " + original + " 已拷贝至: " + dest);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
    public static String getFileNameWithoutExtension(String fileName)
    {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }
 
    private static boolean hasCopy(Context context)
    {
        File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt");
        return _file.exists();
    }
 
    private static void deleteRecord(Context context)
    {
        File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt");
        if (_file.exists())
        {
            _file.delete();
        }
    }
 
    private static void recordCopy(Context context)
    {
        File _file = new File(context.getExternalFilesDir(""), "/assetCopyFinish.txt");
        try
        {
            FileOutputStream _fos = new FileOutputStream(_file);
            Writer _writer = new OutputStreamWriter(_fos, "UTF-8");
            _writer.write(1);
            _writer.close();
            _fos.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}