From 9b086f7fa51af4d581ea9ef3638c97f4804fb60d Mon Sep 17 00:00:00 2001 From: hch <305670599@qq.com> Date: 星期二, 26 三月 2024 23:35:04 +0800 Subject: [PATCH] 0312 白包 --- SdkProject/library/src/main/java/com/secondworld/sdk/utils/FileUtil.java | 304 ++++++++++++++++++++++++++++---------------------- 1 files changed, 171 insertions(+), 133 deletions(-) diff --git a/SdkProject/library/src/main/java/com/secondworld/sdk/utils/FileUtil.java b/SdkProject/library/src/main/java/com/secondworld/sdk/utils/FileUtil.java index 443a481..a113fed 100644 --- a/SdkProject/library/src/main/java/com/secondworld/sdk/utils/FileUtil.java +++ b/SdkProject/library/src/main/java/com/secondworld/sdk/utils/FileUtil.java @@ -1,133 +1,171 @@ -package com.secondworld.sdk.utils; - -import android.content.res.AssetManager; - -import com.secondworld.sdk.GameAppProxy; -import com.secondworld.sdk.UnityMsgHandler; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; - -/** - * Created by Administrator on 2018/6/14 0014. - */ - -public class FileUtil { - - /** - * Unity闅忓寘璧勬簮鎵归噺鎷疯礉 - */ - public static void copyAssets() { - - // 鑾峰彇褰撳墠鍖呯殑鐗堟湰鍙� - final long currentVer = DeviceUtil.getVersionCode(); - - // 宸茬粡鎷疯礉杩�, 鍒欏啀娆″垽鏂竴娆$増鏈彿 - // 濡傛灉鐗堟湰鍙蜂笉鐩稿悓, 鍒欓噸鏂拌繘琛屾嫹璐� - - // 鑾峰彇鏈湴瀛樺偍鐨勭増鏈彿 - long installVer = SPUtils.get().getLong(SPUtils.LAST_COPY_VERSION, 0); - - if (installVer == currentVer) { - LogUtil.i("copyAssets", "宸叉嫹璐濊繃锛宨nstallVer:" + installVer + "锛�" + "currentVer:" + currentVer); - UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); - return; - } - - String _dest = GameAppProxy.app.getExternalFilesDir("").getAbsolutePath(); - AssetManager _assetMgr = GameAppProxy.app.getAssets(); - try { - String[] _fileNames = _assetMgr.list("android"); - if (_fileNames != null) { - for (String _fileName : _fileNames) { - copy("android" + File.separator + _fileName, - _dest + File.separator + _fileName); - } - } - LogUtil.i("copyAssets", "鍏ㄩ儴鎷疯礉瀹屾垚," + "currentVer:" + currentVer); - // 瀛樺偍姝ゆ鐨勬嫹璐濈増鏈� - SPUtils.get().edit().putLong(SPUtils.LAST_COPY_VERSION, currentVer).apply(); - Map<String, Object> _msg = new HashMap<>(); - UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * unity鍗曚釜璧勬簮鎷疯礉 - * - * @param fileName - */ - public static void copyOneAsset(String fileName) { - String originalPath = "android" + File.separator + fileName; - String destPath = GameAppProxy.app.getExternalFilesDir( - "").getAbsolutePath() + File.separator + fileName; - String _destDir = destPath.substring(0, destPath.lastIndexOf('/') + 1); - File _file = new File(_destDir); - if (!_file.exists()) { - boolean mkdir = _file.mkdir(); - } - try { - InputStream _is = GameAppProxy.app.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(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void copy(String original, String dest) { - try { - String[] fileNames = GameAppProxy.app.getAssets().list(original); - if (fileNames.length > 0) { - File dir = new File(dest); - String mkdir = dir.mkdir() ? "鎴愬姛" : "澶辫触"; - LogUtil.i("copyAssets", "[" + original + "] 鏄竴涓枃浠跺す, 鍒涘缓鏂囦欢澶�: [" + dest + "] " + mkdir); - for (String _fileName : fileNames) { - copy(original + File.separator + _fileName, - dest + File.separator + _fileName); - } - } else { - InputStream _is = GameAppProxy.app.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("copyAssets", "鏂囦欢: " + original + " 宸叉嫹璐濊嚦: " + dest); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static String getFileNameWithoutExtension(String fileName) { - return fileName.substring(0, fileName.lastIndexOf(".")); - } - - /** - * 鑾峰彇鏂囦欢鍚庣紑鍚� - */ - public static String getSuffixName(File file) { - String fileName = file.getName(); - int index = fileName.lastIndexOf("."); - if (index == -1) - return ""; - return fileName.substring(index + 1); - } -} +package com.secondworld.sdk.utils; + +import android.content.res.AssetManager; + +import com.secondworld.sdk.GameAppProxy; +import com.secondworld.sdk.UnityMsgHandler; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Administrator on 2018/6/14 0014. + */ + +public class FileUtil { + + /** + * Unity闅忓寘璧勬簮鎵归噺鎷疯礉 + */ + public static void copyAssets() { + + // 鑾峰彇褰撳墠鍖呯殑鐗堟湰鍙� + final long currentVer = DeviceUtil.getVersionCode(); + + // 宸茬粡鎷疯礉杩�, 鍒欏啀娆″垽鏂竴娆$増鏈彿 + // 濡傛灉鐗堟湰鍙蜂笉鐩稿悓, 鍒欓噸鏂拌繘琛屾嫹璐� + + // 鑾峰彇鏈湴瀛樺偍鐨勭増鏈彿 + long installVer = SPUtils.get().getLong(SPUtils.LAST_COPY_VERSION, 0); + + if (installVer == currentVer) { + LogUtil.i("copyAssets", "宸叉嫹璐濊繃锛宨nstallVer:" + installVer + "锛�" + "currentVer:" + currentVer); + UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); + return; + } + + String _dest = GameAppProxy.app.getExternalFilesDir("").getAbsolutePath(); + AssetManager _assetMgr = GameAppProxy.app.getAssets(); + try { + String[] _fileNames = _assetMgr.list("android"); + if (_fileNames != null) { + for (String _fileName : _fileNames) { + copy("android" + File.separator + _fileName, + _dest + File.separator + _fileName); + } + } + LogUtil.i("copyAssets", "鍏ㄩ儴鎷疯礉瀹屾垚," + "currentVer:" + currentVer); + // 瀛樺偍姝ゆ鐨勬嫹璐濈増鏈� + SPUtils.get().edit().putLong(SPUtils.LAST_COPY_VERSION, currentVer).apply(); + Map<String, Object> _msg = new HashMap<>(); + UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void copyConfigs() { + + // 鑾峰彇褰撳墠鍖呯殑鐗堟湰鍙� + final long currentVer = DeviceUtil.getVersionCode(); + + // 宸茬粡鎷疯礉杩�, 鍒欏啀娆″垽鏂竴娆$増鏈彿 + // 濡傛灉鐗堟湰鍙蜂笉鐩稿悓, 鍒欓噸鏂拌繘琛屾嫹璐� + + // 鑾峰彇鏈湴瀛樺偍鐨勭増鏈彿 + long installVer = SPUtils.get().getLong("configsversion", 0); + + if (installVer == currentVer) { + LogUtil.i("copyAssets", "宸叉嫹璐濊繃锛宨nstallVer:" + installVer + "锛�" + "currentVer:" + currentVer); + UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); + return; + } + + String _dest = GameAppProxy.app.getExternalFilesDir("").getAbsolutePath(); + AssetManager _assetMgr = GameAppProxy.app.getAssets(); + try { + String[] _fileNames = _assetMgr.list("android/config"); + if (_fileNames != null) { + for (String _fileName : _fileNames) { + copy("android/config" + File.separator + _fileName, + _dest + File.separator + _fileName); + } + } + LogUtil.i("copyAssets", "鍏ㄩ儴鎷疯礉瀹屾垚," + "currentVer:" + currentVer); + // 瀛樺偍姝ゆ鐨勬嫹璐濈増鏈� + SPUtils.get().edit().putLong("configsversion", currentVer).apply(); + Map<String, Object> _msg = new HashMap<>(); + UnityMsgHandler.sendMessageToUnity(CodeA2U.AssetCopyFinished); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * unity鍗曚釜璧勬簮鎷疯礉 + * + * @param fileName + */ + public static void copyOneAsset(String fileName) { + String originalPath = "android" + File.separator + fileName; + String destPath = GameAppProxy.app.getExternalFilesDir( + "").getAbsolutePath() + File.separator + fileName; + String _destDir = destPath.substring(0, destPath.lastIndexOf('/') + 1); + File _file = new File(_destDir); + if (!_file.exists()) { + boolean mkdir = _file.mkdir(); + } + try { + InputStream _is = GameAppProxy.app.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(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void copy(String original, String dest) { + try { + String[] fileNames = GameAppProxy.app.getAssets().list(original); + if (fileNames.length > 0) { + File dir = new File(dest); + String mkdir = dir.mkdir() ? "鎴愬姛" : "澶辫触"; + LogUtil.i("copyAssets", "[" + original + "] 鏄竴涓枃浠跺す, 鍒涘缓鏂囦欢澶�: [" + dest + "] " + mkdir); + for (String _fileName : fileNames) { + copy(original + File.separator + _fileName, + dest + File.separator + _fileName); + } + } else { + InputStream _is = GameAppProxy.app.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("copyAssets", "鏂囦欢: " + original + " 宸叉嫹璐濊嚦: " + dest); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static String getFileNameWithoutExtension(String fileName) { + return fileName.substring(0, fileName.lastIndexOf(".")); + } + + /** + * 鑾峰彇鏂囦欢鍚庣紑鍚� + */ + public static String getSuffixName(File file) { + String fileName = file.getName(); + int index = fileName.lastIndexOf("."); + if (index == -1) + return ""; + return fileName.substring(index + 1); + } +} -- Gitblit v1.8.0