mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-24 21:13:01 +00:00
add NavMeshFileExtractor
This commit is contained in:
parent
587c90df5f
commit
44c43c15f4
2 changed files with 81 additions and 0 deletions
70
Assets/Scripts/Editor/NavMeshFileExtractor.cs
Normal file
70
Assets/Scripts/Editor/NavMeshFileExtractor.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SanAndreasUnity.Editor
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public class NavMeshFileExtractor
|
||||
{
|
||||
private const string NavMeshesFolderPath = "NavMeshes/";
|
||||
private const string NavMeshesArchiveFileName = "NavMeshes.zip";
|
||||
|
||||
|
||||
static NavMeshFileExtractor()
|
||||
{
|
||||
string fullFolderPath = Path.Combine(Application.dataPath, NavMeshesFolderPath);
|
||||
|
||||
if (!Directory.Exists(fullFolderPath))
|
||||
{
|
||||
Debug.LogError("Failed to extract nav mesh files: folder with nav meshes not found");
|
||||
return;
|
||||
}
|
||||
|
||||
string zipFilePath = Path.Combine(fullFolderPath, NavMeshesArchiveFileName);
|
||||
if (!File.Exists(zipFilePath))
|
||||
return;
|
||||
|
||||
Debug.Log("Attempting to extract nav mesh files");
|
||||
|
||||
var extractedFileNames = new List<string>();
|
||||
long totalFileSizeExtracted = 0;
|
||||
|
||||
using var zipArchive = new ZipArchive(File.OpenRead(zipFilePath), ZipArchiveMode.Read);
|
||||
foreach (var entry in zipArchive.Entries)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(entry.Name))
|
||||
continue;
|
||||
|
||||
using var writeStream = File.OpenWrite(Path.Combine(fullFolderPath, entry.Name));
|
||||
using var entryStream = entry.Open();
|
||||
entryStream.CopyTo(writeStream, 4 * 1024 * 1024);
|
||||
writeStream.Flush(true);
|
||||
|
||||
writeStream.Dispose();
|
||||
entryStream.Dispose();
|
||||
|
||||
extractedFileNames.Add(entry.Name);
|
||||
totalFileSizeExtracted += entry.Length;
|
||||
}
|
||||
|
||||
// close archive so it can be deleted
|
||||
zipArchive.Dispose();
|
||||
|
||||
// delete zip file to reduce size of project, and to prevent it from being extracted again
|
||||
File.Delete(zipFilePath);
|
||||
|
||||
// also delete it's meta file
|
||||
string metaFilePath = zipFilePath + ".meta";
|
||||
if (File.Exists(metaFilePath))
|
||||
File.Delete(metaFilePath);
|
||||
|
||||
AssetDatabase.Refresh();
|
||||
|
||||
Debug.Log($"Successfully extracted nav mesh files, total extracted files' size {totalFileSizeExtracted}, " +
|
||||
$"files extracted:\n{string.Join("\n", extractedFileNames)}");
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Editor/NavMeshFileExtractor.cs.meta
Normal file
11
Assets/Scripts/Editor/NavMeshFileExtractor.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 59048888468f66c498c775ac30e1c895
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue