refactored File, Path and Directory usages into FileUtil and cleaned up logs

This commit is contained in:
Alex
2025-01-14 11:42:27 +00:00
parent a615e82086
commit 7ff822be0f
9 changed files with 81 additions and 145 deletions
+5
View File
@@ -25,6 +25,11 @@ public class FileUtil
return Path.GetExtension(path).Replace(".", "");
}
public string GetFileName(string path)
{
return Path.GetFileName(path);
}
public string StripExtension(string path, bool keepPath = false)
{
return keepPath ? path.Split('.').First() : Path.GetFileNameWithoutExtension(path);
+6 -119
View File
@@ -43,18 +43,16 @@ public class ImporterUtil
foreach (var file in files)
{
if (_fileUtil.GetFileExtension(file) != "json") continue;
if (filesToIgnore.Contains(Path.GetFileName(file).ToLower())) continue;
if (filesToIgnore.Contains(_fileUtil.GetFileName(file).ToLower())) continue;
tasks.Add(
Task.Factory.StartNew(() =>
{
// const filename = this.vfs.stripExtension(file);
// const filePathAndName = `${filepath}${file}`;
var fileData = File.ReadAllText(file);
var fileData = _fileUtil.ReadFile(file);
if (onReadCallback != null)
onReadCallback(file, fileData);
var setMethod = GetSetMethod(
Path.GetFileNameWithoutExtension(file).ToLower(),
_fileUtil.StripExtension(file).ToLower(),
loadedType,
out var propertyType,
out var isDictionary
@@ -66,7 +64,7 @@ public class ImporterUtil
onObjectDeserialized(file, fileDeserialized);
setMethod.Invoke(result,
isDictionary ? [Path.GetFileNameWithoutExtension(file), fileDeserialized] : [fileDeserialized]);
isDictionary ? [_fileUtil.StripExtension(file), fileDeserialized] : [fileDeserialized]);
}
catch (Exception e)
{
@@ -116,124 +114,13 @@ public class ImporterUtil
{
var matchedProperty = type.GetProperties()
.FirstOrDefault(prop =>
prop.Name.ToLower() == Path.GetFileNameWithoutExtension(propertyName).ToLower());
prop.Name.ToLower() == _fileUtil.StripExtension(propertyName).ToLower());
if (matchedProperty == null)
throw new Exception(
$"Unable to find property '{Path.GetFileNameWithoutExtension(propertyName)}' for type '{type.Name}'");
$"Unable to find property '{_fileUtil.StripExtension(propertyName)}' for type '{type.Name}'");
propertyType = matchedProperty.PropertyType;
setMethod = matchedProperty.GetSetMethod();
}
return setMethod;
}
/**
* Load files into js objects recursively (synchronous)
* @param filepath Path to folder with files
* @returns
*/
public object LoadRecursive(
string filepath,
Type loadedType,
Action<string, string>? onReadCallback = null,
Action<string, object>? onObjectDeserialized = null
)
{
var result = Activator.CreateInstance(loadedType);
// get all filepaths
var files = Directory.GetFiles(filepath);
var directories = Directory.GetDirectories(filepath);
foreach (var file in files)
if (Path.GetExtension(file) == "json")
{
// const filename = this.vfs.stripExtension(file);
// const filePathAndName = `${filepath}${file}`;
var fileData = File.ReadAllText(file);
onReadCallback(file, fileData);
var matchedProperty = loadedType.GetProperties()
.FirstOrDefault(prop => prop.Name.ToLower() == Path.GetFileNameWithoutExtension(file).ToLower());
if (matchedProperty == null)
throw new Exception($"Unable to find property '{Path.GetFileNameWithoutExtension(file)}' for type '{loadedType.Name}'");
var propertyType = matchedProperty.PropertyType;
var fileDeserialized = _jsonUtil.Deserialize(fileData, propertyType);
onObjectDeserialized(file, fileDeserialized);
matchedProperty.GetSetMethod().Invoke(result, [fileDeserialized]);
}
// deep tree search
foreach (var directory in directories)
{
var matchedProperty = loadedType.GetProperties().FirstOrDefault(prop => prop.Name.ToLower() == directory.ToLower());
if (matchedProperty == null)
throw new Exception($"Unable to find property '{directory}' for type '{loadedType.Name}'");
matchedProperty.GetSetMethod().Invoke(result, [LoadRecursive($"{filepath}{directory}/", matchedProperty.PropertyType)]);
}
// return the result of all async fetch
return result;
}
public async Task<object> LoadAsync(
string filepath,
Type loadedType,
string strippablePath = "",
Action<string, string>? onReadCallback = null,
Action<string, object>? onObjectDeserialized = null
)
{
var result = Activator.CreateInstance(loadedType);
var promises = new List<Task<object>>();
var filesToProcess = new Queue<string>(_fileUtil.GetFiles(filepath, true));
while (filesToProcess.Count != 0)
{
var fileNode = filesToProcess.Dequeue();
if (fileNode == null || _fileUtil.GetFileExtension(fileNode) != "json") continue;
promises.Add(File.ReadAllTextAsync(fileNode).ContinueWith(fd =>
{
onReadCallback(fileNode, fd.Result);
return _jsonUtil.Deserialize(fd.Result, typeof(object));
}));
/*
this.vfs
.readFileAsync(filePathAndName)
.then(async (fileData) => {
onReadCallback(filePathAndName, fileData);
return this.jsonUtil.deserializeWithCacheCheckAsync<any>(fileData, filePathAndName);
})
.then(async (fileDeserialized) => {
onObjectDeserialized(filePathAndName, fileDeserialized);
const strippedFilePath = this.vfs.stripExtension(filePathAndName).replace(filepath, "");
this.placeObject(fileDeserialized, strippedFilePath, result, strippablePath);
})
.then(() => progressWriter.increment()),
);*/
}
//await JSType.Promise<>.all(promises).catch((e) => console.error(e));
return result;
}
/*
protected placeObject<T>(fileDeserialized: any, strippedFilePath: string, result: T, strippablePath: string): void {
const strippedFinalPath = strippedFilePath.replace(strippablePath, "");
let temp = result;
const propertiesToVisit = strippedFinalPath.split("/");
for (let i = 0; i < propertiesToVisit.length; i++) {
const property = propertiesToVisit[i];
if (i === propertiesToVisit.length - 1) {
temp[property] = fileDeserialized;
} else {
if (!temp[property]) {
temp[property] = {};
}
temp = temp[property];
}
}
}*/
}