Clean up filesystem class, fix file changes in DBPFFile, Add DBPFFileTest

This commit is contained in:
LazyDuchess 2022-12-22 09:16:51 -03:00
parent 5af1a6bde9
commit c7c04c5aed
11 changed files with 91 additions and 58 deletions

5
.gitignore vendored
View file

@ -72,4 +72,7 @@ crashlytics-build.properties
/[Aa]ssets/[Ss]treamingAssets/aa/*
# OpenTS2 personal configuration
config.json
config.json
# OpenTS2 - Reserved for CI output
/[Tt]est[Ff]iles/

View file

@ -23,7 +23,8 @@ namespace OpenTS2.Content.Changes
GlobalTGI = tgi.LocalGroupID(package.GroupID),
InternalTGI = tgi,
Dynamic = true,
Package = package
Package = package,
FileSize = (uint)FileData.Length
};
}
public ChangedFile(AbstractAsset asset, byte[] fileData) : this(fileData, asset.InternalTGI, asset.Package)

View file

@ -8,6 +8,7 @@ using UnityEngine.UI;
using OpenTS2.Files;
using OpenTS2.Files.Formats.Reia;
using System.Diagnostics;
using System.IO;
namespace OpenTS2.Engine.Tests
{
@ -28,14 +29,16 @@ namespace OpenTS2.Engine.Tests
private void Start()
{
var streamFs = Filesystem.OpenRead(reiaPath);
var realPath = Filesystem.GetRealPath(reiaPath);
var streamFs = File.OpenRead(realPath);
reia = ReiaFile.Read(streamFs, stream);
}
void Reload()
{
reia.Dispose();
var streamFs = Filesystem.OpenRead(reiaPath);
var realPath = Filesystem.GetRealPath(reiaPath);
var streamFs = File.OpenRead(realPath);
reia = ReiaFile.Read(streamFs, stream);
reload = false;
frameCounter = 0f;

View file

@ -67,22 +67,6 @@ namespace OpenTS2.Files
{
var userPath = s_pathProvider.GetUserPath();
var packageList = RemoveNeighborhoodAndCCPackagesFromList(GetPackagesInDirectory(userPath));
/*
var collectionsPath = Path.Combine(userPath, "Collections");
var lockedBinsPath = Path.Combine(userPath, "LockedBins");
var lotCatalogPath = Path.Combine(userPath, "LotCatalog");
var petBreedsPath = Path.Combine(userPath, "PetBreeds");
var petCoatsPath = Path.Combine(userPath, "PetCoats");
var savedSimsPath = Path.Combine(userPath, "SavedSims");
var thumbnailsPath = Path.Combine(userPath, "Thumbnails");
var packageList = new List<string>();
packageList.AddRange(GetPackagesInDirectory(collectionsPath));
packageList.AddRange(GetPackagesInDirectory(lockedBinsPath));
packageList.AddRange(GetPackagesInDirectory(lotCatalogPath));
packageList.AddRange(GetPackagesInDirectory(petBreedsPath));
packageList.AddRange(GetPackagesInDirectory(petCoatsPath));
packageList.AddRange(GetPackagesInDirectory(savedSimsPath));
packageList.AddRange(GetPackagesInDirectory(thumbnailsPath));*/
return packageList;
}
@ -266,28 +250,9 @@ namespace OpenTS2.Files
path = FileUtils.CleanPath(path);
return path;
}
/// <summary>
/// Opens a file for writing.
/// </summary>
/// <param name="path">Unparsed path</param>
/// <returns>A FileStream</returns>
public static FileStream OpenWrite(string path)
{
return File.OpenWrite(GetRealPath(path));
}
/// <summary>
/// Opens a file for reading.
/// </summary>
/// <param name="path">Unparsed path</param>
/// <returns>A FileStream</returns>
public static FileStream OpenRead(string path)
{
return File.OpenRead(GetRealPath(path));
}
/// <summary>
/// Writes a byte array into a file.
/// Writes a byte array into a file, creating all necessary directories.
/// </summary>
/// <param name="path">Path to output file.</param>
/// <param name="bytes">Byte array to write.</param>
@ -297,29 +262,18 @@ namespace OpenTS2.Files
}
/// <summary>
/// Writes a byte array into a file.
/// Writes a byte array into a file, creating all necessary directories.
/// </summary>
/// <param name="path">Path to output file.</param>
/// <param name="bytes">Byte array to write.</param>
public static void Write(string path, byte[] bytes, int size)
{
var realPath = GetRealPath(path);
var dir = Path.GetDirectoryName(realPath);
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir) && !string.IsNullOrEmpty(dir))
Directory.CreateDirectory(dir);
var fStream = new FileStream(realPath, FileMode.Create, FileAccess.Write);
var fStream = new FileStream(path, FileMode.Create, FileAccess.Write);
fStream.Write(bytes, 0, size);
fStream.Dispose();
//File.WriteAllBytes(realPath, bytes);
}
/// <summary>
/// Deletes a file
/// </summary>
/// <param name="path">Path to file to delete.</param>
public static void Delete(string path)
{
File.Delete(path);
}
}
}

