mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-25 16:42:54 +01:00
Compare commits
2 Commits
9d9734d1fd
...
def6c41a77
Author | SHA1 | Date | |
---|---|---|---|
def6c41a77 | |||
3f976295bd |
@ -0,0 +1,92 @@
|
||||
using System.Text;
|
||||
using Kajabity.Tools.Java;
|
||||
|
||||
namespace Phantom.Agent.Minecraft.Java;
|
||||
|
||||
sealed class JavaPropertiesFileEditor {
|
||||
private static readonly Encoding Encoding = Encoding.GetEncoding("ISO-8859-1");
|
||||
|
||||
private readonly Dictionary<string, string> overriddenProperties = new ();
|
||||
|
||||
public void Set(string key, string value) {
|
||||
overriddenProperties[key] = value;
|
||||
}
|
||||
|
||||
public async Task EditOrCreate(string filePath) {
|
||||
if (File.Exists(filePath)) {
|
||||
string tmpFilePath = filePath + ".tmp";
|
||||
File.Copy(filePath, tmpFilePath, overwrite: true);
|
||||
await EditFromCopyOrCreate(filePath, tmpFilePath);
|
||||
File.Move(tmpFilePath, filePath, overwrite: true);
|
||||
}
|
||||
else {
|
||||
await EditFromCopyOrCreate(null, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EditFromCopyOrCreate(string? sourceFilePath, string targetFilePath) {
|
||||
var properties = new JavaProperties();
|
||||
|
||||
if (sourceFilePath != null) {
|
||||
// TODO replace with custom async parser
|
||||
await using var sourceStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
properties.Load(sourceStream, Encoding);
|
||||
}
|
||||
|
||||
foreach (var (key, value) in overriddenProperties) {
|
||||
properties[key] = value;
|
||||
}
|
||||
|
||||
await using var targetStream = new FileStream(targetFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
await using var targetWriter = new StreamWriter(targetStream, Encoding);
|
||||
|
||||
await targetWriter.WriteLineAsync("# Properties");
|
||||
|
||||
foreach (var (key, value) in properties) {
|
||||
await WriteProperty(targetWriter, key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task WriteProperty(StreamWriter writer, string key, string value) {
|
||||
await WritePropertyComponent(writer, key, escapeSpaces: true);
|
||||
await writer.WriteAsync('=');
|
||||
await WritePropertyComponent(writer, value, escapeSpaces: false);
|
||||
await writer.WriteLineAsync();
|
||||
}
|
||||
|
||||
private static async Task WritePropertyComponent(TextWriter writer, string component, bool escapeSpaces) {
|
||||
for (int index = 0; index < component.Length; index++) {
|
||||
var c = component[index];
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '#':
|
||||
case '!':
|
||||
case '=':
|
||||
case ':':
|
||||
case ' ' when escapeSpaces || index == 0:
|
||||
await writer.WriteAsync('\\');
|
||||
await writer.WriteAsync(c);
|
||||
break;
|
||||
case var _ when c > 31 && c < 127:
|
||||
await writer.WriteAsync(c);
|
||||
break;
|
||||
case '\t':
|
||||
await writer.WriteAsync("\\t");
|
||||
break;
|
||||
case '\n':
|
||||
await writer.WriteAsync("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
await writer.WriteAsync("\\r");
|
||||
break;
|
||||
case '\f':
|
||||
await writer.WriteAsync("\\f");
|
||||
break;
|
||||
default:
|
||||
await writer.WriteAsync("\\u");
|
||||
await writer.WriteAsync(((int) c).ToString("X4"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
using System.Text;
|
||||
using Kajabity.Tools.Java;
|
||||
using Phantom.Agent.Minecraft.Instance;
|
||||
using Phantom.Agent.Minecraft.Java;
|
||||
using Phantom.Agent.Minecraft.Server;
|
||||
using Phantom.Common.Minecraft;
|
||||
using Phantom.Common.Data.Java;
|
||||
using Phantom.Utils.Runtime;
|
||||
using Serilog;
|
||||
|
||||
@ -108,21 +107,8 @@ public abstract class BaseLauncher : IServerLauncher {
|
||||
}
|
||||
|
||||
private static async Task UpdateServerProperties(InstanceProperties instanceProperties) {
|
||||
var serverPropertiesFilePath = Path.Combine(instanceProperties.InstanceFolder, "server.properties");
|
||||
var serverPropertiesData = new JavaProperties();
|
||||
|
||||
await using var fileStream = new FileStream(serverPropertiesFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||
try {
|
||||
serverPropertiesData.Load(fileStream);
|
||||
} catch (ParseException e) {
|
||||
throw new Exception("Could not parse server.properties file: " + serverPropertiesFilePath, e);
|
||||
}
|
||||
|
||||
instanceProperties.ServerProperties.SetTo(serverPropertiesData);
|
||||
|
||||
fileStream.Seek(0L, SeekOrigin.Begin);
|
||||
fileStream.SetLength(0L);
|
||||
|
||||
serverPropertiesData.Store(fileStream, true);
|
||||
var serverPropertiesEditor = new JavaPropertiesFileEditor();
|
||||
instanceProperties.ServerProperties.SetTo(serverPropertiesEditor);
|
||||
await serverPropertiesEditor.EditOrCreate(Path.Combine(instanceProperties.InstanceFolder, "server.properties"));
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Common\Phantom.Common.Data\Phantom.Common.Data.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Phantom.Common.Logging\Phantom.Common.Logging.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Phantom.Common.Minecraft\Phantom.Common.Minecraft.csproj" />
|
||||
<ProjectReference Include="..\..\Utils\Phantom.Utils.Collections\Phantom.Utils.Collections.csproj" />
|
||||
<ProjectReference Include="..\..\Utils\Phantom.Utils.Cryptography\Phantom.Utils.Cryptography.csproj" />
|
||||
<ProjectReference Include="..\..\Utils\Phantom.Utils.IO\Phantom.Utils.IO.csproj" />
|
||||
|
@ -1,4 +1,4 @@
|
||||
using Kajabity.Tools.Java;
|
||||
using Phantom.Agent.Minecraft.Java;
|
||||
|
||||
namespace Phantom.Agent.Minecraft.Properties;
|
||||
|
||||
@ -12,7 +12,7 @@ abstract class MinecraftServerProperty<T> {
|
||||
protected abstract T Read(string value);
|
||||
protected abstract string Write(T value);
|
||||
|
||||
public void Set(JavaProperties properties, T value) {
|
||||
properties.SetProperty(key, Write(value));
|
||||
public void Set(JavaPropertiesFileEditor properties, T value) {
|
||||
properties.Set(key, Write(value));
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
using Kajabity.Tools.Java;
|
||||
using Phantom.Agent.Minecraft.Java;
|
||||
|
||||
namespace Phantom.Agent.Minecraft.Properties;
|
||||
|
||||
@ -7,7 +7,7 @@ public sealed record ServerProperties(
|
||||
ushort RconPort,
|
||||
bool EnableRcon = true
|
||||
) {
|
||||
internal void SetTo(JavaProperties properties) {
|
||||
internal void SetTo(JavaPropertiesFileEditor properties) {
|
||||
MinecraftServerProperties.ServerPort.Set(properties, ServerPort);
|
||||
MinecraftServerProperties.RconPort.Set(properties, RconPort);
|
||||
MinecraftServerProperties.EnableRcon.Set(properties, EnableRcon);
|
||||
|
@ -1,6 +1,6 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Phantom.Common.Minecraft;
|
||||
namespace Phantom.Common.Data.Java;
|
||||
|
||||
public static class JvmArgumentsHelper {
|
||||
public static ImmutableArray<string> Split(string arguments) {
|
@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -26,8 +26,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Common.Data.Tests",
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Common.Logging", "Common\Phantom.Common.Logging\Phantom.Common.Logging.csproj", "{D7F55010-B3ED-42A5-8D83-E754FFC5F2A2}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Common.Minecraft", "Common\Phantom.Common.Minecraft\Phantom.Common.Minecraft.csproj", "{48278E42-17BB-442B-9877-ACCE0C02C268}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Common.Messages", "Common\Phantom.Common.Messages\Phantom.Common.Messages.csproj", "{95B55357-F8F0-48C2-A1C2-5EA997651783}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phantom.Server", "Server\Phantom.Server\Phantom.Server.csproj", "{A0F1C595-96B6-4DBF-8C16-6B99223F8F35}"
|
||||
@ -100,10 +98,6 @@ Global
|
||||
{D7F55010-B3ED-42A5-8D83-E754FFC5F2A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D7F55010-B3ED-42A5-8D83-E754FFC5F2A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D7F55010-B3ED-42A5-8D83-E754FFC5F2A2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{48278E42-17BB-442B-9877-ACCE0C02C268}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{48278E42-17BB-442B-9877-ACCE0C02C268}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{48278E42-17BB-442B-9877-ACCE0C02C268}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{48278E42-17BB-442B-9877-ACCE0C02C268}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{95B55357-F8F0-48C2-A1C2-5EA997651783}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{95B55357-F8F0-48C2-A1C2-5EA997651783}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{95B55357-F8F0-48C2-A1C2-5EA997651783}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
@ -184,7 +178,6 @@ Global
|
||||
{AEE8B77E-AB07-423F-9981-8CD829ACB834} = {F5878792-64C8-4ECF-A075-66341FF97127}
|
||||
{6C3DB1E5-F695-4D70-8F3A-78C2957274BE} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18}
|
||||
{D7F55010-B3ED-42A5-8D83-E754FFC5F2A2} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18}
|
||||
{48278E42-17BB-442B-9877-ACCE0C02C268} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18}
|
||||
{95B55357-F8F0-48C2-A1C2-5EA997651783} = {01CB1A81-8950-471C-BFDF-F135FDDB2C18}
|
||||
{435D7981-DFDA-46A0-8CD8-CD8C117935D7} = {D781E00D-8563-4102-A0CD-477A679193B5}
|
||||
{A0F1C595-96B6-4DBF-8C16-6B99223F8F35} = {8AC8FB6C-033A-4626-820F-ED0F908756B2}
|
||||
|
@ -2,12 +2,12 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Phantom.Common.Data;
|
||||
using Phantom.Common.Data.Instance;
|
||||
using Phantom.Common.Data.Java;
|
||||
using Phantom.Common.Data.Minecraft;
|
||||
using Phantom.Common.Data.Replies;
|
||||
using Phantom.Common.Logging;
|
||||
using Phantom.Common.Messages;
|
||||
using Phantom.Common.Messages.ToAgent;
|
||||
using Phantom.Common.Minecraft;
|
||||
using Phantom.Server.Database;
|
||||
using Phantom.Server.Database.Entities;
|
||||
using Phantom.Server.Minecraft;
|
||||
|
@ -12,7 +12,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Common\Phantom.Common.Data\Phantom.Common.Data.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Phantom.Common.Minecraft\Phantom.Common.Minecraft.csproj" />
|
||||
<ProjectReference Include="..\..\Utils\Phantom.Utils.Collections\Phantom.Utils.Collections.csproj" />
|
||||
<ProjectReference Include="..\..\Utils\Phantom.Utils.Events\Phantom.Utils.Events.csproj" />
|
||||
<ProjectReference Include="..\Phantom.Server.Database\Phantom.Server.Database.csproj" />
|
||||
|
@ -1,5 +1,4 @@
|
||||
@using Phantom.Common.Data.Minecraft
|
||||
@using Phantom.Common.Minecraft
|
||||
@using Phantom.Server.Minecraft
|
||||
@using Phantom.Server.Services.Agents
|
||||
@using Phantom.Server.Services.Audit
|
||||
@ -7,10 +6,10 @@
|
||||
@using System.Collections.Immutable
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using System.Diagnostics.CodeAnalysis
|
||||
@using Phantom.Common.Data.Java
|
||||
@using Phantom.Server.Web.Components.Utils
|
||||
@using Phantom.Server.Web.Identity.Interfaces
|
||||
@using Phantom.Common.Data.Instance
|
||||
@using Phantom.Common.Data.Java
|
||||
@using Phantom.Common.Data
|
||||
@inject INavigation Nav
|
||||
@inject MinecraftVersions MinecraftVersions
|
||||
|
Loading…
Reference in New Issue
Block a user