- tweak: Search think
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
namespace Nebula.Launcher.Services;
|
||||
|
||||
@@ -25,38 +28,86 @@ public class ConVar
|
||||
[ServiceRegister]
|
||||
public class ConfigurationService
|
||||
{
|
||||
public ConfigurationService()
|
||||
private readonly FileService _fileService;
|
||||
private readonly DebugService _debugService;
|
||||
|
||||
public ConfigurationService(FileService fileService, DebugService debugService)
|
||||
{
|
||||
|
||||
_fileService = fileService;
|
||||
_debugService = debugService;
|
||||
}
|
||||
|
||||
public object? GetConfigValue(ConVar conVar)
|
||||
{
|
||||
return conVar.DefaultValue;
|
||||
}
|
||||
if(!_fileService.ConfigurationApi.TryOpen(conVar.Name, out var stream) ||
|
||||
!ReadStream(stream, conVar.Type, out var obj))
|
||||
return conVar.DefaultValue;
|
||||
|
||||
public T? GetConfigValue<T>(ConVar conVar)
|
||||
{
|
||||
var value = GetConfigValue(conVar);
|
||||
if (value is not T tv) return default;
|
||||
return tv;
|
||||
}
|
||||
|
||||
public bool TryGetConfigValue(ConVar conVar,[NotNullWhen(true)] out object? value)
|
||||
{
|
||||
value = GetConfigValue(conVar);
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public bool TryGetConfigValue<T>(ConVar conVar, [NotNullWhen(true)] out T? value)
|
||||
{
|
||||
value = GetConfigValue<T>(conVar);
|
||||
return value != null;
|
||||
_debugService.Log("Loading config file: " + conVar.Name);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void SetConfigValue(ConVar conVar, object value)
|
||||
{
|
||||
if(conVar.Type != value.GetType())
|
||||
if(conVar.Type != value.GetType())
|
||||
{
|
||||
_debugService.Error("Error config file " + conVar.Name + ": Type is not equals " + value.GetType() + " " + conVar.Type);
|
||||
return;
|
||||
}
|
||||
|
||||
_debugService.Log("Saving config file: " + conVar.Name);
|
||||
|
||||
var stream = new MemoryStream();
|
||||
try
|
||||
{
|
||||
using var st = new StreamWriter(stream);
|
||||
st.Write(JsonSerializer.Serialize(value));
|
||||
st.Flush();
|
||||
_fileService.ConfigurationApi.Save(conVar.Name, st.BaseStream);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_debugService.Error(e.Message);
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
private bool ReadStream(Stream stream, Type type,[NotNullWhen(true)] out object? obj)
|
||||
{
|
||||
obj = null;
|
||||
try
|
||||
{
|
||||
obj = JsonSerializer.Deserialize(stream, JsonTypeInfo.CreateJsonTypeInfo(type, JsonSerializerOptions.Default));
|
||||
return obj != null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_debugService.Error(e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfigExt
|
||||
{
|
||||
public static T? GetConfigValue<T>(this ConfigurationService configurationService,ConVar conVar)
|
||||
{
|
||||
var value = configurationService.GetConfigValue(conVar);
|
||||
if (value is not T tv) return default;
|
||||
return tv;
|
||||
}
|
||||
|
||||
public static bool TryGetConfigValue(this ConfigurationService configurationService,ConVar conVar,[NotNullWhen(true)] out object? value)
|
||||
{
|
||||
value = configurationService.GetConfigValue(conVar);
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public static bool TryGetConfigValue<T>(this ConfigurationService configurationService,ConVar conVar, [NotNullWhen(true)] out T? value)
|
||||
{
|
||||
value = configurationService.GetConfigValue<T>(conVar);
|
||||
return value != null;
|
||||
}
|
||||
}
|
||||
@@ -11,16 +11,19 @@ using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Launcher.Services;
|
||||
|
||||
[ServiceRegister]
|
||||
public class FileService
|
||||
{
|
||||
public static string RootPath = Path.Join(Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.ApplicationData), "./Datum/");
|
||||
|
||||
private readonly DebugService _debugService;
|
||||
|
||||
public readonly IReadWriteFileApi ContentFileApi;
|
||||
|
||||
public readonly IReadWriteFileApi EngineFileApi;
|
||||
public readonly IReadWriteFileApi ManifestFileApi;
|
||||
public readonly IReadWriteFileApi ConfigurationApi;
|
||||
|
||||
private HashApi? _hashApi;
|
||||
|
||||
public FileService(DebugService debugService)
|
||||
@@ -29,6 +32,7 @@ public class FileService
|
||||
ContentFileApi = CreateFileApi("content/");
|
||||
EngineFileApi = CreateFileApi("engine/");
|
||||
ManifestFileApi = CreateFileApi("manifest/");
|
||||
ConfigurationApi = CreateFileApi("config/");
|
||||
}
|
||||
|
||||
public List<RobustManifestItem> ManifestItems
|
||||
|
||||
36
Nebula.Launcher/Services/PopupMessageService.cs
Normal file
36
Nebula.Launcher/Services/PopupMessageService.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nebula.Launcher.ViewModels;
|
||||
|
||||
namespace Nebula.Launcher.Services;
|
||||
|
||||
[ServiceRegister]
|
||||
public class PopupMessageService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public Action<PopupViewModelBase?>? OnPopupRequired;
|
||||
|
||||
public PopupMessageService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
|
||||
}
|
||||
|
||||
public void PopupInfo(string info)
|
||||
{
|
||||
var message = _serviceProvider.GetService<InfoPopupViewModel>();
|
||||
message.InfoText = info;
|
||||
PopupMessage(message);
|
||||
}
|
||||
|
||||
public void PopupMessage(PopupViewModelBase viewModelBase)
|
||||
{
|
||||
OnPopupRequired?.Invoke(viewModelBase);
|
||||
}
|
||||
|
||||
public void ClosePopup()
|
||||
{
|
||||
OnPopupRequired?.Invoke(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user