초기커밋
This commit is contained in:
51
ServerBase/Meta/MetaAssets/ContentLoader.cs
Normal file
51
ServerBase/Meta/MetaAssets/ContentLoader.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
public static class ContentLoader
|
||||
{
|
||||
public static T? loadFile<T>(string dataDir, string fileName) where T : ContentTableBase<T>
|
||||
{
|
||||
try
|
||||
{
|
||||
string exactPath = Path.GetFullPath(dataDir);
|
||||
|
||||
string data = File.ReadAllText(Path.Combine(dataDir, fileName));
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"content load fail. dataDir: {dataDir}, fileName: {fileName}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static T? loadMultipleFiles<T>(string dataDir, string filePattern) where T : ContentTableBase<T>
|
||||
{
|
||||
var files = Directory.GetFiles(dataDir, filePattern, SearchOption.TopDirectoryOnly);
|
||||
|
||||
List<T> tables = new List<T>();
|
||||
foreach (string file in files)
|
||||
{
|
||||
string data = File.ReadAllText(file);
|
||||
T? json = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
|
||||
|
||||
if (json != null)
|
||||
tables.Add(json);
|
||||
}
|
||||
|
||||
T? oneTable = null;
|
||||
foreach (var table in tables)
|
||||
{
|
||||
if (oneTable == null)
|
||||
oneTable = table;
|
||||
else
|
||||
oneTable.merge(table);
|
||||
}
|
||||
|
||||
return oneTable;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user