View file

@ -311,7 +311,7 @@ namespace OpenTS2.Files.Formats.DBPF
{
_filePath = file;
GroupID = FileUtils.GroupHash(Path.GetFileNameWithoutExtension(file));
var stream = Filesystem.OpenRead(file);
var stream = File.OpenRead(file);
Read(stream);
_changes.Dirty = false;
}
@ -403,6 +403,7 @@ namespace OpenTS2.Files.Formats.DBPF
}
_compressionDIR = (DIRAsset)GetAssetByTGI(ResourceKey.DIR);
}
/// <summary>
/// Write and clear all changes to FilePath.
/// </summary>
@ -412,7 +413,7 @@ namespace OpenTS2.Files.Formats.DBPF
{
Dispose();
Provider?.RemovePackage(this);
Filesystem.Delete(FilePath);
File.Delete(FilePath);
Changes.Clear();
_deleted = true;
return;
@ -420,7 +421,7 @@ namespace OpenTS2.Files.Formats.DBPF
var data = Serialize();
Dispose();
Filesystem.Write(FilePath, data);
var stream = Filesystem.OpenRead(FilePath);
var stream = File.OpenRead(FilePath);
Read(stream);
Changes.Clear();
return;

View file

@ -17,7 +17,7 @@ public class ContentTest
stringSetData.Strings[Languages.USEnglish].Add(new StringValue(usString));
var stringSet = new StringSetAsset(stringSetData);
// Test US Localisation String.
// Test US Localization String.
Assert.AreEqual(usString, stringSet.StringData.GetString(0, Languages.USEnglish));
// Test fallback to US String when UK Language (or any other) is not present.

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 07981c4d1f1b5a047bb25676ce625998
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 54e831512dec5b64084bccde60202d5a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5f7409c616298d94c8f11c35a17154d9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,36 @@
using NUnit.Framework;
using OpenTS2.Files.Formats.DBPF;
using OpenTS2.Common;
using System.IO;
using UnityEngine;
using System.Text;
public class DPBFFileTest
{
[Test]
public void DBPFFileSaveLoadTest()
{
var dummyTGI = new ResourceKey("dummyresource", GroupIDs.Local, 0x0);
var dummyResourceBytes = Encoding.ASCII.GetBytes("TestResource");
var packagePath = "TestFiles/TestPackage.package";
var package = new DBPFFile();
package.FilePath = packagePath;
Assert.IsTrue(package.Empty);
package.Changes.Set(dummyResourceBytes, dummyTGI, false);
Assert.IsTrue(package.Entries.Count == 1);
var data = package.GetItemByTGI(dummyTGI);
Assert.IsTrue(data == dummyResourceBytes);
package.WriteToFile();
package.Dispose();
var loadedPackage = new DBPFFile(packagePath);
Assert.IsTrue(package.Entries.Count == 1);
var dummyResource = loadedPackage.GetItemByTGI(dummyTGI);
Assert.IsTrue(Encoding.ASCII.GetString(dummyResource) == Encoding.ASCII.GetString(dummyResourceBytes));
loadedPackage.Dispose();
if (File.Exists(packagePath))
File.Delete(packagePath);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4d6b1a7a351a4048871fa7985b6dcfd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: