Files
Test-Multiplayer/Library/PackageCache/com.unity.dedicated-server@5a2666836321/CliArguments/Editor/BootConfig.cs
pelpanagiotis 329b7d3183 First Commit
2025-12-21 18:31:02 +02:00

101 lines
2.9 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
namespace Unity.DedicatedServer
{
// Allow multiples when read
// Adding keys replaces the last known key with first value
// Order lines read is maintained for ease in testing
class BootConfig
{
private string _bootConfigFullPath;
private Dictionary<string, int> _data;
private List<string> _lines;
private string BootConfigFullPath(BuildTarget target, string pathToBuiltProject)
{
if (target == BuildTarget.StandaloneOSX)
return Path.Combine(pathToBuiltProject, "Data", "boot.config");
else
{
var projectDir = Path.GetDirectoryName(pathToBuiltProject);
var projectName = Path.GetFileNameWithoutExtension(pathToBuiltProject);
return Path.Combine(projectDir, $"{projectName}_Data", "boot.config");
}
}
public BootConfig(BuildTarget target, string pathToBuiltProject)
{
_bootConfigFullPath = BootConfigFullPath(target, pathToBuiltProject);
_data = new Dictionary<string, int>();
_lines = new List<string>();
}
private void Reset()
{
_data.Clear();
_lines.Clear();
}
public bool Read()
{
try
{
using (StreamReader sr = new StreamReader(_bootConfigFullPath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
_lines.Add(line);
string[] parts = line.Split('=');
if (parts.Length > 2)
{
Reset();
return false;
}
var key = parts[0];
_data[key] = _lines.Count - 1;
}
}
#pragma warning disable 168
}
catch (FileNotFoundException e)
{
Reset();
return false;
}
#pragma warning restore
return true;
}
public void Add(string key, string value)
{
if (_data.ContainsKey(key))
{
var line = _data[key];
_lines[line] = $"{key}={value}";
}
else
{
_lines.Add($"{key}={value}");
}
}
public bool Write()
{
StringBuilder sb = new StringBuilder();
foreach (var line in _lines)
sb.AppendLine(line);
File.WriteAllText(_bootConfigFullPath, sb.ToString());
return true;
}
}
}