mirror of
https://github.com/MikuLeaks/MikuSB.git
synced 2026-06-04 20:13:57 +00:00
enter intro cutscene
This commit is contained in:
43
Common/Data/Config/DictionaryConverter.cs
Normal file
43
Common/Data/Config/DictionaryConverter.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MikuSB.Data.Config;
|
||||
|
||||
class IntDictionaryConverter : JsonConverter<Dictionary<int, int>>
|
||||
{
|
||||
public override Dictionary<int, int>? ReadJson(JsonReader reader, Type objectType, Dictionary<int, int>? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.StartArray)
|
||||
{
|
||||
JArray.Load(reader);
|
||||
return new Dictionary<int, int>();
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.StartObject)
|
||||
{
|
||||
var obj = JObject.Load(reader);
|
||||
var dict = new Dictionary<int, int>();
|
||||
|
||||
foreach (var prop in obj.Properties())
|
||||
{
|
||||
if (int.TryParse(prop.Name, out var key))
|
||||
{
|
||||
dict[key] = prop.Value.ToObject<int>();
|
||||
}
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
|
||||
return new Dictionary<int, int>();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Dictionary<int, int>? value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (var kv in value)
|
||||
{
|
||||
writer.WritePropertyName(kv.Key.ToString());
|
||||
writer.WriteValue(kv.Value);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
18
Common/Data/ExcelResource.cs
Normal file
18
Common/Data/ExcelResource.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace MikuSB.Data;
|
||||
|
||||
public abstract class ExcelResource
|
||||
{
|
||||
public abstract uint GetId();
|
||||
|
||||
public virtual void Loaded()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Finalized()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void AfterAllDone()
|
||||
{
|
||||
}
|
||||
}
|
||||
5
Common/Data/GameData.cs
Normal file
5
Common/Data/GameData.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace MikuSB.Data;
|
||||
|
||||
public static class GameData
|
||||
{
|
||||
}
|
||||
33
Common/Data/ResourceEntity.cs
Normal file
33
Common/Data/ResourceEntity.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace MikuSB.Data;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class ResourceEntity : Attribute
|
||||
{
|
||||
[Obsolete("No effect")]
|
||||
public ResourceEntity(string fileName, bool isCritical = false, bool isMultifile = false)
|
||||
{
|
||||
if (isMultifile)
|
||||
FileName = new List<string>(fileName.Split(','));
|
||||
else
|
||||
FileName = [fileName];
|
||||
IsCritical = isCritical;
|
||||
}
|
||||
|
||||
|
||||
public ResourceEntity(string fileName, bool isMultifile = false)
|
||||
{
|
||||
if (isMultifile)
|
||||
FileName = new List<string>(fileName.Split(','));
|
||||
else
|
||||
FileName = [fileName];
|
||||
}
|
||||
|
||||
public ResourceEntity(string fileName)
|
||||
{
|
||||
FileName = [fileName];
|
||||
}
|
||||
|
||||
public List<string> FileName { get; private set; }
|
||||
|
||||
[Obsolete("No effect")] public bool IsCritical { get; private set; } // deprecated
|
||||
}
|
||||
169
Common/Data/ResourceManager.cs
Normal file
169
Common/Data/ResourceManager.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using MikuSB.Internationalization;
|
||||
using MikuSB.Util;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MikuSB.Data;
|
||||
|
||||
public class ResourceManager
|
||||
{
|
||||
public static Logger Logger { get; } = new("ResourceManager");
|
||||
public static bool IsLoaded { get; set; }
|
||||
|
||||
public static void LoadGameData()
|
||||
{
|
||||
LoadExcel();
|
||||
}
|
||||
|
||||
public static void LoadExcel()
|
||||
{
|
||||
var classes = Assembly.GetExecutingAssembly().GetTypes(); // Get all classes in the assembly
|
||||
List<ExcelResource> resList = [];
|
||||
|
||||
foreach (var cls in classes.Where(x => x.IsSubclassOf(typeof(ExcelResource))))
|
||||
{
|
||||
var res = LoadSingleExcelResource(cls);
|
||||
if (res != null) resList.AddRange(res);
|
||||
}
|
||||
|
||||
foreach (var cls in resList) cls.AfterAllDone();
|
||||
}
|
||||
|
||||
public static List<T>? LoadSingleExcel<T>(Type cls) where T : ExcelResource, new()
|
||||
{
|
||||
return LoadSingleExcelResource(cls) as List<T>;
|
||||
}
|
||||
|
||||
public static List<ExcelResource>? LoadSingleExcelResource(Type cls)
|
||||
{
|
||||
var attribute = (ResourceEntity?)Attribute.GetCustomAttribute(cls, typeof(ResourceEntity));
|
||||
|
||||
if (attribute == null) return null;
|
||||
var resource = (ExcelResource)Activator.CreateInstance(cls)!;
|
||||
var count = 0;
|
||||
List<ExcelResource> resList = [];
|
||||
foreach (var fileName in attribute.FileName)
|
||||
try
|
||||
{
|
||||
var path = ConfigManager.Config.Path.ResourcePath + "/ExcelOutput/" + fileName;
|
||||
var file = new FileInfo(path);
|
||||
if (!file.Exists)
|
||||
{
|
||||
Logger.Error(I18NManager.Translate("Server.ServerInfo.FailedToReadItem", fileName,
|
||||
I18NManager.Translate("Word.NotFound")));
|
||||
continue;
|
||||
}
|
||||
|
||||
var json = file.OpenText().ReadToEnd();
|
||||
using (var reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
reader.Read();
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.StartArray:
|
||||
{
|
||||
// array
|
||||
var jArray = JArray.Parse(json);
|
||||
foreach (var item in jArray)
|
||||
{
|
||||
var res = JsonConvert.DeserializeObject(item.ToString(), cls);
|
||||
resList.Add((ExcelResource)res!);
|
||||
((ExcelResource?)res)?.Loaded();
|
||||
count++;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case JsonToken.StartObject:
|
||||
{
|
||||
// dictionary
|
||||
var jObject = JObject.Parse(json);
|
||||
foreach (var (_, obj) in jObject)
|
||||
{
|
||||
var instance = JsonConvert.DeserializeObject(obj!.ToString(), cls);
|
||||
|
||||
if (((ExcelResource?)instance)?.GetId() == 0 || (ExcelResource?)instance == null)
|
||||
{
|
||||
// Deserialize as JObject to handle nested dictionaries
|
||||
var nestedObject = JsonConvert.DeserializeObject<JObject>(obj.ToString());
|
||||
|
||||
foreach (var nestedItem in nestedObject ?? [])
|
||||
{
|
||||
var nestedInstance =
|
||||
JsonConvert.DeserializeObject(nestedItem.Value!.ToString(), cls);
|
||||
resList.Add((ExcelResource)nestedInstance!);
|
||||
((ExcelResource?)nestedInstance)?.Loaded();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resList.Add((ExcelResource)instance);
|
||||
((ExcelResource)instance).Loaded();
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource.Finalized();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(
|
||||
I18NManager.Translate("Server.ServerInfo.FailedToReadItem", fileName,
|
||||
I18NManager.Translate("Word.Error")), ex);
|
||||
}
|
||||
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.LoadedItems", count.ToString(), cls.Name));
|
||||
|
||||
return resList;
|
||||
}
|
||||
|
||||
public static T? LoadCustomFile<T>(string filetype, string filename)
|
||||
{
|
||||
var type = I18NManager.Translate("Word." + filetype);
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.LoadingItem", type));
|
||||
FileInfo file = new(ConfigManager.Config.Path.DataPath + $"/{filename}.json");
|
||||
T? customFile = default;
|
||||
if (!file.Exists)
|
||||
{
|
||||
Logger.Warn(I18NManager.Translate("Server.ServerInfo.ConfigMissing", type,
|
||||
$"{ConfigManager.Config.Path.DataPath}/{filename}.json", type));
|
||||
return customFile;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var reader = file.OpenRead();
|
||||
using StreamReader reader2 = new(reader);
|
||||
var text = reader2.ReadToEnd();
|
||||
var json = JsonConvert.DeserializeObject<T>(text);
|
||||
customFile = json;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error("Error in reading " + file.Name, ex);
|
||||
}
|
||||
|
||||
switch (customFile)
|
||||
{
|
||||
case Dictionary<int, int> d:
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.LoadedItems", d.Count.ToString(), type));
|
||||
break;
|
||||
case Dictionary<int, List<int>> di:
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.LoadedItems", di.Count.ToString(), type));
|
||||
break;
|
||||
default:
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.LoadedItem", filetype));
|
||||
break;
|
||||
}
|
||||
|
||||
return customFile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user