Upload from upload_mods.ps1
This commit is contained in:
270
Scripts/GameOptions/CustomGameOptions.cs
Normal file
270
Scripts/GameOptions/CustomGameOptions.cs
Normal file
@@ -0,0 +1,270 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
public static class CustomGameOptions
|
||||
{
|
||||
private static string SaveDataPath = "";
|
||||
private static string ModPath = "";
|
||||
private static string GameOptionsFilename = "CustomGameOptions.xml";
|
||||
public static bool ReloadUI = false;
|
||||
public static Dictionary<string, string> StoredOptions;
|
||||
|
||||
static CustomGameOptions()
|
||||
{
|
||||
//Log.Out("CustomGameOptions-CustomGameOptions START");
|
||||
StoredOptions = new Dictionary<string, string>();
|
||||
var mod = ModManager.GetMod("zzz_REBIRTH__Utils", true);
|
||||
ModPath = mod.Path;
|
||||
SaveDataPath = GameIO.GetUserGameDataDir() + "/RebirthData";
|
||||
Directory.CreateDirectory(SaveDataPath);
|
||||
//Log.Out("DataConfigPath = " + SaveDataPath);
|
||||
}
|
||||
|
||||
public static void CheckConfigs()
|
||||
{
|
||||
//Log.Out("CustomGameOptions-CheckConfigs START");
|
||||
if (GameManager.IsDedicatedServer)
|
||||
{
|
||||
string path = SaveDataPath + "/" + GameOptionsFilename;
|
||||
//Log.Out("CustomGameOptions-CheckConfigs IS DEDICATED");
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
//Log.Out("CustomGameOptions-CheckConfigs FILE DOES NOT EXIST");
|
||||
LoadDefaultGameOptions();
|
||||
SaveGameOptions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void LoadConfigs(XmlFile xmlFile)
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadConfigs START");
|
||||
LoadGameOptions(xmlFile);
|
||||
//Log.Out("Custom Game Options Loaded.");
|
||||
RebirthVariables.setConfigVariables();
|
||||
RebirthVariables.randomWeaponInitiated = false;
|
||||
}
|
||||
|
||||
public static void LoadDefaultGameOptions()
|
||||
{
|
||||
var path = ModPath + "/Config/XUi_Menu/";
|
||||
if (File.Exists(path + "windows.xml"))
|
||||
{
|
||||
XDocument xWindows = SdXDocument.Load(path + "windows.xml");
|
||||
|
||||
if (xWindows.Root == null)
|
||||
{
|
||||
throw new Exception("Invalid CustomGameOptions file: " + path);
|
||||
}
|
||||
|
||||
foreach (XElement xmlElement in xWindows.Descendants("gameoption"))
|
||||
{
|
||||
var value = xmlElement.GetAttribute("default_value");
|
||||
var name = xmlElement.GetAttribute("name");
|
||||
|
||||
StoredOptions[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//Log.Out("Custom Game Options Default Values Loaded.");
|
||||
}
|
||||
|
||||
public static void LoadGameOptions(XmlFile xmlFile, bool isContinueGame = false)
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions START");
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions isContinueGame: " + isContinueGame);
|
||||
|
||||
//string savePath = $"{SaveDataPath}/CustomGameOptions/{GamePrefs.GetString(EnumGamePrefs.GameWorld)}/{GamePrefs.GetString(EnumGamePrefs.GameName)}";
|
||||
string savePath = $"{SaveDataPath}";
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions savePath: " + savePath);
|
||||
|
||||
if (GameManager.IsDedicatedServer)
|
||||
{
|
||||
LoadDefaultGameOptions();
|
||||
}
|
||||
|
||||
XDocument xdocument = new XDocument();
|
||||
if (xmlFile == null)
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions LOCAL");
|
||||
if (!string.IsNullOrEmpty(savePath))
|
||||
{
|
||||
string path = savePath + "/" + GameOptionsFilename;
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions PATH EXISTS, SaveDataPath: " + SaveDataPath);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions FILE EXISTS A");
|
||||
xdocument = SdXDocument.Load(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions FILE DOES NOT EXIST, path: " + path);
|
||||
string oldPath = ModPath + "/CustomGameOptions" + "/" + GameOptionsFilename;
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions old path: " + oldPath);
|
||||
if (File.Exists(oldPath))
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions FILE EXISTS B");
|
||||
xdocument = SdXDocument.Load(oldPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions FILE DOES NOT EXIST B");
|
||||
LoadDefaultGameOptions();
|
||||
SaveGameOptions();
|
||||
xdocument = SdXDocument.Load(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Log.Out("CustomGameOptions-LoadGameOptions xmlFile");
|
||||
xdocument = xmlFile.XmlDoc;
|
||||
}
|
||||
|
||||
if (xdocument.Root == null)
|
||||
{
|
||||
throw new Exception("Invalid CustomGameOptions file");
|
||||
}
|
||||
|
||||
foreach (XElement xmlElement in xdocument.Root.Elements())
|
||||
{
|
||||
if (xmlElement.Name == "config")
|
||||
{
|
||||
if (!xmlElement.HasAttribute("name"))
|
||||
{
|
||||
throw new Exception("Attribute 'name' missing on item");
|
||||
}
|
||||
if (!xmlElement.HasAttribute("value"))
|
||||
{
|
||||
throw new Exception("Attribute 'value' missing on item");
|
||||
}
|
||||
|
||||
var value = xmlElement.GetAttribute("value");
|
||||
var name = xmlElement.GetAttribute("name");
|
||||
|
||||
StoredOptions[name] = value;
|
||||
Log.Out($"CustomGameOption {name}: {value}");
|
||||
}
|
||||
}
|
||||
|
||||
if (GameManager.IsDedicatedServer)
|
||||
{
|
||||
SaveGameOptions();
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveGameOptions()
|
||||
{
|
||||
//Log.Out("CustomGameOptions-SaveGameOptions START");
|
||||
XmlDocument xmlDocument = new XmlDocument();
|
||||
XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||||
xmlDocument.InsertBefore(newChild, xmlDocument.DocumentElement);
|
||||
XmlNode parent = xmlDocument.AppendChild(xmlDocument.CreateElement("customgameoptions"));
|
||||
|
||||
foreach (var key in StoredOptions.Keys)
|
||||
{
|
||||
parent.AppendChild(CreateXMLGameOptionChild(xmlDocument, key, StoredOptions[key].ToString()));
|
||||
}
|
||||
|
||||
//Log.Out(SerializeToString(xmlDocument));
|
||||
|
||||
// Make sure folder is there before saving
|
||||
string savePath = $"{SaveDataPath}/CustomGameOptions/{GamePrefs.GetString(EnumGamePrefs.GameWorld)}/{GamePrefs.GetString(EnumGamePrefs.GameName)}";
|
||||
//Log.Out("CustomGameOptions-SaveGameOptions savePath: " + savePath);
|
||||
//Directory.CreateDirectory(savePath);
|
||||
|
||||
Directory.CreateDirectory(SaveDataPath);
|
||||
//var path = savePath + "/" + GameOptionsFilename;
|
||||
//File.WriteAllText(path, SerializeToString(xmlDocument), Encoding.UTF8);
|
||||
// Write a copy to the root of the RebirthData folder
|
||||
|
||||
XmlWriterSettings settings = new XmlWriterSettings
|
||||
{
|
||||
Indent = true, // Enable indentation
|
||||
IndentChars = " ", // Use 4 spaces for indentation (you can adjust this as needed)
|
||||
NewLineOnAttributes = false // Optionally place attributes on new lines
|
||||
};
|
||||
|
||||
string path = SaveDataPath + "/" + GameOptionsFilename;
|
||||
|
||||
// Write the formatted XML to file
|
||||
using (XmlWriter writer = XmlWriter.Create(path, settings))
|
||||
{
|
||||
xmlDocument.Save(writer);
|
||||
}
|
||||
//File.WriteAllText(path, SerializeToString(xmlDocument), Encoding.UTF8);
|
||||
}
|
||||
|
||||
private static XmlElement CreateXMLGameOptionChild(XmlDocument xmlDocument, string name, string value)
|
||||
{
|
||||
XmlElement xmlElement = xmlDocument.CreateElement("config");
|
||||
XmlAttribute xmlAttributeName = xmlDocument.CreateAttribute("name");
|
||||
xmlAttributeName.Value = name;
|
||||
xmlElement.Attributes.Append(xmlAttributeName);
|
||||
XmlAttribute xmlAttributeValue = xmlDocument.CreateAttribute("value");
|
||||
xmlAttributeValue.Value = value.ToLower();
|
||||
xmlElement.Attributes.Append(xmlAttributeValue);
|
||||
|
||||
return xmlElement;
|
||||
}
|
||||
|
||||
private static string SerializeToString(XmlDocument xmlDocument)
|
||||
{
|
||||
string result;
|
||||
using (StringWriter stringWriter = new StringWriter())
|
||||
{
|
||||
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter))
|
||||
{
|
||||
xmlDocument.WriteTo(xmlTextWriter);
|
||||
result = stringWriter.ToString();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool HasValue(string name)
|
||||
{
|
||||
return StoredOptions.ContainsKey(name);
|
||||
}
|
||||
|
||||
public static void SetValue(string name, object value)
|
||||
{
|
||||
//Log.Out("CustomGameOptions-SetValue NEW VALUE StoredOptions[" + name + "]: " + value);
|
||||
StoredOptions[name] = value.ToString();
|
||||
//Log.Out("CustomGameOptions-SetValue SET VALUE StoredOptions[" + name + "]: " + StoredOptions[name]);
|
||||
}
|
||||
|
||||
public static int GetInt(string name, int defaultValue = 0)
|
||||
{
|
||||
if (!StoredOptions.ContainsKey(name))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return int.TryParse(StoredOptions[name], out var output) ? output : defaultValue;
|
||||
}
|
||||
|
||||
public static string GetString(string name, string defaultValue = "")
|
||||
{
|
||||
if (!StoredOptions.ContainsKey(name))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//Log.Out("StoredOptions[" + name + "].ToString(): " + StoredOptions[name].ToString());
|
||||
//Log.Out("LOCALIZED StoredOptions[" + name + "].ToString(): " + Localization.Get(StoredOptions[name].ToString()));
|
||||
return StoredOptions[name].ToString();
|
||||
}
|
||||
|
||||
public static bool GetBool(string name, bool defaultValue = false)
|
||||
{
|
||||
if (!StoredOptions.ContainsKey(name))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return bool.TryParse(StoredOptions[name], out var output) ? output : defaultValue;
|
||||
}
|
||||
}
|
||||
47
Scripts/GameOptions/CustomOptionsConfig.cs
Normal file
47
Scripts/GameOptions/CustomOptionsConfig.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using IntegratedConfigs.Core.Entities;
|
||||
using IntegratedConfigs.Scripts;
|
||||
using System.Collections;
|
||||
|
||||
namespace YourNamespace
|
||||
{
|
||||
public class CustomOptionsConfig : IIntegratedConfig
|
||||
{
|
||||
public XmlLoadInfo RegistrationInfo { get; }
|
||||
|
||||
private const string XML_NAME = "CustomGameOptions";
|
||||
|
||||
// This provided here as
|
||||
public static readonly string ModName = "zzz_REBIRTH__Utils";
|
||||
public static readonly Mod Mod = ModManager.GetMod(ModName, true);
|
||||
public static readonly string ModPath = Mod.Path;
|
||||
// an example.
|
||||
|
||||
public CustomOptionsConfig()
|
||||
{
|
||||
//Log.Out("ModPath: " + ModPath);
|
||||
string SaveDataPath = GameIO.GetUserGameDataDir() + "/RebirthData/";
|
||||
Log.Out("Rebirth Data location: " + SaveDataPath);
|
||||
|
||||
CustomGameOptions.CheckConfigs();
|
||||
|
||||
//RegistrationInfo = new XmlLoadInfo(XML_NAME, $"{ModPath}/CustomGameOptions", false, true, OnLoad, null, OnLoadComplete);
|
||||
RegistrationInfo = new XmlLoadInfo(XML_NAME, $"{SaveDataPath}", false, true, OnLoad, null, OnLoadComplete);
|
||||
}
|
||||
|
||||
private IEnumerator OnLoad(XmlFile xmlFile)
|
||||
{
|
||||
Log.Out($"OnLoad called for custom XML: {XML_NAME}");
|
||||
CustomGameOptions.LoadConfigs(xmlFile);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
private static IEnumerator OnLoadComplete()
|
||||
{
|
||||
Log.Out($"OnLoadComplete called for custom XML: {XML_NAME}");
|
||||
yield break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
145
Scripts/GameOptions/ExtensionMethods.cs
Normal file
145
Scripts/GameOptions/ExtensionMethods.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public static class ExtensionMethods
|
||||
{
|
||||
public static bool GetBool(this Dictionary<string, string> dictionary, string key)
|
||||
{
|
||||
if (dictionary.ContainsKey(key))
|
||||
{
|
||||
var output = false;
|
||||
bool.TryParse(dictionary[key], out output);
|
||||
return output;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//public static void SetBool(this Dictionary<string, string> dictionary, string key, bool value)
|
||||
//{
|
||||
// if (dictionary.ContainsKey(key))
|
||||
// dictionary[key] = value.ToString();
|
||||
// else
|
||||
// dictionary.Add(key, value.ToString());
|
||||
//}
|
||||
|
||||
public static string GetString(this Dictionary<string, string> dictionary, string key)
|
||||
{
|
||||
if (dictionary.ContainsKey(key))
|
||||
{
|
||||
return dictionary[key];
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
//public static void SetString(this Dictionary<string, string> dictionary, string key, string value)
|
||||
//{
|
||||
// if (dictionary.ContainsKey(key))
|
||||
// dictionary[key] = value;
|
||||
// else
|
||||
// dictionary.Add(key, value);
|
||||
//}
|
||||
|
||||
public static int GetInt(this Dictionary<string, string> dictionary, string key)
|
||||
{
|
||||
if (dictionary.ContainsKey(key))
|
||||
{
|
||||
var output = 0;
|
||||
int.TryParse(dictionary[key], out output);
|
||||
return output;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//public static void SetInt(this Dictionary<string, string> dictionary, string key, int value)
|
||||
//{
|
||||
// if (dictionary.ContainsKey(key))
|
||||
// dictionary[key] = value.ToString();
|
||||
// else
|
||||
// dictionary.Add(key, value.ToString());
|
||||
//}
|
||||
|
||||
public static void Set(this Dictionary<string, string> dictionary, string key, object value)
|
||||
{
|
||||
if (dictionary.ContainsKey(key))
|
||||
dictionary[key] = value.ToString();
|
||||
else
|
||||
dictionary.Add(key, value.ToString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// EntityAlive extensions for attack targeting
|
||||
|
||||
public static Ray GetAttackRay(this EntityAlive entityAlive)
|
||||
{
|
||||
return new Ray(entityAlive.GetAttackOrigin(), entityAlive.GetAttackVector());
|
||||
}
|
||||
|
||||
public static Vector3 GetAttackOrigin(this EntityAlive entityAlive)
|
||||
{
|
||||
Vector3 v = entityAlive.position + new Vector3(0f, entityAlive.GetAttackHeight(), 0f);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a height closer to ground level than the eyes height
|
||||
/// </summary>
|
||||
/// <param name="entityAlive"></param>
|
||||
/// <returns></returns>
|
||||
public static float GetAttackHeight(this EntityAlive entityAlive)
|
||||
{
|
||||
/*
|
||||
// disabled 21/06/2024 - todo: find a better formula
|
||||
var rightArm = entityAlive.GetComponentsInChildren<Collider>().Where(x => x.CompareTag("E_BP_RArm")).FirstOrDefault();
|
||||
if (rightArm != null)
|
||||
{
|
||||
var worldPos = entityAlive.transform.position;
|
||||
var rArmPos = rightArm.transform.position;
|
||||
var armHeight = MathF.Abs(rArmPos.y - worldPos.y);
|
||||
//Log.Out($"entity: {entityAlive}, worldPos: {worldPos}, armPos: {rArmPos}, armHeight: {armHeight}");
|
||||
return armHeight;
|
||||
}*/
|
||||
|
||||
// load generic default values?
|
||||
|
||||
var walkType = entityAlive.GetWalkType();
|
||||
if (walkType == 21) return 0.2f; // 21 is crawler
|
||||
if (walkType == 22) return 0.5f; // 22 is spider
|
||||
|
||||
// any other case?
|
||||
|
||||
return 0.7f; // because tall zombies can't hit snakes...
|
||||
}
|
||||
|
||||
public static Vector3 GetAttackVector(this EntityAlive entityAlive)
|
||||
{
|
||||
var myTarget = entityAlive.GetAttackTarget();
|
||||
|
||||
if (myTarget == null) return entityAlive.GetDefaultLookVector();
|
||||
|
||||
var col = myTarget.GetComponentsInChildren<Collider>().Where(x => x.CompareTag(RebirthUtilities.GetRandomBodyTag())).FirstOrDefault();
|
||||
|
||||
if (col == null) return entityAlive.GetDefaultLookVector();
|
||||
|
||||
Vector3 v = (col.transform.position - (entityAlive.transform.position + new Vector3(0f, entityAlive.GetAttackHeight(), 0f)));
|
||||
//Log.Out($"Attack vector: {v}");
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the base class(EntityAlive) default look vector
|
||||
/// </summary>
|
||||
/// <param name="entityAlive"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 GetDefaultLookVector(this EntityAlive entityAlive)
|
||||
{
|
||||
float num = Mathf.Cos(entityAlive.rotation.y * 0.0175f - MathF.PI);
|
||||
float num2 = Mathf.Sin(entityAlive.rotation.y * 0.0175f - MathF.PI);
|
||||
float num3 = 0f - Mathf.Cos(entityAlive.rotation.x * 0.0175f);
|
||||
float y = Mathf.Sin(entityAlive.rotation.x * 0.0175f);
|
||||
return new Vector3(num2 * num3, y, num * num3);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user