Last active
January 21, 2024 19:42
-
-
Save sophiathekitty/6786ab2356738d07106934bf760b02ec to your computer and use it in GitHub Desktop.
utility class for access vars in custom data strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sandbox.Game.EntityComponents; | |
using Sandbox.ModAPI.Ingame; | |
using Sandbox.ModAPI.Interfaces; | |
using SpaceEngineers.Game.ModAPI.Ingame; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.Immutable; | |
using System.Linq; | |
using System.Text; | |
using VRage; | |
using VRage.Collections; | |
using VRage.Game; | |
using VRage.Game.Components; | |
using VRage.Game.GUI.TextPanel; | |
using VRage.Game.ModAPI.Ingame; | |
using VRage.Game.ModAPI.Ingame.Utilities; | |
using VRage.Game.ObjectBuilders.Definitions; | |
using VRageMath; | |
namespace IngameScript | |
{ | |
partial class Program | |
{ | |
//---------------------------------------------------------------------- | |
// utility class for parsing CustomData variables | |
// VarName:value | |
//---------------------------------------------------------------------- | |
public class CustomDataVars | |
{ | |
// static get functions | |
public static List<string> GetListVar(string key, string data) | |
{ | |
List<string> vars = new List<string>(); | |
string[] lines = data.Split('\n'); | |
foreach (string line in lines) | |
{ | |
if (line.StartsWith(key)) | |
{ | |
string[] var = line.Split(':'); | |
if (var.Length == 2) | |
{ | |
vars.Add(var[1]); | |
} | |
} | |
} | |
return vars; | |
} | |
public static string GetVar(string key, string data) | |
{ | |
string[] lines = data.Split('\n'); | |
foreach (string line in lines) | |
{ | |
if (line.StartsWith(key)) | |
{ | |
string[] var = line.Split(':'); | |
if (var.Length == 2) | |
{ | |
return var[1]; | |
} | |
} | |
} | |
return ""; | |
} | |
public static T GetVarAs<T>(string key, string data, T defaultValue = default(T)) | |
{ | |
Dictionary<string, string> vars = Parse(data); | |
if (!vars.ContainsKey(key)) return defaultValue; //(T)Convert.ChangeType(null,typeof(T)); | |
return (T)Convert.ChangeType(vars[key], typeof(T)); | |
} | |
public static Dictionary<string, string> Parse(string data) | |
{ | |
Dictionary<string, string> vars = new Dictionary<string, string>(); | |
string[] lines = data.Split('\n'); | |
foreach (string line in lines) | |
{ | |
string[] var = line.Split(':'); | |
if (var.Length == 2) | |
{ | |
if (vars.ContainsKey(var[0])) vars[var[0]] += ";" + var[1]; | |
else vars.Add(var[0], var[1]); | |
} | |
} | |
return vars; | |
} | |
// instance class stuff | |
Dictionary<string, string> vars = new Dictionary<string, string>(); | |
IMyTerminalBlock block; | |
public CustomDataVars(string data) | |
{ | |
vars = Parse(data); | |
} | |
public CustomDataVars(IMyTerminalBlock block) | |
{ | |
this.block = block; | |
vars = Parse(block.CustomData); | |
} | |
public bool HasVar(string key) | |
{ | |
return vars.ContainsKey(key); | |
} | |
public bool Save() | |
{ | |
if (block == null) return false; | |
block.CustomData = ""; | |
bool first = true; | |
foreach (KeyValuePair<string, string> var in vars) | |
{ | |
if (!first) block.CustomData += "\n"; | |
else first = false; | |
block.CustomData += var.Key + ":" + var.Value; | |
} | |
return true; | |
} | |
public string GetVar(string key) | |
{ | |
if (!vars.ContainsKey(key)) return ""; | |
return vars[key]; | |
} | |
public T GetVarAs<T>(string key, T defaultValue = default(T)) | |
{ | |
if (!vars.ContainsKey(key)) return defaultValue; //(T)Convert.ChangeType(null,typeof(T)); | |
return (T)Convert.ChangeType(vars[key], typeof(T)); | |
} | |
} | |
//---------------------------------------------------------------------- | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment