2020-05-31 17:07:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Importing.Archive
|
|
|
|
|
{
|
|
|
|
|
public class LooseArchive : IArchive
|
|
|
|
|
{
|
|
|
|
|
private struct LooseArchiveEntry
|
|
|
|
|
{
|
|
|
|
|
public readonly string FilePath;
|
|
|
|
|
public readonly string Name;
|
|
|
|
|
|
|
|
|
|
public LooseArchiveEntry(string filePath)
|
|
|
|
|
{
|
|
|
|
|
FilePath = filePath;
|
|
|
|
|
Name = Path.GetFileName(filePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly HashSet<string> _sValidExtensions
|
|
|
|
|
= new HashSet<string> {
|
|
|
|
|
".txd",
|
|
|
|
|
".gxt",
|
|
|
|
|
".col",
|
|
|
|
|
".dff",
|
|
|
|
|
".fxp",
|
2019-10-08 16:27:58 +00:00
|
|
|
|
".ifp",
|
|
|
|
|
".ide",
|
|
|
|
|
".ipl",
|
|
|
|
|
".zon",
|
2019-10-09 21:29:34 +00:00
|
|
|
|
".img",
|
2019-10-09 22:18:28 +00:00
|
|
|
|
".dat",
|
2019-10-09 22:22:35 +00:00
|
|
|
|
".cfg",
|
2020-05-31 17:07:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<String, LooseArchiveEntry> _fileDict;
|
|
|
|
|
private readonly Dictionary<String, List<String>> _extDict;
|
|
|
|
|
|
2021-01-31 00:29:36 +00:00
|
|
|
|
public int NumLoadedEntries => _fileDict.Count;
|
|
|
|
|
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
public static LooseArchive Load(string dirPath)
|
|
|
|
|
{
|
|
|
|
|
return new LooseArchive(dirPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LooseArchive(string dirPath)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Loading loose archive: " + dirPath);
|
|
|
|
|
|
|
|
|
|
_fileDict = new Dictionary<string, LooseArchiveEntry>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
|
_extDict = new Dictionary<string, List<string>>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
foreach (var file in Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
var ext = Path.GetExtension(file);
|
|
|
|
|
|
|
|
|
|
ext = ext.ToLower();
|
|
|
|
|
|
|
|
|
|
if (!_sValidExtensions.Contains(ext)) continue;
|
|
|
|
|
|
|
|
|
|
var entry = new LooseArchiveEntry(file);
|
|
|
|
|
|
|
|
|
|
if (_fileDict.ContainsKey(entry.Name))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarningFormat("Already loaded {0}", entry.Name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Debug.Log ("Adding loose archive entry: " + entry.FilePath);
|
|
|
|
|
|
|
|
|
|
_fileDict.Add(entry.Name, entry);
|
|
|
|
|
|
|
|
|
|
if (ext == null) continue;
|
|
|
|
|
|
|
|
|
|
if (!_extDict.ContainsKey(ext))
|
|
|
|
|
{
|
|
|
|
|
_extDict.Add(ext, new List<string>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_extDict[ext].Add(entry.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 01:10:08 +00:00
|
|
|
|
public IEnumerable<string> GetAllFiles()
|
|
|
|
|
{
|
|
|
|
|
return _fileDict.Keys;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
public IEnumerable<string> GetFileNamesWithExtension(string ext)
|
|
|
|
|
{
|
|
|
|
|
return _extDict.ContainsKey(ext) ? _extDict[ext] : Enumerable.Empty<string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ContainsFile(string name)
|
|
|
|
|
{
|
|
|
|
|
return _fileDict.ContainsKey(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public System.IO.Stream ReadFile(string name)
|
|
|
|
|
{
|
|
|
|
|
return File.OpenRead(_fileDict[name].FilePath);
|
|
|
|
|
}
|
2019-10-08 16:27:58 +00:00
|
|
|
|
|
2021-01-31 19:08:48 +00:00
|
|
|
|
public bool GetFilePath(string fileName, ref string filePath)
|
2019-10-08 16:27:58 +00:00
|
|
|
|
{
|
|
|
|
|
if (_fileDict.TryGetValue(fileName, out LooseArchiveEntry entry))
|
|
|
|
|
{
|
2021-01-31 19:08:48 +00:00
|
|
|
|
filePath = entry.FilePath;
|
2019-10-08 16:27:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|